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