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_mode = GL_NEAREST;
86 //int texture_mode = GL_NEAREST_MIPMAP_NEAREST;
87 //int texture_mode = GL_NEAREST_MIPMAP_LINEAR;
88 int texture_mode = GL_LINEAR;
89 //int texture_mode = GL_LINEAR_MIPMAP_NEAREST;
90 //int texture_mode = GL_LINEAR_MIPMAP_LINEAR;
92 int texture_extension_number = 1;
94 float gldepthmin, gldepthmax;
96 const char *gl_vendor;
97 const char *gl_renderer;
98 const char *gl_version;
99 const char *gl_extensions;
101 void (*qglColorTableEXT) (int, int, int, int, int, const void*);
102 void (*qgl3DfxSetPaletteEXT) (GLuint *);
103 void (*qglMTexCoord2f) (GLenum, GLfloat, GLfloat);
104 void (*qglSelectTexture) (GLenum);
106 //static float vid_gamma = 1.0;
108 // LordHavoc: ARB multitexture support
109 int gl_mtex_enum = 0;
111 // LordHavoc: in GLX these are never set, simply provided to make the rest of the code work
112 qboolean is8bit = false;
113 qboolean isPermedia = false;
114 qboolean isATI = false;
115 qboolean isG200 = false;
116 qboolean isRagePro = false;
117 qboolean gl_mtexable = false;
118 qboolean gl_arrays = false;
120 /*-----------------------------------------------------------------------*/
121 void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
125 void D_EndDirectRect (int x, int y, int width, int height)
129 static int XLateKey(XKeyEvent *ev)
138 XLookupString(ev, buf, sizeof buf, &keysym, 0);
143 case XK_Page_Up: key = K_PGUP; break;
145 case XK_KP_Page_Down:
146 case XK_Page_Down: key = K_PGDN; break;
149 case XK_Home: key = K_HOME; break;
152 case XK_End: key = K_END; break;
155 case XK_Left: key = K_LEFTARROW; break;
158 case XK_Right: key = K_RIGHTARROW; break;
161 case XK_Down: key = K_DOWNARROW; break;
164 case XK_Up: key = K_UPARROW; break;
166 case XK_Escape: key = K_ESCAPE; break;
169 case XK_Return: key = K_ENTER; break;
171 case XK_Tab: key = K_TAB; break;
173 case XK_F1: key = K_F1; break;
175 case XK_F2: key = K_F2; break;
177 case XK_F3: key = K_F3; break;
179 case XK_F4: key = K_F4; break;
181 case XK_F5: key = K_F5; break;
183 case XK_F6: key = K_F6; break;
185 case XK_F7: key = K_F7; break;
187 case XK_F8: key = K_F8; break;
189 case XK_F9: key = K_F9; break;
191 case XK_F10: key = K_F10; break;
193 case XK_F11: key = K_F11; break;
195 case XK_F12: key = K_F12; break;
197 case XK_BackSpace: key = K_BACKSPACE; break;
200 case XK_Delete: key = K_DEL; break;
202 case XK_Pause: key = K_PAUSE; break;
205 case XK_Shift_R: key = K_SHIFT; break;
209 case XK_Control_R: key = K_CTRL; break;
214 case XK_Meta_R: key = K_ALT; break;
216 case XK_KP_Begin: key = '5'; break;
219 case XK_Insert:key = K_INS; break;
221 case XK_KP_Multiply: key = '*'; break;
222 case XK_KP_Add: key = '+'; break;
223 case XK_KP_Subtract: key = '-'; break;
224 case XK_KP_Divide: key = '/'; break;
227 case 0x021: key = '1';break;/* [!] */
228 case 0x040: key = '2';break;/* [@] */
229 case 0x023: key = '3';break;/* [#] */
230 case 0x024: key = '4';break;/* [$] */
231 case 0x025: key = '5';break;/* [%] */
232 case 0x05e: key = '6';break;/* [^] */
233 case 0x026: key = '7';break;/* [&] */
234 case 0x02a: key = '8';break;/* [*] */
235 case 0x028: key = '9';;break;/* [(] */
236 case 0x029: key = '0';break;/* [)] */
237 case 0x05f: key = '-';break;/* [_] */
238 case 0x02b: key = '=';break;/* [+] */
239 case 0x07c: key = '\'';break;/* [|] */
240 case 0x07d: key = '[';break;/* [}] */
241 case 0x07b: key = ']';break;/* [{] */
242 case 0x022: key = '\'';break;/* ["] */
243 case 0x03a: key = ';';break;/* [:] */
244 case 0x03f: key = '/';break;/* [?] */
245 case 0x03e: key = '.';break;/* [>] */
246 case 0x03c: key = ',';break;/* [<] */
250 key = *(unsigned char*)buf;
251 if (key >= 'A' && key <= 'Z')
252 key = key - 'A' + 'a';
259 static Cursor CreateNullCursor(Display *display, Window root)
267 cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
268 xgc.function = GXclear;
269 gc = XCreateGC(display, cursormask, GCFunction, &xgc);
270 XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
271 dummycolour.pixel = 0;
273 dummycolour.flags = 04;
274 cursor = XCreatePixmapCursor(display, cursormask, cursormask,
275 &dummycolour,&dummycolour, 0,0);
276 XFreePixmap(display,cursormask);
281 static void install_grabs(void)
285 XDefineCursor(dpy, win, CreateNullCursor(dpy, win));
287 XGrabPointer(dpy, win,
290 GrabModeAsync, GrabModeAsync,
295 if (in_dgamouse.value) {
296 int MajorVersion, MinorVersion;
298 if (!XF86DGAQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
299 // unable to query, probalby not supported
300 Con_Printf( "Failed to detect XF86DGA Mouse\n" );
301 in_dgamouse.value = 0;
304 XF86DGADirectVideo(dpy, DefaultScreen(dpy), XF86DGADirectMouse);
305 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
308 XWarpPointer(dpy, None, win,
310 vid.width / 2, vid.height / 2);
313 XGrabKeyboard(dpy, win,
315 GrabModeAsync, GrabModeAsync,
323 static void uninstall_grabs(void)
330 XF86DGADirectVideo(dpy, DefaultScreen(dpy), 0);
333 XUngrabPointer(dpy, CurrentTime);
334 XUngrabKeyboard(dpy, CurrentTime);
337 XUndefineCursor(dpy, win);
339 mouse_active = false;
342 static void HandleEvents(void)
347 qboolean dowarp = false;
348 int mwx = vid.width/2;
349 int mwy = vid.height/2;
354 while (XPending(dpy)) {
355 XNextEvent(dpy, &event);
357 switch (event.type) {
360 Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
366 mx += (event.xmotion.x + win_x) * 2;
367 my += (event.xmotion.y + win_y) * 2;
371 mx += ((int)event.xmotion.x - mwx) * 2;
372 my += ((int)event.xmotion.y - mwy) * 2;
373 mwx = event.xmotion.x;
374 mwy = event.xmotion.y;
386 if (event.xbutton.button == 1)
388 else if (event.xbutton.button == 2)
390 else if (event.xbutton.button == 3)
393 Key_Event(K_MOUSE1 + b, true);
398 if (event.xbutton.button == 1)
400 else if (event.xbutton.button == 2)
402 else if (event.xbutton.button == 3)
405 Key_Event(K_MOUSE1 + b, false);
409 win_x = event.xcreatewindow.x;
410 win_y = event.xcreatewindow.y;
413 case ConfigureNotify :
414 win_x = event.xconfigure.x;
415 win_y = event.xconfigure.y;
421 /* move the mouse to the window center again */
422 XWarpPointer(dpy, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
427 static void IN_DeactivateMouse( void )
429 if (!mouse_avail || !dpy || !win)
434 mouse_active = false;
438 static void IN_ActivateMouse( void )
440 if (!mouse_avail || !dpy || !win)
444 mx = my = 0; // don't spazz
451 void VID_Shutdown(void)
455 IN_DeactivateMouse();
458 glXDestroyContext(dpy, ctx);
460 XDestroyWindow(dpy, win);
462 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[0]);
465 vidmode_active = false;
471 void signal_handler(int sig)
473 printf("Received signal %d, exiting...\n", sig);
480 signal(SIGHUP, signal_handler);
481 signal(SIGINT, signal_handler);
482 signal(SIGQUIT, signal_handler);
483 signal(SIGILL, signal_handler);
484 signal(SIGTRAP, signal_handler);
485 signal(SIGIOT, signal_handler);
486 signal(SIGBUS, signal_handler);
487 signal(SIGFPE, signal_handler);
488 signal(SIGSEGV, signal_handler);
489 signal(SIGTERM, signal_handler);
493 void (*qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
494 void (*qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
495 void (*qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
496 void (*qglArrayElement)(GLint i);
497 void (*qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
498 void (*qglMTexCoord2f)(GLenum, GLfloat, GLfloat);
499 void (*qglSelectTexture)(GLenum);
500 void (*glColorTableEXT)(int, int, int, int, int, const void*);
502 void CheckVertexArrays (void)
505 if (COM_CheckParm("-novertex"))
507 Con_Printf("...vertex array support disabled\n");
510 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
512 Con_Printf("Unable to open symbol list for main program.\n");
515 qglMTexCoord2fSGIS = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
516 if ((qglArrayElement = (void *) dlsym(prjobj, "glArrayElement"))
517 && (qglColorPointer = (void *) dlsym(prjobj, "glColorPointer"))
518 // && (qglDrawArrays = (void *) dlsym(prjobj, "glDrawArrays"))
519 && (qglDrawElements = (void *) dlsym(prjobj, "glDrawElements"))
520 // && (qglInterleavedArrays = (void *) dlsym(prjobj, "glInterleavedArrays"))
521 && (qglTexCoordPointer = (void *) dlsym(prjobj, "glTexCoordPointer"))
522 && (qglVertexPointer = (void *) dlsym(prjobj, "glVertexPointer"))
525 Con_Printf("...vertex array support detected\n");
531 Con_Printf("...vertex array support disabled (not detected - get a better driver)\n");
536 void CheckMultiTexture(void)
539 qglMTexCoord2f = NULL;
540 qglSelectTexture = NULL;
541 // Check to see if multitexture is disabled
542 if (COM_CheckParm("-nomtex"))
544 Con_Printf("...multitexture disabled\n");
547 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
549 Con_Printf("Unable to open symbol list for main program.\n");
552 // Test for ARB_multitexture
553 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
555 Con_Printf("...using GL_ARB_multitexture\n");
556 qglMTexCoord2f = (void *) dlsym(prjobj, "glMultiTexCoord2fARB");
557 qglSelectTexture = (void *) dlsym(prjobj, "glActiveTextureARB");
559 gl_mtex_enum = GL_TEXTURE0_ARB;
561 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
563 Con_Printf("...using GL_SGIS_multitexture\n");
564 qglMTexCoord2f = (void *) dlsym(prjobj, "glMTexCoord2fSGIS");
565 qglSelectTexture = (void *) dlsym(prjobj, "glSelectTextureSGIS");
567 gl_mtex_enum = TEXTURE0_SGIS;
570 Con_Printf("...multitexture disabled (not detected)\n");
579 extern char *QSG_EXTENSIONS;
582 gl_vendor = glGetString (GL_VENDOR);
583 Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
584 gl_renderer = glGetString (GL_RENDERER);
585 Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
587 gl_version = glGetString (GL_VERSION);
588 Con_Printf ("GL_VERSION: %s\n", gl_version);
589 gl_extensions = glGetString (GL_EXTENSIONS);
590 Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
592 // Con_Printf ("%s %s\n", gl_renderer, gl_version);
595 // CheckVertexArrays();
597 // LordHavoc: report supported extensions
598 Con_Printf ("\nQSG extensions: %s\n", QSG_EXTENSIONS);
600 glCullFace(GL_FRONT);
601 glAlphaFunc(GL_GREATER, 0.5);
610 void GL_BeginRendering (int *x, int *y, int *width, int *height)
614 *height = scr_height;
616 // if (!wglMakeCurrent( maindc, baseRC ))
617 // Sys_Error ("wglMakeCurrent failed");
619 // glViewport (*x, *y, *width, *height);
623 void GL_EndRendering (void)
628 glXSwapBuffers(dpy, win);
631 qboolean VID_Is8bit(void)
636 void VID_Init8bitPalette(void)
638 // Check for 8bit Extensions and initialize them.
642 if (!COM_CheckParm("-8bit"))
644 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
646 Con_Printf("Unable to open symbol list for main program.\n");
650 if (strstr(gl_extensions, "3DFX_set_global_palette") && (qgl3DfxSetPaletteEXT = dlsym(prjobj, "gl3DfxSetPaletteEXT")) != NULL)
652 GLubyte table[256][4];
655 Con_SafePrintf("8-bit GL extensions enabled.\n");
656 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
657 oldpal = (char *) d_8to24table; //d_8to24table3dfx;
660 table[i][2] = *oldpal++;
661 table[i][1] = *oldpal++;
662 table[i][0] = *oldpal++;
666 qgl3DfxSetPaletteEXT((GLuint *)table);
670 else if (strstr(gl_extensions, "GL_EXT_shared_texture_palette") && (qglColorTableEXT = dlsym(prjobj, "glColorTableEXT")) != NULL)
672 char thePalette[256*3];
673 char *oldPalette, *newPalette;
675 Con_SafePrintf("8-bit GL extensions enabled.\n");
676 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
677 oldPalette = (char *) d_8to24table; //d_8to24table3dfx;
678 newPalette = thePalette;
681 *newPalette++ = *oldPalette++;
682 *newPalette++ = *oldPalette++;
683 *newPalette++ = *oldPalette++;
686 qglColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
693 extern void Check_Gamma (unsigned char *pal);
694 void VID_Setup15to8Palette ();
696 void VID_Init(unsigned char *palette)
708 // char gldir[MAX_OSPATH];
709 int width = 640, height = 480;
710 XSetWindowAttributes attr;
713 XVisualInfo *visinfo;
714 qboolean fullscreen = true;
715 int MajorVersion, MinorVersion;
716 int actualWidth, actualHeight;
718 Cvar_RegisterVariable (&vid_mode);
719 Cvar_RegisterVariable (&in_mouse);
720 Cvar_RegisterVariable (&in_dgamouse);
721 Cvar_RegisterVariable (&m_filter);
723 // interpret command-line params
725 // set vid parameters
726 if ((i = COM_CheckParm("-window")) != 0)
729 if ((i = COM_CheckParm("-width")) != 0)
730 width = atoi(com_argv[i+1]);
732 if ((i = COM_CheckParm("-height")) != 0)
733 height = atoi(com_argv[i+1]);
735 if ((i = COM_CheckParm("-conwidth")) != 0)
736 vid.conwidth = atoi(com_argv[i+1]);
740 vid.conwidth &= 0xfff8; // make it a multiple of eight
742 if (vid.conwidth < 320)
745 // pick a conheight that matches with correct aspect
746 vid.conheight = vid.conwidth*3 / 4;
748 if ((i = COM_CheckParm("-conheight")) != 0)
749 vid.conheight = atoi(com_argv[i+1]);
750 if (vid.conheight < 200)
753 if (!(dpy = XOpenDisplay(NULL))) {
754 fprintf(stderr, "Error couldn't open the X display\n");
758 scrnum = DefaultScreen(dpy);
759 root = RootWindow(dpy, scrnum);
761 // Get video mode list
762 MajorVersion = MinorVersion = 0;
763 if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
766 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
770 visinfo = glXChooseVisual(dpy, scrnum, attrib);
772 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
777 int best_fit, best_dist, dist, x, y;
779 XF86VidModeGetAllModeLines(dpy, scrnum, &num_vidmodes, &vidmodes);
781 // Are we going fullscreen? If so, let's change video mode
786 for (i = 0; i < num_vidmodes; i++) {
787 if (width > vidmodes[i]->hdisplay ||
788 height > vidmodes[i]->vdisplay)
791 x = width - vidmodes[i]->hdisplay;
792 y = height - vidmodes[i]->vdisplay;
793 dist = (x * x) + (y * y);
794 if (dist < best_dist) {
800 if (best_fit != -1) {
801 actualWidth = vidmodes[best_fit]->hdisplay;
802 actualHeight = vidmodes[best_fit]->vdisplay;
804 // change to the mode
805 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[best_fit]);
806 vidmode_active = true;
808 // Move the viewport to top left
809 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
815 /* window attributes */
816 attr.background_pixel = 0;
817 attr.border_pixel = 0;
818 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
819 attr.event_mask = X_MASK;
820 if (vidmode_active) {
821 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore |
822 CWEventMask | CWOverrideRedirect;
823 attr.override_redirect = True;
824 attr.backing_store = NotUseful;
825 attr.save_under = False;
827 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
829 win = XCreateWindow(dpy, root, 0, 0, width, height,
830 0, visinfo->depth, InputOutput,
831 visinfo->visual, mask, &attr);
832 XMapWindow(dpy, win);
834 if (vidmode_active) {
835 XMoveWindow(dpy, win, 0, 0);
836 XRaiseWindow(dpy, win);
837 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
839 // Move the viewport to top left
840 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
845 ctx = glXCreateContext(dpy, visinfo, NULL, True);
847 glXMakeCurrent(dpy, win, ctx);
852 if (vid.conheight > height)
853 vid.conheight = height;
854 if (vid.conwidth > width)
855 vid.conwidth = width;
856 vid.width = vid.conwidth;
857 vid.height = vid.conheight;
859 vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
861 InitSig(); // trap evil signals
865 // sprintf (gldir, "%s/glquake", com_gamedir);
866 // Sys_mkdir (gldir);
868 VID_SetPalette(palette);
870 Check_Gamma(palette);
872 VID_Init8bitPalette();
874 if (is8bit) // LordHavoc: avoid calculating 15to8 table if it won't be used
875 VID_Setup15to8Palette ();
877 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
879 vid.recalc_refdef = 1; // force a surface cache flush
882 void Sys_SendKeyEvents(void)
887 void Force_CenterView_f (void)
889 cl.viewangles[PITCH] = 0;
896 void IN_Shutdown(void)
905 void IN_Commands (void)
910 if (vidmode_active || key_dest == key_game)
913 IN_DeactivateMouse ();
921 void IN_MouseMove (usercmd_t *cmd)
928 mx = (mx + old_mouse_x) * 0.5;
929 my = (my + old_mouse_y) * 0.5;
934 mx *= sensitivity.value;
935 my *= sensitivity.value;
937 // add mouse X/Y movement to cmd
938 if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
939 cmd->sidemove += m_side.value * mx;
941 cl.viewangles[YAW] -= m_yaw.value * mx;
943 if (in_mlook.state & 1)
946 if ( (in_mlook.state & 1) && !(in_strafe.state & 1))
948 cl.viewangles[PITCH] += m_pitch.value * my;
949 if (cl.viewangles[PITCH] > 80)
950 cl.viewangles[PITCH] = 80;
951 if (cl.viewangles[PITCH] < -70)
952 cl.viewangles[PITCH] = -70;
956 if ((in_strafe.state & 1) && noclip_anglehack)
957 cmd->upmove -= m_forward.value * my;
959 cmd->forwardmove -= m_forward.value * my;
964 void IN_Move (usercmd_t *cmd)