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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
|
#include <Python.h>
#include <structmember.h>
#include <dlfcn.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#define GLX_CONTEXT_MAJOR_VERSION 0x2091
#define GLX_CONTEXT_MINOR_VERSION 0x2092
#define GLX_CONTEXT_PROFILE_MASK 0x9126
#define GLX_CONTEXT_CORE_PROFILE_BIT 0x0001
#define GLX_RGBA 4
#define GLX_DOUBLEBUFFER 5
#define GLX_RED_SIZE 8
#define GLX_GREEN_SIZE 9
#define GLX_BLUE_SIZE 10
#define GLX_DEPTH_SIZE 12
typedef struct __GLXcontextRec * GLXContext;
typedef struct __GLXFBConfigRec * GLXFBConfig;
typedef XID GLXDrawable;
typedef GLXFBConfig * (* m_glXChooseFBConfigProc)(Display *, int, const int *, int *);
typedef XVisualInfo * (* m_glXChooseVisualProc)(Display *, int, int *);
typedef Display * (* m_glXGetCurrentDisplayProc)();
typedef GLXContext (* m_glXGetCurrentContextProc)();
typedef GLXDrawable (* m_glXGetCurrentDrawableProc)();
typedef Bool (* m_glXMakeCurrentProc)(Display *, GLXDrawable, GLXContext);
typedef void (* m_glXDestroyContextProc)(Display *, GLXContext);
typedef GLXContext (* m_glXCreateContextProc)(Display *, XVisualInfo *, GLXContext, Bool);
typedef void (*(* m_glXGetProcAddressProc)(const unsigned char *))();
typedef GLXContext (* m_glXCreateContextAttribsARBProc)(Display *, GLXFBConfig, GLXContext, int, const int *);
typedef Display * (* m_XOpenDisplayProc)(const char *);
typedef int (* m_XDefaultScreenProc)(Display *);
typedef Window (* m_XRootWindowProc)(Display *, int);
typedef Colormap (* m_XCreateColormapProc)(Display *, Window, Visual *, int);
typedef Window (* m_XCreateWindowProc)(Display *, Window, int, int, unsigned int, unsigned int, unsigned int, int, unsigned int, Visual *, unsigned long, XSetWindowAttributes *);
typedef int (* m_XDestroyWindowProc)(Display *, Window);
typedef int (* m_XCloseDisplayProc)(Display *);
typedef int (* m_XFreeProc)(void *);
typedef XErrorHandler (* m_XSetErrorHandlerProc)(XErrorHandler);
int SilentXErrorHandler(Display * d, XErrorEvent * e) {
return 0;
}
struct GLContext {
PyObject_HEAD
void * libgl;
void * libx11;
Display * dpy;
GLXFBConfig * fbc;
XVisualInfo * vi;
Window wnd;
GLXContext ctx;
int standalone;
int own_window;
void * old_context;
void * old_display;
void * old_window;
m_glXChooseFBConfigProc m_glXChooseFBConfig;
m_glXChooseVisualProc m_glXChooseVisual;
m_glXGetCurrentDisplayProc m_glXGetCurrentDisplay;
m_glXGetCurrentContextProc m_glXGetCurrentContext;
m_glXGetCurrentDrawableProc m_glXGetCurrentDrawable;
m_glXMakeCurrentProc m_glXMakeCurrent;
m_glXDestroyContextProc m_glXDestroyContext;
m_glXCreateContextProc m_glXCreateContext;
m_glXGetProcAddressProc m_glXGetProcAddress;
m_glXCreateContextAttribsARBProc m_glXCreateContextAttribsARB;
m_XOpenDisplayProc m_XOpenDisplay;
m_XDefaultScreenProc m_XDefaultScreen;
m_XRootWindowProc m_XRootWindow;
m_XCreateColormapProc m_XCreateColormap;
m_XCreateWindowProc m_XCreateWindow;
m_XDestroyWindowProc m_XDestroyWindow;
m_XCloseDisplayProc m_XCloseDisplay;
m_XFreeProc m_XFree;
m_XSetErrorHandlerProc m_XSetErrorHandler;
};
PyTypeObject * GLContext_type;
GLContext * meth_create_context(PyObject * self, PyObject * args, PyObject * kwargs) {
static char * keywords[] = {"mode", "libgl", "libx11", "glversion", NULL};
const char * mode = "detect";
const char * libgl = "libGL.so";
const char * libx11 = "libX11.so";
int glversion = 330;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|sssi", keywords, &mode, &libgl, &libx11, &glversion)) {
return NULL;
}
GLContext * res = PyObject_New(GLContext, GLContext_type);
res->libgl = dlopen(libgl, RTLD_LAZY);
if (!res->libgl) {
PyErr_Format(PyExc_Exception, "%s not found in /lib, /usr/lib or LD_LIBRARY_PATH", libgl);
return NULL;
}
res->m_glXChooseFBConfig = (m_glXChooseFBConfigProc)dlsym(res->libgl, "glXChooseFBConfig");
if (!res->m_glXChooseFBConfig) {
PyErr_Format(PyExc_Exception, "glXChooseFBConfig not found");
return NULL;
}
res->m_glXChooseVisual = (m_glXChooseVisualProc)dlsym(res->libgl, "glXChooseVisual");
if (!res->m_glXChooseVisual) {
PyErr_Format(PyExc_Exception, "glXChooseVisual not found");
return NULL;
}
res->m_glXGetCurrentDisplay = (m_glXGetCurrentDisplayProc)dlsym(res->libgl, "glXGetCurrentDisplay");
if (!res->m_glXGetCurrentDisplay) {
PyErr_Format(PyExc_Exception, "glXGetCurrentDisplay not found");
return NULL;
}
res->m_glXGetCurrentContext = (m_glXGetCurrentContextProc)dlsym(res->libgl, "glXGetCurrentContext");
if (!res->m_glXGetCurrentContext) {
PyErr_Format(PyExc_Exception, "glXGetCurrentContext not found");
return NULL;
}
res->m_glXGetCurrentDrawable = (m_glXGetCurrentDrawableProc)dlsym(res->libgl, "glXGetCurrentDrawable");
if (!res->m_glXGetCurrentDrawable) {
PyErr_Format(PyExc_Exception, "glXGetCurrentDrawable not found");
return NULL;
}
res->m_glXMakeCurrent = (m_glXMakeCurrentProc)dlsym(res->libgl, "glXMakeCurrent");
if (!res->m_glXMakeCurrent) {
PyErr_Format(PyExc_Exception, "glXMakeCurrent not found");
return NULL;
}
res->m_glXDestroyContext = (m_glXDestroyContextProc)dlsym(res->libgl, "glXDestroyContext");
if (!res->m_glXDestroyContext) {
PyErr_Format(PyExc_Exception, "glXDestroyContext not found");
return NULL;
}
res->m_glXCreateContext = (m_glXCreateContextProc)dlsym(res->libgl, "glXCreateContext");
if (!res->m_glXCreateContext) {
PyErr_Format(PyExc_Exception, "glXCreateContext not found");
return NULL;
}
res->m_glXGetProcAddress = (m_glXGetProcAddressProc)dlsym(res->libgl, "glXGetProcAddress");
if (!res->m_glXGetProcAddress) {
PyErr_Format(PyExc_Exception, "glXGetProcAddress not found");
return NULL;
}
if (strcmp(mode, "detect")) {
res->libx11 = dlopen(libx11, RTLD_LAZY);
if (!res->libx11) {
PyErr_Format(PyExc_Exception, "(detect) %s not loaded", libx11);
return NULL;
}
res->m_XOpenDisplay = (m_XOpenDisplayProc)dlsym(res->libx11, "XOpenDisplay");
if (!res->m_XOpenDisplay) {
PyErr_Format(PyExc_Exception, "(detect) XOpenDisplay not found");
return NULL;
}
res->m_XDefaultScreen = (m_XDefaultScreenProc)dlsym(res->libx11, "XDefaultScreen");
if (!res->m_XDefaultScreen) {
PyErr_Format(PyExc_Exception, "(detect) XDefaultScreen not found");
return NULL;
}
res->m_XRootWindow = (m_XRootWindowProc)dlsym(res->libx11, "XRootWindow");
if (!res->m_XRootWindow) {
PyErr_Format(PyExc_Exception, "(detect) XRootWindow not found");
return NULL;
}
res->m_XCreateColormap = (m_XCreateColormapProc)dlsym(res->libx11, "XCreateColormap");
if (!res->m_XCreateColormap) {
PyErr_Format(PyExc_Exception, "(detect) XCreateColormap not found");
return NULL;
}
res->m_XCreateWindow = (m_XCreateWindowProc)dlsym(res->libx11, "XCreateWindow");
if (!res->m_XCreateWindow) {
PyErr_Format(PyExc_Exception, "(detect) XCreateWindow not found");
return NULL;
}
res->m_XDestroyWindow = (m_XDestroyWindowProc)dlsym(res->libx11, "XDestroyWindow");
if (!res->m_XDestroyWindow) {
PyErr_Format(PyExc_Exception, "(detect) XDestroyWindow not found");
return NULL;
}
res->m_XCloseDisplay = (m_XCloseDisplayProc)dlsym(res->libx11, "XCloseDisplay");
if (!res->m_XCloseDisplay) {
PyErr_Format(PyExc_Exception, "(detect) XCloseDisplay not found");
return NULL;
}
res->m_XFree = (m_XFreeProc)dlsym(res->libx11, "XFree");
if (!res->m_XFree) {
PyErr_Format(PyExc_Exception, "(detect) XFree not found");
return NULL;
}
res->m_XSetErrorHandler = (m_XSetErrorHandlerProc)dlsym(res->libx11, "XSetErrorHandler");
if (!res->m_XSetErrorHandler) {
PyErr_Format(PyExc_Exception, "(detect) XSetErrorHandler not found");
return NULL;
}
}
if (!strcmp(mode, "detect")) {
res->standalone = false;
res->own_window = false;
res->ctx = res->m_glXGetCurrentContext();
if (!res->ctx) {
PyErr_Format(PyExc_Exception, "(detect) glXGetCurrentContext: cannot detect OpenGL context");
return NULL;
}
res->wnd = res->m_glXGetCurrentDrawable();
if (!res->wnd) {
PyErr_Format(PyExc_Exception, "(detect) glXGetCurrentDrawable failed");
return NULL;
}
res->dpy = res->m_glXGetCurrentDisplay();
if (!res->dpy) {
PyErr_Format(PyExc_Exception, "(detect) glXGetCurrentDisplay failed");
return NULL;
}
res->fbc = NULL;
res->vi = NULL;
return res;
}
if (!strcmp(mode, "share")) {
res->standalone = true;
res->own_window = false;
GLXContext ctx_share = res->m_glXGetCurrentContext();
if (!ctx_share) {
PyErr_Format(PyExc_Exception, "(share) glXGetCurrentContext: cannot detect OpenGL context");
return NULL;
}
res->wnd = res->m_glXGetCurrentDrawable();
if (!res->wnd) {
PyErr_Format(PyExc_Exception, "(share) glXGetCurrentDrawable failed");
return NULL;
}
res->dpy = res->m_glXGetCurrentDisplay();
if (!res->dpy) {
PyErr_Format(PyExc_Exception, "(share) glXGetCurrentDisplay failed");
return NULL;
}
int nelements = 0;
res->fbc = res->m_glXChooseFBConfig(res->dpy, res->m_XDefaultScreen(res->dpy), 0, &nelements);
if (!res->fbc) {
res->m_XCloseDisplay(res->dpy);
PyErr_Format(PyExc_Exception, "(share) glXChooseFBConfig failed");
return NULL;
}
static int attribute_list[] = {
GLX_RGBA,
GLX_DOUBLEBUFFER,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_DEPTH_SIZE, 24,
None,
};
res->vi = res->m_glXChooseVisual(res->dpy, res->m_XDefaultScreen(res->dpy), attribute_list);
if (!res->vi) {
res->m_XCloseDisplay(res->dpy);
PyErr_Format(PyExc_Exception, "(share) glXChooseVisual: cannot choose visual");
return NULL;
}
res->m_XSetErrorHandler(SilentXErrorHandler);
if (glversion) {
void (* proc)() = res->m_glXGetProcAddress((const unsigned char *)"glXCreateContextAttribsARB");
res->m_glXCreateContextAttribsARB = (m_glXCreateContextAttribsARBProc)proc;
if (!res->m_glXCreateContextAttribsARB) {
PyErr_Format(PyExc_Exception, "(share) glXCreateContextAttribsARB not found");
return NULL;
}
int attribs[] = {
GLX_CONTEXT_PROFILE_MASK, GLX_CONTEXT_CORE_PROFILE_BIT,
GLX_CONTEXT_MAJOR_VERSION, glversion / 100 % 10,
GLX_CONTEXT_MINOR_VERSION, glversion / 10 % 10,
0, 0,
};
res->ctx = res->m_glXCreateContextAttribsARB(res->dpy, *res->fbc, ctx_share, true, attribs);
} else {
res->ctx = res->m_glXCreateContext(res->dpy, res->vi, ctx_share, true);
}
if (!res->ctx) {
PyErr_Format(PyExc_Exception, "(share) cannot create context");
return NULL;
}
res->m_XSetErrorHandler(NULL);
if (!res->m_glXMakeCurrent(res->dpy, res->wnd, res->ctx)) {
PyErr_Format(PyExc_Exception, "(share) glXMakeCurrent failed");
return NULL;
}
return res;
}
if (!strcmp(mode, "standalone")) {
res->standalone = true;
res->own_window = true;
res->dpy = res->m_XOpenDisplay(NULL);
if (!res->dpy) {
res->dpy = res->m_XOpenDisplay(":0.0");
}
if (!res->dpy) {
PyErr_Format(PyExc_Exception, "(standalone) XOpenDisplay: cannot open display");
return NULL;
}
int nelements = 0;
res->fbc = res->m_glXChooseFBConfig(res->dpy, res->m_XDefaultScreen(res->dpy), 0, &nelements);
if (!res->fbc) {
res->m_XCloseDisplay(res->dpy);
PyErr_Format(PyExc_Exception, "(standalone) glXChooseFBConfig failed");
return NULL;
}
static int attribute_list[] = {
GLX_RGBA,
GLX_DOUBLEBUFFER,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_DEPTH_SIZE, 24,
None,
};
res->vi = res->m_glXChooseVisual(res->dpy, res->m_XDefaultScreen(res->dpy), attribute_list);
if (!res->vi) {
res->m_XCloseDisplay(res->dpy);
PyErr_Format(PyExc_Exception, "(standalone) glXChooseVisual: cannot choose visual");
return NULL;
}
XSetWindowAttributes swa;
swa.colormap = res->m_XCreateColormap(res->dpy, res->m_XRootWindow(res->dpy, res->vi->screen), res->vi->visual, AllocNone);
swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask;
res->wnd = res->m_XCreateWindow(
res->dpy, res->m_XRootWindow(res->dpy, res->vi->screen), 0, 0, 1, 1, 0, res->vi->depth, InputOutput,
res->vi->visual, CWBorderPixel | CWColormap | CWEventMask, &swa
);
if (!res->wnd) {
res->m_XCloseDisplay(res->dpy);
PyErr_Format(PyExc_Exception, "(standalone) XCreateWindow: cannot create window");
return NULL;
}
res->m_XSetErrorHandler(SilentXErrorHandler);
if (glversion) {
void (* proc)() = res->m_glXGetProcAddress((const unsigned char *)"glXCreateContextAttribsARB");
res->m_glXCreateContextAttribsARB = (m_glXCreateContextAttribsARBProc)proc;
if (!res->m_glXCreateContextAttribsARB) {
PyErr_Format(PyExc_Exception, "(standalone) glXCreateContextAttribsARB not found");
return NULL;
}
int attribs[] = {
GLX_CONTEXT_PROFILE_MASK, GLX_CONTEXT_CORE_PROFILE_BIT,
GLX_CONTEXT_MAJOR_VERSION, glversion / 100 % 10,
GLX_CONTEXT_MINOR_VERSION, glversion / 10 % 10,
0, 0,
};
res->ctx = res->m_glXCreateContextAttribsARB(res->dpy, *res->fbc, NULL, true, attribs);
} else {
res->ctx = res->m_glXCreateContext(res->dpy, res->vi, NULL, true);
}
if (!res->ctx) {
PyErr_Format(PyExc_Exception, "(standalone) cannot create context");
return NULL;
}
res->m_XSetErrorHandler(NULL);
if (!res->m_glXMakeCurrent(res->dpy, res->wnd, res->ctx)) {
PyErr_Format(PyExc_Exception, "(standalone) glXMakeCurrent failed");
return NULL;
}
return res;
}
PyErr_Format(PyExc_Exception, "unknown mode");
return NULL;
}
PyObject * GLContext_meth_load(GLContext * self, PyObject * arg) {
const char * method = PyUnicode_AsUTF8(arg);
void * proc = (void *)dlsym(self->libgl, method);
if (!proc) {
proc = (void *)self->m_glXGetProcAddress((const unsigned char *)method);
}
return PyLong_FromVoidPtr(proc);
}
PyObject * GLContext_meth_enter(GLContext * self) {
self->old_display = (void *)self->m_glXGetCurrentDisplay();
self->old_window = (void *)self->m_glXGetCurrentDrawable();
self->old_context = (void *)self->m_glXGetCurrentContext();
self->m_glXMakeCurrent(self->dpy, self->wnd, self->ctx);
Py_RETURN_NONE;
}
PyObject * GLContext_meth_exit(GLContext * self) {
self->m_glXMakeCurrent((Display *)self->old_display, (Window)self->old_window, (GLXContext)self->old_context);
Py_RETURN_NONE;
}
PyObject * GLContext_meth_release(GLContext * self) {
if (self->standalone) {
self->m_glXMakeCurrent(self->dpy, None, NULL);
self->m_glXDestroyContext(self->dpy, self->ctx);
}
if (self->own_window) {
self->m_XDestroyWindow(self->dpy, self->wnd);
self->m_XCloseDisplay(self->dpy);
}
if (self->fbc) {
self->m_XFree(self->fbc);
self->fbc = NULL;
}
if (self->vi) {
self->m_XFree(self->vi);
self->vi = NULL;
}
Py_RETURN_NONE;
}
void GLContext_dealloc(GLContext * self) {
Py_TYPE(self)->tp_free(self);
}
PyMethodDef GLContext_methods[] = {
{"load", (PyCFunction)GLContext_meth_load, METH_O, NULL},
{"load_opengl_function", (PyCFunction)GLContext_meth_load, METH_O, NULL},
{"release", (PyCFunction)GLContext_meth_release, METH_NOARGS, NULL},
{"__enter__", (PyCFunction)GLContext_meth_enter, METH_NOARGS, NULL},
{"__exit__", (PyCFunction)GLContext_meth_exit, METH_VARARGS, NULL},
{},
};
PyMemberDef GLContext_members[] = {
{"standalone", T_BOOL, offsetof(GLContext, standalone), READONLY, NULL},
{},
};
PyType_Slot GLContext_slots[] = {
{Py_tp_methods, GLContext_methods},
{Py_tp_members, GLContext_members},
{Py_tp_dealloc, (void *)GLContext_dealloc},
{},
};
PyType_Spec GLContext_spec = {"x11.GLContext", sizeof(GLContext), 0, Py_TPFLAGS_DEFAULT, GLContext_slots};
PyMethodDef module_methods[] = {
{"create_context", (PyCFunction)meth_create_context, METH_VARARGS | METH_KEYWORDS, NULL},
{},
};
PyModuleDef module_def = {PyModuleDef_HEAD_INIT, "x11", NULL, -1, module_methods};
extern "C" PyObject * PyInit_x11() {
PyObject * module = PyModule_Create(&module_def);
GLContext_type = (PyTypeObject *)PyType_FromSpec(&GLContext_spec);
PyModule_AddObject(module, "GLContext", (PyObject *)GLContext_type);
return module;
}
|