77 lines
2.4 KiB
C++
77 lines
2.4 KiB
C++
#include "tty_functions.h"
|
|
|
|
/* Reset terminal mode on program exit */
|
|
void ttyResetExit( struct termios * ttyOrig )
|
|
{
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, ttyOrig) == -1)
|
|
perror("tcsetattr");
|
|
}
|
|
|
|
/* Place terminal referred to by 'fd' in cbreak mode (noncanonical mode
|
|
with echoing turned off). This function assumes that the terminal is
|
|
currently in cooked mode (i.e., we shouldn't call it if the terminal
|
|
is currently in raw mode, since it does not undo all of the changes
|
|
made by the ttySetRaw() function below). Return 0 on success, or -1
|
|
on error. If 'prevTermios' is non-NULL, then use the buffer to which
|
|
it points to return the previous terminal settings. */
|
|
int ttySetCbreak(int fd, struct termios *prevTermios)
|
|
{
|
|
struct termios t;
|
|
|
|
if (tcgetattr(fd, &t) == -1)
|
|
return -1;
|
|
|
|
if (prevTermios != nullptr)
|
|
*prevTermios = t;
|
|
|
|
t.c_lflag &= ~(ICANON | ECHO);
|
|
t.c_lflag |= ISIG;
|
|
|
|
t.c_iflag &= ~ICRNL;
|
|
|
|
t.c_cc[VMIN] = 1; /* Character-at-a-time input */
|
|
t.c_cc[VTIME] = 0; /* with blocking */
|
|
|
|
if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* Place terminal referred to by 'fd' in raw mode (noncanonical mode
|
|
with all input and output processing disabled). Return 0 on success,
|
|
or -1 on error. If 'prevTermios' is non-NULL, then use the buffer to
|
|
which it points to return the previous terminal settings. */
|
|
|
|
int ttySetRaw(int fd, struct termios *prevTermios)
|
|
{
|
|
struct termios t;
|
|
|
|
if (tcgetattr(fd, &t) == -1)
|
|
return -1;
|
|
|
|
if (prevTermios != nullptr)
|
|
*prevTermios = t;
|
|
|
|
t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
|
|
/* Noncanonical mode, disable signals, extended
|
|
input processing, and echoing */
|
|
|
|
t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR |
|
|
INPCK | ISTRIP | IXON | PARMRK);
|
|
/* Disable special handling of CR, NL, and BREAK.
|
|
No 8th-bit stripping or parity error handling.
|
|
Disable START/STOP output flow control. */
|
|
|
|
// this breaks cursor position change tracking in parent processes
|
|
//t.c_oflag &= ~OPOST; /* Disable all output processing */
|
|
|
|
t.c_cc[VMIN] = 1; /* Character-at-a-time input */
|
|
t.c_cc[VTIME] = 0; /* with blocking */
|
|
|
|
if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
|
|
return -1;
|
|
|
|
return 0;
|
|
} |