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 "qapi/error.h"
9 #include "qemu/config-file.h"
10 #include "qemu/module.h"
11 #include "qemu/sockets.h"
12 #include "sysemu/sysemu.h"
14 #include "qom/object_interfaces.h"
15 #include "sysemu/iothread.h"
16 #include "block/aio.h"
18 #include <sys/ioctl.h>
19 #include "standard-headers/linux/input.h"
21 static bool linux_is_button(unsigned int lnx
)
26 if (lnx
>= 0x160 && lnx
< 0x2c0) {
32 #define TYPE_INPUT_LINUX "input-linux"
33 #define INPUT_LINUX(obj) \
34 OBJECT_CHECK(InputLinux, (obj), TYPE_INPUT_LINUX)
35 #define INPUT_LINUX_GET_CLASS(obj) \
36 OBJECT_GET_CLASS(InputLinuxClass, (obj), TYPE_INPUT_LINUX)
37 #define INPUT_LINUX_CLASS(klass) \
38 OBJECT_CLASS_CHECK(InputLinuxClass, (klass), TYPE_INPUT_LINUX)
40 typedef struct InputLinux InputLinux
;
41 typedef struct InputLinuxClass InputLinuxClass
;
52 bool keydown
[KEY_CNT
];
65 struct input_event event
;
68 enum GrabToggleKeys grab_toggle
;
70 QTAILQ_ENTRY(InputLinux
) next
;
73 struct InputLinuxClass
{
74 ObjectClass parent_class
;
77 static QTAILQ_HEAD(, InputLinux
) inputs
= QTAILQ_HEAD_INITIALIZER(inputs
);
79 static void input_linux_toggle_grab(InputLinux
*il
)
81 intptr_t request
= !il
->grab_active
;
85 rc
= ioctl(il
->fd
, EVIOCGRAB
, request
);
89 il
->grab_active
= !il
->grab_active
;
94 QTAILQ_FOREACH(item
, &inputs
, next
) {
95 if (item
== il
|| item
->grab_all
) {
96 /* avoid endless loops */
99 if (item
->grab_active
!= il
->grab_active
) {
100 input_linux_toggle_grab(item
);
105 static bool input_linux_check_toggle(InputLinux
*il
)
107 switch (il
->grab_toggle
) {
108 case GRAB_TOGGLE_KEYS_CTRL_CTRL
:
109 return il
->keydown
[KEY_LEFTCTRL
] &&
110 il
->keydown
[KEY_RIGHTCTRL
];
112 case GRAB_TOGGLE_KEYS_ALT_ALT
:
113 return il
->keydown
[KEY_LEFTALT
] &&
114 il
->keydown
[KEY_RIGHTALT
];
116 case GRAB_TOGGLE_KEYS_META_META
:
117 return il
->keydown
[KEY_LEFTMETA
] &&
118 il
->keydown
[KEY_RIGHTMETA
];
120 case GRAB_TOGGLE_KEYS_SCROLLLOCK
:
121 return il
->keydown
[KEY_SCROLLLOCK
];
123 case GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK
:
124 return (il
->keydown
[KEY_LEFTCTRL
] ||
125 il
->keydown
[KEY_RIGHTCTRL
]) &&
126 il
->keydown
[KEY_SCROLLLOCK
];
128 case GRAB_TOGGLE_KEYS__MAX
:
129 /* avoid gcc error */
135 static bool input_linux_should_skip(InputLinux
*il
,
136 struct input_event
*event
)
138 return (il
->grab_toggle
== GRAB_TOGGLE_KEYS_SCROLLLOCK
||
139 il
->grab_toggle
== GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK
) &&
140 event
->code
== KEY_SCROLLLOCK
;
143 static void input_linux_handle_keyboard(InputLinux
*il
,
144 struct input_event
*event
)
146 if (event
->type
== EV_KEY
) {
147 if (event
->value
> 2 || (event
->value
> 1 && !il
->repeat
)) {
149 * ignore autorepeat + unknown key events
150 * 0 == up, 1 == down, 2 == autorepeat, other == undefined
154 if (event
->code
>= KEY_CNT
) {
156 * Should not happen. But better safe than sorry,
157 * and we make Coverity happy too.
162 /* keep track of key state */
163 if (!il
->keydown
[event
->code
] && event
->value
) {
164 il
->keydown
[event
->code
] = true;
167 if (il
->keydown
[event
->code
] && !event
->value
) {
168 il
->keydown
[event
->code
] = false;
172 /* send event to guest when grab is active */
173 if (il
->grab_active
&& !input_linux_should_skip(il
, event
)) {
174 int qcode
= qemu_input_linux_to_qcode(event
->code
);
175 qemu_input_event_send_key_qcode(NULL
, qcode
, event
->value
);
178 /* hotkey -> record switch request ... */
179 if (input_linux_check_toggle(il
)) {
180 il
->grab_request
= true;
184 * ... and do the switch when all keys are lifted, so we
185 * confuse neither guest nor host with keys which seem to
186 * be stuck due to missing key-up events.
188 if (il
->grab_request
&& !il
->keycount
) {
189 il
->grab_request
= false;
190 input_linux_toggle_grab(il
);
195 static void input_linux_event_mouse_button(int button
)
197 qemu_input_queue_btn(NULL
, button
, true);
198 qemu_input_event_sync();
199 qemu_input_queue_btn(NULL
, button
, false);
200 qemu_input_event_sync();
203 static void input_linux_handle_mouse(InputLinux
*il
, struct input_event
*event
)
205 if (!il
->grab_active
) {
209 switch (event
->type
) {
211 switch (event
->code
) {
213 qemu_input_queue_btn(NULL
, INPUT_BUTTON_LEFT
, event
->value
);
216 qemu_input_queue_btn(NULL
, INPUT_BUTTON_RIGHT
, event
->value
);
219 qemu_input_queue_btn(NULL
, INPUT_BUTTON_MIDDLE
, event
->value
);
222 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_UP
, event
->value
);
225 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_DOWN
,
229 qemu_input_queue_btn(NULL
, INPUT_BUTTON_SIDE
, event
->value
);
232 qemu_input_queue_btn(NULL
, INPUT_BUTTON_EXTRA
, event
->value
);
237 switch (event
->code
) {
239 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, event
->value
);
242 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, event
->value
);
245 il
->wheel
= event
->value
;
250 switch (event
->code
) {
252 qemu_input_queue_abs(NULL
, INPUT_AXIS_X
, event
->value
,
253 il
->abs_x_min
, il
->abs_x_max
);
256 qemu_input_queue_abs(NULL
, INPUT_AXIS_Y
, event
->value
,
257 il
->abs_y_min
, il
->abs_y_max
);
262 qemu_input_event_sync();
263 if (il
->wheel
!= 0) {
264 input_linux_event_mouse_button((il
->wheel
> 0)
265 ? INPUT_BUTTON_WHEEL_UP
266 : INPUT_BUTTON_WHEEL_DOWN
);
273 static void input_linux_event(void *opaque
)
275 InputLinux
*il
= opaque
;
278 uint8_t *p
= (uint8_t *)&il
->event
;
281 read_size
= sizeof(il
->event
) - il
->read_offset
;
282 rc
= read(il
->fd
, &p
[il
->read_offset
], read_size
);
283 if (rc
!= read_size
) {
284 if (rc
< 0 && errno
!= EAGAIN
) {
285 fprintf(stderr
, "%s: read: %s\n", __func__
, strerror(errno
));
286 qemu_set_fd_handler(il
->fd
, NULL
, NULL
, NULL
);
289 il
->read_offset
+= rc
;
296 input_linux_handle_keyboard(il
, &il
->event
);
298 if ((il
->has_rel_x
|| il
->has_abs_x
) && il
->num_btns
) {
299 input_linux_handle_mouse(il
, &il
->event
);
304 static void input_linux_complete(UserCreatable
*uc
, Error
**errp
)
306 InputLinux
*il
= INPUT_LINUX(uc
);
307 uint8_t evtmap
, relmap
, absmap
;
308 uint8_t keymap
[KEY_CNT
/ 8], keystate
[KEY_CNT
/ 8];
311 struct input_absinfo absinfo
;
314 error_setg(errp
, "no input device specified");
318 il
->fd
= open(il
->evdev
, O_RDWR
);
320 error_setg_file_open(errp
, errno
, il
->evdev
);
323 qemu_set_nonblock(il
->fd
);
325 rc
= ioctl(il
->fd
, EVIOCGVERSION
, &ver
);
327 error_setg(errp
, "%s: is not an evdev device", il
->evdev
);
331 rc
= ioctl(il
->fd
, EVIOCGBIT(0, sizeof(evtmap
)), &evtmap
);
333 error_setg(errp
, "%s: failed to read event bits", il
->evdev
);
337 if (evtmap
& (1 << EV_REL
)) {
339 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_REL
, sizeof(relmap
)), &relmap
);
340 if (relmap
& (1 << REL_X
)) {
341 il
->has_rel_x
= true;
345 if (evtmap
& (1 << EV_ABS
)) {
347 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_ABS
, sizeof(absmap
)), &absmap
);
348 if (absmap
& (1 << ABS_X
)) {
349 il
->has_abs_x
= true;
350 rc
= ioctl(il
->fd
, EVIOCGABS(ABS_X
), &absinfo
);
351 il
->abs_x_min
= absinfo
.minimum
;
352 il
->abs_x_max
= absinfo
.maximum
;
353 rc
= ioctl(il
->fd
, EVIOCGABS(ABS_Y
), &absinfo
);
354 il
->abs_y_min
= absinfo
.minimum
;
355 il
->abs_y_max
= absinfo
.maximum
;
359 if (evtmap
& (1 << EV_KEY
)) {
360 memset(keymap
, 0, sizeof(keymap
));
361 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_KEY
, sizeof(keymap
)), keymap
);
362 rc
= ioctl(il
->fd
, EVIOCGKEY(sizeof(keystate
)), keystate
);
363 for (i
= 0; i
< KEY_CNT
; i
++) {
364 if (keymap
[i
/ 8] & (1 << (i
% 8))) {
365 if (linux_is_button(i
)) {
370 if (keystate
[i
/ 8] & (1 << (i
% 8))) {
371 il
->keydown
[i
] = true;
378 qemu_set_fd_handler(il
->fd
, input_linux_event
, NULL
, il
);
380 /* delay grab until all keys are released */
381 il
->grab_request
= true;
383 input_linux_toggle_grab(il
);
385 QTAILQ_INSERT_TAIL(&inputs
, il
, next
);
386 il
->initialized
= true;
394 static void input_linux_instance_finalize(Object
*obj
)
396 InputLinux
*il
= INPUT_LINUX(obj
);
398 if (il
->initialized
) {
399 QTAILQ_REMOVE(&inputs
, il
, next
);
405 static char *input_linux_get_evdev(Object
*obj
, Error
**errp
)
407 InputLinux
*il
= INPUT_LINUX(obj
);
409 return g_strdup(il
->evdev
);
412 static void input_linux_set_evdev(Object
*obj
, const char *value
,
415 InputLinux
*il
= INPUT_LINUX(obj
);
418 error_setg(errp
, "evdev property already set");
421 il
->evdev
= g_strdup(value
);
424 static bool input_linux_get_grab_all(Object
*obj
, Error
**errp
)
426 InputLinux
*il
= INPUT_LINUX(obj
);
431 static void input_linux_set_grab_all(Object
*obj
, bool value
,
434 InputLinux
*il
= INPUT_LINUX(obj
);
436 il
->grab_all
= value
;
439 static bool input_linux_get_repeat(Object
*obj
, Error
**errp
)
441 InputLinux
*il
= INPUT_LINUX(obj
);
446 static void input_linux_set_repeat(Object
*obj
, bool value
,
449 InputLinux
*il
= INPUT_LINUX(obj
);
454 static int input_linux_get_grab_toggle(Object
*obj
, Error
**errp
)
456 InputLinux
*il
= INPUT_LINUX(obj
);
458 return il
->grab_toggle
;
461 static void input_linux_set_grab_toggle(Object
*obj
, int value
,
464 InputLinux
*il
= INPUT_LINUX(obj
);
466 il
->grab_toggle
= value
;
469 static void input_linux_instance_init(Object
*obj
)
471 object_property_add_str(obj
, "evdev",
472 input_linux_get_evdev
,
473 input_linux_set_evdev
, NULL
);
474 object_property_add_bool(obj
, "grab_all",
475 input_linux_get_grab_all
,
476 input_linux_set_grab_all
, NULL
);
477 object_property_add_bool(obj
, "repeat",
478 input_linux_get_repeat
,
479 input_linux_set_repeat
, NULL
);
480 object_property_add_enum(obj
, "grab-toggle", "GrabToggleKeys",
481 &GrabToggleKeys_lookup
,
482 input_linux_get_grab_toggle
,
483 input_linux_set_grab_toggle
, NULL
);
486 static void input_linux_class_init(ObjectClass
*oc
, void *data
)
488 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
490 ucc
->complete
= input_linux_complete
;
493 static const TypeInfo input_linux_info
= {
494 .name
= TYPE_INPUT_LINUX
,
495 .parent
= TYPE_OBJECT
,
496 .class_size
= sizeof(InputLinuxClass
),
497 .class_init
= input_linux_class_init
,
498 .instance_size
= sizeof(InputLinux
),
499 .instance_init
= input_linux_instance_init
,
500 .instance_finalize
= input_linux_instance_finalize
,
501 .interfaces
= (InterfaceInfo
[]) {
502 { TYPE_USER_CREATABLE
},
507 static void register_types(void)
509 type_register_static(&input_linux_info
);
512 type_init(register_types
);