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);
473 void (*qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
474 void (*qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
475 void (*qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
476 void (*qglArrayElement)(GLint i);
477 void (*qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
478 void (*qglMTexCoord2f)(GLenum, GLfloat, GLfloat);
479 void (*qglSelectTexture)(GLenum);
481 void VID_CheckVertexArrays (void)
484 if (COM_CheckParm("-novertex"))
486 Con_Printf("...vertex array support disabled\n");
489 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
491 Con_Printf("Unable to open symbol list for main program.\n");
494 qglMTexCoord2fSGIS = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
495 if ((qglArrayElement = (void *) dlsym(prjobj, "glArrayElement"))
496 && (qglColorPointer = (void *) dlsym(prjobj, "glColorPointer"))
497 // && (qglDrawArrays = (void *) dlsym(prjobj, "glDrawArrays"))
498 && (qglDrawElements = (void *) dlsym(prjobj, "glDrawElements"))
499 // && (qglInterleavedArrays = (void *) dlsym(prjobj, "glInterleavedArrays"))
500 && (qglTexCoordPointer = (void *) dlsym(prjobj, "glTexCoordPointer"))
501 && (qglVertexPointer = (void *) dlsym(prjobj, "glVertexPointer"))
504 Con_Printf("...vertex array support detected\n");
510 Con_Printf("...vertex array support disabled (not detected - get a better driver)\n");
515 // LordHavoc: require OpenGL 1.2.x
516 void VID_CheckVertexArrays (void)
521 void VID_CheckMultitexture(void)
524 qglMTexCoord2f = NULL;
525 qglSelectTexture = NULL;
526 // Check to see if multitexture is disabled
527 if (COM_CheckParm("-nomtex"))
529 Con_Printf("...multitexture disabled\n");
532 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
534 Con_Printf("Unable to open symbol list for main program.\n");
537 // Test for ARB_multitexture
538 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
540 Con_Printf("...using GL_ARB_multitexture\n");
541 qglMTexCoord2f = (void *) dlsym(prjobj, "glMultiTexCoord2fARB");
542 qglSelectTexture = (void *) dlsym(prjobj, "glActiveTextureARB");
544 gl_mtex_enum = GL_TEXTURE0_ARB;
546 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
548 Con_Printf("...using GL_SGIS_multitexture\n");
549 qglMTexCoord2f = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
550 qglSelectTexture = (void *) dlsym(prjobj, "glSelectTextureSGIS");
552 gl_mtex_enum = TEXTURE0_SGIS;
555 Con_Printf("...multitexture disabled (not detected)\n");
565 void GL_BeginRendering (int *x, int *y, int *width, int *height)
569 *height = scr_height;
571 // glViewport (*x, *y, *width, *height);
575 void GL_EndRendering (void)
580 glXSwapBuffers(dpy, win);
595 // char gldir[MAX_OSPATH];
596 int width = 640, height = 480;
597 XSetWindowAttributes attr;
600 XVisualInfo *visinfo;
601 qboolean fullscreen = true;
602 int MajorVersion, MinorVersion;
603 int actualWidth, actualHeight;
605 Cvar_RegisterVariable (&vid_mode);
606 Cvar_RegisterVariable (&in_mouse);
607 Cvar_RegisterVariable (&in_dgamouse);
608 Cvar_RegisterVariable (&m_filter);
610 // interpret command-line params
612 // set vid parameters
613 if ((i = COM_CheckParm("-window")) != 0)
616 if ((i = COM_CheckParm("-width")) != 0)
617 width = atoi(com_argv[i+1]);
619 if ((i = COM_CheckParm("-height")) != 0)
620 height = atoi(com_argv[i+1]);
622 if ((i = COM_CheckParm("-conwidth")) != 0)
623 vid.conwidth = atoi(com_argv[i+1]);
627 vid.conwidth &= 0xfff8; // make it a multiple of eight
629 if (vid.conwidth < 320)
632 // pick a conheight that matches with correct aspect
633 vid.conheight = vid.conwidth*3 / 4;
635 if ((i = COM_CheckParm("-conheight")) != 0)
636 vid.conheight = atoi(com_argv[i+1]);
637 if (vid.conheight < 200)
640 if (!(dpy = XOpenDisplay(NULL))) {
641 fprintf(stderr, "Error couldn't open the X display\n");
645 scrnum = DefaultScreen(dpy);
646 root = RootWindow(dpy, scrnum);
648 // Get video mode list
649 MajorVersion = MinorVersion = 0;
650 if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
653 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
657 visinfo = glXChooseVisual(dpy, scrnum, attrib);
659 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
664 int best_fit, best_dist, dist, x, y;
666 XF86VidModeGetAllModeLines(dpy, scrnum, &num_vidmodes, &vidmodes);
668 // Are we going fullscreen? If so, let's change video mode
673 for (i = 0; i < num_vidmodes; i++) {
674 if (width > vidmodes[i]->hdisplay ||
675 height > vidmodes[i]->vdisplay)
678 x = width - vidmodes[i]->hdisplay;
679 y = height - vidmodes[i]->vdisplay;
680 dist = (x * x) + (y * y);
681 if (dist < best_dist) {
687 if (best_fit != -1) {
688 actualWidth = vidmodes[best_fit]->hdisplay;
689 actualHeight = vidmodes[best_fit]->vdisplay;
691 // change to the mode
692 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[best_fit]);
693 vidmode_active = true;
695 // Move the viewport to top left
696 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
702 /* window attributes */
703 attr.background_pixel = 0;
704 attr.border_pixel = 0;
705 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
706 attr.event_mask = X_MASK;
707 if (vidmode_active) {
708 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore |
709 CWEventMask | CWOverrideRedirect;
710 attr.override_redirect = True;
711 attr.backing_store = NotUseful;
712 attr.save_under = False;
714 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
716 win = XCreateWindow(dpy, root, 0, 0, width, height,
717 0, visinfo->depth, InputOutput,
718 visinfo->visual, mask, &attr);
719 XMapWindow(dpy, win);
721 if (vidmode_active) {
722 XMoveWindow(dpy, win, 0, 0);
723 XRaiseWindow(dpy, win);
724 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
726 // Move the viewport to top left
727 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
732 ctx = glXCreateContext(dpy, visinfo, NULL, True);
734 glXMakeCurrent(dpy, win, ctx);
739 if (vid.conheight > height)
740 vid.conheight = height;
741 if (vid.conwidth > width)
742 vid.conwidth = width;
743 vid.width = vid.conwidth;
744 vid.height = vid.conheight;
746 vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
748 InitSig(); // trap evil signals
752 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
754 vid.recalc_refdef = 1; // force a surface cache flush
759 void Sys_SendKeyEvents(void)
764 void Force_CenterView_f (void)
766 cl.viewangles[PITCH] = 0;
773 void IN_Shutdown(void)
782 void IN_Commands (void)
787 if (vidmode_active || key_dest == key_game)
790 IN_DeactivateMouse ();
798 void IN_MouseMove (usercmd_t *cmd)
806 if (m_filter.value) {
807 mouse_x = (mouse_x + old_mouse_x) * 0.5;
808 mouse_y = (mouse_y + old_mouse_y) * 0.5;
810 old_mouse_x = mouse_x;
811 old_mouse_y = mouse_y;
814 mouse_x *= sensitivity.value;
815 mouse_y *= sensitivity.value;
817 if (in_strafe.state & 1)
818 cmd->sidemove += m_side.value * mouse_x;
820 cl.viewangles[YAW] -= m_yaw.value * mouse_x;
825 if (/*freelook && */!(in_strafe.state & 1)) {
826 cl.viewangles[PITCH] += m_pitch.value * mouse_y;
827 cl.viewangles[PITCH] = bound (-70, cl.viewangles[PITCH], 80);
829 if ((in_strafe.state & 1) && noclip_anglehack)
830 cmd->upmove -= m_forward.value * mouse_y;
832 cmd->forwardmove -= m_forward.value * mouse_y;
834 mouse_x = mouse_y = 0.0;
837 void IN_Move (usercmd_t *cmd)