-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKeyboard.cpp
291 lines (252 loc) · 6.23 KB
/
Keyboard.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Camera Mouse Suite
* Copyright (C) 2015, Andrew Kurauchi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdexcept>
#include <QDebug>
#include "Keyboard.h"
#ifdef Q_OS_LINUX
#include <QThread>
#include <QMutex>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysymdef.h>
#include <stdio.h>
#include <ctype.h>
#include <queue>
#elif defined Q_OS_WIN
#include <queue>
#include <Windows.h>
#include <QMutex>
#elif defined Q_OS_MAC
#else
#endif
namespace CMS {
KeyEvent::KeyEvent(Key key, KeyState state) :
key(key), state(state)
{}
Key KeyEvent::getKey()
{
return key;
}
KeyState KeyEvent::getState()
{
return state;
}
IKeyboard::~IKeyboard()
{}
IKeyboard* KeyboardFactory::newKeyboard()
{
#ifdef Q_OS_LINUX
return new LinuxKeyboard;
#elif defined Q_OS_WIN
return new WindowsKeyboard;
#elif defined Q_OS_MAC
return new MacKeyboard;
#else
std::runtime_error("Operating System not supported. Cannot control mouse.");
return 0;
#endif
}
#ifdef Q_OS_LINUX
// Use an unnamed namespace to restrict global variables scope
namespace {
int instances = 0;
std::queue<KeyEvent> events;
QMutex mutex;
} // namespace
class KeyboardListener : public QThread
{
public:
KeyboardListener() : running(true)
{}
void run()
{
// Code from: http://stackoverflow.com/questions/22749444/listening-to-keyboard-events-without-consuming-them-in-x11-keyboard-hooking
Display* d = XOpenDisplay(NULL);
Window root = DefaultRootWindow(d);
Window curFocus;
char buf[17];
KeySym ks;
XComposeStatus comp;
int revert;
XGetInputFocus(d, &curFocus, &revert);
XSelectInput(d, curFocus, KeyPressMask|KeyReleaseMask|FocusChangeMask);
bool stop;
runningMutex.lock();
stop = !running;
runningMutex.unlock();
while (!stop)
{
XEvent ev;
XNextEvent(d, &ev); // Will block here, find out a way to move on if the program finishes
switch (ev.type)
{
case FocusOut:
if (curFocus != root)
XSelectInput(d, curFocus, 0);
XGetInputFocus (d, &curFocus, &revert);
if (curFocus == PointerRoot)
curFocus = root;
XSelectInput(d, curFocus, KeyPressMask|KeyReleaseMask|FocusChangeMask);
break;
case KeyPress:
XLookupString(&ev.xkey, buf, 16, &ks, &comp);
switch (ks)
{
case XK_Control_L:
case XK_Control_R:
mutex.lock();
events.push(KeyEvent(KEY_CONTROL, KEY_STATE_DOWN));
mutex.unlock();
break;
}
}
runningMutex.lock();
stop = !running;
runningMutex.unlock();
}
}
void stop()
{
runningMutex.lock();
running = false;
runningMutex.unlock();
}
private:
bool running;
QMutex runningMutex;
};
// Use an unnamed namespace to restrict global variables scope
namespace {
KeyboardListener listener;
} // namespace
LinuxKeyboard::LinuxKeyboard()
{
instances++;
if (instances == 1)
{
listener.start();
}
}
LinuxKeyboard::~LinuxKeyboard()
{
instances--;
if (instances == 0)
{
listener.stop();
}
}
KeyEvent LinuxKeyboard::nextEvent()
{
if (!hasNextEvent())
{
throw std::logic_error("No events available");
}
mutex.lock();
KeyEvent event = events.front();
events.pop();
mutex.unlock();
return event;
}
bool LinuxKeyboard::hasNextEvent()
{
return !events.empty();
}
#elif defined Q_OS_WIN
// Use an unnamed namespace to restrict global variables scope
namespace {
int instances = 0;
HHOOK keyboardHook;
std::queue<KeyEvent> events;
QMutex mutex;
} // namespace
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
Key key = KEY_NONE;
KeyState state = KEY_STATE_NONE;
if (nCode == HC_ACTION)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
switch (p->vkCode)
{
case VK_RCONTROL:
case VK_LCONTROL:
case VK_CONTROL:
key = KEY_CONTROL;
break;
case VK_RMENU:
case VK_LMENU:
case VK_MENU:
key = KEY_ALT;
break;
}
switch (wParam)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
state = KEY_STATE_DOWN;
break;
case WM_KEYUP:
case WM_SYSKEYUP:
state = KEY_STATE_UP;
break;
}
}
if (key != KEY_NONE && state != KEY_STATE_NONE)
{
mutex.lock();
events.push(KeyEvent(key, state));
mutex.unlock();
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
WindowsKeyboard::WindowsKeyboard()
{
instances++;
if (instances == 1)
{
keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, 0, 0);
}
}
WindowsKeyboard::~WindowsKeyboard()
{
instances--;
if (instances == 0)
{
UnhookWindowsHookEx(keyboardHook);
}
}
KeyEvent WindowsKeyboard::nextEvent()
{
if (!hasNextEvent())
{
throw std::logic_error("No events available");
}
mutex.lock();
KeyEvent event = events.front();
events.pop();
mutex.unlock();
return event;
}
bool WindowsKeyboard::hasNextEvent()
{
return !events.empty();
}
#elif defined Q_OS_MAC
#else
#endif
} // namespace CMS