ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/tui/session.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define TUI_SESSION_DETAIL_BUFFER_SIZE 256
4#define TUI_SESSION_OFFSET_TEXT_BUFFER_SIZE 32
5
6/* Existing buffer budgets in bytes, including NUL for text storage. */
7#define PRESENTATION_TIME_HISTORY_COUNT 120
8
9#include <ascii-chat/common.h>
10#include <ascii-chat/log/log.h>
19#include <libplatform/terminal.h>
20#include <options/options.h>
21#include <stdio.h>
22#include <string.h>
23
54
56 ui_session_t *s = SAFE_CALLOC(1, sizeof(*s), ui_session_t *);
57 if (!s)
58 return NULL;
59 s->frame = frame_buffer_create(24, 80);
60 s->media = frame_buffer_create(24, 80);
62 s->owns_logs = !s->logs;
63 if (s->owns_logs)
65 if (!s->frame || !s->media || !s->logs) {
67 return NULL;
68 }
69 s->started = time(NULL);
71 s->size = (terminal_size_t){.cols = 80, .rows = 24};
72 s->active = -1;
73 s->saved_volume = 1;
74 s->dirty = true;
75 s->splash.use_colors = true;
76 animation_init(&s->animation, "session_splash",
77 (animation_config_t){.type = ANIMATION_SPLASH_RAINBOW,
78 .fps = 60,
79 .speed = 1,
80 .hidden_policy = ANIMATION_HIDDEN_PAUSE});
81 return s;
82}
83
96
97void ui_session_resize(ui_session_t *s, int cols, int rows) {
98 if (!s || cols < 1 || rows < 1 || cols > 4096 || rows > 4096) {
100 "Invalid TUI session or terminal dimensions");
101 return;
102 }
103 if (cols != s->size.cols || rows != s->size.rows) {
104 s->size = (terminal_size_t){.cols = cols, .rows = rows};
105 s->dirty = true;
106 }
107}
108
109void ui_session_loading(ui_session_t *s, bool loading, const char *message) {
110 if (!s)
111 return;
112 if (loading != s->loading) {
113 s->loading = loading;
115 s->dirty = true;
116 }
117 if (message && strcmp(message, s->splash.connection_target)) {
119 sizeof(s->splash.connection_target));
120 s->dirty = true;
121 }
122}
123
124void ui_session_reconnecting(ui_session_t *s, bool reconnecting,
125 const ui_reconnect_view_t *view) {
126 if (!s)
127 return;
128 s->reconnecting = reconnecting;
129 if (view) {
130 SAFE_STRNCPY(s->reconnect_target, view->target ? view->target : "server",
131 sizeof(s->reconnect_target));
133 view->transport ? view->transport : "LOST",
134 sizeof(s->reconnect_transport));
136 view->encryption ? view->encryption : "WAITING",
137 sizeof(s->reconnect_encryption));
139 view->handshake ? view->handshake : "WAITING",
140 sizeof(s->reconnect_handshake));
142 view->protocol ? view->protocol : "WAITING",
143 sizeof(s->reconnect_protocol));
145 view->last_error ? view->last_error : "connection closed",
146 sizeof(s->reconnect_error));
147 s->reconnect = *view;
154 }
155 s->dirty = true;
156}
157
158void ui_session_log(ui_session_t *s, const char *message) {
159 if (s && message) {
160 session_log_buffer_append(s->logs, message);
161 s->dirty = true;
162 }
163}
164
166 size_t length) {
167 if (!s || !data || length > UI_CONTROLLER_MAX_FRAME_BYTES)
168 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid session media frame");
170 frame_buffer_append(s->media, data, length);
171 s->dirty = true;
172 return ASCIICHAT_OK;
173}
174
175static bool ui_session_logs_key_sized(keyboard_key_t key, terminal_size_t size);
176
178 if (!s)
179 return UI_ACTION_NONE;
180 if (s->show_logs && ui_session_logs_key_sized(key, s->size)) {
181 s->dirty = true;
182 return UI_ACTION_LOGS;
183 }
184 bool retry = s->active == UI_SCREEN_RECONNECT && s->reconnect.can_retry &&
185 (key == 'r' || key == 'R');
187 retry ? UI_ACTION_RETRY : ui_session_key(key, &s->saved_volume);
188 if (action == UI_ACTION_HELP) {
189 s->help = !s->help;
190 s->show_logs = false;
191 } else if (action == UI_ACTION_LOGS) {
192 s->show_logs = !s->show_logs;
193 s->help = false;
194 } else if (action == UI_ACTION_CLOSE && s->show_logs) {
195 s->show_logs = false;
197 } else if (action == UI_ACTION_CLOSE && s->help) {
198 s->help = false;
200 }
201 s->dirty |= action != UI_ACTION_NONE;
202 return action;
203}
204
205int ui_session_select_screen(uint32_t visible_screens) {
206 for (int screen = UI_SCREEN_COUNT - 1; screen >= 0; --screen)
207 if (visible_screens & (1u << screen))
208 return screen;
209 return -1;
210}
211
212bool ui_session_is_covered(const ui_session_t *s) { return s && s->covered; }
213
215 size_t length = s ? frame_buffer_get_length(s->frame) : 0;
216 return length ? length - 1 : 0;
217}
218
221 : UINT64_MAX;
222}
223
225 uint64_t now_ns) {
226 if (!s || !view)
227 return NULL;
228 bool changed = s->active != view->screen;
229 s->active = view->screen;
230 bool covered = view->screen >= 0 && ui_too_small(s->size, view->minimum);
231 bool transition = changed || covered != s->covered ||
232 s->size.cols != s->presented_size.cols ||
233 s->size.rows != s->presented_size.rows ||
234 view->minimum.cols != s->minimum.cols ||
235 view->minimum.rows != s->minimum.rows;
236 s->covered = covered;
237 bool visible =
238 (view->screen == UI_SCREEN_SPLASH ||
239 view->screen == UI_SCREEN_RECONNECT || view->screen == UI_SCREEN_HELP ||
240 view->screen == UI_SCREEN_MDNS) &&
241 !covered;
242 if (visible != s->animation.visible) {
243 // Account for the elapsed interval under the old visibility before changing
244 // it, so resuming after an idle modal does not advance a paused animation.
245 if (animation_update(&s->animation, now_ns, &s->splash.animation) !=
247 return NULL;
248 animation_set_visible(&s->animation, visible);
249 }
250 if (animation_update(&s->animation, now_ns, &s->splash.animation) !=
252 return NULL;
253 if (view->screen < 0 || !view->draw)
254 return NULL;
255 bool animated = visible && s->splash.animation.changed;
256 bool refresh =
257 view->refresh_ns && now_ns - s->last_render_ns >= view->refresh_ns;
258 if (covered ? !(transition || s->dirty)
259 : !(transition || s->dirty || view->dirty || animated || refresh))
260 return NULL;
261 s->dirty = false;
262 s->presented_size = s->size;
263 s->minimum = view->minimum;
264 s->last_render_ns = now_ns;
266 if (transition || view->clear) {
267 frame_buffer_append(s->frame, "\033[0m", 4);
269 }
270 if (covered)
271 ui_too_small_render(s->frame, s->size, view->minimum);
272 else {
273 // Hide the cursor in the submitted media frame, before moving it to draw
274 // the art. A separate stdout write can be reordered against presentation,
275 // and cached visibility becomes stale when another screen shows the cursor.
276 if (view->screen == UI_SCREEN_MEDIA)
277 frame_buffer_append(s->frame, "\033[?25l", 6);
278 if (view->screen == UI_SCREEN_HELP) {
279 int top = (s->size.rows - view->minimum.rows) / 2;
280 terminal_color_mode_t mode = GET_OPTION(color_mode);
281 bool colors = mode != TERM_COLOR_NONE &&
283 !GET_OPTION(strip_ansi) &&
284 (mode != TERM_COLOR_AUTO ||
286 .color_level != TERM_COLOR_NONE);
287 frame_buffer_printf(s->frame, "\033[%d;1H", top + 2);
289 (int)(s->splash.animation.seconds * 60), colors,
290 false);
291 }
292 view->draw(s->frame, s->size, &s->splash.animation, view->context);
293 }
294 frame_buffer_append(s->frame, "\0", 1);
296}
297
298static void draw_help(frame_buffer_t *output, terminal_size_t size,
299 const animation_sample_t *animation, void *context) {
300 (void)size;
301 (void)animation;
302 const char *text = context;
303 frame_buffer_append(output, text, strlen(text));
304}
305
306static void draw_splash(frame_buffer_t *output, terminal_size_t size,
307 const animation_sample_t *animation, void *context) {
308 ui_session_t *s = context;
309 s->splash.animation = *animation;
310 ui_session_splash_render(output, size, &s->splash);
311}
312
313static void draw_reconnect(frame_buffer_t *output, terminal_size_t size,
314 const animation_sample_t *animation, void *context) {
315 ui_session_t *s = context;
317 output, size, &s->reconnect, (uint64_t)(animation->seconds * 60.0));
318 if (result != ASCIICHAT_OK)
319 SET_ERRNO(result, "Cannot render reconnect view");
320}
321
322void ui_session_logs_render(frame_buffer_t *output, terminal_size_t size,
323 ui_session_t *s) {
325 size_t offset = terminal_screen_logs_offset();
326 char offset_text[TUI_SESSION_OFFSET_TEXT_BUFFER_SIZE];
327 snprintf(offset_text, sizeof(offset_text), "%zu", offset);
328 const char *colored_offset = colored_string(LOG_COLOR_FATAL, offset_text);
329 snprintf(detail, sizeof(detail), "%s | %s rows from tail",
330 ui_status_log_controls(), colored_offset);
331 ui_status_t status = {
332 .mode_name = "", .start_time = s->started, .detail = detail};
333 terminal_screen_config_t config = {.fixed_header_lines = 4,
334 .render_header = ui_status_render_header,
335 .user_data = &status,
336 .show_logs = true};
337 terminal_screen_compose(output, size, &config, s->logs);
338}
339
340static bool ui_session_logs_key_sized(keyboard_key_t key,
341 terminal_size_t size) {
342 if (key == KEY_ESCAPE && log_search_is_active()) {
344 return true;
345 }
346 if (log_search_should_handle(key)) {
348 return true;
349 }
351 return true;
352 // Client-like log screens own these keys while the renderer is hidden.
353 // Search and log navigation take precedence over rendering shortcuts.
354 switch ((int)key) {
355 case 'c':
356 case 'C':
357 case 'r':
358 case 'R':
359 case 'S':
360 case 'f':
361 case 'F':
362 case 'x':
363 case 'X':
364 case 'y':
365 case 'Y':
366 case '1':
367 case '2':
368 case '3':
369 case 'w':
370 case 'W':
371 case 'z':
372 case 'Z':
373 case '-':
374 return true;
375 default:
376 break;
377 }
378 return false;
379}
380
381bool ui_session_logs_key(keyboard_key_t key) {
382 return ui_session_logs_key_sized(key, ui_controller_size());
383}
384
385static void draw_logs(frame_buffer_t *output, terminal_size_t size,
386 const animation_sample_t *animation, void *context) {
387 (void)animation;
388 ui_session_logs_render(output, size, context);
389}
390
391static void draw_media(frame_buffer_t *output, terminal_size_t size,
392 const animation_sample_t *animation, void *context) {
393 (void)size;
394 (void)animation;
395 ui_session_t *s = context;
398}
399
400const char *ui_session_tick(ui_session_t *s, uint64_t now_ns) {
401 if (!s)
402 return NULL;
403 if (!s->dirty && !s->show_logs && !s->help && !s->loading &&
405 return NULL;
406 uint32_t visible = 1u << UI_SCREEN_MEDIA;
407 if (s->loading)
408 visible |= 1u << UI_SCREEN_SPLASH;
409 if (s->reconnecting)
410 visible |= 1u << UI_SCREEN_RECONNECT;
411 if (s->show_logs)
412 visible |= 1u << UI_SCREEN_LOGS;
413 if (s->help)
414 visible |= 1u << UI_SCREEN_HELP;
415 ui_session_view_t view = {.screen = ui_session_select_screen(visible),
416 .minimum = {1, 1},
417 .clear = !s->help,
418 .context = s};
419 char *help = NULL;
420 if (view.screen == UI_SCREEN_HELP) {
421 help = ui_session_help_render(s->size, true, true, &view.minimum);
422 if (!help)
423 return NULL;
424 view.draw = draw_help;
425 view.context = help;
426 } else if (view.screen == UI_SCREEN_LOGS) {
427 view.minimum = (terminal_size_t){.cols = 40, .rows = 6};
428 view.refresh_ns = 100000000;
429 view.draw = draw_logs;
430 } else if (view.screen == UI_SCREEN_SPLASH) {
431 view.minimum = (terminal_size_t){.cols = 52, .rows = 10};
432 view.draw = draw_splash;
433 } else if (view.screen == UI_SCREEN_RECONNECT) {
435 view.refresh_ns = 125000000;
436 view.draw = draw_reconnect;
437 } else {
438 view.draw = draw_media;
439 if (s->dirty && frame_buffer_get_length(s->media))
440 s->presentation_times[s->presentation_index++ % 120] = now_ns;
441 }
442 const char *frame = ui_session_render(s, &view, now_ns);
444 return frame;
445}
446
448 if (s) {
450 s->help = false;
451 s->show_logs = false;
452 s->dirty = true;
453 }
454}
455
456terminal_size_t ui_session_size(const ui_session_t *s) {
457 return s ? s->size : (terminal_size_t){.cols = 80, .rows = 24};
458}
459
461 if (s)
462 s->dirty = true;
463}
464
#define SET_ERRNO(code,...)
@ LOG_COLOR_FATAL
Definition color.h:12
int color_mode
Definition fft.c:26
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len)
Append raw bytes to the buffer.
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.
void frame_buffer_printf(frame_buffer_t *buf, const char *fmt,...)
Printf-style append to the buffer.
void frame_buffer_clear_screen(frame_buffer_t *buf)
Append clear screen and home escape codes (\033[2J\033[H).
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
struct session_log_buffer session_log_buffer_t
Thread-safe fixed-size history ring for session-screen log lines.
session_log_buffer_t * log_get_session_log_buffer(void)
Get the currently registered session log buffer.
Definition log/log.c:2287
void log_clear_session_log_buffer(void)
Unregister the session log buffer.
Definition log/log.c:2272
void log_set_session_log_buffer(session_log_buffer_t *buf)
Register a session log buffer with the logger.
Definition log/log.c:2263
#define GET_OPTION(field)
Definition options.h:1339
#define options_terminal_settings()
Definition options.h:1287
@ COLOR_SETTING_FALSE
Force colors OFF: disable all colors and logging colors.
Definition options.h:250
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
struct ui_session ui_session_t
void invitation_render_logo(frame_buffer_t *buf, int cols, int frame, bool colors, bool compact)
Definition invitation.c:45
void ui_session_redraw(ui_session_t *s)
void ui_session_resize(ui_session_t *s, int cols, int rows)
#define TUI_SESSION_DETAIL_BUFFER_SIZE
int ui_session_select_screen(uint32_t visible_screens)
const char * ui_session_render(ui_session_t *s, const ui_session_view_t *view, uint64_t now_ns)
size_t ui_session_frame_length(const ui_session_t *s)
void ui_session_loading(ui_session_t *s, bool loading, const char *message)
void ui_session_destroy(ui_session_t *s)
bool ui_session_logs_key(keyboard_key_t key)
#define PRESENTATION_TIME_HISTORY_COUNT
asciichat_error_t ui_session_media(ui_session_t *s, const char *data, size_t length)
uint64_t ui_session_next_frame_ns(const ui_session_t *s)
ui_session_action_t ui_session_input(ui_session_t *s, keyboard_key_t key)
terminal_size_t ui_session_size(const ui_session_t *s)
void ui_session_clear(ui_session_t *s)
void ui_session_reconnecting(ui_session_t *s, bool reconnecting, const ui_reconnect_view_t *view)
void ui_session_capture_logs(ui_session_t *s)
#define TUI_SESSION_OFFSET_TEXT_BUFFER_SIZE
const char * ui_session_tick(ui_session_t *s, uint64_t now_ns)
bool ui_session_is_covered(const ui_session_t *s)
void ui_session_log(ui_session_t *s, const char *message)
ui_session_t * ui_session_create(void)
void ui_session_logs_render(frame_buffer_t *output, terminal_size_t size, ui_session_t *s)
session_log_buffer_t * log_get_history_buffer(void)
Definition log/log.c:625
📝 Logging API with multiple log levels and terminal output control
deferred_action_t action
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
terminal_size_t ui_reconnect_minimum(void)
Definition reconnect.c:240
asciichat_error_t ui_reconnect_render(frame_buffer_t *out, terminal_size_t size, const ui_reconnect_view_t *view, uint64_t animation_frame)
Definition reconnect.c:135
#define UI_RECONNECT_TARGET_BUFFER_SIZE
Definition reconnect.h:13
#define UI_RECONNECT_DETAIL_BUFFER_SIZE
Definition reconnect.h:14
asciichat_error_t log_search_init(void)
Initialize interactive grep subsystem.
Definition search.c:148
bool log_search_is_active(void)
Check if filtering is active.
Definition search.c:430
void log_search_exit_mode(bool accept)
Exit search mode.
Definition search.c:312
bool log_search_should_handle(int key)
Check if a key should be handled by grep module.
Definition search.c:441
asciichat_error_t log_search_handle_key(keyboard_key_t key)
Process keyboard input for grep.
Definition search.c:532
Interactive search filtering for terminal screens.
void ui_session_help_free(char *text)
char * ui_session_help_render(terminal_size_t size, bool has_media, bool browser, terminal_size_t *minimum)
ui_session_action_t ui_session_key(keyboard_key_t key, double *saved_volume)
session_log_buffer_t * session_log_buffer_create(void)
Create a new session log buffer instance.
void session_log_buffer_append(session_log_buffer_t *buf, const char *message)
Append a log message to the 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).
void ui_session_splash_render(frame_buffer_t *buf, terminal_size_t term_size, void *user_data)
void ui_status_render_header(frame_buffer_t *buf, terminal_size_t term_size, void *user_data)
void terminal_screen_compose(frame_buffer_t *output, terminal_size_t size, const terminal_screen_config_t *config, session_log_buffer_t *logs)
terminal_capabilities_t detect_terminal_capabilities(const terminal_options_t *options)
const char * ui_status_log_controls(void)
Definition status.c:33
📊 Status screen display and management for server/discovery service modes
🔤 String Manipulation and Shell Escaping Utilities
For animation config, this is the live record that ties type, fps, speed, hidden_policy together....
Configuration for terminal screen rendering.
const char * last_error
Definition reconnect.h:27
bool can_retry
The host can immediately start the next attempt.
Definition reconnect.h:32
const char * encryption
Definition reconnect.h:24
const char * protocol
Definition reconnect.h:26
const char * transport
Definition reconnect.h:23
const char * target
Definition reconnect.h:22
const char * handshake
Definition reconnect.h:25
This is the handoff point for ui session. Callers fill size, frame, media, logs before passing it on;...
char reconnect_encryption[UI_RECONNECT_DETAIL_BUFFER_SIZE]
uint64_t last_render_ns
frame_buffer_t * frame
uint64_t presentation_times[120]
char reconnect_target[UI_RECONNECT_TARGET_BUFFER_SIZE]
unsigned presentation_index
ui_splash_view_t splash
animation_t animation
terminal_size_t minimum
char reconnect_protocol[UI_RECONNECT_DETAIL_BUFFER_SIZE]
terminal_size_t size
char reconnect_transport[UI_RECONNECT_DETAIL_BUFFER_SIZE]
char reconnect_handshake[UI_RECONNECT_DETAIL_BUFFER_SIZE]
terminal_size_t presented_size
session_log_buffer_t * logs
char reconnect_error[UI_RECONNECT_DETAIL_BUFFER_SIZE]
double saved_volume
frame_buffer_t * media
ui_reconnect_view_t reconnect
Status information for server/discovery service modes.
Definition status.h:43
size_t terminal_screen_logs_offset(void)
bool terminal_screen_logs_handle_key_sized(int key, terminal_size_t size)
Reusable "fixed header + scrolling logs" terminal screen abstraction.
void ui_too_small_render(frame_buffer_t *buffer, terminal_size_t actual, terminal_size_t minimum)
Definition too_small.c:44
bool ui_too_small(terminal_size_t actual, terminal_size_t minimum)
Definition too_small.c:10
terminal_size_t ui_controller_size(void)
#define UI_CONTROLLER_MAX_FRAME_BYTES
@ UI_SCREEN_COUNT
@ UI_SCREEN_LOGS
@ UI_SCREEN_MEDIA
@ UI_SCREEN_RECONNECT
@ UI_SCREEN_SPLASH
@ UI_SCREEN_MDNS
@ UI_SCREEN_HELP
void animation_destroy(animation_t *a)
asciichat_error_t animation_update(animation_t *a, uint64_t now, animation_sample_t *out)
void animation_reset(animation_t *a)
asciichat_error_t animation_init(animation_t *a, const char *name, animation_config_t config)
void animation_set_visible(animation_t *a, bool visible)