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
27 if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
28 fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
32 fb_var_screeninfo sinfo;
33 if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
36 fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
40 printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
41 printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
43 screen_rect.x = screen_rect.y = 0;
44 screen_rect.width = sinfo.xres_virtual;
45 screen_rect.height = sinfo.yres_virtual;
46 color_depth = sinfo.bits_per_pixel;
48 int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth);
49 framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
51 if(framebuffer == (void*)-1) {
54 fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
58 // 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));
66 printf("flags: %x\n", vblank.flags);
67 printf("count: %d\n", vblank.count);
68 printf("beam position: %d, %d\n", vblank.hcount, vblank.vcount);
77 clear_screen(0, 0, 0);
85 munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth));
89 unsigned char *get_framebuffer()
94 Rect get_screen_size()
104 void clear_screen(int r, int g, int b)
106 fill_rect(screen_rect, r, g, b);
109 void fill_rect(const Rect &rect, int r, int g, int b)
113 if(drect.x < screen_rect.x) {
114 drect.width -= screen_rect.x - drect.x;
115 drect.x = screen_rect.x;
118 if(drect.y < screen_rect.y) {
119 drect.height -= screen_rect.y - drect.y;
120 drect.y = screen_rect.y;
123 if(drect.x + drect.width >= screen_rect.x + screen_rect.width) {
124 drect.width = screen_rect.width - drect.x;
127 if(drect.y + drect.height >= screen_rect.y + screen_rect.height) {
128 drect.height = screen_rect.height - drect.y;
131 unsigned char *fb = framebuffer + (drect.x + screen_rect.width * drect.y) * 4;
132 for(int i=0; i<drect.height; i++) {
133 for(int j=0; j<drect.width; j++) {
138 fb += screen_rect.width * 4;
142 void set_cursor_visibility(bool visible)
145 curs.enable = visible ? 1 : 0;
147 if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
148 fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
152 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
153 const Rect &dest_rect, int dest_x, int dest_y)
155 int width = src_rect.width;
156 int height = src_rect.height;
158 int xoffs = dest_x - dest_rect.x;
160 dest_x = dest_rect.x;
164 int yoffs = dest_y - dest_rect.y;
166 dest_y = dest_rect.y;
170 int xend = dest_x + width;
171 if(xend >= dest_rect.width) {
172 width -= xend - dest_rect.width;
175 int yend = dest_y + height;
176 if(yend >= dest_rect.height) {
177 height -= yend - dest_rect.height;
180 if(width <= 0 || height <= 0) {
184 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
185 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
187 for(int i=0; i<height; i++) {
188 memcpy(dptr, sptr, width * 4);
189 sptr += src_rect.width * 4;
190 dptr += dest_rect.width * 4;
194 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
195 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
197 int width = src_rect.width;
198 int height = src_rect.height;
200 int xoffs = dest_x - dest_rect.x;
202 dest_x = dest_rect.x;
206 int yoffs = dest_y - dest_rect.y;
208 dest_y = dest_rect.y;
212 int xend = dest_x + width;
213 if(xend >= dest_rect.width) {
214 width -= xend - dest_rect.width;
217 int yend = dest_y + height;
218 if(yend >= dest_rect.height) {
219 height -= yend - dest_rect.height;
222 if(width <= 0 || height <= 0) {
226 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
227 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
229 for(int i=0; i<height; i++) {
230 for(int j=0; j<width; j++) {
232 int g = sptr[j * 4 + 1];
233 int b = sptr[j * 4 + 2];
235 if(r != key_r || g != key_g || b != key_b) {
242 sptr += src_rect.width * 4;
243 dptr += dest_rect.width * 4;
253 unsigned long arg = 0;
254 if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
255 // printf("ioctl error %s\n", strerror(errno));
259 #endif // WINNIE_FBDEV