forked from kasymovga/DarkPlacesRM
/
vid_glx.c
1752 lines (1502 loc) · 49 KB
/
vid_glx.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
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#if !defined(__APPLE__) && !defined(__MACH__) && !defined(SUNOS)
//#define USEDGA
#endif
#include <signal.h>
#include <dlfcn.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/XKBlib.h> // TODO possibly ifdef this out on non-supporting systems... Solaris (as always)?
#include <GL/glx.h>
#include "quakedef.h"
#include "dpsoftrast.h"
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <X11/xpm.h>
#include <X11/extensions/XShm.h>
#ifdef USEDGA
#include <X11/extensions/xf86dga.h>
#endif
#include <X11/extensions/xf86vmode.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
// get the Uchar type
#include "utf8lib.h"
#include "image.h"
#include "nexuiz.xpm"
#include "darkplaces.xpm"
// Tell startup code that we have a client
int cl_available = true;
// note: if we used the XRandR extension we could support refresh rates
qboolean vid_supportrefreshrate = false;
//GLX prototypes
XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
//GLX_ARB_get_proc_address
void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
static dllfunction_t getprocaddressfuncs[] =
{
{"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
{NULL, NULL}
};
//GLX_SGI_swap_control
GLint (GLAPIENTRY *qglXSwapIntervalSGI)(GLint interval);
static dllfunction_t swapcontrolfuncs[] =
{
{"glXSwapIntervalSGI", (void **) &qglXSwapIntervalSGI},
{NULL, NULL}
};
static Display *vidx11_display = NULL;
static int vidx11_screen;
static Window win, root;
static GLXContext ctx = NULL;
static GC vidx11_gc = NULL;
static XImage *vidx11_ximage[2] = { NULL, NULL };
static int vidx11_ximage_pos = 0;
static XShmSegmentInfo vidx11_shminfo[2];
static int vidx11_shmevent = -1;
static int vidx11_shmwait = 0; // number of frames outstanding
Atom wm_delete_window_atom;
Atom net_wm_state_atom;
Atom net_wm_state_hidden_atom;
Atom net_wm_state_fullscreen_atom;
Atom net_wm_icon;
Atom cardinal;
#define KEY_MASK (KeyPressMask | KeyReleaseMask)
#define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
PointerMotionMask | ButtonMotionMask)
#define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | \
StructureNotifyMask | FocusChangeMask | EnterWindowMask | \
LeaveWindowMask)
static qboolean mouse_avail = true;
static qboolean vid_usingmousegrab = false;
static qboolean vid_usingmouse = false;
static qboolean vid_usinghidecursor = false;
static qboolean vid_usingvsync = false;
static qboolean vid_usevsync = false;
static qboolean vid_x11_hardwaregammasupported = false;
#ifdef USEDGA
static qboolean vid_x11_dgasupported = false;
#endif
static int vid_x11_gammarampsize = 0;
#ifdef USEDGA
cvar_t vid_dgamouse = {CVAR_SAVE, "vid_dgamouse", "0", "make use of DGA mouse input"};
static qboolean vid_usingdgamouse = false;
#endif
cvar_t vid_netwmfullscreen = {CVAR_SAVE, "vid_netwmfullscreen", "0", "make use _NET_WM_STATE_FULLSCREEN; turn this off if fullscreen does not work for you"};
qboolean vidmode_ext = false;
static int win_x, win_y;
static XF86VidModeModeInfo init_vidmode, game_vidmode;
static qboolean vid_isfullscreen = false;
static qboolean vid_isvidmodefullscreen = false;
static qboolean vid_isnetwmfullscreen = false;
static qboolean vid_isoverrideredirect = false;
static Visual *vidx11_visual;
static Colormap vidx11_colormap;
/*-----------------------------------------------------------------------*/
//
extern long keysym2ucs(KeySym keysym); // LordHavoc: suppress warning just in this case, it's not worth having a header file for this...
static void DP_Xutf8LookupString(XKeyEvent * ev,
Uchar *uch,
KeySym * keysym_return,
Status * status_return)
{
int rc;
KeySym keysym;
int codepoint;
char buffer[64];
int nbytes = sizeof(buffer);
rc = XLookupString(ev, buffer, nbytes, &keysym, NULL);
if (rc > 0) {
codepoint = buffer[0] & 0xFF;
} else {
codepoint = keysym2ucs(keysym);
}
if (codepoint < 0) {
if (keysym == None) {
*status_return = XLookupNone;
} else {
*status_return = XLookupKeySym;
*keysym_return = keysym;
}
*uch = 0;
return;
}
*uch = codepoint;
if (keysym != None) {
*keysym_return = keysym;
*status_return = XLookupBoth;
} else {
*status_return = XLookupChars;
}
}
static int XLateKey(XKeyEvent *ev, Uchar *ascii)
{
int key = 0;
//char buf[64];
KeySym keysym, shifted;
Status status;
keysym = XLookupKeysym (ev, 0);
DP_Xutf8LookupString(ev, ascii, &shifted, &status);
switch(keysym)
{
case XK_KP_Page_Up: key = K_KP_PGUP; break;
case XK_Page_Up: key = K_PGUP; break;
case XK_KP_Page_Down: key = K_KP_PGDN; break;
case XK_Page_Down: key = K_PGDN; break;
case XK_KP_Home: key = K_KP_HOME; break;
case XK_Home: key = K_HOME; break;
case XK_KP_End: key = K_KP_END; break;
case XK_End: key = K_END; break;
case XK_KP_Left: key = K_KP_LEFTARROW; break;
case XK_Left: key = K_LEFTARROW; break;
case XK_KP_Right: key = K_KP_RIGHTARROW; break;
case XK_Right: key = K_RIGHTARROW; break;
case XK_KP_Down: key = K_KP_DOWNARROW; break;
case XK_Down: key = K_DOWNARROW; break;
case XK_KP_Up: key = K_KP_UPARROW; break;
case XK_Up: key = K_UPARROW; break;
case XK_Escape: key = K_ESCAPE; break;
case XK_KP_Enter: key = K_KP_ENTER; break;
case XK_Return: key = K_ENTER; break;
case XK_Tab: key = K_TAB; break;
case XK_F1: key = K_F1; break;
case XK_F2: key = K_F2; break;
case XK_F3: key = K_F3; break;
case XK_F4: key = K_F4; break;
case XK_F5: key = K_F5; break;
case XK_F6: key = K_F6; break;
case XK_F7: key = K_F7; break;
case XK_F8: key = K_F8; break;
case XK_F9: key = K_F9; break;
case XK_F10: key = K_F10; break;
case XK_F11: key = K_F11; break;
case XK_F12: key = K_F12; break;
case XK_BackSpace: key = K_BACKSPACE; break;
case XK_KP_Delete: key = K_KP_DEL; break;
case XK_Delete: key = K_DEL; break;
case XK_Pause: key = K_PAUSE; break;
case XK_Shift_L:
case XK_Shift_R: key = K_SHIFT; break;
case XK_Execute:
case XK_Control_L:
case XK_Control_R: key = K_CTRL; break;
case XK_Alt_L:
case XK_Meta_L:
case XK_ISO_Level3_Shift:
case XK_Alt_R:
case XK_Meta_R: key = K_ALT; break;
case XK_KP_Begin: key = K_KP_5; break;
case XK_Insert:key = K_INS; break;
case XK_KP_Insert: key = K_KP_INS; break;
case XK_KP_Multiply: key = K_KP_MULTIPLY; break;
case XK_KP_Add: key = K_KP_PLUS; break;
case XK_KP_Subtract: key = K_KP_MINUS; break;
case XK_KP_Divide: key = K_KP_SLASH; break;
case XK_asciicircum: *ascii = key = '^'; break; // for some reason, XLookupString returns "" on this one for Grunt|2
case XK_section: *ascii = key = '~'; break;
default:
if (keysym < 32)
break;
if (keysym >= 'A' && keysym <= 'Z')
key = keysym - 'A' + 'a';
else
key = keysym;
break;
}
return key;
}
static Cursor CreateNullCursor(Display *display, Window root)
{
Pixmap cursormask;
XGCValues xgc;
GC gc;
XColor dummycolour;
Cursor cursor;
cursormask = XCreatePixmap(display, root, 1, 1, 1);
xgc.function = GXclear;
gc = XCreateGC(display, cursormask, GCFunction, &xgc);
XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
dummycolour.pixel = 0;
dummycolour.red = 0;
dummycolour.flags = 04;
cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
XFreePixmap(display,cursormask);
XFreeGC(display,gc);
return cursor;
}
void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
{
static int originalmouseparms_num;
static int originalmouseparms_denom;
static int originalmouseparms_threshold;
static qboolean restore_spi;
#ifdef USEDGA
qboolean usedgamouse;
#endif
if (!vidx11_display || !win)
return;
if (relative)
fullscreengrab = true;
if (!mouse_avail)
fullscreengrab = relative = hidecursor = false;
#ifdef USEDGA
usedgamouse = relative && vid_dgamouse.integer;
if (!vid_x11_dgasupported)
usedgamouse = false;
if (fullscreengrab && vid_usingmouse && (vid_usingdgamouse != usedgamouse))
VID_SetMouse(false, false, false); // ungrab first!
#endif
if (vid_usingmousegrab != fullscreengrab)
{
vid_usingmousegrab = fullscreengrab;
cl_ignoremousemoves = 2;
if (fullscreengrab)
{
XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
if (vid_grabkeyboard.integer || vid_isoverrideredirect)
XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
}
else
{
XUngrabPointer(vidx11_display, CurrentTime);
XUngrabKeyboard(vidx11_display, CurrentTime);
}
}
if (relative)
{
if (!vid_usingmouse)
{
XWindowAttributes attribs_1;
XSetWindowAttributes attribs_2;
XGetWindowAttributes(vidx11_display, win, &attribs_1);
attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
#ifdef USEDGA
vid_usingdgamouse = usedgamouse;
if (usedgamouse)
{
XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
}
else
#endif
XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
// COMMANDLINEOPTION: X11 Input: -noforcemparms disables setting of mouse parameters (not used with DGA, windows only)
#ifdef USEDGA
if (!COM_CheckParm ("-noforcemparms") && !usedgamouse)
#else
if (!COM_CheckParm ("-noforcemparms"))
#endif
{
XGetPointerControl(vidx11_display, &originalmouseparms_num, &originalmouseparms_denom, &originalmouseparms_threshold);
XChangePointerControl (vidx11_display, true, false, 1, 1, -1); // TODO maybe change threshold here, or remove this comment
restore_spi = true;
}
else
restore_spi = false;
cl_ignoremousemoves = 2;
vid_usingmouse = true;
}
}
else
{
if (vid_usingmouse)
{
#ifdef USEDGA
if (vid_usingdgamouse)
XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
vid_usingdgamouse = false;
#endif
cl_ignoremousemoves = 2;
if (restore_spi)
XChangePointerControl (vidx11_display, true, true, originalmouseparms_num, originalmouseparms_denom, originalmouseparms_threshold);
restore_spi = false;
vid_usingmouse = false;
}
}
if (vid_usinghidecursor != hidecursor)
{
vid_usinghidecursor = hidecursor;
if (hidecursor)
XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
else
XUndefineCursor(vidx11_display, win);
}
}
static keynum_t buttonremap[18] =
{
K_MOUSE1,
K_MOUSE3,
K_MOUSE2,
K_MWHEELUP,
K_MWHEELDOWN,
K_MOUSE4,
K_MOUSE5,
K_MOUSE6,
K_MOUSE7,
K_MOUSE8,
K_MOUSE9,
K_MOUSE10,
K_MOUSE11,
K_MOUSE12,
K_MOUSE13,
K_MOUSE14,
K_MOUSE15,
K_MOUSE16,
};
static qboolean BuildXImages(int w, int h)
{
int i;
if(DefaultDepth(vidx11_display, vidx11_screen) != 32 && DefaultDepth(vidx11_display, vidx11_screen) != 24)
{
Con_Printf("Sorry, we only support 24bpp and 32bpp modes\n");
VID_Shutdown();
return false;
}
// match to dpsoftrast's specs
if(vidx11_visual->red_mask != 0x00FF0000)
{
Con_Printf("Sorry, we only support BGR visuals\n");
VID_Shutdown();
return false;
}
if(vidx11_visual->green_mask != 0x0000FF00)
{
Con_Printf("Sorry, we only support BGR visuals\n");
VID_Shutdown();
return false;
}
if(vidx11_visual->blue_mask != 0x000000FF)
{
Con_Printf("Sorry, we only support BGR visuals\n");
VID_Shutdown();
return false;
}
if(vidx11_shmevent >= 0)
{
for(i = 0; i < 2; ++i)
{
vidx11_shminfo[i].shmid = -1;
vidx11_ximage[i] = XShmCreateImage(vidx11_display, vidx11_visual, DefaultDepth(vidx11_display, vidx11_screen), ZPixmap, NULL, &vidx11_shminfo[i], w, h);
if(!vidx11_ximage[i])
{
Con_Printf("Failed to get an XImage segment\n");
VID_Shutdown();
return false;
}
if(vidx11_ximage[i]->bytes_per_line != w * 4)
{
Con_Printf("Sorry, we only support linear pixel layout\n");
VID_Shutdown();
return false;
}
vidx11_shminfo[i].shmid = shmget(IPC_PRIVATE, vidx11_ximage[i]->bytes_per_line * vidx11_ximage[i]->height, IPC_CREAT|0777);
if(vidx11_shminfo[i].shmid < 0)
{
Con_Printf("Failed to get a shm segment\n");
VID_Shutdown();
return false;
}
vidx11_shminfo[i].shmaddr = vidx11_ximage[i]->data = shmat(vidx11_shminfo[i].shmid, NULL, 0);
if(!vidx11_shminfo[i].shmaddr)
{
Con_Printf("Failed to get a shm segment addresst\n");
VID_Shutdown();
return false;
}
vidx11_shminfo[i].readOnly = True;
XShmAttach(vidx11_display, &vidx11_shminfo[i]);
}
}
else
{
for(i = 0; i < 1; ++i) // we only need one buffer if we don't use Xshm
{
char *p = calloc(4, w * h);
vidx11_shminfo[i].shmid = -1;
vidx11_ximage[i] = XCreateImage(vidx11_display, vidx11_visual, DefaultDepth(vidx11_display, vidx11_screen), ZPixmap, 0, (char*)p, w, h, 8, 0);
if(!vidx11_ximage[i])
{
Con_Printf("Failed to get an XImage segment\n");
VID_Shutdown();
return false;
}
if(vidx11_ximage[i]->bytes_per_line != w * 4)
{
Con_Printf("Sorry, we only support linear pixel layout\n");
VID_Shutdown();
return false;
}
}
}
return true;
}
static void DestroyXImages(void)
{
int i;
for(i = 0; i < 2; ++i)
{
if(vidx11_shminfo[i].shmid >= 0)
{
XShmDetach(vidx11_display, &vidx11_shminfo[i]);
XDestroyImage(vidx11_ximage[i]);
vidx11_ximage[i] = NULL;
shmdt(vidx11_shminfo[i].shmaddr);
shmctl(vidx11_shminfo[i].shmid, IPC_RMID, 0);
vidx11_shminfo[i].shmid = -1;
}
if(vidx11_ximage[i])
XDestroyImage(vidx11_ximage[i]);
vidx11_ximage[i] = 0;
}
}
static int in_mouse_x_save = 0, in_mouse_y_save = 0;
static void HandleEvents(void)
{
XEvent event;
int key;
Uchar unicode;
qboolean dowarp = false;
if (!vidx11_display)
return;
in_mouse_x += in_mouse_x_save;
in_mouse_y += in_mouse_y_save;
in_mouse_x_save = 0;
in_mouse_y_save = 0;
while (XPending(vidx11_display))
{
XNextEvent(vidx11_display, &event);
switch (event.type)
{
case KeyPress:
// key pressed
key = XLateKey (&event.xkey, &unicode);
Key_Event(key, unicode, true);
break;
case KeyRelease:
// key released
key = XLateKey (&event.xkey, &unicode);
Key_Event(key, unicode, false);
break;
case MotionNotify:
// mouse moved
if (vid_usingmouse)
{
#ifdef USEDGA
if (vid_usingdgamouse)
{
in_mouse_x += event.xmotion.x_root;
in_mouse_y += event.xmotion.y_root;
}
else
#endif
{
if (!event.xmotion.send_event)
{
in_mouse_x += event.xmotion.x - in_windowmouse_x;
in_mouse_y += event.xmotion.y - in_windowmouse_y;
//if (abs(vid.width/2 - event.xmotion.x) + abs(vid.height/2 - event.xmotion.y))
if (vid_stick_mouse.integer || abs(vid.width/2 - event.xmotion.x) > vid.width / 4 || abs(vid.height/2 - event.xmotion.y) > vid.height / 4)
dowarp = true;
}
}
}
in_windowmouse_x = event.xmotion.x;
in_windowmouse_y = event.xmotion.y;
break;
case ButtonPress:
// mouse button pressed
if (event.xbutton.button <= 18)
Key_Event(buttonremap[event.xbutton.button - 1], 0, true);
else
Con_Printf("HandleEvents: ButtonPress gave value %d, 1-18 expected\n", event.xbutton.button);
break;
case ButtonRelease:
// mouse button released
if (event.xbutton.button <= 18)
Key_Event(buttonremap[event.xbutton.button - 1], 0, false);
else
Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-18 expected\n", event.xbutton.button);
break;
case CreateNotify:
// window created
win_x = event.xcreatewindow.x;
win_y = event.xcreatewindow.y;
break;
case ConfigureNotify:
// window changed size/location
win_x = event.xconfigure.x;
win_y = event.xconfigure.y;
if((vid_resizable.integer < 2 || vid_isnetwmfullscreen) && (vid.width != event.xconfigure.width || vid.height != event.xconfigure.height))
{
vid.width = event.xconfigure.width;
vid.height = event.xconfigure.height;
if(vid_isnetwmfullscreen)
Con_Printf("NetWM fullscreen: actually using resolution %dx%d\n", vid.width, vid.height);
else
Con_DPrintf("Updating to ConfigureNotify resolution %dx%d\n", vid.width, vid.height);
DPSOFTRAST_Flush();
if(vid.softdepthpixels)
free(vid.softdepthpixels);
DestroyXImages();
XSync(vidx11_display, False);
if(!BuildXImages(vid.width, vid.height))
return;
XSync(vidx11_display, False);
vid.softpixels = (unsigned int *) vidx11_ximage[vidx11_ximage_pos]->data;
vid.softdepthpixels = (unsigned int *)calloc(4, vid.width * vid.height);
}
break;
case DestroyNotify:
// window has been destroyed
Sys_Quit(0);
break;
case ClientMessage:
// window manager messages
if ((event.xclient.format == 32) && ((unsigned int)event.xclient.data.l[0] == wm_delete_window_atom))
Sys_Quit(0);
break;
case MapNotify:
if (vid_isoverrideredirect)
break;
// window restored
vid_hidden = false;
VID_RestoreSystemGamma();
if(vid_isvidmodefullscreen)
{
// set our video mode
XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &game_vidmode);
// Move the viewport to top left
XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
}
if(vid_isnetwmfullscreen)
{
// make sure it's fullscreen
XEvent event;
event.type = ClientMessage;
event.xclient.serial = 0;
event.xclient.send_event = True;
event.xclient.message_type = net_wm_state_atom;
event.xclient.window = win;
event.xclient.format = 32;
event.xclient.data.l[0] = 1;
event.xclient.data.l[1] = net_wm_state_fullscreen_atom;
event.xclient.data.l[2] = 0;
event.xclient.data.l[3] = 1;
event.xclient.data.l[4] = 0;
XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
}
dowarp = true;
break;
case UnmapNotify:
if (vid_isoverrideredirect)
break;
// window iconified/rolledup/whatever
vid_hidden = true;
VID_RestoreSystemGamma();
if(vid_isvidmodefullscreen)
XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
break;
case FocusIn:
if (vid_isoverrideredirect)
break;
// window is now the input focus
vid_activewindow = true;
break;
case FocusOut:
if (vid_isoverrideredirect)
break;
if(vid_isnetwmfullscreen && event.xfocus.mode == NotifyNormal)
{
// iconify netwm fullscreen window when it loses focus
// when the user selects it in the taskbar, the window manager will map it again and send MapNotify
XEvent event;
event.type = ClientMessage;
event.xclient.serial = 0;
event.xclient.send_event = True;
event.xclient.message_type = net_wm_state_atom;
event.xclient.window = win;
event.xclient.format = 32;
event.xclient.data.l[0] = 1;
event.xclient.data.l[1] = net_wm_state_hidden_atom;
event.xclient.data.l[2] = 0;
event.xclient.data.l[3] = 1;
event.xclient.data.l[4] = 0;
XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
}
// window is no longer the input focus
vid_activewindow = false;
VID_RestoreSystemGamma();
break;
case EnterNotify:
// mouse entered window
break;
case LeaveNotify:
// mouse left window
break;
default:
if(vidx11_shmevent >= 0 && event.type == vidx11_shmevent)
--vidx11_shmwait;
break;
}
}
if (dowarp)
{
/* move the mouse to the window center again */
// we'll catch the warp motion by its send_event flag, updating the
// stored mouse position without adding any delta motion
XEvent event;
event.type = MotionNotify;
event.xmotion.display = vidx11_display;
event.xmotion.window = win;
event.xmotion.x = vid.width / 2;
event.xmotion.y = vid.height / 2;
XSendEvent(vidx11_display, win, False, PointerMotionMask, &event);
XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
}
}
static void *prjobj = NULL;
static void GL_CloseLibrary(void)
{
if (prjobj)
dlclose(prjobj);
prjobj = NULL;
gl_driver[0] = 0;
qglXGetProcAddressARB = NULL;
gl_extensions = "";
gl_platform = "";
gl_platformextensions = "";
}
static int GL_OpenLibrary(const char *name)
{
Con_Printf("Loading OpenGL driver %s\n", name);
GL_CloseLibrary();
if (!(prjobj = dlopen(name, RTLD_LAZY | RTLD_GLOBAL)))
{
Con_Printf("Unable to open symbol list for %s\n", name);
return false;
}
strlcpy(gl_driver, name, sizeof(gl_driver));
return true;
}
void *GL_GetProcAddress(const char *name)
{
void *p = NULL;
if (qglXGetProcAddressARB != NULL)
p = (void *) qglXGetProcAddressARB((GLubyte *)name);
if (p == NULL)
p = (void *) dlsym(prjobj, name);
return p;
}
void VID_Shutdown(void)
{
if (!vidx11_display)
return;
VID_EnableJoystick(false);
VID_SetMouse(false, false, false);
VID_RestoreSystemGamma();
// FIXME: glXDestroyContext here?
if (vid_isvidmodefullscreen)
XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
if(vidx11_gc)
XFreeGC(vidx11_display, vidx11_gc);
vidx11_gc = NULL;
DestroyXImages();
vidx11_shmevent = -1;
vid.softpixels = NULL;
if (vid.softdepthpixels)
free(vid.softdepthpixels);
vid.softdepthpixels = NULL;
if (win)
XDestroyWindow(vidx11_display, win);
XCloseDisplay(vidx11_display);
vid_hidden = true;
vid_isfullscreen = false;
vid_isnetwmfullscreen = false;
vid_isvidmodefullscreen = false;
vid_isoverrideredirect = false;
vidx11_display = NULL;
win = 0;
ctx = NULL;
GL_CloseLibrary();
Key_ClearStates ();
}
static void signal_handler(int sig)
{
Con_Printf("Received signal %d, exiting...\n", sig);
VID_RestoreSystemGamma();
Sys_Quit(1);
}
static void InitSig(void)
{
signal(SIGHUP, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGILL, signal_handler);
signal(SIGTRAP, signal_handler);
signal(SIGIOT, signal_handler);
signal(SIGBUS, signal_handler);
signal(SIGFPE, signal_handler);
signal(SIGSEGV, signal_handler);
signal(SIGTERM, signal_handler);
}
void VID_Finish (void)
{
vid_usevsync = vid_vsync.integer && !cls.timedemo && qglXSwapIntervalSGI;
switch(vid.renderpath)
{
case RENDERPATH_SOFT:
if(vidx11_shmevent >= 0) {
vidx11_ximage_pos = !vidx11_ximage_pos;
vid.softpixels = (unsigned int *) vidx11_ximage[vidx11_ximage_pos]->data;
DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
// save mouse motion so we can deal with it later
in_mouse_x = 0;
in_mouse_y = 0;
while(vidx11_shmwait)
HandleEvents();
in_mouse_x_save += in_mouse_x;
in_mouse_y_save += in_mouse_y;
in_mouse_x = 0;
in_mouse_y = 0;
++vidx11_shmwait;
XShmPutImage(vidx11_display, win, vidx11_gc, vidx11_ximage[!vidx11_ximage_pos], 0, 0, 0, 0, vid.width, vid.height, True);
} else {
// no buffer switching here, we just flush the renderer
DPSOFTRAST_Finish();
XPutImage(vidx11_display, win, vidx11_gc, vidx11_ximage[vidx11_ximage_pos], 0, 0, 0, 0, vid.width, vid.height);
}
break;
case RENDERPATH_GL11:
case RENDERPATH_GL13:
case RENDERPATH_GL20:
case RENDERPATH_GLES1:
case RENDERPATH_GLES2:
if (vid_usingvsync != vid_usevsync)
{
vid_usingvsync = vid_usevsync;
if (qglXSwapIntervalSGI && qglXSwapIntervalSGI (vid_usevsync))
Con_Print("glXSwapIntervalSGI didn't accept the vid_vsync change, it will take effect on next vid_restart (GLX_SGI_swap_control does not allow turning off vsync)\n");
}
if (!vid_hidden)
{
CHECKGLERROR
if (r_speeds.integer == 2 || gl_finish.integer)
GL_Finish();
qglXSwapBuffers(vidx11_display, win);CHECKGLERROR
}
break;
case RENDERPATH_D3D9:
case RENDERPATH_D3D10:
case RENDERPATH_D3D11:
break;
}
if (vid_x11_hardwaregammasupported)
VID_UpdateGamma(false, vid_x11_gammarampsize);
}
int VID_SetGamma(unsigned short *ramps, int rampsize)
{
return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, rampsize, ramps, ramps + rampsize, ramps + rampsize*2);
}
int VID_GetGamma(unsigned short *ramps, int rampsize)
{
return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, rampsize, ramps, ramps + rampsize, ramps + rampsize*2);
}
void VID_Init(void)
{
#ifdef USEDGA
Cvar_RegisterVariable (&vid_dgamouse);
#endif
Cvar_RegisterVariable (&vid_netwmfullscreen);
InitSig(); // trap evil signals
// COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
if (COM_CheckParm ("-nomouse"))
mouse_avail = false;
vidx11_shminfo[0].shmid = -1;
vidx11_shminfo[1].shmid = -1;
}
static void VID_BuildGLXAttrib(int *attrib, qboolean stencil, qboolean stereobuffer, int samples)
{
*attrib++ = GLX_RGBA;
*attrib++ = GLX_RED_SIZE;*attrib++ = stencil ? 8 : 5;
*attrib++ = GLX_GREEN_SIZE;*attrib++ = stencil ? 8 : 5;
*attrib++ = GLX_BLUE_SIZE;*attrib++ = stencil ? 8 : 5;
*attrib++ = GLX_DOUBLEBUFFER;
*attrib++ = GLX_DEPTH_SIZE;*attrib++ = stencil ? 24 : 16;
// if stencil is enabled, ask for alpha too
if (stencil)
{
*attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
*attrib++ = GLX_ALPHA_SIZE;*attrib++ = 8;
}
if (stereobuffer)