From 22d3cdeb43e36b36419668950443122eb1736726 Mon Sep 17 00:00:00 2001 From: Nakidai Perumenei Date: Sun, 17 Dec 2023 00:41:42 +0300 Subject: [PATCH] Repair cursor on exit for posix --- include/platform/getch.h | 2 ++ src/input.c | 1 + src/platform/getch.c | 16 +++++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) 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 */