forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_c_internal_utils.c
More file actions
162 lines (155 loc) · 4.93 KB
/
_c_internal_utils.c
File metadata and controls
162 lines (155 loc) · 4.93 KB
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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#ifdef __linux__
#include <dlfcn.h>
#endif
#ifdef _WIN32
#include <Objbase.h>
#include <Shobjidl.h>
#include <Windows.h>
#endif
static PyObject*
mpl_display_is_valid(PyObject* module)
{
#ifdef __linux__
void* libX11;
// The getenv check is redundant but helps performance as it is much faster
// than dlopen().
if (getenv("DISPLAY")
&& (libX11 = dlopen("libX11.so.6", RTLD_LAZY))) {
struct Display* display = NULL;
struct Display* (* XOpenDisplay)(char const*) =
dlsym(libX11, "XOpenDisplay");
int (* XCloseDisplay)(struct Display*) =
dlsym(libX11, "XCloseDisplay");
if (XOpenDisplay && XCloseDisplay
&& (display = XOpenDisplay(NULL))) {
XCloseDisplay(display);
}
if (dlclose(libX11)) {
PyErr_SetString(PyExc_RuntimeError, dlerror());
return NULL;
}
if (display) {
Py_RETURN_TRUE;
}
}
void* libwayland_client;
if (getenv("WAYLAND_DISPLAY")
&& (libwayland_client = dlopen("libwayland-client.so.0", RTLD_LAZY))) {
struct wl_display* display = NULL;
struct wl_display* (* wl_display_connect)(char const*) =
dlsym(libwayland_client, "wl_display_connect");
void (* wl_display_disconnect)(struct wl_display*) =
dlsym(libwayland_client, "wl_display_disconnect");
if (wl_display_connect && wl_display_disconnect
&& (display = wl_display_connect(NULL))) {
wl_display_disconnect(display);
}
if (dlclose(libwayland_client)) {
PyErr_SetString(PyExc_RuntimeError, dlerror());
return NULL;
}
if (display) {
Py_RETURN_TRUE;
}
}
Py_RETURN_FALSE;
#else
Py_RETURN_TRUE;
#endif
}
static PyObject*
mpl_GetCurrentProcessExplicitAppUserModelID(PyObject* module)
{
#ifdef _WIN32
wchar_t* appid = NULL;
HRESULT hr = GetCurrentProcessExplicitAppUserModelID(&appid);
if (FAILED(hr)) {
return PyErr_SetFromWindowsErr(hr);
}
PyObject* py_appid = PyUnicode_FromWideChar(appid, -1);
CoTaskMemFree(appid);
return py_appid;
#else
Py_RETURN_NONE;
#endif
}
static PyObject*
mpl_SetCurrentProcessExplicitAppUserModelID(PyObject* module, PyObject* arg)
{
#ifdef _WIN32
wchar_t* appid = PyUnicode_AsWideCharString(arg, NULL);
if (!appid) {
return NULL;
}
HRESULT hr = SetCurrentProcessExplicitAppUserModelID(appid);
PyMem_Free(appid);
if (FAILED(hr)) {
return PyErr_SetFromWindowsErr(hr);
}
Py_RETURN_NONE;
#else
Py_RETURN_NONE;
#endif
}
static PyObject*
mpl_GetForegroundWindow(PyObject* module)
{
#ifdef _WIN32
return PyLong_FromVoidPtr(GetForegroundWindow());
#else
Py_RETURN_NONE;
#endif
}
static PyObject*
mpl_SetForegroundWindow(PyObject* module, PyObject *arg)
{
#ifdef _WIN32
HWND handle = PyLong_AsVoidPtr(arg);
if (PyErr_Occurred()) {
return NULL;
}
if (!SetForegroundWindow(handle)) {
return PyErr_Format(PyExc_RuntimeError, "Error setting window");
}
Py_RETURN_NONE;
#else
Py_RETURN_NONE;
#endif
}
static PyMethodDef functions[] = {
{"display_is_valid", (PyCFunction)mpl_display_is_valid, METH_NOARGS,
"display_is_valid()\n--\n\n"
"Check whether the current X11 or Wayland display is valid.\n\n"
"On Linux, returns True if either $DISPLAY is set and XOpenDisplay(NULL)\n"
"succeeds, or $WAYLAND_DISPLAY is set and wl_display_connect(NULL)\n"
"succeeds. On other platforms, always returns True."},
{"Win32_GetCurrentProcessExplicitAppUserModelID",
(PyCFunction)mpl_GetCurrentProcessExplicitAppUserModelID, METH_NOARGS,
"Win32_GetCurrentProcessExplicitAppUserModelID()\n--\n\n"
"Wrapper for Windows's GetCurrentProcessExplicitAppUserModelID. On \n"
"non-Windows platforms, always returns None."},
{"Win32_SetCurrentProcessExplicitAppUserModelID",
(PyCFunction)mpl_SetCurrentProcessExplicitAppUserModelID, METH_O,
"Win32_SetCurrentProcessExplicitAppUserModelID(appid, /)\n--\n\n"
"Wrapper for Windows's SetCurrentProcessExplicitAppUserModelID. On \n"
"non-Windows platforms, a no-op."},
{"Win32_GetForegroundWindow",
(PyCFunction)mpl_GetForegroundWindow, METH_NOARGS,
"Win32_GetForegroundWindow()\n--\n\n"
"Wrapper for Windows' GetForegroundWindow. On non-Windows platforms, \n"
"always returns None."},
{"Win32_SetForegroundWindow",
(PyCFunction)mpl_SetForegroundWindow, METH_O,
"Win32_SetForegroundWindow(hwnd, /)\n--\n\n"
"Wrapper for Windows' SetForegroundWindow. On non-Windows platforms, \n"
"a no-op."},
{NULL, NULL}}; // sentinel.
static PyModuleDef util_module = {
PyModuleDef_HEAD_INIT, "_c_internal_utils", "", 0, functions, NULL, NULL, NULL, NULL};
#pragma GCC visibility push(default)
PyMODINIT_FUNC PyInit__c_internal_utils(void)
{
return PyModule_Create(&util_module);
}