Makefile: make it more portable
[fbvnc.git] / draw.c
blob317d19380bc2b37d1a6dbb8fcfae13263bc7c5e2
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 */
22 static int xres, yres, xoff, yoff; /* drawing region */
24 static int fb_len(void)
26 return finfo.line_length * vinfo.yres_virtual;
29 static void fb_cmap_save(int save)
31 static unsigned short red[NLEVELS], green[NLEVELS], blue[NLEVELS];
32 struct fb_cmap cmap;
33 if (finfo.visual == FB_VISUAL_TRUECOLOR)
34 return;
35 cmap.start = 0;
36 cmap.len = MAX(nr, MAX(ng, nb));
37 cmap.red = red;
38 cmap.green = green;
39 cmap.blue = blue;
40 cmap.transp = NULL;
41 ioctl(fd, save ? FBIOGETCMAP : FBIOPUTCMAP, &cmap);
44 void fb_cmap(void)
46 unsigned short red[NLEVELS], green[NLEVELS], blue[NLEVELS];
47 struct fb_cmap cmap;
48 int i;
49 if (finfo.visual == FB_VISUAL_TRUECOLOR)
50 return;
52 for (i = 0; i < nr; i++)
53 red[i] = (65535 / (nr - 1)) * i;
54 for (i = 0; i < ng; i++)
55 green[i] = (65535 / (ng - 1)) * i;
56 for (i = 0; i < nb; i++)
57 blue[i] = (65535 / (nb - 1)) * i;
59 cmap.start = 0;
60 cmap.len = MAX(nr, MAX(ng, nb));
61 cmap.red = red;
62 cmap.green = green;
63 cmap.blue = blue;
64 cmap.transp = NULL;
66 ioctl(fd, FBIOPUTCMAP, &cmap);
69 unsigned fb_mode(void)
71 return ((rl < gl) << 22) | ((rl < bl) << 21) | ((gl < bl) << 20) |
72 (bpp << 16) | (vinfo.red.length << 8) |
73 (vinfo.green.length << 4) | (vinfo.blue.length);
76 static void init_colors(void)
78 nr = 1 << vinfo.red.length;
79 ng = 1 << vinfo.blue.length;
80 nb = 1 << vinfo.green.length;
81 rr = 8 - vinfo.red.length;
82 rl = vinfo.red.offset;
83 gr = 8 - vinfo.green.length;
84 gl = vinfo.green.offset;
85 br = 8 - vinfo.blue.length;
86 bl = vinfo.blue.offset;
89 int fb_init(char *dev)
91 char *path = dev ? dev : FBDEV;
92 char *geom = dev ? strchr(dev, ':') : NULL;
93 if (geom) {
94 *geom = '\0';
95 sscanf(geom + 1, "%dx%d%d%d", &xres, &yres, &xoff, &yoff);
97 fd = open(path, O_RDWR);
98 if (fd < 0)
99 goto failed;
100 if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0)
101 goto failed;
102 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) < 0)
103 goto failed;
104 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
105 bpp = (vinfo.bits_per_pixel + 7) >> 3;
106 fb = mmap(NULL, fb_len(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
107 if (fb == MAP_FAILED)
108 goto failed;
109 init_colors();
110 fb_cmap_save(1);
111 fb_cmap();
112 return 0;
113 failed:
114 perror("fb_init()");
115 close(fd);
116 return 1;
119 void fb_free(void)
121 fb_cmap_save(0);
122 munmap(fb, fb_len());
123 close(fd);
126 int fb_rows(void)
128 return yres ? yres : vinfo.yres;
131 int fb_cols(void)
133 return xres ? xres : vinfo.xres;
136 void *fb_mem(int r)
138 return fb + (r + vinfo.yoffset + yoff) * finfo.line_length + (vinfo.xoffset + xoff) * bpp;
141 unsigned fb_val(int r, int g, int b)
143 return ((r >> rr) << rl) | ((g >> gr) << gl) | ((b >> br) << bl);