[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v2 2/5] keymap: use glib hash for kbd_layout_t
From: |
Daniel P . Berrangé |
Subject: |
Re: [Qemu-devel] [PATCH v2 2/5] keymap: use glib hash for kbd_layout_t |
Date: |
Wed, 21 Feb 2018 14:38:00 +0000 |
User-agent: |
Mutt/1.9.2 (2017-12-15) |
On Tue, Feb 20, 2018 at 04:03:54PM +0100, Gerd Hoffmann wrote:
> 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 <address@hidden>
> ---
> ui/keymaps.c | 79
> +++++++++++++++++++++++++++------------------------------
> ui/trace-events | 2 +-
> 2 files changed, 38 insertions(+), 43 deletions(-)
>
> diff --git a/ui/keymaps.c b/ui/keymaps.c
> index 134958a197..bef1405576 100644
> --- a/ui/keymaps.c
> +++ b/ui/keymaps.c
> @@ -28,26 +28,28 @@
> #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;
> };
>
> +static inline gpointer hashkey(int keysym)
> +{
> + intptr_t ptr = keysym;
> + return (gpointer)(ptr);
> +}
> +
> static int get_keysym(const name2keysym_t *table,
> const char *name)
> {
> @@ -91,23 +93,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, hashkey(keysym));
FYI, the glib2 GINT_TO_POINTER() macro is intended to cover this
usecase of storing integers as hash keys.
Regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
- [Qemu-devel] [PATCH v2 0/5] keymap: support kbd layouts with multiple mappings for the same key., Gerd Hoffmann, 2018/02/20
- [Qemu-devel] [PATCH v2 2/5] keymap: use glib hash for kbd_layout_t, Gerd Hoffmann, 2018/02/20
- Re: [Qemu-devel] [PATCH v2 2/5] keymap: use glib hash for kbd_layout_t,
Daniel P . Berrangé <=
- [Qemu-devel] [PATCH v2 4/5] keymap: record multiple keysym -> keycode mappings, Gerd Hoffmann, 2018/02/20
- [Qemu-devel] [PATCH v2 1/5] keymap: make struct kbd_layout_t private to ui/keymaps.c, Gerd Hoffmann, 2018/02/20
- [Qemu-devel] [PATCH v2 5/5] keymap: consider modifier state when picking a mapping, Gerd Hoffmann, 2018/02/20
- [Qemu-devel] [PATCH v2 3/5] keymap: numpad keysyms and keycodes are fixed, Gerd Hoffmann, 2018/02/20