term: handle invalid values for scrolling region
[fbpad.git] / draw.c
blob473c407d0d5df029cd204c679903f0a6a8faa1a2
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 BPP sizeof(fbval_t)
14 #define NLEVELS (1 << 8)
16 static int fd;
17 static unsigned char *fb;
18 static struct fb_var_screeninfo vinfo;
19 static struct fb_fix_screeninfo finfo;
20 static int rl, rr, gl, gr, bl, br;
21 static int nr, ng, nb;
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 = 0;
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 = 0;
65 ioctl(fd, FBIOPUTCMAP, &cmap);
68 static void xerror(char *msg)
70 perror(msg);
71 exit(1);
74 static void xdie(char *msg)
76 fprintf(stderr, "%s\n", msg);
77 exit(1);
80 static void init_colors(void)
82 nr = 1 << vinfo.red.length;
83 ng = 1 << vinfo.green.length;
84 nb = 1 << vinfo.blue.length;
85 rr = 8 - vinfo.red.length;
86 rl = vinfo.red.offset;
87 gr = 8 - vinfo.green.length;
88 gl = vinfo.green.offset;
89 br = 8 - vinfo.blue.length;
90 bl = vinfo.blue.offset;
93 void fb_init(void)
95 fd = open(FBDEV_PATH, O_RDWR);
96 if (fd == -1)
97 xerror("can't open " FBDEV_PATH);
98 if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1)
99 xerror("ioctl failed");
100 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
101 xerror("ioctl failed");
102 if ((vinfo.bits_per_pixel + 7) >> 3 != BPP)
103 xdie("fbval_t does not match framebuffer depth");
104 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
105 init_colors();
106 fb = mmap(NULL, fb_len(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
107 if (fb == MAP_FAILED)
108 xerror("can't map the framebuffer");
109 fb_cmap_save(1);
110 fb_cmap();
113 void fb_set(int r, int c, fbval_t *mem, int len)
115 long loc = (c + vinfo.xoffset) * BPP +
116 (r + vinfo.yoffset) * finfo.line_length;
117 memcpy(fb + loc, mem, len * BPP);
120 void fb_free(void)
122 fb_cmap_save(0);
123 munmap(fb, fb_len());
124 close(fd);
127 fbval_t fb_color(unsigned char r, unsigned char g, unsigned char b)
129 return ((r >> rr) << rl) | ((g >> gr) << gl) | ((b >> br) << bl);
132 int fb_rows(void)
134 return vinfo.yres;
137 int fb_cols(void)
139 return vinfo.xres;