From b0f4cf25a6c17907d16523e0fa3c10e253f81f8b Mon Sep 17 00:00:00 2001 From: Kristina Hanicova Date: Thu, 18 Feb 2021 19:11:22 +0100 Subject: [PATCH] virsh: Add virshKeycodeNameCompleter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kristina Hanicova Reviewed-by: Ján Tomko Signed-off-by: Ján Tomko --- tools/virsh-completer-domain.c | 72 ++++++++++++++++++++++++++++++++++ tools/virsh-completer-domain.h | 4 ++ tools/virsh-domain.c | 1 + 3 files changed, 77 insertions(+) diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c index fc4d7b2e52..15993064fa 100644 --- a/tools/virsh-completer-domain.c +++ b/tools/virsh-completer-domain.c @@ -32,6 +32,9 @@ #include "virperf.h" #include "virbitmap.h" #include "virkeycode.h" +#include "virkeynametable_linux.h" +#include "virkeynametable_osx.h" +#include "virkeynametable_win32.h" char ** virshDomainNameCompleter(vshControl *ctl, @@ -800,3 +803,72 @@ virshCodesetNameCompleter(vshControl *ctl G_GNUC_UNUSED, return g_steal_pointer(&tmp); } + + +char ** +virshKeycodeNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags) +{ + g_auto(GStrv) tmp = NULL; + size_t i = 0; + size_t j = 0; + const char *codeset_option; + int codeset; + const char **names = NULL; + size_t len; + + virCheckFlags(0, NULL); + + if (vshCommandOptStringQuiet(ctl, cmd, "codeset", &codeset_option) <= 0) + codeset_option = "linux"; + + if (STREQ(codeset_option, "rfb")) + codeset_option = "qnum"; + + codeset = virKeycodeSetTypeFromString(codeset_option); + + if (codeset < 0) + return NULL; + + switch ((virKeycodeSet) codeset) { + case VIR_KEYCODE_SET_LINUX: + names = virKeyNameTable_linux; + len = virKeyNameTable_linux_len; + break; + case VIR_KEYCODE_SET_OSX: + names = virKeyNameTable_osx; + len = virKeyNameTable_osx_len; + break; + case VIR_KEYCODE_SET_WIN32: + names = virKeyNameTable_win32; + len = virKeyNameTable_win32_len; + break; + case VIR_KEYCODE_SET_XT: + case VIR_KEYCODE_SET_ATSET1: + case VIR_KEYCODE_SET_ATSET2: + case VIR_KEYCODE_SET_ATSET3: + case VIR_KEYCODE_SET_XT_KBD: + case VIR_KEYCODE_SET_USB: + case VIR_KEYCODE_SET_QNUM: + case VIR_KEYCODE_SET_LAST: + break; + } + + if (!names) + return NULL; + + tmp = g_new0(char *, len + 1); + + for (i = 0; i < len; i++) { + if (!names[i]) + continue; + + tmp[j] = g_strdup(names[i]); + j++; + } + + tmp = g_renew(char *, tmp, j + 1); + + return g_steal_pointer(&tmp); +} diff --git a/tools/virsh-completer-domain.h b/tools/virsh-completer-domain.h index eb8d12455c..04a3705ff9 100644 --- a/tools/virsh-completer-domain.h +++ b/tools/virsh-completer-domain.h @@ -114,3 +114,7 @@ char ** virshDomainLifecycleActionCompleter(vshControl *ctl, char ** virshCodesetNameCompleter(vshControl *ctl, const vshCmd *cmd, unsigned int flags); + +char ** virshKeycodeNameCompleter(vshControl *ctl, + const vshCmd *cmd, + unsigned int flags); diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index d40995f44d..df33467646 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -8802,6 +8802,7 @@ static const vshCmdOptDef opts_send_key[] = { {.name = "keycode", .type = VSH_OT_ARGV, .flags = VSH_OFLAG_REQ, + .completer = virshKeycodeNameCompleter, .help = N_("the key code") }, {.name = NULL}