ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
controller.c File Reference

Go to the source code of this file.

Data Structures

struct  screen_terminal_t
struct  screen_snapshot_t
 When code needs to work with media snapshot slot, it keeps busy, snapshot, capacity here instead of scattering it across globals. Initialization establishes the handles and sizes; teardown must release any allocation, lock, thread, or callback context recorded in those fields. More...
struct  media_snapshot_slot
struct  text_snapshot_t
struct  screen
struct  native_view_t

Macros

#define TUI_CONTROLLER_LAST_TITLE_BUFFER_SIZE   512
#define TUI_CONTROLLER_TITLE_BUFFER_SIZE   512
#define TUI_CONTROLLER_BUFFER_BUFFER_SIZE   16384
#define TUI_MEDIA_SNAPSHOT_CAPACITY   8
#define TUI_LAYER_CAPACITY   3

Typedefs

typedef struct media_snapshot_slot media_snapshot_slot_t
typedef struct screen screen_t

Functions

void ui_controller_show_volume (void)
int ui_controller_current_screen (void)
int ui_controller_active_fd (void)
int ui_controller_interactive_screen (void)
void ui_controller_select_interactive (int screen)
void ui_controller_frame_acquired_at (uint64_t started_ns)
void ui_controller_set_effective_fps (unsigned int fps)
size_t ui_controller_trim_utf8_chunk (const char *data, size_t length)
asciichat_error_t ui_controller_write (int fd, const char *data, size_t len)
void ui_controller_finish (int fd, const char *data, size_t len)
bool ui_controller_is_blocked (void)
bool ui_controller_is_presenting (void)
bool ui_controller_is_owner (void)
terminal_size_t ui_controller_size (void)
const animation_sample_t * ui_controller_animation_sample (void)
void ui_controller_title_ready (void)
void ui_controller_title_render (ui_title_render_t phase)
void ui_controller_title_activity (ui_title_activity_t activity)
ui_title_activity_t ui_controller_get_title_activity (void)
void ui_controller_title_discovery (bool has_session, bool joining)
void ui_controller_packet_begin (void)
void ui_controller_packet_end (void)
asciichat_error_t ui_controller_start_terminal (int fd)
asciichat_error_t ui_controller_title_start (const char *mode, int fd)
bool ui_controller_screen_live (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_remove (ui_screen_t screen)
void ui_controller_dismiss (ui_screen_t screen, int previous)
bool ui_controller_first_use_key (int key)
ui_presentation_state_t ui_controller_state (void)
void ui_controller_redraw (void)
void ui_controller_invalidate_media (void)
void ui_controller_shutdown (void)
asciichat_error_t ui_controller_printf (int fd, const char *format,...)
void ui_controller_restore_terminal (void)
void ui_controller_restore_terminal_output (void)
void ui_controller_hide_logs (void)
void ui_controller_toggle_logs (void)
asciichat_error_t ui_controller_present (ui_screen_t screen, int fd, terminal_size_t minimum, const char *data, size_t len)
asciichat_error_t ui_controller_present_retained (ui_screen_t screen, int fd, terminal_size_t minimum, const char *data, size_t len)

Macro Definition Documentation

◆ TUI_CONTROLLER_BUFFER_BUFFER_SIZE

#define TUI_CONTROLLER_BUFFER_BUFFER_SIZE   16384

Definition at line 5 of file tui/controller.c.

Referenced by ui_controller_printf().

◆ TUI_CONTROLLER_LAST_TITLE_BUFFER_SIZE

#define TUI_CONTROLLER_LAST_TITLE_BUFFER_SIZE   512

Definition at line 3 of file tui/controller.c.

◆ TUI_CONTROLLER_TITLE_BUFFER_SIZE

#define TUI_CONTROLLER_TITLE_BUFFER_SIZE   512

Definition at line 4 of file tui/controller.c.

◆ TUI_LAYER_CAPACITY

#define TUI_LAYER_CAPACITY   3

Definition at line 9 of file tui/controller.c.

◆ TUI_MEDIA_SNAPSHOT_CAPACITY

#define TUI_MEDIA_SNAPSHOT_CAPACITY   8

Definition at line 8 of file tui/controller.c.

Typedef Documentation

◆ media_snapshot_slot_t

Definition at line 60 of file tui/controller.c.

◆ screen_t

typedef struct screen screen_t

Function Documentation

◆ ui_controller_active_fd()

int ui_controller_active_fd ( void )

Output descriptor owned by the active screen, or stdout when none is active.

Definition at line 304 of file tui/controller.c.

304 {
305 int fd = atomic_load(&g_default_fd);
306 if (!lifecycle_is_initialized(&g_lifecycle))
307 return fd;
308 mutex_lock(&g_mutex);
309 if (g_active >= 0 && g_active < UI_SCREEN_COUNT && g_screens[g_active].render)
310 fd = g_screens[g_active].fd;
311 mutex_unlock(&g_mutex);
312 return fd;
313}
@ UI_SCREEN_COUNT

References screen::fd, screen::render, and UI_SCREEN_COUNT.

Referenced by tui_stats_render(), ui_controller_first_use_key(), ui_errno_stacks_toggle(), and ui_input_read_key().

◆ ui_controller_animation_sample()

const animation_sample_t * ui_controller_animation_sample ( void )

Animation sample owned by the common runtime during a presentation callback.

Definition at line 870 of file tui/controller.c.

870 {
871 return g_animation;
872}

Referenced by ui_media_panel_render().

◆ ui_controller_current_screen()

int ui_controller_current_screen ( void )

Last published screen priority; does not acquire application locks.

Definition at line 285 of file tui/controller.c.

285 {
286 int background = atomic_load(&g_background_screen);
287 int interactive = atomic_load(&g_interactive_screen);
288 if (interactive == UI_SCREEN_HELP &&
289 (atomic_load(&g_screen_mask) & (1u << UI_SCREEN_HELP)) &&
290 (background != UI_SCREEN_UPDATE ||
293 return UI_SCREEN_HELP;
294 if ((background >= 0 || interactive >= 0) && ui_error_popup_visible())
295 return UI_SCREEN_ERROR;
296 if (interactive < 0 && background < UI_SCREEN_MDNS &&
297 (atomic_load(&g_screen_mask) & (1u << UI_SCREEN_MDNS)))
298 return UI_SCREEN_MDNS;
299 return background > interactive ? background : interactive;
300}
bool ui_error_popup_visible(void)
Definition error_popup.c:74
int keyboard_help_parent_screen(void)
@ UI_SCREEN_UPDATE
@ UI_SCREEN_MDNS
@ UI_SCREEN_HELP
@ UI_SCREEN_ERROR

References keyboard_help_parent_screen(), ui_error_popup_visible(), UI_SCREEN_ERROR, UI_SCREEN_HELP, UI_SCREEN_MDNS, and UI_SCREEN_UPDATE.

Referenced by keyboard_help_toggle_terminal(), session_pipeline_run_main(), ui_controller_is_presenting(), ui_controller_state(), ui_controller_toggle_logs(), ui_controller_write(), ui_errno_stacks_handle_key(), and ui_input_poll_sync().

◆ ui_controller_dismiss()

void ui_controller_dismiss ( ui_screen_t screen,
int previous )

Retire a modal and atomically restore its previous interactive owner if still live. Does not steal focus from a screen selected after the modal opened.

Definition at line 2073 of file tui/controller.c.

2073 {
2074 remove_screen(screen, previous, NULL);
2075}

Referenced by keyboard_help_toggle_terminal(), and ui_prompt_dismiss().

◆ ui_controller_finish()

void ui_controller_finish ( int fd,
const char * data,
size_t len )

Retire live screens and synchronously render final text before the caller exits.

Definition at line 812 of file tui/controller.c.

812 {
813 atomic_store(&g_sync_finishing, true);
814 if (!g_owner)
815 while (atomic_load(&g_sync_rendering))
816 platform_sleep_ms(1);
818 screen_t retired[UI_SCREEN_COUNT] = {0};
819 bool locked = lifecycle_is_initialized(&g_lifecycle);
820 if (locked) {
821 mutex_lock(&g_mutex);
822 g_finished = true;
823 memcpy(retired, g_screens, sizeof(retired));
824 memset(g_screens, 0, sizeof(g_screens));
825 atomic_store(&g_background_screen, -1);
826 atomic_store(&g_screen_mask, 0);
828 cond_signal(&g_render_requested);
829 uint64_t pending = g_render_started;
830 while (!g_render_snapshot && g_render_completed < pending)
831 cond_wait(&g_render_done, &g_mutex);
832 mutex_unlock(&g_mutex);
833 }
834 for (int i = 0; i < UI_SCREEN_COUNT; ++i)
835 release_screen(&retired[i], i == UI_SCREEN_NOTICE);
836 terminal_screen_leave();
837 platform_write_all(fd, data, len);
838 atomic_store_bool(&g_live, false);
839}
void errno_notifications_enable(bool enabled)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
unsigned long long uint64_t
struct screen screen_t
void ui_controller_select_interactive(int screen)
@ UI_SCREEN_NOTICE

References atomic_store_bool(), errno_notifications_enable(), screen::fd, ui_controller_select_interactive(), UI_SCREEN_COUNT, and UI_SCREEN_NOTICE.

Referenced by ui_notice_present(), and update_banner_print_instructions().

◆ ui_controller_first_use_key()

bool ui_controller_first_use_key ( int key)

Handle input for a first-use notice, retaining its snapshot while it is used.

Definition at line 2077 of file tui/controller.c.

2077 {
2078 if (!lifecycle_is_initialized(&g_lifecycle))
2079 return false;
2080 mutex_lock(&g_mutex);
2081 screen_snapshot_t *owned = g_screens[UI_SCREEN_NOTICE].owned;
2082 if (owned)
2083 atomic_fetch_add(&owned->references, 1);
2084 mutex_unlock(&g_mutex);
2085 if (!owned)
2086 return false;
2087 ui_notice_key_result_t result = ui_notice_first_use_key(owned->data, key);
2088 if (result == UI_NOTICE_KEY_HELP)
2090 if (result == UI_NOTICE_KEY_COMPLETE)
2091 ui_notice_first_use_complete(owned->data);
2092 if (result == UI_NOTICE_KEY_COMPLETE || result == UI_NOTICE_KEY_DISMISS)
2093 remove_screen(UI_SCREEN_NOTICE, -1, owned);
2094 else if (result == UI_NOTICE_KEY_HANDLED &&
2095 (key == '\r' || key == '\n' || key == KEY_LEFT || key == KEY_RIGHT))
2097 release_snapshot(owned);
2098 return result != UI_NOTICE_KEY_ORDINARY;
2099}
void keyboard_help_toggle_terminal(int fd)
ui_notice_key_result_t ui_notice_first_use_key(const void *data, int key)
Definition notice.c:498
void ui_notice_first_use_complete(const void *data)
Definition notice.c:523
ui_notice_key_result_t
Definition notice.h:40
@ UI_NOTICE_KEY_HELP
Definition notice.h:43
@ UI_NOTICE_KEY_ORDINARY
Definition notice.h:41
@ UI_NOTICE_KEY_DISMISS
Definition notice.h:44
@ UI_NOTICE_KEY_COMPLETE
Definition notice.h:45
@ UI_NOTICE_KEY_HANDLED
Definition notice.h:42
When code needs to work with media snapshot slot, it keeps busy, snapshot, capacity here instead of s...
_Atomic unsigned references
void ui_controller_redraw(void)
int ui_controller_active_fd(void)

References keyboard_help_toggle_terminal(), screen_snapshot_t::references, ui_controller_active_fd(), ui_controller_redraw(), ui_notice_first_use_complete(), ui_notice_first_use_key(), UI_NOTICE_KEY_COMPLETE, UI_NOTICE_KEY_DISMISS, UI_NOTICE_KEY_HANDLED, UI_NOTICE_KEY_HELP, UI_NOTICE_KEY_ORDINARY, and UI_SCREEN_NOTICE.

Referenced by ui_input_read_key().

◆ ui_controller_frame_acquired_at()

void ui_controller_frame_acquired_at ( uint64_t started_ns)

Carry the acquisition start time with the next media snapshot on this producer thread.

Definition at line 389 of file tui/controller.c.

389 {
390 g_acquisition_started_ns = started_ns;
391}

Referenced by session_pipeline_run_main().

◆ ui_controller_get_title_activity()

ui_title_activity_t ui_controller_get_title_activity ( void )

Current connection or session activity published by the host.

Definition at line 991 of file tui/controller.c.

991 {
992 return (ui_title_activity_t)atomic_load_int(&g_title_activity);
993}
int atomic_load_int(atomic_t *a)
Atomically load an int value.
ui_title_activity_t

References atomic_load_int().

◆ ui_controller_hide_logs()

void ui_controller_hide_logs ( void )

Definition at line 2372 of file tui/controller.c.

2372 {
2375}
void terminal_screen_logs_reset(void)
void ui_controller_remove(ui_screen_t screen)
@ UI_SCREEN_LOGS

References terminal_screen_logs_reset(), ui_controller_remove(), and UI_SCREEN_LOGS.

Referenced by ui_controller_toggle_logs().

◆ ui_controller_interactive_screen()

int ui_controller_interactive_screen ( void )

One interactive screen replaces the previous one; sync can switch without application locks.

Definition at line 322 of file tui/controller.c.

322 {
323 return atomic_load(&g_interactive_screen);
324}

Referenced by keyboard_help_is_active(), keyboard_help_is_active_global(), keyboard_help_toggle_terminal(), session_handle_keyboard_input(), stats_runtime_active(), and ui_sync_is_visible().

◆ ui_controller_invalidate_media()

void ui_controller_invalidate_media ( void )

Invalidate a media snapshot already queued for presentation.

Definition at line 2267 of file tui/controller.c.

2267 {
2268 atomic_fetch_add(&g_media_epoch, 1);
2269 mutex_lock(&g_mutex);
2270 cond_signal(&g_render_requested);
2271 mutex_unlock(&g_mutex);
2272}

◆ ui_controller_is_blocked()

bool ui_controller_is_blocked ( void )

Atomic query, safe for interrupt handling.

Definition at line 841 of file tui/controller.c.

841{ return atomic_load_bool(&g_blocked); }
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.

References atomic_load_bool().

Referenced by keyboard_help_is_active_global(), and ui_input_poll_sync().

◆ ui_controller_is_owner()

bool ui_controller_is_owner ( void )

Definition at line 847 of file tui/controller.c.

847{ return g_owner; }

Referenced by log_console_impl(), and ui_notice_present().

◆ ui_controller_is_presenting()

bool ui_controller_is_presenting ( void )

Lock-free query for console logging, including logs from rendering callbacks.

Definition at line 843 of file tui/controller.c.

843 {
844 return ui_controller_current_screen() >= 0;
845}
int ui_controller_current_screen(void)

References ui_controller_current_screen().

Referenced by log_console_impl(), log_msg(), ui_controller_write(), ui_notice_present(), and ui_prompt_dismiss().

◆ ui_controller_packet_begin()

void ui_controller_packet_begin ( void )

Keep a complete terminal protocol packet and its flush separate from title updates. Call outside presentation callbacks; application shutdown must follow packet completion.

Definition at line 1755 of file tui/controller.c.

1755 {
1756 if (lifecycle_is_initialized(&g_lifecycle))
1757 mutex_lock(&g_title_packet_mutex);
1758}

Referenced by kitty_image_write().

◆ ui_controller_packet_end()

void ui_controller_packet_end ( void )

Definition at line 1760 of file tui/controller.c.

1760 {
1761 if (lifecycle_is_initialized(&g_lifecycle))
1762 mutex_unlock(&g_title_packet_mutex);
1763}

Referenced by kitty_image_write().

◆ ui_controller_present()

asciichat_error_t ui_controller_present ( ui_screen_t screen,
int fd,
terminal_size_t minimum,
const char * data,
size_t len )

Publish an owned copy of an already rendered frame.

Definition at line 2393 of file tui/controller.c.

2395 {
2396 return present_text(screen, fd, minimum, data, len, true);
2397}

Referenced by ascii_write(), and session_display_write_ascii_frame().

◆ ui_controller_present_retained()

asciichat_error_t ui_controller_present_retained ( ui_screen_t screen,
int fd,
terminal_size_t minimum,
const char * data,
size_t len )

Redraw retained media without treating it as a newly acquired frame for FPS.

Definition at line 2398 of file tui/controller.c.

2400 {
2401 return present_text(screen, fd, minimum, data, len, false);
2402}

Referenced by session_display_create(), and session_display_write_ascii_frame().

◆ ui_controller_printf()

asciichat_error_t ui_controller_printf ( int fd,
const char * format,
... )

Definition at line 2339 of file tui/controller.c.

2339 {
2341 va_list args;
2342 va_start(args, format);
2343 int len = vsnprintf(buffer, sizeof(buffer), format, args);
2344 va_end(args);
2345 if (len < 0)
2346 return SET_ERRNO(ERROR_INVALID_PARAM, "Cannot format terminal output");
2347 size_t bytes =
2348 (size_t)len < sizeof(buffer) ? (size_t)len : sizeof(buffer) - 1;
2349 return ui_controller_write(fd, buffer, bytes);
2350}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM
action_args_t args
#define TUI_CONTROLLER_BUFFER_BUFFER_SIZE
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)

References args, ERROR_INVALID_PARAM, SET_ERRNO, TUI_CONTROLLER_BUFFER_BUFFER_SIZE, and ui_controller_write().

Referenced by log_search_render_input_line(), and session_render_loop().

◆ ui_controller_redraw()

void ui_controller_redraw ( void )

Definition at line 2258 of file tui/controller.c.

2258 {
2259 if (!lifecycle_is_initialized(&g_lifecycle))
2260 return;
2261 mutex_lock(&g_mutex);
2262 g_redraw = true;
2263 cond_signal(&g_render_requested);
2264 mutex_unlock(&g_mutex);
2265}

Referenced by ascii_write_init(), keyboard_help_render(), session_display_clear(), session_display_cursor_home(), session_display_reset(), terminal_screen_logs_handle_key_sized(), ui_controller_first_use_key(), ui_error_popup_key(), and update_banner_install_update().

◆ ui_controller_remove()

◆ ui_controller_restore_terminal()

void ui_controller_restore_terminal ( void )

Stop presentation and restore cursor visibility on an interactive terminal.

Definition at line 2352 of file tui/controller.c.

2352 {
2355}
void ui_controller_restore_terminal_output(void)
void ui_controller_shutdown(void)

References ui_controller_restore_terminal_output(), and ui_controller_shutdown().

◆ ui_controller_restore_terminal_output()

void ui_controller_restore_terminal_output ( void )

Restore terminal output without acquiring controller/UI locks.

Definition at line 2357 of file tui/controller.c.

2357 {
2358 if (!platform_isatty(STDOUT_FILENO))
2359 return;
2360 // This path runs immediately before a bounded process exit. Avoid the
2361 // controller locks that may be held by the render thread.
2362 terminal_screen_leave();
2363 static const char sequence[] = "\033[0m\033[?25h";
2364 (void)platform_write_all(STDOUT_FILENO, sequence, sizeof(sequence) - 1);
2365}
int platform_isatty(int fd)
Definition util.c:70

References platform_isatty().

Referenced by asciichat_shared_destroy(), and ui_controller_restore_terminal().

◆ ui_controller_screen_live()

bool ui_controller_screen_live ( ui_screen_t screen)

Definition at line 1802 of file tui/controller.c.

1802 {
1803 return screen >= 0 && screen < UI_SCREEN_COUNT &&
1804 (atomic_load(&g_screen_mask) & (1u << screen));
1805}

References UI_SCREEN_COUNT.

Referenced by stats_runtime_start(), and ui_input_read_key().

◆ ui_controller_select_interactive()

void ui_controller_select_interactive ( int screen)

Definition at line 326 of file tui/controller.c.

326 {
327 atomic_store(&g_interactive_screen, screen);
328}

Referenced by stats_runtime_toggle(), ui_controller_finish(), ui_controller_shutdown(), ui_sync_handle_key(), and ui_sync_stop().

◆ ui_controller_set_effective_fps()

void ui_controller_set_effective_fps ( unsigned int fps)

Set the target FPS displayed by the media overlay.

Definition at line 393 of file tui/controller.c.

393 {
394 atomic_store_int(&g_effective_fps, (int)(fps > 0 ? fps : 60));
395 if (g_fps)
397}
void atomic_store_int(atomic_t *a, int value)
Atomically store an int value.
void fps_counter_set_effective_fps(fps_counter_t *counter, unsigned int fps)

References atomic_store_int(), and fps_counter_set_effective_fps().

◆ ui_controller_show_volume()

void ui_controller_show_volume ( void )

Show volume feedback for 1.5 seconds after a volume or mute control is used.

Definition at line 232 of file tui/controller.c.

232 {
233 if (!GET_OPTION(snapshot_mode))
234 atomic_store_u64(&g_volume_until, time_get_ns() + 1500 * NS_PER_MS_INT);
235}
void atomic_store_u64(atomic_t *a, uint64_t value)
Atomically store a uint64_t value.
#define NS_PER_MS_INT
Definition time.h:172
#define GET_OPTION(field)
Definition options.h:1339

References atomic_store_u64(), GET_OPTION, and NS_PER_MS_INT.

◆ ui_controller_shutdown()

void ui_controller_shutdown ( void )

Definition at line 2302 of file tui/controller.c.

2302 {
2303 if (!lifecycle_destroy_once(&g_lifecycle))
2304 return;
2305 mutex_lock(&g_mutex);
2306 atomic_store_bool(&g_presentation_stop_requested, true);
2307 cond_signal(&g_render_requested);
2308 mutex_unlock(&g_mutex);
2309 thread_join(&g_thread, NULL);
2310 // Shutdown may precede the next presentation tick. Keep pending errors
2311 // readable after exit without delaying transport teardown or waiting for
2312 // terminal input.
2313 ui_error_popup_preserve(atomic_load(&g_default_fd),
2314 unpack_size(atomic_load(&g_size)));
2315 ui_session_destroy(g_session);
2316 g_session = NULL;
2319 atomic_store_u64(&g_volume_until, 0);
2320 ui_sync_stop();
2321 for (int i = 0; i < UI_SCREEN_COUNT; ++i) {
2322 release_screen(&g_screens[i], i == UI_SCREEN_NOTICE);
2323 }
2324 destroy_media_snapshots();
2325 cond_destroy(&g_render_requested);
2326 cond_destroy(&g_render_done);
2327 mutex_destroy(&g_title_packet_mutex);
2328 mutex_destroy(&g_mutex);
2329 atomic_store_bool(&g_live, false);
2330 atomic_store(&g_background_screen, -1);
2331 atomic_store(&g_screen_mask, 0);
2333 terminal_screen_leave();
2334 // Named entries are reclaimed by the shared debug-registry teardown. Do
2335 // not acquire its writer lock from the cursor-restoration atexit path.
2336 lifecycle_destroy_commit(&g_lifecycle);
2337}
void ui_error_popup_preserve(int fd, terminal_size_t size)
void ui_error_popup_reset(void)
void ui_session_destroy(ui_session_t *s)
void ui_sync_stop(void)
Definition sync.c:365

References atomic_store_bool(), atomic_store_u64(), errno_notifications_enable(), ui_controller_select_interactive(), ui_error_popup_preserve(), ui_error_popup_reset(), UI_SCREEN_COUNT, UI_SCREEN_NOTICE, ui_session_destroy(), and ui_sync_stop().

Referenced by asciichat_shared_destroy(), stats_runtime_stop(), and ui_controller_restore_terminal().

◆ ui_controller_size()

terminal_size_t ui_controller_size ( void )

Definition at line 849 of file tui/controller.c.

849 {
850 if (g_owner)
851 return g_render_size;
852 terminal_size_t size = {.cols = 80, .rows = 24};
853 if (lifecycle_is_initialized(&g_lifecycle)) {
854 size = unpack_size(atomic_load(&g_size));
855 }
856 // A title-only controller has no screen to poll geometry yet.
857 if (!lifecycle_is_initialized(&g_lifecycle) ||
858 atomic_load(&g_screen_mask) == 0) {
859 terminal_size_t detected = {0};
861 int fd = atomic_load(&g_default_fd);
862 if (platform_isatty(fd) && read_terminal_size(fd, &detected) &&
863 detected.cols > 0 && detected.rows > 0)
864 size = detected;
866 }
867 return size;
868}
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
@ ASCIICHAT_ERRNO_DISMISSED
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...

References ASCIICHAT_ERRNO_DISMISSED, errno_scope_begin(), errno_scope_end(), screen::fd, and platform_isatty().

Referenced by key_identity_format_terminal(), keyboard_help_toggle_terminal(), prompt_unknown_host(), terminal_screen_logs_handle_key(), ui_controller_state(), ui_notice_present(), and ui_session_logs_key().

◆ ui_controller_start_terminal()

asciichat_error_t ui_controller_start_terminal ( int fd)

Start terminal interaction without publishing a background screen.

Definition at line 1765 of file tui/controller.c.

1765 {
1766 if (fd < 0)
1767 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid UI terminal descriptor");
1768 atomic_store(&g_default_fd, fd);
1769 asciichat_error_t result = controller_start();
1770 return result == ASCIICHAT_OK ? ui_sync_start(fd) : result;
1771}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
asciichat_error_t ui_sync_start(int fd)
Definition sync.c:335

References ASCIICHAT_OK, ERROR_INVALID_PARAM, SET_ERRNO, and ui_sync_start().

Referenced by stats_runtime_start().

◆ ui_controller_state()

ui_presentation_state_t ui_controller_state ( void )

Definition at line 2101 of file tui/controller.c.

2101 {
2102 ui_presentation_state_t state = {.screen = -1};
2103 if (!lifecycle_is_initialized(&g_lifecycle))
2104 return state;
2106 if (state.screen == UI_SCREEN_ERROR) {
2107 state.covered =
2109 return state;
2110 }
2111 if (state.screen >= 0 && state.screen < UI_SCREEN_COUNT) {
2112 uint64_t published = atomic_load(&g_input_state);
2113 // Sync has an independent mailbox instead of a controller slot. Treat an
2114 // unpublished slot as absent so the session can dispatch queued switches.
2115 if (!(published & (UINT64_C(1) << state.screen)))
2116 state.screen = -1;
2117 else
2118 state.covered = (published & (UINT64_C(1) << (state.screen + 32))) != 0;
2119 }
2120 return state;
2121}
terminal_size_t ui_error_popup_minimum(void)
Definition error_popup.c:84
bool ui_too_small(terminal_size_t actual, terminal_size_t minimum)
Definition too_small.c:10
terminal_size_t ui_controller_size(void)

References ui_presentation_state_t::covered, ui_presentation_state_t::screen, ui_controller_current_screen(), ui_controller_size(), ui_error_popup_minimum(), UI_SCREEN_COUNT, UI_SCREEN_ERROR, and ui_too_small().

Referenced by stats_runtime_start(), and ui_input_read_key().

◆ ui_controller_submit()

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 )

Copies snapshot before returning. Interactive callbacks run on the presentation thread; batch output renders synchronously. Pointer members must remain valid until ui_controller_remove() returns. Callbacks may remove themselves. Callbacks run without the controller mutex. External removal waits for an in-flight callback to finish, so producers must not hold locks needed by that callback while removing its screen.

Definition at line 2004 of file tui/controller.c.

2007 {
2008 return submit_screen(screen, fd, minimum, render, snapshot, bytes, NULL,
2009 true);
2010}

Referenced by keyboard_help_toggle_terminal(), terminal_screen_render(), ui_controller_toggle_logs(), ui_media_loading_present(), ui_notice_present(), ui_notice_present_first_use(), ui_password_prompt_present(), ui_prompt_present_kind(), ui_reconnect_controller_set(), and update_banner_show_prompt().

◆ ui_controller_title_activity()

void ui_controller_title_activity ( ui_title_activity_t activity)

◆ ui_controller_title_discovery()

void ui_controller_title_discovery ( bool has_session,
bool joining )

Definition at line 994 of file tui/controller.c.

994 {
996 : has_session ? UI_TITLE_WAITING_PEERS
998}
void ui_controller_title_activity(ui_title_activity_t activity)
@ UI_TITLE_WAITING_PEERS
@ UI_TITLE_CREATING_SESSION
@ UI_TITLE_JOINING_SESSION

References ui_controller_title_activity(), UI_TITLE_CREATING_SESSION, UI_TITLE_JOINING_SESSION, and UI_TITLE_WAITING_PEERS.

Referenced by splash_set_discovery_session().

◆ ui_controller_title_ready()

void ui_controller_title_ready ( void )

Definition at line 978 of file tui/controller.c.

978 {
979 atomic_store_bool(&g_title_ready, true);
980}

References atomic_store_bool().

Referenced by session_display_write_ascii_frame(), session_server_like_run(), and ui_controller_title_render().

◆ ui_controller_title_render()

void ui_controller_title_render ( ui_title_render_t phase)

Definition at line 981 of file tui/controller.c.

981 {
982 atomic_store_int(&g_title_render, phase);
983 if (phase != UI_TITLE_IDLE)
985}
void ui_controller_title_ready(void)
@ UI_TITLE_IDLE

References atomic_store_int(), ui_controller_title_ready(), and UI_TITLE_IDLE.

Referenced by render_file_create(), render_file_destroy(), and render_file_write_frame().

◆ ui_controller_title_start()

asciichat_error_t ui_controller_title_start ( const char * mode,
int fd )

Start interactive title presentation. Producer events never write to the terminal.

Definition at line 1773 of file tui/controller.c.

1773 {
1774 if (!mode || !platform_isatty(fd) ||
1775 (GET_OPTION(snapshot_mode) && !GET_OPTION(render_file)[0] &&
1776 !GET_OPTION(render_kitty)))
1777 return ASCIICHAT_OK;
1778 if (atomic_load_bool(&g_title_active))
1780 "Terminal title is already initialized");
1781 atomic_store(&g_default_fd, fd);
1782 asciichat_error_t result = controller_start();
1783 if (result != ASCIICHAT_OK)
1784 return result;
1785 g_title_fd = platform_dup(fd);
1786 if (g_title_fd < 0)
1787 return SET_ERRNO_SYS(ERROR_TERMINAL, "Cannot retain title terminal");
1788 g_title_mode = mode;
1789 g_title_started = time_get_ns();
1790 g_last_title[0] = '\0';
1791 g_title_written = false;
1792 g_title_startup_finished = false;
1793 atomic_store_int(&g_title_render, UI_TITLE_IDLE);
1794 atomic_store_int(&g_title_activity, UI_TITLE_ACTIVITY_IDLE);
1795 atomic_store_bool(&g_title_ready, false);
1796 // Publish only after the retained descriptor and immutable configuration are
1797 // ready.
1798 atomic_store_bool(&g_title_active, true);
1799 return ASCIICHAT_OK;
1800}
#define SET_ERRNO_SYS(code,...)
@ ERROR_INVALID_STATE
@ ERROR_TERMINAL
Definition error_codes.h:68
@ UI_TITLE_ACTIVITY_IDLE

References ASCIICHAT_OK, atomic_load_bool(), atomic_store_bool(), atomic_store_int(), ERROR_INVALID_STATE, ERROR_TERMINAL, GET_OPTION, platform_isatty(), SET_ERRNO, SET_ERRNO_SYS, UI_TITLE_ACTIVITY_IDLE, and UI_TITLE_IDLE.

◆ ui_controller_toggle_logs()

void ui_controller_toggle_logs ( void )

Definition at line 2377 of file tui/controller.c.

2377 {
2381 return;
2382 }
2383 if (!platform_isatty(STDOUT_FILENO) || GET_OPTION(snapshot_mode))
2384 return;
2385 const bool snapshot = true;
2387 UI_SCREEN_LOGS, STDOUT_FILENO, (terminal_size_t){.cols = 40, .rows = 6},
2388 render_logs, &snapshot, sizeof(snapshot));
2389 if (result != ASCIICHAT_OK)
2390 log_warn("Cannot show application logs: %d", result);
2391}
void ui_errno_stacks_hide(void)
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
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_hide_logs(void)

References ASCIICHAT_OK, GET_OPTION, log_warn, platform_isatty(), ui_controller_current_screen(), ui_controller_hide_logs(), ui_controller_submit(), ui_errno_stacks_hide(), and UI_SCREEN_LOGS.

◆ ui_controller_trim_utf8_chunk()

size_t ui_controller_trim_utf8_chunk ( const char * data,
size_t length )

Return the largest prefix of a chunk that does not end in a UTF-8 continuation byte.

Definition at line 574 of file tui/controller.c.

574 {
575 while (data && length && ((unsigned char)data[length - 1] & 0xc0) == 0x80)
576 --length;
577 return length;
578}

Referenced by ui_controller_write().

◆ ui_controller_write()

asciichat_error_t ui_controller_write ( int fd,
const char * data,
size_t len )

Final output sink. Live screen writes must originate in a presentation callback. Other terminal text is suppressed while a screen owns the terminal; non-TTY output remains synchronous.

Definition at line 580 of file tui/controller.c.

580 {
581 if (!data || !len)
582 return ASCIICHAT_OK;
583 if (g_capture) {
584 frame_buffer_append(g_capture, data, len);
585 return ASCIICHAT_OK;
586 }
587 // Logging may call this while holding subsystem locks; never wait for a
588 // renderer callback here. Live screens already capture logs in their model.
589 if (!g_owner && ui_controller_is_presenting())
590 return ASCIICHAT_OK;
591 if (g_owner && g_render_fd >= 0)
592 fd = g_render_fd;
593 bool measure_media_write = g_stats_media;
594 bool media_snapshot_stale =
595 g_render_snapshot && g_rendering_screen == UI_SCREEN_MEDIA &&
596 g_render_snapshot->media_epoch != atomic_load(&g_media_epoch);
597 if (media_snapshot_stale)
598 return ASCIICHAT_OK;
599 if (measure_media_write)
601 uint64_t stats_start = g_stats_media ? time_get_ns() : 0;
602 bool help_media_overlay = ui_controller_current_screen() == UI_SCREEN_HELP &&
604 // Terminals supporting synchronized output present the art and its overlays
605 // together, even when a large UTF-8 frame requires several writes.
606 bool synchronized = g_render_snapshot != NULL || help_media_overlay;
607 g_help_sync_open = help_media_overlay;
608 // A frame can legitimately occupy the terminal's last column and row.
609 // Disable DECAWM while presenting it so writing the bottom-right cell does
610 // not trigger an implicit wrap/scroll before the synchronized update ends.
611 static const char begin_media_frame[] = "\033[?2026h\033[?7l";
612 static const char begin_media_write[] = "\033[?7l";
613 bool protect_media_wrap =
614 ui_controller_current_screen() == UI_SCREEN_MEDIA || help_media_overlay;
615 const char *begin_sequence =
616 synchronized ? begin_media_frame : begin_media_write;
617 size_t begin_length = synchronized ? sizeof(begin_media_frame) - 1
618 : sizeof(begin_media_write) - 1;
619 bool started =
620 !protect_media_wrap ||
621 write_output_chunk(fd, begin_sequence, begin_length) == begin_length;
622 uint64_t sync_started_ns = g_stats_media && synchronized ? time_get_ns() : 0;
623 size_t written = 0;
624 bool superseded = false;
625 while (started && written < len) {
626 if (shutdown_is_requested()) {
627 superseded = true;
628 break;
629 }
630 if (g_render_snapshot && g_rendering_screen == UI_SCREEN_MEDIA &&
631 g_render_snapshot->media_epoch != atomic_load(&g_media_epoch)) {
632 superseded = true;
633 break;
634 }
635 // A pending editor or overlay supersedes the remainder of a media frame.
636 // Bound each console write so a large frame cannot delay that transition.
637 if (g_stats_media && ui_controller_current_screen() != UI_SCREEN_MEDIA &&
639 superseded = true;
640 break;
641 }
642 size_t count = len - written;
643 bool row_boundary = false;
644 if (g_stats_media && count > 16384) {
645 count = 16384;
646 size_t boundary = media_chunk_boundary(data + written, count);
647 if (boundary) {
648 count = boundary;
649 row_boundary = true;
650 }
651 count = ui_controller_trim_utf8_chunk(data + written, count);
652 }
653 size_t sent = write_output_chunk(fd, data + written, count);
654 written += sent;
655 if (sent != count)
656 break;
657 if (synchronized && g_stats_media && written < len && row_boundary &&
658 time_get_ns() - sync_started_ns >= 40 * NS_PER_MS_INT) {
659 // Give the terminal a complete set of rows before its synchronized
660 // output watchdog can expose a partly written row.
661 if (write_output_chunk(fd, "\033[?2026l\033[?2026h", 16) != 16) {
662 started = false;
663 break;
664 }
665 sync_started_ns = time_get_ns();
666 }
667 }
668 // When help covers media, keep the FPS overlay inside the same synchronized
669 // update as the menu and backdrop. Writing it afterward causes a visible
670 // one-frame gap while the terminal presents the help screen.
671 bool include_media_overlay = g_stats_media || help_media_overlay;
672 if (started && include_media_overlay && GET_OPTION(fps_counter)) {
673 char overlay[128];
674 size_t overlay_length = fps_counter_format(g_fps, overlay, sizeof(overlay),
675 g_render_size.cols, true);
676 if (overlay_length) {
677 if (write_output_chunk(fd, overlay, overlay_length) == overlay_length)
678 g_media_overlay_written = true;
679 else
680 started = false;
681 }
682 }
683 // End the update even when input superseded the frame halfway through.
684 static const char end_with_query[] =
685 "\033[1G\033[?7h\033[?2026l\033]11;?\033\\";
686 bool probe = synchronized && g_probe_frame && written == len && started;
687 if (probe)
688 g_probe_requested_ns = time_get_ns();
689 static const char end_frame[] = "\033[1G\033[?7h\033[?2026l";
690 static const char end_media[] = "\033[1G\033[?7h";
691 const char *end_sequence = probe ? end_with_query
692 : synchronized ? end_frame
693 : end_media;
694 size_t end_length = probe ? sizeof(end_with_query) - 1
695 : synchronized ? sizeof(end_frame) - 1
696 : sizeof(end_media) - 1;
697 bool ended = !protect_media_wrap ||
698 write_output_chunk(fd, end_sequence, end_length) == end_length;
699 if (superseded && ended) {
700 // A prompt can take ownership while a large media frame is being sent.
701 // The frame is deliberately allowed to stop at a row boundary, but its
702 // already-presented rows must not remain underneath the prompt.
703 static const char clear_aborted_media[] = "\033[r\033[0m\033[2J\033[H";
704 ended = write_output_chunk(fd, clear_aborted_media,
705 sizeof(clear_aborted_media) - 1) ==
706 sizeof(clear_aborted_media) - 1;
707 }
708 if (!ended)
709 g_probe_requested_ns = 0;
710 // Switching screens intentionally cancels the remainder of an old media
711 // frame.
712 bool complete = (written == len || superseded) && started && ended;
713 if (g_stats_media) {
714 g_stats_write_ns += time_get_ns() - stats_start;
715 g_stats_writes++;
716 g_stats_failed |= !complete;
717 }
718 if (measure_media_write)
719 fps_counter_write_end(g_fps, complete);
720 return complete ? ASCIICHAT_OK
722 "Incomplete terminal frame write (%zu/%zu bytes)",
723 written, len);
724}
void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len)
Append raw bytes to the buffer.
@ ERROR_FILE_OPERATION
void fps_counter_write_begin(fps_counter_t *counter)
size_t fps_counter_format(fps_counter_t *counter, char *buffer, size_t capacity, int columns, bool redrawn)
void fps_counter_write_end(fps_counter_t *counter, bool complete)
bool shutdown_is_requested(void)
Check if shutdown has been requested.
Definition common.c:87
bool ui_controller_is_presenting(void)
size_t ui_controller_trim_utf8_chunk(const char *data, size_t length)
@ UI_SCREEN_MEDIA

References ASCIICHAT_OK, ERROR_FILE_OPERATION, screen::fd, fps_counter_format(), fps_counter_write_begin(), fps_counter_write_end(), frame_buffer_append(), GET_OPTION, keyboard_help_parent_screen(), NS_PER_MS_INT, SET_ERRNO, shutdown_is_requested(), ui_controller_current_screen(), ui_controller_is_presenting(), ui_controller_trim_utf8_chunk(), UI_SCREEN_HELP, and UI_SCREEN_MEDIA.

Referenced by ascii_write(), ascii_write_destroy(), ascii_write_init(), log_console_impl(), log_search_render_input_line(), session_display_write_ascii_frame(), session_display_write_raw(), tui_stats_render(), ui_controller_printf(), ui_notice_present(), ui_prompt_dismiss(), and ui_sync_render().