-
Notifications
You must be signed in to change notification settings - Fork 44
/
yaft.c
237 lines (202 loc) · 5.65 KB
/
yaft.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* See LICENSE for licence details. */
/* yaft.c: include main function */
#include "yaft.h"
#include "conf.h"
#include "util.h"
#include "fb/common.h"
#include "terminal.h"
#include "ctrlseq/esc.h"
#include "ctrlseq/csi.h"
#include "ctrlseq/osc.h"
#include "ctrlseq/dcs.h"
#include "parse.h"
void sig_handler(int signo)
{
sigset_t sigset;
/* global */
extern volatile sig_atomic_t vt_active;
extern volatile sig_atomic_t child_alive;
extern volatile sig_atomic_t need_redraw;
logging(DEBUG, "caught signal! no:%d\n", signo);
if (signo == SIGCHLD) {
child_alive = false;
wait(NULL);
} else if (signo == SIGUSR1) { /* vt activate */
vt_active = true;
need_redraw = true;
ioctl(STDIN_FILENO, VT_RELDISP, VT_ACKACQ);
} else if (signo == SIGUSR2) { /* vt deactivate */
vt_active = false;
ioctl(STDIN_FILENO, VT_RELDISP, 1);
if (BACKGROUND_DRAW) { /* update passive cursor */
need_redraw = true;
} else { /* sleep until next vt switching */
sigfillset(&sigset);
sigdelset(&sigset, SIGUSR1);
sigsuspend(&sigset);
}
}
}
void set_rawmode(int fd, struct termios *save_tm)
{
struct termios tm;
tm = *save_tm;
tm.c_iflag = tm.c_oflag = 0;
tm.c_cflag &= ~CSIZE;
tm.c_cflag |= CS8;
tm.c_lflag &= ~(ECHO | ISIG | ICANON);
tm.c_cc[VMIN] = 1; /* min data size (byte) */
tm.c_cc[VTIME] = 0; /* time out */
etcsetattr(fd, TCSAFLUSH, &tm);
}
bool tty_init(struct termios *termios_orig)
{
struct sigaction sigact;
memset(&sigact, 0, sizeof(struct sigaction));
sigact.sa_handler = sig_handler;
sigact.sa_flags = SA_RESTART;
esigaction(SIGCHLD, &sigact, NULL);
if (VT_CONTROL) {
esigaction(SIGUSR1, &sigact, NULL);
esigaction(SIGUSR2, &sigact, NULL);
struct vt_mode vtm;
vtm.mode = VT_PROCESS;
vtm.waitv = 0;
vtm.acqsig = SIGUSR1;
vtm.relsig = SIGUSR2;
vtm.frsig = 0;
if (ioctl(STDIN_FILENO, VT_SETMODE, &vtm))
logging(WARN, "ioctl: VT_SETMODE failed (maybe here is not console)\n");
if (FORCE_TEXT_MODE == false) {
if (ioctl(STDIN_FILENO, KDSETMODE, KD_GRAPHICS))
logging(WARN, "ioctl: KDSETMODE failed (maybe here is not console)\n");
}
}
etcgetattr(STDIN_FILENO, termios_orig);
set_rawmode(STDIN_FILENO, termios_orig);
ewrite(STDIN_FILENO, "\033[?25l", 6); /* make cusor invisible */
return true;
}
void tty_die(struct termios *termios_orig)
{
/* no error handling */
struct sigaction sigact;
struct vt_mode vtm;
memset(&sigact, 0, sizeof(struct sigaction));
sigact.sa_handler = SIG_DFL;
sigaction(SIGCHLD, &sigact, NULL);
if (VT_CONTROL) {
sigaction(SIGUSR1, &sigact, NULL);
sigaction(SIGUSR2, &sigact, NULL);
vtm.mode = VT_AUTO;
vtm.waitv = 0;
vtm.relsig = vtm.acqsig = vtm.frsig = 0;
ioctl(STDIN_FILENO, VT_SETMODE, &vtm);
if (FORCE_TEXT_MODE == false)
ioctl(STDIN_FILENO, KDSETMODE, KD_TEXT);
}
tcsetattr(STDIN_FILENO, TCSAFLUSH, termios_orig);
fflush(stdout);
ewrite(STDIN_FILENO, "\033[?25h", 6); /* make cursor visible */
}
bool fork_and_exec(int *master, const char *cmd, char *const argv[], int lines, int cols)
{
pid_t pid;
struct winsize ws = {.ws_row = lines, .ws_col = cols,
/* XXX: this variables are UNUSED (man tty_ioctl),
but useful for calculating terminal cell size */
.ws_ypixel = CELL_HEIGHT * lines, .ws_xpixel = CELL_WIDTH * cols};
pid = eforkpty(master, NULL, NULL, &ws);
if (pid < 0)
return false;
else if (pid == 0) { /* child */
esetenv("TERM", term_name, 1);
eexecvp(cmd, argv);
/* never reach here */
exit(EXIT_FAILURE);
}
return true;
}
int check_fds(fd_set *fds, struct timeval *tv, int input, int master)
{
FD_ZERO(fds);
FD_SET(input, fds);
FD_SET(master, fds);
tv->tv_sec = 0;
tv->tv_usec = SELECT_TIMEOUT;
return eselect(master + 1, fds, NULL, NULL, tv);
}
int main(int argc, char *const argv[])
{
extern const char *shell_cmd; /* defined in conf.h */
const char *cmd;
uint8_t buf[BUFSIZE];
ssize_t size;
fd_set fds;
struct timeval tv;
struct framebuffer_t fb;
struct terminal_t term;
/* global */
extern volatile sig_atomic_t need_redraw;
extern volatile sig_atomic_t child_alive;
extern struct termios termios_orig;
/* init */
if (setlocale(LC_ALL, "") == NULL) /* for wcwidth() */
logging(WARN, "setlocale falied\n");
if (!fb_init(&fb)) {
logging(FATAL, "framebuffer initialize failed\n");
goto fb_init_failed;
}
if (!term_init(&term, fb.info.width, fb.info.height)) {
logging(FATAL, "terminal initialize failed\n");
goto term_init_failed;
}
if (!tty_init(&termios_orig)) {
logging(FATAL, "tty initialize failed\n");
goto tty_init_failed;
}
/* fork and exec shell */
cmd = (argc < 2) ? shell_cmd: argv[1];
if (!fork_and_exec(&term.fd, cmd, argv + 1, term.lines, term.cols)) {
logging(FATAL, "forkpty failed\n");
goto tty_init_failed;
}
child_alive = true;
/* main loop */
while (child_alive) {
if (need_redraw) {
need_redraw = false;
cmap_update(fb.fd, fb.cmap); /* after VT switching, need to restore cmap (in 8bpp mode) */
redraw(&term);
refresh(&fb, &term);
}
if (check_fds(&fds, &tv, STDIN_FILENO, term.fd) == -1)
continue;
if (FD_ISSET(STDIN_FILENO, &fds)) {
if ((size = read(STDIN_FILENO, buf, BUFSIZE)) > 0)
ewrite(term.fd, buf, size);
}
if (FD_ISSET(term.fd, &fds)) {
if ((size = read(term.fd, buf, BUFSIZE)) > 0) {
if (VERBOSE)
ewrite(STDOUT_FILENO, buf, size);
parse(&term, buf, size);
if (LAZY_DRAW && size == BUFSIZE)
continue; /* maybe more data arrives soon */
refresh(&fb, &term);
}
}
}
/* normal exit */
tty_die(&termios_orig);
term_die(&term);
fb_die(&fb);
return EXIT_SUCCESS;
/* error exit */
tty_init_failed:
term_die(&term);
term_init_failed:
fb_die(&fb);
fb_init_failed:
return EXIT_FAILURE;
}