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

Go to the source code of this file.

Data Structures

struct  ui_discovery_view_t
struct  ui_splash_view_t
struct  ui_session_view_t

Macros

#define TUI_SESSION_SESSION_STRING_BUFFER_SIZE   256
#define TUI_SESSION_UPDATE_NOTIFICATION_BUFFER_SIZE   1024
#define TUI_SESSION_CONNECTION_TARGET_BUFFER_SIZE   512

Typedefs

typedef struct ui_session ui_session_t
typedef void(* ui_session_draw_fn) (frame_buffer_t *output, terminal_size_t size, const animation_sample_t *animation, void *context)
 The useful part of ui session view is in screen, minimum, dirty, clear: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path.

Enumerations

enum  ui_session_action_t {
  UI_ACTION_NONE , UI_ACTION_SETTINGS , UI_ACTION_HELP , UI_ACTION_CLOSE ,
  UI_ACTION_SEEK_BACK , UI_ACTION_SEEK_FORWARD , UI_ACTION_PAUSE , UI_ACTION_VOLUME ,
  UI_ACTION_DEBUG , UI_ACTION_LOGS , UI_ACTION_TOGGLE_MEDIA , UI_ACTION_QUIT ,
  UI_ACTION_FORCE_QUIT , UI_ACTION_RETRY
}

Functions

void ui_session_splash_render (frame_buffer_t *buf, terminal_size_t size, void *view)
char * ui_session_help_render (terminal_size_t size, bool has_media, bool browser, terminal_size_t *minimum)
void ui_session_help_free (char *text)
char * ui_session_context_help_render (terminal_size_t size, ui_screen_t screen, terminal_size_t *minimum)
ui_session_action_t ui_session_key (keyboard_key_t key, double *saved_volume)
terminal_size_t ui_session_size (const ui_session_t *session)
int ui_session_select_screen (uint32_t visible_screens)
const char * ui_session_render (ui_session_t *session, const ui_session_view_t *view, uint64_t now_ns)
bool ui_session_is_covered (const ui_session_t *session)
size_t ui_session_frame_length (const ui_session_t *session)
uint64_t ui_session_next_frame_ns (const ui_session_t *session)
ui_session_t * ui_session_create (void)
void ui_session_destroy (ui_session_t *session)
void ui_session_resize (ui_session_t *session, int cols, int rows)
void ui_session_loading (ui_session_t *session, bool loading, const char *message)
void ui_session_reconnecting (ui_session_t *session, bool reconnecting, const ui_reconnect_view_t *view)
void ui_session_log (ui_session_t *session, const char *message)
asciichat_error_t ui_session_media (ui_session_t *session, const char *data, size_t length)
ui_session_action_t ui_session_input (ui_session_t *session, keyboard_key_t key)
const char * ui_session_tick (ui_session_t *session, uint64_t now_ns)
void ui_session_clear (ui_session_t *session)
void ui_session_redraw (ui_session_t *session)
void ui_session_logs_render (frame_buffer_t *output, terminal_size_t size, ui_session_t *session)
bool ui_session_logs_key (keyboard_key_t key)
void ui_session_capture_logs (ui_session_t *session)

Macro Definition Documentation

◆ TUI_SESSION_CONNECTION_TARGET_BUFFER_SIZE

#define TUI_SESSION_CONNECTION_TARGET_BUFFER_SIZE   512

Definition at line 6 of file include/ascii-chat/tui/session.h.

◆ TUI_SESSION_SESSION_STRING_BUFFER_SIZE

#define TUI_SESSION_SESSION_STRING_BUFFER_SIZE   256

Definition at line 4 of file include/ascii-chat/tui/session.h.

◆ TUI_SESSION_UPDATE_NOTIFICATION_BUFFER_SIZE

#define TUI_SESSION_UPDATE_NOTIFICATION_BUFFER_SIZE   1024

Definition at line 5 of file include/ascii-chat/tui/session.h.

Typedef Documentation

◆ ui_session_draw_fn

typedef void(* ui_session_draw_fn) (frame_buffer_t *output, terminal_size_t size, const animation_sample_t *animation, void *context)

The useful part of ui session view is in screen, minimum, dirty, clear: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path.

A borrowed view, valid for the duration of ui_session_render(). Hosts retain

The useful part of ui session view is in screen, minimum, dirty, clear: together they describe what is pending, who owns it, and how much data is valid. Keep the object with its parent operation and use the parent cleanup path. snapshots and serialize access to a session; callbacks must not recurse into its renderer. The runtime owns selection policy, transitions and animation.

Definition at line 80 of file include/ascii-chat/tui/session.h.

◆ ui_session_t

typedef struct ui_session ui_session_t

Definition at line 66 of file include/ascii-chat/tui/session.h.

Enumeration Type Documentation

◆ ui_session_action_t

Enumerator
UI_ACTION_NONE 
UI_ACTION_SETTINGS 
UI_ACTION_HELP 
UI_ACTION_CLOSE 
UI_ACTION_SEEK_BACK 
UI_ACTION_SEEK_FORWARD 
UI_ACTION_PAUSE 
UI_ACTION_VOLUME 
UI_ACTION_DEBUG 
UI_ACTION_LOGS 
UI_ACTION_TOGGLE_MEDIA 
UI_ACTION_QUIT 
UI_ACTION_FORCE_QUIT 
UI_ACTION_RETRY 

Definition at line 46 of file include/ascii-chat/tui/session.h.

46 {

Function Documentation

◆ ui_session_capture_logs()

void ui_session_capture_logs ( ui_session_t * session)

Attach this instance to the module's existing C logger. Detaches on destruction.

Definition at line 465 of file lib/tui/session.c.

465 {
466 if (s)
468}
void log_set_session_log_buffer(session_log_buffer_t *buf)
Register a session log buffer with the logger.
Definition log/log.c:2263

References log_set_session_log_buffer(), and ui_session::logs.

Referenced by SessionUiBinding::SessionUiBinding().

◆ ui_session_clear()

void ui_session_clear ( ui_session_t * session)

Definition at line 447 of file lib/tui/session.c.

447 {
448 if (s) {
449 frame_buffer_reset(s->media);
450 s->help = false;
451 s->show_logs = false;
452 s->dirty = true;
453 }
454}
void frame_buffer_reset(frame_buffer_t *buf)
Clear buffer contents while keeping allocation.

References ui_session::dirty, frame_buffer_reset(), ui_session::help, ui_session::media, and ui_session::show_logs.

Referenced by SessionUiBinding::clear().

◆ ui_session_context_help_render()

char * ui_session_context_help_render ( terminal_size_t size,
ui_screen_t screen,
terminal_size_t * minimum )

Definition at line 289 of file session_help.c.

290 {
291 if (!minimum) {
292 SET_ERRNO(ERROR_INVALID_PARAM, "Help minimum geometry output is required");
293 return NULL;
294 }
295 static const ui_help_entry_t sync[] = {{"0 / Esc", "Close sync viewer"},
296 {"Up / Down", "Select primitive"},
297 {"Left / Right", "Previous/next page"},
298 {"Home / End", "First/last primitive"},
299 {"=", "Running app statistics"},
300 {"]", "Log viewer"},
301 {"?", "Close this help"}};
302 static const ui_help_entry_t stats[] = {
303 {"= / Esc", "Close statistics"},
304 {"Tab / Left / Right", "Switch data page"},
305 {"Up / Down", "Select and scroll rows"},
306 {"Home / End", "First/last row"},
307 {"0", "Sync primitives"},
308 {"]", "Log viewer"},
309 {"?", "Close this help"}};
310 static const ui_help_entry_t errors[] = {
311 {"` / Esc", "Close error stacks"},
312 {"Tab", "Current/history page"},
313 {"Up / Down", "Select and scroll errors"},
314 {"s", "Change sort order"},
315 {"x", "Clear selected error"},
316 {"=", "Running app statistics"},
317 {"0", "Sync primitives"},
318 {"?", "Close this help"}};
319 static const ui_help_entry_t update[] = {
320 {"F12 / Esc", "Close update checker"},
321 {"Left / Right / Tab", "Select button"},
322 {"Enter", "Activate selected button"},
323 {"O", "Open release page"},
324 {"R", "Check again"},
325 {"?", "Close this help"}};
326 static const ui_help_entry_t lan[] = {
327 {"Up / Down / j / k", "Select LAN server"},
328 {"0-9", "Enter server number"},
329 {"Enter", "Connect to selected server"},
330 {"Esc", "Cancel LAN selection"},
331 {"=", "Running app statistics"},
332 {"0", "Sync primitives"},
333 {"?", "Close this help"}};
334 static const ui_help_entry_t media[] = {
335 {"Enter", "Play the entered paths or URLs"},
336 {"Esc / Ctrl-O", "Cancel media input"},
337 {"Left / Right", "Move the text cursor"},
338 {"Home / End", "Beginning/end of input"},
339 {"Backspace / Delete", "Delete a character"},
340 {"?", "Help when the input is empty"}};
341 const ui_help_entry_t *entries = NULL;
342 size_t count = 0;
343 const char *title = NULL;
344 switch (screen) {
345 case UI_SCREEN_ERROR: {
346 static const ui_help_entry_t popup[] = {
347 {"Left / Right", "Previous / next error"},
348 {"Home / End", "First / last error"},
349 {"Up / Down", "Scroll error details"},
350 {"PgUp / PgDn", "Scroll a page"},
351 {"Enter / Esc", "Dismiss all errors"},
352 {"q", "Dismiss all errors"},
353 {"?", "Close this help"}};
354 entries = popup;
355 count = sizeof(popup) / sizeof(*popup);
356 title = "Error Popup Keyboard Shortcuts";
357 break;
358 }
359 case UI_SCREEN_SYNC:
360 entries = sync;
361 count = sizeof(sync) / sizeof(*sync);
362 title = "Sync Viewer Keyboard Shortcuts";
363 break;
364 case UI_SCREEN_STATS:
365 entries = stats;
366 count = sizeof(stats) / sizeof(*stats);
367 title = "Statistics Keyboard Shortcuts";
368 break;
369 case UI_SCREEN_ERRNO:
370 entries = errors;
371 count = sizeof(errors) / sizeof(*errors);
372 title = "Error Stacks Keyboard Shortcuts";
373 break;
374 case UI_SCREEN_PROMPT:
375 entries = media;
376 count = sizeof(media) / sizeof(*media);
377 title = "Media Input Keyboard Shortcuts";
378 break;
379 case UI_SCREEN_MDNS:
380 entries = lan;
381 count = sizeof(lan) / sizeof(*lan);
382 title = "LAN Selection Keyboard Shortcuts";
383 break;
384 case UI_SCREEN_UPDATE:
385 entries = update;
386 count = sizeof(update) / sizeof(*update);
387 title = "Update Checker Keyboard Shortcuts";
388 break;
389 default:
391 size, GET_OPTION(media_file)[0] || GET_OPTION(media_url)[0], false,
392 minimum);
393 }
395 snprintf(footer, sizeof(footer), "Press %s to close",
397 ui_help_menu_t menu = {.title = title,
398 .entries = entries,
399 .count = count,
400 .footer = footer,
401 .top_margin = 0};
402 *minimum = ui_help_menu_measure(&menu);
403 frame_buffer_t *buffer = frame_buffer_create(size.rows, size.cols);
404 if (!buffer)
405 return NULL;
406 ui_help_menu_render(buffer, size, &menu);
407 size_t length = frame_buffer_get_length(buffer);
408 char *result = SAFE_MALLOC(length + 1, char *);
409 memcpy(result, frame_buffer_get_content(buffer), length);
410 result[length] = '\0';
411 frame_buffer_destroy(buffer);
412 return result;
413}
#define SET_ERRNO(code,...)
@ LOG_COLOR_WARN
Definition color.h:10
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.
struct frame_buffer frame_buffer_t
Opaque frame buffer handle.
#define SAFE_MALLOC(size, cast)
@ ERROR_INVALID_PARAM
#define GET_OPTION(field)
Definition options.h:1339
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
terminal_size_t ui_help_menu_measure(const ui_help_menu_t *m)
Definition help_menu.c:86
bool ui_help_menu_render(frame_buffer_t *out, terminal_size_t size, const ui_help_menu_t *m)
Definition help_menu.c:103
char * ui_session_help_render(terminal_size_t size, bool has_media, bool browser, terminal_size_t *minimum)
#define TUI_SESSION_HELP_FOOTER_BUFFER_SIZE
Definition session_help.c:6
Definition help_menu.h:7
ui help entry keeps the values that move through the subsystem: keys, description....
Definition help_menu.h:19
@ UI_SCREEN_PROMPT
@ UI_SCREEN_ERRNO
@ UI_SCREEN_STATS
@ UI_SCREEN_UPDATE
@ UI_SCREEN_SYNC
@ UI_SCREEN_MDNS
@ UI_SCREEN_ERROR

References colored_string(), ERROR_INVALID_PARAM, frame_buffer_create(), frame_buffer_destroy(), frame_buffer_get_content(), frame_buffer_get_length(), GET_OPTION, LOG_COLOR_WARN, SAFE_MALLOC, SET_ERRNO, TUI_SESSION_HELP_FOOTER_BUFFER_SIZE, ui_help_menu_measure(), ui_help_menu_render(), UI_SCREEN_ERRNO, UI_SCREEN_ERROR, UI_SCREEN_MDNS, UI_SCREEN_PROMPT, UI_SCREEN_STATS, UI_SCREEN_SYNC, UI_SCREEN_UPDATE, and ui_session_help_render().

◆ ui_session_create()

ui_session_t * ui_session_create ( void )

Definition at line 55 of file lib/tui/session.c.

55 {
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}
#define SAFE_CALLOC(count, size, cast)
struct ui_session ui_session_t
void ui_session_destroy(ui_session_t *s)
session_log_buffer_t * log_get_history_buffer(void)
Definition log/log.c:625
asciichat_error_t log_search_init(void)
Initialize interactive grep subsystem.
Definition search.c:148
session_log_buffer_t * session_log_buffer_create(void)
Create a new session log buffer instance.
frame_buffer_t * frame
ui_splash_view_t splash
animation_t animation
terminal_size_t size
session_log_buffer_t * logs
double saved_volume
frame_buffer_t * media
asciichat_error_t animation_init(animation_t *a, const char *name, animation_config_t config)

References ui_session::active, ui_session::animation, animation_init(), ui_session::dirty, ui_session::frame, frame_buffer_create(), log_get_history_buffer(), log_search_init(), ui_session::logs, ui_session::media, ui_session::owns_logs, SAFE_CALLOC, ui_session::saved_volume, session_log_buffer_create(), ui_session::size, ui_session::splash, ui_session::started, ui_session_destroy(), and ui_splash_view_t::use_colors.

Referenced by SessionUiBinding::SessionUiBinding().

◆ ui_session_destroy()

void ui_session_destroy ( ui_session_t * session)

Definition at line 84 of file lib/tui/session.c.

84 {
85 if (!s)
86 return;
87 if (log_get_session_log_buffer() == s->logs)
89 animation_destroy(&s->animation);
90 frame_buffer_destroy(s->frame);
91 frame_buffer_destroy(s->media);
92 if (s->owns_logs)
94 SAFE_FREE(s);
95}
#define SAFE_FREE(ptr)
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 session_log_buffer_destroy(session_log_buffer_t *buf)
Cleanup session log buffer instance.
void animation_destroy(animation_t *a)

References ui_session::animation, animation_destroy(), ui_session::frame, frame_buffer_destroy(), log_clear_session_log_buffer(), log_get_session_log_buffer(), ui_session::logs, ui_session::media, ui_session::owns_logs, SAFE_FREE, and session_log_buffer_destroy().

Referenced by SessionUiBinding::close(), ui_controller_shutdown(), and ui_session_create().

◆ ui_session_frame_length()

size_t ui_session_frame_length ( const ui_session_t * session)

Definition at line 214 of file lib/tui/session.c.

214 {
215 size_t length = s ? frame_buffer_get_length(s->frame) : 0;
216 return length ? length - 1 : 0;
217}

References ui_session::frame, and frame_buffer_get_length().

◆ ui_session_help_free()

void ui_session_help_free ( char * text)

Release rendered help with the same allocator that created it.

Definition at line 33 of file session_help.c.

33{ SAFE_FREE(text); }

References SAFE_FREE.

Referenced by ui_session_tick().

◆ ui_session_help_render()

char * ui_session_help_render ( terminal_size_t size,
bool has_media,
bool browser,
terminal_size_t * minimum )

Returns owned ANSI text, released with SAFE_FREE.

Definition at line 130 of file session_help.c.

131 {
132 if (!minimum) {
133 SET_ERRNO(ERROR_INVALID_PARAM, "Help minimum geometry output is required");
134 return NULL;
135 }
136 const char *navigation[] = {
137 "Navigation & Control:",
138 "─────────────────────",
139 "? Toggle this help screen TUI",
140 "q / Esc Quit ascii-chat (Shift+Key to panic exit)",
141 browser ? NULL : "v Open from clipboard",
142 browser ? NULL : "Paste Queue dropped/pasted media",
143 browser ? NULL : "< / > Previous/next media (restart)",
144 has_media ? "Space Play/Pause (files only)" : NULL,
145 has_media ? "[ Toggle Media Playback" : NULL,
146 has_media ? (terminal_should_use_utf8(GET_OPTION(force_utf8))
147 ? "← / → Seek backward/forward 30s"
148 : "Left / Right Seek backward/forward 30s")
149 : NULL,
150 "m Mute/Unmute audio",
152 ? "↑ / ↓ Volume up/down (10%)"
153 : "Up / Down Volume up/down (10%)",
154 "c Cycle color mode",
155 "f Cycle color filter",
156 "x Flip webcam horizontally",
157 "y Flip webcam vertically",
158 "w Toggle Face Follow",
159 "Shift-s Toggle stabilization",
160 "r Cycle render mode",
161 "Shift-r Cycle text renderer",
162 "- FPS counter",
163#if defined(ASCIICHAT_DIAGNOSTIC_UI)
164 browser ? NULL : "` Error stacks diagnostic TUI",
165#endif
166 browser ? NULL : "o TUI to open media from path or url",
167 "] Live log viewer/searcher TUI",
168 "F12 Version update checker TUI",
169 browser ? NULL : "= Running app statistics TUI",
170#ifndef NDEBUG
171 browser ? NULL : "0 C code sync primitives TUI",
172#endif
173 };
175 [SESSION_HELP_SETTING_BUFFER_SIZE] = {"Current Settings:", ""};
177 format_volume_bar(GET_OPTION(speakers_volume), volume_bar,
178 sizeof(volume_bar));
179 snprintf(settings[2], sizeof(settings[2]), "Audio : %s",
180 status_indicator(GET_OPTION(audio_enabled)));
181 snprintf(settings[3], sizeof(settings[3]), "Volume : %s", volume_bar);
182 snprintf(settings[4], sizeof(settings[4]), "Color : %s",
184 snprintf(settings[5], sizeof(settings[5]), "Filter : %s",
185 color_filter_to_string(GET_OPTION(color_filter)));
186 snprintf(settings[6], sizeof(settings[6]), "Render : %s",
188 snprintf(settings[7], sizeof(settings[7]), "Renderer: %s",
189 renderer_to_string(GET_OPTION(renderer)));
190 snprintf(settings[8], sizeof(settings[8]), "Flip : rows=%s cols=%s",
191 status_indicator(GET_OPTION(flip_y)),
192 status_indicator(GET_OPTION(flip_x)));
193 snprintf(settings[9], sizeof(settings[9]), "Face Follow : %s",
194 status_indicator(GET_OPTION(face_follow)));
195 snprintf(settings[10], sizeof(settings[10]), "Stabilize: %s",
196 status_indicator(GET_OPTION(stabilize)));
197 int settings_count = 11;
198 settings[settings_count++][0] = '\0';
199 snprintf(settings[settings_count++], sizeof(settings[0]),
200 "Animations (number key toggle):");
201 snprintf(settings[settings_count++], sizeof(settings[0]),
202 "(1) Matrix \"Digital Rain\" : %s",
203 status_indicator(GET_OPTION(matrix_rain)));
204 snprintf(settings[settings_count++], sizeof(settings[0]),
205 "(2) Audio Waveform : %s", status_indicator(GET_OPTION(waveform)));
206 snprintf(settings[settings_count++], sizeof(settings[0]),
207 "(3) Audio Frequencies (FFT) : %s",
208 status_indicator(GET_OPTION(fft)));
209 snprintf(settings[settings_count++], sizeof(settings[0]),
210 "(z) Zalgo intensity : %d", GET_OPTION(zalgo));
211 snprintf(settings[settings_count++], sizeof(settings[0]),
212 "(-) FPS Counter : %s", status_indicator(GET_OPTION(fps_counter)));
213 bool server = !browser && (GET_OPTION(detected_mode) == MODE_SERVER ||
214 GET_OPTION(detected_mode) == MODE_MCP_SERVER);
215 bool discovery =
216 !browser && GET_OPTION(detected_mode) == MODE_DISCOVERY_SERVICE;
218 char key_storage[TUI_SESSION_HELP_KEY_STORAGE_COUNT]
220 size_t count = 0;
221 for (size_t i = 0; i < sizeof(navigation) / sizeof(navigation[0]); ++i) {
222 const char *line = navigation[i];
223 if (!line || i == 1)
224 continue;
225 if (line[0] == '?' || line[0] == ']' || line[0] == '=' || line[0] == '0' ||
226 line[0] == '`' || line[0] == 'o' || !strncmp(line, "F12", 3))
227 continue;
228 if ((server || discovery) && i > 1 && line[0] != '?' &&
229 strncmp(line, "Esc", 3) && line[0] != ']' && line[0] != '=' &&
230 line[0] != '0' && line[0] != '`' && strncmp(line, "F12", 3))
231 continue;
232 const char *end = strstr(line, " ");
233 if (end) {
234 size_t length = (size_t)(end - line);
235 if (length >= sizeof(key_storage[0]))
236 length = sizeof(key_storage[0]) - 1;
237 memcpy(key_storage[count], line, length);
238 key_storage[count][length] = '\0';
239 while (*end == ' ')
240 ++end;
241 entries[count] =
242 (ui_help_entry_t){.keys = key_storage[count], .description = end};
243 } else
244 entries[count] = (ui_help_entry_t){.description = line};
245 ++count;
246 }
247 count += tui_shortcut_help(entries + count, 40 - count, server || discovery,
248 browser);
249 if (server || discovery) {
250 settings_count = 4;
251 snprintf(settings[0], sizeof(settings[0]), "%s",
252 discovery ? "Discovery service:" : "Server:");
253 snprintf(settings[1], sizeof(settings[1]), "Port : %d", GET_OPTION(port));
254 snprintf(settings[2], sizeof(settings[2]), "Status screen : %s",
255 status_indicator(GET_OPTION(status_screen)));
256 snprintf(settings[3], sizeof(settings[3]), "Log level : %d",
257 GET_OPTION(log_level));
258 }
259 const char *settings_text[TUI_SESSION_HELP_SETTINGS_COUNT];
260 for (int i = 0; i < settings_count; ++i)
261 settings_text[i] = settings[i];
263 snprintf(footer, sizeof(footer), "Press %s to close",
265 ui_help_menu_t menu = {
266 .title = discovery ? "ascii-chat Discovery Service Keyboard Shortcuts"
267 : server ? "ascii-chat Server Keyboard Shortcuts"
268 : "ascii-chat Keyboard Shortcuts",
269 .entries = entries,
270 .count = count,
271 .settings = settings_text,
272 .settings_count = (size_t)settings_count,
273 .footer = footer,
274 // Browser sessions draw a four-row animated logo above the popup.
275 .top_margin = browser ? 6 : 0};
276 *minimum = ui_help_menu_measure(&menu);
277 frame_buffer_t *output = frame_buffer_create(size.rows, size.cols);
278 if (!output)
279 return NULL;
280 ui_help_menu_render(output, size, &menu);
281 size_t length = frame_buffer_get_length(output);
282 char *result = SAFE_MALLOC(length + 1, char *);
283 memcpy(result, frame_buffer_get_content(output), length);
284 result[length] = '\0';
285 frame_buffer_destroy(output);
286 return result;
287}
int color_mode
Definition fft.c:26
terminal_render_mode_t render_mode
Definition fft.c:27
@ MODE_MCP_SERVER
Model Context Protocol rendering server.
Definition options.h:922
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
Definition options.h:920
@ MODE_SERVER
Server mode - network server options.
Definition options.h:917
const char * color_mode_to_string(terminal_color_mode_t mode)
Convert color mode enum to string.
const char * render_mode_to_string(terminal_render_mode_t mode)
if(!queries||!out||index >=DATABASE_QUERY_COUNT) return false
#define TUI_SESSION_HELP_KEY_STORAGE_COUNT
Definition session_help.c:5
#define SESSION_HELP_ENTRY_CAPACITY
#define TUI_SESSION_HELP_VOLUME_BAR_BUFFER_SIZE
Definition session_help.c:4
#define SESSION_HELP_KEY_BUFFER_SIZE
#define SESSION_HELP_SETTING_BUFFER_SIZE
Definition session_help.c:9
#define TUI_SESSION_HELP_SETTINGS_COUNT
Definition session_help.c:3
size_t tui_shortcut_help(ui_help_entry_t *entries, size_t capacity, bool server, bool browser)
Definition shortcuts.c:70
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)

References color_mode, color_mode_to_string(), colored_string(), ERROR_INVALID_PARAM, frame_buffer_create(), frame_buffer_destroy(), frame_buffer_get_content(), frame_buffer_get_length(), GET_OPTION, LOG_COLOR_WARN, MODE_DISCOVERY_SERVICE, MODE_MCP_SERVER, MODE_SERVER, render_mode, render_mode_to_string(), SAFE_MALLOC, SESSION_HELP_ENTRY_CAPACITY, SESSION_HELP_KEY_BUFFER_SIZE, SESSION_HELP_SETTING_BUFFER_SIZE, SET_ERRNO, terminal_should_use_utf8(), TUI_SESSION_HELP_FOOTER_BUFFER_SIZE, TUI_SESSION_HELP_KEY_STORAGE_COUNT, TUI_SESSION_HELP_SETTINGS_COUNT, TUI_SESSION_HELP_VOLUME_BAR_BUFFER_SIZE, tui_shortcut_help(), ui_help_menu_measure(), and ui_help_menu_render().

Referenced by ui_session_context_help_render(), and ui_session_tick().

◆ ui_session_input()

ui_session_action_t ui_session_input ( ui_session_t * session,
keyboard_key_t key )

Definition at line 177 of file lib/tui/session.c.

177 {
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}
deferred_action_t action
ui_session_action_t ui_session_key(keyboard_key_t key, double *saved_volume)
@ UI_SCREEN_RECONNECT

References action, ui_session::active, ui_reconnect_view_t::can_retry, ui_session::dirty, ui_session::help, ui_session::reconnect, ui_session::saved_volume, ui_session::show_logs, ui_session::size, UI_ACTION_CLOSE, UI_ACTION_HELP, UI_ACTION_LOGS, UI_ACTION_NONE, UI_ACTION_RETRY, UI_SCREEN_RECONNECT, and ui_session_key().

Referenced by SessionUiBinding::key().

◆ ui_session_is_covered()

bool ui_session_is_covered ( const ui_session_t * session)

Definition at line 212 of file lib/tui/session.c.

212{ return s && s->covered; }

References ui_session::covered.

◆ ui_session_key()

ui_session_action_t ui_session_key ( keyboard_key_t key,
double * saved_volume )

Mutates shared options for settings keys; returns an action for the host.

Definition at line 111 of file session_input.c.

111 {
112 switch ((int)key) {
113 case 3: // Ctrl-C from the terminal input stream
114 log_info("Ctrl-C key received from terminal input");
115 return UI_ACTION_QUIT;
116 case 'q':
117 return UI_ACTION_QUIT;
118 case 'Q':
120 case KEY_QUESTION:
121 return UI_ACTION_HELP;
122 case '[':
124 case ']':
125 return UI_ACTION_LOGS;
126 case KEY_ESCAPE:
127 return UI_ACTION_CLOSE;
128 case KEY_SHIFT_ESCAPE:
130 case KEY_LEFT:
131 return UI_ACTION_SEEK_BACK;
132 case KEY_RIGHT:
134 case KEY_SPACE:
135 return UI_ACTION_PAUSE;
136 case KEY_BACKTICK:
137 return UI_ACTION_DEBUG;
138 // ===== VOLUME CONTROLS =====
139 case KEY_DOWN: { // Decrease volume 10%
140 double current_volume = GET_OPTION(speakers_volume);
141 double new_volume = clamp_volume(current_volume - 0.1);
142 options_adjust("speakers_volume", key == KEY_DOWN ? -0.1 : 0.1);
143
144 double verify_volume = GET_OPTION(speakers_volume);
145 log_info("Volume DOWN: %.0f%% → %.0f%% (verified: %.0f%%)",
146 current_volume * 100.0, new_volume * 100.0, verify_volume * 100.0);
147 return UI_ACTION_VOLUME;
148 }
149
150 case KEY_UP: { // Increase volume 10%
151 double current_volume = GET_OPTION(speakers_volume);
152 double new_volume = clamp_volume(current_volume + 0.1);
153 options_adjust("speakers_volume", key == KEY_DOWN ? -0.1 : 0.1);
154
155 double verify_volume = GET_OPTION(speakers_volume);
156 log_info("Volume UP: %.0f%% → %.0f%% (verified: %.0f%%)",
157 current_volume * 100.0, new_volume * 100.0, verify_volume * 100.0);
158 return UI_ACTION_SETTINGS;
159 }
160
161 // ===== COLOR MODE CONTROL =====
162 case KEY_C:
163 case 'C': {
164 int current_mode = (int)GET_OPTION(color_mode);
165 int next_mode = next_color_mode(current_mode);
166 options_cycle("color_mode", 4);
167
168 const char *mode_names[] = {"Mono", "16-color", "256-color", "Truecolor"};
169 if (next_mode >= 0 && next_mode <= 3) {
170 log_info("Color mode: %s", mode_names[next_mode]);
171 }
172 return UI_ACTION_SETTINGS;
173 }
174
175 // ===== MUTE CONTROL =====
176 case KEY_M:
177 case 'M': {
178 if (toggle_mute(saved_volume) != ASCIICHAT_OK)
179 log_warn("Could not toggle mute");
180
181 return UI_ACTION_SETTINGS;
182 }
183
184 // ===== RENDER MODE CONTROL =====
185 case 'r': {
186 int current_mode = (int)GET_OPTION(render_mode);
187 int next_mode = next_render_mode(current_mode);
188 options_cycle("render_mode", 3);
189
190 const char *mode_names[] = {"Foreground", "Background", "Half-block"};
191 if (next_mode >= 0 && next_mode <= 2) {
192 log_info("Render mode: %s", mode_names[next_mode]);
193 }
194 return UI_ACTION_SETTINGS;
195 }
196
197 // ===== TEXT RENDERER CONTROL =====
198 case 'R': {
199 if (options_cycle("renderer", 2) != ASCIICHAT_OK) {
200 log_warn("Could not change text renderer");
201 return UI_ACTION_SETTINGS;
202 }
203 log_info("Text renderer: %s", GET_OPTION(renderer) == ASCII_RENDERER_GLYPH
204 ? "Glyph"
205 : "Luminance");
206 return UI_ACTION_SETTINGS;
207 }
208
209 // ===== FRAME STABILIZATION CONTROL =====
210 case 'S': {
211 if (options_toggle("stabilize") != ASCIICHAT_OK)
212 log_warn("Could not toggle frame stabilization");
213 else
214 log_info("Frame stabilization: %s",
215 GET_OPTION(stabilize) ? "enabled" : "disabled");
216 return UI_ACTION_SETTINGS;
217 }
218
219 // ===== COLOR FILTER CONTROL =====
220 case KEY_F:
221 case 'F': {
222 int current_filter = (int)GET_OPTION(color_filter);
223 int next_filter = next_color_filter(current_filter);
224 options_cycle("color_filter", 13);
225
226 if (next_filter >= 0 && next_filter < (int)COLOR_FILTER_COUNT) {
227 log_info("Color filter: %s", COLOR_FILTER_NAMES[next_filter]);
228 }
229 return UI_ACTION_SETTINGS;
230 }
231
232 // ===== HORIZONTAL FLIP CONTROL =====
233 case 'X':
234 case 'x': {
235 log_info("✓ X KEY PRESSED - handling horizontal flip");
236 bool current_flip_x = (bool)GET_OPTION(flip_x);
237 options_toggle("flip_x");
238 log_info("Horizontal flip: %s (was: %s)",
239 !current_flip_x ? "enabled" : "disabled",
240 current_flip_x ? "enabled" : "disabled");
241 return UI_ACTION_SETTINGS;
242 }
243
244 // ===== VERTICAL FLIP CONTROL =====
245 case 'Y':
246 case 'y': {
247 bool current_flip_y = (bool)GET_OPTION(flip_y);
248 options_toggle("flip_y");
249 log_info("Vertical flip: %s", !current_flip_y ? "enabled" : "disabled");
250 return UI_ACTION_SETTINGS;
251 }
252
253 // ===== MATRIX RAIN EFFECT CONTROL =====
254 case KEY_1: {
255 bool current_matrix = (bool)GET_OPTION(matrix_rain);
256 if (options_toggle("matrix_rain") != ASCIICHAT_OK)
257 return UI_ACTION_SETTINGS;
258 log_info("Matrix rain effect: %s",
259 !current_matrix ? "enabled" : "disabled");
260 return UI_ACTION_SETTINGS;
261 }
262
263 case KEY_2: {
264 if (!GET_OPTION(audio_enabled)) {
265 log_info("Audio waveform requires audio to be enabled");
266 return UI_ACTION_SETTINGS;
267 }
268 bool enabled = !GET_OPTION(waveform);
269 if (options_toggle("waveform") != ASCIICHAT_OK)
270 return UI_ACTION_SETTINGS;
271 log_info("Audio waveform: %s", enabled ? "enabled" : "disabled");
272 return UI_ACTION_SETTINGS;
273 }
274
275 case KEY_3: {
276 if (!GET_OPTION(audio_enabled)) {
277 log_info("Audio frequency display requires audio to be enabled");
278 return UI_ACTION_SETTINGS;
279 }
280 bool enabled = !GET_OPTION(fft);
282 log_error("Unable to initialize FFTW frequency display");
283 return UI_ACTION_SETTINGS;
284 }
285 if (options_toggle("fft") != ASCIICHAT_OK)
286 return UI_ACTION_SETTINGS;
287 log_info("Audio frequency display: %s", enabled ? "enabled" : "disabled");
288 return UI_ACTION_SETTINGS;
289 }
290
291 case 'w':
292 case 'W': {
293 if (options_toggle("face_follow") == ASCIICHAT_OK)
294 log_info("Face follow: %s",
295 GET_OPTION(face_follow) ? "enabled" : "disabled");
296 return UI_ACTION_SETTINGS;
297 }
298
299 case 'z':
300 case 'Z': {
301 if (options_set_int("zalgo", GET_OPTION(zalgo) ? 0 : 8) == ASCIICHAT_OK)
302 log_info("Zalgo intensity: %d", GET_OPTION(zalgo));
303 return UI_ACTION_SETTINGS;
304 }
305
306 // ===== FPS COUNTER TOGGLE =====
307 case KEY_MINUS: {
308 bool current = (bool)GET_OPTION(fps_counter);
309 options_toggle("fps_counter");
310 log_info("FPS counter: %s", !current ? "enabled" : "disabled");
311 return UI_ACTION_SETTINGS;
312 }
313
314 default:
315 return UI_ACTION_NONE;
316 }
317}
asciichat_error_t audio_visualization_fft_init(void)
Definition fft.c:82
bool enabled
Is filtering active?
Definition grep.c:63
@ ASCIICHAT_OK
Definition error_codes.h:50
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
asciichat_error_t options_set_int(const char *name, int value)
Check if an option was explicitly set via command-line.
Definition rcu.c:964
asciichat_error_t options_adjust(const char *name, double amount)
Definition rcu.c:989
asciichat_error_t options_toggle(const char *name)
Definition rcu.c:985
asciichat_error_t options_cycle(const char *name, int count)
Definition rcu.c:995
@ ASCII_RENDERER_GLYPH

References ASCII_RENDERER_GLYPH, ASCIICHAT_OK, audio_visualization_fft_init(), color_mode, enabled, GET_OPTION, log_error, log_info, log_warn, options_adjust(), options_cycle(), options_set_int(), options_toggle(), render_mode, UI_ACTION_CLOSE, UI_ACTION_DEBUG, UI_ACTION_FORCE_QUIT, UI_ACTION_HELP, UI_ACTION_LOGS, UI_ACTION_NONE, UI_ACTION_PAUSE, UI_ACTION_QUIT, UI_ACTION_SEEK_BACK, UI_ACTION_SEEK_FORWARD, UI_ACTION_SETTINGS, UI_ACTION_TOGGLE_MEDIA, and UI_ACTION_VOLUME.

Referenced by ui_session_input().

◆ ui_session_loading()

void ui_session_loading ( ui_session_t * session,
bool loading,
const char * message )

Definition at line 109 of file lib/tui/session.c.

109 {
110 if (!s)
111 return;
112 if (loading != s->loading) {
113 s->loading = loading;
114 animation_reset(&s->animation);
115 s->dirty = true;
116 }
117 if (message && strcmp(message, s->splash.connection_target)) {
118 SAFE_STRNCPY(s->splash.connection_target, message,
119 sizeof(s->splash.connection_target));
120 s->dirty = true;
121 }
122}
#define SAFE_STRNCPY(dst, src, size)
void animation_reset(animation_t *a)

References ui_session::animation, animation_reset(), ui_splash_view_t::connection_target, ui_session::dirty, ui_session::loading, SAFE_STRNCPY, and ui_session::splash.

Referenced by SessionUiBinding::loading().

◆ ui_session_log()

void ui_session_log ( ui_session_t * session,
const char * message )

Definition at line 158 of file lib/tui/session.c.

158 {
159 if (s && message) {
160 session_log_buffer_append(s->logs, message);
161 s->dirty = true;
162 }
163}
void session_log_buffer_append(session_log_buffer_t *buf, const char *message)
Append a log message to the buffer.

References ui_session::dirty, ui_session::logs, and session_log_buffer_append().

Referenced by SessionUiBinding::log().

◆ ui_session_logs_key()

bool ui_session_logs_key ( keyboard_key_t key)

Existing server log-search controls; true when consumed.

Definition at line 381 of file lib/tui/session.c.

381 {
382 return ui_session_logs_key_sized(key, ui_controller_size());
383}
terminal_size_t ui_controller_size(void)

References ui_controller_size().

◆ ui_session_logs_render()

void ui_session_logs_render ( frame_buffer_t * output,
terminal_size_t size,
ui_session_t * session )

Shared live log view, used by the native adapter and browser compositor.

Definition at line 322 of file lib/tui/session.c.

323 {
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}
@ LOG_COLOR_FATAL
Definition color.h:12
#define TUI_SESSION_DETAIL_BUFFER_SIZE
#define TUI_SESSION_OFFSET_TEXT_BUFFER_SIZE
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)
const char * ui_status_log_controls(void)
Definition status.c:33
Configuration for terminal screen rendering.
Status information for server/discovery service modes.
Definition status.h:43
size_t terminal_screen_logs_offset(void)

References colored_string(), LOG_COLOR_FATAL, ui_session::logs, ui_session::started, terminal_screen_compose(), terminal_screen_logs_offset(), TUI_SESSION_DETAIL_BUFFER_SIZE, TUI_SESSION_OFFSET_TEXT_BUFFER_SIZE, ui_status_log_controls(), and ui_status_render_header().

◆ ui_session_media()

asciichat_error_t ui_session_media ( ui_session_t * session,
const char * data,
size_t length )

Definition at line 165 of file lib/tui/session.c.

166 {
167 if (!s || !data || length > UI_CONTROLLER_MAX_FRAME_BYTES)
168 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid session media frame");
169 frame_buffer_reset(s->media);
170 frame_buffer_append(s->media, data, length);
171 s->dirty = true;
172 return ASCIICHAT_OK;
173}
void frame_buffer_append(frame_buffer_t *buf, const char *str, size_t len)
Append raw bytes to the buffer.
#define UI_CONTROLLER_MAX_FRAME_BYTES

References ASCIICHAT_OK, ui_session::dirty, ERROR_INVALID_PARAM, frame_buffer_append(), frame_buffer_reset(), ui_session::media, SET_ERRNO, and UI_CONTROLLER_MAX_FRAME_BYTES.

Referenced by SessionUiBinding::media().

◆ ui_session_next_frame_ns()

uint64_t ui_session_next_frame_ns ( const ui_session_t * session)

Next visible animation deadline, or UINT64_MAX while animation is paused/hidden.

Definition at line 219 of file lib/tui/session.c.

219 {
220 return s && s->animation.started ? s->splash.animation.next_frame_ns
221 : UINT64_MAX;
222}

References ui_session::animation, ui_splash_view_t::animation, animation_sample_t::next_frame_ns, ui_session::splash, and animation_t::started.

◆ ui_session_reconnecting()

void ui_session_reconnecting ( ui_session_t * session,
bool reconnecting,
const ui_reconnect_view_t * view )

Show the independent reconnect view while the host continues retrying.

Definition at line 124 of file lib/tui/session.c.

125 {
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));
132 SAFE_STRNCPY(s->reconnect_transport,
133 view->transport ? view->transport : "LOST",
134 sizeof(s->reconnect_transport));
135 SAFE_STRNCPY(s->reconnect_encryption,
136 view->encryption ? view->encryption : "WAITING",
137 sizeof(s->reconnect_encryption));
138 SAFE_STRNCPY(s->reconnect_handshake,
139 view->handshake ? view->handshake : "WAITING",
140 sizeof(s->reconnect_handshake));
141 SAFE_STRNCPY(s->reconnect_protocol,
142 view->protocol ? view->protocol : "WAITING",
143 sizeof(s->reconnect_protocol));
144 SAFE_STRNCPY(s->reconnect_error,
145 view->last_error ? view->last_error : "connection closed",
146 sizeof(s->reconnect_error));
147 s->reconnect = *view;
148 s->reconnect.target = s->reconnect_target;
149 s->reconnect.transport = s->reconnect_transport;
150 s->reconnect.encryption = s->reconnect_encryption;
151 s->reconnect.handshake = s->reconnect_handshake;
152 s->reconnect.protocol = s->reconnect_protocol;
153 s->reconnect.last_error = s->reconnect_error;
154 }
155 s->dirty = true;
156}
const char * last_error
Definition reconnect.h:27
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

References ui_session::dirty, ui_reconnect_view_t::encryption, ui_reconnect_view_t::handshake, ui_reconnect_view_t::last_error, ui_reconnect_view_t::protocol, ui_session::reconnect, ui_session::reconnect_encryption, ui_session::reconnect_error, ui_session::reconnect_handshake, ui_session::reconnect_protocol, ui_session::reconnect_target, ui_session::reconnect_transport, ui_session::reconnecting, SAFE_STRNCPY, ui_reconnect_view_t::target, and ui_reconnect_view_t::transport.

Referenced by SessionUiBinding::reconnecting().

◆ ui_session_redraw()

void ui_session_redraw ( ui_session_t * session)

Definition at line 460 of file lib/tui/session.c.

460 {
461 if (s)
462 s->dirty = true;
463}

References ui_session::dirty.

Referenced by SessionUiBinding::redraw(), and ui_controller_redraw().

◆ ui_session_render()

const char * ui_session_render ( ui_session_t * session,
const ui_session_view_t * view,
uint64_t now_ns )

Present a host view through the same compositor used by ui_session_tick(). A screen of -1 hides presentation. NULL means no redraw is required.

Definition at line 224 of file lib/tui/session.c.

225 {
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;
265 frame_buffer_reset(s->frame);
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);
288 invitation_render_logo(s->frame, s->size.cols,
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);
295 return frame_buffer_get_content(s->frame);
296}
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
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).
#define options_terminal_settings()
Definition options.h:1287
@ COLOR_SETTING_FALSE
Force colors OFF: disable all colors and logging colors.
Definition options.h:250
void invitation_render_logo(frame_buffer_t *buf, int cols, int frame, bool colors, bool compact)
Definition invitation.c:45
terminal_capabilities_t detect_terminal_capabilities(const terminal_options_t *options)
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
@ UI_SCREEN_MEDIA
@ UI_SCREEN_SPLASH
@ UI_SCREEN_HELP
asciichat_error_t animation_update(animation_t *a, uint64_t now, animation_sample_t *out)
void animation_set_visible(animation_t *a, bool visible)

References ui_session::active, ui_session::animation, ui_splash_view_t::animation, animation_set_visible(), animation_update(), ASCIICHAT_OK, animation_sample_t::changed, ui_session_view_t::clear, color_mode, COLOR_SETTING_FALSE, colors, ui_session_view_t::context, ui_session::covered, detect_terminal_capabilities(), ui_session::dirty, ui_session_view_t::dirty, ui_session_view_t::draw, ui_session::frame, frame_buffer_append(), frame_buffer_clear_screen(), frame_buffer_get_content(), frame_buffer_printf(), frame_buffer_reset(), GET_OPTION, invitation_render_logo(), ui_session::last_render_ns, ui_session::minimum, ui_session_view_t::minimum, options_terminal_settings, ui_session::presented_size, ui_session_view_t::refresh_ns, ui_session_view_t::screen, animation_sample_t::seconds, ui_session::size, ui_session::splash, UI_SCREEN_HELP, UI_SCREEN_MDNS, UI_SCREEN_MEDIA, UI_SCREEN_RECONNECT, UI_SCREEN_SPLASH, ui_too_small(), ui_too_small_render(), and animation_t::visible.

Referenced by ui_session_tick().

◆ ui_session_resize()

void ui_session_resize ( ui_session_t * session,
int cols,
int rows )

Definition at line 97 of file lib/tui/session.c.

97 {
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}

References ui_session::dirty, ERROR_INVALID_PARAM, SET_ERRNO, and ui_session::size.

Referenced by SessionUiBinding::resize().

◆ ui_session_select_screen()

int ui_session_select_screen ( uint32_t visible_screens)

Highest visible screen from the common priority order, or -1.

Definition at line 205 of file lib/tui/session.c.

205 {
206 for (int screen = UI_SCREEN_COUNT - 1; screen >= 0; --screen)
207 if (visible_screens & (1u << screen))
208 return screen;
209 return -1;
210}
@ UI_SCREEN_COUNT

References UI_SCREEN_COUNT.

Referenced by ui_session_tick().

◆ ui_session_size()

terminal_size_t ui_session_size ( const ui_session_t * session)

Definition at line 456 of file lib/tui/session.c.

456 {
457 return s ? s->size : (terminal_size_t){.cols = 80, .rows = 24};
458}

References ui_session::size.

Referenced by ui_controller_size().

◆ ui_session_splash_render()

void ui_session_splash_render ( frame_buffer_t * buf,
terminal_size_t size,
void * view )

Definition at line 19 of file session_splash.c.

20 {
21 const ui_splash_view_t *ctx = (const ui_splash_view_t *)user_data;
22 if (!ctx) {
23 return;
24 }
25 bool unicode = terminal_should_use_utf8(GET_OPTION(force_utf8));
26
27 if (ctx->discovery.enabled) {
28 invitation_render(buf, term_size, ctx->discovery.session_string,
29 ctx->discovery.joining,
30 (int)(ctx->animation.seconds * 60), ctx->use_colors);
31 return;
32 }
33
34 char
36 snprintf(tagline_with_version, sizeof(tagline_with_version),
37 "Video chat in your terminal + %s", ASCII_CHAT_VERSION_STRING);
38 const char *tagline = tagline_with_version;
39 frame_buffer_render_divider(buf, term_size.cols - 1, unicode,
40 ctx->use_colors);
41 invitation_render_logo(buf, term_size.cols - 1,
42 (int)(ctx->animation.seconds * 60), ctx->use_colors,
43 false);
44
45 // Line 6: Tagline (centered, truncated if too long)
47 int tagline_len = (int)strlen(tagline);
48 int tagline_pad = (term_size.cols - tagline_len) / 2;
49 if (tagline_pad < 0) {
50 tagline_pad = 0;
51 }
52
53 int tpos = 0;
54 for (int j = 0; j < tagline_pad && tpos < (int)sizeof(plain_tagline) - 1;
55 j++) {
56 plain_tagline[tpos++] = ' ';
57 }
58 snprintf(plain_tagline + tpos, sizeof(plain_tagline) - tpos, "%s", tagline);
59
60 // Check visible width and truncate if needed
61 int tagline_visible_width = display_width(plain_tagline);
62 if (tagline_visible_width < 0) {
63 tagline_visible_width = (int)strlen(plain_tagline);
64 }
65 if (term_size.cols > 0 && tagline_visible_width >= term_size.cols) {
66 plain_tagline[term_size.cols - 1] = '\0';
67 }
68
69 frame_buffer_printf(buf, "%s\n", plain_tagline);
70
71 // Line 7: Update notification (if available)
72 if (ctx->update_notification[0] != '\0') {
74 int update_len = (int)strlen(ctx->update_notification);
75 int update_pad = (term_size.cols - update_len) / 2;
76 if (update_pad < 0) {
77 update_pad = 0;
78 }
79
80 int upos = 0;
81 for (int j = 0; j < update_pad && upos < (int)sizeof(plain_update) - 1;
82 j++) {
83 plain_update[upos++] = ' ';
84 }
85 snprintf(plain_update + upos, sizeof(plain_update) - upos, "%s",
87
88 // Check visible width and truncate if needed
89 int update_visible_width = display_width(plain_update);
90 if (update_visible_width < 0) {
91 update_visible_width = (int)strlen(plain_update);
92 }
93 if (term_size.cols > 0 && update_visible_width >= term_size.cols) {
94 plain_update[term_size.cols - 1] = '\0';
95 }
96
97 // Print in yellow/warning color
98 frame_buffer_printf(buf, "%s\n",
99 colored_string(LOG_COLOR_WARN, plain_update));
100 }
101
102 // Line 8 (or 7 if no update): Connection target (centered, truncated if too
103 // long)
104 const char *connection_target = ctx->connection_target;
105
107 int connection_len = (int)strlen(connection_target);
108 int connection_pad = (term_size.cols - connection_len) / 2;
109 if (connection_pad < 0) {
110 connection_pad = 0;
111 }
112
113 int cpos = 0;
114 for (int j = 0;
115 j < connection_pad && cpos < (int)sizeof(plain_connection) - 1; j++) {
116 plain_connection[cpos++] = ' ';
117 }
118 snprintf(plain_connection + cpos, sizeof(plain_connection) - cpos, "%s",
119 connection_target);
120
121 // Check visible width and truncate if needed
122 int connection_visible_width = display_width(plain_connection);
123 if (connection_visible_width < 0) {
124 connection_visible_width = (int)strlen(plain_connection);
125 }
126 if (term_size.cols > 0 && connection_visible_width >= term_size.cols) {
127 plain_connection[term_size.cols - 1] = '\0';
128 }
129
130 frame_buffer_printf(buf, "%s\n", plain_connection);
131
133 static const char spin[] = "|/-\\";
134 const char *text = "Checking for updates...";
135 int pad = term_size.cols - (int)strlen(text) - 3;
136 if (pad < 0)
137 pad = 0;
138 frame_buffer_printf(buf, "%*s%c %s\n", pad, "",
139 spin[((int)(ctx->animation.seconds * 8)) % 4], text);
140 }
141
142 // Bottom border
143 frame_buffer_render_divider(buf, term_size.cols - 1, unicode,
144 ctx->use_colors);
145}
void frame_buffer_render_divider(frame_buffer_t *buf, int width, bool use_utf8, bool use_colors)
bool update_banner_is_checking(void)
int display_width(const char *text)
Calculate the visible display width of text with ANSI escape codes.
void invitation_render(frame_buffer_t *buf, terminal_size_t size, const char *session, bool joining, int frame, bool colors)
Definition invitation.c:79
#define TUI_SESSION_SPLASH_PLAIN_CONNECTION_BUFFER_SIZE
#define TUI_SESSION_SPLASH_PLAIN_TAGLINE_BUFFER_SIZE
#define TUI_SESSION_SPLASH_PLAIN_UPDATE_BUFFER_SIZE
#define TUI_SESSION_SPLASH_TAGLINE_WITH_VERSION_BUFFER_SIZE

References ui_splash_view_t::animation, colored_string(), ui_splash_view_t::connection_target, ui_splash_view_t::discovery, display_width(), ui_discovery_view_t::enabled, frame_buffer_printf(), frame_buffer_render_divider(), GET_OPTION, invitation_render(), invitation_render_logo(), ui_discovery_view_t::joining, LOG_COLOR_WARN, animation_sample_t::seconds, ui_discovery_view_t::session_string, terminal_should_use_utf8(), TUI_SESSION_SPLASH_PLAIN_CONNECTION_BUFFER_SIZE, TUI_SESSION_SPLASH_PLAIN_TAGLINE_BUFFER_SIZE, TUI_SESSION_SPLASH_PLAIN_UPDATE_BUFFER_SIZE, TUI_SESSION_SPLASH_TAGLINE_WITH_VERSION_BUFFER_SIZE, update_banner_is_checking(), ui_splash_view_t::update_notification, and ui_splash_view_t::use_colors.

◆ ui_session_tick()

const char * ui_session_tick ( ui_session_t * session,
uint64_t now_ns )

No sleeps or I/O. Returned frame remains valid until the next call. NULL means unchanged.

Definition at line 400 of file lib/tui/session.c.

400 {
401 if (!s)
402 return NULL;
403 if (!s->dirty && !s->show_logs && !s->help && !s->loading &&
404 !s->reconnecting && s->active == UI_SCREEN_MEDIA)
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}
unsigned int uint32_t
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)
terminal_size_t ui_reconnect_minimum(void)
Definition reconnect.c:240
void ui_session_help_free(char *text)
@ UI_SCREEN_LOGS

References ui_session::active, ui_session_view_t::context, ui_session::dirty, ui_session_view_t::draw, frame_buffer_get_length(), ui_session::help, ui_session::loading, ui_session::media, ui_session_view_t::minimum, ui_session::presentation_index, ui_session::presentation_times, ui_session::reconnecting, ui_session_view_t::refresh_ns, ui_session_view_t::screen, ui_session::show_logs, ui_session::size, ui_reconnect_minimum(), UI_SCREEN_HELP, UI_SCREEN_LOGS, UI_SCREEN_MEDIA, UI_SCREEN_RECONNECT, UI_SCREEN_SPLASH, ui_session_help_free(), ui_session_help_render(), ui_session_render(), and ui_session_select_screen().

Referenced by SessionUiBinding::tick().