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"
20 #include "qom/object.h"
22 static bool linux_is_button(unsigned int lnx
)
27 if (lnx
>= 0x160 && lnx
< 0x2c0) {
33 #define TYPE_INPUT_LINUX "input-linux"
34 OBJECT_DECLARE_SIMPLE_TYPE(InputLinux
,
47 bool keydown
[KEY_CNT
];
60 struct input_event event
;
63 enum GrabToggleKeys grab_toggle
;
65 QTAILQ_ENTRY(InputLinux
) next
;
69 static QTAILQ_HEAD(, InputLinux
) inputs
= QTAILQ_HEAD_INITIALIZER(inputs
);
71 static void input_linux_toggle_grab(InputLinux
*il
)
73 intptr_t request
= !il
->grab_active
;
77 rc
= ioctl(il
->fd
, EVIOCGRAB
, request
);
81 il
->grab_active
= !il
->grab_active
;
86 QTAILQ_FOREACH(item
, &inputs
, next
) {
87 if (item
== il
|| item
->grab_all
) {
88 /* avoid endless loops */
91 if (item
->grab_active
!= il
->grab_active
) {
92 input_linux_toggle_grab(item
);
97 static bool input_linux_check_toggle(InputLinux
*il
)
99 switch (il
->grab_toggle
) {
100 case GRAB_TOGGLE_KEYS_CTRL_CTRL
:
101 return il
->keydown
[KEY_LEFTCTRL
] &&
102 il
->keydown
[KEY_RIGHTCTRL
];
104 case GRAB_TOGGLE_KEYS_ALT_ALT
:
105 return il
->keydown
[KEY_LEFTALT
] &&
106 il
->keydown
[KEY_RIGHTALT
];
108 case GRAB_TOGGLE_KEYS_SHIFT_SHIFT
:
109 return il
->keydown
[KEY_LEFTSHIFT
] &&
110 il
->keydown
[KEY_RIGHTSHIFT
];
112 case GRAB_TOGGLE_KEYS_META_META
:
113 return il
->keydown
[KEY_LEFTMETA
] &&
114 il
->keydown
[KEY_RIGHTMETA
];
116 case GRAB_TOGGLE_KEYS_SCROLLLOCK
:
117 return il
->keydown
[KEY_SCROLLLOCK
];
119 case GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK
:
120 return (il
->keydown
[KEY_LEFTCTRL
] ||
121 il
->keydown
[KEY_RIGHTCTRL
]) &&
122 il
->keydown
[KEY_SCROLLLOCK
];
124 case GRAB_TOGGLE_KEYS__MAX
:
125 /* avoid gcc error */
131 static bool input_linux_should_skip(InputLinux
*il
,
132 struct input_event
*event
)
134 return (il
->grab_toggle
== GRAB_TOGGLE_KEYS_SCROLLLOCK
||
135 il
->grab_toggle
== GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK
) &&
136 event
->code
== KEY_SCROLLLOCK
;
139 static void input_linux_handle_keyboard(InputLinux
*il
,
140 struct input_event
*event
)
142 if (event
->type
== EV_KEY
) {
143 if (event
->value
> 2 || (event
->value
> 1 && !il
->repeat
)) {
145 * ignore autorepeat + unknown key events
146 * 0 == up, 1 == down, 2 == autorepeat, other == undefined
150 if (event
->code
>= KEY_CNT
) {
152 * Should not happen. But better safe than sorry,
153 * and we make Coverity happy too.
158 /* keep track of key state */
159 if (!il
->keydown
[event
->code
] && event
->value
) {
160 il
->keydown
[event
->code
] = true;
163 if (il
->keydown
[event
->code
] && !event
->value
) {
164 il
->keydown
[event
->code
] = false;
168 /* send event to guest when grab is active */
169 if (il
->grab_active
&& !input_linux_should_skip(il
, event
)) {
170 int qcode
= qemu_input_linux_to_qcode(event
->code
);
171 qemu_input_event_send_key_qcode(NULL
, qcode
, event
->value
);
174 /* hotkey -> record switch request ... */
175 if (input_linux_check_toggle(il
)) {
176 il
->grab_request
= true;
180 * ... and do the switch when all keys are lifted, so we
181 * confuse neither guest nor host with keys which seem to
182 * be stuck due to missing key-up events.
184 if (il
->grab_request
&& !il
->keycount
) {
185 il
->grab_request
= false;
186 input_linux_toggle_grab(il
);
191 static void input_linux_event_mouse_button(int button
)
193 qemu_input_queue_btn(NULL
, button
, true);
194 qemu_input_event_sync();
195 qemu_input_queue_btn(NULL
, button
, false);
196 qemu_input_event_sync();
199 static void input_linux_handle_mouse(InputLinux
*il
, struct input_event
*event
)
201 if (!il
->grab_active
) {
205 switch (event
->type
) {
207 switch (event
->code
) {
209 qemu_input_queue_btn(NULL
, INPUT_BUTTON_LEFT
, event
->value
);
212 qemu_input_queue_btn(NULL
, INPUT_BUTTON_RIGHT
, event
->value
);
215 qemu_input_queue_btn(NULL
, INPUT_BUTTON_MIDDLE
, event
->value
);
218 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_UP
, event
->value
);
221 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_DOWN
,
225 qemu_input_queue_btn(NULL
, INPUT_BUTTON_SIDE
, event
->value
);
228 qemu_input_queue_btn(NULL
, INPUT_BUTTON_EXTRA
, event
->value
);
233 switch (event
->code
) {
235 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, event
->value
);
238 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, event
->value
);
241 il
->wheel
= event
->value
;
246 switch (event
->code
) {
248 qemu_input_queue_abs(NULL
, INPUT_AXIS_X
, event
->value
,
249 il
->abs_x_min
, il
->abs_x_max
);
252 qemu_input_queue_abs(NULL
, INPUT_AXIS_Y
, event
->value
,
253 il
->abs_y_min
, il
->abs_y_max
);
258 qemu_input_event_sync();
259 if (il
->wheel
!= 0) {
260 input_linux_event_mouse_button((il
->wheel
> 0)
261 ? INPUT_BUTTON_WHEEL_UP
262 : INPUT_BUTTON_WHEEL_DOWN
);
269 static void input_linux_event(void *opaque
)
271 InputLinux
*il
= opaque
;
274 uint8_t *p
= (uint8_t *)&il
->event
;
277 read_size
= sizeof(il
->event
) - il
->read_offset
;
278 rc
= read(il
->fd
, &p
[il
->read_offset
], read_size
);
279 if (rc
!= read_size
) {
280 if (rc
< 0 && errno
!= EAGAIN
) {
281 fprintf(stderr
, "%s: read: %s\n", __func__
, strerror(errno
));
282 qemu_set_fd_handler(il
->fd
, NULL
, NULL
, NULL
);
285 il
->read_offset
+= rc
;
292 input_linux_handle_keyboard(il
, &il
->event
);
294 if ((il
->has_rel_x
|| il
->has_abs_x
) && il
->num_btns
) {
295 input_linux_handle_mouse(il
, &il
->event
);
300 static void input_linux_complete(UserCreatable
*uc
, Error
**errp
)
302 InputLinux
*il
= INPUT_LINUX(uc
);
303 uint8_t evtmap
, relmap
, absmap
;
304 uint8_t keymap
[KEY_CNT
/ 8], keystate
[KEY_CNT
/ 8];
307 struct input_absinfo absinfo
;
310 error_setg(errp
, "no input device specified");
314 il
->fd
= open(il
->evdev
, O_RDWR
);
316 error_setg_file_open(errp
, errno
, il
->evdev
);
319 qemu_set_nonblock(il
->fd
);
321 rc
= ioctl(il
->fd
, EVIOCGVERSION
, &ver
);
323 error_setg(errp
, "%s: is not an evdev device", il
->evdev
);
327 rc
= ioctl(il
->fd
, EVIOCGBIT(0, sizeof(evtmap
)), &evtmap
);
329 goto err_read_event_bits
;
332 if (evtmap
& (1 << EV_REL
)) {
334 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_REL
, sizeof(relmap
)), &relmap
);
336 goto err_read_event_bits
;
338 if (relmap
& (1 << REL_X
)) {
339 il
->has_rel_x
= true;
343 if (evtmap
& (1 << EV_ABS
)) {
345 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_ABS
, sizeof(absmap
)), &absmap
);
347 goto err_read_event_bits
;
349 if (absmap
& (1 << ABS_X
)) {
350 il
->has_abs_x
= true;
351 rc
= ioctl(il
->fd
, EVIOCGABS(ABS_X
), &absinfo
);
353 error_setg(errp
, "%s: failed to get get absolute X value",
357 il
->abs_x_min
= absinfo
.minimum
;
358 il
->abs_x_max
= absinfo
.maximum
;
359 rc
= ioctl(il
->fd
, EVIOCGABS(ABS_Y
), &absinfo
);
361 error_setg(errp
, "%s: failed to get get absolute Y value",
365 il
->abs_y_min
= absinfo
.minimum
;
366 il
->abs_y_max
= absinfo
.maximum
;
370 if (evtmap
& (1 << EV_KEY
)) {
371 memset(keymap
, 0, sizeof(keymap
));
372 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_KEY
, sizeof(keymap
)), keymap
);
374 goto err_read_event_bits
;
376 rc
= ioctl(il
->fd
, EVIOCGKEY(sizeof(keystate
)), keystate
);
378 error_setg(errp
, "%s: failed to get global key state", il
->evdev
);
381 for (i
= 0; i
< KEY_CNT
; i
++) {
382 if (keymap
[i
/ 8] & (1 << (i
% 8))) {
383 if (linux_is_button(i
)) {
388 if (keystate
[i
/ 8] & (1 << (i
% 8))) {
389 il
->keydown
[i
] = true;
396 qemu_set_fd_handler(il
->fd
, input_linux_event
, NULL
, il
);
398 /* delay grab until all keys are released */
399 il
->grab_request
= true;
401 input_linux_toggle_grab(il
);
403 QTAILQ_INSERT_TAIL(&inputs
, il
, next
);
404 il
->initialized
= true;
408 error_setg(errp
, "%s: failed to read event bits", il
->evdev
);
415 static void input_linux_instance_finalize(Object
*obj
)
417 InputLinux
*il
= INPUT_LINUX(obj
);
419 if (il
->initialized
) {
420 QTAILQ_REMOVE(&inputs
, il
, next
);
426 static char *input_linux_get_evdev(Object
*obj
, Error
**errp
)
428 InputLinux
*il
= INPUT_LINUX(obj
);
430 return g_strdup(il
->evdev
);
433 static void input_linux_set_evdev(Object
*obj
, const char *value
,
436 InputLinux
*il
= INPUT_LINUX(obj
);
439 error_setg(errp
, "evdev property already set");
442 il
->evdev
= g_strdup(value
);
445 static bool input_linux_get_grab_all(Object
*obj
, Error
**errp
)
447 InputLinux
*il
= INPUT_LINUX(obj
);
452 static void input_linux_set_grab_all(Object
*obj
, bool value
,
455 InputLinux
*il
= INPUT_LINUX(obj
);
457 il
->grab_all
= value
;
460 static bool input_linux_get_repeat(Object
*obj
, Error
**errp
)
462 InputLinux
*il
= INPUT_LINUX(obj
);
467 static void input_linux_set_repeat(Object
*obj
, bool value
,
470 InputLinux
*il
= INPUT_LINUX(obj
);
475 static int input_linux_get_grab_toggle(Object
*obj
, Error
**errp
)
477 InputLinux
*il
= INPUT_LINUX(obj
);
479 return il
->grab_toggle
;
482 static void input_linux_set_grab_toggle(Object
*obj
, int value
,
485 InputLinux
*il
= INPUT_LINUX(obj
);
487 il
->grab_toggle
= value
;
490 static void input_linux_instance_init(Object
*obj
)
492 object_property_add_str(obj
, "evdev",
493 input_linux_get_evdev
,
494 input_linux_set_evdev
);
495 object_property_add_bool(obj
, "grab_all",
496 input_linux_get_grab_all
,
497 input_linux_set_grab_all
);
498 object_property_add_bool(obj
, "repeat",
499 input_linux_get_repeat
,
500 input_linux_set_repeat
);
501 object_property_add_enum(obj
, "grab-toggle", "GrabToggleKeys",
502 &GrabToggleKeys_lookup
,
503 input_linux_get_grab_toggle
,
504 input_linux_set_grab_toggle
);
507 static void input_linux_class_init(ObjectClass
*oc
, void *data
)
509 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
511 ucc
->complete
= input_linux_complete
;
514 static const TypeInfo input_linux_info
= {
515 .name
= TYPE_INPUT_LINUX
,
516 .parent
= TYPE_OBJECT
,
517 .class_init
= input_linux_class_init
,
518 .instance_size
= sizeof(InputLinux
),
519 .instance_init
= input_linux_instance_init
,
520 .instance_finalize
= input_linux_instance_finalize
,
521 .interfaces
= (InterfaceInfo
[]) {
522 { TYPE_USER_CREATABLE
},
527 static void register_types(void)
529 type_register_static(&input_linux_info
);
532 type_init(register_types
);