ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/tui/update_banner.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define TUI_UPDATE_BANNER_CLIPPED_BUFFER_SIZE 1024
4#define TUI_UPDATE_BANNER_HEADING_BUFFER_SIZE 160
5#define TUI_UPDATE_BANNER_INSTALLED_BUFFER_SIZE 160
6#define TUI_UPDATE_BANNER_LATEST_BUFFER_SIZE 160
7#define TUI_UPDATE_BANNER_INSTALLED_VERSION_BUFFER_SIZE 128
8#define TUI_UPDATE_BANNER_COMMAND_BUFFER_SIZE 512
9#define TUI_UPDATE_BANNER_INSTRUCTIONS_BUFFER_SIZE 128
10#define TUI_UPDATE_BANNER_DOMAIN_LINK_BUFFER_SIZE 128
11#define TUI_UPDATE_BANNER_STYLED_INSTRUCTIONS_BUFFER_SIZE 256
12#define TUI_UPDATE_BANNER_INSTRUCTIONS_EXTENDED_BUFFER_SIZE 256
13#define TUI_UPDATE_BANNER_OUTPUT_BUFFER_SIZE 2048
14#define TUI_UPDATE_BANNER_NOTIFICATION_BUFFER_SIZE 1024
15
16#include <ascii-chat/otel.h>
17#include <options/options.h>
18#include <ascii-chat/ui/popup.h>
29
38#include "session/display.h"
39#include <ascii-chat/common.h>
40#include <libplatform/terminal.h>
41#include <libplatform/keyboard.h>
42#include <libplatform/system.h>
43#include <libplatform/filesystem.h>
44#include <libplatform/thread.h>
45#include <ascii-chat/log/log.h>
49#include <libplatform/lifecycle.h>
50
51#include <stdio.h>
52#include <stdatomic.h>
53#include <signal.h>
54#include <string.h>
55#include <uv.h>
56
57// Thread-safe storage for the update check result
58static update_check_result_t g_update_result;
59static mutex_t g_update_result_mutex;
60static bool g_update_result_set = false;
61static lifecycle_t g_update_result_lifecycle = LIFECYCLE_INIT_MUTEX(&g_update_result_mutex);
62
63// Background update check thread
64static platform_thread_t g_update_thread;
65static bool g_update_thread_started = false;
66static bool g_update_checking = false;
67static bool g_update_finished = false;
68static _Atomic bool g_update_overlay = false;
69static _Atomic bool g_update_install_failed = false;
70static _Atomic bool g_update_cancel_requested = false;
71static _Atomic int g_update_selected_action = 0;
72static _Atomic uint64_t g_update_visible_until_ns = 0;
73static void update_banner_open_release(void);
74static void update_banner_present_overlay(void);
75
76static bool update_banner_copy_result(update_check_result_t *result) {
77 if (!result || !lifecycle_is_initialized(&g_update_result_lifecycle)) {
78 return false;
79 }
80 mutex_lock(&g_update_result_mutex);
81 bool set = g_update_result_set;
82 if (set) {
83 *result = g_update_result;
84 }
85 mutex_unlock(&g_update_result_mutex);
86 return set;
87}
88
90 if (!result) {
91 return;
92 }
93
94 // Resolve the command before publishing a result to the presentation thread.
97
98 if (lifecycle_init_once(&g_update_result_lifecycle)) {
99 mutex_init(&g_update_result_mutex, "update_banner_result");
100 lifecycle_init_commit(&g_update_result_lifecycle);
101 }
102
103 ascii_otel_ui_usage("update", result->check_succeeded ? "check_completed" : "check_failed", 0);
104 mutex_lock(&g_update_result_mutex);
105 memcpy(&g_update_result, result, sizeof(g_update_result));
106 g_update_result_set = true;
107 mutex_unlock(&g_update_result_mutex);
108}
109
111 update_check_result_t result = {0};
112 return update_banner_copy_result(&result) && result.update_available;
113}
114
115static void update_center_line(frame_buffer_t *out, int row, int left, int width, const char *text, log_color_t color) {
117 truncate_with_ellipsis(text, clipped, sizeof(clipped), width);
118 int padding = (width - display_width(clipped)) / 2;
119 frame_buffer_printf(out, "\033[%d;%dH%s", row, left + padding, colored_string(color, clipped));
120}
121
122static void update_border(frame_buffer_t *out, int row, int left, int width, const char *first, const char *horizontal, const char *last, log_color_t color) {
123 frame_buffer_printf(out, "\033[%d;%dH%s", row, left, colored_string(color, first));
124 int edges = (first[0] ? 1 : 0) + (last[0] ? 1 : 0);
125 const char *stroke = colored_string(color, horizontal);
126 for (int col = 0; col < width - edges; ++col)
127 frame_buffer_append(out, stroke, strlen(stroke));
128 frame_buffer_printf(out, "%s", colored_string(color, last));
129}
130
131static int update_action_count(bool startup, bool checking, bool available) {
132 return startup ? 3 : checking ? 1 : available ? 4 : 2;
133}
134
135static bool update_select_key(int key, int count) {
136 if (key != KEY_LEFT && key != KEY_UP && key != KEY_RIGHT && key != KEY_DOWN && key != '\t')
137 return false;
138 ui_button_t buttons[4] = {{.label = ""}, {.label = ""}, {.label = ""}, {.label = ""}};
139 ui_buttons_t state = {.selected = (size_t)atomic_load(&g_update_selected_action)};
140 (void)ui_buttons_key(&state, buttons, (size_t)count, (keyboard_key_t)key);
141 atomic_store(&g_update_selected_action, (int)state.selected);
143 return true;
144}
145
146static void render_update_dialog(terminal_size_t size, const update_check_result_t *result, bool startup, bool checking) {
147 bool available = !checking && result->update_available;
148 bool unicode = terminal_should_use_utf8(GET_OPTION(force_utf8));
153 const char *horizontal = unicode ? "─" : "-";
154 const char *vertical = unicode ? "│" : "|";
155 const char *labels[4] = {0};
156 int count = update_action_count(startup, checking, available);
157 if (startup) {
158 labels[0] = " Enter Update now ";
159 labels[1] = " O GitHub ";
160 labels[2] = " Esc Continue ";
161 } else if (available) {
162 labels[0] = " Enter Update now ";
163 labels[1] = " O GitHub ";
164 labels[2] = " Esc / F12 Close ";
165 labels[3] = " R Check again ";
166 } else {
167 labels[0] = " Esc / F12 Close ";
168 labels[1] = " R Check again ";
169 }
170 int buttons_width = (count - 1) * 3;
171 for (int i = 0; i < count; ++i)
172 buttons_width += display_width(labels[i]);
173 int width = size.cols - 4;
174 int preferred_width = available ? 78 : checking ? 62 : 58;
175 if (width > preferred_width)
176 width = preferred_width;
177 bool stacked = buttons_width > width - 6;
178 int height = (available ? 18 : checking ? 15 : 13) + (stacked ? count - 1 : 0);
179 bool failed = atomic_load(&g_update_install_failed);
180 if (width < 24 || height > size.rows - 2)
181 return;
182 int left = (size.cols - width) / 2 + 1;
183 int top = (size.rows - height) / 2 + 1;
184 frame_buffer_t *out = frame_buffer_create(size.rows, size.cols);
185 if (!out)
186 return;
187 frame_buffer_printf(out, startup ? "\033[?25l\033[0m\033[2J\033[H" : "\033[?25l\033[0m");
188 ui_popup_frame(out, top, left, width, height, cyan);
189 const char *title = checking ? "Check for updates" : available ? "Update available" : result->check_succeeded ? "You're up to date" : "Update check unavailable";
191 snprintf(heading, sizeof(heading), "\033[1m%s", title);
192 update_center_line(out, top + (checking ? 1 : 2), left + 2, width - 4, heading, checking ? cyan : available ? yellow : result->check_succeeded ? green : yellow);
193 if (checking) {
194 ui_spinner_render(out, top + 3, left + (width - 7) / 2, time_get_ns());
195 update_center_line(out, top + 7, left + 2, width - 4, "Contacting GitHub Releases...", LOG_COLOR_RESET);
196 update_center_line(out, top + 9, left + 2, width - 4, "Video chat in your terminal", muted);
197 } else if (result->check_succeeded) {
199 snprintf(installed_version, sizeof(installed_version), "%s%s", result->current_version[0] == 'v' ? "" : "v", result->current_version);
200 snprintf(installed, sizeof(installed), "Installed %s", colored_string(available ? yellow : green, installed_version));
201 snprintf(latest, sizeof(latest), "Latest %s", colored_string(green, result->latest_version));
202 update_center_line(out, top + 4, left + 2, width - 4, installed, LOG_COLOR_RESET);
203 update_center_line(out, top + 5, left + 2, width - 4, latest, LOG_COLOR_RESET);
204 if (available) {
205 update_center_line(out, top + 6, left + 2, width - 4, "A new version of ascii-chat is available.", muted);
208 int command_width = display_width(command) + 6;
209 if (command_width > width - 6)
210 command_width = width - 6;
211 int command_left = left + (width - command_width) / 2;
212 update_border(out, top + 7, command_left, command_width, unicode ? "┌" : "+", horizontal, unicode ? "┐" : "+", cyan);
213 frame_buffer_printf(out, "\033[%d;%dH%s%*s%s", top + 8, command_left, colored_string(cyan, vertical), command_width - 2, "", colored_string(cyan, vertical));
214 update_center_line(out, top + 8, command_left + 2, command_width - 4, command, cyan);
215 update_border(out, top + 9, command_left, command_width, unicode ? "└" : "+", horizontal, unicode ? "┘" : "+", cyan);
217 truncate_with_ellipsis("Full instructions on ascii-chat.com", instructions, sizeof(instructions), width - 4);
218 int instructions_left = left + 2 + (width - 4 - display_width(instructions)) / 2;
220 (void)terminal_format_hyperlink(domain_link, sizeof(domain_link), "https://ascii-chat.com", "ascii-chat.com", !GET_OPTION(strip_ansi));
221 char *domain = strstr(instructions, "ascii-chat.com");
222 if (domain) {
223 *domain = '\0';
224 snprintf(styled_instructions, sizeof(styled_instructions), "%s%s", instructions, domain_link);
225 } else {
226 snprintf(styled_instructions, sizeof(styled_instructions), "%s", instructions);
227 }
228 frame_buffer_printf(out, "\033[%d;%dH%s", top + 10, instructions_left, colored_string(LOG_COLOR_WHITE, styled_instructions));
229 update_center_line(out, top + 11, left + 2, width - 4, "Binaries for every OS with release notes on GitHub:", muted);
230 update_center_line(out, top + 12, left + 2, width - 4, result->release_url, cyan);
231 } else {
232 update_center_line(out, top + 7, left + 2, width - 4, "Checked just now", muted);
233 }
234 } else {
235 update_center_line(out, top + 6, left + 2, width - 4, "No network result is available.", muted);
236 update_center_line(out, top + 7, left + 2, width - 4, "Press R to retry.", muted);
237 }
238 int button_row = top + (available ? 16 : checking ? 13 : 11);
239 if (failed) {
240 update_center_line(out, button_row - 3, left + 2, width - 4, "Update command failed. See the logs and retry.", yellow);
241 }
242 update_border(out, button_row - 2, left + 2, width - 4, "", horizontal, "", cyan);
243 int selected = atomic_load(&g_update_selected_action) % count;
244 ui_button_t buttons[4];
245 for (int i = 0; i < count; ++i)
246 buttons[i] = (ui_button_t){.label = labels[i]};
247 ui_buttons_render(out, button_row, left + 3, width - 6, buttons, (size_t)count, (size_t)selected);
250}
251
252static void render_update_prompt(terminal_size_t size, const void *data) {
253 render_update_dialog(size, data, true, false);
254}
255
256static void render_update_overlay(terminal_size_t size, const void *data) {
257 (void)data;
259 mutex_lock(&g_update_result_mutex);
260 result = g_update_result;
261 bool checking = g_update_checking;
262 mutex_unlock(&g_update_result_mutex);
263 render_update_dialog(size, &result, false, checking);
264}
265
267 if (!ctx || !terminal_can_prompt_user(!GET_OPTION(snapshot_mode)))
268 return false;
270 mutex_lock(&g_update_result_mutex);
271 result = g_update_result;
272 mutex_unlock(&g_update_result_mutex);
273 if (update_check_is_dismissed(&result)) {
274 return false;
275 }
276 atomic_store(&g_update_selected_action, 0);
277 if (ui_controller_submit(UI_SCREEN_UPDATE, STDOUT_FILENO, (terminal_size_t){.cols = 32, .rows = 23}, render_update_prompt, &result, sizeof(result)) != ASCIICHAT_OK)
278 return false;
279 bool update = false;
280 while (!shutdown_is_requested()) {
281 /* The update prompt is synchronous, but a failed DNS check can put the
282 * error popup above it. Route keys to that modal until it is dismissed. */
285 continue;
286 }
287 keyboard_key_t key = ui_input_wait_key(UI_SCREEN_UPDATE, 100);
288 if (g_update_overlay)
289 return false;
290 if (update_select_key(key, 3))
291 continue;
292 if (key == 'o' || key == 'O' || ((key == '\r' || key == '\n') && atomic_load(&g_update_selected_action) == 1)) {
293 update_banner_open_release();
294 continue;
295 }
296 if (key == '\r' || key == '\n') {
297 update = atomic_load(&g_update_selected_action) == 0;
298 break;
299 }
300 if (key == 'y' || key == 'Y') {
301 update = true;
302 break;
303 }
304 if (key == 'n' || key == 'N' || key == KEY_ESCAPE)
305 break;
306 }
307 if (!g_update_overlay) {
309 }
310 if (!update && !g_update_overlay) {
313 }
314 return update;
315}
316
318 mutex_lock(&g_update_result_mutex);
320 memcpy(&result, &g_update_result, sizeof(result));
321 mutex_unlock(&g_update_result_mutex);
322
324 (void)terminal_format_hyperlink(domain_link, sizeof(domain_link), "https://ascii-chat.com", "ascii-chat.com", !GET_OPTION(strip_ansi));
325 snprintf(instructions, sizeof(instructions), "Full instructions on %s", domain_link);
327 int len = snprintf(output, sizeof(output),
328 "\033[0m\033[2J\033[H\033[?25h"
329 "\nUpdated ascii-chat: %s -> %s\n\nRun ascii-chat again to start the new version.\n\n"
330 "Binaries for every OS with release notes on GitHub:\n%s\n\n"
331 "%s\n\n",
332 result.current_version, result.latest_version, result.release_url, colored_string(LOG_COLOR_WHITE, instructions));
333 if (len > 0 && len < (int)sizeof(output))
334 ui_controller_finish(STDOUT_FILENO, output, (size_t)len);
335}
336
338 update_check_result_t result = {0};
339 if (!update_banner_copy_result(&result) || !result.update_available) {
340 return false;
341 }
342 g_update_install_failed = false;
343
345 int exit_code = -1;
346#ifndef NDEBUG
347 const char *test_command = SAFE_GETENV("ASCII_CHAT_TEST_UPDATE_COMMAND");
348 if (test_command && test_command[0]) {
349#ifdef _WIN32
350 const char *argv[] = {"cmd", "/d", "/c", test_command, NULL};
351 exit_code = platform_execute_subprocess("cmd", argv, NULL, 0, NULL, NULL);
352#else
353 const char *argv[] = {"/bin/sh", "-c", test_command, NULL};
354 exit_code = platform_execute_subprocess("/bin/sh", argv, NULL, 0, NULL, NULL);
355#endif
356 } else
357#endif
358 if (method == INSTALL_METHOD_HOMEBREW) {
359 const char *argv[] = {"brew", "upgrade", "ascii-chat", NULL};
360 exit_code = platform_execute_subprocess("brew", argv, NULL, 0, NULL, NULL);
361 } else if (method == INSTALL_METHOD_SCOOP) {
362 // Scoop is a PowerShell script/shim, rather than a native executable.
363 const char *argv[] = {"powershell", "-NoProfile", "-Command", "$ErrorActionPreference = 'Stop'; scoop update ascii-chat; if (!$?) { exit 1 }", NULL};
364 exit_code = platform_execute_subprocess("powershell", argv, NULL, 0, NULL, NULL);
365 } else if (method == INSTALL_METHOD_ARCH_AUR) {
366 const char *helper = update_check_get_aur_helper();
367 if (!helper) {
368 log_warn("No supported AUR helper is available");
369 g_update_install_failed = true;
371 return false;
372 }
373 const char *argv[] = {helper, "-S", "ascii-chat", NULL};
374 exit_code = platform_execute_subprocess(helper, argv, NULL, 0, NULL, NULL);
375 } else {
376#ifdef _WIN32
377 const char *argv[] = {"powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", "$ErrorActionPreference = 'Stop'; irm https://ascii-chat.com/install.ps1 | iex", NULL};
378 exit_code = platform_execute_subprocess("powershell", argv, NULL, 0, NULL, NULL);
379#else
380 const char *argv[] = {"bash", "-o", "pipefail", "-c", "curl -fsSL https://ascii-chat.com/install.sh | bash", NULL};
381 exit_code = platform_execute_subprocess("bash", argv, NULL, 0, NULL, NULL);
382#endif
383 }
384 if (exit_code != 0) {
385 log_warn("ascii-chat update command exited with status %d", exit_code);
386 g_update_install_failed = true;
388 return false;
389 }
393 return true;
394}
395
396typedef struct {
397 bool exited;
398 int64_t status;
406
407static void update_child_exited(uv_process_t *process, int64_t status, int signal) {
408 update_child_result_t *result = process->data;
409 result->status = status;
410 result->signal = signal;
411 result->exited = true;
412 uv_close((uv_handle_t *)process, NULL);
413}
414
415// A separate process can be terminated on shutdown even while DNS or TLS is blocked.
416static asciichat_error_t update_check_background(update_check_result_t *result, bool *used_cache) {
417 if (used_cache)
418 *used_cache = false;
419 if (shutdown_is_requested() || atomic_load(&g_update_cancel_requested))
421 if (update_check_try_fresh_cache(result)) {
422 if (used_cache)
423 *used_cache = true;
424 return ASCIICHAT_OK;
425 }
426 char executable[PLATFORM_MAX_PATH_LENGTH];
427 if (!platform_get_executable_path(executable, sizeof(executable)))
428 return SET_ERRNO(ERROR_PROCESS_FAILED, "Cannot locate executable for background update check");
429 uv_loop_t loop;
430 int rc = uv_loop_init(&loop);
431 if (rc != 0)
432 return SET_ERRNO(ERROR_PROCESS_FAILED, "Cannot initialize update process: %s", uv_strerror(rc));
433 char *args[] = {executable, "--check-update", NULL};
434 uv_stdio_container_t streams[3] = {0};
435 for (size_t i = 0; i < 3; ++i)
436 streams[i].flags = UV_IGNORE;
437 update_child_result_t child_result = {0};
438 uv_process_t child = {.data = &child_result};
439 uv_process_options_t options = {.file = executable, .args = args, .exit_cb = update_child_exited, .stdio_count = 3, .stdio = streams, .flags = UV_PROCESS_WINDOWS_HIDE};
440 rc = uv_spawn(&loop, &child, &options);
441 if (rc != 0) {
442 uv_loop_close(&loop);
443 return SET_ERRNO(ERROR_PROCESS_FAILED, "Cannot start background update check: %s", uv_strerror(rc));
444 }
445 bool killed = false, timed_out = false;
446 uint64_t deadline = time_get_ns() + 15 * NS_PER_SEC_INT;
447 while (!child_result.exited) {
448 uv_run(&loop, UV_RUN_NOWAIT);
449 bool cancelled = shutdown_is_requested() || atomic_load(&g_update_cancel_requested);
450 bool expired = time_get_ns() >= deadline;
451 if (!child_result.exited && (cancelled || expired) && !killed) {
452 int kill_result = uv_process_kill(&child, SIGKILL);
453 if (kill_result != 0 && kill_result != UV_ESRCH) {
454 log_warn("Cannot stop background update check: %s", uv_strerror(kill_result));
455 uv_close((uv_handle_t *)&child, NULL);
456 uv_run(&loop, UV_RUN_DEFAULT);
457 uv_loop_close(&loop);
458 return cancelled ? ERROR_SIGNAL_INTERRUPT : SET_ERRNO(ERROR_PROCESS_FAILED, "Cannot stop update check");
459 }
460 killed = true;
461 timed_out = expired && !cancelled;
462 }
463 if (!child_result.exited)
464 platform_sleep_ms(10);
465 }
466 uv_run(&loop, UV_RUN_DEFAULT);
467 uv_loop_close(&loop);
468 if (timed_out)
469 return SET_ERRNO(ERROR_NETWORK, "Background update check timed out");
470 if (killed)
472 if (child_result.status != 0 || child_result.signal != 0 || !update_check_try_fresh_cache(result))
473 return SET_ERRNO(ERROR_NETWORK, "Background update check failed");
474 return ASCIICHAT_OK;
475}
476
477static void *update_check_thread_func(void *arg) {
478 bool force = arg != NULL;
479 uint64_t visible_until = time_get_ns() + 2 * NS_PER_SEC_INT;
481 bool used_cache = false;
482 bool background = !force && !splash_should_display(true) && (GET_OPTION(detected_mode) == MODE_MIRROR || GET_OPTION(detected_mode) == MODE_CLIENT || GET_OPTION(detected_mode) == MODE_DISCOVERY);
484 if (background) {
486 bool previous_notifications = errno_notifications_suppress(true);
487 err = update_check_background(&result, &used_cache);
488 if (err != ASCIICHAT_OK && err != ERROR_SIGNAL_INTERRUPT)
489 log_debug("Automatic update check failed: %s", asciichat_error_string(err));
490 errno_notifications_suppress(previous_notifications);
492 } else {
494 bool previous_notifications = errno_notifications_suppress(true);
495 err = force ? update_check_perform(&result) : update_check_startup(&result);
496 errno_notifications_suppress(previous_notifications);
498 }
499 while (!shutdown_is_requested() && !atomic_load(&g_update_cancel_requested)) {
500 uint64_t overlay_until = atomic_load(&g_update_visible_until_ns);
501 if (overlay_until > visible_until)
502 visible_until = overlay_until;
503 if (time_get_ns() >= visible_until)
504 break;
505 platform_sleep_ms(10);
506 }
507 if (err == ASCIICHAT_OK) {
508 if (result.update_available) {
510 update_check_format_notification(&result, notification, sizeof(notification));
511 log_info("%s", notification);
512 splash_set_update_notification(notification);
513 } else {
514 if (used_cache)
515 log_info("Using cached update check result: %s", result.current_version);
516 else
517 log_info("Already on latest version: %s", result.current_version);
518 }
519
520 // Store every terminal state for the reusable update overlay.
522 } else {
523 // A failed retry must not advertise an earlier successful result.
524 update_check_result_t unavailable = {0};
525 update_banner_set_result(&unavailable);
526 }
527 mutex_lock(&g_update_result_mutex);
528 g_update_checking = false;
529 g_update_finished = true;
530 mutex_unlock(&g_update_result_mutex);
531 // With no splash, the check runs in the background instead of holding up
532 // the first frame. Notify interactively once its result is available.
533 if (!force && err == ASCIICHAT_OK && result.update_available && !splash_should_display(true) && (GET_OPTION(detected_mode) == MODE_MIRROR || GET_OPTION(detected_mode) == MODE_CLIENT) &&
534 !atomic_load(&g_update_cancel_requested) && !shutdown_is_requested() && terminal_can_prompt_user(!GET_OPTION(snapshot_mode)) && !update_check_is_dismissed(&result)) {
535 bool expected = false;
536 if (atomic_compare_exchange_strong(&g_update_overlay, &expected, true)) {
537 atomic_store(&g_update_selected_action, 0);
538 update_banner_present_overlay();
539 }
540 }
542 return NULL;
543}
544
545static void update_banner_start_check_impl(bool force) {
546 if (g_update_thread_started)
547 return;
548 atomic_store(&g_update_cancel_requested, false);
549 if (lifecycle_init_once(&g_update_result_lifecycle)) {
550 mutex_init(&g_update_result_mutex, "update_banner_result");
551 lifecycle_init_commit(&g_update_result_lifecycle);
552 }
553 ascii_otel_ui_usage("update", "check_started", 0);
554 mutex_lock(&g_update_result_mutex);
555 g_update_checking = true;
556 g_update_finished = false;
557 atomic_store(&g_update_selected_action, 0);
558 memset(&g_update_result, 0, sizeof(g_update_result));
559 g_update_result_set = false;
560 mutex_unlock(&g_update_result_mutex);
561 if (platform_thread_create(&g_update_thread, "update_check", update_check_thread_func, force ? (void *)1 : NULL) == 0) {
562 g_update_thread_started = true;
563 } else {
564 ascii_otel_ui_usage("update", "check_failed", 0);
565 mutex_lock(&g_update_result_mutex);
566 g_update_checking = false;
567 g_update_finished = true;
568 mutex_unlock(&g_update_result_mutex);
569 }
571}
572
574 update_banner_start_check_impl(false);
575}
576
578 if (g_update_overlay) {
579 ascii_otel_ui_usage("update", "dismissed", 0);
580 g_update_overlay = false;
582 return;
583 }
584 ascii_otel_ui_usage("update", "opened", 0);
585 g_update_overlay = true;
586 atomic_store(&g_update_visible_until_ns, time_get_ns() + 2 * NS_PER_SEC_INT);
587 atomic_store(&g_update_selected_action, 0);
588 bool finished = false;
589 if (lifecycle_is_initialized(&g_update_result_lifecycle)) {
590 mutex_lock(&g_update_result_mutex);
591 finished = g_update_finished;
592 mutex_unlock(&g_update_result_mutex);
593 }
594 if (g_update_thread_started && finished)
596 if (!g_update_thread_started)
597 update_banner_start_check_impl(true);
598 update_banner_present_overlay();
599}
600
601static void update_banner_present_overlay(void) {
602 const bool snapshot = true;
604 if (ui_controller_submit(UI_SCREEN_UPDATE, STDOUT_FILENO, (terminal_size_t){.cols = 32, .rows = 24}, render_update_overlay, &snapshot, sizeof(snapshot)) != ASCIICHAT_OK) {
605 g_update_overlay = false;
606 LOG_ERRNO_IF_SET("Cannot show update checker");
607 }
609}
610
612 if (!lifecycle_is_initialized(&g_update_result_lifecycle))
613 return false;
614 mutex_lock(&g_update_result_mutex);
615 bool checking = g_update_checking;
616 mutex_unlock(&g_update_result_mutex);
617 return checking;
618}
619
621 return g_update_overlay;
622}
623
624static void update_banner_open_release(void) {
626 mutex_lock(&g_update_result_mutex);
627 result = g_update_result;
628 mutex_unlock(&g_update_result_mutex);
629 // The URL can originate in the disk cache. Keep it a single HTTPS argument,
630 // including when Windows passes it through cmd's start builtin.
631 if (!result.check_succeeded || strncmp(result.release_url, "https://", 8) != 0 || !result.release_url[8] || strpbrk(result.release_url, "\"'&|^<>%!`()\\") != NULL)
632 return;
633 for (const unsigned char *ch = (const unsigned char *)result.release_url; *ch; ++ch) {
634 if (*ch <= ' ' || *ch == 127)
635 return;
636 }
637 ascii_otel_ui_usage("update", "release_open_requested", 0);
638#ifdef _WIN32
639 const char *argv[] = {"cmd", "/c", "start", "", result.release_url, NULL};
640 (void)platform_execute_subprocess("cmd", argv, NULL, 0, NULL, NULL);
641#elif defined(__APPLE__)
642 const char *argv[] = {"open", result.release_url, NULL};
643 (void)platform_execute_subprocess("open", argv, NULL, 0, NULL, NULL);
644#else
645 const char *argv[] = {"xdg-open", result.release_url, NULL};
646 (void)platform_execute_subprocess("xdg-open", argv, NULL, 0, NULL, NULL);
647#endif
648}
649
651 if (key == KEY_F12) {
653 return true;
654 }
655 if (!g_update_overlay)
656 return false;
657 bool checking = update_banner_is_checking();
658 update_check_result_t result = {0};
659 bool available = !checking && update_banner_copy_result(&result) && result.update_available;
660 int count = update_action_count(false, checking, available);
661 if (update_select_key(key, count))
662 return true;
663 if (key == '\r' || key == '\n') {
664 int selected = atomic_load(&g_update_selected_action) % count;
665 if ((available && selected == 2) || (!available && selected == 0)) {
667 return true;
668 }
669 if (checking)
670 return true;
671 if (available && selected == 0) {
673 return true;
674 }
675 if (available && selected == 1) {
676 update_banner_open_release();
677 return true;
678 }
679 key = 'r';
680 }
681 if (key == KEY_ESCAPE) {
683 return true;
684 }
685 if (key == 'o' || key == 'O') {
687 update_banner_open_release();
688 return true;
689 }
690 if (key == 'r' || key == 'R') {
692 return true;
694 update_banner_start_check_impl(true);
695 return true;
696 }
697 return g_update_overlay;
698}
699
701 if (!g_update_thread_started) {
702 return;
703 }
704 // Shutdown must remain responsive after cancellation. The worker checks
705 // g_update_cancel_requested and terminates its child process, so one second
706 // is sufficient for normal teardown while avoiding a multi-second Ctrl-C
707 // stall when a platform/network call is slow to unwind.
708 int ret = thread_join_timeout(&g_update_thread, NULL, 1LL * NS_PER_SEC_INT);
709 if (ret != 0) {
710 // The thread has been cancelled, but teardown must not block behind a
711 // platform/network call that ignored cancellation. Detach it and let the
712 // process reclaim its remaining resources at exit.
713 log_warn("Update check thread did not finish within 1s; detaching it during shutdown");
714 thread_detach(&g_update_thread);
715 }
716 g_update_thread_started = false;
717}
718
720 atomic_store(&g_update_cancel_requested, true);
721}
Application-level callbacks for library code.
#define APP_CALLBACK_VOID(callback_name)
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
bool errno_notifications_suppress(bool suppress)
asciichat_errno_scope_t errno_scope_begin(void)
#define SET_ERRNO(code,...)
@ ASCIICHAT_ERRNO_HANDLED
#define LOG_ERRNO_IF_SET(message)
void ui_buttons_render(frame_buffer_t *out, int row, int left, int width, const ui_button_t *b, size_t n, size_t selected)
Definition buttons.c:29
int ui_buttons_key(ui_buttons_t *s, const ui_button_t *b, size_t n, keyboard_key_t key)
Definition buttons.c:8
bool ui_error_popup_visible(void)
Definition error_popup.c:72
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len)
Append raw bytes to the buffer.
void frame_buffer_destroy(frame_buffer_t *buf)
Destroy a frame buffer.
size_t frame_buffer_get_length(const frame_buffer_t *buf)
Get the current content length of the frame buffer.
const char * frame_buffer_get_content(const frame_buffer_t *buf)
Get a pointer to the buffer content.
frame_buffer_t * frame_buffer_create(int rows, int cols)
Create a new frame buffer.
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.
Buffered terminal frame rendering.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
#define SAFE_GETENV(name)
#define PLATFORM_MAX_PATH_LENGTH
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_SIGNAL_INTERRUPT
@ ERROR_NETWORK
Definition error_codes.h:76
@ ERROR_PROCESS_FAILED
@ ASCIICHAT_OK
Definition error_codes.h:50
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
log_color_t
Color enum for logging - indexes into color arrays.
Definition log/log.h:146
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
@ LOG_COLOR_RESET
Definition log/log.h:154
@ LOG_COLOR_GREY
Definition log/log.h:153
@ LOG_COLOR_DEBUG
Definition log/log.h:148
@ LOG_COLOR_INFO
Definition log/log.h:149
@ LOG_COLOR_WARN
Definition log/log.h:150
@ LOG_COLOR_WHITE
Definition log/log.h:155
#define NS_PER_SEC_INT
Definition time.h:173
asciichat_error_t update_check_startup(update_check_result_t *result)
Perform startup update check with caching.
install_method_t
Installation method for suggesting upgrade command.
void update_check_dismiss_for_a_week(update_check_result_t *result)
void update_check_get_upgrade_suggestion(install_method_t method, const char *latest_version, char *buffer, size_t buffer_size)
Get upgrade suggestion string.
const char * update_check_get_aur_helper(void)
asciichat_error_t update_check_perform(update_check_result_t *result)
Check for updates from GitHub releases API.
bool update_check_try_fresh_cache(update_check_result_t *result)
bool update_check_is_dismissed(const update_check_result_t *result)
install_method_t update_check_detect_install_method(void)
Detect installation method.
void update_check_format_notification(const update_check_result_t *result, char *buffer, size_t buffer_size)
Format update notification message.
@ INSTALL_METHOD_ARCH_AUR
Installed via Arch AUR (paru/yay).
@ INSTALL_METHOD_SCOOP
Installed via Scoop.
@ INSTALL_METHOD_HOMEBREW
Installed via Homebrew.
#define GET_OPTION(field)
Definition options.h:1333
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:918
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network).
Definition options.h:919
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:921
bool update_banner_install_update(void)
void update_banner_cancel_check(void)
struct session_display_ctx session_display_ctx_t
session display config is intentionally a small, concrete record. Fields such as snapshot_mode,...
void update_banner_wait_for_check(void)
Wait for the background update check thread to finish.
bool update_banner_show_prompt(session_display_ctx_t *ctx)
Show the update prompt screen and wait for user input.
void update_banner_start_check(void)
Start the background update check thread.
bool update_banner_handle_key(int key)
void splash_set_update_notification(const char *notification)
Set update notification to display on splash/status screens.
Definition splash.c:476
bool splash_should_display(bool is_intro)
Render splash header with rainbow animation (callback for terminal_screen).
Definition splash.c:242
bool update_banner_has_update(void)
Check if an update is available (thread-safe).
bool update_banner_is_checking(void)
void update_banner_toggle_overlay(void)
void update_banner_set_result(const update_check_result_t *result)
Store the update check result (thread-safe).
void update_banner_print_instructions(void)
Print upgrade instructions to stdout and prepare for clean exit.
bool update_banner_is_overlay_open(void)
void shutdown_request(void)
Definition common.c:82
bool shutdown_is_requested(void)
Check if shutdown has been requested.
Definition common.c:86
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
void truncate_with_ellipsis(const char *input, char *output, size_t output_size, int max_width)
Truncate a string to a maximum display width, with ellipsis.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
🖥️ Display Width and Terminal Centering Utilities
action_args_t args
#define TUI_UPDATE_BANNER_HEADING_BUFFER_SIZE
#define TUI_UPDATE_BANNER_INSTALLED_VERSION_BUFFER_SIZE
#define TUI_UPDATE_BANNER_COMMAND_BUFFER_SIZE
#define TUI_UPDATE_BANNER_INSTRUCTIONS_BUFFER_SIZE
#define TUI_UPDATE_BANNER_INSTALLED_BUFFER_SIZE
#define TUI_UPDATE_BANNER_LATEST_BUFFER_SIZE
#define TUI_UPDATE_BANNER_NOTIFICATION_BUFFER_SIZE
#define TUI_UPDATE_BANNER_DOMAIN_LINK_BUFFER_SIZE
#define TUI_UPDATE_BANNER_CLIPPED_BUFFER_SIZE
#define TUI_UPDATE_BANNER_STYLED_INSTRUCTIONS_BUFFER_SIZE
#define TUI_UPDATE_BANNER_OUTPUT_BUFFER_SIZE
#define TUI_UPDATE_BANNER_INSTRUCTIONS_EXTENDED_BUFFER_SIZE
📝 Logging API with multiple log levels and terminal output control
void signal_exit(void)
Definition main.c:252
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
void ascii_otel_ui_usage(const char *screen, const char *event, uint64_t duration_ns)
Definition otel.c:411
asciichat_error_t ui_popup_frame(frame_buffer_t *out, int row, int col, int width, int height, log_color_t color)
Definition popup.c:27
void ui_spinner_render(frame_buffer_t *out, int row, int col, uint64_t elapsed_ns)
Definition progress.c:41
static bool size_t gotel_query_t * out
Definition queries.h:8
Shutdown check system for clean library/application separation.
Intro and status screen display for ascii-chat with animated rainbow effects.
🖥️ Unified terminal display abstraction for session-based rendering
bool terminal_can_prompt_user(bool user_setting)
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
When code needs to work with ui button, it keeps label, shortcut, disabled, color here instead of sca...
Definition buttons.h:22
size_t selected
Definition buttons.h:23
update check result keeps the values that move through the subsystem: update_available,...
char latest_version[64]
Latest version tag (e.g., "v0.9.0").
char current_version[64]
Current version string.
bool update_available
True if newer version exists.
bool check_succeeded
True if check completed (even if no update).
char release_url[512]
GitHub release page URL.
int platform_execute_subprocess(const char *executable, const char **argv, char *output_buffer, size_t output_size, bool(*cancel)(void *), void *data)
Definition system.c:35
⏱️ High-precision timing utilities using sokol_time.h and uthash
void ui_controller_remove(ui_screen_t screen)
asciichat_error_t ui_controller_submit(ui_screen_t screen, int fd, terminal_size_t minimum, ui_render_fn render, const void *snapshot, size_t bytes)
void ui_controller_redraw(void)
void ui_controller_finish(int fd, const char *data, size_t len)
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
@ UI_SCREEN_UPDATE
@ UI_SCREEN_ERROR
keyboard_key_t ui_input_wait_key(ui_screen_t screen, unsigned timeout_ms)
Definition tui/input.c:538
Update available prompt screen shown after splash.