-
Notifications
You must be signed in to change notification settings - Fork 0
/
unix.c
1642 lines (1403 loc) · 35.1 KB
/
unix.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
unix.c
This file contains system specific information
(e.g. displaying routines, joystick access,
window controls etc ...)
*/
#ifdef UNIX
#ifdef GLIDE
#ifdef FRAMEBUFFER
#error: Do not try to make GLIDE & FRAMEBUFFER !!!
#endif
#if !defined JOYSTICK && !defined BSD_JOYSTICK
#error: Do not try to make GLIDE w/o JOYSTICK/BSD_JOYSTICK !!!
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "globals.h"
#include "sys.h"
#include "gameboy.h"
#include "z80.h"
#include "arplay.h"
#include "settings.h"
/* unix signal processing */
#define __USE_POSIX
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#ifndef GLIDE
/* X specific */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#ifdef MIT_SHM
#include <X11/extensions/XShm.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#endif
#ifdef WITH_XVIDEO
#include <X11/extensions/Xvlib.h>
#endif
#endif
#ifdef JOYSTICK
/* linux joystick device */
#include <linux/joystick.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#endif
#ifdef BSD_JOYSTICK
/* BSD joystick device */
#include <sys/joystick.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#endif
#ifdef FRAMEBUFFER
/* framebuffer device */
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <termios.h>
#include <linux/vt.h>
char *fbdevname="/dev/fb0";
struct fb_fix_screeninfo fb_finfo;
struct fb_var_screeninfo fb_vinfo,fb_vinfo_orig;
struct termios termsave;
struct vt_mode vt_modesave;
int fbdev,tty,fbgb_linesize,ttystate;
int usingfb;
char *fbmem,*fbgb;
#endif
#ifdef GLIDE
#include <glide.h>
#include <sst1vid.h>
#include <termios.h>
struct termios termsave;
#endif
#ifdef DIALOGLINK
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
char *sock_tcpsignature="cingb___init___socket";
int dialoglink,dlglink_status,sock_d,sock_nd,sock_desc;
int dlglink_init(void);
#endif
/* experimental ***************************************/
#ifdef SOUND
#include "sound.h"
#endif
#include "joypad.h"
#ifndef GLIDE
char *gbwintitle="cingb";
Display *display;
Visual *visual;
Window gbwin;
Colormap colormap;
int screen;
XColor *colors;
static XImage *gameboyscreen;
GC gbgc;
static size_t lcdbuffersize=0;
#ifdef MIT_SHM
/* XShm */
static Status use_xshm;
static XShmSegmentInfo shminfo;
#endif
#ifdef WITH_XVIDEO
static int use_xvideo=0;
static unsigned int xv_port=0, xv_format;
static XvImage *xv_gameboyscreen;
static int win_width=GB_LCDXSCREENSIZE;
static int win_height=GB_LCDYSCREENSIZE;
#endif
#endif
int joypaddev,usejoypad,usekeys,timerset,
resolution,doublesize,dsupdatenow,usingsound,bitmapbits;
volatile int refreshtimer;
struct itimerval itv;
int REDSHL,GREENSHL,BLUESHL;
int REDOFS,GREENOFS,BLUEOFS;
#ifdef JOYSTICK
struct JS_DATA_TYPE js;
#endif
#ifdef BSD_JOYSTICK
struct joystick js;
#endif
uchar GB_STD8BITPAL[]=
{
215,
172,
86,
0
};
unsigned long int GB_STDPAL[4];
void timerhandler(int signr)
{
#ifdef _LINUX
signal(SIGALRM, timerhandler);
#endif
refreshtimer++;
}
#ifdef FRAMEBUFFER
void FB_ClearScreen(void)
{
memset(fbmem,0,fb_vinfo.xres*fb_vinfo.yres*resolution/8);
}
void tty_switchhandler(int signal)
{
switch (signal) {
case SIGUSR1:
ttystate=0;
ioctl(tty, VT_RELDISP, 1);
break;
case SIGUSR2:
ttystate=1;
ioctl(tty, VT_RELDISP, VT_ACKACQ);
FB_ClearScreen();
break;
}
}
int InitFB(void)
{
struct termios term;
struct vt_mode vtm;
struct sigaction old,now;
fbdev=open(fbdevname,O_RDWR,0);
if (!fbdev) {
fprintf(stderr,"Could not open %s or an X connection.\n",fbdevname);
return 1;
}
if (ioctl(fbdev,FBIOGET_FSCREENINFO,&fb_finfo)<0) {
fprintf(stderr,"Error: FBIOGET_FSCREENINFO\n");
return 1;
}
if (ioctl(fbdev,FBIOGET_VSCREENINFO,&fb_vinfo)<0) {
fprintf(stderr,"Error: FBIOGET_VSCREENINFO\n");
return 1;
}
bitmapbits=resolution=fb_vinfo.bits_per_pixel;
if (resolution<16) {
fprintf(stderr,"Sorry: I don't support %d bits resolution, yet.\n",
resolution);
return 1;
}
memcpy(&fb_vinfo_orig,&fb_vinfo,sizeof(fb_vinfo_orig));
fbmem=mmap(NULL,fb_finfo.smem_len,PROT_WRITE,
MAP_SHARED,fbdev,0);
if (fbmem==MAP_FAILED) {
fprintf(stderr,"Error: mmap failed.\n");
return 1;
}
if (!smallview) {
fbgb=fbmem+(fb_vinfo.xres-GB_LCDXSCREENSIZE*2)*resolution/16+
(fb_vinfo.yres-GB_LCDYSCREENSIZE*2)/2*fb_finfo.line_length;
fbgb_linesize=GB_LCDXSCREENSIZE*2*resolution/8;
} else {
fbgb=fbmem+(fb_vinfo.xres-GB_LCDXSCREENSIZE)*resolution/16+
(fb_vinfo.yres-GB_LCDYSCREENSIZE)/2*fb_finfo.line_length;
fbgb_linesize=GB_LCDXSCREENSIZE*resolution/8;
}
if (fb_vinfo.xoffset!=0 || fb_vinfo.yoffset!=0) {
fb_vinfo.xoffset=0;
fb_vinfo.yoffset=0;
if (ioctl(fbdev,FBIOPAN_DISPLAY,&fb_vinfo)<0) {
fprintf(OUTSTREAM,"Error: FBIOPAN_DISPLAY\n");
return 1;
}
}
usingfb=1;
FB_ClearScreen();
logfile=fopen("cingb.log","w");
if (logfile==NULL) {
fprintf(stdout,"Could not open logfile (using stdout).\n");
logfile=stdout;
}
#ifdef DEBUG
printf("FB_TYPE : %d\n",fb_finfo.type);
printf("FB_VISUAL: %d\n",fb_finfo.visual);
printf("Resolution: %dx%dx%d\n",fb_vinfo.xres,fb_vinfo.yres,
fb_vinfo.bits_per_pixel);
printf("Pixel format: red : %d-%d\n",fb_vinfo.red.offset,
fb_vinfo.red.offset+fb_vinfo.red.length-1);
printf(" green: %d-%d\n",fb_vinfo.green.offset,
fb_vinfo.green.offset+fb_vinfo.green.length-1);
printf(" blue : %d-%d\n",fb_vinfo.blue.offset,
fb_vinfo.blue.offset+fb_vinfo.blue.length-1);
#endif
REDOFS =fb_vinfo.red.offset;
GREENOFS=fb_vinfo.green.offset;
BLUEOFS =fb_vinfo.blue.offset;
REDSHL =fb_vinfo.red.length-5;
GREENSHL=fb_vinfo.green.length-5;
BLUESHL =fb_vinfo.blue.length-5;
/* init terminal settings */
fprintf(OUTSTREAM,"Setting up terminal ... ");
tcgetattr(0,&termsave);
memcpy(&term,&termsave,sizeof(struct termios));
cfmakeraw(&term);
term.c_iflag|=BRKINT;
term.c_lflag|=ISIG;
tcsetattr(0,TCSANOW,&term);
out_ok;
/* tty init */
fprintf(OUTSTREAM,"Setting up tty device ... ");
if ((tty=open("/dev/tty",O_RDWR))<0) {
fprintf(OUTSTREAM,"Error opening tty device.\n");
return 1;
}
if (ioctl(tty,VT_GETMODE,&vt_modesave)<0) {
fprintf(OUTSTREAM,"Error writing to tty device.\n");
return 1;
}
memcpy(&vtm,&vt_modesave,sizeof(struct vt_mode));
now.sa_handler = tty_switchhandler;
now.sa_flags = 0;
now.sa_restorer = NULL;
sigemptyset(&now.sa_mask);
sigaction(SIGUSR1,&now,&old);
sigaction(SIGUSR2,&now,&old);
vtm.mode = VT_PROCESS;
vtm.waitv = 0;
vtm.relsig = SIGUSR1;
vtm.acqsig = SIGUSR2;
if (ioctl(tty,VT_SETMODE,&vtm)<0) {
fprintf(OUTSTREAM,"Error writing to tty device.\n");
return 1;
}
ttystate=1;
out_ok;
return 0;
}
void DoneFB(void)
{
/* clean up */
ioctl(tty,VT_SETMODE,&vt_modesave);
close(tty);
tcsetattr(0,TCSANOW,&termsave);
FB_ClearScreen();
if (ioctl(fbdev,FBIOPUT_VSCREENINFO,&fb_vinfo_orig)<0) {
fprintf(stderr,"Error: FBIOPUT_VSCREENINFO. (restoring state)\n");
}
if (munmap(fbmem,fb_finfo.smem_len)) {
fprintf(stderr,"Error: munmap failed.\n");
}
close(fbdev);
#ifdef VERBOSE
fprintf(OUTSTREAM,"Framebuffer device closed.\n");
#endif
fclose(logfile);
logfile=stdout;
}
#endif
void allocate_lcdbuffer(void) {
lcdbuffersize=doublesize ?
GB_XBUFFERSIZE*GB_LCDYSCREENSIZE*(bitmapbits/2) :
GB_XBUFFERSIZE*GB_LCDYSCREENSIZE*(bitmapbits/8);
fprintf(OUTSTREAM,"Allocating lcd buffer (%lu bytes%s)... ",
(size_t)lcdbuffersize, doublesize ? "; double-size" : "");
lcdbuffer=malloc(lcdbuffersize);
if (lcdbuffer==NULL) out_failed; else out_ok;
}
#ifdef GLIDE
/* **************************************** */
int initGlide(void)
{
GrHwConfiguration hwconfig;
struct termios term;
/* init terminal settings */
fprintf(OUTSTREAM,"Setting up terminal ... ");
tcgetattr(0,&termsave);
memcpy(&term,&termsave,sizeof(struct termios));
cfmakeraw(&term);
term.c_iflag|=BRKINT;
term.c_lflag|=ISIG;
tcsetattr(0,TCSANOW,&term);
out_ok;
fprintf(OUTSTREAM,"Initializing glide ... ");
grGlideInit();
fprintf(OUTSTREAM,"ok\nSearching glide hardware ...\n");
if (grSstQueryHardware(&hwconfig)==FXTRUE) {
grSstSelect(0);
if (grSstWinOpen(0,GR_RESOLUTION_640x480,GR_REFRESH_NONE,
GR_COLORFORMAT_ARGB,GR_ORIGIN_UPPER_LEFT,2,1)!=FXTRUE) {
fprintf(OUTSTREAM,
"*** Error: Failed to set the standard 640x480 mode.\n");
return 1;
}
} else {
fprintf(OUTSTREAM,"*** Error: glide hardware not found.\n");
return 1;
}
grColorCombine( GR_COMBINE_FUNCTION_LOCAL,
GR_COMBINE_FACTOR_NONE,
GR_COMBINE_LOCAL_CONSTANT,
GR_COMBINE_OTHER_NONE,
FXFALSE );
grBufferClear(0,0,0);
grBufferSwap(0);
grBufferClear(0,0,0);
grDisableAllEffects();
/* fixed sst1 format (there aren't others defined) */
bitmapbits=resolution=16;
REDSHL=BLUESHL=0;
GREENSHL=1;
REDOFS=11;
GREENOFS=5;
BLUEOFS=0;
out_ok;
allocate_lcdbuffer();
return 0;
}
void doneGlide(void)
{
grGlideShutdown();
tcsetattr(0,TCSANOW,&termsave);
}
#else
int InitXConnection(void)
{
XVisualInfo visualinfo;
XVisualInfo *retvinfo;
char *displayname=NULL;
int i,v;
#ifdef MIT_SHM
int major,minor;
Bool shared;
#endif
#ifdef WITH_XVIDEO
unsigned int xv_ver, xv_rel, xv_reqbase, xv_eventbase, xv_errbase,
adaptorcount;
XvAdaptorInfo *adaptorinfo = NULL;
#endif
display=XOpenDisplay(displayname);
if (display==NULL)
{
#if defined(FRAMEBUFFER) && !defined(DEBUG)
return InitFB();
#else
fprintf(stderr,"Cannot connect to X-Server.\n");
return 1;
#endif
}
#ifdef WITH_XVIDEO
if (check_xvideo) {
fprintf(OUTSTREAM, "Checking for XVideo extension ... ");
use_xvideo=(XvQueryExtension(display, &xv_ver, &xv_rel,
&xv_reqbase, &xv_eventbase, &xv_errbase)==Success);
if (!use_xvideo) out_failed;
else {
out_ok;
fprintf(OUTSTREAM, "Fetching XVideo adapters ... ");
if (XvQueryAdaptors(display,DefaultRootWindow(display),
&adaptorcount,&adaptorinfo)!=Success){
use_xvideo=0;
out_failed;
} else out_ok;
}
} else {
use_xvideo=0;
}
if (use_xvideo) {
int i;
int inuse=0;
xv_port=0;
fprintf(OUTSTREAM, "Opening XVideo port (adapters: %d) ... ",
adaptorcount);
for (i=0; i<adaptorcount && xv_port==0; i++) {
if ((adaptorinfo[i].type&XvInputMask) &&
(adaptorinfo[i].type&XvImageMask)) {
XvPortID id;
for (id=adaptorinfo[i].base_id;
id<adaptorinfo[i].base_id+
adaptorinfo[i].num_ports; id++)
if (XvGrabPort(display, id,
CurrentTime)==Success) {
xv_port = id;
break;
} inuse=1;
}
}
if (xv_port==0) {
if (inuse) fprintf(OUTSTREAM, "all ports in use ");
out_failed;
use_xvideo=0;
} else {
int i;
int formats=0;
XvImageFormatValues *fo;
fo = XvListImageFormats(display, xv_port, (int*)&formats);
xv_format=0;
for(i=0; i<formats; i++){
fprintf(OUTSTREAM, "Available XVideo image format: 0x%x (%4.4s) %s\n", fo[i].id,(char*)&fo[i].id, (fo[i].format == XvPacked) ? "packed" : "planar");
if (xv_format==0 &&
(fo[i].id==0x59565955|| fo[i].id==0x55595659)) {
xv_format = fo[i].id;
fprintf(OUTSTREAM, "y:%d, u:%d, v:%d (bits/pixel:%d)\n",
fo[i].y_sample_bits,
fo[i].u_sample_bits,
fo[i].v_sample_bits, fo[i].bits_per_pixel);
}
}
fprintf(OUTSTREAM, "Selecting XVideo output format ... ");
if (xv_format==0) {
XvUngrabPort(display, xv_port, CurrentTime);
use_xvideo=0;
out_failed;
} else out_ok;
}
}
#endif
#ifdef MIT_SHM
use_xshm=XShmQueryVersion (display, &major, &minor, &shared) && shared;
#endif
#ifdef FRAMEBUFFER
usingfb=0;
#endif
if (!smallview) doublesize=1;
screen=DefaultScreen(display);
visual=DefaultVisual(display, screen);
visualinfo.visualid=visual->visualid;
retvinfo=XGetVisualInfo(display, VisualIDMask, &visualinfo, &i);
if (retvinfo==NULL) {
fprintf(stderr, "Error getting VisualInfo for visual id %u.\n",
(unsigned int)visual->visualid);
exit(-1);
}
memcpy(&visualinfo, retvinfo, sizeof(XVisualInfo));
XFree(retvinfo);
bitmapbits=BitmapUnit(display);
if (use_xvideo) {
bitmapbits=32;
fprintf(OUTSTREAM,"Screen bitmap bits is %i, resolution is %i bits (YUV).\n", bitmapbits, resolution);
} else {
resolution = visualinfo.depth;
fprintf(OUTSTREAM,"Screen bitmap bits is %i, resolution is %i bits.\n", bitmapbits, resolution);
}
if (bitmapbits==8) {
/* create a palette with 6 shades for every color
this will be slower (multiplication) but it
looks far better than 4 shades. */
colormap=XCreateColormap(display, RootWindow(display,screen),
visual,AllocAll);
colors=(XColor *)calloc(256,sizeof(XColor));
for (i=0;i<256;i++)
{
colors[i].pixel=i;
colors[i].flags=DoRed|DoGreen|DoBlue;
if (i<216)
{
v=(i / 36) % 6;
colors[i].red=v*13107;
v=(i / 6) % 6;
colors[i].green=v*13107;
v=i % 6;
colors[i].blue=v*13107;
}
}
XStoreColors(display,colormap,colors,256);
} else {
REDOFS=BLUEOFS=GREENOFS=0;
REDSHL=BLUESHL=GREENSHL=0;
for (i=visualinfo.red_mask;!(i&1);i>>=1,REDOFS++);
for (;i&1;i>>=1,REDSHL++);
for (i=visualinfo.green_mask;!(i&1);i>>=1,GREENOFS++);
for (;i&1;i>>=1,GREENSHL++);
for (i=visualinfo.blue_mask;!(i&1);i>>=1,BLUEOFS++);
for (;i&1;i>>=1,BLUESHL++);
REDSHL-=5;GREENSHL-=5;BLUESHL-=5;
}
return 0;
}
#endif
/* signal hookfunc */
void DoBreak(int signum)
{
if (!ABORT_EMULATION) {
ABORT_EMULATION=1;
savestate();
tidyup();
exit(0);
}
}
#ifdef MIT_SHM
int xerrorhandler (Display *display, XErrorEvent *event) {
use_xshm=0;
return 0;
}
#endif
#ifndef GLIDE
int InitXRessources(void)
{
XSetWindowAttributes gbwinattr;
XTextProperty prop_gbwintitle;
XGCValues values;
#ifdef MIT_SHM
XErrorHandler old_handler;
#endif
fprintf(OUTSTREAM,"Window initialization ... ");
gbwinattr.event_mask=StructureNotifyMask|ExposureMask|KeyPressMask|
KeyReleaseMask;
gbwinattr.colormap=colormap;
gbwinattr.border_pixel=0;
gbwin=XCreateWindow(display,
RootWindow(display,screen),
100,50,
smallview ? GB_LCDXSCREENSIZE : GB_LCDXSCREENSIZE*2,
smallview ? GB_LCDYSCREENSIZE : GB_LCDYSCREENSIZE*2,
0,resolution,
InputOutput,
visual,
CWEventMask|
CWColormap|
CWBorderPixel,
&gbwinattr);
if (!gbwin) {
out_failed;
return 1;
} else out_ok;
values.background=0;
gbgc=XCreateGC(display,gbwin,GCBackground,&values);
XStringListToTextProperty(&gbwintitle,1,&prop_gbwintitle);
XSetWMName(display,gbwin,&prop_gbwintitle);
/*XSelectInput(display,gbwin,ExposureMask|KeyPressMask|KeyReleaseMask);*/
XMapRaised(display,gbwin);
XFlush(display);
dsupdatenow=0;
fprintf(OUTSTREAM,"Display initialized.\nCreating display image buffer ... ");
#ifdef MIT_SHM
tryagain:
if (use_xshm) {
#ifdef WITH_XVIDEO
if (use_xvideo) {
fprintf(OUTSTREAM,"(xv+shared) ");
xv_gameboyscreen=(XvImage *)XvShmCreateImage(display,
xv_port, xv_format, lcdbuffer,
GB_XBUFFERSIZE*2, GB_LCDYSCREENSIZE, &shminfo);
if (xv_gameboyscreen==NULL) {
fprintf(OUTSTREAM,"screen allocation failed for XVideo"
" trying X11\n Creating image buffer ... ");
use_xvideo=0;
goto tryagain;
}
}
else
#endif
{
fprintf(OUTSTREAM,"(shared) ");
gameboyscreen=XShmCreateImage(display,visual,resolution,
ZPixmap,NULL,&shminfo,
smallview ? GB_XBUFFERSIZE : GB_XBUFFERSIZE*2,
smallview ? GB_LCDYSCREENSIZE : GB_LCDYSCREENSIZE*2);
}
if (gameboyscreen==NULL) {
use_xshm=0;
out_failed;
fprintf(OUTSTREAM,"Trying normal mode ...\n");
goto tryagain;
}
shminfo.shmid=shmget(IPC_PRIVATE,gameboyscreen->bytes_per_line*
gameboyscreen->height,
IPC_CREAT|0777);
if (shminfo.shmid<0) {
out_failed;
fprintf(OUTSTREAM,"Trying normal mode ...\n");
XDestroyImage(gameboyscreen);
use_xshm=0;
goto tryagain;
}
lcdbuffer=shminfo.shmaddr=gameboyscreen->data=shmat(shminfo.shmid,0,0);
if (!lcdbuffer) {
out_failed;
fprintf(OUTSTREAM,"Trying normal mode ...\n");
XDestroyImage(gameboyscreen);
shmctl(shminfo.shmid,IPC_RMID,0);
use_xshm=0;
goto tryagain;
}
shminfo.readOnly=False;
old_handler=XSetErrorHandler(xerrorhandler);
XShmAttach(display,&shminfo);
XSync(display,False);
XShmPutImage(display,gbwin,gbgc,gameboyscreen,8,0,0,0,
1,1,0);
XSync(display,False);
XSetErrorHandler (old_handler);
if (!use_xshm) {
out_failed;
fprintf(OUTSTREAM,"Trying normal mode ...\n");
XDestroyImage(gameboyscreen);
shmdt(shminfo.shmaddr);
shmctl(shminfo.shmid,IPC_RMID,0);
goto tryagain;
} else fprintf(OUTSTREAM,"shared OK\n");
} else {
#endif
allocate_lcdbuffer();
gameboyscreen=NULL;
#ifdef WITH_XVIDEO
xv_gameboyscreen=NULL;
if (use_xvideo) {
fprintf(OUTSTREAM,"(xv) ");
xv_gameboyscreen=(XvImage *)XvCreateImage(display,
xv_port, xv_format, lcdbuffer,
GB_XBUFFERSIZE*2, GB_LCDYSCREENSIZE);
if (xv_gameboyscreen==NULL) {
fprintf(OUTSTREAM,"screen allocation failed for XVideo"
" trying X11\n Creating image buffer ... ");
use_xvideo=0;
free(lcdbuffer);
goto tryagain;
}
} else
#endif
{
fprintf(OUTSTREAM,"(x11) ");
gameboyscreen=XCreateImage(display,visual,resolution,ZPixmap,
0, lcdbuffer,
smallview ? GB_XBUFFERSIZE : GB_XBUFFERSIZE*2,
smallview ? GB_LCDYSCREENSIZE : GB_LCDYSCREENSIZE*2,
bitmapbits,0);
}
#ifdef MIT_SHM
}
#endif
fprintf(OUTSTREAM,"Preparing image ... ");
if (!use_xvideo) {
fprintf(OUTSTREAM,"X11 ... ");
if (gameboyscreen) out_ok; else {
out_failed;
return 1;
}
} else {
fprintf(OUTSTREAM,"XVideo (using %d bytes) ... ",
xv_gameboyscreen->data_size);
if (!xv_gameboyscreen) {
out_failed;
return 1;
}
if (xv_gameboyscreen->data_size>lcdbuffersize) {
out_failed;
return 1;
}
doublesize=0;
smallview=1;
out_ok;
}
#ifndef DEBUG
XAutoRepeatOff(display);
#endif
return 0;
}
#endif
int initsys(void)
{
int retval;
#ifdef JOYSTICK
int status;
struct JS_DATA_SAVE_TYPE_32 jsd;
#endif
doublesize=0;
#if defined(GLIDE) && !defined(DEBUG)
retval=initGlide();
#else
if ((retval=InitXConnection())!=0) return 1;
if (!retval
#ifdef FRAMEBUFFER
&& !usingfb
#endif
) retval=InitXRessources();
#endif
#ifdef MIT_SHM
if (use_xshm)
#endif
fprintf(OUTSTREAM,"Buffer allocation ... ");
if (lcdbuffer==NULL) {
out_failed;
retval=1;
} else out_ok;
/* B/W palette initialisation */
GB_STDPAL[0]=color_translate(0x7FFF);
GB_STDPAL[1]=color_translate(0x56B5);
GB_STDPAL[2]=color_translate(0x2D6B);
GB_STDPAL[3]=color_translate(0x0000);
usejoypad=0;
#if defined JOYSTICK || defined BSD_JOYSTICK
fprintf(OUTSTREAM,"Opening joypad device ... ");
joypaddev=open(joy_device,O_RDONLY);
if (joypaddev<0) {
out_failed;
} else {
out_ok;
#ifdef JOYSTICK
fprintf(OUTSTREAM,"Reading joystick info ... ");
status=ioctl(joypaddev,JS_GET_ALL,&jsd);
if (status<0) {
out_failed;
close(joypaddev);
} else {
out_ok;
usejoypad=1;
}
#endif
#ifdef BSD_JOYSTICK
usejoypad=1;
#endif
}
#endif
#ifdef GLIDE
if (!usejoypad) {
doneGlide();
return 1;
}
#endif
signal(SIGHUP,DoBreak);signal(SIGINT,DoBreak);
signal(SIGQUIT,DoBreak);signal(SIGTERM,DoBreak);
signal(SIGPIPE,DoBreak);
#ifdef __SVR4
sigset(SIGALRM,timerhandler);
#else
signal(SIGALRM,timerhandler);
#endif
itv.it_value.tv_usec=20000;
itv.it_value.tv_sec=0;
itv.it_interval.tv_usec=20000;
itv.it_interval.tv_sec=0;
timerset=setitimer(ITIMER_REAL,&itv,NULL);
refreshtimer=0;
if (timerset<0) {
fprintf(OUTSTREAM,"setitimer() failed./n");
fprintf(OUTSTREAM,"no real-time emulation will be available.\n");
}
#ifdef SOUND
if (producesound) {
fprintf(OUTSTREAM,"Initialize sound emulation ... \n");
usingsound=initsound();
fprintf(OUTSTREAM,"Sound %s\n",usingsound ? "OK" : "FAILED");
}
#endif
#ifdef DIALOGLINK
if (dialoglink)
dlglink_status=dlglink_init();
#endif
return retval;
}
#ifndef GLIDE
void DoneXConnection(void)
{
#ifndef DEBUG
XAutoRepeatOn(display);
#endif
#ifdef WITH_XVIDEO
fprintf(OUTSTREAM, "Freeing XVideo ressources ... ");
if (use_xvideo && xv_port) {
XvUngrabPort(display, xv_port, CurrentTime);
}
#endif
#ifdef VERBOSE
fprintf(OUTSTREAM,"Destroying window and freeing memory ... ");
#endif
#ifdef MIT_SHM
if (use_xshm) {
XShmDetach(display,&shminfo);
}
#endif
#ifdef WITH_XVIDEO
if (!use_xvideo)
#endif
XDestroyImage(gameboyscreen);
#ifdef MIT_SHM
if (use_xshm) {
shmdt(shminfo.shmaddr);
shmctl(shminfo.shmid,IPC_RMID,0);
}
#endif
XUnmapWindow(display,gbwin);
XDestroyWindow(display,gbwin);
XFreeGC(display,gbgc);
if (bitmapbits==8) {
XFreeColormap(display,colormap);
free(colors);
}
#ifdef VERBOSE
out_ok;
#endif
#ifdef VERBOSE
fprintf(OUTSTREAM,"Shutting down X-connection ... ");fflush(stdout);
#endif
XCloseDisplay(display);
#ifdef VERBOSE
out_ok;
#endif
}
#endif
void donesys(void)
{
#ifdef DIALOGLINK
switch (dlglink_status) {
case 1:
close(sock_nd);
close(sock_d);
break;
case 2:
close(sock_d);
break;