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