From 32442b6286166c216151a008c06d02c850a96701 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 1 May 2016 14:57:46 -0400 Subject: [PATCH] Disallow ctrl+m keybinding and revert to default settings on invalid bindings Note: Ctrl+m uses the same control sequence as the enter key on many systems --- misc/toxic.conf.example | 1 + src/settings.c | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/misc/toxic.conf.example b/misc/toxic.conf.example index a6419d0..f6302b4 100644 --- a/misc/toxic.conf.example +++ b/misc/toxic.conf.example @@ -90,6 +90,7 @@ sounds = { // Currently supported: Ctrl modified keys, Tab, PAGEUP and PAGEDOWN (case insensitive) // Note: All printable keys register as input +// Note2: Ctrl+M does not work keys = { next_tab="Ctrl+P"; prev_tab="Ctrl+O"; diff --git a/src/settings.c b/src/settings.c index 1cbd6e2..75ecc80 100644 --- a/src/settings.c +++ b/src/settings.c @@ -230,11 +230,11 @@ static const struct sound_strings { }; #endif -static int key_parse(const char** bind){ +static int key_parse(const char **bind) { int len = strlen(*bind); if (len > 5) { - if(strncasecmp(*bind, "ctrl+", 5) == 0) + if(strncasecmp(*bind, "ctrl+", 5) == 0 && toupper(bind[0][5]) != 'M') /* ctrl+m cannot be used */ return toupper(bind[0][5]) - 'A' + 1; } @@ -247,6 +247,14 @@ static int key_parse(const char** bind){ return -1; } +static void set_key_binding(int *key, const char **bind) { + int code = key_parse(bind); + + if (code != -1) { + *key = code; + } +} + int settings_load(struct user_settings *s, const char *patharg) { config_t cfg[1]; @@ -383,27 +391,27 @@ int settings_load(struct user_settings *s, const char *patharg) if ((setting = config_lookup(cfg, key_strings.self)) != NULL) { const char* tmp = NULL; if (config_setting_lookup_string(setting, key_strings.next_tab, &tmp)) - s->key_next_tab = key_parse(&tmp); + set_key_binding(&s->key_next_tab, &tmp); if (config_setting_lookup_string(setting, key_strings.prev_tab, &tmp)) - s->key_prev_tab = key_parse(&tmp); + set_key_binding(&s->key_prev_tab, &tmp); if (config_setting_lookup_string(setting, key_strings.scroll_line_up, &tmp)) - s->key_scroll_line_up = key_parse(&tmp); + set_key_binding(&s->key_scroll_line_up, &tmp); if (config_setting_lookup_string(setting, key_strings.scroll_line_down, &tmp)) - s->key_scroll_line_down= key_parse(&tmp); + set_key_binding(&s->key_scroll_line_down, &tmp); if (config_setting_lookup_string(setting, key_strings.half_page_up, &tmp)) - s->key_half_page_up = key_parse(&tmp); + set_key_binding(&s->key_half_page_up, &tmp); if (config_setting_lookup_string(setting, key_strings.half_page_down, &tmp)) - s->key_half_page_down = key_parse(&tmp); + set_key_binding(&s->key_half_page_down, &tmp); if (config_setting_lookup_string(setting, key_strings.page_bottom, &tmp)) - s->key_page_bottom = key_parse(&tmp); + set_key_binding(&s->key_page_bottom, &tmp); if (config_setting_lookup_string(setting, key_strings.peer_list_up, &tmp)) - s->key_peer_list_up = key_parse(&tmp); + set_key_binding(&s->key_peer_list_up, &tmp); if (config_setting_lookup_string(setting, key_strings.peer_list_down, &tmp)) - s->key_peer_list_down = key_parse(&tmp); + set_key_binding(&s->key_peer_list_down, &tmp); if (config_setting_lookup_string(setting, key_strings.toggle_peerlist, &tmp)) - s->key_toggle_peerlist = key_parse(&tmp); + set_key_binding(&s->key_toggle_peerlist, &tmp); if (config_setting_lookup_string(setting, key_strings.toggle_pastemode, &tmp)) - s->key_toggle_pastemode = key_parse(&tmp); + set_key_binding(&s->key_toggle_pastemode, &tmp); } #ifdef AUDIO