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>
41 #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
43 static unsigned char *framebuffer;
45 static int rgb_order[3];
58 if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
62 get_subsys()->graphics_offset = (int)((char*)gfx - (char*)get_pool());
66 if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
67 fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
71 fb_var_screeninfo sinfo;
72 if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
75 fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
79 printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
80 printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
82 gfx->screen_rect.x = gfx->screen_rect.y = 0;
83 gfx->screen_rect.width = sinfo.xres_virtual;
84 gfx->screen_rect.height = sinfo.yres_virtual;
85 gfx->color_depth = sinfo.bits_per_pixel;
87 rgb_order[0] = sinfo.red.offset / 8;
88 rgb_order[1] = sinfo.green.offset / 8;
89 rgb_order[2] = sinfo.blue.offset / 8;
91 set_clipping_rect(gfx->screen_rect);
93 int sz = FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth);
94 framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
96 if(framebuffer == (void*)-1) {
99 fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
103 // TODO: uncomment when I find how to use intelfb instead of i915 GRRRR.-
105 if(ioctl(dev_fd, FBIOGET_VBLANK, &vblank) == -1) {
106 // fprintf(stderr, "FBIOGET_VBLANK error: %s\n", strerror(errno));
110 printf("flags: %x\n", vblank.flags);
111 printf("count: %d\n", vblank.count);
112 printf("beam position: %d, %d\n", vblank.hcount, vblank.vcount);
116 if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
117 fprintf(stderr, "Failed to allocate pixmap.\n");
121 gfx->pixmap->width = gfx->screen_rect.width;
122 gfx->pixmap->height = gfx->screen_rect.height;
124 int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
125 if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
126 fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
135 clear_screen(0, 0, 0);
136 gfx_update(gfx->screen_rect);
144 munmap(framebuffer, FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth));
147 sh_free(gfx->pixmap->pixels);
148 gfx->pixmap->pixels = 0;
149 sh_free(gfx->pixmap);
153 unsigned char *get_framebuffer()
155 return gfx->pixmap->pixels;
158 Pixmap *get_framebuffer_pixmap()
163 Rect get_screen_size()
165 return gfx->screen_rect;
168 int get_color_depth()
170 return gfx->color_depth;
173 void set_clipping_rect(const Rect &rect)
175 gfx->clipping_rect = rect_intersection(rect, get_screen_size());
178 const Rect &get_clipping_rect()
180 return gfx->clipping_rect;
183 void set_cursor_visibility(bool visible)
186 curs.enable = visible ? 1 : 0;
188 if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
189 fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
193 void gfx_update(const Rect &upd_rect)
195 Rect rect = rect_intersection(upd_rect, gfx->screen_rect);
196 unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
197 unsigned char *dptr = framebuffer + (rect.y * gfx->screen_rect.width + rect.x) * 4;
199 for(int i=0; i<rect.height; i++) {
200 memcpy(dptr, sptr, rect.width * 4);
201 sptr += gfx->screen_rect.width * 4;
202 dptr += gfx->screen_rect.width * 4;
208 unsigned long arg = 0;
209 if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
210 // printf("ioctl error %s\n", strerror(errno));
214 void get_rgb_order(int *r, int *g, int *b)
221 #endif // WINNIE_FBDEV