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