[/font]
[font="Courier New"]#include[/font][font="Courier New"] <stdio.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <stdlib.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <sys/mman.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <sys/ioctl.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <cairo/cairo.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <linux/types.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <linux/limits.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <linux/fb.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <unistd.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <fcntl.h>[/font]
[font="Courier New"]#include[/font][font="Courier New"] <math.h>[/font]
[font="Courier New"]/* Locals */[/font]
[font="Courier New"]static[/font][font="Courier New"] cairo_t *fb_context;[/font]
[font="Courier New"]static[/font][font="Courier New"] cairo_surface_t *fb_surface;[/font]
[font="Courier New"]static void[/font][font="Courier New"] *fb_data;[/font]
[font="Courier New"]static int[/font][font="Courier New"] fb_handle;[/font]
[font="Courier New"]static int[/font][font="Courier New"] fb_height;[/font]
[font="Courier New"]static int[/font][font="Courier New"] fb_stride;[/font]
[font="Courier New"]static int[/font][font="Courier New"] fb_width;[/font]
[font="Courier New"]static int[/font][font="Courier New"] fb_size;[/font]
[font="Courier New"]static int[/font][font="Courier New"] fb_bpp;[/font]
[font="Courier New"]/* Open the Fraembuffer Device */[/font]
[font="Courier New"]static int[/font][font="Courier New"] OpenFramebuffer(void)[/font]
[font="Courier New"]{[/font]
[font="Courier New"] int handle = -1;[/font]
[font="Courier New"] if ((handle = open("/System/Devices/fb0", O_RDWR)) < 0)[/font]
[font="Courier New"] {[/font]
[font="Courier New"] printf("Open Framebuffer failed!\n");[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] struct fb_fix_screeninfo fix;[/font]
[font="Courier New"] if (ioctl(handle, FBIOGET_FSCREENINFO, &fix) < 0)[/font]
[font="Courier New"] {[/font]
[font="Courier New"] printf("FBIOGET_FSCREENINFO failed!\n");[/font]
[font="Courier New"] close(handle);[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] struct fb_var_screeninfo var;[/font]
[font="Courier New"] if (ioctl(handle, FBIOGET_VSCREENINFO, &var) < 0)[/font]
[font="Courier New"] {[/font]
[font="Courier New"] printf("FBIOGET_VSCREENINFO failed!\n");[/font]
[font="Courier New"] close(handle);[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] void *data = NULL;[/font]
[font="Courier New"] size_t length = (var.yres * fix.line_length);[/font]
[font="Courier New"] if ((data = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, handle, 0)) == MAP_FAILED)[/font]
[font="Courier New"] {[/font]
[font="Courier New"] printf("Map Memory failed!\n");[/font]
[font="Courier New"] close(handle);[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] fb_handle = handle;[/font]
[font="Courier New"] fb_width = (int)var.xres;[/font]
[font="Courier New"] fb_height = (int)var.yres;[/font]
[font="Courier New"] fb_stride = (int)fix.line_length;[/font]
[font="Courier New"] fb_size = (int)(var.yres * fix.line_length);[/font]
[font="Courier New"] fb_bpp = (int)var.bits_per_pixel;[/font]
[font="Courier New"] fb_data = data;[/font]
[font="Courier New"] printf("Width: %d\n", fb_width);[/font]
[font="Courier New"] printf("Height: %d\n", fb_height);[/font]
[font="Courier New"] printf("Bpp: %d\n", fb_bpp);[/font]
[font="Courier New"] return 1;[/font]
[font="Courier New"]}[/font]
[font="Courier New"]/* Get the Cairo Format from Framebuffer Device. */[/font]
[font="Courier New"]static[/font][font="Courier New"] cairo_format_t GetFramebufferFormat(int bpp)[/font]
[font="Courier New"]{[/font]
[font="Courier New"] switch (bpp)[/font]
[font="Courier New"] {[/font]
[font="Courier New"] case 16:[/font]
[font="Courier New"] return CAIRO_FORMAT_RGB16_565;[/font]
[font="Courier New"] case 24:[/font]
[font="Courier New"] return CAIRO_FORMAT_RGB24;[/font]
[font="Courier New"] case 32:[/font]
[font="Courier New"] return CAIRO_FORMAT_ARGB32;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] return CAIRO_FORMAT_INVALID;[/font]
[font="Courier New"]}[/font]
[font="Courier New"]/* Creates the Framebuffer Surface. */[/font]
[font="Courier New"]static int[/font][font="Courier New"] CreateFramebufferSurface(void)[/font]
[font="Courier New"]{[/font]
[font="Courier New"] cairo_format_t format;[/font]
[font="Courier New"] if ((fb_data == NULL) (fb_width <= 0) (fb_height <= 0))[/font]
[font="Courier New"] {[/font]
[font="Courier New"] printf("Invalid Framebuffer!\n");[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] format = GetFramebufferFormat(fb_bpp);[/font]
[font="Courier New"] if (format == CAIRO_FORMAT_INVALID)[/font]
[font="Courier New"] {[/font]
[font="Courier New"] printf("Invalid Framebuffer format!\n");[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] fb_surface = cairo_image_surface_create_for_data((unsigned char*)fb_data, format, fb_width, fb_height, fb_stride);[/font]
[font="Courier New"] if ((fb_surface == NULL))[/font]
[font="Courier New"] {[/font]
[font="Courier New"] printf("Create Surface failed!\n");[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] return 1;[/font]
[font="Courier New"]}[/font]
[font="Courier New"]static int[/font][font="Courier New"] DestroySurface(void)[/font]
[font="Courier New"]{[/font]
[font="Courier New"] return 1;[/font]
[font="Courier New"]}[/font]
[font="Courier New"]static int[/font][font="Courier New"] DrawText(void)[/font]
[font="Courier New"]{[/font]
[font="Courier New"] cairo_save(fb_context);[/font]
[font="Courier New"] cairo_set_source_rgb(fb_context, 0.0, 1.0, 1.0); [/font]
[font="Courier New"] cairo_set_font_size(fb_context, 30);[/font]
[font="Courier New"] cairo_move_to(fb_context, 20, 30);[/font]
[font="Courier New"] cairo_show_text(fb_context, "Most relationships seem so transitory");[/font]
[font="Courier New"] cairo_move_to(fb_context, 20, 60);[/font]
[font="Courier New"] cairo_show_text(fb_context, "They're all good but not the permanent one");[/font]
[font="Courier New"] cairo_move_to(fb_context, 20, 120);[/font]
[font="Courier New"] cairo_show_text(fb_context, "Who doesn't long for someone to hold");[/font]
[font="Courier New"] cairo_move_to(fb_context, 20, 150);[/font]
[font="Courier New"] cairo_show_text(fb_context, "Who knows how to love you without being told");[/font]
[font="Courier New"] cairo_move_to(fb_context, 20, 180);[/font]
[font="Courier New"] cairo_show_text(fb_context, "Somebody tell me why I'm on my own");[/font]
[font="Courier New"] cairo_move_to(fb_context, 20, 210);[/font]
[font="Courier New"] cairo_show_text(fb_context, "If there's a soulmate for everyone");[/font]
[font="Courier New"] cairo_restore(fb_context);[/font]
[font="Courier New"] return 1;[/font]
[font="Courier New"]}[/font]
[font="Courier New"]/* Close the Framebuffer Device */[/font]
[font="Courier New"]static int[/font][font="Courier New"] CloseFramebuffer(void)[/font]
[font="Courier New"]{[/font]
[font="Courier New"] if ((fb_data != NULL))[/font]
[font="Courier New"] {[/font]
[font="Courier New"] munmap(fb_data, fb_size);[/font]
[font="Courier New"] fb_data = NULL;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] if ((fb_handle >= 0))[/font]
[font="Courier New"] {[/font]
[font="Courier New"] close(fb_handle);[/font]
[font="Courier New"] fb_handle = -1;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"]}[/font]
[font="Courier New"]/* Entry Point */[/font]
[font="Courier New"]int[/font][font="Courier New"] main(int argc, char *argv[])[/font]
[font="Courier New"]{[/font]
[font="Courier New"] int count = 0;[/font]
[font="Courier New"] fb_context = NULL;[/font]
[font="Courier New"] fb_surface = NULL;[/font]
[font="Courier New"] /* Open the Framebuffer */[/font]
[font="Courier New"] if ((OpenFramebuffer() <= 0))[/font]
[font="Courier New"] {[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] if ((CreateFramebufferSurface() <= 0))[/font]
[font="Courier New"] {[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] fb_context = cairo_create(fb_surface);[/font]
[font="Courier New"] if ((fb_context == NULL))[/font]
[font="Courier New"] {[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] cairo_set_source_rgb(fb_context, 0, 0, 0);[/font]
[font="Courier New"] cairo_set_line_width(fb_context, 1);[/font]
[font="Courier New"] cairo_rectangle(fb_context, 20, 20, 120, 80);[/font]
[font="Courier New"] cairo_rectangle(fb_context, 180, 20, 80, 80);[/font]
[font="Courier New"] cairo_stroke_preserve(fb_context);[/font]
[font="Courier New"] cairo_set_source_rgb(fb_context, 1, 1, 1);[/font]
[font="Courier New"] cairo_fill(fb_context);[/font]
[font="Courier New"] cairo_set_source_rgb(fb_context, 0, 0, 0);[/font]
[font="Courier New"] cairo_arc(fb_context, 330, 60, 40, 0, 2*M_PI);[/font]
[font="Courier New"] cairo_stroke_preserve(fb_context);[/font]
[font="Courier New"] cairo_set_source_rgb(fb_context, 1, 1, 1);[/font]
[font="Courier New"] cairo_fill(fb_context);[/font]
[font="Courier New"] cairo_set_source_rgb(fb_context, 0, 0, 0);[/font]
[font="Courier New"] cairo_arc(fb_context, 90, 160, 40, M_PI/4, M_PI);[/font]
[font="Courier New"] cairo_close_path(fb_context);[/font]
[font="Courier New"] cairo_stroke_preserve(fb_context);[/font]
[font="Courier New"] cairo_set_source_rgb(fb_context, 1, 1, 1);[/font]
[font="Courier New"] cairo_fill(fb_context);[/font]
[font="Courier New"] cairo_set_source_rgb(fb_context, 0, 0, 0);[/font]
[font="Courier New"] cairo_translate(fb_context, 220, 180);[/font]
[font="Courier New"] cairo_scale(fb_context, 1, 0.7);[/font]
[font="Courier New"] cairo_arc(fb_context, 0, 0, 50, 0, 2*M_PI);[/font]
[font="Courier New"] cairo_stroke_preserve(fb_context);[/font]
[font="Courier New"] cairo_set_source_rgb(fb_context, 1, 1, 1);[/font]
[font="Courier New"] cairo_fill(fb_context);[/font]
[font="Courier New"] while ((count <= 10))[/font]
[font="Courier New"] {[/font]
[font="Courier New"] DrawText();[/font]
[font="Courier New"] sleep(1);[/font]
[font="Courier New"] count++;[/font]
[font="Courier New"] }[/font]
[font="Courier New"] cairo_destroy(fb_context);[/font]
[font="Courier New"] DestroySurface();[/font]
[font="Courier New"] CloseFramebuffer();
[/font]
[font="Courier New"] return 0;[/font]
[font="Courier New"]}