From ace786476f942cb29134b04d0821f2cd8b79f2c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Mill=C3=A1n?= Date: Fri, 19 Feb 2016 18:36:20 +0100 Subject: [PATCH] Keystroke popagation: properly manage key handler return codes The following return codes can be get from key handlers: - KEY_HANDLED - KEY_PROPAGATED - key to be handled by default handler Previous implementation invalidated the third one --- src/curses/ui_call_flow.c | 2 +- src/curses/ui_call_raw.c | 2 +- src/curses/ui_manager.c | 19 ++++++++++--------- src/curses/ui_manager.h | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/curses/ui_call_flow.c b/src/curses/ui_call_flow.c index d3e81f3..a27c7d5 100644 --- a/src/curses/ui_call_flow.c +++ b/src/curses/ui_call_flow.c @@ -1282,7 +1282,7 @@ call_flow_handle_key(PANEL *panel, int key) break; case ACTION_CLEAR_CALLS: // Propagate the key to the previous panel - return -1; + return KEY_PROPAGATED; default: // Parse next action diff --git a/src/curses/ui_call_raw.c b/src/curses/ui_call_raw.c index f6b360f..4fe0c42 100644 --- a/src/curses/ui_call_raw.c +++ b/src/curses/ui_call_raw.c @@ -269,7 +269,7 @@ call_raw_handle_key(PANEL *panel, int key) break; case ACTION_CLEAR_CALLS: // Propagate the key to the previous panel - return -1; + return KEY_PROPAGATED; default: // Parse next action continue; diff --git a/src/curses/ui_manager.c b/src/curses/ui_manager.c index 473dbf6..0ec3eec 100644 --- a/src/curses/ui_manager.c +++ b/src/curses/ui_manager.c @@ -330,23 +330,24 @@ wait_for_input() continue; // Handle received key - int hdl = -1; - while (hdl != KEY_HANDLED) { + int key = c; + while (c != KEY_HANDLED) { // Check if current panel has custom bindings for that key - hdl = ui_handle_key(ui, c); + c = ui_handle_key(ui, c); - if (hdl == KEY_HANDLED) { + if (c == KEY_HANDLED) { // Panel handled this key continue; - } else if (hdl == KEY_PROPAGATED) { + } else if (c == KEY_PROPAGATED) { + // restore the key value + c = key; // 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 - default_handle_key(ui, c); - break; + c = default_handle_key(ui, c); } } } @@ -402,8 +403,8 @@ default_handle_key(ui_t *ui, int key) break; } - // Return this is a valid handled key - return (action == ERR) ? key : 0; + // Consider the key handled at this point + return KEY_HANDLED; } void diff --git a/src/curses/ui_manager.h b/src/curses/ui_manager.h index ebc82d5..6a582b1 100644 --- a/src/curses/ui_manager.h +++ b/src/curses/ui_manager.h @@ -304,7 +304,7 @@ wait_for_input(); /** * @brief Default handler for keys * - * If ui doesn't handle the given key (ui_handle_key returns -1) + * If ui doesn't handle the given key (ui_handle_key returns the key value) * then the default handler will be invoked * * @param ui Current displayed UI structure