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

Go to the source code of this file.

Functions

void acip_render_settings_options (const acip_render_settings_t *state, options_t *values)
terminal_color_mode_t acip_render_settings_normalize_color_mode (terminal_color_mode_t mode)
asciichat_error_t acip_render_settings_prepare (acip_render_settings_t *candidate, const options_t *values)
asciichat_error_t acip_render_settings_capabilities (acip_render_settings_t *state, const terminal_capabilities_packet_t *packet)
asciichat_error_t acip_settings_decode (const void *payload, size_t length, acip_settings_header_t *header, const char **json, size_t *json_length)
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_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_send (acip_transport_t *transport, const options_t *values, uint32_t request, uint32_t expected_revision)

Function Documentation

◆ acip_render_settings_capabilities()

asciichat_error_t acip_render_settings_capabilities ( acip_render_settings_t * state,
const terminal_capabilities_packet_t * packet )

Definition at line 80 of file lib/network/acip/settings.c.

80 {
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}
#define SET_ERRNO(code,...)
#define NET_TO_HOST_U16(val)
Definition endian.h:115
#define NET_TO_HOST_U32(val)
Definition endian.h:85
unsigned int uint32_t
#define SAFE_STRNCPY(dst, src, size)
#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
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
@ 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
terminal_color_mode_t acip_render_settings_normalize_color_mode(terminal_color_mode_t mode)
void acip_render_settings_options(const acip_render_settings_t *state, options_t *values)
acip render settings keeps the values that move through the subsystem: caps, width,...
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
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
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

References acip_render_settings_normalize_color_mode(), acip_render_settings_options(), ASCII_RENDERER_GLYPH, ASCII_RENDERER_LUMINANCE, ASCIICHAT_OK, terminal_capabilities_packet_t::capabilities, acip_render_settings_t::caps, options_state::color_filter, terminal_capabilities_packet_t::color_filter, terminal_capabilities_packet_t::color_level, options_state::color_mode, terminal_capabilities_packet_t::desired_fps, ERROR_INVALID_PARAM, options_state::fps, options_state::height, terminal_capabilities_packet_t::height, options_state::matrix_rain, NET_TO_HOST_U16, NET_TO_HOST_U32, options_state::palette_custom, terminal_capabilities_packet_t::palette_custom, options_state::palette_explicit, PALETTE_STANDARD, options_state::palette_type, terminal_capabilities_packet_t::palette_type, PALETTE_UNSET, options_state::render_mode, terminal_capabilities_packet_t::render_mode, options_state::renderer, SAFE_FREE, SAFE_MALLOC, SAFE_STRNCPY, SET_ERRNO, options_state::stabilize, TERM_CAP_EXPLICIT_PALETTE, TERM_CAP_GLYPH_RENDERER, TERM_CAP_STABILIZE, terminal_capabilities_packet_t::utf8_support, terminal_capabilities_packet_t::wants_padding, options_state::width, and terminal_capabilities_packet_t::width.

Referenced by handle_client_capabilities_packet(), and session_host_receive_packet().

◆ acip_render_settings_normalize_color_mode()

terminal_color_mode_t acip_render_settings_normalize_color_mode ( terminal_color_mode_t mode)

Resolve local-only color sentinels before render settings cross the ACIP boundary.

Definition at line 27 of file lib/network/acip/settings.c.

27 {
28 return mode == TERM_COLOR_AUTO ? TERM_COLOR_TRUECOLOR : mode;
29}

Referenced by acip_render_settings_capabilities(), acip_settings_request(), and session_protocol_build_capabilities().

◆ acip_render_settings_options()

void acip_render_settings_options ( const acip_render_settings_t * state,
options_t * values )

Definition at line 10 of file lib/network/acip/settings.c.

10 {
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}
options_t options_t_new(void)
Initialize options by parsing command-line arguments.
Definition options.c:688
bool palette_custom_set
True if custom palette was set.
Definition options.h:1222

References acip_render_settings_t::caps, options_state::color_filter, options_state::color_mode, options_state::fps, acip_render_settings_t::height, options_state::height, options_state::matrix_rain, options_t_new(), options_state::palette_custom, options_state::palette_custom_set, options_state::palette_explicit, PALETTE_STANDARD, options_state::palette_type, options_state::render_mode, options_state::renderer, SAFE_STRNCPY, options_state::stabilize, acip_render_settings_t::width, and options_state::width.

Referenced by acip_render_settings_capabilities(), acip_settings_request(), browser_client_settings_receive(), browser_client_settings_values(), client_settings_receive(), client_settings_values(), and session_protocol_dispatch().

◆ acip_render_settings_prepare()

asciichat_error_t acip_render_settings_prepare ( acip_render_settings_t * candidate,
const options_t * values )

Definition at line 31 of file lib/network/acip/settings.c.

31 {
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}
unsigned short uint16_t
unsigned long long uint64_t
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
bool ascii_render_palette_supported(ascii_renderer_t kind, const char *palette)

References ascii_render_palette_supported(), ASCII_RENDERER_GLYPH, ASCIICHAT_OK, acip_render_settings_t::caps, options_state::color_filter, options_state::color_mode, ERROR_INVALID_PARAM, options_state::fps, acip_render_settings_t::fps_limit, acip_render_settings_t::height, options_state::height, initialize_client_palette(), acip_render_settings_t::luminance, options_state::matrix_rain, acip_render_settings_t::palette, PALETTE_COUNT, options_state::palette_custom, options_state::palette_explicit, acip_render_settings_t::palette_length, PALETTE_STANDARD, options_state::palette_type, options_state::render_mode, options_state::renderer, acip_render_settings_t::revision, SAFE_STRNCPY, SET_ERRNO, options_state::stabilize, acip_render_settings_t::width, and options_state::width.

Referenced by browser_client_settings_receive(), client_settings_receive(), session_participant_request_settings(), and session_protocol_dispatch().

◆ acip_settings_decode()

asciichat_error_t acip_settings_decode ( const void * payload,
size_t length,
acip_settings_header_t * header,
const char ** json,
size_t * json_length )

Definition at line 115 of file lib/network/acip/settings.c.

115 {
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}
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:74
acip settings header keeps the values that move through the subsystem: schema, request,...

References ACIP_SETTINGS_HEADER_SIZE, ACIP_SETTINGS_MAX_SIZE, ACIP_SETTINGS_SCHEMA, ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_NOT_SUPPORTED, NET_TO_HOST_U32, and SET_ERRNO.

Referenced by acip_settings_request(), browser_client_settings_receive(), client_settings_receive(), session_participant_receive_settings(), and session_protocol_dispatch().

◆ acip_settings_encode()

asciichat_error_t acip_settings_encode ( const acip_settings_header_t * header,
const char * json,
uint8_t * buffer,
size_t capacity,
size_t * length )

Definition at line 128 of file lib/network/acip/settings.c.

128 {
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}
#define HOST_TO_NET_U32(val)
Definition endian.h:70

References ACIP_SETTINGS_HEADER_SIZE, ASCIICHAT_OK, ERROR_INVALID_PARAM, HOST_TO_NET_U32, and SET_ERRNO.

Referenced by acip_settings_request(), acip_settings_send(), handle_client_capabilities_packet(), and session_host_receive_packet().

◆ acip_settings_request()

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 )

A rejected request returns current effective values without changing state.

Definition at line 142 of file lib/network/acip/settings.c.

142 {
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}
void errno_scope_end(asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_scope_t errno_scope_begin(void)
@ ASCIICHAT_ERRNO_HANDLED
@ ERROR_INVALID_STATE
@ ERROR_MEMORY
Definition error_codes.h:55
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_settings_decode(const void *payload, size_t length, acip_settings_header_t *header, const char **json, size_t *json_length)
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
When code needs to work with asciichat errno snapshot, it keeps total, copied here instead of scatter...

References acip_render_settings_normalize_color_mode(), acip_render_settings_options(), acip_settings_decode(), acip_settings_encode(), ASCIICHAT_ERRNO_HANDLED, ASCIICHAT_OK, errno_scope_begin(), errno_scope_end(), ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_MEMORY, options_prepare_json(), options_values_json(), acip_render_settings_t::revision, SAFE_FREE, SAFE_MALLOC, and SET_ERRNO.

Referenced by handle_runtime_settings(), and session_host_receive_packet().

◆ acip_settings_send()

asciichat_error_t acip_settings_send ( acip_transport_t * transport,
const options_t * values,
uint32_t request,
uint32_t expected_revision )

Definition at line 179 of file lib/network/acip/settings.c.

179 {
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}
unsigned char uint8_t
@ PACKET_TYPE_ACIP_SETTINGS_SYNC
Settings sync (Initiator -> Host -> All Participants).
Definition packet.h:499
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

References acip_settings_encode(), ACIP_SETTINGS_MAX_SIZE, ACIP_SETTINGS_SCHEMA, ASCIICHAT_OK, ERROR_MEMORY, options_values_json(), packet_send_via_transport(), PACKET_TYPE_ACIP_SETTINGS_SYNC, SAFE_FREE, and SET_ERRNO.

Referenced by client_protocol_start(), and session_participant_request_settings().