#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <term.h>
#include <unistd.h>
#include "terminal.h"
static int ft_putchar(int c)
{
return (write(STDERR_FILENO, &c, 1));
}
void terminal_send(char *cmd)
{
tputs(tgetstr(cmd, NULL), 1, ft_putchar);
}
/*
** Modes changed:
** - ~ECHO: Disable the printing of keypresses into the terminal.
** - ~ICANON: Read input char-by-char instead of line-by-line.
** - VIM = 0: Read a minimum bytes to read to 0 bytes.
** - VTIME = 1: A maximum of 100ms per keypress;
*/
void terminal_configure(enum e_terminal_configure_action action)
{
static struct termios original;
struct termios new;
if (action == TERMINAL_CONFIGURE_SETUP)
{
tgetent(NULL, getenv("TERM"));
terminal_send(TERMINAL_ENABLE_CURSOR_MODE);
terminal_send(TERMINAL_CURSOR_INVISIBLE);
tcgetattr(STDIN_FILENO, &original);
new = original;
new.c_lflag &= ~(ECHO | ICANON);
new.c_cc[VMIN] = 0;
new.c_cc[VTIME] = 1;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &new);
}
else
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
terminal_send(TERMINAL_DISABLE_CURSOR_MODE);
terminal_send(TERMINAL_CURSOR_VISIBLE);
}
}