-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeyboard.cpp
More file actions
397 lines (345 loc) · 11.1 KB
/
keyboard.cpp
File metadata and controls
397 lines (345 loc) · 11.1 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <QApplication>
#include <QPushButton>
#include <QScreen>
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <linux/uinput.h>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include "keyboard.h"
#include "key.h"
Keyboard::Keyboard(QString fileName, QWidget *parent) : KXmlGuiWindow(parent)
{
totalWidth = 0;
totalHeight = 0;
initvdev();
QSharedPointer<QJsonObject> layout = loadLayout(fileName);
if (!layout)
{
qWarning() << "Invalid Config File";
exit(1);
}
setupUI(layout);
setupRows(layout);
setupCustomKeys(layout);
}
Keyboard::~Keyboard()
{
ioctl(fd, UI_DEV_DESTROY);
::close(fd);
}
void Keyboard::initvdev()
{
struct uinput_setup usetup;
struct uinput_abs_setup abs_usetup_x;
struct uinput_abs_setup abs_usetup_y;
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
// https://www.kernel.org/doc/html/v6.4/input/event-codes.html
ioctl(fd, UI_SET_EVBIT, EV_KEY);
for (int i = 0; i < 248; i++)
ioctl(fd, UI_SET_KEYBIT, i);
// Without BTN_LEFT, the mouse will not work
ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(fd, UI_SET_EVBIT, EV_REL);
ioctl(fd, UI_SET_RELBIT, REL_X);
ioctl(fd, UI_SET_RELBIT, REL_Y);
ioctl(fd, UI_SET_EVBIT, EV_ABS);
ioctl(fd, UI_SET_ABSBIT, ABS_X);
ioctl(fd, UI_SET_ABSBIT, ABS_Y);
ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_POINTER);
ioctl(fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
memset(&usetup, 0, sizeof(usetup));
usetup.id.bustype = BUS_USB;
usetup.id.vendor = 0xFFFF;
usetup.id.product = 0xFFFF;
strncpy(usetup.name, "Virtual Input", UINPUT_MAX_NAME_SIZE);
ioctl(fd, UI_DEV_SETUP, &usetup);
// ABS setup is independent
// Without this, typing would not work
QScreen *screen = QGuiApplication::primaryScreen();
QSize screenSize = screen->size();
memset(&abs_usetup_x, 0, sizeof(abs_usetup_x));
abs_usetup_x.code = ABS_X;
abs_usetup_x.absinfo.maximum = screenSize.width();
ioctl(fd, UI_ABS_SETUP, &abs_usetup_x);
memset(&abs_usetup_y, 0, sizeof(abs_usetup_y));
abs_usetup_y.code = ABS_Y;
abs_usetup_y.absinfo.maximum = screenSize.height();
ioctl(fd, UI_ABS_SETUP, &abs_usetup_y);
ioctl(fd, UI_DEV_CREATE);
}
QSharedPointer<QJsonObject> Keyboard::loadLayout(QString filename)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qWarning() << "Invalid Config File";
exit(1);
}
QByteArray jsonData = file.readAll();
file.close();
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(jsonData, &parseError);
if (parseError.error == QJsonParseError::NoError)
{
QSharedPointer<QJsonObject> layout = QSharedPointer<QJsonObject>::create(doc.object());
return layout;
}
else
{
qWarning() << "Invalid JSON";
exit(1);
}
}
void Keyboard::setupUI(QSharedPointer<QJsonObject> layout)
{
setFocusPolicy(Qt::NoFocus);
setAttribute(Qt::WA_ShowWithoutActivating);
setWindowFlags(Qt::WindowStaysOnTopHint | Qt::ToolTip | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
QScreen *screen = QGuiApplication::primaryScreen();
QSize screenSize = screen->size();
int x = 0;
int y = 0;
if (layout->contains("totalWidth"))
totalWidth = (*layout)["totalWidth"].toInt();
else
totalWidth = screenSize.width();
if (layout->contains("totalHeight"))
totalHeight = (*layout)["totalHeight"].toInt();
else
totalHeight = screenSize.height() / 2;
if (layout->contains("x"))
x = (*layout)["x"].toInt();
else
x = (screenSize.width() - totalWidth) / 2;
if (layout->contains("y"))
y = (*layout)["y"].toInt();
else
y = screenSize.height() - totalHeight;
setGeometry(x, y, totalWidth, totalHeight);
if (layout->contains("styleSheet"))
styleSheet = (*layout)["styleSheet"].toString();
}
void Keyboard::setupRows(QSharedPointer<QJsonObject> layout)
{
QJsonArray rows = (*layout)["rows"].toArray();
int nRows = rows.size();
double rowSpanSum = 0.0;
// pass 1: loop rows to normalize height
for (int i = 0; i < nRows; ++i)
{
double rowSpan = 1.0;
QJsonObject row = rows[i].toObject();
if (row.contains("rowSpan"))
{
rowSpan = row["rowSpan"].toDouble();
if (rowSpan <= 0)
{
qWarning() << "Invalid Row Span for row" << i;
exit(1);
}
}
else
row.insert("rowSpan", rowSpan);
rowSpanSum += rowSpan;
rows[i] = row;
}
double unitHeight = totalHeight / rowSpanSum;
// pass 2: loop rows for creating keys
int y = 0;
for (int i = 0; i < nRows; ++i)
{
QJsonObject row = rows[i].toObject();
if (!row.contains("keys"))
{
qWarning() << "Invalid Row";
exit(1);
}
QJsonArray rowKeys = row["keys"].toArray();
int nKeys = rowKeys.size();
int height = (int)(unitHeight * row["rowSpan"].toDouble());
double columnSpanSum = 0.0;
// pass 1: loop keys to normalize width
for (int j = 0; j < nKeys; ++j)
{
QJsonObject keyObject = rowKeys[j].toObject();
double columnSpan = 1.0;
if (keyObject.contains("columnSpan"))
{
columnSpan = keyObject["columnSpan"].toDouble();
if (columnSpan <= 0)
{
qWarning() << "Invalid Column Span for (row, key)" << i << j;
exit(1);
}
}
else
keyObject.insert("columnSpan", columnSpan);
columnSpanSum += columnSpan;
rowKeys[j] = keyObject;
}
double unitWidth = (totalWidth - (nKeys - 1) * 0) / columnSpanSum;
// pass 2: resize keys by their columnSpan
int x = 0;
for (int j = 0; j < nKeys; ++j)
{
QJsonObject keyObject = rowKeys[j].toObject();
int width = (int)(unitWidth * rowKeys[j].toObject()["columnSpan"].toDouble());
setupKey(layout, &keyObject, x, y, width, height);
x += width;
}
y += height;
}
}
void Keyboard::setupCustomKeys(QSharedPointer<QJsonObject> layout)
{
QJsonArray customKeys = (*layout)["customKeys"].toArray();
int nKeys = customKeys.size();
for (int i = 0; i < nKeys; ++i)
{
QJsonObject keyObject = customKeys[i].toObject();
int x = keyObject["x"].toDouble() * totalWidth;
int y = keyObject["y"].toDouble() * totalHeight;
int w = keyObject["w"].toDouble() * totalWidth;
int h = keyObject["h"].toDouble() * totalHeight;
if (x < 0 || x >= totalWidth || y < 0 || y >= totalHeight ||
w <= 0 || w > totalWidth || h <= 0 || h > totalHeight)
{
qWarning() << "Invalid Custom Key";
exit(1);
}
setupKey(layout, &keyObject, x, y, w, h);
}
}
void Keyboard::setupKey(QSharedPointer<QJsonObject> layout, QJsonObject *keyObject,
int x, int y, int w, int h)
{
int code = 0;
QString label;
// TODO additional types, crrently assume .type = key
// switch over the key types
QString type = (*keyObject)["type"].toString();
if (type == "key")
{
if (!keyObject->contains("code"))
{
qWarning() << "Invalid Key";
exit(1);
}
else
{
// cat /usr/include/linux/input-event-codes.h
code = (*keyObject)["code"].toInt();
if (code < 0 || code >= KEY_MAX)
{
qWarning() << "Invalid Key";
exit(1);
}
}
// & -> &&
if (keyObject->contains("label"))
label = (*keyObject)["label"].toString();
else
label = QString::number(code);
QString keyStylesheet = "";
if (keyObject->contains("styleSheet"))
keyStylesheet = (*keyObject)["styleSheet"].toString();
RegularKey *key = new RegularKey(x, y, w, h, code, label, keyStylesheet, this);
keys.append(key);
}
else if (type == "mouse")
{
QString mouseType;
int mouseX, mouseY;
if (!keyObject->contains("mouseType"))
{
qWarning() << "Invalid Mouse Key";
exit(1);
}
else
// Type checking in instantiation
mouseType = (*keyObject)["mouseType"].toString();
if (!keyObject->contains("mouseX") || !keyObject->contains("mouseY"))
{
qWarning() << "Invalid Mouse Key";
exit(1);
}
else
{
mouseX = (int)((*keyObject)["mouseX"].toDouble() * totalWidth);
mouseY = (int)((*keyObject)["mouseY"].toDouble() * totalHeight);
}
if (keyObject->contains("label"))
label = (*keyObject)["label"].toString();
else
label = mouseType;
QString keyStylesheet = "";
if (keyObject->contains("styleSheet"))
keyStylesheet = (*keyObject)["styleSheet"].toString();
MouseKey *key = new MouseKey(x, y, w, h, mouseType, mouseX, mouseY,
label, keyStylesheet, this);
keys.append(key);
}
else if (type == "special")
{
QString keyStylesheet = "";
int code = (*keyObject)["code"].toInt();
if (keyObject->contains("label"))
label = (*keyObject)["label"].toString();
else
label = QString::number(code);
if (keyObject->contains("styleSheet"))
keyStylesheet = (*keyObject)["styleSheet"].toString();
switch (code)
{
case SPECIAL_EXIT:
{
ExitKey *key = new ExitKey(x, y, w, h,
label, keyStylesheet, this);
keys.append(key);
break;
}
case SPECIAL_REPOSITION:
{
RepositionKey *key = new RepositionKey(x, y, w, h,
label, keyStylesheet, this);
keys.append(key);
break;
}
case SPECIAL_PLACEHOLDER:
{
break;
}
default:
{
qWarning() << "Invalid Special Key";
exit(1);
}
}
}
else if (type == "macro")
{
QString keyStylesheet = "";
int macro = (*keyObject)["macro"].toInt();
if (keyObject->contains("label"))
label = (*keyObject)["label"].toString();
else
label = macro;
if (keyObject->contains("styleSheet"))
keyStylesheet = (*keyObject)["styleSheet"].toString();
MacroKey *key = new MacroKey(x, y, w, h,
(*layout)["macros"].toArray()[macro].toArray(),
label, keyStylesheet, this);
keys.append(key);
}
else
{
qWarning() << "Invalid Key type";
exit(1);
}
}