From 96dc8c602c4ad6e4700cab35b92397240a167a9a Mon Sep 17 00:00:00 2001 From: Quantum Date: Sat, 30 Jan 2021 04:52:52 -0500 Subject: [PATCH] [client] keybind: display help overlay while ScrollLock is held This overlay will show the list of keybindings. --- client/include/app.h | 4 ++++ client/src/app.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/client/include/app.h b/client/include/app.h index 79e8eae1..93bd879c 100644 --- a/client/include/app.h +++ b/client/include/app.h @@ -110,5 +110,9 @@ void app_releaseKeybind(KeybindHandle * handle); */ void app_releaseAllKeybinds(void); +/** + * Changes whether the help message is displayed or not. + */ +void app_showHelp(bool show); #endif diff --git a/client/src/app.c b/client/src/app.c index 327713a2..263b00db 100644 --- a/client/src/app.c +++ b/client/src/app.c @@ -189,6 +189,7 @@ void app_handleKeyPress(int sc) { g_state.escapeActive = true; g_state.escapeAction = -1; + app_showHelp(true); return; } @@ -240,7 +241,10 @@ void app_handleKeyRelease(int sc) } if (sc == g_params.escapeKey) + { g_state.escapeActive = false; + app_showHelp(false); + } } if (!core_inputEnabled()) @@ -496,3 +500,55 @@ void app_releaseAllKeybinds(void) g_state.bindings[i] = NULL; } } + +static char * build_help_str() +{ + size_t size = 50; + size_t offset = 0; + char * buffer = malloc(size); + + if (!buffer) + return NULL; + + const char * escapeName = xfree86_to_display[g_params.escapeKey]; + + offset += snprintf(buffer, size, "%s %-10s Toggle capture mode\n", escapeName, ""); + if (offset >= size) + { + DEBUG_ERROR("Help string somehow overflowed. This should be impossible."); + return NULL; + } + + for (int i = 0; i < KEY_MAX; ++i) + { + if (g_state.keyDescription[i]) + { + const char * keyName = xfree86_to_display[i]; + const char * desc = g_state.keyDescription[i]; + int needed = snprintf(buffer + offset, size - offset, "%s+%-10s %s\n", escapeName, keyName, desc); + if (offset + needed < size) + offset += needed; + else + { + size = size * 2 + needed; + void * new = realloc(buffer, size); + if (!new) { + free(buffer); + DEBUG_ERROR("Out of memory when constructing help text"); + return NULL; + } + buffer = new; + offset += snprintf(buffer + offset, size - offset, "%s+%-10s %s\n", escapeName, keyName, desc); + } + } + } + + return buffer; +} + +void app_showHelp(bool show) +{ + char * help = show ? build_help_str() : NULL; + g_state.lgr->on_help(g_state.lgrData, help); + free(help); +}