summaryrefslogtreecommitdiff
path: root/external/plyer/platforms/win/libs/win_api_defs.py
blob: 7aed430a8195810c5e7f84bbc8ff418bce75ae27 (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
''' Defines ctypes windows api.
'''

__all__ = ('GUID', 'get_DLLVERSIONINFO', 'MAKEDLLVERULL',
           'get_NOTIFYICONDATAW', 'CreateWindowExW', 'WindowProc',
           'DefWindowProcW', 'get_WNDCLASSEXW', 'GetModuleHandleW',
           'RegisterClassExW', 'UpdateWindow', 'LoadImageW',
           'Shell_NotifyIconW', 'DestroyIcon', 'UnregisterClassW',
           'DestroyWindow', 'LoadIconW')

import ctypes
from ctypes import Structure, windll, sizeof, POINTER, WINFUNCTYPE
from ctypes.wintypes import (DWORD, HICON, HWND, UINT, WCHAR, WORD, BYTE,
    LPCWSTR, INT, LPVOID, HINSTANCE, HMENU, LPARAM, WPARAM,
    HBRUSH, HMODULE, ATOM, BOOL, HANDLE)
LRESULT = LPARAM
HRESULT = HANDLE
HCURSOR = HICON


class GUID(Structure):
    _fields_ = [
        ('Data1', DWORD),
        ('Data2', WORD),
        ('Data3', WORD),
        ('Data4', BYTE * 8)
    ]


class DLLVERSIONINFO(Structure):
    _fields_ = [
        ('cbSize', DWORD),
        ('dwMajorVersion', DWORD),
        ('dwMinorVersion', DWORD),
        ('dwBuildNumber', DWORD),
        ('dwPlatformID', DWORD),
    ]


def get_DLLVERSIONINFO(*largs):
    version_info = DLLVERSIONINFO(*largs)
    version_info.cbSize = sizeof(DLLVERSIONINFO)
    return version_info


def MAKEDLLVERULL(major, minor, build, sp):
    return (major << 48) | (minor << 32) | (build << 16) | sp


NOTIFYICONDATAW_fields = [
    ("cbSize", DWORD),
    ("hWnd", HWND),
    ("uID", UINT),
    ("uFlags", UINT),
    ("uCallbackMessage", UINT),
    ("hIcon", HICON),
    ("szTip", WCHAR * 128),
    ("dwState", DWORD),
    ("dwStateMask", DWORD),
    ("szInfo", WCHAR * 256),
    ("uVersion", UINT),
    ("szInfoTitle", WCHAR * 64),
    ("dwInfoFlags", DWORD),
    ("guidItem", GUID),
    ("hBalloonIcon", HICON),
]


class NOTIFYICONDATAW(Structure):
    _fields_ = NOTIFYICONDATAW_fields[:]


class NOTIFYICONDATAW_V3(Structure):
    _fields_ = NOTIFYICONDATAW_fields[:-1]


class NOTIFYICONDATAW_V2(Structure):
    _fields_ = NOTIFYICONDATAW_fields[:-2]


class NOTIFYICONDATAW_V1(Structure):
    _fields_ = NOTIFYICONDATAW_fields[:6]


NOTIFYICONDATA_V3_SIZE = sizeof(NOTIFYICONDATAW_V3)
NOTIFYICONDATA_V2_SIZE = sizeof(NOTIFYICONDATAW_V2)
NOTIFYICONDATA_V1_SIZE = sizeof(NOTIFYICONDATAW_V1)


def get_NOTIFYICONDATAW(*largs):
    notify_data = NOTIFYICONDATAW(*largs)

    # get shell32 version to find correct NOTIFYICONDATAW size
    DllGetVersion = windll.Shell32.DllGetVersion
    DllGetVersion.argtypes = [POINTER(DLLVERSIONINFO)]
    DllGetVersion.restype = HRESULT

    version = get_DLLVERSIONINFO()
    if DllGetVersion(version):
        raise Exception('Cannot get Windows version numbers.')
    v = MAKEDLLVERULL(version.dwMajorVersion, version.dwMinorVersion,
                      version.dwBuildNumber, version.dwPlatformID)

    # from the version info find the NOTIFYICONDATA size
    if v >= MAKEDLLVERULL(6, 0, 6, 0):
        notify_data.cbSize = sizeof(NOTIFYICONDATAW)
    elif v >= MAKEDLLVERULL(6, 0, 0, 0):
        notify_data.cbSize = NOTIFYICONDATA_V3_SIZE
    elif v >= MAKEDLLVERULL(5, 0, 0, 0):
        notify_data.cbSize = NOTIFYICONDATA_V2_SIZE
    else:
        notify_data.cbSize = NOTIFYICONDATA_V1_SIZE
    return notify_data


CreateWindowExW = windll.User32.CreateWindowExW
CreateWindowExW.argtypes = [DWORD, ATOM, LPCWSTR, DWORD, INT, INT, INT, INT,
                            HWND, HMENU, HINSTANCE, LPVOID]
CreateWindowExW.restype = HWND

GetModuleHandleW = windll.Kernel32.GetModuleHandleW
GetModuleHandleW.argtypes = [LPCWSTR]
GetModuleHandleW.restype = HMODULE

WindowProc = WINFUNCTYPE(LRESULT, HWND, UINT, WPARAM, LPARAM)
DefWindowProcW = windll.User32.DefWindowProcW
DefWindowProcW.argtypes = [HWND, UINT, WPARAM, LPARAM]
DefWindowProcW.restype = LRESULT


class WNDCLASSEXW(Structure):
    _fields_ = [
        ('cbSize', UINT),
        ('style', UINT),
        ('lpfnWndProc', WindowProc),
        ('cbClsExtra', INT),
        ('cbWndExtra', INT),
        ('hInstance', HINSTANCE),
        ('hIcon', HICON),
        ('hCursor', HCURSOR),
        ('hbrBackground', HBRUSH),
        ('lpszMenuName', LPCWSTR),
        ('lpszClassName', LPCWSTR),
        ('hIconSm', HICON),
    ]


def get_WNDCLASSEXW(*largs):
    wnd_class = WNDCLASSEXW(*largs)
    wnd_class.cbSize = sizeof(WNDCLASSEXW)
    return wnd_class

RegisterClassExW = windll.User32.RegisterClassExW
RegisterClassExW.argtypes = [POINTER(WNDCLASSEXW)]
RegisterClassExW.restype = ATOM

UpdateWindow = windll.User32.UpdateWindow
UpdateWindow.argtypes = [HWND]
UpdateWindow.restype = BOOL

LoadImageW = windll.User32.LoadImageW
LoadImageW.argtypes = [HINSTANCE, LPCWSTR, UINT, INT, INT, UINT]
LoadImageW.restype = HANDLE

Shell_NotifyIconW = windll.Shell32.Shell_NotifyIconW
Shell_NotifyIconW.argtypes = [DWORD, POINTER(NOTIFYICONDATAW)]
Shell_NotifyIconW.restype = BOOL

DestroyIcon = windll.User32.DestroyIcon
DestroyIcon.argtypes = [HICON]
DestroyIcon.restype = BOOL

UnregisterClassW = windll.User32.UnregisterClassW
UnregisterClassW.argtypes = [ATOM, HINSTANCE]
UnregisterClassW.restype = BOOL

DestroyWindow = windll.User32.DestroyWindow
DestroyWindow.argtypes = [HWND]
DestroyWindow.restype = BOOL

LoadIconW = windll.User32.LoadIconW
LoadIconW.argtypes = [HINSTANCE, LPCWSTR]
LoadIconW.restype = HICON


class SYSTEM_POWER_STATUS(ctypes.Structure):
    _fields_ = [
        ('ACLineStatus', BYTE),
        ('BatteryFlag', BYTE),
        ('BatteryLifePercent', BYTE),
        ('Reserved1', BYTE),
        ('BatteryLifeTime', DWORD),
        ('BatteryFullLifeTime', DWORD),
    ]

SystemPowerStatusP = ctypes.POINTER(SYSTEM_POWER_STATUS)

GetSystemPowerStatus = ctypes.windll.kernel32.GetSystemPowerStatus
GetSystemPowerStatus.argtypes = [SystemPowerStatusP]
GetSystemPowerStatus.restype = BOOL