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/main-loop.h"
11 #include "qemu/module.h"
12 #include "qemu/sockets.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_SHIFT_SHIFT
:
117 return il
->keydown
[KEY_LEFTSHIFT
] &&
118 il
->keydown
[KEY_RIGHTSHIFT
];
120 case GRAB_TOGGLE_KEYS_META_META
:
121 return il
->keydown
[KEY_LEFTMETA
] &&
122 il
->keydown
[KEY_RIGHTMETA
];
124 case GRAB_TOGGLE_KEYS_SCROLLLOCK
:
125 return il
->keydown
[KEY_SCROLLLOCK
];
127 case GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK
:
128 return (il
->keydown
[KEY_LEFTCTRL
] ||
129 il
->keydown
[KEY_RIGHTCTRL
]) &&
130 il
->keydown
[KEY_SCROLLLOCK
];
132 case GRAB_TOGGLE_KEYS__MAX
:
133 /* avoid gcc error */
139 static bool input_linux_should_skip(InputLinux
*il
,
140 struct input_event
*event
)
142 return (il
->grab_toggle
== GRAB_TOGGLE_KEYS_SCROLLLOCK
||
143 il
->grab_toggle
== GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK
) &&
144 event
->code
== KEY_SCROLLLOCK
;
147 static void input_linux_handle_keyboard(InputLinux
*il
,
148 struct input_event
*event
)
150 if (event
->type
== EV_KEY
) {
151 if (event
->value
> 2 || (event
->value
> 1 && !il
->repeat
)) {
153 * ignore autorepeat + unknown key events
154 * 0 == up, 1 == down, 2 == autorepeat, other == undefined
158 if (event
->code
>= KEY_CNT
) {
160 * Should not happen. But better safe than sorry,
161 * and we make Coverity happy too.
166 /* keep track of key state */
167 if (!il
->keydown
[event
->code
] && event
->value
) {
168 il
->keydown
[event
->code
] = true;
171 if (il
->keydown
[event
->code
] && !event
->value
) {
172 il
->keydown
[event
->code
] = false;
176 /* send event to guest when grab is active */
177 if (il
->grab_active
&& !input_linux_should_skip(il
, event
)) {
178 int qcode
= qemu_input_linux_to_qcode(event
->code
);
179 qemu_input_event_send_key_qcode(NULL
, qcode
, event
->value
);
182 /* hotkey -> record switch request ... */
183 if (input_linux_check_toggle(il
)) {
184 il
->grab_request
= true;
188 * ... and do the switch when all keys are lifted, so we
189 * confuse neither guest nor host with keys which seem to
190 * be stuck due to missing key-up events.
192 if (il
->grab_request
&& !il
->keycount
) {
193 il
->grab_request
= false;
194 input_linux_toggle_grab(il
);
199 static void input_linux_event_mouse_button(int button
)
201 qemu_input_queue_btn(NULL
, button
, true);
202 qemu_input_event_sync();
203 qemu_input_queue_btn(NULL
, button
, false);
204 qemu_input_event_sync();
207 static void input_linux_handle_mouse(InputLinux
*il
, struct input_event
*event
)
209 if (!il
->grab_active
) {
213 switch (event
->type
) {
215 switch (event
->code
) {
217 qemu_input_queue_btn(NULL
, INPUT_BUTTON_LEFT
, event
->value
);
220 qemu_input_queue_btn(NULL
, INPUT_BUTTON_RIGHT
, event
->value
);
223 qemu_input_queue_btn(NULL
, INPUT_BUTTON_MIDDLE
, event
->value
);
226 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_UP
, event
->value
);
229 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_DOWN
,
233 qemu_input_queue_btn(NULL
, INPUT_BUTTON_SIDE
, event
->value
);
236 qemu_input_queue_btn(NULL
, INPUT_BUTTON_EXTRA
, event
->value
);
241 switch (event
->code
) {
243 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, event
->value
);
246 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, event
->value
);
249 il
->wheel
= event
->value
;
254 switch (event
->code
) {
256 qemu_input_queue_abs(NULL
, INPUT_AXIS_X
, event
->value
,
257 il
->abs_x_min
, il
->abs_x_max
);
260 qemu_input_queue_abs(NULL
, INPUT_AXIS_Y
, event
->value
,
261 il
->abs_y_min
, il
->abs_y_max
);
266 qemu_input_event_sync();
267 if (il
->wheel
!= 0) {
268 input_linux_event_mouse_button((il
->wheel
> 0)
269 ? INPUT_BUTTON_WHEEL_UP
270 : INPUT_BUTTON_WHEEL_DOWN
);
277 static void input_linux_event(void *opaque
)
279 InputLinux
*il
= opaque
;
282 uint8_t *p
= (uint8_t *)&il
->event
;
285 read_size
= sizeof(il
->event
) - il
->read_offset
;
286 rc
= read(il
->fd
, &p
[il
->read_offset
], read_size
);
287 if (rc
!= read_size
) {
288 if (rc
< 0 && errno
!= EAGAIN
) {
289 fprintf(stderr
, "%s: read: %s\n", __func__
, strerror(errno
));
290 qemu_set_fd_handler(il
->fd
, NULL
, NULL
, NULL
);
293 il
->read_offset
+= rc
;
300 input_linux_handle_keyboard(il
, &il
->event
);
302 if ((il
->has_rel_x
|| il
->has_abs_x
) && il
->num_btns
) {
303 input_linux_handle_mouse(il
, &il
->event
);
308 static void input_linux_complete(UserCreatable
*uc
, Error
**errp
)
310 InputLinux
*il
= INPUT_LINUX(uc
);
311 uint8_t evtmap
, relmap
, absmap
;
312 uint8_t keymap
[KEY_CNT
/ 8], keystate
[KEY_CNT
/ 8];
315 struct input_absinfo absinfo
;
318 error_setg(errp
, "no input device specified");
322 il
->fd
= open(il
->evdev
, O_RDWR
);
324 error_setg_file_open(errp
, errno
, il
->evdev
);
327 qemu_set_nonblock(il
->fd
);
329 rc
= ioctl(il
->fd
, EVIOCGVERSION
, &ver
);
331 error_setg(errp
, "%s: is not an evdev device", il
->evdev
);
335 rc
= ioctl(il
->fd
, EVIOCGBIT(0, sizeof(evtmap
)), &evtmap
);
337 goto err_read_event_bits
;
340 if (evtmap
& (1 << EV_REL
)) {
342 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_REL
, sizeof(relmap
)), &relmap
);
344 goto err_read_event_bits
;
346 if (relmap
& (1 << REL_X
)) {
347 il
->has_rel_x
= true;
351 if (evtmap
& (1 << EV_ABS
)) {
353 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_ABS
, sizeof(absmap
)), &absmap
);
355 goto err_read_event_bits
;
357 if (absmap
& (1 << ABS_X
)) {
358 il
->has_abs_x
= true;
359 rc
= ioctl(il
->fd
, EVIOCGABS(ABS_X
), &absinfo
);
361 error_setg(errp
, "%s: failed to get get absolute X value",
365 il
->abs_x_min
= absinfo
.minimum
;
366 il
->abs_x_max
= absinfo
.maximum
;
367 rc
= ioctl(il
->fd
, EVIOCGABS(ABS_Y
), &absinfo
);
369 error_setg(errp
, "%s: failed to get get absolute Y value",
373 il
->abs_y_min
= absinfo
.minimum
;
374 il
->abs_y_max
= absinfo
.maximum
;
378 if (evtmap
& (1 << EV_KEY
)) {
379 memset(keymap
, 0, sizeof(keymap
));
380 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_KEY
, sizeof(keymap
)), keymap
);
382 goto err_read_event_bits
;
384 rc
= ioctl(il
->fd
, EVIOCGKEY(sizeof(keystate
)), keystate
);
386 error_setg(errp
, "%s: failed to get global key state", il
->evdev
);
389 for (i
= 0; i
< KEY_CNT
; i
++) {
390 if (keymap
[i
/ 8] & (1 << (i
% 8))) {
391 if (linux_is_button(i
)) {
396 if (keystate
[i
/ 8] & (1 << (i
% 8))) {
397 il
->keydown
[i
] = true;
404 qemu_set_fd_handler(il
->fd
, input_linux_event
, NULL
, il
);
406 /* delay grab until all keys are released */
407 il
->grab_request
= true;
409 input_linux_toggle_grab(il
);
411 QTAILQ_INSERT_TAIL(&inputs
, il
, next
);
412 il
->initialized
= true;
416 error_setg(errp
, "%s: failed to read event bits", il
->evdev
);
423 static void input_linux_instance_finalize(Object
*obj
)
425 InputLinux
*il
= INPUT_LINUX(obj
);
427 if (il
->initialized
) {
428 QTAILQ_REMOVE(&inputs
, il
, next
);
434 static char *input_linux_get_evdev(Object
*obj
, Error
**errp
)
436 InputLinux
*il
= INPUT_LINUX(obj
);
438 return g_strdup(il
->evdev
);
441 static void input_linux_set_evdev(Object
*obj
, const char *value
,
444 InputLinux
*il
= INPUT_LINUX(obj
);
447 error_setg(errp
, "evdev property already set");
450 il
->evdev
= g_strdup(value
);
453 static bool input_linux_get_grab_all(Object
*obj
, Error
**errp
)
455 InputLinux
*il
= INPUT_LINUX(obj
);
460 static void input_linux_set_grab_all(Object
*obj
, bool value
,
463 InputLinux
*il
= INPUT_LINUX(obj
);
465 il
->grab_all
= value
;
468 static bool input_linux_get_repeat(Object
*obj
, Error
**errp
)
470 InputLinux
*il
= INPUT_LINUX(obj
);
475 static void input_linux_set_repeat(Object
*obj
, bool value
,
478 InputLinux
*il
= INPUT_LINUX(obj
);
483 static int input_linux_get_grab_toggle(Object
*obj
, Error
**errp
)
485 InputLinux
*il
= INPUT_LINUX(obj
);
487 return il
->grab_toggle
;
490 static void input_linux_set_grab_toggle(Object
*obj
, int value
,
493 InputLinux
*il
= INPUT_LINUX(obj
);
495 il
->grab_toggle
= value
;
498 static void input_linux_instance_init(Object
*obj
)
500 object_property_add_str(obj
, "evdev",
501 input_linux_get_evdev
,
502 input_linux_set_evdev
, NULL
);
503 object_property_add_bool(obj
, "grab_all",
504 input_linux_get_grab_all
,
505 input_linux_set_grab_all
, NULL
);
506 object_property_add_bool(obj
, "repeat",
507 input_linux_get_repeat
,
508 input_linux_set_repeat
, NULL
);
509 object_property_add_enum(obj
, "grab-toggle", "GrabToggleKeys",
510 &GrabToggleKeys_lookup
,
511 input_linux_get_grab_toggle
,
512 input_linux_set_grab_toggle
, NULL
);
515 static void input_linux_class_init(ObjectClass
*oc
, void *data
)
517 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
519 ucc
->complete
= input_linux_complete
;
522 static const TypeInfo input_linux_info
= {
523 .name
= TYPE_INPUT_LINUX
,
524 .parent
= TYPE_OBJECT
,
525 .class_size
= sizeof(InputLinuxClass
),
526 .class_init
= input_linux_class_init
,
527 .instance_size
= sizeof(InputLinux
),
528 .instance_init
= input_linux_instance_init
,
529 .instance_finalize
= input_linux_instance_finalize
,
530 .interfaces
= (InterfaceInfo
[]) {
531 { TYPE_USER_CREATABLE
},
536 static void register_types(void)
538 type_register_static(&input_linux_info
);
541 type_init(register_types
);