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/network/acip/settings.c
Go to the documentation of this file.
7#include <ascii-chat/common.h>
8#include <string.h>
9
11 *values = options_t_new();
12 values->width = state->width;
13 values->height = state->height;
14 values->fps = state->caps.desired_fps;
15 values->color_mode = (terminal_color_mode_t)state->caps.color_level;
16 values->color_filter = state->caps.color_filter;
17 values->render_mode = state->caps.render_mode;
18 values->renderer = state->caps.text_renderer;
19 values->stabilize = state->caps.stabilize;
20 values->palette_explicit = state->caps.palette_explicit;
21 values->palette_type = state->caps.palette_type ? state->caps.palette_type : PALETTE_STANDARD;
22 SAFE_STRNCPY(values->palette_custom, state->caps.palette_custom, sizeof(values->palette_custom));
23 values->palette_custom_set = values->palette_custom[0] != 0;
24 values->matrix_rain = (state->caps.capabilities & TERM_CAP_MATRIX_RAIN) != 0;
25}
26
27terminal_color_mode_t acip_render_settings_normalize_color_mode(terminal_color_mode_t mode) {
28 return mode == TERM_COLOR_AUTO ? TERM_COLOR_TRUECOLOR : mode;
29}
30
32 if (!candidate || !values || values->width < 1 || values->width > 4096 || values->height < 1 || values->height > 4096 || (uint64_t)values->width * values->height > 1048576 || values->fps < 0 ||
33 values->fps > 144 || values->color_mode < TERM_COLOR_NONE || values->color_mode > TERM_COLOR_TRUECOLOR || values->color_filter < 0 || values->color_filter >= COLOR_FILTER_COUNT ||
34 values->render_mode < 0 || values->render_mode > RENDER_MODE_HALF_BLOCK || values->palette_type < PALETTE_STANDARD || values->palette_type >= PALETTE_COUNT ||
35 strlen(values->palette_custom) >= sizeof(candidate->caps.palette_custom))
36 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid remote render settings (size=%dx%d fps=%d color=%d filter=%d mode=%d palette=%d custom=%zu)", values->width, values->height, values->fps,
37 values->color_mode, values->color_filter, values->render_mode, values->palette_type, strlen(values->palette_custom));
38 acip_render_settings_t next = *candidate;
39 next.width = (uint16_t)values->width;
40 next.height = (uint16_t)values->height;
41 next.caps.desired_fps = candidate->fps_limit && values->fps > candidate->fps_limit ? candidate->fps_limit : values->fps;
42 next.caps.color_level = values->color_mode;
43 next.caps.color_count = values->color_mode == TERM_COLOR_TRUECOLOR ? 16777216 : values->color_mode == TERM_COLOR_256 ? 256 : values->color_mode == TERM_COLOR_16 ? 16 : 2;
44 next.caps.color_filter = values->color_filter;
45 next.caps.render_mode = values->render_mode;
46 next.caps.text_renderer = values->renderer;
47 next.caps.stabilize = values->stabilize;
48 next.caps.palette_explicit = values->palette_explicit;
49 next.caps.wants_background = values->render_mode == RENDER_MODE_BACKGROUND;
50 next.caps.palette_type = values->palette_type;
51 SAFE_STRNCPY(next.caps.palette_custom, values->palette_custom, sizeof(next.caps.palette_custom));
52 if (values->matrix_rain)
53 next.caps.capabilities |= TERM_CAP_MATRIX_RAIN;
54 else
55 next.caps.capabilities &= ~(uint32_t)TERM_CAP_MATRIX_RAIN;
56 if (initialize_client_palette(next.caps.palette_type, values->palette_custom, next.palette, &next.palette_length, next.luminance))
57 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid rendering palette");
59 return SET_ERRNO(ERROR_INVALID_PARAM, "Glyph palette contains characters without glyph masks");
60 if (values->renderer == ASCII_RENDERER_GLYPH && !values->palette_explicit) {
61 SAFE_STRNCPY(next.palette, " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", sizeof(next.palette));
62 next.palette_length = strlen(next.palette);
63 }
64 next.revision = candidate->revision + 1;
65 *candidate = next;
66 return ASCIICHAT_OK;
67}
68
69static asciichat_error_t prepare_for_viewer(acip_render_settings_t *state, options_t *values) {
70 if (values->palette_type < PALETTE_STANDARD || values->palette_type >= PALETTE_COUNT)
71 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid rendering palette");
72 if (!state->caps.utf8_support && values->render_mode == RENDER_MODE_HALF_BLOCK)
73 values->render_mode = RENDER_MODE_FOREGROUND;
74 values->palette_type = select_compatible_palette(values->palette_type, state->caps.utf8_support);
75 if (values->palette_type == PALETTE_CUSTOM && !state->caps.utf8_support && palette_requires_utf8_encoding(values->palette_custom, strlen(values->palette_custom)))
76 return SET_ERRNO(ERROR_INVALID_PARAM, "Custom palette requires UTF-8 support");
77 return acip_render_settings_prepare(state, values);
78}
79
81 if (!state || !packet || !memchr(packet->palette_custom, 0, sizeof(packet->palette_custom)))
82 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid render capabilities");
83 options_t *values = SAFE_MALLOC(sizeof(*values), options_t *);
84 acip_render_settings_options(state, values);
85 values->width = NET_TO_HOST_U16(packet->width);
86 values->height = NET_TO_HOST_U16(packet->height);
87 values->fps = packet->desired_fps;
88 values->color_mode = NET_TO_HOST_U32(packet->color_level);
89 values->color_filter = packet->color_filter;
90 values->render_mode = NET_TO_HOST_U32(packet->render_mode);
91 values->palette_type = NET_TO_HOST_U32(packet->palette_type);
92 // AUTO is a local option sentinel, but older/native/browser senders may
93 // still advertise it. Resolve it at the shared wire boundary so the peer
94 // cannot reject an otherwise valid capabilities packet.
96 if (values->palette_type == PALETTE_UNSET)
98 SAFE_STRNCPY(values->palette_custom, packet->palette_custom, sizeof(values->palette_custom));
99 values->matrix_rain = (NET_TO_HOST_U32(packet->capabilities) & TERM_CAP_MATRIX_RAIN) != 0;
100 uint32_t flags = NET_TO_HOST_U32(packet->capabilities);
102 values->palette_explicit = (flags & TERM_CAP_EXPLICIT_PALETTE) != 0;
103 values->stabilize = (flags & TERM_CAP_STABILIZE) != 0;
104 acip_render_settings_t next = *state;
105 next.caps.capabilities = NET_TO_HOST_U32(packet->capabilities);
106 next.caps.utf8_support = NET_TO_HOST_U32(packet->utf8_support) != 0;
107 next.caps.wants_padding = packet->wants_padding != 0;
108 asciichat_error_t result = prepare_for_viewer(&next, values);
109 if (result == ASCIICHAT_OK)
110 *state = next;
111 SAFE_FREE(values);
112 return result;
113}
114
115asciichat_error_t acip_settings_decode(const void *payload, size_t length, acip_settings_header_t *header, const char **json, size_t *json_length) {
116 if (!payload || !header || !json || !json_length || length < ACIP_SETTINGS_HEADER_SIZE || length > ACIP_SETTINGS_MAX_SIZE)
117 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid settings packet length");
118 uint32_t fields[5];
119 memcpy(fields, payload, sizeof(fields));
120 *header = (acip_settings_header_t){NET_TO_HOST_U32(fields[0]), NET_TO_HOST_U32(fields[1]), NET_TO_HOST_U32(fields[2]), NET_TO_HOST_U32(fields[3]), NET_TO_HOST_U32(fields[4])};
121 if (header->schema != ACIP_SETTINGS_SCHEMA)
122 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Unsupported settings schema");
123 *json = (const char *)payload + ACIP_SETTINGS_HEADER_SIZE;
124 *json_length = length - ACIP_SETTINGS_HEADER_SIZE;
125 return ASCIICHAT_OK;
126}
127
128asciichat_error_t acip_settings_encode(const acip_settings_header_t *header, const char *json, uint8_t *buffer, size_t capacity, size_t *length) {
129 if (!header || !json || !buffer || !length)
130 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing settings encoding argument");
131 size_t size = strlen(json);
132 if (size > 4096 || capacity < ACIP_SETTINGS_HEADER_SIZE + size)
133 return SET_ERRNO(ERROR_INVALID_PARAM, "Settings output too small");
134 uint32_t fields[5] = {HOST_TO_NET_U32(header->schema), HOST_TO_NET_U32(header->request), HOST_TO_NET_U32(header->expected_revision), HOST_TO_NET_U32(header->revision),
135 HOST_TO_NET_U32(header->status)};
136 memcpy(buffer, fields, sizeof(fields));
137 memcpy(buffer + sizeof(fields), json, size);
138 *length = sizeof(fields) + size;
139 return ASCIICHAT_OK;
140}
141
142asciichat_error_t acip_settings_request(acip_render_settings_t *state, const void *payload, size_t length, uint8_t *reply, size_t capacity, size_t *reply_length) {
143 acip_settings_header_t header = {0};
144 const char *json = NULL;
145 size_t json_length = 0;
146 asciichat_error_t result = acip_settings_decode(payload, length, &header, &json, &json_length);
147 if (result != ASCIICHAT_OK)
148 return result;
150 options_t *current = SAFE_MALLOC(sizeof(*current), options_t *), *candidate = SAFE_MALLOC(sizeof(*candidate), options_t *);
151 acip_render_settings_options(state, current);
152 if (header.status)
153 result = SET_ERRNO(ERROR_INVALID_PARAM, "Unexpected settings request status");
154 else if (header.expected_revision != UINT32_MAX && header.expected_revision != state->revision)
155 result = SET_ERRNO(ERROR_INVALID_STATE, "Remote settings changed; refresh and retry");
156 else if (json_length) {
157 result = options_prepare_json(current, json, json_length, candidate, true);
158 if (result == ASCIICHAT_OK) {
159 // AUTO is a local option sentinel and is not valid in the negotiated
160 // render state. Keep legacy settings updates from invalidating a live
161 // session when they serialize the raw AUTO option.
162 candidate->color_mode = acip_render_settings_normalize_color_mode(candidate->color_mode);
163 result = prepare_for_viewer(state, candidate);
164 }
165 }
166 header.status = result;
167 header.revision = state->revision;
168 // The rejection is represented by the response status, preserving older errors.
170 acip_render_settings_options(state, current);
171 char *effective = options_values_json(current, true);
172 asciichat_error_t encoded = effective ? acip_settings_encode(&header, effective, reply, capacity, reply_length) : ERROR_MEMORY;
173 SAFE_FREE(effective);
174 SAFE_FREE(candidate);
175 SAFE_FREE(current);
176 return encoded;
177}
178
179asciichat_error_t acip_settings_send(acip_transport_t *transport, const options_t *values, uint32_t request, uint32_t expected_revision) {
180 char *json = options_values_json(values, true);
181 if (!json)
182 return SET_ERRNO(ERROR_MEMORY, "Cannot encode render preferences");
184 size_t length = 0;
185 acip_settings_header_t header = {.schema = ACIP_SETTINGS_SCHEMA, .request = request, .expected_revision = expected_revision};
186 asciichat_error_t result = acip_settings_encode(&header, json, buffer, sizeof(buffer), &length);
187 if (result == ASCIICHAT_OK)
188 result = packet_send_via_transport(transport, PACKET_TYPE_ACIP_SETTINGS_SYNC, buffer, length, 0);
189 SAFE_FREE(json);
190 return result;
191}
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
#define SET_ERRNO(code,...)
@ ASCIICHAT_ERRNO_HANDLED
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U32(val)
Definition endian.h:70
#define NET_TO_HOST_U16(val)
Definition endian.h:115
#define NET_TO_HOST_U32(val)
Definition endian.h:85
unsigned short uint16_t
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
unsigned long long uint64_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:74
@ PACKET_TYPE_ACIP_SETTINGS_SYNC
Settings sync (Initiator -> Host -> All Participants).
Definition packet.h:499
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
options_t options_t_new(void)
Initialize options by parsing command-line arguments.
Definition options.c:688
bool palette_requires_utf8_encoding(const char *chars, size_t len)
Check if palette requires UTF-8 encoding.
Definition palette.c:53
palette_type_t select_compatible_palette(palette_type_t requested, bool client_utf8)
Select compatible palette based on client capabilities.
Definition palette.c:212
int initialize_client_palette(palette_type_t palette_type, const char *custom_chars, char client_palette_chars[ASCII_PALETTE_BUFFER_SIZE], size_t *client_palette_len, char client_luminance_palette[ASCII_LUMINANCE_LEVELS])
Initialize client palette with full configuration.
Definition palette.c:292
@ PALETTE_COUNT
Number of palette types (not a valid palette).
Definition palette.h:112
@ PALETTE_CUSTOM
User-defined via –palette-chars.
Definition palette.h:110
@ PALETTE_STANDARD
Standard ASCII palette: " ...',;:clodxkO0KXNWM".
Definition palette.h:100
@ PALETTE_UNSET
Unset palette type (will be resolved to default or custom).
Definition palette.h:98
struct acip_transport acip_transport_t
Forward declaration of ACIP transport.
Definition host.h:508
⚙️ Common definitions, error codes, macros, and types shared throughout the application
asciichat_error_t acip_settings_send(acip_transport_t *transport, const options_t *values, uint32_t request, uint32_t expected_revision)
asciichat_error_t acip_render_settings_capabilities(acip_render_settings_t *state, const terminal_capabilities_packet_t *packet)
asciichat_error_t acip_settings_encode(const acip_settings_header_t *header, const char *json, uint8_t *buffer, size_t capacity, size_t *length)
asciichat_error_t acip_render_settings_prepare(acip_render_settings_t *candidate, const options_t *values)
terminal_color_mode_t acip_render_settings_normalize_color_mode(terminal_color_mode_t mode)
asciichat_error_t acip_settings_request(acip_render_settings_t *state, const void *payload, size_t length, uint8_t *reply, size_t capacity, size_t *reply_length)
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)
bool ascii_render_palette_supported(ascii_renderer_t kind, const char *palette)
ASCII Palette Management for Video-to-ASCII Conversion.
char * options_values_json(const options_t *values, bool remote_only)
Definition rcu.c:1110
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 packet_send_via_transport(acip_transport_t *transport, packet_type_t type, const void *payload, size_t payload_len, uint32_t client_id)
Definition send.c:183
ACIP shared/bidirectional packet sending functions.
🔤 String Manipulation and Shell Escaping Utilities
acip render settings keeps the values that move through the subsystem: caps, width,...
acip settings header keeps the values that move through the subsystem: schema, request,...
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...
terminal_color_mode_t color_mode
Color mode (auto/none/16/256/truecolor).
Definition options.h:1084
bool palette_explicit
Palette explicitly selected.
Definition options.h:1223
int height
Terminal height in characters (int for OPTION_TYPE_INT compat).
Definition options.h:992
bool matrix_rain
Matrix digital rain effect (false = disabled).
Definition options.h:1145
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
int width
Terminal width in characters (int for OPTION_TYPE_INT compat).
Definition options.h:991
palette_type_t palette_type
Selected palette type.
Definition options.h:1220
Terminal capabilities packet structure (Packet Type 5).
Definition packet.h:1097
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
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
char palette_custom[64]
Custom palette characters (if palette_type == PALETTE_CUSTOM).
Definition packet.h:1121
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
@ TERM_CAP_STABILIZE
@ TERM_CAP_EXPLICIT_PALETTE
@ TERM_CAP_GLYPH_RENDERER
@ ASCII_RENDERER_GLYPH
@ ASCII_RENDERER_LUMINANCE