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.
20 #if !defined(__APPLE__) && !defined(__MACH__) && !defined(SUNOS)
29 #include <X11/Xutil.h>
30 #include <X11/Xatom.h>
31 #include <X11/XKBlib.h> // TODO possibly ifdef this out on non-supporting systems... Solaris (as always)?
35 #include "dpsoftrast.h"
37 #include <X11/keysym.h>
38 #include <X11/cursorfont.h>
41 #include <X11/extensions/XShm.h>
43 #include <X11/extensions/xf86dga.h>
45 #include <X11/extensions/xf86vmode.h>
49 #include <X11/extensions/XShm.h>
56 #include "darkplaces.xpm"
58 // Tell startup code that we have a client
59 int cl_available = true;
61 // note: if we used the XRandR extension we could support refresh rates
62 qboolean vid_supportrefreshrate = false;
65 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
66 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
67 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
68 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
69 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
70 const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
72 //GLX_ARB_get_proc_address
73 void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
75 static dllfunction_t getprocaddressfuncs[] =
77 {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
81 //GLX_SGI_swap_control
82 GLint (GLAPIENTRY *qglXSwapIntervalSGI)(GLint interval);
84 static dllfunction_t swapcontrolfuncs[] =
86 {"glXSwapIntervalSGI", (void **) &qglXSwapIntervalSGI},
90 static Display *vidx11_display = NULL;
91 static int vidx11_screen;
92 static Window win, root;
93 static GLXContext ctx = NULL;
94 static GC vidx11_gc = NULL;
95 static XImage *vidx11_ximage[2] = { NULL, NULL };
96 static int vidx11_ximage_pos = 0;
97 static XShmSegmentInfo vidx11_shminfo[2];
98 static int vidx11_shmevent = -1;
99 static int vidx11_shmwait = 0; // number of frames outstanding
101 Atom wm_delete_window_atom;
102 Atom net_wm_state_atom;
103 Atom net_wm_state_hidden_atom;
104 Atom net_wm_state_fullscreen_atom;
108 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
109 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
110 PointerMotionMask | ButtonMotionMask)
111 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | \
112 StructureNotifyMask | FocusChangeMask | EnterWindowMask | \
116 static qboolean mouse_avail = true;
117 static qboolean vid_usingmousegrab = false;
118 static qboolean vid_usingmouse = false;
119 static qboolean vid_usinghidecursor = false;
120 static qboolean vid_usingvsync = false;
121 static qboolean vid_usevsync = false;
123 static qboolean vid_x11_dgasupported = false;
127 cvar_t vid_dgamouse = {CVAR_SAVE, "vid_dgamouse", "0", "make use of DGA mouse input"};
128 static qboolean vid_usingdgamouse = false;
131 qboolean vidmode_ext = false;
133 static int win_x, win_y;
135 static XF86VidModeModeInfo init_vidmode, game_vidmode;
136 static qboolean vid_isfullscreen = false;
137 static qboolean vid_isvidmodefullscreen = false;
138 static qboolean vid_isdesktopfullscreen = false;
139 static qboolean vid_isoverrideredirect = false;
141 static vid_mode_t desktop_mode;
142 static Visual *vidx11_visual;
143 static Colormap vidx11_colormap;
145 /*-----------------------------------------------------------------------*/
148 extern long keysym2ucs(KeySym keysym); // LordHavoc: suppress warning just in this case, it's not worth having a header file for this...
149 static void DP_Xutf8LookupString(XKeyEvent * ev,
151 KeySym * keysym_return,
152 Status * status_return)
158 int nbytes = sizeof(buffer);
160 rc = XLookupString(ev, buffer, nbytes, &keysym, NULL);
163 codepoint = buffer[0] & 0xFF;
165 codepoint = keysym2ucs(keysym);
169 if (keysym == None) {
170 *status_return = XLookupNone;
172 *status_return = XLookupKeySym;
173 *keysym_return = keysym;
181 if (keysym != None) {
182 *keysym_return = keysym;
183 *status_return = XLookupBoth;
185 *status_return = XLookupChars;
188 static int XLateKey(XKeyEvent *ev, Uchar *ascii)
192 KeySym keysym, shifted;
195 keysym = XLookupKeysym (ev, 0);
196 DP_Xutf8LookupString(ev, ascii, &shifted, &status);
200 case XK_KP_Page_Up: key = K_KP_PGUP; break;
201 case XK_Page_Up: key = K_PGUP; break;
203 case XK_KP_Page_Down: key = K_KP_PGDN; break;
204 case XK_Page_Down: key = K_PGDN; break;
206 case XK_KP_Home: key = K_KP_HOME; break;
207 case XK_Home: key = K_HOME; break;
209 case XK_KP_End: key = K_KP_END; break;
210 case XK_End: key = K_END; break;
212 case XK_KP_Left: key = K_KP_LEFTARROW; break;
213 case XK_Left: key = K_LEFTARROW; break;
215 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
216 case XK_Right: key = K_RIGHTARROW; break;
218 case XK_KP_Down: key = K_KP_DOWNARROW; break;
219 case XK_Down: key = K_DOWNARROW; break;
221 case XK_KP_Up: key = K_KP_UPARROW; break;
222 case XK_Up: key = K_UPARROW; break;
224 case XK_Escape: key = K_ESCAPE; break;
226 case XK_KP_Enter: key = K_KP_ENTER; break;
227 case XK_Return: key = K_ENTER; break;
229 case XK_Tab: key = K_TAB; break;
231 case XK_F1: key = K_F1; break;
233 case XK_F2: key = K_F2; break;
235 case XK_F3: key = K_F3; break;
237 case XK_F4: key = K_F4; break;
239 case XK_F5: key = K_F5; break;
241 case XK_F6: key = K_F6; break;
243 case XK_F7: key = K_F7; break;
245 case XK_F8: key = K_F8; break;
247 case XK_F9: key = K_F9; break;
249 case XK_F10: key = K_F10; break;
251 case XK_F11: key = K_F11; break;
253 case XK_F12: key = K_F12; break;
255 case XK_BackSpace: key = K_BACKSPACE; break;
257 case XK_KP_Delete: key = K_KP_DEL; break;
258 case XK_Delete: key = K_DEL; break;
260 case XK_Pause: key = K_PAUSE; break;
263 case XK_Shift_R: key = K_SHIFT; break;
267 case XK_Control_R: key = K_CTRL; break;
271 case XK_ISO_Level3_Shift:
273 case XK_Meta_R: key = K_ALT; break;
275 case XK_KP_Begin: key = K_KP_5; break;
277 case XK_Insert:key = K_INS; break;
278 case XK_KP_Insert: key = K_KP_INS; break;
280 case XK_KP_Multiply: key = K_KP_MULTIPLY; break;
281 case XK_KP_Add: key = K_KP_PLUS; break;
282 case XK_KP_Subtract: key = K_KP_MINUS; break;
283 case XK_KP_Divide: key = K_KP_SLASH; break;
285 case XK_Num_Lock: key = K_NUMLOCK; break;
286 case XK_Caps_Lock: key = K_CAPSLOCK; break;
287 case XK_Scroll_Lock: key = K_SCROLLOCK; break;
289 case XK_asciicircum: *ascii = key = '^'; break; // for some reason, XLookupString returns "" on this one for Grunt|2
291 case XK_section: *ascii = key = '~'; break;
297 if (keysym >= 'A' && keysym <= 'Z')
298 key = keysym - 'A' + 'a';
308 static Cursor CreateNullCursor(Display *display, Window root)
316 cursormask = XCreatePixmap(display, root, 1, 1, 1);
317 xgc.function = GXclear;
318 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
319 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
320 dummycolour.pixel = 0;
322 dummycolour.flags = 04;
323 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
324 XFreePixmap(display,cursormask);
329 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
331 static int originalmouseparms_num;
332 static int originalmouseparms_denom;
333 static int originalmouseparms_threshold;
334 static qboolean restore_spi;
337 qboolean usedgamouse;
340 if (!vidx11_display || !win)
344 fullscreengrab = true;
347 fullscreengrab = relative = hidecursor = false;
350 usedgamouse = relative && vid_dgamouse.integer;
351 if (!vid_x11_dgasupported)
353 if (fullscreengrab && vid_usingmouse && (vid_usingdgamouse != usedgamouse))
354 VID_SetMouse(false, false, false); // ungrab first!
357 if (vid_usingmousegrab != fullscreengrab)
359 vid_usingmousegrab = fullscreengrab;
360 cl_ignoremousemoves = 2;
363 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
364 if (vid_grabkeyboard.integer || vid_isoverrideredirect)
365 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
369 XUngrabPointer(vidx11_display, CurrentTime);
370 XUngrabKeyboard(vidx11_display, CurrentTime);
378 XWindowAttributes attribs_1;
379 XSetWindowAttributes attribs_2;
381 XGetWindowAttributes(vidx11_display, win, &attribs_1);
382 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
383 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
386 vid_usingdgamouse = usedgamouse;
389 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
390 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
394 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
396 // COMMANDLINEOPTION: X11 Input: -noforcemparms disables setting of mouse parameters (not used with DGA, windows only)
398 if (!COM_CheckParm ("-noforcemparms") && !usedgamouse)
400 if (!COM_CheckParm ("-noforcemparms"))
403 XGetPointerControl(vidx11_display, &originalmouseparms_num, &originalmouseparms_denom, &originalmouseparms_threshold);
404 XChangePointerControl (vidx11_display, true, false, 1, 1, -1); // TODO maybe change threshold here, or remove this comment
410 cl_ignoremousemoves = 2;
411 vid_usingmouse = true;
419 if (vid_usingdgamouse)
420 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
421 vid_usingdgamouse = false;
423 cl_ignoremousemoves = 2;
426 XChangePointerControl (vidx11_display, true, true, originalmouseparms_num, originalmouseparms_denom, originalmouseparms_threshold);
429 vid_usingmouse = false;
433 if (vid_usinghidecursor != hidecursor)
435 vid_usinghidecursor = hidecursor;
437 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
439 XUndefineCursor(vidx11_display, win);
443 static keynum_t buttonremap[18] =
465 static qboolean BuildXImages(int w, int h)
468 if(DefaultDepth(vidx11_display, vidx11_screen) != 32 && DefaultDepth(vidx11_display, vidx11_screen) != 24)
470 Con_Printf("Sorry, we only support 24bpp and 32bpp modes\n");
474 // match to dpsoftrast's specs
475 if(vidx11_visual->red_mask != 0x00FF0000)
477 Con_Printf("Sorry, we only support BGR visuals\n");
481 if(vidx11_visual->green_mask != 0x0000FF00)
483 Con_Printf("Sorry, we only support BGR visuals\n");
487 if(vidx11_visual->blue_mask != 0x000000FF)
489 Con_Printf("Sorry, we only support BGR visuals\n");
493 if(vidx11_shmevent >= 0)
495 for(i = 0; i < 2; ++i)
497 vidx11_shminfo[i].shmid = -1;
498 vidx11_ximage[i] = XShmCreateImage(vidx11_display, vidx11_visual, DefaultDepth(vidx11_display, vidx11_screen), ZPixmap, NULL, &vidx11_shminfo[i], w, h);
499 if(!vidx11_ximage[i])
501 Con_Printf("Failed to get an XImage segment\n");
505 if(vidx11_ximage[i]->bytes_per_line != w * 4)
507 Con_Printf("Sorry, we only support linear pixel layout\n");
511 vidx11_shminfo[i].shmid = shmget(IPC_PRIVATE, vidx11_ximage[i]->bytes_per_line * vidx11_ximage[i]->height, IPC_CREAT|0777);
512 if(vidx11_shminfo[i].shmid < 0)
514 Con_Printf("Failed to get a shm segment\n");
518 vidx11_shminfo[i].shmaddr = vidx11_ximage[i]->data = shmat(vidx11_shminfo[i].shmid, NULL, 0);
519 if(!vidx11_shminfo[i].shmaddr)
521 Con_Printf("Failed to get a shm segment addresst\n");
525 vidx11_shminfo[i].readOnly = True;
526 XShmAttach(vidx11_display, &vidx11_shminfo[i]);
531 for(i = 0; i < 1; ++i) // we only need one buffer if we don't use Xshm
533 char *p = calloc(4, w * h);
534 vidx11_shminfo[i].shmid = -1;
535 vidx11_ximage[i] = XCreateImage(vidx11_display, vidx11_visual, DefaultDepth(vidx11_display, vidx11_screen), ZPixmap, 0, (char*)p, w, h, 8, 0);
536 if(!vidx11_ximage[i])
538 Con_Printf("Failed to get an XImage segment\n");
542 if(vidx11_ximage[i]->bytes_per_line != w * 4)
544 Con_Printf("Sorry, we only support linear pixel layout\n");
552 static void DestroyXImages(void)
555 for(i = 0; i < 2; ++i)
557 if(vidx11_shminfo[i].shmid >= 0)
559 XShmDetach(vidx11_display, &vidx11_shminfo[i]);
560 XDestroyImage(vidx11_ximage[i]);
561 vidx11_ximage[i] = NULL;
562 shmdt(vidx11_shminfo[i].shmaddr);
563 shmctl(vidx11_shminfo[i].shmid, IPC_RMID, 0);
564 vidx11_shminfo[i].shmid = -1;
567 XDestroyImage(vidx11_ximage[i]);
568 vidx11_ximage[i] = 0;
572 static int in_mouse_x_save = 0, in_mouse_y_save = 0;
573 static void HandleEvents(void)
578 qboolean dowarp = false;
583 in_mouse_x += in_mouse_x_save;
584 in_mouse_y += in_mouse_y_save;
588 while (XPending(vidx11_display))
590 XNextEvent(vidx11_display, &event);
596 key = XLateKey (&event.xkey, &unicode);
597 Key_Event(key, unicode, true);
602 key = XLateKey (&event.xkey, &unicode);
603 Key_Event(key, unicode, false);
611 if (vid_usingdgamouse)
613 in_mouse_x += event.xmotion.x_root;
614 in_mouse_y += event.xmotion.y_root;
619 if (!event.xmotion.send_event)
621 in_mouse_x += event.xmotion.x - in_windowmouse_x;
622 in_mouse_y += event.xmotion.y - in_windowmouse_y;
623 //if (abs(vid.width/2 - event.xmotion.x) + abs(vid.height/2 - event.xmotion.y))
624 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)
629 in_windowmouse_x = event.xmotion.x;
630 in_windowmouse_y = event.xmotion.y;
634 // mouse button pressed
635 if (event.xbutton.button <= 18)
636 Key_Event(buttonremap[event.xbutton.button - 1], 0, true);
638 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-18 expected\n", event.xbutton.button);
642 // mouse button released
643 if (event.xbutton.button <= 18)
644 Key_Event(buttonremap[event.xbutton.button - 1], 0, false);
646 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-18 expected\n", event.xbutton.button);
651 win_x = event.xcreatewindow.x;
652 win_y = event.xcreatewindow.y;
655 case ConfigureNotify:
656 // window changed size/location
657 win_x = event.xconfigure.x;
658 win_y = event.xconfigure.y;
659 // HACK on X11, we just request fullscreen mode, but
660 // cannot guess what the window manager will do for us
661 // exactly. That is why we read back the resolution we
662 // actually got here.
663 if(vid_isdesktopfullscreen)
665 desktop_mode.width = event.xconfigure.width;
666 desktop_mode.height = event.xconfigure.height;
668 if((vid_resizable.integer < 2 || vid_isdesktopfullscreen) && (vid.width != event.xconfigure.width || vid.height != event.xconfigure.height))
670 vid.width = event.xconfigure.width;
671 vid.height = event.xconfigure.height;
672 if(vid_isdesktopfullscreen)
673 Con_Printf("NetWM fullscreen: actually using resolution %dx%d\n", vid.width, vid.height);
675 Con_DPrintf("Updating to ConfigureNotify resolution %dx%d\n", vid.width, vid.height);
677 if(vid.renderpath == RENDERPATH_SOFT)
680 if(vid.softdepthpixels)
681 free(vid.softdepthpixels);
683 XSync(vidx11_display, False);
684 if(!BuildXImages(vid.width, vid.height))
686 XSync(vidx11_display, False);
687 vid.softpixels = (unsigned int *) vidx11_ximage[vidx11_ximage_pos]->data;
688 vid.softdepthpixels = (unsigned int *)calloc(4, vid.width * vid.height);
693 // window has been destroyed
697 // window manager messages
698 if ((event.xclient.format == 32) && ((unsigned int)event.xclient.data.l[0] == wm_delete_window_atom))
702 if (vid_isoverrideredirect)
707 if(vid_isvidmodefullscreen)
709 // set our video mode
710 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &game_vidmode);
712 // Move the viewport to top left
713 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
716 if(vid_isdesktopfullscreen)
718 // make sure it's fullscreen
720 event.type = ClientMessage;
721 event.xclient.serial = 0;
722 event.xclient.send_event = True;
723 event.xclient.message_type = net_wm_state_atom;
724 event.xclient.window = win;
725 event.xclient.format = 32;
726 event.xclient.data.l[0] = 1;
727 event.xclient.data.l[1] = net_wm_state_fullscreen_atom;
728 event.xclient.data.l[2] = 0;
729 event.xclient.data.l[3] = 1;
730 event.xclient.data.l[4] = 0;
731 XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
738 if (vid_isoverrideredirect)
740 // window iconified/rolledup/whatever
743 if(vid_isvidmodefullscreen)
744 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
748 if (vid_isoverrideredirect)
750 // window is now the input focus
751 vid_activewindow = true;
754 if (vid_isoverrideredirect)
757 if(vid_isdesktopfullscreen && event.xfocus.mode == NotifyNormal)
759 // iconify netwm fullscreen window when it loses focus
760 // when the user selects it in the taskbar, the window manager will map it again and send MapNotify
762 event.type = ClientMessage;
763 event.xclient.serial = 0;
764 event.xclient.send_event = True;
765 event.xclient.message_type = net_wm_state_atom;
766 event.xclient.window = win;
767 event.xclient.format = 32;
768 event.xclient.data.l[0] = 1;
769 event.xclient.data.l[1] = net_wm_state_hidden_atom;
770 event.xclient.data.l[2] = 0;
771 event.xclient.data.l[3] = 1;
772 event.xclient.data.l[4] = 0;
773 XSendEvent(vidx11_display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &event);
776 // window is no longer the input focus
777 vid_activewindow = false;
781 // mouse entered window
787 if(vidx11_shmevent >= 0 && event.type == vidx11_shmevent)
795 /* move the mouse to the window center again */
796 // we'll catch the warp motion by its send_event flag, updating the
797 // stored mouse position without adding any delta motion
799 event.type = MotionNotify;
800 event.xmotion.display = vidx11_display;
801 event.xmotion.window = win;
802 event.xmotion.x = vid.width / 2;
803 event.xmotion.y = vid.height / 2;
804 XSendEvent(vidx11_display, win, False, PointerMotionMask, &event);
805 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
809 static void *prjobj = NULL;
811 static void GL_CloseLibrary(void)
817 qglXGetProcAddressARB = NULL;
820 gl_platformextensions = "";
823 static int GL_OpenLibrary(const char *name)
825 Con_Printf("Loading OpenGL driver %s\n", name);
827 if (!(prjobj = dlopen(name, RTLD_LAZY | RTLD_GLOBAL)))
829 Con_Printf("Unable to open symbol list for %s\n", name);
832 strlcpy(gl_driver, name, sizeof(gl_driver));
836 void *GL_GetProcAddress(const char *name)
839 if (qglXGetProcAddressARB != NULL)
840 p = (void *) qglXGetProcAddressARB((GLubyte *)name);
842 p = (void *) dlsym(prjobj, name);
846 void VID_Shutdown(void)
851 VID_EnableJoystick(false);
852 VID_SetMouse(false, false, false);
854 // FIXME: glXDestroyContext here?
855 if (vid_isvidmodefullscreen)
856 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, &init_vidmode);
859 XFreeGC(vidx11_display, vidx11_gc);
863 vidx11_shmevent = -1;
864 vid.softpixels = NULL;
866 if (vid.softdepthpixels)
867 free(vid.softdepthpixels);
868 vid.softdepthpixels = NULL;
871 XDestroyWindow(vidx11_display, win);
872 XCloseDisplay(vidx11_display);
875 vid_isfullscreen = false;
876 vid_isdesktopfullscreen = false;
877 vid_isvidmodefullscreen = false;
878 vid_isoverrideredirect = false;
879 vidx11_display = NULL;
887 static void signal_handler(int sig)
889 Con_Printf("Received signal %d, exiting...\n", sig);
893 static void InitSig(void)
895 signal(SIGHUP, signal_handler);
896 signal(SIGINT, signal_handler);
897 signal(SIGQUIT, signal_handler);
898 signal(SIGILL, signal_handler);
899 signal(SIGTRAP, signal_handler);
900 signal(SIGIOT, signal_handler);
901 signal(SIGBUS, signal_handler);
902 signal(SIGFPE, signal_handler);
903 signal(SIGSEGV, signal_handler);
904 signal(SIGTERM, signal_handler);
907 void VID_Finish (void)
909 vid_usevsync = vid_vsync.integer && !cls.timedemo && qglXSwapIntervalSGI;
910 switch(vid.renderpath)
912 case RENDERPATH_SOFT:
913 if(vidx11_shmevent >= 0) {
914 vidx11_ximage_pos = !vidx11_ximage_pos;
915 vid.softpixels = (unsigned int *) vidx11_ximage[vidx11_ximage_pos]->data;
916 DPSOFTRAST_SetRenderTargets(vid.width, vid.height, vid.softdepthpixels, vid.softpixels, NULL, NULL, NULL);
919 XShmPutImage(vidx11_display, win, vidx11_gc, vidx11_ximage[!vidx11_ximage_pos], 0, 0, 0, 0, vid.width, vid.height, True);
921 // save mouse motion so we can deal with it later
924 while(vidx11_shmwait > 1)
926 in_mouse_x_save += in_mouse_x;
927 in_mouse_y_save += in_mouse_y;
931 // no buffer switching here, we just flush the renderer
933 XPutImage(vidx11_display, win, vidx11_gc, vidx11_ximage[vidx11_ximage_pos], 0, 0, 0, 0, vid.width, vid.height);
937 case RENDERPATH_GL11:
938 case RENDERPATH_GL13:
939 case RENDERPATH_GL20:
940 case RENDERPATH_GLES1:
941 case RENDERPATH_GLES2:
942 if (vid_usingvsync != vid_usevsync)
944 vid_usingvsync = vid_usevsync;
945 if (qglXSwapIntervalSGI && qglXSwapIntervalSGI (vid_usevsync))
946 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");
952 if (r_speeds.integer == 2 || gl_finish.integer)
954 qglXSwapBuffers(vidx11_display, win);CHECKGLERROR
958 case RENDERPATH_D3D9:
959 case RENDERPATH_D3D10:
960 case RENDERPATH_D3D11:
970 Cvar_RegisterVariable (&vid_dgamouse);
972 Cvar_RegisterVariable (&vid_desktopfullscreen);
973 InitSig(); // trap evil signals
974 // COMMANDLINEOPTION: Input: -nomouse disables mouse support (see also vid_mouse cvar)
975 if (COM_CheckParm ("-nomouse"))
977 vidx11_shminfo[0].shmid = -1;
978 vidx11_shminfo[1].shmid = -1;
981 static void VID_BuildGLXAttrib(int *attrib, qboolean stencil, qboolean stereobuffer, int samples)
983 *attrib++ = GLX_RGBA;
984 *attrib++ = GLX_RED_SIZE;*attrib++ = stencil ? 8 : 5;
985 *attrib++ = GLX_GREEN_SIZE;*attrib++ = stencil ? 8 : 5;
986 *attrib++ = GLX_BLUE_SIZE;*attrib++ = stencil ? 8 : 5;
987 *attrib++ = GLX_DOUBLEBUFFER;
988 *attrib++ = GLX_DEPTH_SIZE;*attrib++ = stencil ? 24 : 16;
989 // if stencil is enabled, ask for alpha too
992 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
993 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 8;
996 *attrib++ = GLX_STEREO;
999 *attrib++ = GLX_SAMPLE_BUFFERS_ARB;
1001 *attrib++ = GLX_SAMPLES_ARB;
1002 *attrib++ = samples;
1007 static qboolean VID_InitModeSoft(viddef_mode_t *mode)
1010 XSetWindowAttributes attr;
1011 XClassHint *clshints;
1013 XSizeHints *szhints;
1015 int MajorVersion, MinorVersion;
1018 unsigned char *data;
1020 const char *dpyname;
1023 vid_isfullscreen = false;
1024 vid_isdesktopfullscreen = false;
1025 vid_isvidmodefullscreen = false;
1026 vid_isoverrideredirect = false;
1028 if (!(vidx11_display = XOpenDisplay(NULL)))
1030 Con_Print("Couldn't open the X display\n");
1033 dpyname = XDisplayName(NULL);
1035 // LordHavoc: making the close button on a window do the right thing
1036 // seems to involve this mess, sigh...
1037 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
1038 net_wm_state_atom = XInternAtom(vidx11_display, "_NET_WM_STATE", false);
1039 net_wm_state_fullscreen_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_FULLSCREEN", false);
1040 net_wm_state_hidden_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_HIDDEN", false);
1041 net_wm_icon = XInternAtom(vidx11_display, "_NET_WM_ICON", false);
1042 cardinal = XInternAtom(vidx11_display, "CARDINAL", false);
1044 // make autorepeat send keypress/keypress/.../keyrelease instead of intervening keyrelease
1045 XkbSetDetectableAutoRepeat(vidx11_display, true, NULL);
1047 vidx11_screen = DefaultScreen(vidx11_display);
1048 root = RootWindow(vidx11_display, vidx11_screen);
1050 desktop_mode.width = DisplayWidth(vidx11_display, vidx11_screen);
1051 desktop_mode.height = DisplayHeight(vidx11_display, vidx11_screen);
1052 desktop_mode.bpp = DefaultDepth(vidx11_display, vidx11_screen);
1053 desktop_mode.refreshrate = 60; // FIXME
1054 desktop_mode.pixelheight_num = 1; // FIXME
1055 desktop_mode.pixelheight_denom = 1; // FIXME
1057 // Get video mode list
1058 MajorVersion = MinorVersion = 0;
1059 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
1060 vidmode_ext = false;
1063 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
1067 if (mode->fullscreen)
1069 if(vid_desktopfullscreen.integer)
1071 // TODO detect WM support
1072 vid_isdesktopfullscreen = true;
1073 vid_isfullscreen = true;
1074 // width and height will be filled in later
1075 Con_DPrintf("Using NetWM fullscreen mode\n");
1078 if(!vid_isfullscreen && vidmode_ext)
1080 int best_fit, best_dist, dist, x, y;
1082 // Are we going fullscreen? If so, let's change video mode
1083 XF86VidModeModeLine *current_vidmode;
1084 XF86VidModeModeInfo **vidmodes;
1087 // This nice hack comes from the SDL source code
1088 current_vidmode = (XF86VidModeModeLine*)((char*)&init_vidmode + sizeof(init_vidmode.dotclock));
1089 XF86VidModeGetModeLine(vidx11_display, vidx11_screen, (int*)&init_vidmode.dotclock, current_vidmode);
1091 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
1095 for (i = 0; i < num_vidmodes; i++)
1097 if (mode->width > vidmodes[i]->hdisplay || mode->height > vidmodes[i]->vdisplay)
1100 x = mode->width - vidmodes[i]->hdisplay;
1101 y = mode->height - vidmodes[i]->vdisplay;
1102 dist = (x * x) + (y * y);
1103 if (best_fit == -1 || dist < best_dist)
1112 // LordHavoc: changed from ActualWidth/ActualHeight =,
1113 // to width/height =, so the window will take the full area of
1115 mode->width = vidmodes[best_fit]->hdisplay;
1116 mode->height = vidmodes[best_fit]->vdisplay;
1118 // change to the mode
1119 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
1120 memcpy(&game_vidmode, vidmodes[best_fit], sizeof(game_vidmode));
1121 vid_isvidmodefullscreen = true;
1122 vid_isfullscreen = true;
1124 // Move the viewport to top left
1125 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
1126 Con_DPrintf("Using XVidMode fullscreen mode at %dx%d\n", mode->width, mode->height);
1132 if(!vid_isfullscreen)
1134 // sorry, no FS available
1135 // use the full desktop resolution
1136 vid_isfullscreen = true;
1137 // width and height will be filled in later
1138 mode->width = DisplayWidth(vidx11_display, vidx11_screen);
1139 mode->height = DisplayHeight(vidx11_display, vidx11_screen);
1140 Con_DPrintf("Using X11 fullscreen mode at %dx%d\n", mode->width, mode->height);
1144 // LordHavoc: save the visual for use in gamma ramp settings later
1145 vidx11_visual = DefaultVisual(vidx11_display, vidx11_screen);
1147 /* window attributes */
1148 attr.background_pixel = 0;
1149 attr.border_pixel = 0;
1150 // LordHavoc: save the colormap for later, too
1151 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, vidx11_visual, AllocNone);
1152 attr.event_mask = X_MASK;
1154 if (mode->fullscreen)
1156 if(vid_isdesktopfullscreen)
1158 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask;
1159 attr.backing_store = NotUseful;
1160 attr.save_under = False;
1164 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
1165 attr.override_redirect = True;
1166 attr.backing_store = NotUseful;
1167 attr.save_under = False;
1168 vid_isoverrideredirect = true; // so it knows to grab
1173 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1176 win = XCreateWindow(vidx11_display, root, 0, 0, mode->width, mode->height, 0, CopyFromParent, InputOutput, vidx11_visual, mask, &attr);
1178 data = loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
1181 // use _NET_WM_ICON too
1182 static long netwm_icon[MAX_NETWM_ICON];
1188 if(pos + 2 * image_width * image_height < MAX_NETWM_ICON)
1190 netwm_icon[pos++] = image_width;
1191 netwm_icon[pos++] = image_height;
1192 for(i = 0; i < image_height; ++i)
1193 for(j = 0; j < image_width; ++j)
1194 netwm_icon[pos++] = BuffLittleLong(&data[(i*image_width+j)*4]);
1198 Con_Printf("Skipping NETWM icon #%d because there is no space left\n", i);
1202 data = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "darkplaces-icon%d", i), false, false, false, NULL);
1204 XChangeProperty(vidx11_display, win, net_wm_icon, cardinal, 32, PropModeReplace, (const unsigned char *) netwm_icon, pos);
1207 // fallthrough for old window managers
1208 xpm = (char *) FS_LoadFile("darkplaces-icon.xpm", tempmempool, false, NULL);
1211 idata = XPM_DecodeString(xpm);
1213 idata = ENGINE_ICON;
1215 wmhints = XAllocWMHints();
1216 if(XpmCreatePixmapFromData(vidx11_display, win,
1218 &wmhints->icon_pixmap, &wmhints->icon_mask, NULL) == XpmSuccess)
1219 wmhints->flags |= IconPixmapHint | IconMaskHint;
1224 clshints = XAllocClassHint();
1225 clshints->res_name = strdup(gamename);
1226 clshints->res_class = strdup("DarkPlaces");
1228 szhints = XAllocSizeHints();
1229 if(vid_resizable.integer == 0 && !vid_isdesktopfullscreen)
1231 szhints->min_width = szhints->max_width = mode->width;
1232 szhints->min_height = szhints->max_height = mode->height;
1233 szhints->flags |= PMinSize | PMaxSize;
1236 XmbSetWMProperties(vidx11_display, win, gamename, gamename, (char **) com_argv, com_argc, szhints, wmhints, clshints);
1237 // strdup() allocates using malloc(), should be freed with free()
1238 free(clshints->res_name);
1239 free(clshints->res_class);
1244 //XStoreName(vidx11_display, win, gamename);
1245 XMapWindow(vidx11_display, win);
1247 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
1249 if (vid_isoverrideredirect)
1251 XMoveWindow(vidx11_display, win, 0, 0);
1252 XRaiseWindow(vidx11_display, win);
1253 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
1254 XFlush(vidx11_display);
1257 if(vid_isvidmodefullscreen)
1259 // Move the viewport to top left
1260 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
1263 //XSync(vidx11_display, False);
1265 // COMMANDLINEOPTION: Unix GLX: -noshm disables XShm extensioon
1266 if(dpyname && dpyname[0] == ':' && dpyname[1] && (dpyname[2] < '0' || dpyname[2] > '9') && !COM_CheckParm("-noshm") && XShmQueryExtension(vidx11_display))
1268 Con_Printf("Using XShm\n");
1269 vidx11_shmevent = XShmGetEventBase(vidx11_display) + ShmCompletion;
1273 Con_Printf("Not using XShm\n");
1274 vidx11_shmevent = -1;
1276 BuildXImages(mode->width, mode->height);
1278 vidx11_ximage_pos = 0;
1279 vid.softpixels = (unsigned int *) vidx11_ximage[vidx11_ximage_pos]->data;
1281 vid.softdepthpixels = (unsigned int *)calloc(1, mode->width * mode->height * 4);
1283 memset(&gcval, 0, sizeof(gcval));
1284 vidx11_gc = XCreateGC(vidx11_display, win, 0, &gcval);
1286 if (DPSOFTRAST_Init(mode->width, mode->height, vid_soft_threads.integer, vid_soft_interlace.integer, (unsigned int *)vid.softpixels, (unsigned int *)vid.softdepthpixels) < 0)
1288 Con_Printf("Failed to initialize software rasterizer\n");
1293 XSync(vidx11_display, False);
1295 vid_usingmousegrab = false;
1296 vid_usingmouse = false;
1297 vid_usinghidecursor = false;
1298 vid_usingvsync = false;
1300 vid_activewindow = true;
1302 vid_x11_dgasupported = XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion);
1303 if (!vid_x11_dgasupported)
1304 Con_Print( "Failed to detect XF86DGA Mouse extension\n" );
1307 VID_Soft_SharedSetup();
1312 static qboolean VID_InitModeGL(viddef_mode_t *mode)
1316 XSetWindowAttributes attr;
1317 XClassHint *clshints;
1319 XSizeHints *szhints;
1321 XVisualInfo *visinfo;
1322 int MajorVersion, MinorVersion;
1323 const char *drivername;
1326 unsigned char *data;
1329 vid_isfullscreen = false;
1330 vid_isdesktopfullscreen = false;
1331 vid_isvidmodefullscreen = false;
1332 vid_isoverrideredirect = false;
1334 #if defined(__APPLE__) && defined(__MACH__)
1335 drivername = "/usr/X11R6/lib/libGL.1.dylib";
1337 drivername = "libGL.so.1";
1339 // 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
1340 // 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
1341 // LordHavoc: although this works on MacOSX, it's useless there (as there is only one system libGL)
1342 i = COM_CheckParm("-gl_driver");
1343 if (i && i < com_argc - 1)
1344 drivername = com_argv[i + 1];
1345 if (!GL_OpenLibrary(drivername))
1347 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
1351 if (!(vidx11_display = XOpenDisplay(NULL)))
1353 Con_Print("Couldn't open the X display\n");
1357 // LordHavoc: making the close button on a window do the right thing
1358 // seems to involve this mess, sigh...
1359 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
1360 net_wm_state_atom = XInternAtom(vidx11_display, "_NET_WM_STATE", false);
1361 net_wm_state_fullscreen_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_FULLSCREEN", false);
1362 net_wm_state_hidden_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_HIDDEN", false);
1363 net_wm_icon = XInternAtom(vidx11_display, "_NET_WM_ICON", false);
1364 cardinal = XInternAtom(vidx11_display, "CARDINAL", false);
1366 // make autorepeat send keypress/keypress/.../keyrelease instead of intervening keyrelease
1367 XkbSetDetectableAutoRepeat(vidx11_display, true, NULL);
1369 vidx11_screen = DefaultScreen(vidx11_display);
1370 root = RootWindow(vidx11_display, vidx11_screen);
1372 desktop_mode.width = DisplayWidth(vidx11_display, vidx11_screen);
1373 desktop_mode.height = DisplayHeight(vidx11_display, vidx11_screen);
1374 desktop_mode.bpp = DefaultDepth(vidx11_display, vidx11_screen);
1375 desktop_mode.refreshrate = 60; // FIXME
1376 desktop_mode.pixelheight_num = 1; // FIXME
1377 desktop_mode.pixelheight_denom = 1; // FIXME
1379 // Get video mode list
1380 MajorVersion = MinorVersion = 0;
1381 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
1382 vidmode_ext = false;
1385 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
1389 if ((qglXChooseVisual = (XVisualInfo *(GLAPIENTRY *)(Display *dpy, int screen, int *attribList))GL_GetProcAddress("glXChooseVisual")) == NULL
1390 || (qglXCreateContext = (GLXContext (GLAPIENTRY *)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct))GL_GetProcAddress("glXCreateContext")) == NULL
1391 || (qglXDestroyContext = (void (GLAPIENTRY *)(Display *dpy, GLXContext ctx))GL_GetProcAddress("glXDestroyContext")) == NULL
1392 || (qglXMakeCurrent = (Bool (GLAPIENTRY *)(Display *dpy, GLXDrawable drawable, GLXContext ctx))GL_GetProcAddress("glXMakeCurrent")) == NULL
1393 || (qglXSwapBuffers = (void (GLAPIENTRY *)(Display *dpy, GLXDrawable drawable))GL_GetProcAddress("glXSwapBuffers")) == NULL
1394 || (qglXQueryExtensionsString = (const char *(GLAPIENTRY *)(Display *dpy, int screen))GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
1396 Con_Printf("glX functions not found in %s\n", gl_driver);
1400 VID_BuildGLXAttrib(attrib, mode->bitsperpixel == 32, mode->stereobuffer, mode->samples);
1401 visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
1404 Con_Print("Couldn't get an RGB, Double-buffered, Depth visual\n");
1408 if (mode->fullscreen)
1410 if(vid_desktopfullscreen.integer)
1412 // TODO detect WM support
1413 vid_isdesktopfullscreen = true;
1414 vid_isfullscreen = true;
1415 // width and height will be filled in later
1416 Con_DPrintf("Using NetWM fullscreen mode\n");
1419 if(!vid_isfullscreen && vidmode_ext)
1421 int best_fit, best_dist, dist, x, y;
1423 // Are we going fullscreen? If so, let's change video mode
1424 XF86VidModeModeLine *current_vidmode;
1425 XF86VidModeModeInfo **vidmodes;
1428 // This nice hack comes from the SDL source code
1429 current_vidmode = (XF86VidModeModeLine*)((char*)&init_vidmode + sizeof(init_vidmode.dotclock));
1430 XF86VidModeGetModeLine(vidx11_display, vidx11_screen, (int*)&init_vidmode.dotclock, current_vidmode);
1432 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
1436 for (i = 0; i < num_vidmodes; i++)
1438 if (mode->width > vidmodes[i]->hdisplay || mode->height > vidmodes[i]->vdisplay)
1441 x = mode->width - vidmodes[i]->hdisplay;
1442 y = mode->height - vidmodes[i]->vdisplay;
1443 dist = (x * x) + (y * y);
1444 if (best_fit == -1 || dist < best_dist)
1453 // LordHavoc: changed from ActualWidth/ActualHeight =,
1454 // to width/height =, so the window will take the full area of
1456 mode->width = vidmodes[best_fit]->hdisplay;
1457 mode->height = vidmodes[best_fit]->vdisplay;
1459 // change to the mode
1460 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
1461 memcpy(&game_vidmode, vidmodes[best_fit], sizeof(game_vidmode));
1462 vid_isvidmodefullscreen = true;
1463 vid_isfullscreen = true;
1465 // Move the viewport to top left
1466 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
1467 Con_DPrintf("Using XVidMode fullscreen mode at %dx%d\n", mode->width, mode->height);
1473 if(!vid_isfullscreen)
1475 // sorry, no FS available
1476 // use the full desktop resolution
1477 vid_isfullscreen = true;
1478 // width and height will be filled in later
1479 mode->width = DisplayWidth(vidx11_display, vidx11_screen);
1480 mode->height = DisplayHeight(vidx11_display, vidx11_screen);
1481 Con_DPrintf("Using X11 fullscreen mode at %dx%d\n", mode->width, mode->height);
1485 // LordHavoc: save the visual for use in gamma ramp settings later
1486 vidx11_visual = visinfo->visual;
1488 /* window attributes */
1489 attr.background_pixel = 0;
1490 attr.border_pixel = 0;
1491 // LordHavoc: save the colormap for later, too
1492 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
1493 attr.event_mask = X_MASK;
1495 if (mode->fullscreen)
1497 if(vid_isdesktopfullscreen)
1499 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask;
1500 attr.backing_store = NotUseful;
1501 attr.save_under = False;
1505 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
1506 attr.override_redirect = True;
1507 attr.backing_store = NotUseful;
1508 attr.save_under = False;
1509 vid_isoverrideredirect = true; // so it knows to grab
1514 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
1517 win = XCreateWindow(vidx11_display, root, 0, 0, mode->width, mode->height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
1519 data = loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
1522 // use _NET_WM_ICON too
1523 static long netwm_icon[MAX_NETWM_ICON];
1529 if(pos + 2 * image_width * image_height < MAX_NETWM_ICON)
1531 netwm_icon[pos++] = image_width;
1532 netwm_icon[pos++] = image_height;
1533 for(i = 0; i < image_height; ++i)
1534 for(j = 0; j < image_width; ++j)
1535 netwm_icon[pos++] = BuffLittleLong(&data[(i*image_width+j)*4]);
1539 Con_Printf("Skipping NETWM icon #%d because there is no space left\n", i);
1543 data = loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "darkplaces-icon%d", i), false, false, false, NULL);
1545 XChangeProperty(vidx11_display, win, net_wm_icon, cardinal, 32, PropModeReplace, (const unsigned char *) netwm_icon, pos);
1548 // fallthrough for old window managers
1549 xpm = (char *) FS_LoadFile("darkplaces-icon.xpm", tempmempool, false, NULL);
1552 idata = XPM_DecodeString(xpm);
1554 idata = ENGINE_ICON;
1556 wmhints = XAllocWMHints();
1557 if(XpmCreatePixmapFromData(vidx11_display, win,
1559 &wmhints->icon_pixmap, &wmhints->icon_mask, NULL) == XpmSuccess)
1560 wmhints->flags |= IconPixmapHint | IconMaskHint;
1565 clshints = XAllocClassHint();
1566 clshints->res_name = strdup(gamename);
1567 clshints->res_class = strdup("DarkPlaces");
1569 szhints = XAllocSizeHints();
1570 if(vid_resizable.integer == 0 && !vid_isdesktopfullscreen)
1572 szhints->min_width = szhints->max_width = mode->width;
1573 szhints->min_height = szhints->max_height = mode->height;
1574 szhints->flags |= PMinSize | PMaxSize;
1577 XmbSetWMProperties(vidx11_display, win, gamename, gamename, (char **) com_argv, com_argc, szhints, wmhints, clshints);
1578 // strdup() allocates using malloc(), should be freed with free()
1579 free(clshints->res_name);
1580 free(clshints->res_class);
1585 //XStoreName(vidx11_display, win, gamename);
1586 XMapWindow(vidx11_display, win);
1588 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
1590 if (vid_isoverrideredirect)
1592 XMoveWindow(vidx11_display, win, 0, 0);
1593 XRaiseWindow(vidx11_display, win);
1594 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
1595 XFlush(vidx11_display);
1598 if(vid_isvidmodefullscreen)
1600 // Move the viewport to top left
1601 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
1604 //XSync(vidx11_display, False);
1606 ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
1607 XFree(visinfo); // glXChooseVisual man page says to use XFree to free visinfo
1610 Con_Printf ("glXCreateContext failed\n");
1614 if (!qglXMakeCurrent(vidx11_display, win, ctx))
1616 Con_Printf ("glXMakeCurrent failed\n");
1620 XSync(vidx11_display, False);
1622 if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
1624 Con_Printf ("glGetString not found in %s\n", gl_driver);
1628 gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
1629 gl_platform = "GLX";
1630 gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
1632 // COMMANDLINEOPTION: Linux GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1633 // COMMANDLINEOPTION: BSD GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1634 // COMMANDLINEOPTION: MacOSX GLX: -nogetprocaddress disables GLX_ARB_get_proc_address (not required, more formal method of getting extension functions)
1635 GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
1636 // COMMANDLINEOPTION: Linux GLX: -novideosync disables GLX_SGI_swap_control
1637 // COMMANDLINEOPTION: BSD GLX: -novideosync disables GLX_SGI_swap_control
1638 // COMMANDLINEOPTION: MacOSX GLX: -novideosync disables GLX_SGI_swap_control
1639 GL_CheckExtension("GLX_SGI_swap_control", swapcontrolfuncs, "-novideosync", false);
1641 vid_usingmousegrab = false;
1642 vid_usingmouse = false;
1643 vid_usinghidecursor = false;
1644 vid_usingvsync = false;
1646 vid_activewindow = true;
1648 vid_x11_dgasupported = XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion);
1649 if (!vid_x11_dgasupported)
1650 Con_Print( "Failed to detect XF86DGA Mouse extension\n" );
1657 qboolean VID_InitMode(viddef_mode_t *mode)
1660 if (vid_soft.integer)
1661 return VID_InitModeSoft(mode);
1664 return VID_InitModeGL(mode);
1667 void Sys_SendKeyEvents(void)
1669 static qboolean sound_active = true;
1671 // enable/disable sound on focus gain/loss
1672 if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
1677 sound_active = true;
1685 sound_active = false;
1692 void VID_BuildJoyState(vid_joystate_t *joystate)
1694 VID_Shared_BuildJoyState_Begin(joystate);
1695 VID_Shared_BuildJoyState_Finish(joystate);
1698 void VID_EnableJoystick(qboolean enable)
1700 int index = joy_enable.integer > 0 ? joy_index.integer : -1;
1701 qboolean success = false;
1702 int sharedcount = 0;
1703 sharedcount = VID_Shared_SetJoystick(index);
1704 if (index >= 0 && index < sharedcount)
1707 // update cvar containing count of XInput joysticks
1708 if (joy_detected.integer != sharedcount)
1709 Cvar_SetValueQuick(&joy_detected, sharedcount);
1711 Cvar_SetValueQuick(&joy_active, success ? 1 : 0);
1716 vid_joystate_t joystate;
1717 VID_EnableJoystick(true);
1718 VID_BuildJoyState(&joystate);
1719 VID_ApplyJoyState(&joystate);
1722 vid_mode_t *VID_GetDesktopMode(void)
1724 return &desktop_mode;
1727 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
1733 XF86VidModeModeInfo **vidmodes;
1736 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
1738 for (i = 0; i < num_vidmodes; i++)
1742 // we don't get bpp info, so let's just assume all of 8, 15, 16, 24, 32 work
1743 for(bpp = 8; bpp <= 32; bpp = ((bpp == 8) ? 15 : (bpp & 0xF8) + 8))
1747 modes[k].width = vidmodes[i]->hdisplay;
1748 modes[k].height = vidmodes[i]->vdisplay;
1750 if(vidmodes[i]->dotclock && vidmodes[i]->htotal && vidmodes[i]->vtotal)
1751 modes[k].refreshrate = vidmodes[i]->dotclock / vidmodes[i]->htotal / vidmodes[i]->vtotal;
1753 modes[k].refreshrate = 60;
1754 modes[k].pixelheight_num = 1;
1755 modes[k].pixelheight_denom = 1; // xvidmode does not provide this
1759 // manpage of XF86VidModeGetAllModeLines says it should be freed by the caller
1763 return 0; // FIXME implement this