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 /*-----------------------------------------------------------------------*/
94 XLateKey(XKeyEvent *ev/*, qboolean modified*/)
100 keysym = XLookupKeysym(ev, 0);
103 case XK_KP_Page_Up: key = KP_PGUP; break;
104 case XK_Page_Up: key = K_PGUP; break;
106 case XK_KP_Page_Down: key = KP_PGDN; break;
107 case XK_Page_Down: key = K_PGDN; break;
109 case XK_KP_Home: key = KP_HOME; break;
110 case XK_Home: key = K_HOME; break;
112 case XK_KP_End: key = KP_END; break;
113 case XK_End: key = K_END; break;
115 case XK_KP_Left: key = KP_LEFTARROW; break;
116 case XK_Left: key = K_LEFTARROW; break;
118 case XK_KP_Right: key = KP_RIGHTARROW; break;
119 case XK_Right: key = K_RIGHTARROW; break;
121 case XK_KP_Down: key = KP_DOWNARROW; break;
122 case XK_Down: key = K_DOWNARROW; break;
124 case XK_KP_Up: key = KP_UPARROW; break;
125 case XK_Up: key = K_UPARROW; break;
127 case XK_Escape: key = K_ESCAPE; break;
129 case XK_KP_Enter: key = KP_ENTER; break;
130 case XK_Return: key = K_ENTER; break;
132 case XK_Tab: key = K_TAB; break;
134 case XK_F1: key = K_F1; break;
135 case XK_F2: key = K_F2; break;
136 case XK_F3: key = K_F3; break;
137 case XK_F4: key = K_F4; break;
138 case XK_F5: key = K_F5; break;
139 case XK_F6: key = K_F6; break;
140 case XK_F7: key = K_F7; break;
141 case XK_F8: key = K_F8; break;
142 case XK_F9: key = K_F9; break;
143 case XK_F10: key = K_F10; break;
144 case XK_F11: key = K_F11; break;
145 case XK_F12: key = K_F12; break;
147 case XK_BackSpace: key = K_BACKSPACE; break;
149 case XK_KP_Delete: key = KP_DEL; break;
150 case XK_Delete: key = K_DEL; break;
152 case XK_Pause: key = K_PAUSE; break;
155 case XK_Shift_R: key = K_SHIFT; break;
159 case XK_Control_R: key = K_CTRL; break;
165 case XK_Meta_R: key = K_ALT; break;
167 case XK_Caps_Lock: key = K_CAPSLOCK; break;
168 case XK_KP_Begin: key = KP_5; break;
170 case XK_Insert: key = K_INS; break;
171 case XK_KP_Insert: key = KP_INS; break;
173 case XK_KP_Multiply: key = KP_MULTIPLY; break;
174 case XK_KP_Add: key = KP_PLUS; break;
175 case XK_KP_Subtract: key = KP_MINUS; break;
176 case XK_KP_Divide: key = KP_DIVIDE; break;
178 /* For Sun keyboards */
179 case XK_F27: key = K_HOME; break;
180 case XK_F29: key = K_PGUP; break;
181 case XK_F33: key = K_END; break;
182 case XK_F35: key = K_PGDN; break;
188 if (/*!modified && */((key >= 'A') && (key <= 'Z'))) {
189 key = key + ('a' - 'A');
198 static Cursor CreateNullCursor(Display *display, Window root)
206 cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
207 xgc.function = GXclear;
208 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
209 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
210 dummycolour.pixel = 0;
212 dummycolour.flags = 04;
213 cursor = XCreatePixmapCursor(display, cursormask, cursormask,
214 &dummycolour,&dummycolour, 0,0);
215 XFreePixmap(display,cursormask);
220 static void install_grabs(void)
222 XWindowAttributes attribs_1;
223 XSetWindowAttributes attribs_2;
225 XGetWindowAttributes(dpy, win, &attribs_1);
226 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
227 XChangeWindowAttributes(dpy, win, CWEventMask, &attribs_2);
230 XDefineCursor(dpy, win, CreateNullCursor(dpy, win));
232 XGrabPointer(dpy, win, True, 0, GrabModeAsync, GrabModeAsync,
233 win, None, CurrentTime);
236 int MajorVersion, MinorVersion;
238 if (!XF86DGAQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
239 // unable to query, probalby not supported
240 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
244 XF86DGADirectVideo(dpy, DefaultScreen(dpy), XF86DGADirectMouse);
245 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
248 XWarpPointer(dpy, None, win,
250 vid.width / 2, vid.height / 2);
253 XGrabKeyboard(dpy, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
256 mouse_x = mouse_y = 0;
261 static void uninstall_grabs(void)
266 if (in_dga.value == 1) {
267 XF86DGADirectVideo(dpy, DefaultScreen(dpy), 0);
270 XUngrabPointer(dpy, CurrentTime);
271 XUngrabKeyboard(dpy, CurrentTime);
274 XUndefineCursor(dpy, win);
276 mouse_active = false;
279 static void HandleEvents(void)
284 qboolean dowarp = false;
289 while (XPending(dpy)) {
290 XNextEvent(dpy, &event);
292 switch (event.type) {
295 Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
299 if (in_dga.value == 1) {
300 mouse_x += event.xmotion.x_root * in_dga_mouseaccel.value;
301 mouse_y += event.xmotion.y_root * in_dga_mouseaccel.value;
303 if (!p_mouse_x && !p_mouse_y) {
304 Con_Printf("event->xmotion.x: %d\n", event.xmotion.x);
305 Con_Printf("event->xmotion.y: %d\n", event.xmotion.y);
307 if (vid_fullscreen.value || _windowed_mouse.value) {
308 if (!event.xmotion.send_event) {
309 mouse_x += (event.xmotion.x - p_mouse_x);
310 mouse_y += (event.xmotion.y - p_mouse_y);
311 if (abs(vid.width/2 - event.xmotion.x) > vid.width / 4
312 || abs(vid.height/2 - event.xmotion.y) > vid.height / 4) {
317 mouse_x += (event.xmotion.x - p_mouse_x);
318 mouse_y += (event.xmotion.y - p_mouse_y);
320 p_mouse_x = event.xmotion.x;
321 p_mouse_y = event.xmotion.y;
327 if (event.xbutton.button == 1)
329 else if (event.xbutton.button == 2)
331 else if (event.xbutton.button == 3)
334 Key_Event(K_MOUSE1 + b, true);
339 if (event.xbutton.button == 1)
341 else if (event.xbutton.button == 2)
343 else if (event.xbutton.button == 3)
346 Key_Event(K_MOUSE1 + b, false);
350 win_x = event.xcreatewindow.x;
351 win_y = event.xcreatewindow.y;
354 case ConfigureNotify :
355 win_x = event.xconfigure.x;
356 win_y = event.xconfigure.y;
362 /* move the mouse to the window center again */
363 p_mouse_x = vid.width / 2;
364 p_mouse_y = vid.height / 2;
365 XWarpPointer(dpy, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
370 static void IN_DeactivateMouse( void )
372 if (!mouse_avail || !dpy || !win)
377 mouse_active = false;
381 static void IN_ActivateMouse( void )
383 if (!mouse_avail || !dpy || !win)
387 mouse_x = mouse_y = 0; // don't spazz
394 void VID_Shutdown(void)
403 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[0]);
404 /* Disabled, causes a segfault during shutdown.
406 glXDestroyContext(dpy, ctx);
409 XDestroyWindow(dpy, win);
412 vidmode_active = false;
418 void signal_handler(int sig)
420 printf("Received signal %d, exiting...\n", sig);
427 signal(SIGHUP, signal_handler);
428 signal(SIGINT, signal_handler);
429 signal(SIGQUIT, signal_handler);
430 signal(SIGILL, signal_handler);
431 signal(SIGTRAP, signal_handler);
432 signal(SIGIOT, signal_handler);
433 signal(SIGBUS, signal_handler);
434 signal(SIGFPE, signal_handler);
435 signal(SIGSEGV, signal_handler);
436 signal(SIGTERM, signal_handler);
439 void VID_CheckMultitexture(void)
442 qglMTexCoord2f = NULL;
443 qglSelectTexture = NULL;
444 // Check to see if multitexture is disabled
445 if (COM_CheckParm("-nomtex"))
447 Con_Printf("...multitexture disabled\n");
450 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
452 Con_Printf("Unable to open symbol list for main program.\n");
455 // Test for ARB_multitexture
456 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
458 Con_Printf("...using GL_ARB_multitexture\n");
459 qglMTexCoord2f = (void *) dlsym(prjobj, "glMultiTexCoord2fARB");
460 qglSelectTexture = (void *) dlsym(prjobj, "glActiveTextureARB");
462 gl_mtex_enum = GL_TEXTURE0_ARB;
464 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
466 Con_Printf("...using GL_SGIS_multitexture\n");
467 qglMTexCoord2f = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
468 qglSelectTexture = (void *) dlsym(prjobj, "glSelectTextureSGIS");
470 gl_mtex_enum = TEXTURE0_SGIS;
473 Con_Printf("...multitexture disabled (not detected)\n");
477 void VID_CheckCVA(void)
480 qglLockArraysEXT = NULL;
481 qglUnlockArraysEXT = NULL;
482 gl_supportslockarrays = false;
483 if (COM_CheckParm("-nocva"))
485 Con_Printf("...compiled vertex arrays disabled\n");
488 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
490 Con_Printf("Unable to open symbol list for main program.\n");
493 if (strstr(gl_extensions, "GL_EXT_compiled_vertex_array"))
495 Con_Printf("...using compiled vertex arrays\n");
496 qglLockArraysEXT = (void *) dlsym(prjobj, "glLockArraysEXT");
497 qglUnlockArraysEXT = (void *) dlsym(prjobj, "glUnlockArraysEXT");
498 gl_supportslockarrays = true;
509 void GL_BeginRendering (int *x, int *y, int *width, int *height)
513 *height = scr_height;
515 // glViewport (*x, *y, *width, *height);
519 void GL_EndRendering (void)
524 glXSwapBuffers(dpy, win);
539 // char gldir[MAX_OSPATH];
540 int width = 640, height = 480;
541 XSetWindowAttributes attr;
544 XVisualInfo *visinfo;
545 qboolean fullscreen = true;
546 int MajorVersion, MinorVersion;
547 int actualWidth, actualHeight;
549 Cvar_RegisterVariable (&vid_mode);
550 Cvar_RegisterVariable (&vid_fullscreen);
551 Cvar_RegisterVariable (&in_mouse);
552 Cvar_RegisterVariable (&in_dga);
553 Cvar_RegisterVariable (&in_dga_mouseaccel);
554 Cvar_RegisterVariable (&m_filter);
556 // interpret command-line params
558 // set vid parameters
559 if ((i = COM_CheckParm("-window")) != 0)
562 if ((i = COM_CheckParm("-width")) != 0)
563 width = atoi(com_argv[i+1]);
565 if ((i = COM_CheckParm("-height")) != 0)
566 height = atoi(com_argv[i+1]);
568 if ((i = COM_CheckParm("-conwidth")) != 0)
569 vid.conwidth = atoi(com_argv[i+1]);
573 vid.conwidth &= 0xfff8; // make it a multiple of eight
575 if (vid.conwidth < 320)
578 // pick a conheight that matches with correct aspect
579 vid.conheight = vid.conwidth*3 / 4;
581 if ((i = COM_CheckParm("-conheight")) != 0)
582 vid.conheight = atoi(com_argv[i+1]);
583 if (vid.conheight < 200)
586 if (!(dpy = XOpenDisplay(NULL))) {
587 fprintf(stderr, "Error couldn't open the X display\n");
591 scrnum = DefaultScreen(dpy);
592 root = RootWindow(dpy, scrnum);
594 // Get video mode list
595 MajorVersion = MinorVersion = 0;
596 if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
599 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
603 visinfo = glXChooseVisual(dpy, scrnum, attrib);
605 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
610 int best_fit, best_dist, dist, x, y;
612 XF86VidModeGetAllModeLines(dpy, scrnum, &num_vidmodes, &vidmodes);
614 // Are we going fullscreen? If so, let's change video mode
619 for (i = 0; i < num_vidmodes; i++) {
620 if (width > vidmodes[i]->hdisplay ||
621 height > vidmodes[i]->vdisplay)
624 x = width - vidmodes[i]->hdisplay;
625 y = height - vidmodes[i]->vdisplay;
626 dist = (x * x) + (y * y);
627 if (dist < best_dist) {
633 if (best_fit != -1) {
634 actualWidth = vidmodes[best_fit]->hdisplay;
635 actualHeight = vidmodes[best_fit]->vdisplay;
637 // change to the mode
638 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[best_fit]);
639 vidmode_active = true;
641 // Move the viewport to top left
642 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
648 /* window attributes */
649 attr.background_pixel = 0;
650 attr.border_pixel = 0;
651 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
652 attr.event_mask = X_MASK;
653 if (vidmode_active) {
654 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore |
655 CWEventMask | CWOverrideRedirect;
656 attr.override_redirect = True;
657 attr.backing_store = NotUseful;
658 attr.save_under = False;
660 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
662 win = XCreateWindow(dpy, root, 0, 0, width, height,
663 0, visinfo->depth, InputOutput,
664 visinfo->visual, mask, &attr);
665 XMapWindow(dpy, win);
667 if (vidmode_active) {
668 XMoveWindow(dpy, win, 0, 0);
669 XRaiseWindow(dpy, win);
670 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
672 // Move the viewport to top left
673 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
678 ctx = glXCreateContext(dpy, visinfo, NULL, True);
680 glXMakeCurrent(dpy, win, ctx);
685 if (vid.conheight > height)
686 vid.conheight = height;
687 if (vid.conwidth > width)
688 vid.conwidth = width;
689 vid.width = vid.conwidth;
690 vid.height = vid.conheight;
692 vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
694 InitSig(); // trap evil signals
698 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
700 vid.recalc_refdef = 1; // force a surface cache flush
705 void Sys_SendKeyEvents(void)
710 void Force_CenterView_f (void)
712 cl.viewangles[PITCH] = 0;
719 void IN_Shutdown(void)
728 void IN_Commands (void)
733 if (vidmode_active || key_dest == key_game)
736 IN_DeactivateMouse ();
744 void IN_MouseMove (usercmd_t *cmd)
752 if (m_filter.value) {
753 mouse_x = (mouse_x + old_mouse_x) * 0.5;
754 mouse_y = (mouse_y + old_mouse_y) * 0.5;
756 old_mouse_x = mouse_x;
757 old_mouse_y = mouse_y;
760 mouse_x *= sensitivity.value;
761 mouse_y *= sensitivity.value;
763 if (in_strafe.state & 1)
764 cmd->sidemove += m_side.value * mouse_x;
766 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
771 if (/*freelook && */!(in_strafe.state & 1)) {
772 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
773 cl.viewangles[PITCH] = bound (-70, cl.viewangles[PITCH], 80);
775 if ((in_strafe.state & 1) && noclip_anglehack)
776 cmd->upmove -= m_forward.value * mouse_y;
778 cmd->forwardmove -= m_forward.value * mouse_y;
780 mouse_x = mouse_y = 0.0;
783 void IN_Move (usercmd_t *cmd)