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 //#include <termios.h>
21 //#include <sys/ioctl.h>
22 //#include <sys/stat.h>
31 #include <X11/Xutil.h>
34 #include <X11/keysym.h>
35 #include <X11/cursorfont.h>
37 #include <X11/extensions/XShm.h>
38 #include <X11/extensions/xf86dga.h>
39 #include <X11/extensions/xf86vmode.h>
43 int cl_available = true;
46 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
47 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
48 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
49 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
50 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
51 const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
53 //GLX_ARB_get_proc_address
54 void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
56 static dllfunction_t getprocaddressfuncs[] =
58 {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
63 GLint (GLAPIENTRY *qglXGetVideoSyncSGI)(GLuint *count);
64 GLint (GLAPIENTRY *qglXWaitVideoSyncSGI)(GLint divisor, GLint remainder, GLuint *count);
66 static dllfunction_t videosyncfuncs[] =
68 {"glXGetVideoSyncSGI", (void **) &qglXGetVideoSyncSGI},
69 {"glXWaitVideoSyncSGI", (void **) &qglXWaitVideoSyncSGI},
73 static Display *vidx11_display = NULL;
74 static int vidx11_screen;
76 static GLXContext ctx = NULL;
78 Atom wm_delete_window_atom;
80 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
81 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
82 PointerMotionMask | ButtonMotionMask )
83 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | StructureNotifyMask )
86 static qboolean mouse_avail = true;
87 static qboolean mouse_active = false, usingmouse = false;
88 static float mouse_x, mouse_y;
89 static int p_mouse_x, p_mouse_y;
91 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
92 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
94 qboolean vidmode_ext = false;
96 static int win_x, win_y;
98 static int scr_width, scr_height;
100 static XF86VidModeModeInfo **vidmodes;
101 static int num_vidmodes;
102 static qboolean vidmode_active = false;
104 static Visual *vidx11_visual;
105 static Colormap vidx11_colormap;
107 /*-----------------------------------------------------------------------*/
109 static int XLateKey(XKeyEvent *ev)
115 XLookupString(ev, buf, sizeof buf, &keysym, 0);
119 case XK_KP_Page_Up: key = K_KP_PGUP; break;
120 case XK_Page_Up: key = K_PGUP; break;
122 case XK_KP_Page_Down: key = K_KP_PGDN; break;
123 case XK_Page_Down: key = K_PGDN; break;
125 case XK_KP_Home: key = K_KP_HOME; break;
126 case XK_Home: key = K_HOME; break;
128 case XK_KP_End: key = K_KP_END; break;
129 case XK_End: key = K_END; break;
131 case XK_KP_Left: key = K_KP_LEFTARROW; break;
132 case XK_Left: key = K_LEFTARROW; break;
134 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
135 case XK_Right: key = K_RIGHTARROW; break;
137 case XK_KP_Down: key = K_KP_DOWNARROW; break;
138 case XK_Down: key = K_DOWNARROW; break;
140 case XK_KP_Up: key = K_KP_UPARROW; break;
141 case XK_Up: key = K_UPARROW; break;
143 case XK_Escape: key = K_ESCAPE; break;
145 case XK_KP_Enter: key = K_KP_ENTER; break;
146 case XK_Return: key = K_ENTER; break;
148 case XK_Tab: key = K_TAB; break;
150 case XK_F1: key = K_F1; break;
152 case XK_F2: key = K_F2; break;
154 case XK_F3: key = K_F3; break;
156 case XK_F4: key = K_F4; break;
158 case XK_F5: key = K_F5; break;
160 case XK_F6: key = K_F6; break;
162 case XK_F7: key = K_F7; break;
164 case XK_F8: key = K_F8; break;
166 case XK_F9: key = K_F9; break;
168 case XK_F10: key = K_F10; break;
170 case XK_F11: key = K_F11; break;
172 case XK_F12: key = K_F12; break;
174 case XK_BackSpace: key = K_BACKSPACE; break;
176 case XK_KP_Delete: key = K_KP_DEL; break;
177 case XK_Delete: key = K_DEL; break;
179 case XK_Pause: key = K_PAUSE; break;
182 case XK_Shift_R: key = K_SHIFT; break;
186 case XK_Control_R: key = K_CTRL; break;
191 case XK_Meta_R: key = K_ALT; break;
193 case XK_KP_Begin: key = K_KP_5; break;
195 case XK_Insert:key = K_INS; break;
196 case XK_KP_Insert: key = K_KP_INS; break;
198 case XK_KP_Multiply: key = '*'; break;
199 case XK_KP_Add: key = K_KP_PLUS; break;
200 case XK_KP_Subtract: key = K_KP_MINUS; break;
201 case XK_KP_Divide: key = K_KP_SLASH; break;
204 case 0x021: key = '1';break;/* [!] */
205 case 0x040: key = '2';break;/* [@] */
206 case 0x023: key = '3';break;/* [#] */
207 case 0x024: key = '4';break;/* [$] */
208 case 0x025: key = '5';break;/* [%] */
209 case 0x05e: key = '6';break;/* [^] */
210 case 0x026: key = '7';break;/* [&] */
211 case 0x02a: key = '8';break;/* [*] */
212 case 0x028: key = '9';;break;/* [(] */
213 case 0x029: key = '0';break;/* [)] */
214 case 0x05f: key = '-';break;/* [_] */
215 case 0x02b: key = '=';break;/* [+] */
216 case 0x07c: key = '\'';break;/* [|] */
217 case 0x07d: key = '[';break;/* [}] */
218 case 0x07b: key = ']';break;/* [{] */
219 case 0x022: key = '\'';break;/* ["] */
220 case 0x03a: key = ';';break;/* [:] */
221 case 0x03f: key = '/';break;/* [?] */
222 case 0x03e: key = '.';break;/* [>] */
223 case 0x03c: key = ',';break;/* [<] */
227 key = *(unsigned char*)buf;
228 if (key >= 'A' && key <= 'Z')
229 key = key - 'A' + 'a';
236 static Cursor CreateNullCursor(Display *display, Window root)
244 cursormask = XCreatePixmap(display, root, 1, 1, 1);
245 xgc.function = GXclear;
246 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
247 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
248 dummycolour.pixel = 0;
250 dummycolour.flags = 04;
251 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
252 XFreePixmap(display,cursormask);
257 static void install_grabs(void)
259 XWindowAttributes attribs_1;
260 XSetWindowAttributes attribs_2;
262 XGetWindowAttributes(vidx11_display, win, &attribs_1);
263 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
264 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
267 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
269 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
273 int MajorVersion, MinorVersion;
275 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
277 // unable to query, probalby not supported
278 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
284 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
285 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
289 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, scr_width / 2, scr_height / 2);
291 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
294 mouse_x = mouse_y = 0;
297 static void uninstall_grabs(void)
299 if (!vidx11_display || !win)
302 if (vid_dga.integer == 1)
303 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
305 XUngrabPointer(vidx11_display, CurrentTime);
306 XUngrabKeyboard(vidx11_display, CurrentTime);
309 XUndefineCursor(vidx11_display, win);
311 mouse_active = false;
314 static void HandleEvents(void)
317 qboolean dowarp = false;
322 while (XPending(vidx11_display))
324 XNextEvent(vidx11_display, &event);
330 Key_Event(XLateKey(&event.xkey), true);
335 Key_Event(XLateKey(&event.xkey), false);
342 if (vid_dga.integer == 1)
344 mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
345 mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
350 if (!event.xmotion.send_event)
352 mouse_x += event.xmotion.x - p_mouse_x;
353 mouse_y += event.xmotion.y - p_mouse_y;
354 if (abs(scr_width/2 - event.xmotion.x) > scr_width / 4 || abs(scr_height/2 - event.xmotion.y) > scr_height / 4)
357 p_mouse_x = event.xmotion.x;
358 p_mouse_y = event.xmotion.y;
362 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
366 // mouse button pressed
367 switch(event.xbutton.button)
370 Key_Event(K_MOUSE1, true);
373 Key_Event(K_MOUSE3, true);
376 Key_Event(K_MOUSE2, true);
379 Key_Event(K_MWHEELUP, true);
382 Key_Event(K_MWHEELDOWN, true);
385 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-5 expected\n", event.xbutton.button);
391 // mouse button released
392 switch(event.xbutton.button)
395 Key_Event(K_MOUSE1, false);
398 Key_Event(K_MOUSE3, false);
401 Key_Event(K_MOUSE2, false);
404 Key_Event(K_MWHEELUP, false);
407 Key_Event(K_MWHEELDOWN, false);
410 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-5 expected\n", event.xbutton.button);
417 win_x = event.xcreatewindow.x;
418 win_y = event.xcreatewindow.y;
421 case ConfigureNotify:
422 // window changed size/location
423 win_x = event.xconfigure.x;
424 win_y = event.xconfigure.y;
427 // window has been destroyed
431 // window manager messages
432 if ((event.xclient.format == 32) && (event.xclient.data.l[0] == wm_delete_window_atom))
438 vid_allowhwgamma = true;
441 // window iconified/rolledup/whatever
443 vid_allowhwgamma = false;
444 VID_RestoreSystemGamma();
447 // window is now the input focus
448 vid_allowhwgamma = true;
451 // window is no longer the input focus
452 vid_allowhwgamma = false;
453 VID_RestoreSystemGamma();
456 // mouse entered window
457 vid_allowhwgamma = true;
461 vid_allowhwgamma = false;
462 VID_RestoreSystemGamma();
469 /* move the mouse to the window center again */
470 p_mouse_x = scr_width / 2;
471 p_mouse_y = scr_height / 2;
472 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
477 static void IN_DeactivateMouse( void )
479 if (!mouse_avail || !vidx11_display || !win)
485 mouse_active = false;
489 static void IN_ActivateMouse( void )
491 if (!mouse_avail || !vidx11_display || !win)
496 mouse_x = mouse_y = 0; // don't spazz
502 void VID_Shutdown(void)
504 if (!ctx || !vidx11_display)
511 VID_RestoreSystemGamma();
514 // FIXME: glXDestroyContext here?
516 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[0]);
518 XDestroyWindow(vidx11_display, win);
519 XCloseDisplay(vidx11_display);
521 vidmode_active = false;
522 vidx11_display = NULL;
529 void signal_handler(int sig)
531 printf("Received signal %d, exiting...\n", sig);
532 VID_RestoreSystemGamma();
539 signal(SIGHUP, signal_handler);
540 signal(SIGINT, signal_handler);
541 signal(SIGQUIT, signal_handler);
542 signal(SIGILL, signal_handler);
543 signal(SIGTRAP, signal_handler);
544 signal(SIGIOT, signal_handler);
545 signal(SIGBUS, signal_handler);
546 signal(SIGFPE, signal_handler);
547 signal(SIGSEGV, signal_handler);
548 signal(SIGTERM, signal_handler);
556 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
560 *height = scr_height;
563 void VID_Finish (void)
566 if (r_render.integer)
569 qglXSwapBuffers(vidx11_display, win);
572 // handle the mouse state when windowed if that's changed
574 if (vid_mouse.integer && !key_consoleactive)
591 IN_DeactivateMouse ();
596 int VID_SetGamma(unsigned short *ramps)
598 return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
601 int VID_GetGamma(unsigned short *ramps)
603 return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
608 Cvar_RegisterVariable (&vid_dga);
609 Cvar_RegisterVariable (&vid_dga_mouseaccel);
610 InitSig(); // trap evil signals
611 if (COM_CheckParm ("-nomouse") || COM_CheckParm("-safe"))
615 void VID_BuildGLXAttrib(int *attrib, int stencil)
617 *attrib++ = GLX_RGBA;
618 *attrib++ = GLX_RED_SIZE;*attrib++ = 1;
619 *attrib++ = GLX_GREEN_SIZE;*attrib++ = 1;
620 *attrib++ = GLX_BLUE_SIZE;*attrib++ = 1;
621 *attrib++ = GLX_DOUBLEBUFFER;
622 *attrib++ = GLX_DEPTH_SIZE;*attrib++ = 1;
623 // if stencil is enabled, ask for alpha too
626 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
627 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 1;
632 int VID_InitMode(int fullscreen, int width, int height, int bpp, int stencil)
636 XSetWindowAttributes attr;
639 XVisualInfo *visinfo;
640 int MajorVersion, MinorVersion;
641 const char *drivername;
643 drivername = "libGL.so.1";
644 i = COM_CheckParm("-gl_driver");
645 if (i && i < com_argc - 1)
646 drivername = com_argv[i + 1];
647 if (!GL_OpenLibrary(drivername))
649 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
653 if (!(vidx11_display = XOpenDisplay(NULL)))
655 Con_Printf("Couldn't open the X display\n");
659 vidx11_screen = DefaultScreen(vidx11_display);
660 root = RootWindow(vidx11_display, vidx11_screen);
662 // Get video mode list
663 MajorVersion = MinorVersion = 0;
664 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
668 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
672 if ((qglXChooseVisual = GL_GetProcAddress("glXChooseVisual")) == NULL
673 || (qglXCreateContext = GL_GetProcAddress("glXCreateContext")) == NULL
674 || (qglXMakeCurrent = GL_GetProcAddress("glXMakeCurrent")) == NULL
675 || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL
676 || (qglXQueryExtensionsString = GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
678 Con_Printf("glX functions not found in %s\n", gl_driver);
682 VID_BuildGLXAttrib(attrib, stencil);
683 visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
686 Con_Printf("Couldn't get an RGB, Double-buffered, Depth visual\n");
692 int best_fit, best_dist, dist, x, y;
694 // Are we going fullscreen? If so, let's change video mode
697 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
701 for (i = 0; i < num_vidmodes; i++)
703 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
706 x = width - vidmodes[i]->hdisplay;
707 y = height - vidmodes[i]->vdisplay;
708 dist = (x * x) + (y * y);
709 if (dist < best_dist)
718 // LordHavoc: changed from ActualWidth/ActualHeight =,
719 // to width/height =, so the window will take the full area of
721 width = vidmodes[best_fit]->hdisplay;
722 height = vidmodes[best_fit]->vdisplay;
724 // change to the mode
725 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
726 vidmode_active = true;
728 // Move the viewport to top left
729 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
736 // LordHavoc: save the visual for use in gamma ramp settings later
737 vidx11_visual = visinfo->visual;
739 /* window attributes */
740 attr.background_pixel = 0;
741 attr.border_pixel = 0;
742 // LordHavoc: save the colormap for later, too
743 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
744 attr.event_mask = X_MASK;
747 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
748 attr.override_redirect = True;
749 attr.backing_store = NotUseful;
750 attr.save_under = False;
753 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
755 win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
756 XStoreName(vidx11_display, win, gamename);
757 XMapWindow(vidx11_display, win);
759 // LordHavoc: making the close button on a window do the right thing
760 // seems to involve this mess, sigh...
761 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
762 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
766 XMoveWindow(vidx11_display, win, 0, 0);
767 XRaiseWindow(vidx11_display, win);
768 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
769 XFlush(vidx11_display);
770 // Move the viewport to top left
771 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
774 //XSync(vidx11_display, False);
776 ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
778 Sys_Error ("glXCreateContext failed\n");
780 if (!qglXMakeCurrent(vidx11_display, win, ctx))
781 Sys_Error ("glXMakeCurrent failed\n");
783 XSync(vidx11_display, False);
788 if ((qglGetString = GL_GetProcAddress("glGetString")) == NULL)
789 Sys_Error("glGetString not found in %s", gl_driver);
791 gl_renderer = qglGetString(GL_RENDERER);
792 gl_vendor = qglGetString(GL_VENDOR);
793 gl_version = qglGetString(GL_VERSION);
794 gl_extensions = qglGetString(GL_EXTENSIONS);
796 gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
798 GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
799 gl_videosyncavailable = GL_CheckExtension("GLX_SGI_video_sync", videosyncfuncs, "-novideosync", false);
803 vid_allowhwgamma = true;
808 void Sys_SendKeyEvents(void)
818 void IN_Commands (void)
822 void IN_Move (usercmd_t *cmd)
825 IN_Mouse(cmd, mouse_x, mouse_y);
830 static void *prjobj = NULL;
832 int GL_OpenLibrary(const char *name)
834 Con_Printf("Loading GL driver %s\n", name);
836 if (!(prjobj = dlopen(name, RTLD_LAZY)))
838 Con_Printf("Unable to open symbol list for %s\n", name);
841 strcpy(gl_driver, name);
845 void GL_CloseLibrary(void)
851 qglXGetProcAddressARB = NULL;
854 gl_platformextensions = "";
857 void *GL_GetProcAddress(const char *name)
860 if (qglXGetProcAddressARB != NULL)
861 p = (void *) qglXGetProcAddressARB(name);
863 p = (void *) dlsym(prjobj, name);