From patchwork Thu Feb 22 10:23:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 876577 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zn9Wt1H8Rz9sW3 for ; Thu, 22 Feb 2018 21:27:06 +1100 (AEDT) Received: from localhost ([::1]:37170 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eoo5g-0005wP-9q for incoming@patchwork.ozlabs.org; Thu, 22 Feb 2018 05:27:04 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50339) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eoo2G-0003Jl-FW for qemu-devel@nongnu.org; Thu, 22 Feb 2018 05:23:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eoo2B-000205-E1 for qemu-devel@nongnu.org; Thu, 22 Feb 2018 05:23:32 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:40142 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 1eoo2B-0001yL-4e for qemu-devel@nongnu.org; Thu, 22 Feb 2018 05:23:27 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DB7F98A3DD for ; Thu, 22 Feb 2018 10:23:24 +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 58E2C10A9704; Thu, 22 Feb 2018 10:23:23 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 1594A17535; Thu, 22 Feb 2018 11:23:18 +0100 (CET) From: Gerd Hoffmann To: qemu-devel@nongnu.org Date: Thu, 22 Feb 2018 11:23:14 +0100 Message-Id: <20180222102317.25776-7-kraxel@redhat.com> In-Reply-To: <20180222102317.25776-1-kraxel@redhat.com> References: <20180222102317.25776-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Thu, 22 Feb 2018 10:23:24 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Thu, 22 Feb 2018 10:23:24 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.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] [PULL 6/9] keymap: use glib hash for kbd_layout_t 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Drop home-grown lookup code, which is a strange mix of a lookup table and a list. Use standard glib hash instead. Signed-off-by: Gerd Hoffmann Reviewed-by: Daniel P. Berrangé Message-id: 20180222070513.8740-3-kraxel@redhat.com --- ui/keymaps.c | 73 ++++++++++++++++++++++++--------------------------------- ui/trace-events | 2 +- 2 files changed, 32 insertions(+), 43 deletions(-) diff --git a/ui/keymaps.c b/ui/keymaps.c index 134958a197..449c3dec22 100644 --- a/ui/keymaps.c +++ b/ui/keymaps.c @@ -28,22 +28,18 @@ #include "trace.h" #include "qemu/error-report.h" -#define MAX_NORMAL_KEYCODE 512 -#define MAX_EXTRA_COUNT 256 - struct key_range { int start; int end; struct key_range *next; }; +struct keysym2code { + uint16_t keycode; +}; + struct kbd_layout_t { - uint16_t keysym2keycode[MAX_NORMAL_KEYCODE]; - struct { - int keysym; - uint16_t keycode; - } keysym2keycode_extra[MAX_EXTRA_COUNT]; - int extra_count; + GHashTable *hash; struct key_range *keypad_range; struct key_range *numlock_range; }; @@ -91,23 +87,19 @@ static void add_to_key_range(struct key_range **krp, int code) { } } -static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) { - if (keysym < MAX_NORMAL_KEYCODE) { - trace_keymap_add("normal", keysym, keycode, line); - k->keysym2keycode[keysym] = keycode; - } else { - if (k->extra_count >= MAX_EXTRA_COUNT) { - warn_report("Could not assign keysym %s (0x%x)" - " because of memory constraints.", line, keysym); - } else { - trace_keymap_add("extra", keysym, keycode, line); - k->keysym2keycode_extra[k->extra_count]. - keysym = keysym; - k->keysym2keycode_extra[k->extra_count]. - keycode = keycode; - k->extra_count++; - } +static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) +{ + struct keysym2code *keysym2code; + + keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym)); + if (keysym2code) { + return; } + + keysym2code = g_new0(struct keysym2code, 1); + keysym2code->keycode = keycode; + g_hash_table_replace(k->hash, GINT_TO_POINTER(keysym), keysym2code); + trace_keymap_add(keysym, keycode, line); } static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table, @@ -131,6 +123,7 @@ static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table, if (!k) { k = g_new0(kbd_layout_t, 1); + k->hash = g_hash_table_new(NULL, NULL); } for(;;) { @@ -215,26 +208,22 @@ kbd_layout_t *init_keyboard_layout(const name2keysym_t *table, int keysym2scancode(kbd_layout_t *k, int keysym) { - if (keysym < MAX_NORMAL_KEYCODE) { - if (k->keysym2keycode[keysym] == 0) { - trace_keymap_unmapped(keysym); - warn_report("no scancode found for keysym %d", keysym); - } - return k->keysym2keycode[keysym]; - } else { - int i; + struct keysym2code *keysym2code; + #ifdef XK_ISO_Left_Tab - if (keysym == XK_ISO_Left_Tab) { - keysym = XK_Tab; - } + if (keysym == XK_ISO_Left_Tab) { + keysym = XK_Tab; + } #endif - for (i = 0; i < k->extra_count; i++) { - if (k->keysym2keycode_extra[i].keysym == keysym) { - return k->keysym2keycode_extra[i].keycode; - } - } + + keysym2code = g_hash_table_lookup(k->hash, GINT_TO_POINTER(keysym)); + if (!keysym2code) { + trace_keymap_unmapped(keysym); + warn_report("no scancode found for keysym %d", keysym); + return 0; } - return 0; + + return keysym2code->keycode; } int keycode_is_keypad(kbd_layout_t *k, int keycode) diff --git a/ui/trace-events b/ui/trace-events index 34229e6747..861b68a305 100644 --- a/ui/trace-events +++ b/ui/trace-events @@ -78,7 +78,7 @@ qemu_spice_create_update(uint32_t left, uint32_t right, uint32_t top, uint32_t b # ui/keymaps.c keymap_parse(const char *file) "file %s" -keymap_add(const char *type, int sym, int code, const char *line) "%-6s sym=0x%04x code=0x%04x (line: %s)" +keymap_add(int sym, int code, const char *line) "sym=0x%04x code=0x%04x (line: %s)" keymap_unmapped(int sym) "sym=0x%04x" # ui/x_keymap.c