2 winnie - an experimental window system
4 Copyright (C) 2013 Eleni Maria Stea
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
30 #include <sys/ioctl.h>
40 #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
42 static unsigned char *framebuffer;
44 static int rgb_order[3];
57 if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
63 if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
64 fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
68 fb_var_screeninfo sinfo;
69 if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
72 fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
76 printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
77 printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
79 gfx->screen_rect.x = gfx->screen_rect.y = 0;
80 gfx->screen_rect.width = sinfo.xres_virtual;
81 gfx->screen_rect.height = sinfo.yres_virtual;
82 gfx->color_depth = sinfo.bits_per_pixel;
84 rgb_order[0] = sinfo.red.offset / 8;
85 rgb_order[1] = sinfo.green.offset / 8;
86 rgb_order[2] = sinfo.blue.offset / 8;
88 set_clipping_rect(gfx->screen_rect);
90 int sz = FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth);
91 framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
93 if(framebuffer == (void*)-1) {
96 fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
100 // TODO: uncomment when I find how to use intelfb instead of i915 GRRRR.-
102 if(ioctl(dev_fd, FBIOGET_VBLANK, &vblank) == -1) {
103 // fprintf(stderr, "FBIOGET_VBLANK error: %s\n", strerror(errno));
107 printf("flags: %x\n", vblank.flags);
108 printf("count: %d\n", vblank.count);
109 printf("beam position: %d, %d\n", vblank.hcount, vblank.vcount);
113 if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
114 fprintf(stderr, "Failed to allocate pixmap.\n");
118 gfx->pixmap->width = gfx->screen_rect.width;
119 gfx->pixmap->height = gfx->screen_rect.height;
121 int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
122 if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
123 fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
132 clear_screen(0, 0, 0);
133 gfx_update(gfx->screen_rect);
141 munmap(framebuffer, FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth));
144 sh_free(gfx->pixmap->pixels);
145 gfx->pixmap->pixels = 0;
146 sh_free(gfx->pixmap);
150 unsigned char *get_framebuffer()
152 return gfx->pixmap->pixels;
155 Pixmap *get_framebuffer_pixmap()
160 Rect get_screen_size()
162 return gfx->screen_rect;
165 int get_color_depth()
167 return gfx->color_depth;
170 void set_clipping_rect(const Rect &rect)
172 gfx->clipping_rect = rect_intersection(rect, get_screen_size());
175 const Rect &get_clipping_rect()
177 return gfx->clipping_rect;
180 void set_cursor_visibility(bool visible)
183 curs.enable = visible ? 1 : 0;
185 if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
186 fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
190 void gfx_update(const Rect &upd_rect)
192 Rect rect = rect_intersection(upd_rect, gfx->screen_rect);
193 unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
194 unsigned char *dptr = framebuffer + (rect.y * gfx->screen_rect.width + rect.x) * 4;
196 for(int i=0; i<rect.height; i++) {
197 memcpy(dptr, sptr, rect.width * 4);
198 sptr += gfx->screen_rect.width * 4;
199 dptr += gfx->screen_rect.width * 4;
205 unsigned long arg = 0;
206 if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
207 // printf("ioctl error %s\n", strerror(errno));
211 void get_rgb_order(int *r, int *g, int *b)
218 #endif // WINNIE_FBDEV