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

Go to the source code of this file.

Functions

asciichat_error_t animation_init (animation_t *a, const char *name, animation_config_t config)
void animation_destroy (animation_t *a)
void animation_reset (animation_t *a)
void animation_set_paused (animation_t *a, bool paused)
void animation_set_visible (animation_t *a, bool visible)
asciichat_error_t animation_set_speed (animation_t *a, double speed)
asciichat_error_t animation_sample_at (animation_config_t config, double seconds, animation_sample_t *out)
asciichat_error_t animation_update (animation_t *a, uint64_t now, animation_sample_t *out)
asciichat_error_t animation_apply (const animation_sample_t *s, animation_target_t *t)

Function Documentation

◆ animation_apply()

asciichat_error_t animation_apply ( const animation_sample_t * s,
animation_target_t * t )

Definition at line 83 of file video/anim/controller.c.

83 {
84 if (!s || !t || !isfinite(s->seconds) || s->seconds < 0)
85 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation sample or target");
86 switch (s->type) {
88 if (t->type != ANIMATION_TARGET_COLOR || !t->color.out || !isfinite(t->color.position))
89 break;
90 static const rgb_pixel_t colors[] = {{255, 0, 0}, {255, 165, 0}, {255, 255, 0}, {0, 255, 0}, {0, 255, 255}, {0, 0, 255}, {255, 0, 255}};
91 double position = fmod(t->color.position, 1.0) + fmod(s->seconds, 2.5) * 0.4;
92 double phase = (position - floor(position)) * 6;
93 int i = (int)phase;
94 double blend = phase - i;
95 rgb_pixel_t a = colors[i], b = colors[i + 1];
96 *t->color.out = (rgb_pixel_t){(uint8_t)(a.r * (1 - blend) + b.r * blend), (uint8_t)(a.g * (1 - blend) + b.g * blend), (uint8_t)(a.b * (1 - blend) + b.b * blend)};
97 return ASCIICHAT_OK;
98 }
100 if (t->type == ANIMATION_TARGET_COLOR && t->color.out) {
101 color_filter_calculate_rainbow((float)fmod(s->seconds, 3.5), &t->color.out->r, &t->color.out->g, &t->color.out->b);
102 return ASCIICHAT_OK;
103 }
104 if (t->type != ANIMATION_TARGET_ANSI || !t->ansi.input || !t->ansi.out)
105 break;
106 *t->ansi.out = rainbow_render_ansi_at(t->ansi.input, (float)fmod(s->seconds, 3.5));
107 return *t->ansi.out ? ASCIICHAT_OK : SET_ERRNO(ERROR_MEMORY, "Rainbow output allocation failed");
109 if (t->type != ANIMATION_TARGET_ANSI || !t->ansi.input || !t->ansi.out || !t->ansi.rain)
110 break;
111 *t->ansi.out = digital_rain_render_at(t->ansi.rain, t->ansi.input, (float)s->seconds);
112 return *t->ansi.out ? ASCIICHAT_OK : SET_ERRNO(ERROR_MEMORY, "Digital rain output allocation failed");
115 break;
117 }
118 return SET_ERRNO(ERROR_INVALID_PARAM, "Animation type does not support this target");
119}
#define SET_ERRNO(code,...)
char * rainbow_render_ansi_at(const char *frame, float seconds)
asciichat_error_t test_pattern_render_at(struct test_pattern *pattern, int index, double time_ms, bool cadence)
char * digital_rain_render_at(digital_rain_t *rain, const char *frame, float seconds)
unsigned char colors[(512 *256 *2) *3]
Definition fft.c:23
unsigned char uint8_t
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
void color_filter_calculate_rainbow(float time, uint8_t *r, uint8_t *g, uint8_t *b)
Calculate rainbow color from time (3.5s cycle).
struct animation_target_t::@105141136260326341273057264343342151154253073027::@021005305237221242221055207105224353364212233103 test_pattern
struct animation_target_t::@105141136260326341273057264343342151154253073027::@072247246125252004245152136267001044326224304340 color
struct animation_target_t::@105141136260326341273057264343342151154253073027::@256117116162340230264137326272113315112047155330 ansi
struct test_pattern * context
struct digital_rain * rain
animation_target_type_t type
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).
@ ANIMATION_RAINBOW_FILTER
@ ANIMATION_DIGITAL_RAIN
@ ANIMATION_TEST_PATTERN
@ ANIMATION_SPLASH_RAINBOW
@ ANIMATION_TARGET_COLOR
@ ANIMATION_TARGET_TEST_PATTERN
@ ANIMATION_TARGET_ANSI

References ANIMATION_DIGITAL_RAIN, ANIMATION_RAINBOW_FILTER, ANIMATION_SPLASH_RAINBOW, ANIMATION_TARGET_ANSI, ANIMATION_TARGET_COLOR, ANIMATION_TARGET_TEST_PATTERN, ANIMATION_TEST_PATTERN, animation_target_t::ansi, ASCIICHAT_OK, rgb_pixel_t::b, animation_target_t::cadence, animation_target_t::color, color_filter_calculate_rainbow(), colors, animation_target_t::context, digital_rain_render_at(), ERROR_INVALID_PARAM, ERROR_MEMORY, rgb_pixel_t::g, animation_target_t::index, animation_target_t::input, animation_target_t::out, animation_target_t::position, rgb_pixel_t::r, animation_target_t::rain, rainbow_render_ansi_at(), animation_sample_t::seconds, SET_ERRNO, animation_target_t::test_pattern, test_pattern_render_at(), animation_sample_t::type, and animation_target_t::type.

Referenced by digital_rain_apply(), rainbow_replace_ansi_colors(), and test_pattern_render().

◆ animation_destroy()

void animation_destroy ( animation_t * a)

Definition at line 23 of file video/anim/controller.c.

23 {
24 if (!a)
25 return;
27 memset(a, 0, sizeof(*a));
28}
#define NAMED_UNREGISTER(ptr)
Unregister a pointer.

References NAMED_UNREGISTER.

Referenced by digital_rain_destroy(), and ui_session_destroy().

◆ animation_init()

asciichat_error_t animation_init ( animation_t * a,
const char * name,
animation_config_t config )

Definition at line 15 of file video/anim/controller.c.

15 {
16 if (!a || !name || !valid_config(config))
17 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation configuration");
18 *a = (animation_t){.config = config, .visible = true};
19 NAMED_REGISTER(a, name, "animation", NULL, NULL);
20 return ASCIICHAT_OK;
21}
#define NAMED_REGISTER(ptr, name, type, fmt, parent_ptr)
Register any pointer with base name, type, format spec, and location (auto-suffix).

References ASCIICHAT_OK, ERROR_INVALID_PARAM, NAMED_REGISTER, and SET_ERRNO.

Referenced by digital_rain_init(), and ui_session_create().

◆ animation_reset()

void animation_reset ( animation_t * a)

Definition at line 30 of file video/anim/controller.c.

30 {
31 if (!a)
32 return;
33 a->seconds = 0;
34 a->started = false;
35 a->last_frame = 0;
36}

References animation_t::last_frame, animation_t::seconds, and animation_t::started.

Referenced by digital_rain_reset(), and ui_session_loading().

◆ animation_sample_at()

asciichat_error_t animation_sample_at ( animation_config_t config,
double seconds,
animation_sample_t * out )

Definition at line 52 of file video/anim/controller.c.

52 {
53 if (!out || !valid_config(config) || !isfinite(seconds) || seconds < 0 || seconds * config.fps >= (double)UINT64_MAX)
54 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation timestamp");
55 *out = (animation_sample_t){.type = config.type, .seconds = seconds, .frame = (uint64_t)(seconds * config.fps), .changed = true, .next_frame_ns = UINT64_MAX};
56 return ASCIICHAT_OK;
57}
unsigned long long uint64_t
static bool size_t gotel_query_t * out
Definition queries.h:10
For animation config, this is the live record that ties type, fps, speed, hidden_policy together....

References ASCIICHAT_OK, ERROR_INVALID_PARAM, animation_config_t::fps, out, SET_ERRNO, and animation_config_t::type.

Referenced by animation_update(), rainbow_replace_ansi_colors(), and test_pattern_render().

◆ animation_set_paused()

void animation_set_paused ( animation_t * a,
bool paused )

Definition at line 37 of file video/anim/controller.c.

37 {
38 if (a)
39 a->paused = paused;
40}

References animation_t::paused.

◆ animation_set_speed()

asciichat_error_t animation_set_speed ( animation_t * a,
double speed )

Definition at line 45 of file video/anim/controller.c.

45 {
46 if (!a || !isfinite(speed) || speed < 0)
47 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation speed");
48 a->config.speed = speed;
49 return ASCIICHAT_OK;
50}
animation_config_t config

References ASCIICHAT_OK, animation_t::config, ERROR_INVALID_PARAM, SET_ERRNO, and animation_config_t::speed.

Referenced by digital_rain_apply().

◆ animation_set_visible()

void animation_set_visible ( animation_t * a,
bool visible )

Definition at line 41 of file video/anim/controller.c.

41 {
42 if (a)
43 a->visible = visible;
44}

References animation_t::visible.

Referenced by ui_session_render().

◆ animation_update()

asciichat_error_t animation_update ( animation_t * a,
uint64_t now,
animation_sample_t * out )

Definition at line 59 of file video/anim/controller.c.

59 {
60 if (!a || !out || !valid_config(a->config) || (a->started && now < a->last_ns))
61 return SET_ERRNO(ERROR_INVALID_PARAM, "Animation timestamps must be monotonic; reset before seeking");
62 bool running = !a->paused && a->config.speed > 0 && (a->visible || a->config.hidden_policy == ANIMATION_HIDDEN_CONTINUE);
63 double seconds = a->seconds;
64 if (a->started && running)
65 seconds += (double)(now - a->last_ns) / 1e9 * a->config.speed;
67 if (err != ASCIICHAT_OK)
68 return err;
69 out->changed = !a->started || out->frame != a->last_frame;
70 if (running) {
71 double remaining = ((double)out->frame + 1.0) / a->config.fps - seconds;
72 double delay = ceil(fmax(1.0, remaining * 1e9 / a->config.speed));
73 if (delay < (double)(UINT64_MAX - now))
74 out->next_frame_ns = now + (uint64_t)delay;
75 }
76 a->seconds = seconds;
77 a->last_ns = now;
78 a->last_frame = out->frame;
79 a->started = true;
80 return ASCIICHAT_OK;
81}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
animation_hidden_policy_t hidden_policy
asciichat_error_t animation_sample_at(animation_config_t config, double seconds, animation_sample_t *out)
@ ANIMATION_HIDDEN_CONTINUE

References ANIMATION_HIDDEN_CONTINUE, animation_sample_at(), ASCIICHAT_OK, animation_t::config, ERROR_INVALID_PARAM, animation_config_t::fps, animation_config_t::hidden_policy, animation_t::last_frame, animation_t::last_ns, out, animation_t::paused, animation_t::seconds, SET_ERRNO, animation_config_t::speed, animation_t::started, and animation_t::visible.

Referenced by digital_rain_apply(), digital_rain_init(), digital_rain_reset(), and ui_session_render().