]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - vid_glx.c
added -gl_driver commandline option, and cleaned up some glX init stuff a bit
[xonotic/darkplaces.git] / vid_glx.c
index 07104c14adf49155653176979b390f7a818039ce..542690a6f8b6548b2b05f3bfef9e4eb7cafad47b 100644 (file)
--- a/vid_glx.c
+++ b/vid_glx.c
@@ -25,7 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 //#include <stdio.h>
 #include <signal.h>
 
-//#include <dlfcn.h>
+#include <dlfcn.h>
 
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
@@ -40,12 +40,35 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "quakedef.h"
 
+int cl_available = true;
+
+//GLX prototypes
 XVisualInfo *(GLAPIENTRY *qglXChooseVisual)(Display *dpy, int screen, int *attribList);
 GLXContext (GLAPIENTRY *qglXCreateContext)(Display *dpy, XVisualInfo *vis, GLXContext shareList, Bool direct);
 void (GLAPIENTRY *qglXDestroyContext)(Display *dpy, GLXContext ctx);
 Bool (GLAPIENTRY *qglXMakeCurrent)(Display *dpy, GLXDrawable drawable, GLXContext ctx);
 void (GLAPIENTRY *qglXSwapBuffers)(Display *dpy, GLXDrawable drawable);
+const char *(GLAPIENTRY *qglXQueryExtensionsString)(Display *dpy, int screen);
+
+//GLX_ARB_get_proc_address
+void *(GLAPIENTRY *qglXGetProcAddressARB)(const GLubyte *procName);
 
+static gl_extensionfunctionlist_t getprocaddressfuncs[] =
+{
+       {"glXGetProcAddressARB", (void **) &qglXGetProcAddressARB},
+       {NULL, NULL}
+};
+
+//GLX_SGI_video_sync
+GLint (GLAPIENTRY *qglXGetVideoSyncSGI)(GLuint *count);
+GLint (GLAPIENTRY *qglXWaitVideoSyncSGI)(int divisor, int remainder, unsigned int *count);
+
+static gl_extensionfunctionlist_t videosyncfuncs[] =
+{
+       {"glXGetVideoSyncSGI", (void **) &qglXGetVideoSyncSGI},
+       {"glXWaitVideoSyncSGI", (void **) &qglXWaitVideoSyncSGI},
+       {NULL, NULL}
+};
 
 static Display *vidx11_display = NULL;
 static int scrnum;
@@ -60,8 +83,6 @@ Atom wm_delete_window_atom;
 #define X_MASK (KEY_MASK | MOUSE_MASK | VisibilityChangeMask | StructureNotifyMask )
 
 
-viddef_t       vid;                            // global video state
-
 static qboolean                mouse_avail = true;
 static qboolean                mouse_active = false, usingmouse = false;
 static float   mouse_x, mouse_y;
@@ -453,10 +474,13 @@ void VID_Shutdown(void)
        if (!ctx || !vidx11_display)
                return;
 
+       vid_hidden = true;
+       usingmouse = false;
        if (vidx11_display)
        {
                uninstall_grabs();
 
+               // FIXME: glXDestroyContext here?
                if (vidmode_active)
                        XF86VidModeSwitchToMode(vidx11_display, scrnum, vidmodes[0]);
                if (win)
@@ -515,7 +539,7 @@ void VID_Finish (void)
 
 // handle the mouse state when windowed if that's changed
        usemouse = false;
-       if (vid_mouse.integer && key_dest == key_game)
+       if (vid_mouse.integer && !key_consoleactive)
                usemouse = true;
        if (vidmode_active)
                usemouse = true;
@@ -615,78 +639,57 @@ int VID_SetGamma(float prescale, float gamma, float scale, float base)
 
 void VID_Init(void)
 {
-       int i;
-// LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
-#if 0
-       int gammaattrib[] =
+       Cvar_RegisterVariable (&vid_dga);
+       Cvar_RegisterVariable (&vid_dga_mouseaccel);
+       InitSig(); // trap evil signals
+       if (COM_CheckParm ("-nomouse") || COM_CheckParm("-safe"))
+               mouse_avail = false;
+}
+
+void VID_BuildGLXAttrib(int *attrib, int stencil, int gamma)
+{
+       *attrib++ = GLX_RGBA;
+       *attrib++ = GLX_RED_SIZE;*attrib++ = 1;
+       *attrib++ = GLX_GREEN_SIZE;*attrib++ = 1;
+       *attrib++ = GLX_BLUE_SIZE;*attrib++ = 1;
+       *attrib++ = GLX_DOUBLEBUFFER;
+       *attrib++ = GLX_DEPTH_SIZE;*attrib++ = 1;
+       if (stencil)
        {
-               GLX_RGBA,
-               GLX_RED_SIZE, 1,
-               GLX_GREEN_SIZE, 1,
-               GLX_BLUE_SIZE, 1,
-               GLX_DOUBLEBUFFER,
-               GLX_DEPTH_SIZE, 1,
-               GLX_X_VISUAL_TYPE, GLX_DIRECT_COLOR,
-               None
-       };
-#endif
-       int nogammaattrib[] =
+               *attrib++ = GLX_STENCIL_SIZE;*attrib++ = 8;
+       }
+       if (gamma)
        {
-               GLX_RGBA,
-               GLX_RED_SIZE, 1,
-               GLX_GREEN_SIZE, 1,
-               GLX_BLUE_SIZE, 1,
-               GLX_DOUBLEBUFFER,
-               GLX_DEPTH_SIZE, 1,
-               None
+               *attrib++ = GLX_X_VISUAL_TYPE;*attrib++ = GLX_DIRECT_COLOR;
        };
-       int width = 640, height = 480;
+       *attrib++ = None;
+}
+
+int VID_InitMode(int fullscreen, int width, int height, int bpp, int stencil)
+{
+       int i;
+       int attrib[32];
        XSetWindowAttributes attr;
        unsigned long mask;
        Window root;
        XVisualInfo *visinfo;
-       qboolean fullscreen = true;
        int MajorVersion, MinorVersion;
+       const char *drivername;
 
-       GL_OpenLibrary();
-
-       Cvar_RegisterVariable (&vid_dga);
-       Cvar_RegisterVariable (&vid_dga_mouseaccel);
-
-// interpret command-line params
-
-// set vid parameters
-       if ((i = COM_CheckParm("-window")) != 0)
-               fullscreen = false;
-
-       if ((i = COM_CheckParm("-width")) != 0)
-               width = atoi(com_argv[i+1]);
-
-       if ((i = COM_CheckParm("-height")) != 0)
-               height = atoi(com_argv[i+1]);
-
-       if ((i = COM_CheckParm("-conwidth")) != 0)
-               vid.conwidth = atoi(com_argv[i+1]);
-       else
-               vid.conwidth = 640;
-
-       vid.conwidth &= 0xfff8; // make it a multiple of eight
-
-       if (vid.conwidth < 320)
-               vid.conwidth = 320;
-
-       // pick a conheight that matches with correct aspect
-       vid.conheight = vid.conwidth*3 / 4;
-
-       if ((i = COM_CheckParm("-conheight")) != 0)
-               vid.conheight = atoi(com_argv[i+1]);
-       if (vid.conheight < 200)
-               vid.conheight = 200;
+       drivername = "libGL.so.1";
+       i = COM_CheckParm("-gl_driver");
+       if (i && i < com_argc - 1)
+               drivername = com_argv[i + 1];
+       if (!GL_OpenLibrary(drivername))
+       {
+               Con_Printf("Unable to load GL driver \"%s\"\n", drivername);
+               return false;
+       }
 
        if (!(vidx11_display = XOpenDisplay(NULL)))
        {
-               fprintf(stderr, "Error couldn't open the X display\n");
-               exit(1);
+               Con_Printf("Couldn't open the X display\n");
+               return false;
        }
 
        scrnum = DefaultScreen(vidx11_display);
@@ -705,22 +708,30 @@ void VID_Init(void)
        if ((qglXChooseVisual = GL_GetProcAddress("glXChooseVisual")) == NULL
         || (qglXCreateContext = GL_GetProcAddress("glXCreateContext")) == NULL
         || (qglXMakeCurrent = GL_GetProcAddress("glXMakeCurrent")) == NULL
-        || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL)
-               Sys_Error("glX functions not found in libGL.so.1\n");
+        || (qglXSwapBuffers = GL_GetProcAddress("glXSwapBuffers")) == NULL
+        || (qglXQueryExtensionsString = GL_GetProcAddress("glXQueryExtensionsString")) == NULL)
+       {
+               Con_Printf("glX functions not found in %s\n", gl_driver);
+               return false;
+       }
 
        visinfo = NULL;
 // LordHavoc: FIXME: finish this code, we need to allocate colors before we can store them
 #if 0
        if (!COM_CheckParm("-nogamma"))
-               visinfo = qglXChooseVisual(vidx11_display, scrnum, gammaattrib);
+       {
+               VID_BuildGLXAttrib(attrib, stencil, true);
+               visinfo = qglXChooseVisual(vidx11_display, scrnum, attrib);
+       }
 #endif
        if (!visinfo)
        {
-               visinfo = qglXChooseVisual(vidx11_display, scrnum, nogammaattrib);
+               VID_BuildGLXAttrib(attrib, stencil, false);
+               visinfo = qglXChooseVisual(vidx11_display, scrnum, attrib);
                if (!visinfo)
                {
-                       Sys_Error("couldn't get an RGB, Double-buffered, Depth visual\n");
-                       exit(1);
+                       Con_Printf("Couldn't get an RGB, Double-buffered, Depth visual\n");
+                       return false;
                }
        }
 
@@ -809,27 +820,37 @@ void VID_Init(void)
                XF86VidModeSetViewPort(vidx11_display, scrnum, 0, 0);
        }
 
-       XFlush(vidx11_display);
+       //XSync(vidx11_display, False);
 
        ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
+       if (!ctx)
+               Sys_Error ("glXCreateContext failed\n");
 
-       qglXMakeCurrent(vidx11_display, win, ctx);
+       if (!qglXMakeCurrent(vidx11_display, win, ctx))
+               Sys_Error ("glXMakeCurrent failed\n");
+
+       XSync(vidx11_display, False);
 
        scr_width = width;
        scr_height = height;
 
-       if (vid.conheight > height)
-               vid.conheight = height;
-       if (vid.conwidth > width)
-               vid.conwidth = width;
+       if ((qglGetString = GL_GetProcAddress("glGetString")) == NULL)
+               Sys_Error("glGetString not found in %s", gl_driver);
 
-       InitSig(); // trap evil signals
+       gl_renderer = qglGetString(GL_RENDERER);
+       gl_vendor = qglGetString(GL_VENDOR);
+       gl_version = qglGetString(GL_VERSION);
+       gl_extensions = qglGetString(GL_EXTENSIONS);
+       gl_platform = "GLX";
+       gl_platformextensions = qglXQueryExtensionsString(vidx11_display, scrnum);
 
-       vid_hidden = false;
+       GL_CheckExtension("GLX_ARB_get_proc_address", getprocaddressfuncs, "-nogetprocaddress", false);
+       gl_videosyncavailable = GL_CheckExtension("GLX_SGI_video_sync", videosyncfuncs, "-novideosync", false);
 
+       usingmouse = false;
+       vid_hidden = false;
        GL_Init();
-
-       Con_SafePrintf ("Video mode %dx%d initialized.\n", width, height);
+       return true;
 }
 
 void Sys_SendKeyEvents(void)
@@ -837,16 +858,6 @@ void Sys_SendKeyEvents(void)
        HandleEvents();
 }
 
-void IN_Init(void)
-{
-       if (COM_CheckParm ("-nomouse"))
-               mouse_avail = false;
-}
-
-void IN_Shutdown(void)
-{
-}
-
 /*
 ===========
 IN_Commands
@@ -864,3 +875,39 @@ void IN_Move (usercmd_t *cmd)
        mouse_y = 0;
 }
 
+static void *prjobj = NULL;
+
+int GL_OpenLibrary(const char *name)
+{
+       Con_Printf("Loading GL driver %s\n", name);
+       GL_CloseLibrary();
+       if (!(prjobj = dlopen(name, RTLD_LAZY)))
+       {
+               Con_Printf("Unable to open symbol list for %s\n", name);
+               return false;
+       }
+       strcpy(gl_driver, name);
+       return true;
+}
+
+void GL_CloseLibrary(void)
+{
+       if (prjobj)
+               dlclose(prjobj);
+       prjobj = NULL;
+       gl_driver[0] = 0;
+       qglXGetProcAddressARB = NULL;
+       gl_extensions = "";
+       gl_platform = "";
+       gl_platformextensions = "";
+}
+
+void *GL_GetProcAddress(const char *name)
+{
+       void *p = NULL;
+       if (qglXGetProcAddressARB != NULL)
+               p = (void *) qglXGetProcAddressARB(name);
+       if (p == NULL)
+               p = (void *) dlsym(prjobj, name);
+       return p;
+}