Home | History | Annotate | Download | only in src
      1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <fcntl.h>
      6 #include <linux/fb.h>
      7 #include <stdint.h>
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <sys/mman.h>
     11 #include <sys/ioctl.h>
     12 #include <unistd.h>
     13 
     14 int main(int argc, char *argv[])
     15 {
     16   const char* device_name = "/dev/fb0";
     17   struct fb_var_screeninfo info;
     18   uint32_t screen_size = 0;
     19   void *ptr = NULL;
     20   int fd = -1;
     21   FILE *file = NULL;
     22   int ret = -1;
     23 
     24   if (argc < 2) {
     25     printf("Usage: getfb [filename]\n");
     26     printf("Writes the active framebuffer to output file [filename].\n");
     27     return 0;
     28   }
     29 
     30   // Open the file for reading and writing
     31   fd = open(device_name, O_RDONLY);
     32   if (fd == -1) {
     33     fprintf(stderr, "Cannot open framebuffer device %s\n", device_name);
     34     goto exit;
     35   }
     36   printf("The framebuffer device was opened successfully.\n");
     37 
     38   // Get fixed screen information
     39   if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) {
     40     fprintf(stderr, "Error reading variable screen information.\n");
     41     goto exit;
     42   }
     43 
     44   printf("Framebuffer info: %dx%d, %dbpp\n", info.xres, info.yres,
     45          info.bits_per_pixel);
     46 
     47   // Figure out the size of the screen in bytes
     48   screen_size = info.xres * info.yres * info.bits_per_pixel / 8;
     49 
     50   // Map the device to memory
     51   ptr = mmap(0, screen_size, PROT_READ, MAP_SHARED, fd, 0);
     52   if (ptr == MAP_FAILED) {
     53     fprintf(stderr, "Error: failed to map framebuffer device to memory.\n");
     54     goto exit;
     55   }
     56   printf("The framebuffer device was mapped to memory successfully.\n");
     57 
     58   // Write it to output file.
     59   file = fopen(argv[1], "w");
     60   if (!file) {
     61     fprintf(stderr, "Could not open file %s for writing.\n", argv[1]);
     62     goto exit;
     63   }
     64 
     65   if (fwrite(ptr, screen_size, 1, file) < 1) {
     66     fprintf(stderr, "Error while writing framebuffer to file.\n");
     67     goto exit;
     68   }
     69 
     70   ret = 0;
     71 
     72 exit:
     73   if (file)
     74     fclose(file);
     75 
     76   if (ptr != MAP_FAILED && ptr)
     77     munmap(ptr, screen_size);
     78 
     79   if (fd >= 0)
     80     close(fd);
     81 
     82   return ret;
     83 }
     84