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)?
31 #include "dpsoftrast.h"
33 #include <X11/keysym.h>
34 #include <X11/cursorfont.h>
37 #include <X11/extensions/XShm.h>
38 #if !defined(__APPLE__) && !defined(__MACH__) && !defined(SUNOS)
39 #include <X11/extensions/xf86dga.h>
41 #include <X11/extensions/xf86vmode.h>
45 #include <X11/extensions/XShm.h>
52 #include "darkplaces.xpm"
54 // Tell startup code that we have a client
55 int cl_available = true;
57 // note: if we used the XRandR extension we could support refresh rates
58 qboolean vid_supportrefreshrate = false;
61 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
62 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
63 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
64 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
65 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
66 const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
68 //GLX_ARB_get_proc_address
69 void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
71 static dllfunction_t getprocaddressfuncs[] =
73 {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
77 //GLX_SGI_swap_control
78 GLint (GLAPIENTRY *qglXSwapIntervalSGI)(GLint interval);
80 static dllfunction_t swapcontrolfuncs[] =
82 {"glXSwapIntervalSGI", (void **) &qglXSwapIntervalSGI},
86 static Display *vidx11_display = NULL;
87 static int vidx11_screen;
88 static Window win, root;
89 static GLXContext ctx = NULL;
90 static GC vidx11_gc = NULL;
91 static XImage *vidx11_ximage[2] = { NULL, NULL };
92 static int vidx11_ximage_pos = 0;
93 static XShmSegmentInfo vidx11_shminfo[2] = { -1, -1 };
94 static int vidx11_shmevent = -1;
95 static int vidx11_shmwait = 0; // number of frames outstanding
97 Atom wm_delete_window_atom;
98 Atom net_wm_state_atom;
99 Atom net_wm_state_hidden_atom;
100 Atom net_wm_state_fullscreen_atom;
104 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
105 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
106 PointerMotionMask | ButtonMotionMask)
107 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | \
108 StructureNotifyMask | FocusChangeMask | EnterWindowMask | \
112 static qboolean mouse_avail = true;
113 static qboolean vid_usingmousegrab = false;
114 static qboolean vid_usingmouse = false;
115 static qboolean vid_usinghidecursor = false;
116 static qboolean vid_usingvsync = false;
117 static qboolean vid_usevsync = false;
118 static qboolean vid_x11_hardwaregammasupported = false;
119 static qboolean vid_x11_dgasupported = false;
120 static int vid_x11_gammarampsize = 0;
122 #if !defined(__APPLE__) && !defined(SUNOS)
123 cvar_t vid_dgamouse = {CVAR_SAVE, "vid_dgamouse", "0", "make use of DGA mouse input"};
124 static qboolean vid_usingdgamouse = false;
126 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"};
128 qboolean vidmode_ext = false;
130 static int win_x, win_y;
132 static XF86VidModeModeInfo init_vidmode, game_vidmode;
133 static qboolean vid_isfullscreen = false;
134 static qboolean vid_isvidmodefullscreen = false;
135 static qboolean vid_isnetwmfullscreen = false;
136 static qboolean vid_isoverrideredirect = false;
138 static Visual *vidx11_visual;
139 static Colormap vidx11_colormap;
141 /*-----------------------------------------------------------------------*/
144 long keysym2ucs(KeySym keysym);
145 void DP_Xutf8LookupString(XKeyEvent * ev,
147 KeySym * keysym_return,
148 Status * status_return)
154 int nbytes = sizeof(buffer);
156 rc = XLookupString(ev, buffer, nbytes, &keysym, NULL);
159 codepoint = buffer[0] & 0xFF;
161 codepoint = keysym2ucs(keysym);
165 if (keysym == None) {
166 *status_return = XLookupNone;
168 *status_return = XLookupKeySym;
169 *keysym_return = keysym;
177 if (keysym != None) {
178 *keysym_return = keysym;
179 *status_return = XLookupBoth;
181 *status_return = XLookupChars;
184 static int XLateKey(XKeyEvent *ev, Uchar *ascii)
188 KeySym keysym, shifted;
191 keysym = XLookupKeysym (ev, 0);
192 DP_Xutf8LookupString(ev, ascii, &shifted, &status);
196 case XK_KP_Page_Up: key = K_KP_PGUP; break;
197 case XK_Page_Up: key = K_PGUP; break;
199 case XK_KP_Page_Down: key = K_KP_PGDN; break;
200 case XK_Page_Down: key = K_PGDN; break;
202 case XK_KP_Home: key = K_KP_HOME; break;
203 case XK_Home: key = K_HOME; break;
205 case XK_KP_End: key = K_KP_END; break;
206 case XK_End: key = K_END; break;
208 case XK_KP_Left: key = K_KP_LEFTARROW; break;
209 case XK_Left: key = K_LEFTARROW; break;
211 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
212 case XK_Right: key = K_RIGHTARROW; break;
214 case XK_KP_Down: key = K_KP_DOWNARROW; break;
215 case XK_Down: key = K_DOWNARROW; break;
217 case XK_KP_Up: key = K_KP_UPARROW; break;
218 case XK_Up: key = K_UPARROW; break;
220 case XK_Escape: key = K_ESCAPE; break;
222 case XK_KP_Enter: key = K_KP_ENTER; break;
223 case XK_Return: key = K_ENTER; break;
225 case XK_Tab: key = K_TAB; break;
227 case XK_F1: key = K_F1; break;
229 case XK_F2: key = K_F2; break;
231 case XK_F3: key = K_F3; break;
233 case XK_F4: key = K_F4; break;
235 case XK_F5: key = K_F5; break;
237 case XK_F6: key = K_F6; break;
239 case XK_F7: key = K_F7; break;
241 case XK_F8: key = K_F8; break;
243 case XK_F9: key = K_F9; break;
245 case XK_F10: key = K_F10; break;
247 case XK_F11: key = K_F11; break;
249 case XK_F12: key = K_F12; break;
251 case XK_BackSpace: key = K_BACKSPACE; break;
253 case XK_KP_Delete: key = K_KP_DEL; break;
254 case XK_Delete: key = K_DEL; break;
256 case XK_Pause: key = K_PAUSE; break;
259 case XK_Shift_R: key = K_SHIFT; break;
263 case XK_Control_R: key = K_CTRL; break;
267 case XK_ISO_Level3_Shift:
269 case XK_Meta_R: key = K_ALT; break;
271 case XK_KP_Begin: key = K_KP_5; break;
273 case XK_Insert:key = K_INS; break;
274 case XK_KP_Insert: key = K_KP_INS; break;
276 case XK_KP_Multiply: key = K_KP_MULTIPLY; break;
277 case XK_KP_Add: key = K_KP_PLUS; break;
278 case XK_KP_Subtract: key = K_KP_MINUS; break;
279 case XK_KP_Divide: key = K_KP_SLASH; break;
281 case XK_section: key = '~'; break;
287 if (keysym >= 'A' && keysym <= 'Z')
288 key = keysym - 'A' + 'a';
298 static Cursor CreateNullCursor(Display *display, Window root)
306 cursormask = XCreatePixmap(display, root, 1, 1, 1);
307 xgc.function = GXclear;
308 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
309 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
310 dummycolour.pixel = 0;
312 dummycolour.flags = 04;
313 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
314 XFreePixmap(display,cursormask);
319 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
321 static int originalmouseparms_num;
322 static int originalmouseparms_denom;
323 static int originalmouseparms_threshold;
324 static qboolean restore_spi;
326 #if !defined(__APPLE__) && !defined(SUNOS)
327 qboolean usedgamouse;
330 if (!vidx11_display || !win)
334 fullscreengrab = true;
337 fullscreengrab = relative = hidecursor = false;
339 #if !defined(__APPLE__) && !defined(SUNOS)
340 usedgamouse = relative && vid_dgamouse.integer;
341 if (!vid_x11_dgasupported)
343 if (fullscreengrab && vid_usingmouse && (vid_usingdgamouse != usedgamouse))
344 VID_SetMouse(false, false, false); // ungrab first!
347 if (vid_usingmousegrab != fullscreengrab)
349 vid_usingmousegrab = fullscreengrab;
350 cl_ignoremousemoves = 2;
353 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
354 if (vid_grabkeyboard.integer || vid_isoverrideredirect)
355 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
359 XUngrabPointer(vidx11_display, CurrentTime);
360 XUngrabKeyboard(vidx11_display, CurrentTime);
368 XWindowAttributes attribs_1;
369 XSetWindowAttributes attribs_2;
371 XGetWindowAttributes(vidx11_display, win, &attribs_1);
372 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
373 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
375 #if !defined(__APPLE__) && !defined(SUNOS)
376 vid_usingdgamouse = usedgamouse;
379 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
380 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
384 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
386 // COMMANDLINEOPTION: X11 Input: -noforcemparms disables setting of mouse parameters (not used with DGA, windows only)
387 #if !defined(__APPLE__) && !defined(SUNOS)
388 if (!COM_CheckParm ("-noforcemparms") && !usedgamouse)
390 if (!COM_CheckParm ("-noforcemparms"))
393 XGetPointerControl(vidx11_display, &originalmouseparms_num, &originalmouseparms_denom, &originalmouseparms_threshold);
394 XChangePointerControl (vidx11_display, true, false, 1, 1, -1); // TODO maybe change threshold here, or remove this comment
400 cl_ignoremousemoves = 2;
401 vid_usingmouse = true;
408 #if !defined(__APPLE__) && !defined(SUNOS)
409 if (vid_usingdgamouse)
410 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
411 vid_usingdgamouse = false;
413 cl_ignoremousemoves = 2;
416 XChangePointerControl (vidx11_display, true, true, originalmouseparms_num, originalmouseparms_denom, originalmouseparms_threshold);
419 vid_usingmouse = false;
423 if (vid_usinghidecursor != hidecursor)
425 vid_usinghidecursor = hidecursor;
427 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
429 XUndefineCursor(vidx11_display, win);
433 static keynum_t buttonremap[18] =
455 static qboolean BuildXImages(int w, int h)
458 if(DefaultDepth(vidx11_display, vidx11_screen) != 32 && DefaultDepth(vidx11_display, vidx11_screen) != 24)
460 Con_Printf("Sorry, we only support 24bpp and 32bpp modes\n");
464 // match to dpsoftrast's specs
465 if(vidx11_visual->red_mask != 0x00FF0000)
467 Con_Printf("Sorry, we only support BGR visuals\n");
471 if(vidx11_visual->green_mask != 0x0000FF00)
473 Con_Printf("Sorry, we only support BGR visuals\n");
477 if(vidx11_visual->blue_mask != 0x000000FF)
479 Con_Printf("Sorry, we only support BGR visuals\n");
483 if(vidx11_shmevent >= 0)
485 for(i = 0; i < 2; ++i)
487 vidx11_shminfo[i].shmid = -1;
488 vidx11_ximage[i] = XShmCreateImage(vidx11_display, vidx11_visual, DefaultDepth(vidx11_display, vidx11_screen), ZPixmap, NULL, &vidx11_shminfo[i], w, h);
489 if(!vidx11_ximage[i])
491 Con_Printf("Failed to get an XImage segment\n");
495 if(vidx11_ximage[i]->bytes_per_line != w * 4)
497 Con_Printf("Sorry, we only support linear pixel layout\n");
501 vidx11_shminfo[i].shmid = shmget(IPC_PRIVATE, vidx11_ximage[i]->bytes_per_line * vidx11_ximage[i]->height, IPC_CREAT|0777);
502 if(vidx11_shminfo[i].shmid < 0)
504 Con_Printf("Failed to get a shm segment\n");
508 vidx11_shminfo[i].shmaddr = vidx11_ximage[i]->data = shmat(vidx11_shminfo[i].shmid, NULL, 0);
509 if(!vidx11_shminfo[i].shmaddr)
511 Con_Printf("Failed to get a shm segment addresst\n");
515 vidx11_shminfo[i].readOnly = True;
516 XShmAttach(vidx11_display, &vidx11_shminfo[i]);
521 for(i = 0; i < 1; ++i) // we only need one buffer if we don't use Xshm
523 char *p = calloc(4, w * h);
524 vidx11_shminfo[i].shmid = -1;
525 vidx11_ximage[i] = XCreateImage(vidx11_display, vidx11_visual, DefaultDepth(vidx11_display, vidx11_screen), ZPixmap, 0, (char*)p, w, h, 8, 0);
526 if(!vidx11_ximage[i])
528 Con_Printf("Failed to get an XImage segment\n");
532 if(vidx11_ximage[i]->bytes_per_line != w * 4)
534 Con_Printf("Sorry, we only support linear pixel layout\n");
542 static void DestroyXImages(void)
545 for(i = 0; i < 2; ++i)
547 if(vidx11_shminfo[i].shmid >= 0)
549 XShmDetach(vidx11_display, &vidx11_shminfo[i]);
550 XDestroyImage(vidx11_ximage[i]);
551 vidx11_ximage[i] = NULL;
552 shmdt(vidx11_shminfo[i].shmaddr);
553 shmctl(vidx11_shminfo[i].shmid, IPC_RMID, 0);
554 vidx11_shminfo[i].shmid = -1;
557 XDestroyImage(vidx11_ximage[i]);
558 vidx11_ximage[i] = 0;
562 static int in_mouse_x_save = 0, in_mouse_y_save = 0;
563 static void HandleEvents(void)
568 qboolean dowarp = false;
573 in_mouse_x += in_mouse_x_save;
574 in_mouse_y += in_mouse_y_save;
578 while (XPending(vidx11_display))
580 XNextEvent(vidx11_display, &event);
586 key = XLateKey (&event.xkey, &unicode);
587 Key_Event(key, unicode, true);
592 key = XLateKey (&event.xkey, &unicode);
593 Key_Event(key, unicode, false);
600 #if !defined(__APPLE__) && !defined(SUNOS)
601 if (vid_usingdgamouse)
603 in_mouse_x += event.xmotion.x_root;
604 in_mouse_y += event.xmotion.y_root;
609 if (!event.xmotion.send_event)
611 in_mouse_x += event.xmotion.x - in_windowmouse_x;
612 in_mouse_y += event.xmotion.y - in_windowmouse_y;
613 //if (abs(vid.width/2 - event.xmotion.x) + abs(vid.height/2 - event.xmotion.y))
614 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)
619 in_windowmouse_x = event.xmotion.x;
620 in_windowmouse_y = event.xmotion.y;
624 // mouse button pressed
625 if (event.xbutton.button <= 18)
626 Key_Event(buttonremap[event.xbutton.button - 1], 0, true);
628 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-18 expected\n", event.xbutton.button);
632 // mouse button released
633 if (event.xbutton.button <= 18)
634 Key_Event(buttonremap[event.xbutton.button - 1], 0, false);
636 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-18 expected\n", event.xbutton.button);
641 win_x = event.xcreatewindow.x;
642 win_y = event.xcreatewindow.y;
645 case ConfigureNotify:
646 // window changed size/location
647 win_x = event.xconfigure.x;
648 win_y = event.xconfigure.y;
649 if((vid_resizable.integer < 2 || vid_isnetwmfullscreen) && (vid.width != event.xconfigure.width || vid.height != event.xconfigure.height))
651 vid.width = event.xconfigure.width;
652 vid.height = event.xconfigure.height;
653 if(vid_isnetwmfullscreen)
654 Con_Printf("NetWM fullscreen: actually using resolution %dx%d\n", vid.width, vid.height);
656 Con_DPrintf("Updating to ConfigureNotify resolution %dx%d\n", vid.width, vid.height);
660 if(vid.softdepthpixels)
661 free(vid.softdepthpixels);
664 XSync(vidx11_display, False);
665 if(!BuildXImages(vid.width, vid.height))
667 XSync(vidx11_display, False);
669 vid.softpixels = (unsigned int *) vidx11_ximage[vidx11_ximage_pos]->data;
670 vid.softdepthpixels = (unsigned int *)calloc(4, vid.width * vid.height);
674 // window has been destroyed
678 // window manager messages
679 if ((event.xclient.format == 32) && ((unsigned int)event.xclient.data.l[0] == wm_delete_window_atom))
683 if (vid_isoverrideredirect)
687 VID_RestoreSystemGamma();
689 if(vid_isvidmodefullscreen)
691 // set our video mode
692 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &game_vidmode);
694 // Move the viewport to top left
695 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
698 if(vid_isnetwmfullscreen)
700 // make sure it's fullscreen
702 event.type = ClientMessage;
703 event.xclient.serial = 0;
704 event.xclient.send_event = True;
705 event.xclient.message_type = net_wm_state_atom;
706 event.xclient.window = win;
707 event.xclient.format = 32;
708 event.xclient.data.l[0] = 1;
709 event.xclient.data.l[1] = net_wm_state_fullscreen_atom;
710 event.xclient.data.l[2] = 0;
711 event.xclient.data.l[3] = 1;
712 event.xclient.data.l[4] = 0;
713 XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
720 if (vid_isoverrideredirect)
722 // window iconified/rolledup/whatever
724 VID_RestoreSystemGamma();
726 if(vid_isvidmodefullscreen)
727 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
731 if (vid_isoverrideredirect)
733 // window is now the input focus
734 vid_activewindow = true;
737 if (vid_isoverrideredirect)
740 if(vid_isnetwmfullscreen && event.xfocus.mode == NotifyNormal)
742 // iconify netwm fullscreen window when it loses focus
743 // when the user selects it in the taskbar, the window manager will map it again and send MapNotify
745 event.type = ClientMessage;
746 event.xclient.serial = 0;
747 event.xclient.send_event = True;
748 event.xclient.message_type = net_wm_state_atom;
749 event.xclient.window = win;
750 event.xclient.format = 32;
751 event.xclient.data.l[0] = 1;
752 event.xclient.data.l[1] = net_wm_state_hidden_atom;
753 event.xclient.data.l[2] = 0;
754 event.xclient.data.l[3] = 1;
755 event.xclient.data.l[4] = 0;
756 XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
759 // window is no longer the input focus
760 vid_activewindow = false;
761 VID_RestoreSystemGamma();
765 // mouse entered window
771 if(vidx11_shmevent >= 0 && event.type == vidx11_shmevent)
779 /* move the mouse to the window center again */
780 // we'll catch the warp motion by its send_event flag, updating the
781 // stored mouse position without adding any delta motion
783 event.type = MotionNotify;
784 event.xmotion.display = vidx11_display;
785 event.xmotion.window = win;
786 event.xmotion.x = vid.width / 2;
787 event.xmotion.y = vid.height / 2;
788 XSendEvent(vidx11_display, win, False, PointerMotionMask, &event);
789 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
793 static void *prjobj = NULL;
795 static void GL_CloseLibrary(void)
801 qglXGetProcAddressARB = NULL;
804 gl_platformextensions = "";
807 static int GL_OpenLibrary(const char *name)
809 Con_Printf("Loading OpenGL driver %s\n", name);
811 if (!(prjobj = dlopen(name, RTLD_LAZY | RTLD_GLOBAL)))
813 Con_Printf("Unable to open symbol list for %s\n", name);
816 strlcpy(gl_driver, name, sizeof(gl_driver));
820 void *GL_GetProcAddress(const char *name)
823 if (qglXGetProcAddressARB != NULL)
824 p = (void *) qglXGetProcAddressARB((GLubyte *)name);
826 p = (void *) dlsym(prjobj, name);
830 void VID_Shutdown(void)
835 VID_SetMouse(false, false, false);
836 VID_RestoreSystemGamma();
838 // FIXME: glXDestroyContext here?
839 if (vid_isvidmodefullscreen)
840 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
843 XFreeGC(vidx11_display, vidx11_gc);
847 vidx11_shmevent = -1;
848 vid.softpixels = NULL;
850 if (vid.softdepthpixels)
851 free(vid.softdepthpixels);
852 vid.softdepthpixels = NULL;
855 XDestroyWindow(vidx11_display, win);
856 XCloseDisplay(vidx11_display);
859 vid_isfullscreen = false;
860 vid_isnetwmfullscreen = false;
861 vid_isvidmodefullscreen = false;
862 vid_isoverrideredirect = false;
863 vidx11_display = NULL;
871 void signal_handler(int sig)
873 Con_Printf("Received signal %d, exiting...\n", sig);
874 VID_RestoreSystemGamma();
880 signal(SIGHUP, signal_handler);
881 signal(SIGINT, signal_handler);
882 signal(SIGQUIT, signal_handler);
883 signal(SIGILL, signal_handler);
884 signal(SIGTRAP, signal_handler);
885 signal(SIGIOT, signal_handler);
886 signal(SIGBUS, signal_handler);
887 signal(SIGFPE, signal_handler);
888 signal(SIGSEGV, signal_handler);
889 signal(SIGTERM, signal_handler);
892 void VID_Finish (void)
894 vid_usevsync = vid_vsync.integer && !cls.timedemo && qglXSwapIntervalSGI;
895 switch(vid.renderpath)
897 case RENDERPATH_SOFT:
898 if(vidx11_shmevent >= 0) {
899 vidx11_ximage_pos = !vidx11_ximage_pos;
900 vid.softpixels = (unsigned int *) vidx11_ximage[vidx11_ximage_pos]->data;
901 DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
903 // save mouse motion so we can deal with it later
906 while(vidx11_shmwait)
908 in_mouse_x_save += in_mouse_x;
909 in_mouse_y_save += in_mouse_y;
914 XShmPutImage(vidx11_display, win, vidx11_gc, vidx11_ximage[!vidx11_ximage_pos], 0, 0, 0, 0, vid.width, vid.height, True);
916 // no buffer switching here, we just flush the renderer
918 XPutImage(vidx11_display, win, vidx11_gc, vidx11_ximage[vidx11_ximage_pos], 0, 0, 0, 0, vid.width, vid.height);
922 case RENDERPATH_GL11:
923 case RENDERPATH_GL13:
924 case RENDERPATH_GL20:
925 case RENDERPATH_GLES2:
926 if (vid_usingvsync != vid_usevsync)
928 vid_usingvsync = vid_usevsync;
929 if (qglXSwapIntervalSGI (vid_usevsync))
930 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");
936 if (r_speeds.integer == 2 || gl_finish.integer)
938 qglXSwapBuffers(vidx11_display, win);CHECKGLERROR
942 case RENDERPATH_D3D9:
943 case RENDERPATH_D3D10:
944 case RENDERPATH_D3D11:
948 if (vid_x11_hardwaregammasupported)
949 VID_UpdateGamma(false, vid_x11_gammarampsize);
952 int VID_SetGamma(unsigned short *ramps, int rampsize)
954 return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, rampsize, ramps, ramps + rampsize, ramps + rampsize*2);
957 int VID_GetGamma(unsigned short *ramps, int rampsize)
959 return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, rampsize, ramps, ramps + rampsize, ramps + rampsize*2);
964 #if !defined(__APPLE__) && !defined(SUNOS)
965 Cvar_RegisterVariable (&vid_dgamouse);
967 Cvar_RegisterVariable (&vid_netwmfullscreen);
968 InitSig(); // trap evil signals
969 // COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
970 if (COM_CheckParm ("-nomouse"))
972 vidx11_shminfo[0].shmid = -1;
973 vidx11_shminfo[1].shmid = -1;
976 void VID_BuildGLXAttrib(int *attrib, qboolean stencil, qboolean stereobuffer, int samples)
978 *attrib++ = GLX_RGBA;
979 *attrib++ = GLX_RED_SIZE;*attrib++ = stencil ? 8 : 5;
980 *attrib++ = GLX_GREEN_SIZE;*attrib++ = stencil ? 8 : 5;
981 *attrib++ = GLX_BLUE_SIZE;*attrib++ = stencil ? 8 : 5;
982 *attrib++ = GLX_DOUBLEBUFFER;
983 *attrib++ = GLX_DEPTH_SIZE;*attrib++ = stencil ? 24 : 16;
984 // if stencil is enabled, ask for alpha too
987 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
988 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 8;
991 *attrib++ = GLX_STEREO;
994 *attrib++ = GLX_SAMPLE_BUFFERS_ARB;
996 *attrib++ = GLX_SAMPLES_ARB;
1002 qboolean VID_InitModeSoft(viddef_mode_t *mode)
1005 XSetWindowAttributes attr;
1006 XClassHint *clshints;
1008 XSizeHints *szhints;
1010 int MajorVersion, MinorVersion;
1013 unsigned char *data;
1015 const char *dpyname;
1017 vid_isfullscreen = false;
1018 vid_isnetwmfullscreen = false;
1019 vid_isvidmodefullscreen = false;
1020 vid_isoverrideredirect = false;
1022 if (!(vidx11_display = XOpenDisplay(NULL)))
1024 Con_Print("Couldn't open the X display\n");
1027 dpyname = XDisplayName(NULL);
1029 // LordHavoc: making the close button on a window do the right thing
1030 // seems to involve this mess, sigh...
1031 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
1032 net_wm_state_atom = XInternAtom(vidx11_display, "_NET_WM_STATE", false);
1033 net_wm_state_fullscreen_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_FULLSCREEN", false);
1034 net_wm_state_hidden_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_HIDDEN", false);
1035 net_wm_icon = XInternAtom(vidx11_display, "_NET_WM_ICON", false);
1036 cardinal = XInternAtom(vidx11_display, "CARDINAL", false);
1038 // make autorepeat send keypress/keypress/.../keyrelease instead of intervening keyrelease
1039 XkbSetDetectableAutoRepeat(vidx11_display, true, NULL);
1041 vidx11_screen = DefaultScreen(vidx11_display);
1042 root = RootWindow(vidx11_display, vidx11_screen);
1044 // Get video mode list
1045 MajorVersion = MinorVersion = 0;
1046 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
1047 vidmode_ext = false;
1050 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
1054 if (mode->fullscreen)
1056 if(vid_netwmfullscreen.integer)
1058 // TODO detect WM support
1059 vid_isnetwmfullscreen = true;
1060 vid_isfullscreen = true;
1061 // width and height will be filled in later
1062 Con_DPrintf("Using NetWM fullscreen mode\n");
1065 if(!vid_isfullscreen && vidmode_ext)
1067 int best_fit, best_dist, dist, x, y;
1069 // Are we going fullscreen? If so, let's change video mode
1070 XF86VidModeModeLine *current_vidmode;
1071 XF86VidModeModeInfo **vidmodes;
1074 // This nice hack comes from the SDL source code
1075 current_vidmode = (XF86VidModeModeLine*)((char*)&init_vidmode + sizeof(init_vidmode.dotclock));
1076 XF86VidModeGetModeLine(vidx11_display, vidx11_screen, (int*)&init_vidmode.dotclock, current_vidmode);
1078 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
1082 for (i = 0; i < num_vidmodes; i++)
1084 if (mode->width > vidmodes[i]->hdisplay || mode->height > vidmodes[i]->vdisplay)
1087 x = mode->width - vidmodes[i]->hdisplay;
1088 y = mode->height - vidmodes[i]->vdisplay;
1089 dist = (x * x) + (y * y);
1090 if (best_fit == -1 || dist < best_dist)
1099 // LordHavoc: changed from ActualWidth/ActualHeight =,
1100 // to width/height =, so the window will take the full area of
1102 mode->width = vidmodes[best_fit]->hdisplay;
1103 mode->height = vidmodes[best_fit]->vdisplay;
1105 // change to the mode
1106 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
1107 memcpy(&game_vidmode, vidmodes[best_fit], sizeof(game_vidmode));
1108 vid_isvidmodefullscreen = true;
1109 vid_isfullscreen = true;
1111 // Move the viewport to top left
1112 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
1113 Con_DPrintf("Using XVidMode fullscreen mode at %dx%d\n", mode->width, mode->height);
1119 if(!vid_isfullscreen)
1121 // sorry, no FS available
1122 // use the full desktop resolution
1123 vid_isfullscreen = true;
1124 // width and height will be filled in later
1125 mode->width = DisplayWidth(vidx11_display, vidx11_screen);
1126 mode->height = DisplayHeight(vidx11_display, vidx11_screen);
1127 Con_DPrintf("Using X11 fullscreen mode at %dx%d\n", mode->width, mode->height);
1131 // LordHavoc: save the visual for use in gamma ramp settings later
1132 vidx11_visual = DefaultVisual(vidx11_display, vidx11_screen);
1134 /* window attributes */
1135 attr.background_pixel = 0;
1136 attr.border_pixel = 0;
1137 // LordHavoc: save the colormap for later, too
1138 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, vidx11_visual, AllocNone);
1139 attr.event_mask = X_MASK;
1141 if (mode->fullscreen)
1143 if(vid_isnetwmfullscreen)
1145 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask;
1146 attr.backing_store = NotUseful;
1147 attr.save_under = False;
1151 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
1152 attr.override_redirect = True;
1153 attr.backing_store = NotUseful;
1154 attr.save_under = False;
1155 vid_isoverrideredirect = true; // so it knows to grab
1160 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1163 win = XCreateWindow(vidx11_display, root, 0, 0, mode->width, mode->height, 0, CopyFromParent, InputOutput, vidx11_visual, mask, &attr);
1165 data = loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
1168 // use _NET_WM_ICON too
1169 static long netwm_icon[MAX_NETWM_ICON];
1175 if(pos + 2 * image_width * image_height < MAX_NETWM_ICON)
1177 netwm_icon[pos++] = image_width;
1178 netwm_icon[pos++] = image_height;
1179 for(i = 0; i < image_height; ++i)
1180 for(j = 0; j < image_width; ++j)
1181 netwm_icon[pos++] = BuffLittleLong(&data[(i*image_width+j)*4]);
1185 Con_Printf("Skipping NETWM icon #%d because there is no space left\n", i);
1189 data = loadimagepixelsbgra(va("darkplaces-icon%d", i), false, false, false, NULL);
1191 XChangeProperty(vidx11_display, win, net_wm_icon, cardinal, 32, PropModeReplace, (const unsigned char *) netwm_icon, pos);
1194 // fallthrough for old window managers
1195 xpm = (char *) FS_LoadFile("darkplaces-icon.xpm", tempmempool, false, NULL);
1198 idata = XPM_DecodeString(xpm);
1200 idata = ENGINE_ICON;
1202 wmhints = XAllocWMHints();
1203 if(XpmCreatePixmapFromData(vidx11_display, win,
1205 &wmhints->icon_pixmap, &wmhints->icon_mask, NULL) == XpmSuccess)
1206 wmhints->flags |= IconPixmapHint | IconMaskHint;
1211 clshints = XAllocClassHint();
1212 clshints->res_name = strdup(gamename);
1213 clshints->res_class = strdup("DarkPlaces");
1215 szhints = XAllocSizeHints();
1216 if(vid_resizable.integer == 0 && !vid_isnetwmfullscreen)
1218 szhints->min_width = szhints->max_width = mode->width;
1219 szhints->min_height = szhints->max_height = mode->height;
1220 szhints->flags |= PMinSize | PMaxSize;
1223 XmbSetWMProperties(vidx11_display, win, gamename, gamename, (char **) com_argv, com_argc, szhints, wmhints, clshints);
1224 // strdup() allocates using malloc(), should be freed with free()
1225 free(clshints->res_name);
1226 free(clshints->res_class);
1231 //XStoreName(vidx11_display, win, gamename);
1232 XMapWindow(vidx11_display, win);
1234 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
1236 if (vid_isoverrideredirect)
1238 XMoveWindow(vidx11_display, win, 0, 0);
1239 XRaiseWindow(vidx11_display, win);
1240 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
1241 XFlush(vidx11_display);
1244 if(vid_isvidmodefullscreen)
1246 // Move the viewport to top left
1247 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
1250 //XSync(vidx11_display, False);
1252 // COMMANDLINEOPTION: Unix GLX: -noshm disables XShm extensioon
1253 if(dpyname && dpyname[0] == ':' && dpyname[1] && (dpyname[2] < '0' || dpyname[2] > '9') && !COM_CheckParm("-noshm") && XShmQueryExtension(vidx11_display))
1255 Con_Printf("Using XShm\n");
1256 vidx11_shmevent = XShmGetEventBase(vidx11_display) + ShmCompletion;
1260 Con_Printf("Not using XShm\n");
1261 vidx11_shmevent = -1;
1263 BuildXImages(mode->width, mode->height);
1265 vidx11_ximage_pos = 0;
1266 vid.softpixels = (unsigned int *) vidx11_ximage[vidx11_ximage_pos]->data;
1268 vid.softdepthpixels = (unsigned int *)calloc(1, mode->width * mode->height * 4);
1270 memset(&gcval, 0, sizeof(gcval));
1271 vidx11_gc = XCreateGC(vidx11_display, win, 0, &gcval);
1273 if (DPSOFTRAST_Init(mode->width, mode->height, vid_soft_threads.integer, vid_soft_interlace.integer, (unsigned int *)vid.softpixels, (unsigned int *)vid.softdepthpixels) < 0)
1275 Con_Printf("Failed to initialize software rasterizer\n");
1280 XSync(vidx11_display, False);
1282 vid_usingmousegrab = false;
1283 vid_usingmouse = false;
1284 vid_usinghidecursor = false;
1285 vid_usingvsync = false;
1287 vid_activewindow = true;
1288 vid_x11_hardwaregammasupported = XF86VidModeGetGammaRampSize(vidx11_display, vidx11_screen, &vid_x11_gammarampsize) != 0;
1289 #if !defined(__APPLE__) && !defined(SUNOS)
1290 vid_x11_dgasupported = XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion);
1291 if (!vid_x11_dgasupported)
1292 Con_Print( "Failed to detect XF86DGA Mouse extension\n" );
1295 VID_Soft_SharedSetup();
1299 qboolean VID_InitModeGL(viddef_mode_t *mode)
1303 XSetWindowAttributes attr;
1304 XClassHint *clshints;
1306 XSizeHints *szhints;
1308 XVisualInfo *visinfo;
1309 int MajorVersion, MinorVersion;
1310 const char *drivername;
1313 unsigned char *data;
1315 vid_isfullscreen = false;
1316 vid_isnetwmfullscreen = false;
1317 vid_isvidmodefullscreen = false;
1318 vid_isoverrideredirect = false;
1320 #if defined(__APPLE__) && defined(__MACH__)
1321 drivername = "/usr/X11R6/lib/libGL.1.dylib";
1323 drivername = "libGL.so.1";
1325 // 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
1326 // 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
1327 // LordHavoc: although this works on MacOSX, it's useless there (as there is only one system libGL)
1328 i = COM_CheckParm("-gl_driver");
1329 if (i && i < com_argc - 1)
1330 drivername = com_argv[i + 1];
1331 if (!GL_OpenLibrary(drivername))
1333 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
1337 if (!(vidx11_display = XOpenDisplay(NULL)))
1339 Con_Print("Couldn't open the X display\n");
1343 // LordHavoc: making the close button on a window do the right thing
1344 // seems to involve this mess, sigh...
1345 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
1346 net_wm_state_atom = XInternAtom(vidx11_display, "_NET_WM_STATE", false);
1347 net_wm_state_fullscreen_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_FULLSCREEN", false);
1348 net_wm_state_hidden_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_HIDDEN", false);
1349 net_wm_icon = XInternAtom(vidx11_display, "_NET_WM_ICON", false);
1350 cardinal = XInternAtom(vidx11_display, "CARDINAL", false);
1352 // make autorepeat send keypress/keypress/.../keyrelease instead of intervening keyrelease
1353 XkbSetDetectableAutoRepeat(vidx11_display, true, NULL);
1355 vidx11_screen = DefaultScreen(vidx11_display);
1356 root = RootWindow(vidx11_display, vidx11_screen);
1358 // Get video mode list
1359 MajorVersion = MinorVersion = 0;
1360 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
1361 vidmode_ext = false;
1364 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
1368 if ((qglXChooseVisual = (XVisualInfo *(GLAPIENTRY *)(Display *dpy, int screen, int *attribList))GL_GetProcAddress("glXChooseVisual")) == NULL
1369 || (qglXCreateContext = (GLXContext (GLAPIENTRY *)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct))GL_GetProcAddress("glXCreateContext")) == NULL
1370 || (qglXDestroyContext = (void (GLAPIENTRY *)(Display *dpy, GLXContext ctx))GL_GetProcAddress("glXDestroyContext")) == NULL
1371 || (qglXMakeCurrent = (Bool (GLAPIENTRY *)(Display *dpy, GLXDrawable drawable, GLXContext ctx))GL_GetProcAddress("glXMakeCurrent")) == NULL
1372 || (qglXSwapBuffers = (void (GLAPIENTRY *)(Display *dpy, GLXDrawable drawable))GL_GetProcAddress("glXSwapBuffers")) == NULL
1373 || (qglXQueryExtensionsString = (const char *(GLAPIENTRY *)(Display *dpy, int screen))GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
1375 Con_Printf("glX functions not found in %s\n", gl_driver);
1379 VID_BuildGLXAttrib(attrib, mode->bitsperpixel == 32, mode->stereobuffer, mode->samples);
1380 visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
1383 Con_Print("Couldn't get an RGB, Double-buffered, Depth visual\n");
1387 if (mode->fullscreen)
1389 if(vid_netwmfullscreen.integer)
1391 // TODO detect WM support
1392 vid_isnetwmfullscreen = true;
1393 vid_isfullscreen = true;
1394 // width and height will be filled in later
1395 Con_DPrintf("Using NetWM fullscreen mode\n");
1398 if(!vid_isfullscreen && vidmode_ext)
1400 int best_fit, best_dist, dist, x, y;
1402 // Are we going fullscreen? If so, let's change video mode
1403 XF86VidModeModeLine *current_vidmode;
1404 XF86VidModeModeInfo **vidmodes;
1407 // This nice hack comes from the SDL source code
1408 current_vidmode = (XF86VidModeModeLine*)((char*)&init_vidmode + sizeof(init_vidmode.dotclock));
1409 XF86VidModeGetModeLine(vidx11_display, vidx11_screen, (int*)&init_vidmode.dotclock, current_vidmode);
1411 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
1415 for (i = 0; i < num_vidmodes; i++)
1417 if (mode->width > vidmodes[i]->hdisplay || mode->height > vidmodes[i]->vdisplay)
1420 x = mode->width - vidmodes[i]->hdisplay;
1421 y = mode->height - vidmodes[i]->vdisplay;
1422 dist = (x * x) + (y * y);
1423 if (best_fit == -1 || dist < best_dist)
1432 // LordHavoc: changed from ActualWidth/ActualHeight =,
1433 // to width/height =, so the window will take the full area of
1435 mode->width = vidmodes[best_fit]->hdisplay;
1436 mode->height = vidmodes[best_fit]->vdisplay;
1438 // change to the mode
1439 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
1440 memcpy(&game_vidmode, vidmodes[best_fit], sizeof(game_vidmode));
1441 vid_isvidmodefullscreen = true;
1442 vid_isfullscreen = true;
1444 // Move the viewport to top left
1445 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
1446 Con_DPrintf("Using XVidMode fullscreen mode at %dx%d\n", mode->width, mode->height);
1452 if(!vid_isfullscreen)
1454 // sorry, no FS available
1455 // use the full desktop resolution
1456 vid_isfullscreen = true;
1457 // width and height will be filled in later
1458 mode->width = DisplayWidth(vidx11_display, vidx11_screen);
1459 mode->height = DisplayHeight(vidx11_display, vidx11_screen);
1460 Con_DPrintf("Using X11 fullscreen mode at %dx%d\n", mode->width, mode->height);
1464 // LordHavoc: save the visual for use in gamma ramp settings later
1465 vidx11_visual = visinfo->visual;
1467 /* window attributes */
1468 attr.background_pixel = 0;
1469 attr.border_pixel = 0;
1470 // LordHavoc: save the colormap for later, too
1471 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
1472 attr.event_mask = X_MASK;
1474 if (mode->fullscreen)
1476 if(vid_isnetwmfullscreen)
1478 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask;
1479 attr.backing_store = NotUseful;
1480 attr.save_under = False;
1484 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
1485 attr.override_redirect = True;
1486 attr.backing_store = NotUseful;
1487 attr.save_under = False;
1488 vid_isoverrideredirect = true; // so it knows to grab
1493 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1496 win = XCreateWindow(vidx11_display, root, 0, 0, mode->width, mode->height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
1498 data = loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
1501 // use _NET_WM_ICON too
1502 static long netwm_icon[MAX_NETWM_ICON];
1508 if(pos + 2 * image_width * image_height < MAX_NETWM_ICON)
1510 netwm_icon[pos++] = image_width;
1511 netwm_icon[pos++] = image_height;
1512 for(i = 0; i < image_height; ++i)
1513 for(j = 0; j < image_width; ++j)
1514 netwm_icon[pos++] = BuffLittleLong(&data[(i*image_width+j)*4]);
1518 Con_Printf("Skipping NETWM icon #%d because there is no space left\n", i);
1522 data = loadimagepixelsbgra(va("darkplaces-icon%d", i), false, false, false, NULL);
1524 XChangeProperty(vidx11_display, win, net_wm_icon, cardinal, 32, PropModeReplace, (const unsigned char *) netwm_icon, pos);
1527 // fallthrough for old window managers
1528 xpm = (char *) FS_LoadFile("darkplaces-icon.xpm", tempmempool, false, NULL);
1531 idata = XPM_DecodeString(xpm);
1533 idata = ENGINE_ICON;
1535 wmhints = XAllocWMHints();
1536 if(XpmCreatePixmapFromData(vidx11_display, win,
1538 &wmhints->icon_pixmap, &wmhints->icon_mask, NULL) == XpmSuccess)
1539 wmhints->flags |= IconPixmapHint | IconMaskHint;
1544 clshints = XAllocClassHint();
1545 clshints->res_name = strdup(gamename);
1546 clshints->res_class = strdup("DarkPlaces");
1548 szhints = XAllocSizeHints();
1549 if(vid_resizable.integer == 0 && !vid_isnetwmfullscreen)
1551 szhints->min_width = szhints->max_width = mode->width;
1552 szhints->min_height = szhints->max_height = mode->height;
1553 szhints->flags |= PMinSize | PMaxSize;
1556 XmbSetWMProperties(vidx11_display, win, gamename, gamename, (char **) com_argv, com_argc, szhints, wmhints, clshints);
1557 // strdup() allocates using malloc(), should be freed with free()
1558 free(clshints->res_name);
1559 free(clshints->res_class);
1564 //XStoreName(vidx11_display, win, gamename);
1565 XMapWindow(vidx11_display, win);
1567 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
1569 if (vid_isoverrideredirect)
1571 XMoveWindow(vidx11_display, win, 0, 0);
1572 XRaiseWindow(vidx11_display, win);
1573 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
1574 XFlush(vidx11_display);
1577 if(vid_isvidmodefullscreen)
1579 // Move the viewport to top left
1580 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
1583 //XSync(vidx11_display, False);
1585 ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
1586 XFree(visinfo); // glXChooseVisual man page says to use XFree to free visinfo
1589 Con_Printf ("glXCreateContext failed\n");
1593 if (!qglXMakeCurrent(vidx11_display, win, ctx))
1595 Con_Printf ("glXMakeCurrent failed\n");
1599 XSync(vidx11_display, False);
1601 if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
1603 Con_Printf ("glGetString not found in %s\n", gl_driver);
1607 gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
1608 gl_platform = "GLX";
1609 gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
1611 // COMMANDLINEOPTION: Linux GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1612 // COMMANDLINEOPTION: BSD GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1613 // COMMANDLINEOPTION: MacOSX GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1614 GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
1615 // COMMANDLINEOPTION: Linux GLX: -novideosync disables GLX_SGI_swap_control
1616 // COMMANDLINEOPTION: BSD GLX: -novideosync disables GLX_SGI_swap_control
1617 // COMMANDLINEOPTION: MacOSX GLX: -novideosync disables GLX_SGI_swap_control
1618 GL_CheckExtension("GLX_SGI_swap_control", swapcontrolfuncs, "-novideosync", false);
1620 vid_usingmousegrab = false;
1621 vid_usingmouse = false;
1622 vid_usinghidecursor = false;
1623 vid_usingvsync = false;
1625 vid_activewindow = true;
1626 vid_x11_hardwaregammasupported = XF86VidModeGetGammaRampSize(vidx11_display, vidx11_screen, &vid_x11_gammarampsize) != 0;
1627 #if !defined(__APPLE__) && !defined(SUNOS)
1628 vid_x11_dgasupported = XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion);
1629 if (!vid_x11_dgasupported)
1630 Con_Print( "Failed to detect XF86DGA Mouse extension\n" );
1637 qboolean VID_InitMode(viddef_mode_t *mode)
1640 if (vid_soft.integer)
1641 return VID_InitModeSoft(mode);
1644 return VID_InitModeGL(mode);
1647 void Sys_SendKeyEvents(void)
1649 static qboolean sound_active = true;
1651 // enable/disable sound on focus gain/loss
1652 if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
1657 sound_active = true;
1665 sound_active = false;
1676 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
1682 XF86VidModeModeInfo **vidmodes;
1685 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
1687 for (i = 0; i < num_vidmodes; i++)
1691 // we don't get bpp info, so let's just assume all of 8, 15, 16, 24, 32 work
1692 for(bpp = 8; bpp <= 32; bpp = ((bpp == 8) ? 15 : (bpp & 0xF8) + 8))
1696 modes[k].width = vidmodes[i]->hdisplay;
1697 modes[k].height = vidmodes[i]->vdisplay;
1699 if(vidmodes[i]->dotclock && vidmodes[i]->htotal && vidmodes[i]->vtotal)
1700 modes[k].refreshrate = vidmodes[i]->dotclock / vidmodes[i]->htotal / vidmodes[i]->vtotal;
1702 modes[k].refreshrate = 60;
1703 modes[k].pixelheight_num = 1;
1704 modes[k].pixelheight_denom = 1; // xvidmode does not provide this
1708 // manpage of XF86VidModeGetAllModeLines says it should be freed by the caller
1712 return 0; // FIXME implement this