4
4
#define WINVER 0x0A00
5
5
#define _WIN32_WINNT 0x0A00
6
6
#endif
7
- #define PY_SSIZE_T_CLEAN
8
- #include < Python.h>
7
+ #include < pybind11/pybind11.h>
9
8
#ifdef __linux__
10
9
#include < dlfcn.h>
11
10
#endif
15
14
#include < Windows.h>
16
15
#endif
17
16
18
- static PyObject*
<
10000
/td>19
- mpl_display_is_valid (PyObject* module)
17
+ namespace py = pybind11;
18
+
19
+ static bool
20
+ mpl_display_is_valid (void )
20
21
{
21
22
#ifdef __linux__
22
23
void * libX11;
@@ -34,11 +35,10 @@ mpl_display_is_valid(PyObject* module)
34
35
XCloseDisplay (display);
35
36
}
36
37
if (dlclose (libX11)) {
37
- PyErr_SetString (PyExc_RuntimeError, dlerror ());
38
- return NULL ;
38
+ throw std::runtime_error (dlerror ());
39
39
}
40
40
if (display) {
41
- Py_RETURN_TRUE ;
41
+ return true ;
42
42
}
43
43
}
44
44
void * libwayland_client;
@@ -56,84 +56,74 @@ mpl_display_is_valid(PyObject* module)
56
56
wl_display_disconnect (display);
57
57
}
58
58
if (dlclose (libwayland_client)) {
59
- PyErr_SetString (PyExc_RuntimeError, dlerror ());
60
- return NULL ;
59
+ throw std::runtime_error (dlerror ());
61
60
}
62
61
if (display) {
63
- Py_RETURN_TRUE ;
62
+ return true ;
64
63
}
65
64
}
66
- Py_RETURN_FALSE ;
65
+ return false ;
67
66
#else
68
- Py_RETURN_TRUE ;
67
+ return true ;
69
68
#endif
70
69
}
71
70
72
- static PyObject*
73
- mpl_GetCurrentProcessExplicitAppUserModelID (PyObject* module )
71
+ static py::object
72
+ mpl_GetCurrentProcessExplicitAppUserModelID (void )
74
73
{
75
74
#ifdef _WIN32
76
75
wchar_t * appid = NULL ;
77
76
HRESULT hr = GetCurrentProcessExplicitAppUserModelID (&appid);
78
77
if (FAILED (hr)) {
79
- return PyErr_SetFromWindowsErr (hr);
78
+ PyErr_SetFromWindowsErr (hr);
79
+ throw py::error_already_set ();
80
80
}
81
- PyObject* py_appid = PyUnicode_FromWideChar (appid, - 1 );
81
+ auto py_appid = py::cast (appid);
82
82
CoTaskMemFree (appid);
83
83
return py_appid;
84
84
#else
85
- Py_RETURN_NONE ;
85
+ return py::none () ;
86
86
#endif
87
87
}
88
88
89
- static PyObject*
90
- mpl_SetCurrentProcessExplicitAppUserModelID (PyObject* module, PyObject* arg )
89
+ static void
90
+ mpl_SetCurrentProcessExplicitAppUserModelID (wchar_t *appid )
91
91
{
92
92
#ifdef _WIN32
93
- wchar_t * appid = PyUnicode_AsWideCharString (arg, NULL );
94
- if (!appid) {
95
- return NULL ;
96
- }
97
93
HRESULT hr = SetCurrentProcessExplicitAppUserModelID (appid);
98
- PyMem_Free (appid);
99
94
if (FAILED (hr)) {
100
- return PyErr_SetFromWindowsErr (hr);
95
+ PyErr_SetFromWindowsErr (hr);
96
+ throw py::error_already_set ();
101
97
}
102
- Py_RETURN_NONE;
103
- #else
104
- Py_RETURN_NONE;
105
98
#endif
106
99
}
107
100
108
- static PyObject*
109
- mpl_GetForegroundWindow (PyObject* module )
101
+ static py::object
102
+ mpl_GetForegroundWindow (void )
110
103
{
111
104
#ifdef _WIN32
112
- return PyLong_FromVoidPtr (GetForegroundWindow ());
105
+ return py::capsule (GetForegroundWindow (), " HWND " );
113
106
#else
114
- Py_RETURN_NONE ;
107
+ return py::none () ;
115
108
#endif
116
109
}
117
110
118
- static PyObject*
119
- mpl_SetForegroundWindow (PyObject* module, PyObject *arg )
111
+ static void
112
+ mpl_SetForegroundWindow (py::capsule handle_p )
120
113
{
121
114
#ifdef _WIN32
122
- HWND handle = PyLong_AsVoidPtr (arg);
123
- if (PyErr_Occurred ()) {
124
- return NULL ;
125
- }
126
- if (!SetForegroundWindow (handle)) {
127
- return PyErr_Format (PyExc_RuntimeError, " Error setting window" );
128
10000
- }
129
- Py_RETURN_NONE;
130
- #else
131
- Py_RETURN_NONE;
115
+ if (handle_p.name () != " HWND" ) {
116
+ throw std::runtime_error (" Handle must be a value returned from Win32_GetForegroundWindow" );
117
+ }
118
+ HWND handle = static_cast <HWND>(handle_p.get_pointer ());
119
+ if (!SetForegroundWindow (handle)) {
120
+ throw std::runtime_error (" Error setting window" );
121
+ }
132
122
#endif
133
123
}
134
124
135
- static PyObject*
136
- mpl_SetProcessDpiAwareness_max (PyObject* module )
125
+ static void
126
+ mpl_SetProcessDpiAwareness_max (void )
137
127
{
138
128
#ifdef _WIN32
139
129
#ifdef _DPI_AWARENESS_CONTEXTS_
@@ -171,49 +161,52 @@ mpl_SetProcessDpiAwareness_max(PyObject* module)
171
161
SetProcessDPIAware ();
172
162
#endif
173
163
#endif
174
- Py_RETURN_NONE;
175
164
}
176
165
177
- static PyMethodDef functions[] = {
178
- {" display_is_valid" , (PyCFunction)mpl_display_is_valid, METH_NOARGS,
179
- " display_is_valid()\n --\n\n "
180
- " Check whether the current X11 or Wayland display is valid.\n\n "
181
- " On Linux, returns True if either $DISPLAY is set and XOpenDisplay(NULL)\n "
182
- " succeeds, or $WAYLAND_DISPLAY is set and wl_display_connect(NULL)\n "
183
- " succeeds.\n\n "
184
- " On other platforms, always returns True." },
185
- {" Win32_GetCurrentProcessExplicitAppUserModelID" ,
186
-
10000
(PyCFunction)mpl_GetCurrentProcessExplicitAppUserModelID, METH_NOARGS,
187
- " Win32_GetCurrentProcessExplicitAppUserModelID()\n --\n\n "
188
- " Wrapper for Windows's GetCurrentProcessExplicitAppUserModelID.\n\n "
189
- " On non-Windows platforms, always returns None." },
190
- {" Win32_SetCurrentProcessExplicitAppUserModelID" ,
191
- (PyCFunction)mpl_SetCurrentProcessExplicitAppUserModelID, METH_O,
192
- " Win32_SetCurrentProcessExplicitAppUserModelID(appid, /)\n --\n\n "
193
- " Wrapper for Windows's SetCurrentProcessExplicitAppUserModelID.\n\n "
194
- " On non-Windows platforms, does nothing." },
195
- {" Win32_GetForegroundWindow" ,
196
- (PyCFunction)mpl_GetForegroundWindow, METH_NOARGS,
197
- " Win32_GetForegroundWindow()\n --\n\n "
198
- " Wrapper for Windows' GetForegroundWindow.\n\n "
199
- " On non-Windows platforms, always returns None." },
200
- {" Win32_SetForegroundWindow" ,
201
- (PyCFunction)mpl_SetForegroundWindow, METH_O,
202
- " Win32_SetForegroundWindow(hwnd, /)\n --\n\n "
203
- " Wrapper for Windows' SetForegroundWindow.\n\n "
204
- " On non-Windows platforms, does nothing." },
205
- {" Win32_SetProcessDpiAwareness_max" ,
206
- (PyCFunction)mpl_SetProcessDpiAwareness_max, METH_NOARGS,
207
- " Win32_SetProcessDpiAwareness_max()\n --\n\n "
208
- " Set Windows' process DPI awareness to best option available.\n\n "
209
- " On non-Windows platforms, does nothing." },
210
- {NULL , NULL }}; // sentinel.
211
- static PyModuleDef util_module = {
212
- PyModuleDef_HEAD_INIT, " _c_internal_utils" , NULL , 0 , functions
213
- };
214
-
215
- #pragma GCC visibility push(default)
216
- PyMODINIT_FUNC PyInit__c_internal_utils (void )
166
+ PYBIND11_MODULE (_c_internal_utils, m)
217
167
{
218
- return PyModule_Create (&util_module);
168
+ m.def (
169
+ " display_is_valid" , &mpl_display_is_valid,
170
+ R"""( --
171
+ Check whether the current X11 or Wayland display is valid.
172
+
173
+ On Linux, returns True if either $DISPLAY is set and XOpenDisplay(NULL)
174
+ succeeds, or $WAYLAND_DISPLAY is set and wl_display_connect(NULL)
175
+ succeeds.
176
+
177
+ On other platforms, always returns True.)""" );
178
+ m.def (
179
+ " Win32_GetCurrentProcessExplicitAppUserModelID" ,
180
+ &mpl_GetCurrentProcessExplicitAppUserModelID,
181
+ R"""( --
182
+ Wrapper for Windows's GetCurrentProcessExplicitAppUserModelID.
183
+
184
+ On non-Windows platforms, always returns None.)""" );
185
+ m.def (
186
+ " Win32_SetCurrentProcessExplicitAppUserModelID" ,
187
+ &mp
93C6
l_SetCurrentProcessExplicitAppUserModelID,
188
+ py::arg (" appid" ), py::pos_only (),
189
+ R"""( --
190
+ Wrapper for Windows's SetCurrentProcessExplicitAppUserModelID.
191
+
192
+ On non-Windows platforms, does nothing.)""" );
193
+ m.def (
194
+ " Win32_GetForegroundWindow" , &mpl_GetForegroundWindow,
195
+ R"""( --
196
+ Wrapper for Windows' GetForegroundWindow.
197
+
198
+ On non-Windows platforms, always returns None.)""" );
199
+ m.def (
200
+ " Win32_SetForegroundWindow" , &mpl_SetForegroundWindow,
201
+ py::arg (" hwnd" ),
202
+ R"""( --
203
+ Wrapper for Windows' SetForegroundWindow.
204
+
205
+ On non-Windows platforms, does nothing.)""" );
206
+ m.def (
207
+ " Win32_SetProcessDpiAwareness_max" , &mpl_SetProcessDpiAwareness_max,
208
+ R"""( --
209
+ Set Windows' process DPI awareness to best option available.
210
+
211
+ On non-Windows platforms, does nothing.)""" );
219
212
}
0 commit comments