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-common.h"
10 #include "qemu/config-file.h"
11 #include "qemu/sockets.h"
12 #include "sysemu/sysemu.h"
14 #include "qom/object_interfaces.h"
16 #include <sys/ioctl.h>
17 #include "standard-headers/linux/input.h"
19 static bool linux_is_button(unsigned int lnx
)
24 if (lnx
>= 0x160 && lnx
< 0x2c0) {
30 #define TYPE_INPUT_LINUX "input-linux"
31 #define INPUT_LINUX(obj) \
32 OBJECT_CHECK(InputLinux, (obj), TYPE_INPUT_LINUX)
33 #define INPUT_LINUX_GET_CLASS(obj) \
34 OBJECT_GET_CLASS(InputLinuxClass, (obj), TYPE_INPUT_LINUX)
35 #define INPUT_LINUX_CLASS(klass) \
36 OBJECT_CLASS_CHECK(InputLinuxClass, (klass), TYPE_INPUT_LINUX)
38 typedef struct InputLinux InputLinux
;
39 typedef struct InputLinuxClass InputLinuxClass
;
50 bool keydown
[KEY_CNT
];
63 struct input_event event
;
66 QTAILQ_ENTRY(InputLinux
) next
;
69 struct InputLinuxClass
{
70 ObjectClass parent_class
;
73 static QTAILQ_HEAD(, InputLinux
) inputs
= QTAILQ_HEAD_INITIALIZER(inputs
);
75 static void input_linux_toggle_grab(InputLinux
*il
)
77 intptr_t request
= !il
->grab_active
;
81 rc
= ioctl(il
->fd
, EVIOCGRAB
, request
);
85 il
->grab_active
= !il
->grab_active
;
90 QTAILQ_FOREACH(item
, &inputs
, next
) {
91 if (item
== il
|| item
->grab_all
) {
92 /* avoid endless loops */
95 if (item
->grab_active
!= il
->grab_active
) {
96 input_linux_toggle_grab(item
);
101 static void input_linux_handle_keyboard(InputLinux
*il
,
102 struct input_event
*event
)
104 if (event
->type
== EV_KEY
) {
105 if (event
->value
> 2 || (event
->value
> 1 && !il
->repeat
)) {
107 * ignore autorepeat + unknown key events
108 * 0 == up, 1 == down, 2 == autorepeat, other == undefined
112 if (event
->code
>= KEY_CNT
) {
114 * Should not happen. But better safe than sorry,
115 * and we make Coverity happy too.
120 /* keep track of key state */
121 if (!il
->keydown
[event
->code
] && event
->value
) {
122 il
->keydown
[event
->code
] = true;
125 if (il
->keydown
[event
->code
] && !event
->value
) {
126 il
->keydown
[event
->code
] = false;
130 /* send event to guest when grab is active */
131 if (il
->grab_active
) {
132 int qcode
= qemu_input_linux_to_qcode(event
->code
);
133 qemu_input_event_send_key_qcode(NULL
, qcode
, event
->value
);
136 /* hotkey -> record switch request ... */
137 if (il
->keydown
[KEY_LEFTCTRL
] &&
138 il
->keydown
[KEY_RIGHTCTRL
]) {
139 il
->grab_request
= true;
143 * ... and do the switch when all keys are lifted, so we
144 * confuse neither guest nor host with keys which seem to
145 * be stuck due to missing key-up events.
147 if (il
->grab_request
&& !il
->keycount
) {
148 il
->grab_request
= false;
149 input_linux_toggle_grab(il
);
154 static void input_linux_event_mouse_button(int button
)
156 qemu_input_queue_btn(NULL
, button
, true);
157 qemu_input_event_sync();
158 qemu_input_queue_btn(NULL
, button
, false);
159 qemu_input_event_sync();
162 static void input_linux_handle_mouse(InputLinux
*il
, struct input_event
*event
)
164 if (!il
->grab_active
) {
168 switch (event
->type
) {
170 switch (event
->code
) {
172 qemu_input_queue_btn(NULL
, INPUT_BUTTON_LEFT
, event
->value
);
175 qemu_input_queue_btn(NULL
, INPUT_BUTTON_RIGHT
, event
->value
);
178 qemu_input_queue_btn(NULL
, INPUT_BUTTON_MIDDLE
, event
->value
);
181 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_UP
, event
->value
);
184 qemu_input_queue_btn(NULL
, INPUT_BUTTON_WHEEL_DOWN
,
188 qemu_input_queue_btn(NULL
, INPUT_BUTTON_SIDE
, event
->value
);
191 qemu_input_queue_btn(NULL
, INPUT_BUTTON_EXTRA
, event
->value
);
196 switch (event
->code
) {
198 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, event
->value
);
201 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, event
->value
);
204 il
->wheel
= event
->value
;
209 switch (event
->code
) {
211 qemu_input_queue_abs(NULL
, INPUT_AXIS_X
, event
->value
,
212 il
->abs_x_min
, il
->abs_x_max
);
215 qemu_input_queue_abs(NULL
, INPUT_AXIS_Y
, event
->value
,
216 il
->abs_y_min
, il
->abs_y_max
);
221 qemu_input_event_sync();
222 if (il
->wheel
!= 0) {
223 input_linux_event_mouse_button((il
->wheel
> 0)
224 ? INPUT_BUTTON_WHEEL_UP
225 : INPUT_BUTTON_WHEEL_DOWN
);
232 static void input_linux_event(void *opaque
)
234 InputLinux
*il
= opaque
;
237 uint8_t *p
= (uint8_t *)&il
->event
;
240 read_size
= sizeof(il
->event
) - il
->read_offset
;
241 rc
= read(il
->fd
, &p
[il
->read_offset
], read_size
);
242 if (rc
!= read_size
) {
243 if (rc
< 0 && errno
!= EAGAIN
) {
244 fprintf(stderr
, "%s: read: %s\n", __func__
, strerror(errno
));
245 qemu_set_fd_handler(il
->fd
, NULL
, NULL
, NULL
);
248 il
->read_offset
+= rc
;
255 input_linux_handle_keyboard(il
, &il
->event
);
257 if ((il
->has_rel_x
|| il
->has_abs_x
) && il
->num_btns
) {
258 input_linux_handle_mouse(il
, &il
->event
);
263 static void input_linux_complete(UserCreatable
*uc
, Error
**errp
)
265 InputLinux
*il
= INPUT_LINUX(uc
);
266 uint8_t evtmap
, relmap
, absmap
;
267 uint8_t keymap
[KEY_CNT
/ 8], keystate
[KEY_CNT
/ 8];
270 struct input_absinfo absinfo
;
273 error_setg(errp
, "no input device specified");
277 il
->fd
= open(il
->evdev
, O_RDWR
);
279 error_setg_file_open(errp
, errno
, il
->evdev
);
282 qemu_set_nonblock(il
->fd
);
284 rc
= ioctl(il
->fd
, EVIOCGVERSION
, &ver
);
286 error_setg(errp
, "%s: is not an evdev device", il
->evdev
);
290 rc
= ioctl(il
->fd
, EVIOCGBIT(0, sizeof(evtmap
)), &evtmap
);
292 error_setg(errp
, "%s: failed to read event bits", il
->evdev
);
296 if (evtmap
& (1 << EV_REL
)) {
298 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_REL
, sizeof(relmap
)), &relmap
);
299 if (relmap
& (1 << REL_X
)) {
300 il
->has_rel_x
= true;
304 if (evtmap
& (1 << EV_ABS
)) {
306 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_ABS
, sizeof(absmap
)), &absmap
);
307 if (absmap
& (1 << ABS_X
)) {
308 il
->has_abs_x
= true;
309 rc
= ioctl(il
->fd
, EVIOCGABS(ABS_X
), &absinfo
);
310 il
->abs_x_min
= absinfo
.minimum
;
311 il
->abs_x_max
= absinfo
.maximum
;
312 rc
= ioctl(il
->fd
, EVIOCGABS(ABS_Y
), &absinfo
);
313 il
->abs_y_min
= absinfo
.minimum
;
314 il
->abs_y_max
= absinfo
.maximum
;
318 if (evtmap
& (1 << EV_KEY
)) {
319 memset(keymap
, 0, sizeof(keymap
));
320 rc
= ioctl(il
->fd
, EVIOCGBIT(EV_KEY
, sizeof(keymap
)), keymap
);
321 rc
= ioctl(il
->fd
, EVIOCGKEY(sizeof(keystate
)), keystate
);
322 for (i
= 0; i
< KEY_CNT
; i
++) {
323 if (keymap
[i
/ 8] & (1 << (i
% 8))) {
324 if (linux_is_button(i
)) {
329 if (keystate
[i
/ 8] & (1 << (i
% 8))) {
330 il
->keydown
[i
] = true;
337 qemu_set_fd_handler(il
->fd
, input_linux_event
, NULL
, il
);
339 /* delay grab until all keys are released */
340 il
->grab_request
= true;
342 input_linux_toggle_grab(il
);
344 QTAILQ_INSERT_TAIL(&inputs
, il
, next
);
345 il
->initialized
= true;
353 static void input_linux_instance_finalize(Object
*obj
)
355 InputLinux
*il
= INPUT_LINUX(obj
);
357 if (il
->initialized
) {
358 QTAILQ_REMOVE(&inputs
, il
, next
);
364 static char *input_linux_get_evdev(Object
*obj
, Error
**errp
)
366 InputLinux
*il
= INPUT_LINUX(obj
);
368 return g_strdup(il
->evdev
);
371 static void input_linux_set_evdev(Object
*obj
, const char *value
,
374 InputLinux
*il
= INPUT_LINUX(obj
);
377 error_setg(errp
, "evdev property already set");
380 il
->evdev
= g_strdup(value
);
383 static bool input_linux_get_grab_all(Object
*obj
, Error
**errp
)
385 InputLinux
*il
= INPUT_LINUX(obj
);
390 static void input_linux_set_grab_all(Object
*obj
, bool value
,
393 InputLinux
*il
= INPUT_LINUX(obj
);
395 il
->grab_all
= value
;
398 static bool input_linux_get_repeat(Object
*obj
, Error
**errp
)
400 InputLinux
*il
= INPUT_LINUX(obj
);
405 static void input_linux_set_repeat(Object
*obj
, bool value
,
408 InputLinux
*il
= INPUT_LINUX(obj
);
413 static void input_linux_instance_init(Object
*obj
)
415 object_property_add_str(obj
, "evdev",
416 input_linux_get_evdev
,
417 input_linux_set_evdev
, NULL
);
418 object_property_add_bool(obj
, "grab_all",
419 input_linux_get_grab_all
,
420 input_linux_set_grab_all
, NULL
);
421 object_property_add_bool(obj
, "repeat",
422 input_linux_get_repeat
,
423 input_linux_set_repeat
, NULL
);
426 static void input_linux_class_init(ObjectClass
*oc
, void *data
)
428 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
430 ucc
->complete
= input_linux_complete
;
433 static const TypeInfo input_linux_info
= {
434 .name
= TYPE_INPUT_LINUX
,
435 .parent
= TYPE_OBJECT
,
436 .class_size
= sizeof(InputLinuxClass
),
437 .class_init
= input_linux_class_init
,
438 .instance_size
= sizeof(InputLinux
),
439 .instance_init
= input_linux_instance_init
,
440 .instance_finalize
= input_linux_instance_finalize
,
441 .interfaces
= (InterfaceInfo
[]) {
442 { TYPE_USER_CREATABLE
},
447 static void register_types(void)
449 type_register_static(&input_linux_info
);
452 type_init(register_types
);