fbpad: re-register signals in signalreceived()
[fbpad.git] / pad.c
blob15fd1cb653e579030eb05774b9a19dad01a5ef06
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "config.h"
6 #include "draw.h"
7 #include "font.h"
8 #include "util.h"
9 #include "pad.h"
11 static unsigned int cd[] = {
12 COLOR0, COLOR1, COLOR2, COLOR3,
13 COLOR4, COLOR5, COLOR6, COLOR7,
14 COLOR8, COLOR9, COLOR10, COLOR11,
15 COLOR12, COLOR13, COLOR14, COLOR15};
16 static int rows, cols;
18 int pad_init(void)
20 if (fb_init())
21 return 1;
22 if (sizeof(fbval_t) != FBM_BPP(fb_mode())) {
23 fprintf(stderr, "pad_init: fbval_t doesn't match fb depth\n");
24 return 1;
26 font_init();
27 rows = fb_rows() / font_rows();
28 cols = fb_cols() / font_cols();
29 return 0;
32 void pad_free(void)
34 fb_free();
37 #define CR(a) (((a) >> 16) & 0x0000ff)
38 #define CG(a) (((a) >> 8) & 0x0000ff)
39 #define CB(a) ((a) & 0x0000ff)
40 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
42 static unsigned mixed_color(int fg, int bg, unsigned char val)
44 unsigned int fore = cd[fg], back = cd[bg];
45 unsigned char r = COLORMERGE(CR(fore), CR(back), val);
46 unsigned char g = COLORMERGE(CG(fore), CG(back), val);
47 unsigned char b = COLORMERGE(CB(fore), CB(back), val);
48 return FB_VAL(r, g, b);
51 static unsigned color2fb(int c)
53 return FB_VAL(CR(cd[c]), CG(cd[c]), CB(cd[c]));
56 #define NCACHE (1 << 11)
57 static fbval_t cache[NCACHE * MAXDOTS];
58 static struct glyph {
59 int c;
60 short fg, bg;
61 } cacheid[NCACHE];
63 static int glyph_hash(struct glyph *g)
65 return (g->c ^ (g->fg << 7) ^ (g->bg << 6)) & (NCACHE - 1);
68 static fbval_t *bitmap(int c, short fg, short bg)
70 unsigned char *bits;
71 fbval_t *fbbits;
72 struct glyph glyph = {0};
73 int hash;
74 int i;
75 int nbits = font_rows() * font_cols();
76 if (c < 0 || (c < 256 && (!isprint(c) || isspace(c))))
77 return NULL;
78 glyph.c = c;
79 glyph.fg = fg;
80 glyph.bg = bg;
81 hash = glyph_hash(&glyph);
82 fbbits = &cache[hash * MAXDOTS];
83 if (!memcmp(&glyph, &cacheid[hash], sizeof(glyph)))
84 return fbbits;
85 bits = font_bitmap(c);
86 if (!bits)
87 return NULL;
88 memcpy(&cacheid[hash], &glyph, sizeof(glyph));
89 for (i = 0; i < nbits; i++)
90 fbbits[i] = mixed_color(fg, bg, bits[i]);
91 return fbbits;
94 #define MAXFBWIDTH (1 << 12)
95 static void fb_box(int sr, int sc, int er, int ec, fbval_t val)
97 static fbval_t line[MAXFBWIDTH];
98 int cn = ec - sc;
99 int i;
100 for (i = 0; i < cn; i++)
101 line[i] = val;
102 for (i = sr; i < er; i++)
103 fb_set(i, sc, line, cn);
106 void pad_put(int ch, int r, int c, int fg, int bg)
108 int sr = font_rows() * r;
109 int sc = font_cols() * c;
110 int frows = font_rows(), fcols = font_cols();
111 int i;
112 fbval_t *bits = bitmap(ch, fg, bg);
113 if (!bits)
114 fb_box(sr, sc, sr + frows, sc + fcols, color2fb(bg));
115 else
116 for (i = 0; i < frows; i++)
117 fb_set(sr + i, sc, bits + (i * fcols), fcols);
120 void pad_blank(int c)
122 fb_box(0, 0, fb_rows(), fb_cols(), color2fb(c));
125 void pad_blankrow(int r, int bg)
127 int sr = r * font_rows();
128 int er = r == rows - 1 ? fb_rows() : (r + 1) * font_rows();
129 fb_box(sr, 0, er, fb_cols(), color2fb(bg));
132 int pad_rows(void)
134 return rows;
137 int pad_cols(void)
139 return cols;
142 void pad_shown(void)
144 fb_cmap();