title: How to get pixel colour from framebuffer on linux (Raspberry Pi)
body: <p>I am trying to make a small program to control the colour of an RGB LED according to the colour of certain pixels on the screen. 
  Since this is on Raspberry Pi running Raspbmc, I cant use XLib because everything is drawn from the frame buffer(not sure if this is true, but from what I read on the FAQ this appears to be the case). I tried using XLib but couldn't get the display to be detected (makes sense why it doesn't work now).</p>
  
  <p>This is an example I found online. The problem is, it compiles fine, but when run, the second error message appears: "Error reading fixed information".
  I tried displaying the content of fbfd but that results in a Segmentation Error. So it appears nothing is written to fbfd? </p>
  
  <p>I cant make sense of it because everything I find about the frame buffer is basicly this same code, so I don't know why it doesn't work.</p>
  
  <p>Hope someone can help!</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;        /* for fcntl */
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/mman.h&gt;        /* for mmap */
  #include &lt;sys/ioctl.h&gt;
  #include &lt;linux/fb.h&gt;
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main() {
      long int screensize = 0;    
      int fbfd =0;                    /* frame buffer file descriptor */
      struct fb_var_screeninfo vinfo;
      struct fb_fix_screeninfo finfo;
      char* fbp;                    /* pointer to framebuffer */
      int location;                    /* iterate to location */
  
      int x, y;                    /* x and y location */
  
      /* open the file for reading and writing */
      fbfd = open("/dev/fb0", O_RDWR);
      //printf("%s\n",fbfd);
      if (!fbfd) {
          printf("Error: cannot open framebuffer device.\n");
          exit(1);
      }
      printf ("The framebuffer device was opened successfully.\n");
  
      /* get the fixed screen information */
      if (ioctl (fbfd, FBIOGET_FSCREENINFO, &amp;finfo)) {
          printf("Error reading fixed information.\n");
          exit(2);
      }
  
      /* get variable screen information */
      if (ioctl (fbfd, FBIOGET_VSCREENINFO, &amp;vinfo)) {
          printf("Error reading variable information.\n");
          exit(3);
      }
  
      /* figure out the size of the screen in bytes */
      //screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  
      /* map the device to memory */
      fbp = (char*)mmap(0, finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  
      if ((int)fbp == -1) {
          printf ("Error: failed to map framebuffer device to memory.\n");
          exit(4);
      }
      printf ("Framebuffer device was mapped to memory successfully.\n");
  
      // Figure out where in memory to put the pixel
      for ( y = 0; y &lt; (vinfo.yres/2); y++ )
          for ( x = 0; x &lt; vinfo.xres; x++ ) { 
              location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + (y+vinfo.yoffset) * finfo.line_length;
              if ( vinfo.bits_per_pixel == 32 ) { 
                  *(fbp + location) = 100; // Some blue 
                  *(fbp + location + 1) = 15+(x-100)/2; // A little green 
                  *(fbp + location + 2) = 200-(y-100)/5; // A lot of red
                  *(fbp + location + 3) = 0; // No transparency 
              } else { //assume 16bpp 
                  int b = 10; int g = (x-100)/6; // A little green 
                  int r = 31-(y-100)/16; // A lot of red 
                  unsigned short int t = r&lt;&lt;11 | g &lt;&lt; 5 | b; 
                  *((unsigned short int*)(fbp + location)) = t; 
              }
          }
      munmap(fbp, screensize);
      close(fbfd);
  
      return 0;
  }
  </code></pre>