-
Notifications
You must be signed in to change notification settings - Fork 17
/
pad.c
264 lines (234 loc) · 5.6 KB
/
pad.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "conf.h"
#include "draw.h"
#include "fbpad.h"
static int fbroff, fbcoff, fbrows, fbcols;
static int rows, cols;
static int fnrows, fncols;
static int bpp;
static struct font *fonts[3];
static int gc_init(void);
static void gc_free(void);
static void gc_refresh(void);
int pad_init(void)
{
if (pad_font(FR, FI, FB))
return 1;
fnrows = font_rows(fonts[0]);
fncols = font_cols(fonts[0]);
if (gc_init())
return 1;
rows = fb_rows() / fnrows;
cols = fb_cols() / fncols;
bpp = FBM_BPP(fb_mode());
pad_conf(0, 0, fb_rows(), fb_cols());
return 0;
}
void pad_conf(int roff, int coff, int _rows, int _cols)
{
fbroff = roff;
fbcoff = coff;
fbrows = _rows;
fbcols = _cols;
rows = fbrows / fnrows;
cols = fbcols / fncols;
}
void pad_free(void)
{
gc_free();
font_free(fonts[0]);
font_free(fonts[1]);
font_free(fonts[2]);
}
#define CR(a) (((a) >> 16) & 0x0000ff)
#define CG(a) (((a) >> 8) & 0x0000ff)
#define CB(a) ((a) & 0x0000ff)
#define COLORMERGE(f, b, c) ((b) + (((f) - (b)) * (c) >> 8u))
static unsigned mixed_color(int fg, int bg, unsigned val)
{
unsigned char r = COLORMERGE(CR(fg), CR(bg), val);
unsigned char g = COLORMERGE(CG(fg), CG(bg), val);
unsigned char b = COLORMERGE(CB(fg), CB(bg), val);
return FB_VAL(r, g, b);
}
static unsigned color2fb(int c)
{
return FB_VAL(CR(c), CG(c), CB(c));
}
/* glyph bitmap cache: use CGLCNT lists of size CGLLEN each */
#define GCLCNT (1 << 7) /* glyph cache list count */
#define GCLLEN (1 << 4) /* glyph cache list length */
#define GCGLEN (fnrows * fncols * 4) /* bytes to store a glyph */
#define GCN (GCLCNT * GCLLEN) /* total glpyhs */
#define GCIDX(c) ((c) & (GCLCNT - 1))
static char *gc_mem; /* cached glyph's memory */
static int gc_next[GCLCNT]; /* the next slot to use in each list */
static int gc_glyph[GCN]; /* cached glyphs */
static int gc_bg[GCN];
static int gc_fg[GCN];
static int gc_init(void)
{
gc_mem = malloc(GCLCNT * GCLLEN * GCGLEN);
return !gc_mem;
}
static void gc_free(void)
{
free(gc_mem);
}
static char *gc_get(int c, int fg, int bg)
{
int idx = GCIDX(c) * GCLLEN;
int i;
for (i = idx; i < idx + GCLLEN; i++)
if (gc_glyph[i] == c && gc_fg[i] == fg && gc_bg[i] == bg)
return gc_mem + i * GCGLEN;
return NULL;
}
static char *gc_put(int c, int fg, int bg)
{
int lst = GCIDX(c);
int pos = gc_next[lst]++;
int idx = lst * GCLLEN + pos;
if (gc_next[lst] >= GCLLEN)
gc_next[lst] = 0;
gc_glyph[idx] = c;
gc_fg[idx] = fg;
gc_bg[idx] = bg;
return gc_mem + idx * GCGLEN;
}
static void gc_refresh(void)
{
memset(gc_next, 0, sizeof(gc_next));
memset(gc_glyph, 0, sizeof(gc_glyph));
}
static void bmp2fb(char *d, char *s, int fg, int bg, int nr, int nc)
{
int i, j, k;
for (i = 0; i < fnrows; i++) {
char *p = d + i * fncols * bpp;
for (j = 0; j < fncols; j++) {
unsigned v = i < nr && j < nc ?
(unsigned char) s[i * nc + j] : 0;
unsigned c = mixed_color(fg, bg, v);
for (k = 0; k < bpp; k++) /* little-endian */
*p++ = (c >> (k << 3)) & 0xff;
}
}
}
static char *ch2fb(int fn, int c, int fg, int bg)
{
static char bits[1024 * 4];
char *fbbits;
if (c < 0 || (c < 128 && (!isprint(c) || isspace(c))))
return NULL;
if ((fbbits = gc_get(c, fg, bg)))
return fbbits;
if (font_bitmap(fonts[fn], bits, c))
return NULL;
fbbits = gc_put(c, fg, bg);
bmp2fb(fbbits, bits, fg & FN_C, bg & FN_C,
font_rows(fonts[fn]), font_cols(fonts[fn]));
return fbbits;
}
static void fb_set(int r, int c, void *mem, int len)
{
memcpy(fb_mem(fbroff + r) + (fbcoff + c) * bpp, mem, len * bpp);
}
static char *rowbuf(unsigned c, int len)
{
static char row[32 * 1024];
char *p = row;
int i, k;
for (i = 0; i < len; i++)
for (k = 0; k < bpp; k++) /* little-endian */
*p++ = (c >> (k * 8)) & 0xff;
return row;
}
static void fb_box(int sr, int er, int sc, int ec, unsigned val)
{
char *row = rowbuf(val, ec - sc);
int i;
for (i = sr; i < er; i++)
fb_set(i, sc, row, ec - sc);
}
void pad_border(unsigned c, int wid)
{
int v = color2fb(c & FN_C);
if (fbroff < wid || fbcoff < wid)
return;
fb_box(-wid, 0, -wid, fbcols + wid, v);
fb_box(fbrows, fbrows + wid, -wid, fbcols + wid, v);
fb_box(-wid, fbrows + wid, -wid, 0, v);
fb_box(-wid, fbrows + wid, fbcols, fbcols + wid, v);
}
static int fnsel(int fg, int bg)
{
if ((fg | bg) & FN_B)
return fonts[2] ? 2 : 0;
if ((fg | bg) & FN_I)
return fonts[1] ? 1 : 0;
return 0;
}
void pad_put(int ch, int r, int c, int fg, int bg)
{
int sr = fnrows * r;
int sc = fncols * c;
char *bits;
int i;
bits = ch2fb(fnsel(fg, bg), ch, fg, bg);
if (!bits)
bits = ch2fb(0, ch, fg, bg);
if (!bits)
fb_box(sr, sr + fnrows, sc, sc + fncols, color2fb(bg & FN_C));
else
for (i = 0; i < fnrows; i++)
fb_set(sr + i, sc, bits + (i * fncols * bpp), fncols);
}
void pad_fill(int sr, int er, int sc, int ec, int c)
{
int fber = er >= 0 ? er * fnrows : fbrows;
int fbec = ec >= 0 ? ec * fncols : fbcols;
fb_box(sr * fnrows, fber, sc * fncols, fbec, color2fb(c & FN_C));
}
int pad_rows(void)
{
return rows;
}
int pad_cols(void)
{
return cols;
}
int pad_font(char *fr, char *fi, char *fb)
{
struct font *r = fr ? font_open(fr) : NULL;
if (!r)
return 1;
font_free(fonts[0]);
font_free(fonts[1]);
font_free(fonts[2]);
fonts[0] = r;
fonts[1] = fi ? font_open(fi) : NULL;
fonts[2] = fb ? font_open(fb) : NULL;
gc_refresh();
return 0;
}
char *pad_fbdev(void)
{
static char fbdev[1024];
snprintf(fbdev, sizeof(fbdev), "FBDEV=%s:%dx%d%+d%+d",
fb_dev(), fbcols, fbrows, fbcoff, fbroff);
return fbdev;
}
/* character height */
int pad_crows(void)
{
return fnrows;
}
/* character width */
int pad_ccols(void)
{
return fncols;
}