-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreview.c
More file actions
347 lines (308 loc) · 10.4 KB
/
Copy pathpreview.c
File metadata and controls
347 lines (308 loc) · 10.4 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#pragma once
#include "color.c"
#include "global.h"
#include "sixel.c"
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
static pthread_t g_preview_thread;
static bool g_preview_thread_started = false;
static pthread_cond_t g_preview_cond = PTHREAD_COND_INITIALIZER;
static pid_t g_preview_child_pid = -1;
static bool g_preview_has_request = false;
static int g_preview_req_offset_x = 0;
static char g_preview_req_file_name[PATH_MAX];
static char g_preview_current_path[PATH_MAX];
static time_t g_preview_current_mtime;
static void preview_invalidate(void) {
g_preview_current_path[0] = '\0';
}
static bool preview_is_current(const char *file_name) {
if (g_preview_current_path[0] == '\0')
return false;
char full_path[PATH_MAX];
const int needed = snprintf(full_path, sizeof full_path, "%s/%s", g_cwd, file_name);
if (needed < 0 || needed >= (int)sizeof full_path)
return false;
if (strcmp(g_preview_current_path, full_path) != 0)
return false;
struct stat st;
if (stat(full_path, &st) != 0)
return false;
return st.st_mtime == g_preview_current_mtime;
}
static void preview_stop_previous(void) {
pid_t pid = g_preview_child_pid;
if (pid > 0) {
g_preview_child_pid = -1;
kill(pid, SIGTERM);
for (int i = 0; i < 50; i++) {
int status;
pid_t r = waitpid(pid, &status, WNOHANG);
if (r == pid) {
return;
}
usleep(10000);
}
kill(pid, SIGKILL);
int status;
(void)waitpid(pid, &status, 0);
}
}
static void render_ansi_line(const int preview_x, const int preview_width, const int preview_bottom, const char *buf,
const size_t len, int *y, int *lsp, bool *truncated, ansi_state_t *state) {
// Don't render if we've already reached or passed the bottom
if (*y >= preview_bottom) {
*truncated = true;
return;
}
const int y_before = *y;
render_ansi_str(buf, (int)len, preview_x, y, preview_x + preview_width, true, TAB_WIDTH, preview_bottom, truncated,
state);
for (int line = y_before + 1; line <= *y && !*truncated; line++) {
(*lsp)++;
if (*lsp >= 3 || line == 2) {
tb_present();
*lsp = 0;
}
}
if (!*truncated) {
(*y)++;
(*lsp)++;
if (*lsp >= 3 || *y == 2) {
tb_present();
*lsp = 0;
}
if (*y >= preview_bottom) {
*truncated = true;
}
} else {
// render_ansi_str set *truncated when wrapping past bottom_y; the last
// chunk was written to termbox2's buffer via tb_printf but never flushed
tb_present();
*lsp = 0;
}
}
static void *preview_worker(void *arg) {
(void)arg;
unsigned int my_generation = 0;
int current_offset_x = 0;
char current_file[PATH_MAX];
for (;;) {
pthread_mutex_lock(&g_preview_mutex);
while (!g_preview_has_request) {
pthread_cond_wait(&g_preview_cond, &g_preview_mutex);
}
my_generation = g_preview_generation;
current_offset_x = g_preview_req_offset_x;
strlcpy(current_file, g_preview_req_file_name, sizeof(current_file));
g_preview_has_request = false;
pthread_mutex_unlock(&g_preview_mutex);
preview_stop_previous();
int pipefd[2];
if (pipe(pipefd) == -1) {
continue;
}
const pid_t pid = fork();
if (pid == -1) {
close(pipefd[0]);
close(pipefd[1]);
continue;
}
if (pid == 0) {
// Detach from the controlling terminal and cut off stdin so previewers
// (e.g. chafa) can't probe the tty; their query replies would otherwise
// reach clf's input and get misread as keystrokes.
setsid();
const int nullfd = open("/dev/null", O_RDONLY);
if (nullfd >= 0) {
dup2(nullfd, STDIN_FILENO);
if (nullfd > STDERR_FILENO)
close(nullfd);
}
dup2(pipefd[1], STDOUT_FILENO);
dup2(pipefd[1], STDERR_FILENO);
close(pipefd[0]);
close(pipefd[1]);
char width_str[16];
char height_str[16];
snprintf(width_str, sizeof(width_str), "%d", tb_width() - current_offset_x);
snprintf(height_str, sizeof(height_str), "%d", tb_height());
execlp(CMD_PREVIEW, CMD_PREVIEW, current_file, width_str, height_str, (char *)NULL);
execlp("head", "head", "-n", height_str, current_file, (char *)NULL); // fallback
_exit(127);
}
close(pipefd[1]);
g_preview_child_pid = pid;
const int flags = fcntl(pipefd[0], F_GETFL, 0);
fcntl(pipefd[0], F_SETFL, flags | O_NONBLOCK);
const int preview_x = current_offset_x + 2;
const int preview_width = MAX(tb_width() - preview_x, 1);
const size_t read_cap = (size_t)MAX(BUF_SIZE, preview_width * 4);
char *read_buf = malloc(read_cap);
if (!read_buf) {
close(pipefd[0]);
continue;
}
const size_t line_cap = (size_t)MAX((int)preview_width * 8 + 128, BUF_SIZE);
char *line_buf = malloc(line_cap);
if (!line_buf) {
free(read_buf);
close(pipefd[0]);
continue;
}
size_t line_len = 0;
int y = 1;
bool truncated = false;
int lsp = 0;
bool stale = false;
ansi_state_t ansi_state;
ansi_state_reset(&ansi_state);
char *sixel_data = NULL;
bool sixel_esc_waiting = false;
int preview_bottom = tb_height() - (g_msg_line_count ? g_msg_line_count : 1);
for (;;) {
pthread_mutex_lock(&g_preview_mutex);
stale = (g_preview_generation != my_generation);
pthread_mutex_unlock(&g_preview_mutex);
if (stale) {
preview_stop_previous();
break;
}
preview_bottom = tb_height() - (g_msg_line_count ? g_msg_line_count : 1);
bool eof = false;
struct pollfd pfd = {.fd = pipefd[0], .events = POLLIN | POLLHUP};
const int pr = poll(&pfd, 1, 50);
if (pr > 0 && (pfd.revents & (POLLIN | POLLHUP))) {
for (;;) {
ssize_t n = read(pipefd[0], read_buf, read_cap);
if (n > 0) {
for (ssize_t i = 0; i < n; i++) {
const unsigned char ch = (unsigned char)read_buf[i];
if (OPT_IMAGE_SIXEL) {
size_t sixel_data_len = 0;
const int parse_result =
sixel_try_parse_char(&sixel_esc_waiting, ch, (const unsigned char *)read_buf + i + 1,
(size_t)n - (size_t)i - 1, line_buf, &line_len, &sixel_data, &sixel_data_len);
if (parse_result == 1)
continue;
if (parse_result == 2) {
if (line_len > 0) {
line_buf[line_len] = '\0';
pthread_mutex_lock(&g_tb_mutex);
render_ansi_line(preview_x, preview_width, preview_bottom, line_buf, line_len, &y, &lsp, &truncated,
&ansi_state);
line_len = 0;
pthread_mutex_unlock(&g_tb_mutex);
if (truncated)
goto cleanup;
}
stale = sixel_display(pipefd[0], &sixel_data, sixel_data_len, read_buf, read_cap, my_generation,
preview_x, preview_width);
goto cleanup;
}
}
if (ch == '\0') {
preview_stop_previous();
pthread_mutex_lock(&g_tb_mutex);
tb_print(preview_x, 1, COLOR_REVERSE, COLOR_DEFAULT, "binary");
tb_present();
pthread_mutex_unlock(&g_tb_mutex);
stale = true;
goto cleanup;
}
if (ch == '\n') {
line_buf[line_len] = '\0';
pthread_mutex_lock(&g_tb_mutex);
render_ansi_line(preview_x, preview_width, preview_bottom, line_buf, line_len, &y, &lsp, &truncated,
&ansi_state);
line_len = 0;
pthread_mutex_unlock(&g_tb_mutex);
if (truncated)
goto cleanup;
} else {
if (line_len >= line_cap - 1) {
line_buf[line_len] = '\0';
pthread_mutex_lock(&g_tb_mutex);
render_ansi_line(preview_x, preview_width, preview_bottom, line_buf, line_len, &y, &lsp, &truncated,
&ansi_state);
line_len = 0;
pthread_mutex_unlock(&g_tb_mutex);
if (truncated)
goto cleanup;
}
line_buf[line_len++] = ch;
}
}
} else if (n == 0) {
eof = true;
break;
} else {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
}
eof = true;
break;
}
}
}
int status;
(void)waitpid(pid, &status, WNOHANG);
if (eof) {
break;
}
}
cleanup:
pthread_mutex_lock(&g_tb_mutex);
if (!stale && !truncated && line_len > 0) {
line_buf[line_len] = '\0';
render_ansi_line(preview_x, preview_width, preview_bottom, line_buf, line_len, &y, &lsp, &truncated, &ansi_state);
line_len = 0;
}
if (lsp > 0) {
tb_present();
}
pthread_mutex_unlock(&g_tb_mutex);
if (truncated) {
preview_stop_previous();
} else if (!stale) {
int status;
(void)waitpid(pid, &status, 0);
g_preview_child_pid = -1;
}
close(pipefd[0]);
free(sixel_data);
free(read_buf);
free(line_buf);
}
preview_stop_previous();
return NULL;
}
void preview_start(const int offset_x, const char *file_name) {
pthread_mutex_lock(&g_preview_mutex);
if (!g_preview_thread_started) {
if (pthread_create(&g_preview_thread, NULL, preview_worker, NULL) == 0) {
pthread_detach(g_preview_thread);
g_preview_thread_started = true;
}
}
g_preview_req_offset_x = offset_x;
strlcpy(g_preview_req_file_name, file_name, sizeof(g_preview_req_file_name));
{
char full_path[PATH_MAX];
const int needed = snprintf(full_path, sizeof full_path, "%s/%s", g_cwd, file_name);
if (needed >= 0 && needed < (int)sizeof full_path) {
strlcpy(g_preview_current_path, full_path, sizeof g_preview_current_path);
struct stat st;
g_preview_current_mtime = (stat(full_path, &st) == 0) ? st.st_mtime : 0;
}
}
g_preview_has_request = true;
g_preview_generation++;
pthread_cond_signal(&g_preview_cond);
pthread_mutex_unlock(&g_preview_mutex);
}