font: better font baseline detection
[fbpad.git] / draw.c
bloba0939f3a654e4dd8edd658d76ed4aed87086e5d3
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 "config.h"
10 #include "draw.h"
11 #include "util.h"
13 #define FBDEV_PATH "/dev/fb0"
14 #define MAXWIDTH (1 << 12)
15 #define BPP sizeof(fbval_t)
16 #define NLEVELS (1 << 8)
18 static int fd;
19 static unsigned char *fb;
20 static struct fb_var_screeninfo vinfo;
21 static struct fb_fix_screeninfo finfo;
23 static int fb_len()
25 return vinfo.xres_virtual * vinfo.yres_virtual * BPP;
28 static void fb_cmap_save(int save)
30 static unsigned short red[NLEVELS], green[NLEVELS], blue[NLEVELS];
31 struct fb_cmap cmap;
32 int mr = 1 << vinfo.red.length;
33 int mg = 1 << vinfo.green.length;
34 int mb = 1 << vinfo.blue.length;
35 if (finfo.visual == FB_VISUAL_TRUECOLOR)
36 return;
37 cmap.start = 0;
38 cmap.len = MAX(mr, MAX(mg, mb));
39 cmap.red = red;
40 cmap.green = green;
41 cmap.blue = blue;
42 cmap.transp = 0;
43 if (save)
44 ioctl(fd, FBIOGETCMAP, &cmap);
45 else
46 ioctl(fd, FBIOPUTCMAP, &cmap);
49 void fb_cmap(void)
51 unsigned short red[NLEVELS], green[NLEVELS], blue[NLEVELS];
52 struct fb_cmap cmap;
53 int mr = 1 << vinfo.red.length;
54 int mg = 1 << vinfo.green.length;
55 int mb = 1 << vinfo.blue.length;
56 int i;
57 if (finfo.visual == FB_VISUAL_TRUECOLOR)
58 return;
60 for (i = 0; i < mr; i++)
61 red[i] = (65535 / (mr - 1)) * i;
62 for (i = 0; i < mg; i++)
63 green[i] = (65535 / (mg - 1)) * i;
64 for (i = 0; i < mb; i++)
65 blue[i] = (65535 / (mb - 1)) * i;
67 cmap.start = 0;
68 cmap.len = MAX(mr, MAX(mg, mb));
69 cmap.red = red;
70 cmap.green = green;
71 cmap.blue = blue;
72 cmap.transp = 0;
74 ioctl(fd, FBIOPUTCMAP, &cmap);
77 void fb_init(void)
79 fd = open(FBDEV_PATH, O_RDWR);
80 if (fd == -1)
81 xerror("can't open " FBDEV_PATH);
82 if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1)
83 xerror("ioctl failed");
84 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
85 xerror("ioctl failed");
86 fb = mmap(NULL, fb_len(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
87 if (fb == MAP_FAILED)
88 xerror("can't map the framebuffer");
89 fb_cmap_save(1);
90 fb_cmap();
93 void fb_set(int r, int c, fbval_t *mem, int len)
95 long loc = (c + vinfo.xoffset) * BPP +
96 (r + vinfo.yoffset) * finfo.line_length;
97 memcpy(fb + loc, mem, len * BPP);
100 void fb_free()
102 fb_cmap_save(0);
103 munmap(fb, fb_len());
104 close(fd);
107 static fbval_t color_bits(struct fb_bitfield *bf, fbval_t v)
109 fbval_t moved = v >> (8 - bf->length);
110 return moved << bf->offset;
113 fbval_t fb_color(unsigned char r, unsigned char g, unsigned char b)
115 return color_bits(&vinfo.red, r) |
116 color_bits(&vinfo.green, g) |
117 color_bits(&vinfo.blue, b);
120 int fb_rows(void)
122 return vinfo.yres;
125 int fb_cols(void)
127 return vinfo.xres;
130 static unsigned char *rowaddr(int r)
132 return fb + (r + vinfo.yoffset) * finfo.line_length;
135 static unsigned long cache[MAXWIDTH];
136 void fb_box(int sr, int sc, int er, int ec, fbval_t val)
138 int i;
139 int pc = sizeof(cache[0]) / sizeof(val);
140 int cn = MIN((ec - sc) / pc + 1, ARRAY_SIZE(cache));
141 unsigned long nv = val;
142 for (i = 1; i < pc; i++)
143 nv = (nv << (sizeof(val) * 8)) | val;
144 for (i = 0; i < cn; i++)
145 cache[i] = nv;
146 for (i = sr; i < er; i++)
147 memcpy(rowaddr(i) + sc * BPP, cache, (ec - sc) * BPP);