ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
options/registry/mode_defaults.c
Go to the documentation of this file.
1
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_INVALID, NULL, NULL, NULL}};
35
42 for (size_t i = 0; g_mode_descriptors[i].name; i++) {
43 if (g_mode_descriptors[i].mode == mode) {
44 return &g_mode_descriptors[i];
45 }
46 }
47 return NULL;
48}
49
56const mode_descriptor_t *get_modes_by_group(const char *group, size_t *out_count) {
57 if (!group || !out_count) {
58 return NULL;
59 }
60
61 // Count matching modes
62 size_t count = 0;
63 for (size_t i = 0; g_mode_descriptors[i].name; i++) {
64 if (strcmp(g_mode_descriptors[i].group, group) == 0) {
65 count++;
66 }
67 }
68
69 if (count == 0) {
70 *out_count = 0;
71 return NULL;
72 }
73
74 // Allocate result array
75 mode_descriptor_t *result = SAFE_MALLOC((count + 1) * sizeof(mode_descriptor_t), mode_descriptor_t *);
76 size_t idx = 0;
77
78 // Fill result array
79 for (size_t i = 0; g_mode_descriptors[i].name && idx < count; i++) {
80 if (strcmp(g_mode_descriptors[i].group, group) == 0) {
81 result[idx++] = g_mode_descriptors[i];
82 }
83 }
84
85 // Add terminator
86 result[idx].mode = MODE_INVALID;
87 result[idx].name = NULL;
88 result[idx].description = NULL;
89 result[idx].group = NULL;
90
91 *out_count = count;
92 return result;
93}
94
95// ============================================================================
96// Mode-Aware Default: log-file
97// ============================================================================
98
110 (void)mode; // Unused
111 // Return empty string - let options_get_log_filepath() handle the defaults
112 return "";
113}
114
115// ============================================================================
116// Mode-Aware Default: port
117// ============================================================================
118
119static const int g_port_server = OPT_PORT_INT_DEFAULT; // 27224
120static const int g_port_client = OPT_PORT_INT_DEFAULT; // 27224
121static const int g_port_discovery_service = OPT_ACDS_PORT_INT_DEFAULT; // 27225
122static const int g_port_discovery = OPT_PORT_INT_DEFAULT; // 27224 (connects via discovery)
123
128 switch (mode) {
129 case MODE_SERVER:
130 return &g_port_server;
131 case MODE_CLIENT:
132 return &g_port_client;
134 return &g_port_discovery_service;
135 case MODE_DISCOVERY:
136 return &g_port_discovery;
137 case MODE_MIRROR:
138 case MODE_INVALID:
139 default:
140 // Fallback
141 return &g_port_server;
142 }
143}
144
145// ============================================================================
146// Mode-Aware Default: websocket-port
147// ============================================================================
148
149static const int g_websocket_port_server = OPT_WEBSOCKET_PORT_SERVER_DEFAULT; // 27226
150static const int g_websocket_port_discovery_service = OPT_WEBSOCKET_PORT_ACDS_DEFAULT; // 27227
151
156 switch (mode) {
157 case MODE_SERVER:
158 return &g_websocket_port_server;
160 return &g_websocket_port_discovery_service;
161 case MODE_CLIENT:
162 case MODE_MIRROR:
163 case MODE_DISCOVERY:
164 case MODE_INVALID:
165 default:
166 // Fallback (should not be used for client/mirror/discovery)
167 return &g_websocket_port_server;
168 }
169}
170
171// ============================================================================
172// Mode-Aware Default Application
173// ============================================================================
174
185 if (!opts) {
186 return;
187 }
188
189 // Apply port default based on mode
190 const int *mode_port = (const int *)get_default_port(opts->detected_mode);
191 if (mode_port) {
192 // Only update if still at a generic default (27224 or 27225)
193 if (opts->port == OPT_PORT_INT_DEFAULT || opts->port == OPT_ACDS_PORT_INT_DEFAULT) {
194 opts->port = *mode_port;
195 }
196 }
197
198 // Apply websocket-port default based on mode
199 const int *mode_websocket_port = (const int *)get_default_websocket_port(opts->detected_mode);
200 if (mode_websocket_port) {
201 // Only update if still at a generic default (27226 or 27227)
204 opts->websocket_port = *mode_websocket_port;
205 }
206 }
207
208 // If TLS cert+key are provided and websocket-port was not explicitly set,
209 // default to 443 (standard HTTPS/WSS port) instead of the mode default.
210 if (opts->websocket_tls_cert[0] != '\0' && opts->websocket_tls_key[0] != '\0') {
213 opts->websocket_port = 443;
214 }
215 }
216}
#define SAFE_MALLOC(size, cast)
Definition common.h:264
asciichat_mode_t
Mode type for options parsing.
#define OPT_ACDS_PORT_INT_DEFAULT
Default ACDS discovery service port (integer)
#define OPT_WEBSOCKET_PORT_ACDS_DEFAULT
Default WebSocket port for discovery-service mode (integer)
#define OPT_PORT_INT_DEFAULT
Default TCP port for client/server communication (integer)
#define OPT_WEBSOCKET_PORT_SERVER_DEFAULT
Default WebSocket port for server mode (integer)
@ MODE_CLIENT
Client mode - network client options.
@ MODE_DISCOVERY_SERVICE
Discovery server mode - session management and WebRTC signaling.
@ MODE_INVALID
Invalid mode.
@ MODE_SERVER
Server mode - network server options.
@ MODE_MIRROR
Mirror mode - local webcam viewing (no network)
@ MODE_DISCOVERY
Discovery mode - participant that can dynamically become host.
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
Mode-aware default value getters and mode metadata.
Shared structures and macros for registry implementation.
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.
Metadata for a mode.
Definition main.c:366
const char * name
Definition main.c:367
const char * group
const char * description
Definition main.c:368
asciichat_mode_t mode
Consolidated options structure.
asciichat_mode_t detected_mode
Mode detected from command-line arguments.
int websocket_port
WebSocket server port (server/discovery-service only)
char websocket_tls_cert[256]
WebSocket TLS certificate file path (server/discovery-service only)
char websocket_tls_key[256]
WebSocket TLS private key file path (server/discovery-service only)
Cross-platform system functions interface for ascii-chat.