Debian Codegraph @

plyer/platforms/win/libs/win_api_defs.py

raw symbols SCIP
1 ''' Defines ctypes windows api.
2 '''
3
4 __all__ = ('GUID', 'get_DLLVERSIONINFO', 'MAKEDLLVERULL',
5 'get_NOTIFYICONDATAW', 'CreateWindowExW', 'WindowProc',
6 'DefWindowProcW', 'get_WNDCLASSEXW', 'GetModuleHandleW',
7 'RegisterClassExW', 'UpdateWindow', 'LoadImageW',
8 'Shell_NotifyIconW', 'DestroyIcon', 'UnregisterClassW',
9 'DestroyWindow', 'LoadIconW', 'get_PATH')
10
11 import ctypes
12 from ctypes import Structure, windll, sizeof, POINTER, WINFUNCTYPE
13 from ctypes.wintypes import (
14 DWORD, HICON, HWND, UINT, WCHAR, WORD, BYTE,
15 LPCWSTR, INT, LPVOID, HINSTANCE, HMENU, LPARAM,
16 WPARAM, HBRUSH, HMODULE, ATOM, BOOL, HANDLE
17 )
18 LRESULT = LPARAM
19 HRESULT = HANDLE
20 HCURSOR = HICON
21
22
23 class GUID(Structure):
24 _fields_ = [
25 ('Data1', DWORD),
26 ('Data2', WORD),
27 ('Data3', WORD),
28 ('Data4', BYTE * 8)
29 ]
30
31
32 class DLLVERSIONINFO(Structure):
33 _fields_ = [
34 ('cbSize', DWORD),
35 ('dwMajorVersion', DWORD),
36 ('dwMinorVersion', DWORD),
37 ('dwBuildNumber', DWORD),
38 ('dwPlatformID', DWORD),
39 ]
40
41
42 def get_DLLVERSIONINFO(*largs):
43 version_info = DLLVERSIONINFO(*largs)
44 version_info.cbSize = sizeof(DLLVERSIONINFO)
45 return version_info
46
47
48 def MAKEDLLVERULL(major, minor, build, sp):
49 return (major << 48) | (minor << 32) | (build << 16) | sp
50
51
52 NOTIFYICONDATAW_fields = [
53 ("cbSize", DWORD),
54 ("hWnd", HWND),
55 ("uID", UINT),
56 ("uFlags", UINT),
57 ("uCallbackMessage", UINT),
58 ("hIcon", HICON),
59 ("szTip", WCHAR * 128),
60 ("dwState", DWORD),
61 ("dwStateMask", DWORD),
62 ("szInfo", WCHAR * 256),
63 ("uVersion", UINT),
64 ("szInfoTitle", WCHAR * 64),
65 ("dwInfoFlags", DWORD),
66 ("guidItem", GUID),
67 ("hBalloonIcon", HICON),
68 ]
69
70
71 class NOTIFYICONDATAW(Structure):
72 _fields_ = NOTIFYICONDATAW_fields[:]
73
74
75 class NOTIFYICONDATAW_V3(Structure):
76 _fields_ = NOTIFYICONDATAW_fields[:-1]
77
78
79 class NOTIFYICONDATAW_V2(Structure):
80 _fields_ = NOTIFYICONDATAW_fields[:-2]
81
82
83 class NOTIFYICONDATAW_V1(Structure):
84 _fields_ = NOTIFYICONDATAW_fields[:6]
85
86
87 NOTIFYICONDATA_V3_SIZE = sizeof(NOTIFYICONDATAW_V3)
88 NOTIFYICONDATA_V2_SIZE = sizeof(NOTIFYICONDATAW_V2)
89 NOTIFYICONDATA_V1_SIZE = sizeof(NOTIFYICONDATAW_V1)
90
91
92 def get_NOTIFYICONDATAW(*largs):
93 notify_data = NOTIFYICONDATAW(*largs)
94
95 # get shell32 version to find correct NOTIFYICONDATAW size
96 DllGetVersion = windll.Shell32.DllGetVersion
97 DllGetVersion.argtypes = [POINTER(DLLVERSIONINFO)]
98 DllGetVersion.restype = HRESULT
99
100 version = get_DLLVERSIONINFO()
101 if DllGetVersion(version):
102 raise Exception('Cannot get Windows version numbers.')
103 v = MAKEDLLVERULL(version.dwMajorVersion, version.dwMinorVersion,
104 version.dwBuildNumber, version.dwPlatformID)
105
106 # from the version info find the NOTIFYICONDATA size
107 if v >= MAKEDLLVERULL(6, 0, 6, 0):
108 notify_data.cbSize = sizeof(NOTIFYICONDATAW)
109 elif v >= MAKEDLLVERULL(6, 0, 0, 0):
110 notify_data.cbSize = NOTIFYICONDATA_V3_SIZE
111 elif v >= MAKEDLLVERULL(5, 0, 0, 0):
112 notify_data.cbSize = NOTIFYICONDATA_V2_SIZE
113 else:
114 notify_data.cbSize = NOTIFYICONDATA_V1_SIZE
115 return notify_data
116
117
118 CreateWindowExW = windll.User32.CreateWindowExW
119 CreateWindowExW.argtypes = [DWORD, ATOM, LPCWSTR, DWORD, INT, INT, INT, INT,
120 HWND, HMENU, HINSTANCE, LPVOID]
121 CreateWindowExW.restype = HWND
122
123 GetModuleHandleW = windll.Kernel32.GetModuleHandleW
124 GetModuleHandleW.argtypes = [LPCWSTR]
125 GetModuleHandleW.restype = HMODULE
126
127 WindowProc = WINFUNCTYPE(LRESULT, HWND, UINT, WPARAM, LPARAM)
128 DefWindowProcW = windll.User32.DefWindowProcW
129 DefWindowProcW.argtypes = [HWND, UINT, WPARAM, LPARAM]
130 DefWindowProcW.restype = LRESULT
131
132
133 class WNDCLASSEXW(Structure):
134 _fields_ = [
135 ('cbSize', UINT),
136 ('style', UINT),
137 ('lpfnWndProc', WindowProc),
138 ('cbClsExtra', INT),
139 ('cbWndExtra', INT),
140 ('hInstance', HINSTANCE),
141 ('hIcon', HICON),
142 ('hCursor', HCURSOR),
143 ('hbrBackground', HBRUSH),
144 ('lpszMenuName', LPCWSTR),
145 ('lpszClassName', LPCWSTR),
146 ('hIconSm', HICON),
147 ]
148
149
150 def get_WNDCLASSEXW(*largs):
151 wnd_class = WNDCLASSEXW(*largs)
152 wnd_class.cbSize = sizeof(WNDCLASSEXW)
153 return wnd_class
154
155
156 RegisterClassExW = windll.User32.RegisterClassExW
157 RegisterClassExW.argtypes = [POINTER(WNDCLASSEXW)]
158 RegisterClassExW.restype = ATOM
159
160 UpdateWindow = windll.User32.UpdateWindow
161 UpdateWindow.argtypes = [HWND]
162 UpdateWindow.restype = BOOL
163
164 LoadImageW = windll.User32.LoadImageW
165 LoadImageW.argtypes = [HINSTANCE, LPCWSTR, UINT, INT, INT, UINT]
166 LoadImageW.restype = HANDLE
167
168 Shell_NotifyIconW = windll.Shell32.Shell_NotifyIconW
169 Shell_NotifyIconW.argtypes = [DWORD, POINTER(NOTIFYICONDATAW)]
170 Shell_NotifyIconW.restype = BOOL
171
172 DestroyIcon = windll.User32.DestroyIcon
173 DestroyIcon.argtypes = [HICON]
174 DestroyIcon.restype = BOOL
175
176 UnregisterClassW = windll.User32.UnregisterClassW
177 UnregisterClassW.argtypes = [ATOM, HINSTANCE]
178 UnregisterClassW.restype = BOOL
179
180 DestroyWindow = windll.User32.DestroyWindow
181 DestroyWindow.argtypes = [HWND]
182 DestroyWindow.restype = BOOL
183
184 LoadIconW = windll.User32.LoadIconW
185 LoadIconW.argtypes = [HINSTANCE, LPCWSTR]
186 LoadIconW.restype = HICON
187
188
189 class SYSTEM_POWER_STATUS(Structure):
190 _fields_ = [
191 ('ACLineStatus', BYTE),
192 ('BatteryFlag', BYTE),
193 ('BatteryLifePercent', BYTE),
194 ('Reserved1', BYTE),
195 ('BatteryLifeTime', DWORD),
196 ('BatteryFullLifeTime', DWORD),
197 ]
198
199
200 SystemPowerStatusP = POINTER(SYSTEM_POWER_STATUS)
201
202 GetSystemPowerStatus = windll.kernel32.GetSystemPowerStatus
203 GetSystemPowerStatus.argtypes = [SystemPowerStatusP]
204 GetSystemPowerStatus.restype = BOOL
205
206
207 class GUID_(Structure):
208 _fields_ = [
209 ('Data1', DWORD),
210 ('Data2', WORD),
211 ('Data3', WORD),
212 ('Data4', BYTE * 8)
213 ]
214
215 def __init__(self, uuid_):
216 Structure.__init__(self)
217 self.Data1, self.Data2, self.Data3, self.Data4[0], self.Data4[1], rest\
218 = uuid_.fields
219 for i in range(2, 8):
220 self.Data4[i] = rest >> (8 - i - 1) * 8 & 0xff
221
222
223 _CoTaskMemFree = windll.ole32.CoTaskMemFree
224 _CoTaskMemFree.restype = None
225 _CoTaskMemFree.argtypes = [ctypes.c_void_p]
226
227 _SHGetKnownFolderPath = windll.shell32.SHGetKnownFolderPath
228 _SHGetKnownFolderPath.argtypes = [
229 POINTER(GUID_),
230 DWORD,
231 HANDLE,
232 POINTER(ctypes.c_wchar_p)
233 ]
234
235
236 class PathNotFoundException(Exception):
237 pass
238
239
240 def get_PATH(folderid):
241 fid = GUID_(folderid)
242 pPath = ctypes.c_wchar_p()
243 S_OK = 0
244 Result = _SHGetKnownFolderPath(ctypes.byref(fid),
245 0, None, ctypes.byref(pPath))
246 if Result != S_OK:
247 raise PathNotFoundException()
248 path = pPath.value
249 _CoTaskMemFree(pPath)
250 return path