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