-
Notifications
You must be signed in to change notification settings - Fork 20
/
terminal_posix.c
79 lines (66 loc) · 1.4 KB
/
terminal_posix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* gbsplay is a Gameboy sound player
*
* 2020 (C) by Tobias Diedrich <[email protected]>
*
* Licensed under GNU GPL v1 or, at your option, any later version.
*/
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <signal.h>
#include <fcntl.h>
#include "common.h"
#include "terminal.h"
static long terminit;
static struct termios ots;
void exit_handler(int signum)
{
printf(_("\nCaught signal %d, exiting...\n"), signum);
restore_terminal();
exit(1);
}
void stop_handler(int signum)
{
restore_terminal();
}
void cont_handler(int signum)
{
setup_terminal();
redraw = true;
}
static void setup_handlers(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = exit_handler;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGSEGV, &sa, NULL);
sa.sa_handler = stop_handler;
sigaction(SIGSTOP, &sa, NULL);
sigaction(SIGABRT, &sa, NULL);
sa.sa_handler = cont_handler;
sigaction(SIGCONT, &sa, NULL);
}
void setup_terminal(void)
{
struct termios ts;
setup_handlers();
if (tcgetattr(STDIN_FILENO, &ts) == -1)
return;
ots = ts;
ts.c_lflag &= ~(ICANON | ECHO | ECHONL);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &ts);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
terminit = 1;
}
void restore_terminal(void)
{
if (terminit)
tcsetattr(STDIN_FILENO, TCSAFLUSH, &ots);
}
long get_input(char *c)
{
return read(STDIN_FILENO, c, 1) == 1;
}