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

Gerd Hoffmann posted 5 patches 7 years, 9 months ago
[Qemu-devel] [RFC PATCH 2/5] kbd-state: add hotkey registry
Posted by Gerd Hoffmann 7 years, 9 months ago
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.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 include/ui/kbd-state.h | 27 +++++++++++++++++++++++++++
 ui/kbd-state.c         | 41 +++++++++++++++++++++++++++++++++++++----
 2 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/include/ui/kbd-state.h b/include/ui/kbd-state.h
index c961da45b2..3f13649b63 100644
--- a/include/ui/kbd-state.h
+++ b/include/ui/kbd-state.h
@@ -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);
diff --git a/ui/kbd-state.c b/ui/kbd-state.c
index 7a9fe268c2..812cb368e3 100644
--- a/ui/kbd-state.c
+++ b/ui/kbd-state.c
@@ -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) {
+        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);
+}
+
+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;
+}
-- 
2.9.3


Re: [Qemu-devel] [RFC PATCH 2/5] kbd-state: add hotkey registry
Posted by Programmingkid 7 years, 6 months ago
> On Feb 21, 2018, at 12:08 PM, Gerd Hoffmann <kraxel@redhat.com> wrote:
> 
> 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. 

> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
> include/ui/kbd-state.h | 27 +++++++++++++++++++++++++++
> ui/kbd-state.c         | 41 +++++++++++++++++++++++++++++++++++++----
> 2 files changed, 64 insertions(+), 4 deletions(-)
> 
> diff --git a/include/ui/kbd-state.h b/include/ui/kbd-state.h
> index c961da45b2..3f13649b63 100644
> --- a/include/ui/kbd-state.h
> +++ b/include/ui/kbd-state.h
> @@ -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);
> diff --git a/ui/kbd-state.c b/ui/kbd-state.c
> index 7a9fe268c2..812cb368e3 100644
> --- a/ui/kbd-state.c
> +++ b/ui/kbd-state.c
> @@ -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) {
> +        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);

> +
> +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;
> +}
> -- 
> 2.9.3
> 

 


Re: [Qemu-devel] [RFC PATCH 2/5] kbd-state: add hotkey registry
Posted by Gerd Hoffmann 7 years, 6 months ago
  Hi,

> 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


Re: [Qemu-devel] [RFC PATCH 2/5] kbd-state: add hotkey registry
Posted by Programmingkid 7 years, 6 months ago
> On Jun 5, 2018, at 6:20 AM, Gerd Hoffmann <kraxel@redhat.com> wrote:
> 
>  Hi,
> 
>> 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

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.