From d212e8f1122c6ae16b90ad7751237fd167dc6b51 Mon Sep 17 00:00:00 2001 From: Brad Grantham Date: Sun, 20 Nov 2016 12:27:58 -0800 Subject: [PATCH] Allow running without TTY (e.g. in Instruments) Continue running if TTY cannot be opened --- keyboard.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/keyboard.cpp b/keyboard.cpp index 0fb2dab..249a232 100644 --- a/keyboard.cpp +++ b/keyboard.cpp @@ -111,21 +111,26 @@ unsigned char get_any_key_down_and_clear_strobe() return 0x00; } +bool started = false; + void start_keyboard() { // Set raw mode on stdin. if (ttyraw(0) < 0) { - fprintf(stderr,"Can't go to raw mode.\n"); - exit(1); + fprintf(stderr,"Can't go to raw mode. Oh, well!\n"); + // exit(1); + return; } + started = true; } void stop_keyboard() { - if (ttyreset(0) < 0) { + if (started && (ttyreset(0) < 0)) { fprintf(stderr, "Cannot reset terminal!\n"); exit(-1); } + started = false; } bool peek_key(char *k) @@ -145,6 +150,9 @@ void poll_keyboard() int i; char c; + if(!started) + return; + i = read(0, &c, 1); if (i == -1) { if (errno == EAGAIN) {