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
|
/*
* This work is licensed under the terms of the GNU GPL, version 2 or
* (at your option) any later version. See the COPYING file in the
* top-level directory.
*/
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "qemu/config-file.h"
#include "qemu/sockets.h"
#include "sysemu/sysemu.h"
#include "ui/input.h"
#include <sys/ioctl.h>
#include "standard-headers/linux/input.h"
static int linux_to_qcode[KEY_CNT] = {
[KEY_ESC] = Q_KEY_CODE_ESC,
[KEY_1] = Q_KEY_CODE_1,
[KEY_2] =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TRACT,
[KEY_KPPLUS] = Q_KEY_CODE_KP_ADD,
[KEY_KPDOT] = Q_KEY_CODE_KP_DECIMAL,
[KEY_KPENTER] = Q_KEY_CODE_KP_ENTER,
[KEY_KPSLASH] = Q_KEY_CODE_KP_DIVIDE,
[KEY_KPASTERISK] = Q_KEY_CODE_KP_MULTIPLY,
[KEY_F11] = Q_KEY_CODE_F11,
[KEY_F12] = Q_KEY_CODE_F12,
[KEY_RIGHTCTRL] = Q_KEY_CODE_CTRL_R,
[KEY_SYSRQ] = Q_KEY_CODE_SYSRQ,
[KEY_RIGHTALT] = Q_KEY_CODE_ALT_R,
[KEY_HOME] = Q_KEY_CODE_HOME,
[KEY_UP] = Q_KEY_CODE_UP,
[KEY_PAGEUP] = Q_KEY_CODE_PGUP,
[KEY_LEFT] = Q_KEY_CODE_LEFT,
[KEY_RIGHT] = Q_KEY_CODE_RIGHT,
[KEY_END] = Q_KEY_CODE_END,
[KEY_DOWN] = Q_KEY_CODE_DOWN,
[KEY_PAGEDOWN] = Q_KEY_CODE_PGDN,
[KEY_INSERT] = Q_KEY_CODE_INSERT,
[KEY_DELETE] = Q_KEY_CODE_DELETE,
[KEY_LEFTMETA] = Q_KEY_CODE_META_L,
[KEY_RIGHTMETA] = Q_KEY_CODE_META_R,
[KEY_MENU] = Q_KEY_CODE_MENU,
};
static int qemu_input_linux_to_qcode(unsigned int lnx)
{
assert(lnx < KEY_CNT);
return linux_to_qcode[lnx];
}
typedef struct InputLinux InputLinux;
struct InputLinux {
const char *evdev;
int fd;
bool grab_request;
bool grab_active;
bool grab_all;
bool keydown[KEY_CNT];
int keycount;
int wheel;
QTAILQ_ENTRY(InputLinux) next;
};
static QTAILQ_HEAD(, InputLinux) inputs = QTAILQ_HEAD_INITIALIZER(inputs);
static void input_linux_toggle_grab(InputLinux *il)
{
intptr_t request = !il->grab_active;
InputLinux *item;
int rc;
rc = ioctl(il->fd, EVIOCGRAB, request);
if (rc < 0) {
return;
}
il->grab_active = !il->grab_active;
if (!il->grab_all) {
return;
}
QTAILQ_FOREACH(item, &inputs, next) {
if (item == il || item->grab_all) {
/* avoid endless loops */
continue;
}
if (item->grab_active != il->grab_active) {
input_linux_toggle_grab(item);
}
}
}
static void input_linux_event_keyboard(void *opaque)
{
InputLinux *il = opaque;
struct input_event event;
int rc;
for (;;) {
rc = read(il->fd, &event, sizeof(event));
if (rc != sizeof(event)) {
if (rc < 0 && errno != EAGAIN) {
fprintf(stderr, "%s: read: %s\n", __func__, strerror(errno));
qemu_set_fd_handler(il->fd, NULL, NULL, NULL);
close(il->fd);
}
break;
}
switch (event.type) {
case EV_KEY:
if (event.value > 1) {
/*
* ignore autorepeat + unknown key events
* 0 == up, 1 == down, 2 == autorepeat, other == undefined
*/
continue;
}
/* keep track of key state */
if (!il->keydown[event.code] && event.value) {
il->keydown[event.code] = true;
il->keycount++;
}
if (il->keydown[event.code] && !event.value) {
il->keydown[event.code] = false;
il->keycount--;
}
/* send event to guest when grab is active */
if (il->grab_active) {
int qcode = qemu_input_linux_to_qcode(event.code);
qemu_input_event_send_key_qcode(NULL, qcode, event.value);
}
/* hotkey -> record switch request ... */
if (il->keydown[KEY_LEFTCTRL] &&
il->keydown[KEY_RIGHTCTRL]) {
il->grab_request = true;
}
/*
* ... and do the switch when all keys are lifted, so we
* confuse neither guest nor host with keys which seem to
* be stuck due to missing key-up events.
*/
if (il->grab_request && !il->keycount) {
il->grab_request = false;
input_linux_toggle_grab(il);
}
break;
}
}
}
static void input_linux_event_mouse_button(int button)
{
qemu_input_queue_btn(NULL, button, true);
qemu_input_event_sync();
qemu_input_queue_btn(NULL, button, false);
qemu_input_event_sync();
}
static void input_linux_event_mouse(void *opaque)
{
InputLinux *il = opaque;
struct input_event event;
int rc;
for (;;) {
rc = read(il->fd, &event, sizeof(event));
if (rc != sizeof(event)) {
if (rc < 0 && errno != EAGAIN) {
fprintf(stderr, "%s: read: %s\n", __func__, strerror(errno));
qemu_set_fd_handler(il->fd, NULL, NULL, NULL);
close(il->fd);
}
break;
}
/* only send event to guest when grab is active */
if (!il->grab_active) {
continue;
}
switch (event.type) {
case EV_KEY:
switch (event.code) {
case BTN_LEFT:
qemu_input_queue_btn(NULL, INPUT_BUTTON_LEFT, event.value);
break;
case BTN_RIGHT:
qemu_input_queue_btn(NULL, INPUT_BUTTON_RIGHT, event.value);
break;
case BTN_MIDDLE:
qemu_input_queue_btn(NULL, INPUT_BUTTON_MIDDLE, event.value);
break;
case BTN_GEAR_UP:
qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_UP, event.value);
break;
case BTN_GEAR_DOWN:
qemu_input_queue_btn(NULL, INPUT_BUTTON_WHEEL_DOWN,
event.value);
break;
};
break;
case EV_REL:
switch (event.code) {
case REL_X:
qemu_input_queue_rel(NULL, INPUT_AXIS_X, event.value);
break;
case REL_Y:
qemu_input_queue_rel(NULL, INPUT_AXIS_Y, event.value);
break;
case REL_WHEEL:
il->wheel = event.value;
break;
}
break;
case EV_SYN:
qemu_input_event_sync();
if (il->wheel != 0) {
input_linux_event_mouse_button((il->wheel > 0)
? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN);
il->wheel = 0;
}
break;
}
}
}
int input_linux_init(void *opaque, QemuOpts *opts, Error **errp)
{
InputLinux *il = g_new0(InputLinux, 1);
uint32_t evtmap;
int rc, ver;
il->evdev = qemu_opt_get(opts, "evdev");
il->grab_all = qemu_opt_get_bool(opts, "grab-all", false);
if (!il->evdev) {
error_setg(errp, "no input device specified");
goto err_free;
}
il->fd = open(il->evdev, O_RDWR);
if (il->fd < 0) {
error_setg_file_open(errp, errno, il->evdev);
goto err_free;
}
qemu_set_nonblock(il->fd);
rc = ioctl(il->fd, EVIOCGVERSION, &ver);
if (rc < 0) {
error_setg(errp, "%s: is not an evdev device", il->evdev);
goto err_close;
}
rc = ioctl(il->fd, EVIOCGBIT(0, sizeof(evtmap)), &evtmap);
if (evtmap & (1 << EV_REL)) {
/* has relative axis -> assume mouse */
qemu_set_fd_handler(il->fd, input_linux_event_mouse, NULL, il);
} else if (evtmap & (1 << EV_ABS)) {
/* has absolute axis -> not supported */
error_setg(errp, "tablet/touchscreen not supported");
goto err_close;
} else if (evtmap & (1 << EV_KEY)) {
/* has keys/buttons (and no axis) -> assume keyboard */
qemu_set_fd_handler(il->fd, input_linux_event_keyboard, NULL, il);
} else {
/* Huh? What is this? */
error_setg(errp, "unknown kind of input device");
goto err_close;
}
input_linux_toggle_grab(il);
QTAILQ_INSERT_TAIL(&inputs, il, next);
return 0;
err_close:
close(il->fd);
err_free:
g_free(il);
return -1;
}
static QemuOptsList qemu_input_linux_opts = {
.name = "input-linux",
.head = QTAILQ_HEAD_INITIALIZER(qemu_input_linux_opts.head),
.implied_opt_name = "evdev",
.desc = {
{
.name = "evdev",
.type = QEMU_OPT_STRING,
},{
.name = "grab-all",
.type = QEMU_OPT_BOOL,
},
{ /* end of list */ }
},
};
static void input_linux_register_config(void)
{
qemu_add_opts(&qemu_input_linux_opts);
}
machine_init(input_linux_register_config);
|