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
Go to the documentation of this file.
1#include <ascii-chat/otel.h>
16
24#include <libplatform/system.h>
25#include <libplatform/compiler.h>
26#include <libplatform/system.h>
27#include <libplatform/terminal.h>
28#include <libplatform/keyboard.h>
29#include <libplatform/mutex.h>
30#include "ascii-chat/log/grep.h"
31#include "ascii-chat/log/log.h"
32#include <options/options.h>
33#include "ascii-chat/common.h"
34#include <libplatform/atomic.h>
36#include <stdio.h>
37#include <string.h>
38#include <stdbool.h>
39#include <errno.h>
40#include <limits.h>
41
42// Module-level static frame buffer (reused across renders to avoid malloc per frame)
43static frame_buffer_t *g_frame_buf = NULL;
44
45// Cleanup never waits for terminal I/O. An active renderer retains ownership
46// until it returns, then services any deferred cleanup request.
48static atomic_t g_screen_state = {0};
49
50static void terminal_screen_free_buffer(void) {
51 frame_buffer_destroy(g_frame_buf);
52 g_frame_buf = NULL;
53}
54
55// Shared process history, with an owned fallback for standalone screen users.
56static session_log_buffer_t *g_session_log_buffer = NULL;
57static bool g_owns_log_buffer;
58
59/* Offset is expressed in rendered terminal rows from the live tail. */
60static size_t g_log_scroll_rows;
61static bool g_log_help;
62static bool g_log_help_too_small;
63static uint64_t g_log_last_tail_sequence;
64static mutex_t g_log_viewer_mutex;
66static atomic_t g_log_viewer_mutex_state = {0};
67
68static bool terminal_screen_logs_mutex_init(void) {
69 for (;;) {
70 int state = atomic_load_int(&g_log_viewer_mutex_state);
71 if (state == LOG_VIEWER_MUTEX_READY)
72 return true;
73 if (state == LOG_VIEWER_MUTEX_UNINITIALIZED) {
74 int expected = LOG_VIEWER_MUTEX_UNINITIALIZED;
75 if (!atomic_cas_int(&g_log_viewer_mutex_state, &expected, LOG_VIEWER_MUTEX_INITIALIZING))
76 continue;
77 if (mutex_init(&g_log_viewer_mutex, "log_viewer") != 0) {
78 atomic_store_int(&g_log_viewer_mutex_state, LOG_VIEWER_MUTEX_UNINITIALIZED);
79 return false;
80 }
81 atomic_store_int(&g_log_viewer_mutex_state, LOG_VIEWER_MUTEX_READY);
82 return true;
83 }
84 platform_sleep_ms(1);
85 }
86}
87
88static void terminal_screen_logs_reset_locked(void) {
89 g_log_scroll_rows = 0;
90 g_log_help = false;
91 g_log_help_too_small = false;
92 g_log_last_tail_sequence = 0;
93}
94
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}
104
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}
113
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}
123
124size_t terminal_screen_logs_retain_position(uint64_t tail_sequence, size_t appended_rows, size_t maximum_offset) {
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}
141
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}
150
151static size_t terminal_screen_log_step_locked(int width) {
152 if (!g_session_log_buffer)
153 return 1;
155 if (!entries)
156 return 1;
157 size_t count = session_log_buffer_get_recent(g_session_log_buffer, entries, SESSION_LOG_BUFFER_SIZE);
158 size_t offset = g_log_scroll_rows;
159 size_t step = 1;
160 for (size_t index = count; index; --index) {
161 int height = display_height(entries[index - 1].message, width);
162 size_t rows = (size_t)(height > 0 ? height : 1);
163 if (offset < rows) {
164 step = rows;
165 break;
166 }
167 offset -= rows;
168 }
169 SAFE_FREE(entries);
170 return step;
171}
172
173bool terminal_screen_logs_handle_key_sized(int key, terminal_size_t size) {
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}
249
253
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}
262
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}
271
272static void terminal_screen_render_impl(const terminal_screen_config_t *config) {
273 if (!config || !config->render_header)
274 return;
275 terminal_size_t size = ui_controller_size();
276 if (!g_frame_buf)
277 g_frame_buf = frame_buffer_create(size.rows, size.cols);
278 if (!g_frame_buf)
279 return;
280 frame_buffer_reset(g_frame_buf);
281 terminal_screen_compose(g_frame_buf, size, config, g_session_log_buffer);
283}
284
285static void terminal_screen_render_owned(const terminal_screen_config_t *config) {
286 int expected = SCREEN_IDLE;
287 if (!atomic_cas_int(&g_screen_state, &expected, SCREEN_RENDERING))
288 return;
289
290 terminal_screen_render_impl(config);
291
292 expected = SCREEN_RENDERING;
293 if (!atomic_cas_int(&g_screen_state, &expected, SCREEN_IDLE)) {
294 // Cleanup requested ownership while we were rendering or blocked in write.
295 // Keep other renderers out until the buffer is released.
296 terminal_screen_free_buffer();
297 atomic_store_int(&g_screen_state, SCREEN_IDLE);
298 }
299}
300
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}
316
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}
332
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}
349
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}
356
358 return g_session_log_buffer;
359}
360
364
365typedef struct {
367 unsigned char header[];
369static void render_screen_snapshot(terminal_size_t size, const void *data) {
370 (void)size;
371 const screen_snapshot_t *snapshot = data;
372 terminal_screen_config_t config = snapshot->config;
373 config.user_data = (void *)snapshot->header;
374 terminal_screen_render_owned(&config);
375}
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}
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.
Named object registry for debugging — log identifiable resource names.
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
void frame_buffer_set_screen_output_fd(int fd)
Set output file descriptor for terminal screen (splash/status) rendering.
void frame_buffer_reset(frame_buffer_t *buf)
Clear buffer contents while keeping allocation.
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.
int frame_buffer_get_screen_output_fd(void)
Get current output file descriptor for terminal screens.
Buffered terminal frame rendering.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned long long uint64_t
#define NAMED_REGISTER_ATOMIC(a, name, parent_ptr)
Register an atomic_t with automatic format specifier.
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.
struct session_log_buffer session_log_buffer_t
Thread-safe fixed-size history ring for session-screen log lines.
int display_height(const char *text, int terminal_width)
Calculate how many display lines text will consume when wrapped.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
🖥️ Display Width and Terminal Centering Utilities
session_log_buffer_t * log_get_history_buffer(void)
📝 Logging API with multiple log levels and terminal output control
⚙️ 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
static bool size_t index
Definition queries.h:8
asciichat_error_t log_search_init(void)
Initialize interactive grep subsystem.
Definition search.c:144
bool log_search_is_entering(void)
Check if currently in input mode (typing pattern).
Definition search.c:408
Interactive search filtering for terminal screens.
session_log_buffer_t * session_log_buffer_create(void)
Create a new session log buffer instance.
void session_log_buffer_clear(session_log_buffer_t *buf)
Clear all log entries from buffer.
size_t session_log_buffer_get_recent(session_log_buffer_t *buf, session_log_entry_t *out_entries, size_t max_count)
Get recent log entries from buffer.
void session_log_buffer_destroy(session_log_buffer_t *buf)
Cleanup session log buffer instance.
Thread-safe circular log buffer for session screens (status, splash).
#define SESSION_LOG_BUFFER_SIZE
void terminal_screen_compose(frame_buffer_t *output, terminal_size_t size, const terminal_screen_config_t *config, session_log_buffer_t *logs)
🔤 String Manipulation and Shell Escaping Utilities
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
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[]
One formatted session log line and its monotonic history sequence.
Configuration for terminal screen rendering.
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.
size_t terminal_screen_logs_offset(void)
void terminal_screen_logs_set_offset(size_t rows)
@ LOG_VIEWER_MUTEX_READY
@ LOG_VIEWER_MUTEX_DESTROYING
@ LOG_VIEWER_MUTEX_UNINITIALIZED
@ LOG_VIEWER_MUTEX_INITIALIZING
bool terminal_screen_logs_help_open(void)
void terminal_screen_log_destroy(void)
Standard log cleanup for terminal screens.
@ SCREEN_CLEANING
@ SCREEN_CLEANUP_PENDING
@ SCREEN_IDLE
@ SCREEN_RENDERING
size_t terminal_screen_logs_retain_position(uint64_t tail_sequence, size_t appended_rows, size_t maximum_offset)
bool terminal_screen_logs_handle_key_sized(int key, terminal_size_t size)
session_log_buffer_t * terminal_screen_get_log_buffer(void)
Get the current session log buffer.
void terminal_screen_render(const terminal_screen_config_t *config)
Render a terminal screen with fixed header and scrolling logs.
bool terminal_screen_logs_handle_key(int key)
void terminal_screen_cleanup(void)
Clean up terminal screen resources.
uint64_t terminal_screen_logs_last_tail_sequence(void)
void terminal_screen_logs_reset(void)
void terminal_screen_log_clear(void)
Clear buffered logs for terminal screens.
bool terminal_screen_logs_help_too_small(void)
session_log_buffer_t * terminal_screen_log_init(void)
Initialize session log buffer for terminal screens.
void terminal_screen_set_output_fd(int fd)
Set output file descriptor for terminal screens (splash/status).
Reusable "fixed header + scrolling logs" terminal screen abstraction.
⏱️ 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)
terminal_size_t ui_controller_size(void)
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
@ UI_SCREEN_SPLASH
@ UI_SCREEN_STATUS