]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_glx.c
renamed byte to qbyte throughout engine to eliminate a mingw conflict
[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 VID_GetWindowSize
482 =================
483 */
484 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
485 {
486         *x = *y = 0;
487         *width = scr_width;
488         *height = scr_height;
489 }
490
491 void VID_Finish (void)
492 {
493         int usemouse;
494         if (r_render.integer)
495         {
496                 glFinish();
497                 glXSwapBuffers(vidx11_display, win);
498         }
499
500 // handle the mouse state when windowed if that's changed
501         usemouse = false;
502         if (vid_mouse.integer && key_dest == key_game)
503                 usemouse = true;
504         if (vidmode_active)
505                 usemouse = true;
506         if (usemouse)
507         {
508                 if (!usingmouse)
509                 {
510                         usingmouse = true;
511                         IN_ActivateMouse ();
512                 }
513         }
514         else
515         {
516                 if (usingmouse)
517                 {
518                         usingmouse = false;
519                         IN_DeactivateMouse ();
520                 }
521         }
522 }
523
524 // LordHavoc: ported from SDL 1.2.2, this was far more difficult to port from
525 // SDL than to simply use the XFree gamma ramp extension, but that affects the
526 // whole screen even when the game window is inactive, this only affects the
527 // screen while the window is active, very desirable behavior :)
528 int VID_SetGamma(float prescale, float gamma, float scale, float base)
529 {
530 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
531 #if 1
532         return FALSE;
533 #else
534         int i, ncolors, c;
535         unsigned int Rmask, Gmask, Bmask, Rloss, Gloss, Bloss, Rshift, Gshift, Bshift, mask;
536         XColor xcmap[256];
537         unsigned short ramp[256];
538
539         if (COM_CheckParm("-nogamma"))
540                 return FALSE;
541
542         if (vidx11_visual->class != DirectColor)
543         {
544                 Con_Printf("X11 Visual class is %d, can only do gamma on %d\n", vidx11_visual->class, DirectColor);
545                 return FALSE;
546         }
547
548         Rmask = vidx11_visual->red_mask;
549         Gmask = vidx11_visual->green_mask;
550         Bmask = vidx11_visual->blue_mask;
551
552         Rshift = 0;
553         Rloss = 8;
554         if ((mask = Rmask))
555         {
556                 for (;!(mask & 1);mask >>= 1)
557                         ++Rshift;
558                 for (;(mask & 1);mask >>= 1)
559                         --Rloss;
560         }
561         Gshift = 0;
562         Gloss = 8;
563         if ((mask = Gmask))
564         {
565                 for (;!(mask & 1);mask >>= 1)
566                         ++Gshift;
567                 for (;(mask & 1);mask >>= 1)
568                         --Gloss;
569         }
570         Bshift = 0;
571         Bloss = 8;
572         if ((mask = Bmask))
573         {
574                 for (;!(mask & 1);mask >>= 1)
575                         ++Bshift;
576                 for (;(mask & 1);mask >>= 1)
577                         --Bloss;
578         }
579
580         BuildGammaTable16(prescale, gamma, scale, base, ramp);
581
582         // convert gamma ramp to palette (yes this seems odd)
583         ncolors = vidx11_visual->map_entries;
584         for (i = 0;i < ncolors;i++)
585         {
586                 c = (256 * i / ncolors);
587                 xcmap[i].pixel = ((c >> Rloss) << Rshift) | ((c >> Gloss) << Gshift) | ((c >> Bloss) << Bshift);
588                 xcmap[i].red   = ramp[c];
589                 xcmap[i].green = ramp[c];
590                 xcmap[i].blue  = ramp[c];
591                 xcmap[i].flags = (DoRed|DoGreen|DoBlue);
592         }
593         XStoreColors(vidx11_display, vidx11_colormap, xcmap, ncolors);
594         XSync(vidx11_display, false);
595         // FIXME: should this check for BadAccess/BadColor/BadValue errors produced by XStoreColors before setting this true?
596         return TRUE;
597 #endif
598 }
599
600 void VID_Init(void)
601 {
602         int i;
603 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
604 #if 0
605         int gammaattrib[] =
606         {
607                 GLX_RGBA,
608                 GLX_RED_SIZE, 1,
609                 GLX_GREEN_SIZE, 1,
610                 GLX_BLUE_SIZE, 1,
611                 GLX_DOUBLEBUFFER,
612                 GLX_DEPTH_SIZE, 1,
613                 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
614                 None
615         };
616 #endif
617         int nogammaattrib[] =
618         {
619                 GLX_RGBA,
620                 GLX_RED_SIZE, 1,
621                 GLX_GREEN_SIZE, 1,
622                 GLX_BLUE_SIZE, 1,
623                 GLX_DOUBLEBUFFER,
624                 GLX_DEPTH_SIZE, 1,
625                 None
626         };
627 //      char    gldir[MAX_OSPATH];
628         int width = 640, height = 480;
629         XSetWindowAttributes attr;
630         unsigned long mask;
631         Window root;
632         XVisualInfo *visinfo;
633         qboolean fullscreen = true;
634         int MajorVersion, MinorVersion;
635
636         Cvar_RegisterVariable (&vid_dga);
637         Cvar_RegisterVariable (&vid_dga_mouseaccel);
638         Cvar_RegisterVariable (&m_filter);
639
640 // interpret command-line params
641
642 // set vid parameters
643         if ((i = COM_CheckParm("-window")) != 0)
644                 fullscreen = false;
645
646         if ((i = COM_CheckParm("-width")) != 0)
647                 width = atoi(com_argv[i+1]);
648
649         if ((i = COM_CheckParm("-height")) != 0)
650                 height = atoi(com_argv[i+1]);
651
652         if ((i = COM_CheckParm("-conwidth")) != 0)
653                 vid.conwidth = atoi(com_argv[i+1]);
654         else
655                 vid.conwidth = 640;
656
657         vid.conwidth &= 0xfff8; // make it a multiple of eight
658
659         if (vid.conwidth < 320)
660                 vid.conwidth = 320;
661
662         // pick a conheight that matches with correct aspect
663         vid.conheight = vid.conwidth*3 / 4;
664
665         if ((i = COM_CheckParm("-conheight")) != 0)
666                 vid.conheight = atoi(com_argv[i+1]);
667         if (vid.conheight < 200)
668                 vid.conheight = 200;
669
670         if (!(vidx11_display = XOpenDisplay(NULL)))
671         {
672                 fprintf(stderr, "Error couldn't open the X display\n");
673                 exit(1);
674         }
675
676         scrnum = DefaultScreen(vidx11_display);
677         root = RootWindow(vidx11_display, scrnum);
678
679         // Get video mode list
680         MajorVersion = MinorVersion = 0;
681         if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
682                 vidmode_ext = false;
683         else
684         {
685                 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
686                 vidmode_ext = true;
687         }
688
689         visinfo = NULL;
690 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
691 #if 0
692         if (!COM_CheckParm("-nogamma"))
693                 visinfo = glXChooseVisual(vidx11_display, scrnum, gammaattrib);
694 #endif
695         if (!visinfo)
696         {
697                 visinfo = glXChooseVisual(vidx11_display, scrnum, nogammaattrib);
698                 if (!visinfo)
699                 {
700                         fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
701                         exit(1);
702                 }
703         }
704
705         if (vidmode_ext)
706         {
707                 int best_fit, best_dist, dist, x, y;
708
709                 XF86VidModeGetAllModeLines(vidx11_display, scrnum, &num_vidmodes, &vidmodes);
710
711                 // Are we going fullscreen?  If so, let's change video mode
712                 if (fullscreen)
713                 {
714                         best_dist = 9999999;
715                         best_fit = -1;
716
717                         for (i = 0; i < num_vidmodes; i++)
718                         {
719                                 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
720                                         continue;
721
722                                 x = width - vidmodes[i]->hdisplay;
723                                 y = height - vidmodes[i]->vdisplay;
724                                 dist = (x * x) + (y * y);
725                                 if (dist < best_dist)
726                                 {
727                                         best_dist = dist;
728                                         best_fit = i;
729                                 }
730                         }
731
732                         if (best_fit != -1)
733                         {
734                                 // LordHavoc: changed from ActualWidth/ActualHeight =,
735                                 // to width/height =, so the window will take the full area of
736                                 // the mode chosen
737                                 width = vidmodes[best_fit]->hdisplay;
738                                 height = vidmodes[best_fit]->vdisplay;
739
740                                 // change to the mode
741                                 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[best_fit]);
742                                 vidmode_active = true;
743
744                                 // Move the viewport to top left
745                                 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
746                         }
747                         else
748                                 fullscreen = 0;
749                 }
750         }
751
752         // LordHavoc: save the visual for use in gamma ramp settings later
753         vidx11_visual = visinfo->visual;
754
755         /* window attributes */
756         attr.background_pixel = 0;
757         attr.border_pixel = 0;
758         // LordHavoc: save the colormap for later, too
759         vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
760         attr.event_mask = X_MASK;
761         if (vidmode_active)
762         {
763                 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
764                 attr.override_redirect = True;
765                 attr.backing_store = NotUseful;
766                 attr.save_under = False;
767         }
768         else
769                 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
770
771         win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
772         XStoreName(vidx11_display, win, gamename);
773         XMapWindow(vidx11_display, win);
774
775         if (vidmode_active)
776         {
777                 XMoveWindow(vidx11_display, win, 0, 0);
778                 XRaiseWindow(vidx11_display, win);
779                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
780                 XFlush(vidx11_display);
781                 // Move the viewport to top left
782                 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
783         }
784
785         XFlush(vidx11_display);
786
787         ctx = glXCreateContext(vidx11_display, visinfo, NULL, True);
788
789         glXMakeCurrent(vidx11_display, win, ctx);
790
791         scr_width = width;
792         scr_height = height;
793
794         if (vid.conheight > height)
795                 vid.conheight = height;
796         if (vid.conwidth > width)
797                 vid.conwidth = width;
798
799         InitSig(); // trap evil signals
800
801         GL_Init();
802
803         Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
804
805         // force a surface cache flush
806 //      vid.recalc_refdef = 1;
807 }
808
809 void Sys_SendKeyEvents(void)
810 {
811         HandleEvents();
812 }
813
814 void IN_Init(void)
815 {
816         if (COM_CheckParm ("-nomouse"))
817                 mouse_avail = false;
818 }
819
820 void IN_Shutdown(void)
821 {
822 }
823
824 /*
825 ===========
826 IN_Commands
827 ===========
828 */
829 void IN_Commands (void)
830 {
831 }
832
833 /*
834 ===========
835 IN_Move
836 ===========
837 */
838 void IN_MouseMove (usercmd_t *cmd)
839 {
840         if (!mouse_avail)
841                 return;
842
843         if (m_filter.integer)
844         {
845                 mouse_x = (mouse_x + old_mouse_x) * 0.5;
846                 mouse_y = (mouse_y + old_mouse_y) * 0.5;
847
848                 old_mouse_x = mouse_x;
849                 old_mouse_y = mouse_y;
850         }
851
852         // LordHavoc: viewzoom affects mouse sensitivity for sniping
853         mouse_x *= sensitivity.value * cl.viewzoom;
854         mouse_y *= sensitivity.value * cl.viewzoom;
855
856         if (in_strafe.state & 1)
857                 cmd->sidemove += m_side.value * mouse_x;
858         else
859                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
860
861         //if (freelook)
862                 V_StopPitchDrift ();
863
864         if (/*freelook && */!(in_strafe.state & 1))
865                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
866         else
867         {
868                 if ((in_strafe.state & 1) && noclip_anglehack)
869                         cmd->upmove -= m_forward.value * mouse_y;
870                 else
871                         cmd->forwardmove -= m_forward.value * mouse_y;
872         }
873         mouse_x = mouse_y = 0.0;
874 }
875
876 void IN_Move (usercmd_t *cmd)
877 {
878         IN_MouseMove(cmd);
879         cl.viewangles[PITCH] = bound (in_pitch_min.value, cl.viewangles[PITCH], in_pitch_max.value);
880 }
881
882