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.
21 #include <sys/ioctl.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>
41 static Display *vidx11_display = NULL;
44 static GLXContext ctx = NULL;
46 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
47 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
48 PointerMotionMask | ButtonMotionMask )
49 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | StructureNotifyMask )
52 viddef_t vid; // global video state
54 static qboolean mouse_avail = true;
55 static qboolean mouse_active = false, usingmouse = false;
56 // static qboolean dga_active;
57 static float mouse_x, mouse_y;
58 static float old_mouse_x, old_mouse_y;
59 static int p_mouse_x, p_mouse_y;
61 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
62 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
63 cvar_t m_filter = {0, "m_filter", "0"};
65 qboolean vidmode_ext = false;
67 static int win_x, win_y;
69 static int scr_width, scr_height;
71 static XF86VidModeModeInfo **vidmodes;
72 //static int default_dotclock_vidmode;
73 static int num_vidmodes;
74 static qboolean vidmode_active = false;
76 static Visual *vidx11_visual;
77 static Colormap vidx11_colormap;
79 /*-----------------------------------------------------------------------*/
81 float gldepthmin, gldepthmax;
83 const char *gl_vendor;
84 const char *gl_renderer;
85 const char *gl_version;
86 const char *gl_extensions;
88 /*-----------------------------------------------------------------------*/
90 XLateKey(XKeyEvent *ev/*, qboolean modified*/)
96 keysym = XLookupKeysym(ev, 0);
100 case XK_KP_Page_Up: key = KP_PGUP; break;
101 case XK_Page_Up: key = K_PGUP; break;
103 case XK_KP_Page_Down: key = KP_PGDN; break;
104 case XK_Page_Down: key = K_PGDN; break;
106 case XK_KP_Home: key = KP_HOME; break;
107 case XK_Home: key = K_HOME; break;
109 case XK_KP_End: key = KP_END; break;
110 case XK_End: key = K_END; break;
112 case XK_KP_Left: key = KP_LEFTARROW; break;
113 case XK_Left: key = K_LEFTARROW; break;
115 case XK_KP_Right: key = KP_RIGHTARROW; break;
116 case XK_Right: key = K_RIGHTARROW; break;
118 case XK_KP_Down: key = KP_DOWNARROW; break;
119 case XK_Down: key = K_DOWNARROW; break;
121 case XK_KP_Up: key = KP_UPARROW; break;
122 case XK_Up: key = K_UPARROW; break;
124 case XK_Escape: key = K_ESCAPE; break;
126 case XK_KP_Enter: key = KP_ENTER; break;
127 case XK_Return: key = K_ENTER; break;
129 case XK_Tab: key = K_TAB; break;
131 case XK_F1: key = K_F1; break;
132 case XK_F2: key = K_F2; break;
133 case XK_F3: key = K_F3; break;
134 case XK_F4: key = K_F4; break;
135 case XK_F5: key = K_F5; break;
136 case XK_F6: key = K_F6; break;
137 case XK_F7: key = K_F7; break;
138 case XK_F8: key = K_F8; break;
139 case XK_F9: key = K_F9; break;
140 case XK_F10: key = K_F10; break;
141 case XK_F11: key = K_F11; break;
142 case XK_F12: key = K_F12; break;
144 case XK_BackSpace: key = K_BACKSPACE; break;
146 case XK_KP_Delete: key = KP_DEL; break;
147 case XK_Delete: key = K_DEL; break;
149 case XK_Pause: key = K_PAUSE; break;
152 case XK_Shift_R: key = K_SHIFT; break;
156 case XK_Control_R: key = K_CTRL; break;
162 case XK_Meta_R: key = K_ALT; break;
164 case XK_Caps_Lock: key = K_CAPSLOCK; break;
165 case XK_KP_Begin: key = KP_5; break;
167 case XK_Insert: key = K_INS; break;
168 case XK_KP_Insert: key = KP_INS; break;
170 case XK_KP_Multiply: key = KP_MULTIPLY; break;
171 case XK_KP_Add: key = KP_PLUS; break;
172 case XK_KP_Subtract: key = KP_MINUS; break;
173 case XK_KP_Divide: key = KP_DIVIDE; break;
175 /* For Sun keyboards */
176 case XK_F27: key = K_HOME; break;
177 case XK_F29: key = K_PGUP; break;
178 case XK_F33: key = K_END; break;
179 case XK_F35: key = K_PGDN; break;
186 if (/*!modified && */((key >= 'A') && (key <= 'Z')))
187 key = key + ('a' - 'A');
195 static Cursor CreateNullCursor(Display *display, Window root)
203 cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
204 xgc.function = GXclear;
205 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
206 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
207 dummycolour.pixel = 0;
209 dummycolour.flags = 04;
210 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
211 XFreePixmap(display,cursormask);
216 static void install_grabs(void)
218 XWindowAttributes attribs_1;
219 XSetWindowAttributes attribs_2;
221 XGetWindowAttributes(vidx11_display, win, &attribs_1);
222 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
223 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
226 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
228 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
232 int MajorVersion, MinorVersion;
234 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
236 // unable to query, probalby not supported
237 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
243 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
244 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
248 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, scr_width / 2, scr_height / 2);
250 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
253 mouse_x = mouse_y = 0;
255 // XSync(vidx11_display, True);
258 static void uninstall_grabs(void)
260 if (!vidx11_display || !win)
263 if (vid_dga.integer == 1)
264 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
266 XUngrabPointer(vidx11_display, CurrentTime);
267 XUngrabKeyboard(vidx11_display, CurrentTime);
270 XUndefineCursor(vidx11_display, win);
272 mouse_active = false;
275 static void HandleEvents(void)
279 qboolean dowarp = false;
284 while (XPending(vidx11_display))
286 XNextEvent(vidx11_display, &event);
292 Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
298 if (vid_dga.integer == 1)
300 mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
301 mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
306 if (!p_mouse_x && !p_mouse_y)
308 Con_Printf("event->xmotion.x: %d\n", event.xmotion.x);
309 Con_Printf("event->xmotion.y: %d\n", event.xmotion.y);
314 if (!event.xmotion.send_event)
316 mouse_x += event.xmotion.x - p_mouse_x;
317 mouse_y += event.xmotion.y - p_mouse_y;
318 if (abs(scr_width/2 - event.xmotion.x) > scr_width / 4 || abs(scr_height/2 - event.xmotion.y) > scr_height / 4)
325 mouse_x += (event.xmotion.x - p_mouse_x);
326 mouse_y += (event.xmotion.y - p_mouse_y);
329 p_mouse_x = event.xmotion.x;
330 p_mouse_y = event.xmotion.y;
334 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
338 switch(event.xbutton.button)
341 Key_Event(K_MOUSE1, true);
344 Key_Event(K_MOUSE3, true);
347 Key_Event(K_MOUSE2, true);
350 Key_Event(K_MWHEELUP, true);
353 Key_Event(K_MWHEELDOWN, true);
356 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-5 expected\n", event.xbutton.button);
362 switch(event.xbutton.button)
365 Key_Event(K_MOUSE1, false);
368 Key_Event(K_MOUSE3, false);
371 Key_Event(K_MOUSE2, false);
374 Key_Event(K_MWHEELUP, false);
377 Key_Event(K_MWHEELDOWN, false);
380 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-5 expected\n", event.xbutton.button);
386 win_x = event.xcreatewindow.x;
387 win_y = event.xcreatewindow.y;
390 case ConfigureNotify :
391 win_x = event.xconfigure.x;
392 win_y = event.xconfigure.y;
399 /* move the mouse to the window center again */
400 p_mouse_x = scr_width / 2;
401 p_mouse_y = scr_height / 2;
402 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
407 static void IN_DeactivateMouse( void )
409 if (!mouse_avail || !vidx11_display || !win)
415 mouse_active = false;
419 static void IN_ActivateMouse( void )
421 if (!mouse_avail || !vidx11_display || !win)
426 mouse_x = mouse_y = 0; // don't spazz
433 void VID_Shutdown(void)
435 if (!ctx || !vidx11_display)
443 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[0]);
444 /* Disabled, causes a segfault during shutdown.
446 glXDestroyContext(vidx11_display, ctx);
449 XDestroyWindow(vidx11_display, win);
450 XCloseDisplay(vidx11_display);
452 vidmode_active = false;
453 vidx11_display = NULL;
458 void signal_handler(int sig)
460 printf("Received signal %d, exiting...\n", sig);
467 signal(SIGHUP, signal_handler);
468 signal(SIGINT, signal_handler);
469 signal(SIGQUIT, signal_handler);
470 signal(SIGILL, signal_handler);
471 signal(SIGTRAP, signal_handler);
472 signal(SIGIOT, signal_handler);
473 signal(SIGBUS, signal_handler);
474 signal(SIGFPE, signal_handler);
475 signal(SIGSEGV, signal_handler);
476 signal(SIGTERM, signal_handler);
485 void GL_BeginRendering (int *x, int *y, int *width, int *height)
489 *height = scr_height;
491 // glViewport (*x, *y, *width, *height);
495 void GL_EndRendering (void)
498 if (!r_render.integer)
501 glXSwapBuffers(vidx11_display, win);
503 // handle the mouse state when windowed if that's changed
505 if (vid_mouse.integer && key_dest == key_game)
522 IN_DeactivateMouse ();
527 // LordHavoc: ported from SDL 1.2.2, this was far more difficult to port from
528 // SDL than to simply use the XFree gamma ramp extension, but that affects the
529 // whole screen even when the game window is inactive, this only affects the
530 // screen while the window is active, very desirable behavior :)
531 int VID_SetGamma(float prescale, float gamma, float scale, float base)
533 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
538 unsigned int Rmask, Gmask, Bmask, Rloss, Gloss, Bloss, Rshift, Gshift, Bshift, mask;
540 unsigned short ramp[256];
542 if (COM_CheckParm("-nogamma"))
545 if (vidx11_visual->class != DirectColor)
547 Con_Printf("X11 Visual class is %d, can only do gamma on %d\n", vidx11_visual->class, DirectColor);
551 Rmask = vidx11_visual->red_mask;
552 Gmask = vidx11_visual->green_mask;
553 Bmask = vidx11_visual->blue_mask;
559 for (;!(mask & 1);mask >>= 1)
561 for (;(mask & 1);mask >>= 1)
568 for (;!(mask & 1);mask >>= 1)
570 for (;(mask & 1);mask >>= 1)
577 for (;!(mask & 1);mask >>= 1)
579 for (;(mask & 1);mask >>= 1)
583 BuildGammaTable16(prescale, gamma, scale, base, ramp);
585 // convert gamma ramp to palette (yes this seems odd)
586 ncolors = vidx11_visual->map_entries;
587 for (i = 0;i < ncolors;i++)
589 c = (256 * i / ncolors);
590 xcmap[i].pixel = ((c >> Rloss) << Rshift) | ((c >> Gloss) << Gshift) | ((c >> Bloss) << Bshift);
591 xcmap[i].red = ramp[c];
592 xcmap[i].green = ramp[c];
593 xcmap[i].blue = ramp[c];
594 xcmap[i].flags = (DoRed|DoGreen|DoBlue);
596 XStoreColors(vidx11_display, vidx11_colormap, xcmap, ncolors);
597 XSync(vidx11_display, false);
598 // FIXME: should this check for BadAccess/BadColor/BadValue errors produced by XStoreColors before setting this true?
606 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
616 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
620 int nogammaattrib[] =
630 // char gldir[MAX_OSPATH];
631 int width = 640, height = 480;
632 XSetWindowAttributes attr;
635 XVisualInfo *visinfo;
636 qboolean fullscreen = true;
637 int MajorVersion, MinorVersion;
639 Cvar_RegisterVariable (&vid_dga);
640 Cvar_RegisterVariable (&vid_dga_mouseaccel);
641 Cvar_RegisterVariable (&m_filter);
643 // interpret command-line params
645 // set vid parameters
646 if ((i = COM_CheckParm("-window")) != 0)
649 if ((i = COM_CheckParm("-width")) != 0)
650 width = atoi(com_argv[i+1]);
652 if ((i = COM_CheckParm("-height")) != 0)
653 height = atoi(com_argv[i+1]);
655 if ((i = COM_CheckParm("-conwidth")) != 0)
656 vid.conwidth = atoi(com_argv[i+1]);
660 vid.conwidth &= 0xfff8; // make it a multiple of eight
662 if (vid.conwidth < 320)
665 // pick a conheight that matches with correct aspect
666 vid.conheight = vid.conwidth*3 / 4;
668 if ((i = COM_CheckParm("-conheight")) != 0)
669 vid.conheight = atoi(com_argv[i+1]);
670 if (vid.conheight < 200)
673 if (!(vidx11_display = XOpenDisplay(NULL)))
675 fprintf(stderr, "Error couldn't open the X display\n");
679 scrnum = DefaultScreen(vidx11_display);
680 root = RootWindow(vidx11_display, scrnum);
682 // Get video mode list
683 MajorVersion = MinorVersion = 0;
684 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
688 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
693 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
695 if (!COM_CheckParm("-nogamma"))
696 visinfo = glXChooseVisual(vidx11_display, scrnum, gammaattrib);
700 visinfo = glXChooseVisual(vidx11_display, scrnum, nogammaattrib);
703 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
710 int best_fit, best_dist, dist, x, y;
712 XF86VidModeGetAllModeLines(vidx11_display, scrnum, &num_vidmodes, &vidmodes);
714 // Are we going fullscreen? If so, let's change video mode
720 for (i = 0; i < num_vidmodes; i++)
722 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
725 x = width - vidmodes[i]->hdisplay;
726 y = height - vidmodes[i]->vdisplay;
727 dist = (x * x) + (y * y);
728 if (dist < best_dist)
737 // LordHavoc: changed from ActualWidth/ActualHeight =,
738 // to width/height =, so the window will take the full area of
740 width = vidmodes[best_fit]->hdisplay;
741 height = vidmodes[best_fit]->vdisplay;
743 // change to the mode
744 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[best_fit]);
745 vidmode_active = true;
747 // Move the viewport to top left
748 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
755 // LordHavoc: save the visual for use in gamma ramp settings later
756 vidx11_visual = visinfo->visual;
758 /* window attributes */
759 attr.background_pixel = 0;
760 attr.border_pixel = 0;
761 // LordHavoc: save the colormap for later, too
762 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
763 attr.event_mask = X_MASK;
766 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
767 attr.override_redirect = True;
768 attr.backing_store = NotUseful;
769 attr.save_under = False;
772 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
774 win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
775 XStoreName(vidx11_display, win, gamename);
776 XMapWindow(vidx11_display, win);
780 XMoveWindow(vidx11_display, win, 0, 0);
781 XRaiseWindow(vidx11_display, win);
782 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
783 XFlush(vidx11_display);
784 // Move the viewport to top left
785 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
788 XFlush(vidx11_display);
790 ctx = glXCreateContext(vidx11_display, visinfo, NULL, True);
792 glXMakeCurrent(vidx11_display, win, ctx);
797 if (vid.conheight > height)
798 vid.conheight = height;
799 if (vid.conwidth > width)
800 vid.conwidth = width;
802 InitSig(); // trap evil signals
806 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
808 // force a surface cache flush
809 // vid.recalc_refdef = 1;
812 void Sys_SendKeyEvents(void)
819 if (COM_CheckParm ("-nomouse"))
823 void IN_Shutdown(void)
832 void IN_Commands (void)
841 void IN_MouseMove (usercmd_t *cmd)
846 if (m_filter.integer)
848 mouse_x = (mouse_x + old_mouse_x) * 0.5;
849 mouse_y = (mouse_y + old_mouse_y) * 0.5;
851 old_mouse_x = mouse_x;
852 old_mouse_y = mouse_y;
855 mouse_x *= sensitivity.value;
856 mouse_y *= sensitivity.value;
858 if (in_strafe.state & 1)
859 cmd->sidemove += m_side.value * mouse_x;
861 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
866 if (/*freelook && */!(in_strafe.state & 1))
868 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
869 cl.viewangles[PITCH] = bound (-90, cl.viewangles[PITCH], 90);
873 if ((in_strafe.state & 1) && noclip_anglehack)
874 cmd->upmove -= m_forward.value * mouse_y;
876 cmd->forwardmove -= m_forward.value * mouse_y;
878 mouse_x = mouse_y = 0.0;
881 void IN_Move (usercmd_t *cmd)