51 lines
1.6 KiB
C
51 lines
1.6 KiB
C
#include "termio.h"
|
|
|
|
/* c_iflag bit masks */
|
|
#define ifl_RAW (~(IMAXBEL))
|
|
/* NOTE: the line below won't work, bad bitwise ops */
|
|
#define ifl_NOPARITY ((IGNPAR) & ~(PARMRK | INPCK | ISTRIP))
|
|
|
|
/* c_cflag bit masks */
|
|
#define cfl_NOPARITY (~(PARENB))
|
|
|
|
/* c_lflag bit masks */
|
|
#define lfl_RAW (~(ISIG | ICANON))
|
|
#define lfl_ECHO ((ECHO | ECHOE | ECHOK | ECHONL | ECHO))
|
|
// & ~(ECHOCTL | ECHOPRT | ECHOKE))
|
|
|
|
/*
|
|
* NOTE: This function is taken from the GNU C Library (glibc),
|
|
* NOTE: specifically `glibc/termios/cfmakeraw.c`.
|
|
*/
|
|
int termmode_raw(struct ct_term *restrict const term) {
|
|
term->termios.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
|
|
term->termios.c_oflag &= ~OPOST;
|
|
term->termios.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
|
|
term->termios.c_cflag &= ~(CSIZE|PARENB);
|
|
term->termios.c_cflag |= CS8;
|
|
term->termios.c_cc[VMIN] = 1; /* read returns when one char is available. */
|
|
term->termios.c_cc[VTIME] = 0;
|
|
return ct_applyterm(term);
|
|
}
|
|
|
|
int termmode_canon(struct ct_term *restrict const term) {
|
|
/* TODO: inverse of termmode_raw */
|
|
term->termios.c_lflag &= ~lfl_RAW & lfl_ECHO;
|
|
return ct_applyterm(term);
|
|
}
|
|
|
|
/* Set ncurses terminal mode (buffering, character processing,
|
|
* Key->SIG handling, and other termios(3) functionality).
|
|
*/
|
|
static inline int termmode(struct ct_term *restrict const term,
|
|
const enum crs_termmode mode) {
|
|
switch (mode) {
|
|
case TMODE_RAW:
|
|
return termmode_raw(term);
|
|
case TMODE_CANON:
|
|
return termmode_canon(term); /* ITS A CANON EVENT OHMAHGOH */
|
|
default:
|
|
/* defaulting is not possible. */
|
|
return 1;
|
|
}
|
|
}
|