ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
session_input.c
Go to the documentation of this file.
1#include <ascii-chat/common.h>
6#include <options/options.h>
7#include <options/runtime.h>
9static const char *COLOR_FILTER_NAMES[COLOR_FILTER_COUNT] = {
10 "None", // COLOR_FILTER_NONE = 0
11 "Black", // COLOR_FILTER_BLACK = 1
12 "White", // COLOR_FILTER_WHITE = 2
13 "Green", // COLOR_FILTER_GREEN = 3
14 "Magenta", // COLOR_FILTER_MAGENTA = 4
15 "Fuchsia", // COLOR_FILTER_FUCHSIA = 5
16 "Orange", // COLOR_FILTER_ORANGE = 6
17 "Teal", // COLOR_FILTER_TEAL = 7
18 "Cyan", // COLOR_FILTER_CYAN = 8
19 "Pink", // COLOR_FILTER_PINK = 9
20 "Red", // COLOR_FILTER_RED = 10
21 "Yellow", // COLOR_FILTER_YELLOW = 11
22 "Rainbow", // COLOR_FILTER_RAINBOW = 12
23};
24
36static double clamp_volume(double volume) {
37 if (volume < 0.0) {
38 return 0.0;
39 }
40 if (volume > 1.0) {
41 return 1.0;
42 }
43 return volume;
44}
45
49static int next_color_mode(int current) {
50 // Cycle: TERM_COLOR_NONE → TERM_COLOR_16 → TERM_COLOR_256 →
51 // TERM_COLOR_TRUECOLOR → TERM_COLOR_NONE
52 switch (current) {
53 case 0: // TERM_COLOR_NONE
54 return 1; // TERM_COLOR_16
55 case 1: // TERM_COLOR_16
56 return 2; // TERM_COLOR_256
57 case 2: // TERM_COLOR_256
58 return 3; // TERM_COLOR_TRUECOLOR
59 default:
60 return 0; // Back to TERM_COLOR_NONE
61 }
62}
63
67static int next_render_mode(int current) {
68 // 3 render modes: FOREGROUND (0), BACKGROUND (1), HALF_BLOCK (2)
69 return (current + 1) % 3;
70}
71
75static int next_color_filter(int current) {
76 return (current + 1) % COLOR_FILTER_COUNT;
77}
78
79static asciichat_error_t toggle_mute(double *saved_volume) {
80 options_snapshot_t *snapshot =
81 SAFE_MALLOC(sizeof(*snapshot), options_snapshot_t *);
82 asciichat_error_t result;
83 for (;;) {
84 result = options_read_snapshot(snapshot);
85 if (result != ASCIICHAT_OK)
86 break;
87 double current = snapshot->values.speakers_volume;
88 double restore = *saved_volume > 0.0 ? *saved_volume : 0.5;
89 options_edit_t edit = {.name = "speakers_volume",
91 .value.number = current > 0.01 ? 0.0 : restore};
92 options_patch_t patch = {
93 .edits = &edit, .count = 1, .expected_revision = snapshot->revision};
94 result = options_apply_patch(&patch, NULL);
95 if (result == ERROR_INVALID_STATE) {
97 continue;
98 }
99 if (result == ASCIICHAT_OK) {
100 if (current > 0.01)
101 *saved_volume = current;
102 log_info("%s: %.0f%%", current > 0.01 ? "Muted" : "Unmuted",
103 edit.value.number * 100.0);
104 }
105 break;
106 }
107 SAFE_FREE(snapshot);
108 return result;
109}
110
111ui_session_action_t ui_session_key(keyboard_key_t key, double *saved_volume) {
112 switch ((int)key) {
113 case 3: // Ctrl-C from the terminal input stream
114 log_info("Ctrl-C key received from terminal input");
115 return UI_ACTION_QUIT;
116 case 'q':
117 return UI_ACTION_QUIT;
118 case 'Q':
120 case KEY_QUESTION:
121 return UI_ACTION_HELP;
122 case '[':
124 case ']':
125 return UI_ACTION_LOGS;
126 case KEY_ESCAPE:
127 return UI_ACTION_CLOSE;
128 case KEY_SHIFT_ESCAPE:
130 case KEY_LEFT:
131 return UI_ACTION_SEEK_BACK;
132 case KEY_RIGHT:
134 case KEY_SPACE:
135 return UI_ACTION_PAUSE;
136 case KEY_BACKTICK:
137 return UI_ACTION_DEBUG;
138 // ===== VOLUME CONTROLS =====
139 case KEY_DOWN: { // Decrease volume 10%
140 double current_volume = GET_OPTION(speakers_volume);
141 double new_volume = clamp_volume(current_volume - 0.1);
142 options_adjust("speakers_volume", key == KEY_DOWN ? -0.1 : 0.1);
143
144 double verify_volume = GET_OPTION(speakers_volume);
145 log_info("Volume DOWN: %.0f%% → %.0f%% (verified: %.0f%%)",
146 current_volume * 100.0, new_volume * 100.0, verify_volume * 100.0);
147 return UI_ACTION_VOLUME;
148 }
149
150 case KEY_UP: { // Increase volume 10%
151 double current_volume = GET_OPTION(speakers_volume);
152 double new_volume = clamp_volume(current_volume + 0.1);
153 options_adjust("speakers_volume", key == KEY_DOWN ? -0.1 : 0.1);
154
155 double verify_volume = GET_OPTION(speakers_volume);
156 log_info("Volume UP: %.0f%% → %.0f%% (verified: %.0f%%)",
157 current_volume * 100.0, new_volume * 100.0, verify_volume * 100.0);
158 return UI_ACTION_SETTINGS;
159 }
160
161 // ===== COLOR MODE CONTROL =====
162 case KEY_C:
163 case 'C': {
164 int current_mode = (int)GET_OPTION(color_mode);
165 int next_mode = next_color_mode(current_mode);
166 options_cycle("color_mode", 4);
167
168 const char *mode_names[] = {"Mono", "16-color", "256-color", "Truecolor"};
169 if (next_mode >= 0 && next_mode <= 3) {
170 log_info("Color mode: %s", mode_names[next_mode]);
171 }
172 return UI_ACTION_SETTINGS;
173 }
174
175 // ===== MUTE CONTROL =====
176 case KEY_M:
177 case 'M': {
178 if (toggle_mute(saved_volume) != ASCIICHAT_OK)
179 log_warn("Could not toggle mute");
180
181 return UI_ACTION_SETTINGS;
182 }
183
184 // ===== RENDER MODE CONTROL =====
185 case 'r': {
186 int current_mode = (int)GET_OPTION(render_mode);
187 int next_mode = next_render_mode(current_mode);
188 options_cycle("render_mode", 3);
189
190 const char *mode_names[] = {"Foreground", "Background", "Half-block"};
191 if (next_mode >= 0 && next_mode <= 2) {
192 log_info("Render mode: %s", mode_names[next_mode]);
193 }
194 return UI_ACTION_SETTINGS;
195 }
196
197 // ===== TEXT RENDERER CONTROL =====
198 case 'R': {
199 if (options_cycle("renderer", 2) != ASCIICHAT_OK) {
200 log_warn("Could not change text renderer");
201 return UI_ACTION_SETTINGS;
202 }
203 log_info("Text renderer: %s", GET_OPTION(renderer) == ASCII_RENDERER_GLYPH
204 ? "Glyph"
205 : "Luminance");
206 return UI_ACTION_SETTINGS;
207 }
208
209 // ===== FRAME STABILIZATION CONTROL =====
210 case 'S': {
211 if (options_toggle("stabilize") != ASCIICHAT_OK)
212 log_warn("Could not toggle frame stabilization");
213 else
214 log_info("Frame stabilization: %s",
215 GET_OPTION(stabilize) ? "enabled" : "disabled");
216 return UI_ACTION_SETTINGS;
217 }
218
219 // ===== COLOR FILTER CONTROL =====
220 case KEY_F:
221 case 'F': {
222 int current_filter = (int)GET_OPTION(color_filter);
223 int next_filter = next_color_filter(current_filter);
224 options_cycle("color_filter", 13);
225
226 if (next_filter >= 0 && next_filter < (int)COLOR_FILTER_COUNT) {
227 log_info("Color filter: %s", COLOR_FILTER_NAMES[next_filter]);
228 }
229 return UI_ACTION_SETTINGS;
230 }
231
232 // ===== HORIZONTAL FLIP CONTROL =====
233 case 'X':
234 case 'x': {
235 log_info("✓ X KEY PRESSED - handling horizontal flip");
236 bool current_flip_x = (bool)GET_OPTION(flip_x);
237 options_toggle("flip_x");
238 log_info("Horizontal flip: %s (was: %s)",
239 !current_flip_x ? "enabled" : "disabled",
240 current_flip_x ? "enabled" : "disabled");
241 return UI_ACTION_SETTINGS;
242 }
243
244 // ===== VERTICAL FLIP CONTROL =====
245 case 'Y':
246 case 'y': {
247 bool current_flip_y = (bool)GET_OPTION(flip_y);
248 options_toggle("flip_y");
249 log_info("Vertical flip: %s", !current_flip_y ? "enabled" : "disabled");
250 return UI_ACTION_SETTINGS;
251 }
252
253 // ===== MATRIX RAIN EFFECT CONTROL =====
254 case KEY_1: {
255 bool current_matrix = (bool)GET_OPTION(matrix_rain);
256 if (options_toggle("matrix_rain") != ASCIICHAT_OK)
257 return UI_ACTION_SETTINGS;
258 log_info("Matrix rain effect: %s",
259 !current_matrix ? "enabled" : "disabled");
260 return UI_ACTION_SETTINGS;
261 }
262
263 case KEY_2: {
264 if (!GET_OPTION(audio_enabled)) {
265 log_info("Audio waveform requires audio to be enabled");
266 return UI_ACTION_SETTINGS;
267 }
268 bool enabled = !GET_OPTION(waveform);
269 if (options_toggle("waveform") != ASCIICHAT_OK)
270 return UI_ACTION_SETTINGS;
271 log_info("Audio waveform: %s", enabled ? "enabled" : "disabled");
272 return UI_ACTION_SETTINGS;
273 }
274
275 case KEY_3: {
276 if (!GET_OPTION(audio_enabled)) {
277 log_info("Audio frequency display requires audio to be enabled");
278 return UI_ACTION_SETTINGS;
279 }
280 bool enabled = !GET_OPTION(fft);
282 log_error("Unable to initialize FFTW frequency display");
283 return UI_ACTION_SETTINGS;
284 }
285 if (options_toggle("fft") != ASCIICHAT_OK)
286 return UI_ACTION_SETTINGS;
287 log_info("Audio frequency display: %s", enabled ? "enabled" : "disabled");
288 return UI_ACTION_SETTINGS;
289 }
290
291 case 'w':
292 case 'W': {
293 if (options_toggle("face_follow") == ASCIICHAT_OK)
294 log_info("Face follow: %s",
295 GET_OPTION(face_follow) ? "enabled" : "disabled");
296 return UI_ACTION_SETTINGS;
297 }
298
299 case 'z':
300 case 'Z': {
301 if (options_set_int("zalgo", GET_OPTION(zalgo) ? 0 : 8) == ASCIICHAT_OK)
302 log_info("Zalgo intensity: %d", GET_OPTION(zalgo));
303 return UI_ACTION_SETTINGS;
304 }
305
306 // ===== FPS COUNTER TOGGLE =====
307 case KEY_MINUS: {
308 bool current = (bool)GET_OPTION(fps_counter);
309 options_toggle("fps_counter");
310 log_info("FPS counter: %s", !current ? "enabled" : "disabled");
311 return UI_ACTION_SETTINGS;
312 }
313
314 default:
315 return UI_ACTION_NONE;
316 }
317}
#define CLEAR_ERRNO()
int color_mode
Definition fft.c:26
asciichat_error_t audio_visualization_fft_init(void)
Definition fft.c:82
terminal_render_mode_t render_mode
Definition fft.c:27
bool enabled
Is filtering active?
Definition grep.c:63
#define SAFE_FREE(ptr)
#define SAFE_MALLOC(size, cast)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
#define log_warn(...)
Log a WARN message.
Definition log/log.h:591
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define GET_OPTION(field)
Definition options.h:1339
asciichat_error_t options_set_int(const char *name, int value)
Check if an option was explicitly set via command-line.
Definition rcu.c:964
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
@ OPTIONS_VALUE_DOUBLE
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
asciichat_error_t options_adjust(const char *name, double amount)
Definition rcu.c:989
asciichat_error_t options_apply_patch(const options_patch_t *patch, options_patch_result_t *result)
Definition rcu.c:897
asciichat_error_t options_toggle(const char *name)
Definition rcu.c:985
asciichat_error_t options_read_snapshot(options_snapshot_t *snapshot)
Definition rcu.c:934
asciichat_error_t options_cycle(const char *name, int count)
Definition rcu.c:995
ui_session_action_t ui_session_key(keyboard_key_t key, double *saved_volume)
union options_edit_t::@262352151300275353235176125230070205337075074271 value
float speakers_volume
Speaker volume multiplier (0.0-1.0, default 1.0).
Definition options.h:1124
@ ASCII_RENDERER_GLYPH