pad: fix reverse font color
[fbpad.git] / draw.c
blob4eaa0cf5f7b556d4f07d6510f1b75557ccccb3cc
1 #include <fcntl.h>
2 #include <linux/fb.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/ioctl.h>
6 #include <sys/mman.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include "draw.h"
10 #include "util.h"
12 #define FBDEV_PATH "/dev/fb0"
13 #define BPP 2
15 static int fd;
16 static char *fb;
17 static struct fb_var_screeninfo vinfo;
18 static struct fb_fix_screeninfo finfo;
20 static int fb_len()
22 return vinfo.xres_virtual * vinfo.yres_virtual * BPP;
25 void fb_init(void)
27 fd = open(FBDEV_PATH, O_RDWR);
28 if (fd == -1)
29 xerror("can't open " FBDEV_PATH);
30 if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1)
31 xerror("ioctl failed");
32 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
33 xerror("ioctl failed");
34 fb = mmap(NULL, fb_len(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
35 if (fb == MAP_FAILED)
36 xerror("can't map the framebuffer");
39 void fb_put(int r, int c, u16_t val)
41 long loc = (c + vinfo.xoffset) * BPP +
42 (r + vinfo.yoffset) * finfo.line_length;
43 memcpy(fb + loc, &val, BPP);
46 void fb_free()
48 munmap(fb, fb_len());
49 close(fd);
52 static u16_t color_bits(struct fb_bitfield *bf, unsigned char v)
54 unsigned char moved = v >> (8 - bf->length);
55 return moved << bf->offset;
58 u16_t fb_color(u8_t r, u8_t g, u8_t b)
60 return color_bits(&vinfo.red, r) |
61 color_bits(&vinfo.green, g) |
62 color_bits(&vinfo.blue, b);
65 int fb_rows(void)
67 return vinfo.yres_virtual;
70 int fb_cols(void)
72 return vinfo.xres_virtual;
75 void fb_box(int sr, int sc, int er, int ec, u16_t val)
77 int r, c;
78 for (r = sr; r < er; r++)
79 for (c = sc; c < ec; c++)
80 fb_put(r, c, val);
83 static char *rowaddr(int r)
85 return fb + (r + vinfo.yoffset) * finfo.line_length;
88 void fb_scroll(int sr, int nr, int n, u16_t val)
90 int r;
91 for (r = sr; r < sr + nr; r++)
92 memcpy(rowaddr(r + n), rowaddr(r), finfo.line_length);
93 if (n > 0)
94 fb_box(sr, 0, n, sr + n, val);
95 else
96 fb_box(sr + nr + n, 0, sr + nr, fb_cols(), val);