From b38668a2ef5d93013c77104a2aeac403ca173803 Mon Sep 17 00:00:00 2001 From: Steve Penrod Date: Wed, 16 Jan 2019 23:49:15 -0800 Subject: [PATCH] Fix Ctrl+C handling in CLI Ctrl+C was not being properly handled by the CLI, resulting in an unclean shutdown and other unexpected consequences -- e.g. a "./start-mycroft.sh debug" would kill the background processes on exit. The KeyboardException was never being triggered. Now the SIGINT is being captured and Ctrl+C behaves (as intended) just like pressing Ctrl+X. --- mycroft/client/text/text_client.py | 53 +++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/mycroft/client/text/text_client.py b/mycroft/client/text/text_client.py index a46a701f26..07fecfa146 100644 --- a/mycroft/client/text/text_client.py +++ b/mycroft/client/text/text_client.py @@ -14,6 +14,7 @@ # import sys import io +import signal from math import ceil from .gui_server import start_qml_gui @@ -100,11 +101,30 @@ CLR_LOG_CMDMESSAGE = 0 CLR_METER_CUR = 0 CLR_METER = 0 +# Allow Ctrl+C catching... +ctrl_c_was_pressed = False + + +def ctrl_c_handler(signum, frame): + global ctrl_c_was_pressed + ctrl_c_was_pressed = True + + +def ctrl_c_pressed(): + global ctrl_c_was_pressed + if ctrl_c_was_pressed: + ctrl_c_was_pressed = False + return True + else: + return False + + +signal.signal(signal.SIGINT, ctrl_c_handler) + ############################################################################## # Helper functions - def clamp(n, smallest, largest): """ Force n to be between smallest and largest, inclusive """ return max(smallest, min(n, largest)) @@ -1151,25 +1171,22 @@ def gui_main(stdscr): try: while True: set_screen_dirty() + c = 0 + code = 0 try: - # Don't block, this allows us to refresh the screen while - # waiting on initial messagebus connection, etc - scr.timeout(1) - c = scr.get_wch() # unicode char or int for special keys - if c == -1: - continue - except KeyboardInterrupt: - # User hit Ctrl+C to quit - if find_str: - # End the find session - find_str = None - rebuild_filtered_log() - continue # Consumed the Ctrl+C, get next character + if ctrl_c_pressed(): + # User hit Ctrl+C. treat same as Ctrl+X + c = 24 else: - c = 24 # treat as Ctrl+X (Exit) + # Don't block, this allows us to refresh the screen while + # waiting on initial messagebus connection, etc + scr.timeout(1) + c = scr.get_wch() # unicode char or int for special keys + if c == -1: + continue except curses.error: - # This happens in odd cases, such as when you Ctrl+Z suspend + # This happens in odd cases, such as when you Ctrl+Z # the CLI and then resume. Curses fails on get_wch(). continue @@ -1319,7 +1336,11 @@ def gui_main(stdscr): # End the find session find_str = None rebuild_filtered_log() + elif line.startswith(":"): + # cancel command mode + line = "" else: + # exit CLI break elif code > 31 and isinstance(c, str): # Accept typed character in the utterance