-
Notifications
You must be signed in to change notification settings - Fork 2
/
x11.c
278 lines (242 loc) · 6.38 KB
/
x11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <Component.h>
#include "prima_gl.h"
#include <unix/guts.h>
#define Drawable XDrawable
#define Font XFont
#define Window XWindow
#include <GL/gl.h>
#include <GL/glx.h>
#ifdef __cplusplus
extern "C" {
#endif
#define var (( PComponent) widget)
#define ctx (( Context*) context)
#define sys (( PDrawableSysData) var-> sysData)
typedef struct {
Drawable drawable;
GLXContext context;
int pixmap;
} Context;
typedef struct {
GLXDrawable drawable;
GLXContext context;
} ContextStackEntry;
ContextStackEntry stack[CONTEXT_STACK_SIZE];
int stack_ptr = 0;
#define ERROR_CHOOSE_VISUAL 1
#define ERROR_CREATE_CONTEXT 2
#define ERROR_OTHER 3
#define ERROR_NO_PRINTER 4
#define ERROR_NO_PIXMAPS 5
#define ERROR_STACK_UNDERFLOW 6
#define ERROR_STACK_OVERFLOW 7
int last_error = 0;
UnixGuts * pguts;
#define CLEAR_ERROR last_error = 0
#define SET_ERROR(s) last_error = s
static XVisualInfo *
select_visual( Display * display, int screen, int * attr_list)
{
int n_attrs;
GLXFBConfig * fb;
fb = glXChooseFBConfig(display, screen, attr_list, &n_attrs);
if ( !fb) return NULL;
return glXGetVisualFromFBConfig( display, *fb);
}
Handle
gl_context_create( Handle widget, GLRequest * request)
{
int attr_list[64], *attr = attr_list;
XVisualInfo * visual;
GLXContext context;
Context * ret;
if ( pguts == NULL )
pguts = (UnixGuts*) apc_system_action("unix_guts");
CLEAR_ERROR;
XCHECKPOINT;
#define ATTR(in,out) \
if ( request-> in) { \
*(attr++) = out; \
*(attr++) = request-> in; \
}
if ( request-> pixels == GLREQ_PIXEL_RGBA) *(attr++) = GLX_RGBA;
if ( request-> double_buffer == GLREQ_TRUE) *(attr++) = GLX_DOUBLEBUFFER;
if ( request-> stereo == GLREQ_TRUE) *(attr++) = GLX_STEREO;
ATTR( layer , GLX_LEVEL )
ATTR( color_bits , GLX_BUFFER_SIZE )
ATTR( aux_buffers , GLX_AUX_BUFFERS )
ATTR( red_bits , GLX_RED_SIZE )
ATTR( green_bits , GLX_GREEN_SIZE )
ATTR( blue_bits , GLX_BLUE_SIZE )
ATTR( alpha_bits , GLX_ALPHA_SIZE )
ATTR( depth_bits , GLX_DEPTH_SIZE )
ATTR( stencil_bits , GLX_STENCIL_SIZE )
ATTR( accum_red_bits , GLX_ACCUM_RED_SIZE )
ATTR( accum_green_bits, GLX_ACCUM_GREEN_SIZE )
ATTR( accum_blue_bits , GLX_ACCUM_BLUE_SIZE )
ATTR( accum_alpha_bits, GLX_ACCUM_ALPHA_SIZE )
switch ( request-> target) {
case GLREQ_TARGET_BITMAP:
case GLREQ_TARGET_IMAGE:
*(attr++) = GLX_X_RENDERABLE;
*(attr++) = 1;
*(attr++) = GLX_DRAWABLE_TYPE;
*(attr++) = GLX_PIXMAP_BIT;
break;
}
*(attr++) = 0;
if ( request-> target == GLREQ_TARGET_WINDOW && sys-> flags. layered) {
visual = sys-> visual;
} else if ( !( visual = select_visual( DISP, SCREEN, attr_list ))) {
if ( request-> pixels != GLREQ_PIXEL_RGBA) {
/* emulate win32 which does it softly, i.e. if RGBA fails, it proposes PALETTED */
request-> pixels = GLREQ_PIXEL_RGBA;
return gl_context_create( widget, request);
}
if ( request-> double_buffer == GLREQ_DONTCARE) {
request-> double_buffer = GLREQ_TRUE;
return gl_context_create( widget, request );
}
SET_ERROR( ERROR_CHOOSE_VISUAL );
return (Handle) 0;
}
XCHECKPOINT;
if ( !( context = glXCreateContext( DISP, visual, 0, request-> render != GLREQ_RENDER_XSERVER))) {
SET_ERROR( ERROR_CREATE_CONTEXT );
return (Handle) 0;
}
ret = malloc( sizeof( Context ));
memset( ret, 0, sizeof( Context));
ret-> context = context;
switch ( request-> target) {
case GLREQ_TARGET_WINDOW:
ret-> drawable = var-> handle;
break;
case GLREQ_TARGET_APPLICATION:
/* doesn't work with gnome and kde anyway */
ret-> drawable = RootWindow( DISP, SCREEN);
break;
case GLREQ_TARGET_BITMAP:
case GLREQ_TARGET_IMAGE:
{
GLXContext old_context;
GLXDrawable old_drawable;
Bool success;
XCHECKPOINT;
ret-> drawable = glXCreateGLXPixmap( DISP, visual, sys-> gdrawable);
ret-> pixmap = 1;
/* check if pixmaps are supported on this visual at all */
old_context = glXGetCurrentContext();
old_drawable = glXGetCurrentDrawable();
success = glXMakeCurrent( DISP, ret-> drawable, ret-> context);
glXMakeCurrent( DISP, old_drawable, old_context);
if ( !success ) {
SET_ERROR( ERROR_NO_PIXMAPS );
glXDestroyGLXPixmap( DISP, ret-> drawable);
glXDestroyContext( DISP, ret-> context );
free(ret);
return 0;
}
break;
}
case GLREQ_TARGET_PRINTER:
SET_ERROR(ERROR_NO_PRINTER);
free(ret);
return 0;
}
return (Handle) ret;
}
void
gl_context_destroy( Handle context)
{
CLEAR_ERROR;
XCHECKPOINT;
if ( glXGetCurrentContext() == ctx-> context)
glXMakeCurrent( DISP, 0, NULL);
if ( ctx-> pixmap)
glXDestroyGLXPixmap( DISP, ctx-> drawable);
glXDestroyContext( DISP, ctx-> context );
free(( void*) ctx );
}
Bool
gl_context_make_current( Handle context)
{
Bool ret;
CLEAR_ERROR;
XCHECKPOINT;
if ( context ) {
ret = glXMakeCurrent( DISP, ctx-> drawable, ctx-> context);
} else {
ret = glXMakeCurrent( DISP, 0, NULL );
}
if ( !ret ) SET_ERROR( ERROR_OTHER );
return ret;
}
Bool
gl_flush( Handle context)
{
CLEAR_ERROR;
XCHECKPOINT;
glXSwapBuffers( DISP, ctx-> drawable );
return true;
}
int
gl_context_push(void)
{
CLEAR_ERROR;
if ( stack_ptr >= CONTEXT_STACK_SIZE ) {
SET_ERROR( ERROR_STACK_OVERFLOW );
return 0;
}
stack[stack_ptr].context = glXGetCurrentContext();
stack[stack_ptr].drawable = glXGetCurrentDrawable();
stack_ptr++;
return 1;
}
int
gl_context_pop(void)
{
CLEAR_ERROR;
if ( stack_ptr <= 0) {
SET_ERROR( ERROR_STACK_UNDERFLOW );
return 0;
}
stack_ptr--;
return glXMakeCurrent( DISP, stack[stack_ptr].drawable, stack[stack_ptr].context);
}
char *
gl_error_string(char * buf, int len)
{
switch ( last_error ) {
case 0:
return NULL;
case ERROR_CHOOSE_VISUAL:
return "glXChooseVisual: cannot find a requested GL visual";
case ERROR_CREATE_CONTEXT:
return "glXCreateContext error";
case ERROR_OTHER:
return "unknown error";
case ERROR_NO_PRINTER:
return "No printer support on X11";
case ERROR_NO_PIXMAPS:
return "Pixmaps are unsupported on this GL visual";
case ERROR_STACK_UNDERFLOW:
return "No GL contexts on stack";
case ERROR_STACK_OVERFLOW:
return "No more space for GL contexts on stack";
}
return NULL;
}
Bool
gl_is_direct(Handle context)
{
CLEAR_ERROR;
XCHECKPOINT;
return glXIsDirect( DISP, ctx-> context );
}
#ifdef __cplusplus
}
#endif