17 #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
19 static unsigned char *framebuffer;
20 static int dev_fd = -1;
22 static Rect screen_rect;
23 static int color_depth; // bits per pixel
25 static Pixmap *pixmap;
29 if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
30 fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
34 fb_var_screeninfo sinfo;
35 if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
38 fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
42 printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
43 printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
45 screen_rect.x = screen_rect.y = 0;
46 screen_rect.width = sinfo.xres_virtual;
47 screen_rect.height = sinfo.yres_virtual;
48 color_depth = sinfo.bits_per_pixel;
50 int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth);
51 framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
53 if(framebuffer == (void*)-1) {
56 fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
60 // TODO: uncomment when I find how to use intelfb instead of i915 GRRRR.-
62 if(ioctl(dev_fd, FBIOGET_VBLANK, &vblank) == -1) {
63 // fprintf(stderr, "FBIOGET_VBLANK error: %s\n", strerror(errno));
67 printf("flags: %x\n", vblank.flags);
68 printf("count: %d\n", vblank.count);
69 printf("beam position: %d, %d\n", vblank.hcount, vblank.vcount);
74 pixmap->width = screen_rect.width;
75 pixmap->height = screen_rect.height;
76 pixmap->pixels = framebuffer;
83 clear_screen(0, 0, 0);
91 munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth));
97 unsigned char *get_framebuffer()
102 Pixmap *get_framebuffer_pixmap()
107 Rect get_screen_size()
112 int get_color_depth()
117 void clear_screen(int r, int g, int b)
119 fill_rect(screen_rect, r, g, b);
122 void fill_rect(const Rect &rect, int r, int g, int b)
126 if(drect.x < screen_rect.x) {
127 drect.width -= screen_rect.x - drect.x;
128 drect.x = screen_rect.x;
131 if(drect.y < screen_rect.y) {
132 drect.height -= screen_rect.y - drect.y;
133 drect.y = screen_rect.y;
136 if(drect.x + drect.width >= screen_rect.x + screen_rect.width) {
137 drect.width = screen_rect.width - drect.x;
140 if(drect.y + drect.height >= screen_rect.y + screen_rect.height) {
141 drect.height = screen_rect.height - drect.y;
144 unsigned char *fb = framebuffer + (drect.x + screen_rect.width * drect.y) * 4;
145 for(int i=0; i<drect.height; i++) {
146 for(int j=0; j<drect.width; j++) {
151 fb += screen_rect.width * 4;
155 void set_cursor_visibility(bool visible)
158 curs.enable = visible ? 1 : 0;
160 if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
161 fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
165 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
166 const Rect &dest_rect, int dest_x, int dest_y)
168 int width = src_rect.width;
169 int height = src_rect.height;
171 int xoffs = dest_x - dest_rect.x;
173 dest_x = dest_rect.x;
177 int yoffs = dest_y - dest_rect.y;
179 dest_y = dest_rect.y;
183 int xend = dest_x + width;
184 if(xend >= dest_rect.width) {
185 width -= xend - dest_rect.width;
188 int yend = dest_y + height;
189 if(yend >= dest_rect.height) {
190 height -= yend - dest_rect.height;
193 if(width <= 0 || height <= 0) {
197 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
198 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
200 for(int i=0; i<height; i++) {
201 memcpy(dptr, sptr, width * 4);
202 sptr += src_rect.width * 4;
203 dptr += dest_rect.width * 4;
207 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
208 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
210 int width = src_rect.width;
211 int height = src_rect.height;
213 int xoffs = dest_x - dest_rect.x;
215 dest_x = dest_rect.x;
219 int yoffs = dest_y - dest_rect.y;
221 dest_y = dest_rect.y;
225 int xend = dest_x + width;
226 if(xend >= dest_rect.width) {
227 width -= xend - dest_rect.width;
230 int yend = dest_y + height;
231 if(yend >= dest_rect.height) {
232 height -= yend - dest_rect.height;
235 if(width <= 0 || height <= 0) {
239 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
240 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
242 for(int i=0; i<height; i++) {
243 for(int j=0; j<width; j++) {
245 int g = sptr[j * 4 + 1];
246 int b = sptr[j * 4 + 2];
248 if(r != key_r || g != key_g || b != key_b) {
255 sptr += src_rect.width * 4;
256 dptr += dest_rect.width * 4;
266 unsigned long arg = 0;
267 if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
268 // printf("ioctl error %s\n", strerror(errno));
272 #endif // WINNIE_FBDEV