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 #define WARP_WIDTH 320
41 #define WARP_HEIGHT 200
43 static Display *dpy = NULL;
46 static GLXContext ctx = NULL;
48 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
49 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
50 PointerMotionMask | ButtonMotionMask )
51 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | StructureNotifyMask )
54 unsigned short d_8to16table[256];
55 unsigned d_8to24table[256];
56 unsigned char d_15to8table[65536];
58 cvar_t vid_mode = {"vid_mode","0",false};
60 viddef_t vid; // global video state
62 static qboolean mouse_avail;
63 static qboolean mouse_active;
65 static int old_mouse_x, old_mouse_y;
67 static cvar_t in_mouse = {"in_mouse", "1", false};
68 static cvar_t in_dgamouse = {"in_dgamouse", "1", false};
69 static cvar_t m_filter = {"m_filter", "0"};
71 qboolean dgamouse = false;
72 qboolean vidmode_ext = false;
74 static int win_x, win_y;
76 static int scr_width, scr_height;
78 static XF86VidModeModeInfo **vidmodes;
79 //static int default_dotclock_vidmode;
80 static int num_vidmodes;
81 static qboolean vidmode_active = false;
83 /*-----------------------------------------------------------------------*/
85 int texture_extension_number = 1;
87 float gldepthmin, gldepthmax;
89 const char *gl_vendor;
90 const char *gl_renderer;
91 const char *gl_version;
92 const char *gl_extensions;
94 void (*qglColorTableEXT) (int, int, int, int, int, const void*);
95 void (*qgl3DfxSetPaletteEXT) (GLuint *);
96 void (*qglMTexCoord2f) (GLenum, GLfloat, GLfloat);
97 void (*qglSelectTexture) (GLenum);
99 //static float vid_gamma = 1.0;
101 // LordHavoc: ARB multitexture support
102 int gl_mtex_enum = 0;
104 // LordHavoc: in GLX these are never set, simply provided to make the rest of the code work
105 qboolean is8bit = false;
106 qboolean isPermedia = false;
107 qboolean isATI = false;
108 qboolean isG200 = false;
109 qboolean isRagePro = false;
110 qboolean gl_mtexable = false;
111 qboolean gl_arrays = false;
113 /*-----------------------------------------------------------------------*/
114 void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
118 void D_EndDirectRect (int x, int y, int width, int height)
122 static int XLateKey(XKeyEvent *ev)
131 XLookupString(ev, buf, sizeof buf, &keysym, 0);
136 case XK_Page_Up: key = K_PGUP; break;
138 case XK_KP_Page_Down:
139 case XK_Page_Down: key = K_PGDN; break;
142 case XK_Home: key = K_HOME; break;
145 case XK_End: key = K_END; break;
148 case XK_Left: key = K_LEFTARROW; break;
151 case XK_Right: key = K_RIGHTARROW; break;
154 case XK_Down: key = K_DOWNARROW; break;
157 case XK_Up: key = K_UPARROW; break;
159 case XK_Escape: key = K_ESCAPE; break;
162 case XK_Return: key = K_ENTER; break;
164 case XK_Tab: key = K_TAB; break;
166 case XK_F1: key = K_F1; break;
168 case XK_F2: key = K_F2; break;
170 case XK_F3: key = K_F3; break;
172 case XK_F4: key = K_F4; break;
174 case XK_F5: key = K_F5; break;
176 case XK_F6: key = K_F6; break;
178 case XK_F7: key = K_F7; break;
180 case XK_F8: key = K_F8; break;
182 case XK_F9: key = K_F9; break;
184 case XK_F10: key = K_F10; break;
186 case XK_F11: key = K_F11; break;
188 case XK_F12: key = K_F12; break;
190 case XK_BackSpace: key = K_BACKSPACE; break;
193 case XK_Delete: key = K_DEL; break;
195 case XK_Pause: key = K_PAUSE; break;
198 case XK_Shift_R: key = K_SHIFT; break;
202 case XK_Control_R: key = K_CTRL; break;
207 case XK_Meta_R: key = K_ALT; break;
209 case XK_KP_Begin: key = '5'; break;
212 case XK_Insert:key = K_INS; break;
214 case XK_KP_Multiply: key = '*'; break;
215 case XK_KP_Add: key = '+'; break;
216 case XK_KP_Subtract: key = '-'; break;
217 case XK_KP_Divide: key = '/'; break;
220 case 0x021: key = '1';break;/* [!] */
221 case 0x040: key = '2';break;/* [@] */
222 case 0x023: key = '3';break;/* [#] */
223 case 0x024: key = '4';break;/* [$] */
224 case 0x025: key = '5';break;/* [%] */
225 case 0x05e: key = '6';break;/* [^] */
226 case 0x026: key = '7';break;/* [&] */
227 case 0x02a: key = '8';break;/* [*] */
228 case 0x028: key = '9';;break;/* [(] */
229 case 0x029: key = '0';break;/* [)] */
230 case 0x05f: key = '-';break;/* [_] */
231 case 0x02b: key = '=';break;/* [+] */
232 case 0x07c: key = '\'';break;/* [|] */
233 case 0x07d: key = '[';break;/* [}] */
234 case 0x07b: key = ']';break;/* [{] */
235 case 0x022: key = '\'';break;/* ["] */
236 case 0x03a: key = ';';break;/* [:] */
237 case 0x03f: key = '/';break;/* [?] */
238 case 0x03e: key = '.';break;/* [>] */
239 case 0x03c: key = ',';break;/* [<] */
243 key = *(unsigned char*)buf;
244 if (key >= 'A' && key <= 'Z')
245 key = key - 'A' + 'a';
252 static Cursor CreateNullCursor(Display *display, Window root)
260 cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
261 xgc.function = GXclear;
262 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
263 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
264 dummycolour.pixel = 0;
266 dummycolour.flags = 04;
267 cursor = XCreatePixmapCursor(display, cursormask, cursormask,
268 &dummycolour,&dummycolour, 0,0);
269 XFreePixmap(display,cursormask);
274 static void install_grabs(void)
278 XDefineCursor(dpy, win, CreateNullCursor(dpy, win));
280 XGrabPointer(dpy, win,
283 GrabModeAsync, GrabModeAsync,
288 if (in_dgamouse.value) {
289 int MajorVersion, MinorVersion;
291 if (!XF86DGAQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
292 // unable to query, probalby not supported
293 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
294 in_dgamouse.value = 0;
297 XF86DGADirectVideo(dpy, DefaultScreen(dpy), XF86DGADirectMouse);
298 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
301 XWarpPointer(dpy, None, win,
303 vid.width / 2, vid.height / 2);
306 XGrabKeyboard(dpy, win,
308 GrabModeAsync, GrabModeAsync,
316 static void uninstall_grabs(void)
323 XF86DGADirectVideo(dpy, DefaultScreen(dpy), 0);
326 XUngrabPointer(dpy, CurrentTime);
327 XUngrabKeyboard(dpy, CurrentTime);
330 XUndefineCursor(dpy, win);
332 mouse_active = false;
335 static void HandleEvents(void)
340 qboolean dowarp = false;
341 int mwx = vid.width/2;
342 int mwy = vid.height/2;
347 while (XPending(dpy)) {
348 XNextEvent(dpy, &event);
350 switch (event.type) {
353 Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
359 mx += (event.xmotion.x + win_x) * 2;
360 my += (event.xmotion.y + win_y) * 2;
364 mx += ((int)event.xmotion.x - mwx) * 2;
365 my += ((int)event.xmotion.y - mwy) * 2;
366 mwx = event.xmotion.x;
367 mwy = event.xmotion.y;
379 if (event.xbutton.button == 1)
381 else if (event.xbutton.button == 2)
383 else if (event.xbutton.button == 3)
386 Key_Event(K_MOUSE1 + b, true);
391 if (event.xbutton.button == 1)
393 else if (event.xbutton.button == 2)
395 else if (event.xbutton.button == 3)
398 Key_Event(K_MOUSE1 + b, false);
402 win_x = event.xcreatewindow.x;
403 win_y = event.xcreatewindow.y;
406 case ConfigureNotify :
407 win_x = event.xconfigure.x;
408 win_y = event.xconfigure.y;
414 /* move the mouse to the window center again */
415 XWarpPointer(dpy, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
420 static void IN_DeactivateMouse( void )
422 if (!mouse_avail || !dpy || !win)
427 mouse_active = false;
431 static void IN_ActivateMouse( void )
433 if (!mouse_avail || !dpy || !win)
437 mx = my = 0; // don't spazz
444 void VID_Shutdown(void)
448 IN_DeactivateMouse();
451 glXDestroyContext(dpy, ctx);
453 XDestroyWindow(dpy, win);
455 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[0]);
458 vidmode_active = false;
464 void signal_handler(int sig)
466 printf("Received signal %d, exiting...\n", sig);
473 signal(SIGHUP, signal_handler);
474 signal(SIGINT, signal_handler);
475 signal(SIGQUIT, signal_handler);
476 signal(SIGILL, signal_handler);
477 signal(SIGTRAP, signal_handler);
478 signal(SIGIOT, signal_handler);
479 signal(SIGBUS, signal_handler);
480 signal(SIGFPE, signal_handler);
481 signal(SIGSEGV, signal_handler);
482 signal(SIGTERM, signal_handler);
486 void (*qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
487 void (*qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
488 void (*qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
489 void (*qglArrayElement)(GLint i);
490 void (*qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
491 void (*qglMTexCoord2f)(GLenum, GLfloat, GLfloat);
492 void (*qglSelectTexture)(GLenum);
493 void (*glColorTableEXT)(int, int, int, int, int, const void*);
495 void CheckVertexArrays (void)
498 if (COM_CheckParm("-novertex"))
500 Con_Printf("...vertex array support disabled\n");
503 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
505 Con_Printf("Unable to open symbol list for main program.\n");
508 qglMTexCoord2fSGIS = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
509 if ((qglArrayElement = (void *) dlsym(prjobj, "glArrayElement"))
510 && (qglColorPointer = (void *) dlsym(prjobj, "glColorPointer"))
511 // && (qglDrawArrays = (void *) dlsym(prjobj, "glDrawArrays"))
512 && (qglDrawElements = (void *) dlsym(prjobj, "glDrawElements"))
513 // && (qglInterleavedArrays = (void *) dlsym(prjobj, "glInterleavedArrays"))
514 && (qglTexCoordPointer = (void *) dlsym(prjobj, "glTexCoordPointer"))
515 && (qglVertexPointer = (void *) dlsym(prjobj, "glVertexPointer"))
518 Con_Printf("...vertex array support detected\n");
524 Con_Printf("...vertex array support disabled (not detected - get a better driver)\n");
529 void CheckMultiTexture(void)
532 qglMTexCoord2f = NULL;
533 qglSelectTexture = NULL;
534 // Check to see if multitexture is disabled
535 if (COM_CheckParm("-nomtex"))
537 Con_Printf("...multitexture disabled\n");
540 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
542 Con_Printf("Unable to open symbol list for main program.\n");
545 // Test for ARB_multitexture
546 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
548 Con_Printf("...using GL_ARB_multitexture\n");
549 qglMTexCoord2f = (void *) dlsym(prjobj, "glMultiTexCoord2fARB");
550 qglSelectTexture = (void *) dlsym(prjobj, "glActiveTextureARB");
552 gl_mtex_enum = GL_TEXTURE0_ARB;
554 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
556 Con_Printf("...using GL_SGIS_multitexture\n");
557 qglMTexCoord2f = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
558 qglSelectTexture = (void *) dlsym(prjobj, "glSelectTextureSGIS");
560 gl_mtex_enum = TEXTURE0_SGIS;
563 Con_Printf("...multitexture disabled (not detected)\n");
572 extern char *QSG_EXTENSIONS;
575 gl_vendor = glGetString (GL_VENDOR);
576 Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
577 gl_renderer = glGetString (GL_RENDERER);
578 Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
580 gl_version = glGetString (GL_VERSION);
581 Con_Printf ("GL_VERSION: %s\n", gl_version);
582 gl_extensions = glGetString (GL_EXTENSIONS);
583 Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
585 // Con_Printf ("%s %s\n", gl_renderer, gl_version);
588 // CheckVertexArrays();
590 // LordHavoc: report supported extensions
591 Con_Printf ("\nQSG extensions: %s\n", QSG_EXTENSIONS);
593 glCullFace(GL_FRONT);
594 glAlphaFunc(GL_GREATER, 0.5);
603 void GL_BeginRendering (int *x, int *y, int *width, int *height)
607 *height = scr_height;
609 // if (!wglMakeCurrent( maindc, baseRC ))
610 // Sys_Error ("wglMakeCurrent failed");
612 // glViewport (*x, *y, *width, *height);
616 void GL_EndRendering (void)
621 glXSwapBuffers(dpy, win);
624 qboolean VID_Is8bit(void)
629 void VID_Init8bitPalette(void)
631 // Check for 8bit Extensions and initialize them.
635 if (!COM_CheckParm("-8bit"))
637 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
639 Con_Printf("Unable to open symbol list for main program.\n");
643 if (strstr(gl_extensions, "3DFX_set_global_palette") && (qgl3DfxSetPaletteEXT = dlsym(prjobj, "gl3DfxSetPaletteEXT")) != NULL)
645 GLubyte table[256][4];
648 Con_SafePrintf("8-bit GL extensions enabled.\n");
649 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
650 oldpal = (char *) d_8to24table; //d_8to24table3dfx;
653 table[i][2] = *oldpal++;
654 table[i][1] = *oldpal++;
655 table[i][0] = *oldpal++;
659 qgl3DfxSetPaletteEXT((GLuint *)table);
663 else if (strstr(gl_extensions, "GL_EXT_shared_texture_palette") && (qglColorTableEXT = dlsym(prjobj, "glColorTableEXT")) != NULL)
665 char thePalette[256*3];
666 char *oldPalette, *newPalette;
668 Con_SafePrintf("8-bit GL extensions enabled.\n");
669 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
670 oldPalette = (char *) d_8to24table; //d_8to24table3dfx;
671 newPalette = thePalette;
674 *newPalette++ = *oldPalette++;
675 *newPalette++ = *oldPalette++;
676 *newPalette++ = *oldPalette++;
679 qglColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
686 extern void Check_Gamma (unsigned char *pal);
687 void VID_Setup15to8Palette ();
689 void VID_Init(unsigned char *palette)
701 // char gldir[MAX_OSPATH];
702 int width = 640, height = 480;
703 XSetWindowAttributes attr;
706 XVisualInfo *visinfo;
707 qboolean fullscreen = true;
708 int MajorVersion, MinorVersion;
709 int actualWidth, actualHeight;
711 Cvar_RegisterVariable (&vid_mode);
712 Cvar_RegisterVariable (&in_mouse);
713 Cvar_RegisterVariable (&in_dgamouse);
714 Cvar_RegisterVariable (&m_filter);
716 // interpret command-line params
718 // set vid parameters
719 if ((i = COM_CheckParm("-window")) != 0)
722 if ((i = COM_CheckParm("-width")) != 0)
723 width = atoi(com_argv[i+1]);
725 if ((i = COM_CheckParm("-height")) != 0)
726 height = atoi(com_argv[i+1]);
728 if ((i = COM_CheckParm("-conwidth")) != 0)
729 vid.conwidth = atoi(com_argv[i+1]);
733 vid.conwidth &= 0xfff8; // make it a multiple of eight
735 if (vid.conwidth < 320)
738 // pick a conheight that matches with correct aspect
739 vid.conheight = vid.conwidth*3 / 4;
741 if ((i = COM_CheckParm("-conheight")) != 0)
742 vid.conheight = atoi(com_argv[i+1]);
743 if (vid.conheight < 200)
746 if (!(dpy = XOpenDisplay(NULL))) {
747 fprintf(stderr, "Error couldn't open the X display\n");
751 scrnum = DefaultScreen(dpy);
752 root = RootWindow(dpy, scrnum);
754 // Get video mode list
755 MajorVersion = MinorVersion = 0;
756 if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
759 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
763 visinfo = glXChooseVisual(dpy, scrnum, attrib);
765 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
770 int best_fit, best_dist, dist, x, y;
772 XF86VidModeGetAllModeLines(dpy, scrnum, &num_vidmodes, &vidmodes);
774 // Are we going fullscreen? If so, let's change video mode
779 for (i = 0; i < num_vidmodes; i++) {
780 if (width > vidmodes[i]->hdisplay ||
781 height > vidmodes[i]->vdisplay)
784 x = width - vidmodes[i]->hdisplay;
785 y = height - vidmodes[i]->vdisplay;
786 dist = (x * x) + (y * y);
787 if (dist < best_dist) {
793 if (best_fit != -1) {
794 actualWidth = vidmodes[best_fit]->hdisplay;
795 actualHeight = vidmodes[best_fit]->vdisplay;
797 // change to the mode
798 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[best_fit]);
799 vidmode_active = true;
801 // Move the viewport to top left
802 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
808 /* window attributes */
809 attr.background_pixel = 0;
810 attr.border_pixel = 0;
811 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
812 attr.event_mask = X_MASK;
813 if (vidmode_active) {
814 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore |
815 CWEventMask | CWOverrideRedirect;
816 attr.override_redirect = True;
817 attr.backing_store = NotUseful;
818 attr.save_under = False;
820 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
822 win = XCreateWindow(dpy, root, 0, 0, width, height,
823 0, visinfo->depth, InputOutput,
824 visinfo->visual, mask, &attr);
825 XMapWindow(dpy, win);
827 if (vidmode_active) {
828 XMoveWindow(dpy, win, 0, 0);
829 XRaiseWindow(dpy, win);
830 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
832 // Move the viewport to top left
833 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
838 ctx = glXCreateContext(dpy, visinfo, NULL, True);
840 glXMakeCurrent(dpy, win, ctx);
845 if (vid.conheight > height)
846 vid.conheight = height;
847 if (vid.conwidth > width)
848 vid.conwidth = width;
849 vid.width = vid.conwidth;
850 vid.height = vid.conheight;
852 vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
854 InitSig(); // trap evil signals
858 // sprintf (gldir, "%s/glquake", com_gamedir);
859 // Sys_mkdir (gldir);
861 VID_SetPalette(palette);
863 Check_Gamma(palette);
865 VID_Init8bitPalette();
867 if (is8bit) // LordHavoc: avoid calculating 15to8 table if it won't be used
868 VID_Setup15to8Palette ();
870 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
872 vid.recalc_refdef = 1; // force a surface cache flush
875 void Sys_SendKeyEvents(void)
880 void Force_CenterView_f (void)
882 cl.viewangles[PITCH] = 0;
889 void IN_Shutdown(void)
898 void IN_Commands (void)
903 if (vidmode_active || key_dest == key_game)
906 IN_DeactivateMouse ();
914 void IN_MouseMove (usercmd_t *cmd)
921 mx = (mx + old_mouse_x) * 0.5;
922 my = (my + old_mouse_y) * 0.5;
927 mx *= sensitivity.value;
928 my *= sensitivity.value;
930 // add mouse X/Y movement to cmd
931 if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
932 cmd->sidemove += m_side.value * mx;
934 cl.viewangles[YAW] -= m_yaw.value * mx;
936 if (in_mlook.state & 1)
939 if ( (in_mlook.state & 1) && !(in_strafe.state & 1))
941 cl.viewangles[PITCH] += m_pitch.value * my;
942 if (cl.viewangles[PITCH] > 80)
943 cl.viewangles[PITCH] = 80;
944 if (cl.viewangles[PITCH] < -70)
945 cl.viewangles[PITCH] = -70;
949 if ((in_strafe.state & 1) && noclip_anglehack)
950 cmd->upmove -= m_forward.value * my;
952 cmd->forwardmove -= m_forward.value * my;
957 void IN_Move (usercmd_t *cmd)