-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvi_xlib.c
741 lines (629 loc) · 14.6 KB
/
vi_xlib.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#include "wl_def.h"
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>
#include <X11/Xatom.h>
#include <X11/extensions/XShm.h>
byte *gfxbuf = NULL;
byte *disbuf = NULL;
Display *dpy;
myint screen;
Window root, win;
XVisualInfo *vi;
GC gc;
XImage *img;
Colormap cmap;
Atom wmDeleteWindow;
XShmSegmentInfo shminfo;
XColor clr[256];
myint indexmode;
myint shmmode;
static byte cpal[768];
static word spal[768];
static dword ipal[768];
static myint ByteOrder; /* 0 = LSBFirst, 1 = MSBFirst */
static myint NeedSwap;
myint MyDepth;
void GetVisual()
{
XVisualInfo vitemp;
myint i, numVisuals;
vitemp.screen = screen;
vitemp.depth = 8;
vitemp.class = PseudoColor;
vi = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask |
VisualClassMask, &vitemp, &numVisuals);
if (vi && (numVisuals > 0)) {
indexmode = 1;
cmap = XCreateColormap(dpy, root, vi->visual, AllocAll);
for (i = 0; i < 256; i++) {
clr[i].pixel = i;
clr[i].flags = DoRed|DoGreen|DoBlue;
}
return;
}
vitemp.depth = 15;
vitemp.class = TrueColor;
vitemp.red_mask = 0x7C00;
vitemp.green_mask = 0x03E0;
vitemp.blue_mask = 0x001F;
vi = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask |
VisualClassMask, &vitemp, &numVisuals);
if (vi && (numVisuals > 0)) {
indexmode = 0;
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
return;
}
vitemp.depth = 16;
vitemp.red_mask = 0xF800;
vitemp.green_mask = 0x07E0;
vitemp.blue_mask = 0x001F;
vi = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask |
VisualClassMask, &vitemp, &numVisuals);
if (vi && (numVisuals > 0)) {
indexmode = 0;
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
return;
}
vitemp.depth = 24;
vitemp.red_mask = 0xFF0000;
vitemp.green_mask = 0x00FF00;
vitemp.blue_mask = 0x0000FF;
vi = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask |
VisualClassMask, &vitemp, &numVisuals);
if (vi && (numVisuals > 0)) {
indexmode = 0;
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
return;
}
vitemp.depth = 32;
vi = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask |
VisualClassMask, &vitemp, &numVisuals);
if (vi && (numVisuals > 0)) {
indexmode = 0;
cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
return;
}
Quit("No usable visual found!");
}
myint GetBPP()
{
switch(img->depth) {
case 4:
break;
case 8:
if (img->bits_per_pixel == 8)
return 8;
break;
case 15:
if (img->bits_per_pixel == 16)
return 15;
break;
case 16:
if (img->bits_per_pixel == 16)
return 16;
break;
case 24:
if (img->bits_per_pixel == 24)
return 24;
else
return 32;
break;
case 32:
if (img->bits_per_pixel == 32)
return 32;
break;
}
fprintf(stderr, "Unsupported combination of depth %d and bits per pixel %d...\n", img->depth, img->bits_per_pixel);
fprintf(stderr, "pad = %d, unit = %d, bits = %d, bpl = %d, rgb = %d, depth = %d (%d)\n", img->bitmap_pad, img->bitmap_unit, img->bits_per_pixel, img->bytes_per_line, vi->bits_per_rgb, img->depth, vi->depth);
exit(EXIT_FAILURE);
}
void VL_Startup()
{
XSetWindowAttributes attr;
XSizeHints sizehints;
XGCValues gcvalues;
Pixmap bitmap;
Cursor cursor;
XColor bg = { 0 };
XColor fg = { 0 };
char data[8] = { 0x01 };
char *disp;
myint attrmask;
disp = getenv("DISPLAY");
dpy = XOpenDisplay(disp);
if (dpy == NULL) {
fprintf(stderr, "Unable to open display %s!\n", XDisplayName(disp));
exit(EXIT_FAILURE);
}
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
GetVisual();
attr.colormap = cmap;
attr.event_mask = KeyPressMask | KeyReleaseMask | ExposureMask
| FocusChangeMask | StructureNotifyMask;
attrmask = CWColormap | CWEventMask;
win = XCreateWindow(dpy, root, 0, 0, vwidth, vheight, 0, CopyFromParent,
InputOutput, vi->visual, attrmask, &attr);
if (win == None) {
Quit("Unable to create window!");
}
gcvalues.foreground = BlackPixel(dpy, screen);
gcvalues.background = WhitePixel(dpy, screen);
gc = XCreateGC(dpy, win, GCForeground | GCBackground, &gcvalues);
sizehints.min_width = vwidth;
sizehints.min_height = vheight;
sizehints.max_width = vwidth;
sizehints.max_height = vheight;
sizehints.base_width = vwidth;
sizehints.base_height = vheight;
sizehints.flags = PMinSize | PMaxSize | PBaseSize;
XSetWMProperties(dpy, win, NULL, NULL, _argv, _argc, &sizehints, None, None);
XStoreName(dpy, win, GAMENAME);
XSetIconName(dpy, win, GAMENAME);
wmDeleteWindow = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dpy, win, &wmDeleteWindow, 1);
bitmap = XCreateBitmapFromData(dpy, win, data, 8, 8);
cursor = XCreatePixmapCursor(dpy, bitmap, bitmap, &fg, &bg, 0, 0);
XDefineCursor(dpy, win, cursor);
shmmode = 0;
if (
#ifndef EMBEDDED
!MS_CheckParm("noshm") &&
#endif
(XShmQueryExtension(dpy) == True)) {
img = XShmCreateImage(dpy, vi->visual, vi->depth, ZPixmap,
NULL, &shminfo, vwidth, vheight);
shminfo.shmid = shmget(IPC_PRIVATE, img->bytes_per_line * img->height, IPC_CREAT | 0777);
shminfo.shmaddr = img->data = shmat(shminfo.shmid, 0, 0);
shminfo.readOnly = False;
disbuf = (byte *)img->data;
if (indexmode)
gfxbuf = disbuf;
else
gfxbuf = malloc(vwidth * vheight * 1);
if (XShmAttach(dpy, &shminfo) == True) {
printf("Using XShm Extension...\n");
shmmode = 1;
shmctl(shminfo.shmid, IPC_RMID, 0);
} else {
printf("Error with XShm...\n");
}
}
if (img == NULL) {
XImage *imgtmp;
char *gb;
printf("Falling back on XImage...\n");
gb = (char *)malloc(320);
imgtmp = XCreateImage(dpy, vi->visual, vi->depth, ZPixmap, 0,
gb, 16, 1, 8, 16*4);
if (gfxbuf == NULL)
gfxbuf = malloc(vwidth * vheight * 1);
if (indexmode)
disbuf = gfxbuf;
else
disbuf = malloc(vwidth * vheight * (imgtmp->bits_per_pixel / 8));
img = XCreateImage(dpy, vi->visual, vi->depth, ZPixmap, 0,
(char *)disbuf, vwidth, vheight, 8, 0 * (imgtmp->bits_per_pixel / 8));
if (img == NULL) {
Quit("XCreateImage returned NULL");
}
XInitImage(img);
XDestroyImage(imgtmp);
}
MyDepth = GetBPP();
#if BYTE_ORDER == BIG_ENDIAN
ByteOrder = 1;
#else
ByteOrder = 0;
#endif
NeedSwap = (ByteOrder != img->byte_order);
XMapRaised(dpy, win);
}
void VL_Shutdown()
{
if ( !shmmode && (gfxbuf != NULL) ) {
free(gfxbuf);
gfxbuf = NULL;
}
if ( shmmode && !indexmode && (gfxbuf != NULL) ) {
free(gfxbuf);
gfxbuf = NULL;
}
if (shmmode) {
XShmDetach(dpy, &shminfo);
XDestroyImage(img);
shmdt(shminfo.shmaddr);
shmctl(shminfo.shmid, IPC_RMID, 0);
} else if ( (indexmode == 0) && (disbuf != NULL) ) {
free(disbuf);
disbuf = NULL;
}
}
void VL_WaitVBL(myint vbls)
{
/* hack - but it works for me */
long last = get_TimeCount() + vbls;
while (last > get_TimeCount()) ;
}
void VW_UpdateScreen()
{
dword *ptri;
word *ptrs;
byte *ptrb;
int size;
myint i;
#if 0
for (i = 0; i < 64; i++) {
gfxbuf[i] = i;
gfxbuf[i*128 + 1] = i;
gfxbuf[i*128 + 2] = i;
gfxbuf[i*128 + 3] = i;
gfxbuf[i*128 + 4] = i + 0x40;
gfxbuf[i*128 + 5] = i + 0x40;
gfxbuf[i*128 + 6] = i + 0x40;
gfxbuf[i*128 + 7] = i + 0x40;
gfxbuf[i*128 + 8] = i + 0x80;
gfxbuf[i*128 + 9] = i + 0x80;
gfxbuf[i*128 + 10] = i + 0x80;
gfxbuf[i*128 + 11] = i + 0x80;
gfxbuf[i*128 + 12] = i + 0xc0;
gfxbuf[i*128 + 13] = i + 0xc0;
gfxbuf[i*128 + 14] = i + 0xc0;
gfxbuf[i*128 + 15] = i + 0xc0;
}
#endif
#if 0
for (i = 0; i < 128 * 64; i++) {
gfxbuf[i] = 0x1f - pal4bit[gfxbuf[i]];
}
#endif
size = vwidth * vheight;
if (indexmode == 0) {
switch(MyDepth) {
case 15:
case 16:
ptrs = (word *)disbuf;
for (i = 0; i < size; i++) {
*ptrs = spal[gfxbuf[i]];
ptrs++;
}
break;
case 24: /* Endian Safe? Untested. */
ptrb = disbuf;
for (i = 0; i < size; i++) {
*ptrb = cpal[gfxbuf[i]*3+2] << 2; ptrb++;
*ptrb = cpal[gfxbuf[i]*3+1] << 2; ptrb++;
*ptrb = cpal[gfxbuf[i]*3+0] << 2; ptrb++;
}
break;
case 32:
ptri = (dword *)disbuf;
for (i = 0; i < size; i++) {
*ptri = ipal[gfxbuf[i]];
ptri++;
}
break;
default:
break;
/* ... */
}
}
if (shmmode)
XShmPutImage(dpy, win, gc, img, 0, 0, 0, 0, vwidth, vheight, False);
else
XPutImage(dpy, win, gc, img, 0, 0, 0, 0, vwidth, vheight);
}
void keyboard_handler(myint code, myint press);
static myint XKeysymToScancode(KeySym keysym);
static void HandleXEvents()
{
XEvent event;
if (XPending(dpy)) {
do {
XNextEvent(dpy, &event);
switch(event.type) {
case KeyPress:
keyboard_handler(XKeysymToScancode(XKeycodeToKeysym(dpy, event.xkey.keycode, 0)), 1);
break;
case KeyRelease:
keyboard_handler(XKeysymToScancode(XKeycodeToKeysym(dpy, event.xkey.keycode, 0)), 0);
break;
case Expose:
VW_UpdateScreen();
break;
case ClientMessage:
if (event.xclient.data.l[0] == wmDeleteWindow)
Quit(NULL);
break;
case ConfigureNotify:
break;
case FocusIn:
break;
case FocusOut:
break;
default:
break;
}
} while (XPending(dpy));
}
}
/* ======================================================================== */
/*
=================
=
= VL_SetPalette
=
=================
*/
void VL_SetPalette(const byte *palette)
{
myint i;
VL_WaitVBL(1);
if (indexmode) {
for (i = 0; i < 256; i++) {
clr[i].red = palette[i*3+0] << 10;
clr[i].green = palette[i*3+1] << 10;
clr[i].blue = palette[i*3+2] << 10;
}
XStoreColors(dpy, cmap, clr, 256);
} else {
memcpy(cpal, palette, 768);
for (i = 0; i < 256; i++) {
/* TODO: this should really use the visual for creating the pixel */
switch(MyDepth) {
case 8:
break;
case 15: /* Endian Safe? Untested. */
spal[i] = ((palette[i*3+0] >> 1) << 10) | ((palette[i*3+1] >> 1) << 5) | ((palette[i*3+2] >> 1) << 0);
if (NeedSwap)
spal[i] = SwapInt16(spal[i]);
break;
case 16:
spal[i] = ((palette[i*3+0] >> 1) << 11) | ((palette[i*3+1] >> 0) << 5) | ((palette[i*3+2] >> 1) << 0);
if (NeedSwap)
spal[i] = SwapInt16(spal[i]);
break;
case 32:
ipal[i] = (palette[i*3+0] << 18) | (palette[i*3+1] << 10) | (palette[i*3+2] << 2);
if (NeedSwap)
ipal[i] = SwapInt32(ipal[i]);
break;
}
}
VW_UpdateScreen();
}
}
/*
=================
=
= VL_GetPalette
=
=================
*/
void VL_GetPalette(byte *palette)
{
myint i;
if (indexmode) {
for (i = 0; i < 256; i++) {
palette[i*3+0] = clr[i].red >> 10;
palette[i*3+1] = clr[i].green >> 10;
palette[i*3+2] = clr[i].blue >> 10;
}
} else {
memcpy(palette, cpal, 768);
}
}
myint main(myint argc, char *argv[])
{
#ifdef EMBEDDED
vwidth = 128;
vheight = 96;
#else
vwidth = 320;
vheight = 200;
#endif
return WolfMain(argc, argv);
}
/*
==========================
=
= Quit
=
==========================
*/
void DisplayTextSplash(const byte *text, myint l);
void Quit(const char *error)
{
memptr screen = NULL;
myint l = 0;
#ifdef ENABLE_QUITMSG
if (!error || !*error) {
CA_CacheGrChunk(ORDERSCREEN);
screen = grsegs[ORDERSCREEN];
l = 24;
WriteConfig();
} else if (error) {
CA_CacheGrChunk(ERRORSCREEN);
screen = grsegs[ERRORSCREEN];
l = 7;
}
#endif
ShutdownId();
#ifndef EMBEDDED
if (screen) {
DisplayTextSplash(screen, l);
}
#endif
if (error && *error) {
fprintf(stderr, "Quit: %s\n", error);
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
static myint XKeysymToScancode(KeySym keysym)
{
switch (keysym) {
case XK_1:
return sc_1;
case XK_2:
return sc_2;
case XK_3:
return sc_3;
case XK_4:
return sc_4;
case XK_5:
return sc_5;
case XK_6:
return sc_6;
case XK_a:
return sc_A;
case XK_b:
return sc_B;
case XK_c:
return sc_C;
case XK_d:
return sc_D;
case XK_g:
return sc_G;
case XK_h:
return sc_H;
case XK_i:
return sc_I;
case XK_l:
return sc_L;
case XK_m:
return sc_M;
case XK_n:
return sc_N;
case XK_q:
return sc_Q;
case XK_s:
return sc_S;
case XK_t:
return sc_T;
case XK_y:
return sc_Y;
case XK_F1:
return sc_F1;
case XK_F2:
return sc_F2;
case XK_F3:
return sc_F3;
case XK_F10:
return sc_F10;
case XK_F11:
return sc_F11;
case XK_F12:
return sc_F12;
case XK_Left:
case XK_KP_Left:
return sc_LeftArrow;
case XK_Right:
case XK_KP_Right:
return sc_RightArrow;
case XK_Up:
case XK_KP_Up:
return sc_UpArrow;
case XK_Down:
case XK_KP_Down:
return sc_DownArrow;
case XK_Control_L:
case XK_Control_R:
return sc_Control;
case XK_Alt_L:
case XK_Alt_R:
return sc_Alt;
case XK_Shift_L:
return sc_LShift;
case XK_Shift_R:
return sc_RShift;
case XK_Escape:
return sc_Escape;
case XK_space:
case XK_KP_Space:
return sc_Space;
case XK_KP_Enter:
case XK_Return:
return sc_Enter;
case XK_Tab:
return sc_Tab;
case XK_BackSpace:
return sc_BackSpace;
case XK_Pause:
return 0xE1;
default:
printf("unknown: %s\n", XKeysymToString(keysym));
return sc_None;
}
}
byte IN_MouseButtons()
{
return 0;
}
void IN_GetMouseDelta(myint *dx, myint *dy)
{
}
#ifdef ENABLE_JOYSTICK
/*
===================
=
= IN_JoyButtons
=
===================
*/
byte IN_JoyButtons()
{
return 0;
}
///////////////////////////////////////////////////////////////////////////
//
// IN_GetJoyAbs() - Reads the absolute position of the specified joystick
//
///////////////////////////////////////////////////////////////////////////
void IN_GetJoyAbs(word joy,word *xp,word *yp)
{
*xp = 0;
*yp = 0;
}
///////////////////////////////////////////////////////////////////////////
//
// INL_GetJoyDelta() - Returns the relative movement of the specified
// joystick (from +/-127)
//
///////////////////////////////////////////////////////////////////////////
void INL_GetJoyDelta(word joy,myint *dx,myint *dy)
{
*dx = 0;
*dy = 0;
}
///////////////////////////////////////////////////////////////////////////
//
// INL_GetJoyButtons() - Returns the button status of the specified
// joystick
//
///////////////////////////////////////////////////////////////////////////
word INL_GetJoyButtons(word joy)
{
return 0;
}
///////////////////////////////////////////////////////////////////////////
//
// IN_SetupJoy() - Sets up thresholding values and calls INL_SetJoyScale()
// to set up scaling values
//
///////////////////////////////////////////////////////////////////////////
void IN_SetupJoy(word joy,word minx,word maxx,word miny,word maxy)
{
}
#endif
void INL_Update()
{
HandleXEvents();
}