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