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)
626 glXSwapBuffers(dpy, win);
629 qboolean VID_Is8bit(void)
634 void VID_Init8bitPalette(void)
636 // Check for 8bit Extensions and initialize them.
640 if (!COM_CheckParm("-8bit"))
642 if ((prjobj = dlopen(NULL, RTLD_LAZY)) == NULL)
644 Con_Printf("Unable to open symbol list for main program.\n");
648 if (strstr(gl_extensions, "3DFX_set_global_palette") && (qgl3DfxSetPaletteEXT = dlsym(prjobj, "gl3DfxSetPaletteEXT")) != NULL)
650 GLubyte table[256][4];
653 Con_SafePrintf("8-bit GL extensions enabled.\n");
654 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
655 oldpal = (char *) d_8to24table; //d_8to24table3dfx;
658 table[i][2] = *oldpal++;
659 table[i][1] = *oldpal++;
660 table[i][0] = *oldpal++;
664 qgl3DfxSetPaletteEXT((GLuint *)table);
668 else if (strstr(gl_extensions, "GL_EXT_shared_texture_palette") && (qglColorTableEXT = dlsym(prjobj, "glColorTableEXT")) != NULL)
670 char thePalette[256*3];
671 char *oldPalette, *newPalette;
673 Con_SafePrintf("8-bit GL extensions enabled.\n");
674 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
675 oldPalette = (char *) d_8to24table; //d_8to24table3dfx;
676 newPalette = thePalette;
679 *newPalette++ = *oldPalette++;
680 *newPalette++ = *oldPalette++;
681 *newPalette++ = *oldPalette++;
684 qglColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
691 extern void Check_Gamma (unsigned char *pal);
692 void VID_Setup15to8Palette ();
694 void VID_Init(unsigned char *palette)
706 // char gldir[MAX_OSPATH];
707 int width = 640, height = 480;
708 XSetWindowAttributes attr;
711 XVisualInfo *visinfo;
712 qboolean fullscreen = true;
713 int MajorVersion, MinorVersion;
714 int actualWidth, actualHeight;
716 Cvar_RegisterVariable (&vid_mode);
717 Cvar_RegisterVariable (&in_mouse);
718 Cvar_RegisterVariable (&in_dgamouse);
719 Cvar_RegisterVariable (&m_filter);
721 // interpret command-line params
723 // set vid parameters
724 if ((i = COM_CheckParm("-window")) != 0)
727 if ((i = COM_CheckParm("-width")) != 0)
728 width = atoi(com_argv[i+1]);
730 if ((i = COM_CheckParm("-height")) != 0)
731 height = atoi(com_argv[i+1]);
733 if ((i = COM_CheckParm("-conwidth")) != 0)
734 vid.conwidth = atoi(com_argv[i+1]);
738 vid.conwidth &= 0xfff8; // make it a multiple of eight
740 if (vid.conwidth < 320)
743 // pick a conheight that matches with correct aspect
744 vid.conheight = vid.conwidth*3 / 4;
746 if ((i = COM_CheckParm("-conheight")) != 0)
747 vid.conheight = atoi(com_argv[i+1]);
748 if (vid.conheight < 200)
751 if (!(dpy = XOpenDisplay(NULL))) {
752 fprintf(stderr, "Error couldn't open the X display\n");
756 scrnum = DefaultScreen(dpy);
757 root = RootWindow(dpy, scrnum);
759 // Get video mode list
760 MajorVersion = MinorVersion = 0;
761 if (!XF86VidModeQueryVersion(dpy, &MajorVersion, &MinorVersion)) {
764 Con_Printf("Using XFree86-VidModeExtension Version %d.%d\n", MajorVersion, MinorVersion);
768 visinfo = glXChooseVisual(dpy, scrnum, attrib);
770 fprintf(stderr, "qkHack: Error couldn't get an RGB, Double-buffered, Depth visual\n");
775 int best_fit, best_dist, dist, x, y;
777 XF86VidModeGetAllModeLines(dpy, scrnum, &num_vidmodes, &vidmodes);
779 // Are we going fullscreen? If so, let's change video mode
784 for (i = 0; i < num_vidmodes; i++) {
785 if (width > vidmodes[i]->hdisplay ||
786 height > vidmodes[i]->vdisplay)
789 x = width - vidmodes[i]->hdisplay;
790 y = height - vidmodes[i]->vdisplay;
791 dist = (x * x) + (y * y);
792 if (dist < best_dist) {
798 if (best_fit != -1) {
799 actualWidth = vidmodes[best_fit]->hdisplay;
800 actualHeight = vidmodes[best_fit]->vdisplay;
802 // change to the mode
803 XF86VidModeSwitchToMode(dpy, scrnum, vidmodes[best_fit]);
804 vidmode_active = true;
806 // Move the viewport to top left
807 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
813 /* window attributes */
814 attr.background_pixel = 0;
815 attr.border_pixel = 0;
816 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
817 attr.event_mask = X_MASK;
818 if (vidmode_active) {
819 mask = CWBackPixel | CWColormap | CWSaveUnder | CWBackingStore |
820 CWEventMask | CWOverrideRedirect;
821 attr.override_redirect = True;
822 attr.backing_store = NotUseful;
823 attr.save_under = False;
825 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
827 win = XCreateWindow(dpy, root, 0, 0, width, height,
828 0, visinfo->depth, InputOutput,
829 visinfo->visual, mask, &attr);
830 XMapWindow(dpy, win);
832 if (vidmode_active) {
833 XMoveWindow(dpy, win, 0, 0);
834 XRaiseWindow(dpy, win);
835 XWarpPointer(dpy, None, win, 0, 0, 0, 0, 0, 0);
837 // Move the viewport to top left
838 XF86VidModeSetViewPort(dpy, scrnum, 0, 0);
843 ctx = glXCreateContext(dpy, visinfo, NULL, True);
845 glXMakeCurrent(dpy, win, ctx);
850 if (vid.conheight > height)
851 vid.conheight = height;
852 if (vid.conwidth > width)
853 vid.conwidth = width;
854 vid.width = vid.conwidth;
855 vid.height = vid.conheight;
857 vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
859 InitSig(); // trap evil signals
863 // sprintf (gldir, "%s/glquake", com_gamedir);
864 // Sys_mkdir (gldir);
866 VID_SetPalette(palette);
868 Check_Gamma(palette);
870 VID_Init8bitPalette();
872 if (is8bit) // LordHavoc: avoid calculating 15to8 table if it won't be used
873 VID_Setup15to8Palette ();
875 Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
877 vid.recalc_refdef = 1; // force a surface cache flush
880 void Sys_SendKeyEvents(void)
885 void Force_CenterView_f (void)
887 cl.viewangles[PITCH] = 0;
894 void IN_Shutdown(void)
903 void IN_Commands (void)
908 if (vidmode_active || key_dest == key_game)
911 IN_DeactivateMouse ();
919 void IN_MouseMove (usercmd_t *cmd)
926 mx = (mx + old_mouse_x) * 0.5;
927 my = (my + old_mouse_y) * 0.5;
932 mx *= sensitivity.value;
933 my *= sensitivity.value;
935 // add mouse X/Y movement to cmd
936 if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
937 cmd->sidemove += m_side.value * mx;
939 cl.viewangles[YAW] -= m_yaw.value * mx;
941 if (in_mlook.state & 1)
944 if ( (in_mlook.state & 1) && !(in_strafe.state & 1))
946 cl.viewangles[PITCH] += m_pitch.value * my;
947 if (cl.viewangles[PITCH] > 80)
948 cl.viewangles[PITCH] = 80;
949 if (cl.viewangles[PITCH] < -70)
950 cl.viewangles[PITCH] = -70;
954 if ((in_strafe.state & 1) && noclip_anglehack)
955 cmd->upmove -= m_forward.value * my;
957 cmd->forwardmove -= m_forward.value * my;
962 void IN_Move (usercmd_t *cmd)