-
Notifications
You must be signed in to change notification settings - Fork 0
/
winsys.cpp
226 lines (187 loc) · 5.46 KB
/
winsys.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <QDebug>
#include <windows.h>
#include "winsys.h"
typedef DWORD (WINAPI *XInputGetState_t)(DWORD dwUserIndex, XINPUT_STATE *pState);
static XInputGetState_t XInputGetStateL = nullptr;
typedef enum {
QUNS_NOT_PRESENT = 1,
QUNS_BUSY = 2,
QUNS_RUNNING_D3D_FULL_SCREEN = 3,
QUNS_PRESENTATION_MODE = 4,
QUNS_ACCEPTS_NOTIFICATIONS = 5,
QUNS_QUIET_TIME = 6,
QUNS_APP = 7
} QUERY_USER_NOTIFICATION_STATE;
typedef HRESULT (WINAPI *SHQueryUserNotificationState_t)(QUERY_USER_NOTIFICATION_STATE *pquns);
static SHQueryUserNotificationState_t SHQueryUserNotificationStateL = nullptr;
WinSys::WinSys(QObject *parent) : QObject(parent), m_status(StatusOK)
{
m_xinputLib.setFileName(QStringLiteral("xinput1_4.dll"));
if (!m_xinputLib.load()) {
m_xinputLib.setFileName(QStringLiteral("xinput1_3.dll"));
m_xinputLib.load();
}
if (!m_xinputLib.isLoaded()) {
setStatus(StatusXInputLibraryNotFound);
return;
}
XInputGetStateL = (XInputGetState_t) m_xinputLib.resolve("XInputGetState");
if (XInputGetStateL == nullptr) {
setStatus(StatusXInputSymbolNotFound);
return;
}
m_shellLib.setFileName(QStringLiteral("shell32.dll"));
if (!m_shellLib.load()) {
setStatus(StatusShell32NotFound);
return;
}
SHQueryUserNotificationStateL = (SHQueryUserNotificationState_t)m_shellLib.resolve("SHQueryUserNotificationState");
if (SHQueryUserNotificationStateL == nullptr)
{
setStatus(StatusShell32SymbolNotFound);
return;
}
}
WinSys* WinSys::instance()
{
static WinSys *w = new WinSys();
return w;
}
void WinSys::sendVirtualKeyDown(quint16 virtualKey)
{
INPUT input = {};
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = 0;
input.ki.wVk = virtualKey;
SendInput(1, &input, sizeof(input));
}
void WinSys::sendVirtualKeyUp()
{
INPUT input = {};
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
void WinSys::sendUnicodeKeyDown(quint16 unicodeKey)
{
INPUT input = {};
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = KEYEVENTF_UNICODE;
input.ki.wScan = unicodeKey;
SendInput(1, &input, sizeof(input));
}
void WinSys::sendUnicodeKeyUp()
{
INPUT input = {};
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(input));
}
void WinSys::sendKeyDown(bool isVirtual, quint16 key)
{
if (isVirtual)
sendVirtualKeyDown(key);
else
sendUnicodeKeyDown(key);
}
void WinSys::sendKeyUp(bool isVirtual)
{
if (isVirtual)
sendVirtualKeyUp();
else
sendUnicodeKeyUp();
}
void WinSys::sendMouseWheel(qint32 scroll)
{
INPUT input = {};
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_WHEEL;
input.mi.mouseData = scroll;
SendInput(1, &input, sizeof(input));
}
void WinSys::sendMouseButton(MouseButton leftButton, MouseButton rightButton)
{
INPUT input = {};
switch (leftButton) {
case MouseButton::Down:
input.mi.dwFlags |= MOUSEEVENTF_LEFTDOWN;
break;
case MouseButton::Up:
input.mi.dwFlags |= MOUSEEVENTF_LEFTUP;
break;
case MouseButton::None:
break;
}
switch (rightButton) {
case MouseButton::Down:
input.mi.dwFlags |= MOUSEEVENTF_RIGHTDOWN;
break;
case MouseButton::Up:
input.mi.dwFlags |= MOUSEEVENTF_RIGHTUP;
break;
case MouseButton::None:
break;
}
if (input.mi.dwFlags > 0) {
input.type = INPUT_MOUSE;
SendInput(1, &input, sizeof(input));
}
}
void WinSys::moveMouse(double deltax, double deltay)
{
INPUT input = {};
POINT p;
GetCursorPos(&p);
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK;
input.mi.dx = (p.x + deltax) / GetSystemMetrics(SM_CXVIRTUALSCREEN) * 65535;
input.mi.dy = (p.y - deltay) / GetSystemMetrics(SM_CYVIRTUALSCREEN) * 65535;
SendInput(1, &input, sizeof(input));
}
WinSys::Status WinSys::status()
{
return m_status;
}
void WinSys::setStatus(Status status)
{
if (m_status != status) {
m_status = status;
emit statusChanged();
}
}
bool WinSys::isFullScreenMouseCursorHidden()
{
CURSORINFO c;
QUERY_USER_NOTIFICATION_STATE quns;
Q_ASSERT(SHQueryUserNotificationStateL != nullptr);
SHQueryUserNotificationStateL(&quns);
if (quns != QUNS_RUNNING_D3D_FULL_SCREEN && quns != QUNS_BUSY)
return false;
c.cbSize = sizeof(CURSORINFO);
if (!GetCursorInfo(&c))
return false;
return c.flags != CURSOR_SHOWING;
}
quint32 WinSys::XInputGetState(DWORD controllerIndex, XINPUT_STATE *xinput)
{
Q_ASSERT(XInputGetStateL != nullptr);
return XInputGetStateL(controllerIndex, xinput);
}
bool WinSys::hasAdminRights()
{
SID_IDENTIFIER_AUTHORITY authority = { SECURITY_NT_AUTHORITY };
PSID adminGroup;
// Initialize SID.
if (!AllocateAndInitializeSid(&authority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&adminGroup))
return false;
BOOL isInAdminGroup = FALSE;
if (!CheckTokenMembership(0, adminGroup, &isInAdminGroup))
isInAdminGroup = FALSE;
FreeSid(adminGroup);
return isInAdminGroup;
}