]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/glwrap_funcs.c
Use a centralized packet framework
[gldbg.git] / flavors / gl / 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 "arraysize.h"
10 #include "functions.h"
11 #include "glwrap.h"
12 #include "opengl.h"
13 #include "packet.h"
14
15 static int in_begin_block = 0;
16 static GLenum cur_error = GL_NO_ERROR;
17
18 static void check_error()
19 {
20         static GLenum (*orig_glGetError)() = NULL;
21         GLenum code;
22
23         if(in_begin_block)
24                 return;
25
26         if(!orig_glGetError)
27                 orig_glGetError = glsym("glGetError");
28
29         code = orig_glGetError();
30         if(code!=GL_NO_ERROR)
31         {
32                 GlPacket *pkt;
33
34                 pkt = packet_begin(FUNC_GLDERROR);
35                 packet_write_int(pkt, code);
36                 packet_send(pkt, get_out_fd());
37
38                 if(cur_error==GL_NO_ERROR)
39                         cur_error = code;
40         }
41 }
42
43 void APIENTRY glBegin(GLenum mode)
44 {
45         static void (*orig)(GLenum) = NULL;
46         GlPacket *pkt;
47
48         if(!orig)
49                 orig = glsym("glBegin");
50         orig(mode);
51
52         pkt = packet_begin(FUNC_GLBEGIN);
53         packet_write_int(pkt, mode);
54         packet_send(pkt, get_out_fd());
55
56         in_begin_block = 1;
57 }
58
59 void APIENTRY glEnd()
60 {
61         static void (*orig)() = NULL;
62         GlPacket *pkt;
63
64         if(!orig)
65                 orig = glsym("glEnd");
66         orig();
67
68         pkt = packet_begin(FUNC_GLEND);
69         packet_send(pkt, get_out_fd());
70
71         in_begin_block = 0;
72         check_error();
73 }
74
75 GLenum APIENTRY glGetError()
76 {
77         GLenum ret = GL_NO_ERROR;
78         GlPacket *pkt;
79
80         if(in_begin_block)
81         {
82                 if(cur_error==GL_NO_ERROR)
83                         cur_error = GL_INVALID_OPERATION;
84         }
85         else
86         {
87                 ret = cur_error;
88                 cur_error = GL_NO_ERROR;
89         }
90
91         pkt = packet_begin(FUNC_GLGETERROR);
92         packet_write_int(pkt, ret);
93         packet_send(pkt, get_out_fd());
94
95         return ret;
96 }
97
98 void (*glXGetProcAddress(const GLubyte *procname))(void)
99 {
100         void *handle = 0;
101         void (*ret)() = 0;
102         GlPacket *pkt;
103
104         if(glsym((const char *)procname))
105         {
106                 handle = dlopen(NULL, RTLD_LAZY);
107                 ret = dlsym(handle, (const char *)procname);
108         }
109
110         pkt = packet_begin(FUNC_GLXGETPROCADDRESS);
111         packet_write_string(pkt, (const char *)procname);
112         packet_write_pointer(pkt, ret);
113         packet_send(pkt, get_out_fd());
114
115         return ret;
116 }
117
118 void (*glXGetProcAddressARB(const GLubyte *))(void) __attribute__((alias("glXGetProcAddress")));
119
120 #include "gensrc/glwrap.funcs"