-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextinjector.cpp
More file actions
380 lines (336 loc) · 13 KB
/
Copy pathtextinjector.cpp
File metadata and controls
380 lines (336 loc) · 13 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
#include "textinjector.h"
#include <QString>
#include <QGuiApplication>
#include <QClipboard>
#include <QTimer>
// ─────────────────────────────────────────────────────────────────────────────
// Common (platform-independent) — mode state + dispatch + clipboard paste.
// Kept ABOVE the per-platform blocks so the Qt headers are parsed before any
// X11 headers (whose macros — None/Bool/… — would otherwise clash with Qt).
// ─────────────────────────────────────────────────────────────────────────────
namespace {
TextInjector::Mode g_mode = TextInjector::Mode::Type;
}
void TextInjector::setMode(Mode mode) { g_mode = mode; }
TextInjector::Mode TextInjector::mode() { return g_mode; }
void TextInjector::typeText(const QString& text)
{
if (text.isEmpty())
return;
if (g_mode == Mode::ClipboardPaste) {
QClipboard* cb = QGuiApplication::clipboard();
const QString previous = cb->text();
cb->setText(text);
sendPasteShortcut();
// Restore the user's previous clipboard shortly after the paste lands.
QTimer::singleShot(300, cb, [previous]() {
QGuiApplication::clipboard()->setText(previous);
});
return;
}
injectUnicode(text);
}
// ─────────────────────────────────────────────────────────────
// WINDOWS — SendInput
// ─────────────────────────────────────────────────────────────
#if defined(PLATFORM_WINDOWS)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <vector>
void TextInjector::injectUnicode(const QString& text)
{
const int n = text.size();
if (n <= 0)
return;
// One key-down + key-up per UTF-16 code unit; surrogate pairs are sent as
// two consecutive KEYEVENTF_UNICODE events, which Windows recombines.
std::vector<INPUT> events;
events.reserve(size_t(n) * 2);
for (int i = 0; i < n; ++i) {
INPUT down{};
down.type = INPUT_KEYBOARD;
down.ki.wScan = WORD(text.at(i).unicode());
down.ki.dwFlags = KEYEVENTF_UNICODE;
events.push_back(down);
INPUT up = down;
up.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
events.push_back(up);
}
SendInput(UINT(events.size()), events.data(), sizeof(INPUT));
}
void TextInjector::sendBackspaces(int count)
{
if (count <= 0)
return;
std::vector<INPUT> events;
events.reserve(size_t(count) * 2);
for (int i = 0; i < count; ++i) {
INPUT down{};
down.type = INPUT_KEYBOARD;
down.ki.wVk = VK_BACK;
events.push_back(down);
INPUT up = down;
up.ki.dwFlags = KEYEVENTF_KEYUP;
events.push_back(up);
}
SendInput(UINT(events.size()), events.data(), sizeof(INPUT));
}
void TextInjector::sendPasteShortcut()
{
INPUT in[4] = {};
in[0].type = INPUT_KEYBOARD; in[0].ki.wVk = VK_CONTROL;
in[1].type = INPUT_KEYBOARD; in[1].ki.wVk = 'V';
in[2].type = INPUT_KEYBOARD; in[2].ki.wVk = 'V'; in[2].ki.dwFlags = KEYEVENTF_KEYUP;
in[3].type = INPUT_KEYBOARD; in[3].ki.wVk = VK_CONTROL; in[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4, in, sizeof(INPUT));
}
void TextInjector::moveCursorLeft(int count)
{
if (count <= 0)
return;
std::vector<INPUT> events;
events.reserve(size_t(count) * 2);
for (int i = 0; i < count; ++i) {
INPUT down{};
down.type = INPUT_KEYBOARD;
down.ki.wVk = VK_LEFT;
events.push_back(down);
INPUT up = down;
up.ki.dwFlags = KEYEVENTF_KEYUP;
events.push_back(up);
}
SendInput(UINT(events.size()), events.data(), sizeof(INPUT));
}
// ─────────────────────────────────────────────────────────────
// macOS — CoreGraphics (implemented in macos_utils.mm)
// ─────────────────────────────────────────────────────────────
#elif defined(PLATFORM_MACOS)
#include "macos_utils.h"
void TextInjector::injectUnicode(const QString& text) { macTypeUnicode(text); }
void TextInjector::sendBackspaces(int count) { macSendBackspaces(count); }
void TextInjector::moveCursorLeft(int count) { macSendLeftArrows(count); }
void TextInjector::sendPasteShortcut() { macSendPasteShortcut(); }
// ─────────────────────────────────────────────────────────────
// Linux — X11 XTest, with a best-effort uinput fallback
// ─────────────────────────────────────────────────────────────
#elif defined(PLATFORM_LINUX)
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
#include <linux/uinput.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <cstring>
namespace {
// ── X11 helpers ──────────────────────────────────────────────
KeySym keysymForCodepoint(uint cp)
{
switch (cp) {
case '\n': case '\r': return XK_Return;
case '\t': return XK_Tab;
case '\b': return XK_BackSpace;
default: break;
}
if (cp < 0x100)
return KeySym(cp);
return KeySym(0x01000000UL | cp); // X11 Unicode keysym range
}
// Type via XTest by temporarily remapping a spare keycode to each keysym (so any
// character can be produced at modifier level 0). Returns false if no X display.
bool x11Type(const QString& text)
{
Display* dpy = XOpenDisplay(nullptr);
if (!dpy)
return false;
int minKc = 0, maxKc = 0;
XDisplayKeycodes(dpy, &minKc, &maxKc);
int per = 0;
KeySym* map = XGetKeyboardMapping(dpy, KeyCode(minKc), maxKc - minKc + 1, &per);
int spare = 0;
if (map && per > 0) {
for (int kc = minKc; kc <= maxKc; ++kc) {
bool empty = true;
for (int j = 0; j < per; ++j)
if (map[(kc - minKc) * per + j] != NoSymbol) { empty = false; break; }
if (empty) { spare = kc; break; }
}
}
if (map)
XFree(map);
if (spare == 0) { XCloseDisplay(dpy); return false; }
const auto ucs4 = text.toUcs4();
for (uint cp : ucs4) {
KeySym ks = keysymForCodepoint(cp);
XChangeKeyboardMapping(dpy, spare, 1, &ks, 1);
XSync(dpy, False);
XTestFakeKeyEvent(dpy, KeyCode(spare), True, CurrentTime);
XTestFakeKeyEvent(dpy, KeyCode(spare), False, CurrentTime);
XFlush(dpy);
}
KeySym none = NoSymbol;
XChangeKeyboardMapping(dpy, spare, 1, &none, 1);
XSync(dpy, False);
XCloseDisplay(dpy);
return true;
}
bool x11FakeChord(KeySym mod, KeySym key, int repeat)
{
Display* dpy = XOpenDisplay(nullptr);
if (!dpy)
return false;
const KeyCode modKc = mod ? XKeysymToKeycode(dpy, mod) : 0;
const KeyCode keyKc = XKeysymToKeycode(dpy, key);
for (int i = 0; i < repeat; ++i) {
if (modKc) XTestFakeKeyEvent(dpy, modKc, True, CurrentTime);
XTestFakeKeyEvent(dpy, keyKc, True, CurrentTime);
XTestFakeKeyEvent(dpy, keyKc, False, CurrentTime);
if (modKc) XTestFakeKeyEvent(dpy, modKc, False, CurrentTime);
}
XFlush(dpy);
XCloseDisplay(dpy);
return true;
}
// ── uinput fallback (ASCII / US layout, best-effort) ─────────
// Used only when there is no X display (e.g. pure Wayland without XWayland).
// uinput emits key *codes*, so it cannot produce arbitrary Unicode — only the
// ASCII subset below. For full Unicode on Wayland, use clipboard-paste mode.
bool asciiToKey(QChar qc, int& code, bool& shift)
{
static const int kLetter[26] = {
KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z
};
const ushort u = qc.unicode();
shift = false;
if (u >= 'a' && u <= 'z') { code = kLetter[u - 'a']; return true; }
if (u >= 'A' && u <= 'Z') { shift = true; code = kLetter[u - 'A']; return true; }
if (u >= '1' && u <= '9') { code = KEY_1 + (u - '1'); return true; }
if (u == '0') { code = KEY_0; return true; }
switch (u) {
case ' ': code = KEY_SPACE; return true;
case '.': code = KEY_DOT; return true;
case ',': code = KEY_COMMA; return true;
case '\n': case '\r': code = KEY_ENTER; return true;
case '\t': code = KEY_TAB; return true;
default: return false; // unmapped — skip
}
}
struct Uinput {
int fd = -1;
bool open()
{
fd = ::open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (fd < 0)
return false;
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_EVBIT, EV_SYN);
for (int k = 1; k < 128; ++k) // enable the whole low keycode range
ioctl(fd, UI_SET_KEYBIT, k);
struct uinput_setup us;
std::memset(&us, 0, sizeof(us));
us.id.bustype = BUS_USB;
us.id.vendor = 0x1234;
us.id.product = 0x5678;
std::strncpy(us.name, "TrackType virtual keyboard", sizeof(us.name) - 1);
if (ioctl(fd, UI_DEV_SETUP, &us) < 0 || ioctl(fd, UI_DEV_CREATE) < 0) {
::close(fd);
fd = -1;
return false;
}
usleep(50000); // give udev time to create the device node
return true;
}
// NB: not named emit() — that is a Qt keyword macro (expands to nothing).
void writeEvent(int type, int code, int val)
{
struct input_event ev;
std::memset(&ev, 0, sizeof(ev));
ev.type = type; ev.code = code; ev.value = val;
const ssize_t r = ::write(fd, &ev, sizeof(ev));
(void)r;
}
void syn() { writeEvent(EV_SYN, SYN_REPORT, 0); }
void tap(int code, bool shift)
{
if (shift) { writeEvent(EV_KEY, KEY_LEFTSHIFT, 1); syn(); }
writeEvent(EV_KEY, code, 1); syn();
writeEvent(EV_KEY, code, 0); syn();
if (shift) { writeEvent(EV_KEY, KEY_LEFTSHIFT, 0); syn(); }
}
void tapWithCtrl(int code)
{
writeEvent(EV_KEY, KEY_LEFTCTRL, 1); syn();
writeEvent(EV_KEY, code, 1); syn();
writeEvent(EV_KEY, code, 0); syn();
writeEvent(EV_KEY, KEY_LEFTCTRL, 0); syn();
}
void close()
{
if (fd >= 0) { ioctl(fd, UI_DEV_DESTROY); ::close(fd); fd = -1; }
}
};
} // namespace
void TextInjector::injectUnicode(const QString& text)
{
if (x11Type(text))
return; // X11 / XWayland path (full Unicode)
// Fallback: uinput can only synthesize the ASCII subset (layout-dependent).
Uinput kb;
if (!kb.open())
return;
for (const QChar qc : text) {
int code; bool shift;
if (asciiToKey(qc, code, shift))
kb.tap(code, shift);
}
kb.close();
}
void TextInjector::sendBackspaces(int count)
{
if (count <= 0)
return;
if (x11FakeChord(0, XK_BackSpace, count))
return;
Uinput kb;
if (!kb.open())
return;
for (int i = 0; i < count; ++i)
kb.tap(KEY_BACKSPACE, false);
kb.close();
}
void TextInjector::sendPasteShortcut()
{
if (x11FakeChord(XK_Control_L, XK_v, 1))
return;
Uinput kb;
if (!kb.open())
return;
kb.tapWithCtrl(KEY_V);
kb.close();
}
void TextInjector::moveCursorLeft(int count)
{
if (count <= 0)
return;
if (x11FakeChord(0, XK_Left, count))
return;
Uinput kb;
if (!kb.open())
return;
for (int i = 0; i < count; ++i)
kb.tap(KEY_LEFT, false);
kb.close();
}
// ─────────────────────────────────────────────────────────────
// Fallback (unsupported platform)
// ─────────────────────────────────────────────────────────────
#else
void TextInjector::injectUnicode(const QString&) {}
void TextInjector::sendBackspaces(int) {}
void TextInjector::moveCursorLeft(int) {}
void TextInjector::sendPasteShortcut() {}
#endif
// Common across platforms.
bool TextInjector::hasInputDeviceAccess() { return true; }