16 #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
18 static unsigned char *framebuffer;
19 static int dev_fd = -1;
21 static Rect screen_rect;
22 static int color_depth; // bits per pixel
26 if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
27 fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
31 fb_var_screeninfo sinfo;
32 if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
35 fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
39 printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
40 printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
42 screen_rect.x = screen_rect.y = 0;
43 screen_rect.width = sinfo.xres_virtual;
44 screen_rect.height = sinfo.yres_virtual;
45 color_depth = sinfo.bits_per_pixel;
47 int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth);
48 framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
50 if(framebuffer == (void*)-1) {
53 fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
62 clear_screen(0, 0, 0);
70 munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth));
74 unsigned char *get_framebuffer()
79 Rect get_screen_size()
89 void clear_screen(int r, int g, int b)
91 fill_rect(screen_rect, r, g, b);
94 void fill_rect(const Rect &rect, int r, int g, int b)
98 if(drect.x < screen_rect.x) {
99 drect.x = screen_rect.x;
102 if(drect.y < screen_rect.y) {
103 drect.y = screen_rect.y;
106 if(drect.x + drect.width > screen_rect.width) {
107 drect.width = screen_rect.width - drect.x;
110 if(drect.y + drect.height > screen_rect.height) {
111 drect.height = screen_rect.height - drect.y;
114 unsigned char *fb = framebuffer + (drect.x + screen_rect.width * drect.y) * 4;
115 for(int i=0; i<drect.height; i++) {
116 for(int j=0; j<drect.width; j++) {
121 fb += screen_rect.width * 4;
125 void set_cursor_visibility(bool visible)
128 curs.enable = visible ? 1 : 0;
130 if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
131 fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
135 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
136 const Rect &dest_rect, int dest_x, int dest_y)
138 int width = src_rect.width;
139 int height = src_rect.height;
141 int xoffs = dest_x - dest_rect.x;
143 dest_x = dest_rect.x;
147 int yoffs = dest_y - dest_rect.y;
149 dest_y = dest_rect.y;
153 int xend = dest_x + width;
154 if(xend >= dest_rect.width) {
155 width -= xend - dest_rect.width;
158 int yend = dest_y + height;
159 if(yend >= dest_rect.height) {
160 height -= yend - dest_rect.height;
163 if(width <= 0 || height <= 0) {
167 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
168 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
170 for(int i=0; i<height; i++) {
171 memcpy(dptr, sptr, width * 4);
172 sptr += src_rect.width * 4;
173 dptr += dest_rect.width * 4;
177 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
178 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
180 int width = src_rect.width;
181 int height = src_rect.height;
183 int xoffs = dest_x - dest_rect.x;
185 dest_x = dest_rect.x;
189 int yoffs = dest_y - dest_rect.y;
191 dest_y = dest_rect.y;
195 int xend = dest_x + width;
196 if(xend >= dest_rect.width) {
197 width -= xend - dest_rect.width;
200 int yend = dest_y + height;
201 if(yend >= dest_rect.height) {
202 height -= yend - dest_rect.height;
205 if(width <= 0 || height <= 0) {
209 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
210 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
212 for(int i=0; i<height; i++) {
213 for(int j=0; j<width; j++) {
215 int g = sptr[j * 4 + 1];
216 int b = sptr[j * 4 + 2];
218 if(r != key_r || g != key_g || b != key_b) {
225 sptr += src_rect.width * 4;
226 dptr += dest_rect.width * 4;
234 #endif // WINNIE_FBDEV