Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/platform/console/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,24 @@ void console_init() {

#if USE_TERM_IO
struct winsize consoleSize;
struct termios original_termios;
struct termios raw_termios, original_termios;

/**
* @brief Handler for Ctrl+C
* @brief Handler for:
* - Ctrl+C
* - Switch back from suspended by Ctrl+Z
*/
void handle_signal(int sig) {
brun_break();
switch (sig) {
case SIGCONT:
tcsetattr(STDIN_FILENO, TCSANOW, &raw_termios);
break;
case SIGINT:
case SIGTERM:
case SIGHUP:
brun_break();
break;
}
}

/**
Expand All @@ -149,7 +160,7 @@ void terminal_init(void) {
tcgetattr(STDIN_FILENO, &original_termios);

// 2. Copy them for restoration later
struct termios raw = original_termios;
raw_termios = original_termios;

// 3. Modify settings:
// Disable canonical mode (ICANON)
Expand All @@ -161,25 +172,26 @@ void terminal_init(void) {
// Disable parity checking (INPCK)
// Disable stripping of 8th bit of each input (ISTRIP)
// Set character size to 8 bit (CS8)
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw.c_cflag |= (CS8);
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
raw_termios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
raw_termios.c_cflag |= (CS8);
raw_termios.c_lflag &= ~(ECHO | ICANON | IEXTEN);

// 4. Set timeout and minimum number of bytes for read()
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 0;
raw_termios.c_cc[VMIN] = 0;
raw_termios.c_cc[VTIME] = 0;

// 5. Apply the new settings
tcsetattr(STDIN_FILENO, TCSANOW, &raw);
tcsetattr(STDIN_FILENO, TCSANOW, &raw_termios);

// 6. Register signal handlers
struct sigaction sa = {0};
sa.sa_handler = handle_signal;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESETHAND;
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGCONT, &sa, NULL);
}

void terminal_getSize(int *x, int *y) {
Expand Down