fbpad: pass NULL instead of 0 to wait_pid()
[fbpad.git] / pad.c
bloba2a91c395147826aef5633a21ad4ed02f6d7c3c0
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 void pad_init(void)
20 fb_init();
21 font_init();
22 rows = fb_rows() / font_rows();
23 cols = fb_cols() / font_cols();
26 void pad_free(void)
28 fb_free();
31 #define CR(a) (((a) >> 16) & 0x0000ff)
32 #define CG(a) (((a) >> 8) & 0x0000ff)
33 #define CB(a) ((a) & 0x0000ff)
34 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
36 static fbval_t mixed_color(int fg, int bg, unsigned char val)
38 unsigned int fore = cd[fg], back = cd[bg];
39 unsigned char r = COLORMERGE(CR(fore), CR(back), val);
40 unsigned char g = COLORMERGE(CG(fore), CG(back), val);
41 unsigned char b = COLORMERGE(CB(fore), CB(back), val);
42 return fb_color(r, g, b);
45 static fbval_t color2fb(int c)
47 return fb_color(CR(cd[c]), CG(cd[c]), CB(cd[c]));
50 #define NCACHE (1 << 11)
51 static fbval_t cache[NCACHE * MAXDOTS];
52 static struct glyph {
53 int c;
54 short fg, bg;
55 } cacheid[NCACHE];
57 static int glyph_hash(struct glyph *g)
59 return (g->c ^ (g->fg << 7) ^ (g->bg << 6)) & (NCACHE - 1);
62 static fbval_t *bitmap(int c, short fg, short bg)
64 unsigned char *bits;
65 fbval_t *fbbits;
66 struct glyph glyph = {0};
67 int hash;
68 int i;
69 int nbits = font_rows() * font_cols();
70 if (c < 0 || (c < 256 && (!isprint(c) || isspace(c))))
71 return NULL;
72 glyph.c = c;
73 glyph.fg = fg;
74 glyph.bg = bg;
75 hash = glyph_hash(&glyph);
76 fbbits = &cache[hash * MAXDOTS];
77 if (!memcmp(&glyph, &cacheid[hash], sizeof(glyph)))
78 return fbbits;
79 bits = font_bitmap(c);
80 if (!bits)
81 return NULL;
82 memcpy(&cacheid[hash], &glyph, sizeof(glyph));
83 for (i = 0; i < nbits; i++)
84 fbbits[i] = mixed_color(fg, bg, bits[i]);
85 return fbbits;
88 #define MAXFBWIDTH (1 << 12)
89 void fb_box(int sr, int sc, int er, int ec, fbval_t val)
91 static fbval_t line[MAXFBWIDTH];
92 int cn = ec - sc;
93 int i;
94 for (i = 0; i < cn; i++)
95 line[i] = val;
96 for (i = sr; i < er; i++)
97 fb_set(i, sc, line, cn);
100 void pad_put(int ch, int r, int c, int fg, int bg)
102 int sr = font_rows() * r;
103 int sc = font_cols() * c;
104 int frows = font_rows(), fcols = font_cols();
105 int i;
106 fbval_t *bits = bitmap(ch, fg, bg);
107 if (!bits)
108 fb_box(sr, sc, sr + frows, sc + fcols, color2fb(bg));
109 else
110 for (i = 0; i < frows; i++)
111 fb_set(sr + i, sc, bits + (i * fcols), fcols);
114 void pad_blank(int c)
116 fb_box(0, 0, fb_rows(), fb_cols(), color2fb(c));
119 void pad_blankrow(int r, int bg)
121 int sr = r * font_rows();
122 int er = r == rows - 1 ? fb_rows() : (r + 1) * font_rows();
123 fb_box(sr, 0, er, fb_cols(), color2fb(bg));
126 int pad_rows(void)
128 return rows;
131 int pad_cols(void)
133 return cols;
136 void pad_shown(void)
138 fb_cmap();