Run mirror mode main loop.
Initializes webcam and terminal, then continuously captures frames, converts them to ASCII art, and displays them locally.
209 {
211
212
214
215#ifndef _WIN32
217#endif
218
219
221 if (webcam_result != 0) {
222 log_fatal(
"Failed to initialize webcam: %s", asciichat_error_string(webcam_result));
224 return webcam_result;
225 }
226
227
228 if (mirror_display_init() != 0) {
229 log_fatal(
"Failed to initialize display");
232 }
233
234
237
238
245 }
246
247
248 char palette_chars[256] = {0};
249 size_t palette_len = 0;
250 char luminance_palette[256] = {0};
251
255 log_fatal(
"Failed to initialize palette");
256 mirror_display_cleanup();
259 }
260
261
262 const long frame_interval_ms = 1000 / 60;
263 struct timespec last_frame_time = {0, 0};
264
265
266 struct timespec snapshot_start_time = {0, 0};
267 bool snapshot_done = false;
269 (void)clock_gettime(CLOCK_MONOTONIC, &snapshot_start_time);
270 }
271
272
274 struct timespec fps_report_time;
275 (void)clock_gettime(CLOCK_MONOTONIC, &fps_report_time);
276
277 log_info(
"Mirror mode running - press Ctrl+C to exit");
279
280 while (!mirror_should_exit()) {
281
282 struct timespec current_time;
283 (void)clock_gettime(CLOCK_MONOTONIC, ¤t_time);
284
285 long elapsed_ms = (current_time.tv_sec - last_frame_time.tv_sec) * 1000 +
286 (current_time.tv_nsec - last_frame_time.tv_nsec) / 1000000;
287
288 if (elapsed_ms < frame_interval_ms) {
290 continue;
291 }
292
293
294 if (
GET_OPTION(snapshot_mode) && !snapshot_done) {
295 double elapsed_sec = (double)(current_time.tv_sec - snapshot_start_time.tv_sec) +
296 (double)(current_time.tv_nsec - snapshot_start_time.tv_nsec) / 1e9;
297
298 float snapshot_delay =
GET_OPTION(snapshot_delay);
299 if (elapsed_sec >= snapshot_delay) {
300 snapshot_done = true;
301 }
302 }
303
304
306 if (!image) {
308 continue;
309 }
310
311
312
313
316 unsigned short int height =
GET_OPTION(height);
317 bool preserve_aspect_ratio = !stretch;
319 palette_chars, luminance_palette);
320
321 if (ascii_frame) {
322
323
324 bool snapshot_mode =
GET_OPTION(snapshot_mode);
325 bool should_write = !snapshot_mode || g_mirror_has_tty || snapshot_done;
326 if (should_write) {
327 mirror_write_frame(ascii_frame);
328 }
329
330
331 if (snapshot_mode && snapshot_done) {
334 break;
335 }
336
338 frame_count++;
339 }
340
342 last_frame_time = current_time;
343
344
346 ((
uint64_t)fps_report_time.tv_sec * 1000000 + (
uint64_t)fps_report_time.tv_nsec / 1000);
347
348 if (fps_elapsed_us >= 5000000) {
349 double fps = (double)frame_count / ((double)fps_elapsed_us / 1000000.0);
351 frame_count = 0;
352 fps_report_time = current_time;
353 }
354 }
355
356
358 log_info(
"Mirror mode shutting down");
359
360 mirror_display_cleanup();
362
363 return 0;
364}
unsigned long long uint64_t
#define log_fatal(...)
Log a FATAL message.
#define log_info(...)
Log an INFO message.
#define log_debug(...)
Log a DEBUG message.
void log_set_terminal_output(bool enabled)
Control stderr output to terminal.
#define GET_OPTION(field)
Safely get a specific option field (lock-free read)
int initialize_client_palette(palette_type_t palette_type, const char *custom_chars, char client_palette_chars[256], size_t *client_palette_len, char client_luminance_palette[256])
Initialize client palette with full configuration.
palette_type_t
Built-in palette type enumeration.
void ansi_fast_init_256color(void)
Initialize 256-color mode lookup tables.
void ansi_fast_init(void)
Initialize the decimal lookup table.
void ansi_fast_init_16color(void)
Initialize 16-color mode lookup tables.
void image_destroy(image_t *p)
Destroy an image allocated with image_new()
char * ascii_convert_with_capabilities(image_t *original, const ssize_t width, const ssize_t height, const terminal_capabilities_t *caps, const bool use_aspect_ratio, const bool stretch, const char *palette_chars, const char luminance_palette[256])
Convert image to ASCII art with terminal capability awareness.
asciichat_error_t webcam_init(unsigned short int webcam_index)
Initialize global webcam interface.
image_t * webcam_read(void)
Capture a frame from global webcam.
void webcam_print_init_error_help(asciichat_error_t error_code)
Print helpful error diagnostics for webcam initialization failures.
void webcam_cleanup(void)
Clean up global webcam interface.
Complete terminal capabilities structure.
terminal_color_mode_t color_level
Detected color support level (terminal_color_mode_t)