Repair cursor on exit for posix

fix-mingw
Nakidai Perumenei 2023-12-17 00:41:42 +03:00
parent cf71b4d57d
commit 22d3cdeb43
3 changed files with 18 additions and 1 deletions

View File

@ -4,8 +4,10 @@
#ifdef _WIN32 #ifdef _WIN32
#include <conio.h> #include <conio.h>
#define getch _getch #define getch _getch
inline int getchInit(void) { return 0; }
#else #else
int getch(void); int getch(void);
int getchInit(void);
#endif /* _WIN32 */ #endif /* _WIN32 */
#endif /* __GETCH_H__ */ #endif /* __GETCH_H__ */

View File

@ -7,6 +7,7 @@
ThreadR input(void *vargp) ThreadR input(void *vargp)
{ {
getchInit();
int *out = ((InputArgs *)vargp)->out; int *out = ((InputArgs *)vargp)->out;
bool *alive = ((InputArgs *)vargp)->alive; bool *alive = ((InputArgs *)vargp)->alive;

View File

@ -2,11 +2,14 @@
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
static struct termios DefaultState = {0};
int getch(void) int getch(void)
{ {
char buf = 0; char buf = 0;
struct termios old = { 0 }; struct termios old = {0};
fflush(stdout); fflush(stdout);
if (tcgetattr(0, &old) < 0) perror("tcsetattr()"); if (tcgetattr(0, &old) < 0) perror("tcsetattr()");
old.c_lflag &= ~ICANON; // local modes = Non Canonical mode old.c_lflag &= ~ICANON; // local modes = Non Canonical mode
@ -21,4 +24,15 @@ int getch(void)
return (int)buf; return (int)buf;
} }
static void resetTerminalState(void)
{
tcsetattr(0, TCSANOW, &DefaultState);
}
int getchInit(void)
{
tcgetattr(0, &DefaultState);
return atexit(resetTerminalState);
}
#endif /* !_WIN32 */ #endif /* !_WIN32 */