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>
46 #include "darkplaces.xpm"
48 // Tell startup code that we have a client
49 int cl_available = true;
51 // note: if we used the XRandR extension we could support refresh rates
52 qboolean vid_supportrefreshrate = false;
55 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
56 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
57 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
58 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
59 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
60 const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
62 //GLX_ARB_get_proc_address
63 void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
65 static dllfunction_t getprocaddressfuncs[] =
67 {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
71 //GLX_SGI_swap_control
72 GLint (GLAPIENTRY *qglXSwapIntervalSGI)(GLint interval);
74 static dllfunction_t swapcontrolfuncs[] =
76 {"glXSwapIntervalSGI", (void **) &qglXSwapIntervalSGI},
80 static Display *vidx11_display = NULL;
81 static int vidx11_screen;
82 static Window win, root;
83 static GLXContext ctx = NULL;
85 Atom wm_delete_window_atom;
86 Atom net_wm_state_atom;
87 Atom net_wm_state_hidden_atom;
88 Atom net_wm_state_fullscreen_atom;
90 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
91 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
92 PointerMotionMask | ButtonMotionMask)
93 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | \
94 StructureNotifyMask | FocusChangeMask | EnterWindowMask | \
98 static qboolean mouse_avail = true;
99 static qboolean vid_usingmousegrab = false;
100 static qboolean vid_usingmouse = false;
101 static qboolean vid_usinghidecursor = false;
102 static qboolean vid_usingvsync = false;
103 static qboolean vid_usevsync = false;
104 static qboolean vid_x11_hardwaregammasupported = false;
105 static qboolean vid_x11_dgasupported = false;
106 static int vid_x11_gammarampsize = 0;
108 #if !defined(__APPLE__) && !defined(SUNOS)
109 cvar_t vid_dgamouse = {CVAR_SAVE, "vid_dgamouse", "0", "make use of DGA mouse input"};
110 static qboolean vid_usingdgamouse = false;
112 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"};
114 qboolean vidmode_ext = false;
116 static int win_x, win_y;
118 static XF86VidModeModeInfo init_vidmode, game_vidmode;
119 static qboolean vid_isfullscreen = false;
120 static qboolean vid_isvidmodefullscreen = false;
121 static qboolean vid_isnetwmfullscreen = false;
122 static qboolean vid_isoverrideredirect = false;
124 static Visual *vidx11_visual;
125 static Colormap vidx11_colormap;
127 /*-----------------------------------------------------------------------*/
130 long keysym2ucs(KeySym keysym);
131 void DP_Xutf8LookupString(XKeyEvent * ev,
133 KeySym * keysym_return,
134 Status * status_return)
140 int nbytes = sizeof(buffer);
142 rc = XLookupString(ev, buffer, nbytes, &keysym, NULL);
145 codepoint = buffer[0] & 0xFF;
147 codepoint = keysym2ucs(keysym);
151 if (keysym == None) {
152 *status_return = XLookupNone;
154 *status_return = XLookupKeySym;
155 *keysym_return = keysym;
163 if (keysym != None) {
164 *keysym_return = keysym;
165 *status_return = XLookupBoth;
167 *status_return = XLookupChars;
170 static int XLateKey(XKeyEvent *ev, Uchar *ascii)
174 KeySym keysym, shifted;
177 keysym = XLookupKeysym (ev, 0);
178 DP_Xutf8LookupString(ev, ascii, &shifted, &status);
182 case XK_KP_Page_Up: key = K_KP_PGUP; break;
183 case XK_Page_Up: key = K_PGUP; break;
185 case XK_KP_Page_Down: key = K_KP_PGDN; break;
186 case XK_Page_Down: key = K_PGDN; break;
188 case XK_KP_Home: key = K_KP_HOME; break;
189 case XK_Home: key = K_HOME; break;
191 case XK_KP_End: key = K_KP_END; break;
192 case XK_End: key = K_END; break;
194 case XK_KP_Left: key = K_KP_LEFTARROW; break;
195 case XK_Left: key = K_LEFTARROW; break;
197 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
198 case XK_Right: key = K_RIGHTARROW; break;
200 case XK_KP_Down: key = K_KP_DOWNARROW; break;
201 case XK_Down: key = K_DOWNARROW; break;
203 case XK_KP_Up: key = K_KP_UPARROW; break;
204 case XK_Up: key = K_UPARROW; break;
206 case XK_Escape: key = K_ESCAPE; break;
208 case XK_KP_Enter: key = K_KP_ENTER; break;
209 case XK_Return: key = K_ENTER; break;
211 case XK_Tab: key = K_TAB; break;
213 case XK_F1: key = K_F1; break;
215 case XK_F2: key = K_F2; break;
217 case XK_F3: key = K_F3; break;
219 case XK_F4: key = K_F4; break;
221 case XK_F5: key = K_F5; break;
223 case XK_F6: key = K_F6; break;
225 case XK_F7: key = K_F7; break;
227 case XK_F8: key = K_F8; break;
229 case XK_F9: key = K_F9; break;
231 case XK_F10: key = K_F10; break;
233 case XK_F11: key = K_F11; break;
235 case XK_F12: key = K_F12; break;
237 case XK_BackSpace: key = K_BACKSPACE; break;
239 case XK_KP_Delete: key = K_KP_DEL; break;
240 case XK_Delete: key = K_DEL; break;
242 case XK_Pause: key = K_PAUSE; break;
245 case XK_Shift_R: key = K_SHIFT; break;
249 case XK_Control_R: key = K_CTRL; break;
252 case XK_Super_R: key = K_SUPER; break;
256 case XK_ISO_Level3_Shift:
257 case XK_Alt_R: key = K_ALT; break;
259 case XK_KP_Begin: key = K_KP_5; break;
261 case XK_Insert:key = K_INS; break;
262 case XK_KP_Insert: key = K_KP_INS; break;
264 case XK_KP_Multiply: key = K_KP_MULTIPLY; break;
265 case XK_KP_Add: key = K_KP_PLUS; break;
266 case XK_KP_Subtract: key = K_KP_MINUS; break;
267 case XK_KP_Divide: key = K_KP_SLASH; break;
269 case XK_section: key = '~'; break;
275 if (keysym >= 'A' && keysym <= 'Z')
276 key = keysym - 'A' + 'a';
286 static Cursor CreateNullCursor(Display *display, Window root)
294 cursormask = XCreatePixmap(display, root, 1, 1, 1);
295 xgc.function = GXclear;
296 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
297 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
298 dummycolour.pixel = 0;
300 dummycolour.flags = 04;
301 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
302 XFreePixmap(display,cursormask);
307 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
309 static int originalmouseparms_num;
310 static int originalmouseparms_denom;
311 static int originalmouseparms_threshold;
312 static qboolean restore_spi;
314 #if !defined(__APPLE__) && !defined(SUNOS)
315 qboolean usedgamouse;
318 if (!vidx11_display || !win)
322 fullscreengrab = true;
325 fullscreengrab = relative = hidecursor = false;
327 #if !defined(__APPLE__) && !defined(SUNOS)
328 usedgamouse = relative && vid_dgamouse.integer;
329 if (!vid_x11_dgasupported)
331 if (fullscreengrab && vid_usingmouse && (vid_usingdgamouse != usedgamouse))
332 VID_SetMouse(false, false, false); // ungrab first!
335 if (vid_usingmousegrab != fullscreengrab)
337 vid_usingmousegrab = fullscreengrab;
338 cl_ignoremousemoves = 2;
341 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
342 if (vid_grabkeyboard.integer || vid_isoverrideredirect)
343 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
347 XUngrabPointer(vidx11_display, CurrentTime);
348 XUngrabKeyboard(vidx11_display, CurrentTime);
356 XWindowAttributes attribs_1;
357 XSetWindowAttributes attribs_2;
359 XGetWindowAttributes(vidx11_display, win, &attribs_1);
360 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
361 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
363 #if !defined(__APPLE__) && !defined(SUNOS)
364 vid_usingdgamouse = usedgamouse;
367 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
368 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
372 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
374 // COMMANDLINEOPTION: X11 Input: -noforcemparms disables setting of mouse parameters (not used with DGA, windows only)
375 #if !defined(__APPLE__) && !defined(SUNOS)
376 if (!COM_CheckParm ("-noforcemparms") && !usedgamouse)
378 if (!COM_CheckParm ("-noforcemparms"))
381 XGetPointerControl(vidx11_display, &originalmouseparms_num, &originalmouseparms_denom, &originalmouseparms_threshold);
382 XChangePointerControl (vidx11_display, true, false, 1, 1, -1); // TODO maybe change threshold here, or remove this comment
388 cl_ignoremousemoves = 2;
389 vid_usingmouse = true;
396 #if !defined(__APPLE__) && !defined(SUNOS)
397 if (vid_usingdgamouse)
398 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
399 vid_usingdgamouse = false;
401 cl_ignoremousemoves = 2;
404 XChangePointerControl (vidx11_display, true, true, originalmouseparms_num, originalmouseparms_denom, originalmouseparms_threshold);
407 vid_usingmouse = false;
411 if (vid_usinghidecursor != hidecursor)
413 vid_usinghidecursor = hidecursor;
415 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
417 XUndefineCursor(vidx11_display, win);
421 static keynum_t buttonremap[18] =
443 static void HandleEvents(void)
448 qboolean dowarp = false;
453 while (XPending(vidx11_display))
455 XNextEvent(vidx11_display, &event);
461 key = XLateKey (&event.xkey, &unicode);
462 Key_Event(key, unicode, true);
467 key = XLateKey (&event.xkey, &unicode);
468 Key_Event(key, unicode, false);
475 #if !defined(__APPLE__) && !defined(SUNOS)
476 if (vid_usingdgamouse)
478 in_mouse_x += event.xmotion.x_root;
479 in_mouse_y += event.xmotion.y_root;
484 if (!event.xmotion.send_event)
486 in_mouse_x += event.xmotion.x - in_windowmouse_x;
487 in_mouse_y += event.xmotion.y - in_windowmouse_y;
488 //if (abs(vid.width/2 - event.xmotion.x) + abs(vid.height/2 - event.xmotion.y))
489 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)
494 in_windowmouse_x = event.xmotion.x;
495 in_windowmouse_y = event.xmotion.y;
499 // mouse button pressed
500 if (event.xbutton.button <= 18)
501 Key_Event(buttonremap[event.xbutton.button - 1], 0, true);
503 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-18 expected\n", event.xbutton.button);
507 // mouse button released
508 if (event.xbutton.button <= 18)
509 Key_Event(buttonremap[event.xbutton.button - 1], 0, false);
511 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-18 expected\n", event.xbutton.button);
516 win_x = event.xcreatewindow.x;
517 win_y = event.xcreatewindow.y;
520 case ConfigureNotify:
521 // window changed size/location
522 win_x = event.xconfigure.x;
523 win_y = event.xconfigure.y;
524 if(vid_resizable.integer < 2 || vid_isnetwmfullscreen)
526 vid.width = event.xconfigure.width;
527 vid.height = event.xconfigure.height;
528 if(vid_isnetwmfullscreen)
529 Con_Printf("NetWM fullscreen: actually using resolution %dx%d\n", vid.width, vid.height);
531 Con_DPrintf("Updating to ConfigureNotify resolution %dx%d\n", vid.width, vid.height);
535 // window has been destroyed
539 // window manager messages
540 if ((event.xclient.format == 32) && ((unsigned int)event.xclient.data.l[0] == wm_delete_window_atom))
544 if (vid_isoverrideredirect)
548 VID_RestoreSystemGamma();
550 if(vid_isvidmodefullscreen)
552 // set our video mode
553 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &game_vidmode);
555 // Move the viewport to top left
556 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
559 if(vid_isnetwmfullscreen)
561 // make sure it's fullscreen
563 event.type = ClientMessage;
564 event.xclient.serial = 0;
565 event.xclient.send_event = True;
566 event.xclient.message_type = net_wm_state_atom;
567 event.xclient.window = win;
568 event.xclient.format = 32;
569 event.xclient.data.l[0] = 1;
570 event.xclient.data.l[1] = net_wm_state_fullscreen_atom;
571 event.xclient.data.l[2] = 0;
572 event.xclient.data.l[3] = 1;
573 event.xclient.data.l[4] = 0;
574 XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
581 if (vid_isoverrideredirect)
583 // window iconified/rolledup/whatever
585 VID_RestoreSystemGamma();
587 if(vid_isvidmodefullscreen)
588 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
592 if (vid_isoverrideredirect)
594 // window is now the input focus
595 vid_activewindow = true;
598 if (vid_isoverrideredirect)
601 if(vid_isnetwmfullscreen && event.xfocus.mode == NotifyNormal)
603 // iconify netwm fullscreen window when it loses focus
604 // when the user selects it in the taskbar, the window manager will map it again and send MapNotify
606 event.type = ClientMessage;
607 event.xclient.serial = 0;
608 event.xclient.send_event = True;
609 event.xclient.message_type = net_wm_state_atom;
610 event.xclient.window = win;
611 event.xclient.format = 32;
612 event.xclient.data.l[0] = 1;
613 event.xclient.data.l[1] = net_wm_state_hidden_atom;
614 event.xclient.data.l[2] = 0;
615 event.xclient.data.l[3] = 1;
616 event.xclient.data.l[4] = 0;
617 XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
620 // window is no longer the input focus
621 vid_activewindow = false;
622 VID_RestoreSystemGamma();
626 // mouse entered window
636 /* move the mouse to the window center again */
637 // we'll catch the warp motion by its send_event flag, updating the
638 // stored mouse position without adding any delta motion
640 event.type = MotionNotify;
641 event.xmotion.display = vidx11_display;
642 event.xmotion.window = win;
643 event.xmotion.x = vid.width / 2;
644 event.xmotion.y = vid.height / 2;
645 XSendEvent(vidx11_display, win, False, PointerMotionMask, &event);
646 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
650 static void *prjobj = NULL;
652 static void GL_CloseLibrary(void)
658 qglXGetProcAddressARB = NULL;
661 gl_platformextensions = "";
664 static int GL_OpenLibrary(const char *name)
666 Con_Printf("Loading OpenGL driver %s\n", name);
668 if (!(prjobj = dlopen(name, RTLD_LAZY | RTLD_GLOBAL)))
670 Con_Printf("Unable to open symbol list for %s\n", name);
673 strlcpy(gl_driver, name, sizeof(gl_driver));
677 void *GL_GetProcAddress(const char *name)
680 if (qglXGetProcAddressARB != NULL)
681 p = (void *) qglXGetProcAddressARB((GLubyte *)name);
683 p = (void *) dlsym(prjobj, name);
687 void VID_Shutdown(void)
689 if (!ctx || !vidx11_display)
692 VID_SetMouse(false, false, false);
693 VID_RestoreSystemGamma();
695 // FIXME: glXDestroyContext here?
696 if (vid_isvidmodefullscreen)
697 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
699 XDestroyWindow(vidx11_display, win);
700 XCloseDisplay(vidx11_display);
703 vid_isfullscreen = false;
704 vid_isnetwmfullscreen = false;
705 vid_isvidmodefullscreen = false;
706 vid_isoverrideredirect = false;
707 vidx11_display = NULL;
715 void signal_handler(int sig)
717 Con_Printf("Received signal %d, exiting...\n", sig);
718 VID_RestoreSystemGamma();
724 signal(SIGHUP, signal_handler);
725 signal(SIGINT, signal_handler);
726 signal(SIGQUIT, signal_handler);
727 signal(SIGILL, signal_handler);
728 signal(SIGTRAP, signal_handler);
729 signal(SIGIOT, signal_handler);
730 signal(SIGBUS, signal_handler);
731 signal(SIGFPE, signal_handler);
732 signal(SIGSEGV, signal_handler);
733 signal(SIGTERM, signal_handler);
736 void VID_Finish (void)
738 vid_usevsync = vid_vsync.integer && !cls.timedemo && qglXSwapIntervalSGI;
739 if (vid_usingvsync != vid_usevsync)
741 vid_usingvsync = vid_usevsync;
742 if (qglXSwapIntervalSGI (vid_usevsync))
743 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");
749 if (r_speeds.integer == 2 || gl_finish.integer)
751 qglFinish();CHECKGLERROR
753 qglXSwapBuffers(vidx11_display, win);CHECKGLERROR
756 if (vid_x11_hardwaregammasupported)
757 VID_UpdateGamma(false, vid_x11_gammarampsize);
760 int VID_SetGamma(unsigned short *ramps, int rampsize)
762 return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, rampsize, ramps, ramps + rampsize, ramps + rampsize*2);
765 int VID_GetGamma(unsigned short *ramps, int rampsize)
767 return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, rampsize, ramps, ramps + rampsize, ramps + rampsize*2);
772 #if !defined(__APPLE__) && !defined(SUNOS)
773 Cvar_RegisterVariable (&vid_dgamouse);
775 Cvar_RegisterVariable (&vid_netwmfullscreen);
776 InitSig(); // trap evil signals
777 // COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
778 if (COM_CheckParm ("-nomouse"))
782 void VID_BuildGLXAttrib(int *attrib, qboolean stencil, qboolean stereobuffer, int samples)
784 *attrib++ = GLX_RGBA;
785 *attrib++ = GLX_RED_SIZE;*attrib++ = stencil ? 8 : 5;
786 *attrib++ = GLX_GREEN_SIZE;*attrib++ = stencil ? 8 : 5;
787 *attrib++ = GLX_BLUE_SIZE;*attrib++ = stencil ? 8 : 5;
788 *attrib++ = GLX_DOUBLEBUFFER;
789 *attrib++ = GLX_DEPTH_SIZE;*attrib++ = stencil ? 24 : 16;
790 // if stencil is enabled, ask for alpha too
793 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
794 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 8;
797 *attrib++ = GLX_STEREO;
800 *attrib++ = GLX_SAMPLE_BUFFERS_ARB;
802 *attrib++ = GLX_SAMPLES_ARB;
808 qboolean VID_InitMode(viddef_mode_t *mode)
812 XSetWindowAttributes attr;
813 XClassHint *clshints;
817 XVisualInfo *visinfo;
818 int MajorVersion, MinorVersion;
819 const char *drivername;
821 vid_isfullscreen = false;
822 vid_isnetwmfullscreen = false;
823 vid_isvidmodefullscreen = false;
824 vid_isoverrideredirect = false;
826 #if defined(__APPLE__) && defined(__MACH__)
827 drivername = "/usr/X11R6/lib/libGL.1.dylib";
829 drivername = "libGL.so.1";
831 // 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
832 // 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
833 // LordHavoc: although this works on MacOSX, it's useless there (as there is only one system libGL)
834 i = COM_CheckParm("-gl_driver");
835 if (i && i < com_argc - 1)
836 drivername = com_argv[i + 1];
837 if (!GL_OpenLibrary(drivername))
839 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
843 if (!(vidx11_display = XOpenDisplay(NULL)))
845 Con_Print("Couldn't open the X display\n");
849 // LordHavoc: making the close button on a window do the right thing
850 // seems to involve this mess, sigh...
851 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
852 net_wm_state_atom = XInternAtom(vidx11_display, "_NET_WM_STATE", false);
853 net_wm_state_fullscreen_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_FULLSCREEN", false);
854 net_wm_state_hidden_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_HIDDEN", false);
856 // make autorepeat send keypress/keypress/.../keyrelease instead of intervening keyrelease
857 XkbSetDetectableAutoRepeat(vidx11_display, true, NULL);
859 vidx11_screen = DefaultScreen(vidx11_display);
860 root = RootWindow(vidx11_display, vidx11_screen);
862 // Get video mode list
863 MajorVersion = MinorVersion = 0;
864 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
868 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
872 if ((qglXChooseVisual = (XVisualInfo *(GLAPIENTRY *)(Display *dpy, int screen, int *attribList))GL_GetProcAddress("glXChooseVisual")) == NULL
873 || (qglXCreateContext = (GLXContext (GLAPIENTRY *)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct))GL_GetProcAddress("glXCreateContext")) == NULL
874 || (qglXDestroyContext = (void (GLAPIENTRY *)(Display *dpy, GLXContext ctx))GL_GetProcAddress("glXDestroyContext")) == NULL
875 || (qglXMakeCurrent = (Bool (GLAPIENTRY *)(Display *dpy, GLXDrawable drawable, GLXContext ctx))GL_GetProcAddress("glXMakeCurrent")) == NULL
876 || (qglXSwapBuffers = (void (GLAPIENTRY *)(Display *dpy, GLXDrawable drawable))GL_GetProcAddress("glXSwapBuffers")) == NULL
877 || (qglXQueryExtensionsString = (const char *(GLAPIENTRY *)(Display *dpy, int screen))GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
879 Con_Printf("glX functions not found in %s\n", gl_driver);
883 VID_BuildGLXAttrib(attrib, mode->bitsperpixel == 32, mode->stereobuffer, mode->samples);
884 visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
887 Con_Print("Couldn't get an RGB, Double-buffered, Depth visual\n");
891 if (mode->fullscreen)
893 if(vid_netwmfullscreen.integer)
895 // TODO detect WM support
896 vid_isnetwmfullscreen = true;
897 vid_isfullscreen = true;
898 // width and height will be filled in later
899 Con_DPrintf("Using NetWM fullscreen mode\n");
902 if(!vid_isfullscreen && vidmode_ext)
904 int best_fit, best_dist, dist, x, y;
906 // Are we going fullscreen? If so, let's change video mode
907 XF86VidModeModeLine *current_vidmode;
908 XF86VidModeModeInfo **vidmodes;
911 // This nice hack comes from the SDL source code
912 current_vidmode = (XF86VidModeModeLine*)((char*)&init_vidmode + sizeof(init_vidmode.dotclock));
913 XF86VidModeGetModeLine(vidx11_display, vidx11_screen, (int*)&init_vidmode.dotclock, current_vidmode);
915 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
919 for (i = 0; i < num_vidmodes; i++)
921 if (mode->width > vidmodes[i]->hdisplay || mode->height > vidmodes[i]->vdisplay)
924 x = mode->width - vidmodes[i]->hdisplay;
925 y = mode->height - vidmodes[i]->vdisplay;
926 dist = (x * x) + (y * y);
927 if (best_fit == -1 || dist < best_dist)
936 // LordHavoc: changed from ActualWidth/ActualHeight =,
937 // to width/height =, so the window will take the full area of
939 mode->width = vidmodes[best_fit]->hdisplay;
940 mode->height = vidmodes[best_fit]->vdisplay;
942 // change to the mode
943 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
944 memcpy(&game_vidmode, vidmodes[best_fit], sizeof(game_vidmode));
945 vid_isvidmodefullscreen = true;
946 vid_isfullscreen = true;
948 // Move the viewport to top left
949 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
950 Con_DPrintf("Using XVidMode fullscreen mode at %dx%d\n", mode->width, mode->height);
956 if(!vid_isfullscreen)
958 // sorry, no FS available
959 // use the full desktop resolution
960 vid_isfullscreen = true;
961 // width and height will be filled in later
962 mode->width = DisplayWidth(vidx11_display, vidx11_screen);
963 mode->height = DisplayHeight(vidx11_display, vidx11_screen);
964 Con_DPrintf("Using X11 fullscreen mode at %dx%d\n", mode->width, mode->height);
968 // LordHavoc: save the visual for use in gamma ramp settings later
969 vidx11_visual = visinfo->visual;
971 /* window attributes */
972 attr.background_pixel = 0;
973 attr.border_pixel = 0;
974 // LordHavoc: save the colormap for later, too
975 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
976 attr.event_mask = X_MASK;
978 if (mode->fullscreen)
980 if(vid_isnetwmfullscreen)
982 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask;
983 attr.backing_store = NotUseful;
984 attr.save_under = False;
988 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
989 attr.override_redirect = True;
990 attr.backing_store = NotUseful;
991 attr.save_under = False;
992 vid_isoverrideredirect = true; // so it knows to grab
997 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1000 win = XCreateWindow(vidx11_display, root, 0, 0, mode->width, mode->height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
1002 wmhints = XAllocWMHints();
1003 if(XpmCreatePixmapFromData(vidx11_display, win,
1004 (gamemode == GAME_NEXUIZ) ? nexuiz_xpm : darkplaces_xpm,
1005 &wmhints->icon_pixmap, &wmhints->icon_mask, NULL) == XpmSuccess)
1006 wmhints->flags |= IconPixmapHint | IconMaskHint;
1008 clshints = XAllocClassHint();
1009 clshints->res_name = strdup(gamename);
1010 clshints->res_class = strdup("DarkPlaces");
1012 szhints = XAllocSizeHints();
1013 if(vid_resizable.integer == 0 && !vid_isnetwmfullscreen)
1015 szhints->min_width = szhints->max_width = mode->width;
1016 szhints->min_height = szhints->max_height = mode->height;
1017 szhints->flags |= PMinSize | PMaxSize;
1020 XmbSetWMProperties(vidx11_display, win, gamename, gamename, (char **) com_argv, com_argc, szhints, wmhints, clshints);
1021 // strdup() allocates using malloc(), should be freed with free()
1022 free(clshints->res_name);
1023 free(clshints->res_class);
1028 //XStoreName(vidx11_display, win, gamename);
1029 XMapWindow(vidx11_display, win);
1031 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
1033 if (vid_isoverrideredirect)
1035 XMoveWindow(vidx11_display, win, 0, 0);
1036 XRaiseWindow(vidx11_display, win);
1037 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
1038 XFlush(vidx11_display);
1041 if(vid_isvidmodefullscreen)
1043 // Move the viewport to top left
1044 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
1047 //XSync(vidx11_display, False);
1049 ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
1050 XFree(visinfo); // glXChooseVisual man page says to use XFree to free visinfo
1053 Con_Printf ("glXCreateContext failed\n");
1057 if (!qglXMakeCurrent(vidx11_display, win, ctx))
1059 Con_Printf ("glXMakeCurrent failed\n");
1063 XSync(vidx11_display, False);
1065 if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
1067 Con_Printf ("glGetString not found in %s\n", gl_driver);
1071 gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
1072 gl_platform = "GLX";
1073 gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
1075 // COMMANDLINEOPTION: Linux GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1076 // COMMANDLINEOPTION: BSD GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1077 // COMMANDLINEOPTION: MacOSX GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1078 GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
1079 // COMMANDLINEOPTION: Linux GLX: -novideosync disables GLX_SGI_swap_control
1080 // COMMANDLINEOPTION: BSD GLX: -novideosync disables GLX_SGI_swap_control
1081 // COMMANDLINEOPTION: MacOSX GLX: -novideosync disables GLX_SGI_swap_control
1082 GL_CheckExtension("GLX_SGI_swap_control", swapcontrolfuncs, "-novideosync", false);
1084 vid_usingmousegrab = false;
1085 vid_usingmouse = false;
1086 vid_usinghidecursor = false;
1087 vid_usingvsync = false;
1089 vid_activewindow = true;
1090 vid_x11_hardwaregammasupported = XF86VidModeGetGammaRampSize(vidx11_display, vidx11_screen, &vid_x11_gammarampsize) != 0;
1091 #if !defined(__APPLE__) && !defined(SUNOS)
1092 vid_x11_dgasupported = XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion);
1093 if (!vid_x11_dgasupported)
1094 Con_Print( "Failed to detect XF86DGA Mouse extension\n" );
1101 void Sys_SendKeyEvents(void)
1103 static qboolean sound_active = true;
1105 // enable/disable sound on focus gain/loss
1106 if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
1111 sound_active = true;
1119 sound_active = false;
1130 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
1136 XF86VidModeModeInfo **vidmodes;
1139 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
1141 for (i = 0; i < num_vidmodes; i++)
1145 // we don't get bpp info, so let's just assume all of 8, 15, 16, 24, 32 work
1146 for(bpp = 8; bpp <= 32; bpp = ((bpp == 8) ? 15 : (bpp & 0xF8) + 8))
1150 modes[k].width = vidmodes[i]->hdisplay;
1151 modes[k].height = vidmodes[i]->vdisplay;
1153 if(vidmodes[i]->dotclock && vidmodes[i]->htotal && vidmodes[i]->vtotal)
1154 modes[k].refreshrate = vidmodes[i]->dotclock / vidmodes[i]->htotal / vidmodes[i]->vtotal;
1156 modes[k].refreshrate = 60;
1157 modes[k].pixelheight_num = 1;
1158 modes[k].pixelheight_denom = 1; // xvidmode does not provide this
1162 // manpage of XF86VidModeGetAllModeLines says it should be freed by the caller
1166 return 0; // FIXME implement this