-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactionsource_win32.cpp
237 lines (200 loc) · 8.22 KB
/
interactionsource_win32.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
226
227
228
229
230
231
232
233
234
235
236
237
#include "interactionsource.h"
#include <QDebug>
#include <Windows.h>
#include <QCoreApplication>
#include <strsafe.h>
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
InteractionSource::InteractionSource()
{
_timer.setSingleShot(true);
connect(&_timer, &QTimer::timeout, this, &InteractionSource::_timerExpired);
HINSTANCE hInst = GetModuleHandle(NULL);
WNDCLASS wc = {};
wc.hInstance = hInst;
wc.lpfnWndProc = &DefWindowProc;
wc.lpszClassName = L"rih";
ATOM rihClass = RegisterClass(&wc);
if (rihClass == 0) {
qCritical() << "reg class failed";
}
HWND hwnd = CreateWindow(wc.lpszClassName,NULL,0,0,0,0,0,HWND_MESSAGE,NULL,hInst,NULL);
if (hwnd == NULL) {
ErrorExit(L"CreateWindow");
qCritical() << "creawin failed";
}
RegisterHotKey(hwnd, 1, MOD_NOREPEAT | MOD_WIN, 'A');
RegisterHotKey(hwnd, 2, MOD_NOREPEAT | MOD_WIN, 'Z');
RegisterHotKey(hwnd, 3, MOD_NOREPEAT | MOD_WIN, 'C');
RAWINPUTDEVICE rid[2];
rid[0].usUsagePage = 0x01;
rid[0].usUsage = 0x06;
rid[0].dwFlags = RIDEV_NOLEGACY | RIDEV_INPUTSINK;
rid[0].hwndTarget = hwnd;
rid[1].usUsagePage = 0x01;
rid[1].usUsage = 0x02;
rid[1].dwFlags = RIDEV_NOLEGACY | RIDEV_INPUTSINK;
rid[1].hwndTarget = hwnd;
if (RegisterRawInputDevices(rid, 2, sizeof(rid[0])) == FALSE) {
qCritical() << "reg failed";
}
}
InteractionSource::~InteractionSource()
{
}
void
InteractionSource::simulateClick(Qt::MouseButton button, ButtonAction action)
{
bool leftButton = (button == Qt::LeftButton);
DWORD dwFlagsDown = leftButton ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN;
DWORD dwFlagsUp = leftButton ? MOUSEEVENTF_LEFTUP : MOUSEEVENTF_RIGHTUP;
if (action & ButtonDown || action & ButtonDoubleClick)
mouse_event(dwFlagsDown, 0, 0, 0, 0);
if (action & ButtonUp || action & ButtonDoubleClick)
mouse_event(dwFlagsUp, 0, 0, 0, 0);
if (action & ButtonDoubleClick) {
mouse_event(dwFlagsDown, 0, 0, 0, 0);
mouse_event(dwFlagsUp, 0, 0, 0, 0);
}
}
bool
InteractionSource::nativeEventFilter(const QByteArray &, void *message, long *result)
{
static LPBYTE inputBuffer = NULL;
static UINT inputBufferSize = 0;
MSG &msg = *static_cast<MSG *>(message);
if (msg.message == WM_INPUT) {
UINT dwSize;
HRESULT hResult;
TCHAR szTempOutput[1024];
GetRawInputData((HRAWINPUT)msg.lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
if (dwSize > inputBufferSize) {
delete[] inputBuffer;
inputBuffer = new BYTE[dwSize];
inputBufferSize = dwSize;
}
if (GetRawInputData((HRAWINPUT)msg.lParam, RID_INPUT, inputBuffer, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize )
OutputDebugString (TEXT("GetRawInputData does not return correct size !\n"));
RAWINPUT* raw = (RAWINPUT*)inputBuffer;
if (raw->header.dwType == RIM_TYPEKEYBOARD)
{
hResult = StringCchPrintf(szTempOutput, STRSAFE_MAX_CCH, TEXT(" Kbd: make=%04x Flags:%04x Reserved:%04x ExtraInformation:%08x, msg=%04x VK=%04x \n"),
raw->data.keyboard.MakeCode,
raw->data.keyboard.Flags,
raw->data.keyboard.Reserved,
raw->data.keyboard.ExtraInformation,
raw->data.keyboard.Message,
raw->data.keyboard.VKey);
if (FAILED(hResult))
{
// TODO: write error handler
}
OutputDebugString(szTempOutput);
bool pressed = !(raw->data.keyboard.Flags & RI_KEY_BREAK);
bool metaKey = raw->data.keyboard.VKey == VK_LWIN || raw->data.keyboard.VKey == VK_RWIN;
bool commandKey =
raw->data.keyboard.VKey == 'C' || raw->data.keyboard.VKey == 'Z' || raw->data.keyboard.VKey == 'A';
//qDebug() << "pressed" << pressed << "metakey" << metaKey << "a" << (raw->data.keyboard.VKey == 'A');
if (!metaKey) {
qDebug() << "PRESSED" << "(metadown?" << _metaDown << ")";
if (_metaDown && commandKey) {
qDebug() << "METADOWN && COMMANDKEY";
_xDown = raw->data.keyboard.VKey == 'C';
_zDown = raw->data.keyboard.VKey == 'Z';
if (raw->data.keyboard.VKey == 'A') {
qDebug() << "A";
emit dragShortcutPressed();
}
} else
qDebug() << "NOPE";
} else {
if (pressed) {
_metaDown = true;
_xDown = false;
_zDown = false;
} else{
_metaDown = false;
_xDown = false;
_zDown = false;
}
}
qDebug() << _metaDown << _xDown << _zDown;
}
else if (raw->header.dwType == RIM_TYPEMOUSE)
{
hResult = StringCchPrintf(szTempOutput, STRSAFE_MAX_CCH, TEXT("Mouse: usFlags=%04x ulButtons=%04x usButtonFlags=%04x usButtonData=%04x ulRawButtons=%04x lLastX=%04d lLastY=%04d ulExtraInformation=%04x\r\n"),
raw->data.mouse.usFlags,
raw->data.mouse.ulButtons,
raw->data.mouse.usButtonFlags,
raw->data.mouse.usButtonData,
raw->data.mouse.ulRawButtons,
raw->data.mouse.lLastX,
raw->data.mouse.lLastY,
raw->data.mouse.ulExtraInformation);
if (FAILED(hResult))
{
// TODO: write error handler
}
OutputDebugString(szTempOutput);
if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_DOWN) {
_leftButtonDown = true;
} else if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP || raw->data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_UP) {
if (raw->data.mouse.usButtonFlags & RI_MOUSE_LEFT_BUTTON_UP) {
_leftButtonDown = false;
_distanceSinceButtonDown = 0;
}
if (raw->header.hDevice == NULL) {
qDebug() << "ignored autoclick.";
} else {
qDebug() << "Click canceling next dwell.";
_timer.stop();
emit userClicked();
}
} else if (!raw->data.mouse.usButtonFlags) {
if (raw->data.mouse.usFlags) {
if (raw->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE)
qWarning() << "Can't handle MOUSE_MOVE_ABSOLUTE";
if (raw->data.mouse.usFlags & MOUSE_VIRTUAL_DESKTOP)
qWarning() << "Can't handle MOUSE_VIRTUAL_DESKTOP";
qWarning() << "Ignored mouse input.";
return true;
}
uint distance = std::abs(raw->data.mouse.lLastX) +
std::abs(raw->data.mouse.lLastY);
_distanceSinceLastTimer += distance;
if (_leftButtonDown)
_distanceSinceButtonDown += distance;
if (_distanceSinceButtonDown > 10)
emit userDragged();
_timer.start(350);
}
}
return true;
}
return false;
}