ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
mode_defaults.c
Go to the documentation of this file.
1
12
16#include <libplatform/system.h>
17#include <string.h>
18#include <stdio.h>
19
20// ============================================================================
21// Mode Descriptors - Names, Descriptions, and Groups
22// ============================================================================
23// Note: mode_descriptor_t is defined in mode_defaults.h (not here)
24
28static const mode_descriptor_t g_mode_descriptors[] = {
29 {MODE_SERVER, "server", "Multi-client video broadcast server", "server-like"},
30 {MODE_DISCOVERY_SERVICE, "discovery-service", "Session discovery and registration service", "server-like"},
31 {MODE_CLIENT, "client", "Connect to a server and send/receive streams", "client-like"},
32 {MODE_MIRROR, "mirror", "Local webcam preview without networking", "client-like"},
33 {MODE_DISCOVERY, "discovery", "Participant with dynamic session discovery", "client-like"},
34 {MODE_MCP_SERVER, "mcp-server", "MCP media rendering tools", "server-like"},
35 {MODE_API_SERVER, "api-server", "HTTP image-to-ASCII API", "server-like"},
36 {MODE_INVALID, NULL, NULL, NULL}};
37
44 for (size_t i = 0; g_mode_descriptors[i].name; i++) {
45 if (g_mode_descriptors[i].mode == mode) {
46 return &g_mode_descriptors[i];
47 }
48 }
49 return NULL;
50}
51
58const mode_descriptor_t *get_modes_by_group(const char *group, size_t *out_count) {
59 if (!group || !out_count) {
60 return NULL;
61 }
62
63 // Count matching modes
64 size_t count = 0;
65 for (size_t i = 0; g_mode_descriptors[i].name; i++) {
66 if (strcmp(g_mode_descriptors[i].group, group) == 0) {
67 count++;
68 }
69 }
70
71 if (count == 0) {
72 *out_count = 0;
73 return NULL;
74 }
75
76 // Allocate result array
77 mode_descriptor_t *result = SAFE_MALLOC((count + 1) * sizeof(mode_descriptor_t), mode_descriptor_t *);
78 size_t idx = 0;
79
80 // Fill result array
81 for (size_t i = 0; g_mode_descriptors[i].name && idx < count; i++) {
82 if (strcmp(g_mode_descriptors[i].group, group) == 0) {
83 result[idx++] = g_mode_descriptors[i];
84 }
85 }
86
87 // Add terminator
88 result[idx].mode = MODE_INVALID;
89 result[idx].name = NULL;
90 result[idx].description = NULL;
91 result[idx].group = NULL;
92
93 *out_count = count;
94 return result;
95}
96
97// ============================================================================
98// Mode-Aware Default: log-file
99// ============================================================================
100
112 (void)mode; // Unused
113 // Return empty string - let options_get_log_filepath() handle the defaults
114 return "";
115}
116
117// ============================================================================
118// Mode-Aware Default: port
119// ============================================================================
120
121static const int g_port_server = OPT_PORT_INT_DEFAULT; // 27224
122static const int g_port_client = OPT_PORT_INT_DEFAULT; // 27224
123static const int g_port_discovery_service = OPT_ACDS_PORT_INT_DEFAULT; // 27225
124static const int g_port_discovery = OPT_PORT_INT_DEFAULT; // 27224 (connects via discovery)
125
130 switch (mode) {
131 case MODE_MCP_SERVER: {
132 static const int port = OPT_MCP_PORT_DEFAULT;
133 return &port;
134 }
135 case MODE_API_SERVER: {
136 static const int port = OPT_MCP_PORT_DEFAULT;
137 return &port;
138 }
139 case MODE_SERVER:
140 return &g_port_server;
141 case MODE_CLIENT:
142 return &g_port_client;
144 return &g_port_discovery_service;
145 case MODE_DISCOVERY:
146 return &g_port_discovery;
147 case MODE_MIRROR:
148 case MODE_INVALID:
149 default:
150 // Fallback
151 return &g_port_server;
152 }
153}
154
155// ============================================================================
156// Mode-Aware Default: websocket-port
157// ============================================================================
158
159static const int g_websocket_port_server = OPT_WEBSOCKET_PORT_SERVER_DEFAULT; // 27226
160static const int g_websocket_port_discovery_service = OPT_WEBSOCKET_PORT_ACDS_DEFAULT; // 27227
161
166 switch (mode) {
167 case MODE_SERVER:
168 return &g_websocket_port_server;
170 return &g_websocket_port_discovery_service;
171 case MODE_CLIENT:
172 case MODE_MIRROR:
173 case MODE_DISCOVERY:
174 case MODE_INVALID:
175 default:
176 // Fallback (should not be used for client/mirror/discovery)
177 return &g_websocket_port_server;
178 }
179}
180
181// ============================================================================
182// Mode-Aware Default Application
183// ============================================================================
184
195 if (!opts) {
196 return;
197 }
198
199 // Apply port default based on mode
200 const int *mode_port = (const int *)get_default_port(opts->detected_mode);
201 if (mode_port && opts->detected_mode != MODE_MCP_SERVER && opts->detected_mode != MODE_API_SERVER) {
202 // Only update if still at a generic default (27224 or 27225)
203 if (opts->port == OPT_PORT_INT_DEFAULT || opts->port == OPT_ACDS_PORT_INT_DEFAULT) {
204 opts->port = *mode_port;
205 }
206 }
207
208 // Apply websocket-port default based on mode
209 const int *mode_websocket_port = (const int *)get_default_websocket_port(opts->detected_mode);
210 if (mode_websocket_port) {
211 // Only update if still at a generic default (27226 or 27227)
214 opts->websocket_port = *mode_websocket_port;
215 }
216 }
217
218 // If TLS cert+key are provided and websocket-port was not explicitly set,
219 // default to 443 (standard HTTPS/WSS port) instead of the mode default.
220 if (opts->tls_cert[0] != '\0' && opts->tls_key[0] != '\0') {
223 opts->websocket_port = 443;
224 }
225 }
226}
#define SAFE_MALLOC(size, cast)
Definition common.h:265
asciichat_mode_t
Mode type for options parsing.
Definition options.h:919
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
#define OPT_ACDS_PORT_INT_DEFAULT
Default ACDS discovery service port (integer).
Definition options.h:594
#define OPT_WEBSOCKET_PORT_ACDS_DEFAULT
Default WebSocket port for discovery-service mode (integer).
Definition options.h:573
#define OPT_MCP_PORT_DEFAULT
Definition options.h:576
#define OPT_PORT_INT_DEFAULT
Default TCP port for client/server communication (integer).
Definition options.h:567
#define OPT_WEBSOCKET_PORT_SERVER_DEFAULT
Default WebSocket port for server mode (integer).
Definition options.h:570
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:921
@ MODE_MCP_SERVER
Model Context Protocol rendering server.
Definition options.h:925
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
Definition options.h:923
@ MODE_API_SERVER
Image-to-ASCII HTTP API server.
Definition options.h:926
@ MODE_INVALID
Invalid mode.
Definition options.h:927
@ MODE_SERVER
Server mode - network server options.
Definition options.h:920
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network).
Definition options.h:922
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:924
void apply_mode_specific_defaults(options_t *opts)
Apply mode-specific defaults to an options struct after mode detection.
const mode_descriptor_t * get_modes_by_group(const char *group, size_t *out_count)
Get all mode descriptors grouped by category.
const void * get_default_websocket_port(asciichat_mode_t mode)
Get mode-specific default value for –websocket-port.
const mode_descriptor_t * get_mode_descriptor(asciichat_mode_t mode)
Get mode descriptor by mode.
const void * get_default_port(asciichat_mode_t mode)
Get mode-specific default value for –port.
const void * get_default_log_file(asciichat_mode_t mode)
Get mode-specific default value for –log-file.
Mode-aware default value getters and mode metadata.
Shared structures and macros for registry implementation.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
🔤 String Manipulation and Shell Escaping Utilities
Metadata for a mode.
Definition main.c:410
const char * name
Definition main.c:411
const char * group
const char * description
Definition main.c:412
asciichat_mode_t mode
asciichat_mode_t detected_mode
Mode detected from command-line arguments.
Definition options.h:983
int websocket_port
WebSocket server port (server/discovery-service only).
Definition options.h:1010
char tls_key[256]
Shared TLS private key file path for secure network listeners.
Definition options.h:1012
char tls_cert[256]
Shared TLS certificate file path for secure network listeners.
Definition options.h:1011
int port
Server port number.
Definition options.h:1009