got rid of buildnumber.c and buildnum program, now uses builddate.c (touched each...
[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_dga);
640         Cvar_RegisterVariable (&vid_dga_mouseaccel);
641         Cvar_RegisterVariable (&m_filter);
642
643 // interpret command-line params
644
645 // set vid parameters
646         if ((i = COM_CheckParm("-window")) != 0)
647                 fullscreen = false;
648
649         if ((i = COM_CheckParm("-width")) != 0)
650                 width = atoi(com_argv[i+1]);
651
652         if ((i = COM_CheckParm("-height")) != 0)
653                 height = atoi(com_argv[i+1]);
654
655         if ((i = COM_CheckParm("-conwidth")) != 0)
656                 vid.conwidth = atoi(com_argv[i+1]);
657         else
658                 vid.conwidth = 640;
659
660         vid.conwidth &= 0xfff8; // make it a multiple of eight
661
662         if (vid.conwidth < 320)
663                 vid.conwidth = 320;
664
665         // pick a conheight that matches with correct aspect
666         vid.conheight = vid.conwidth*3 / 4;
667
668         if ((i = COM_CheckParm("-conheight")) != 0)
669                 vid.conheight = atoi(com_argv[i+1]);
670         if (vid.conheight < 200)
671                 vid.conheight = 200;
672
673         if (!(vidx11_display = XOpenDisplay(NULL)))
674         {
675                 fprintf(stderr, "Error couldn't open the X display\n");
676                 exit(1);
677         }
678
679         scrnum = DefaultScreen(vidx11_display);
680         root = RootWindow(vidx11_display, scrnum);
681
682         // Get video mode list
683         MajorVersion = MinorVersion = 0;
684         if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
685                 vidmode_ext = false;
686         else
687         {
688                 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
689                 vidmode_ext = true;
690         }
691
692         visinfo = NULL;
693 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
694 #if 0
695         if (!COM_CheckParm("-nogamma"))
696                 visinfo = glXChooseVisual(vidx11_display, scrnum, gammaattrib);
697 #endif
698         if (!visinfo)
699         {
700                 visinfo = glXChooseVisual(vidx11_display, scrnum, nogammaattrib);
701                 if (!visinfo)
702                 {
703                         fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
704                         exit(1);
705                 }
706         }
707
708         if (vidmode_ext)
709         {
710                 int best_fit, best_dist, dist, x, y;
711
712                 XF86VidModeGetAllModeLines(vidx11_display, scrnum, &num_vidmodes, &vidmodes);
713
714                 // Are we going fullscreen?  If so, let's change video mode
715                 if (fullscreen)
716                 {
717                         best_dist = 9999999;
718                         best_fit = -1;
719
720                         for (i = 0; i < num_vidmodes; i++)
721                         {
722                                 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
723                                         continue;
724
725                                 x = width - vidmodes[i]->hdisplay;
726                                 y = height - vidmodes[i]->vdisplay;
727                                 dist = (x * x) + (y * y);
728                                 if (dist < best_dist)
729                                 {
730                                         best_dist = dist;
731                                         best_fit = i;
732                                 }
733                         }
734
735                         if (best_fit != -1)
736                         {
737                                 // LordHavoc: changed from ActualWidth/ActualHeight =,
738                                 // to width/height =, so the window will take the full area of
739                                 // the mode chosen
740                                 width = vidmodes[best_fit]->hdisplay;
741                                 height = vidmodes[best_fit]->vdisplay;
742
743                                 // change to the mode
744                                 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[best_fit]);
745                                 vidmode_active = true;
746
747                                 // Move the viewport to top left
748                                 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
749                         }
750                         else
751                                 fullscreen = 0;
752                 }
753         }
754
755         // LordHavoc: save the visual for use in gamma ramp settings later
756         vidx11_visual = visinfo->visual;
757
758         /* window attributes */
759         attr.background_pixel = 0;
760         attr.border_pixel = 0;
761         // LordHavoc: save the colormap for later, too
762         vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
763         attr.event_mask = X_MASK;
764         if (vidmode_active)
765         {
766                 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
767                 attr.override_redirect = True;
768                 attr.backing_store = NotUseful;
769                 attr.save_under = False;
770         }
771         else
772                 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
773
774         win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
775         XStoreName(vidx11_display, win, gamename);
776         XMapWindow(vidx11_display, win);
777
778         if (vidmode_active)
779         {
780                 XMoveWindow(vidx11_display, win, 0, 0);
781                 XRaiseWindow(vidx11_display, win);
782                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
783                 XFlush(vidx11_display);
784                 // Move the viewport to top left
785                 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
786         }
787
788         XFlush(vidx11_display);
789
790         ctx = glXCreateContext(vidx11_display, visinfo, NULL, True);
791
792         glXMakeCurrent(vidx11_display, win, ctx);
793
794         scr_width = width;
795         scr_height = height;
796
797         if (vid.conheight > height)
798                 vid.conheight = height;
799         if (vid.conwidth > width)
800                 vid.conwidth = width;
801
802         InitSig(); // trap evil signals
803
804         GL_Init();
805
806         Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
807
808         // force a surface cache flush
809 //      vid.recalc_refdef = 1;
810 }
811
812 void Sys_SendKeyEvents(void)
813 {
814         HandleEvents();
815 }
816
817 void IN_Init(void)
818 {
819         if (COM_CheckParm ("-nomouse"))
820                 mouse_avail = false;
821 }
822
823 void IN_Shutdown(void)
824 {
825 }
826
827 /*
828 ===========
829 IN_Commands
830 ===========
831 */
832 void IN_Commands (void)
833 {
834 }
835
836 /*
837 ===========
838 IN_Move
839 ===========
840 */
841 void IN_MouseMove (usercmd_t *cmd)
842 {
843         if (!mouse_avail)
844                 return;
845
846         if (m_filter.integer)
847         {
848                 mouse_x = (mouse_x + old_mouse_x) * 0.5;
849                 mouse_y = (mouse_y + old_mouse_y) * 0.5;
850
851                 old_mouse_x = mouse_x;
852                 old_mouse_y = mouse_y;
853         }
854
855         mouse_x *= sensitivity.value;
856         mouse_y *= sensitivity.value;
857
858         if (in_strafe.state & 1)
859                 cmd->sidemove += m_side.value * mouse_x;
860         else
861                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
862
863         //if (freelook)
864                 V_StopPitchDrift ();
865
866         if (/*freelook && */!(in_strafe.state & 1))
867         {
868                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
869                 cl.viewangles[PITCH] = bound (-90, cl.viewangles[PITCH], 90);
870         }
871         else
872         {
873                 if ((in_strafe.state & 1) && noclip_anglehack)
874                         cmd->upmove -= m_forward.value * mouse_y;
875                 else
876                         cmd->forwardmove -= m_forward.value * mouse_y;
877         }
878         mouse_x = mouse_y = 0.0;
879 }
880
881 void IN_Move (usercmd_t *cmd)
882 {
883         IN_MouseMove(cmd);
884 }
885
886