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