]> git.tdb.fi Git - gldbg.git/blob - source/glwrap_funcs.c
Fix glXGetProcAddress serialisation order
[gldbg.git] / source / glwrap_funcs.c
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <dlfcn.h>
9 #include <X11/Xlib.h>
10 #include <GL/gl.h>
11 #include <GL/glx.h>
12 #include "arraysize.h"
13 #include "functions.h"
14 #include "glwrap.h"
15
16 int in_begin_block = 0;
17 GLenum cur_error = GL_NO_ERROR;
18
19 static void check_error()
20 {
21         GLenum (*orig_glGetError)() = 0;
22         GLenum code;
23
24         if(in_begin_block)
25                 return;
26
27         if(!orig_glGetError)
28                 orig_glGetError = glsym("glGetError");
29
30         code = orig_glGetError();
31         if(code!=GL_NO_ERROR)
32         {
33                 begin_packet(FUNC_GLDERROR);
34                 write_int(code);
35                 send_packet();
36
37                 if(cur_error==GL_NO_ERROR)
38                         cur_error = code;
39         }
40 }
41
42 void APIENTRY glBegin(GLenum mode)
43 {
44         static void (*orig)(GLenum);
45         if(!orig)
46                 orig = glsym("glBegin");
47         orig(mode);
48
49         begin_packet(FUNC_GLBEGIN);
50         write_int(mode);
51         send_packet();
52
53         in_begin_block = 1;
54 }
55
56 void APIENTRY glEnd()
57 {
58         static void (*orig)();
59         if(!orig)
60                 orig = glsym("glEnd");
61         orig();
62
63         begin_packet(FUNC_GLEND);
64         send_packet();
65
66         in_begin_block = 0;
67         check_error();
68 }
69
70 GLenum APIENTRY glGetError()
71 {
72         GLenum ret = GL_NO_ERROR;
73
74         if(in_begin_block)
75         {
76                 if(cur_error==GL_NO_ERROR)
77                         cur_error = GL_INVALID_OPERATION;
78         }
79         else
80         {
81                 ret = cur_error;
82                 cur_error = GL_NO_ERROR;
83         }
84
85         begin_packet(FUNC_GLGETERROR);
86         write_int(ret);
87         send_packet();
88
89         return ret;
90 }
91
92 void (*glXGetProcAddress(const GLubyte *procname))(void)
93 {
94         void *handle = 0;
95         void (*ret)() = 0;
96
97         if(glsym((const char *)procname))
98         {
99                 handle = dlopen(NULL, RTLD_LAZY);
100                 ret = dlsym(handle, (const char *)procname);
101         }
102
103         begin_packet(FUNC_GLXGETPROCADDRESS);
104         write_string((const char *)procname);
105         write_pointer(ret);
106         send_packet();
107
108         return ret;
109 }
110
111 void (*glXGetProcAddressARB(const GLubyte *))(void) __attribute__((alias("glXGetProcAddress")));
112
113 #include "gensrc/glwrap.funcs"