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

Go to the source code of this file.

Data Structures

struct  session_protocol_media_t
struct  session_protocol_t
 session protocol media keeps the values that move through the subsystem: context, terminal_output, frame_workspace. Some members are pointers into caller- or subsystem-owned memory, so lifetime follows the API that populated them rather than the struct copy itself. More...

Typedefs

typedef asciichat_error_t(* session_protocol_send_settings_fn) (const options_t *values, uint32_t request, void *context)

Functions

void session_protocol_init (session_protocol_t *protocol, acip_transport_t *transport, session_protocol_media_t media, acip_client_callbacks_t control)
asciichat_error_t session_protocol_dispatch (session_protocol_t *protocol, packet_type_t type, const void *payload, size_t length)
void session_protocol_build_capabilities (unsigned short width, unsigned short height, terminal_capabilities_packet_t *packet)
asciichat_error_t session_protocol_update_settings (terminal_size_t *advertised, uint64_t *advertised_revision, session_protocol_send_settings_fn send, void *context)

Typedef Documentation

◆ session_protocol_send_settings_fn

typedef asciichat_error_t(* session_protocol_send_settings_fn) (const options_t *values, uint32_t request, void *context)

Definition at line 57 of file src/common/session/protocol.h.

Function Documentation

◆ session_protocol_build_capabilities()

void session_protocol_build_capabilities ( unsigned short width,
unsigned short height,
terminal_capabilities_packet_t * packet )

Definition at line 254 of file common/session/protocol.c.

254 {
255 const options_t *opts = options_get();
256 // Log the dimensions being sent to server (helps debug dimension mismatch issues)
257 log_debug("Sending terminal size to server: %ux%u (auto_width=%d, auto_height=%d)", width, height, opts->auto_width, opts->auto_height);
258
259 // Connection and transport availability is checked by threaded_send_packet()
260
261 // Build terminal capabilities packet locally
262 // Detect terminal capabilities automatically
263 terminal_capabilities_t caps = detect_terminal_capabilities(options_terminal_settings());
264
265 // Set wants_padding based on snapshot mode and TTY status
266 // Disable padding when:
267 // - In snapshot mode (one frame and exit)
268 // - When stdout is not a TTY (piped/redirected output)
269 // Enable padding for interactive terminal sessions
270 bool is_snapshot_mode = opts->snapshot_mode;
271 bool is_interactive = terminal_is_interactive();
272 caps.wants_padding = is_interactive && !is_snapshot_mode;
273
274 log_debug("Client capabilities: wants_padding=%d (snapshot=%d, interactive=%d, stdin_tty=%d, stdout_tty=%d)", caps.wants_padding, is_snapshot_mode, is_interactive, terminal_is_stdin_tty(),
276
277 // Apply user's color mode override
279
280 // TERM_COLOR_AUTO is an option/configuration sentinel, not a wire-level
281 // capability. A remote peer must receive a concrete color level; this can
282 // otherwise make the server reject the complete capabilities packet. The
283 // browser/native renderer contract supports truecolor as the safe default.
284 terminal_color_mode_t normalized_color = acip_render_settings_normalize_color_mode(caps.color_level);
285 if (normalized_color != caps.color_level) {
286 log_debug("Terminal color detection returned AUTO; advertising truecolor");
287 caps.color_level = normalized_color;
288 caps.color_count = 16777216;
289 caps.capabilities |= TERM_CAP_COLOR_16 | TERM_CAP_COLOR_256 | TERM_CAP_COLOR_TRUE;
290 }
291
292 // Check if detection was reliable, use fallback only for auto-detection
293 if (!caps.detection_reliable && opts->color_mode == COLOR_MODE_AUTO) {
294 log_warn("Terminal capability detection not reliable, using fallback");
295 SAFE_MEMSET(&caps, sizeof(caps), 0, sizeof(caps));
296 caps.color_level = TERM_COLOR_NONE;
297 caps.color_count = 2;
298 caps.capabilities = 0;
299 SAFE_STRNCPY(caps.term_type, "unknown", sizeof(caps.term_type));
300 SAFE_STRNCPY(caps.colorterm, "", sizeof(caps.colorterm));
301 caps.detection_reliable = 0;
302 // Preserve wants_padding even in fallback mode
303 caps.wants_padding = is_interactive && !is_snapshot_mode;
304 }
305
306 // Convert to network packet format with proper byte order
307 terminal_capabilities_packet_t net_packet = {0};
308 if (opts->matrix_rain)
309 caps.capabilities |= TERM_CAP_MATRIX_RAIN;
310 else
311 caps.capabilities &= ~(uint32_t)TERM_CAP_MATRIX_RAIN;
312 if (opts->renderer == ASCII_RENDERER_GLYPH)
313 caps.capabilities |= TERM_CAP_GLYPH_RENDERER;
314 if (opts->palette_explicit)
315 caps.capabilities |= TERM_CAP_EXPLICIT_PALETTE;
316 if (opts->stabilize)
317 caps.capabilities |= TERM_CAP_STABILIZE;
318 net_packet.capabilities = HOST_TO_NET_U32(caps.capabilities);
319 net_packet.color_level = HOST_TO_NET_U32(caps.color_level);
320 net_packet.color_count = HOST_TO_NET_U32(caps.color_count);
321 net_packet.render_mode = HOST_TO_NET_U32(opts->render_mode);
322 net_packet.color_filter = (uint8_t)opts->color_filter;
323 net_packet.width = HOST_TO_NET_U16(width);
324 net_packet.height = HOST_TO_NET_U16(height);
325 net_packet.palette_type = HOST_TO_NET_U32(opts->palette_type);
326 net_packet.utf8_support = HOST_TO_NET_U32(caps.utf8_support ? 1 : 0);
327
328 if (opts->palette_type == PALETTE_CUSTOM && opts->palette_custom_set) {
329 const char *palette_custom = opts && opts->palette_custom_set ? opts->palette_custom : "";
330 SAFE_STRNCPY(net_packet.palette_custom, palette_custom, sizeof(net_packet.palette_custom));
331 net_packet.palette_custom[sizeof(net_packet.palette_custom) - 1] = '\0';
332 } else {
333 SAFE_MEMSET(net_packet.palette_custom, sizeof(net_packet.palette_custom), 0, sizeof(net_packet.palette_custom));
334 }
335
336 // Set desired FPS
337 int fps = opts->fps;
338 if (fps > 0) {
339 net_packet.desired_fps = (uint8_t)(fps > 144 ? 144 : fps);
340 } else {
341 net_packet.desired_fps = caps.desired_fps;
342 }
343
344 if (net_packet.desired_fps == 0) {
345 net_packet.desired_fps = DEFAULT_MAX_FPS;
346 }
347
348 SAFE_STRNCPY(net_packet.term_type, caps.term_type, sizeof(net_packet.term_type));
349 net_packet.term_type[sizeof(net_packet.term_type) - 1] = '\0';
350
351 SAFE_STRNCPY(net_packet.colorterm, caps.colorterm, sizeof(net_packet.colorterm));
352 net_packet.colorterm[sizeof(net_packet.colorterm) - 1] = '\0';
353
354 net_packet.detection_reliable = caps.detection_reliable;
355 // Send UTF-8 support flag: true for AUTO (default) and TRUE settings, false for FALSE setting
356 net_packet.utf8_support = (opts->force_utf8 != UTF8_SETTING_FALSE) ? 1 : 0;
357
358 // Set wants_padding flag (1=padding enabled, 0=no padding for snapshot/piped modes)
359 net_packet.wants_padding = caps.wants_padding ? 1 : 0;
360
361 // Set codec capabilities (client supports all codecs by default)
364
365 *packet = net_packet;
366}
#define AUDIO_CODEC_CAP_ALL
Definition codecs.h:89
#define HOST_TO_NET_U16(val)
Definition endian.h:100
#define HOST_TO_NET_U32(val)
Definition endian.h:70
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_MEMSET(dest, dest_size, ch, count)
unsigned char uint8_t
#define DEFAULT_MAX_FPS
Default maximum frame rate (frames per second).
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define VIDEO_CODEC_CAP_ALL
Default video codec capabilities (all codecs supported).
Definition codecs.h:56
#define COLOR_MODE_AUTO
Backward compatibility aliases for color mode enum values.
Definition options.h:223
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
const options_t * options_get(void)
Get pointer to current options struct (lock-free, thread-safe).
Definition rcu.c:513
#define options_terminal_settings()
Definition options.h:1287
@ UTF8_SETTING_FALSE
Force UTF-8 OFF: disable UTF-8 support.
Definition options.h:269
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:110
terminal_color_mode_t acip_render_settings_normalize_color_mode(terminal_color_mode_t mode)
terminal_capabilities_t apply_color_mode_override(terminal_capabilities_t caps, const terminal_options_t *)
terminal_capabilities_t detect_terminal_capabilities(const terminal_options_t *options)
bool terminal_is_interactive(void)
bool terminal_is_stdin_tty(void)
bool terminal_is_stdout_tty(void)
bool snapshot_mode
Snapshot mode (one frame and exit).
Definition options.h:1142
terminal_color_mode_t color_mode
Color mode (auto/none/16/256/truecolor).
Definition options.h:1084
int force_utf8
UTF-8 support setting (auto/true/false).
Definition options.h:1089
bool palette_explicit
Palette explicitly selected.
Definition options.h:1223
bool matrix_rain
Matrix digital rain effect (false = disabled).
Definition options.h:1145
bool auto_height
Auto-detect height from terminal.
Definition options.h:994
char palette_custom[256]
Custom palette characters.
Definition options.h:1221
bool palette_custom_set
True if custom palette was set.
Definition options.h:1222
terminal_render_mode_t render_mode
Render mode (foreground/background/half-block).
Definition options.h:1087
bool stabilize
Reduce frame-to-frame character flicker.
Definition options.h:1131
color_filter_t color_filter
Monochromatic color filter (none/black/white/green/etc).
Definition options.h:1085
int fps
Target framerate (0 = auto, 1-144).
Definition options.h:1091
ascii_renderer_t renderer
Character selection algorithm.
Definition options.h:1133
palette_type_t palette_type
Selected palette type.
Definition options.h:1220
bool auto_width
Auto-detect width from terminal.
Definition options.h:993
Terminal capabilities packet structure (Packet Type 5).
Definition packet.h:1097
uint32_t codec_capabilities_video
Video codec capabilities bitmask (VIDEO_CODEC_CAP_*).
Definition packet.h:1129
uint32_t capabilities
Terminal capabilities bitmask (TERM_CAP_* flags).
Definition packet.h:1099
uint32_t color_level
Color level enum value (terminal_color_mode_t).
Definition packet.h:1101
uint32_t color_count
Actual color count (16, 256, or 16777216).
Definition packet.h:1103
uint16_t height
Terminal height in characters.
Definition packet.h:1109
uint8_t wants_padding
Whether client wants frame padding (1=padding enabled, 0=no padding for snapshot/piped modes).
Definition packet.h:1127
uint32_t render_mode
Render mode enum value (foreground/background/half-block).
Definition packet.h:1105
uint16_t width
Terminal width in characters.
Definition packet.h:1107
uint8_t desired_fps
Client's desired frame rate (1-144 FPS).
Definition packet.h:1123
uint8_t detection_reliable
Detection reliability flag (1=reliable detection, 0=best guess).
Definition packet.h:1115
uint32_t codec_capabilities_audio
Audio codec capabilities bitmask (AUDIO_CODEC_CAP_*).
Definition packet.h:1131
char palette_custom[64]
Custom palette characters (if palette_type == PALETTE_CUSTOM).
Definition packet.h:1121
char colorterm[32]
$COLORTERM environment variable value (for debugging)
Definition packet.h:1113
uint32_t palette_type
Palette type enum value (palette_type_t).
Definition packet.h:1119
uint8_t color_filter
Monochromatic color filter enum value (color_filter_t).
Definition packet.h:1125
uint32_t utf8_support
UTF-8 support flag (0=no UTF-8, 1=UTF-8 supported).
Definition packet.h:1117
char term_type[32]
$TERM environment variable value (for debugging)
Definition packet.h:1111
@ TERM_CAP_STABILIZE
@ TERM_CAP_EXPLICIT_PALETTE
@ TERM_CAP_GLYPH_RENDERER
@ ASCII_RENDERER_GLYPH

References acip_render_settings_normalize_color_mode(), apply_color_mode_override(), ASCII_RENDERER_GLYPH, AUDIO_CODEC_CAP_ALL, options_state::auto_height, options_state::auto_width, terminal_capabilities_packet_t::capabilities, terminal_capabilities_packet_t::codec_capabilities_audio, terminal_capabilities_packet_t::codec_capabilities_video, terminal_capabilities_packet_t::color_count, options_state::color_filter, terminal_capabilities_packet_t::color_filter, terminal_capabilities_packet_t::color_level, options_state::color_mode, COLOR_MODE_AUTO, terminal_capabilities_packet_t::colorterm, DEFAULT_MAX_FPS, terminal_capabilities_packet_t::desired_fps, detect_terminal_capabilities(), terminal_capabilities_packet_t::detection_reliable, options_state::force_utf8, options_state::fps, height, terminal_capabilities_packet_t::height, HOST_TO_NET_U16, HOST_TO_NET_U32, log_debug, log_warn, options_state::matrix_rain, options_get(), options_terminal_settings, PALETTE_CUSTOM, options_state::palette_custom, terminal_capabilities_packet_t::palette_custom, options_state::palette_custom_set, options_state::palette_explicit, options_state::palette_type, terminal_capabilities_packet_t::palette_type, options_state::render_mode, terminal_capabilities_packet_t::render_mode, options_state::renderer, SAFE_MEMSET, SAFE_STRNCPY, options_state::snapshot_mode, options_state::stabilize, TERM_CAP_EXPLICIT_PALETTE, TERM_CAP_GLYPH_RENDERER, TERM_CAP_STABILIZE, terminal_capabilities_packet_t::term_type, terminal_is_interactive(), terminal_is_stdin_tty(), terminal_is_stdout_tty(), UTF8_SETTING_FALSE, terminal_capabilities_packet_t::utf8_support, VIDEO_CODEC_CAP_ALL, terminal_capabilities_packet_t::wants_padding, terminal_capabilities_packet_t::width, and width.

Referenced by threaded_send_terminal_size_with_auto_detect().

◆ session_protocol_dispatch()

asciichat_error_t session_protocol_dispatch ( session_protocol_t * protocol,
packet_type_t type,
const void * payload,
size_t length )

Definition at line 196 of file common/session/protocol.c.

196 {
197 if (!protocol || (!payload && length))
198 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid participant protocol packet");
199 protocol->callback_result = ASCIICHAT_OK;
200 if (type == PACKET_TYPE_ACIP_SETTINGS_ACK) {
202 const char *json;
203 size_t json_length;
204 asciichat_error_t result = acip_settings_decode(payload, length, &header, &json, &json_length);
205 if (result != ASCIICHAT_OK)
206 return result;
207 if ((header.request && header.request < protocol->settings_request) || header.revision < protocol->settings_revision)
208 return ASCIICHAT_OK;
209 options_t *current = SAFE_MALLOC(sizeof(*current), options_t *), *candidate = SAFE_MALLOC(sizeof(*candidate), options_t *);
211 result = options_prepare_json(current, json, json_length, candidate, true);
212 if (result == ASCIICHAT_OK)
213 result = acip_render_settings_prepare(&protocol->effective_render_settings, candidate);
214 SAFE_FREE(current);
215 SAFE_FREE(candidate);
216 if (result != ASCIICHAT_OK)
217 return result;
218 protocol->effective_render_settings.revision = header.revision;
219 if (header.request)
220 protocol->settings_request = header.request;
221 protocol->settings_revision = header.revision;
222 protocol->settings_status = header.status;
223 if (header.status)
224 log_warn("Remote rendering update rejected (request=%u, status=%u)", header.request, header.status);
225 else
226 log_info("Remote rendering update accepted (request=%u, revision=%u)", header.request, header.revision);
227 return ASCIICHAT_OK;
228 }
230 .on_ascii_frame = receive_frame, .on_audio_batch = receive_samples, .on_audio_opus_batch = receive_opus_batch, .on_server_state = receive_state, .on_error = receive_error, .app_ctx = protocol};
231 bool audio = type == PACKET_TYPE_AUDIO_BATCH || type == PACKET_TYPE_AUDIO_OPUS_BATCH;
232 bool common = audio || type == PACKET_TYPE_ASCII_FRAME || type == PACKET_TYPE_SERVER_STATE || type == PACKET_TYPE_ERROR_MESSAGE;
233 asciichat_error_t result;
234 if (type == PACKET_TYPE_PING && !protocol->control.on_ping)
235 result = acip_send_pong(protocol->transport);
236 else {
237 acip_client_session_t dispatch = protocol->client_session;
238 dispatch.callbacks = common ? media : protocol->control;
239 result = acip_client_session_dispatch(&dispatch, type, payload, length);
240 }
241 if (result == ASCIICHAT_OK)
242 result = protocol->callback_result;
243 if (result == ASCIICHAT_OK) {
244 protocol->packets_received++;
245 if (audio) {
246 protocol->audio_packets_received++;
247 if (protocol->media.on_audio_packet)
248 protocol->media.on_audio_packet(length, protocol->media.context);
249 }
250 }
251 return result;
252}
#define SET_ERRNO(code,...)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
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
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
@ PACKET_TYPE_AUDIO_OPUS_BATCH
Batched Opus-encoded audio frames.
Definition packet.h:412
@ PACKET_TYPE_SERVER_STATE
Server sends current state to clients.
Definition packet.h:435
@ PACKET_TYPE_ACIP_SETTINGS_ACK
Settings acknowledgment (Participant -> Initiator).
Definition packet.h:501
@ PACKET_TYPE_ASCII_FRAME
Complete ASCII frame with all metadata.
Definition packet.h:397
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:388
@ PACKET_TYPE_PING
Keepalive ping packet.
Definition packet.h:421
@ PACKET_TYPE_AUDIO_BATCH
Batched audio packets for efficiency.
Definition packet.h:410
asciichat_error_t acip_client_session_dispatch(acip_client_session_t *session, packet_type_t type, const void *payload, size_t length)
asciichat_error_t acip_render_settings_prepare(acip_render_settings_t *candidate, const options_t *values)
asciichat_error_t acip_settings_decode(const void *payload, size_t length, acip_settings_header_t *header, const char **json, size_t *json_length)
void acip_render_settings_options(const acip_render_settings_t *state, options_t *values)
asciichat_error_t options_prepare_json(const options_t *current, const char *json, size_t length, options_t *candidate, bool remote_only)
Definition rcu.c:1076
asciichat_error_t acip_send_pong(acip_transport_t *transport)
Send pong packet.
Definition send.c:382
Client-side packet handler callbacks.
Definition handlers.h:55
void(* on_ping)(void *ctx)
Called when ping received (should send pong).
Definition handlers.h:81
acip settings header keeps the values that move through the subsystem: schema, request,...
void(* on_audio_packet)(size_t length, void *context)
acip_render_settings_t effective_render_settings
session_protocol_media_t media
acip_client_session_t client_session
acip_client_callbacks_t control

References acip_client_session_dispatch(), acip_render_settings_options(), acip_render_settings_prepare(), acip_send_pong(), acip_settings_decode(), ASCIICHAT_OK, session_protocol_t::audio_packets_received, session_protocol_t::callback_result, acip_client_session_t::callbacks, session_protocol_t::client_session, session_protocol_media_t::context, session_protocol_t::control, session_protocol_t::effective_render_settings, ERROR_INVALID_PARAM, log_info, log_warn, session_protocol_t::media, session_protocol_media_t::on_audio_packet, acip_client_callbacks_t::on_ping, options_prepare_json(), PACKET_TYPE_ACIP_SETTINGS_ACK, PACKET_TYPE_ASCII_FRAME, PACKET_TYPE_AUDIO_BATCH, PACKET_TYPE_AUDIO_OPUS_BATCH, PACKET_TYPE_ERROR_MESSAGE, PACKET_TYPE_PING, PACKET_TYPE_SERVER_STATE, session_protocol_t::packets_received, acip_render_settings_t::revision, SAFE_FREE, SAFE_MALLOC, SET_ERRNO, session_protocol_t::settings_request, session_protocol_t::settings_revision, session_protocol_t::settings_status, and session_protocol_t::transport.

◆ session_protocol_init()

void session_protocol_init ( session_protocol_t * protocol,
acip_transport_t * transport,
session_protocol_media_t media,
acip_client_callbacks_t control )

Definition at line 190 of file common/session/protocol.c.

190 {
191 *protocol = (session_protocol_t){.transport = transport, .media = media, .control = control};
192 acip_client_session_init(&protocol->client_session, transport, &control);
193 protocol->effective_render_settings.caps.utf8_support = GET_OPTION(force_utf8) != UTF8_SETTING_FALSE;
194}
#define GET_OPTION(field)
Definition options.h:1339
void acip_client_session_init(acip_client_session_t *session, acip_transport_t *transport, const acip_client_callbacks_t *callbacks)
session protocol media keeps the values that move through the subsystem: context, terminal_output,...

References acip_client_session_init(), acip_render_settings_t::caps, session_protocol_t::client_session, session_protocol_t::effective_render_settings, GET_OPTION, and UTF8_SETTING_FALSE.

Referenced by client_protocol_start().

◆ session_protocol_update_settings()

asciichat_error_t session_protocol_update_settings ( terminal_size_t * advertised,
uint64_t * advertised_revision,
session_protocol_send_settings_fn send,
void * context )

Definition at line 368 of file common/session/protocol.c.

368 {
369 if (!advertised || !advertised_revision || !send)
370 return SET_ERRNO(ERROR_INVALID_PARAM, "Settings update requires state and a send callback");
371 if (advertised->cols > 0 && advertised->rows > 0 && *advertised_revision == options_revision())
372 return ASCIICHAT_OK;
374 asciichat_error_t result = options_read_snapshot(snapshot);
375 if (result != ASCIICHAT_OK) {
376 return result;
377 }
378 terminal_size_t size = {.cols = snapshot->values.width, .rows = snapshot->values.height};
379 if (size.cols <= 0 || size.rows <= 0) {
380 return ASCIICHAT_OK;
381 }
382 if (snapshot->values.color_mode == TERM_COLOR_AUTO) {
383 terminal_capabilities_t caps = detect_terminal_capabilities(options_terminal_settings());
384 snapshot->values.color_mode = caps.detection_reliable ? caps.color_level : TERM_COLOR_NONE;
385 }
386 result = send(&snapshot->values, (uint32_t)snapshot->revision, context);
387 if (result == ASCIICHAT_OK) {
388 *advertised = size;
389 *advertised_revision = snapshot->revision;
390 }
391
392 return result;
393}
#define SAFE_MALLOC_DEFER_FREE(name, size, cast)
uint64_t options_revision(void)
Definition rcu.c:945
asciichat_error_t options_read_snapshot(options_snapshot_t *snapshot)
Definition rcu.c:934

References ASCIICHAT_OK, detect_terminal_capabilities(), ERROR_INVALID_PARAM, options_read_snapshot(), options_revision(), options_terminal_settings, SAFE_MALLOC_DEFER_FREE, and SET_ERRNO.