Skip to contentSkip to conversation

convert sendkey to qapi

PATCH Superseded Amos Kong 2 participants 8 messages Jun 20, 2012

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Amos Kong

author
#0 lore raw

v3 cover convert sendkey to qapi

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

Amos Kong (6):
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: convert sendkey
ps2: output warning when event queue full

 hmp-commands.hx       |   10 ++--
 hmp.c                 |   45 ++++++++++++++++++++++
 hmp.h                 |    1 +
 hw/ps2.c              |    4 +-
 monitor.c             |   99 +++++++++++++++++++++++--------------------------
 monitor.h             |    2 +
 qapi-schema.json      |   45 ++++++++++++++++++++++
 qmp-commands.hx       |   27 +++++++++++++
 scripts/qapi-types.py |   16 +++++++-
 scripts/qapi-visit.py |   14 ++++++-
 10 files changed, 202 insertions(+), 61 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

author
#1 lore raw

PATCH v3 1/6 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 also not supported.

---
 hmp-commands.hx |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
hmp-commands.hx +3 -3
index f5d9d91..30243b6 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 v3 2/6 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 renames '<' to 'less', QAPI would 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 f6107ba..e87677d 100644
@@ -1431,7 +1431,7 @@ static const KeyDef key_defs[] = {
     { 0x48, "kp_8" },
     { 0x49, "kp_9" },
 
-    { 0x56, "<" },
+    { 0x56, "less" },
 
     { 0x57, "f11" },
     { 0x58, "f12" },
@@ -1535,6 +1535,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 v3 3/6 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 30243b6..e336251 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 e87677d..6fa0104 100644
@@ -1511,9 +1511,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);
@@ -1523,10 +1523,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;
@@ -1552,7 +1552,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 v3 4/6 qapi: generate list struct and visit_list for enum

In reply to Amos Kong

Currently, if 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 makes qapi can generate List struct 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 4a734f5..648eac7 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
@@ -265,7 +275,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"
@@ -289,6 +300,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 8d4e94a..4d282d5 100644
@@ -175,6 +175,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('''
 
@@ -293,10 +303,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 v3 5/6 qapi: convert sendkey

In reply to Amos Kong

Convert 'sendkey' to use QAPI. do_sendkey() depends on some variables/functions in monitor.c, so reserve qmp_sendkey() to monitor.c

key_defs[] in monitor.c is the mapping of key name to keycode, Keys' order in the enmu and key_defs[] is same.

---
 hmp-commands.hx  |    2 +-
 hmp.c            |   45 ++++++++++++++++++++++++
 hmp.h            |    1 +
 monitor.c        |  102 +++++++++++++++++++++++------------------------------
 monitor.h        |    2 +
 qapi-schema.json |   45 ++++++++++++++++++++++++
 qmp-commands.hx  |   27 ++++++++++++++
 7 files changed, 165 insertions(+), 59 deletions(-)
hmp-commands.hx +1 -1
index e336251..fee4c14 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_sendkey,
     },
 
 STEXI
hmp.c +45 -0

54 lines not on this page · raw message

hmp.h +1 -0
index 79d138d..bcc74d2 100644
@@ -64,5 +64,6 @@ void hmp_device_del(Monitor *mon, const QDict *qdict);
 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict);
 void hmp_netdev_add(Monitor *mon, const QDict *qdict);
 void hmp_netdev_del(Monitor *mon, const QDict *qdict);
+void hmp_sendkey(Monitor *mon, const QDict *qdict);
 
 #endif
monitor.c +44 -58

142 lines not on this page · raw message

monitor.h +2 -0
index 5f4de1b..1723622 100644
@@ -86,4 +86,6 @@ int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret);
 
 int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret);
 
+int get_key_index(const char *key);
+
 #endif /* !MONITOR_H */
qapi-schema.json +45 -0

48 lines not on this page · raw message

qmp-commands.hx +27 -0
index 2e1a38e..bcc6c4b 100644
@@ -335,6 +335,33 @@ Example:
 EQMP
 
     {
+        .name       = "sendkey",
+        .args_type  = "keys:O,hold-time:i?",
+        .mhandler.cmd_new = qmp_marshal_input_sendkey,
+    },
+
+SQMP
+sendkey
+----------
+
+Send keys to VM.
+
+Arguments:
+
+keys array:
+    - "key": key sequence (json-string)
+
+- hold-time: time to delay key up events, milliseconds (josn-int, optional)
+
+Example:
+
+-> { "execute": "sendkey",
+     "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
#6 lore raw

Re: [Qemu-devel] [PATCH v3 5/6] qapi: convert sendkey

Quoted 7 lines · Amos Kong

Convert 'sendkey' to use QAPI. do_sendkey() depends on some variables/functions in monitor.c, so reserve qmp_sendkey() to monitor.c

key_defs[] in monitor.c is the mapping of key name to keycode, Keys' order in the enmu and key_defs[] is same.

The interface looks like we have settled into something useful that libvirt can live with. I will leave a more thorough review of the series to those more familiar with qemu's internals. However, as long as we are fine-tuning things, I have some questions that might be worth one more spin:

Review

Quoted 2 lines
(quoted diff) +2 -0
+##
+# @sendkey:

Should this be named 'send-key', given that most QMP commands separate words with '-' rather than squasheverythingtogether?

qmp-commands.hx Example: · line 352

Quoted 10 lines
qmp-commands.hx +10 -0
+SQMP
+sendkey
+----------
+
+Send keys to VM.
+
+Arguments:
+
+keys array:
+    - "key": key sequence (json-string)

I'm not sure if json-string is the right designation any more. Rather, it is a json-array of key enum values.

Quoted 2 lines

+
+- hold-time: time to delay key up events, milliseconds (josn-int, optional)

s/josn/json/

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#7 lore raw

PATCH v3 6/6 ps2: output warning when event queue full

In reply to Amos Kong

Event would be ignored if ps2 queue is full, this patch added a warning in ignore path.

---
 hw/ps2.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
hw/ps2.c +3 -1
index f93cd24..799c36b 100644
@@ -137,8 +137,10 @@ void ps2_queue(void *opaque, int b)
     PS2State *s = (PS2State *)opaque;
     PS2Queue *q = &s->queue;
 
-    if (q->count >= PS2_QUEUE_SIZE)
+    if (q->count >= PS2_QUEUE_SIZE) {
+        fprintf(stderr, "ps2: warning: event queue full\n");
         return;
+    }
     q->data[q->wptr] = b;
     if (++q->wptr == PS2_QUEUE_SIZE)
         q->wptr = 0;
Signature · 1 line
1.7.1