From b795b67334fbcc4e12670e7fa5ce2236491bb8ec Mon Sep 17 00:00:00 2001 From: Kaian Date: Sat, 20 Feb 2016 19:28:42 +0100 Subject: [PATCH] keyhandlers: return KEY_HANDLED/KEY_NOT_HANDLED --- src/curses/ui_call_flow.c | 6 +++--- src/curses/ui_call_list.c | 11 +++++------ src/curses/ui_call_raw.c | 2 +- src/curses/ui_column_select.c | 16 ++++++++++------ src/curses/ui_filter.c | 11 +++++++---- src/curses/ui_manager.c | 16 +++++++--------- src/curses/ui_manager.h | 4 ---- src/curses/ui_msg_diff.c | 2 +- src/curses/ui_panel.c | 2 +- src/curses/ui_panel.h | 7 +++++++ src/curses/ui_save.c | 5 +++-- src/curses/ui_settings.c | 10 +++++----- src/curses/ui_stats.c | 3 ++- 13 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/curses/ui_call_flow.c b/src/curses/ui_call_flow.c index c568c7a..a66da67 100644 --- a/src/curses/ui_call_flow.c +++ b/src/curses/ui_call_flow.c @@ -937,7 +937,7 @@ call_flow_handle_key(ui_t *ui, int key) // Sanity check, this should not happen if (!info) - return -1; + return KEY_NOT_HANDLED; getmaxyx(info->flow_win, height, width); @@ -1074,7 +1074,7 @@ call_flow_handle_key(ui_t *ui, int key) } // Return if this panel has handled or not the key - return (action == ERR) ? key : 0; + return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; } int @@ -1084,7 +1084,7 @@ call_flow_help(ui_t *ui) int height, width; // Create a new panel and show centered - height = 27; + height = 28; width = 65; help_win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2); diff --git a/src/curses/ui_call_list.c b/src/curses/ui_call_list.c index 1fd8a3e..84527f2 100644 --- a/src/curses/ui_call_list.c +++ b/src/curses/ui_call_list.c @@ -585,13 +585,12 @@ call_list_handle_key(ui_t *ui, int key) // Handle quit from this screen unless requested if (setting_enabled(SETTING_EXITPROMPT)) { if (dialog_confirm("Confirm exit", "Are you sure you want to quit?", "Yes,No") == 0) { - return KEY_ESC; - } else { - return 0; + ui_destroy(ui); } } else { - return KEY_ESC; + ui_destroy(ui); } + return KEY_HANDLED; break; default: // Parse next action @@ -620,7 +619,7 @@ call_list_handle_key(ui_t *ui, int key) // Return if this panel has handled or not the key - return (action == ERR) ? key : 0; + return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; } int @@ -706,7 +705,7 @@ call_list_handle_form_key(ui_t *ui, int key) filter_set(FILTER_CALL_LIST, strlen(dfilter) ? dfilter : NULL); // Return if this panel has handled or not the key - return (action == ERR) ? key : 0; + return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; } int diff --git a/src/curses/ui_call_raw.c b/src/curses/ui_call_raw.c index 082213c..3294303 100644 --- a/src/curses/ui_call_raw.c +++ b/src/curses/ui_call_raw.c @@ -269,7 +269,7 @@ call_raw_handle_key(ui_t *ui, int key) } // Return if this panel has handled or not the key - return (action == ERR) ? key : 0; + return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; } int diff --git a/src/curses/ui_column_select.c b/src/curses/ui_column_select.c index e13deb0..338ba54 100644 --- a/src/curses/ui_column_select.c +++ b/src/curses/ui_column_select.c @@ -235,7 +235,8 @@ column_select_handle_key_menu(ui_t *ui, int key) break; case ACTION_CONFIRM: column_select_update_columns(ui); - return 27; + ui_destroy(ui); + return KEY_HANDLED; default: // Parse next action continue; @@ -249,7 +250,7 @@ column_select_handle_key_menu(ui_t *ui, int key) draw_vscrollbar(info->menu_win, top_row(menu), item_count(menu) - 1, 0); wnoutrefresh(info->menu_win); // Return if this panel has handled or not the key - return (action == ERR) ? key : 0; + return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; } int @@ -287,13 +288,16 @@ column_select_handle_key_form(ui_t *ui, int key) switch(field_idx) { case FLD_COLUMNS_ACCEPT: column_select_update_columns(ui); - return 27; + ui_destroy(ui); + return KEY_HANDLED; case FLD_COLUMNS_CANCEL: - return 27; + ui_destroy(ui); + return KEY_HANDLED; case FLD_COLUMNS_SAVE: column_select_update_columns(ui); column_select_save_columns(ui); - return 27; + ui_destroy(ui); + return KEY_HANDLED; } break; default: @@ -326,7 +330,7 @@ column_select_handle_key_form(ui_t *ui, int key) } // Return if this panel has handled or not the key - return (action == ERR) ? key : 0; + return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; } void diff --git a/src/curses/ui_filter.c b/src/curses/ui_filter.c index 188b0c6..7d6b552 100644 --- a/src/curses/ui_filter.c +++ b/src/curses/ui_filter.c @@ -260,16 +260,19 @@ filter_handle_key(ui_t *ui, int key) } break; case FLD_FILTER_CANCEL: - return KEY_ESC; + ui_destroy(ui); + return KEY_HANDLED; case FLD_FILTER_FILTER: filter_save_options(ui); - return KEY_ESC; + ui_destroy(ui); + return KEY_HANDLED; } break; case ACTION_CONFIRM: if (field_idx != FLD_FILTER_CANCEL) filter_save_options(ui); - return KEY_ESC; + ui_destroy(ui); + return KEY_HANDLED; default: // Parse next action continue; @@ -295,7 +298,7 @@ filter_handle_key(ui_t *ui, int key) } // Return if this panel has handled or not the key - return (action == ERR) ? key : 0; + return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; } void diff --git a/src/curses/ui_manager.c b/src/curses/ui_manager.c index c39b406..6d564f2 100644 --- a/src/curses/ui_manager.c +++ b/src/curses/ui_manager.c @@ -229,30 +229,28 @@ wait_for_input() capture_lock(); // Handle received key - int key = c; - while (c != KEY_HANDLED) { + int hld = KEY_NOT_HANDLED; + while (hld != KEY_HANDLED) { // Check if current panel has custom bindings for that key - c = ui_handle_key(ui, c); + hld = ui_handle_key(ui, c); - if (c == KEY_HANDLED) { + if (hld == KEY_HANDLED) { // Panel handled this key continue; - } else if (c == KEY_PROPAGATED) { - // restore the key value - c = key; + } else if (hld == KEY_PROPAGATED) { // Destroy current panel ui_destroy(ui); // Try to handle this key with the previus panel ui = ui_find_by_panel(panel_below(NULL)); } else { // Key not handled by UI nor propagated. Use default handler - c = default_handle_key(ui, c); + hld = default_handle_key(ui, c); } } capture_unlock(); } - return -1; + return 0; } int diff --git a/src/curses/ui_manager.h b/src/curses/ui_manager.h index e8dd8ff..09c5d6f 100644 --- a/src/curses/ui_manager.h +++ b/src/curses/ui_manager.h @@ -49,10 +49,6 @@ #define DIALOG_MAX_WIDTH 100 #define DIALOG_MIN_WIDTH 40 -//! Possible key handler results -#define KEY_HANDLED 0 -#define KEY_PROPAGATED -1 - /** * Define existing panels */ diff --git a/src/curses/ui_msg_diff.c b/src/curses/ui_msg_diff.c index e0daee4..b358c42 100644 --- a/src/curses/ui_msg_diff.c +++ b/src/curses/ui_msg_diff.c @@ -225,7 +225,7 @@ msg_diff_draw_message(WINDOW *win, sip_msg_t *msg, char *highlight) int msg_diff_handle_key(ui_t *ui, int key) { - return key; + return KEY_NOT_HANDLED; } int diff --git a/src/curses/ui_panel.c b/src/curses/ui_panel.c index 18f7dde..adc12a3 100644 --- a/src/curses/ui_panel.c +++ b/src/curses/ui_panel.c @@ -124,7 +124,7 @@ ui_handle_key(ui_t *ui, int key) return ui->handle_key(ui, key); } - return 0; + return KEY_NOT_HANDLED; } diff --git a/src/curses/ui_panel.h b/src/curses/ui_panel.h index ba5b92a..0c774b7 100644 --- a/src/curses/ui_panel.h +++ b/src/curses/ui_panel.h @@ -40,6 +40,13 @@ #include #include +//! Possible key handler results +enum key_handler_ret { + KEY_HANDLED = 0, + KEY_NOT_HANDLED = -1, + KEY_PROPAGATED = -2 +}; + /** * @brief Enum for available panel types * diff --git a/src/curses/ui_save.c b/src/curses/ui_save.c index 0850109..487aa29 100644 --- a/src/curses/ui_save.c +++ b/src/curses/ui_save.c @@ -336,7 +336,8 @@ save_handle_key(ui_t *ui, int key) if (field_idx != FLD_SAVE_CANCEL) { return save_to_file(ui); } - return KEY_ESC; + ui_destroy(ui); + return KEY_HANDLED; default: // Parse next action continue; @@ -362,7 +363,7 @@ save_handle_key(ui_t *ui, int key) } // Return if this panel has handled or not the key - return (action == ERR) ? key : 0; + return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; } void diff --git a/src/curses/ui_settings.c b/src/curses/ui_settings.c index 8ec4d2a..2cae96f 100644 --- a/src/curses/ui_settings.c +++ b/src/curses/ui_settings.c @@ -355,7 +355,8 @@ settings_handle_key(ui_t *ui, int key) break; case ACTION_CONFIRM: ui_settings_update_settings(ui); - return KEY_ESC; + ui_destroy(ui); + return KEY_HANDLED; default: // Parse next action continue; @@ -383,12 +384,11 @@ settings_handle_key(ui_t *ui, int key) break; case ACTION_SELECT: case ACTION_CONFIRM: - if (field_idx == BTN_SETTINGS_CANCEL) - return KEY_ESC; if (field_idx == BTN_SETTINGS_SAVE) ui_settings_save(ui); ui_settings_update_settings(ui); - return KEY_ESC; + ui_destroy(ui); + return KEY_HANDLED; default: continue; } @@ -410,7 +410,7 @@ settings_handle_key(ui_t *ui, int key) } // Return if this panel has handled or not the key - return (action == ERR) ? key : 0; + return (action == ERR) ? KEY_NOT_HANDLED : KEY_HANDLED; } settings_entry_t * diff --git a/src/curses/ui_stats.c b/src/curses/ui_stats.c index 272ac3f..83a61ca 100644 --- a/src/curses/ui_stats.c +++ b/src/curses/ui_stats.c @@ -197,5 +197,6 @@ stats_create(ui_t *ui) int stats_handle_key(ui_t *ui, int key) { - return KEY_ESC; + ui_destroy(ui); + return KEY_HANDLED; }