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