From 11c252ad5ec2031ad6eb9db0e4baba5e3aeb746a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 8 Aug 2020 18:46:27 +1000 Subject: [PATCH] when server removes a hotkey, restore to user's hotkey (if set) instead of leaving it empty --- src/Input.c | 51 ++++++++++++++++++++++++++++++++------------------ src/Input.h | 2 ++ src/Protocol.c | 1 + 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/Input.c b/src/Input.c index 4c50db579..9536f48d3 100644 --- a/src/Input.c +++ b/src/Input.c @@ -466,37 +466,52 @@ int Hotkeys_FindPartial(int key) { return -1; } -/* Initialises and loads hotkeys from options. */ -static void Hotkeys_Init(void) { - static const String prefix = String_FromConst("hotkey-"); +static const String prefix = String_FromConst("hotkey-"); +static void Hotkeys_FromLine(String* key, String* value) { String strKey, strMods, strMore, strText; - String entry, key, value; - int i; - int trigger; cc_uint8 modifiers; cc_bool more; + /* Format is: key&modifiers = more-input&text */ + key->length -= prefix.length; key->buffer += prefix.length; + + if (!String_UNSAFE_Separate(key, '&', &strKey, &strMods)) return; + if (!String_UNSAFE_Separate(value, '&', &strMore, &strText)) return; + + trigger = Utils_ParseEnum(&strKey, KEY_NONE, Input_Names, INPUT_COUNT); + if (trigger == KEY_NONE) return; + if (!Convert_ParseUInt8(&strMods, &modifiers)) return; + if (!Convert_ParseBool(&strMore, &more)) return; + + Hotkeys_Add(trigger, modifiers, &strText, more); +} + +/* Initialises and loads hotkeys from options. */ +static void Hotkeys_Init(void) { + String entry, key, value; + int i; + for (i = 0; i < Options.count; i++) { entry = StringsBuffer_UNSAFE_Get(&Options, i); String_UNSAFE_Separate(&entry, '=', &key, &value); if (!String_CaselessStarts(&key, &prefix)) continue; - /* Format is: key&modifiers = more-input&text */ - key.length -= prefix.length; key.buffer += prefix.length; - - if (!String_UNSAFE_Separate(&key, '&', &strKey, &strMods)) continue; - if (!String_UNSAFE_Separate(&value, '&', &strMore, &strText)) continue; - - trigger = Utils_ParseEnum(&strKey, KEY_NONE, Input_Names, INPUT_COUNT); - if (trigger == KEY_NONE) continue; - if (!Convert_ParseUInt8(&strMods, &modifiers)) continue; - if (!Convert_ParseBool(&strMore, &more)) continue; - - Hotkeys_Add(trigger, modifiers, &strText, more); + Hotkeys_FromLine(&key, &value); } } +void Hotkeys_AddDefault(int trigger, cc_uint8 modifiers) { + String key, value; char keyBuffer[STRING_SIZE]; + String_InitArray(key, keyBuffer); + + String_Format2(&key, "hotkey-%c&%b", Input_Names[trigger], &modifiers); + key.buffer[key.length] = '\0'; /* TODO: Avoid this null terminator */ + + Options_UNSAFE_Get(key.buffer, &value); + Hotkeys_FromLine(&key, &value); +} + void Hotkeys_UserRemovedHotkey(int trigger, cc_uint8 modifiers) { String key; char keyBuffer[STRING_SIZE]; String_InitArray(key, keyBuffer); diff --git a/src/Input.h b/src/Input.h index 285858ba9..55e8cfe07 100644 --- a/src/Input.h +++ b/src/Input.h @@ -147,6 +147,8 @@ cc_bool Hotkeys_Remove(int trigger, cc_uint8 modifiers); /* Returns the first hotkey which is bound to the given key and has its modifiers pressed. */ /* NOTE: The hotkeys list is sorted, so hotkeys with most modifiers are checked first. */ int Hotkeys_FindPartial(int key); + +void Hotkeys_AddDefault(int trigger, cc_uint8 modifiers); /* Called when user has removed a hotkey. (removes it from options) */ void Hotkeys_UserRemovedHotkey(int trigger, cc_uint8 modifiers); /* Called when user has added a hotkey. (Adds it to options) */ diff --git a/src/Protocol.c b/src/Protocol.c index 6b852a436..b39f9638b 100644 --- a/src/Protocol.c +++ b/src/Protocol.c @@ -1001,6 +1001,7 @@ static void CPE_SetTextHotkey(cc_uint8* data) { if (!action.length) { Hotkeys_Remove(key, keyMods); + Hotkeys_AddDefault(key, keyMods); } else if (action.buffer[action.length - 1] == '\n') { action.length--; Hotkeys_Add(key, keyMods, &action, false);