bmodel rotation physics now work
[xonotic/darkplaces.git] / vid_glx.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 #include <termios.h>
21 #include <sys/ioctl.h>
22 #include <sys/stat.h>
23 #include <sys/vt.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <signal.h>
27
28 #include <dlfcn.h>
29
30 #include "quakedef.h"
31
32 #include <GL/glx.h>
33
34 #include <X11/keysym.h>
35 #include <X11/cursorfont.h>
36
37 #include <X11/extensions/xf86dga.h>
38 #include <X11/extensions/xf86vmode.h>
39
40 static Display *dpy = NULL;
41 static int scrnum;
42 static Window win;
43 static GLXContext ctx = NULL;
44
45 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
46 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
47                     PointerMotionMask | ButtonMotionMask )
48 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | StructureNotifyMask )
49
50
51 unsigned                d_8to24table[256];
52 unsigned char   d_15to8table[65536];
53
54 cvar_t  vid_mode = {"vid_mode","0",false};
55  
56 viddef_t        vid;                            // global video state
57
58 static qboolean        mouse_avail = true;
59 static qboolean        mouse_active = false;
60 static float   mouse_x, mouse_y;
61 static float    old_mouse_x, old_mouse_y;
62 static int p_mouse_x, p_mouse_y;
63
64 static cvar_t in_mouse = {"in_mouse", "1", false};
65 static cvar_t in_dgamouse = {"in_dgamouse", "1", false};
66 static cvar_t m_filter = {"m_filter", "0"};
67
68 qboolean dgamouse = false;
69 qboolean vidmode_ext = false;
70
71 static int win_x, win_y;
72
73 static int scr_width, scr_height;
74
75 static XF86VidModeModeInfo **vidmodes;
76 //static int default_dotclock_vidmode;
77 static int num_vidmodes;
78 static qboolean vidmode_active = false;
79
80 /*-----------------------------------------------------------------------*/
81
82 int             texture_extension_number = 1;
83
84 float           gldepthmin, gldepthmax;
85
86 const char *gl_vendor;
87 const char *gl_renderer;
88 const char *gl_version;
89 const char *gl_extensions;
90
91 void (*qglMTexCoord2f) (GLenum, GLfloat, GLfloat);
92 void (*qglSelectTexture) (GLenum);
93
94 //static float vid_gamma = 1.0;
95
96 // LordHavoc: ARB multitexture support
97 int gl_mtex_enum = 0;
98
99 // LordHavoc: in GLX these are never set, simply provided to make the rest of the code work
100 qboolean isG200 = false;
101 qboolean isRagePro = false;
102 qboolean gl_mtexable = false;
103 qboolean gl_arrays = false;
104
105 /*-----------------------------------------------------------------------*/
106 static int
107 XLateKey(XKeyEvent *ev/*, qboolean modified*/)
108 {
109         char tmp[2];
110         int key = 0;
111         KeySym keysym;
112
113 /*      if (!modified) {*/
114                 keysym = XLookupKeysym(ev, 0);
115 /*      } else {
116                 XLookupString(ev, tmp, 1, &keysym, NULL);
117         }*/
118
119         switch(keysym) {
120                 case XK_KP_Page_Up:     key = KP_PGUP; break;
121                 case XK_Page_Up:        key = K_PGUP; break;
122
123                 case XK_KP_Page_Down:   key = KP_PGDN; break;
124                 case XK_Page_Down:      key = K_PGDN; break;
125
126                 case XK_KP_Home:        key = KP_HOME; break;
127                 case XK_Home:           key = K_HOME; break;
128
129                 case XK_KP_End:         key = KP_END; break;
130                 case XK_End:            key = K_END; break;
131
132                 case XK_KP_Left:        key = KP_LEFTARROW; break;
133                 case XK_Left:           key = K_LEFTARROW; break;
134
135                 case XK_KP_Right:       key = KP_RIGHTARROW; break;
136                 case XK_Right:          key = K_RIGHTARROW; break;
137
138                 case XK_KP_Down:        key = KP_DOWNARROW; break;
139                 case XK_Down:           key = K_DOWNARROW; break;
140
141                 case XK_KP_Up:          key = KP_UPARROW; break;
142                 case XK_Up:                     key = K_UPARROW; break;
143
144                 case XK_Escape:         key = K_ESCAPE; break;
145
146                 case XK_KP_Enter:       key = KP_ENTER; break;
147                 case XK_Return:         key = K_ENTER; break;
148
149                 case XK_Tab:            key = K_TAB; break;
150
151                 case XK_F1:                     key = K_F1; break;
152                 case XK_F2:                     key = K_F2; break;
153                 case XK_F3:                     key = K_F3; break;
154                 case XK_F4:                     key = K_F4; break;
155                 case XK_F5:                     key = K_F5; break;
156                 case XK_F6:                     key = K_F6; break;
157                 case XK_F7:                     key = K_F7; break;
158                 case XK_F8:                     key = K_F8; break;
159                 case XK_F9:                     key = K_F9; break;
160                 case XK_F10:            key = K_F10; break;
161                 case XK_F11:            key = K_F11; break;
162                 case XK_F12:            key = K_F12; break;
163
164                 case XK_BackSpace:      key = K_BACKSPACE; break;
165
166                 case XK_KP_Delete:      key = KP_DEL; break;
167                 case XK_Delete:         key = K_DEL; break;
168
169                 case XK_Pause:          key = K_PAUSE; break;
170
171                 case XK_Shift_L:
172                 case XK_Shift_R:        key = K_SHIFT; break;
173
174                 case XK_Execute:
175                 case XK_Control_L:
176                 case XK_Control_R:      key = K_CTRL; break;
177
178                 case XK_Mode_switch:
179                 case XK_Alt_L:
180                 case XK_Meta_L:
181                 case XK_Alt_R:
182                 case XK_Meta_R:         key = K_ALT; break;
183
184                 case XK_Caps_Lock:      key = K_CAPSLOCK; break;
185                 case XK_KP_Begin:       key = KP_5; break;
186
187                 case XK_Insert:         key = K_INS; break;
188                 case XK_KP_Insert:      key = KP_INS; break;
189
190                 case XK_KP_Multiply:    key = KP_MULTIPLY; break;
191                 case XK_KP_Add:         key = KP_PLUS; break;
192                 case XK_KP_Subtract:    key = KP_MINUS; break;
193                 case XK_KP_Divide:      key = KP_DIVIDE; break;
194
195                 /* For Sun keyboards */
196                 case XK_F27:            key = K_HOME; break;
197                 case XK_F29:            key = K_PGUP; break;
198                 case XK_F33:            key = K_END; break;
199                 case XK_F35:            key = K_PGDN; break;
200
201                 default:
202                         if (keysym < 128) {
203                                 /* ASCII keys */
204                                 key = keysym;
205                                 if (/*!modified && */((key >= 'A') && (key <= 'Z'))) {
206                                         key = key + ('a' - 'A');
207                                 }
208                         }
209                         break;
210         }
211
212         return key;
213 }
214
215 static Cursor CreateNullCursor(Display *display, Window root)
216 {
217     Pixmap cursormask; 
218     XGCValues xgc;
219     GC gc;
220     XColor dummycolour;
221     Cursor cursor;
222
223     cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
224     xgc.function = GXclear;
225     gc =  XCreateGC(display, cursormask, GCFunction, &xgc);
226     XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
227     dummycolour.pixel = 0;
228     dummycolour.red = 0;
229     dummycolour.flags = 04;
230     cursor = XCreatePixmapCursor(display, cursormask, cursormask,
231           &dummycolour,&dummycolour, 0,0);
232     XFreePixmap(display,cursormask);
233     XFreeGC(display,gc);
234     return cursor;
235 }
236
237 static void install_grabs(void)
238 {
239         XWindowAttributes attribs_1;
240         XSetWindowAttributes attribs_2;
241
242         XGetWindowAttributes(dpy, win, &attribs_1);
243         attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
244         XChangeWindowAttributes(dpy, win, CWEventMask, &attribs_2);
245
246 // inviso cursor
247         XDefineCursor(dpy, win, CreateNullCursor(dpy, win));
248
249         XGrabPointer(dpy, win,  True, 0, GrabModeAsync, GrabModeAsync,
250                      win, None, CurrentTime);
251
252 /*      if (in_dgamouse.value) {
253                 int MajorVersion, MinorVersion;
254
255                 if (!XF86DGAQueryVersion(dpy, &MajorVersion, &MinorVersion)) { 
256                         // unable to query, probalby not supported
257                         Con_Printf( "Failed to detect XF86DGA Mouse\n" );
258                         in_dgamouse.value = 0;
259                 } else {
260                         dgamouse = true;
261                         XF86DGADirectVideo(dpy, DefaultScreen(dpy), XF86DGADirectMouse);
262                         XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
263                 }
264         } else {*/
265                 XWarpPointer(dpy, None, win,
266                                          0, 0, 0, 0,
267                                          vid.width / 2, vid.height / 2);
268 /*      }*/
269
270         XGrabKeyboard(dpy, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
271
272         mouse_active = true;
273         mouse_x = mouse_y = 0;
274
275 //      XSync(dpy, True);
276 }
277
278 static void uninstall_grabs(void)
279 {
280         if (!dpy || !win)
281                 return;
282
283 /*      if (dgamouse) {
284                 dgamouse = false;
285                 XF86DGADirectVideo(dpy, DefaultScreen(dpy), 0);
286         }*/
287
288         XUngrabPointer(dpy, CurrentTime);
289         XUngrabKeyboard(dpy, CurrentTime);
290
291 // inviso cursor
292         XUndefineCursor(dpy, win);
293
294         mouse_active = false;
295 }
296
297 static void HandleEvents(void)
298 {
299         XEvent event;
300 //      KeySym ks;
301         int b;
302         qboolean dowarp = false;
303
304         if (!dpy)
305                 return;
306
307         while (XPending(dpy)) {
308                 XNextEvent(dpy, &event);
309
310                 switch (event.type) {
311                 case KeyPress:
312                 case KeyRelease:
313                         Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
314                         break;
315
316                 case MotionNotify:
317                         if (dgamouse) {
318                                 mouse_x += event.xmotion.x_root/* * in_dga_mouseaccel.value*/;
319                                 mouse_y += event.xmotion.y_root/* * in_dga_mouseaccel.value*/;
320                         } else {
321                                 if (!p_mouse_x && !p_mouse_y) {
322                                         Con_Printf("event->xmotion.x: %d\n", event.xmotion.x); 
323                                         Con_Printf("event->xmotion.y: %d\n", event.xmotion.y); 
324                                 }
325 /*                              if (vid_fullscreen.value || _windowed_mouse.value) {*/
326                                         if (!event.xmotion.send_event) {
327                                                 mouse_x += (event.xmotion.x - p_mouse_x);
328                                                 mouse_y += (event.xmotion.y - p_mouse_y);
329                                                 if (abs(vid.width/2 - event.xmotion.x) > vid.width / 4
330                                                     || abs(vid.height/2 - event.xmotion.y) > vid.height / 4) {
331                                                         dowarp = true;
332                                                 }
333                                         }
334 /*                              } else {
335                                         mouse_x += (event.xmotion.x - p_mouse_x);
336                                         mouse_y += (event.xmotion.y - p_mouse_y);
337                                 }*/
338                                 p_mouse_x = event.xmotion.x;
339                                 p_mouse_y = event.xmotion.y;
340                         }
341         
342 /*                      if (mouse_active) {
343                                 if (dgamouse) {
344                                         mouse_x += (event.xmotion.x + win_x) * 2;
345                                         mouse_y += (event.xmotion.y + win_y) * 2;
346                                 } 
347                                 else
348                                 {
349                                         mouse_x += ((int)event.xmotion.x - mwx) * 2;
350                                         mouse_y += ((int)event.xmotion.y - mwy) * 2;
351                                         mwx = event.xmotion.x;
352                                         mwy = event.xmotion.y;
353
354                                         if (mouse_x || mouse_y)
355                                                 dowarp = true;
356                                 }
357                         }*/
358                         break;
359
360                 case ButtonPress:
361                         b=-1;
362                         if (event.xbutton.button == 1)
363                                 b = 0;
364                         else if (event.xbutton.button == 2)
365                                 b = 2;
366                         else if (event.xbutton.button == 3)
367                                 b = 1;
368                         if (b>=0)
369                                 Key_Event(K_MOUSE1 + b, true);
370                         break;
371
372                 case ButtonRelease:
373                         b=-1;
374                         if (event.xbutton.button == 1)
375                                 b = 0;
376                         else if (event.xbutton.button == 2)
377                                 b = 2;
378                         else if (event.xbutton.button == 3)
379                                 b = 1;
380                         if (b>=0)
381                                 Key_Event(K_MOUSE1 + b, false);
382                         break;
383
384                 case CreateNotify :
385                         win_x = event.xcreatewindow.x;
386                         win_y = event.xcreatewindow.y;
387                         break;
388
389                 case ConfigureNotify :
390                         win_x = event.xconfigure.x;
391                         win_y = event.xconfigure.y;
392                         break;
393                 }
394         }
395
396         if (dowarp) {
397                 /* move the mouse to the window center again */
398                 p_mouse_x = vid.width / 2;
399                 p_mouse_y = vid.height / 2;
400                 XWarpPointer(dpy, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
401         }
402
403 }
404
405 static void IN_DeactivateMouse( void ) 
406 {
407         if (!mouse_avail || !dpy || !win)
408                 return;
409
410         if (mouse_active) {
411                 uninstall_grabs();
412                 mouse_active = false;
413         }
414 }
415
416 static void IN_ActivateMouse( void ) 
417 {
418         if (!mouse_avail || !dpy || !win)
419                 return;
420
421         if (!mouse_active) {
422                 mouse_x = mouse_y = 0; // don't spazz
423                 install_grabs();
424                 mouse_active = true;
425         }
426 }
427
428
429 void VID_Shutdown(void)
430 {
431         if (!ctx || !dpy)
432                 return;
433
434         if (dpy) {
435                 uninstall_grabs();
436
437                 if (ctx)
438                         glXDestroyContext(dpy, ctx);
439                 if (win)
440                         XDestroyWindow(dpy, win);
441                 if (vidmode_active)
442                         XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[0]);
443                 XCloseDisplay(dpy);
444         }
445         vidmode_active = false;
446         dpy = NULL;
447         win = 0;
448         ctx = NULL;
449 }
450
451 void signal_handler(int sig)
452 {
453         printf("Received signal %d, exiting...\n", sig);
454         Sys_Quit();
455         exit(0);
456 }
457
458 void InitSig(void)
459 {
460         signal(SIGHUP, signal_handler);
461         signal(SIGINT, signal_handler);
462         signal(SIGQUIT, signal_handler);
463         signal(SIGILL, signal_handler);
464         signal(SIGTRAP, signal_handler);
465         signal(SIGIOT, signal_handler);
466         signal(SIGBUS, signal_handler);
467         signal(SIGFPE, signal_handler);
468         signal(SIGSEGV, signal_handler);
469         signal(SIGTERM, signal_handler);
470 }
471
472 void (*qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
473 void (*qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
474 void (*qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
475 void (*qglArrayElement)(GLint i);
476 void (*qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
477 void (*qglMTexCoord2f)(GLenum, GLfloat, GLfloat);
478 void (*qglSelectTexture)(GLenum);
479
480 void VID_CheckVertexArrays (void)
481 {
482         void *prjobj;
483         if (COM_CheckParm("-novertex"))
484         {
485                 Con_Printf("...vertex array support disabled\n");
486                 return;
487         }
488         if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
489         {
490                 Con_Printf("Unable to open symbol list for main program.\n");
491                 return;
492         }
493         qglMTexCoord2fSGIS = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
494         if ((qglArrayElement = (void *) dlsym(prjobj, "glArrayElement"))
495          && (qglColorPointer = (void *) dlsym(prjobj, "glColorPointer"))
496 //       && (qglDrawArrays = (void *) dlsym(prjobj, "glDrawArrays"))
497          && (qglDrawElements = (void *) dlsym(prjobj, "glDrawElements"))
498 //       && (qglInterleavedArrays = (void *) dlsym(prjobj, "glInterleavedArrays"))
499          && (qglTexCoordPointer = (void *) dlsym(prjobj, "glTexCoordPointer"))
500          && (qglVertexPointer = (void *) dlsym(prjobj, "glVertexPointer"))
501                 )
502         {
503                 Con_Printf("...vertex array support detected\n");
504                 gl_arrays = true;
505                 dlclose(prjobj);
506                 return;
507         }
508
509         Con_Printf("...vertex array support disabled (not detected - get a better driver)\n");
510         dlclose(prjobj);
511 }
512
513 void VID_CheckMultiTexture(void) 
514 {
515         void *prjobj;
516         qglMTexCoord2f = NULL;
517         qglSelectTexture = NULL;
518         // Check to see if multitexture is disabled
519         if (COM_CheckParm("-nomtex"))
520         {
521                 Con_Printf("...multitexture disabled\n");
522                 return;
523         }
524         if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
525         {
526                 Con_Printf("Unable to open symbol list for main program.\n");
527                 return;
528         }
529         // Test for ARB_multitexture
530         if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
531         {
532                 Con_Printf("...using GL_ARB_multitexture\n");
533                 qglMTexCoord2f = (void *) dlsym(prjobj, "glMultiTexCoord2fARB");
534                 qglSelectTexture = (void *) dlsym(prjobj, "glActiveTextureARB");
535                 gl_mtexable = true;
536                 gl_mtex_enum = GL_TEXTURE0_ARB;
537         }
538         else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
539         {
540                 Con_Printf("...using GL_SGIS_multitexture\n");
541                 qglMTexCoord2f = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
542                 qglSelectTexture = (void *) dlsym(prjobj, "glSelectTextureSGIS");
543                 gl_mtexable = true;
544                 gl_mtex_enum = TEXTURE0_SGIS;
545         }
546         if (!gl_mtexable)
547                 Con_Printf("...multitexture disabled (not detected)\n");
548         dlclose(prjobj);
549 }
550
551 /*
552 =================
553 GL_BeginRendering
554
555 =================
556 */
557 void GL_BeginRendering (int *x, int *y, int *width, int *height)
558 {
559         *x = *y = 0;
560         *width = scr_width;
561         *height = scr_height;
562
563 //      glViewport (*x, *y, *width, *height);
564 }
565
566
567 void GL_EndRendering (void)
568 {
569         if (!r_render.value)
570                 return;
571         glFlush();
572         glXSwapBuffers(dpy, win);
573 }
574
575 void VID_Init()
576 {
577         int i;
578         int attrib[] = {
579                 GLX_RGBA,
580                 GLX_RED_SIZE, 1,
581                 GLX_GREEN_SIZE, 1,
582                 GLX_BLUE_SIZE, 1,
583                 GLX_DOUBLEBUFFER,
584                 GLX_DEPTH_SIZE, 1,
585                 None
586         };
587 //      char    gldir[MAX_OSPATH];
588         int width = 640, height = 480;
589         XSetWindowAttributes attr;
590         unsigned long mask;
591         Window root;
592         XVisualInfo *visinfo;
593         qboolean fullscreen = true;
594         int MajorVersion, MinorVersion;
595         int actualWidth, actualHeight;
596
597         Cvar_RegisterVariable (&vid_mode);
598         Cvar_RegisterVariable (&in_mouse);
599         Cvar_RegisterVariable (&in_dgamouse);
600         Cvar_RegisterVariable (&m_filter);
601         
602 // interpret command-line params
603
604 // set vid parameters
605         if ((i = COM_CheckParm("-window")) != 0)
606                 fullscreen = false;
607
608         if ((i = COM_CheckParm("-width")) != 0)
609                 width = atoi(com_argv[i+1]);
610
611         if ((i = COM_CheckParm("-height")) != 0)
612                 height = atoi(com_argv[i+1]);
613
614         if ((i = COM_CheckParm("-conwidth")) != 0)
615                 vid.conwidth = atoi(com_argv[i+1]);
616         else
617                 vid.conwidth = 640;
618
619         vid.conwidth &= 0xfff8; // make it a multiple of eight
620
621         if (vid.conwidth < 320)
622                 vid.conwidth = 320;
623
624         // pick a conheight that matches with correct aspect
625         vid.conheight = vid.conwidth*3 / 4;
626
627         if ((i = COM_CheckParm("-conheight")) != 0)
628                 vid.conheight = atoi(com_argv[i+1]);
629         if (vid.conheight < 200)
630                 vid.conheight = 200;
631
632         if (!(dpy = XOpenDisplay(NULL))) {
633                 fprintf(stderr, "Error couldn't open the X display\n");
634                 exit(1);
635         }
636
637         scrnum = DefaultScreen(dpy);
638         root = RootWindow(dpy, scrnum);
639
640         // Get video mode list
641         MajorVersion = MinorVersion = 0;
642         if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) { 
643                 vidmode_ext = false;
644         } else {
645                 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
646                 vidmode_ext = true;
647         }
648
649         visinfo = glXChooseVisual(dpy, scrnum, attrib);
650         if (!visinfo) {
651                 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
652                 exit(1);
653         }
654
655         if (vidmode_ext) {
656                 int best_fit, best_dist, dist, x, y;
657                 
658                 XF86VidModeGetAllModeLines(dpy, scrnum, &num_vidmodes, &vidmodes);
659
660                 // Are we going fullscreen?  If so, let's change video mode
661                 if (fullscreen) {
662                         best_dist = 9999999;
663                         best_fit = -1;
664
665                         for (i = 0; i < num_vidmodes; i++) {
666                                 if (width > vidmodes[i]->hdisplay ||
667                                         height > vidmodes[i]->vdisplay)
668                                         continue;
669
670                                 x = width - vidmodes[i]->hdisplay;
671                                 y = height - vidmodes[i]->vdisplay;
672                                 dist = (x * x) + (y * y);
673                                 if (dist < best_dist) {
674                                         best_dist = dist;
675                                         best_fit = i;
676                                 }
677                         }
678
679                         if (best_fit != -1) {
680                                 actualWidth = vidmodes[best_fit]->hdisplay;
681                                 actualHeight = vidmodes[best_fit]->vdisplay;
682
683                                 // change to the mode
684                                 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[best_fit]);
685                                 vidmode_active = true;
686
687                                 // Move the viewport to top left
688                                 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
689                         } else
690                                 fullscreen = 0;
691                 }
692         }
693
694         /* window attributes */
695         attr.background_pixel = 0;
696         attr.border_pixel = 0;
697         attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
698         attr.event_mask = X_MASK;
699         if (vidmode_active) {
700                 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | 
701                         CWEventMask | CWOverrideRedirect;
702                 attr.override_redirect = True;
703                 attr.backing_store = NotUseful;
704                 attr.save_under = False;
705         } else
706                 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
707
708         win = XCreateWindow(dpy, root, 0, 0, width, height,
709                                                 0, visinfo->depth, InputOutput,
710                                                 visinfo->visual, mask, &attr);
711         XMapWindow(dpy, win);
712
713         if (vidmode_active) {
714                 XMoveWindow(dpy, win, 0, 0);
715                 XRaiseWindow(dpy, win);
716                 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
717                 XFlush(dpy);
718                 // Move the viewport to top left
719                 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
720         }
721
722         XFlush(dpy);
723
724         ctx = glXCreateContext(dpy, visinfo, NULL, True);
725
726         glXMakeCurrent(dpy, win, ctx);
727
728         scr_width = width;
729         scr_height = height;
730
731         if (vid.conheight > height)
732                 vid.conheight = height;
733         if (vid.conwidth > width)
734                 vid.conwidth = width;
735         vid.width = vid.conwidth;
736         vid.height = vid.conheight;
737
738         vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
739
740         InitSig(); // trap evil signals
741
742         GL_Init();
743
744         Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
745
746         vid.recalc_refdef = 1;                          // force a surface cache flush
747
748         install_grabs();
749 }
750
751 void Sys_SendKeyEvents(void)
752 {
753         HandleEvents();
754 }
755
756 void Force_CenterView_f (void)
757 {
758         cl.viewangles[PITCH] = 0;
759 }
760
761 void IN_Init(void)
762 {
763 }
764
765 void IN_Shutdown(void)
766 {
767 }
768
769 /*
770 ===========
771 IN_Commands
772 ===========
773 */
774 void IN_Commands (void)
775 {
776         if (!dpy || !win)
777                 return;
778
779         if (vidmode_active || key_dest == key_game)
780                 IN_ActivateMouse();
781         else
782                 IN_DeactivateMouse ();
783 }
784
785 /*
786 ===========
787 IN_Move
788 ===========
789 */
790 void IN_MouseMove (usercmd_t *cmd)
791 {
792         if (!mouse_avail)
793                 return;
794
795         if (!mouse_avail)
796                 return;
797
798         if (m_filter.value) {
799                 mouse_x = (mouse_x + old_mouse_x) * 0.5;
800                 mouse_y = (mouse_y + old_mouse_y) * 0.5;
801
802                 old_mouse_x = mouse_x;
803                 old_mouse_y = mouse_y;
804         }
805
806         mouse_x *= sensitivity.value;
807         mouse_y *= sensitivity.value;
808         
809         if (in_strafe.state & 1)
810                 cmd->sidemove += m_side.value * mouse_x;
811         else
812                 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
813                 
814 /*        if (freelook)*/
815                 V_StopPitchDrift ();
816
817         if (/*freelook && */!(in_strafe.state & 1)) {
818                 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
819                 cl.viewangles[PITCH] = bound (-70, cl.viewangles[PITCH], 80);
820         } else {
821                 if ((in_strafe.state & 1) && noclip_anglehack)
822                         cmd->upmove -= m_forward.value * mouse_y;
823                 else
824                         cmd->forwardmove -= m_forward.value * mouse_y;
825         }
826         mouse_x = mouse_y = 0.0;
827 }
828
829 void IN_Move (usercmd_t *cmd)
830 {
831         IN_MouseMove(cmd);
832 }
833
834