Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions glcontext/egl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Python.h>
#include <structmember.h>

#include <stdlib.h>
#include <dlfcn.h>

struct Display;
Expand Down Expand Up @@ -90,12 +91,25 @@ struct GLContext {

PyTypeObject * GLContext_type;

static void *try_dlopen(const char **names) {
void *lib;

for (const char **name = names; *name != NULL; name++) {
if ((lib = dlopen(*name, RTLD_LAZY)) != NULL) {
return lib;
}
}
return NULL;
}

GLContext * meth_create_context(PyObject * self, PyObject * args, PyObject * kwargs) {
static char * keywords[] = {"mode", "libgl", "libegl", "glversion", "device_index", NULL};
static const char * keywords[] = {"mode", "libgl", "libegl", "glversion", "device_index", NULL};

const char * mode = "standalone";
const char * libgl = "libGL.so";
const char * libegl = "libEGL.so";
const char * libgl = NULL;
const char * libgl_names[] = {"libGL.so", "libGL.so.1", NULL};
const char * libegl = NULL;
const char * libegl_names[] = {"libEGL.so", "libEGL.so.1", NULL};
int glversion = 330;
int device_index = 0;

Expand All @@ -105,16 +119,32 @@ GLContext * meth_create_context(PyObject * self, PyObject * args, PyObject * kwa

GLContext * res = PyObject_New(GLContext, GLContext_type);

res->libgl = dlopen(libgl, RTLD_LAZY);
if (!res->libgl) {
PyErr_Format(PyExc_Exception, "%s not loaded", libgl);
return NULL;
if (libgl) {
res->libgl = dlopen(libgl, RTLD_LAZY);
if (!res->libgl) {
PyErr_Format(PyExc_Exception, "%s not found by dynamic linker. Check with 'ldconfig -v'.", libgl);
return NULL;
}
} else {
res->libgl = try_dlopen(libgl_names);
if (!res->libgl) {
PyErr_Format(PyExc_Exception, "%s not found by dynamic linker. Check with 'ldconfig -v'.", libgl_names[0]);
return NULL;
}
}

res->libegl = dlopen(libegl, RTLD_LAZY);
if (!res->libegl) {
PyErr_Format(PyExc_Exception, "%s not loaded", libegl);
return NULL;
if (libegl) {
res->libegl = dlopen(libegl, RTLD_LAZY);
if (!res->libegl) {
PyErr_Format(PyExc_Exception, "%s not found by dynamic linker. Check with 'ldconfig -v'.", libegl);
return NULL;
}
} else {
res->libegl = try_dlopen(libegl_names);
if (!res->libegl) {
PyErr_Format(PyExc_Exception, "%s not found by dynamic linker. Check with 'ldconfig -v'.", libegl_names[0]);
return NULL;
}
}

res->m_eglGetError = (m_eglGetErrorProc)dlsym(res->libegl, "eglGetError");
Expand Down
52 changes: 41 additions & 11 deletions glcontext/x11.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Python.h>
#include <structmember.h>

#include <stdlib.h>
#include <dlfcn.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
Expand Down Expand Up @@ -87,12 +88,25 @@ struct GLContext {

PyTypeObject * GLContext_type;

static void *try_dlopen(const char **names) {
void *lib;

for (const char **name = names; *name != NULL; name++) {
if ((lib = dlopen(*name, RTLD_LAZY)) != NULL) {
return lib;
}
}
return NULL;
}

GLContext * meth_create_context(PyObject * self, PyObject * args, PyObject * kwargs) {
static char * keywords[] = {"mode", "libgl", "libx11", "glversion", NULL};
static const char * keywords[] = {"mode", "libgl", "libx11", "glversion", NULL};

const char * mode = "detect";
const char * libgl = "libGL.so";
const char * libx11 = "libX11.so";
const char * libgl = NULL;
const char * libgl_names[] = {"libGL.so", "libGL.so.1", NULL};
const char * libx11 = NULL;
const char * libx11_names[] = {"libX11.so", "libX11.so.6", NULL};
int glversion = 330;

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|sssi", keywords, &mode, &libgl, &libx11, &glversion)) {
Expand All @@ -101,10 +115,18 @@ GLContext * meth_create_context(PyObject * self, PyObject * args, PyObject * kwa

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;
if (libgl) {
res->libgl = dlopen(libgl, RTLD_LAZY);
if (!res->libgl) {
PyErr_Format(PyExc_Exception, "%s not found by dynamic linker. Check with 'ldconfig -v'.", libgl);
return NULL;
}
} else {
res->libgl = try_dlopen(libgl_names);
if (!res->libgl) {
PyErr_Format(PyExc_Exception, "%s not found by dynamic linker. Check with 'ldconfig -v'.", libgl_names[0]);
return NULL;
}
}

res->m_glXChooseFBConfig = (m_glXChooseFBConfigProc)dlsym(res->libgl, "glXChooseFBConfig");
Expand Down Expand Up @@ -162,10 +184,18 @@ GLContext * meth_create_context(PyObject * self, PyObject * args, PyObject * kwa
}

if (strcmp(mode, "detect")) {
res->libx11 = dlopen(libx11, RTLD_LAZY);
if (!res->libx11) {
PyErr_Format(PyExc_Exception, "(detect) %s not loaded", libx11);
return NULL;
if (libx11) {
res->libx11 = dlopen(libx11, RTLD_LAZY);
if (!res->libx11) {
PyErr_Format(PyExc_Exception, "(detect) %s not found by dynamic linker. Check with 'ldconfig -v'.", libx11);
return NULL;
}
} else {
res->libx11 = try_dlopen(libx11_names);
if (!res->libx11) {
PyErr_Format(PyExc_Exception, "(detect) %s not found by dynamic linker. Check with 'ldconfig -v'.", libx11_names[0]);
return NULL;
}
}

res->m_XOpenDisplay = (m_XOpenDisplayProc)dlsym(res->libx11, "XOpenDisplay");
Expand Down
Loading