forked from martanne/abduco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
144 lines (130 loc) · 3.33 KB
/
client.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
static void client_sigwinch_handler(int sig) {
client.need_resize = true;
}
static bool client_send_packet(Packet *pkt) {
print_packet("client-send:", pkt);
if (send_packet(server.socket, pkt))
return true;
debug("FAILED\n");
server.running = false;
return false;
}
static bool client_recv_packet(Packet *pkt) {
if (recv_packet(server.socket, pkt)) {
print_packet("client-recv:", pkt);
return true;
}
debug("client-recv: FAILED\n");
server.running = false;
return false;
}
static void client_restore_terminal(void) {
if (!has_term)
return;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_term);
if (alternate_buffer) {
printf("\033[?25h\033[?1049l");
fflush(stdout);
alternate_buffer = false;
}
}
static void client_setup_terminal(void) {
if (!has_term)
return;
atexit(client_restore_terminal);
cur_term = orig_term;
cur_term.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF);
cur_term.c_oflag &= ~(OPOST);
cur_term.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
cur_term.c_cflag &= ~(CSIZE|PARENB);
cur_term.c_cflag |= CS8;
cur_term.c_cc[VLNEXT] = _POSIX_VDISABLE;
cur_term.c_cc[VMIN] = 1;
cur_term.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &cur_term);
if (!alternate_buffer) {
printf("\033[?1049h\033[H");
fflush(stdout);
alternate_buffer = true;
}
}
static int client_mainloop(void) {
sigset_t emptyset, blockset;
sigemptyset(&emptyset);
sigemptyset(&blockset);
sigaddset(&blockset, SIGWINCH);
sigprocmask(SIG_BLOCK, &blockset, NULL);
client.need_resize = true;
Packet pkt = {
.type = MSG_ATTACH,
.u.i = client.flags,
.len = sizeof(pkt.u.i),
};
client_send_packet(&pkt);
while (server.running) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(server.socket, &fds);
if (client.need_resize) {
struct winsize ws;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) != -1) {
Packet pkt = {
.type = MSG_RESIZE,
.u = { .ws = { .rows = ws.ws_row, .cols = ws.ws_col } },
.len = sizeof(pkt.u.ws),
};
if (client_send_packet(&pkt))
client.need_resize = false;
}
}
if (pselect(server.socket+1, &fds, NULL, NULL, NULL, &emptyset) == -1) {
if (errno == EINTR)
continue;
die("client-mainloop");
}
if (FD_ISSET(server.socket, &fds)) {
Packet pkt;
if (client_recv_packet(&pkt)) {
switch (pkt.type) {
case MSG_CONTENT:
if (!passthrough)
write_all(STDOUT_FILENO, pkt.u.msg, pkt.len);
break;
case MSG_RESIZE:
client.need_resize = true;
break;
case MSG_EXIT:
client_send_packet(&pkt);
close(server.socket);
return pkt.u.i;
}
}
}
if (FD_ISSET(STDIN_FILENO, &fds)) {
Packet pkt = { .type = MSG_CONTENT };
ssize_t len = read(STDIN_FILENO, pkt.u.msg, sizeof(pkt.u.msg));
if (len == -1 && errno != EAGAIN && errno != EINTR)
die("client-stdin");
if (len > 0) {
debug("client-stdin: %c\n", pkt.u.msg[0]);
pkt.len = len;
if (KEY_REDRAW && pkt.u.msg[0] == KEY_REDRAW) {
client.need_resize = true;
} else if (pkt.u.msg[0] == KEY_DETACH) {
pkt.type = MSG_DETACH;
pkt.len = 0;
client_send_packet(&pkt);
close(server.socket);
return -1;
} else if (!(client.flags & CLIENT_READONLY)) {
client_send_packet(&pkt);
}
} else if (len == 0) {
debug("client-stdin: EOF\n");
return -1;
}
}
}
return -EIO;
}