ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
src/client/display.c
Go to the documentation of this file.
72
75#include "display.h"
76#include "session/display.h"
77#include "main.h"
78#include "../main.h" // Global exit API
79
80#include "display.h"
81#include "capture.h"
85#include <libplatform/compiler.h>
86#include <libplatform/system.h>
87#include <libplatform/terminal.h>
88#include <libplatform/keyboard.h>
89#include <options/options.h>
90#include <options/rcu.h> // For RCU-based options access
93#include <ascii-chat/common.h>
94
95#include <fcntl.h>
96#include <string.h>
97#include <stdio.h>
98#include <libplatform/atomic.h>
100#ifndef _WIN32
101#include <unistd.h>
102#endif
103
104/* ============================================================================
105 * Session Display Context
106 * ============================================================================ */
107
116static session_display_ctx_t *g_display_ctx = NULL;
117
127
135static bool g_keyboard_enabled = false;
136
146static session_capture_ctx_t *g_display_capture_ctx = NULL;
147
148/* TTY detection and terminal reset now handled by session display library */
149
150/* Frame rendering now handled by session display library */
151
152/* ============================================================================
153 * Internal Setter Functions (for framework integration)
154 * ============================================================================ */
155
168 g_display_ctx = display_ctx;
169 g_keyboard_enabled = display_ctx && terminal_is_stdin_tty();
170}
171
172/* ============================================================================
173 * Public Interface Functions
174 * ============================================================================ */
175
187 // Build display configuration from options
188 session_display_config_t config = {
189 .snapshot_mode = GET_OPTION(snapshot_mode),
190 .palette_type = GET_OPTION(palette_type),
191 .custom_palette = GET_OPTION(palette_custom_set) ? GET_OPTION(palette_custom) : NULL,
192 .color_mode = TERM_COLOR_AUTO // Will be overridden by command-line options
193 };
194
195 // Create display context using session library
196 g_display_ctx = session_display_create(&config);
197 if (!g_display_ctx) {
198 SET_ERRNO(ERROR_DISPLAY, "Failed to initialize display");
199 return -1;
200 }
201
202 // Check if client mode should load local media (--file or --url)
203 const char *media_url = GET_OPTION(media_url);
204 const char *media_file = GET_OPTION(media_file);
205
206 if ((media_url && strlen(media_url) > 0) || (media_file && strlen(media_file) > 0)) {
207 // Client mode with local media file/URL - create capture context for keyboard controls
208 session_capture_config_t capture_config = {0};
209 capture_config.target_fps = 60;
210 capture_config.resize_for_network = false;
211 capture_config.should_exit_callback = NULL;
212 capture_config.callback_data = NULL;
213
214 if (media_url && strlen(media_url) > 0) {
215 capture_config.type = MEDIA_SOURCE_FILE;
216 capture_config.path = media_url;
217 } else {
218 if (strcmp(media_file, "-") == 0) {
219 capture_config.type = MEDIA_SOURCE_STDIN;
220 capture_config.path = NULL;
221 } else {
222 capture_config.type = MEDIA_SOURCE_FILE;
223 capture_config.path = media_file;
224 }
225 }
226
227 capture_config.loop = GET_OPTION(media_loop);
228 capture_config.initial_seek_timestamp = GET_OPTION(media_seek_timestamp);
229
230 g_display_capture_ctx = session_capture_create(&capture_config);
231 if (!g_display_capture_ctx) {
232 log_warn("Failed to create capture context for local media - keyboard seek/pause disabled");
233 g_display_capture_ctx = NULL;
234 }
235 }
236
237 // Keyboard is pre-initialized from asciichat_shared_init()
238 // Only enable keyboard input if stdin is a TTY (avoid interfering with piped/redirected I/O)
239 g_keyboard_enabled = terminal_is_stdin_tty();
240
241 return 0;
242}
243
252 return session_display_has_tty(g_display_ctx);
253}
254
264 if (g_display_ctx && !should_exit()) {
265 session_display_reset(g_display_ctx);
266 }
267}
268
280
290 // Atomically check if this is the first frame and mark as not-first
291 // Avoids TOCTOU race where multiple threads could both think they're first
292 bool was_first_frame = atomic_exchange_bool(&connection->display_first_frame, false);
293
294 if (was_first_frame) {
295 // Disable terminal logging before clearing display and rendering first frame
297
298 // Signal the intro splash screen to stop - first frame is ready to render
300 }
301}
302
315void display_render_frame(client_connection_t *connection, const char *frame_data) {
316 if (!connection->display_context)
317 return;
318 if (!frame_data) {
319 SET_ERRNO(ERROR_INVALID_PARAM, "Attempted to render NULL frame data");
320 return;
321 }
322
323 // Stop splash screen animation on first frame
324 // This must be called BEFORE any frame rendering to prevent splash/frame flickering
326
327 // Help owns presentation while the connection owner continues polling input.
328 if (keyboard_help_is_active(connection->display_context)) {
330 return;
331 }
332 session_display_render_frame(connection->display_context, frame_data);
333}
334
336 if (!g_keyboard_enabled || !connection->display_context)
337 return;
338 keyboard_key_t key = ui_input_read_key(UI_SCREEN_MEDIA);
339 if (key != KEY_NONE)
341}
342
352 // Cleanup keyboard input if it was initialized
353 if (g_keyboard_enabled) {
356 g_keyboard_enabled = false;
357 }
358
359 // Cleanup optional local media capture context if it was created
360 if (g_display_capture_ctx) {
361 session_capture_destroy(g_display_capture_ctx);
362 g_display_capture_ctx = NULL;
363 }
364
365 // Display context is now managed by session_client_like framework
366 // Do not destroy it here - the framework will handle cleanup
367 g_display_ctx = NULL;
368}
#define SET_ERRNO(code,...)
void atomic_store_bool(atomic_t *a, bool value)
Atomically store a boolean value.
bool atomic_exchange_bool(atomic_t *a, bool new_value)
Atomically exchange a boolean and return the old value.
ascii-chat Client Media Capture Management Interface
ascii-chat Client Mode Entry Point Header
Named object registry for debugging — log identifiable resource names.
void display_set_context(session_display_ctx_t *display_ctx)
Set the display context created by session_client_like framework.
void display_reset_for_new_connection(client_connection_t *connection)
Reset display state for new connection.
void display_disable_logging_for_first_frame(client_connection_t *connection)
Disable terminal logging for first frame.
bool display_has_tty()
Check if display has TTY capability.
int display_init()
Initialize what is necessary to display ascii frames.
void display_full_reset()
Perform full display reset.
void display_render_frame(client_connection_t *connection, const char *frame_data)
Render ASCII frame to display.
void display_cleanup()
Cleanup display subsystem.
@ ERROR_DISPLAY
@ ERROR_INVALID_PARAM
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
void log_set_terminal_output(bool enabled)
Control stderr output to terminal.
@ MEDIA_SOURCE_STDIN
Piped or redirected input.
Definition source.h:104
@ MEDIA_SOURCE_FILE
Media file (video/audio).
Definition source.h:103
#define GET_OPTION(field)
Definition options.h:1333
session_display_ctx_t * session_display_create(const session_display_config_t *config)
Create a new session display context.
bool keyboard_help_is_active(session_display_ctx_t *ctx)
Check if keyboard help is currently active.
struct session_display_ctx session_display_ctx_t
session display config is intentionally a small, concrete record. Fields such as snapshot_mode,...
int splash_intro_done(void)
Signal that intro splash should end (first frame ready to render).
Definition splash.c:421
bool session_display_has_tty(session_display_ctx_t *ctx)
Check if display has a TTY (terminal) available.
void keyboard_help_render(session_display_ctx_t *ctx)
Render keyboard help TUI overlay.
void session_handle_keyboard_input(session_capture_ctx_t *capture, session_display_ctx_t *display, keyboard_key_t key)
Handle keyboard input in a session.
struct session_capture_ctx session_capture_ctx_t
session capture config keeps the values that move through the subsystem: type, path,...
void session_display_render_frame(session_display_ctx_t *ctx, const char *frame_data)
Render an ASCII frame to the terminal.
session_capture_ctx_t * session_capture_create(const session_capture_config_t *config)
Legacy function - creates either mirror or network capture based on options.
void session_capture_destroy(session_capture_ctx_t *ctx)
Destroy session capture context and free resources.
void session_display_reset(session_display_ctx_t *ctx)
Reset terminal to default state.
⚙️ Common definitions, error codes, macros, and types shared throughout the application
🎮 Keyboard input handler for interactive session controls
🆘 Interactive keyboard help overlay for session keyboard shortcuts
bool should_exit(void)
Definition main.c:248
Global application exit and signal handling API.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
🎬 Unified media source abstraction for webcam, files, and stdin
Intro and status screen display for ascii-chat with animated rainbow effects.
struct client_connection client_connection_t
void display_poll_keyboard(client_connection_t *connection)
ascii-chat Client Display Management Interface
🖥️ Unified terminal display abstraction for session-based rendering
bool terminal_is_stdin_tty(void)
🔤 String Manipulation and Shell Escaping Utilities
session_capture_ctx_t * capture_context
session_display_ctx_t * display_context
Media-source and loader settings used to create a session capture context.
media_source_type_t type
Media source type (WEBCAM, FILE, STDIN, TEST).
const char * path
Device index (for webcam) or file path (for file sources).
uint32_t target_fps
Target frame rate in FPS (e.g., 60 for display, 144 for network).
double initial_seek_timestamp
Seek to this timestamp after opening media (0.0 = no seek).
session_capture_should_exit_fn should_exit_callback
Optional cancellation callback, also invoked by media loader threads. The callback and its data must ...
void * callback_data
Opaque data passed to should_exit_callback.
bool resize_for_network
Resize frames to network-optimal dimensions (MAX_FRAME_WIDTH x MAX_FRAME_HEIGHT).
bool loop
Enable loop playback for file sources.
Terminal-rendering and snapshot settings used to create a display context.
void ui_controller_remove(ui_screen_t screen)
@ UI_SCREEN_MEDIA
@ UI_SCREEN_HELP
keyboard_key_t ui_input_read_key(ui_screen_t screen)
Definition tui/input.c:346