From nobody Thu Dec 18 08:27:21 2025 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1500904987390900.8383879024443; Mon, 24 Jul 2017 07:03:07 -0700 (PDT) Received: from localhost ([::1]:55037 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dZdws-00045u-DX for importer@patchew.org; Mon, 24 Jul 2017 10:03:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38491) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dZdvL-0003F5-7d for qemu-devel@nongnu.org; Mon, 24 Jul 2017 10:01:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dZdvI-0005pj-UE for qemu-devel@nongnu.org; Mon, 24 Jul 2017 10:01:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:8924) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dZdvI-0005oj-KX for qemu-devel@nongnu.org; Mon, 24 Jul 2017 10:01:24 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7AE39356DB for ; Mon, 24 Jul 2017 14:01:23 +0000 (UTC) Received: from sirius.home.kraxel.org (ovpn-116-225.ams2.redhat.com [10.36.116.225]) by smtp.corp.redhat.com (Postfix) with ESMTP id 984AF6E51E; Mon, 24 Jul 2017 14:01:18 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id C230D3BB; Mon, 24 Jul 2017 16:01:17 +0200 (CEST) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7AE39356DB Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=kraxel@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 7AE39356DB From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Mon, 24 Jul 2017 16:01:17 +0200 Message-Id: <20170724140117.27651-4-kraxel@redhat.com> In-Reply-To: <20170724140117.27651-1-kraxel@redhat.com> References: <20170724140117.27651-1-kraxel@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 24 Jul 2017 14:01:23 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/3] Add qemu-keymap tool. 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" qemu-keymap generates qemu reverse keymaps from xkb keymaps, which can be used with the qemu "-k" command line switch. Signed-off-by: Gerd Hoffmann --- configure | 23 ++++++ Makefile | 5 ++ qemu-keymap.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 271 insertions(+) create mode 100644 qemu-keymap.c diff --git a/configure b/configure index c573a777fc..e8e074c5f9 100755 --- a/configure +++ b/configure @@ -292,6 +292,7 @@ vde=3D"" vnc_sasl=3D"" vnc_jpeg=3D"" vnc_png=3D"" +xkbcommon=3D"" xen=3D"" xen_ctrl_version=3D"" xen_pv_domain_build=3D"no" @@ -2912,6 +2913,21 @@ EOF fi =20 ########################################## +# xkbcommon probe +if test "$xkbcommon" !=3D "no" ; then + if $pkg_config xkbcommon --exists; then + xkbcommon_cflags=3D$($pkg_config xkbcommon --cflags) + xkbcommon_libs=3D$($pkg_config xkbcommon --libs) + xkbcommon=3Dyes + else + if test "$xkbcommon" =3D "yes" ; then + feature_not_found "xkbcommon" "Install libxkbcommon-devel" + fi + xkbcommon=3Dno + fi +fi + +########################################## # fnmatch() probe, used for ACL routines fnmatch=3D"no" cat > $TMPC << EOF @@ -5066,6 +5082,9 @@ if test "$softmmu" =3D yes ; then fi fi fi +if test "$xkbcommon" =3D "yes"; then + tools=3D"qemu-keymap\$(EXESUF) $tools" +fi =20 # Probe for guest agent support/options =20 @@ -5562,6 +5581,10 @@ fi if test "$vnc_png" =3D "yes" ; then echo "CONFIG_VNC_PNG=3Dy" >> $config_host_mak fi +if test "$xkbcommon" =3D "yes" ; then + echo "XKBCOMMON_CFLAGS=3D$xkbcommon_cflags" >> $config_host_mak + echo "XKBCOMMON_LIBS=3D$xkbcommon_libs" >> $config_host_mak +fi if test "$fnmatch" =3D "yes" ; then echo "CONFIG_FNMATCH=3Dy" >> $config_host_mak fi diff --git a/Makefile b/Makefile index ef721480eb..0b8ddfd2dd 100644 --- a/Makefile +++ b/Makefile @@ -381,6 +381,8 @@ qemu-io$(EXESUF): qemu-io.o $(block-obj-y) $(crypto-obj= -y) $(io-obj-y) $(qom-obj =20 qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o $(COMMON_LDADDS) =20 +qemu-keymap$(EXESUF): qemu-keymap.o ui/input-keymap.o + fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/9p-m= arshal.o fsdev/9p-iov-marshal.o $(COMMON_LDADDS) fsdev/virtfs-proxy-helper$(EXESUF): LIBS +=3D -lcap =20 @@ -390,6 +392,9 @@ qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PAT= H)/scripts/hxtool qemu-ga$(EXESUF): LIBS =3D $(LIBS_QGA) qemu-ga$(EXESUF): QEMU_CFLAGS +=3D -I qga/qapi-generated =20 +qemu-keymap$(EXESUF): LIBS =3D $(XKBCOMMON_LIBS) +qemu-keymap$(EXESUF): QEMU_CFLAGS +=3D $(XKBCOMMON_CFLAGS) + gen-out-type =3D $(subst .,-,$(suffix $@)) =20 qapi-py =3D $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py diff --git a/qemu-keymap.c b/qemu-keymap.c new file mode 100644 index 0000000000..950fd54bdc --- /dev/null +++ b/qemu-keymap.c @@ -0,0 +1,243 @@ +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qapi-types.h" +#include "qemu/notify.h" +#include "ui/input.h" + +#include + +struct xkb_rule_names names =3D { + .rules =3D NULL, + .model =3D "pc105", + .layout =3D "us", + .variant =3D NULL, + .options =3D NULL, +}; + +static xkb_mod_mask_t shift; +static xkb_mod_mask_t ctrl; +static xkb_mod_mask_t altgr; + +static xkb_mod_mask_t numlock; + +static FILE *outfile; + +/* -----------------------------------------------------------------------= - */ + +static void print_sym(xkb_keysym_t sym, uint32_t number, const char *mod) +{ + char name[64]; + + if (sym =3D=3D XKB_KEY_NoSymbol) { + return; + } + xkb_keysym_get_name(sym, name, sizeof(name)); + fprintf(outfile, "%s 0x%02x%s\n", name, number, mod); +} + +static void walk_map(struct xkb_keymap *map, xkb_keycode_t code, void *dat= a) +{ + struct xkb_state *state =3D data; + xkb_keysym_t kbase, knumlock, kshift, kaltgr, kaltgrshift; + uint32_t evdev, qcode, number; + KeyValue keyvalue; + char name[64]; + + fprintf(outfile, "\n"); + + /* + * map xkb keycode -> QKeyCode + * + * xkb keycode is linux evdev shifted by 8 + */ + evdev =3D code - 8; + qcode =3D qemu_input_linux_to_qcode(evdev); + if (qcode =3D=3D Q_KEY_CODE_UNMAPPED) { + fprintf(outfile, "# evdev %d (0x%x): no evdev -> qcode mapping", + evdev, evdev); + goto nomap; + } + + /* + * map QKeyCode -> number + * + * TODO: long-term we should use QKeyCode names in keymaps instead + */ + keyvalue.type =3D KEY_VALUE_KIND_QCODE; + keyvalue.u.qcode.data =3D qcode; + number =3D qemu_input_key_value_to_number(&keyvalue); + if (number =3D=3D 0) { + fprintf(outfile, + "# evdev %d (0x%x), qcode %d: no qcode -> number mapping", + evdev, evdev, qcode); + goto nomap; + } + fprintf(outfile, "# evdev %d (0x%x), qcode %d, number 0x%x\n", + evdev, evdev, qcode, number); + + /* + * check which modifier states generate which keysyms + */ + xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0); + kbase =3D xkb_state_key_get_one_sym(state, code); + print_sym(kbase, number, ""); + + xkb_state_update_mask(state, 0, 0, numlock, 0, 0, 0); + knumlock =3D xkb_state_key_get_one_sym(state, code); + if (kbase !=3D knumlock) { + print_sym(knumlock, number, " numlock"); + } + + xkb_state_update_mask(state, shift, 0, 0, 0, 0, 0); + kshift =3D xkb_state_key_get_one_sym(state, code); + if (kbase !=3D kshift && knumlock !=3D kshift) { + print_sym(kshift, number, " shift"); + } + + xkb_state_update_mask(state, altgr, 0, 0, 0, 0, 0); + kaltgr =3D xkb_state_key_get_one_sym(state, code); + if (kbase !=3D kaltgr) { + print_sym(kaltgr, number, " altgr"); + } + + xkb_state_update_mask(state, altgr | shift, 0, 0, 0, 0, 0); + kaltgrshift =3D xkb_state_key_get_one_sym(state, code); + if (kshift !=3D kaltgrshift && kaltgr !=3D kaltgrshift) { + print_sym(kaltgrshift, number, " shift altgr"); + } + return; + +nomap: + xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0); + kbase =3D xkb_state_key_get_one_sym(state, code); + xkb_keysym_get_name(kbase, name, sizeof(name)); + fprintf(outfile, " (xkb keysym %s)\n", name); +} + +static void usage(FILE *out) +{ + fprintf(out, + "\n" + "This tool generates qemu reverse keymaps from xkb keymaps,\n" + "which can be used with the qemu \"-k\" command line switch.\n" + "\n" + "usage: qemu-keymap \n" + "options:\n" + " -h print this text\n" + " -f set output file (default: stdout)\n" + " -m set kbd model (default: %s)\n" + " -l set kbd layout (default: %s)\n" + " -v set kbd variant (default: %s)\n" + " -o set kbd options (default: %s)\n" + "\n", + names.model, names.layout, + names.variant ?: "-", + names.options ?: "-"); +} + +int main(int argc, char *argv[]) +{ + struct xkb_context *ctx; + struct xkb_keymap *map; + struct xkb_state *state; + xkb_mod_index_t mod, mods; + int rc; + + for (;;) { + rc =3D getopt(argc, argv, "hm:l:v:o:f:"); + if (rc =3D=3D -1) + break; + switch (rc) { + case 'm': + names.model =3D optarg; + break; + case 'l': + names.layout =3D optarg; + break; + case 'v': + names.variant =3D optarg; + break; + case 'o': + names.options =3D optarg; + break; + case 'f': + outfile =3D fopen(optarg, "w"); + if (outfile =3D=3D NULL) { + fprintf(stderr, "open %s: %s\n", optarg, strerror(errno)); + exit(1); + } + break; + case 'h': + usage(stdout); + exit(0); + default: + usage(stderr); + exit(1); + } + } + + if (outfile =3D=3D NULL) { + outfile =3D stdout; + } + + fprintf(outfile, + "#\n" + "# generated by qemu-keymap\n" + "# model : %s\n" + "# layout : %s\n" + "# variant : %s\n" + "# options : %s\n" + "#\n\n", + names.model, names.layout, + names.variant ?: "-", + names.options ?: "-"); + + ctx =3D xkb_context_new(XKB_CONTEXT_NO_FLAGS); + map =3D xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_F= LAGS); + if (!map) { + /* libxkbcommon prints error */ + exit(1); + } + + fprintf(outfile, "# modifiers\n"); + mods =3D xkb_keymap_num_mods(map); + for (mod =3D 0; mod < mods; mod++) { + fprintf(outfile, "# %2d: %s\n", + mod, xkb_keymap_mod_get_name(map, mod)); + } + + mod =3D xkb_keymap_mod_get_index(map, "Shift"); + shift =3D (1 << mod); + mod =3D xkb_keymap_mod_get_index(map, "Control"); + ctrl =3D (1 << mod); + mod =3D xkb_keymap_mod_get_index(map, "AltGr"); + altgr =3D (1 << mod); + mod =3D xkb_keymap_mod_get_index(map, "NumLock"); + numlock =3D (1 << mod); + + state =3D xkb_state_new(map); + xkb_keymap_key_for_each(map, walk_map, state); + + /* + * add quirks + * + * Sometimes multiple keysyms generate the same keycodes. + * With our keycode -> keysym lookup we'll find only one + * of the keysyms. So append them here. + */ + fprintf(outfile, "\n# quirks section\n" + "\n" + "Print 0xb7\n" + "Sys_Req 0xb7\n" + "Execute 0xb7\n" + "\n" + "KP_Decimal 0x53 numlock\n" + "KP_Separator 0x53 numlock\n" + "\n" + "Alt_R 0xb8\n" + "ISO_Level3_Shift 0xb8\n" + "Mode_switch 0xb8\n" + "\n"); + + exit(0); +} --=20 2.9.3