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 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 /*-----------------------------------------------------------------------*/
78 float gldepthmin, gldepthmax;
80 const char *gl_vendor;
81 const char *gl_renderer;
82 const char *gl_version;
83 const char *gl_extensions;
85 /*-----------------------------------------------------------------------*/
87 XLateKey(XKeyEvent *ev/*, qboolean modified*/)
93 keysym = XLookupKeysym(ev, 0);
97 case XK_KP_Page_Up: key = KP_PGUP; break;
98 case XK_Page_Up: key = K_PGUP; break;
100 case XK_KP_Page_Down: key = KP_PGDN; break;
101 case XK_Page_Down: key = K_PGDN; break;
103 case XK_KP_Home: key = KP_HOME; break;
104 case XK_Home: key = K_HOME; break;
106 case XK_KP_End: key = KP_END; break;
107 case XK_End: key = K_END; break;
109 case XK_KP_Left: key = KP_LEFTARROW; break;
110 case XK_Left: key = K_LEFTARROW; break;
112 case XK_KP_Right: key = KP_RIGHTARROW; break;
113 case XK_Right: key = K_RIGHTARROW; break;
115 case XK_KP_Down: key = KP_DOWNARROW; break;
116 case XK_Down: key = K_DOWNARROW; break;
118 case XK_KP_Up: key = KP_UPARROW; break;
119 case XK_Up: key = K_UPARROW; break;
121 case XK_Escape: key = K_ESCAPE; break;
123 case XK_KP_Enter: key = KP_ENTER; break;
124 case XK_Return: key = K_ENTER; break;
126 case XK_Tab: key = K_TAB; break;
128 case XK_F1: key = K_F1; break;
129 case XK_F2: key = K_F2; break;
130 case XK_F3: key = K_F3; break;
131 case XK_F4: key = K_F4; break;
132 case XK_F5: key = K_F5; break;
133 case XK_F6: key = K_F6; break;
134 case XK_F7: key = K_F7; break;
135 case XK_F8: key = K_F8; break;
136 case XK_F9: key = K_F9; break;
137 case XK_F10: key = K_F10; break;
138 case XK_F11: key = K_F11; break;
139 case XK_F12: key = K_F12; break;
141 case XK_BackSpace: key = K_BACKSPACE; break;
143 case XK_KP_Delete: key = KP_DEL; break;
144 case XK_Delete: key = K_DEL; break;
146 case XK_Pause: key = K_PAUSE; break;
149 case XK_Shift_R: key = K_SHIFT; break;
153 case XK_Control_R: key = K_CTRL; break;
159 case XK_Meta_R: key = K_ALT; break;
161 case XK_Caps_Lock: key = K_CAPSLOCK; break;
162 case XK_KP_Begin: key = KP_5; break;
164 case XK_Insert: key = K_INS; break;
165 case XK_KP_Insert: key = KP_INS; break;
167 case XK_KP_Multiply: key = KP_MULTIPLY; break;
168 case XK_KP_Add: key = KP_PLUS; break;
169 case XK_KP_Subtract: key = KP_MINUS; break;
170 case XK_KP_Divide: key = KP_DIVIDE; break;
172 /* For Sun keyboards */
173 case XK_F27: key = K_HOME; break;
174 case XK_F29: key = K_PGUP; break;
175 case XK_F33: key = K_END; break;
176 case XK_F35: key = K_PGDN; break;
183 if (/*!modified && */((key >= 'A') && (key <= 'Z')))
184 key = key + ('a' - 'A');
192 static Cursor CreateNullCursor(Display *display, Window root)
200 cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
201 xgc.function = GXclear;
202 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
203 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
204 dummycolour.pixel = 0;
206 dummycolour.flags = 04;
207 cursor = XCreatePixmapCursor(display, cursormask, cursormask, &dummycolour,&dummycolour, 0,0);
208 XFreePixmap(display,cursormask);
213 static void install_grabs(void)
215 XWindowAttributes attribs_1;
216 XSetWindowAttributes attribs_2;
218 XGetWindowAttributes(dpy, win, &attribs_1);
219 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
220 XChangeWindowAttributes(dpy, win, CWEventMask, &attribs_2);
223 XDefineCursor(dpy, win, CreateNullCursor(dpy, win));
225 XGrabPointer(dpy, win, True, 0, GrabModeAsync, GrabModeAsync, win, None, CurrentTime);
229 int MajorVersion, MinorVersion;
231 if (!XF86DGAQueryVersion(dpy, &MajorVersion, &MinorVersion))
233 // unable to query, probalby not supported
234 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
240 XF86DGADirectVideo(dpy, DefaultScreen(dpy), XF86DGADirectMouse);
241 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
245 XWarpPointer(dpy, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
247 XGrabKeyboard(dpy, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
250 mouse_x = mouse_y = 0;
255 static void uninstall_grabs(void)
260 if (vid_dga.value == 1)
261 XF86DGADirectVideo(dpy, DefaultScreen(dpy), 0);
263 XUngrabPointer(dpy, CurrentTime);
264 XUngrabKeyboard(dpy, CurrentTime);
267 XUndefineCursor(dpy, win);
269 mouse_active = false;
272 static void HandleEvents(void)
276 qboolean dowarp = false;
281 while (XPending(dpy))
283 XNextEvent(dpy, &event);
289 Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
295 if (vid_dga.value == 1)
297 mouse_x += event.xmotion.x_root * vid_dga_mouseaccel.value;
298 mouse_y += event.xmotion.y_root * vid_dga_mouseaccel.value;
303 if (!p_mouse_x && !p_mouse_y)
305 Con_Printf("event->xmotion.x: %d\n", event.xmotion.x);
306 Con_Printf("event->xmotion.y: %d\n", event.xmotion.y);
311 if (!event.xmotion.send_event)
313 mouse_x += event.xmotion.x - p_mouse_x;
314 mouse_y += event.xmotion.y - p_mouse_y;
315 if (abs(vid.width/2 - event.xmotion.x) > vid.width / 4 || abs(vid.height/2 - event.xmotion.y) > vid.height / 4)
322 mouse_x += (event.xmotion.x - p_mouse_x);
323 mouse_y += (event.xmotion.y - p_mouse_y);
326 p_mouse_x = event.xmotion.x;
327 p_mouse_y = event.xmotion.y;
331 ui_mouseupdate(event.xmotion.x, event.xmotion.y);
335 switch(event.xbutton.button)
338 Key_Event(K_MOUSE1, true);
341 Key_Event(K_MOUSE3, true);
344 Key_Event(K_MOUSE2, true);
347 Key_Event(K_MWHEELUP, true);
350 Key_Event(K_MWHEELDOWN, true);
353 Con_Printf("HandleEvents: ButtonPress gave value %d, 1-5 expected\n", event.xbutton.button);
359 switch(event.xbutton.button)
362 Key_Event(K_MOUSE1, false);
365 Key_Event(K_MOUSE3, false);
368 Key_Event(K_MOUSE2, false);
371 Key_Event(K_MWHEELUP, false);
374 Key_Event(K_MWHEELDOWN, false);
377 Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-5 expected\n", event.xbutton.button);
383 win_x = event.xcreatewindow.x;
384 win_y = event.xcreatewindow.y;
387 case ConfigureNotify :
388 win_x = event.xconfigure.x;
389 win_y = event.xconfigure.y;
396 /* move the mouse to the window center again */
397 p_mouse_x = vid.width / 2;
398 p_mouse_y = vid.height / 2;
399 XWarpPointer(dpy, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
404 static void IN_DeactivateMouse( void )
406 if (!mouse_avail || !dpy || !win)
412 mouse_active = false;
416 static void IN_ActivateMouse( void )
418 if (!mouse_avail || !dpy || !win)
423 mouse_x = mouse_y = 0; // don't spazz
430 void VID_Shutdown(void)
440 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[0]);
441 /* Disabled, causes a segfault during shutdown.
443 glXDestroyContext(dpy, ctx);
446 XDestroyWindow(dpy, win);
449 vidmode_active = false;
455 void signal_handler(int sig)
457 printf("Received signal %d, exiting...\n", sig);
464 signal(SIGHUP, signal_handler);
465 signal(SIGINT, signal_handler);
466 signal(SIGQUIT, signal_handler);
467 signal(SIGILL, signal_handler);
468 signal(SIGTRAP, signal_handler);
469 signal(SIGIOT, signal_handler);
470 signal(SIGBUS, signal_handler);
471 signal(SIGFPE, signal_handler);
472 signal(SIGSEGV, signal_handler);
473 signal(SIGTERM, signal_handler);
476 void VID_CheckMultitexture(void)
479 qglMTexCoord2f = NULL;
480 qglSelectTexture = NULL;
481 // Check to see if multitexture is disabled
482 if (COM_CheckParm("-nomtex"))
484 Con_Printf("...multitexture disabled\n");
487 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
489 Con_Printf("Unable to open symbol list for main program.\n");
492 // Test for ARB_multitexture
493 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
495 Con_Printf("...using GL_ARB_multitexture\n");
496 qglMTexCoord2f = (void *) dlsym(prjobj, "glMultiTexCoord2fARB");
497 qglSelectTexture = (void *) dlsym(prjobj, "glActiveTextureARB");
498 if (qglMTexCoord2f != NULL && qglSelectTexture != NULL)
501 gl_mtex_enum = GL_TEXTURE0_ARB;
505 if (qglMTexCoord2f == NULL)
506 Con_Printf("dlsym(prjobj, \"glMultiTexCoord2fARB\") == NULL!\n");
507 if (qglSelectTexture == NULL)
508 Con_Printf("dlsym(prjobj, \"glActiveTextureARB\") == NULL!\n");
511 if (!gl_mtexable && strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
513 Con_Printf("...using GL_SGIS_multitexture\n");
514 qglMTexCoord2f = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
515 qglSelectTexture = (void *) dlsym(prjobj, "glSelectTextureSGIS");
516 if (qglMTexCoord2f != NULL && qglSelectTexture != NULL)
519 gl_mtex_enum = TEXTURE0_SGIS;
523 if (qglMTexCoord2f == NULL)
524 Con_Printf("dlsym(prjobj, \"glMTexCoord2fSGIS\") == NULL!\n");
525 if (qglSelectTexture == NULL)
526 Con_Printf("dlsym(prjobj, \"glSelectTextureSGIS\") == NULL!\n");
530 Con_Printf("...multitexture disabled (not detected)\n");
534 void VID_CheckCVA(void)
537 qglLockArraysEXT = NULL;
538 qglUnlockArraysEXT = NULL;
539 gl_supportslockarrays = false;
540 if (COM_CheckParm("-nocva"))
542 Con_Printf("...compiled vertex arrays disabled\n");
545 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
547 Con_Printf("Unable to open symbol list for main program.\n");
550 if (strstr(gl_extensions, "GL_EXT_compiled_vertex_array"))
552 Con_Printf("...using compiled vertex arrays\n");
553 qglLockArraysEXT = (void *) dlsym(prjobj, "glLockArraysEXT");
554 qglUnlockArraysEXT = (void *) dlsym(prjobj, "glUnlockArraysEXT");
555 if (qglLockArraysEXT != NULL && qglUnlockArraysEXT != NULL)
556 gl_supportslockarrays = true;
559 if (qglLockArraysEXT == NULL)
560 Con_Printf("dlsym(prjobj, \"glLockArraysEXT\") == NULL!\n");
561 if (qglUnlockArraysEXT == NULL)
562 Con_Printf("dlsym(prjobj, \"glUnlockArraysEXT\") == NULL!\n");
563 Con_Printf("...compiled vertex arrays disabled\n");
575 void GL_BeginRendering (int *x, int *y, int *width, int *height)
579 *height = scr_height;
581 // glViewport (*x, *y, *width, *height);
585 void GL_EndRendering (void)
591 glXSwapBuffers(dpy, win);
593 // handle the mouse state when windowed if that's changed
595 if (vid_mouse.value && key_dest == key_game)
612 IN_DeactivateMouse ();
617 int VID_SetGamma(float prescale, float gamma, float scale, float base)
635 // char gldir[MAX_OSPATH];
636 int width = 640, height = 480;
637 XSetWindowAttributes attr;
640 XVisualInfo *visinfo;
641 qboolean fullscreen = true;
642 int MajorVersion, MinorVersion;
643 int actualWidth, actualHeight;
645 Cvar_RegisterVariable (&vid_mouse);
646 Cvar_RegisterVariable (&vid_dga);
647 Cvar_RegisterVariable (&vid_dga_mouseaccel);
648 Cvar_RegisterVariable (&m_filter);
650 // interpret command-line params
652 // set vid parameters
653 if ((i = COM_CheckParm("-window")) != 0)
656 if ((i = COM_CheckParm("-width")) != 0)
657 width = atoi(com_argv[i+1]);
659 if ((i = COM_CheckParm("-height")) != 0)
660 height = atoi(com_argv[i+1]);
662 if ((i = COM_CheckParm("-conwidth")) != 0)
663 vid.conwidth = atoi(com_argv[i+1]);
667 vid.conwidth &= 0xfff8; // make it a multiple of eight
669 if (vid.conwidth < 320)
672 // pick a conheight that matches with correct aspect
673 vid.conheight = vid.conwidth*3 / 4;
675 if ((i = COM_CheckParm("-conheight")) != 0)
676 vid.conheight = atoi(com_argv[i+1]);
677 if (vid.conheight < 200)
680 if (!(dpy = XOpenDisplay(NULL)))
682 fprintf(stderr, "Error couldn't open the X display\n");
686 scrnum = DefaultScreen(dpy);
687 root = RootWindow(dpy, scrnum);
689 // Get video mode list
690 MajorVersion = MinorVersion = 0;
691 if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion))
695 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
699 visinfo = glXChooseVisual(dpy, scrnum, attrib);
702 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
708 int best_fit, best_dist, dist, x, y;
710 XF86VidModeGetAllModeLines(dpy, scrnum, &num_vidmodes, &vidmodes);
712 // Are we going fullscreen? If so, let's change video mode
718 for (i = 0; i < num_vidmodes; i++)
720 if (width > vidmodes[i]->hdisplay || height > vidmodes[i]->vdisplay)
723 x = width - vidmodes[i]->hdisplay;
724 y = height - vidmodes[i]->vdisplay;
725 dist = (x * x) + (y * y);
726 if (dist < best_dist)
735 actualWidth = vidmodes[best_fit]->hdisplay;
736 actualHeight = vidmodes[best_fit]->vdisplay;
738 // change to the mode
739 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[best_fit]);
740 vidmode_active = true;
742 // Move the viewport to top left
743 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
750 /* window attributes */
751 attr.background_pixel = 0;
752 attr.border_pixel = 0;
753 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
754 attr.event_mask = X_MASK;
757 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore | CWEventMask | CWOverrideRedirect;
758 attr.override_redirect = True;
759 attr.backing_store = NotUseful;
760 attr.save_under = False;
763 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
765 win = XCreateWindow(dpy, root, 0, 0, width, height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
766 XStoreName(dpy, win, "DarkPlaces-GLX");
767 XMapWindow(dpy, win);
771 XMoveWindow(dpy, win, 0, 0);
772 XRaiseWindow(dpy, win);
773 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
775 // Move the viewport to top left
776 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
781 ctx = glXCreateContext(dpy, visinfo, NULL, True);
783 glXMakeCurrent(dpy, win, ctx);
788 if (vid.conheight > height)
789 vid.conheight = height;
790 if (vid.conwidth > width)
791 vid.conwidth = width;
792 vid.width = vid.conwidth;
793 vid.height = vid.conheight;
795 InitSig(); // trap evil signals
799 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
801 // force a surface cache flush
802 vid.recalc_refdef = 1;
805 void Sys_SendKeyEvents(void)
814 void IN_Shutdown(void)
823 void IN_Commands (void)
832 void IN_MouseMove (usercmd_t *cmd)
839 mouse_x = (mouse_x + old_mouse_x) * 0.5;
840 mouse_y = (mouse_y + old_mouse_y) * 0.5;
842 old_mouse_x = mouse_x;
843 old_mouse_y = mouse_y;
846 mouse_x *= sensitivity.value;
847 mouse_y *= sensitivity.value;
849 if (in_strafe.state & 1)
850 cmd->sidemove += m_side.value * mouse_x;
852 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
857 if (/*freelook && */!(in_strafe.state & 1))
859 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
860 cl.viewangles[PITCH] = bound (-70, cl.viewangles[PITCH], 80);
864 if ((in_strafe.state & 1) && noclip_anglehack)
865 cmd->upmove -= m_forward.value * mouse_y;
867 cmd->forwardmove -= m_forward.value * mouse_y;
869 mouse_x = mouse_y = 0.0;
872 void IN_Move (usercmd_t *cmd)