All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PULL 8/8] input-linux: better capability checks, merge input_linux_event_{mouse, keyboard}
Date: Tue, 12 Jul 2016 10:21:43 +0200	[thread overview]
Message-ID: <1468311703-27209-9-git-send-email-kraxel@redhat.com> (raw)
In-Reply-To: <1468311703-27209-1-git-send-email-kraxel@redhat.com>

Improve capability checks (count keys and buttons), store results.

Merge the input_linux_event_mouse and input_linux_event_keyboard
functions into one, dispatch into input_linux_handle_mouse and
input_linux_handle_keyboard depending on device capabilities.

Allow calling both handle functions, so we can handle mice which
also send key events, by routing those key events to the keyboard.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Message-id: 1466067800-25434-4-git-send-email-kraxel@redhat.com
---
 ui/input-linux.c | 92 +++++++++++++++++++++++++++++---------------------------
 1 file changed, 47 insertions(+), 45 deletions(-)

diff --git a/ui/input-linux.c b/ui/input-linux.c
index ffff97f..0e230ce 100644
--- a/ui/input-linux.c
+++ b/ui/input-linux.c
@@ -129,6 +129,17 @@ static int qemu_input_linux_to_qcode(unsigned int lnx)
     return linux_to_qcode[lnx];
 }
 
+static bool linux_is_button(unsigned int lnx)
+{
+    if (lnx < 0x100) {
+        return false;
+    }
+    if (lnx >= 0x160 && lnx < 0x2c0) {
+        return false;
+    }
+    return true;
+}
+
 #define TYPE_INPUT_LINUX "input-linux"
 #define INPUT_LINUX(obj) \
     OBJECT_CHECK(InputLinux, (obj), TYPE_INPUT_LINUX)
@@ -153,6 +164,12 @@ struct InputLinux {
     int         keycount;
     int         wheel;
     bool        initialized;
+
+    bool        has_rel_x;
+    bool        has_abs_x;
+    int         num_keys;
+    int         num_btns;
+
     QTAILQ_ENTRY(InputLinux) next;
 };
 
@@ -241,27 +258,6 @@ static void input_linux_handle_keyboard(InputLinux *il,
     }
 }
 
-static void input_linux_event_keyboard(void *opaque)
-{
-    InputLinux *il = opaque;
-    struct input_event event;
-    int rc;
-
-    for (;;) {
-        rc = read(il->fd, &event, sizeof(event));
-        if (rc != sizeof(event)) {
-            if (rc < 0 && errno != EAGAIN) {
-                fprintf(stderr, "%s: read: %s\n", __func__, strerror(errno));
-                qemu_set_fd_handler(il->fd, NULL, NULL, NULL);
-                close(il->fd);
-            }
-            break;
-        }
-
-        input_linux_handle_keyboard(il, &event);
-    }
-}
-
 static void input_linux_event_mouse_button(int button)
 {
     qemu_input_queue_btn(NULL, button, true);
@@ -322,7 +318,7 @@ static void input_linux_handle_mouse(InputLinux *il, struct input_event *event)
     }
 }
 
-static void input_linux_event_mouse(void *opaque)
+static void input_linux_event(void *opaque)
 {
     InputLinux *il = opaque;
     struct input_event event;
@@ -339,14 +335,20 @@ static void input_linux_event_mouse(void *opaque)
             break;
         }
 
-        input_linux_handle_mouse(il, &event);
+        if (il->num_keys) {
+            input_linux_handle_keyboard(il, &event);
+        }
+        if (il->has_rel_x && il->num_btns) {
+            input_linux_handle_mouse(il, &event);
+        }
     }
 }
 
 static void input_linux_complete(UserCreatable *uc, Error **errp)
 {
     InputLinux *il = INPUT_LINUX(uc);
-    uint32_t evtmap, relmap, absmap;
+    uint8_t evtmap, relmap, absmap, keymap[KEY_CNT / 8];
+    unsigned int i;
     int rc, ver;
 
     if (!il->evdev) {
@@ -374,36 +376,36 @@ static void input_linux_complete(UserCreatable *uc, Error **errp)
     }
 
     if (evtmap & (1 << EV_REL)) {
+        relmap = 0;
         rc = ioctl(il->fd, EVIOCGBIT(EV_REL, sizeof(relmap)), &relmap);
-        if (rc < 0) {
-            relmap = 0;
+        if (relmap & (1 << REL_X)) {
+            il->has_rel_x = true;
         }
     }
 
     if (evtmap & (1 << EV_ABS)) {
-        ioctl(il->fd, EVIOCGBIT(EV_ABS, sizeof(absmap)), &absmap);
-        if (rc < 0) {
-            absmap = 0;
+        absmap = 0;
+        rc = ioctl(il->fd, EVIOCGBIT(EV_ABS, sizeof(absmap)), &absmap);
+        if (absmap & (1 << ABS_X)) {
+            il->has_abs_x = true;
         }
     }
 
-    if ((evtmap & (1 << EV_REL)) &&
-        (relmap & (1 << REL_X))) {
-        /* has relative x axis -> assume mouse */
-        qemu_set_fd_handler(il->fd, input_linux_event_mouse, NULL, il);
-    } else if ((evtmap & (1 << EV_ABS)) &&
-               (absmap & (1 << ABS_X))) {
-        /* has absolute x axis -> not supported */
-        error_setg(errp, "tablet/touchscreen not supported");
-        goto err_close;
-    } else if (evtmap & (1 << EV_KEY)) {
-        /* has keys/buttons (and no x axis) -> assume keyboard */
-        qemu_set_fd_handler(il->fd, input_linux_event_keyboard, NULL, il);
-    } else {
-        /* Huh? What is this? */
-        error_setg(errp, "unknown kind of input device");
-        goto err_close;
+    if (evtmap & (1 << EV_KEY)) {
+        memset(keymap, 0, sizeof(keymap));
+        rc = ioctl(il->fd, EVIOCGBIT(EV_KEY, sizeof(keymap)), keymap);
+        for (i = 0; i < KEY_CNT; i++) {
+            if (keymap[i / 8] & (1 << (i % 8))) {
+                if (linux_is_button(i)) {
+                    il->num_btns++;
+                } else {
+                    il->num_keys++;
+                }
+            }
+        }
     }
+
+    qemu_set_fd_handler(il->fd, input_linux_event, NULL, il);
     input_linux_toggle_grab(il);
     QTAILQ_INSERT_TAIL(&inputs, il, next);
     il->initialized = true;
-- 
1.8.3.1

  parent reply	other threads:[~2016-07-12  8:22 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-12  8:21 [Qemu-devel] [PULL 0/8] input patch queue Gerd Hoffmann
2016-07-12  8:21 ` [Qemu-devel] [PULL 1/8] msmouse: add MouseState, unregister handler on close Gerd Hoffmann
2016-07-12  8:21 ` [Qemu-devel] [PULL 2/8] msmouse: fix buffer handling Gerd Hoffmann
2016-07-12  8:21 ` [Qemu-devel] [PULL 3/8] msmouse: switch to new input interface Gerd Hoffmann
2016-07-12  8:21 ` [Qemu-devel] [PULL 4/8] msmouse: send short messages if possible Gerd Hoffmann
2016-07-12  8:21 ` [Qemu-devel] [PULL 5/8] input: add trace events for full queues Gerd Hoffmann
2016-07-12  8:21 ` [Qemu-devel] [PULL 6/8] input-linux: factor out input_linux_handle_mouse Gerd Hoffmann
2016-07-12  8:21 ` [Qemu-devel] [PULL 7/8] input-linux: factor out input_linux_handle_keyboard Gerd Hoffmann
2016-07-12  8:21 ` Gerd Hoffmann [this message]
2016-07-12 11:03 ` [Qemu-devel] [PULL 0/8] input patch queue Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1468311703-27209-9-git-send-email-kraxel@redhat.com \
    --to=kraxel@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.