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
97 lines (91 loc) · 2.83 KB
/
Copy path_c_internal_utils.c
File metadata and controls
97 lines (91 loc) · 2.83 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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#ifdef _WIN32
#include <Objbase.h>
#include <Shobjidl.h>
#include <Windows.h>
#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[] = {
{"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);
}