you can now open/close the console independently of menu and messagemode
[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 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
44 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
45 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
46 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
47 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
48
49
50 static Display *vidx11_display = NULL;
51 static int scrnum;
52 static Window win;
53 static GLXContext ctx = NULL;
54
55 Atom wm_delete_window_atom;
56
57 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
58 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
59                     PointerMotionMask | ButtonMotionMask )
60 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | StructureNotifyMask )
61
62
63 viddef_t        vid;                            // global video state
64
65 static qboolean         mouse_avail = true;
66 static qboolean         mouse_active = false, usingmouse = false;
67 static float    mouse_x, mouse_y;
68 static int p_mouse_x, p_mouse_y;
69
70 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
71 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
72
73 qboolean vidmode_ext = false;
74
75 static int win_x, win_y;
76
77 static int scr_width, scr_height;
78
79 static XF86VidModeModeInfo **vidmodes;
80 static int num_vidmodes;
81 static qboolean vidmode_active = false;
82
83 static Visual *vidx11_visual;
84 static Colormap vidx11_colormap;
85
86 /*-----------------------------------------------------------------------*/
87
88 static int
89 XLateKey(XKeyEvent *ev)
90 {
91         int key = 0;
92         KeySym keysym;
93
94         keysym = XLookupKeysym(ev, 0);
95
96         switch(keysym)
97         {
98                 case XK_KP_Page_Up:     key = KP_PGUP; break;
99                 case XK_Page_Up:        key = K_PGUP; break;
100
101                 case XK_KP_Page_Down:   key = KP_PGDN; break;
102                 case XK_Page_Down:      key = K_PGDN; break;
103
104                 case XK_KP_Home:        key = KP_HOME; break;
105                 case XK_Home:           key = K_HOME; break;
106
107                 case XK_KP_End:         key = KP_END; break;
108                 case XK_End:            key = K_END; break;
109
110                 case XK_KP_Left:        key = KP_LEFTARROW; break;
111                 case XK_Left:           key = K_LEFTARROW; break;
112
113                 case XK_KP_Right:       key = KP_RIGHTARROW; break;
114                 case XK_Right:          key = K_RIGHTARROW; break;
115
116                 case XK_KP_Down:        key = KP_DOWNARROW; break;
117                 case XK_Down:           key = K_DOWNARROW; break;
118
119                 case XK_KP_Up:          key = KP_UPARROW; break;
120                 case XK_Up:                     key = K_UPARROW; break;
121
122                 case XK_Escape:         key = K_ESCAPE; break;
123
124                 case XK_KP_Enter:       key = KP_ENTER; break;
125                 case XK_Return:         key = K_ENTER; break;
126
127                 case XK_Tab:            key = K_TAB; break;
128
129                 case XK_F1:                     key = K_F1; break;
130                 case XK_F2:                     key = K_F2; break;
131                 case XK_F3:                     key = K_F3; break;
132                 case XK_F4:                     key = K_F4; break;
133                 case XK_F5:                     key = K_F5; break;
134                 case XK_F6:                     key = K_F6; break;
135                 case XK_F7:                     key = K_F7; break;
136                 case XK_F8:                     key = K_F8; break;
137                 case XK_F9:                     key = K_F9; break;
138                 case XK_F10:            key = K_F10; break;
139                 case XK_F11:            key = K_F11; break;
140                 case XK_F12:            key = K_F12; break;
141
142                 case XK_BackSpace:      key = K_BACKSPACE; break;
143
144                 case XK_KP_Delete:      key = KP_DEL; break;
145                 case XK_Delete:         key = K_DEL; break;
146
147                 case XK_Pause:          key = K_PAUSE; break;
148
149                 case XK_Shift_L:
150                 case XK_Shift_R:        key = K_SHIFT; break;
151
152                 case XK_Execute:
153                 case XK_Control_L:
154                 case XK_Control_R:      key = K_CTRL; break;
155
156                 case XK_Mode_switch:
157                 case XK_Alt_L:
158                 case XK_Meta_L:
159                 case XK_Alt_R:
160                 case XK_Meta_R:         key = K_ALT; break;
161
162                 case XK_Caps_Lock:      key = K_CAPSLOCK; break;
163                 case XK_KP_Begin:       key = KP_5; break;
164
165                 case XK_Insert:         key = K_INS; break;
166                 case XK_KP_Insert:      key = KP_INS; break;
167
168                 case XK_KP_Multiply:    key = KP_MULTIPLY; break;
169                 case XK_KP_Add:         key = KP_PLUS; break;
170                 case XK_KP_Subtract:    key = KP_MINUS; break;
171                 case XK_KP_Divide:      key = KP_DIVIDE; break;
172
173                 /* For Sun keyboards */
174                 case XK_F27:            key = K_HOME; break;
175                 case XK_F29:            key = K_PGUP; break;
176                 case XK_F33:            key = K_END; break;
177                 case XK_F35:            key = K_PGDN; break;
178
179                 default:
180                         if (keysym < 128)
181                         {
182                                 /* ASCII keys */
183                                 key = keysym;
184                                 if ((key >= 'A') && (key <= 'Z'))
185                                         key = key + ('a' - 'A');
186                         }
187                         break;
188         }
189
190         return key;
191 }
192
193 static Cursor CreateNullCursor(Display *display, Window root)
194 {
195         Pixmap cursormask;
196         XGCValues xgc;
197         GC gc;
198         XColor dummycolour;
199         Cursor cursor;
200
201         cursormask = XCreatePixmap(display, root, 1, 1, 1);
202         xgc.function = GXclear;
203         gc =  XCreateGC(display, cursormask, GCFunction, &xgc);
204         XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
205         dummycolour.pixel = 0;
206         dummycolour.red = 0;
207         dummycolour.flags = 04;
208         cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
209         XFreePixmap(display,cursormask);
210         XFreeGC(display,gc);
211         return cursor;
212 }
213
214 static void install_grabs(void)
215 {
216         XWindowAttributes attribs_1;
217         XSetWindowAttributes attribs_2;
218
219         XGetWindowAttributes(vidx11_display, win, &attribs_1);
220         attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
221         XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
222
223 // inviso cursor
224         XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
225
226         XGrabPointer(vidx11_display, win,  True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
227
228         if (vid_dga.integer)
229         {
230                 int MajorVersion, MinorVersion;
231
232                 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
233                 {
234                         // unable to query, probalby not supported
235                         Con_Printf( "Failed to detect XF86DGA Mouse\n" );
236                         vid_dga.integer = 0;
237                 }
238                 else
239                 {
240                         vid_dga.integer = 1;
241                         XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
242                         XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
243                 }
244         }
245         else
246                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, scr_width / 2, scr_height / 2);
247
248         XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
249
250         mouse_active = true;
251         mouse_x = mouse_y = 0;
252 }
253
254 static void uninstall_grabs(void)
255 {
256         if (!vidx11_display || !win)
257                 return;
258
259         if (vid_dga.integer == 1)
260                 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
261
262         XUngrabPointer(vidx11_display, CurrentTime);
263         XUngrabKeyboard(vidx11_display, CurrentTime);
264
265 // inviso cursor
266         XUndefineCursor(vidx11_display, win);
267
268         mouse_active = false;
269 }
270
271 static void HandleEvents(void)
272 {
273         XEvent event;
274         qboolean dowarp = false;
275
276         if (!vidx11_display)
277                 return;
278
279         while (XPending(vidx11_display))
280         {
281                 XNextEvent(vidx11_display, &event);
282
283                 switch (event.type)
284                 {
285                 case KeyPress:
286                         // key pressed
287                         Key_Event(XLateKey(&event.xkey), true);
288                         break;
289
290                 case KeyRelease:
291                         // key released
292                         Key_Event(XLateKey(&event.xkey), false);
293                         break;
294
295                 case MotionNotify:
296                         // mouse moved
297                         if (usingmouse)
298                         {
299                                 if (vid_dga.integer == 1)
300                                 {
301                                         mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
302                                         mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
303                                 }
304                                 else
305                                 {
306
307                                         if (!event.xmotion.send_event)
308                                         {
309                                                 mouse_x += event.xmotion.x - p_mouse_x;
310                                                 mouse_y += event.xmotion.y - p_mouse_y;
311                                                 if (abs(scr_width/2 - event.xmotion.x) > scr_width / 4 || abs(scr_height/2 - event.xmotion.y) > scr_height / 4)
312                                                         dowarp = true;
313                                         }
314                                         p_mouse_x = event.xmotion.x;
315                                         p_mouse_y = event.xmotion.y;
316                                 }
317                         }
318                         else
319                                 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
320                         break;
321
322                 case ButtonPress:
323                         // mouse button pressed
324                         switch(event.xbutton.button)
325                         {
326                         case 1:
327                                 Key_Event(K_MOUSE1, true);
328                                 break;
329                         case 2:
330                                 Key_Event(K_MOUSE3, true);
331                                 break;
332                         case 3:
333                                 Key_Event(K_MOUSE2, true);
334                                 break;
335                         case 4:
336                                 Key_Event(K_MWHEELUP, true);
337                                 break;
338                         case 5:
339                                 Key_Event(K_MWHEELDOWN, true);
340                                 break;
341                 default:
342                                 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-5 expected\n", event.xbutton.button);
343                                 break;
344                         }
345                         break;
346
347                 case ButtonRelease:
348                         // mouse button released
349                         switch(event.xbutton.button)
350                         {
351                         case 1:
352                                 Key_Event(K_MOUSE1, false);
353                                 break;
354                         case 2:
355                                 Key_Event(K_MOUSE3, false);
356                                 break;
357                         case 3:
358                                 Key_Event(K_MOUSE2, false);
359                                 break;
360                         case 4:
361                                 Key_Event(K_MWHEELUP, false);
362                                 break;
363                         case 5:
364                                 Key_Event(K_MWHEELDOWN, false);
365                                 break;
366                 default:
367                                 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-5 expected\n", event.xbutton.button);
368                                 break;
369                         }
370                         break;
371
372                 case CreateNotify:
373                         // window created
374                         win_x = event.xcreatewindow.x;
375                         win_y = event.xcreatewindow.y;
376                         break;
377
378                 case ConfigureNotify:
379                         // window changed size/location
380                         win_x = event.xconfigure.x;
381                         win_y = event.xconfigure.y;
382                         break;
383                 case DestroyNotify:
384                         // window has been destroyed
385                         Sys_Quit();
386                         break;
387                 case ClientMessage:
388                         // window manager messages
389                         if ((event.xclient.format == 32) && (event.xclient.data.l[0] == wm_delete_window_atom))
390                                 Sys_Quit();
391                         break;
392                 case MapNotify:
393                         // window restored
394                         vid_hidden = false;
395                         break;
396                 case UnmapNotify:
397                         // window iconified/rolledup/whatever
398                         vid_hidden = true;
399                         break;
400                 case FocusIn:
401                         // window is now the input focus
402                         break;
403                 case FocusOut:
404                         // window is no longer the input focus
405                         break;
406                 case EnterNotify:
407                         // mouse entered window
408                         break;
409                 case LeaveNotify:
410                         // mouse left window
411                         break;
412                 }
413         }
414
415         if (dowarp)
416         {
417                 /* move the mouse to the window center again */
418                 p_mouse_x = scr_width / 2;
419                 p_mouse_y = scr_height / 2;
420                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
421         }
422
423 }
424
425 static void IN_DeactivateMouse( void )
426 {
427         if (!mouse_avail || !vidx11_display || !win)
428                 return;
429
430         if (mouse_active)
431         {
432                 uninstall_grabs();
433                 mouse_active = false;
434         }
435 }
436
437 static void IN_ActivateMouse( void )
438 {
439         if (!mouse_avail || !vidx11_display || !win)
440                 return;
441
442         if (!mouse_active)
443         {
444                 mouse_x = mouse_y = 0; // don't spazz
445                 install_grabs();
446                 mouse_active = true;
447         }
448 }
449
450
451 void VID_Shutdown(void)
452 {
453         if (!ctx || !vidx11_display)
454                 return;
455
456         if (vidx11_display)
457         {
458                 uninstall_grabs();
459
460                 if (vidmode_active)
461                         XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[0]);
462                 if (win)
463                         XDestroyWindow(vidx11_display, win);
464                 XCloseDisplay(vidx11_display);
465         }
466         vidmode_active = false;
467         vidx11_display = NULL;
468         win = 0;
469         ctx = NULL;
470
471         GL_CloseLibrary();
472 }
473
474 void signal_handler(int sig)
475 {
476         printf("Received signal %d, exiting...\n", sig);
477         Sys_Quit();
478         exit(0);
479 }
480
481 void InitSig(void)
482 {
483         signal(SIGHUP, signal_handler);
484         signal(SIGINT, signal_handler);
485         signal(SIGQUIT, signal_handler);
486         signal(SIGILL, signal_handler);
487         signal(SIGTRAP, signal_handler);
488         signal(SIGIOT, signal_handler);
489         signal(SIGBUS, signal_handler);
490         signal(SIGFPE, signal_handler);
491         signal(SIGSEGV, signal_handler);
492         signal(SIGTERM, signal_handler);
493 }
494
495 /*
496 =================
497 VID_GetWindowSize
498 =================
499 */
500 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
501 {
502         *x = *y = 0;
503         *width = scr_width;
504         *height = scr_height;
505 }
506
507 void VID_Finish (void)
508 {
509         int usemouse;
510         if (r_render.integer)
511         {
512                 qglFinish();
513                 qglXSwapBuffers(vidx11_display, win);
514         }
515
516 // handle the mouse state when windowed if that's changed
517         usemouse = false;
518         if (vid_mouse.integer && !key_consoleactive)
519                 usemouse = true;
520         if (vidmode_active)
521                 usemouse = true;
522         if (usemouse)
523         {
524                 if (!usingmouse)
525                 {
526                         usingmouse = true;
527                         IN_ActivateMouse ();
528                 }
529         }
530         else
531         {
532                 if (usingmouse)
533                 {
534                         usingmouse = false;
535                         IN_DeactivateMouse ();
536                 }
537         }
538 }
539
540 // LordHavoc: ported from SDL 1.2.2, this was far more difficult to port from
541 // SDL than to simply use the XFree gamma ramp extension, but that affects the
542 // whole screen even when the game window is inactive, this only affects the
543 // screen while the window is active, very desirable behavior :)
544 int VID_SetGamma(float prescale, float gamma, float scale, float base)
545 {
546 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
547 #if 1
548         return FALSE;
549 #else
550         int i, ncolors, c;
551         unsigned int Rmask, Gmask, Bmask, Rloss, Gloss, Bloss, Rshift, Gshift, Bshift, mask;
552         XColor xcmap[256];
553         unsigned short ramp[256];
554
555         if (COM_CheckParm("-nogamma"))
556                 return FALSE;
557
558         if (vidx11_visual->class != DirectColor)
559         {
560                 Con_Printf("X11 Visual class is %d, can only do gamma on %d\n", vidx11_visual->class, DirectColor);
561                 return FALSE;
562         }
563
564         Rmask = vidx11_visual->red_mask;
565         Gmask = vidx11_visual->green_mask;
566         Bmask = vidx11_visual->blue_mask;
567
568         Rshift = 0;
569         Rloss = 8;
570         if ((mask = Rmask))
571         {
572                 for (;!(mask & 1);mask >>= 1)
573                         ++Rshift;
574                 for (;(mask & 1);mask >>= 1)
575                         --Rloss;
576         }
577         Gshift = 0;
578         Gloss = 8;
579         if ((mask = Gmask))
580         {
581                 for (;!(mask & 1);mask >>= 1)
582                         ++Gshift;
583                 for (;(mask & 1);mask >>= 1)
584                         --Gloss;
585         }
586         Bshift = 0;
587         Bloss = 8;
588         if ((mask = Bmask))
589         {
590                 for (;!(mask & 1);mask >>= 1)
591                         ++Bshift;
592                 for (;(mask & 1);mask >>= 1)
593                         --Bloss;
594         }
595
596         BuildGammaTable16(prescale, gamma, scale, base, ramp);
597
598         // convert gamma ramp to palette (yes this seems odd)
599         ncolors = vidx11_visual->map_entries;
600         for (i = 0;i < ncolors;i++)
601         {
602                 c = (256 * i / ncolors);
603                 xcmap[i].pixel = ((c >> Rloss) << Rshift) | ((c >> Gloss) << Gshift) | ((c >> Bloss) << Bshift);
604                 xcmap[i].red   = ramp[c];
605                 xcmap[i].green = ramp[c];
606                 xcmap[i].blue  = ramp[c];
607                 xcmap[i].flags = (DoRed|DoGreen|DoBlue);
608         }
609         XStoreColors(vidx11_display, vidx11_colormap, xcmap, ncolors);
610         XSync(vidx11_display, false);
611         // FIXME: should this check for BadAccess/BadColor/BadValue errors produced by XStoreColors before setting this true?
612         return TRUE;
613 #endif
614 }
615
616 void VID_Init(void)
617 {
618         int i;
619 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
620 #if 0
621         int gammaattrib[] =
622         {
623                 GLX_RGBA,
624                 GLX_RED_SIZE, 1,
625                 GLX_GREEN_SIZE, 1,
626                 GLX_BLUE_SIZE, 1,
627                 GLX_DOUBLEBUFFER,
628                 GLX_DEPTH_SIZE, 1,
629                 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
630                 None
631         };
632 #endif
633         int nogammaattrib[] =
634         {
635                 GLX_RGBA,
636                 GLX_RED_SIZE, 1,
637                 GLX_GREEN_SIZE, 1,
638                 GLX_BLUE_SIZE, 1,
639                 GLX_DOUBLEBUFFER,
640                 GLX_DEPTH_SIZE, 1,
641                 None
642         };
643         int width = 640, height = 480;
644         XSetWindowAttributes attr;
645         unsigned long mask;
646         Window root;
647         XVisualInfo *visinfo;
648         qboolean fullscreen = true;
649         int MajorVersion, MinorVersion;
650
651         GL_OpenLibrary();
652
653         Cvar_RegisterVariable (&vid_dga);
654         Cvar_RegisterVariable (&vid_dga_mouseaccel);
655
656 // interpret command-line params
657
658 // set vid parameters
659         if ((i = COM_CheckParm("-window")) != 0)
660                 fullscreen = false;
661
662         if ((i = COM_CheckParm("-width")) != 0)
663                 width = atoi(com_argv[i+1]);
664
665         if ((i = COM_CheckParm("-height")) != 0)
666                 height = atoi(com_argv[i+1]);
667
668         if ((i = COM_CheckParm("-conwidth")) != 0)
669                 vid.conwidth = atoi(com_argv[i+1]);
670         else
671                 vid.conwidth = 640;
672
673         vid.conwidth &= 0xfff8; // make it a multiple of eight
674
675         if (vid.conwidth < 320)
676                 vid.conwidth = 320;
677
678         // pick a conheight that matches with correct aspect
679         vid.conheight = vid.conwidth*3 / 4;
680
681         if ((i = COM_CheckParm("-conheight")) != 0)
682                 vid.conheight = atoi(com_argv[i+1]);
683         if (vid.conheight < 200)
684                 vid.conheight = 200;
685
686         if (!(vidx11_display = XOpenDisplay(NULL)))
687         {
688                 fprintf(stderr, "Error couldn't open the X display\n");
689                 exit(1);
690         }
691
692         scrnum = DefaultScreen(vidx11_display);
693         root = RootWindow(vidx11_display, scrnum);
694
695         // Get video mode list
696         MajorVersion = MinorVersion = 0;
697         if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
698                 vidmode_ext = false;
699         else
700         {
701                 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
702                 vidmode_ext = true;
703         }
704
705         if ((qglXChooseVisual = GL_GetProcAddress("glXChooseVisual")) == NULL
706          || (qglXCreateContext = GL_GetProcAddress("glXCreateContext")) == NULL
707          || (qglXMakeCurrent = GL_GetProcAddress("glXMakeCurrent")) == NULL
708          || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL)
709                 Sys_Error("glX functions not found in libGL.so.1\n");
710
711         visinfo = NULL;
712 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
713 #if 0
714         if (!COM_CheckParm("-nogamma"))
715                 visinfo = qglXChooseVisual(vidx11_display, scrnum, gammaattrib);
716 #endif
717         if (!visinfo)
718         {
719                 visinfo = qglXChooseVisual(vidx11_display, scrnum, nogammaattrib);
720                 if (!visinfo)
721                 {
722                         Sys_Error("couldn't get an RGB, Double-buffered, Depth visual\n");
723                         exit(1);
724                 }
725         }
726
727         if (vidmode_ext)
728         {
729                 int best_fit, best_dist, dist, x, y;
730
731                 XF86VidModeGetAllModeLines(vidx11_display, scrnum, &num_vidmodes, &vidmodes);
732
733                 // Are we going fullscreen?  If so, let's change video mode
734                 if (fullscreen)
735                 {
736                         best_dist = 9999999;
737                         best_fit = -1;
738
739                         for (i = 0; i < num_vidmodes; i++)
740                         {
741                                 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
742                                         continue;
743
744                                 x = width - vidmodes[i]->hdisplay;
745                                 y = height - vidmodes[i]->vdisplay;
746                                 dist = (x * x) + (y * y);
747                                 if (dist < best_dist)
748                                 {
749                                         best_dist = dist;
750                                         best_fit = i;
751                                 }
752                         }
753
754                         if (best_fit != -1)
755                         {
756                                 // LordHavoc: changed from ActualWidth/ActualHeight =,
757                                 // to width/height =, so the window will take the full area of
758                                 // the mode chosen
759                                 width = vidmodes[best_fit]->hdisplay;
760                                 height = vidmodes[best_fit]->vdisplay;
761
762                                 // change to the mode
763                                 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[best_fit]);
764                                 vidmode_active = true;
765
766                                 // Move the viewport to top left
767                                 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
768                         }
769                         else
770                                 fullscreen = 0;
771                 }
772         }
773
774         // LordHavoc: save the visual for use in gamma ramp settings later
775         vidx11_visual = visinfo->visual;
776
777         /* window attributes */
778         attr.background_pixel = 0;
779         attr.border_pixel = 0;
780         // LordHavoc: save the colormap for later, too
781         vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
782         attr.event_mask = X_MASK;
783         if (vidmode_active)
784         {
785                 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
786                 attr.override_redirect = True;
787                 attr.backing_store = NotUseful;
788                 attr.save_under = False;
789         }
790         else
791                 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
792
793         win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
794         XStoreName(vidx11_display, win, gamename);
795         XMapWindow(vidx11_display, win);
796
797         // LordHavoc: making the close button on a window do the right thing
798         // seems to involve this mess, sigh...
799         wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
800         XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
801
802         if (vidmode_active)
803         {
804                 XMoveWindow(vidx11_display, win, 0, 0);
805                 XRaiseWindow(vidx11_display, win);
806                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
807                 XFlush(vidx11_display);
808                 // Move the viewport to top left
809                 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
810         }
811
812         XFlush(vidx11_display);
813
814         ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
815
816         qglXMakeCurrent(vidx11_display, win, ctx);
817
818         scr_width = width;
819         scr_height = height;
820
821         if (vid.conheight > height)
822                 vid.conheight = height;
823         if (vid.conwidth > width)
824                 vid.conwidth = width;
825
826         InitSig(); // trap evil signals
827
828         vid_hidden = false;
829
830         GL_Init();
831
832         Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
833 }
834
835 void Sys_SendKeyEvents(void)
836 {
837         HandleEvents();
838 }
839
840 void IN_Init(void)
841 {
842         if (COM_CheckParm ("-nomouse"))
843                 mouse_avail = false;
844 }
845
846 void IN_Shutdown(void)
847 {
848 }
849
850 /*
851 ===========
852 IN_Commands
853 ===========
854 */
855 void IN_Commands (void)
856 {
857 }
858
859 void IN_Move (usercmd_t *cmd)
860 {
861         if (mouse_avail)
862                 IN_Mouse(cmd, mouse_x, mouse_y);
863         mouse_x = 0;
864         mouse_y = 0;
865 }
866