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