pad: use memcpy instead of equals operator
[fbpad.git] / pad.c
blob6f56bd81e8b56a6c99ac12e86a16aff83ceb5e7b
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <wctype.h>
6 #include "config.h"
7 #include "draw.h"
8 #include "font.h"
9 #include "util.h"
10 #include "pad.h"
12 static unsigned int cd[] = {
13 COLOR0, COLOR1, COLOR2, COLOR3,
14 COLOR4, COLOR5, COLOR6, COLOR7,
15 COLOR8, COLOR9, COLOR10, COLOR11,
16 COLOR12, COLOR13, COLOR14, COLOR15};
17 static int rows, cols;
19 void pad_init(void)
21 fb_init();
22 font_init();
23 rows = fb_rows() / font_rows();
24 cols = fb_cols() / font_cols();
27 void pad_free(void)
29 fb_free();
32 #define CR(a) (((a) >> 16) & 0x0000ff)
33 #define CG(a) (((a) >> 8) & 0x0000ff)
34 #define CB(a) ((a) & 0x0000ff)
35 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
37 static fbval_t mixed_color(int fg, int bg, unsigned char val)
39 unsigned int fore = cd[fg], back = cd[bg];
40 unsigned char r = COLORMERGE(CR(fore), CR(back), val);
41 unsigned char g = COLORMERGE(CG(fore), CG(back), val);
42 unsigned char b = COLORMERGE(CB(fore), CB(back), val);
43 return fb_color(r, g, b);
46 static fbval_t color2fb(int c)
48 return fb_color(CR(cd[c]), CG(cd[c]), CB(cd[c]));
51 #define NCACHE ((1 << 11) - 1)
52 static fbval_t cache[NCACHE * MAXDOTS];
53 static struct glyph {
54 int c;
55 short fg, bg;
56 } cacheid[NCACHE];
58 static int glyph_hash(struct glyph *g)
60 return (g->c | (((g->fg + 1) ^ g->bg) << 7)) % NCACHE;
63 static fbval_t *bitmap(int c, short fg, short bg)
65 unsigned char *bits;
66 fbval_t *fbbits;
67 struct glyph glyph = {0};
68 int hash;
69 int i;
70 int nbits = font_rows() * font_cols();
71 if (c < 0 || (c < 256 && (!isprint(c) || isspace(c))))
72 return NULL;
73 glyph.c = c;
74 glyph.fg = fg;
75 glyph.bg = bg;
76 hash = glyph_hash(&glyph);
77 fbbits = &cache[hash * MAXDOTS];
78 if (!memcmp(&glyph, &cacheid[hash], sizeof(glyph)))
79 return fbbits;
80 bits = font_bitmap(c);
81 if (!bits)
82 return NULL;
83 memcpy(&cacheid[hash], &glyph, sizeof(glyph));
84 for (i = 0; i < nbits; i++)
85 fbbits[i] = mixed_color(fg, bg, bits[i]);
86 return fbbits;
89 void pad_put(int ch, int r, int c, int fg, int bg)
91 int sr = font_rows() * r;
92 int sc = font_cols() * c;
93 int frows = font_rows(), fcols = font_cols();
94 int i;
95 fbval_t *bits = bitmap(ch, fg, bg);
96 if (!bits)
97 fb_box(sr, sc, sr + frows, sc + fcols, color2fb(bg));
98 else
99 for (i = 0; i < frows; i++)
100 fb_set(sr + i, sc, bits + (i * fcols), fcols);
103 void pad_blank(int c)
105 fb_box(0, 0, fb_rows(), fb_cols(), color2fb(c));
108 void pad_blankrow(int r, int bg)
110 int sr = r * font_rows();
111 int er = r == rows - 1 ? fb_rows() : (r + 1) * font_rows();
112 fb_box(sr, 0, er, fb_cols(), color2fb(bg));
115 int pad_rows(void)
117 return rows;
120 int pad_cols(void)
122 return cols;
125 void pad_shown(void)
127 fb_cmap();