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 int p_mouse_x, p_mouse_y;
60 cvar_t vid_dga = {CVAR_SAVE, "vid_dga", "1"};
61 cvar_t vid_dga_mouseaccel = {0, "vid_dga_mouseaccel", "1"};
63 qboolean vidmode_ext = false;
65 static int win_x, win_y;
67 static int scr_width, scr_height;
69 static XF86VidModeModeInfo **vidmodes;
70 //static int default_dotclock_vidmode;
71 static int num_vidmodes;
72 static qboolean vidmode_active = false;
74 static Visual *vidx11_visual;
75 static Colormap vidx11_colormap;
77 /*-----------------------------------------------------------------------*/
79 float gldepthmin, gldepthmax;
81 const char *gl_vendor;
82 const char *gl_renderer;
83 const char *gl_version;
84 const char *gl_extensions;
86 /*-----------------------------------------------------------------------*/
88 XLateKey(XKeyEvent *ev/*, qboolean modified*/)
94 keysym = XLookupKeysym(ev, 0);
98 case XK_KP_Page_Up: key = KP_PGUP; break;
99 case XK_Page_Up: key = K_PGUP; break;
101 case XK_KP_Page_Down: key = KP_PGDN; break;
102 case XK_Page_Down: key = K_PGDN; break;
104 case XK_KP_Home: key = KP_HOME; break;
105 case XK_Home: key = K_HOME; break;
107 case XK_KP_End: key = KP_END; break;
108 case XK_End: key = K_END; break;
110 case XK_KP_Left: key = KP_LEFTARROW; break;
111 case XK_Left: key = K_LEFTARROW; break;
113 case XK_KP_Right: key = KP_RIGHTARROW; break;
114 case XK_Right: key = K_RIGHTARROW; break;
116 case XK_KP_Down: key = KP_DOWNARROW; break;
117 case XK_Down: key = K_DOWNARROW; break;
119 case XK_KP_Up: key = KP_UPARROW; break;
120 case XK_Up: key = K_UPARROW; break;
122 case XK_Escape: key = K_ESCAPE; break;
124 case XK_KP_Enter: key = KP_ENTER; break;
125 case XK_Return: key = K_ENTER; break;
127 case XK_Tab: key = K_TAB; break;
129 case XK_F1: key = K_F1; break;
130 case XK_F2: key = K_F2; break;
131 case XK_F3: key = K_F3; break;
132 case XK_F4: key = K_F4; break;
133 case XK_F5: key = K_F5; break;
134 case XK_F6: key = K_F6; break;
135 case XK_F7: key = K_F7; break;
136 case XK_F8: key = K_F8; break;
137 case XK_F9: key = K_F9; break;
138 case XK_F10: key = K_F10; break;
139 case XK_F11: key = K_F11; break;
140 case XK_F12: key = K_F12; break;
142 case XK_BackSpace: key = K_BACKSPACE; break;
144 case XK_KP_Delete: key = KP_DEL; break;
145 case XK_Delete: key = K_DEL; break;
147 case XK_Pause: key = K_PAUSE; break;
150 case XK_Shift_R: key = K_SHIFT; break;
154 case XK_Control_R: key = K_CTRL; break;
160 case XK_Meta_R: key = K_ALT; break;
162 case XK_Caps_Lock: key = K_CAPSLOCK; break;
163 case XK_KP_Begin: key = KP_5; break;
165 case XK_Insert: key = K_INS; break;
166 case XK_KP_Insert: key = KP_INS; break;
168 case XK_KP_Multiply: key = KP_MULTIPLY; break;
169 case XK_KP_Add: key = KP_PLUS; break;
170 case XK_KP_Subtract: key = KP_MINUS; break;
171 case XK_KP_Divide: key = KP_DIVIDE; break;
173 /* For Sun keyboards */
174 case XK_F27: key = K_HOME; break;
175 case XK_F29: key = K_PGUP; break;
176 case XK_F33: key = K_END; break;
177 case XK_F35: key = K_PGDN; break;
184 if (/*!modified && */((key >= 'A') && (key <= 'Z')))
185 key = key + ('a' - 'A');
193 static Cursor CreateNullCursor(Display *display, Window root)
201 cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
202 xgc.function = GXclear;
203 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
204 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
205 dummycolour.pixel = 0;
207 dummycolour.flags = 04;
208 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
209 XFreePixmap(display,cursormask);
214 static void install_grabs(void)
216 XWindowAttributes attribs_1;
217 XSetWindowAttributes attribs_2;
219 XGetWindowAttributes(vidx11_display, win, &attribs_1);
220 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
221 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
224 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
226 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
230 int MajorVersion, MinorVersion;
232 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
234 // unable to query, probalby not supported
235 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
241 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
242 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
246 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, scr_width / 2, scr_height / 2);
248 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
251 mouse_x = mouse_y = 0;
253 // XSync(vidx11_display, True);
256 static void uninstall_grabs(void)
258 if (!vidx11_display || !win)
261 if (vid_dga.integer == 1)
262 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
264 XUngrabPointer(vidx11_display, CurrentTime);
265 XUngrabKeyboard(vidx11_display, CurrentTime);
268 XUndefineCursor(vidx11_display, win);
270 mouse_active = false;
273 static void HandleEvents(void)
277 qboolean dowarp = false;
282 while (XPending(vidx11_display))
284 XNextEvent(vidx11_display, &event);
290 Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
296 if (vid_dga.integer == 1)
298 mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
299 mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
304 if (!p_mouse_x && !p_mouse_y)
306 Con_Printf("event->xmotion.x: %d\n", event.xmotion.x);
307 Con_Printf("event->xmotion.y: %d\n", event.xmotion.y);
312 if (!event.xmotion.send_event)
314 mouse_x += event.xmotion.x - p_mouse_x;
315 mouse_y += event.xmotion.y - p_mouse_y;
316 if (abs(scr_width/2 - event.xmotion.x) > scr_width / 4 || abs(scr_height/2 - event.xmotion.y) > scr_height / 4)
323 mouse_x += (event.xmotion.x - p_mouse_x);
324 mouse_y += (event.xmotion.y - p_mouse_y);
327 p_mouse_x = event.xmotion.x;
328 p_mouse_y = event.xmotion.y;
332 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
336 switch(event.xbutton.button)
339 Key_Event(K_MOUSE1, true);
342 Key_Event(K_MOUSE3, true);
345 Key_Event(K_MOUSE2, true);
348 Key_Event(K_MWHEELUP, true);
351 Key_Event(K_MWHEELDOWN, true);
354 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-5 expected\n", event.xbutton.button);
360 switch(event.xbutton.button)
363 Key_Event(K_MOUSE1, false);
366 Key_Event(K_MOUSE3, false);
369 Key_Event(K_MOUSE2, false);
372 Key_Event(K_MWHEELUP, false);
375 Key_Event(K_MWHEELDOWN, false);
378 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-5 expected\n", event.xbutton.button);
384 win_x = event.xcreatewindow.x;
385 win_y = event.xcreatewindow.y;
388 case ConfigureNotify :
389 win_x = event.xconfigure.x;
390 win_y = event.xconfigure.y;
397 /* move the mouse to the window center again */
398 p_mouse_x = scr_width / 2;
399 p_mouse_y = scr_height / 2;
400 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
405 static void IN_DeactivateMouse( void )
407 if (!mouse_avail || !vidx11_display || !win)
413 mouse_active = false;
417 static void IN_ActivateMouse( void )
419 if (!mouse_avail || !vidx11_display || !win)
424 mouse_x = mouse_y = 0; // don't spazz
431 void VID_Shutdown(void)
433 if (!ctx || !vidx11_display)
441 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[0]);
442 /* Disabled, causes a segfault during shutdown.
444 glXDestroyContext(vidx11_display, ctx);
447 XDestroyWindow(vidx11_display, win);
448 XCloseDisplay(vidx11_display);
450 vidmode_active = false;
451 vidx11_display = NULL;
456 void signal_handler(int sig)
458 printf("Received signal %d, exiting...\n", sig);
465 signal(SIGHUP, signal_handler);
466 signal(SIGINT, signal_handler);
467 signal(SIGQUIT, signal_handler);
468 signal(SIGILL, signal_handler);
469 signal(SIGTRAP, signal_handler);
470 signal(SIGIOT, signal_handler);
471 signal(SIGBUS, signal_handler);
472 signal(SIGFPE, signal_handler);
473 signal(SIGSEGV, signal_handler);
474 signal(SIGTERM, signal_handler);
482 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
486 *height = scr_height;
489 void VID_Finish (void)
492 if (r_render.integer)
495 glXSwapBuffers(vidx11_display, win);
498 // handle the mouse state when windowed if that's changed
500 if (vid_mouse.integer && key_dest == key_game)
517 IN_DeactivateMouse ();
522 // LordHavoc: ported from SDL 1.2.2, this was far more difficult to port from
523 // SDL than to simply use the XFree gamma ramp extension, but that affects the
524 // whole screen even when the game window is inactive, this only affects the
525 // screen while the window is active, very desirable behavior :)
526 int VID_SetGamma(float prescale, float gamma, float scale, float base)
528 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
533 unsigned int Rmask, Gmask, Bmask, Rloss, Gloss, Bloss, Rshift, Gshift, Bshift, mask;
535 unsigned short ramp[256];
537 if (COM_CheckParm("-nogamma"))
540 if (vidx11_visual->class != DirectColor)
542 Con_Printf("X11 Visual class is %d, can only do gamma on %d\n", vidx11_visual->class, DirectColor);
546 Rmask = vidx11_visual->red_mask;
547 Gmask = vidx11_visual->green_mask;
548 Bmask = vidx11_visual->blue_mask;
554 for (;!(mask & 1);mask >>= 1)
556 for (;(mask & 1);mask >>= 1)
563 for (;!(mask & 1);mask >>= 1)
565 for (;(mask & 1);mask >>= 1)
572 for (;!(mask & 1);mask >>= 1)
574 for (;(mask & 1);mask >>= 1)
578 BuildGammaTable16(prescale, gamma, scale, base, ramp);
580 // convert gamma ramp to palette (yes this seems odd)
581 ncolors = vidx11_visual->map_entries;
582 for (i = 0;i < ncolors;i++)
584 c = (256 * i / ncolors);
585 xcmap[i].pixel = ((c >> Rloss) << Rshift) | ((c >> Gloss) << Gshift) | ((c >> Bloss) << Bshift);
586 xcmap[i].red = ramp[c];
587 xcmap[i].green = ramp[c];
588 xcmap[i].blue = ramp[c];
589 xcmap[i].flags = (DoRed|DoGreen|DoBlue);
591 XStoreColors(vidx11_display, vidx11_colormap, xcmap, ncolors);
592 XSync(vidx11_display, false);
593 // FIXME: should this check for BadAccess/BadColor/BadValue errors produced by XStoreColors before setting this true?
601 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
611 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
615 int nogammaattrib[] =
625 // char gldir[MAX_OSPATH];
626 int width = 640, height = 480;
627 XSetWindowAttributes attr;
630 XVisualInfo *visinfo;
631 qboolean fullscreen = true;
632 int MajorVersion, MinorVersion;
634 Cvar_RegisterVariable (&vid_dga);
635 Cvar_RegisterVariable (&vid_dga_mouseaccel);
637 // interpret command-line params
639 // set vid parameters
640 if ((i = COM_CheckParm("-window")) != 0)
643 if ((i = COM_CheckParm("-width")) != 0)
644 width = atoi(com_argv[i+1]);
646 if ((i = COM_CheckParm("-height")) != 0)
647 height = atoi(com_argv[i+1]);
649 if ((i = COM_CheckParm("-conwidth")) != 0)
650 vid.conwidth = atoi(com_argv[i+1]);
654 vid.conwidth &= 0xfff8; // make it a multiple of eight
656 if (vid.conwidth < 320)
659 // pick a conheight that matches with correct aspect
660 vid.conheight = vid.conwidth*3 / 4;
662 if ((i = COM_CheckParm("-conheight")) != 0)
663 vid.conheight = atoi(com_argv[i+1]);
664 if (vid.conheight < 200)
667 if (!(vidx11_display = XOpenDisplay(NULL)))
669 fprintf(stderr, "Error couldn't open the X display\n");
673 scrnum = DefaultScreen(vidx11_display);
674 root = RootWindow(vidx11_display, scrnum);
676 // Get video mode list
677 MajorVersion = MinorVersion = 0;
678 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
682 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
687 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
689 if (!COM_CheckParm("-nogamma"))
690 visinfo = glXChooseVisual(vidx11_display, scrnum, gammaattrib);
694 visinfo = glXChooseVisual(vidx11_display, scrnum, nogammaattrib);
697 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
704 int best_fit, best_dist, dist, x, y;
706 XF86VidModeGetAllModeLines(vidx11_display, scrnum, &num_vidmodes, &vidmodes);
708 // Are we going fullscreen? If so, let's change video mode
714 for (i = 0; i < num_vidmodes; i++)
716 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
719 x = width - vidmodes[i]->hdisplay;
720 y = height - vidmodes[i]->vdisplay;
721 dist = (x * x) + (y * y);
722 if (dist < best_dist)
731 // LordHavoc: changed from ActualWidth/ActualHeight =,
732 // to width/height =, so the window will take the full area of
734 width = vidmodes[best_fit]->hdisplay;
735 height = vidmodes[best_fit]->vdisplay;
737 // change to the mode
738 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[best_fit]);
739 vidmode_active = true;
741 // Move the viewport to top left
742 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
749 // LordHavoc: save the visual for use in gamma ramp settings later
750 vidx11_visual = visinfo->visual;
752 /* window attributes */
753 attr.background_pixel = 0;
754 attr.border_pixel = 0;
755 // LordHavoc: save the colormap for later, too
756 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
757 attr.event_mask = X_MASK;
760 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
761 attr.override_redirect = True;
762 attr.backing_store = NotUseful;
763 attr.save_under = False;
766 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
768 win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
769 XStoreName(vidx11_display, win, gamename);
770 XMapWindow(vidx11_display, win);
774 XMoveWindow(vidx11_display, win, 0, 0);
775 XRaiseWindow(vidx11_display, win);
776 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
777 XFlush(vidx11_display);
778 // Move the viewport to top left
779 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
782 XFlush(vidx11_display);
784 ctx = glXCreateContext(vidx11_display, visinfo, NULL, True);
786 glXMakeCurrent(vidx11_display, win, ctx);
791 if (vid.conheight > height)
792 vid.conheight = height;
793 if (vid.conwidth > width)
794 vid.conwidth = width;
796 InitSig(); // trap evil signals
800 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
802 // force a surface cache flush
803 // vid.recalc_refdef = 1;
806 void Sys_SendKeyEvents(void)
813 if (COM_CheckParm ("-nomouse"))
817 void IN_Shutdown(void)
826 void IN_Commands (void)
830 void IN_Move (usercmd_t *cmd)
833 IN_Mouse(cmd, mouse_x, mouse_y);