From patchwork Thu Jun 16 09:03:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 9180251 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id F110560760 for ; Thu, 16 Jun 2016 09:29:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D875C26490 for ; Thu, 16 Jun 2016 09:29:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CD69828308; Thu, 16 Jun 2016 09:29:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.9 required=2.0 tests=BAYES_00,FSL_HELO_HOME, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4B5F7282FF for ; Thu, 16 Jun 2016 09:29:21 +0000 (UTC) Received: from localhost ([::1]:47532 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDTc0-00058s-5r for patchwork-qemu-devel@patchwork.kernel.org; Thu, 16 Jun 2016 05:29:20 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53210) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDTD4-0004hZ-Tx for qemu-devel@nongnu.org; Thu, 16 Jun 2016 05:03:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bDTCx-0006Jg-Cd for qemu-devel@nongnu.org; Thu, 16 Jun 2016 05:03:33 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36610) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bDTCx-0006JP-4W for qemu-devel@nongnu.org; Thu, 16 Jun 2016 05:03:27 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D436EC0467E5 for ; Thu, 16 Jun 2016 09:03:25 +0000 (UTC) Received: from nilsson.home.kraxel.org (ovpn-116-27.ams2.redhat.com [10.36.116.27]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u5G93O7n009686; Thu, 16 Jun 2016 05:03:25 -0400 Received: by nilsson.home.kraxel.org (Postfix, from userid 500) id 23AC380E6B; Thu, 16 Jun 2016 11:03:24 +0200 (CEST) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Thu, 16 Jun 2016 11:03:20 +0200 Message-Id: <1466067800-25434-4-git-send-email-kraxel@redhat.com> In-Reply-To: <1466067800-25434-1-git-send-email-kraxel@redhat.com> References: <1466067800-25434-1-git-send-email-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 16 Jun 2016 09:03:25 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/3] input-linux: better capability checks, merge input_linux_event_{mouse, keyboard} X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Gerd Hoffmann Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 --- 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;