2 * This work is licensed under the terms of the GNU GPL, version 2 or
3 * (at your option) any later version. See the COPYING file in the
7 #include "qemu/osdep.h"
8 #include "qemu-common.h"
9 #include "qemu/config-file.h"
10 #include "qemu/sockets.h"
11 #include "sysemu/sysemu.h"
14 #include <sys/ioctl.h>
15 #include "standard-headers/linux/input.h"
17 static int linux_to_qcode
[KEY_CNT
] = {
18 [KEY_ESC
] = Q_KEY_CODE_ESC
,
19 [KEY_1
] = Q_KEY_CODE_1
,
20 [KEY_2
] = Q_KEY_CODE_2
,
21 [KEY_3
] = Q_KEY_CODE_3
,
22 [KEY_4
] = Q_KEY_CODE_4
,
23 [KEY_5
] = Q_KEY_CODE_5
,
24 [KEY_6
] = Q_KEY_CODE_6
,
25 [KEY_7
] = Q_KEY_CODE_7
,
26 [KEY_8
] = Q_KEY_CODE_8
,
27 [KEY_9
] = Q_KEY_CODE_9
,
28 [KEY_0
] = Q_KEY_CODE_0
,
29 [KEY_MINUS
] = Q_KEY_CODE_MINUS
,
30 [KEY_EQUAL
] = Q_KEY_CODE_EQUAL
,
31 [KEY_BACKSPACE
] = Q_KEY_CODE_BACKSPACE
,
32 [KEY_TAB
] = Q_KEY_CODE_TAB
,
33 [KEY_Q
] = Q_KEY_CODE_Q
,
34 [KEY_W
] = Q_KEY_CODE_W
,
35 [KEY_E
] = Q_KEY_CODE_E
,
36 [KEY_R
] = Q_KEY_CODE_R
,
37 [KEY_T
] = Q_KEY_CODE_T
,
38 [KEY_Y
] = Q_KEY_CODE_Y
,
39 [KEY_U
] = Q_KEY_CODE_U
,
40 [KEY_I
] = Q_KEY_CODE_I
,
41 [KEY_O
] = Q_KEY_CODE_O
,
42 [KEY_P
] = Q_KEY_CODE_P
,
43 [KEY_LEFTBRACE
] = Q_KEY_CODE_BRACKET_LEFT
,
44 [KEY_RIGHTBRACE
] = Q_KEY_CODE_BRACKET_RIGHT
,
45 [KEY_ENTER
] = Q_KEY_CODE_RET
,
46 [KEY_LEFTCTRL
] = Q_KEY_CODE_CTRL
,
47 [KEY_A
] = Q_KEY_CODE_A
,
48 [KEY_S
] = Q_KEY_CODE_S
,
49 [KEY_D
] = Q_KEY_CODE_D
,
50 [KEY_F
] = Q_KEY_CODE_F
,
51 [KEY_G
] = Q_KEY_CODE_G
,
52 [KEY_H
] = Q_KEY_CODE_H
,
53 [KEY_J
] = Q_KEY_CODE_J
,
54 [KEY_K
] = Q_KEY_CODE_K
,
55 [KEY_L
] = Q_KEY_CODE_L
,
56 [KEY_SEMICOLON
] = Q_KEY_CODE_SEMICOLON
,
57 [KEY_APOSTROPHE
] = Q_KEY_CODE_APOSTROPHE
,
58 [KEY_GRAVE
] = Q_KEY_CODE_GRAVE_ACCENT
,
59 [KEY_LEFTSHIFT
] = Q_KEY_CODE_SHIFT
,
60 [KEY_BACKSLASH
] = Q_KEY_CODE_BACKSLASH
,
61 [KEY_102ND
] = Q_KEY_CODE_LESS
,
62 [KEY_Z
] = Q_KEY_CODE_Z
,
63 [KEY_X
] = Q_KEY_CODE_X
,
64 [KEY_C
] = Q_KEY_CODE_C
,
65 [KEY_V
] = Q_KEY_CODE_V
,
66 [KEY_B
] = Q_KEY_CODE_B
,
67 [KEY_N
] = Q_KEY_CODE_N
,
68 [KEY_M
] = Q_KEY_CODE_M
,
69 [KEY_COMMA
] = Q_KEY_CODE_COMMA
,
70 [KEY_DOT
] = Q_KEY_CODE_DOT
,
71 [KEY_SLASH
] = Q_KEY_CODE_SLASH
,
72 [KEY_RIGHTSHIFT
] = Q_KEY_CODE_SHIFT_R
,
73 [KEY_LEFTALT
] = Q_KEY_CODE_ALT
,
74 [KEY_SPACE
] = Q_KEY_CODE_SPC
,
75 [KEY_CAPSLOCK
] = Q_KEY_CODE_CAPS_LOCK
,
76 [KEY_F1
] = Q_KEY_CODE_F1
,
77 [KEY_F2
] = Q_KEY_CODE_F2
,
78 [KEY_F3
] = Q_KEY_CODE_F3
,
79 [KEY_F4
] = Q_KEY_CODE_F4
,
80 [KEY_F5
] = Q_KEY_CODE_F5
,
81 [KEY_F6
] = Q_KEY_CODE_F6
,
82 [KEY_F7
] = Q_KEY_CODE_F7
,
83 [KEY_F8
] = Q_KEY_CODE_F8
,
84 [KEY_F9
] = Q_KEY_CODE_F9
,
85 [KEY_F10
] = Q_KEY_CODE_F10
,
86 [KEY_NUMLOCK
] = Q_KEY_CODE_NUM_LOCK
,
87 [KEY_SCROLLLOCK
] = Q_KEY_CODE_SCROLL_LOCK
,
88 [KEY_KP0
] = Q_KEY_CODE_KP_0
,
89 [KEY_KP1
] = Q_KEY_CODE_KP_1
,
90 [KEY_KP2
] = Q_KEY_CODE_KP_2
,
91 [KEY_KP3
] = Q_KEY_CODE_KP_3
,
92 [KEY_KP4
] = Q_KEY_CODE_KP_4
,
93 [KEY_KP5
] = Q_KEY_CODE_KP_5
,
94 [KEY_KP6
] = Q_KEY_CODE_KP_6
,
95 [KEY_KP7
] = Q_KEY_CODE_KP_7
,
96 [KEY_KP8
] = Q_KEY_CODE_KP_8
,
97 [KEY_KP9
] = Q_KEY_CODE_KP_9
,
98 [KEY_KPMINUS
] = Q_KEY_CODE_KP_SUBTRACT
,
99 [KEY_KPPLUS
] = Q_KEY_CODE_KP_ADD
,
100 [KEY_KPDOT
] = Q_KEY_CODE_KP_DECIMAL
,
101 [KEY_KPENTER
] = Q_KEY_CODE_KP_ENTER
,
102 [KEY_KPSLASH
] = Q_KEY_CODE_KP_DIVIDE
,
103 [KEY_KPASTERISK
] = Q_KEY_CODE_KP_MULTIPLY
,
104 [KEY_F11
] = Q_KEY_CODE_F11
,
105 [KEY_F12
] = Q_KEY_CODE_F12
,
106 [KEY_RIGHTCTRL
] = Q_KEY_CODE_CTRL_R
,
107 [KEY_SYSRQ
] = Q_KEY_CODE_SYSRQ
,
108 [KEY_RIGHTALT
] = Q_KEY_CODE_ALT_R
,
109 [KEY_HOME
] = Q_KEY_CODE_HOME
,
110 [KEY_UP
] = Q_KEY_CODE_UP
,
111 [KEY_PAGEUP
] = Q_KEY_CODE_PGUP
,
112 [KEY_LEFT
] = Q_KEY_CODE_LEFT
,
113 [KEY_RIGHT
] = Q_KEY_CODE_RIGHT
,
114 [KEY_END
] = Q_KEY_CODE_END
,
115 [KEY_DOWN
] = Q_KEY_CODE_DOWN
,
116 [KEY_PAGEDOWN
] = Q_KEY_CODE_PGDN
,
117 [KEY_INSERT
] = Q_KEY_CODE_INSERT
,
118 [KEY_DELETE
] = Q_KEY_CODE_DELETE
,
119 [KEY_LEFTMETA
] = Q_KEY_CODE_META_L
,
120 [KEY_RIGHTMETA
] = Q_KEY_CODE_META_R
,
121 [KEY_MENU
] = Q_KEY_CODE_MENU
,
124 static int qemu_input_linux_to_qcode(unsigned int lnx
)
126 assert(lnx
< KEY_CNT
);
127 return linux_to_qcode
[lnx
];
130 typedef struct InputLinux InputLinux
;
139 bool keydown
[KEY_CNT
];
142 QTAILQ_ENTRY(InputLinux
) next
;
145 static QTAILQ_HEAD(, InputLinux
) inputs
= QTAILQ_HEAD_INITIALIZER(inputs
);
147 static void input_linux_toggle_grab(InputLinux
*il
)
149 intptr_t request
= !il
->grab_active
;
153 rc
= ioctl(il
->fd
, EVIOCGRAB
, request
);
157 il
->grab_active
= !il
->grab_active
;
162 QTAILQ_FOREACH(item
, &inputs
, next
) {
163 if (item
== il
|| item
->grab_all
) {
164 /* avoid endless loops */
167 if (item
->grab_active
!= il
->grab_active
) {
168 input_linux_toggle_grab(item
);
173 static void input_linux_event_keyboard(void *opaque
)
175 InputLinux
*il
= opaque
;
176 struct input_event event
;
180 rc
= read(il
->fd
, &event
, sizeof(event
));
181 if (rc
!= sizeof(event
)) {
182 if (rc
< 0 && errno
!= EAGAIN
) {
183 fprintf(stderr
, "%s: read: %s\n", __func__
, strerror(errno
));
184 qemu_set_fd_handler(il
->fd
, NULL
, NULL
, NULL
);
190 switch (event
.type
) {
192 if (event
.value
> 2 || (event
.value
> 1 && !il
->repeat
)) {
194 * ignore autorepeat + unknown key events
195 * 0 == up, 1 == down, 2 == autorepeat, other == undefined
199 /* keep track of key state */
200 if (!il
->keydown
[event
.code
] && event
.value
) {
201 il
->keydown
[event
.code
] = true;
204 if (il
->keydown
[event
.code
] && !event
.value
) {
205 il
->keydown
[event
.code
] = false;
209 /* send event to guest when grab is active */
210 if (il
->grab_active
) {
211 int qcode
= qemu_input_linux_to_qcode(event
.code
);
212 qemu_input_event_send_key_qcode(NULL
, qcode
, event
.value
);
215 /* hotkey -> record switch request ... */
216 if (il
->keydown
[KEY_LEFTCTRL
] &&
217 il
->keydown
[KEY_RIGHTCTRL
]) {
218 il
->grab_request
= true;
222 * ... and do the switch when all keys are lifted, so we
223 * confuse neither guest nor host with keys which seem to
224 * be stuck due to missing key-up events.
226 if (il
->grab_request
&& !il
->keycount
) {
227 il
->grab_request
= false;
228 input_linux_toggle_grab(il
);
235 static void input_linux_event_mouse_button(int button
)
237 qemu_input_queue_btn(NULL
, button
, true);
238 qemu_input_event_sync();
239 qemu_input_queue_btn(NULL
, button
, false);
240 qemu_input_event_sync();
243 static void input_linux_event_mouse(void *opaque
)
245 InputLinux
*il
= opaque
;
246 struct input_event event
;
250 rc
= read(il
->fd
, &event
, sizeof(event
));
251 if (rc
!= sizeof(event
)) {
252 if (rc
< 0 && errno
!= EAGAIN
) {
253 fprintf(stderr
, "%s: read: %s\n", __func__
, strerror(errno
));
254 qemu_set_fd_handler(il
->fd
, NULL
, NULL
, NULL
);
260 /* only send event to guest when grab is active */
261 if (!il
->grab_active
) {
265 switch (event
.type
) {
267 switch (event
.code
) {
269 qemu_input_queue_btn(NULL
, INPUT_BUTTON_LEFT
, event
.value
);
272 qemu_input_queue_btn(NULL
, INPUT_BUTTON_RIGHT
, event
.value
);
275 qemu_input_queue_btn(NULL
, INPUT_BUTTON_MIDDLE
, event
.value
);
278 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_UP
, event
.value
);
281 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_DOWN
,
287 switch (event
.code
) {
289 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, event
.value
);
292 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, event
.value
);
295 il
->wheel
= event
.value
;
300 qemu_input_event_sync();
301 if (il
->wheel
!= 0) {
302 input_linux_event_mouse_button((il
->wheel
> 0)
303 ? INPUT_BUTTON_WHEEL_UP
304 : INPUT_BUTTON_WHEEL_DOWN
);
312 int input_linux_init(void *opaque
, QemuOpts
*opts
, Error
**errp
)
314 InputLinux
*il
= g_new0(InputLinux
, 1);
318 il
->evdev
= qemu_opt_get(opts
, "evdev");
319 il
->grab_all
= qemu_opt_get_bool(opts
, "grab-all", false);
320 il
->repeat
= qemu_opt_get_bool(opts
, "repeat", false);
323 error_setg(errp
, "no input device specified");
327 il
->fd
= open(il
->evdev
, O_RDWR
);
329 error_setg_file_open(errp
, errno
, il
->evdev
);
332 qemu_set_nonblock(il
->fd
);
334 rc
= ioctl(il
->fd
, EVIOCGVERSION
, &ver
);
336 error_setg(errp
, "%s: is not an evdev device", il
->evdev
);
340 rc
= ioctl(il
->fd
, EVIOCGBIT(0, sizeof(evtmap
)), &evtmap
);
342 if (evtmap
& (1 << EV_REL
)) {
343 /* has relative axis -> assume mouse */
344 qemu_set_fd_handler(il
->fd
, input_linux_event_mouse
, NULL
, il
);
345 } else if (evtmap
& (1 << EV_ABS
)) {
346 /* has absolute axis -> not supported */
347 error_setg(errp
, "tablet/touchscreen not supported");
349 } else if (evtmap
& (1 << EV_KEY
)) {
350 /* has keys/buttons (and no axis) -> assume keyboard */
351 qemu_set_fd_handler(il
->fd
, input_linux_event_keyboard
, NULL
, il
);
353 /* Huh? What is this? */
354 error_setg(errp
, "unknown kind of input device");
357 input_linux_toggle_grab(il
);
358 QTAILQ_INSERT_TAIL(&inputs
, il
, next
);
368 static QemuOptsList qemu_input_linux_opts
= {
369 .name
= "input-linux",
370 .head
= QTAILQ_HEAD_INITIALIZER(qemu_input_linux_opts
.head
),
371 .implied_opt_name
= "evdev",
375 .type
= QEMU_OPT_STRING
,
378 .type
= QEMU_OPT_BOOL
,
381 .type
= QEMU_OPT_BOOL
,
383 { /* end of list */ }
387 static void input_linux_register_config(void)
389 qemu_add_opts(&qemu_input_linux_opts
);
391 machine_init(input_linux_register_config
);