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>
39 #include <X11/extensions/xf86dga.h>
41 #include <X11/extensions/xf86vmode.h>
45 int cl_available = true;
48 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
49 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
50 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
51 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
52 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
53 const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
55 //GLX_ARB_get_proc_address
56 void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
58 static dllfunction_t getprocaddressfuncs[] =
60 {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
65 GLint (GLAPIENTRY *qglXGetVideoSyncSGI)(GLuint *count);
66 GLint (GLAPIENTRY *qglXWaitVideoSyncSGI)(GLint divisor, GLint remainder, GLuint *count);
68 static dllfunction_t videosyncfuncs[] =
70 {"glXGetVideoSyncSGI", (void **) &qglXGetVideoSyncSGI},
71 {"glXWaitVideoSyncSGI", (void **) &qglXWaitVideoSyncSGI},
75 static Display *vidx11_display = NULL;
76 static int vidx11_screen;
78 static GLXContext ctx = NULL;
80 Atom wm_delete_window_atom;
82 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
83 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
84 PointerMotionMask | ButtonMotionMask)
85 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | \
86 StructureNotifyMask | FocusChangeMask | EnterWindowMask | \
90 static qboolean mouse_avail = true;
91 static qboolean mouse_active = false, usingmouse = false;
92 static float mouse_x, mouse_y;
93 static int p_mouse_x, p_mouse_y;
96 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
97 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
100 qboolean vidmode_ext = false;
102 static int win_x, win_y;
104 static int scr_width, scr_height;
106 static XF86VidModeModeInfo **vidmodes;
107 static int num_vidmodes;
108 static qboolean vidmode_active = false;
110 static Visual *vidx11_visual;
111 static Colormap vidx11_colormap;
113 /*-----------------------------------------------------------------------*/
115 static int XLateKey(XKeyEvent *ev, char *ascii)
119 KeySym keysym, shifted;
121 keysym = XLookupKeysym (ev, 0);
122 XLookupString(ev, buf, sizeof buf, &shifted, 0);
127 case XK_KP_Page_Up: key = K_KP_PGUP; break;
128 case XK_Page_Up: key = K_PGUP; break;
130 case XK_KP_Page_Down: key = K_KP_PGDN; break;
131 case XK_Page_Down: key = K_PGDN; break;
133 case XK_KP_Home: key = K_KP_HOME; break;
134 case XK_Home: key = K_HOME; break;
136 case XK_KP_End: key = K_KP_END; break;
137 case XK_End: key = K_END; break;
139 case XK_KP_Left: key = K_KP_LEFTARROW; break;
140 case XK_Left: key = K_LEFTARROW; break;
142 case XK_KP_Right: key = K_KP_RIGHTARROW; break;
143 case XK_Right: key = K_RIGHTARROW; break;
145 case XK_KP_Down: key = K_KP_DOWNARROW; break;
146 case XK_Down: key = K_DOWNARROW; break;
148 case XK_KP_Up: key = K_KP_UPARROW; break;
149 case XK_Up: key = K_UPARROW; break;
151 case XK_Escape: key = K_ESCAPE; break;
153 case XK_KP_Enter: key = K_KP_ENTER; break;
154 case XK_Return: key = K_ENTER; break;
156 case XK_Tab: key = K_TAB; break;
158 case XK_F1: key = K_F1; break;
160 case XK_F2: key = K_F2; break;
162 case XK_F3: key = K_F3; break;
164 case XK_F4: key = K_F4; break;
166 case XK_F5: key = K_F5; break;
168 case XK_F6: key = K_F6; break;
170 case XK_F7: key = K_F7; break;
172 case XK_F8: key = K_F8; break;
174 case XK_F9: key = K_F9; break;
176 case XK_F10: key = K_F10; break;
178 case XK_F11: key = K_F11; break;
180 case XK_F12: key = K_F12; break;
182 case XK_BackSpace: key = K_BACKSPACE; break;
184 case XK_KP_Delete: key = K_KP_DEL; break;
185 case XK_Delete: key = K_DEL; break;
187 case XK_Pause: key = K_PAUSE; break;
190 case XK_Shift_R: key = K_SHIFT; break;
194 case XK_Control_R: key = K_CTRL; break;
199 case XK_Meta_R: key = K_ALT; break;
201 case XK_KP_Begin: key = K_KP_5; break;
203 case XK_Insert:key = K_INS; break;
204 case XK_KP_Insert: key = K_KP_INS; break;
206 case XK_KP_Multiply: key = '*'; break;
207 case XK_KP_Add: key = K_KP_PLUS; break;
208 case XK_KP_Subtract: key = K_KP_MINUS; break;
209 case XK_KP_Divide: key = K_KP_SLASH; break;
212 if (keysym < 32 && keysym > 126)
215 if (keysym >= 'A' && keysym <= 'Z')
216 key = keysym - 'A' + 'a';
226 static Cursor CreateNullCursor(Display *display, Window root)
234 cursormask = XCreatePixmap(display, root, 1, 1, 1);
235 xgc.function = GXclear;
236 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
237 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
238 dummycolour.pixel = 0;
240 dummycolour.flags = 04;
241 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
242 XFreePixmap(display,cursormask);
247 static void install_grabs(void)
249 XWindowAttributes attribs_1;
250 XSetWindowAttributes attribs_2;
252 XGetWindowAttributes(vidx11_display, win, &attribs_1);
253 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
254 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
257 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
259 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
264 int MajorVersion, MinorVersion;
266 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
268 // unable to query, probalby not supported
269 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
275 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
276 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
281 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, scr_width / 2, scr_height / 2);
283 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
286 mouse_x = mouse_y = 0;
289 static void uninstall_grabs(void)
291 if (!vidx11_display || !win)
295 if (vid_dga.integer == 1)
296 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
299 XUngrabPointer(vidx11_display, CurrentTime);
300 XUngrabKeyboard(vidx11_display, CurrentTime);
303 XUndefineCursor(vidx11_display, win);
305 mouse_active = false;
308 static void HandleEvents(void)
313 qboolean dowarp = false;
318 while (XPending(vidx11_display))
320 XNextEvent(vidx11_display, &event);
326 key = XLateKey (&event.xkey, &ascii);
327 Key_Event(key, ascii, true);
332 key = XLateKey (&event.xkey, &ascii);
333 Key_Event(key, ascii, false);
341 if (vid_dga.integer == 1)
343 mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
344 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, 0, true);
373 Key_Event(K_MOUSE3, 0, true);
376 Key_Event(K_MOUSE2, 0, true);
379 Key_Event(K_MWHEELUP, 0, true);
382 Key_Event(K_MWHEELDOWN, 0, true);
385 Key_Event(K_MOUSE4, 0, true);
388 Key_Event(K_MOUSE5, 0, true);
391 Key_Event(K_MOUSE6, 0, true);
394 Key_Event(K_MOUSE7, 0, true);
397 Key_Event(K_MOUSE8, 0, true);
400 Key_Event(K_MOUSE9, 0, true);
403 Key_Event(K_MOUSE10, 0, true);
406 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-12 expected\n", event.xbutton.button);
412 // mouse button released
413 switch(event.xbutton.button)
416 Key_Event(K_MOUSE1, 0, false);
419 Key_Event(K_MOUSE3, 0, false);
422 Key_Event(K_MOUSE2, 0, false);
425 Key_Event(K_MWHEELUP, 0, false);
428 Key_Event(K_MWHEELDOWN, 0, false);
431 Key_Event(K_MOUSE4, 0, false);
434 Key_Event(K_MOUSE5, 0, false);
437 Key_Event(K_MOUSE6, 0, false);
440 Key_Event(K_MOUSE7, 0, false);
443 Key_Event(K_MOUSE8, 0, false);
446 Key_Event(K_MOUSE9, 0, false);
449 Key_Event(K_MOUSE10, 0, false);
452 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-12 expected\n", event.xbutton.button);
459 win_x = event.xcreatewindow.x;
460 win_y = event.xcreatewindow.y;
463 case ConfigureNotify:
464 // window changed size/location
465 win_x = event.xconfigure.x;
466 win_y = event.xconfigure.y;
469 // window has been destroyed
473 // window manager messages
474 if ((event.xclient.format == 32) && ((unsigned int)event.xclient.data.l[0] == wm_delete_window_atom))
480 vid_activewindow = false;
481 VID_RestoreSystemGamma();
484 // window iconified/rolledup/whatever
486 vid_activewindow = false;
487 VID_RestoreSystemGamma();
490 // window is now the input focus
491 vid_activewindow = true;
494 // window is no longer the input focus
495 vid_activewindow = false;
496 VID_RestoreSystemGamma();
499 // mouse entered window
509 /* move the mouse to the window center again */
510 p_mouse_x = scr_width / 2;
511 p_mouse_y = scr_height / 2;
512 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
517 static void IN_DeactivateMouse( void )
519 if (!mouse_avail || !vidx11_display || !win)
525 mouse_active = false;
529 static void IN_ActivateMouse( void )
531 if (!mouse_avail || !vidx11_display || !win)
536 mouse_x = mouse_y = 0; // don't spazz
542 void VID_Shutdown(void)
544 if (!ctx || !vidx11_display)
551 VID_RestoreSystemGamma();
554 // FIXME: glXDestroyContext here?
556 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[0]);
558 XDestroyWindow(vidx11_display, win);
559 XCloseDisplay(vidx11_display);
561 vidmode_active = false;
562 vidx11_display = NULL;
570 void signal_handler(int sig)
572 printf("Received signal %d, exiting...\n", sig);
573 VID_RestoreSystemGamma();
580 signal(SIGHUP, signal_handler);
581 signal(SIGINT, signal_handler);
582 signal(SIGQUIT, signal_handler);
583 signal(SIGILL, signal_handler);
584 signal(SIGTRAP, signal_handler);
585 signal(SIGIOT, signal_handler);
586 signal(SIGBUS, signal_handler);
587 signal(SIGFPE, signal_handler);
588 signal(SIGSEGV, signal_handler);
589 signal(SIGTERM, signal_handler);
597 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
601 *height = scr_height;
604 void VID_Finish (void)
607 if (r_render.integer)
610 qglXSwapBuffers(vidx11_display, win);
613 // handle the mouse state when windowed if that's changed
615 if (vid_mouse.integer && !key_consoleactive)
632 IN_DeactivateMouse ();
637 int VID_SetGamma(unsigned short *ramps)
639 return XF86VidModeSetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
642 int VID_GetGamma(unsigned short *ramps)
644 return XF86VidModeGetGammaRamp(vidx11_display, vidx11_screen, 256, ramps, ramps + 256, ramps + 512);
650 Cvar_RegisterVariable (&vid_dga);
651 Cvar_RegisterVariable (&vid_dga_mouseaccel);
653 InitSig(); // trap evil signals
654 if (COM_CheckParm ("-nomouse") || COM_CheckParm("-safe"))
658 void VID_BuildGLXAttrib(int *attrib, int stencil)
660 *attrib++ = GLX_RGBA;
661 *attrib++ = GLX_RED_SIZE;*attrib++ = 1;
662 *attrib++ = GLX_GREEN_SIZE;*attrib++ = 1;
663 *attrib++ = GLX_BLUE_SIZE;*attrib++ = 1;
664 *attrib++ = GLX_DOUBLEBUFFER;
665 *attrib++ = GLX_DEPTH_SIZE;*attrib++ = 1;
666 // if stencil is enabled, ask for alpha too
669 *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
670 *attrib++ = GLX_ALPHA_SIZE;*attrib++ = 1;
675 int VID_InitMode(int fullscreen, int width, int height, int bpp)
679 XSetWindowAttributes attr;
682 XVisualInfo *visinfo;
683 int MajorVersion, MinorVersion;
684 const char *drivername;
686 drivername = "libGL.so.1";
687 i = COM_CheckParm("-gl_driver");
688 if (i && i < com_argc - 1)
689 drivername = com_argv[i + 1];
690 if (!GL_OpenLibrary(drivername))
692 Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
696 if (!(vidx11_display = XOpenDisplay(NULL)))
698 Con_Printf("Couldn't open the X display\n");
702 vidx11_screen = DefaultScreen(vidx11_display);
703 root = RootWindow(vidx11_display, vidx11_screen);
705 // Get video mode list
706 MajorVersion = MinorVersion = 0;
707 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
711 Con_DPrintf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
715 if ((qglXChooseVisual = GL_GetProcAddress("glXChooseVisual")) == NULL
716 || (qglXCreateContext = GL_GetProcAddress("glXCreateContext")) == NULL
717 || (qglXMakeCurrent = GL_GetProcAddress("glXMakeCurrent")) == NULL
718 || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL
719 || (qglXQueryExtensionsString = GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
721 Con_Printf("glX functions not found in %s\n", gl_driver);
725 VID_BuildGLXAttrib(attrib, bpp == 32);
726 visinfo = qglXChooseVisual(vidx11_display, vidx11_screen, attrib);
729 Con_Printf("Couldn't get an RGB, Double-buffered, Depth visual\n");
735 int best_fit, best_dist, dist, x, y;
737 // Are we going fullscreen? If so, let's change video mode
740 XF86VidModeGetAllModeLines(vidx11_display, vidx11_screen, &num_vidmodes, &vidmodes);
744 for (i = 0; i < num_vidmodes; i++)
746 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
749 x = width - vidmodes[i]->hdisplay;
750 y = height - vidmodes[i]->vdisplay;
751 dist = (x * x) + (y * y);
752 if (dist < best_dist)
761 // LordHavoc: changed from ActualWidth/ActualHeight =,
762 // to width/height =, so the window will take the full area of
764 width = vidmodes[best_fit]->hdisplay;
765 height = vidmodes[best_fit]->vdisplay;
767 // change to the mode
768 XF86VidModeSwitchToMode(vidx11_display, vidx11_screen, vidmodes[best_fit]);
769 vidmode_active = true;
771 // Move the viewport to top left
772 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
779 // LordHavoc: save the visual for use in gamma ramp settings later
780 vidx11_visual = visinfo->visual;
782 /* window attributes */
783 attr.background_pixel = 0;
784 attr.border_pixel = 0;
785 // LordHavoc: save the colormap for later, too
786 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
787 attr.event_mask = X_MASK;
790 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
791 attr.override_redirect = True;
792 attr.backing_store = NotUseful;
793 attr.save_under = False;
796 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
798 win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
799 XStoreName(vidx11_display, win, gamename);
800 XMapWindow(vidx11_display, win);
802 // LordHavoc: making the close button on a window do the right thing
803 // seems to involve this mess, sigh...
804 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
805 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
809 XMoveWindow(vidx11_display, win, 0, 0);
810 XRaiseWindow(vidx11_display, win);
811 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
812 XFlush(vidx11_display);
813 // Move the viewport to top left
814 XF86VidModeSetViewPort(vidx11_display, vidx11_screen, 0, 0);
817 //XSync(vidx11_display, False);
819 ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
821 Sys_Error ("glXCreateContext failed\n");
823 if (!qglXMakeCurrent(vidx11_display, win, ctx))
824 Sys_Error ("glXMakeCurrent failed\n");
826 XSync(vidx11_display, False);
831 if ((qglGetString = GL_GetProcAddress("glGetString")) == NULL)
832 Sys_Error("glGetString not found in %s", gl_driver);
834 gl_renderer = qglGetString(GL_RENDERER);
835 gl_vendor = qglGetString(GL_VENDOR);
836 gl_version = qglGetString(GL_VERSION);
837 gl_extensions = qglGetString(GL_EXTENSIONS);
839 gl_platformextensions = qglXQueryExtensionsString(vidx11_display, vidx11_screen);
841 GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
842 gl_videosyncavailable = GL_CheckExtension("GLX_SGI_video_sync", videosyncfuncs, "-novideosync", false);
846 vid_activewindow = true;
851 void Sys_SendKeyEvents(void)
861 void IN_Commands (void)
865 void IN_Move (usercmd_t *cmd)
868 IN_Mouse(cmd, mouse_x, mouse_y);
873 static void *prjobj = NULL;
875 int GL_OpenLibrary(const char *name)
877 Con_Printf("Loading OpenGL driver %s\n", name);
879 if (!(prjobj = dlopen(name, RTLD_LAZY)))
881 Con_Printf("Unable to open symbol list for %s\n", name);
884 strcpy(gl_driver, name);
888 void GL_CloseLibrary(void)
894 qglXGetProcAddressARB = NULL;
897 gl_platformextensions = "";
900 void *GL_GetProcAddress(const char *name)
903 if (qglXGetProcAddressARB != NULL)
904 p = (void *) qglXGetProcAddressARB(name);
906 p = (void *) dlsym(prjobj, name);