ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
terminal_screen.c File Reference

Reusable "fixed header + scrolling logs" terminal screen abstraction. More...

Go to the source code of this file.

Data Structures

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...

Enumerations

enum  { SCREEN_IDLE , SCREEN_RENDERING , SCREEN_CLEANUP_PENDING , SCREEN_CLEANING }
enum  { LOG_VIEWER_MUTEX_UNINITIALIZED , LOG_VIEWER_MUTEX_INITIALIZING , LOG_VIEWER_MUTEX_READY , LOG_VIEWER_MUTEX_DESTROYING }

Functions

void terminal_screen_logs_reset (void)
size_t terminal_screen_logs_offset (void)
void terminal_screen_logs_set_offset (size_t rows)
size_t terminal_screen_logs_retain_position (uint64_t tail_sequence, size_t appended_rows, size_t maximum_offset)
uint64_t terminal_screen_logs_last_tail_sequence (void)
bool terminal_screen_logs_handle_key_sized (int key, terminal_size_t size)
bool terminal_screen_logs_handle_key (int key)
bool terminal_screen_logs_help_open (void)
bool terminal_screen_logs_help_too_small (void)
void terminal_screen_cleanup (void)
 Clean up terminal screen resources.
session_log_buffer_t * terminal_screen_log_init (void)
 Initialize session log buffer for terminal screens.
void terminal_screen_log_destroy (void)
 Standard log cleanup for terminal screens.
void terminal_screen_log_clear (void)
 Clear buffered logs for terminal screens.
session_log_buffer_t * terminal_screen_get_log_buffer (void)
 Get the current session log buffer.
void terminal_screen_set_output_fd (int fd)
 Set output file descriptor for terminal screens (splash/status).
void terminal_screen_render (const terminal_screen_config_t *config)
 Render a terminal screen with fixed header and scrolling logs.

Detailed Description

Reusable "fixed header + scrolling logs" terminal screen abstraction.

This module extracts the common rendering logic from splash.c and server_status.c into a unified, testable abstraction.

Design:

  • Callback-based architecture for header content generation
  • Automatic log scrolling with line wrapping
  • Thread-safe log buffer management
  • Terminal resize handling
  • Color scheme integration

Definition in file terminal_screen.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
LOG_VIEWER_MUTEX_UNINITIALIZED 
LOG_VIEWER_MUTEX_INITIALIZING 
LOG_VIEWER_MUTEX_READY 
LOG_VIEWER_MUTEX_DESTROYING 

Definition at line 65 of file terminal_screen.c.

◆ anonymous enum

anonymous enum
Enumerator
SCREEN_IDLE 
SCREEN_RENDERING 
SCREEN_CLEANUP_PENDING 
SCREEN_CLEANING 

Definition at line 47 of file terminal_screen.c.

Function Documentation

◆ terminal_screen_cleanup()

void terminal_screen_cleanup ( void )

Clean up terminal screen resources.

Destroys the internal static frame buffer allocated by terminal_screen_render(). Call at program exit or when terminal screen rendering is no longer needed. If a render is active, destruction is deferred until that render returns; cleanup never waits for blocked terminal output.

Definition at line 301 of file terminal_screen.c.

301 {
302 for (;;) {
303 int state = atomic_load_int(&g_screen_state);
304 if (state == SCREEN_CLEANUP_PENDING || state == SCREEN_CLEANING)
305 return;
307 if (!atomic_cas_int(&g_screen_state, &state, next))
308 continue;
309 if (next == SCREEN_CLEANING) {
310 terminal_screen_free_buffer();
311 atomic_store_int(&g_screen_state, SCREEN_IDLE);
312 }
313 return;
314 }
315}
void atomic_store_int(atomic_t *a, int value)
Atomically store an int value.
bool atomic_cas_int(atomic_t *a, int *expected, int new_value)
Atomically compare-and-swap an int.
int atomic_load_int(atomic_t *a)
Atomically load an int value.

References atomic_cas_int(), atomic_load_int(), atomic_store_int(), SCREEN_CLEANING, SCREEN_CLEANUP_PENDING, SCREEN_IDLE, and SCREEN_RENDERING.

Referenced by asciichat_shared_destroy().

◆ terminal_screen_get_log_buffer()

session_log_buffer_t * terminal_screen_get_log_buffer ( void )

Get the current session log buffer.

Returns the log buffer owned by this module, or NULL if not initialized. Used by splash/status screens to register with the logger.

Returns
Pointer to log buffer, or NULL

Definition at line 357 of file terminal_screen.c.

357 {
358 return g_session_log_buffer;
359}

◆ terminal_screen_log_clear()

void terminal_screen_log_clear ( void )

Clear buffered logs for terminal screens.

Clears all previously captured log entries from the buffer. Useful when transitioning between screens to start fresh.

Definition at line 350 of file terminal_screen.c.

350 {
351 // Process history survives splash/status transitions.
352 if (g_session_log_buffer && g_owns_log_buffer) {
353 session_log_buffer_clear(g_session_log_buffer);
354 }
355}
void session_log_buffer_clear(session_log_buffer_t *buf)
Clear all log entries from buffer.

References session_log_buffer_clear().

Referenced by splash_log_clear(), ui_mdns_log_clear(), and ui_status_log_clear().

◆ terminal_screen_log_destroy()

void terminal_screen_log_destroy ( void )

Standard log cleanup for terminal screens.

Destroys the session log buffer after screens are done rendering.

Definition at line 333 of file terminal_screen.c.

333 {
336 NAMED_UNREGISTER(&g_screen_state);
337 if (g_session_log_buffer) {
338 if (g_owns_log_buffer)
339 session_log_buffer_destroy(g_session_log_buffer);
340 g_session_log_buffer = NULL;
341 }
343 int expected = LOG_VIEWER_MUTEX_READY;
344 if (atomic_cas_int(&g_log_viewer_mutex_state, &expected, LOG_VIEWER_MUTEX_DESTROYING)) {
345 mutex_destroy(&g_log_viewer_mutex);
346 atomic_store_int(&g_log_viewer_mutex_state, LOG_VIEWER_MUTEX_UNINITIALIZED);
347 }
348}
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
void session_log_buffer_destroy(session_log_buffer_t *buf)
Cleanup session log buffer instance.
void terminal_screen_logs_reset(void)
void ui_controller_remove(ui_screen_t screen)
@ UI_SCREEN_SPLASH
@ UI_SCREEN_STATUS

References atomic_cas_int(), atomic_store_int(), LOG_VIEWER_MUTEX_DESTROYING, LOG_VIEWER_MUTEX_READY, LOG_VIEWER_MUTEX_UNINITIALIZED, NAMED_UNREGISTER, session_log_buffer_destroy(), terminal_screen_logs_reset(), ui_controller_remove(), UI_SCREEN_SPLASH, and UI_SCREEN_STATUS.

Referenced by splash_log_destroy(), ui_mdns_log_destroy(), and ui_status_log_destroy().

◆ terminal_screen_log_init()

session_log_buffer_t * terminal_screen_log_init ( void )

Initialize session log buffer for terminal screens.

Creates a new session log buffer owned by this module. Splash and status screens use this buffer to display logs.

Returns
Pointer to created buffer, or NULL on failure

Definition at line 317 of file terminal_screen.c.

317 {
319 if (!terminal_screen_logs_mutex_init())
320 return NULL;
321 if (g_session_log_buffer) {
322 return g_session_log_buffer; // Already initialized
323 }
324
325 NAMED_REGISTER_ATOMIC(&g_screen_state, "terminal_screen_state", NULL);
326 g_session_log_buffer = log_get_history_buffer();
327 g_owns_log_buffer = !g_session_log_buffer;
328 if (g_owns_log_buffer)
329 g_session_log_buffer = session_log_buffer_create();
330 return g_session_log_buffer;
331}
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.
session_log_buffer_t * log_get_history_buffer(void)
asciichat_error_t log_search_init(void)
Initialize interactive grep subsystem.
Definition search.c:144
session_log_buffer_t * session_log_buffer_create(void)
Create a new session log buffer instance.

References log_get_history_buffer(), log_search_init(), NAMED_REGISTER_ATOMIC, and session_log_buffer_create().

Referenced by splash_log_init(), ui_mdns_log_init(), and ui_status_log_init().

◆ terminal_screen_logs_handle_key()

bool terminal_screen_logs_handle_key ( int key)

Handle a key while the live log viewer owns the terminal.

Definition at line 250 of file terminal_screen.c.

250 {
252}
bool terminal_screen_logs_handle_key_sized(int key, terminal_size_t size)
terminal_size_t ui_controller_size(void)

References terminal_screen_logs_handle_key_sized(), and ui_controller_size().

Referenced by ui_input_read_key().

◆ terminal_screen_logs_handle_key_sized()

bool terminal_screen_logs_handle_key_sized ( int key,
terminal_size_t size )

Browser/embedded sessions supply their own viewport instead of a native controller.

Definition at line 173 of file terminal_screen.c.

173 {
174 if (!terminal_screen_logs_mutex_init())
175 return false;
176 bool redraw = false;
177 mutex_lock(&g_log_viewer_mutex);
178 if (g_log_help) {
179 if (key == KEY_ESCAPE || key == '?') {
180 g_log_help = false;
181 redraw = true;
182 }
183 mutex_unlock(&g_log_viewer_mutex);
184 if (redraw)
186 return true;
187 }
188 if (key == '?') {
189 g_log_help_too_small = size.rows < 12 || size.cols < 8;
190 g_log_help = !g_log_help_too_small;
191 redraw = true;
192 mutex_unlock(&g_log_viewer_mutex);
194 return true;
195 }
196 int direction = 0;
197 if (key == KEY_UP || key == 'k')
198 direction = 1;
199 else if (key == KEY_DOWN || key == 'j')
200 direction = -1;
201 else if (key == KEY_PAGEUP)
202 direction = 1000000;
203 else if (key == KEY_PAGEDOWN)
204 direction = -1000000;
205 else if (key == KEY_HOME) {
206 g_log_scroll_rows = SIZE_MAX;
207 g_log_help_too_small = false;
208 mutex_unlock(&g_log_viewer_mutex);
210 return true;
211 } else if (key == KEY_END) {
212 g_log_scroll_rows = 0;
213 g_log_help_too_small = false;
214 mutex_unlock(&g_log_viewer_mutex);
216 return true;
217 } else if (key == KEY_LEFT || key == 21)
218 direction = 1000000;
219 else if (key == KEY_RIGHT || key == 4)
220 direction = -1000000;
221 else {
222 g_log_help_too_small = false;
223 mutex_unlock(&g_log_viewer_mutex);
224 return false;
225 }
226
227 ascii_otel_ui_usage("logs", direction > 0 ? "scroll_older" : "scroll_newer", 0);
228 size_t step = 1;
229 if (direction == 1 || direction == -1) {
230 /* Arrow keys move by one complete log entry, including wrapped rows. */
231 step = terminal_screen_log_step_locked(size.cols);
232 } else {
233 int rows = size.rows;
234 step = (size_t)(rows > 6 ? (rows - 5) / 2 : 1);
235 }
236 if (direction > 0) {
237 if (SIZE_MAX - g_log_scroll_rows < step)
238 g_log_scroll_rows = SIZE_MAX;
239 else
240 g_log_scroll_rows += step;
241 } else {
242 g_log_scroll_rows = g_log_scroll_rows > step ? g_log_scroll_rows - step : 0;
243 }
244 g_log_help_too_small = false;
245 mutex_unlock(&g_log_viewer_mutex);
247 return true;
248}
void ascii_otel_ui_usage(const char *screen, const char *event, uint64_t duration_ns)
Definition otel.c:411
void ui_controller_redraw(void)

References ascii_otel_ui_usage(), and ui_controller_redraw().

Referenced by terminal_screen_logs_handle_key().

◆ terminal_screen_logs_help_open()

bool terminal_screen_logs_help_open ( void )

True while the log-viewer's own shortcut dialog is displayed.

Definition at line 254 of file terminal_screen.c.

254 {
255 if (!terminal_screen_logs_mutex_init())
256 return false;
257 mutex_lock(&g_log_viewer_mutex);
258 bool help_open = g_log_help;
259 mutex_unlock(&g_log_viewer_mutex);
260 return help_open;
261}

Referenced by terminal_screen_compose(), and ui_input_read_key().

◆ terminal_screen_logs_help_too_small()

bool terminal_screen_logs_help_too_small ( void )

True when help was requested but the current terminal cannot display it.

Definition at line 263 of file terminal_screen.c.

263 {
264 if (!terminal_screen_logs_mutex_init())
265 return false;
266 mutex_lock(&g_log_viewer_mutex);
267 bool too_small = g_log_help_too_small;
268 mutex_unlock(&g_log_viewer_mutex);
269 return too_small;
270}

Referenced by terminal_screen_compose().

◆ terminal_screen_logs_last_tail_sequence()

uint64_t terminal_screen_logs_last_tail_sequence ( void )

Sequence number at the live tail from the previous composition.

Definition at line 142 of file terminal_screen.c.

142 {
143 if (!terminal_screen_logs_mutex_init())
144 return 0;
145 mutex_lock(&g_log_viewer_mutex);
146 uint64_t sequence = g_log_last_tail_sequence;
147 mutex_unlock(&g_log_viewer_mutex);
148 return sequence;
149}
unsigned long long uint64_t

Referenced by terminal_screen_compose().

◆ terminal_screen_logs_offset()

size_t terminal_screen_logs_offset ( void )

Number of rendered rows currently held above the live tail.

Definition at line 105 of file terminal_screen.c.

105 {
106 if (!terminal_screen_logs_mutex_init())
107 return g_log_scroll_rows;
108 mutex_lock(&g_log_viewer_mutex);
109 size_t offset = g_log_scroll_rows;
110 mutex_unlock(&g_log_viewer_mutex);
111 return offset;
112}

Referenced by ui_session_logs_render().

◆ terminal_screen_logs_reset()

void terminal_screen_logs_reset ( void )

Reset the retained log-viewer position when the viewer is closed.

Definition at line 95 of file terminal_screen.c.

95 {
96 if (!terminal_screen_logs_mutex_init()) {
97 terminal_screen_logs_reset_locked();
98 return;
99 }
100 mutex_lock(&g_log_viewer_mutex);
101 terminal_screen_logs_reset_locked();
102 mutex_unlock(&g_log_viewer_mutex);
103}

Referenced by terminal_screen_log_destroy(), and ui_controller_hide_logs().

◆ terminal_screen_logs_retain_position()

size_t terminal_screen_logs_retain_position ( uint64_t tail_sequence,
size_t appended_rows,
size_t maximum_offset )

Retain the selected history row while new entries arrive, then clamp it.

Definition at line 124 of file terminal_screen.c.

124 {
125 if (!terminal_screen_logs_mutex_init())
126 return 0;
127 mutex_lock(&g_log_viewer_mutex);
128 if (g_log_scroll_rows && g_log_last_tail_sequence && tail_sequence > g_log_last_tail_sequence) {
129 if (SIZE_MAX - g_log_scroll_rows < appended_rows)
130 g_log_scroll_rows = SIZE_MAX;
131 else
132 g_log_scroll_rows += appended_rows;
133 }
134 g_log_last_tail_sequence = tail_sequence;
135 if (g_log_scroll_rows > maximum_offset)
136 g_log_scroll_rows = maximum_offset;
137 size_t offset = g_log_scroll_rows;
138 mutex_unlock(&g_log_viewer_mutex);
139 return offset;
140}

Referenced by terminal_screen_compose().

◆ terminal_screen_logs_set_offset()

void terminal_screen_logs_set_offset ( size_t rows)

Clamp the retained position after the available history changes.

Definition at line 114 of file terminal_screen.c.

114 {
115 if (!terminal_screen_logs_mutex_init()) {
116 g_log_scroll_rows = rows;
117 return;
118 }
119 mutex_lock(&g_log_viewer_mutex);
120 g_log_scroll_rows = rows;
121 mutex_unlock(&g_log_viewer_mutex);
122}

◆ terminal_screen_render()

void terminal_screen_render ( const terminal_screen_config_t * config)

Render a terminal screen with fixed header and scrolling logs.

Renders a screen following the pattern:

  1. Clear screen and move cursor to home (both stdout and stderr)
  2. Call render_header() callback to draw fixed header
  3. Calculate log area: rows - fixed_header_lines - 1 (prevent scroll)
  4. Fetch recent logs from session_log_buffer
  5. Calculate which logs fit (working backwards, accounting for wrapping)
  6. Display logs chronologically (oldest to newest, latest at bottom)
  7. Fill remaining lines to prevent terminal scroll
  8. Flush stdout
Parameters
configScreen configuration (header callback, line count, etc.)

Copies the header data and submits it to the UI controller. The controller owns size checks, terminal writes, and resize refreshes.

Definition at line 376 of file terminal_screen.c.

376 {
377 if (!config || !config->render_header || !config->user_data || !config->user_data_size)
378 return;
379 size_t bytes = sizeof(screen_snapshot_t) + config->user_data_size;
380 screen_snapshot_t *snapshot = SAFE_MALLOC(bytes, screen_snapshot_t *);
381 if (!snapshot)
382 return;
383 snapshot->config = *config;
384 memcpy(snapshot->header, config->user_data, config->user_data_size);
385 terminal_size_t minimum = {.cols = config->minimum_cols, .rows = config->fixed_header_lines + (log_search_is_entering() ? 4 : 2)};
386 (void)ui_controller_submit(config->screen, config->output_fd, minimum, render_screen_snapshot, snapshot, bytes);
387 SAFE_FREE(snapshot);
388}
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
bool log_search_is_entering(void)
Check if currently in input mode (typing pattern).
Definition search.c:408
When code needs to work with media snapshot slot, it keeps busy, snapshot, capacity here instead of s...
terminal_screen_config_t config
unsigned char header[]
terminal_screen_header_fn render_header
Callback to draw header content.
int fixed_header_lines
How many lines the header takes (e.g., 4 for status, 8 for splash).
void * user_data
Passed to render_header callback.
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)

References screen_snapshot_t::config, terminal_screen_config_t::fixed_header_lines, screen_snapshot_t::header, log_search_is_entering(), terminal_screen_config_t::minimum_cols, terminal_screen_config_t::output_fd, terminal_screen_config_t::render_header, SAFE_FREE, SAFE_MALLOC, terminal_screen_config_t::screen, ui_controller_submit(), terminal_screen_config_t::user_data, and terminal_screen_config_t::user_data_size.

Referenced by ui_status_display(), and ui_status_display_interactive().

◆ terminal_screen_set_output_fd()

void terminal_screen_set_output_fd ( int fd)

Set output file descriptor for terminal screens (splash/status).

Parameters
fdFile descriptor (STDOUT_FILENO for TTY, STDERR_FILENO for piped)

Configures where splash and status screens write their output. Should be called before terminal_screen_render() to control output destination.

Definition at line 361 of file terminal_screen.c.

361 {
363}
void frame_buffer_set_screen_output_fd(int fd)
Set output file descriptor for terminal screen (splash/status) rendering.

References frame_buffer_set_screen_output_fd().

Referenced by splash_intro_start(), and ui_status_display().