ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
controller.h File Reference

Go to the source code of this file.

Data Structures

struct  animation_config_t
struct  animation_sample_t
 For animation config, this is the live record that ties type, fps, speed, hidden_policy together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record. More...
struct  animation_t
struct  animation_target_t

Enumerations

enum  animation_type_t { ANIMATION_SPLASH_RAINBOW , ANIMATION_DIGITAL_RAIN , ANIMATION_RAINBOW_FILTER , ANIMATION_TEST_PATTERN }
enum  animation_hidden_policy_t { ANIMATION_HIDDEN_PAUSE , ANIMATION_HIDDEN_CONTINUE }
enum  animation_target_type_t { ANIMATION_TARGET_COLOR , ANIMATION_TARGET_ANSI , ANIMATION_TARGET_TEST_PATTERN }

Functions

asciichat_error_t animation_init (animation_t *animation, const char *name, animation_config_t config)
void animation_destroy (animation_t *animation)
void animation_reset (animation_t *animation)
void animation_set_paused (animation_t *animation, bool paused)
void animation_set_visible (animation_t *animation, bool visible)
asciichat_error_t animation_set_speed (animation_t *animation, double speed)
asciichat_error_t animation_update (animation_t *animation, uint64_t timestamp_ns, animation_sample_t *out)
asciichat_error_t animation_sample_at (animation_config_t config, double seconds, animation_sample_t *out)
asciichat_error_t animation_apply (const animation_sample_t *sample, animation_target_t *target)

Enumeration Type Documentation

◆ animation_hidden_policy_t

Enumerator
ANIMATION_HIDDEN_PAUSE 
ANIMATION_HIDDEN_CONTINUE 

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

◆ animation_target_type_t

Enumerator
ANIMATION_TARGET_COLOR 
ANIMATION_TARGET_ANSI 
ANIMATION_TARGET_TEST_PATTERN 

Definition at line 66 of file video/anim/controller.h.

◆ animation_type_t

Enumerator
ANIMATION_SPLASH_RAINBOW 
ANIMATION_DIGITAL_RAIN 
ANIMATION_RAINBOW_FILTER 
ANIMATION_TEST_PATTERN 

Definition at line 8 of file video/anim/controller.h.

Function Documentation

◆ animation_apply()

asciichat_error_t animation_apply ( const animation_sample_t * sample,
animation_target_t * target )

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

89 {
90 if (!s || !t || !isfinite(s->seconds) || s->seconds < 0)
91 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation sample or target");
92 switch (s->type) {
94 if (t->type != ANIMATION_TARGET_COLOR || !t->color.out || !isfinite(t->color.position))
95 break;
96 static const rgb_pixel_t colors[] = {{255, 0, 0}, {255, 165, 0}, {255, 255, 0}, {0, 255, 0},
97 {0, 255, 255}, {0, 0, 255}, {255, 0, 255}};
98 double position = fmod(t->color.position, 1.0) + fmod(s->seconds, 2.5) * 0.4;
99 double phase = (position - floor(position)) * 6;
100 int i = (int)phase;
101 double blend = phase - i;
102 rgb_pixel_t a = colors[i], b = colors[i + 1];
103 *t->color.out =
104 (rgb_pixel_t){(uint8_t)(a.r * (1 - blend) + b.r * blend), (uint8_t)(a.g * (1 - blend) + b.g * blend),
105 (uint8_t)(a.b * (1 - blend) + b.b * blend)};
106 return ASCIICHAT_OK;
107 }
109 if (t->type == ANIMATION_TARGET_COLOR && t->color.out) {
110 color_filter_calculate_rainbow((float)fmod(s->seconds, 3.5), &t->color.out->r, &t->color.out->g,
111 &t->color.out->b);
112 return ASCIICHAT_OK;
113 }
114 if (t->type != ANIMATION_TARGET_ANSI || !t->ansi.input || !t->ansi.out)
115 break;
116 *t->ansi.out = rainbow_render_ansi_at(t->ansi.input, (float)fmod(s->seconds, 3.5));
117 return *t->ansi.out ? ASCIICHAT_OK : SET_ERRNO(ERROR_MEMORY, "Rainbow output allocation failed");
119 if (t->type != ANIMATION_TARGET_ANSI || !t->ansi.input || !t->ansi.out || !t->ansi.rain)
120 break;
121 *t->ansi.out = digital_rain_render_at(t->ansi.rain, t->ansi.input, (float)s->seconds);
122 return *t->ansi.out ? ASCIICHAT_OK : SET_ERRNO(ERROR_MEMORY, "Digital rain output allocation failed");
124 if (t->type != ANIMATION_TARGET_TEST_PATTERN || !t->test_pattern.context)
125 break;
126 return test_pattern_render_at(t->test_pattern.context, t->test_pattern.index, s->seconds * 1000.0,
127 t->test_pattern.cadence);
128 }
129 return SET_ERRNO(ERROR_INVALID_PARAM, "Animation type does not support this target");
130}
#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:22
unsigned char uint8_t
Definition common.h:60
@ ERROR_MEMORY
Definition error_codes.h:51
@ ASCIICHAT_OK
Definition error_codes.h:46
@ 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).
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).

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 * animation)

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

24 {
25 if (!a)
26 return;
28 memset(a, 0, sizeof(*a));
29}
#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 * animation,
const char * name,
animation_config_t config )

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

16 {
17 if (!a || !name || !valid_config(config))
18 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation configuration");
19 *a = (animation_t){.config = config, .visible = true};
20 NAMED_REGISTER(a, name, "animation", NULL, NULL);
21 return ASCIICHAT_OK;
22}
#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 * animation)

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

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

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 53 of file video/anim/controller.c.

53 {
54 if (!out || !valid_config(config) || !isfinite(seconds) || seconds < 0 || seconds * config.fps >= (double)UINT64_MAX)
55 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid animation timestamp");
56 *out = (animation_sample_t){.type = config.type,
57 .seconds = seconds,
58 .frame = (uint64_t)(seconds * config.fps),
59 .changed = true,
60 .next_frame_ns = UINT64_MAX};
61 return ASCIICHAT_OK;
62}
unsigned long long uint64_t
Definition common.h:63
static bool size_t gotel_query_t * out
Definition queries.h:8
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 * animation,
bool paused )

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

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

References animation_t::paused.

◆ animation_set_speed()

asciichat_error_t animation_set_speed ( animation_t * animation,
double speed )

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

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

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 * animation,
bool visible )

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

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

References animation_t::visible.

Referenced by ui_session_render().

◆ animation_update()

asciichat_error_t animation_update ( animation_t * animation,
uint64_t timestamp_ns,
animation_sample_t * out )

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

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

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