-
Notifications
You must be signed in to change notification settings - Fork 2
/
fbsd_vid.c
396 lines (349 loc) · 9 KB
/
fbsd_vid.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* ---------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp): Maxim
* Sobolev <[email protected]> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet
* some day, and you think this stuff is worth it, you can buy me a beer in
* return.
*
* Maxim Sobolev
* ---------------------------------------------------------------------------
*/
#include <sys/fbio.h>
#include <sys/kbio.h>
#include <sys/consio.h>
#include <vgl.h>
#include <signal.h>
#include <osreldate.h>
#include "def.h"
#include "hardware.h"
#include "title_gz.h"
extern Uint3 *vgatable[];
extern Uint3 *ascii2vga[];
Uint3 **sprites = vgatable;
Uint3 **alphas = ascii2vga;
Sint4 xratio = 2;
Sint4 yratio = 2;
Sint4 yoffset = 0;
Sint4 hratio = 2;
Sint4 wratio = 2;
#define virt2scrx(x) (x*xratio)
#define virt2scry(y) (y*yratio+yoffset)
#define virt2scrw(w) (w*wratio)
#define virt2scrh(h) (h*hratio)
typedef Uint3 palette[3];
/* palette1, normal intensity */
palette vga16_pal1[] = \
{{0, 0, 0}, {225, 0, 0}, {0, 225, 0}, {225, 225, 0}, {0, 0, 225}, {225, 0, 225}, {0, 210, 225}, \
{225, 225, 225}, {210, 210, 210}, {255, 0, 0}, {0, 255, 0}, {255, 255, 0}, {0, 0, 255}, {255, 0, 255}, \
{0, 255, 255}, {255, 255, 255}};
/* palette1, high intensity */
palette vga16_pal1i[] = \
{{0, 0, 0}, {255, 0, 0}, {0, 255, 0}, {255, 255, 0}, {0, 0, 255}, {255, 0, 255}, {0, 225, 255}, \
{240, 240, 240}, {225, 225, 225}, {255, 225, 225}, {225, 255, 225}, {255, 255, 225}, \
{225, 225, 255}, {255, 225, 255}, {225, 255, 255}, {255, 255, 255}};
/* palette2, normal intensity */
palette vga16_pal2[] = \
{{0, 0, 0}, {0, 225, 0}, {0, 0, 225}, {0, 210, 225}, {225, 0, 0}, {225, 225, 0}, {225, 0, 225}, \
{225, 225, 225}, {210, 210, 210}, {0, 255, 0}, {0, 0, 255}, {0, 255, 255}, {255, 0, 0}, {255, 255, 0}, \
{255, 0, 255}, {255, 255, 255}};
/* palette2, high intensity */
palette vga16_pal2i[] = \
{{0, 0, 0}, {0, 255, 0}, {0, 0, 255}, {0, 225, 255}, {255, 0, 0}, {255, 255, 0}, {255, 0, 255}, \
{240, 240, 240}, {225, 225, 225}, {225, 255, 225}, {225, 225, 255}, {225, 255, 255}, \
{255, 225, 225}, {255, 255, 225}, {255, 225, 255}, {255, 255, 255}};
palette *npalettes[] = {vga16_pal1, vga16_pal2};
palette *ipalettes[] = {vga16_pal1i, vga16_pal2i};
Sint4 currpal = 0;
#if __FreeBSD_version < 400000
VGLBitmap *
VGLBitmapCreate(int type, int xsize, int ysize, byte * bits)
{
VGLBitmap *object;
if (type != MEMBUF)
return NULL;
if (xsize < 0 || ysize < 0)
return NULL;
object = (VGLBitmap *) malloc(sizeof(*object));
if (object == NULL)
return NULL;
object->Type = type;
object->Xsize = xsize;
object->Ysize = ysize;
object->Bitmap = bits;
return object;
}
void
VGLBitmapDestroy(VGLBitmap * object)
{
if (object->Bitmap)
free(object->Bitmap);
free(object);
}
int
VGLBitmapAllocateBits(VGLBitmap * object)
{
object->Bitmap = (byte *) malloc(object->Xsize * object->Ysize);
if (object->Bitmap == NULL)
return -1;
return 0;
}
#endif
VGLBitmap *
ch2bmap(Uint3 * sprite, Sint4 w, Sint4 h)
{
Sint4 realw, realh;
VGLBitmap *tmp;
realw = virt2scrw(w * 4);
realh = virt2scrh(h);
tmp = VGLBitmapCreate(MEMBUF, realw, realh, NULL);
tmp->Bitmap = sprite;
return (tmp);
}
void
graphicsoff(void)
{
VGLEnd();
}
void
vgainit(void)
{
if (geteuid() != 0) {
fprintf(stderr, "The current graphics console architecture only permits " \
"super-user to access it, therefore you either have to obtain such permissions" \
"or ask your sysadmin to put set-user-id on digger executable.\n");
exit(1);
}
if (VGLInit(SW_VESA_CG640x400) != 0) {
fprintf(stderr, "WARNING! Could not initialise VESA mode. " \
"Trying to fallback to the VGA 640x480 mode\n");
if (VGLInit(SW_CG640x480) == 0)
yoffset = 40; /* Center the image */
else {
fprintf(stderr, "WARNING! Could not initialise VGA mode either. " \
"Please check your kernel.\n");
exit(1);
}
}
/*
* Since the VGL library doesn't provide a default way to restore console
* and keyboard after uncatched by the program signal, we should try to
* catch at least what we could catch and pray to God that he would not
* send SIGKILL to us.
*/
signal(SIGHUP, catcher);
signal(SIGINT, catcher);
signal(SIGQUIT, catcher);
signal(SIGABRT, catcher);
signal(SIGTERM, catcher);
signal(SIGSEGV, catcher);
signal(SIGBUS, catcher);
signal(SIGILL, catcher);
}
void
vgaclear(void)
{
VGLClear(VGLDisplay, 0);
}
void
setpal(palette * pal)
{
Sint4 i;
for (i = 0; i < 16; i++) {
VGLSetPaletteIndex(i, (pal[i])[2], \
(pal[i])[1], (pal[i])[0]);
}
}
void
vgainten(Sint4 inten)
{
if (inten == 1)
setpal(ipalettes[currpal]);
else
setpal(npalettes[currpal]);
}
void
vgapal(Sint4 pal)
{
setpal(npalettes[pal]);
currpal = pal;
}
void
vgaputi(Sint4 x, Sint4 y, Uint3 * p, Sint4 w, Sint4 h)
{
VGLBitmap *tmp;
Sint4 realx, realy, realh, realw;
realx = virt2scrx(x);
realy = virt2scry(y);
realw = virt2scrw(w * 4);
realh = virt2scrh(h);
memcpy(&tmp, p, (sizeof(VGLBitmap *)));
VGLBitmapCopy(tmp, 0, 0, VGLDisplay, realx, realy, realw, realh);
}
void
vgageti(Sint4 x, Sint4 y, Uint3 * p, Sint4 w, Sint4 h)
{
VGLBitmap *tmp;
Sint4 realx, realy, realh, realw;
memcpy(&tmp, p, (sizeof(VGLBitmap *)));
if (tmp != NULL)
VGLBitmapDestroy(tmp); /* Destroy previously allocated bitmap */
realx = virt2scrx(x);
realy = virt2scry(y);
realw = virt2scrw(w * 4);
realh = virt2scrh(h);
tmp = VGLBitmapCreate(MEMBUF, realw, realh, NULL);
VGLBitmapAllocateBits(tmp);
VGLClear(tmp, 0);
VGLBitmapCopy(VGLDisplay, realx, realy, tmp, 0, 0, realw, realh);
memcpy(p, &tmp, (sizeof(VGLBitmap *)));
}
Sint4
vgagetpix(Sint4 x, Sint4 y)
{
VGLBitmap *tmp = NULL;
Uint4 xi, yi;
Uint4 i = 0;
Sint4 rval = 0;
if ((x > 319) || (y > 199))
return (0xff);
vgageti(x, y, (Uint3 *) & tmp, 1, 1);
for (yi = 0; yi < tmp->Ysize; yi++)
for (xi = 0; xi < tmp->Xsize; xi++)
if (tmp->Bitmap[i++])
rval |= 0x80 >> xi;
VGLBitmapDestroy(tmp);
return (rval & 0xee);
}
void
vgaputim(Sint4 x, Sint4 y, Sint4 ch, Sint4 w, Sint4 h)
{
VGLBitmap *tmp;
VGLBitmap *mask;
VGLBitmap *scr = NULL;
Sint4 realsize;
Sint4 i;
tmp = ch2bmap(sprites[ch * 2], w, h);
mask = ch2bmap(sprites[ch * 2 + 1], w, h);
vgageti(x, y, (Uint3 *) & scr, w, h);
realsize = scr->Xsize * scr->Ysize;
for (i = 0; i < realsize; i++)
if (tmp->Bitmap[i] != 0xff)
scr->Bitmap[i] = (scr->Bitmap[i] & mask->Bitmap[i]) | \
tmp->Bitmap[i];
vgaputi(x, y, (Uint3 *) & scr, w, h);
tmp->Bitmap = NULL; /* We should NULL'ify these ppointers, or the
* VGLBitmapDestroy */
mask->Bitmap = NULL; /* will shoot itself in the foot by trying to
* dellocate statically */
VGLBitmapDestroy(tmp); /* allocated arrays */
VGLBitmapDestroy(mask);
VGLBitmapDestroy(scr);
}
void
vgawrite(Sint4 x, Sint4 y, Sint4 ch, Sint4 c)
{
VGLBitmap *tmp;
Uint3 *copy;
Uint3 color;
Sint4 w = 3, h = 12, size;
Sint4 i;
if(((ch - 32) >= 0x5f) || (ch < 32))
return;
tmp = ch2bmap(alphas[ch - 32], w, h);
size = tmp->Xsize * tmp->Ysize;
copy = malloc(size);
memcpy(copy, tmp->Bitmap, size);
for (i = size; i != 0;) {
i--;
color = copy[i];
if (color == 10) {
if (c == 2)
color = 12;
else if (c == 3)
color = 14;
} else {
if (color == 12) {
if (c == 1)
color = 2;
else if (c == 2)
color = 4;
else if (c == 3)
color = 6;
}
}
copy[i] = color;
}
tmp->Bitmap = copy;
vgaputi(x, y, (Uint3 *) & tmp, w, h);
VGLBitmapDestroy(tmp);
}
void
vgatitle(void)
{
VGLBitmap *tmp = NULL;
vgageti(0, 0, (Uint3 *) & tmp, 80, 200);
gettitle(tmp->Bitmap);
vgaputi(0, 0, (Uint3 *) & tmp, 80, 200);
VGLBitmapDestroy(tmp);
}
void
gretrace(void)
{
FIXME("gretrace called");
}
void
savescreen(void)
{
FILE *f;
int i;
f = fopen("screen.saw", "w");
for (i = 0; i < (VGLDisplay->Xsize * VGLDisplay->Ysize); i++)
fputc(VGLDisplay->Bitmap[i], f);
fclose(f);
}
/*
* Depreciated functions, necessary only to avoid "Undefined symbol:..."
* compiler errors.
*/
void
cgainit(void)
{
}
void
cgaclear(void)
{
}
void
cgatitle(void)
{
}
void
cgawrite(Sint4 x, Sint4 y, Sint4 ch, Sint4 c)
{
}
void
cgaputim(Sint4 x, Sint4 y, Sint4 ch, Sint4 w, Sint4 h)
{
}
void
cgageti(Sint4 x, Sint4 y, Uint3 * p, Sint4 w, Sint4 h)
{
}
void
cgaputi(Sint4 x, Sint4 y, Uint3 * p, Sint4 w, Sint4 h)
{
}
void
cgapal(Sint4 pal)
{
}
void
cgainten(Sint4 inten)
{
}
Sint4
cgagetpix(Sint4 x, Sint4 y)
{
return (0);
}