diff --git a/include/platform/getch.h b/include/platform/getch.h index 4603264..15c66fe 100644 --- a/include/platform/getch.h +++ b/include/platform/getch.h @@ -4,8 +4,10 @@ #ifdef _WIN32 #include #define getch _getch +inline int getchInit(void) { return 0; } #else int getch(void); +int getchInit(void); #endif /* _WIN32 */ #endif /* __GETCH_H__ */ diff --git a/src/input.c b/src/input.c index f467079..e3f6d92 100644 --- a/src/input.c +++ b/src/input.c @@ -7,6 +7,7 @@ ThreadR input(void *vargp) { + getchInit(); int *out = ((InputArgs *)vargp)->out; bool *alive = ((InputArgs *)vargp)->alive; diff --git a/src/platform/getch.c b/src/platform/getch.c index 9828ccd..954a204 100644 --- a/src/platform/getch.c +++ b/src/platform/getch.c @@ -2,11 +2,14 @@ #include #include #include +#include + +static struct termios DefaultState = {0}; int getch(void) { char buf = 0; - struct termios old = { 0 }; + struct termios old = {0}; fflush(stdout); if (tcgetattr(0, &old) < 0) perror("tcsetattr()"); old.c_lflag &= ~ICANON; // local modes = Non Canonical mode @@ -21,4 +24,15 @@ int getch(void) return (int)buf; } +static void resetTerminalState(void) +{ + tcsetattr(0, TCSANOW, &DefaultState); +} + +int getchInit(void) +{ + tcgetattr(0, &DefaultState); + return atexit(resetTerminalState); +} + #endif /* !_WIN32 */