ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
rcu.c File Reference

Go to the source code of this file.

Data Structures

struct  retired_options
 Deferred free list for old options structs. More...
struct  runtime_field_t

Macros

#define FIELD(name, type, cli)

Typedefs

typedef struct retired_options retired_options_t
 Deferred free list for old options structs.

Functions

size_t options_retired_snapshot_count (void)
 __attribute__ ((constructor))
 Constructor to initialize fork handlers at startup.
asciichat_error_t options_state_init (void)
 Initialize RCU options system.
asciichat_error_t options_state_set (const options_t *opts)
 Set options from a parsed options struct.
void options_state_destroy (void)
 Shutdown RCU options system.
const options_t * options_get (void)
 Get pointer to current options struct (lock-free, thread-safe).
const terminal_options_t * options_terminal_settings_ptr (void)
bool options_remote_render_field (const char *name)
asciichat_error_t options_prepare_patch (const options_t *current, const options_patch_t *patch, options_t *candidate, bool remote_only)
asciichat_error_t options_apply_patch (const options_patch_t *patch, options_patch_result_t *result)
asciichat_error_t options_read_snapshot (options_snapshot_t *snapshot)
uint64_t options_revision (void)
asciichat_error_t options_set_int (const char *name, int value)
 Check if an option was explicitly set via command-line.
asciichat_error_t options_set_bool (const char *name, bool value)
asciichat_error_t options_set_double (const char *name, double value)
asciichat_error_t options_set_string (const char *name, const char *value)
asciichat_error_t options_toggle (const char *name)
asciichat_error_t options_adjust (const char *name, double amount)
asciichat_error_t options_cycle (const char *name, int count)
asciichat_error_t options_prepare_json (const options_t *current, const char *json, size_t length, options_t *candidate, bool remote_only)
asciichat_error_t options_apply_json (const char *json, size_t length, uint64_t expected_revision, options_origin_t origin, options_patch_result_t *result)
char * options_values_json (const options_t *values, bool remote_only)

Macro Definition Documentation

◆ FIELD

#define FIELD ( name,
type,
cli )
Value:
{#name, type, offsetof(options_t, name), sizeof(((options_t *)0)->name), cli}
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.

Definition at line 548 of file rcu.c.

Typedef Documentation

◆ retired_options_t

Deferred free list for old options structs.

We can't immediately free old structs because readers might still be using them. Simple strategy: Keep old pointers in a list, free them at shutdown.

Future improvements:

  • Hazard pointers: Track which structs readers are using
  • Epoch-based reclamation: Free after grace period
  • Reference counting: Free when no readers

Current approach: Never free until shutdown (acceptable for rare updates)

Function Documentation

◆ __attribute__()

__attribute__ ( (constructor) )

Constructor to initialize fork handlers at startup.

This is called before main() by the linker, ensuring fork handlers are registered early.

Definition at line 364 of file rcu.c.

364 {
365 // Register the child handler that will be called after fork() in the child process
366 pthread_atfork(NULL, NULL, options_rcu_atfork_child);
367}

◆ options_adjust()

asciichat_error_t options_adjust ( const char * name,
double amount )

Definition at line 907 of file rcu.c.

907 {
908 return apply_one((options_edit_t){.name = name, .type = OPTIONS_VALUE_DOUBLE, .operation = OPTIONS_ADD, .value.number = amount});
909}
@ OPTIONS_ADD
@ OPTIONS_VALUE_DOUBLE

References OPTIONS_ADD, and OPTIONS_VALUE_DOUBLE.

Referenced by ui_session_key().

◆ options_apply_json()

asciichat_error_t options_apply_json ( const char * json,
size_t length,
uint64_t expected_revision,
options_origin_t origin,
options_patch_result_t * result )

Definition at line 989 of file rcu.c.

989 {
990 yyjson_doc *doc = NULL;
992 size_t count = 0;
993 asciichat_error_t error = parse_json_edits(json, length, &doc, edits, &count);
994 options_patch_t patch = {.edits = edits, .count = count, .expected_revision = expected_revision, .origin = origin};
995 if (error == ASCIICHAT_OK)
996 error = options_apply_patch(&patch, result);
997 yyjson_doc_free(doc);
998 return error;
999}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
#define OPTIONS_PATCH_MAX_EDITS
asciichat_error_t options_apply_patch(const options_patch_t *patch, options_patch_result_t *result)
Definition rcu.c:826

References ASCIICHAT_OK, options_apply_patch(), and OPTIONS_PATCH_MAX_EDITS.

Referenced by patch_options().

◆ options_apply_patch()

asciichat_error_t options_apply_patch ( const options_patch_t * patch,
options_patch_result_t * result )

No observer callbacks or network I/O run under the writer mutex. A rejected patch publishes nothing.

Definition at line 826 of file rcu.c.

826 {
827 if (!patch || !patch->edits || !patch->count || patch->count > OPTIONS_PATCH_MAX_EDITS)
828 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid options patch");
829 if (!lifecycle_is_initialized(&g_options_lifecycle))
830 return SET_ERRNO(ERROR_INVALID_STATE, "Options are not initialized");
831 mutex_lock(&g_options_write_mutex);
832 if (result)
833 *result = (options_patch_result_t){.revision = g_revision};
834 if (patch->expected_revision != OPTIONS_REVISION_ANY && patch->expected_revision != g_revision) {
835 mutex_unlock(&g_options_write_mutex);
836 return SET_ERRNO(ERROR_INVALID_STATE, "Options changed; refresh and retry");
837 }
838 options_t *current = atomic_ptr_load(&g_options);
839 options_t *candidate = SAFE_MALLOC(sizeof(*candidate), options_t *);
840 asciichat_error_t error = options_prepare_patch(current, patch, candidate, false);
841 if (error == ASCIICHAT_OK && memcmp(current, candidate, sizeof(*current))) {
842 atomic_ptr_store(&g_options, candidate);
843 deferred_free_add(current);
844 ++g_revision;
845 g_origin = patch->origin;
846 for (size_t i = 0; i < patch->count; ++i) {
847 if (!strcmp(patch->edits[i].name, "flip_x"))
848 flags_explicit_state_set("flip-x", true);
849 }
850 if (result)
851 *result = (options_patch_result_t){.revision = g_revision, .changed = true};
852 } else
853 SAFE_FREE(candidate);
854 mutex_unlock(&g_options_write_mutex);
855 return error;
856}
#define SET_ERRNO(code,...)
void atomic_ptr_store(atomic_ptr_t *a, void *value)
Atomically store a pointer.
void * atomic_ptr_load(atomic_ptr_t *a)
Atomically load a pointer.
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
@ ERROR_INVALID_STATE
@ ERROR_INVALID_PARAM
#define OPTIONS_REVISION_ANY
asciichat_error_t options_prepare_patch(const options_t *current, const options_patch_t *patch, options_t *candidate, bool remote_only)
Definition rcu.c:771
const char * name
const options_edit_t * edits
uint64_t expected_revision
options_origin_t origin

References ASCIICHAT_OK, atomic_ptr_load(), atomic_ptr_store(), options_patch_t::count, options_patch_t::edits, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, options_patch_t::expected_revision, options_edit_t::name, OPTIONS_PATCH_MAX_EDITS, options_prepare_patch(), OPTIONS_REVISION_ANY, options_patch_t::origin, SAFE_FREE, SAFE_MALLOC, and SET_ERRNO.

Referenced by options_apply_json().

◆ options_cycle()

asciichat_error_t options_cycle ( const char * name,
int count )

Definition at line 910 of file rcu.c.

910 {
911 return apply_one((options_edit_t){.name = name, .type = OPTIONS_VALUE_INT, .operation = OPTIONS_CYCLE, .value.integer = count});
912}
@ OPTIONS_CYCLE
@ OPTIONS_VALUE_INT

References OPTIONS_CYCLE, and OPTIONS_VALUE_INT.

Referenced by ui_session_key().

◆ options_prepare_json()

asciichat_error_t options_prepare_json ( const options_t * current,
const char * json,
size_t length,
options_t * candidate,
bool remote_only )

Definition at line 977 of file rcu.c.

977 {
978 yyjson_doc *doc = NULL;
980 size_t count = 0;
981 asciichat_error_t error = parse_json_edits(json, length, &doc, edits, &count);
982 options_patch_t patch = {.edits = edits, .count = count, .expected_revision = OPTIONS_REVISION_ANY};
983 if (error == ASCIICHAT_OK)
984 error = options_prepare_patch(current, &patch, candidate, remote_only);
985 yyjson_doc_free(doc);
986 return error;
987}

References ASCIICHAT_OK, OPTIONS_PATCH_MAX_EDITS, options_prepare_patch(), and OPTIONS_REVISION_ANY.

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

◆ options_prepare_patch()

asciichat_error_t options_prepare_patch ( const options_t * current,
const options_patch_t * patch,
options_t * candidate,
bool remote_only )

Prepare caller-owned state. The owner serializes access; remote edits use an allowlist.

Definition at line 771 of file rcu.c.

771 {
772 if (!current || !candidate || current == candidate || !patch || !patch->edits || !patch->count || patch->count > OPTIONS_PATCH_MAX_EDITS)
773 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid options patch");
774 *candidate = *current;
775 bool palette_explicit_set = false;
776 bool palette_explicit_value = false;
777 for (size_t i = 0; i < patch->count; ++i) {
778 const options_edit_t *edit = &patch->edits[i];
779 const runtime_field_t *field = find_field(edit->name);
780 if (field && !strcmp(field->name, "palette_explicit")) {
781 palette_explicit_set = true;
782 palette_explicit_value = edit->operation == OPTIONS_TOGGLE ? !current->palette_explicit : edit->value.boolean;
783 }
784 }
785 for (size_t i = 0; i < patch->count; ++i) {
786 if (remote_only && (!options_remote_render_field(patch->edits[i].name) || patch->edits[i].operation != OPTIONS_ASSIGN))
787 return SET_ERRNO(ERROR_INVALID_PARAM, "Option is not a remote render preference");
788 asciichat_error_t error = edit_candidate(candidate, &patch->edits[i]);
789 if (error != ASCIICHAT_OK)
790 return error;
791 }
792 // An explicit palette flag is authoritative for the entire patch. Palette
793 // type/custom edits may imply explicit selection while they are applied, but
794 // must not overwrite an explicit false value supplied in the same patch.
795 if (palette_explicit_set)
796 candidate->palette_explicit = palette_explicit_value;
797 bool animation_changed = false;
798 bool stabilize_changed = false;
799 for (size_t i = 0; i < patch->count; ++i) {
800 const char *name = patch->edits[i].name;
801 animation_changed |= !strcmp(name, "matrix_rain") || !strcmp(name, "waveform") || !strcmp(name, "fft");
802 stabilize_changed |= !strcmp(name, "stabilize");
803 }
804 if (animation_changed && !stabilize_changed)
805 candidate->stabilize = !(candidate->waveform || candidate->fft);
806 bool orientation_changed = false;
807 bool flip_x_set_in_patch = false;
808 for (size_t i = 0; i < patch->count; ++i) {
809 const char *name = patch->edits[i].name;
810 orientation_changed |= !strcmp(name, "media_input") || !strcmp(name, "waveform") || !strcmp(name, "fft");
811 flip_x_set_in_patch |= !strcmp(name, "flip_x");
812 }
813 if (orientation_changed && !flip_x_set_in_patch && !flags_explicit_state_was_set("flip-x"))
814 candidate->flip_x = !candidate->media_input && !candidate->waveform && !candidate->fft;
815 for (size_t i = 0; i < patch->count; ++i) {
816 asciichat_error_t error = validate_field(find_field(patch->edits[i].name), candidate);
817 if (error != ASCIICHAT_OK)
818 return error;
819 }
820 asciichat_error_t palette_error = validate_glyph_palette(candidate);
821 if (palette_error != ASCIICHAT_OK)
822 return palette_error;
823 return ASCIICHAT_OK;
824}
asciichat_error_t validate_glyph_palette(const options_t *opts)
Definition validation.c:25
@ OPTIONS_TOGGLE
@ OPTIONS_ASSIGN
bool options_remote_render_field(const char *name)
Definition rcu.c:655
options_operation_t operation
union options_edit_t::@262352151300275353235176125230070205337075074271 value
bool fft
Replace the video image with a live audio frequency spectrogram.
Definition options.h:1117
bool palette_explicit
Palette explicitly selected.
Definition options.h:1223
bool media_input
Active input is a file/URL rather than the webcam.
Definition options.h:1097
bool stabilize
Reduce frame-to-frame character flicker.
Definition options.h:1131
bool waveform
Replace the video image with a live audio waveform.
Definition options.h:1116
bool flip_x
Effective horizontal orientation.
Definition options.h:1095
const char * name
Definition rcu.c:538

References ASCIICHAT_OK, options_edit_t::boolean, options_patch_t::count, options_patch_t::edits, ERROR_INVALID_PARAM, options_state::fft, options_state::flip_x, options_state::media_input, options_edit_t::name, runtime_field_t::name, options_edit_t::operation, OPTIONS_ASSIGN, OPTIONS_PATCH_MAX_EDITS, options_remote_render_field(), OPTIONS_TOGGLE, options_state::palette_explicit, SET_ERRNO, options_state::stabilize, validate_glyph_palette(), options_edit_t::value, and options_state::waveform.

Referenced by options_apply_patch(), and options_prepare_json().

◆ options_read_snapshot()

asciichat_error_t options_read_snapshot ( options_snapshot_t * snapshot)

Capture values and revision together. Callers own the copied snapshot.

Definition at line 858 of file rcu.c.

858 {
859 if (!snapshot || !lifecycle_is_initialized(&g_options_lifecycle))
860 return SET_ERRNO(ERROR_INVALID_STATE, "Options are not initialized");
861 mutex_lock(&g_options_write_mutex);
862 snapshot->values = *(options_t *)atomic_ptr_load(&g_options);
863 snapshot->revision = g_revision;
864 snapshot->origin = g_origin;
865 mutex_unlock(&g_options_write_mutex);
866 return ASCIICHAT_OK;
867}
options_origin_t origin

References ASCIICHAT_OK, atomic_ptr_load(), ERROR_INVALID_STATE, options_snapshot_t::origin, options_snapshot_t::revision, SET_ERRNO, and options_snapshot_t::values.

Referenced by client_protocol_start(), initial_options(), session_protocol_update_settings(), and session_settings_from_options().

◆ options_remote_render_field()

bool options_remote_render_field ( const char * name)

Only explicitly exposed fields may enter a remote rendering patch.

Definition at line 655 of file rcu.c.

655 {
656 const runtime_field_t *field = find_field(name);
657 if (!field)
658 return false;
659 const char *allowed[] = {"width", "height", "fps", "color_mode", "color_filter", "render_mode", "palette_type", "palette_custom", "palette_explicit", "renderer", "stabilize", "matrix_rain"};
660 for (size_t i = 0; i < sizeof(allowed) / sizeof(*allowed); ++i)
661 if (!strcmp(field->name, allowed[i]))
662 return true;
663 return false;
664}

References runtime_field_t::name.

Referenced by options_prepare_patch(), and options_values_json().

◆ options_retired_snapshot_count()

size_t options_retired_snapshot_count ( void )

Number of retired borrowed snapshots retained until shutdown.

Definition at line 316 of file rcu.c.

316 {
317 if (!lifecycle_is_initialized(&g_options_lifecycle))
318 return 0;
319 mutex_lock(&g_deferred_free_mutex);
320 size_t count = g_retired_count;
321 mutex_unlock(&g_deferred_free_mutex);
322 return count;
323}

◆ options_revision()

uint64_t options_revision ( void )

Observers coalesce changes by reading a new snapshot when this revision changes.

Definition at line 869 of file rcu.c.

869 {
870 if (!lifecycle_is_initialized(&g_options_lifecycle))
871 return 0;
872 mutex_lock(&g_options_write_mutex);
873 uint64_t revision = g_revision;
874 mutex_unlock(&g_options_write_mutex);
875 return revision;
876}
unsigned long long uint64_t

Referenced by OptionsBinding::revision(), and session_protocol_update_settings().

◆ options_state_destroy()

void options_state_destroy ( void )

Shutdown RCU options system.

Frees the current options struct and cleans up resources. Should be called at program shutdown after all threads have exited.

Definition at line 465 of file rcu.c.

465 {
466 // Check if initialized using lifecycle
467 if (!lifecycle_is_initialized(&g_options_lifecycle)) {
468 return;
469 }
470
471 flags_explicit_state_destroy();
472
473 /* Shutdown lifecycle to mark as no longer initialized */
474 lifecycle_shutdown(&g_options_lifecycle);
475
476 // Get current options pointer
477 options_t *current = atomic_ptr_load(&g_options);
478
479 // Clear the atomic pointer (prevent further reads)
480 atomic_ptr_store(&g_options, NULL);
481
482 // Free current struct
483 SAFE_FREE(current);
484
485 // Free all deferred structs
486 deferred_free_all();
487
488 // Destroy dynamically created mutexes
489 mutex_destroy(&g_options_write_mutex);
490 mutex_destroy(&g_deferred_free_mutex);
491
492 log_debug("Options state shutdown complete");
493}
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589

References atomic_ptr_load(), atomic_ptr_store(), log_debug, and SAFE_FREE.

Referenced by asciichat_shared_destroy(), main(), and mirror_cleanup().

◆ options_state_init()

asciichat_error_t options_state_init ( void )

Initialize RCU options system.

Must be called once at program startup before any threads access options. Allocates initial options struct and sets up atomic pointer.

Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 374 of file rcu.c.

374 {
375 // RCU setters validate against the option registry, including when callers
376 // use the state API directly in tests or embedded integrations. Initialize
377 // the registry here so those callers observe the same metadata as the CLI.
379
380 // Detect fork: after fork(), child process must reinitialize
381 pid_t current_pid = platform_get_pid();
382 if ((lifecycle_is_initialized(&g_options_lifecycle) || g_init_pid != -1) && g_init_pid != current_pid) {
383 log_debug("Detected fork (parent PID %d, child PID %d) - resetting lifecycle", g_init_pid, current_pid);
384 /* Reset lifecycle to allow reinitialization after fork */
385 lifecycle_shutdown(&g_options_lifecycle);
386 g_init_pid = current_pid;
387 }
388
389 // Check if already initialized in this process using lifecycle
390 if (lifecycle_is_initialized(&g_options_lifecycle) && g_init_pid == current_pid && atomic_ptr_load(&g_options) != NULL) {
391 log_warn("Options state already initialized");
392 return ASCIICHAT_OK;
393 }
394 if (lifecycle_is_initialized(&g_options_lifecycle) && atomic_ptr_load(&g_options) == NULL) {
395 lifecycle_shutdown(&g_options_lifecycle);
396 }
397
398 /* Use lifecycle to serialize initialization */
399 if (!lifecycle_init(&g_options_lifecycle, "options")) {
400 /* Another thread is initializing or already initialized */
401 if (lifecycle_is_initialized(&g_options_lifecycle)) {
402 return ASCIICHAT_OK;
403 }
404 return SET_ERRNO(ERROR_INVALID_STATE, "Failed to acquire options initialization lock");
405 }
406
407 // Initialize write mutex
408 if (mutex_init(&g_options_write_mutex, "options_write") != 0) {
409 lifecycle_shutdown(&g_options_lifecycle);
410 return SET_ERRNO(ERROR_THREAD, "Failed to initialize options write mutex");
411 }
412
413 // Initialize deferred free mutex
414 if (mutex_init(&g_deferred_free_mutex, "deferred_free") != 0) {
415 mutex_destroy(&g_options_write_mutex);
416 lifecycle_shutdown(&g_options_lifecycle);
417 return SET_ERRNO(ERROR_THREAD, "Failed to initialize deferred free mutex");
418 }
419
420 // Allocate initial options struct (will be populated later by options_state_populate_from_globals)
421 options_t *initial_opts = SAFE_MALLOC(sizeof(options_t), options_t *);
422 if (!initial_opts) {
423 mutex_destroy(&g_options_write_mutex);
424 mutex_destroy(&g_deferred_free_mutex);
425 lifecycle_shutdown(&g_options_lifecycle);
426 return SET_ERRNO(ERROR_MEMORY, "Failed to allocate initial options struct");
427 }
428
429 // Initialize all defaults using options_t_new()
430 *initial_opts = options_t_new();
431
432 // Publish initial struct (release semantics - make all fields visible to readers)
433 atomic_ptr_store(&g_options, initial_opts);
434
435 ++g_revision;
436 g_origin = OPTIONS_ORIGIN_CONFIG;
437 g_init_pid = current_pid; // Record PID to detect fork later
438 log_dev("Options state initialized with RCU pattern (PID %d)", current_pid);
439
440 return ASCIICHAT_OK;
441}
void registry_init_size(void)
Initialize registry size and metadata.
Definition core.c:107
@ ERROR_MEMORY
Definition error_codes.h:55
@ ERROR_THREAD
#define log_warn(...)
Log a WARN message.
Definition log/log.h:615
#define log_dev(...)
Log a DEV message (most verbose, development only).
Definition log/log.h:575
options_t options_t_new(void)
Initialize options by parsing command-line arguments.
Definition options.c:643
@ OPTIONS_ORIGIN_CONFIG
pid_t platform_get_pid(void)
Definition system.c:67

References ASCIICHAT_OK, atomic_ptr_load(), atomic_ptr_store(), ERROR_INVALID_STATE, ERROR_MEMORY, ERROR_THREAD, log_debug, log_dev, log_warn, OPTIONS_ORIGIN_CONFIG, options_t_new(), platform_get_pid(), registry_init_size(), SAFE_MALLOC, and SET_ERRNO.

Referenced by options_init().

◆ options_state_set()

asciichat_error_t options_state_set ( const options_t * opts)

Set options from a parsed options struct.

Called by options_init() after parsing to publish the options struct to RCU. This atomically replaces the current options with the provided struct.

Parameters
optsPointer to options struct to publish (will be copied)
Returns
ASCIICHAT_OK on success, error code on failure

Definition at line 443 of file rcu.c.

443 {
444 if (!opts) {
445 return SET_ERRNO(ERROR_INVALID_PARAM, "opts is NULL");
446 }
447
448 // Check initialization state using lifecycle
449 if (!lifecycle_is_initialized(&g_options_lifecycle)) {
450 return SET_ERRNO(ERROR_INVALID_STATE, "Options state not initialized (call options_state_init first)");
451 }
452
453 mutex_lock(&g_options_write_mutex);
454 options_t *next = SAFE_MALLOC(sizeof(*next), options_t *);
455 *next = *opts;
456 options_t *previous = atomic_ptr_load(&g_options);
457 atomic_ptr_store(&g_options, next);
458 ++g_revision;
459 g_origin = OPTIONS_ORIGIN_CONFIG;
460 deferred_free_add(previous);
461 mutex_unlock(&g_options_write_mutex);
462 return ASCIICHAT_OK;
463}

References ASCIICHAT_OK, atomic_ptr_load(), atomic_ptr_store(), ERROR_INVALID_PARAM, ERROR_INVALID_STATE, retired_options::next, OPTIONS_ORIGIN_CONFIG, SAFE_MALLOC, and SET_ERRNO.

Referenced by config_load_and_apply().

◆ options_toggle()

asciichat_error_t options_toggle ( const char * name)

Definition at line 904 of file rcu.c.

904 {
905 return apply_one((options_edit_t){.name = name, .type = OPTIONS_VALUE_BOOL, .operation = OPTIONS_TOGGLE});
906}
@ OPTIONS_VALUE_BOOL

References OPTIONS_TOGGLE, and OPTIONS_VALUE_BOOL.

Referenced by ui_session_key().

◆ options_values_json()

char * options_values_json ( const options_t * values,
bool remote_only )

Caller releases JSON with SAFE_FREE. Only public runtime fields are serialized.

Definition at line 1001 of file rcu.c.

1001 {
1002 yyjson_mut_doc *doc = yyjson_mut_doc_new(&runtime_json_allocator);
1003 if (!doc)
1004 return NULL;
1005 yyjson_mut_val *root = yyjson_mut_obj(doc);
1006 yyjson_mut_doc_set_root(doc, root);
1007 for (size_t i = 0; i < sizeof(runtime_fields) / sizeof(*runtime_fields); ++i) {
1008 const runtime_field_t *field = &runtime_fields[i];
1009 bool exposed = options_remote_render_field(field->name);
1010 if (!remote_only && (!strcmp(field->name, "flip_x") || !strcmp(field->name, "media_input") || !strcmp(field->name, "flip_y") || !strcmp(field->name, "waveform") || !strcmp(field->name, "fft") ||
1011 !strcmp(field->name, "fps_counter") || !strcmp(field->name, "audio_source") || !strcmp(field->name, "speakers_volume") || !strcmp(field->name, "microphone_sensitivity") ||
1012 !strcmp(field->name, "audio_enabled")))
1013 exposed = true;
1014 if (!exposed)
1015 continue;
1016 const char *ptr = (const char *)values + field->offset;
1017 switch (field->type) {
1018 case OPTIONS_VALUE_INT:
1019 yyjson_mut_obj_add_int(doc, root, field->name, *(const int *)ptr);
1020 break;
1021 case OPTIONS_VALUE_BOOL:
1022 yyjson_mut_obj_add_bool(doc, root, field->name, *(const bool *)ptr);
1023 break;
1025 yyjson_mut_obj_add_real(doc, root, field->name, field->size == sizeof(float) ? *(const float *)ptr : *(const double *)ptr);
1026 break;
1028 yyjson_mut_obj_add_str(doc, root, field->name, ptr);
1029 break;
1030 }
1031 }
1032 /*
1033 * The effective flip value is source-dependent: webcam input is mirrored
1034 * automatically until the user explicitly chooses an orientation. Expose
1035 * that provenance beside the effective value so browser clients can echo
1036 * engine-owned changes without turning them into an explicit preference.
1037 */
1038 if (!remote_only)
1039 yyjson_mut_obj_add_bool(doc, root, "flip_x_explicit", flags_explicit_state_was_set("flip-x"));
1040 char *json = yyjson_mut_write_opts(doc, 0, &runtime_json_allocator, NULL, NULL);
1041 yyjson_mut_doc_free(doc);
1042 return json;
1043}
@ OPTIONS_VALUE_STRING
options_value_type_t type
Definition rcu.c:539
size_t offset
Definition rcu.c:540
size_t size
Definition rcu.c:540

References runtime_field_t::name, runtime_field_t::offset, options_remote_render_field(), OPTIONS_VALUE_BOOL, OPTIONS_VALUE_DOUBLE, OPTIONS_VALUE_INT, OPTIONS_VALUE_STRING, runtime_field_t::size, and runtime_field_t::type.

Referenced by acip_settings_request(), acip_settings_send(), browser_client_settings_values(), and client_settings_values().