amarullz

fb_example.c

Aug 10th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. /* EXAMPLE BY amarullz.com */
  2.  
  3. /* Includes Needed Headers for Framebuffer */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. #include <fcntl.h>
  9. #include <linux/fb.h>
  10. #include <sys/mman.h>
  11.  
  12. /* Android Framebuffer Device */
  13. #define ANDROID_FRAMEBUFFER "/dev/graphics/fb0"
  14.  
  15. /* Defining Data-Types for shorting usage */
  16. typedef unsigned char   byte;
  17. typedef unsigned int    dword;    /* RGBX using 4bytes per pixel */
  18. typedef byte *          bytep;
  19. typedef dword *         dwordp;
  20.  
  21. /* Variable */
  22. static int fb = 0;                      // FrameBuffer Handler
  23. static struct fb_fix_screeninfo fbfix;  // Fixed Info
  24. static struct fb_var_screeninfo fbvar;  // Vars Info
  25. static dwordp buffer;                   // Framebuffer Data
  26.  
  27. int init_framebuffer(){
  28.   fb = open(ANDROID_FRAMEBUFFER, O_RDWR, 0);
  29.  
  30.   if (fb>0) {
  31.     // Retrive Framebuffer Fix&Var Informations
  32.     ioctl(fb, FBIOGET_FSCREENINFO, &fbfix);
  33.     ioctl(fb, FBIOGET_VSCREENINFO, &fbvar);
  34.    
  35.     // Retrive Framebuffer Memory Location
  36.     buffer = (dwordp) mmap(0, fbfix.smem_len, PROT_READ|PROT_WRITE, MAP_SHARED, fb, 0);
  37.    
  38.     return 1;
  39.   }
  40.  
  41.   return 0;
  42. }
  43.  
  44. //-- RELEASE AMARULLZ GRAPHIC
  45. void close_framebuffer() {
  46.   if (fb > 0) {
  47.     munmap(buffer, fbfix.smem_len);
  48.     close(fb);
  49.   }
  50. }
  51.  
  52. void syncfb(){
  53.   fsync(fb);
  54.   fbvar.yoffset   = 0;
  55.   fbvar.activate |= FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
  56.   ioctl(fb, FBIOPUT_VSCREENINFO, &fbvar);
  57. }
  58.  
  59. int posxy(int x, int y){
  60.   /*
  61.     fbfix.line_length = length by bytes not by pixels.
  62.     Because 1 pixel was 4 bytes, we need to div it with 4
  63.    
  64.       return (y * (fbfix.line_length/4)) + x;
  65.    
  66.     But for faster calculation, bitwise is better.
  67.   */
  68.   return (y * (fbfix.line_length >> 2)) + x;
  69. }
  70. void setpixel(int x, int y, dword color) {
  71.   buffer[posxy(x,y)]=color;
  72. }
  73. dword getpixel(int x, int y) {
  74.   return buffer[posxy(x,y)];
  75. }
  76.  
  77. /* Build 32bit Color From RGBA Channels */
  78. dword RGBA(byte r, byte g, byte b, byte a) {
  79.   return (dword)
  80.          (
  81.            ((r & 0xff) << 16) |
  82.            ((g & 0xff) << 8) |
  83.            (b & 0xff) |
  84.            ((a & 0xff) << 24)
  85.          );
  86. }
  87. dword RGB(byte r, byte g, byte b) {
  88.   return RGBA(r, g, b, 0xff);
  89. }
  90. byte GetR(dword color) {
  91.   return (byte) ((color >> 16) & 0xff);
  92. }
  93. byte GetG(dword color) {
  94.   return (byte) ((color >> 8) & 0xff);
  95. }
  96. byte GetB(dword color) {
  97.   return (byte) (color & 0xff);
  98. }
  99. byte GetA(dword color) {
  100.   return (byte) ((color >> 24) & 0xff);
  101. }
  102.  
  103. int main(int argc, char *argv[])
  104. {
  105.   if (init_framebuffer()==0){
  106.     /* Error */
  107.     return -1;
  108.   }
  109.  
  110.   // Pause Parent Proccess First, so framebuffer
  111.   // will not interrupted by recovery
  112.   int parent_pid = getppid();
  113.   kill(parent_pid, 19);
  114.  
  115.   // Draw Rectangle
  116.   int x, y;
  117.   for (y=0; y<fbvar.yres; y++){
  118.     for (x=0; x<fbvar.xres; x++){
  119.       // Blue = 44, 100, 200
  120.       setpixel(x, y, RGB(44, 100, 200));
  121.     }
  122.   }
  123.  
  124.   // Show It On Display
  125.   syncfb();
  126.  
  127.   sleep(3);
  128.  
  129.   // Draw Other Rectangle
  130.   for (y=0; y<fbvar.yres; y++){
  131.     for (x=0; x<fbvar.xres; x++){
  132.       // Red
  133.       setpixel(x, y, RGB(180, 50, 50));
  134.     }
  135.   }
  136.  
  137.   // Show It On Display
  138.   syncfb();
  139.  
  140.   sleep(3);
  141.  
  142.   // Resume Parent Pid
  143.   kill(parent_pid, 18);
  144.  
  145.   close_framebuffer();
  146.   return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment