]> git.tdb.fi Git - gldbg.git/blobdiff - source/glwrap.c
Support command aliases
[gldbg.git] / source / glwrap.c
index 79d6404d2a34b21cf9cfeb3bfb045e6ecb7bda0b..68351143d31aca0567dca79c1d7051a885ced85c 100644 (file)
@@ -21,10 +21,13 @@ static inline void *glsym(const char *sym)
        static void *libgl = NULL;
        if(!libgl)
        {
-               libgl = dlopen("libGL.so", RTLD_NOW);
+               const char *libgl_name = getenv("GLWRAP_LIBGL");
+               if(!libgl_name)
+                       libgl_name = "libGL.so";
+               libgl = dlopen(libgl_name, RTLD_NOW);
                if(!libgl)
                {
-                       fprintf(stderr, "Could not open libGL: %s\n", dlerror());
+                       fprintf(stderr, "Could not open %s: %s\n", libgl_name, dlerror());
                        abort();
                }
        }
@@ -76,12 +79,7 @@ static inline void write_long(long v)
        write_bytes((char *)&v, sizeof(long));
 }
 
-static inline void write_ulong(unsigned long v)
-{
-       write_bytes((char *)&v, sizeof(unsigned long));
-}
-
-static inline void write_longlong(long long v)
+static inline void write_long_long(long long v)
 {
        write_bytes((char *)&v, sizeof(long long));
 }
@@ -117,15 +115,15 @@ static inline void write_data(const void *data, unsigned size)
                write_int(0);
 }
 
-static inline void write_string(const unsigned char *s)
+static inline void write_string(const char *s)
 {
        write_data(s, strlen(s)+1);
 }
 
-static inline void write_string_array(const unsigned char **sa, unsigned size)
+static inline void write_string_array(const char **sa, unsigned size)
 {
        unsigned i;
-       size /= sizeof(const unsigned char *);
+       size /= sizeof(const char *);
        write_int(size);
        for(i=0; i<size; ++i)
                write_string(sa[i]);
@@ -162,43 +160,98 @@ static inline void send_packet()
        writev(fd, iovecs, cur_vec-iovecs);
 }
 
+int in_begin_block = 0;
 GLenum cur_error = GL_NO_ERROR;
 
 static void check_error()
 {
        GLenum (*orig_glGetError)() = 0;
        GLenum code;
+
+       if(in_begin_block)
+               return;
+
        if(!orig_glGetError)
                orig_glGetError = glsym("glGetError");
+
        code = orig_glGetError();
        if(code!=GL_NO_ERROR)
        {
                begin_packet(FUNC_GLDERROR);
                write_int(code);
                send_packet();
+
                if(cur_error==GL_NO_ERROR)
                        cur_error = code;
        }
 }
 
+void APIENTRY glBegin(GLenum mode)
+{
+       static void (*orig)(GLenum);
+       if(!orig)
+               orig = glsym("glBegin");
+       orig(mode);
+
+       begin_packet(FUNC_GLBEGIN);
+       write_int(mode);
+       send_packet();
+
+       in_begin_block = 1;
+}
+
+void APIENTRY glEnd()
+{
+       static void (*orig)();
+       if(!orig)
+               orig = glsym("glEnd");
+       orig();
+
+       begin_packet(FUNC_GLEND);
+       send_packet();
+
+       in_begin_block = 0;
+       check_error();
+}
+
 GLenum APIENTRY glGetError()
 {
-       GLenum ret = cur_error;
-       cur_error = GL_NO_ERROR;
+       GLenum ret = GL_NO_ERROR;
+
+       if(in_begin_block)
+       {
+               if(cur_error==GL_NO_ERROR)
+                       cur_error = GL_INVALID_OPERATION;
+       }
+       else
+       {
+               ret = cur_error;
+               cur_error = GL_NO_ERROR;
+       }
+
        begin_packet(FUNC_GLGETERROR);
        write_int(ret);
        send_packet();
+
        return ret;
 }
 
 void (*glXGetProcAddress(const GLubyte *procname))(void)
 {
-       void *handle = dlopen(NULL, RTLD_LAZY);
-       void (*ret)() = dlsym(handle, (const char *)procname);
+       void *handle = 0;
+       void (*ret)() = 0;
+
+       if(glsym((const char *)procname))
+       {
+               handle = dlopen(NULL, RTLD_LAZY);
+               ret = dlsym(handle, (const char *)procname);
+       }
+
        begin_packet(FUNC_GLXGETPROCADDRESS);
        write_pointer(ret);
-       write_string(procname);
+       write_string((const char *)procname);
        send_packet();
+
        return ret;
 }