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 *dpy = 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 cvar_t vid_mode = {"vid_mode", "0", false};
53 cvar_t vid_fullscreen = {"vid_fullscreen", "1"};
55 viddef_t vid; // global video state
57 static qboolean mouse_avail = true;
58 static qboolean mouse_active = false;
59 // static qboolean dga_active;
60 static float mouse_x, mouse_y;
61 static float old_mouse_x, old_mouse_y;
62 static int p_mouse_x, p_mouse_y;
64 static cvar_t in_mouse = {"in_mouse", "1", false};
65 static cvar_t in_dga = {"in_dga", "1", false};
66 static cvar_t in_dga_mouseaccel = {"in_dga_mouseaccel", "1", false};
67 static cvar_t m_filter = {"m_filter", "0"};
68 static cvar_t _windowed_mouse = {"_windowed_mouse", "1"};
70 qboolean vidmode_ext = false;
72 static int win_x, win_y;
74 static int scr_width, scr_height;
76 static XF86VidModeModeInfo **vidmodes;
77 //static int default_dotclock_vidmode;
78 static int num_vidmodes;
79 static qboolean vidmode_active = false;
81 /*-----------------------------------------------------------------------*/
83 float gldepthmin, gldepthmax;
85 const char *gl_vendor;
86 const char *gl_renderer;
87 const char *gl_version;
88 const char *gl_extensions;
90 //static float vid_gamma = 1.0;
92 // LordHavoc: ARB multitexture support
95 /*-----------------------------------------------------------------------*/
97 XLateKey(XKeyEvent *ev/*, qboolean modified*/)
103 keysym = XLookupKeysym(ev, 0);
106 case XK_KP_Page_Up: key = KP_PGUP; break;
107 case XK_Page_Up: key = K_PGUP; break;
109 case XK_KP_Page_Down: key = KP_PGDN; break;
110 case XK_Page_Down: key = K_PGDN; break;
112 case XK_KP_Home: key = KP_HOME; break;
113 case XK_Home: key = K_HOME; break;
115 case XK_KP_End: key = KP_END; break;
116 case XK_End: key = K_END; break;
118 case XK_KP_Left: key = KP_LEFTARROW; break;
119 case XK_Left: key = K_LEFTARROW; break;
121 case XK_KP_Right: key = KP_RIGHTARROW; break;
122 case XK_Right: key = K_RIGHTARROW; break;
124 case XK_KP_Down: key = KP_DOWNARROW; break;
125 case XK_Down: key = K_DOWNARROW; break;
127 case XK_KP_Up: key = KP_UPARROW; break;
128 case XK_Up: key = K_UPARROW; break;
130 case XK_Escape: key = K_ESCAPE; break;
132 case XK_KP_Enter: key = KP_ENTER; break;
133 case XK_Return: key = K_ENTER; break;
135 case XK_Tab: key = K_TAB; break;
137 case XK_F1: key = K_F1; break;
138 case XK_F2: key = K_F2; break;
139 case XK_F3: key = K_F3; break;
140 case XK_F4: key = K_F4; break;
141 case XK_F5: key = K_F5; break;
142 case XK_F6: key = K_F6; break;
143 case XK_F7: key = K_F7; break;
144 case XK_F8: key = K_F8; break;
145 case XK_F9: key = K_F9; break;
146 case XK_F10: key = K_F10; break;
147 case XK_F11: key = K_F11; break;
148 case XK_F12: key = K_F12; break;
150 case XK_BackSpace: key = K_BACKSPACE; break;
152 case XK_KP_Delete: key = KP_DEL; break;
153 case XK_Delete: key = K_DEL; break;
155 case XK_Pause: key = K_PAUSE; break;
158 case XK_Shift_R: key = K_SHIFT; break;
162 case XK_Control_R: key = K_CTRL; break;
168 case XK_Meta_R: key = K_ALT; break;
170 case XK_Caps_Lock: key = K_CAPSLOCK; break;
171 case XK_KP_Begin: key = KP_5; break;
173 case XK_Insert: key = K_INS; break;
174 case XK_KP_Insert: key = KP_INS; break;
176 case XK_KP_Multiply: key = KP_MULTIPLY; break;
177 case XK_KP_Add: key = KP_PLUS; break;
178 case XK_KP_Subtract: key = KP_MINUS; break;
179 case XK_KP_Divide: key = KP_DIVIDE; break;
181 /* For Sun keyboards */
182 case XK_F27: key = K_HOME; break;
183 case XK_F29: key = K_PGUP; break;
184 case XK_F33: key = K_END; break;
185 case XK_F35: key = K_PGDN; break;
191 if (/*!modified && */((key >= 'A') && (key <= 'Z'))) {
192 key = key + ('a' - 'A');
201 static Cursor CreateNullCursor(Display *display, Window root)
209 cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
210 xgc.function = GXclear;
211 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
212 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
213 dummycolour.pixel = 0;
215 dummycolour.flags = 04;
216 cursor = XCreatePixmapCursor(display, cursormask, cursormask,
217 &dummycolour,&dummycolour, 0,0);
218 XFreePixmap(display,cursormask);
223 static void install_grabs(void)
225 XWindowAttributes attribs_1;
226 XSetWindowAttributes attribs_2;
228 XGetWindowAttributes(dpy, win, &attribs_1);
229 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
230 XChangeWindowAttributes(dpy, win, CWEventMask, &attribs_2);
233 XDefineCursor(dpy, win, CreateNullCursor(dpy, win));
235 XGrabPointer(dpy, win, True, 0, GrabModeAsync, GrabModeAsync,
236 win, None, CurrentTime);
239 int MajorVersion, MinorVersion;
241 if (!XF86DGAQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
242 // unable to query, probalby not supported
243 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
247 XF86DGADirectVideo(dpy, DefaultScreen(dpy), XF86DGADirectMouse);
248 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
251 XWarpPointer(dpy, None, win,
253 vid.width / 2, vid.height / 2);
256 XGrabKeyboard(dpy, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
259 mouse_x = mouse_y = 0;
264 static void uninstall_grabs(void)
269 if (in_dga.value == 1) {
270 XF86DGADirectVideo(dpy, DefaultScreen(dpy), 0);
273 XUngrabPointer(dpy, CurrentTime);
274 XUngrabKeyboard(dpy, CurrentTime);
277 XUndefineCursor(dpy, win);
279 mouse_active = false;
282 static void HandleEvents(void)
287 qboolean dowarp = false;
292 while (XPending(dpy)) {
293 XNextEvent(dpy, &event);
295 switch (event.type) {
298 Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
302 if (in_dga.value == 1) {
303 mouse_x += event.xmotion.x_root * in_dga_mouseaccel.value;
304 mouse_y += event.xmotion.y_root * in_dga_mouseaccel.value;
306 if (!p_mouse_x && !p_mouse_y) {
307 Con_Printf("event->xmotion.x: %d\n", event.xmotion.x);
308 Con_Printf("event->xmotion.y: %d\n", event.xmotion.y);
310 if (vid_fullscreen.value || _windowed_mouse.value) {
311 if (!event.xmotion.send_event) {
312 mouse_x += (event.xmotion.x - p_mouse_x);
313 mouse_y += (event.xmotion.y - p_mouse_y);
314 if (abs(vid.width/2 - event.xmotion.x) > vid.width / 4
315 || abs(vid.height/2 - event.xmotion.y) > vid.height / 4) {
320 mouse_x += (event.xmotion.x - p_mouse_x);
321 mouse_y += (event.xmotion.y - p_mouse_y);
323 p_mouse_x = event.xmotion.x;
324 p_mouse_y = event.xmotion.y;
330 if (event.xbutton.button == 1)
332 else if (event.xbutton.button == 2)
334 else if (event.xbutton.button == 3)
337 Key_Event(K_MOUSE1 + b, true);
342 if (event.xbutton.button == 1)
344 else if (event.xbutton.button == 2)
346 else if (event.xbutton.button == 3)
349 Key_Event(K_MOUSE1 + b, false);
353 win_x = event.xcreatewindow.x;
354 win_y = event.xcreatewindow.y;
357 case ConfigureNotify :
358 win_x = event.xconfigure.x;
359 win_y = event.xconfigure.y;
365 /* move the mouse to the window center again */
366 p_mouse_x = vid.width / 2;
367 p_mouse_y = vid.height / 2;
368 XWarpPointer(dpy, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
373 static void IN_DeactivateMouse( void )
375 if (!mouse_avail || !dpy || !win)
380 mouse_active = false;
384 static void IN_ActivateMouse( void )
386 if (!mouse_avail || !dpy || !win)
390 mouse_x = mouse_y = 0; // don't spazz
397 void VID_Shutdown(void)
406 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[0]);
407 /* Disabled, causes a segfault during shutdown.
409 glXDestroyContext(dpy, ctx);
412 XDestroyWindow(dpy, win);
415 vidmode_active = false;
421 void signal_handler(int sig)
423 printf("Received signal %d, exiting...\n", sig);
430 signal(SIGHUP, signal_handler);
431 signal(SIGINT, signal_handler);
432 signal(SIGQUIT, signal_handler);
433 signal(SIGILL, signal_handler);
434 signal(SIGTRAP, signal_handler);
435 signal(SIGIOT, signal_handler);
436 signal(SIGBUS, signal_handler);
437 signal(SIGFPE, signal_handler);
438 signal(SIGSEGV, signal_handler);
439 signal(SIGTERM, signal_handler);
442 void VID_CheckMultitexture(void)
445 qglMTexCoord2f = NULL;
446 qglSelectTexture = NULL;
447 // Check to see if multitexture is disabled
448 if (COM_CheckParm("-nomtex"))
450 Con_Printf("...multitexture disabled\n");
453 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
455 Con_Printf("Unable to open symbol list for main program.\n");
458 // Test for ARB_multitexture
459 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
461 Con_Printf("...using GL_ARB_multitexture\n");
462 qglMTexCoord2f = (void *) dlsym(prjobj, "glMultiTexCoord2fARB");
463 qglSelectTexture = (void *) dlsym(prjobj, "glActiveTextureARB");
465 gl_mtex_enum = GL_TEXTURE0_ARB;
467 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
469 Con_Printf("...using GL_SGIS_multitexture\n");
470 qglMTexCoord2f = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
471 qglSelectTexture = (void *) dlsym(prjobj, "glSelectTextureSGIS");
473 gl_mtex_enum = TEXTURE0_SGIS;
476 Con_Printf("...multitexture disabled (not detected)\n");
480 void VID_CheckCVA(void)
483 qglLockArraysEXT = NULL;
484 qglUnlockArraysEXT = NULL;
485 gl_supportslockarrays = false;
486 if (COM_CheckParm("-nocva"))
488 Con_Printf("...compiled vertex arrays disabled\n");
491 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
493 Con_Printf("Unable to open symbol list for main program.\n");
496 if (strstr(gl_extensions, "GL_EXT_compiled_vertex_array"))
498 Con_Printf("...using compiled vertex arrays\n");
499 qglLockArraysEXT = (void *) dlsym(prjobj, "glLockArraysEXT");
500 qglUnlockArraysEXT = (void *) dlsym(prjobj, "glUnlockArraysEXT");
501 gl_supportslockarrays = true;
512 void GL_BeginRendering (int *x, int *y, int *width, int *height)
516 *height = scr_height;
518 // glViewport (*x, *y, *width, *height);
522 void GL_EndRendering (void)
527 glXSwapBuffers(dpy, win);
542 // char gldir[MAX_OSPATH];
543 int width = 640, height = 480;
544 XSetWindowAttributes attr;
547 XVisualInfo *visinfo;
548 qboolean fullscreen = true;
549 int MajorVersion, MinorVersion;
550 int actualWidth, actualHeight;
552 Cvar_RegisterVariable (&vid_mode);
553 Cvar_RegisterVariable (&vid_fullscreen);
554 Cvar_RegisterVariable (&in_mouse);
555 Cvar_RegisterVariable (&in_dga);
556 Cvar_RegisterVariable (&in_dga_mouseaccel);
557 Cvar_RegisterVariable (&m_filter);
559 // interpret command-line params
561 // set vid parameters
562 if ((i = COM_CheckParm("-window")) != 0)
565 if ((i = COM_CheckParm("-width")) != 0)
566 width = atoi(com_argv[i+1]);
568 if ((i = COM_CheckParm("-height")) != 0)
569 height = atoi(com_argv[i+1]);
571 if ((i = COM_CheckParm("-conwidth")) != 0)
572 vid.conwidth = atoi(com_argv[i+1]);
576 vid.conwidth &= 0xfff8; // make it a multiple of eight
578 if (vid.conwidth < 320)
581 // pick a conheight that matches with correct aspect
582 vid.conheight = vid.conwidth*3 / 4;
584 if ((i = COM_CheckParm("-conheight")) != 0)
585 vid.conheight = atoi(com_argv[i+1]);
586 if (vid.conheight < 200)
589 if (!(dpy = XOpenDisplay(NULL))) {
590 fprintf(stderr, "Error couldn't open the X display\n");
594 scrnum = DefaultScreen(dpy);
595 root = RootWindow(dpy, scrnum);
597 // Get video mode list
598 MajorVersion = MinorVersion = 0;
599 if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
602 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
606 visinfo = glXChooseVisual(dpy, scrnum, attrib);
608 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
613 int best_fit, best_dist, dist, x, y;
615 XF86VidModeGetAllModeLines(dpy, scrnum, &num_vidmodes, &vidmodes);
617 // Are we going fullscreen? If so, let's change video mode
622 for (i = 0; i < num_vidmodes; i++) {
623 if (width > vidmodes[i]->hdisplay ||
624 height > vidmodes[i]->vdisplay)
627 x = width - vidmodes[i]->hdisplay;
628 y = height - vidmodes[i]->vdisplay;
629 dist = (x * x) + (y * y);
630 if (dist < best_dist) {
636 if (best_fit != -1) {
637 actualWidth = vidmodes[best_fit]->hdisplay;
638 actualHeight = vidmodes[best_fit]->vdisplay;
640 // change to the mode
641 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[best_fit]);
642 vidmode_active = true;
644 // Move the viewport to top left
645 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
651 /* window attributes */
652 attr.background_pixel = 0;
653 attr.border_pixel = 0;
654 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
655 attr.event_mask = X_MASK;
656 if (vidmode_active) {
657 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore |
658 CWEventMask | CWOverrideRedirect;
659 attr.override_redirect = True;
660 attr.backing_store = NotUseful;
661 attr.save_under = False;
663 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
665 win = XCreateWindow(dpy, root, 0, 0, width, height,
666 0, visinfo->depth, InputOutput,
667 visinfo->visual, mask, &attr);
668 XMapWindow(dpy, win);
670 if (vidmode_active) {
671 XMoveWindow(dpy, win, 0, 0);
672 XRaiseWindow(dpy, win);
673 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
675 // Move the viewport to top left
676 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
681 ctx = glXCreateContext(dpy, visinfo, NULL, True);
683 glXMakeCurrent(dpy, win, ctx);
688 if (vid.conheight > height)
689 vid.conheight = height;
690 if (vid.conwidth > width)
691 vid.conwidth = width;
692 vid.width = vid.conwidth;
693 vid.height = vid.conheight;
695 vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
697 InitSig(); // trap evil signals
701 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
703 vid.recalc_refdef = 1; // force a surface cache flush
708 void Sys_SendKeyEvents(void)
713 void Force_CenterView_f (void)
715 cl.viewangles[PITCH] = 0;
722 void IN_Shutdown(void)
731 void IN_Commands (void)
736 if (vidmode_active || key_dest == key_game)
739 IN_DeactivateMouse ();
747 void IN_MouseMove (usercmd_t *cmd)
755 if (m_filter.value) {
756 mouse_x = (mouse_x + old_mouse_x) * 0.5;
757 mouse_y = (mouse_y + old_mouse_y) * 0.5;
759 old_mouse_x = mouse_x;
760 old_mouse_y = mouse_y;
763 mouse_x *= sensitivity.value;
764 mouse_y *= sensitivity.value;
766 if (in_strafe.state & 1)
767 cmd->sidemove += m_side.value * mouse_x;
769 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
774 if (/*freelook && */!(in_strafe.state & 1)) {
775 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
776 cl.viewangles[PITCH] = bound (-70, cl.viewangles[PITCH], 80);
778 if ((in_strafe.state & 1) && noclip_anglehack)
779 cmd->upmove -= m_forward.value * mouse_y;
781 cmd->forwardmove -= m_forward.value * mouse_y;
783 mouse_x = mouse_y = 0.0;
786 void IN_Move (usercmd_t *cmd)