gdrm-kms-addfb: restore original FB on exit
authorThomas White <taw@bitwiz.org.uk>
Mon, 3 Aug 2009 18:11:29 +0000 (19:11 +0100)
committerThomas White <taw@bitwiz.org.uk>
Mon, 3 Aug 2009 18:11:29 +0000 (19:11 +0100)
gdrm-kms-addfb.c

index 6f9acc2..4395230 100644 (file)
 #define OBJECT_SIZE (640*480*2)
 
 
-static int addfb(int fd, int handle)
-{
-       int ret = 0;
-       drmModeResPtr r;
-       drmModeCrtcPtr crtc;
-       int crtc_id;
-       int conn_id;
-       int fb_id;
-
-       r = drmModeGetResources(fd);
-       printf("There are %i CRTCs, %i connectors, %i encoders"
-              " and %i framebuffers\n",
-              r->count_crtcs, r->count_connectors, r->count_encoders,
-              r->count_fbs);
-
-       crtc_id = r->crtcs[0];
-       conn_id = r->connectors[0];
-       printf("The first CRTC ID is %i\n", crtc_id);
-       printf("The first connector ID is %i\n", conn_id);
-
-       drmModeAddFB(fd, 480, 640, 16, 16, 480*2, handle, &fb_id);
-       printf("My new FB handle is %i\n", fb_id);
-       if (!fb_id) {
-               printf("Could not add FB\n");
-               return 1;
-       }
-
-       printf("Getting CRTC info\n");
-       crtc = drmModeGetCrtc(fd, crtc_id);
-
-       printf("crtc=%p\n", crtc);
-       ret = drmModeSetCrtc(fd, crtc_id, fb_id, 0, 0, r->connectors, 1,
-                            &(crtc->mode));
-       if ( ret ) {
-               printf("drmModeSetCrtc returned %i\n", ret);
-       }
-
-       return fb_id;
-}
-
-
 int main(int argc, char **argv)
 {
        int fd;
@@ -123,7 +82,12 @@ int main(int argc, char **argv)
        int handle;
        char tmp[4];
        int i;
+       drmModeResPtr r;
+       drmModeCrtcPtr crtc;
+       int crtc_id;
+       int conn_id;
        int fb_id;
+       uint32_t old_buffer_id;
 
        fd = drm_open_any();
 
@@ -146,22 +110,48 @@ int main(int argc, char **argv)
        addr16 = (uint16_t *)addr;
        printf("Virtual address is 0x%p\n", addr);
 
-       printf("Adding FB\n");
-       fb_id = addfb(fd, handle);
+       r = drmModeGetResources(fd);
+       printf("There are %i CRTCs, %i connectors, %i encoders"
+              " and %i framebuffers\n",
+              r->count_crtcs, r->count_connectors, r->count_encoders,
+              r->count_fbs);
+
+       crtc_id = r->crtcs[0];
+       conn_id = r->connectors[0];
+       printf("The first CRTC ID is %i\n", crtc_id);
+       printf("The first connector ID is %i\n", conn_id);
+
+       drmModeAddFB(fd, 480, 640, 16, 16, 480*2, handle, &fb_id);
+       printf("My new FB handle is %i\n", fb_id);
+       if (!fb_id) {
+               printf("Could not add FB\n");
+               return 1;
+       }
+
+       printf("Getting CRTC info\n");
+       crtc = drmModeGetCrtc(fd, crtc_id);
+       old_buffer_id = crtc->buffer_id;
+       printf("Old FB handle is %i\n", old_buffer_id);
+
+       printf("crtc=%p\n", crtc);
+       if ( drmModeSetCrtc(fd, crtc_id, fb_id, 0, 0, r->connectors, 1,
+                           &(crtc->mode)) ) {
+               printf("drmModeSetCrtc returned %i\n", ret);
+       }
 
        do {
 
                uint16_t val;
 
-               printf("Press <enter> to continue, q<enter> to finish...");
-               fgets(tmp, 3, stdin);
-
                val = random() & 0xff;
                val += (random() & 0xff)<<8;
                for ( i=0; i<OBJECT_SIZE/2; i++ ) {
                        addr16[i] = val;
                }
 
+               printf("Press <enter> to continue, q<enter> to finish...");
+               fgets(tmp, 3, stdin);
+
        } while ( tmp[0] != 'q' );
 
        printf("Unmapping\n");
@@ -170,6 +160,12 @@ int main(int argc, char **argv)
        printf("Removing FB\n");
        drmModeRmFB(fd, fb_id);
 
+       printf("Restoring original FB\n");
+       if ( drmModeSetCrtc(fd, crtc_id, old_buffer_id, 0, 0, r->connectors, 1,
+                            &(crtc->mode)) ) {
+               printf("drmModeSetCrtc returned %i\n", ret);
+       }
+
        close(fd);
 
        return 0;