|
1 | 1 | #define PY_SSIZE_T_CLEAN |
2 | 2 | #include <Python.h> |
| 3 | +#ifdef __linux__ |
| 4 | +#include <dlfcn.h> |
| 5 | +#endif |
3 | 6 | #ifdef _WIN32 |
4 | 7 | #include <Objbase.h> |
5 | 8 | #include <Shobjidl.h> |
6 | 9 | #include <Windows.h> |
7 | 10 | #endif |
8 | 11 |
|
| 12 | +static PyObject* mpl_display_is_invalid(PyObject* module) |
| 13 | +{ |
| 14 | +#ifdef __linux__ |
| 15 | + void* libX11; |
| 16 | + typedef struct Display Display; |
| 17 | + Display* (* XOpenDisplay)(char* display_name); |
| 18 | + int (* XCloseDisplay)(Display* display); |
| 19 | + if (!(libX11 = dlopen("libX11.so", RTLD_LAZY)) || |
| 20 | + !(XOpenDisplay = dlsym(libX11, "XOpenDisplay")) || |
| 21 | + !(XCloseDisplay = dlsym(libX11, "XCloseDisplay"))) { |
| 22 | + Py_RETURN_FALSE; |
| 23 | + } |
| 24 | + Display* display = XOpenDisplay(NULL); |
| 25 | + if (display) { |
| 26 | + XCloseDisplay(display); |
| 27 | + } |
| 28 | + if (dlclose(libX11)) { |
| 29 | + PyErr_SetString(PyExc_RuntimeError, dlerror()); |
| 30 | + return NULL; |
| 31 | + } |
| 32 | + if (display) { |
| 33 | + Py_RETURN_FALSE; |
| 34 | + } else { |
| 35 | + Py_RETURN_TRUE; |
| 36 | + } |
| 37 | +#else |
| 38 | + Py_RETURN_FALSE; |
| 39 | +#endif |
| 40 | +} |
| 41 | + |
9 | 42 | static PyObject* mpl_GetCurrentProcessExplicitAppUserModelID(PyObject* module) |
10 | 43 | { |
11 | 44 | #ifdef _WIN32 |
@@ -66,6 +99,11 @@ static PyObject* mpl_SetForegroundWindow(PyObject* module, PyObject *arg) |
66 | 99 | } |
67 | 100 |
|
68 | 101 | static PyMethodDef functions[] = { |
| 102 | + {"display_is_invalid", (PyCFunction)mpl_display_is_invalid, METH_NOARGS, |
| 103 | + "display_is_invalid()\n--\n\n" |
| 104 | + "Attempt to check whether the current X11 display is invalid.\n\n" |
| 105 | + "Returns True if running on Linux and libX11 can be loaded and \n" |
| 106 | + "XOpenDisplay(NULL) returns NULL, False otherwise."}, |
69 | 107 | {"Win32_GetCurrentProcessExplicitAppUserModelID", |
70 | 108 | (PyCFunction)mpl_GetCurrentProcessExplicitAppUserModelID, METH_NOARGS, |
71 | 109 | "Win32_GetCurrentProcessExplicitAppUserModelID()\n--\n\n" |
|
0 commit comments