]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_glx.c
beginning to add Q2 bsp support (Q3 bsp support coming later)
[divverent/darkplaces.git] / vid_glx.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 //#include <termios.h>
21 //#include <sys/ioctl.h>
22 //#include <sys/stat.h>
23 //#include <sys/vt.h>
24 //#include <stdarg.h>
25 //#include <stdio.h>
26 #include <signal.h>
27
28 #include <dlfcn.h>
29
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <GL/glx.h>
33
34 #include <X11/keysym.h>
35 #include <X11/cursorfont.h>
36
37 #include <X11/extensions/XShm.h>
38 #include <X11/extensions/xf86dga.h>
39 #include <X11/extensions/xf86vmode.h>
40
41 #include "quakedef.h"
42
43 int cl_available = true;
44
45 //GLX prototypes
46 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
47 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
48 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
49 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
50 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
51 const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
52
53 //GLX_ARB_get_proc_address
54 void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
55
56 static dllfunction_t getprocaddressfuncs[] =
57 {
58         {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
59         {NULL, NULL}
60 };
61
62 //GLX_SGI_video_sync
63 GLint (GLAPIENTRY *qglXGetVideoSyncSGI)(GLuint *count);
64 GLint (GLAPIENTRY *qglXWaitVideoSyncSGI)(GLint divisor, GLint remainder, GLuint *count);
65
66 static dllfunction_t videosyncfuncs[] =
67 {
68         {"glXGetVideoSyncSGI", (void **) &qglXGetVideoSyncSGI},
69         {"glXWaitVideoSyncSGI", (void **) &qglXWaitVideoSyncSGI},
70         {NULL, NULL}
71 };
72
73 static Display *vidx11_display = NULL;
74 static int vidx11_screen;
75 static Window win;
76 static GLXContext ctx = NULL;
77
78 Atom wm_delete_window_atom;
79
80 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
81 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
82                     PointerMotionMask | ButtonMotionMask )
83 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | StructureNotifyMask )
84
85
86 static qboolean         mouse_avail = true;
87 static qboolean         mouse_active = false, usingmouse = false;
88 static float    mouse_x, mouse_y;
89 static int p_mouse_x, p_mouse_y;
90
91 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
92 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
93
94 qboolean vidmode_ext = false;
95
96 static int win_x, win_y;
97
98 static int scr_width, scr_height;
99
100 static XF86VidModeModeInfo **vidmodes;
101 static int num_vidmodes;
102 static qboolean vidmode_active = false;
103
104 static Visual *vidx11_visual;
105 static Colormap vidx11_colormap;
106
107 /*-----------------------------------------------------------------------*/
108
109 static int XLateKey(XKeyEvent *ev)
110 {
111         int key = 0;
112         char buf[64];
113         KeySym keysym;
114
115         XLookupString(ev, buf, sizeof buf, &keysym, 0);
116
117         switch(keysym)
118         {
119                 case XK_KP_Page_Up:      key = K_KP_PGUP; break;
120                 case XK_Page_Up:         key = K_PGUP; break;
121
122                 case XK_KP_Page_Down: key = K_KP_PGDN; break;
123                 case XK_Page_Down:       key = K_PGDN; break;
124
125                 case XK_KP_Home: key = K_KP_HOME; break;
126                 case XK_Home:    key = K_HOME; break;
127
128                 case XK_KP_End:  key = K_KP_END; break;
129                 case XK_End:     key = K_END; break;
130
131                 case XK_KP_Left: key = K_KP_LEFTARROW; break;
132                 case XK_Left:    key = K_LEFTARROW; break;
133
134                 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
135                 case XK_Right:  key = K_RIGHTARROW;             break;
136
137                 case XK_KP_Down: key = K_KP_DOWNARROW; break;
138                 case XK_Down:    key = K_DOWNARROW; break;
139
140                 case XK_KP_Up:   key = K_KP_UPARROW; break;
141                 case XK_Up:              key = K_UPARROW;        break;
142
143                 case XK_Escape: key = K_ESCAPE;         break;
144
145                 case XK_KP_Enter: key = K_KP_ENTER;     break;
146                 case XK_Return: key = K_ENTER;           break;
147
148                 case XK_Tab:            key = K_TAB;                     break;
149
150                 case XK_F1:              key = K_F1;                            break;
151
152                 case XK_F2:              key = K_F2;                            break;
153
154                 case XK_F3:              key = K_F3;                            break;
155
156                 case XK_F4:              key = K_F4;                            break;
157
158                 case XK_F5:              key = K_F5;                            break;
159
160                 case XK_F6:              key = K_F6;                            break;
161
162                 case XK_F7:              key = K_F7;                            break;
163
164                 case XK_F8:              key = K_F8;                            break;
165
166                 case XK_F9:              key = K_F9;                            break;
167
168                 case XK_F10:            key = K_F10;                     break;
169
170                 case XK_F11:            key = K_F11;                     break;
171
172                 case XK_F12:            key = K_F12;                     break;
173
174                 case XK_BackSpace: key = K_BACKSPACE; break;
175
176                 case XK_KP_Delete: key = K_KP_DEL; break;
177                 case XK_Delete: key = K_DEL; break;
178
179                 case XK_Pause:  key = K_PAUSE;           break;
180
181                 case XK_Shift_L:
182                 case XK_Shift_R:        key = K_SHIFT;          break;
183
184                 case XK_Execute:
185                 case XK_Control_L:
186                 case XK_Control_R:      key = K_CTRL;            break;
187
188                 case XK_Alt_L:
189                 case XK_Meta_L:
190                 case XK_Alt_R:
191                 case XK_Meta_R: key = K_ALT;                    break;
192
193                 case XK_KP_Begin: key = K_KP_5; break;
194
195                 case XK_Insert:key = K_INS; break;
196                 case XK_KP_Insert: key = K_KP_INS; break;
197
198                 case XK_KP_Multiply: key = '*'; break;
199                 case XK_KP_Add:  key = K_KP_PLUS; break;
200                 case XK_KP_Subtract: key = K_KP_MINUS; break;
201                 case XK_KP_Divide: key = K_KP_SLASH; break;
202
203 #if 0
204                 case 0x021: key = '1';break;/* [!] */
205                 case 0x040: key = '2';break;/* [@] */
206                 case 0x023: key = '3';break;/* [#] */
207                 case 0x024: key = '4';break;/* [$] */
208                 case 0x025: key = '5';break;/* [%] */
209                 case 0x05e: key = '6';break;/* [^] */
210                 case 0x026: key = '7';break;/* [&] */
211                 case 0x02a: key = '8';break;/* [*] */
212                 case 0x028: key = '9';;break;/* [(] */
213                 case 0x029: key = '0';break;/* [)] */
214                 case 0x05f: key = '-';break;/* [_] */
215                 case 0x02b: key = '=';break;/* [+] */
216                 case 0x07c: key = '\'';break;/* [|] */
217                 case 0x07d: key = '[';break;/* [}] */
218                 case 0x07b: key = ']';break;/* [{] */
219                 case 0x022: key = '\'';break;/* ["] */
220                 case 0x03a: key = ';';break;/* [:] */
221                 case 0x03f: key = '/';break;/* [?] */
222                 case 0x03e: key = '.';break;/* [>] */
223                 case 0x03c: key = ',';break;/* [<] */
224 #endif
225
226                 default:
227                         key = *(unsigned char*)buf;
228                         if (key >= 'A' && key <= 'Z')
229                                 key = key - 'A' + 'a';
230                         break;
231         }
232
233         return key;
234 }
235
236 static Cursor CreateNullCursor(Display *display, Window root)
237 {
238         Pixmap cursormask;
239         XGCValues xgc;
240         GC gc;
241         XColor dummycolour;
242         Cursor cursor;
243
244         cursormask = XCreatePixmap(display, root, 1, 1, 1);
245         xgc.function = GXclear;
246         gc =  XCreateGC(display, cursormask, GCFunction, &xgc);
247         XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
248         dummycolour.pixel = 0;
249         dummycolour.red = 0;
250         dummycolour.flags = 04;
251         cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
252         XFreePixmap(display,cursormask);
253         XFreeGC(display,gc);
254         return cursor;
255 }
256
257 static void install_grabs(void)
258 {
259         XWindowAttributes attribs_1;
260         XSetWindowAttributes attribs_2;
261
262         XGetWindowAttributes(vidx11_display, win, &attribs_1);
263         attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
264         XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
265
266 // inviso cursor
267         XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
268
269         XGrabPointer(vidx11_display, win,  True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
270
271         if (vid_dga.integer)
272         {
273                 int MajorVersion, MinorVersion;
274
275                 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
276                 {
277                         // unable to query, probalby not supported
278                         Con_Printf( "Failed to detect XF86DGA Mouse\n" );
279                         vid_dga.integer = 0;
280                 }
281                 else
282                 {
283                         vid_dga.integer = 1;
284                         XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
285                         XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
286                 }
287         }
288         else
289                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, scr_width / 2, scr_height / 2);
290
291         XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
292
293         mouse_active = true;
294         mouse_x = mouse_y = 0;
295 }
296
297 static void uninstall_grabs(void)
298 {
299         if (!vidx11_display || !win)
300                 return;
301
302         if (vid_dga.integer == 1)
303                 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
304
305         XUngrabPointer(vidx11_display, CurrentTime);
306         XUngrabKeyboard(vidx11_display, CurrentTime);
307
308 // inviso cursor
309         XUndefineCursor(vidx11_display, win);
310
311         mouse_active = false;
312 }
313
314 static void HandleEvents(void)
315 {
316         XEvent event;
317         qboolean dowarp = false;
318
319         if (!vidx11_display)
320                 return;
321
322         while (XPending(vidx11_display))
323         {
324                 XNextEvent(vidx11_display, &event);
325
326                 switch (event.type)
327                 {
328                 case KeyPress:
329                         // key pressed
330                         Key_Event(XLateKey(&event.xkey), true);
331                         break;
332
333                 case KeyRelease:
334                         // key released
335                         Key_Event(XLateKey(&event.xkey), false);
336                         break;
337
338                 case MotionNotify:
339                         // mouse moved
340                         if (usingmouse)
341                         {
342                                 if (vid_dga.integer == 1)
343                                 {
344                                         mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
345                                         mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
346                                 }
347                                 else
348                                 {
349
350                                         if (!event.xmotion.send_event)
351                                         {
352                                                 mouse_x += event.xmotion.x - p_mouse_x;
353                                                 mouse_y += event.xmotion.y - p_mouse_y;
354                                                 if (abs(scr_width/2 - event.xmotion.x) > scr_width / 4 || abs(scr_height/2 - event.xmotion.y) > scr_height / 4)
355                                                         dowarp = true;
356                                         }
357                                         p_mouse_x = event.xmotion.x;
358                                         p_mouse_y = event.xmotion.y;
359                                 }
360                         }
361                         else
362                                 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
363                         break;
364
365                 case ButtonPress:
366                         // mouse button pressed
367                         switch(event.xbutton.button)
368                         {
369                         case 1:
370                                 Key_Event(K_MOUSE1, true);
371                                 break;
372                         case 2:
373                                 Key_Event(K_MOUSE3, true);
374                                 break;
375                         case 3:
376                                 Key_Event(K_MOUSE2, true);
377                                 break;
378                         case 4:
379                                 Key_Event(K_MWHEELUP, true);
380                                 break;
381                         case 5:
382                                 Key_Event(K_MWHEELDOWN, true);
383                                 break;
384                         case 6:
385                                 Key_Event(K_MOUSE4, true);
386                                 break;
387                         case 7:
388                                 Key_Event(K_MOUSE5, true);
389                                 break;
390                         case 8:
391                                 Key_Event(K_MOUSE6, true);
392                                 break;
393                         case 9:
394                                 Key_Event(K_MOUSE7, true);
395                                 break;
396                         case 10:
397                                 Key_Event(K_MOUSE8, true);
398                                 break;
399                         case 11:
400                                 Key_Event(K_MOUSE9, true);
401                                 break;
402                         case 12:
403                                 Key_Event(K_MOUSE10, true);
404                                 break;
405                         default:
406                                 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-12 expected\n", event.xbutton.button);
407                                 break;
408                         }
409                         break;
410
411                 case ButtonRelease:
412                         // mouse button released
413                         switch(event.xbutton.button)
414                         {
415                         case 1:
416                                 Key_Event(K_MOUSE1, false);
417                                 break;
418                         case 2:
419                                 Key_Event(K_MOUSE3, false);
420                                 break;
421                         case 3:
422                                 Key_Event(K_MOUSE2, false);
423                                 break;
424                         case 4:
425                                 Key_Event(K_MWHEELUP, false);
426                                 break;
427                         case 5:
428                                 Key_Event(K_MWHEELDOWN, false);
429                                 break;
430                         case 6:
431                                 Key_Event(K_MOUSE4, false);
432                                 break;
433                         case 7:
434                                 Key_Event(K_MOUSE5, false);
435                                 break;
436                         case 8:
437                                 Key_Event(K_MOUSE6, false);
438                                 break;
439                         case 9:
440                                 Key_Event(K_MOUSE7, false);
441                                 break;
442                         case 10:
443                                 Key_Event(K_MOUSE8, false);
444                                 break;
445                         case 11:
446                                 Key_Event(K_MOUSE9, false);
447                                 break;
448                         case 12:
449                                 Key_Event(K_MOUSE10, false);
450                                 break;
451                         default:
452                                 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-12 expected\n", event.xbutton.button);
453                                 break;
454                         }
455                         break;
456
457                 case CreateNotify:
458                         // window created
459                         win_x = event.xcreatewindow.x;
460                         win_y = event.xcreatewindow.y;
461                         break;
462
463                 case ConfigureNotify:
464                         // window changed size/location
465                         win_x = event.xconfigure.x;
466                         win_y = event.xconfigure.y;
467                         break;
468                 case DestroyNotify:
469                         // window has been destroyed
470                         Sys_Quit();
471                         break;
472                 case ClientMessage:
473                         // window manager messages
474                         if ((event.xclient.format == 32) && (event.xclient.data.l[0] == wm_delete_window_atom))
475                                 Sys_Quit();
476                         break;
477                 case MapNotify:
478                         // window restored
479                         vid_hidden = false;
480                         vid_allowhwgamma = true;
481                         break;
482                 case UnmapNotify:
483                         // window iconified/rolledup/whatever
484                         vid_hidden = true;
485                         vid_allowhwgamma = false;
486                         VID_RestoreSystemGamma();
487                         break;
488                 case FocusIn:
489                         // window is now the input focus
490                         vid_allowhwgamma = true;
491                         break;
492                 case FocusOut:
493                         // window is no longer the input focus
494                         vid_allowhwgamma = false;
495                         VID_RestoreSystemGamma();
496                         break;
497                 case EnterNotify:
498                         // mouse entered window
499                         vid_allowhwgamma = true;
500                         break;
501                 case LeaveNotify:
502                         // mouse left window
503                         vid_allowhwgamma = false;
504                         VID_RestoreSystemGamma();
505                         break;
506                 }
507         }
508
509         if (dowarp)
510         {
511                 /* move the mouse to the window center again */
512                 p_mouse_x = scr_width / 2;
513                 p_mouse_y = scr_height / 2;
514                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
515         }
516
517 }
518
519 static void IN_DeactivateMouse( void )
520 {
521         if (!mouse_avail || !vidx11_display || !win)
522                 return;
523
524         if (mouse_active)
525         {
526                 uninstall_grabs();
527                 mouse_active = false;
528         }
529 }
530
531 static void IN_ActivateMouse( void )
532 {
533         if (!mouse_avail || !vidx11_display || !win)
534                 return;
535
536         if (!mouse_active)
537         {
538                 mouse_x = mouse_y = 0; // don't spazz
539                 install_grabs();
540                 mouse_active = true;
541         }
542 }
543
544 void VID_Shutdown(void)
545 {
546         if (!ctx || !vidx11_display)
547                 return;
548
549         vid_hidden = true;
550         usingmouse = false;
551         if (vidx11_display)
552         {
553                 VID_RestoreSystemGamma();
554                 uninstall_grabs();
555
556                 // FIXME: glXDestroyContext here?
557                 if (vidmode_active)
558                         XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[0]);
559                 if (win)
560                         XDestroyWindow(vidx11_display, win);
561                 XCloseDisplay(vidx11_display);
562         }
563         vidmode_active = false;
564         vidx11_display = NULL;
565         win = 0;
566         ctx = NULL;
567
568         GL_CloseLibrary();
569 }
570
571 void signal_handler(int sig)
572 {
573         printf("Received signal %d, exiting...\n", sig);
574         VID_RestoreSystemGamma();
575         Sys_Quit();
576         exit(0);
577 }
578
579 void InitSig(void)
580 {
581         signal(SIGHUP, signal_handler);
582         signal(SIGINT, signal_handler);
583         signal(SIGQUIT, signal_handler);
584         signal(SIGILL, signal_handler);
585         signal(SIGTRAP, signal_handler);
586         signal(SIGIOT, signal_handler);
587         signal(SIGBUS, signal_handler);
588         signal(SIGFPE, signal_handler);
589         signal(SIGSEGV, signal_handler);
590         signal(SIGTERM, signal_handler);
591 }
592
593 /*
594 =================
595 VID_GetWindowSize
596 =================
597 */
598 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
599 {
600         *x = *y = 0;
601         *width = scr_width;
602         *height = scr_height;
603 }
604
605 void VID_Finish (void)
606 {
607         int usemouse;
608         if (r_render.integer)
609         {
610                 qglFinish();
611                 qglXSwapBuffers(vidx11_display, win);
612         }
613
614 // handle the mouse state when windowed if that's changed
615         usemouse = false;
616         if (vid_mouse.integer && !key_consoleactive)
617                 usemouse = true;
618         if (vidmode_active)
619                 usemouse = true;
620         if (usemouse)
621         {
622                 if (!usingmouse)
623                 {
624                         usingmouse = true;
625                         IN_ActivateMouse ();
626                 }
627         }
628         else
629         {
630                 if (usingmouse)
631                 {
632                         usingmouse = false;
633                         IN_DeactivateMouse ();
634                 }
635         }
636 }
637
638 int VID_SetGamma(unsigned short *ramps)
639 {
640         return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
641 }
642
643 int VID_GetGamma(unsigned short *ramps)
644 {
645         return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
646 }
647
648 void VID_Init(void)
649 {
650         Cvar_RegisterVariable (&vid_dga);
651         Cvar_RegisterVariable (&vid_dga_mouseaccel);
652         InitSig(); // trap evil signals
653         if (COM_CheckParm ("-nomouse") || COM_CheckParm("-safe"))
654                 mouse_avail = false;
655 }
656
657 void VID_BuildGLXAttrib(int *attrib, int stencil)
658 {
659         *attrib++ = GLX_RGBA;
660         *attrib++ = GLX_RED_SIZE;*attrib++ = 1;
661         *attrib++ = GLX_GREEN_SIZE;*attrib++ = 1;
662         *attrib++ = GLX_BLUE_SIZE;*attrib++ = 1;
663         *attrib++ = GLX_DOUBLEBUFFER;
664         *attrib++ = GLX_DEPTH_SIZE;*attrib++ = 1;
665         // if stencil is enabled, ask for alpha too
666         if (stencil)
667         {
668                 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
669                 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 1;
670         }
671         *attrib++ = None;
672 }
673
674 int VID_InitMode(int fullscreen, int width, int height, int bpp, int stencil)
675 {
676         int i;
677         int attrib[32];
678         XSetWindowAttributes attr;
679         unsigned long mask;
680         Window root;
681         XVisualInfo *visinfo;
682         int MajorVersion, MinorVersion;
683         const char *drivername;
684
685         drivername = "libGL.so.1";
686         i = COM_CheckParm("-gl_driver");
687         if (i && i < com_argc - 1)
688                 drivername = com_argv[i + 1];
689         if (!GL_OpenLibrary(drivername))
690         {
691                 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
692                 return false;
693         }
694
695         if (!(vidx11_display = XOpenDisplay(NULL)))
696         {
697                 Con_Printf("Couldn't open the X display\n");
698                 return false;
699         }
700
701         vidx11_screen = DefaultScreen(vidx11_display);
702         root = RootWindow(vidx11_display, vidx11_screen);
703
704         // Get video mode list
705         MajorVersion = MinorVersion = 0;
706         if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
707                 vidmode_ext = false;
708         else
709         {
710                 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
711                 vidmode_ext = true;
712         }
713
714         if ((qglXChooseVisual = GL_GetProcAddress("glXChooseVisual")) == NULL
715          || (qglXCreateContext = GL_GetProcAddress("glXCreateContext")) == NULL
716          || (qglXMakeCurrent = GL_GetProcAddress("glXMakeCurrent")) == NULL
717          || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL
718          || (qglXQueryExtensionsString = GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
719         {
720                 Con_Printf("glX functions not found in %s\n", gl_driver);
721                 return false;
722         }
723
724         VID_BuildGLXAttrib(attrib, stencil);
725         visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
726         if (!visinfo)
727         {
728                 Con_Printf("Couldn't get an RGB, Double-buffered, Depth visual\n");
729                 return false;
730         }
731
732         if (vidmode_ext)
733         {
734                 int best_fit, best_dist, dist, x, y;
735
736                 // Are we going fullscreen?  If so, let's change video mode
737                 if (fullscreen)
738                 {
739                         XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
740                         best_dist = 9999999;
741                         best_fit = -1;
742
743                         for (i = 0; i < num_vidmodes; i++)
744                         {
745                                 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
746                                         continue;
747
748                                 x = width - vidmodes[i]->hdisplay;
749                                 y = height - vidmodes[i]->vdisplay;
750                                 dist = (x * x) + (y * y);
751                                 if (dist < best_dist)
752                                 {
753                                         best_dist = dist;
754                                         best_fit = i;
755                                 }
756                         }
757
758                         if (best_fit != -1)
759                         {
760                                 // LordHavoc: changed from ActualWidth/ActualHeight =,
761                                 // to width/height =, so the window will take the full area of
762                                 // the mode chosen
763                                 width = vidmodes[best_fit]->hdisplay;
764                                 height = vidmodes[best_fit]->vdisplay;
765
766                                 // change to the mode
767                                 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
768                                 vidmode_active = true;
769
770                                 // Move the viewport to top left
771                                 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
772                         }
773                         else
774                                 fullscreen = 0;
775                 }
776         }
777
778         // LordHavoc: save the visual for use in gamma ramp settings later
779         vidx11_visual = visinfo->visual;
780
781         /* window attributes */
782         attr.background_pixel = 0;
783         attr.border_pixel = 0;
784         // LordHavoc: save the colormap for later, too
785         vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
786         attr.event_mask = X_MASK;
787         if (vidmode_active)
788         {
789                 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
790                 attr.override_redirect = True;
791                 attr.backing_store = NotUseful;
792                 attr.save_under = False;
793         }
794         else
795                 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
796
797         win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
798         XStoreName(vidx11_display, win, gamename);
799         XMapWindow(vidx11_display, win);
800
801         // LordHavoc: making the close button on a window do the right thing
802         // seems to involve this mess, sigh...
803         wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
804         XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
805
806         if (vidmode_active)
807         {
808                 XMoveWindow(vidx11_display, win, 0, 0);
809                 XRaiseWindow(vidx11_display, win);
810                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
811                 XFlush(vidx11_display);
812                 // Move the viewport to top left
813                 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
814         }
815
816         //XSync(vidx11_display, False);
817
818         ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
819         if (!ctx)
820                 Sys_Error ("glXCreateContext failed\n");
821
822         if (!qglXMakeCurrent(vidx11_display, win, ctx))
823                 Sys_Error ("glXMakeCurrent failed\n");
824
825         XSync(vidx11_display, False);
826
827         scr_width = width;
828         scr_height = height;
829
830         if ((qglGetString = GL_GetProcAddress("glGetString")) == NULL)
831                 Sys_Error("glGetString not found in %s", gl_driver);
832
833         gl_renderer = qglGetString(GL_RENDERER);
834         gl_vendor = qglGetString(GL_VENDOR);
835         gl_version = qglGetString(GL_VERSION);
836         gl_extensions = qglGetString(GL_EXTENSIONS);
837         gl_platform = "GLX";
838         gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
839
840         GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
841         gl_videosyncavailable = GL_CheckExtension("GLX_SGI_video_sync", videosyncfuncs, "-novideosync", false);
842
843         usingmouse = false;
844         vid_hidden = false;
845         vid_allowhwgamma = true;
846         GL_Init();
847         return true;
848 }
849
850 void Sys_SendKeyEvents(void)
851 {
852         HandleEvents();
853 }
854
855 /*
856 ===========
857 IN_Commands
858 ===========
859 */
860 void IN_Commands (void)
861 {
862 }
863
864 void IN_Move (usercmd_t *cmd)
865 {
866         if (mouse_avail)
867                 IN_Mouse(cmd, mouse_x, mouse_y);
868         mouse_x = 0;
869         mouse_y = 0;
870 }
871
872 static void *prjobj = NULL;
873
874 int GL_OpenLibrary(const char *name)
875 {
876         Con_Printf("Loading GL driver %s\n", name);
877         GL_CloseLibrary();
878         if (!(prjobj = dlopen(name, RTLD_LAZY)))
879         {
880                 Con_Printf("Unable to open symbol list for %s\n", name);
881                 return false;
882         }
883         strcpy(gl_driver, name);
884         return true;
885 }
886
887 void GL_CloseLibrary(void)
888 {
889         if (prjobj)
890                 dlclose(prjobj);
891         prjobj = NULL;
892         gl_driver[0] = 0;
893         qglXGetProcAddressARB = NULL;
894         gl_extensions = "";
895         gl_platform = "";
896         gl_platformextensions = "";
897 }
898
899 void *GL_GetProcAddress(const char *name)
900 {
901         void *p = NULL;
902         if (qglXGetProcAddressARB != NULL)
903                 p = (void *) qglXGetProcAddressARB(name);
904         if (p == NULL)
905                 p = (void *) dlsym(prjobj, name);
906         return p;
907 }
908