csnake/src/input.c

36 lines
1.0 KiB
C
Raw Normal View History

2023-10-29 21:31:33 +03:00
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
2023-10-29 21:31:33 +03:00
#include <unistd.h>
#include <termios.h>
char getch(void)
{
char buf = 0;
struct termios old = { 0 };
fflush(stdout);
if (tcgetattr(0, &old) < 0) perror("tcsetattr()");
old.c_lflag &= ~ICANON; // local modes = Non Canonical mode
old.c_lflag &= ~ECHO; // local modes = Disable echo.
old.c_cc[VMIN] = 1; // control chars (MIN value) = 1
old.c_cc[VTIME] = 0; // control chars (TIME value) = 0 (No time)
if (tcsetattr(0, TCSANOW, &old) < 0) perror("tcsetattr ICANON");
if (read(0, &buf, 1) < 0) perror("read()");
old.c_lflag |= ICANON; // local modes = Canonical mode
old.c_lflag |= ECHO; // local modes = Enable echo.
if (tcsetattr(0, TCSADRAIN, &old) < 0) perror ("tcsetattr ~ICANON");
return buf;
}
2023-10-30 02:51:46 +03:00
int input(void *vargp)
2023-10-29 21:31:33 +03:00
{
2023-10-29 22:35:37 +03:00
char *out = ((InputArgs *)vargp)->out;
bool *alive = ((InputArgs *)vargp)->alive;
2023-10-29 21:31:33 +03:00
while (*alive)
{
*out = getch();
}
2023-10-30 02:51:46 +03:00
return 0;
2023-10-29 21:31:33 +03:00
}