forked from CeRiAl/Xhomer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm_x11.c
1938 lines (1492 loc) · 47.4 KB
/
term_x11.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
/* term_x11.c: X-windows terminal code (screen update and keyboard)
Copyright (c) 1997-2003, Tarik Isani (xhomer@isani.org)
This file is part of Xhomer.
Xhomer is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
Xhomer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Xhomer; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* TBD:
-alloc only 2 colors for non-ebo 8-bit
-use better technique to detect CLUT modes
-add DGA mode that only writes each pixel once
-colormap is not loaded for DGA+PCM
-get screensaver blanking to work during DGA switch
Xserver DGA2 bugs:
Currently, standard X event handling is used, as the DGA stuff is broken
in DGA2
XFree 4.0.2 Neomagic
-Use of more than 4MB of graphics memory causes crash during modeswitch
-Menu does not work if more than X MB of graphics memory is used
-XDGA events are not being seen (see #ifdef DGAXXX)
-XDGAFlipImmediate is not supported. So, xhomer will wait until the
viewport update has been completed, causing slower scrolling with
this server. Servers that support XDGAFlipImmediate are not
affected.
XFree 4.0.2 I128
-XDGASetMode(..., ..., 0) does *not* return to initial screen mode
-grabbing keyboard and mouse does not work, unless sleep(1) first
-framebuffer height is incorrectly reported as that of single a screen
-XDGA events are not being seen (see #ifdef DGAXXX)
-Expose events are not sent in DGA mode
-EnterNotify is sent, instead, *most* of the time
-look for "pro_nine_workaround"
*/
#ifdef PRO
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#ifdef SHM
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif
#ifdef DGA
#include <X11/extensions/xf86dga.h>
#ifndef DGA2
#include <X11/extensions/xf86vmode.h>
#endif
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <math.h>
#include "pro_defs.h"
#include "pro_lk201.h"
#define PRO_KEYBOARD_FIFO_DEPTH 1024
int pro_nine_workaround = 0; /* workaround for #9 Xserver bugs */
int pro_libc_workaround = 0; /* workaround for pre-glibc-2.0 bug */
int pro_window_x = 0; /* window x position */
int pro_window_y = 0; /* window y position */
int pro_screen_full = 0; /* 0 = window, 1 = full-screen */
int pro_screen_window_scale = 2; /* vertical window scale factor */
int pro_screen_full_scale = 3; /* vertical DGA scale factor */
int pro_screen_gamma = 10; /* 10x gamma correction factor */
int pro_screen_pcm = 1; /* 0 = don't use PCM, 1 = use PCM (private color map) */
int pro_screen_framebuffers = 0; /* number of DGA framebuffers (1..3 or 0 to auto-detect) */
LOCAL int pro_screen_open = 0;
LOCAL int pro_screen_winheight; /* height of framebuffer */
LOCAL int pro_screen_bufheight; /* height of allocated framebuffer */
LOCAL int pro_screen_updateheight; /* height to update */
LOCAL int pro_keyboard_fifo_h; /* FIFO head pointer */
LOCAL int pro_keyboard_fifo_t; /* FIFO tail pointer */
LOCAL int pro_keyboard_fifo[PRO_KEYBOARD_FIFO_DEPTH];
int pro_mouse_x = 0; /* mouse x */
int pro_mouse_y = 0; /* mouse y */
int pro_mouse_l = 0; /* left mouse button */
int pro_mouse_m = 0; /* middle mouse button */
int pro_mouse_r = 0; /* right mouse button */
int pro_mouse_in = 0; /* mouse in window */
LOCAL Display *ProDisplay; /* pointer to the display */
LOCAL int ProScreen;
LOCAL Window ProWindow; /* window structure */
LOCAL GC ProGC;
LOCAL Colormap ProColormap;
LOCAL XColor ProColor_map[8];
LOCAL XColor ProColor_nonmap[8];
LOCAL int ProDepth; /* color depth */
const long ProEventMask = ExposureMask | KeyPressMask
| KeyReleaseMask | PointerMotionMask
| ButtonPressMask | ButtonReleaseMask
| EnterWindowMask | LeaveWindowMask;
LOCAL unsigned long ProWhitePixel;
LOCAL unsigned long ProBlackPixel;
#ifdef DGA
#ifdef DGA2
LOCAL int pro_vid_dga_eventbase;
LOCAL int pro_vid_dga_errorbase;
#else
LOCAL XF86VidModeModeInfo pro_modeline;
#endif
LOCAL char *pro_vid_dga_addr;
LOCAL int pro_vid_dga_width;
LOCAL int pro_vid_dga_height;
#ifndef DGA2
LOCAL int pro_vid_dga_banksize;
#endif
LOCAL int pro_vid_dga_memsize;
LOCAL Cursor ProBlankCursor;
LOCAL XColor ProBlackColor;
LOCAL int pro_old_scroll; /* used to determine when to scroll */
LOCAL int pro_menu_scroll; /* scroll position when menu is first turned on in DGA mode */
LOCAL int pro_old_overlay_on; /* used to determine when overlay has been turned off */
LOCAL int pro_mouse_y_screen = 0;
#endif
LOCAL XImage *pro_image;
LOCAL XKeyboardControl pro_keyboard_control;
LOCAL XKeyboardState pro_keyboard_state; /* state to be restored on exit */
LOCAL unsigned char pro_keyboard_keys[32]; /* key vector */
LOCAL unsigned char *pro_image_data;
LOCAL int pro_image_stride;
LOCAL int pro_screen_clutmode; /* indicates whether window is in CLUT mode */
LOCAL int pro_screen_pixsize; /* bytes per pixel */
LOCAL int pro_screen_pixsize_act; /* active bytes per pixel */
LOCAL int pro_screen_clsize; /* screen cache line size in bytes */
LOCAL int pro_screen_blank = 0; /* indicates whether screen is blanked */
LOCAL int pro_mask_r; /* mask for red bits */
LOCAL int pro_mask_g; /* mask for green bits */
LOCAL int pro_mask_b; /* mask for blue bits */
LOCAL int pro_nonmap[8]; /* EBO non-mapped colors */
LOCAL int pro_colormap[8]; /* 8-entry colormap for EBO */
LOCAL int *pro_lut; /* points to nonmap or colormap */
#ifdef SHM
LOCAL int shm_present = 1; /* indicates whether XShm may be used */
LOCAL XShmSegmentInfo shminfo;
LOCAL int (*OldXErrorHandler)(Display*, XErrorEvent*);
LOCAL int XShmErrorHandler(Display *dpy, XErrorEvent *xev)
{
char error[256];
XGetErrorText(dpy, xev->error_code, error, 255); error[255] = '\0';
fprintf(stderr, "warning: can't use Shm - %s\r\n", error);
shm_present = 0;
return 0;
}
#endif
LOCAL char* current_title = NULL;
LOCAL int title_needs_update = 0;
/* Put a title on the display window */
void pro_screen_title (char *title)
{
if (current_title)
free(current_title);
current_title = strdup(title);
title_needs_update = 1;
}
void pro_screen_clear ()
{
int i, j, k;
unsigned char *mptr;
/* Clear image buffer */
mptr = pro_image_data;
for(i=0; i<pro_screen_bufheight;
i++, mptr += pro_image_stride*pro_screen_pixsize)
for(j=0; j<PRO_VID_SCRWIDTH; j++)
for(k=0; k<pro_screen_pixsize; k++)
*mptr++ = ProBlackPixel >> (8*k);
}
/* Save and restore keyboard control settings */
void pro_screen_save_old_keyboard ()
{
XGetKeyboardControl(ProDisplay, &pro_keyboard_state);
}
void pro_screen_restore_old_keyboard ()
{
XKeyboardControl key_control;
key_control.bell_pitch = (int)pro_keyboard_state.bell_pitch;
key_control.bell_duration = (int)pro_keyboard_state.bell_duration;
key_control.bell_percent = pro_keyboard_state.bell_percent;
key_control.key_click_percent = pro_keyboard_state.key_click_percent;
key_control.auto_repeat_mode = pro_keyboard_state.global_auto_repeat;
XChangeKeyboardControl(ProDisplay, KBAutoRepeatMode | KBKeyClickPercent
| KBBellPitch | KBBellDuration | KBBellPercent,
&key_control);
}
void pro_screen_restore_keyboard ()
{
if (pro_mouse_in == 1)
XChangeKeyboardControl(ProDisplay, KBAutoRepeatMode | KBKeyClickPercent
| KBBellPitch | KBBellDuration | KBBellPercent,
&pro_keyboard_control);
}
/* Keycode lookup routine (key press) */
LOCAL int pro_keyboard_lookup_down (int xkey)
{
switch (xkey)
{
case XK_Escape: return PRO_LK201_HOLD;
case XK_F1: return PRO_LK201_PRINT;
case XK_F2: return PRO_LK201_SETUP;
case XK_F3: return PRO_LK201_FFOUR;
case XK_F4: return PRO_LK201_BREAK;
case XK_F5: return PRO_LK201_INT;
case XK_F6: return PRO_LK201_RESUME;
case XK_F7: return PRO_LK201_CANCEL;
case XK_F8: return PRO_LK201_MAIN;
case XK_F9: return PRO_LK201_EXIT;
case XK_F10: return PRO_LK201_ESC;
case XK_F11: return PRO_LK201_BS;
case XK_F12: return PRO_LK201_LF;
case XK_Num_Lock: /* XXX temp., since Sys_Req is broken! */
#ifndef XK_Sys_Req
#define XK_Sys_Req 0xFF15
#endif
case XK_Sys_Req: return PRO_LK201_ADDOP; /* XXX seems broken */
case XK_Multi_key: return PRO_LK201_HELP; /* XXX check this! */
case XK_Pause: return PRO_LK201_DO;
case XK_a: return PRO_LK201_A;
case XK_b: return PRO_LK201_B;
case XK_c: return PRO_LK201_C;
case XK_d: return PRO_LK201_D;
case XK_e: return PRO_LK201_E;
case XK_f: return PRO_LK201_F;
case XK_g: return PRO_LK201_G;
case XK_h: return PRO_LK201_H;
case XK_i: return PRO_LK201_I;
case XK_j: return PRO_LK201_J;
case XK_k: return PRO_LK201_K;
case XK_l: return PRO_LK201_L;
case XK_m: return PRO_LK201_M;
case XK_n: return PRO_LK201_N;
case XK_o: return PRO_LK201_O;
case XK_p: return PRO_LK201_P;
case XK_q: return PRO_LK201_Q;
case XK_r: return PRO_LK201_R;
case XK_s: return PRO_LK201_S;
case XK_t: return PRO_LK201_T;
case XK_u: return PRO_LK201_U;
case XK_v: return PRO_LK201_V;
case XK_w: return PRO_LK201_W;
case XK_x: return PRO_LK201_X;
case XK_y: return PRO_LK201_Y;
case XK_z: return PRO_LK201_Z;
case XK_0: return PRO_LK201_0;
case XK_1: return PRO_LK201_1;
case XK_2: return PRO_LK201_2;
case XK_3: return PRO_LK201_3;
case XK_4: return PRO_LK201_4;
case XK_5: return PRO_LK201_5;
case XK_6: return PRO_LK201_6;
case XK_7: return PRO_LK201_7;
case XK_8: return PRO_LK201_8;
case XK_9: return PRO_LK201_9;
case XK_minus: return PRO_LK201_MINUS;
case XK_equal: return PRO_LK201_EQUAL;
case XK_bracketleft: return PRO_LK201_LEFTB;
case XK_bracketright: return PRO_LK201_RIGHTB;
case XK_semicolon: return PRO_LK201_SEMI;
case XK_quoteright: return PRO_LK201_QUOTE;
case XK_backslash: return PRO_LK201_BACKSL;
case XK_comma: return PRO_LK201_COMMA;
case XK_period: return PRO_LK201_PERIOD;
case XK_slash: return PRO_LK201_SLASH;
case XK_quoteleft: return PRO_LK201_TICK;
case XK_space: return PRO_LK201_SPACE;
case XK_BackSpace: return PRO_LK201_DEL;
case XK_Return: return PRO_LK201_RETURN;
case XK_Tab: return PRO_LK201_TAB;
case XK_Up: return PRO_LK201_UP;
case XK_Down: return PRO_LK201_DOWN;
case XK_Left: return PRO_LK201_LEFT;
case XK_Right: return PRO_LK201_RIGHT;
case XK_Find: return PRO_LK201_FIND;
case XK_Home: return PRO_LK201_INSERT;
case XK_Prior: return PRO_LK201_REMOVE;
case XK_Delete: return PRO_LK201_SELECT;
case XK_End: return PRO_LK201_PREV;
case XK_Next: return PRO_LK201_NEXT;
case XK_Caps_Lock: return PRO_LK201_LOCK;
case XK_Shift_L: return PRO_LK201_SHIFT;
case XK_Shift_R: return PRO_LK201_SHIFT;
case XK_Control_L: return PRO_LK201_CTRL;
case XK_Control_R: return PRO_LK201_CTRL;
case XK_Alt_L: return PRO_LK201_COMPOSE;
default: return PRO_NOCHAR;
}
}
/* Keycode lookup routine (key release) */
LOCAL int pro_keyboard_lookup_up (int xkey)
{
switch (xkey)
{
case XK_Shift_L: return PRO_LK201_ALLUPS;
case XK_Shift_R: return PRO_LK201_ALLUPS;
case XK_Control_L: return PRO_LK201_ALLUPS;
case XK_Control_R: return PRO_LK201_ALLUPS;
default: return PRO_NOCHAR;
}
}
/* Put character in keycode FIFO */
LOCAL void pro_keyboard_fifo_put (int key)
{
/* Filter keycodes for commands */
/* This is to avoid changing the menu state while screen is closed */
if (pro_screen_open)
key = pro_menu(key);
if (key != PRO_NOCHAR)
{
/* First check if FIFO is full */
if ((pro_keyboard_fifo_h != (pro_keyboard_fifo_t-1))
&& ((pro_keyboard_fifo_h != (PRO_KEYBOARD_FIFO_DEPTH-1))
|| (pro_keyboard_fifo_t != 0)))
{
pro_keyboard_fifo[pro_keyboard_fifo_h] = key;
pro_keyboard_fifo_h++;
if (pro_keyboard_fifo_h == PRO_KEYBOARD_FIFO_DEPTH)
pro_keyboard_fifo_h = 0;
}
}
}
/* External keyboard polling routine */
int pro_keyboard_get ()
{
int key;
/* Get character from keycode FIFO */
/* Check if FIFO is empty */
if (pro_keyboard_fifo_h == pro_keyboard_fifo_t)
key = PRO_NOCHAR;
else
{
key = pro_keyboard_fifo[pro_keyboard_fifo_t];
pro_keyboard_fifo_t++;
if (pro_keyboard_fifo_t == PRO_KEYBOARD_FIFO_DEPTH)
pro_keyboard_fifo_t = 0;
}
return key;
}
/* Save keymap */
void pro_screen_save_keys ()
{
int i;
unsigned char keys[32];
XQueryKeymap(ProDisplay, keys);
for(i=0; i<32; i++)
pro_keyboard_keys[i] = keys[i];
}
/* Bring emulator state up to date with shift/ctrl key state that may have changed
while focus was lost */
void pro_screen_update_keys ()
{
int i, key, oldkey, curkey;
unsigned char keys[32];
XQueryKeymap(ProDisplay, keys);
for(i=0; i<32*8; i++)
{
oldkey = pro_keyboard_keys[i/8]&(1<<(i%8));
curkey = keys[i/8]&(1<<(i%8));
if (oldkey != curkey)
{
key = XKeycodeToKeysym(ProDisplay, i, 0);
if (curkey == 0)
key = pro_keyboard_lookup_up(key);
else
key = pro_keyboard_lookup_down(key);
if ((key == PRO_LK201_SHIFT) || (key == PRO_LK201_CTRL)
|| (key == PRO_LK201_ALLUPS))
pro_keyboard_fifo_put(key);
}
}
}
/* Initialize the display */
int pro_screen_init ()
{
XSetWindowAttributes ProWindowAttributes; /* structure of window attibutes */
unsigned long ProWindowMask; /* mask for window attributes */
XSizeHints ProSizeHints; /* window hints for window manger */
int x, y; /* window position */
int border_width = 0; /* border width of the window */
XGCValues ProGCValues;
int i, index;
unsigned long planeret[8];
unsigned long pixret[8];
XEvent event;
Window focus_window;
int revert_to;
#ifdef DGA
int old_uid = geteuid();
#endif
if (pro_screen_open == 0)
{
#ifndef DGA
if (pro_screen_full)
{
printf("Emulator not compiled with DGA support!\r\n");
return PRO_FAIL;
}
#endif
/* Set window coordinates */
x = pro_window_x;
y = pro_window_y;
ProDisplay = XOpenDisplay ("");
if (ProDisplay == NULL)
{
fprintf (stderr,
"ERROR: Could not open a connection to X on display %s\r\n",
XDisplayName (NULL));
return PRO_FAIL;
}
ProScreen = DefaultScreen (ProDisplay);
ProDepth = DefaultDepth (ProDisplay, ProScreen);
ProWhitePixel = WhitePixel(ProDisplay, ProScreen);
ProBlackPixel = BlackPixel(ProDisplay, ProScreen);
ProWindowAttributes.border_pixel = BlackPixel (ProDisplay, ProScreen);
ProWindowAttributes.background_pixel = BlackPixel (ProDisplay, ProScreen);
ProWindowAttributes.override_redirect = False;
ProWindowAttributes.event_mask = ProEventMask;
ProWindowMask = CWEventMask | CWBackPixel | CWBorderPixel;
#ifdef DGA
if (pro_screen_full)
{
int modecount;
#ifdef DGA2
XDGADevice *dgadevice;
XDGAMode *dgamode;
#else
int clk;
XF86VidModeModeLine ml;
XF86VidModeModeInfo **modesinfo;
#endif
if (seteuid(0) != 0)
{
printf("DGA error: emulator must be run as root or setuid root\r\n");
return PRO_FAIL;
}
#ifdef DGA2
XDGAOpenFramebuffer(ProDisplay, ProScreen);
dgamode = XDGAQueryModes(ProDisplay, ProScreen, &modecount);
for(i=0; i<modecount; i++)
if ((dgamode[i].viewportWidth == PRO_VID_SCRWIDTH)
&& (dgamode[i].viewportHeight == pro_screen_full_scale*PRO_VID_SCRHEIGHT))
break;
XFree(dgamode);
if (i == modecount)
{
printf("DGA2 Error - unable to find correct screen mode\r\n");
printf("%d x %d viewport required\r\n", PRO_VID_SCRWIDTH, pro_screen_full_scale*PRO_VID_SCRHEIGHT);
seteuid(old_uid);
return PRO_FAIL;
}
dgadevice = XDGASetMode(ProDisplay, ProScreen, i+1);
pro_vid_dga_addr = dgadevice -> data;
pro_vid_dga_width = (dgadevice -> mode).imageWidth;
pro_vid_dga_height = (dgadevice -> mode).imageHeight;
if (pro_nine_workaround)
{
/* This is a workaround for a #9 DGA2 bug that reports
framebuffer height incorrectly as that of a single screen */
if (pro_vid_dga_height <= 1024)
pro_vid_dga_height = 2304;
}
pro_vid_dga_memsize = pro_vid_dga_height * (dgadevice -> mode).bytesPerScanline / 1024;
XFree(dgadevice);
#ifdef DGAXXX
/* XXX Does not seem to pass DGA events ??? Use XEvents for now... */
XDGASelectInput(ProDisplay, ProScreen, ProEventMask);
#endif
XDGAQueryExtension(ProDisplay, &pro_vid_dga_eventbase, &pro_vid_dga_errorbase);
#else /* DGA1 */
/* Get current screen mode */
XF86VidModeGetModeLine(ProDisplay, ProScreen, &clk, &ml);
if (ml.privsize != 0)
XFree(ml.private);
/* Attempt to find appropriate screen mode */
XF86VidModeGetAllModeLines(ProDisplay, ProScreen, &modecount, &modesinfo);
for(i=0; i<modecount; i++)
if ((modesinfo[i]->hdisplay == PRO_VID_SCRWIDTH)
&& (modesinfo[i]->vdisplay == pro_screen_full_scale*PRO_VID_SCRHEIGHT))
break;
if (i == modecount)
{
printf("DGA1 Error - unable to find correct screen mode\r\n");
printf("%d x %d viewport required\r\n", PRO_VID_SCRWIDTH, pro_screen_full_scale*PRO_VID_SCRHEIGHT);
seteuid(old_uid);
return PRO_FAIL;
}
/* Save current screen mode */
memset(&pro_modeline, 0, sizeof(pro_modeline));
pro_modeline.dotclock = clk;
pro_modeline.hdisplay = ml.hdisplay;
pro_modeline.hsyncstart = ml.hsyncstart;
pro_modeline.hsyncend = ml.hsyncend;
pro_modeline.htotal = ml.htotal;
pro_modeline.vdisplay = ml.vdisplay;
pro_modeline.vsyncstart = ml.vsyncstart;
pro_modeline.vsyncend = ml.vsyncend;
pro_modeline.vtotal = ml.vtotal;
pro_modeline.flags = ml.flags;
/* XXX Blank screen here */
/* Switch screen mode */
XF86VidModeSwitchToMode(ProDisplay, ProScreen, modesinfo[i]);
/* XXX is this correct? */
for(i=0; i<modecount; i++)
if (modesinfo[i]->privsize != 0)
XFree(modesinfo[i]->private);
XFree(modesinfo); /* XXX correct? */
/* Get frame buffer parameters */
XF86DGAGetVideo(ProDisplay, ProScreen,
&pro_vid_dga_addr,
&pro_vid_dga_width,
&pro_vid_dga_banksize,
&pro_vid_dga_memsize);
/* Enter DGA mode */
XF86DGADirectVideo(ProDisplay, ProScreen,
XF86DGADirectGraphics
| XF86DGADirectMouse
| XF86DGADirectKeyb);
#endif
seteuid(old_uid);
x = 0;
y = 0;
}
#endif
/* Determine pixel depth */
/* XXX
printf("%d bpp\r\n", ProDepth);
*/
/* XXX use better technique to detect CLUT modes */
switch(ProDepth)
{
case 8:
pro_screen_clutmode = 1;
pro_screen_pixsize = 1;
pro_screen_pixsize_act = 1;
break;
case 12:
pro_screen_clutmode = 1;
pro_screen_pixsize = 2;
pro_screen_pixsize_act = 2;
break;
case 15:
case 16:
pro_screen_clutmode = 0;
pro_screen_pixsize = 2;
pro_screen_pixsize_act = 2;
break;
case 24:
pro_screen_clutmode = 0;
pro_screen_pixsize = 4;
pro_screen_pixsize_act = 3;
break;
default:
printf("Unsupported pixel depth\r\n");
return PRO_FAIL;
}
#ifdef DGA
if (pro_screen_full)
{
/* Check if number of framebuffers should be auto-detected */
if (pro_screen_framebuffers == 0)
{
pro_screen_framebuffers = (pro_vid_dga_memsize*1024) /
(pro_vid_dga_width*pro_screen_pixsize*pro_screen_full_scale*PRO_VID_MEMHEIGHT);
if (pro_screen_framebuffers > 3)
pro_screen_framebuffers = 3;
if (pro_screen_framebuffers < 1)
pro_screen_framebuffers = 1;
printf("Auto-detected %d framebuffer", pro_screen_framebuffers);
if (pro_screen_framebuffers > 1)
printf("s");
printf("\r\n");
}
pro_screen_bufheight = pro_screen_framebuffers*pro_screen_full_scale*PRO_VID_MEMHEIGHT;
pro_screen_winheight = pro_screen_bufheight;
pro_screen_updateheight = PRO_VID_MEMHEIGHT;
}
else
#endif
{
pro_screen_bufheight = PRO_VID_SCRHEIGHT;
pro_screen_winheight = pro_screen_window_scale*pro_screen_bufheight;
pro_screen_updateheight = PRO_VID_SCRHEIGHT;
}
ProWindow = XCreateWindow (ProDisplay,
RootWindow (ProDisplay, ProScreen),
x, y, PRO_VID_SCRWIDTH, pro_screen_winheight, border_width,
ProDepth, InputOutput, CopyFromParent,
ProWindowMask, &ProWindowAttributes);
ProSizeHints.flags = PSize | PMinSize | PMaxSize;
if ((x != 0) && (y != 0))
{
ProSizeHints.flags |= PPosition;
ProSizeHints.x = x;
ProSizeHints.y = y;
}
ProSizeHints.width = ProSizeHints.min_width = ProSizeHints.max_width
= PRO_VID_SCRWIDTH;
ProSizeHints.height = ProSizeHints.min_height = ProSizeHints.max_height
= pro_screen_winheight;
XSetNormalHints (ProDisplay, ProWindow, &ProSizeHints);
ProGC = XCreateGC (ProDisplay,
ProWindow,
(unsigned long) 0,
&ProGCValues);
/* error... cannot create gc */
if (ProGC == 0)
{
XDestroyWindow(ProDisplay, ProScreen);
return PRO_FAIL;
}
XSetForeground (ProDisplay, ProGC, ProBlackPixel);
XSetBackground (ProDisplay, ProGC, ProWhitePixel);
XMapWindow (ProDisplay, ProWindow);
/* Wait for window to become visible */
/* XXX #9 server sometimes hangs here with no events */
#ifdef DGA
#ifdef DGA2
/* XXX Major #9 hack to account for missing Expose/EnterNotify
events in DGA mode */
if (!pro_screen_full || !pro_nine_workaround)
#endif
#endif
do
XNextEvent(ProDisplay, &event);
while ((event.type != Expose) && (event.type != EnterNotify));
/* Determine whether window has input focus */
#ifdef DGA
if (pro_screen_full)
pro_mouse_in = 1;
else
#endif
{
XGetInputFocus (ProDisplay, &focus_window, &revert_to);
if (focus_window == ProWindow)
pro_mouse_in = 1;
else
pro_mouse_in = 0;
}
pro_screen_title("Pro 350");
pro_screen_clsize = PRO_VID_CLS_PIX * pro_screen_pixsize;
if ((pro_screen_pcm == 1) && (pro_screen_clutmode == 1))
/* Allocate private colormap */
ProColormap = XCreateColormap(ProDisplay,
ProWindow,
DefaultVisual(ProDisplay, DefaultScreen(ProDisplay)),
AllocNone);
else
/* Use default colormap */
ProColormap = DefaultColormap(ProDisplay, ProScreen);
/* Allocate colors */
/* XXX also, alloc only 2 colors if non-ebo */
if (pro_screen_clutmode == 1)
XAllocColorCells(ProDisplay, ProColormap, True, planeret, 0, pixret, 8);
for(i=0; i<8; i++)
{
if (pro_screen_clutmode == 1)
{
index = pixret[i];
ProColor_nonmap[i].pixel = index;
ProColor_map[i].pixel = index;
/* Determine whether PRO is in colormapped mode */
if ((pro_vid_csr & PRO_VID_CME) != 0)
XStoreColor(ProDisplay, ProColormap, &ProColor_map[i]);
else
XStoreColor(ProDisplay, ProColormap, &ProColor_nonmap[i]);
pro_colormap[i] = index;
pro_nonmap[i] = index;
}
else
{
XAllocColor(ProDisplay, ProColormap, &ProColor_nonmap[i]);
pro_nonmap[i] = ProColor_nonmap[i].pixel;
XAllocColor(ProDisplay, ProColormap, &ProColor_map[i]);
pro_colormap[i] = ProColor_map[i].pixel;
}
}
/* Assign RGB mask values (for overlay blending) */
if (pro_screen_clutmode == 1)
{
pro_mask_r = ~0;
pro_mask_g = 0;
pro_mask_b = 0;
}
else
{
pro_mask_r = pro_nonmap[1];
pro_mask_g = pro_nonmap[2];
pro_mask_b = pro_nonmap[4];
}
XSetWindowColormap(ProDisplay, ProWindow, ProColormap);
#if !PRO_EBO_PRESENT
pro_nonmap[1] = pro_nonmap[7]; /* XXX change this to 1 */
#endif
/* Create image buffer for screen updates */
#ifdef DGA
if (pro_screen_full)
{
pro_image_data = pro_vid_dga_addr;
pro_image_stride = pro_vid_dga_width - PRO_VID_SCRWIDTH;
}
else
#endif
{
pro_image_stride = 0;
#ifdef SHM
if (XShmQueryExtension(ProDisplay))
{
/* Use shared memory */
OldXErrorHandler = XSetErrorHandler(XShmErrorHandler);
memset(&shminfo, 0, sizeof(shminfo));
if (!(pro_image = XShmCreateImage(ProDisplay,
DefaultVisual(ProDisplay, DefaultScreen(ProDisplay)),
ProDepth,
ZPixmap,
NULL,
&shminfo,
PRO_VID_SCRWIDTH, PRO_VID_SCRHEIGHT)))
shm_present = 0;
else if ((shminfo.shmid =
shmget(IPC_PRIVATE, pro_image->bytes_per_line*PRO_VID_SCRHEIGHT, IPC_CREAT|0777))==-1)
shm_present = 0;
else if ((shminfo.shmaddr = pro_image->data = shmat(shminfo.shmid, 0, 0))==(char*)-1)
shm_present = 0;
else
{
if (XShmAttach(ProDisplay, &shminfo)==0) shm_present = 0;
XSync(ProDisplay, True);
shmctl(shminfo.shmid, IPC_RMID, 0);
if (!shm_present) shmdt(shminfo.shmaddr);
}
if (pro_image && !shm_present) XDestroyImage(pro_image);
XSync(ProDisplay, True);
XSetErrorHandler(OldXErrorHandler);
}
else shm_present = 0;
if (shm_present != 0)
pro_image_data = pro_image->data;
else
#endif /* SHM */
{
/* Use non-shared memory */
pro_image_data = (unsigned char *)malloc(PRO_VID_SCRWIDTH*PRO_VID_SCRHEIGHT*pro_screen_pixsize);
pro_image = XCreateImage(ProDisplay,
DefaultVisual(ProDisplay, DefaultScreen(ProDisplay)),
ProDepth,
ZPixmap,
0,
(char*) pro_image_data,
PRO_VID_SCRWIDTH, PRO_VID_SCRHEIGHT,
pro_screen_pixsize*8,
PRO_VID_CLS_PIX*pro_screen_pixsize);
}
}
/* Save old keyboard state */
pro_screen_save_old_keyboard ();
/* Restore emulator keyboard state */
pro_screen_restore_keyboard();
XFlush (ProDisplay);
/* Make emulator state consistent with keyboard state */
pro_screen_update_keys();
/* Initialize the overlay frame buffer */
pro_overlay_init(pro_screen_pixsize, pro_screen_clutmode,
(int)ProBlackPixel, (int)ProWhitePixel);
pro_screen_open = 1;
#ifdef DGA
if (pro_screen_full)
{
/* Check if enough video memory */
if (pro_screen_winheight*pro_vid_dga_width*pro_screen_pixsize
> pro_vid_dga_memsize*1024)
{
printf("DGA error: not enough video memory! (%d required, only %d present)\r\n",
pro_screen_winheight*pro_vid_dga_width*pro_screen_pixsize,