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);
484 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
488 *height = scr_height;
491 void VID_Finish (void)
494 if (r_render.integer)
497 glXSwapBuffers(vidx11_display, win);
500 // handle the mouse state when windowed if that's changed
502 if (vid_mouse.integer && key_dest == key_game)
519 IN_DeactivateMouse ();
524 // LordHavoc: ported from SDL 1.2.2, this was far more difficult to port from
525 // SDL than to simply use the XFree gamma ramp extension, but that affects the
526 // whole screen even when the game window is inactive, this only affects the
527 // screen while the window is active, very desirable behavior :)
528 int VID_SetGamma(float prescale, float gamma, float scale, float base)
530 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
535 unsigned int Rmask, Gmask, Bmask, Rloss, Gloss, Bloss, Rshift, Gshift, Bshift, mask;
537 unsigned short ramp[256];
539 if (COM_CheckParm("-nogamma"))
542 if (vidx11_visual->class != DirectColor)
544 Con_Printf("X11 Visual class is %d, can only do gamma on %d\n", vidx11_visual->class, DirectColor);
548 Rmask = vidx11_visual->red_mask;
549 Gmask = vidx11_visual->green_mask;
550 Bmask = vidx11_visual->blue_mask;
556 for (;!(mask & 1);mask >>= 1)
558 for (;(mask & 1);mask >>= 1)
565 for (;!(mask & 1);mask >>= 1)
567 for (;(mask & 1);mask >>= 1)
574 for (;!(mask & 1);mask >>= 1)
576 for (;(mask & 1);mask >>= 1)
580 BuildGammaTable16(prescale, gamma, scale, base, ramp);
582 // convert gamma ramp to palette (yes this seems odd)
583 ncolors = vidx11_visual->map_entries;
584 for (i = 0;i < ncolors;i++)
586 c = (256 * i / ncolors);
587 xcmap[i].pixel = ((c >> Rloss) << Rshift) | ((c >> Gloss) << Gshift) | ((c >> Bloss) << Bshift);
588 xcmap[i].red = ramp[c];
589 xcmap[i].green = ramp[c];
590 xcmap[i].blue = ramp[c];
591 xcmap[i].flags = (DoRed|DoGreen|DoBlue);
593 XStoreColors(vidx11_display, vidx11_colormap, xcmap, ncolors);
594 XSync(vidx11_display, false);
595 // FIXME: should this check for BadAccess/BadColor/BadValue errors produced by XStoreColors before setting this true?
603 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
613 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
617 int nogammaattrib[] =
627 // char gldir[MAX_OSPATH];
628 int width = 640, height = 480;
629 XSetWindowAttributes attr;
632 XVisualInfo *visinfo;
633 qboolean fullscreen = true;
634 int MajorVersion, MinorVersion;
636 Cvar_RegisterVariable (&vid_dga);
637 Cvar_RegisterVariable (&vid_dga_mouseaccel);
638 Cvar_RegisterVariable (&m_filter);
640 // interpret command-line params
642 // set vid parameters
643 if ((i = COM_CheckParm("-window")) != 0)
646 if ((i = COM_CheckParm("-width")) != 0)
647 width = atoi(com_argv[i+1]);
649 if ((i = COM_CheckParm("-height")) != 0)
650 height = atoi(com_argv[i+1]);
652 if ((i = COM_CheckParm("-conwidth")) != 0)
653 vid.conwidth = atoi(com_argv[i+1]);
657 vid.conwidth &= 0xfff8; // make it a multiple of eight
659 if (vid.conwidth < 320)
662 // pick a conheight that matches with correct aspect
663 vid.conheight = vid.conwidth*3 / 4;
665 if ((i = COM_CheckParm("-conheight")) != 0)
666 vid.conheight = atoi(com_argv[i+1]);
667 if (vid.conheight < 200)
670 if (!(vidx11_display = XOpenDisplay(NULL)))
672 fprintf(stderr, "Error couldn't open the X display\n");
676 scrnum = DefaultScreen(vidx11_display);
677 root = RootWindow(vidx11_display, scrnum);
679 // Get video mode list
680 MajorVersion = MinorVersion = 0;
681 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
685 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
690 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
692 if (!COM_CheckParm("-nogamma"))
693 visinfo = glXChooseVisual(vidx11_display, scrnum, gammaattrib);
697 visinfo = glXChooseVisual(vidx11_display, scrnum, nogammaattrib);
700 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
707 int best_fit, best_dist, dist, x, y;
709 XF86VidModeGetAllModeLines(vidx11_display, scrnum, &num_vidmodes, &vidmodes);
711 // Are we going fullscreen? If so, let's change video mode
717 for (i = 0; i < num_vidmodes; i++)
719 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
722 x = width - vidmodes[i]->hdisplay;
723 y = height - vidmodes[i]->vdisplay;
724 dist = (x * x) + (y * y);
725 if (dist < best_dist)
734 // LordHavoc: changed from ActualWidth/ActualHeight =,
735 // to width/height =, so the window will take the full area of
737 width = vidmodes[best_fit]->hdisplay;
738 height = vidmodes[best_fit]->vdisplay;
740 // change to the mode
741 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[best_fit]);
742 vidmode_active = true;
744 // Move the viewport to top left
745 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
752 // LordHavoc: save the visual for use in gamma ramp settings later
753 vidx11_visual = visinfo->visual;
755 /* window attributes */
756 attr.background_pixel = 0;
757 attr.border_pixel = 0;
758 // LordHavoc: save the colormap for later, too
759 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
760 attr.event_mask = X_MASK;
763 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
764 attr.override_redirect = True;
765 attr.backing_store = NotUseful;
766 attr.save_under = False;
769 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
771 win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
772 XStoreName(vidx11_display, win, gamename);
773 XMapWindow(vidx11_display, win);
777 XMoveWindow(vidx11_display, win, 0, 0);
778 XRaiseWindow(vidx11_display, win);
779 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
780 XFlush(vidx11_display);
781 // Move the viewport to top left
782 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
785 XFlush(vidx11_display);
787 ctx = glXCreateContext(vidx11_display, visinfo, NULL, True);
789 glXMakeCurrent(vidx11_display, win, ctx);
794 if (vid.conheight > height)
795 vid.conheight = height;
796 if (vid.conwidth > width)
797 vid.conwidth = width;
799 InitSig(); // trap evil signals
803 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
805 // force a surface cache flush
806 // vid.recalc_refdef = 1;
809 void Sys_SendKeyEvents(void)
816 if (COM_CheckParm ("-nomouse"))
820 void IN_Shutdown(void)
829 void IN_Commands (void)
838 void IN_MouseMove (usercmd_t *cmd)
843 if (m_filter.integer)
845 mouse_x = (mouse_x + old_mouse_x) * 0.5;
846 mouse_y = (mouse_y + old_mouse_y) * 0.5;
848 old_mouse_x = mouse_x;
849 old_mouse_y = mouse_y;
852 // LordHavoc: viewzoom affects mouse sensitivity for sniping
853 mouse_x *= sensitivity.value * cl.viewzoom;
854 mouse_y *= sensitivity.value * cl.viewzoom;
856 if (in_strafe.state & 1)
857 cmd->sidemove += m_side.value * mouse_x;
859 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
864 if (/*freelook && */!(in_strafe.state & 1))
866 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
867 cl.viewangles[PITCH] = bound (-90, cl.viewangles[PITCH], 90);
871 if ((in_strafe.state & 1) && noclip_anglehack)
872 cmd->upmove -= m_forward.value * mouse_y;
874 cmd->forwardmove -= m_forward.value * mouse_y;
876 mouse_x = mouse_y = 0.0;
879 void IN_Move (usercmd_t *cmd)