Skip to contentSkip to conversation

input: linux evdev support

PATCH In review Gerd Hoffmann 2 participants 7 messages Mar 4, 2016 – Mar 10, 2016

lore
Share & tools
lore
#0 lore raw

cover input: linux evdev support

Hi,

This patch series adds support for reading input events directly from linux input devices instead of getting them from the UI (gtk/spice/...). This is useful if you pci passthrough your vga, because you don't need some otherwise dummy UI just to feed input into your guest. Chech the patch #1 commit message for all the details.

It's been a while I posted the patches last time. Undusted them. Rebased to master. Adapted to some QAPI changes. Squashed in some bugfixes accumulated over time. Applied some testing using my new intel test box.

please review,
Gerd

Gerd Hoffmann (3):
input: linux evdev support
input-linux: add option to toggle grab on all devices
input-linux: add switch to enable auto-repeat events

 include/ui/input.h |   2 +
 qemu-options.hx    |   9 ++
 ui/Makefile.objs   |   1 +
 ui/input-linux.c   | 391 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 vl.c               |  11 ++
 5 files changed, 414 insertions(+)
 create mode 100644 ui/input-linux.c
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/3 input-linux: add option to toggle grab on all devices

Maintain a list of all input devices. Add an option to make grab work across all devices (so toggling grab on the keybard can switch over the mouse too).

---
 ui/input-linux.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
ui/input-linux.c +29 -0
index 5374a2e..ad43963 100644
@@ -134,14 +134,19 @@ struct InputLinux {
     int         fd;
     bool        grab_request;
     bool        grab_active;
+    bool        grab_all;
     bool        keydown[KEY_CNT];
     int         keycount;
     int         wheel;
+    QTAILQ_ENTRY(InputLinux) next;
 };
 
+static QTAILQ_HEAD(, InputLinux) inputs = QTAILQ_HEAD_INITIALIZER(inputs);
+
 static void input_linux_toggle_grab(InputLinux *il)
 {
     intptr_t request = !il->grab_active;
+    InputLinux *item;
     int rc;
 
     rc = ioctl(il->fd, EVIOCGRAB, request);
@@ -149,6 +154,19 @@ static void input_linux_toggle_grab(InputLinux *il)
         return;
     }
     il->grab_active = !il->grab_active;
+
+    if (!il->grab_all) {
+        return;
+    }
+    QTAILQ_FOREACH(item, &inputs, next) {
+        if (item == il || item->grab_all) {
+            /* avoid endless loops */
+            continue;
+        }
+        if (item->grab_active != il->grab_active) {
+            input_linux_toggle_grab(item);
+        }
+    }
 }
 
 static void input_linux_event_keyboard(void *opaque)
@@ -238,6 +256,11 @@ static void input_linux_event_mouse(void *opaque)
             break;
         }
 
+        /* only send event to guest when grab is active */
+        if (!il->grab_active) {
+            continue;
+        }
+
         switch (event.type) {
         case EV_KEY:
             switch (event.code) {
@@ -292,6 +315,8 @@ int input_linux_init(void *opaque, QemuOpts *opts, Error **errp)
     int rc, ver;
 
     il->evdev = qemu_opt_get(opts, "evdev");
+    il->grab_all = qemu_opt_get_bool(opts, "grab-all", false);
+
     if (!il->evdev) {
         error_setg(errp, "no input device specified");
         goto err_free;
@@ -328,6 +353,7 @@ int input_linux_init(void *opaque, QemuOpts *opts, Error **errp)
         goto err_close;
     }
     input_linux_toggle_grab(il);
+    QTAILQ_INSERT_TAIL(&inputs, il, next);
     return 0;
 
 err_close:
@@ -345,6 +371,9 @@ static QemuOptsList qemu_input_linux_opts = {
         {
             .name = "evdev",
             .type = QEMU_OPT_STRING,
+        },{
+            .name = "grab-all",
+            .type = QEMU_OPT_BOOL,
         },
         { /* end of list */ }
     },
Signature · 1 line
1.8.3.1
#3 lore raw

PATCH 3/3 input-linux: add switch to enable auto-repeat events

In reply to Gerd Hoffmann

Enable with "-input-linux /dev/input/${device},repeat=on".

---
 ui/input-linux.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
ui/input-linux.c +6 -1
index ad43963..0bc0405 100644
@@ -132,6 +132,7 @@ typedef struct InputLinux InputLinux;
 struct InputLinux {
     const char  *evdev;
     int         fd;
+    bool        repeat;
     bool        grab_request;
     bool        grab_active;
     bool        grab_all;
@@ -188,7 +189,7 @@ static void input_linux_event_keyboard(void *opaque)
 
         switch (event.type) {
         case EV_KEY:
-            if (event.value > 1) {
+            if (event.value > 2 || (event.value > 1 && !il->repeat)) {
                 /*
                  * ignore autorepeat + unknown key events
                  * 0 == up, 1 == down, 2 == autorepeat, other == undefined
@@ -316,6 +317,7 @@ int input_linux_init(void *opaque, QemuOpts *opts, Error **errp)
 
     il->evdev = qemu_opt_get(opts, "evdev");
     il->grab_all = qemu_opt_get_bool(opts, "grab-all", false);
+    il->repeat = qemu_opt_get_bool(opts, "repeat", false);
 
     if (!il->evdev) {
         error_setg(errp, "no input device specified");
@@ -374,6 +376,9 @@ static QemuOptsList qemu_input_linux_opts = {
         },{
             .name = "grab-all",
             .type = QEMU_OPT_BOOL,
+        },{
+            .name = "repeat",
+            .type = QEMU_OPT_BOOL,
         },
         { /* end of list */ }
     },
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/3 input: linux evdev support

In reply to Gerd Hoffmann

This patch adds support for reading input events directly from linux evdev devices and forward them to the guest. Unlike virtio-input-host which simply passes on all events to the guest without looking at them this will interpret the events and feed them into the qemu input subsystem.

Therefore this is limited to what the qemu input subsystem and the emulated input devices are able to handle. Also there is no support for absolute coordinates (tablet/touchscreen). So we are talking here about basic mouse and keyboard support.

The advantage is that it'll work without virtio-input drivers in the guest, the events are delivered to the usual ps/2 or usb input devices (depending on what the machine happens to have). And for keyboards qemu is able to switch the keyboard between guest and host on hotkey. The hotkey is hard-coded for now (both control keys), initialy the guest owns the keyboard.

Probably most useful when assigning vga devices with vfio and using a physical monitor instead of vnc/spice/gtk as guest display.

Usage: Add '-input-linux /dev/input/event<nr>' to the qemu command line. Note that udev has rules which populate /dev/input/by-{id,path} with static names, which might be more convinient to use.

---
 include/ui/input.h |   2 +
 qemu-options.hx    |   9 ++
 ui/Makefile.objs   |   1 +
 ui/input-linux.c   | 357 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 vl.c               |  11 ++
 5 files changed, 380 insertions(+)
 create mode 100644 ui/input-linux.c
include/ui/input.h +2 -0
index d06a12d..102d8a3 100644
@@ -65,4 +65,6 @@ void qemu_input_check_mode_change(void);
 void qemu_add_mouse_mode_change_notifier(Notifier *notify);
 void qemu_remove_mouse_mode_change_notifier(Notifier *notify);
 
+int input_linux_init(void *opaque, QemuOpts *opts, Error **errp);
+
 #endif /* INPUT_H */
qemu-options.hx +9 -0
index 144e6a9..bda92d2 100644
@@ -1226,6 +1226,15 @@ STEXI
 Set the initial graphical resolution and depth (PPC, SPARC only).
 ETEXI
 
+DEF("input-linux", 1, QEMU_OPTION_input_linux,
+    "-input-linux <evdev>\n"
+    "                Use input device.\n", QEMU_ARCH_ALL)
+STEXI
+@item -input-linux @var{dev}
+@findex -input-linux
+Use input device.
+ETEXI
+
 DEF("vnc", HAS_ARG, QEMU_OPTION_vnc ,
     "-vnc display    start a VNC server on display\n", QEMU_ARCH_ALL)
 STEXI
ui/Makefile.objs +1 -0
index 728393c..dc936f1 100644
@@ -9,6 +9,7 @@ vnc-obj-y += vnc-jobs.o
 
 common-obj-y += keymaps.o console.o cursor.o qemu-pixman.o
 common-obj-y += input.o input-keymap.o input-legacy.o
+common-obj-$(CONFIG_LINUX) += input-linux.o
 common-obj-$(CONFIG_SPICE) += spice-core.o spice-input.o spice-display.o
 common-obj-$(CONFIG_SDL) += sdl.mo x_keymap.o
 common-obj-$(CONFIG_COCOA) += cocoa.o
ui/input-linux.c new file +357 -0

357 lines not on this page · raw message

vl.c +11 -0
index adeddd9..7a28982 100644
@@ -72,6 +72,7 @@ int main(int argc, char **argv)
 #include "net/slirp.h"
 #include "monitor/monitor.h"
 #include "ui/console.h"
+#include "ui/input.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/numa.h"
 #include "exec/gdbstub.h"
@@ -3728,6 +3729,12 @@ int main(int argc, char **argv, char **envp)
 #endif
                 break;
             }
+            case QEMU_OPTION_input_linux:
+                if (!qemu_opts_parse_noisily(qemu_find_opts("input-linux"),
+                                             optarg, true)) {
+                    exit(1);
+                }
+                break;
             case QEMU_OPTION_no_acpi:
                 acpi_enabled = 0;
                 break;
@@ -4591,6 +4598,10 @@ int main(int argc, char **argv, char **envp)
         qemu_spice_display_init();
     }
 #endif
+#ifdef CONFIG_LINUX
+    qemu_opts_foreach(qemu_find_opts("input-linux"),
+                      input_linux_init, NULL, &error_fatal);
+#endif
 
     if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
         exit(1);
Signature · 1 line
1.8.3.1
#4 lore raw
In reply to Gerd Hoffmann
Quoted 8 lines · Gerd Hoffmann

Hi,

This patch series adds support for reading input events directly from linux input devices instead of getting them from the UI (gtk/spice/...). This is useful if you pci passthrough your vga, because you don't need some otherwise dummy UI just to feed input into your guest. Chech the patch #1 commit message for all the details.

Quoted 4 lines

It's been a while I posted the patches last time. Undusted them. Rebased to master. Adapted to some QAPI changes. Squashed in some bugfixes accumulated over time. Applied some testing using my new intel test box.

This is nice! I have used virtio-input-host to do some pseudo-multiseat, but it was only for Linux guests.

However, instead of adding a new -input-linux option, could you make it a QOM object which implements UserCreatable? Then you can add it with something like "-object input-linux,path=/dev/input/input10" (perhaps "input-evdev" would be more specific). This has three advantages:

1) you get hotplug for free;

2) you don't add yet another option to vl.c (btw patch 2 and 3 are not updating the docs);

3) it's easier to add more backends, though the only ones that come to mind are rather silly (e.g. input-msmouse could take a chardev and parse the serial mouse protocol).

If you cannot use QOM, even just using "-inputdev [backend=]linux,path=/dev/input/input10" would provide (3), but QOM seems superior at the cost of a little more boilerplate in ui/input-linux.c.

Thanks,

Paolo

#5 lore raw

Hi,

Quoted 4 lines

However, instead of adding a new -input-linux option, could you make it a QOM object which implements UserCreatable? Then you can add it with something like "-object input-linux,path=/dev/input/input10" (perhaps "input-evdev" would be more specific). This has three advantages:

It's merged meanwhile, but when doing it before 2.6 we can still change it I think. Do you have a pointer to some sample code doing this and/or docs for me?

thanks,
Gerd

#6 lore raw
Quoted 7 lines · Gerd Hoffmann
Quoted 4 lines

However, instead of adding a new -input-linux option, could you make it a QOM object which implements UserCreatable? Then you can add it with something like "-object input-linux,path=/dev/input/input10" (perhaps "input-evdev" would be more specific). This has three advantages:

It's merged meanwhile, but when doing it before 2.6 we can still change it I think. Do you have a pointer to some sample code doing this and/or docs for me?

backends/hostmem.c is probably as close as it can get.

Paolo