19 #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
21 static unsigned char *framebuffer;
35 if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
41 if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
42 fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
46 fb_var_screeninfo sinfo;
47 if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
50 fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
54 printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
55 printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
57 gfx->screen_rect.x = gfx->screen_rect.y = 0;
58 gfx->screen_rect.width = sinfo.xres_virtual;
59 gfx->screen_rect.height = sinfo.yres_virtual;
60 gfx->color_depth = sinfo.bits_per_pixel;
62 set_clipping_rect(gfx->screen_rect);
64 int sz = FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth);
65 framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
67 if(framebuffer == (void*)-1) {
70 fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
74 // TODO: uncomment when I find how to use intelfb instead of i915 GRRRR.-
76 if(ioctl(dev_fd, FBIOGET_VBLANK, &vblank) == -1) {
77 // fprintf(stderr, "FBIOGET_VBLANK error: %s\n", strerror(errno));
81 printf("flags: %x\n", vblank.flags);
82 printf("count: %d\n", vblank.count);
83 printf("beam position: %d, %d\n", vblank.hcount, vblank.vcount);
87 if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
88 fprintf(stderr, "Failed to allocate pixmap.\n");
92 gfx->pixmap->width = gfx->screen_rect.width;
93 gfx->pixmap->height = gfx->screen_rect.height;
95 int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
96 if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
97 fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
106 clear_screen(0, 0, 0);
115 munmap(framebuffer, FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth));
118 sh_free(gfx->pixmap->pixels);
119 gfx->pixmap->pixels = 0;
120 sh_free(gfx->pixmap);
124 unsigned char *get_framebuffer()
126 return gfx->pixmap->pixels;
129 Pixmap *get_framebuffer_pixmap()
134 Rect get_screen_size()
136 return gfx->screen_rect;
139 int get_color_depth()
141 return gfx->color_depth;
144 void set_clipping_rect(const Rect &rect)
146 gfx->clipping_rect = rect_intersection(rect, get_screen_size());
149 const Rect &get_clipping_rect()
151 return gfx->clipping_rect;
154 void set_cursor_visibility(bool visible)
157 curs.enable = visible ? 1 : 0;
159 if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
160 fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
166 memcpy(framebuffer, gfx->pixmap->pixels, gfx->pixmap->width * gfx->pixmap->height * (gfx->color_depth / 8));
171 unsigned long arg = 0;
172 if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
173 // printf("ioctl error %s\n", strerror(errno));
177 #endif // WINNIE_FBDEV