]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - vid_glx.c
fixed some sloppyness in my earlier input merging
[xonotic/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 int p_mouse_x, p_mouse_y;
59
60 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
61 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
62
63 qboolean vidmode_ext = false;
64
65 static int win_x, win_y;
66
67 static int scr_width, scr_height;
68
69 static XF86VidModeModeInfo **vidmodes;
70 //static int default_dotclock_vidmode;
71 static int num_vidmodes;
72 static qboolean vidmode_active = false;
73
74 static Visual *vidx11_visual;
75 static Colormap vidx11_colormap;
76
77 /*-----------------------------------------------------------------------*/
78
79 float           gldepthmin, gldepthmax;
80
81 const char *gl_vendor;
82 const char *gl_renderer;
83 const char *gl_version;
84 const char *gl_extensions;
85
86 /*-----------------------------------------------------------------------*/
87 static int
88 XLateKey(XKeyEvent *ev/*, qboolean modified*/)
89 {
90 //      char tmp[2];
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 (/*!modified && */((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/*depth*/);
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 //      XSync(vidx11_display, True);
254 }
255
256 static void uninstall_grabs(void)
257 {
258         if (!vidx11_display || !win)
259                 return;
260
261         if (vid_dga.integer == 1)
262                 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
263
264         XUngrabPointer(vidx11_display, CurrentTime);
265         XUngrabKeyboard(vidx11_display, CurrentTime);
266
267 // inviso cursor
268         XUndefineCursor(vidx11_display, win);
269
270         mouse_active = false;
271 }
272
273 static void HandleEvents(void)
274 {
275         XEvent event;
276 //      KeySym ks;
277         qboolean dowarp = false;
278
279         if (!vidx11_display)
280                 return;
281
282         while (XPending(vidx11_display))
283         {
284                 XNextEvent(vidx11_display, &event);
285
286                 switch (event.type)
287                 {
288                 case KeyPress:
289                 case KeyRelease:
290                         Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
291                         break;
292
293                 case MotionNotify:
294                         if (usingmouse)
295                         {
296                                 if (vid_dga.integer == 1)
297                                 {
298                                         mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
299                                         mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
300                                 }
301                                 else
302                                 {
303                                         /*
304                                         if (!p_mouse_x && !p_mouse_y)
305                                         {
306                                                 Con_Printf("event->xmotion.x: %d\n", event.xmotion.x);
307                                                 Con_Printf("event->xmotion.y: %d\n", event.xmotion.y);
308                                         }
309                                         */
310                                         //if (usingmouse)
311                                         {
312                                                 if (!event.xmotion.send_event)
313                                                 {
314                                                         mouse_x += event.xmotion.x - p_mouse_x;
315                                                         mouse_y += event.xmotion.y - p_mouse_y;
316                                                         if (abs(scr_width/2 - event.xmotion.x) > scr_width / 4 || abs(scr_height/2 - event.xmotion.y) > scr_height / 4)
317                                                                 dowarp = true;
318                                                 }
319                                         }
320                                         /*
321                                         else
322                                         {
323                                                 mouse_x += (event.xmotion.x - p_mouse_x);
324                                                 mouse_y += (event.xmotion.y - p_mouse_y);
325                                         }
326                                         */
327                                         p_mouse_x = event.xmotion.x;
328                                         p_mouse_y = event.xmotion.y;
329                                 }
330                         }
331                         else
332                                 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
333                         break;
334
335                 case ButtonPress:
336                         switch(event.xbutton.button)
337                         {
338                         case 1:
339                                 Key_Event(K_MOUSE1, true);
340                                 break;
341                         case 2:
342                                 Key_Event(K_MOUSE3, true);
343                                 break;
344                         case 3:
345                                 Key_Event(K_MOUSE2, true);
346                                 break;
347                         case 4:
348                                 Key_Event(K_MWHEELUP, true);
349                                 break;
350                         case 5:
351                                 Key_Event(K_MWHEELDOWN, true);
352                                 break;
353                 default:
354                                 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-5 expected\n", event.xbutton.button);
355                                 break;
356                         }
357                         break;
358
359                 case ButtonRelease:
360                         switch(event.xbutton.button)
361                         {
362                         case 1:
363                                 Key_Event(K_MOUSE1, false);
364                                 break;
365                         case 2:
366                                 Key_Event(K_MOUSE3, false);
367                                 break;
368                         case 3:
369                                 Key_Event(K_MOUSE2, false);
370                                 break;
371                         case 4:
372                                 Key_Event(K_MWHEELUP, false);
373                                 break;
374                         case 5:
375                                 Key_Event(K_MWHEELDOWN, false);
376                                 break;
377                 default:
378                                 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-5 expected\n", event.xbutton.button);
379                                 break;
380                         }
381                         break;
382
383                 case CreateNotify :
384                         win_x = event.xcreatewindow.x;
385                         win_y = event.xcreatewindow.y;
386                         break;
387
388                 case ConfigureNotify :
389                         win_x = event.xconfigure.x;
390                         win_y = event.xconfigure.y;
391                         break;
392                 }
393         }
394
395         if (dowarp)
396         {
397                 /* move the mouse to the window center again */
398                 p_mouse_x = scr_width / 2;
399                 p_mouse_y = scr_height / 2;
400                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
401         }
402
403 }
404
405 static void IN_DeactivateMouse( void )
406 {
407         if (!mouse_avail || !vidx11_display || !win)
408                 return;
409
410         if (mouse_active)
411         {
412                 uninstall_grabs();
413                 mouse_active = false;
414         }
415 }
416
417 static void IN_ActivateMouse( void )
418 {
419         if (!mouse_avail || !vidx11_display || !win)
420                 return;
421
422         if (!mouse_active)
423         {
424                 mouse_x = mouse_y = 0; // don't spazz
425                 install_grabs();
426                 mouse_active = true;
427         }
428 }
429
430
431 void VID_Shutdown(void)
432 {
433         if (!ctx || !vidx11_display)
434                 return;
435
436         if (vidx11_display)
437         {
438                 uninstall_grabs();
439
440                 if (vidmode_active)
441                         XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[0]);
442 /* Disabled, causes a segfault during shutdown.
443                 if (ctx)
444                         glXDestroyContext(vidx11_display, ctx);
445 */
446                 if (win)
447                         XDestroyWindow(vidx11_display, win);
448                 XCloseDisplay(vidx11_display);
449         }
450         vidmode_active = false;
451         vidx11_display = NULL;
452         win = 0;
453         ctx = NULL;
454 }
455
456 void signal_handler(int sig)
457 {
458         printf("Received signal %d, exiting...\n", sig);
459         Sys_Quit();
460         exit(0);
461 }
462
463 void InitSig(void)
464 {
465         signal(SIGHUP, signal_handler);
466         signal(SIGINT, signal_handler);
467         signal(SIGQUIT, signal_handler);
468         signal(SIGILL, signal_handler);
469         signal(SIGTRAP, signal_handler);
470         signal(SIGIOT, signal_handler);
471         signal(SIGBUS, signal_handler);
472         signal(SIGFPE, signal_handler);
473         signal(SIGSEGV, signal_handler);
474         signal(SIGTERM, signal_handler);
475 }
476
477 /*
478 =================
479 VID_GetWindowSize
480 =================
481 */
482 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
483 {
484         *x = *y = 0;
485         *width = scr_width;
486         *height = scr_height;
487 }
488
489 void VID_Finish (void)
490 {
491         int usemouse;
492         if (r_render.integer)
493         {
494                 glFinish();
495                 glXSwapBuffers(vidx11_display, win);
496         }
497
498 // handle the mouse state when windowed if that's changed
499         usemouse = false;
500         if (vid_mouse.integer && key_dest == key_game)
501                 usemouse = true;
502         if (vidmode_active)
503                 usemouse = true;
504         if (usemouse)
505         {
506                 if (!usingmouse)
507                 {
508                         usingmouse = true;
509                         IN_ActivateMouse ();
510                 }
511         }
512         else
513         {
514                 if (usingmouse)
515                 {
516                         usingmouse = false;
517                         IN_DeactivateMouse ();
518                 }
519         }
520 }
521
522 // LordHavoc: ported from SDL 1.2.2, this was far more difficult to port from
523 // SDL than to simply use the XFree gamma ramp extension, but that affects the
524 // whole screen even when the game window is inactive, this only affects the
525 // screen while the window is active, very desirable behavior :)
526 int VID_SetGamma(float prescale, float gamma, float scale, float base)
527 {
528 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
529 #if 1
530         return FALSE;
531 #else
532         int i, ncolors, c;
533         unsigned int Rmask, Gmask, Bmask, Rloss, Gloss, Bloss, Rshift, Gshift, Bshift, mask;
534         XColor xcmap[256];
535         unsigned short ramp[256];
536
537         if (COM_CheckParm("-nogamma"))
538                 return FALSE;
539
540         if (vidx11_visual->class != DirectColor)
541         {
542                 Con_Printf("X11 Visual class is %d, can only do gamma on %d\n", vidx11_visual->class, DirectColor);
543                 return FALSE;
544         }
545
546         Rmask = vidx11_visual->red_mask;
547         Gmask = vidx11_visual->green_mask;
548         Bmask = vidx11_visual->blue_mask;
549
550         Rshift = 0;
551         Rloss = 8;
552         if ((mask = Rmask))
553         {
554                 for (;!(mask & 1);mask >>= 1)
555                         ++Rshift;
556                 for (;(mask & 1);mask >>= 1)
557                         --Rloss;
558         }
559         Gshift = 0;
560         Gloss = 8;
561         if ((mask = Gmask))
562         {
563                 for (;!(mask & 1);mask >>= 1)
564                         ++Gshift;
565                 for (;(mask & 1);mask >>= 1)
566                         --Gloss;
567         }
568         Bshift = 0;
569         Bloss = 8;
570         if ((mask = Bmask))
571         {
572                 for (;!(mask & 1);mask >>= 1)
573                         ++Bshift;
574                 for (;(mask & 1);mask >>= 1)
575                         --Bloss;
576         }
577
578         BuildGammaTable16(prescale, gamma, scale, base, ramp);
579
580         // convert gamma ramp to palette (yes this seems odd)
581         ncolors = vidx11_visual->map_entries;
582         for (i = 0;i < ncolors;i++)
583         {
584                 c = (256 * i / ncolors);
585                 xcmap[i].pixel = ((c >> Rloss) << Rshift) | ((c >> Gloss) << Gshift) | ((c >> Bloss) << Bshift);
586                 xcmap[i].red   = ramp[c];
587                 xcmap[i].green = ramp[c];
588                 xcmap[i].blue  = ramp[c];
589                 xcmap[i].flags = (DoRed|DoGreen|DoBlue);
590         }
591         XStoreColors(vidx11_display, vidx11_colormap, xcmap, ncolors);
592         XSync(vidx11_display, false);
593         // FIXME: should this check for BadAccess/BadColor/BadValue errors produced by XStoreColors before setting this true?
594         return TRUE;
595 #endif
596 }
597
598 void VID_Init(void)
599 {
600         int i;
601 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
602 #if 0
603         int gammaattrib[] =
604         {
605                 GLX_RGBA,
606                 GLX_RED_SIZE, 1,
607                 GLX_GREEN_SIZE, 1,
608                 GLX_BLUE_SIZE, 1,
609                 GLX_DOUBLEBUFFER,
610                 GLX_DEPTH_SIZE, 1,
611                 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
612                 None
613         };
614 #endif
615         int nogammaattrib[] =
616         {
617                 GLX_RGBA,
618                 GLX_RED_SIZE, 1,
619                 GLX_GREEN_SIZE, 1,
620                 GLX_BLUE_SIZE, 1,
621                 GLX_DOUBLEBUFFER,
622                 GLX_DEPTH_SIZE, 1,
623                 None
624         };
625 //      char    gldir[MAX_OSPATH];
626         int width = 640, height = 480;
627         XSetWindowAttributes attr;
628         unsigned long mask;
629         Window root;
630         XVisualInfo *visinfo;
631         qboolean fullscreen = true;
632         int MajorVersion, MinorVersion;
633
634         Cvar_RegisterVariable (&vid_dga);
635         Cvar_RegisterVariable (&vid_dga_mouseaccel);
636
637 // interpret command-line params
638
639 // set vid parameters
640         if ((i = COM_CheckParm("-window")) != 0)
641                 fullscreen = false;
642
643         if ((i = COM_CheckParm("-width")) != 0)
644                 width = atoi(com_argv[i+1]);
645
646         if ((i = COM_CheckParm("-height")) != 0)
647                 height = atoi(com_argv[i+1]);
648
649         if ((i = COM_CheckParm("-conwidth")) != 0)
650                 vid.conwidth = atoi(com_argv[i+1]);
651         else
652                 vid.conwidth = 640;
653
654         vid.conwidth &= 0xfff8; // make it a multiple of eight
655
656         if (vid.conwidth < 320)
657                 vid.conwidth = 320;
658
659         // pick a conheight that matches with correct aspect
660         vid.conheight = vid.conwidth*3 / 4;
661
662         if ((i = COM_CheckParm("-conheight")) != 0)
663                 vid.conheight = atoi(com_argv[i+1]);
664         if (vid.conheight < 200)
665                 vid.conheight = 200;
666
667         if (!(vidx11_display = XOpenDisplay(NULL)))
668         {
669                 fprintf(stderr, "Error couldn't open the X display\n");
670                 exit(1);
671         }
672
673         scrnum = DefaultScreen(vidx11_display);
674         root = RootWindow(vidx11_display, scrnum);
675
676         // Get video mode list
677         MajorVersion = MinorVersion = 0;
678         if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
679                 vidmode_ext = false;
680         else
681         {
682                 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
683                 vidmode_ext = true;
684         }
685
686         visinfo = NULL;
687 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
688 #if 0
689         if (!COM_CheckParm("-nogamma"))
690                 visinfo = glXChooseVisual(vidx11_display, scrnum, gammaattrib);
691 #endif
692         if (!visinfo)
693         {
694                 visinfo = glXChooseVisual(vidx11_display, scrnum, nogammaattrib);
695                 if (!visinfo)
696                 {
697                         fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
698                         exit(1);
699                 }
700         }
701
702         if (vidmode_ext)
703         {
704                 int best_fit, best_dist, dist, x, y;
705
706                 XF86VidModeGetAllModeLines(vidx11_display, scrnum, &num_vidmodes, &vidmodes);
707
708                 // Are we going fullscreen?  If so, let's change video mode
709                 if (fullscreen)
710                 {
711                         best_dist = 9999999;
712                         best_fit = -1;
713
714                         for (i = 0; i < num_vidmodes; i++)
715                         {
716                                 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
717                                         continue;
718
719                                 x = width - vidmodes[i]->hdisplay;
720                                 y = height - vidmodes[i]->vdisplay;
721                                 dist = (x * x) + (y * y);
722                                 if (dist < best_dist)
723                                 {
724                                         best_dist = dist;
725                                         best_fit = i;
726                                 }
727                         }
728
729                         if (best_fit != -1)
730                         {
731                                 // LordHavoc: changed from ActualWidth/ActualHeight =,
732                                 // to width/height =, so the window will take the full area of
733                                 // the mode chosen
734                                 width = vidmodes[best_fit]->hdisplay;
735                                 height = vidmodes[best_fit]->vdisplay;
736
737                                 // change to the mode
738                                 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[best_fit]);
739                                 vidmode_active = true;
740
741                                 // Move the viewport to top left
742                                 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
743                         }
744                         else
745                                 fullscreen = 0;
746                 }
747         }
748
749         // LordHavoc: save the visual for use in gamma ramp settings later
750         vidx11_visual = visinfo->visual;
751
752         /* window attributes */
753         attr.background_pixel = 0;
754         attr.border_pixel = 0;
755         // LordHavoc: save the colormap for later, too
756         vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
757         attr.event_mask = X_MASK;
758         if (vidmode_active)
759         {
760                 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
761                 attr.override_redirect = True;
762                 attr.backing_store = NotUseful;
763                 attr.save_under = False;
764         }
765         else
766                 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
767
768         win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
769         XStoreName(vidx11_display, win, gamename);
770         XMapWindow(vidx11_display, win);
771
772         if (vidmode_active)
773         {
774                 XMoveWindow(vidx11_display, win, 0, 0);
775                 XRaiseWindow(vidx11_display, win);
776                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
777                 XFlush(vidx11_display);
778                 // Move the viewport to top left
779                 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
780         }
781
782         XFlush(vidx11_display);
783
784         ctx = glXCreateContext(vidx11_display, visinfo, NULL, True);
785
786         glXMakeCurrent(vidx11_display, win, ctx);
787
788         scr_width = width;
789         scr_height = height;
790
791         if (vid.conheight > height)
792                 vid.conheight = height;
793         if (vid.conwidth > width)
794                 vid.conwidth = width;
795
796         InitSig(); // trap evil signals
797
798         GL_Init();
799
800         Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
801
802         // force a surface cache flush
803 //      vid.recalc_refdef = 1;
804 }
805
806 void Sys_SendKeyEvents(void)
807 {
808         HandleEvents();
809 }
810
811 void IN_Init(void)
812 {
813         if (COM_CheckParm ("-nomouse"))
814                 mouse_avail = false;
815 }
816
817 void IN_Shutdown(void)
818 {
819 }
820
821 /*
822 ===========
823 IN_Commands
824 ===========
825 */
826 void IN_Commands (void)
827 {
828 }
829
830 void IN_Move (usercmd_t *cmd)
831 {
832         if (mouse_avail)
833                 IN_Mouse(cmd, mouse_x, mouse_y);
834         mouse_x = 0;
835         mouse_y = 0;
836 }
837
838