fbpad: new tag listing
[fbpad.git] / draw.c
blobe016220e74ce1e068026e2dd7e2b291fa71247a3
1 #include <fcntl.h>
2 #include <linux/fb.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/ioctl.h>
7 #include <sys/mman.h>
8 #include <unistd.h>
9 #include "draw.h"
11 #define MIN(a, b) ((a) < (b) ? (a) : (b))
12 #define MAX(a, b) ((a) > (b) ? (a) : (b))
13 #define NLEVELS (1 << 8)
15 static struct fb_var_screeninfo vinfo; /* linux-specific FB structure */
16 static struct fb_fix_screeninfo finfo; /* linux-specific FB structure */
17 static int fd; /* FB device file descriptor */
18 static void *fb; /* mmap()ed FB memory */
19 static int bpp; /* bytes per pixel */
20 static int nr, ng, nb; /* color levels */
21 static int rl, rr, gl, gr, bl, br; /* shifts per color */
23 static int fb_len(void)
25 return finfo.line_length * vinfo.yres_virtual;
28 static void fb_cmap_save(int save)
30 static unsigned short red[NLEVELS], green[NLEVELS], blue[NLEVELS];
31 struct fb_cmap cmap;
32 if (finfo.visual == FB_VISUAL_TRUECOLOR)
33 return;
34 cmap.start = 0;
35 cmap.len = MAX(nr, MAX(ng, nb));
36 cmap.red = red;
37 cmap.green = green;
38 cmap.blue = blue;
39 cmap.transp = NULL;
40 ioctl(fd, save ? FBIOGETCMAP : FBIOPUTCMAP, &cmap);
43 void fb_cmap(void)
45 unsigned short red[NLEVELS], green[NLEVELS], blue[NLEVELS];
46 struct fb_cmap cmap;
47 int i;
48 if (finfo.visual == FB_VISUAL_TRUECOLOR)
49 return;
51 for (i = 0; i < nr; i++)
52 red[i] = (65535 / (nr - 1)) * i;
53 for (i = 0; i < ng; i++)
54 green[i] = (65535 / (ng - 1)) * i;
55 for (i = 0; i < nb; i++)
56 blue[i] = (65535 / (nb - 1)) * i;
58 cmap.start = 0;
59 cmap.len = MAX(nr, MAX(ng, nb));
60 cmap.red = red;
61 cmap.green = green;
62 cmap.blue = blue;
63 cmap.transp = NULL;
65 ioctl(fd, FBIOPUTCMAP, &cmap);
68 unsigned fb_mode(void)
70 return ((rl < gl) << 22) | ((rl < bl) << 21) | ((gl < bl) << 20) |
71 (bpp << 16) | (vinfo.red.length << 8) |
72 (vinfo.green.length << 4) | (vinfo.blue.length);
75 static void init_colors(void)
77 nr = 1 << vinfo.red.length;
78 ng = 1 << vinfo.blue.length;
79 nb = 1 << vinfo.green.length;
80 rr = 8 - vinfo.red.length;
81 rl = vinfo.red.offset;
82 gr = 8 - vinfo.green.length;
83 gl = vinfo.green.offset;
84 br = 8 - vinfo.blue.length;
85 bl = vinfo.blue.offset;
88 int fb_init(char *dev)
90 fd = open(dev, O_RDWR);
91 if (fd < 0)
92 goto failed;
93 if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0)
94 goto failed;
95 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) < 0)
96 goto failed;
97 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
98 bpp = (vinfo.bits_per_pixel + 7) >> 3;
99 fb = mmap(NULL, fb_len(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
100 if (fb == MAP_FAILED)
101 goto failed;
102 init_colors();
103 fb_cmap_save(1);
104 fb_cmap();
105 return 0;
106 failed:
107 perror("fb_init()");
108 close(fd);
109 return 1;
112 void fb_free(void)
114 fb_cmap_save(0);
115 munmap(fb, fb_len());
116 close(fd);
119 int fb_rows(void)
121 return vinfo.yres;
124 int fb_cols(void)
126 return vinfo.xres;
129 void *fb_mem(int r)
131 return fb + (r + vinfo.yoffset) * finfo.line_length + vinfo.xoffset * bpp;
134 unsigned fb_val(int r, int g, int b)
136 return ((r >> rr) << rl) | ((g >> gr) << gl) | ((b >> br) << bl);