Skip to contentSkip to conversation

ui: add keyboard state and hotkey tracker

RFC PATCH Superseded Gerd Hoffmann 3 participants 10 messages Feb 21, 2018 – Jun 5, 2018

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover ui: add keyboard state and hotkey tracker

Ok, here is my alternative approach to hotkey configuration.

Downside: It does *not* allow to freely choose any keys for hotkeys. Hotkeys are restricted to be one normal key combined with up to three modifiers (no modifiers is fine too).

Upside: The code is simpler. We don't have to keep back events because they might be part of a hotkey. The code also supports multiple hotkeys.

While being at it have the code also track all key state, so user interfaces don't have to do that on their own any more.

The sdl2 ui is used to show this in action. No command line option parsing added yet, so hotkeys are not actually configurable for now. The groundwork to change them at runtime is there though.

Gerd Hoffmann (5):
kbd-state: add keyboard state tracker
kbd-state: add hotkey registry
kbd-state: use state tracker for sdl2
kbd-state: register sdl2 hotkeys
sdl2: use only QKeyCode in sdl2_process_key()

 include/ui/kbd-state.h |  49 ++++++++++++++++
 include/ui/sdl2.h      |   2 +
 ui/kbd-state.c         | 152 +++++++++++++++++++++++++++++++++++++++++++++++++
 ui/sdl2-input.c        |  54 ++----------------
 ui/sdl2.c              | 114 ++++++++++++++++++++-----------------
 ui/Makefile.objs       |   2 +-
 6 files changed, 270 insertions(+), 103 deletions(-)
 create mode 100644 include/ui/kbd-state.h
 create mode 100644 ui/kbd-state.c
Signature · 1 line
2.9.3
#1 lore raw

RFC PATCH 1/5 kbd-state: add keyboard state tracker

Now that most user interfaces are using QKeyCodes it is easier to have common keyboard code useable by all user interfaces.

This patch adds helper code to track the state of all keyboard keys, using a bitmap indexed by QKeyCode. Modifier state is tracked too, as separate bitmap. That makes checking modifier state easier. Likewise we can easily apply special handling for capslock & numlock (toggles on keypress) and ctrl + shift (we have two keys for that).

---
 include/ui/kbd-state.h |  22 +++++++++
 ui/kbd-state.c         | 119 +++++++++++++++++++++++++++++++++++++++++++++++++
 ui/Makefile.objs       |   2 +-
 3 files changed, 142 insertions(+), 1 deletion(-)
 create mode 100644 include/ui/kbd-state.h
 create mode 100644 ui/kbd-state.c
include/ui/kbd-state.h new file +22 -0
new file mode 100644
index 0000000000..c961da45b2
@@ -0,0 +1,22 @@
+typedef enum KbdModifier KbdModifier;
+
+enum KbdModifier {
+    KBD_MOD_NONE = 0,
+
+    KBD_MOD_SHIFT,
+    KBD_MOD_CTRL,
+    KBD_MOD_ALT,
+
+    KBD_MOD_NUMLOCK,
+    KBD_MOD_CAPSLOCK,
+
+    KBD_MOD__MAX
+};
+
+typedef struct KbdState KbdState;
+
+bool kbd_state_modifier_get(KbdState *kbd, KbdModifier mod);
+bool kbd_state_key_get(KbdState *kbd, QKeyCode qcode);
+void kbd_state_key_event(KbdState *kbd, QKeyCode qcode, bool down);
+void kbd_state_lift_all_keys(KbdState *kbd);
+KbdState *kbd_state_init(QemuConsole *con);
ui/kbd-state.c new file +119 -0

119 lines not on this page · raw message

ui/Makefile.objs +1 -1
index ced7d91a63..aa81ce4c47 100644
@@ -8,7 +8,7 @@ vnc-obj-y += vnc-ws.o
 vnc-obj-y += vnc-jobs.o
 
 common-obj-y += keymaps.o console.o cursor.o qemu-pixman.o
-common-obj-y += input.o input-keymap.o input-legacy.o
+common-obj-y += input.o input-keymap.o input-legacy.o kbd-state.o
 common-obj-$(CONFIG_LINUX) += input-linux.o
 common-obj-$(CONFIG_SPICE) += spice-core.o spice-input.o spice-display.o
 common-obj-$(CONFIG_SDL) += sdl.mo
Signature · 1 line
2.9.3
#2 lore raw

RFC PATCH 2/5 kbd-state: add hotkey registry

In reply to Gerd Hoffmann

Add support to register hotkeys and to check whenever a given QKeyCode combined with the current modifier state is a hotkey. A hotkey can be any key combined with up to three modifier keys.

---
 include/ui/kbd-state.h | 27 +++++++++++++++++++++++++++
 ui/kbd-state.c         | 41 +++++++++++++++++++++++++++++++++++++----
 2 files changed, 64 insertions(+), 4 deletions(-)
include/ui/kbd-state.h +27 -0
index c961da45b2..3f13649b63 100644
@@ -20,3 +20,30 @@ bool kbd_state_key_get(KbdState *kbd, QKeyCode qcode);
 void kbd_state_key_event(KbdState *kbd, QKeyCode qcode, bool down);
 void kbd_state_lift_all_keys(KbdState *kbd);
 KbdState *kbd_state_init(QemuConsole *con);
+
+/* ------------------------------------------------------------------ */
+
+typedef enum KbdHotkey KbdHotkey;
+
+enum KbdHotkey {
+    KBD_HOTKEY_NONE = 0,
+
+    KBD_HOTKEY_GRAB,
+    KBD_HOTKEY_FULLSCREEN,
+    KBD_HOTKEY_REDRAW,
+
+    KBD_HOTKEY_CONSOLE_1,
+    KBD_HOTKEY_CONSOLE_2,
+    KBD_HOTKEY_CONSOLE_3,
+    KBD_HOTKEY_CONSOLE_4,
+    KBD_HOTKEY_CONSOLE_5,
+    KBD_HOTKEY_CONSOLE_6,
+    KBD_HOTKEY_CONSOLE_7,
+    KBD_HOTKEY_CONSOLE_8,
+    KBD_HOTKEY_CONSOLE_9,
+};
+
+void kbd_state_hotkey_register(KbdState *kbd, KbdHotkey, QKeyCode qcode,
+                               KbdModifier mod1, KbdModifier mod2,
+                               KbdModifier mod3);
+KbdHotkey kbd_state_hotkey_get(KbdState *kbd, QKeyCode qcode);
ui/kbd-state.c +37 -4

60 lines not on this page · raw message

Signature · 1 line
2.9.3
#3 lore raw

Re: [Qemu-devel] [RFC PATCH 2/5] kbd-state: add hotkey registry

Quoted 6 lines · Gerd Hoffmann

Add support to register hotkeys and to check whenever a given QKeyCode combined with the current modifier state is a hotkey. A hotkey can be any key combined with up to three modifier keys.

I have finally reviewed the patches you sent to me. Sorry it took so long. The patches you sent look good.

Quoted 84 lines

---
include/ui/kbd-state.h | 27 +++++++++++++++++++++++++++
ui/kbd-state.c | 41 +++++++++++++++++++++++++++++++++++++----

2 files changed, 64 insertions(+), 4 deletions(-)
include/ui/kbd-state.h +27 -0
index c961da45b2..3f13649b63 100644
@@ -20,3 +20,30 @@ bool kbd_state_key_get(KbdState *kbd, QKeyCode qcode);
 void kbd_state_key_event(KbdState *kbd, QKeyCode qcode, bool down);
 void kbd_state_lift_all_keys(KbdState *kbd);
 KbdState *kbd_state_init(QemuConsole *con);
+
+/* ------------------------------------------------------------------ */
+
+typedef enum KbdHotkey KbdHotkey;
+
+enum KbdHotkey {
+    KBD_HOTKEY_NONE = 0,
+
+    KBD_HOTKEY_GRAB,
+    KBD_HOTKEY_FULLSCREEN,
+    KBD_HOTKEY_REDRAW,
+
+    KBD_HOTKEY_CONSOLE_1,
+    KBD_HOTKEY_CONSOLE_2,
+    KBD_HOTKEY_CONSOLE_3,
+    KBD_HOTKEY_CONSOLE_4,
+    KBD_HOTKEY_CONSOLE_5,
+    KBD_HOTKEY_CONSOLE_6,
+    KBD_HOTKEY_CONSOLE_7,
+    KBD_HOTKEY_CONSOLE_8,
+    KBD_HOTKEY_CONSOLE_9,
+};
+
+void kbd_state_hotkey_register(KbdState *kbd, KbdHotkey, QKeyCode qcode,
+                               KbdModifier mod1, KbdModifier mod2,
+                               KbdModifier mod3);
+KbdHotkey kbd_state_hotkey_get(KbdState *kbd, QKeyCode qcode);
ui/kbd-state.c +14 -4
index 7a9fe268c2..812cb368e3 100644
@@ -6,20 +6,20 @@
 #include "ui/input.h"
 #include "ui/kbd-state.h"
 
-typedef struct KbdHotkey KbdHotkey;
+typedef struct KbdHotkeyEntry KbdHotkeyEntry;
 
-struct KbdHotkey {
+struct KbdHotkeyEntry {
    uint32_t id;
    QKeyCode qcode;
    DECLARE_BITMAP(mods, KBD_MOD__MAX);
-    QTAILQ_ENTRY(KbdHotkey) next;
+    QTAILQ_ENTRY(KbdHotkeyEntry) next;
 };
 
 struct KbdState {
    QemuConsole *con;
    DECLARE_BITMAP(keys, Q_KEY_CODE__MAX);
    DECLARE_BITMAP(mods, KBD_MOD__MAX);
-    QTAILQ_HEAD(,KbdHotkey) hotkeys;
+    QTAILQ_HEAD(, KbdHotkeyEntry) hotkeys;
 };
 
 static void kbd_state_modifier_update(KbdState *kbd,
@@ -117,3 +117,36 @@ KbdState *kbd_state_init(QemuConsole *con)
 
    return kbd;
 }
+
+void kbd_state_hotkey_register(KbdState *kbd, KbdHotkey id, QKeyCode qcode,
+                               KbdModifier mod1, KbdModifier mod2,
+                               KbdModifier mod3)
+{
+    KbdHotkeyEntry *hotkey = g_new0(KbdHotkeyEntry, 1);
+
+    hotkey->id    = id;
+    hotkey->qcode = qcode;
+    if (mod1 != KBD_MOD_NONE) {

ui/kbd-state.c KbdState *kbd_state_init(QemuConsole *con) · line 139

Quoted 10 lines
ui/kbd-state.c +10 -0
+        set_bit(mod1, hotkey->mods);
+    }
+    if (mod2 != KBD_MOD_NONE) {
+        set_bit(mod2, hotkey->mods);
+    }
+    if (mod3 != KBD_MOD_NONE) {
+        set_bit(mod3, hotkey->mods);
+    }
+    QTAILQ_INSERT_TAIL(&kbd->hotkeys, hotkey, next);
+}

You say the hotkey is a QKeyCode and a modifier key combined. But it looks like a single QKeyCode would be supported. I would prefer being able to use a single key like F16 to ungrab the mouse. Would that be possible here?

Kbd_state_hotkey_register(kbd, KBD_HOTKEY_GRAB, Q_KEY_CODE_F16, KBD_MOD_NONE, KBD_MOD_NONE, KBD_MOD_NONE);

Quoted 16 lines
ui/kbd-state.c +13 -0
+
+KbdHotkey kbd_state_hotkey_get(KbdState *kbd, QKeyCode qcode)
+{
+    KbdHotkeyEntry *hotkey;
+
+    QTAILQ_FOREACH(hotkey, &kbd->hotkeys, next) {
+        if (qcode == hotkey->qcode &&
+            bitmap_equal(kbd->mods, hotkey->mods, KBD_MOD__MAX)) {
+            return hotkey->id;
+        }
+    }
+    return KBD_HOTKEY_NONE;
+}
Signature · 1 line
2.9.3
#4 lore raw

Re: [Qemu-devel] [RFC PATCH 2/5] kbd-state: add hotkey registry

Hi,

Quoted 3 lines

You say the hotkey is a QKeyCode and a modifier key combined. But it looks like a single QKeyCode would be supported. I would prefer being able to use a single key like F16 to ungrab the mouse. Would that be possible here?

Kbd_state_hotkey_register(kbd, KBD_HOTKEY_GRAB, Q_KEY_CODE_F16, KBD_MOD_NONE, KBD_MOD_NONE, KBD_MOD_NONE);

Yes, that should work. I'm working on the series now and then (when I find some time), latest state is here:

https://git.kraxel.org/cgit/qemu/log/?h=sirius/kbd-state

cheers,
Gerd

#5 lore raw

Re: [Qemu-devel] [RFC PATCH 2/5] kbd-state: add hotkey registry

Quoted 9 lines · Gerd Hoffmann

Hi,

Quoted 3 lines

You say the hotkey is a QKeyCode and a modifier key combined. But it looks like a single QKeyCode would be supported. I would prefer being able to use a single key like F16 to ungrab the mouse. Would that be possible here?

Kbd_state_hotkey_register(kbd, KBD_HOTKEY_GRAB, Q_KEY_CODE_F16, KBD_MOD_NONE, KBD_MOD_NONE, KBD_MOD_NONE);

Quoted 7 lines

Yes, that should work. I'm working on the series now and then (when I find some time), latest state is here:

https://git.kraxel.org/cgit/qemu/log/?h=sirius/kbd-state

cheers,
Gerd

Great. I look forward to seeing the completed series. I think we should add the code for specifying the command-line option for setting custom keys to this series if possible.

#6 lore raw

RFC PATCH 3/5 kbd-state: use state tracker for sdl2

In reply to Gerd Hoffmann

Use the new keyboard state tracked for sdl2. We can drop the modifier state tracking from sdl2. Also keyup code is simpler, the state tracker will take care to not send suspious keyup events to the guest.

---
 include/ui/sdl2.h |  2 ++
 ui/sdl2-input.c   | 43 ++-----------------------------------------
 ui/sdl2.c         | 18 +++---------------
 3 files changed, 7 insertions(+), 56 deletions(-)
include/ui/sdl2.h +2 -0
index 51084e6320..c316e42706 100644
@@ -7,6 +7,7 @@
 #include <SDL.h>
 #include <SDL_syswm.h>
 
+#include "ui/kbd-state.h"
 #ifdef CONFIG_OPENGL
 # include "ui/egl-helpers.h"
 #endif
@@ -26,6 +27,7 @@ struct sdl2_console {
     int idle_counter;
     int ignore_hotkeys;
     SDL_GLContext winctx;
+    KbdState *kbd;
 #ifdef CONFIG_OPENGL
     QemuGLShader *gls;
     egl_fb guest_fb;
ui/sdl2-input.c +2 -41

55 lines not on this page · raw message

ui/sdl2.c +3 -15
index 6e96a4a24c..50f5fca0f6 100644
@@ -40,7 +40,6 @@ static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
 static int gui_saved_grab;
 static int gui_fullscreen;
 static int gui_key_modifier_pressed;
-static int gui_keysym;
 static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
 static SDL_Cursor *sdl_cursor_normal;
 static SDL_Cursor *sdl_cursor_hidden;
@@ -331,6 +330,7 @@ static void handle_keydown(SDL_Event *ev)
 {
     int win;
     struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
+    int gui_keysym = 0;
 
     gui_key_modifier_pressed = get_mod_state();
 
@@ -413,23 +413,10 @@ static void handle_keydown(SDL_Event *ev)
 
 static void handle_keyup(SDL_Event *ev)
 {
-    int mod_state;
     struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
 
     scon->ignore_hotkeys = false;
-
-    if (!alt_grab) {
-        mod_state = (ev->key.keysym.mod & gui_grab_code);
-    } else {
-        mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
-    }
-    if (!mod_state && gui_key_modifier_pressed) {
-        gui_key_modifier_pressed = 0;
-        gui_keysym = 0;
-    }
-    if (!gui_keysym) {
-        sdl2_process_key(scon, &ev->key);
-    }
+    sdl2_process_key(scon, &ev->key);
 }
 
 static void handle_textinput(SDL_Event *ev)
@@ -829,6 +816,7 @@ void sdl_display_init(DisplayState *ds, DisplayOptions *o)
         sdl2_console[i].dcl.ops = &dcl_2d_ops;
 #endif
         sdl2_console[i].dcl.con = con;
+        sdl2_console[i].kbd = kbd_state_init(con);
         register_displaychangelistener(&sdl2_console[i].dcl);
 
 #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
Signature · 1 line
2.9.3
#7 lore raw

RFC PATCH 4/5 kbd-state: register sdl2 hotkeys

In reply to Gerd Hoffmann

Register SDL2 hotkeys in keyboard state tracker, use it to check for hotkeys. Drop even more modifier code.

This also changes behavior a bit. The Ctrl-Alt-<key> hotkey combinations accept both ctrl keys now. Likewise the Ctrl-Alt-Shift-<key> mode you can enable with -alt-grab accepts both ctrl and shift keys. The -ctrl-grab mode (use right ctrl key) is not supported.

---
 ui/sdl2-input.c |  1 -
 ui/sdl2.c       | 98 ++++++++++++++++++++++++++++++++++-----------------------
 2 files changed, 59 insertions(+), 40 deletions(-)
ui/sdl2-input.c +0 -1
index d970655616..82ec375ea2 100644
@@ -61,7 +61,6 @@ void sdl2_process_key(struct sdl2_console *scon,
                 break;
             }
         }
-        return;
     }
 
     kbd_state_key_event(scon->kbd, qcode, ev->type == SDL_KEYDOWN);
ui/sdl2.c +59 -39

169 lines not on this page · raw message

Signature · 1 line
2.9.3
#8 lore raw

RFC PATCH 5/5 sdl2: use only QKeyCode in sdl2_process_key()

In reply to Gerd Hoffmann

Small cleanup. Also drop the special backspace handling, kbd_put_qcode_console() learned to handle that meanwhile. And sdl2_process_key is never called with scon == NULL.

---
 ui/sdl2-input.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
ui/sdl2-input.c +3 -7
index 82ec375ea2..f0847dd745 100644
@@ -39,23 +39,19 @@ void sdl2_process_key(struct sdl2_console *scon,
                       SDL_KeyboardEvent *ev)
 {
     int qcode;
-    QemuConsole *con = scon ? scon->dcl.con : NULL;
+    QemuConsole *con = scon->dcl.con;
 
     if (ev->keysym.scancode >= qemu_input_map_usb_to_qcode_len) {
         return;
     }
-
     qcode = qemu_input_map_usb_to_qcode[ev->keysym.scancode];
 
     if (!qemu_console_is_graphic(con)) {
         if (ev->type == SDL_KEYDOWN) {
-            switch (ev->keysym.scancode) {
-            case SDL_SCANCODE_RETURN:
+            switch (qcode) {
+            case Q_KEY_CODE_RET:
                 kbd_put_keysym_console(con, '\n');
                 break;
-            case SDL_SCANCODE_BACKSPACE:
-                kbd_put_keysym_console(con, QEMU_KEY_BACKSPACE);
-                break;
             default:
                 kbd_put_qcode_console(con, qcode);
                 break;
Signature · 1 line
2.9.3
#9 lore raw
In reply to Gerd Hoffmann
This series seems to have some coding style problems. See output below for more information:

Hi,

This series seems to have some coding style problems. See output below for more information:

Type: series
Message-id: [email protected]
Subject: [Qemu-devel] [RFC PATCH 0/5] ui: add keyboard state and hotkey tracker

=== TEST SCRIPT BEGIN ===
#!/bin/bash

BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0

git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram

commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do

    echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
    if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
        failed=1
        echo
    fi
    n=$((n+1))

done

exit $failed
=== TEST SCRIPT END ===

Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
9065fbc367 sdl2: use only QKeyCode in sdl2_process_key() 0c65c8c6bb kbd-state: register sdl2 hotkeys 67a2d8cd4a kbd-state: use state tracker for sdl2 a389e7e994 kbd-state: add hotkey registry d14a0b4fd8 kbd-state: add keyboard state tracker

=== OUTPUT BEGIN ===
Checking PATCH 1/5: kbd-state: add keyboard state tracker...
ERROR: space required after that ',' (ctx:BxV) #86: FILE: ui/kbd-state.c:22:

+    QTAILQ_HEAD(,KbdHotkey) hotkeys;
                 ^

total: 1 errors, 0 warnings, 149 lines checked

Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.

Checking PATCH 2/5: kbd-state: add hotkey registry... Checking PATCH 3/5: kbd-state: use state tracker for sdl2... Checking PATCH 4/5: kbd-state: register sdl2 hotkeys... Checking PATCH 5/5: sdl2: use only QKeyCode in sdl2_process_key()... === OUTPUT END ===

Test command exited with code: 1

---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]