ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
runtime.h File Reference

Go to the source code of this file.

Data Structures

struct  options_edit_t
struct  options_patch_t
struct  options_patch_result_t
struct  options_snapshot_t

Macros

#define OPTIONS_REVISION_ANY   UINT64_MAX
#define OPTIONS_PATCH_MAX_EDITS   32

Enumerations

enum  options_value_type_t { OPTIONS_VALUE_INT , OPTIONS_VALUE_BOOL , OPTIONS_VALUE_DOUBLE , OPTIONS_VALUE_STRING }
enum  options_operation_t { OPTIONS_ASSIGN , OPTIONS_TOGGLE , OPTIONS_ADD , OPTIONS_CYCLE }
enum  options_origin_t { OPTIONS_ORIGIN_LOCAL , OPTIONS_ORIGIN_REMOTE , OPTIONS_ORIGIN_CONFIG }

Functions

asciichat_error_t options_apply_patch (const options_patch_t *patch, options_patch_result_t *result)
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_read_snapshot (options_snapshot_t *snapshot)
uint64_t options_revision (void)
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)
bool options_remote_render_field (const char *name)
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

◆ OPTIONS_PATCH_MAX_EDITS

#define OPTIONS_PATCH_MAX_EDITS   32

◆ OPTIONS_REVISION_ANY

#define OPTIONS_REVISION_ANY   UINT64_MAX

Definition at line 41 of file options/runtime.h.

Referenced by options_apply_patch(), options_prepare_json(), and patch_options().

Enumeration Type Documentation

◆ options_operation_t

Enumerator
OPTIONS_ASSIGN 
OPTIONS_TOGGLE 
OPTIONS_ADD 
OPTIONS_CYCLE 

Definition at line 17 of file options/runtime.h.

17 {
options_operation_t
@ OPTIONS_CYCLE
@ OPTIONS_ADD
@ OPTIONS_TOGGLE
@ OPTIONS_ASSIGN

◆ options_origin_t

Enumerator
OPTIONS_ORIGIN_LOCAL 
OPTIONS_ORIGIN_REMOTE 
OPTIONS_ORIGIN_CONFIG 

Definition at line 23 of file options/runtime.h.

23 {
options_origin_t
@ OPTIONS_ORIGIN_LOCAL
@ OPTIONS_ORIGIN_CONFIG
@ OPTIONS_ORIGIN_REMOTE

◆ options_value_type_t

Runtime edits are applied to the latest snapshot under the writer mutex.

Enumerator
OPTIONS_VALUE_INT 
OPTIONS_VALUE_BOOL 
OPTIONS_VALUE_DOUBLE 
OPTIONS_VALUE_STRING 

Definition at line 11 of file options/runtime.h.

11 {
options_value_type_t
@ OPTIONS_VALUE_DOUBLE
@ OPTIONS_VALUE_INT
@ OPTIONS_VALUE_BOOL
@ OPTIONS_VALUE_STRING

Function Documentation

◆ options_adjust()

asciichat_error_t options_adjust ( const char * name,
double amount )

Definition at line 989 of file rcu.c.

989 {
990 return apply_one((options_edit_t){.name = name,
991 .type = OPTIONS_VALUE_DOUBLE,
992 .operation = OPTIONS_ADD,
993 .value.number = amount});
994}

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 1092 of file rcu.c.

1095 {
1096 yyjson_doc *doc = NULL;
1098 size_t count = 0;
1099 asciichat_error_t error = parse_json_edits(json, length, &doc, edits, &count);
1100 options_patch_t patch = {.edits = edits,
1101 .count = count,
1102 .expected_revision = expected_revision,
1103 .origin = origin};
1104 if (error == ASCIICHAT_OK)
1105 error = options_apply_patch(&patch, result);
1106 yyjson_doc_free(doc);
1107 return error;
1108}
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:897

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 897 of file rcu.c.

898 {
899 if (!patch || !patch->edits || !patch->count ||
901 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid options patch");
902 if (!lifecycle_is_initialized(&g_options_lifecycle))
903 return SET_ERRNO(ERROR_INVALID_STATE, "Options are not initialized");
904 mutex_lock(&g_options_write_mutex);
905 if (result)
906 *result = (options_patch_result_t){.revision = g_revision};
908 patch->expected_revision != g_revision) {
909 mutex_unlock(&g_options_write_mutex);
910 return SET_ERRNO(ERROR_INVALID_STATE, "Options changed; refresh and retry");
911 }
912 options_t *current = atomic_ptr_load(&g_options);
913 options_t *candidate = SAFE_MALLOC(sizeof(*candidate), options_t *);
914 asciichat_error_t error =
915 options_prepare_patch(current, patch, candidate, false);
916 if (error == ASCIICHAT_OK && memcmp(current, candidate, sizeof(*current))) {
917 atomic_ptr_store(&g_options, candidate);
918 deferred_free_add(current);
919 ++g_revision;
920 g_origin = patch->origin;
921 for (size_t i = 0; i < patch->count; ++i) {
922 if (!strcmp(patch->edits[i].name, "flip_x"))
923 flags_explicit_state_set("flip-x", true);
924 }
925 if (result)
926 *result =
927 (options_patch_result_t){.revision = g_revision, .changed = true};
928 } else
929 SAFE_FREE(candidate);
930 mutex_unlock(&g_options_write_mutex);
931 return error;
932}
#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
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
#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:829
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 995 of file rcu.c.

995 {
996 return apply_one((options_edit_t){.name = name,
997 .type = OPTIONS_VALUE_INT,
998 .operation = OPTIONS_CYCLE,
999 .value.integer = count});
1000}

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 1076 of file rcu.c.

1078 {
1079 yyjson_doc *doc = NULL;
1081 size_t count = 0;
1082 asciichat_error_t error = parse_json_edits(json, length, &doc, edits, &count);
1083 options_patch_t patch = {.edits = edits,
1084 .count = count,
1085 .expected_revision = OPTIONS_REVISION_ANY};
1086 if (error == ASCIICHAT_OK)
1087 error = options_prepare_patch(current, &patch, candidate, remote_only);
1088 yyjson_doc_free(doc);
1089 return error;
1090}

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 829 of file rcu.c.

832 {
833 if (!current || !candidate || current == candidate || !patch ||
834 !patch->edits || !patch->count || patch->count > OPTIONS_PATCH_MAX_EDITS)
835 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid options patch");
836 *candidate = *current;
837 bool palette_explicit_set = false;
838 bool palette_explicit_value = false;
839 for (size_t i = 0; i < patch->count; ++i) {
840 const options_edit_t *edit = &patch->edits[i];
841 const runtime_field_t *field = find_field(edit->name);
842 if (field && !strcmp(field->name, "palette_explicit")) {
843 palette_explicit_set = true;
844 palette_explicit_value = edit->operation == OPTIONS_TOGGLE
845 ? !current->palette_explicit
846 : edit->value.boolean;
847 }
848 }
849 for (size_t i = 0; i < patch->count; ++i) {
850 if (remote_only && (!options_remote_render_field(patch->edits[i].name) ||
851 patch->edits[i].operation != OPTIONS_ASSIGN))
853 "Option is not a remote render preference");
854 asciichat_error_t error = edit_candidate(candidate, &patch->edits[i]);
855 if (error != ASCIICHAT_OK)
856 return error;
857 }
858 // An explicit palette flag is authoritative for the entire patch. Palette
859 // type/custom edits may imply explicit selection while they are applied, but
860 // must not overwrite an explicit false value supplied in the same patch.
861 if (palette_explicit_set)
862 candidate->palette_explicit = palette_explicit_value;
863 bool animation_changed = false;
864 bool stabilize_changed = false;
865 for (size_t i = 0; i < patch->count; ++i) {
866 const char *name = patch->edits[i].name;
867 animation_changed |= !strcmp(name, "matrix_rain") ||
868 !strcmp(name, "waveform") || !strcmp(name, "fft");
869 stabilize_changed |= !strcmp(name, "stabilize");
870 }
871 if (animation_changed && !stabilize_changed)
872 candidate->stabilize = !(candidate->waveform || candidate->fft);
873 bool orientation_changed = false;
874 bool flip_x_set_in_patch = false;
875 for (size_t i = 0; i < patch->count; ++i) {
876 const char *name = patch->edits[i].name;
877 orientation_changed |= !strcmp(name, "media_input") ||
878 !strcmp(name, "waveform") || !strcmp(name, "fft");
879 flip_x_set_in_patch |= !strcmp(name, "flip_x");
880 }
881 if (orientation_changed && !flip_x_set_in_patch &&
882 !flags_explicit_state_was_set("flip-x"))
883 candidate->flip_x =
884 !candidate->media_input && !candidate->waveform && !candidate->fft;
885 for (size_t i = 0; i < patch->count; ++i) {
886 asciichat_error_t error =
887 validate_field(find_field(patch->edits[i].name), candidate);
888 if (error != ASCIICHAT_OK)
889 return error;
890 }
891 asciichat_error_t palette_error = validate_glyph_palette(candidate);
892 if (palette_error != ASCIICHAT_OK)
893 return palette_error;
894 return ASCIICHAT_OK;
895}
asciichat_error_t validate_glyph_palette(const options_t *opts)
Definition validation.c:25
bool options_remote_render_field(const char *name)
Definition rcu.c:683
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:560

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 934 of file rcu.c.

934 {
935 if (!snapshot || !lifecycle_is_initialized(&g_options_lifecycle))
936 return SET_ERRNO(ERROR_INVALID_STATE, "Options are not initialized");
937 mutex_lock(&g_options_write_mutex);
938 snapshot->values = *(options_t *)atomic_ptr_load(&g_options);
939 snapshot->revision = g_revision;
940 snapshot->origin = g_origin;
941 mutex_unlock(&g_options_write_mutex);
942 return ASCIICHAT_OK;
943}
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 683 of file rcu.c.

683 {
684 const runtime_field_t *field = find_field(name);
685 if (!field)
686 return false;
687 const char *allowed[] = {"width", "height", "fps",
688 "color_mode", "color_filter", "render_mode",
689 "palette_type", "palette_custom", "palette_explicit",
690 "renderer", "stabilize", "matrix_rain"};
691 for (size_t i = 0; i < sizeof(allowed) / sizeof(*allowed); ++i)
692 if (!strcmp(field->name, allowed[i]))
693 return true;
694 return false;
695}

References runtime_field_t::name.

Referenced by options_prepare_patch(), and options_values_json().

◆ options_revision()

uint64_t options_revision ( void )

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

Definition at line 945 of file rcu.c.

945 {
946 if (!lifecycle_is_initialized(&g_options_lifecycle))
947 return 0;
948 mutex_lock(&g_options_write_mutex);
949 uint64_t revision = g_revision;
950 mutex_unlock(&g_options_write_mutex);
951 return revision;
952}
unsigned long long uint64_t

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

◆ options_toggle()

asciichat_error_t options_toggle ( const char * name)

Definition at line 985 of file rcu.c.

985 {
986 return apply_one((options_edit_t){
987 .name = name, .type = OPTIONS_VALUE_BOOL, .operation = OPTIONS_TOGGLE});
988}

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 1110 of file rcu.c.

1110 {
1111 yyjson_mut_doc *doc = yyjson_mut_doc_new(&runtime_json_allocator);
1112 if (!doc)
1113 return NULL;
1114 yyjson_mut_val *root = yyjson_mut_obj(doc);
1115 yyjson_mut_doc_set_root(doc, root);
1116 for (size_t i = 0; i < sizeof(runtime_fields) / sizeof(*runtime_fields);
1117 ++i) {
1118 const runtime_field_t *field = &runtime_fields[i];
1119 bool exposed = options_remote_render_field(field->name);
1120 if (!remote_only &&
1121 (!strcmp(field->name, "flip_x") ||
1122 !strcmp(field->name, "media_input") ||
1123 !strcmp(field->name, "flip_y") || !strcmp(field->name, "waveform") ||
1124 !strcmp(field->name, "fft") || !strcmp(field->name, "fps_counter") ||
1125 !strcmp(field->name, "audio_source") ||
1126 !strcmp(field->name, "speakers_volume") ||
1127 !strcmp(field->name, "microphone_sensitivity") ||
1128 !strcmp(field->name, "audio_enabled")))
1129 exposed = true;
1130 if (!exposed)
1131 continue;
1132 const char *ptr = (const char *)values + field->offset;
1133 switch (field->type) {
1134 case OPTIONS_VALUE_INT:
1135 yyjson_mut_obj_add_int(doc, root, field->name, *(const int *)ptr);
1136 break;
1137 case OPTIONS_VALUE_BOOL:
1138 yyjson_mut_obj_add_bool(doc, root, field->name, *(const bool *)ptr);
1139 break;
1141 yyjson_mut_obj_add_real(doc, root, field->name,
1142 field->size == sizeof(float)
1143 ? *(const float *)ptr
1144 : *(const double *)ptr);
1145 break;
1147 yyjson_mut_obj_add_str(doc, root, field->name, ptr);
1148 break;
1149 }
1150 }
1151 /*
1152 * The effective flip value is source-dependent: webcam input is mirrored
1153 * automatically until the user explicitly chooses an orientation. Expose
1154 * that provenance beside the effective value so browser clients can echo
1155 * engine-owned changes without turning them into an explicit preference.
1156 */
1157 if (!remote_only)
1158 yyjson_mut_obj_add_bool(doc, root, "flip_x_explicit",
1159 flags_explicit_state_was_set("flip-x"));
1160 char *json =
1161 yyjson_mut_write_opts(doc, 0, &runtime_json_allocator, NULL, NULL);
1162 yyjson_mut_doc_free(doc);
1163 return json;
1164}
options_value_type_t type
Definition rcu.c:561
size_t offset
Definition rcu.c:562
size_t size
Definition rcu.c:562

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().