From nobody Sun Feb 8 11:54:23 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519139514140871.2946883903538; Tue, 20 Feb 2018 07:11:54 -0800 (PST) Received: from localhost ([::1]:56043 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eo9a8-0005nh-6l for importer@patchew.org; Tue, 20 Feb 2018 10:11:48 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35053) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eo9Su-0007w1-C7 for qemu-devel@nongnu.org; Tue, 20 Feb 2018 10:04:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eo9So-0006S0-Er for qemu-devel@nongnu.org; Tue, 20 Feb 2018 10:04:20 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:36440 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eo9So-0006Rk-9T for qemu-devel@nongnu.org; Tue, 20 Feb 2018 10:04:14 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D3FBC402314E for ; Tue, 20 Feb 2018 15:04:03 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-116-147.ams2.redhat.com [10.36.116.147]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6DD8D2024CA9; Tue, 20 Feb 2018 15:04:02 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 5EB3717528; Tue, 20 Feb 2018 16:03:57 +0100 (CET) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Tue, 20 Feb 2018 16:03:57 +0100 Message-Id: <20180220150357.324-6-kraxel@redhat.com> In-Reply-To: <20180220150357.324-1-kraxel@redhat.com> References: <20180220150357.324-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 20 Feb 2018 15:04:03 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Tue, 20 Feb 2018 15:04:03 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kraxel@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2 5/5] keymap: consider modifier state when picking a mapping 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Pass the modifier state to the keymap lookup function. In case multiple keysym -> keycode mappings exist look at the modifier state and prefer the mapping where the modifier state matches. Signed-off-by: Gerd Hoffmann Reviewed-by: Daniel P. Berrang=C3=A9 --- ui/keymaps.h | 3 ++- ui/curses.c | 3 ++- ui/keymaps.c | 33 ++++++++++++++++++++++++++++++++- ui/sdl.c | 6 +++++- ui/vnc.c | 9 +++++++-- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/ui/keymaps.h b/ui/keymaps.h index 17ec03387a..0693588225 100644 --- a/ui/keymaps.h +++ b/ui/keymaps.h @@ -54,7 +54,8 @@ typedef struct kbd_layout_t kbd_layout_t; =20 kbd_layout_t *init_keyboard_layout(const name2keysym_t *table, const char *language); -int keysym2scancode(kbd_layout_t *k, int keysym); +int keysym2scancode(kbd_layout_t *k, int keysym, + bool shift, bool altgr, bool ctrl); int keycode_is_keypad(kbd_layout_t *k, int keycode); int keysym_is_numlock(kbd_layout_t *k, int keysym); =20 diff --git a/ui/curses.c b/ui/curses.c index 85503876c0..31cd43e73c 100644 --- a/ui/curses.c +++ b/ui/curses.c @@ -271,7 +271,8 @@ static void curses_refresh(DisplayChangeListener *dcl) keysym =3D chr; } =20 - keycode =3D keysym2scancode(kbd_layout, keysym & KEYSYM_MASK); + keycode =3D keysym2scancode(kbd_layout, keysym & KEYSYM_MASK, + false, false, false); if (keycode =3D=3D 0) continue; =20 diff --git a/ui/keymaps.c b/ui/keymaps.c index 26a85ff207..a10f4b7a67 100644 --- a/ui/keymaps.c +++ b/ui/keymaps.c @@ -182,8 +182,12 @@ kbd_layout_t *init_keyboard_layout(const name2keysym_t= *table, } =20 =20 -int keysym2scancode(kbd_layout_t *k, int keysym) +int keysym2scancode(kbd_layout_t *k, int keysym, + bool shift, bool altgr, bool ctrl) { + static const uint32_t mask =3D + SCANCODE_SHIFT | SCANCODE_ALTGR | SCANCODE_CTRL; + uint32_t mods, i; struct keysym2code *keysym2code; =20 #ifdef XK_ISO_Left_Tab @@ -199,6 +203,33 @@ int keysym2scancode(kbd_layout_t *k, int keysym) return 0; } =20 + if (keysym2code->count =3D=3D 1) { + return keysym2code->keycodes[0]; + } + + /* + * We have multiple keysym -> keycode mappings. + * + * Check whenever we find one mapping where the modifier state of + * the mapping matches the current user interface modifier state. + * If so, prefer that one. + */ + mods =3D 0; + if (shift) { + mods |=3D SCANCODE_SHIFT; + } + if (altgr) { + mods |=3D SCANCODE_ALTGR; + } + if (ctrl) { + mods |=3D SCANCODE_CTRL; + } + + for (i =3D 0; i < keysym2code->count; i++) { + if ((keysym2code->keycodes[i] & mask) =3D=3D mods) { + return keysym2code->keycodes[i]; + } + } return keysym2code->keycodes[0]; } =20 diff --git a/ui/sdl.c b/ui/sdl.c index a6bff301eb..4c46c40a7f 100644 --- a/ui/sdl.c +++ b/ui/sdl.c @@ -200,6 +200,9 @@ static kbd_layout_t *kbd_layout =3D NULL; =20 static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev) { + bool shift =3D modifiers_state[0x2a] || modifiers_state[0x36]; + bool altgr =3D modifiers_state[0xb8]; + bool ctrl =3D modifiers_state[0x1d] || modifiers_state[0x9d]; int keysym; /* workaround for X11+SDL bug with AltGR */ keysym =3D ev->keysym.sym; @@ -209,7 +212,8 @@ static uint8_t sdl_keyevent_to_keycode_generic(const SD= L_KeyboardEvent *ev) if (keysym =3D=3D 92 && ev->keysym.scancode =3D=3D 133) { keysym =3D 0xa5; } - return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK; + return keysym2scancode(kbd_layout, keysym, + shift, altgr, ctrl) & SCANCODE_KEYMASK; } =20 =20 diff --git a/ui/vnc.c b/ui/vnc.c index a77b568b57..d19f86c7f4 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -1734,7 +1734,8 @@ static void reset_keys(VncState *vs) =20 static void press_key(VncState *vs, int keysym) { - int keycode =3D keysym2scancode(vs->vd->kbd_layout, keysym) & SCANCODE= _KEYMASK; + int keycode =3D keysym2scancode(vs->vd->kbd_layout, keysym, + false, false, false) & SCANCODE_KEYMASK; qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, true); qemu_input_event_send_key_delay(vs->vd->key_delay_ms); qemu_input_event_send_key_number(vs->vd->dcl.con, keycode, false); @@ -1993,6 +1994,9 @@ static const char *code2name(int keycode) =20 static void key_event(VncState *vs, int down, uint32_t sym) { + bool shift =3D vs->modifiers_state[0x2a] || vs->modifiers_state[0x36]; + bool altgr =3D vs->modifiers_state[0xb8]; + bool ctrl =3D vs->modifiers_state[0x1d] || vs->modifiers_state[0x9d]; int keycode; int lsym =3D sym; =20 @@ -2000,7 +2004,8 @@ static void key_event(VncState *vs, int down, uint32_= t sym) lsym =3D lsym - 'A' + 'a'; } =20 - keycode =3D keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF) & SCANC= ODE_KEYMASK; + keycode =3D keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF, + shift, altgr, ctrl) & SCANCODE_KEYMASK; trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); do_key_event(vs, down, keycode, sym); } --=20 2.9.3