Guest User

Untitled

a guest
May 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <linux/fb.h>
  5. #include <sys/mman.h>
  6.  
  7. int main()
  8. {
  9. int fbfd = 0;
  10. struct fb_var_screeninfo vinfo;
  11. struct fb_fix_screeninfo finfo;
  12. long int screensize = 0;
  13. char *fbp = 0;
  14. int x = 0, y = 0;
  15. long int location = 0;
  16.  
  17. // Open the file for reading and writing
  18. fbfd = open("/dev/fb0", O_RDWR);
  19. if (!fbfd) {
  20. printf("Error: cannot open framebuffer device.\n");
  21. exit(1);
  22. }
  23. printf("The framebuffer device was opened successfully.\n");
  24.  
  25. // Get fixed screen information
  26. if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
  27. printf("Error reading fixed information.\n");
  28. exit(2);
  29. }
  30.  
  31. // Get variable screen information
  32. if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
  33. printf("Error reading variable information.\n");
  34. exit(3);
  35. }
  36.  
  37. printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
  38.  
  39. // Figure out the size of the screen in bytes
  40. screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  41.  
  42. // Map the device to memory
  43. fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
  44. fbfd, 0);
  45. if ((int)fbp == -1) {
  46. printf("Error: failed to map framebuffer device to memory.\n");
  47. exit(4);
  48. }
  49. printf("The framebuffer device was mapped to memory successfully.\n");
  50.  
  51. x = 100; y = 100; // Where we are going to put the pixel
  52.  
  53. // Figure out where in memory to put the pixel
  54. for (y = 100; y < 300; y++)
  55. for (x = 100; x < 300; x++) {
  56.  
  57. location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
  58. (y+vinfo.yoffset) * finfo.line_length;
  59.  
  60. if (vinfo.bits_per_pixel == 32) {
  61. *(fbp + location) = 100; // Some blue
  62. *(fbp + location + 1) = 15+(x-100)/2; // A little green
  63. *(fbp + location + 2) = 200-(y-100)/5; // A lot of red
  64. *(fbp + location + 3) = 0; // No transparency
  65. } else { //assume 16bpp
  66. int b = 10;
  67. int g = (x-100)/6; // A little green
  68. int r = 31-(y-100)/16; // A lot of red
  69. unsigned short int t = r<<11 | g << 5 | b;
  70. *((unsigned short int*)(fbp + location)) = t;
  71. }
  72.  
  73. }
  74. munmap(fbp, screensize);
  75. close(fbfd);
  76. return 0;
  77. }
Add Comment
Please, Sign In to add comment