Advertisement
Guest User

write to /dev/fb

a guest
Apr 29th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <linux/fb.h>
  6. #include <sys/mman.h>
  7. #include <sys/ioctl.h>
  8. #include "graphic.h"
  9. #include "common.h"
  10.  
  11.  
  12. static int fbfd = 0;
  13. static struct fb_var_screeninfo vinfo;
  14. static struct fb_fix_screeninfo finfo;
  15. static long int screensize = 0;
  16. static char *fbp = 0;
  17.      
  18.  
  19.  
  20. static void openfb()
  21. {
  22.      fbfd = open("/dev/fb0", O_RDWR);
  23.      if (fbfd == -1) {
  24.          perror("Error: cannot open framebuffer device");
  25.          exit(1);
  26.      }
  27.      printf("The framebuffer device was opened successfully.\n");
  28.  
  29.      if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
  30.          perror("Error reading fixed information");
  31.          exit(2);
  32.      }
  33.        
  34.      if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
  35.          perror("Error reading variable information");
  36.          exit(3);
  37.      }
  38.      
  39.      screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  40.      
  41.      fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  42. }
  43.  
  44.  
  45. static void closefb(){
  46.      munmap(fbp, screensize);
  47.      close(fbfd);
  48. }
  49.  
  50.  
  51. static void clearScreen(){
  52.     for(int i = 0; i<screensize; i++)
  53.     {
  54.         if (vinfo.bits_per_pixel == 32) {
  55.                *(fbp + i) = 0;    
  56.         }
  57.     }
  58. }
  59.  
  60.  
  61. static void line(int x1, int y1, int x2, int y2) {
  62.     int x_start ,x_end, _y1,_y2, temp ;
  63.     long int location;
  64.    
  65.     _y1 = min(y1, y2);
  66.     _y2 = max(y1, y2);
  67.     if (_y1 == _y2) _y2++;
  68.    
  69.     for(int y = _y1; y <= _y2; y++)
  70.     {
  71.         x_start =  (y-_y1) * (x2-x1) / (_y2-_y1) + x1;
  72.         x_end = (y+1-_y1) * (x2-x1) / (_y2-_y1) + x1;
  73.  
  74.         temp = x_start;
  75.         x_start = min(x_start,x_end);
  76.         x_end = max(temp, x_end);
  77.        
  78.         for (int x = x_start; x <= x_end; x++)
  79.         {
  80.             location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
  81.                        (y+vinfo.yoffset) * finfo.line_length;
  82.                            
  83.             if (vinfo.bits_per_pixel == 32) {
  84.                     //RGBA
  85.                      *(fbp + location) = y;          // B
  86.                      *(fbp + location + 1) = x;      // G
  87.                      *(fbp + location + 2) = 200;    // R
  88.                      *(fbp + location + 3) = 0;      // A
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94.  
  95. int test(const char * file_name) {
  96.  
  97.  
  98.      openfb();
  99.      clearScreen();
  100.  
  101.      line(5,5,80,100);
  102.      line(80,5,5,100);
  103.      line(100,5,140,50);
  104.      line(180,5,100,100);
  105.      line(200,5,200,100);
  106.      line(280,5,200,100);
  107.      line(280,5,280,100);
  108.      line(220,5,250,10);
  109.      closefb();
  110.  
  111.     return 1;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement