aboutsummaryrefslogtreecommitdiff
path: root/ui/input-linux.c
diff options
context:
space:
mode:
Diffstat (limited to 'ui/input-linux.c')
-rw-r--r--ui/input-linux.c143
1 files changed, 114 insertions, 29 deletions
diff --git a/ui/input-linux.c b/ui/input-linux.c
index 9720333b2c..e572a2e905 100644
--- a/ui/input-linux.c
+++ b/ui/input-linux.c
@@ -6,15 +6,18 @@
#include "qemu/osdep.h"
#include "qapi/error.h"
-#include "qemu-common.h"
#include "qemu/config-file.h"
+#include "qemu/main-loop.h"
+#include "qemu/module.h"
#include "qemu/sockets.h"
-#include "sysemu/sysemu.h"
#include "ui/input.h"
#include "qom/object_interfaces.h"
+#include "sysemu/iothread.h"
+#include "block/aio.h"
#include <sys/ioctl.h>
#include "standard-headers/linux/input.h"
+#include "qom/object.h"
static bool linux_is_button(unsigned int lnx)
{
@@ -28,15 +31,9 @@ static bool linux_is_button(unsigned int lnx)
}
#define TYPE_INPUT_LINUX "input-linux"
-#define INPUT_LINUX(obj) \
- OBJECT_CHECK(InputLinux, (obj), TYPE_INPUT_LINUX)
-#define INPUT_LINUX_GET_CLASS(obj) \
- OBJECT_GET_CLASS(InputLinuxClass, (obj), TYPE_INPUT_LINUX)
-#define INPUT_LINUX_CLASS(klass) \
- OBJECT_CLASS_CHECK(InputLinuxClass, (klass), TYPE_INPUT_LINUX)
+OBJECT_DECLARE_SIMPLE_TYPE(InputLinux,
+ INPUT_LINUX)
-typedef struct InputLinux InputLinux;
-typedef struct InputLinuxClass InputLinuxClass;
struct InputLinux {
Object parent;
@@ -63,12 +60,11 @@ struct InputLinux {
struct input_event event;
int read_offset;
+ enum GrabToggleKeys grab_toggle;
+
QTAILQ_ENTRY(InputLinux) next;
};
-struct InputLinuxClass {
- ObjectClass parent_class;
-};
static QTAILQ_HEAD(, InputLinux) inputs = QTAILQ_HEAD_INITIALIZER(inputs);
@@ -98,6 +94,48 @@ static void input_linux_toggle_grab(InputLinux *il)
}
}
+static bool input_linux_check_toggle(InputLinux *il)
+{
+ switch (il->grab_toggle) {
+ case GRAB_TOGGLE_KEYS_CTRL_CTRL:
+ return il->keydown[KEY_LEFTCTRL] &&
+ il->keydown[KEY_RIGHTCTRL];
+
+ case GRAB_TOGGLE_KEYS_ALT_ALT:
+ return il->keydown[KEY_LEFTALT] &&
+ il->keydown[KEY_RIGHTALT];
+
+ case GRAB_TOGGLE_KEYS_SHIFT_SHIFT:
+ return il->keydown[KEY_LEFTSHIFT] &&
+ il->keydown[KEY_RIGHTSHIFT];
+
+ case GRAB_TOGGLE_KEYS_META_META:
+ return il->keydown[KEY_LEFTMETA] &&
+ il->keydown[KEY_RIGHTMETA];
+
+ case GRAB_TOGGLE_KEYS_SCROLLLOCK:
+ return il->keydown[KEY_SCROLLLOCK];
+
+ case GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK:
+ return (il->keydown[KEY_LEFTCTRL] ||
+ il->keydown[KEY_RIGHTCTRL]) &&
+ il->keydown[KEY_SCROLLLOCK];
+
+ case GRAB_TOGGLE_KEYS__MAX:
+ /* avoid gcc error */
+ break;
+ }
+ return false;
+}
+
+static bool input_linux_should_skip(InputLinux *il,
+ struct input_event *event)
+{
+ return (il->grab_toggle == GRAB_TOGGLE_KEYS_SCROLLLOCK ||
+ il->grab_toggle == GRAB_TOGGLE_KEYS_CTRL_SCROLLLOCK) &&
+ event->code == KEY_SCROLLLOCK;
+}
+
static void input_linux_handle_keyboard(InputLinux *il,
struct input_event *event)
{
@@ -128,14 +166,13 @@ static void input_linux_handle_keyboard(InputLinux *il,
}
/* send event to guest when grab is active */
- if (il->grab_active) {
+ if (il->grab_active && !input_linux_should_skip(il, event)) {
int qcode = qemu_input_linux_to_qcode(event->code);
qemu_input_event_send_key_qcode(NULL, qcode, event->value);
}
/* hotkey -> record switch request ... */
- if (il->keydown[KEY_LEFTCTRL] &&
- il->keydown[KEY_RIGHTCTRL]) {
+ if (input_linux_check_toggle(il)) {
il->grab_request = true;
}
@@ -279,7 +316,10 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
error_setg_file_open(errp, errno, il->evdev);
return;
}
- qemu_set_nonblock(il->fd);
+ if (!g_unix_set_fd_nonblocking(il->fd, true, NULL)) {
+ error_setg_errno(errp, errno, "Failed to set FD nonblocking");
+ return;
+ }
rc = ioctl(il->fd, EVIOCGVERSION, &ver);
if (rc < 0) {
@@ -289,13 +329,15 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
rc = ioctl(il->fd, EVIOCGBIT(0, sizeof(evtmap)), &evtmap);
if (rc < 0) {
- error_setg(errp, "%s: failed to read event bits", il->evdev);
- goto err_close;
+ goto err_read_event_bits;
}
if (evtmap & (1 << EV_REL)) {
relmap = 0;
rc = ioctl(il->fd, EVIOCGBIT(EV_REL, sizeof(relmap)), &relmap);
+ if (rc < 0) {
+ goto err_read_event_bits;
+ }
if (relmap & (1 << REL_X)) {
il->has_rel_x = true;
}
@@ -304,12 +346,25 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
if (evtmap & (1 << EV_ABS)) {
absmap = 0;
rc = ioctl(il->fd, EVIOCGBIT(EV_ABS, sizeof(absmap)), &absmap);
+ if (rc < 0) {
+ goto err_read_event_bits;
+ }
if (absmap & (1 << ABS_X)) {
il->has_abs_x = true;
rc = ioctl(il->fd, EVIOCGABS(ABS_X), &absinfo);
+ if (rc < 0) {
+ error_setg(errp, "%s: failed to get get absolute X value",
+ il->evdev);
+ goto err_close;
+ }
il->abs_x_min = absinfo.minimum;
il->abs_x_max = absinfo.maximum;
rc = ioctl(il->fd, EVIOCGABS(ABS_Y), &absinfo);
+ if (rc < 0) {
+ error_setg(errp, "%s: failed to get get absolute Y value",
+ il->evdev);
+ goto err_close;
+ }
il->abs_y_min = absinfo.minimum;
il->abs_y_max = absinfo.maximum;
}
@@ -318,7 +373,14 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
if (evtmap & (1 << EV_KEY)) {
memset(keymap, 0, sizeof(keymap));
rc = ioctl(il->fd, EVIOCGBIT(EV_KEY, sizeof(keymap)), keymap);
+ if (rc < 0) {
+ goto err_read_event_bits;
+ }
rc = ioctl(il->fd, EVIOCGKEY(sizeof(keystate)), keystate);
+ if (rc < 0) {
+ error_setg(errp, "%s: failed to get global key state", il->evdev);
+ goto err_close;
+ }
for (i = 0; i < KEY_CNT; i++) {
if (keymap[i / 8] & (1 << (i % 8))) {
if (linux_is_button(i)) {
@@ -345,6 +407,9 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
il->initialized = true;
return;
+err_read_event_bits:
+ error_setg(errp, "%s: failed to read event bits", il->evdev);
+
err_close:
close(il->fd);
return;
@@ -356,6 +421,7 @@ static void input_linux_instance_finalize(Object *obj)
if (il->initialized) {
QTAILQ_REMOVE(&inputs, il, next);
+ qemu_set_fd_handler(il->fd, NULL, NULL, NULL);
close(il->fd);
}
g_free(il->evdev);
@@ -410,17 +476,23 @@ static void input_linux_set_repeat(Object *obj, bool value,
il->repeat = value;
}
+static int input_linux_get_grab_toggle(Object *obj, Error **errp)
+{
+ InputLinux *il = INPUT_LINUX(obj);
+
+ return il->grab_toggle;
+}
+
+static void input_linux_set_grab_toggle(Object *obj, int value,
+ Error **errp)
+{
+ InputLinux *il = INPUT_LINUX(obj);
+
+ il->grab_toggle = value;
+}
+
static void input_linux_instance_init(Object *obj)
{
- object_property_add_str(obj, "evdev",
- input_linux_get_evdev,
- input_linux_set_evdev, NULL);
- object_property_add_bool(obj, "grab_all",
- input_linux_get_grab_all,
- input_linux_set_grab_all, NULL);
- object_property_add_bool(obj, "repeat",
- input_linux_get_repeat,
- input_linux_set_repeat, NULL);
}
static void input_linux_class_init(ObjectClass *oc, void *data)
@@ -428,12 +500,25 @@ static void input_linux_class_init(ObjectClass *oc, void *data)
UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
ucc->complete = input_linux_complete;
+
+ object_class_property_add_str(oc, "evdev",
+ input_linux_get_evdev,
+ input_linux_set_evdev);
+ object_class_property_add_bool(oc, "grab_all",
+ input_linux_get_grab_all,
+ input_linux_set_grab_all);
+ object_class_property_add_bool(oc, "repeat",
+ input_linux_get_repeat,
+ input_linux_set_repeat);
+ object_class_property_add_enum(oc, "grab-toggle", "GrabToggleKeys",
+ &GrabToggleKeys_lookup,
+ input_linux_get_grab_toggle,
+ input_linux_set_grab_toggle);
}
static const TypeInfo input_linux_info = {
.name = TYPE_INPUT_LINUX,
.parent = TYPE_OBJECT,
- .class_size = sizeof(InputLinuxClass),
.class_init = input_linux_class_init,
.instance_size = sizeof(InputLinux),
.instance_init = input_linux_instance_init,