Skip to contentSkip to conversation

convert sendkey to qapi

PATCH Applied Amos Kong 2 participants 9 messages Aug 31, 2012

lore
Share & tools
lore

Amos Kong

author
#0 lore raw

v8 cover convert sendkey to qapi

This series converted 'sendkey' command to qapi. The raw value in hexadecimal format is not supported by 'send-key' of qmp.

Amos Kong (7):
fix doc of using raw values with sendkey
monitor: rename keyname '<' to 'less'
hmp: rename arguments
qapi: generate list struct and visit_list for enum
qapi: add the QKeyCode enum
monitor: move key_defs[] table and introduce two help functions
qapi: convert sendkey

 console.h             |    5 +
 hmp-commands.hx       |   10 +-
 hmp.c                 |   55 +++++++++++
 hmp.h                 |    1 +
 input.c               |  251 +++++++++++++++++++++++++++++++++++++++++++++++++
 monitor.c             |  245 +-----------------------------------------------
 qapi-schema.json      |   46 +++++++++
 qmp-commands.hx       |   28 ++++++
 scripts/qapi-types.py |   16 +++-
 scripts/qapi-visit.py |   14 +++-
 10 files changed, 421 insertions(+), 250 deletions(-)

---
Changes from v1:
- using a JSON array for the key names
- rename new error to 'QERR_OVERFLOW'
- fix command descriptions
- qapi: generate list struct for enum
- add '<' fixing

Changes from v2:
- fix support of raw value in hexadecimal format
- fix bug in processing of '<-x'
- don't generate useless cleanup functions for enum
- introduced two functions for enum in qapi scripts
- fix command description
- drop keys number limitation in sendkey
- drop patch: qerror: add QERR_OVERFLOW

Changes from v3:
- move key_defs[] to console.h
- link mapping tables by enum values
- rename 'sendkey' to 'send-key' for qmp

Changes from v4:
- rename 'KeyCodes' to 'QKeyCode'
- fix default hold-time
- move qmp_send_key(), key_defs to input.c
- duplicate the keylist in qmp_send_key()
- drop struct KeyDef
- handle invalid key/index in hmp_send_key()

Changes from v5:
- split the addition of the QKeyCode enum and the key_defs table
- split help functions definition
- fix cmd completion and make key_defs static
- drop ps2 trival fix, will post it by another thread

Changes from v6:
- split convert patch to two patches, and do the refactorings in first patch.

Changes from v7:
- rebase patchset to latest upstream/master
- move the addition of QKeyCode out of patch5
- update version to 1.3.0

author
#1 lore raw

PATCH v8 1/7 fix doc of using raw values with sendkey

(qemu) sendkey a
(qemu) sendkey 0x1e
(qemu) sendkey #0x1e
unknown key: '#0x1e'

The last command doesn't work, '#' is not requested before raw values, and the raw value in decimal format is not supported.

---
 hmp-commands.hx |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
hmp-commands.hx +3 -3
index f6104b0..6a70a9c 100644
@@ -512,9 +512,9 @@ STEXI
 @item sendkey @var{keys}
 @findex sendkey
 
-Send @var{keys} to the emulator. @var{keys} could be the name of the
-key or @code{#} followed by the raw value in either decimal or hexadecimal
-format. Use @code{-} to press several keys simultaneously. Example:
+Send @var{keys} to the guest. @var{keys} could be the name of the
+key or the raw value in hexadecimal format. Use @code{-} to press
+several keys simultaneously. Example:
 @example
 sendkey ctrl-alt-f1
 @end example
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH v8 2/7 monitor: rename keyname '<' to 'less'

In reply to Amos Kong

There are many maps of keycode 0x56 in pc-bios/keymaps/*
pc-bios/keymaps/common:less 0x56 pc-bios/keymaps/common:greater 0x56 shift pc-bios/keymaps/common:bar 0x56 altgr pc-bios/keymaps/common:brokenbar 0x56 shift altgr

This patch just renamed '<' to 'less', QAPI might add new variable by adding a prefix to keyname, '$PREFIX_<' is not available, '$PREFIX_less' is ok.

For compatibility, convert user inputted '<' to 'less'.

---
 monitor.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
monitor.c +8 -1
index b17b1bb..c97c120 100644
@@ -1400,7 +1400,7 @@ static const KeyDef key_defs[] = {
     { 0x48, "kp_8" },
     { 0x49, "kp_9" },
 
-    { 0x56, "<" },
+    { 0x56, "less" },
 
     { 0x57, "f11" },
     { 0x58, "f12" },
@@ -1504,6 +1504,13 @@ static void do_sendkey(Monitor *mon, const QDict *qdict)
                 monitor_printf(mon, "too many keys\n");
                 return;
             }
+
+            /* Be compatible with old interface, convert user inputted "<" */
+            if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
+                pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
+                keyname_len = 4;
+            }
+
             keyname_buf[keyname_len] = 0;
             keycode = get_keycode(keyname_buf);
             if (keycode < 0) {
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH v8 3/7 hmp: rename arguments

In reply to Amos Kong

Rename 'string' to 'keys', rename 'hold_time' to 'hold-time'.

---
 hmp-commands.hx |    2 +-
 monitor.c       |   14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)
hmp-commands.hx +1 -1
index 6a70a9c..bd0c6c9 100644
@@ -502,7 +502,7 @@ ETEXI
 
     {
         .name       = "sendkey",
-        .args_type  = "string:s,hold_time:i?",
+        .args_type  = "keys:s,hold-time:i?",
         .params     = "keys [hold_ms]",
         .help       = "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)",
         .mhandler.cmd = do_sendkey,
monitor.c +7 -7
index c97c120..84176eb 100644
@@ -1480,9 +1480,9 @@ static void do_sendkey(Monitor *mon, const QDict *qdict)
     char keyname_buf[16];
     char *separator;
     int keyname_len, keycode, i;
-    const char *string = qdict_get_str(qdict, "string");
-    int has_hold_time = qdict_haskey(qdict, "hold_time");
-    int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
+    const char *keys = qdict_get_str(qdict, "keys");
+    int has_hold_time = qdict_haskey(qdict, "hold-time");
+    int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
 
     if (nb_pending_keycodes > 0) {
         qemu_del_timer(key_timer);
@@ -1492,10 +1492,10 @@ static void do_sendkey(Monitor *mon, const QDict *qdict)
         hold_time = 100;
     i = 0;
     while (1) {
-        separator = strchr(string, '-');
-        keyname_len = separator ? separator - string : strlen(string);
+        separator = strchr(keys, '-');
+        keyname_len = separator ? separator - keys : strlen(keys);
         if (keyname_len > 0) {
-            pstrcpy(keyname_buf, sizeof(keyname_buf), string);
+            pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
             if (keyname_len > sizeof(keyname_buf) - 1) {
                 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
                 return;
@@ -1521,7 +1521,7 @@ static void do_sendkey(Monitor *mon, const QDict *qdict)
         }
         if (!separator)
             break;
-        string = separator + 1;
+        keys = separator + 1;
     }
     nb_pending_keycodes = i;
     /* key down events */
Signature · 1 line
1.7.1
author
#4 lore raw

PATCH v8 4/7 qapi: generate list struct and visit_list for enum

In reply to Amos Kong

Currently, if we define an 'enum' and use it in one command's data, list struct for enum could not be generated, but it's used in qmp function.

For example: KeyCodesList could not be generated.

Quoted 1 line
Quoted 1 line
Quoted 1 line

qapi-schema.json:

{ 'enum': 'KeyCodes',
'data': [ 'shift', 'alt' ... ] }
{ 'command': 'sendkey',
'data': { 'keys': ['KeyCodes'], '*hold-time': 'int' } }

Quoted 1 line
Quoted 1 line
Quoted 1 line

qmp-command.h:

void qmp_sendkey(KeyCodesList * keys, bool has_hold_time, int64_t hold_time, Error **errp);

This patch lets qapi generate list struct and visit_list for enum.

---
 scripts/qapi-types.py |   16 +++++++++++++++-
 scripts/qapi-visit.py |   14 +++++++++++++-
 2 files changed, 28 insertions(+), 2 deletions(-)
scripts/qapi-types.py +15 -1
index cf601ae..49ef569 100644
@@ -28,6 +28,16 @@ typedef struct %(name)sList
 ''',
                  name=name)
 
+def generate_fwd_enum_struct(name, members):
+    return mcgen('''
+typedef struct %(name)sList
+{
+    %(name)s value;
+    struct %(name)sList *next;
+} %(name)sList;
+''',
+                 name=name)
+
 def generate_struct(structname, fieldname, members):
     ret = mcgen('''
 struct %(name)s
@@ -276,7 +286,8 @@ for expr in exprs:
     if expr.has_key('type'):
         ret += generate_fwd_struct(expr['type'], expr['data'])
     elif expr.has_key('enum'):
-        ret += generate_enum(expr['enum'], expr['data'])
+        ret += generate_enum(expr['enum'], expr['data']) + "\n"
+        ret += generate_fwd_enum_struct(expr['enum'], expr['data'])
         fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
     elif expr.has_key('union'):
         ret += generate_fwd_struct(expr['union'], expr['data']) + "\n"
@@ -300,6 +311,9 @@ for expr in exprs:
         fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n")
         ret += generate_type_cleanup_decl(expr['union'])
         fdef.write(generate_type_cleanup(expr['union']) + "\n")
+    elif expr.has_key('enum'):
+        ret += generate_type_cleanup_decl(expr['enum'] + "List")
+        fdef.write(generate_type_cleanup(expr['enum'] + "List") + "\n")
     else:
         continue
     fdecl.write(ret)
scripts/qapi-visit.py +13 -1
index 04ef7c4..cbec24d 100644
@@ -217,6 +217,16 @@ void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name,
 
     return ret
 
+def generate_enum_declaration(name, members, genlist=True):
+    ret = ""
+    if genlist:
+        ret += mcgen('''
+void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
+''',
+                     name=name)
+
+    return ret
+
 def generate_decl_enum(name, members, genlist=True):
     return mcgen('''
 
@@ -335,10 +345,12 @@ for expr in exprs:
         ret += generate_declaration(expr['union'], expr['data'])
         fdecl.write(ret)
     elif expr.has_key('enum'):
-        ret = generate_visit_enum(expr['enum'], expr['data'])
+        ret = generate_visit_list(expr['enum'], expr['data'])
+        ret += generate_visit_enum(expr['enum'], expr['data'])
         fdef.write(ret)
 
         ret = generate_decl_enum(expr['enum'], expr['data'])
+        ret += generate_enum_declaration(expr['enum'], expr['data'])
         fdecl.write(ret)
 
 fdecl.write('''
Signature · 1 line
1.7.1
author
#5 lore raw

PATCH v8 5/7 qapi: add the QKeyCode enum

In reply to Amos Kong

key_defs[] in monitor.c is a mapping table of keys and keycodes, this patch added a QKeyCode enum. Key's index in the enmu is same as keycode's index in key_defs[].

---
 qapi-schema.json |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)
qapi-schema.json +26 -0
index bd8ad74..856e11a 100644
@@ -2493,3 +2493,29 @@
 # Since: 1.2.0
 ##
 { 'command': 'query-target', 'returns': 'TargetInfo' }
+
+##
+# @QKeyCode:
+#
+# An enumeration of key name.
+#
+# This is used by the send-key command.
+#
+# Since: 1.3.0
+##
+{ 'enum': 'QKeyCode',
+  'data': [ 'shift', 'shift_r', 'alt', 'alt_r', 'altgr', 'altgr_r', 'ctrl',
+            'ctrl_r', 'menu', 'esc', '1', '2', '3', '4', '5', '6', '7', '8',
+            '9', '0', 'minus', 'equal', 'backspace', 'tab', 'q', 'w', 'e',
+            'r', 't', 'y', 'u', 'i', 'o', 'p', 'bracket_left', 'bracket_right',
+            'ret', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'semicolon',
+            'apostrophe', 'grave_accent', 'backslash', 'z', 'x', 'c', 'v', 'b',
+            'n', 'm', 'comma', 'dot', 'slash', 'asterisk', 'spc', 'caps_lock',
+            'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10',
+            'num_lock', 'scroll_lock', 'kp_divide', 'kp_multiply',
+            'kp_subtract', 'kp_add', 'kp_enter', 'kp_decimal', 'sysrq', 'kp_0',
+            'kp_1', 'kp_2', 'kp_3', 'kp_4', 'kp_5', 'kp_6', 'kp_7', 'kp_8',
+            'kp_9', 'less', 'f11', 'f12', 'print', 'home', 'pgup', 'pgdn', 'end',
+            'left', 'up', 'down', 'right', 'insert', 'delete', 'stop', 'again',
+            'props', 'undo', 'front', 'copy', 'open', 'paste', 'find', 'cut',
+             'lf', 'help', 'meta_l', 'meta_r', 'compose' ] }
Signature · 1 line
1.7.1
author
#6 lore raw

PATCH v8 6/7 monitor: move key_defs[] table and introduce two help functions

In reply to Amos Kong

This patch added two help functions to convert key/code to index of mapping table, those functions will return Q_KEY_CODE_MAX if the code/key is invalid.

Patch also moved key_defs[] to input.c, and removed useless KeyDef struct. Key's index in QKeyCode enmu is same as keycode's index in new key_defs[]. Monitor functions were changed to access key_defs[] directly.

key_defs[] is used in do_send_key(), so export key_defs[]. It will be changed to static in next patch.

---
 console.h |    6 ++
 input.c   |  186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 monitor.c |  183 ++++--------------------------------------------------------
 3 files changed, 203 insertions(+), 172 deletions(-)
console.h +6 -0
index 4334db5..7934b11 100644
@@ -6,6 +6,7 @@
 #include "notify.h"
 #include "monitor.h"
 #include "trace.h"
+#include "qapi-types.h"
 
 /* keyboard/mouse support */
 
@@ -397,4 +398,9 @@ static inline int vnc_display_pw_expire(DisplayState *ds, time_t expires)
 /* curses.c */
 void curses_display_init(DisplayState *ds, int full_screen);
 
+/* input.c */
+extern const int key_defs[];
+int index_from_key(const char *key);
+int index_from_keycode(int code);
+
 #endif
input.c +186 -0

192 lines not on this page · raw message

monitor.c +11 -172

213 lines not on this page · raw message

Signature · 1 line
1.7.1
author
#7 lore raw

PATCH v8 7/7 qapi: convert sendkey

In reply to Amos Kong

Convert 'sendkey' to use QAPI.

QAPI passes key's index of mapping table to qmp_send_key(), not keycode. So we use help functions to convert key/code to index of key_defs, and 'index' will be converted to 'keycode' inside qmp_send_key().

For qmp, QAPI would check invalid key and raise error. For hmp, invalid key is checked in hmp_send_key().

'send-key' of QMP doesn't support key in hexadecimal format.

---
 console.h        |    1 -
 hmp-commands.hx  |    2 +-
 hmp.c            |   55 ++++++++++++++++++++++++++++++++++
 hmp.h            |    1 +
 input.c          |   67 +++++++++++++++++++++++++++++++++++++++++-
 monitor.c        |   87 ------------------------------------------------------
 qapi-schema.json |   20 ++++++++++++
 qmp-commands.hx  |   28 +++++++++++++++++
 8 files changed, 171 insertions(+), 90 deletions(-)
console.h +0 -1
index 7934b11..c702b23 100644
@@ -399,7 +399,6 @@ static inline int vnc_display_pw_expire(DisplayState *ds, time_t expires)
 void curses_display_init(DisplayState *ds, int full_screen);
 
 /* input.c */
-extern const int key_defs[];
 int index_from_key(const char *key);
 int index_from_keycode(int code);
 
hmp-commands.hx +1 -1
index bd0c6c9..5cee131 100644
@@ -505,7 +505,7 @@ ETEXI
         .args_type  = "keys:s,hold-time:i?",
         .params     = "keys [hold_ms]",
         .help       = "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)",
-        .mhandler.cmd = do_sendkey,
+        .mhandler.cmd = hmp_send_key,
     },
 
 STEXI
hmp.c +55 -0

64 lines not on this page · raw message

hmp.h +1 -0
index 7dd93bf..8e7838c 100644
@@ -71,5 +71,6 @@ void hmp_netdev_add(Monitor *mon, const QDict *qdict);
 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
 void hmp_getfd(Monitor *mon, const QDict *qdict);
 void hmp_closefd(Monitor *mon, const QDict *qdict);
+void hmp_send_key(Monitor *mon, const QDict *qdict);
 
 #endif
input.c +66 -1

85 lines not on this page · raw message

monitor.c +0 -87

99 lines not on this page · raw message

qapi-schema.json +20 -0
index 856e11a..5421382 100644
@@ -2519,3 +2519,23 @@
             'left', 'up', 'down', 'right', 'insert', 'delete', 'stop', 'again',
             'props', 'undo', 'front', 'copy', 'open', 'paste', 'find', 'cut',
              'lf', 'help', 'meta_l', 'meta_r', 'compose' ] }
+
+##
+# @send-key:
+#
+# Send keys to guest.
+#
+# @keys: key sequence. 'keys' is the name of the key. Use a JSON array to
+#        press several keys simultaneously.
+#
+# @hold-time: #optional time to delay key up events, milliseconds. Defaults
+#             to 100
+#
+# Returns: Nothing on success
+#          If key is unknown or redundant, InvalidParameter
+#
+# Since: 1.3.0
+#
+##
+{ 'command': 'send-key',
+  'data': { 'keys': ['QKeyCode'], '*hold-time': 'int' } }
qmp-commands.hx +28 -0
index 3745a21..470f08e 100644
@@ -335,6 +335,34 @@ Example:
 EQMP
 
     {
+        .name       = "send-key",
+        .args_type  = "keys:O,hold-time:i?",
+        .mhandler.cmd_new = qmp_marshal_input_send_key,
+    },
+
+SQMP
+send-key
+----------
+
+Send keys to VM.
+
+Arguments:
+
+keys array:
+    - "key": key sequence (a json-array of key enum values)
+
+- hold-time: time to delay key up events, milliseconds. Defaults to 100
+             (json-int, optional)
+
+Example:
+
+-> { "execute": "send-key",
+     "arguments": { 'keys': [ 'ctrl', 'alt', 'delete' ] } }
+<- { "return": {} }
+
+EQMP
+
+    {
         .name       = "cpu",
         .args_type  = "index:i",
         .mhandler.cmd_new = qmp_marshal_input_cpu,
Signature · 1 line
1.7.1
#8 lore raw
In reply to Amos Kong
Quoted 2 lines · On Fri, 31 Aug 2012 10:56:19 +0800 Amos Kong <[email protected]> wrote:

This series converted 'sendkey' command to qapi. The raw value in hexadecimal format is not supported by 'send-key' of qmp.

I've applied this to the qmp-next branch.

Thanks Amos!

Quoted 67 lines

Amos Kong (7):
fix doc of using raw values with sendkey
monitor: rename keyname '<' to 'less'
hmp: rename arguments
qapi: generate list struct and visit_list for enum
qapi: add the QKeyCode enum
monitor: move key_defs[] table and introduce two help functions
qapi: convert sendkey

 console.h             |    5 +
 hmp-commands.hx       |   10 +-
 hmp.c                 |   55 +++++++++++
 hmp.h                 |    1 +
 input.c               |  251 +++++++++++++++++++++++++++++++++++++++++++++++++
 monitor.c             |  245 +-----------------------------------------------
 qapi-schema.json      |   46 +++++++++
 qmp-commands.hx       |   28 ++++++
 scripts/qapi-types.py |   16 +++-
 scripts/qapi-visit.py |   14 +++-
 10 files changed, 421 insertions(+), 250 deletions(-)

---
Changes from v1:
- using a JSON array for the key names
- rename new error to 'QERR_OVERFLOW'
- fix command descriptions
- qapi: generate list struct for enum
- add '<' fixing

Changes from v2:
- fix support of raw value in hexadecimal format
- fix bug in processing of '<-x'
- don't generate useless cleanup functions for enum
- introduced two functions for enum in qapi scripts
- fix command description
- drop keys number limitation in sendkey
- drop patch: qerror: add QERR_OVERFLOW

Changes from v3:
- move key_defs[] to console.h
- link mapping tables by enum values
- rename 'sendkey' to 'send-key' for qmp

Changes from v4:
- rename 'KeyCodes' to 'QKeyCode'
- fix default hold-time
- move qmp_send_key(), key_defs to input.c
- duplicate the keylist in qmp_send_key()
- drop struct KeyDef
- handle invalid key/index in hmp_send_key()

Changes from v5:
- split the addition of the QKeyCode enum and the key_defs table
- split help functions definition
- fix cmd completion and make key_defs static
- drop ps2 trival fix, will post it by another thread

Changes from v6:
- split convert patch to two patches, and do the refactorings in first patch.

Changes from v7:
- rebase patchset to latest upstream/master
- move the addition of QKeyCode out of patch5
- update version to 1.3.0