vt102: CUU should move upwards
[fbpad.git] / pad.c
blobc1ddd9fcbce597a6235dae64273e657d725e3a12
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "draw.h"
6 #include "font.h"
7 #include "util.h"
8 #include "pad.h"
10 static int rows, cols;
11 static unsigned int cd[] = {
12 0x0a0a0a, 0xc04444, 0x339933, 0xcccc66,
13 0x5566bc, 0xcd66af, 0xa166cd, 0xeeeeee,
14 0x71a3b7, 0xc08888, 0x779977, 0xcccc99,
15 0x8899bc, 0xcd99af, 0xa199cd, 0xdedede};
17 void pad_init(void)
19 fb_init();
20 font_init();
21 rows = fb_rows() / font_rows();
22 cols = fb_cols() / font_cols();
25 void pad_free(void)
27 fb_free();
30 #define CR(a) (((a) >> 16) & 0x0000ff)
31 #define CG(a) (((a) >> 8) & 0x0000ff)
32 #define CB(a) ((a) & 0x0000ff)
33 #define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
35 static fbval_t mixed_color(int fg, int bg, unsigned char val)
37 unsigned int fore = cd[fg], back = cd[bg];
38 unsigned char r = COLORMERGE(CR(fore), CR(back), val);
39 unsigned char g = COLORMERGE(CG(fore), CG(back), val);
40 unsigned char b = COLORMERGE(CB(fore), CB(back), val);
41 return fb_color(r, g, b);
44 static fbval_t color2fb(int c)
46 return fb_color(CR(cd[c]), CG(cd[c]), CB(cd[c]));
49 #define NCACHE ((1 << 11) - 1)
50 static fbval_t cache[NCACHE * MAXDOTS];
51 static struct glyph {
52 int c;
53 short fg, bg;
54 } cacheid[NCACHE];
56 static int glyph_hash(struct glyph *g)
58 return (g->c | (((g->fg + 1) ^ g->bg) << 7)) % NCACHE;
61 static fbval_t *bitmap(int c, short fg, short bg)
63 unsigned char *bits;
64 fbval_t *fbbits;
65 struct glyph glyph = {0};
66 int hash;
67 int i;
68 int nbits = font_rows() * font_cols();
69 if (!isprint(c) || isspace(c))
70 return NULL;
71 bits = font_bitmap(c, fg >= 8);
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 cacheid[hash] = glyph;
80 for (i = 0; i < nbits; i++)
81 fbbits[i] = mixed_color(fg, bg, bits[i]);
82 return fbbits;
85 void pad_put(int ch, int r, int c, int fg, int bg)
87 int sr = font_rows() * r;
88 int sc = font_cols() * c;
89 int frows = font_rows(), fcols = font_cols();
90 int i;
91 fbval_t *bits = bitmap(ch, fg, bg);
92 if (!bits)
93 fb_box(sr, sc, sr + frows, sc + fcols, color2fb(bg));
94 else
95 for (i = 0; i < frows; i++)
96 fb_set(sr + i, sc, bits + (i * fcols), fcols);
99 void pad_scroll(int sr, int nr, int n, int c)
101 fb_scroll(sr * font_rows(), nr * font_rows(),
102 n * font_rows(), color2fb(c));
105 void pad_blank(int c)
107 fb_box(0, 0, fb_rows(), fb_cols(), color2fb(c));
110 int pad_rows(void)
112 return rows;
115 int pad_cols(void)
117 return cols;
120 void pad_shown(void)
122 fb_cmap();