Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* EXAMPLE BY amarullz.com */
- /* Includes Needed Headers for Framebuffer */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <linux/fb.h>
- #include <sys/mman.h>
- /* Android Framebuffer Device */
- #define ANDROID_FRAMEBUFFER "/dev/graphics/fb0"
- /* Defining Data-Types for shorting usage */
- typedef unsigned char byte;
- typedef unsigned int dword; /* RGBX using 4bytes per pixel */
- typedef byte * bytep;
- typedef dword * dwordp;
- /* Variable */
- static int fb = 0; // FrameBuffer Handler
- static struct fb_fix_screeninfo fbfix; // Fixed Info
- static struct fb_var_screeninfo fbvar; // Vars Info
- static dwordp buffer; // Framebuffer Data
- int init_framebuffer(){
- fb = open(ANDROID_FRAMEBUFFER, O_RDWR, 0);
- if (fb>0) {
- // Retrive Framebuffer Fix&Var Informations
- ioctl(fb, FBIOGET_FSCREENINFO, &fbfix);
- ioctl(fb, FBIOGET_VSCREENINFO, &fbvar);
- // Retrive Framebuffer Memory Location
- buffer = (dwordp) mmap(0, fbfix.smem_len, PROT_READ|PROT_WRITE, MAP_SHARED, fb, 0);
- return 1;
- }
- return 0;
- }
- //-- RELEASE AMARULLZ GRAPHIC
- void close_framebuffer() {
- if (fb > 0) {
- munmap(buffer, fbfix.smem_len);
- close(fb);
- }
- }
- void syncfb(){
- fsync(fb);
- fbvar.yoffset = 0;
- fbvar.activate |= FB_ACTIVATE_NOW | FB_ACTIVATE_FORCE;
- ioctl(fb, FBIOPUT_VSCREENINFO, &fbvar);
- }
- int posxy(int x, int y){
- /*
- fbfix.line_length = length by bytes not by pixels.
- Because 1 pixel was 4 bytes, we need to div it with 4
- return (y * (fbfix.line_length/4)) + x;
- But for faster calculation, bitwise is better.
- */
- return (y * (fbfix.line_length >> 2)) + x;
- }
- void setpixel(int x, int y, dword color) {
- buffer[posxy(x,y)]=color;
- }
- dword getpixel(int x, int y) {
- return buffer[posxy(x,y)];
- }
- /* Build 32bit Color From RGBA Channels */
- dword RGBA(byte r, byte g, byte b, byte a) {
- return (dword)
- (
- ((r & 0xff) << 16) |
- ((g & 0xff) << 8) |
- (b & 0xff) |
- ((a & 0xff) << 24)
- );
- }
- dword RGB(byte r, byte g, byte b) {
- return RGBA(r, g, b, 0xff);
- }
- byte GetR(dword color) {
- return (byte) ((color >> 16) & 0xff);
- }
- byte GetG(dword color) {
- return (byte) ((color >> 8) & 0xff);
- }
- byte GetB(dword color) {
- return (byte) (color & 0xff);
- }
- byte GetA(dword color) {
- return (byte) ((color >> 24) & 0xff);
- }
- int main(int argc, char *argv[])
- {
- if (init_framebuffer()==0){
- /* Error */
- return -1;
- }
- // Pause Parent Proccess First, so framebuffer
- // will not interrupted by recovery
- int parent_pid = getppid();
- kill(parent_pid, 19);
- // Draw Rectangle
- int x, y;
- for (y=0; y<fbvar.yres; y++){
- for (x=0; x<fbvar.xres; x++){
- // Blue = 44, 100, 200
- setpixel(x, y, RGB(44, 100, 200));
- }
- }
- // Show It On Display
- syncfb();
- sleep(3);
- // Draw Other Rectangle
- for (y=0; y<fbvar.yres; y++){
- for (x=0; x<fbvar.xres; x++){
- // Red
- setpixel(x, y, RGB(180, 50, 50));
- }
- }
- // Show It On Display
- syncfb();
- sleep(3);
- // Resume Parent Pid
- kill(parent_pid, 18);
- close_framebuffer();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment