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/xf86dga.h>
38 #include <X11/extensions/xf86vmode.h>
40 static Display *dpy = NULL;
43 static GLXContext ctx = NULL;
45 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
46 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
47 PointerMotionMask | ButtonMotionMask )
48 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | StructureNotifyMask )
51 unsigned d_8to24table[256];
52 unsigned char d_15to8table[65536];
54 cvar_t vid_mode = {"vid_mode","0",false};
56 viddef_t vid; // global video state
58 static qboolean mouse_avail = true;
59 static qboolean mouse_active = false;
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_dgamouse = {"in_dgamouse", "1", false};
66 static cvar_t m_filter = {"m_filter", "0"};
68 qboolean dgamouse = false;
69 qboolean vidmode_ext = false;
71 static int win_x, win_y;
73 static int scr_width, scr_height;
75 static XF86VidModeModeInfo **vidmodes;
76 //static int default_dotclock_vidmode;
77 static int num_vidmodes;
78 static qboolean vidmode_active = false;
80 /*-----------------------------------------------------------------------*/
82 int texture_extension_number = 1;
84 float gldepthmin, gldepthmax;
86 const char *gl_vendor;
87 const char *gl_renderer;
88 const char *gl_version;
89 const char *gl_extensions;
91 void (*qglMTexCoord2f) (GLenum, GLfloat, GLfloat);
92 void (*qglSelectTexture) (GLenum);
94 //static float vid_gamma = 1.0;
96 // LordHavoc: ARB multitexture support
99 // LordHavoc: in GLX these are never set, simply provided to make the rest of the code work
100 qboolean isG200 = false;
101 qboolean isRagePro = false;
102 qboolean gl_mtexable = false;
103 qboolean gl_arrays = false;
105 /*-----------------------------------------------------------------------*/
107 XLateKey(XKeyEvent *ev/*, qboolean modified*/)
113 /* if (!modified) {*/
114 keysym = XLookupKeysym(ev, 0);
116 XLookupString(ev, tmp, 1, &keysym, NULL);
120 case XK_KP_Page_Up: key = KP_PGUP; break;
121 case XK_Page_Up: key = K_PGUP; break;
123 case XK_KP_Page_Down: key = KP_PGDN; break;
124 case XK_Page_Down: key = K_PGDN; break;
126 case XK_KP_Home: key = KP_HOME; break;
127 case XK_Home: key = K_HOME; break;
129 case XK_KP_End: key = KP_END; break;
130 case XK_End: key = K_END; break;
132 case XK_KP_Left: key = KP_LEFTARROW; break;
133 case XK_Left: key = K_LEFTARROW; break;
135 case XK_KP_Right: key = KP_RIGHTARROW; break;
136 case XK_Right: key = K_RIGHTARROW; break;
138 case XK_KP_Down: key = KP_DOWNARROW; break;
139 case XK_Down: key = K_DOWNARROW; break;
141 case XK_KP_Up: key = KP_UPARROW; break;
142 case XK_Up: key = K_UPARROW; break;
144 case XK_Escape: key = K_ESCAPE; break;
146 case XK_KP_Enter: key = KP_ENTER; break;
147 case XK_Return: key = K_ENTER; break;
149 case XK_Tab: key = K_TAB; break;
151 case XK_F1: key = K_F1; break;
152 case XK_F2: key = K_F2; break;
153 case XK_F3: key = K_F3; break;
154 case XK_F4: key = K_F4; break;
155 case XK_F5: key = K_F5; break;
156 case XK_F6: key = K_F6; break;
157 case XK_F7: key = K_F7; break;
158 case XK_F8: key = K_F8; break;
159 case XK_F9: key = K_F9; break;
160 case XK_F10: key = K_F10; break;
161 case XK_F11: key = K_F11; break;
162 case XK_F12: key = K_F12; break;
164 case XK_BackSpace: key = K_BACKSPACE; break;
166 case XK_KP_Delete: key = KP_DEL; break;
167 case XK_Delete: key = K_DEL; break;
169 case XK_Pause: key = K_PAUSE; break;
172 case XK_Shift_R: key = K_SHIFT; break;
176 case XK_Control_R: key = K_CTRL; break;
182 case XK_Meta_R: key = K_ALT; break;
184 case XK_Caps_Lock: key = K_CAPSLOCK; break;
185 case XK_KP_Begin: key = KP_5; break;
187 case XK_Insert: key = K_INS; break;
188 case XK_KP_Insert: key = KP_INS; break;
190 case XK_KP_Multiply: key = KP_MULTIPLY; break;
191 case XK_KP_Add: key = KP_PLUS; break;
192 case XK_KP_Subtract: key = KP_MINUS; break;
193 case XK_KP_Divide: key = KP_DIVIDE; break;
195 /* For Sun keyboards */
196 case XK_F27: key = K_HOME; break;
197 case XK_F29: key = K_PGUP; break;
198 case XK_F33: key = K_END; break;
199 case XK_F35: key = K_PGDN; break;
205 if (/*!modified && */((key >= 'A') && (key <= 'Z'))) {
206 key = key + ('a' - 'A');
215 static Cursor CreateNullCursor(Display *display, Window root)
223 cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
224 xgc.function = GXclear;
225 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
226 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
227 dummycolour.pixel = 0;
229 dummycolour.flags = 04;
230 cursor = XCreatePixmapCursor(display, cursormask, cursormask,
231 &dummycolour,&dummycolour, 0,0);
232 XFreePixmap(display,cursormask);
237 static void install_grabs(void)
239 XWindowAttributes attribs_1;
240 XSetWindowAttributes attribs_2;
242 XGetWindowAttributes(dpy, win, &attribs_1);
243 attribs_2.event_mask = attribs_1.your_event_mask | KEY_MASK | MOUSE_MASK;
244 XChangeWindowAttributes(dpy, win, CWEventMask, &attribs_2);
247 XDefineCursor(dpy, win, CreateNullCursor(dpy, win));
249 XGrabPointer(dpy, win, True, 0, GrabModeAsync, GrabModeAsync,
250 win, None, CurrentTime);
252 /* if (in_dgamouse.value) {
253 int MajorVersion, MinorVersion;
255 if (!XF86DGAQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
256 // unable to query, probalby not supported
257 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
258 in_dgamouse.value = 0;
261 XF86DGADirectVideo(dpy, DefaultScreen(dpy), XF86DGADirectMouse);
262 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
265 XWarpPointer(dpy, None, win,
267 vid.width / 2, vid.height / 2);
270 XGrabKeyboard(dpy, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
273 mouse_x = mouse_y = 0;
278 static void uninstall_grabs(void)
285 XF86DGADirectVideo(dpy, DefaultScreen(dpy), 0);
288 XUngrabPointer(dpy, CurrentTime);
289 XUngrabKeyboard(dpy, CurrentTime);
292 XUndefineCursor(dpy, win);
294 mouse_active = false;
297 static void HandleEvents(void)
302 qboolean dowarp = false;
307 while (XPending(dpy)) {
308 XNextEvent(dpy, &event);
310 switch (event.type) {
313 Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
318 mouse_x += event.xmotion.x_root/* * in_dga_mouseaccel.value*/;
319 mouse_y += event.xmotion.y_root/* * in_dga_mouseaccel.value*/;
321 if (!p_mouse_x && !p_mouse_y) {
322 Con_Printf("event->xmotion.x: %d\n", event.xmotion.x);
323 Con_Printf("event->xmotion.y: %d\n", event.xmotion.y);
325 /* if (vid_fullscreen.value || _windowed_mouse.value) {*/
326 if (!event.xmotion.send_event) {
327 mouse_x += (event.xmotion.x - p_mouse_x);
328 mouse_y += (event.xmotion.y - p_mouse_y);
329 if (abs(vid.width/2 - event.xmotion.x) > vid.width / 4
330 || abs(vid.height/2 - event.xmotion.y) > vid.height / 4) {
335 mouse_x += (event.xmotion.x - p_mouse_x);
336 mouse_y += (event.xmotion.y - p_mouse_y);
338 p_mouse_x = event.xmotion.x;
339 p_mouse_y = event.xmotion.y;
342 /* if (mouse_active) {
344 mouse_x += (event.xmotion.x + win_x) * 2;
345 mouse_y += (event.xmotion.y + win_y) * 2;
349 mouse_x += ((int)event.xmotion.x - mwx) * 2;
350 mouse_y += ((int)event.xmotion.y - mwy) * 2;
351 mwx = event.xmotion.x;
352 mwy = event.xmotion.y;
354 if (mouse_x || mouse_y)
362 if (event.xbutton.button == 1)
364 else if (event.xbutton.button == 2)
366 else if (event.xbutton.button == 3)
369 Key_Event(K_MOUSE1 + b, true);
374 if (event.xbutton.button == 1)
376 else if (event.xbutton.button == 2)
378 else if (event.xbutton.button == 3)
381 Key_Event(K_MOUSE1 + b, false);
385 win_x = event.xcreatewindow.x;
386 win_y = event.xcreatewindow.y;
389 case ConfigureNotify :
390 win_x = event.xconfigure.x;
391 win_y = event.xconfigure.y;
397 /* move the mouse to the window center again */
398 p_mouse_x = vid.width / 2;
399 p_mouse_y = vid.height / 2;
400 XWarpPointer(dpy, None, win, 0, 0, 0, 0, p_mouse_x, p_mouse_y);
405 static void IN_DeactivateMouse( void )
407 if (!mouse_avail || !dpy || !win)
412 mouse_active = false;
416 static void IN_ActivateMouse( void )
418 if (!mouse_avail || !dpy || !win)
422 mouse_x = mouse_y = 0; // don't spazz
429 void VID_Shutdown(void)
438 glXDestroyContext(dpy, ctx);
440 XDestroyWindow(dpy, win);
442 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[0]);
445 vidmode_active = false;
451 void signal_handler(int sig)
453 printf("Received signal %d, exiting...\n", sig);
460 signal(SIGHUP, signal_handler);
461 signal(SIGINT, signal_handler);
462 signal(SIGQUIT, signal_handler);
463 signal(SIGILL, signal_handler);
464 signal(SIGTRAP, signal_handler);
465 signal(SIGIOT, signal_handler);
466 signal(SIGBUS, signal_handler);
467 signal(SIGFPE, signal_handler);
468 signal(SIGSEGV, signal_handler);
469 signal(SIGTERM, signal_handler);
472 void (*qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
473 void (*qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
474 void (*qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
475 void (*qglArrayElement)(GLint i);
476 void (*qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
477 void (*qglMTexCoord2f)(GLenum, GLfloat, GLfloat);
478 void (*qglSelectTexture)(GLenum);
480 void VID_CheckVertexArrays (void)
483 if (COM_CheckParm("-novertex"))
485 Con_Printf("...vertex array support disabled\n");
488 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
490 Con_Printf("Unable to open symbol list for main program.\n");
493 qglMTexCoord2fSGIS = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
494 if ((qglArrayElement = (void *) dlsym(prjobj, "glArrayElement"))
495 && (qglColorPointer = (void *) dlsym(prjobj, "glColorPointer"))
496 // && (qglDrawArrays = (void *) dlsym(prjobj, "glDrawArrays"))
497 && (qglDrawElements = (void *) dlsym(prjobj, "glDrawElements"))
498 // && (qglInterleavedArrays = (void *) dlsym(prjobj, "glInterleavedArrays"))
499 && (qglTexCoordPointer = (void *) dlsym(prjobj, "glTexCoordPointer"))
500 && (qglVertexPointer = (void *) dlsym(prjobj, "glVertexPointer"))
503 Con_Printf("...vertex array support detected\n");
509 Con_Printf("...vertex array support disabled (not detected - get a better driver)\n");
513 void VID_CheckMultiTexture(void)
516 qglMTexCoord2f = NULL;
517 qglSelectTexture = NULL;
518 // Check to see if multitexture is disabled
519 if (COM_CheckParm("-nomtex"))
521 Con_Printf("...multitexture disabled\n");
524 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
526 Con_Printf("Unable to open symbol list for main program.\n");
529 // Test for ARB_multitexture
530 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
532 Con_Printf("...using GL_ARB_multitexture\n");
533 qglMTexCoord2f = (void *) dlsym(prjobj, "glMultiTexCoord2fARB");
534 qglSelectTexture = (void *) dlsym(prjobj, "glActiveTextureARB");
536 gl_mtex_enum = GL_TEXTURE0_ARB;
538 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
540 Con_Printf("...using GL_SGIS_multitexture\n");
541 qglMTexCoord2f = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
542 qglSelectTexture = (void *) dlsym(prjobj, "glSelectTextureSGIS");
544 gl_mtex_enum = TEXTURE0_SGIS;
547 Con_Printf("...multitexture disabled (not detected)\n");
557 void GL_BeginRendering (int *x, int *y, int *width, int *height)
561 *height = scr_height;
563 // glViewport (*x, *y, *width, *height);
567 void GL_EndRendering (void)
572 glXSwapBuffers(dpy, win);
587 // char gldir[MAX_OSPATH];
588 int width = 640, height = 480;
589 XSetWindowAttributes attr;
592 XVisualInfo *visinfo;
593 qboolean fullscreen = true;
594 int MajorVersion, MinorVersion;
595 int actualWidth, actualHeight;
597 Cvar_RegisterVariable (&vid_mode);
598 Cvar_RegisterVariable (&in_mouse);
599 Cvar_RegisterVariable (&in_dgamouse);
600 Cvar_RegisterVariable (&m_filter);
602 // interpret command-line params
604 // set vid parameters
605 if ((i = COM_CheckParm("-window")) != 0)
608 if ((i = COM_CheckParm("-width")) != 0)
609 width = atoi(com_argv[i+1]);
611 if ((i = COM_CheckParm("-height")) != 0)
612 height = atoi(com_argv[i+1]);
614 if ((i = COM_CheckParm("-conwidth")) != 0)
615 vid.conwidth = atoi(com_argv[i+1]);
619 vid.conwidth &= 0xfff8; // make it a multiple of eight
621 if (vid.conwidth < 320)
624 // pick a conheight that matches with correct aspect
625 vid.conheight = vid.conwidth*3 / 4;
627 if ((i = COM_CheckParm("-conheight")) != 0)
628 vid.conheight = atoi(com_argv[i+1]);
629 if (vid.conheight < 200)
632 if (!(dpy = XOpenDisplay(NULL))) {
633 fprintf(stderr, "Error couldn't open the X display\n");
637 scrnum = DefaultScreen(dpy);
638 root = RootWindow(dpy, scrnum);
640 // Get video mode list
641 MajorVersion = MinorVersion = 0;
642 if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
645 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
649 visinfo = glXChooseVisual(dpy, scrnum, attrib);
651 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
656 int best_fit, best_dist, dist, x, y;
658 XF86VidModeGetAllModeLines(dpy, scrnum, &num_vidmodes, &vidmodes);
660 // Are we going fullscreen? If so, let's change video mode
665 for (i = 0; i < num_vidmodes; i++) {
666 if (width > vidmodes[i]->hdisplay ||
667 height > vidmodes[i]->vdisplay)
670 x = width - vidmodes[i]->hdisplay;
671 y = height - vidmodes[i]->vdisplay;
672 dist = (x * x) + (y * y);
673 if (dist < best_dist) {
679 if (best_fit != -1) {
680 actualWidth = vidmodes[best_fit]->hdisplay;
681 actualHeight = vidmodes[best_fit]->vdisplay;
683 // change to the mode
684 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[best_fit]);
685 vidmode_active = true;
687 // Move the viewport to top left
688 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
694 /* window attributes */
695 attr.background_pixel = 0;
696 attr.border_pixel = 0;
697 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
698 attr.event_mask = X_MASK;
699 if (vidmode_active) {
700 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore |
701 CWEventMask | CWOverrideRedirect;
702 attr.override_redirect = True;
703 attr.backing_store = NotUseful;
704 attr.save_under = False;
706 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
708 win = XCreateWindow(dpy, root, 0, 0, width, height,
709 0, visinfo->depth, InputOutput,
710 visinfo->visual, mask, &attr);
711 XMapWindow(dpy, win);
713 if (vidmode_active) {
714 XMoveWindow(dpy, win, 0, 0);
715 XRaiseWindow(dpy, win);
716 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
718 // Move the viewport to top left
719 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
724 ctx = glXCreateContext(dpy, visinfo, NULL, True);
726 glXMakeCurrent(dpy, win, ctx);
731 if (vid.conheight > height)
732 vid.conheight = height;
733 if (vid.conwidth > width)
734 vid.conwidth = width;
735 vid.width = vid.conwidth;
736 vid.height = vid.conheight;
738 vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
740 InitSig(); // trap evil signals
744 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
746 vid.recalc_refdef = 1; // force a surface cache flush
751 void Sys_SendKeyEvents(void)
756 void Force_CenterView_f (void)
758 cl.viewangles[PITCH] = 0;
765 void IN_Shutdown(void)
774 void IN_Commands (void)
779 if (vidmode_active || key_dest == key_game)
782 IN_DeactivateMouse ();
790 void IN_MouseMove (usercmd_t *cmd)
798 if (m_filter.value) {
799 mouse_x = (mouse_x + old_mouse_x) * 0.5;
800 mouse_y = (mouse_y + old_mouse_y) * 0.5;
802 old_mouse_x = mouse_x;
803 old_mouse_y = mouse_y;
806 mouse_x *= sensitivity.value;
807 mouse_y *= sensitivity.value;
809 if (in_strafe.state & 1)
810 cmd->sidemove += m_side.value * mouse_x;
812 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
817 if (/*freelook && */!(in_strafe.state & 1)) {
818 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
819 cl.viewangles[PITCH] = bound (-70, cl.viewangles[PITCH], 80);
821 if ((in_strafe.state & 1) && noclip_anglehack)
822 cmd->upmove -= m_forward.value * mouse_y;
824 cmd->forwardmove -= m_forward.value * mouse_y;
826 mouse_x = mouse_y = 0.0;
829 void IN_Move (usercmd_t *cmd)