ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
mode_defaults.c
Go to the documentation of this file.
1
12
13#include <options/registry/mode_defaults.h>
14#include <options/registry/common.h>
15#include <options/options.h>
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[] = {{MODE_SERVER, "server", "Multi-client video broadcast server", "server-like"},
29 {MODE_DISCOVERY_SERVICE, "discovery-service", "Session discovery and registration service", "server-like"},
30 {MODE_CLIENT, "client", "Connect to a server and send/receive streams", "client-like"},
31 {MODE_MIRROR, "mirror", "Local webcam preview without networking", "client-like"},
32 {MODE_DISCOVERY, "discovery", "Participant with dynamic session discovery", "client-like"},
33 {MODE_MCP_SERVER, "mcp-server", "MCP media rendering tools", "server-like"},
34 {MODE_API_SERVER, "api-server", "HTTP image-to-ASCII API", "server-like"},
35 {MODE_INVALID, NULL, NULL, NULL}};
36
43 for (size_t i = 0; g_mode_descriptors[i].name; i++) {
44 if (g_mode_descriptors[i].mode == mode) {
45 return &g_mode_descriptors[i];
46 }
47 }
48 return NULL;
49}
50
57const mode_descriptor_t *get_modes_by_group(const char *group, size_t *out_count) {
58 if (!group || !out_count) {
59 return NULL;
60 }
61
62 // Count matching modes
63 size_t count = 0;
64 for (size_t i = 0; g_mode_descriptors[i].name; i++) {
65 if (strcmp(g_mode_descriptors[i].group, group) == 0) {
66 count++;
67 }
68 }
69
70 if (count == 0) {
71 *out_count = 0;
72 return NULL;
73 }
74
75 // Allocate result array
76 mode_descriptor_t *result = SAFE_MALLOC((count + 1) * sizeof(mode_descriptor_t), mode_descriptor_t *);
77 size_t idx = 0;
78
79 // Fill result array
80 for (size_t i = 0; g_mode_descriptors[i].name && idx < count; i++) {
81 if (strcmp(g_mode_descriptors[i].group, group) == 0) {
82 result[idx++] = g_mode_descriptors[i];
83 }
84 }
85
86 // Add terminator
87 result[idx].mode = MODE_INVALID;
88 result[idx].name = NULL;
89 result[idx].description = NULL;
90 result[idx].group = NULL;
91
92 *out_count = count;
93 return result;
94}
95
96// ============================================================================
97// Mode-Aware Default: log-file
98// ============================================================================
99
111 (void)mode; // Unused
112 // Return empty string - let options_get_log_filepath() handle the defaults
113 return "";
114}
115
116// ============================================================================
117// Mode-Aware Default: port
118// ============================================================================
119
120static const int g_port_server = OPT_PORT_INT_DEFAULT; // 27224
121static const int g_port_client = OPT_PORT_INT_DEFAULT; // 27224
122static const int g_port_discovery_service = OPT_ACDS_PORT_INT_DEFAULT; // 27225
123static const int g_port_discovery = OPT_PORT_INT_DEFAULT; // 27224 (connects via discovery)
124
129 switch (mode) {
130 case MODE_MCP_SERVER: {
131 static const int port = OPT_MCP_PORT_DEFAULT;
132 return &port;
133 }
134 case MODE_API_SERVER: {
135 static const int port = OPT_MCP_PORT_DEFAULT;
136 return &port;
137 }
138 case MODE_SERVER:
139 return &g_port_server;
140 case MODE_CLIENT:
141 return &g_port_client;
143 return &g_port_discovery_service;
144 case MODE_DISCOVERY:
145 return &g_port_discovery;
146 case MODE_MIRROR:
147 case MODE_INVALID:
148 default:
149 // Fallback
150 return &g_port_server;
151 }
152}
153
154// ============================================================================
155// Mode-Aware Default: websocket-port
156// ============================================================================
157
158static const int g_websocket_port_server = OPT_WEBSOCKET_PORT_SERVER_DEFAULT; // 27226
159static const int g_websocket_port_discovery_service = OPT_WEBSOCKET_PORT_ACDS_DEFAULT; // 27227
160
165 switch (mode) {
166 case MODE_SERVER:
167 return &g_websocket_port_server;
169 return &g_websocket_port_discovery_service;
170 case MODE_CLIENT:
171 case MODE_MIRROR:
172 case MODE_DISCOVERY:
173 case MODE_INVALID:
174 default:
175 // Fallback (should not be used for client/mirror/discovery)
176 return &g_websocket_port_server;
177 }
178}
179
180// ============================================================================
181// Mode-Aware Default Application
182// ============================================================================
183
194 if (!opts) {
195 return;
196 }
197
198 // Apply port default based on mode
199 const int *mode_port = (const int *)get_default_port(opts->detected_mode);
200 if (mode_port && opts->detected_mode != MODE_MCP_SERVER && opts->detected_mode != MODE_API_SERVER) {
201 // Only update if still at a generic default (27224 or 27225)
202 if (opts->port == OPT_PORT_INT_DEFAULT || opts->port == OPT_ACDS_PORT_INT_DEFAULT) {
203 opts->port = *mode_port;
204 }
205 }
206
207 // Apply websocket-port default based on mode
208 const int *mode_websocket_port = (const int *)get_default_websocket_port(opts->detected_mode);
209 if (mode_websocket_port) {
210 // Only update if still at a generic default (27226 or 27227)
212 opts->websocket_port = *mode_websocket_port;
213 }
214 }
215
216 // If TLS cert+key are provided and websocket-port was not explicitly set,
217 // default to 443 (standard HTTPS/WSS port) instead of the mode default.
218 if (opts->tls_cert[0] != '\0' && opts->tls_key[0] != '\0') {
220 opts->websocket_port = 443;
221 }
222 }
223}
#define SAFE_MALLOC(size, cast)
asciichat_mode_t
Mode type for options parsing.
Definition options.h:916
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:593
#define OPT_WEBSOCKET_PORT_ACDS_DEFAULT
Default WebSocket port for discovery-service mode (integer).
Definition options.h:572
#define OPT_MCP_PORT_DEFAULT
Definition options.h:575
#define OPT_PORT_INT_DEFAULT
Default TCP port for client/server communication (integer).
Definition options.h:562
#define OPT_WEBSOCKET_PORT_SERVER_DEFAULT
Default WebSocket port for server mode (integer).
Definition options.h:565
@ MODE_CLIENT
Client mode - network client options.
Definition options.h:918
@ MODE_MCP_SERVER
Model Context Protocol rendering server.
Definition options.h:922
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
Definition options.h:920
@ MODE_API_SERVER
Image-to-ASCII HTTP API server.
Definition options.h:923
@ MODE_INVALID
Invalid mode.
Definition options.h:924
@ MODE_SERVER
Server mode - network server options.
Definition options.h:917
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network).
Definition options.h:919
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
Definition options.h:921
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.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
🔤 String Manipulation and Shell Escaping Utilities
asciichat_mode_t detected_mode
Mode detected from command-line arguments.
Definition options.h:978
int websocket_port
WebSocket server port (server/discovery-service only).
Definition options.h:1005
char tls_key[256]
Shared TLS private key file path for secure network listeners.
Definition options.h:1007
char tls_cert[256]
Shared TLS certificate file path for secure network listeners.
Definition options.h:1006
int port
Server port number.
Definition options.h:1004