From patchwork Wed Jun 20 04:47:40 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amos Kong X-Patchwork-Id: 165881 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E60E8B701D for ; Wed, 20 Jun 2012 15:08:49 +1000 (EST) Received: from localhost ([::1]:39173 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ShCpk-0001Wj-8R for incoming@patchwork.ozlabs.org; Wed, 20 Jun 2012 00:48:00 -0400 Received: from eggs.gnu.org ([208.118.235.92]:54702) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ShCpO-00019S-Ch for qemu-devel@nongnu.org; Wed, 20 Jun 2012 00:47:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ShCpL-0002yo-MA for qemu-devel@nongnu.org; Wed, 20 Jun 2012 00:47:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:62635) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ShCpL-0002xp-D9 for qemu-devel@nongnu.org; Wed, 20 Jun 2012 00:47:35 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q5K4lXCZ029814 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 20 Jun 2012 00:47:34 -0400 Received: from dhcp-8-167.nay.redhat.com ([10.66.7.126]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q5K4lFd1023684; Wed, 20 Jun 2012 00:47:31 -0400 From: Amos Kong To: aliguori@us.ibm.com, eblake@redhat.com, berrange@redhat.com, lcapitulino@redhat.com, qemu-devel@nongnu.org Date: Wed, 20 Jun 2012 12:47:40 +0800 Message-Id: <1340167661-25499-6-git-send-email-akong@redhat.com> In-Reply-To: <1340167661-25499-1-git-send-email-akong@redhat.com> References: <1340167661-25499-1-git-send-email-akong@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: Amos Kong Subject: [Qemu-devel] [PATCH v3 5/6] qapi: convert sendkey X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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. Signed-off-by: Amos Kong --- 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(-) diff --git a/hmp-commands.hx b/hmp-commands.hx index e336251..fee4c14 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -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 diff --git a/hmp.c b/hmp.c index b9cec1d..846e8b9 100644 --- a/hmp.c +++ b/hmp.c @@ -19,6 +19,7 @@ #include "qemu-timer.h" #include "qmp-commands.h" #include "monitor.h" +#include "qapi-types.h" static void hmp_handle_error(Monitor *mon, Error **errp) { @@ -1000,3 +1001,47 @@ void hmp_netdev_del(Monitor *mon, const QDict *qdict) qmp_netdev_del(id, &err); hmp_handle_error(mon, &err); } + +void hmp_sendkey(Monitor *mon, const QDict *qdict) +{ + const char *keys = qdict_get_str(qdict, "keys"); + KeyCodesList *keylist, *head = NULL, *tmp = NULL; + int has_hold_time = qdict_haskey(qdict, "hold-time"); + int hold_time = qdict_get_try_int(qdict, "hold-time", -1); + Error *err = NULL; + char keyname_buf[16]; + + char *separator; + int keyname_len; + + while (1) { + separator = strchr(keys, '-'); + keyname_len = separator ? separator - keys : strlen(keys); + pstrcpy(keyname_buf, sizeof(keyname_buf), keys); + + /* 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; + + keylist = g_malloc0(sizeof(*keylist)); + keylist->value = get_key_index(keyname_buf); + keylist->next = NULL; + + if (tmp) + tmp->next = keylist; + tmp = keylist; + if (!head) + head = keylist; + + if (!separator) + break; + keys = separator + 1; + } + + qmp_sendkey(head, has_hold_time, hold_time, &err); + hmp_handle_error(mon, &err); + qapi_free_KeyCodesList(head); +} diff --git a/hmp.h b/hmp.h index 79d138d..bcc74d2 100644 --- a/hmp.h +++ b/hmp.h @@ -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 diff --git a/monitor.c b/monitor.c index 6fa0104..863c0c6 100644 --- a/monitor.c +++ b/monitor.c @@ -1470,98 +1470,84 @@ static const KeyDef key_defs[] = { { 0, NULL }, }; -static int get_keycode(const char *key) +int get_key_index(const char *key) { - const KeyDef *p; + int i, keycode; char *endp; - int ret; + const KeyDef *p; - for(p = key_defs; p->name != NULL; p++) { - if (!strcmp(key, p->name)) - return p->keycode; + for (i = 0; KeyCodes_lookup[i] != NULL; i++) { + if (!strcmp(key, KeyCodes_lookup[i])) + return i; } + if (strstart(key, "0x", NULL)) { - ret = strtoul(key, &endp, 0); - if (*endp == '\0' && ret >= 0x01 && ret <= 0xff) - return ret; + keycode = strtoul(key, &endp, 0); + if (*endp == '\0' && keycode >= 0x01 && keycode <= 0xff) + i = 0; + for (p = key_defs; p->name != NULL; p++) { + if (keycode == p->keycode) + return i; + i++; + } } + return -1; } -#define MAX_KEYCODES 16 -static uint8_t keycodes[MAX_KEYCODES]; -static int nb_pending_keycodes; +static KeyCodesList *keycodes; static QEMUTimer *key_timer; static void release_keys(void *opaque) { int keycode; - while (nb_pending_keycodes > 0) { - nb_pending_keycodes--; - keycode = keycodes[nb_pending_keycodes]; + while (keycodes != NULL) { + if (keycodes->value > sizeof(key_defs) / sizeof(*(key_defs))) { + keycodes = NULL; + break; + } + + keycode = key_defs[keycodes->value].keycode; if (keycode & 0x80) kbd_put_keycode(0xe0); kbd_put_keycode(keycode | 0x80); + + keycodes = keycodes->next; } } -static void do_sendkey(Monitor *mon, const QDict *qdict) +void qmp_sendkey(KeyCodesList *keys, bool has_hold_time, int64_t hold_time, + Error **errp) { - char keyname_buf[16]; - char *separator; - int keyname_len, keycode, i; - 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); + int keycode; + char value[5]; - if (nb_pending_keycodes > 0) { + if (keycodes != NULL) { qemu_del_timer(key_timer); release_keys(NULL); } if (!has_hold_time) hold_time = 100; - i = 0; - while (1) { - separator = strchr(keys, '-'); - keyname_len = separator ? separator - keys : strlen(keys); - if (keyname_len > 0) { - pstrcpy(keyname_buf, sizeof(keyname_buf), keys); - if (keyname_len > sizeof(keyname_buf) - 1) { - monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf); - return; - } - if (i == MAX_KEYCODES) { - 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) { - monitor_printf(mon, "unknown key: '%s'\n", keyname_buf); - return; - } - keycodes[i++] = keycode; + keycodes = keys; + while (keycodes != NULL) { + if (keycodes->value > sizeof(key_defs) / sizeof(*(key_defs))) { + sprintf(value, "%d", keycodes->value); + error_set(errp, QERR_INVALID_PARAMETER, value); + return; } - if (!separator) - break; - keys = separator + 1; - } - nb_pending_keycodes = i; - /* key down events */ - for (i = 0; i < nb_pending_keycodes; i++) { - keycode = keycodes[i]; + + /* key down events */ + keycode = key_defs[keycodes->value].keycode; if (keycode & 0x80) kbd_put_keycode(0xe0); kbd_put_keycode(keycode & 0x7f); + + keycodes = keycodes->next; } + keycodes = keys; + /* delayed key up events */ qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) + muldiv64(get_ticks_per_sec(), hold_time, 1000)); diff --git a/monitor.h b/monitor.h index 5f4de1b..1723622 100644 --- a/monitor.h +++ b/monitor.h @@ -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 */ diff --git a/qapi-schema.json b/qapi-schema.json index 3b6e346..5717467 100644 --- a/qapi-schema.json +++ b/qapi-schema.json @@ -1862,3 +1862,48 @@ # Since: 0.14.0 ## { 'command': 'netdev_del', 'data': {'id': 'str'} } + +## +# @KeyCodes: +# +# An enumeration of key name. +# +# This is used by the sendkey command. +# +# Since: 1.2 +## +{ 'enum': 'KeyCodes', + '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' ] } + +## +# @sendkey: +# +# Send keys to VM. +# +# @keys: key sequence +# @hold-time: time to delay key up events, milliseconds +# +# Returns: Nothing on success +# If key is unknown or redundant, QERR_INVALID_PARAMETER +# +# Notes: Send keys to the guest. 'keys' could be the name of the +# key. Use a JSON array to press several keys simultaneously. +# +# Since: 1.2 +## +{ 'command': 'sendkey', + 'data': { 'keys': ['KeyCodes'], '*hold-time': 'int' } } diff --git a/qmp-commands.hx b/qmp-commands.hx index 2e1a38e..bcc6c4b 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -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,