]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_glx.c
fixed r_drawcollisionbrushes 2
[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 <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <GL/glx.h>
33
34 #include <X11/keysym.h>
35 #include <X11/cursorfont.h>
36
37 #include <X11/extensions/XShm.h>
38 #if !defined(__APPLE__) && !defined(__MACH__)
39 #include <X11/extensions/xf86dga.h>
40 #endif
41 #include <X11/extensions/xf86vmode.h>
42
43 #include "quakedef.h"
44
45 // Tell startup code that we have a client
46 int cl_available = true;
47
48 //GLX prototypes
49 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
50 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
51 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
52 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
53 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
54 const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
55
56 //GLX_ARB_get_proc_address
57 void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
58
59 static dllfunction_t getprocaddressfuncs[] =
60 {
61         {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
62         {NULL, NULL}
63 };
64
65 //GLX_SGI_swap_control
66 GLint (GLAPIENTRY *qglXSwapIntervalSGI)(GLint interval);
67
68 static dllfunction_t swapcontrolfuncs[] =
69 {
70         {"glXSwapIntervalSGI", (void **) &qglXSwapIntervalSGI},
71         {NULL, NULL}
72 };
73
74 static Display *vidx11_display = NULL;
75 static int vidx11_screen;
76 static Window win;
77 static GLXContext ctx = NULL;
78
79 Atom wm_delete_window_atom;
80
81 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
82 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
83                     PointerMotionMask | ButtonMotionMask)
84 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | \
85                 StructureNotifyMask | FocusChangeMask | EnterWindowMask | \
86                 LeaveWindowMask)
87
88
89 static qboolean mouse_avail = true;
90 static qboolean vid_usingmouse = false;
91 static qboolean vid_usemouse = false;
92 static qboolean vid_usingvsync = false;
93 static qboolean vid_usevsync = false;
94 static float    mouse_x, mouse_y;
95 static int p_mouse_x, p_mouse_y;
96
97 #ifndef __APPLE__
98 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
99 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
100 #endif
101
102 qboolean vidmode_ext = false;
103
104 static int win_x, win_y;
105
106 static XF86VidModeModeInfo **vidmodes;
107 static int num_vidmodes;
108 static qboolean vid_isfullscreen = false;
109
110 static Visual *vidx11_visual;
111 static Colormap vidx11_colormap;
112
113 /*-----------------------------------------------------------------------*/
114
115 static int XLateKey(XKeyEvent *ev, char *ascii)
116 {
117         int key = 0;
118         char buf[64];
119         KeySym keysym, shifted;
120
121         keysym = XLookupKeysym (ev, 0);
122         XLookupString(ev, buf, sizeof buf, &shifted, 0);
123         *ascii = buf[0];
124
125         switch(keysym)
126         {
127                 case XK_KP_Page_Up:      key = K_KP_PGUP; break;
128                 case XK_Page_Up:         key = K_PGUP; break;
129
130                 case XK_KP_Page_Down: key = K_KP_PGDN; break;
131                 case XK_Page_Down:       key = K_PGDN; break;
132
133                 case XK_KP_Home: key = K_KP_HOME; break;
134                 case XK_Home:    key = K_HOME; break;
135
136                 case XK_KP_End:  key = K_KP_END; break;
137                 case XK_End:     key = K_END; break;
138
139                 case XK_KP_Left: key = K_KP_LEFTARROW; break;
140                 case XK_Left:    key = K_LEFTARROW; break;
141
142                 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
143                 case XK_Right:  key = K_RIGHTARROW;             break;
144
145                 case XK_KP_Down: key = K_KP_DOWNARROW; break;
146                 case XK_Down:    key = K_DOWNARROW; break;
147
148                 case XK_KP_Up:   key = K_KP_UPARROW; break;
149                 case XK_Up:              key = K_UPARROW;        break;
150
151                 case XK_Escape: key = K_ESCAPE;         break;
152
153                 case XK_KP_Enter: key = K_KP_ENTER;     break;
154                 case XK_Return: key = K_ENTER;           break;
155
156                 case XK_Tab:            key = K_TAB;                     break;
157
158                 case XK_F1:              key = K_F1;                            break;
159
160                 case XK_F2:              key = K_F2;                            break;
161
162                 case XK_F3:              key = K_F3;                            break;
163
164                 case XK_F4:              key = K_F4;                            break;
165
166                 case XK_F5:              key = K_F5;                            break;
167
168                 case XK_F6:              key = K_F6;                            break;
169
170                 case XK_F7:              key = K_F7;                            break;
171
172                 case XK_F8:              key = K_F8;                            break;
173
174                 case XK_F9:              key = K_F9;                            break;
175
176                 case XK_F10:            key = K_F10;                     break;
177
178                 case XK_F11:            key = K_F11;                     break;
179
180                 case XK_F12:            key = K_F12;                     break;
181
182                 case XK_BackSpace: key = K_BACKSPACE; break;
183
184                 case XK_KP_Delete: key = K_KP_DEL; break;
185                 case XK_Delete: key = K_DEL; break;
186
187                 case XK_Pause:  key = K_PAUSE;           break;
188
189                 case XK_Shift_L:
190                 case XK_Shift_R:        key = K_SHIFT;          break;
191
192                 case XK_Execute:
193                 case XK_Control_L:
194                 case XK_Control_R:      key = K_CTRL;            break;
195
196                 case XK_Alt_L:
197                 case XK_Meta_L:
198                 case XK_Alt_R:
199                 case XK_Meta_R: key = K_ALT;                    break;
200
201                 case XK_KP_Begin: key = K_KP_5; break;
202
203                 case XK_Insert:key = K_INS; break;
204                 case XK_KP_Insert: key = K_KP_INS; break;
205
206                 case XK_KP_Multiply: key = '*'; break;
207                 case XK_KP_Add:  key = K_KP_PLUS; break;
208                 case XK_KP_Subtract: key = K_KP_MINUS; break;
209                 case XK_KP_Divide: key = K_KP_SLASH; break;
210
211                 default:
212                         if (keysym < 32 && keysym > 126)
213                                 break;
214
215                         if (keysym >= 'A' && keysym <= 'Z')
216                                 key = keysym - 'A' + 'a';
217                         else
218                                 key = keysym;
219
220                         break;
221         }
222
223         return key;
224 }
225
226 static Cursor CreateNullCursor(Display *display, Window root)
227 {
228         Pixmap cursormask;
229         XGCValues xgc;
230         GC gc;
231         XColor dummycolour;
232         Cursor cursor;
233
234         cursormask = XCreatePixmap(display, root, 1, 1, 1);
235         xgc.function = GXclear;
236         gc =  XCreateGC(display, cursormask, GCFunction, &xgc);
237         XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
238         dummycolour.pixel = 0;
239         dummycolour.red = 0;
240         dummycolour.flags = 04;
241         cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
242         XFreePixmap(display,cursormask);
243         XFreeGC(display,gc);
244         return cursor;
245 }
246
247 static void IN_Activate (qboolean grab)
248 {
249         if (!vidx11_display)
250                 return;
251         if (grab)
252         {
253                 if (!vid_usingmouse && mouse_avail && win)
254                 {
255                         XWindowAttributes attribs_1;
256                         XSetWindowAttributes attribs_2;
257
258                         XGetWindowAttributes(vidx11_display, win, &attribs_1);
259                         attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
260                         XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
261
262                 // inviso cursor
263                         XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
264
265                         XGrabPointer(vidx11_display, win,  True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
266
267 #ifndef __APPLE__
268                         if (vid_dga.integer)
269                         {
270                                 int MajorVersion, MinorVersion;
271
272                                 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
273                                 {
274                                         // unable to query, probably not supported
275                                         Con_Print( "Failed to detect XF86DGA Mouse\n" );
276                                         Cvar_SetValueQuick(&vid_dga, 0);
277                                         XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
278                                 }
279                                 else
280                                 {
281                                         XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
282                                         XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
283                                 }
284                         }
285                         else
286 #endif
287                                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
288
289                         XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
290
291                         mouse_x = mouse_y = 0;
292                         cl_ignoremousemove = true;
293                         vid_usingmouse = true;
294                 }
295         }
296         else
297         {
298                 if (vid_usingmouse)
299                 {
300 #ifndef __APPLE__
301                         if (vid_dga.integer)
302                                 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
303 #endif
304
305                         XUngrabPointer(vidx11_display, CurrentTime);
306                         XUngrabKeyboard(vidx11_display, CurrentTime);
307
308                         // inviso cursor
309                         if (win)
310                                 XUndefineCursor(vidx11_display, win);
311
312                         cl_ignoremousemove = true;
313                         vid_usingmouse = false;
314                 }
315         }
316 }
317
318 static void HandleEvents(void)
319 {
320         XEvent event;
321         int key;
322         char ascii;
323         qboolean dowarp = false;
324
325         if (!vidx11_display)
326                 return;
327
328         while (XPending(vidx11_display))
329         {
330                 XNextEvent(vidx11_display, &event);
331
332                 switch (event.type)
333                 {
334                 case KeyPress:
335                         // key pressed
336                         key = XLateKey (&event.xkey, &ascii);
337                         Key_Event(key, ascii, true);
338                         break;
339
340                 case KeyRelease:
341                         // key released
342                         key = XLateKey (&event.xkey, &ascii);
343                         Key_Event(key, ascii, false);
344                         break;
345
346                 case MotionNotify:
347                         // mouse moved
348                         if (vid_usingmouse)
349                         {
350 #ifndef __APPLE__
351                                 if (vid_dga.integer == 1)
352                                 {
353                                         mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
354                                         mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
355                                 }
356                                 else
357 #endif
358                                 {
359
360                                         if (!event.xmotion.send_event)
361                                         {
362                                                 mouse_x += event.xmotion.x - p_mouse_x;
363                                                 mouse_y += event.xmotion.y - p_mouse_y;
364                                                 if (abs(vid.width/2 - event.xmotion.x) > vid.width / 4 || abs(vid.height/2 - event.xmotion.y) > vid.height / 4)
365                                                         dowarp = true;
366                                         }
367                                         p_mouse_x = event.xmotion.x;
368                                         p_mouse_y = event.xmotion.y;
369                                 }
370                         }
371                         //else
372                         //      ui_mouseupdate(event.xmotion.x, event.xmotion.y);
373                         break;
374
375                 case ButtonPress:
376                         // mouse button pressed
377                         switch(event.xbutton.button)
378                         {
379                         case 1:
380                                 Key_Event(K_MOUSE1, 0, true);
381                                 break;
382                         case 2:
383                                 Key_Event(K_MOUSE3, 0, true);
384                                 break;
385                         case 3:
386                                 Key_Event(K_MOUSE2, 0, true);
387                                 break;
388                         case 4:
389                                 Key_Event(K_MWHEELUP, 0, true);
390                                 break;
391                         case 5:
392                                 Key_Event(K_MWHEELDOWN, 0, true);
393                                 break;
394                         case 6:
395                                 Key_Event(K_MOUSE4, 0, true);
396                                 break;
397                         case 7:
398                                 Key_Event(K_MOUSE5, 0, true);
399                                 break;
400                         case 8:
401                                 Key_Event(K_MOUSE6, 0, true);
402                                 break;
403                         case 9:
404                                 Key_Event(K_MOUSE7, 0, true);
405                                 break;
406                         case 10:
407                                 Key_Event(K_MOUSE8, 0, true);
408                                 break;
409                         case 11:
410                                 Key_Event(K_MOUSE9, 0, true);
411                                 break;
412                         case 12:
413                                 Key_Event(K_MOUSE10, 0, true);
414                                 break;
415                         default:
416                                 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-12 expected\n", event.xbutton.button);
417                                 break;
418                         }
419                         break;
420
421                 case ButtonRelease:
422                         // mouse button released
423                         switch(event.xbutton.button)
424                         {
425                         case 1:
426                                 Key_Event(K_MOUSE1, 0, false);
427                                 break;
428                         case 2:
429                                 Key_Event(K_MOUSE3, 0, false);
430                                 break;
431                         case 3:
432                                 Key_Event(K_MOUSE2, 0, false);
433                                 break;
434                         case 4:
435                                 Key_Event(K_MWHEELUP, 0, false);
436                                 break;
437                         case 5:
438                                 Key_Event(K_MWHEELDOWN, 0, false);
439                                 break;
440                         case 6:
441                                 Key_Event(K_MOUSE4, 0, false);
442                                 break;
443                         case 7:
444                                 Key_Event(K_MOUSE5, 0, false);
445                                 break;
446                         case 8:
447                                 Key_Event(K_MOUSE6, 0, false);
448                                 break;
449                         case 9:
450                                 Key_Event(K_MOUSE7, 0, false);
451                                 break;
452                         case 10:
453                                 Key_Event(K_MOUSE8, 0, false);
454                                 break;
455                         case 11:
456                                 Key_Event(K_MOUSE9, 0, false);
457                                 break;
458                         case 12:
459                                 Key_Event(K_MOUSE10, 0, false);
460                                 break;
461                         default:
462                                 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-12 expected\n", event.xbutton.button);
463                                 break;
464                         }
465                         break;
466
467                 case CreateNotify:
468                         // window created
469                         win_x = event.xcreatewindow.x;
470                         win_y = event.xcreatewindow.y;
471                         break;
472
473                 case ConfigureNotify:
474                         // window changed size/location
475                         win_x = event.xconfigure.x;
476                         win_y = event.xconfigure.y;
477                         break;
478                 case DestroyNotify:
479                         // window has been destroyed
480                         Sys_Quit();
481                         break;
482                 case ClientMessage:
483                         // window manager messages
484                         if ((event.xclient.format == 32) && ((unsigned int)event.xclient.data.l[0] == wm_delete_window_atom))
485                                 Sys_Quit();
486                         break;
487                 case MapNotify:
488                         // window restored
489                         vid_hidden = false;
490                         vid_activewindow = false;
491                         VID_RestoreSystemGamma();
492                         break;
493                 case UnmapNotify:
494                         // window iconified/rolledup/whatever
495                         vid_hidden = true;
496                         vid_activewindow = false;
497                         VID_RestoreSystemGamma();
498                         break;
499                 case FocusIn:
500                         // window is now the input focus
501                         vid_activewindow = true;
502                         break;
503                 case FocusOut:
504                         // window is no longer the input focus
505                         vid_activewindow = false;
506                         VID_RestoreSystemGamma();
507                         break;
508                 case EnterNotify:
509                         // mouse entered window
510                         break;
511                 case LeaveNotify:
512                         // mouse left window
513                         break;
514                 }
515         }
516
517         if (dowarp)
518         {
519                 /* move the mouse to the window center again */
520                 p_mouse_x = vid.width / 2;
521                 p_mouse_y = vid.height / 2;
522                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
523         }
524 }
525
526 static void *prjobj = NULL;
527
528 static void GL_CloseLibrary(void)
529 {
530         if (prjobj)
531                 dlclose(prjobj);
532         prjobj = NULL;
533         gl_driver[0] = 0;
534         qglXGetProcAddressARB = NULL;
535         gl_extensions = "";
536         gl_platform = "";
537         gl_platformextensions = "";
538 }
539
540 static int GL_OpenLibrary(const char *name)
541 {
542         Con_Printf("Loading OpenGL driver %s\n", name);
543         GL_CloseLibrary();
544         if (!(prjobj = dlopen(name, RTLD_LAZY)))
545         {
546                 Con_Printf("Unable to open symbol list for %s\n", name);
547                 return false;
548         }
549         strcpy(gl_driver, name);
550         return true;
551 }
552
553 void *GL_GetProcAddress(const char *name)
554 {
555         void *p = NULL;
556         if (qglXGetProcAddressARB != NULL)
557                 p = (void *) qglXGetProcAddressARB(name);
558         if (p == NULL)
559                 p = (void *) dlsym(prjobj, name);
560         return p;
561 }
562
563 void VID_Shutdown(void)
564 {
565         if (!ctx || !vidx11_display)
566                 return;
567
568         if (vidx11_display)
569         {
570                 IN_Activate(false);
571                 VID_RestoreSystemGamma();
572
573                 // FIXME: glXDestroyContext here?
574                 if (vid_isfullscreen)
575                         XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[0]);
576                 if (win)
577                         XDestroyWindow(vidx11_display, win);
578                 XCloseDisplay(vidx11_display);
579         }
580         vid_hidden = true;
581         vid_isfullscreen = false;
582         vidx11_display = NULL;
583         win = 0;
584         ctx = NULL;
585
586         GL_CloseLibrary();
587         Key_ClearStates ();
588 }
589
590 void signal_handler(int sig)
591 {
592         Con_Printf("Received signal %d, exiting...\n", sig);
593         VID_RestoreSystemGamma();
594         Sys_Quit();
595         exit(0);
596 }
597
598 void InitSig(void)
599 {
600         signal(SIGHUP, signal_handler);
601         signal(SIGINT, signal_handler);
602         signal(SIGQUIT, signal_handler);
603         signal(SIGILL, signal_handler);
604         signal(SIGTRAP, signal_handler);
605         signal(SIGIOT, signal_handler);
606         signal(SIGBUS, signal_handler);
607         signal(SIGFPE, signal_handler);
608         signal(SIGSEGV, signal_handler);
609         signal(SIGTERM, signal_handler);
610 }
611
612 void VID_Finish (void)
613 {
614         vid_usevsync = vid_vsync.integer && !cls.timedemo && gl_videosyncavailable;
615         if (vid_usingvsync != vid_usevsync && gl_videosyncavailable)
616         {
617                 vid_usingvsync = vid_usevsync;
618                 if (qglXSwapIntervalSGI (vid_usevsync))
619                         Con_Print("glXSwapIntervalSGI didn't accept the vid_vsync change, it will take effect on next vid_restart (GLX_SGI_swap_control does not allow turning off vsync)\n");
620         }
621
622 // handle the mouse state when windowed if that's changed
623         vid_usemouse = false;
624         if (vid_mouse.integer && !key_consoleactive && !cls.demoplayback)
625                 vid_usemouse = true;
626         if (!vid_activewindow)
627                 vid_usemouse = false;
628         if (vid_isfullscreen)
629                 vid_usemouse = true;
630         IN_Activate(vid_usemouse);
631
632         if (r_render.integer)
633         {
634                 if (r_speeds.integer || gl_finish.integer)
635                         qglFinish();
636                 qglXSwapBuffers(vidx11_display, win);
637         }
638 }
639
640 int VID_SetGamma(unsigned short *ramps)
641 {
642         return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
643 }
644
645 int VID_GetGamma(unsigned short *ramps)
646 {
647         return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
648 }
649
650 void VID_Init(void)
651 {
652 #ifndef __APPLE__
653         Cvar_RegisterVariable (&vid_dga);
654         Cvar_RegisterVariable (&vid_dga_mouseaccel);
655 #endif
656         InitSig(); // trap evil signals
657 // COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
658         if (COM_CheckParm ("-nomouse") || COM_CheckParm("-safe"))
659                 mouse_avail = false;
660 }
661
662 void VID_BuildGLXAttrib(int *attrib, int stencil)
663 {
664         *attrib++ = GLX_RGBA;
665         *attrib++ = GLX_RED_SIZE;*attrib++ = 1;
666         *attrib++ = GLX_GREEN_SIZE;*attrib++ = 1;
667         *attrib++ = GLX_BLUE_SIZE;*attrib++ = 1;
668         *attrib++ = GLX_DOUBLEBUFFER;
669         *attrib++ = GLX_DEPTH_SIZE;*attrib++ = 1;
670         // if stencil is enabled, ask for alpha too
671         if (stencil)
672         {
673                 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
674                 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 1;
675         }
676         *attrib++ = None;
677 }
678
679 int VID_InitMode(int fullscreen, int width, int height, int bpp)
680 {
681         int i;
682         int attrib[32];
683         XSetWindowAttributes attr;
684         unsigned long mask;
685         Window root;
686         XVisualInfo *visinfo;
687         int MajorVersion, MinorVersion;
688         const char *drivername;
689
690 #if defined(__APPLE__) && defined(__MACH__)
691         drivername = "/usr/X11R6/lib/libGL.1.dylib";
692 #else
693         drivername = "libGL.so.1";
694 #endif
695 // COMMANDLINEOPTION: Linux GLX: -gl_driver <drivername> selects a GL driver library, default is libGL.so.1, useful only for using fxmesa or similar, if you don't know what this is for, you don't need it
696 // COMMANDLINEOPTION: BSD GLX: -gl_driver <drivername> selects a GL driver library, default is libGL.so.1, useful only for using fxmesa or similar, if you don't know what this is for, you don't need it
697 // LordHavoc: although this works on MacOSX, it's useless there (as there is only one system libGL)
698         i = COM_CheckParm("-gl_driver");
699         if (i && i < com_argc - 1)
700                 drivername = com_argv[i + 1];
701         if (!GL_OpenLibrary(drivername))
702         {
703                 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
704                 return false;
705         }
706
707         if (!(vidx11_display = XOpenDisplay(NULL)))
708         {
709                 Con_Print("Couldn't open the X display\n");
710                 return false;
711         }
712
713         vidx11_screen = DefaultScreen(vidx11_display);
714         root = RootWindow(vidx11_display, vidx11_screen);
715
716         // Get video mode list
717         MajorVersion = MinorVersion = 0;
718         if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
719                 vidmode_ext = false;
720         else
721         {
722                 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
723                 vidmode_ext = true;
724         }
725
726         if ((qglXChooseVisual = GL_GetProcAddress("glXChooseVisual")) == NULL
727          || (qglXCreateContext = GL_GetProcAddress("glXCreateContext")) == NULL
728          || (qglXMakeCurrent = GL_GetProcAddress("glXMakeCurrent")) == NULL
729          || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL
730          || (qglXQueryExtensionsString = GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
731         {
732                 Con_Printf("glX functions not found in %s\n", gl_driver);
733                 return false;
734         }
735
736         VID_BuildGLXAttrib(attrib, bpp == 32);
737         visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
738         if (!visinfo)
739         {
740                 Con_Print("Couldn't get an RGB, Double-buffered, Depth visual\n");
741                 return false;
742         }
743
744         if (vidmode_ext)
745         {
746                 int best_fit, best_dist, dist, x, y;
747
748                 // Are we going fullscreen?  If so, let's change video mode
749                 if (fullscreen)
750                 {
751                         XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
752                         best_dist = 9999999;
753                         best_fit = -1;
754
755                         for (i = 0; i < num_vidmodes; i++)
756                         {
757                                 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
758                                         continue;
759
760                                 x = width - vidmodes[i]->hdisplay;
761                                 y = height - vidmodes[i]->vdisplay;
762                                 dist = (x * x) + (y * y);
763                                 if (dist < best_dist)
764                                 {
765                                         best_dist = dist;
766                                         best_fit = i;
767                                 }
768                         }
769
770                         if (best_fit != -1)
771                         {
772                                 // LordHavoc: changed from ActualWidth/ActualHeight =,
773                                 // to width/height =, so the window will take the full area of
774                                 // the mode chosen
775                                 width = vidmodes[best_fit]->hdisplay;
776                                 height = vidmodes[best_fit]->vdisplay;
777
778                                 // change to the mode
779                                 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
780                                 vid_isfullscreen = true;
781
782                                 // Move the viewport to top left
783                                 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
784                         }
785                         else
786                                 fullscreen = 0;
787                 }
788         }
789
790         // LordHavoc: save the visual for use in gamma ramp settings later
791         vidx11_visual = visinfo->visual;
792
793         /* window attributes */
794         attr.background_pixel = 0;
795         attr.border_pixel = 0;
796         // LordHavoc: save the colormap for later, too
797         vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
798         attr.event_mask = X_MASK;
799         if (vid_isfullscreen)
800         {
801                 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
802                 attr.override_redirect = True;
803                 attr.backing_store = NotUseful;
804                 attr.save_under = False;
805         }
806         else
807                 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
808
809         win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
810         XStoreName(vidx11_display, win, gamename);
811         XMapWindow(vidx11_display, win);
812
813         // LordHavoc: making the close button on a window do the right thing
814         // seems to involve this mess, sigh...
815         wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
816         XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
817
818         if (vid_isfullscreen)
819         {
820                 XMoveWindow(vidx11_display, win, 0, 0);
821                 XRaiseWindow(vidx11_display, win);
822                 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
823                 XFlush(vidx11_display);
824                 // Move the viewport to top left
825                 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
826         }
827
828         //XSync(vidx11_display, False);
829
830         ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
831         if (!ctx)
832                 Sys_Error ("glXCreateContext failed\n");
833
834         if (!qglXMakeCurrent(vidx11_display, win, ctx))
835                 Sys_Error ("glXMakeCurrent failed\n");
836
837         XSync(vidx11_display, False);
838
839         if ((qglGetString = GL_GetProcAddress("glGetString")) == NULL)
840                 Sys_Error("glGetString not found in %s", gl_driver);
841
842         gl_renderer = qglGetString(GL_RENDERER);
843         gl_vendor = qglGetString(GL_VENDOR);
844         gl_version = qglGetString(GL_VERSION);
845         gl_extensions = qglGetString(GL_EXTENSIONS);
846         gl_platform = "GLX";
847         gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
848
849         gl_videosyncavailable = false;
850
851 // COMMANDLINEOPTION: Linux GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
852 // COMMANDLINEOPTION: BSD GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
853 // COMMANDLINEOPTION: MacOSX GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
854         GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
855 // COMMANDLINEOPTION: Linux GLX: -novideosync disables GLX_SGI_swap_control
856 // COMMANDLINEOPTION: BSD GLX: -novideosync disables GLX_SGI_swap_control
857 // COMMANDLINEOPTION: MacOSX GLX: -novideosync disables GLX_SGI_swap_control
858         gl_videosyncavailable = GL_CheckExtension("GLX_SGI_swap_control", swapcontrolfuncs, "-novideosync", false);
859
860         vid_usingmouse = false;
861         vid_usingvsync = false;
862         vid_hidden = false;
863         vid_activewindow = true;
864         GL_Init();
865         return true;
866 }
867
868 void Sys_SendKeyEvents(void)
869 {
870         HandleEvents();
871 }
872
873 void IN_Move (void)
874 {
875         if (mouse_avail)
876         {
877                 in_mouse_x = mouse_x;
878                 in_mouse_y = mouse_y;
879         }
880         mouse_x = 0;
881         mouse_y = 0;
882 }