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 Atom wm_delete_window_atom;
48 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
49 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
50 PointerMotionMask | ButtonMotionMask )
51 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | StructureNotifyMask )
54 viddef_t vid; // global video state
56 static qboolean mouse_avail = true;
57 static qboolean mouse_active = false, usingmouse = false;
58 static float mouse_x, 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"};
64 qboolean vidmode_ext = false;
66 static int win_x, win_y;
68 static int scr_width, scr_height;
70 static XF86VidModeModeInfo **vidmodes;
71 static int num_vidmodes;
72 static qboolean vidmode_active = false;
74 static Visual *vidx11_visual;
75 static Colormap vidx11_colormap;
77 /*-----------------------------------------------------------------------*/
79 const char *gl_vendor;
80 const char *gl_renderer;
81 const char *gl_version;
82 const char *gl_extensions;
84 /*-----------------------------------------------------------------------*/
86 XLateKey(XKeyEvent *ev)
91 keysym = XLookupKeysym(ev, 0);
95 case XK_KP_Page_Up: key = KP_PGUP; break;
96 case XK_Page_Up: key = K_PGUP; break;
98 case XK_KP_Page_Down: key = KP_PGDN; break;
99 case XK_Page_Down: key = K_PGDN; break;
101 case XK_KP_Home: key = KP_HOME; break;
102 case XK_Home: key = K_HOME; break;
104 case XK_KP_End: key = KP_END; break;
105 case XK_End: key = K_END; break;
107 case XK_KP_Left: key = KP_LEFTARROW; break;
108 case XK_Left: key = K_LEFTARROW; break;
110 case XK_KP_Right: key = KP_RIGHTARROW; break;
111 case XK_Right: key = K_RIGHTARROW; break;
113 case XK_KP_Down: key = KP_DOWNARROW; break;
114 case XK_Down: key = K_DOWNARROW; break;
116 case XK_KP_Up: key = KP_UPARROW; break;
117 case XK_Up: key = K_UPARROW; break;
119 case XK_Escape: key = K_ESCAPE; break;
121 case XK_KP_Enter: key = KP_ENTER; break;
122 case XK_Return: key = K_ENTER; break;
124 case XK_Tab: key = K_TAB; break;
126 case XK_F1: key = K_F1; break;
127 case XK_F2: key = K_F2; break;
128 case XK_F3: key = K_F3; break;
129 case XK_F4: key = K_F4; break;
130 case XK_F5: key = K_F5; break;
131 case XK_F6: key = K_F6; break;
132 case XK_F7: key = K_F7; break;
133 case XK_F8: key = K_F8; break;
134 case XK_F9: key = K_F9; break;
135 case XK_F10: key = K_F10; break;
136 case XK_F11: key = K_F11; break;
137 case XK_F12: key = K_F12; break;
139 case XK_BackSpace: key = K_BACKSPACE; break;
141 case XK_KP_Delete: key = KP_DEL; break;
142 case XK_Delete: key = K_DEL; break;
144 case XK_Pause: key = K_PAUSE; break;
147 case XK_Shift_R: key = K_SHIFT; break;
151 case XK_Control_R: key = K_CTRL; break;
157 case XK_Meta_R: key = K_ALT; break;
159 case XK_Caps_Lock: key = K_CAPSLOCK; break;
160 case XK_KP_Begin: key = KP_5; break;
162 case XK_Insert: key = K_INS; break;
163 case XK_KP_Insert: key = KP_INS; break;
165 case XK_KP_Multiply: key = KP_MULTIPLY; break;
166 case XK_KP_Add: key = KP_PLUS; break;
167 case XK_KP_Subtract: key = KP_MINUS; break;
168 case XK_KP_Divide: key = KP_DIVIDE; break;
170 /* For Sun keyboards */
171 case XK_F27: key = K_HOME; break;
172 case XK_F29: key = K_PGUP; break;
173 case XK_F33: key = K_END; break;
174 case XK_F35: key = K_PGDN; break;
181 if ((key >= 'A') && (key <= 'Z'))
182 key = key + ('a' - 'A');
190 static Cursor CreateNullCursor(Display *display, Window root)
198 cursormask = XCreatePixmap(display, root, 1, 1, 1);
199 xgc.function = GXclear;
200 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
201 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
202 dummycolour.pixel = 0;
204 dummycolour.flags = 04;
205 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
206 XFreePixmap(display,cursormask);
211 static void install_grabs(void)
213 XWindowAttributes attribs_1;
214 XSetWindowAttributes attribs_2;
216 XGetWindowAttributes(vidx11_display, win, &attribs_1);
217 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
218 XChangeWindowAttributes(vidx11_display, win, CWEventMask, &attribs_2);
221 XDefineCursor(vidx11_display, win, CreateNullCursor(vidx11_display, win));
223 XGrabPointer(vidx11_display, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
227 int MajorVersion, MinorVersion;
229 if (!XF86DGAQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
231 // unable to query, probalby not supported
232 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
238 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), XF86DGADirectMouse);
239 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
243 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, scr_width / 2, scr_height / 2);
245 XGrabKeyboard(vidx11_display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
248 mouse_x = mouse_y = 0;
251 static void uninstall_grabs(void)
253 if (!vidx11_display || !win)
256 if (vid_dga.integer == 1)
257 XF86DGADirectVideo(vidx11_display, DefaultScreen(vidx11_display), 0);
259 XUngrabPointer(vidx11_display, CurrentTime);
260 XUngrabKeyboard(vidx11_display, CurrentTime);
263 XUndefineCursor(vidx11_display, win);
265 mouse_active = false;
268 static void HandleEvents(void)
271 qboolean dowarp = false;
276 while (XPending(vidx11_display))
278 XNextEvent(vidx11_display, &event);
284 Key_Event(XLateKey(&event.xkey), true);
289 Key_Event(XLateKey(&event.xkey), false);
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 (!event.xmotion.send_event)
306 mouse_x += event.xmotion.x - p_mouse_x;
307 mouse_y += event.xmotion.y - p_mouse_y;
308 if (abs(scr_width/2 - event.xmotion.x) > scr_width / 4 || abs(scr_height/2 - event.xmotion.y) > scr_height / 4)
311 p_mouse_x = event.xmotion.x;
312 p_mouse_y = event.xmotion.y;
316 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
320 // mouse button pressed
321 switch(event.xbutton.button)
324 Key_Event(K_MOUSE1, true);
327 Key_Event(K_MOUSE3, true);
330 Key_Event(K_MOUSE2, true);
333 Key_Event(K_MWHEELUP, true);
336 Key_Event(K_MWHEELDOWN, true);
339 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-5 expected\n", event.xbutton.button);
345 // mouse button released
346 switch(event.xbutton.button)
349 Key_Event(K_MOUSE1, false);
352 Key_Event(K_MOUSE3, false);
355 Key_Event(K_MOUSE2, false);
358 Key_Event(K_MWHEELUP, false);
361 Key_Event(K_MWHEELDOWN, false);
364 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-5 expected\n", event.xbutton.button);
371 win_x = event.xcreatewindow.x;
372 win_y = event.xcreatewindow.y;
375 case ConfigureNotify:
376 // window changed size/location
377 win_x = event.xconfigure.x;
378 win_y = event.xconfigure.y;
381 // window has been destroyed
385 // window manager messages
386 if ((event.xclient.format == 32) && (event.xclient.data.l[0] == wm_delete_window_atom))
394 // window iconified/rolledup/whatever
398 // window is now the input focus
401 // window is no longer the input focus
404 // mouse entered window
414 /* move the mouse to the window center again */
415 p_mouse_x = scr_width / 2;
416 p_mouse_y = scr_height / 2;
417 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
422 static void IN_DeactivateMouse( void )
424 if (!mouse_avail || !vidx11_display || !win)
430 mouse_active = false;
434 static void IN_ActivateMouse( void )
436 if (!mouse_avail || !vidx11_display || !win)
441 mouse_x = mouse_y = 0; // don't spazz
448 void VID_Shutdown(void)
450 if (!ctx || !vidx11_display)
458 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[0]);
460 XDestroyWindow(vidx11_display, win);
461 XCloseDisplay(vidx11_display);
463 vidmode_active = false;
464 vidx11_display = NULL;
469 void signal_handler(int sig)
471 printf("Received signal %d, exiting...\n", sig);
478 signal(SIGHUP, signal_handler);
479 signal(SIGINT, signal_handler);
480 signal(SIGQUIT, signal_handler);
481 signal(SIGILL, signal_handler);
482 signal(SIGTRAP, signal_handler);
483 signal(SIGIOT, signal_handler);
484 signal(SIGBUS, signal_handler);
485 signal(SIGFPE, signal_handler);
486 signal(SIGSEGV, signal_handler);
487 signal(SIGTERM, signal_handler);
495 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
499 *height = scr_height;
502 void VID_Finish (void)
505 if (r_render.integer)
508 glXSwapBuffers(vidx11_display, win);
511 // handle the mouse state when windowed if that's changed
513 if (vid_mouse.integer && key_dest == key_game)
530 IN_DeactivateMouse ();
535 // LordHavoc: ported from SDL 1.2.2, this was far more difficult to port from
536 // SDL than to simply use the XFree gamma ramp extension, but that affects the
537 // whole screen even when the game window is inactive, this only affects the
538 // screen while the window is active, very desirable behavior :)
539 int VID_SetGamma(float prescale, float gamma, float scale, float base)
541 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
546 unsigned int Rmask, Gmask, Bmask, Rloss, Gloss, Bloss, Rshift, Gshift, Bshift, mask;
548 unsigned short ramp[256];
550 if (COM_CheckParm("-nogamma"))
553 if (vidx11_visual->class != DirectColor)
555 Con_Printf("X11 Visual class is %d, can only do gamma on %d\n", vidx11_visual->class, DirectColor);
559 Rmask = vidx11_visual->red_mask;
560 Gmask = vidx11_visual->green_mask;
561 Bmask = vidx11_visual->blue_mask;
567 for (;!(mask & 1);mask >>= 1)
569 for (;(mask & 1);mask >>= 1)
576 for (;!(mask & 1);mask >>= 1)
578 for (;(mask & 1);mask >>= 1)
585 for (;!(mask & 1);mask >>= 1)
587 for (;(mask & 1);mask >>= 1)
591 BuildGammaTable16(prescale, gamma, scale, base, ramp);
593 // convert gamma ramp to palette (yes this seems odd)
594 ncolors = vidx11_visual->map_entries;
595 for (i = 0;i < ncolors;i++)
597 c = (256 * i / ncolors);
598 xcmap[i].pixel = ((c >> Rloss) << Rshift) | ((c >> Gloss) << Gshift) | ((c >> Bloss) << Bshift);
599 xcmap[i].red = ramp[c];
600 xcmap[i].green = ramp[c];
601 xcmap[i].blue = ramp[c];
602 xcmap[i].flags = (DoRed|DoGreen|DoBlue);
604 XStoreColors(vidx11_display, vidx11_colormap, xcmap, ncolors);
605 XSync(vidx11_display, false);
606 // FIXME: should this check for BadAccess/BadColor/BadValue errors produced by XStoreColors before setting this true?
614 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
624 GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
628 int nogammaattrib[] =
638 int width = 640, height = 480;
639 XSetWindowAttributes attr;
642 XVisualInfo *visinfo;
643 qboolean fullscreen = true;
644 int MajorVersion, MinorVersion;
646 Cvar_RegisterVariable (&vid_dga);
647 Cvar_RegisterVariable (&vid_dga_mouseaccel);
649 // interpret command-line params
651 // set vid parameters
652 if ((i = COM_CheckParm("-window")) != 0)
655 if ((i = COM_CheckParm("-width")) != 0)
656 width = atoi(com_argv[i+1]);
658 if ((i = COM_CheckParm("-height")) != 0)
659 height = atoi(com_argv[i+1]);
661 if ((i = COM_CheckParm("-conwidth")) != 0)
662 vid.conwidth = atoi(com_argv[i+1]);
666 vid.conwidth &= 0xfff8; // make it a multiple of eight
668 if (vid.conwidth < 320)
671 // pick a conheight that matches with correct aspect
672 vid.conheight = vid.conwidth*3 / 4;
674 if ((i = COM_CheckParm("-conheight")) != 0)
675 vid.conheight = atoi(com_argv[i+1]);
676 if (vid.conheight < 200)
679 if (!(vidx11_display = XOpenDisplay(NULL)))
681 fprintf(stderr, "Error couldn't open the X display\n");
685 scrnum = DefaultScreen(vidx11_display);
686 root = RootWindow(vidx11_display, scrnum);
688 // Get video mode list
689 MajorVersion = MinorVersion = 0;
690 if (!XF86VidModeQueryVersion(vidx11_display, &MajorVersion, &MinorVersion))
694 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
699 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
701 if (!COM_CheckParm("-nogamma"))
702 visinfo = glXChooseVisual(vidx11_display, scrnum, gammaattrib);
706 visinfo = glXChooseVisual(vidx11_display, scrnum, nogammaattrib);
709 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
716 int best_fit, best_dist, dist, x, y;
718 XF86VidModeGetAllModeLines(vidx11_display, scrnum, &num_vidmodes, &vidmodes);
720 // Are we going fullscreen? If so, let's change video mode
726 for (i = 0; i < num_vidmodes; i++)
728 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
731 x = width - vidmodes[i]->hdisplay;
732 y = height - vidmodes[i]->vdisplay;
733 dist = (x * x) + (y * y);
734 if (dist < best_dist)
743 // LordHavoc: changed from ActualWidth/ActualHeight =,
744 // to width/height =, so the window will take the full area of
746 width = vidmodes[best_fit]->hdisplay;
747 height = vidmodes[best_fit]->vdisplay;
749 // change to the mode
750 XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[best_fit]);
751 vidmode_active = true;
753 // Move the viewport to top left
754 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
761 // LordHavoc: save the visual for use in gamma ramp settings later
762 vidx11_visual = visinfo->visual;
764 /* window attributes */
765 attr.background_pixel = 0;
766 attr.border_pixel = 0;
767 // LordHavoc: save the colormap for later, too
768 vidx11_colormap = attr.colormap = XCreateColormap(vidx11_display, root, visinfo->visual, AllocNone);
769 attr.event_mask = X_MASK;
772 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
773 attr.override_redirect = True;
774 attr.backing_store = NotUseful;
775 attr.save_under = False;
778 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
780 win = XCreateWindow(vidx11_display, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
781 XStoreName(vidx11_display, win, gamename);
782 XMapWindow(vidx11_display, win);
784 // LordHavoc: making the close button on a window do the right thing
785 // seems to involve this mess, sigh...
786 wm_delete_window_atom = XInternAtom(vidx11_display, "WM_DELETE_WINDOW", false);
787 XSetWMProtocols(vidx11_display, win, &wm_delete_window_atom, 1);
791 XMoveWindow(vidx11_display, win, 0, 0);
792 XRaiseWindow(vidx11_display, win);
793 XWarpPointer(vidx11_display, None, win, 0, 0, 0, 0, 0, 0);
794 XFlush(vidx11_display);
795 // Move the viewport to top left
796 XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
799 XFlush(vidx11_display);
801 ctx = glXCreateContext(vidx11_display, visinfo, NULL, True);
803 glXMakeCurrent(vidx11_display, win, ctx);
808 if (vid.conheight > height)
809 vid.conheight = height;
810 if (vid.conwidth > width)
811 vid.conwidth = width;
813 InitSig(); // trap evil signals
819 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
822 void Sys_SendKeyEvents(void)
829 if (COM_CheckParm ("-nomouse"))
833 void IN_Shutdown(void)
842 void IN_Commands (void)
846 void IN_Move (usercmd_t *cmd)
849 IN_Mouse(cmd, mouse_x, mouse_y);