From 0a9a9ed57e41a6242a8840adebec2cabd8d2c5ae Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sun, 15 May 2022 16:05:11 +1000 Subject: [PATCH] [client] config: enhance input:escapeKey to accept a KEY_* string value This makes it possible to define the escape key by name rather then just it's integer code, while still allowing fallback to using an integer value for codes that may not be defined. Example: `input:escapeKey=KEY_F1` An invalid string value will also print a list of all valid string values. --- client/src/config.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/client/src/config.c b/client/src/config.c index 296a2504..004bd2fa 100644 --- a/client/src/config.c +++ b/client/src/config.c @@ -42,6 +42,8 @@ static char * optPosToString (struct Option * opt); static bool optSizeParse (struct Option * opt, const char * str); static StringList optSizeValues (struct Option * opt); static char * optSizeToString (struct Option * opt); +static bool optScancodeParse (struct Option * opt, const char * str); +static StringList optScancodeValues (struct Option * opt); static bool optScancodeValidate(struct Option * opt, const char ** error); static char * optScancodeToString(struct Option * opt); static bool optRotateValidate (struct Option * opt, const char ** error); @@ -310,10 +312,12 @@ static struct Option options[] = { .module = "input", .name = "escapeKey", - .description = "Specify the escape key, see for valid values", + .description = "Specify the escape/menu key to use (use \"help\" to see valid values)", .shortopt = 'm', .type = OPTION_TYPE_INT, .value.x_int = KEY_SCROLLLOCK, + .parser = optScancodeParse, + .getValues = optScancodeValues, .validator = optScancodeValidate, .toString = optScancodeToString, }, @@ -862,6 +866,20 @@ static char * optSizeToString(struct Option * opt) return str; } +static StringList optScancodeValues(struct Option * opt) +{ + StringList sl = stringlist_new(false); + if (!sl) + return NULL; + + // this typecast is safe as the stringlist doesn't own the values + for(unsigned int i = 0; i < KEY_MAX; ++i) + if (linux_to_str[i]) + stringlist_push(sl, (char *)linux_to_str[i]); + + return sl; +} + static bool optScancodeValidate(struct Option * opt, const char ** error) { if (opt->value.x_int >= 0 && opt->value.x_int < KEY_MAX) @@ -871,6 +889,29 @@ static bool optScancodeValidate(struct Option * opt, const char ** error) return false; } +static bool optScancodeParse(struct Option * opt, const char * str) +{ + for(unsigned int i = 0; i < KEY_MAX; ++i) + { + if (!linux_to_str[i]) + continue; + + if (strcasecmp(linux_to_str[i], str) == 0) + { + opt->value.x_int = i; + return true; + } + } + + // fallback for pre-keycode name support + char * end; + opt->value.x_int = strtol(str, &end, 10); + if (*end) + return false; + + return true; +} + static char * optScancodeToString(struct Option * opt) { char * str;