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 | \
84 StructureNotifyMask | FocusChangeMask | EnterWindowMask | \
88 static qboolean mouse_avail = true;
89 static qboolean mouse_active = false, usingmouse = false;
90 static float mouse_x, mouse_y;
91 static int p_mouse_x, p_mouse_y;
93 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
94 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
96 qboolean vidmode_ext = false;
98 static int win_x, win_y;
100 static int scr_width, scr_height;
102 static XF86VidModeModeInfo **vidmodes;
103 static int num_vidmodes;
104 static qboolean vidmode_active = false;
106 static Visual *vidx11_visual;
107 static Colormap vidx11_colormap;
109 /*-----------------------------------------------------------------------*/
111 static int XLateKey(XKeyEvent *ev, char *ascii)
115 KeySym keysym, shifted;
117 keysym = XLookupKeysym (ev, 0);
118 XLookupString(ev, buf, sizeof buf, &shifted, 0);
123 case XK_KP_Page_Up: key = K_KP_PGUP; break;
124 case XK_Page_Up: key = K_PGUP; break;
126 case XK_KP_Page_Down: key = K_KP_PGDN; break;
127 case XK_Page_Down: key = K_PGDN; break;
129 case XK_KP_Home: key = K_KP_HOME; break;
130 case XK_Home: key = K_HOME; break;
132 case XK_KP_End: key = K_KP_END; break;
133 case XK_End: key = K_END; break;
135 case XK_KP_Left: key = K_KP_LEFTARROW; break;
136 case XK_Left: key = K_LEFTARROW; break;
138 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
139 case XK_Right: key = K_RIGHTARROW; break;
141 case XK_KP_Down: key = K_KP_DOWNARROW; break;
142 case XK_Down: key = K_DOWNARROW; break;
144 case XK_KP_Up: key = K_KP_UPARROW; break;
145 case XK_Up: key = K_UPARROW; break;
147 case XK_Escape: key = K_ESCAPE; break;
149 case XK_KP_Enter: key = K_KP_ENTER; break;
150 case XK_Return: key = K_ENTER; break;
152 case XK_Tab: key = K_TAB; break;
154 case XK_F1: key = K_F1; break;
156 case XK_F2: key = K_F2; break;
158 case XK_F3: key = K_F3; break;
160 case XK_F4: key = K_F4; break;
162 case XK_F5: key = K_F5; break;
164 case XK_F6: key = K_F6; break;
166 case XK_F7: key = K_F7; break;
168 case XK_F8: key = K_F8; break;
170 case XK_F9: key = K_F9; break;
172 case XK_F10: key = K_F10; break;
174 case XK_F11: key = K_F11; break;
176 case XK_F12: key = K_F12; break;
178 case XK_BackSpace: key = K_BACKSPACE; break;
180 case XK_KP_Delete: key = K_KP_DEL; break;
181 case XK_Delete: key = K_DEL; break;
183 case XK_Pause: key = K_PAUSE; break;
186 case XK_Shift_R: key = K_SHIFT; break;
190 case XK_Control_R: key = K_CTRL; break;
195 case XK_Meta_R: key = K_ALT; break;
197 case XK_KP_Begin: key = K_KP_5; break;
199 case XK_Insert:key = K_INS; break;
200 case XK_KP_Insert: key = K_KP_INS; break;
202 case XK_KP_Multiply: key = '*'; break;
203 case XK_KP_Add: key = K_KP_PLUS; break;
204 case XK_KP_Subtract: key = K_KP_MINUS; break;
205 case XK_KP_Divide: key = K_KP_SLASH; break;
208 if (keysym < 32 && keysym > 126)
211 if (keysym >= 'A' && keysym <= 'Z')
212 key = keysym - 'A' + 'a';
222 static Cursor CreateNullCursor(Display *display, Window root)
230 cursormask = XCreatePixmap(display, root, 1, 1, 1);
231 xgc.function = GXclear;
232 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
233 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
234 dummycolour.pixel = 0;
236 dummycolour.flags = 04;
237 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
238 XFreePixmap(display,cursormask);
243 static void install_grabs(void)
245 XWindowAttributes attribs_1;
246 XSetWindowAttributes attribs_2;
248 XGetWindowAttributes(vidx11_display, win, &attribs_1);
249 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
250 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
253 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
255 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
259 int MajorVersion, MinorVersion;
261 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
263 // unable to query, probalby not supported
264 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
270 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
271 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
275 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, scr_width / 2, scr_height / 2);
277 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
280 mouse_x = mouse_y = 0;
283 static void uninstall_grabs(void)
285 if (!vidx11_display || !win)
288 if (vid_dga.integer == 1)
289 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
291 XUngrabPointer(vidx11_display, CurrentTime);
292 XUngrabKeyboard(vidx11_display, CurrentTime);
295 XUndefineCursor(vidx11_display, win);
297 mouse_active = false;
300 static void HandleEvents(void)
305 qboolean dowarp = false;
310 while (XPending(vidx11_display))
312 XNextEvent(vidx11_display, &event);
318 key = XLateKey (&event.xkey, &ascii);
319 Key_Event(key, ascii, true);
324 key = XLateKey (&event.xkey, &ascii);
325 Key_Event(key, ascii, false);
332 if (vid_dga.integer == 1)
334 mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
335 mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
340 if (!event.xmotion.send_event)
342 mouse_x += event.xmotion.x - p_mouse_x;
343 mouse_y += event.xmotion.y - p_mouse_y;
344 if (abs(scr_width/2 - event.xmotion.x) > scr_width / 4 || abs(scr_height/2 - event.xmotion.y) > scr_height / 4)
347 p_mouse_x = event.xmotion.x;
348 p_mouse_y = event.xmotion.y;
352 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
356 // mouse button pressed
357 switch(event.xbutton.button)
360 Key_Event(K_MOUSE1, 0, true);
363 Key_Event(K_MOUSE3, 0, true);
366 Key_Event(K_MOUSE2, 0, true);
369 Key_Event(K_MWHEELUP, 0, true);
372 Key_Event(K_MWHEELDOWN, 0, true);
375 Key_Event(K_MOUSE4, 0, true);
378 Key_Event(K_MOUSE5, 0, true);
381 Key_Event(K_MOUSE6, 0, true);
384 Key_Event(K_MOUSE7, 0, true);
387 Key_Event(K_MOUSE8, 0, true);
390 Key_Event(K_MOUSE9, 0, true);
393 Key_Event(K_MOUSE10, 0, true);
396 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-12 expected\n", event.xbutton.button);
402 // mouse button released
403 switch(event.xbutton.button)
406 Key_Event(K_MOUSE1, 0, false);
409 Key_Event(K_MOUSE3, 0, false);
412 Key_Event(K_MOUSE2, 0, false);
415 Key_Event(K_MWHEELUP, 0, false);
418 Key_Event(K_MWHEELDOWN, 0, false);
421 Key_Event(K_MOUSE4, 0, false);
424 Key_Event(K_MOUSE5, 0, false);
427 Key_Event(K_MOUSE6, 0, false);
430 Key_Event(K_MOUSE7, 0, false);
433 Key_Event(K_MOUSE8, 0, false);
436 Key_Event(K_MOUSE9, 0, false);
439 Key_Event(K_MOUSE10, 0, false);
442 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-12 expected\n", event.xbutton.button);
449 win_x = event.xcreatewindow.x;
450 win_y = event.xcreatewindow.y;
453 case ConfigureNotify:
454 // window changed size/location
455 win_x = event.xconfigure.x;
456 win_y = event.xconfigure.y;
459 // window has been destroyed
463 // window manager messages
464 if ((event.xclient.format == 32) && ((unsigned int)event.xclient.data.l[0] == wm_delete_window_atom))
470 vid_allowhwgamma = true;
473 // window iconified/rolledup/whatever
475 vid_allowhwgamma = false;
476 VID_RestoreSystemGamma();
479 // window is now the input focus
480 vid_allowhwgamma = true;
483 // window is no longer the input focus
484 vid_allowhwgamma = false;
485 VID_RestoreSystemGamma();
488 // mouse entered window
489 vid_allowhwgamma = true;
493 vid_allowhwgamma = false;
494 VID_RestoreSystemGamma();
501 /* move the mouse to the window center again */
502 p_mouse_x = scr_width / 2;
503 p_mouse_y = scr_height / 2;
504 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
509 static void IN_DeactivateMouse( void )
511 if (!mouse_avail || !vidx11_display || !win)
517 mouse_active = false;
521 static void IN_ActivateMouse( void )
523 if (!mouse_avail || !vidx11_display || !win)
528 mouse_x = mouse_y = 0; // don't spazz
534 void VID_Shutdown(void)
536 if (!ctx || !vidx11_display)
543 VID_RestoreSystemGamma();
546 // FIXME: glXDestroyContext here?
548 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[0]);
550 XDestroyWindow(vidx11_display, win);
551 XCloseDisplay(vidx11_display);
553 vidmode_active = false;
554 vidx11_display = NULL;
561 void signal_handler(int sig)
563 printf("Received signal %d, exiting...\n", sig);
564 VID_RestoreSystemGamma();
571 signal(SIGHUP, signal_handler);
572 signal(SIGINT, signal_handler);
573 signal(SIGQUIT, signal_handler);
574 signal(SIGILL, signal_handler);
575 signal(SIGTRAP, signal_handler);
576 signal(SIGIOT, signal_handler);
577 signal(SIGBUS, signal_handler);
578 signal(SIGFPE, signal_handler);
579 signal(SIGSEGV, signal_handler);
580 signal(SIGTERM, signal_handler);
588 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
592 *height = scr_height;
595 void VID_Finish (void)
598 if (r_render.integer)
601 qglXSwapBuffers(vidx11_display, win);
604 // handle the mouse state when windowed if that's changed
606 if (vid_mouse.integer && !key_consoleactive)
623 IN_DeactivateMouse ();
628 int VID_SetGamma(unsigned short *ramps)
630 return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
633 int VID_GetGamma(unsigned short *ramps)
635 return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
640 Cvar_RegisterVariable (&vid_dga);
641 Cvar_RegisterVariable (&vid_dga_mouseaccel);
642 InitSig(); // trap evil signals
643 if (COM_CheckParm ("-nomouse") || COM_CheckParm("-safe"))
647 void VID_BuildGLXAttrib(int *attrib, int stencil)
649 *attrib++ = GLX_RGBA;
650 *attrib++ = GLX_RED_SIZE;*attrib++ = 1;
651 *attrib++ = GLX_GREEN_SIZE;*attrib++ = 1;
652 *attrib++ = GLX_BLUE_SIZE;*attrib++ = 1;
653 *attrib++ = GLX_DOUBLEBUFFER;
654 *attrib++ = GLX_DEPTH_SIZE;*attrib++ = 1;
655 // if stencil is enabled, ask for alpha too
658 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
659 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 1;
664 int VID_InitMode(int fullscreen, int width, int height, int bpp, int stencil)
668 XSetWindowAttributes attr;
671 XVisualInfo *visinfo;
672 int MajorVersion, MinorVersion;
673 const char *drivername;
675 drivername = "libGL.so.1";
676 i = COM_CheckParm("-gl_driver");
677 if (i && i < com_argc - 1)
678 drivername = com_argv[i + 1];
679 if (!GL_OpenLibrary(drivername))
681 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
685 if (!(vidx11_display = XOpenDisplay(NULL)))
687 Con_Printf("Couldn't open the X display\n");
691 vidx11_screen = DefaultScreen(vidx11_display);
692 root = RootWindow(vidx11_display, vidx11_screen);
694 // Get video mode list
695 MajorVersion = MinorVersion = 0;
696 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
700 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
704 if ((qglXChooseVisual = GL_GetProcAddress("glXChooseVisual")) == NULL
705 || (qglXCreateContext = GL_GetProcAddress("glXCreateContext")) == NULL
706 || (qglXMakeCurrent = GL_GetProcAddress("glXMakeCurrent")) == NULL
707 || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL
708 || (qglXQueryExtensionsString = GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
710 Con_Printf("glX functions not found in %s\n", gl_driver);
714 VID_BuildGLXAttrib(attrib, stencil);
715 visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
718 Con_Printf("Couldn't get an RGB, Double-buffered, Depth visual\n");
724 int best_fit, best_dist, dist, x, y;
726 // Are we going fullscreen? If so, let's change video mode
729 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
733 for (i = 0; i < num_vidmodes; i++)
735 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
738 x = width - vidmodes[i]->hdisplay;
739 y = height - vidmodes[i]->vdisplay;
740 dist = (x * x) + (y * y);
741 if (dist < best_dist)
750 // LordHavoc: changed from ActualWidth/ActualHeight =,
751 // to width/height =, so the window will take the full area of
753 width = vidmodes[best_fit]->hdisplay;
754 height = vidmodes[best_fit]->vdisplay;
756 // change to the mode
757 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
758 vidmode_active = true;
760 // Move the viewport to top left
761 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
768 // LordHavoc: save the visual for use in gamma ramp settings later
769 vidx11_visual = visinfo->visual;
771 /* window attributes */
772 attr.background_pixel = 0;
773 attr.border_pixel = 0;
774 // LordHavoc: save the colormap for later, too
775 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
776 attr.event_mask = X_MASK;
779 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
780 attr.override_redirect = True;
781 attr.backing_store = NotUseful;
782 attr.save_under = False;
785 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
787 win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
788 XStoreName(vidx11_display, win, gamename);
789 XMapWindow(vidx11_display, win);
791 // LordHavoc: making the close button on a window do the right thing
792 // seems to involve this mess, sigh...
793 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
794 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
798 XMoveWindow(vidx11_display, win, 0, 0);
799 XRaiseWindow(vidx11_display, win);
800 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
801 XFlush(vidx11_display);
802 // Move the viewport to top left
803 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
806 //XSync(vidx11_display, False);
808 ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
810 Sys_Error ("glXCreateContext failed\n");
812 if (!qglXMakeCurrent(vidx11_display, win, ctx))
813 Sys_Error ("glXMakeCurrent failed\n");
815 XSync(vidx11_display, False);
820 if ((qglGetString = GL_GetProcAddress("glGetString")) == NULL)
821 Sys_Error("glGetString not found in %s", gl_driver);
823 gl_renderer = qglGetString(GL_RENDERER);
824 gl_vendor = qglGetString(GL_VENDOR);
825 gl_version = qglGetString(GL_VERSION);
826 gl_extensions = qglGetString(GL_EXTENSIONS);
828 gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
830 GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
831 gl_videosyncavailable = GL_CheckExtension("GLX_SGI_video_sync", videosyncfuncs, "-novideosync", false);
835 vid_allowhwgamma = true;
840 void Sys_SendKeyEvents(void)
850 void IN_Commands (void)
854 void IN_Move (usercmd_t *cmd)
857 IN_Mouse(cmd, mouse_x, mouse_y);
862 static void *prjobj = NULL;
864 int GL_OpenLibrary(const char *name)
866 Con_Printf("Loading OpenGL driver %s\n", name);
868 if (!(prjobj = dlopen(name, RTLD_LAZY)))
870 Con_Printf("Unable to open symbol list for %s\n", name);
873 strcpy(gl_driver, name);
877 void GL_CloseLibrary(void)
883 qglXGetProcAddressARB = NULL;
886 gl_platformextensions = "";
889 void *GL_GetProcAddress(const char *name)
892 if (qglXGetProcAddressARB != NULL)
893 p = (void *) qglXGetProcAddressARB(name);
895 p = (void *) dlsym(prjobj, name);