2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
25 #include <X11/Xutil.h>
26 #include <X11/Xatom.h>
27 #include <X11/XKBlib.h> // TODO possibly ifdef this out on non-supporting systems... Solaris (as always)?
32 #include <X11/keysym.h>
33 #include <X11/cursorfont.h>
36 #include <X11/extensions/XShm.h>
37 #if !defined(__APPLE__) && !defined(__MACH__) && !defined(SUNOS)
38 #include <X11/extensions/xf86dga.h>
40 #include <X11/extensions/xf86vmode.h>
43 #include "darkplaces.xpm"
45 // Tell startup code that we have a client
46 int cl_available = true;
48 // note: if we used the XRandR extension we could support refresh rates
49 qboolean vid_supportrefreshrate = false;
52 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
53 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
54 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
55 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
56 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
57 const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
59 //GLX_ARB_get_proc_address
60 void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
62 static dllfunction_t getprocaddressfuncs[] =
64 {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
68 //GLX_SGI_swap_control
69 GLint (GLAPIENTRY *qglXSwapIntervalSGI)(GLint interval);
71 static dllfunction_t swapcontrolfuncs[] =
73 {"glXSwapIntervalSGI", (void **) &qglXSwapIntervalSGI},
77 static Display *vidx11_display = NULL;
78 static int vidx11_screen;
79 static Window win, root;
80 static GLXContext ctx = NULL;
82 Atom wm_delete_window_atom;
83 Atom net_wm_state_atom;
84 Atom net_wm_state_hidden_atom;
85 Atom net_wm_state_fullscreen_atom;
87 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
88 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
89 PointerMotionMask | ButtonMotionMask)
90 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | \
91 StructureNotifyMask | FocusChangeMask | EnterWindowMask | \
95 static qboolean mouse_avail = true;
96 static qboolean vid_usingmousegrab = false;
97 static qboolean vid_usingmouse = false;
98 static qboolean vid_usinghidecursor = false;
99 static qboolean vid_usingvsync = false;
100 static qboolean vid_usevsync = false;
101 static qboolean vid_x11_hardwaregammasupported = false;
102 static qboolean vid_x11_dgasupported = false;
103 static int vid_x11_gammarampsize = 0;
105 #if !defined(__APPLE__) && !defined(SUNOS)
106 cvar_t vid_dgamouse = {CVAR_SAVE, "vid_dgamouse", "0", "make use of DGA mouse input"};
107 static qboolean vid_usingdgamouse = false;
109 cvar_t vid_netwmfullscreen = {CVAR_SAVE, "vid_netwmfullscreen", "0", "make use _NET_WM_STATE_FULLSCREEN; turn this off if fullscreen does not work for you"};
111 qboolean vidmode_ext = false;
113 static int win_x, win_y;
115 static XF86VidModeModeInfo init_vidmode, game_vidmode;
116 static qboolean vid_isfullscreen = false;
117 static qboolean vid_isvidmodefullscreen = false;
118 static qboolean vid_isnetwmfullscreen = false;
119 static qboolean vid_isoverrideredirect = false;
121 static Visual *vidx11_visual;
122 static Colormap vidx11_colormap;
124 /*-----------------------------------------------------------------------*/
126 static int XLateKey(XKeyEvent *ev, char *ascii)
130 KeySym keysym, shifted;
132 keysym = XLookupKeysym (ev, 0);
133 XLookupString(ev, buf, sizeof buf, &shifted, 0);
138 case XK_KP_Page_Up: key = K_KP_PGUP; break;
139 case XK_Page_Up: key = K_PGUP; break;
141 case XK_KP_Page_Down: key = K_KP_PGDN; break;
142 case XK_Page_Down: key = K_PGDN; break;
144 case XK_KP_Home: key = K_KP_HOME; break;
145 case XK_Home: key = K_HOME; break;
147 case XK_KP_End: key = K_KP_END; break;
148 case XK_End: key = K_END; break;
150 case XK_KP_Left: key = K_KP_LEFTARROW; break;
151 case XK_Left: key = K_LEFTARROW; break;
153 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
154 case XK_Right: key = K_RIGHTARROW; break;
156 case XK_KP_Down: key = K_KP_DOWNARROW; break;
157 case XK_Down: key = K_DOWNARROW; break;
159 case XK_KP_Up: key = K_KP_UPARROW; break;
160 case XK_Up: key = K_UPARROW; break;
162 case XK_Escape: key = K_ESCAPE; break;
164 case XK_KP_Enter: key = K_KP_ENTER; break;
165 case XK_Return: key = K_ENTER; break;
167 case XK_Tab: key = K_TAB; break;
169 case XK_F1: key = K_F1; break;
171 case XK_F2: key = K_F2; break;
173 case XK_F3: key = K_F3; break;
175 case XK_F4: key = K_F4; break;
177 case XK_F5: key = K_F5; break;
179 case XK_F6: key = K_F6; break;
181 case XK_F7: key = K_F7; break;
183 case XK_F8: key = K_F8; break;
185 case XK_F9: key = K_F9; break;
187 case XK_F10: key = K_F10; break;
189 case XK_F11: key = K_F11; break;
191 case XK_F12: key = K_F12; break;
193 case XK_BackSpace: key = K_BACKSPACE; break;
195 case XK_KP_Delete: key = K_KP_DEL; break;
196 case XK_Delete: key = K_DEL; break;
198 case XK_Pause: key = K_PAUSE; break;
201 case XK_Shift_R: key = K_SHIFT; break;
205 case XK_Control_R: key = K_CTRL; break;
209 case XK_ISO_Level3_Shift:
211 case XK_Meta_R: key = K_ALT; break;
213 case XK_KP_Begin: key = K_KP_5; break;
215 case XK_Insert:key = K_INS; break;
216 case XK_KP_Insert: key = K_KP_INS; break;
218 case XK_KP_Multiply: key = K_KP_MULTIPLY; break;
219 case XK_KP_Add: key = K_KP_PLUS; break;
220 case XK_KP_Subtract: key = K_KP_MINUS; break;
221 case XK_KP_Divide: key = K_KP_SLASH; break;
223 case XK_section: key = '~'; break;
229 if (keysym >= 'A' && keysym <= 'Z')
230 key = keysym - 'A' + 'a';
240 static Cursor CreateNullCursor(Display *display, Window root)
248 cursormask = XCreatePixmap(display, root, 1, 1, 1);
249 xgc.function = GXclear;
250 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
251 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
252 dummycolour.pixel = 0;
254 dummycolour.flags = 04;
255 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
256 XFreePixmap(display,cursormask);
261 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
263 static int originalmouseparms_num;
264 static int originalmouseparms_denom;
265 static int originalmouseparms_threshold;
266 static qboolean restore_spi;
268 #if !defined(__APPLE__) && !defined(SUNOS)
269 qboolean usedgamouse;
272 if (!vidx11_display || !win)
276 fullscreengrab = true;
279 fullscreengrab = relative = hidecursor = false;
281 #if !defined(__APPLE__) && !defined(SUNOS)
282 usedgamouse = relative && vid_dgamouse.integer;
283 if (!vid_x11_dgasupported)
285 if (fullscreengrab && vid_usingmouse && (vid_usingdgamouse != usedgamouse))
286 VID_SetMouse(false, false, false); // ungrab first!
289 if (vid_usingmousegrab != fullscreengrab)
291 vid_usingmousegrab = fullscreengrab;
292 cl_ignoremousemoves = 2;
295 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
296 if (vid_grabkeyboard.integer || vid_isoverrideredirect)
297 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
301 XUngrabPointer(vidx11_display, CurrentTime);
302 XUngrabKeyboard(vidx11_display, CurrentTime);
310 XWindowAttributes attribs_1;
311 XSetWindowAttributes attribs_2;
313 XGetWindowAttributes(vidx11_display, win, &attribs_1);
314 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
315 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
317 #if !defined(__APPLE__) && !defined(SUNOS)
318 vid_usingdgamouse = usedgamouse;
321 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
322 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
326 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
328 // COMMANDLINEOPTION: X11 Input: -noforcemparms disables setting of mouse parameters (not used with DGA, windows only)
329 #if !defined(__APPLE__) && !defined(SUNOS)
330 if (!COM_CheckParm ("-noforcemparms") && !usedgamouse)
332 if (!COM_CheckParm ("-noforcemparms"))
335 XGetPointerControl(vidx11_display, &originalmouseparms_num, &originalmouseparms_denom, &originalmouseparms_threshold);
336 XChangePointerControl (vidx11_display, true, false, 1, 1, -1); // TODO maybe change threshold here, or remove this comment
342 cl_ignoremousemoves = 2;
343 vid_usingmouse = true;
350 #if !defined(__APPLE__) && !defined(SUNOS)
351 if (vid_usingdgamouse)
352 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
353 vid_usingdgamouse = false;
355 cl_ignoremousemoves = 2;
358 XChangePointerControl (vidx11_display, true, true, originalmouseparms_num, originalmouseparms_denom, originalmouseparms_threshold);
361 vid_usingmouse = false;
365 if (vid_usinghidecursor != hidecursor)
367 vid_usinghidecursor = hidecursor;
369 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
371 XUndefineCursor(vidx11_display, win);
375 static keynum_t buttonremap[18] =
397 static void HandleEvents(void)
402 qboolean dowarp = false;
407 while (XPending(vidx11_display))
409 XNextEvent(vidx11_display, &event);
415 key = XLateKey (&event.xkey, &ascii);
416 Key_Event(key, ascii, true);
421 key = XLateKey (&event.xkey, &ascii);
422 Key_Event(key, ascii, false);
429 #if !defined(__APPLE__) && !defined(SUNOS)
430 if (vid_usingdgamouse)
432 in_mouse_x += event.xmotion.x_root;
433 in_mouse_y += event.xmotion.y_root;
438 if (!event.xmotion.send_event)
440 in_mouse_x += event.xmotion.x - in_windowmouse_x;
441 in_mouse_y += event.xmotion.y - in_windowmouse_y;
442 //if (abs(vid.width/2 - event.xmotion.x) + abs(vid.height/2 - event.xmotion.y))
443 if (vid_stick_mouse.integer || abs(vid.width/2 - event.xmotion.x) > vid.width / 4 || abs(vid.height/2 - event.xmotion.y) > vid.height / 4)
448 in_windowmouse_x = event.xmotion.x;
449 in_windowmouse_y = event.xmotion.y;
453 // mouse button pressed
454 if (event.xbutton.button <= 18)
455 Key_Event(buttonremap[event.xbutton.button - 1], 0, true);
457 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-18 expected\n", event.xbutton.button);
461 // mouse button released
462 if (event.xbutton.button <= 18)
463 Key_Event(buttonremap[event.xbutton.button - 1], 0, false);
465 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-18 expected\n", event.xbutton.button);
470 win_x = event.xcreatewindow.x;
471 win_y = event.xcreatewindow.y;
474 case ConfigureNotify:
475 // window changed size/location
476 win_x = event.xconfigure.x;
477 win_y = event.xconfigure.y;
478 if(vid_resizable.integer < 2 || vid_isnetwmfullscreen)
480 vid.width = event.xconfigure.width;
481 vid.height = event.xconfigure.height;
482 if(vid_isnetwmfullscreen)
483 Con_Printf("NetWM fullscreen: actually using resolution %dx%d\n", vid.width, vid.height);
485 Con_DPrintf("Updating to ConfigureNotify resolution %dx%d\n", vid.width, vid.height);
489 // window has been destroyed
493 // window manager messages
494 if ((event.xclient.format == 32) && ((unsigned int)event.xclient.data.l[0] == wm_delete_window_atom))
498 if (vid_isoverrideredirect)
502 VID_RestoreSystemGamma();
504 if(vid_isvidmodefullscreen)
506 // set our video mode
507 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &game_vidmode);
509 // Move the viewport to top left
510 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
513 if(vid_isnetwmfullscreen)
515 // make sure it's fullscreen
517 event.type = ClientMessage;
518 event.xclient.serial = 0;
519 event.xclient.send_event = True;
520 event.xclient.message_type = net_wm_state_atom;
521 event.xclient.window = win;
522 event.xclient.format = 32;
523 event.xclient.data.l[0] = 1;
524 event.xclient.data.l[1] = net_wm_state_fullscreen_atom;
525 event.xclient.data.l[2] = 0;
526 event.xclient.data.l[3] = 1;
527 event.xclient.data.l[4] = 0;
528 XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
535 if (vid_isoverrideredirect)
537 // window iconified/rolledup/whatever
539 VID_RestoreSystemGamma();
541 if(vid_isvidmodefullscreen)
542 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
546 if (vid_isoverrideredirect)
548 // window is now the input focus
549 vid_activewindow = true;
552 if (vid_isoverrideredirect)
555 if(vid_isnetwmfullscreen && event.xfocus.mode == NotifyNormal)
557 // iconify netwm fullscreen window when it loses focus
558 // when the user selects it in the taskbar, the window manager will map it again and send MapNotify
560 event.type = ClientMessage;
561 event.xclient.serial = 0;
562 event.xclient.send_event = True;
563 event.xclient.message_type = net_wm_state_atom;
564 event.xclient.window = win;
565 event.xclient.format = 32;
566 event.xclient.data.l[0] = 1;
567 event.xclient.data.l[1] = net_wm_state_hidden_atom;
568 event.xclient.data.l[2] = 0;
569 event.xclient.data.l[3] = 1;
570 event.xclient.data.l[4] = 0;
571 XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
574 // window is no longer the input focus
575 vid_activewindow = false;
576 VID_RestoreSystemGamma();
580 // mouse entered window
590 /* move the mouse to the window center again */
591 // we'll catch the warp motion by its send_event flag, updating the
592 // stored mouse position without adding any delta motion
594 event.type = MotionNotify;
595 event.xmotion.display = vidx11_display;
596 event.xmotion.window = win;
597 event.xmotion.x = vid.width / 2;
598 event.xmotion.y = vid.height / 2;
599 XSendEvent(vidx11_display, win, False, PointerMotionMask, &event);
600 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
604 static void *prjobj = NULL;
606 static void GL_CloseLibrary(void)
612 qglXGetProcAddressARB = NULL;
615 gl_platformextensions = "";
618 static int GL_OpenLibrary(const char *name)
620 Con_Printf("Loading OpenGL driver %s\n", name);
622 if (!(prjobj = dlopen(name, RTLD_LAZY | RTLD_GLOBAL)))
624 Con_Printf("Unable to open symbol list for %s\n", name);
627 strlcpy(gl_driver, name, sizeof(gl_driver));
631 void *GL_GetProcAddress(const char *name)
634 if (qglXGetProcAddressARB != NULL)
635 p = (void *) qglXGetProcAddressARB((GLubyte *)name);
637 p = (void *) dlsym(prjobj, name);
641 void VID_Shutdown(void)
643 if (!ctx || !vidx11_display)
646 VID_SetMouse(false, false, false);
647 VID_RestoreSystemGamma();
649 // FIXME: glXDestroyContext here?
650 if (vid_isvidmodefullscreen)
651 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
653 XDestroyWindow(vidx11_display, win);
654 XCloseDisplay(vidx11_display);
657 vid_isfullscreen = false;
658 vid_isnetwmfullscreen = false;
659 vid_isvidmodefullscreen = false;
660 vid_isoverrideredirect = false;
661 vidx11_display = NULL;
669 void signal_handler(int sig)
671 Con_Printf("Received signal %d, exiting...\n", sig);
672 VID_RestoreSystemGamma();
678 signal(SIGHUP, signal_handler);
679 signal(SIGINT, signal_handler);
680 signal(SIGQUIT, signal_handler);
681 signal(SIGILL, signal_handler);
682 signal(SIGTRAP, signal_handler);
683 signal(SIGIOT, signal_handler);
684 signal(SIGBUS, signal_handler);
685 signal(SIGFPE, signal_handler);
686 signal(SIGSEGV, signal_handler);
687 signal(SIGTERM, signal_handler);
690 void VID_Finish (void)
692 vid_usevsync = vid_vsync.integer && !cls.timedemo && gl_videosyncavailable;
693 if (vid_usingvsync != vid_usevsync && gl_videosyncavailable)
695 vid_usingvsync = vid_usevsync;
696 if (qglXSwapIntervalSGI (vid_usevsync))
697 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");
703 if (r_speeds.integer == 2 || gl_finish.integer)
705 qglFinish();CHECKGLERROR
707 qglXSwapBuffers(vidx11_display, win);CHECKGLERROR
710 if (vid_x11_hardwaregammasupported)
711 VID_UpdateGamma(false, vid_x11_gammarampsize);
714 int VID_SetGamma(unsigned short *ramps, int rampsize)
716 return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, rampsize, ramps, ramps + rampsize, ramps + rampsize*2);
719 int VID_GetGamma(unsigned short *ramps, int rampsize)
721 return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, rampsize, ramps, ramps + rampsize, ramps + rampsize*2);
726 #if !defined(__APPLE__) && !defined(SUNOS)
727 Cvar_RegisterVariable (&vid_dgamouse);
729 Cvar_RegisterVariable (&vid_netwmfullscreen);
730 InitSig(); // trap evil signals
731 // COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
732 if (COM_CheckParm ("-nomouse"))
736 void VID_BuildGLXAttrib(int *attrib, qboolean stencil, qboolean stereobuffer, int samples)
738 *attrib++ = GLX_RGBA;
739 *attrib++ = GLX_RED_SIZE;*attrib++ = stencil ? 8 : 5;
740 *attrib++ = GLX_GREEN_SIZE;*attrib++ = stencil ? 8 : 5;
741 *attrib++ = GLX_BLUE_SIZE;*attrib++ = stencil ? 8 : 5;
742 *attrib++ = GLX_DOUBLEBUFFER;
743 *attrib++ = GLX_DEPTH_SIZE;*attrib++ = stencil ? 24 : 16;
744 // if stencil is enabled, ask for alpha too
747 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
748 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 8;
751 *attrib++ = GLX_STEREO;
754 *attrib++ = GLX_SAMPLE_BUFFERS_ARB;
756 *attrib++ = GLX_SAMPLES_ARB;
762 qboolean VID_InitMode(viddef_mode_t *mode)
766 XSetWindowAttributes attr;
767 XClassHint *clshints;
771 XVisualInfo *visinfo;
772 int MajorVersion, MinorVersion;
773 const char *drivername;
775 vid_isfullscreen = false;
776 vid_isnetwmfullscreen = false;
777 vid_isvidmodefullscreen = false;
778 vid_isoverrideredirect = false;
780 #if defined(__APPLE__) && defined(__MACH__)
781 drivername = "/usr/X11R6/lib/libGL.1.dylib";
783 drivername = "libGL.so.1";
785 // 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
786 // 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
787 // LordHavoc: although this works on MacOSX, it's useless there (as there is only one system libGL)
788 i = COM_CheckParm("-gl_driver");
789 if (i && i < com_argc - 1)
790 drivername = com_argv[i + 1];
791 if (!GL_OpenLibrary(drivername))
793 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
797 if (!(vidx11_display = XOpenDisplay(NULL)))
799 Con_Print("Couldn't open the X display\n");
803 // LordHavoc: making the close button on a window do the right thing
804 // seems to involve this mess, sigh...
805 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
806 net_wm_state_atom = XInternAtom(vidx11_display, "_NET_WM_STATE", false);
807 net_wm_state_fullscreen_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_FULLSCREEN", false);
808 net_wm_state_hidden_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_HIDDEN", false);
810 // make autorepeat send keypress/keypress/.../keyrelease instead of intervening keyrelease
811 XkbSetDetectableAutoRepeat(vidx11_display, true, NULL);
813 vidx11_screen = DefaultScreen(vidx11_display);
814 root = RootWindow(vidx11_display, vidx11_screen);
816 // Get video mode list
817 MajorVersion = MinorVersion = 0;
818 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
822 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
826 if ((qglXChooseVisual = (XVisualInfo *(GLAPIENTRY *)(Display *dpy, int screen, int *attribList))GL_GetProcAddress("glXChooseVisual")) == NULL
827 || (qglXCreateContext = (GLXContext (GLAPIENTRY *)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct))GL_GetProcAddress("glXCreateContext")) == NULL
828 || (qglXDestroyContext = (void (GLAPIENTRY *)(Display *dpy, GLXContext ctx))GL_GetProcAddress("glXDestroyContext")) == NULL
829 || (qglXMakeCurrent = (Bool (GLAPIENTRY *)(Display *dpy, GLXDrawable drawable, GLXContext ctx))GL_GetProcAddress("glXMakeCurrent")) == NULL
830 || (qglXSwapBuffers = (void (GLAPIENTRY *)(Display *dpy, GLXDrawable drawable))GL_GetProcAddress("glXSwapBuffers")) == NULL
831 || (qglXQueryExtensionsString = (const char *(GLAPIENTRY *)(Display *dpy, int screen))GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
833 Con_Printf("glX functions not found in %s\n", gl_driver);
837 VID_BuildGLXAttrib(attrib, mode->bitsperpixel == 32, mode->stereobuffer, mode->samples);
838 visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
841 Con_Print("Couldn't get an RGB, Double-buffered, Depth visual\n");
845 if (mode->fullscreen)
847 if(vid_netwmfullscreen.integer)
849 // TODO detect WM support
850 vid_isnetwmfullscreen = true;
851 vid_isfullscreen = true;
852 // width and height will be filled in later
853 Con_DPrintf("Using NetWM fullscreen mode\n");
856 if(!vid_isfullscreen && vidmode_ext)
858 int best_fit, best_dist, dist, x, y;
860 // Are we going fullscreen? If so, let's change video mode
861 XF86VidModeModeLine *current_vidmode;
862 XF86VidModeModeInfo **vidmodes;
865 // This nice hack comes from the SDL source code
866 current_vidmode = (XF86VidModeModeLine*)((char*)&init_vidmode + sizeof(init_vidmode.dotclock));
867 XF86VidModeGetModeLine(vidx11_display, vidx11_screen, (int*)&init_vidmode.dotclock, current_vidmode);
869 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
873 for (i = 0; i < num_vidmodes; i++)
875 if (mode->width > vidmodes[i]->hdisplay || mode->height > vidmodes[i]->vdisplay)
878 x = mode->width - vidmodes[i]->hdisplay;
879 y = mode->height - vidmodes[i]->vdisplay;
880 dist = (x * x) + (y * y);
881 if (best_fit == -1 || dist < best_dist)
890 // LordHavoc: changed from ActualWidth/ActualHeight =,
891 // to width/height =, so the window will take the full area of
893 mode->width = vidmodes[best_fit]->hdisplay;
894 mode->height = vidmodes[best_fit]->vdisplay;
896 // change to the mode
897 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
898 memcpy(&game_vidmode, vidmodes[best_fit], sizeof(game_vidmode));
899 vid_isvidmodefullscreen = true;
900 vid_isfullscreen = true;
902 // Move the viewport to top left
903 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
904 Con_DPrintf("Using XVidMode fullscreen mode at %dx%d\n", mode->width, mode->height);
910 if(!vid_isfullscreen)
912 // sorry, no FS available
913 // use the full desktop resolution
914 vid_isfullscreen = true;
915 // width and height will be filled in later
916 mode->width = DisplayWidth(vidx11_display, vidx11_screen);
917 mode->height = DisplayHeight(vidx11_display, vidx11_screen);
918 Con_DPrintf("Using X11 fullscreen mode at %dx%d\n", mode->width, mode->height);
922 // LordHavoc: save the visual for use in gamma ramp settings later
923 vidx11_visual = visinfo->visual;
925 /* window attributes */
926 attr.background_pixel = 0;
927 attr.border_pixel = 0;
928 // LordHavoc: save the colormap for later, too
929 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
930 attr.event_mask = X_MASK;
932 if (mode->fullscreen)
934 if(vid_isnetwmfullscreen)
936 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask;
937 attr.backing_store = NotUseful;
938 attr.save_under = False;
942 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
943 attr.override_redirect = True;
944 attr.backing_store = NotUseful;
945 attr.save_under = False;
946 vid_isoverrideredirect = true; // so it knows to grab
951 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
954 win = XCreateWindow(vidx11_display, root, 0, 0, mode->width, mode->height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
956 wmhints = XAllocWMHints();
957 if(XpmCreatePixmapFromData(vidx11_display, win,
958 (gamemode == GAME_NEXUIZ) ? nexuiz_xpm : darkplaces_xpm,
959 &wmhints->icon_pixmap, &wmhints->icon_mask, NULL) == XpmSuccess)
960 wmhints->flags |= IconPixmapHint | IconMaskHint;
962 clshints = XAllocClassHint();
963 clshints->res_name = strdup(gamename);
964 clshints->res_class = strdup("DarkPlaces");
966 szhints = XAllocSizeHints();
967 if(vid_resizable.integer == 0 && !vid_isnetwmfullscreen)
969 szhints->min_width = szhints->max_width = mode->width;
970 szhints->min_height = szhints->max_height = mode->height;
971 szhints->flags |= PMinSize | PMaxSize;
974 XmbSetWMProperties(vidx11_display, win, gamename, gamename, (char **) com_argv, com_argc, szhints, wmhints, clshints);
979 //XStoreName(vidx11_display, win, gamename);
980 XMapWindow(vidx11_display, win);
982 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
984 if (vid_isoverrideredirect)
986 XMoveWindow(vidx11_display, win, 0, 0);
987 XRaiseWindow(vidx11_display, win);
988 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
989 XFlush(vidx11_display);
992 if(vid_isvidmodefullscreen)
994 // Move the viewport to top left
995 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
998 //XSync(vidx11_display, False);
1000 ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
1003 Con_Printf ("glXCreateContext failed\n");
1007 if (!qglXMakeCurrent(vidx11_display, win, ctx))
1009 Con_Printf ("glXMakeCurrent failed\n");
1013 XSync(vidx11_display, False);
1015 if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
1017 Con_Printf ("glGetString not found in %s\n", gl_driver);
1021 gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
1022 gl_platform = "GLX";
1023 gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
1025 // COMMANDLINEOPTION: Linux GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1026 // COMMANDLINEOPTION: BSD GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1027 // COMMANDLINEOPTION: MacOSX GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1028 GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
1029 // COMMANDLINEOPTION: Linux GLX: -novideosync disables GLX_SGI_swap_control
1030 // COMMANDLINEOPTION: BSD GLX: -novideosync disables GLX_SGI_swap_control
1031 // COMMANDLINEOPTION: MacOSX GLX: -novideosync disables GLX_SGI_swap_control
1032 gl_videosyncavailable = GL_CheckExtension("GLX_SGI_swap_control", swapcontrolfuncs, "-novideosync", false);
1034 vid_usingmousegrab = false;
1035 vid_usingmouse = false;
1036 vid_usinghidecursor = false;
1037 vid_usingvsync = false;
1039 vid_activewindow = true;
1040 vid_x11_hardwaregammasupported = XF86VidModeGetGammaRampSize(vidx11_display, vidx11_screen, &vid_x11_gammarampsize) != 0;
1041 #if !defined(__APPLE__) && !defined(SUNOS)
1042 vid_x11_dgasupported = XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion);
1043 if (!vid_x11_dgasupported)
1044 Con_Print( "Failed to detect XF86DGA Mouse extension\n" );
1051 void Sys_SendKeyEvents(void)
1053 static qboolean sound_active = true;
1055 // enable/disable sound on focus gain/loss
1056 if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
1061 sound_active = true;
1069 sound_active = false;
1080 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
1086 XF86VidModeModeInfo **vidmodes;
1089 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
1091 for (i = 0; i < num_vidmodes; i++)
1095 // we don't get bpp info, so let's just assume all of 8, 15, 16, 24, 32 work
1096 for(bpp = 8; bpp <= 32; bpp = ((bpp == 8) ? 15 : (bpp & 0xF8) + 8))
1100 modes[k].width = vidmodes[i]->hdisplay;
1101 modes[k].height = vidmodes[i]->vdisplay;
1103 if(vidmodes[i]->dotclock && vidmodes[i]->htotal && vidmodes[i]->vtotal)
1104 modes[k].refreshrate = vidmodes[i]->dotclock / vidmodes[i]->htotal / vidmodes[i]->vtotal;
1106 modes[k].refreshrate = 60;
1107 modes[k].pixelheight_num = 1;
1108 modes[k].pixelheight_denom = 1; // xvidmode does not provide this
1114 return 0; // FIXME implement this