ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
keyboard_handler.c
Go to the documentation of this file.
1
2/* Storage capacities in elements; text buffers include the terminating NUL. */
3#define SESSION_KEYBOARD_HANDLER_INPUT_BUFFER_SIZE 8192
4#define SESSION_KEYBOARD_HANDLER_NORMALIZED_BUFFER_SIZE 8192
5
13
17#include "session/capture.h"
18#include "session/display.h"
21#include <options/options.h>
22#include <ascii-chat/log/log.h>
24#include <libplatform/terminal.h>
25#include <libplatform/system.h>
26#include <string.h>
27#include <signal.h>
32#include <libplatform/clipboard.h>
37
38#if defined(ASCIICHAT_DIAGNOSTIC_UI)
40#endif
41
42/* ============================================================================
43 * Internal Helpers
44 * ============================================================================ */
45
47static double g_mute_saved_volume = 1.0;
48
49/* ============================================================================
50 * Keyboard Handler
51 * ============================================================================ */
52
53static void handle_keyboard_input(session_capture_ctx_t *capture, session_display_ctx_t *display, keyboard_key_t key) {
55 return;
57 return;
58 ui_session_action_t action = ui_session_key(key, &g_mute_saved_volume);
63 // Option-only display changes still need to wake the renderer. This is
64 // essential for synthetic video and audio visualizations, which may not
65 // publish a new capture frame solely because the overlay changed.
66 // Audio visualization toggles are different: redrawing here would replay
67 // the cached FFT/waveform frame before the next webcam frame arrives.
68 bool visualization_toggle = key == KEY_2 || key == KEY_3;
69 if (visualization_toggle)
71 else
73 if (key == KEY_UP || key == KEY_DOWN || key == 'm' || key == 'M')
75 return;
76 }
77
78 // Debug: log all key codes to help identify unknown keys
79 if (key != KEY_NONE) {
80 log_debug("KEYBOARD INPUT: code=%d (0x%02x) char='%c'", key, key, (key >= 32 && key < 127) ? key : '?');
81 }
82
83 switch (action) {
84 case UI_ACTION_QUIT:
85 // Match Ctrl+C: request the normal, coordinated shutdown path. When a
86 // help overlay is active, Ctrl+C closes it instead of quitting.
87 if (display && keyboard_help_is_active(display)) {
88 keyboard_help_toggle(display);
89 } else {
90 raise(SIGINT);
91 }
92 break;
94 // Match a second Ctrl+C: skip cleanup and terminate immediately.
96 break;
97 // ===== HELP SCREEN TOGGLE =====
98 case UI_ACTION_LOGS: {
100 break;
101 }
102 case UI_ACTION_HELP: {
103 log_info("✓✓✓ USER PRESSED ? KEY - HELP SCREEN TOGGLE ✓✓✓");
104 if (display) {
105 log_info("Toggling help screen (display=%p)", (void *)display);
106 keyboard_help_toggle(display);
107 log_info("✓ Help screen toggle complete");
108 } else {
109 log_error("Cannot toggle help - display context is NULL");
110 }
111 break;
112 }
113
114 // ===== HELP SCREEN CLOSE / QUIT =====
115 case UI_ACTION_CLOSE: {
118 break;
119 }
120 if (display && keyboard_help_is_active(display)) {
121 // Close help screen if it's active
122 keyboard_help_toggle(display);
123 } else {
124 // If help screen is not active, quit the app (like Ctrl-C)
125 // The signal handler will gracefully shutdown all modes (client, server, mirror, etc.)
126 raise(SIGINT);
127 }
128 break;
129 }
130
131 // ===== SEEK CONTROLS (file sources only) =====
132 case UI_ACTION_SEEK_BACK: { // Seek backward 30 seconds
133 if (capture) {
136 double current_pos = media_source_get_position(source);
137 if (current_pos >= 0.0) {
138 double new_pos = current_pos - 30.0;
139 if (new_pos < 0.0) {
140 new_pos = 0.0;
141 }
143 if (err == ASCIICHAT_OK) {
144 log_info("Seeked backward to %.1f seconds", new_pos);
145 }
146 void *audio_ctx = session_capture_get_audio_context(capture);
147 if (audio_ctx) {
149 }
150 }
151 }
152 }
153 break;
154 }
155
156 case UI_ACTION_SEEK_FORWARD: { // Seek forward 30 seconds
157 if (capture) {
160 double current_pos = media_source_get_position(source);
161 double duration = media_source_get_duration(source);
162 if (current_pos >= 0.0) {
163 double new_pos = current_pos + 30.0;
164 // Clamp to duration if known
165 if (duration > 0.0 && new_pos > duration) {
166 new_pos = duration;
167 }
169 if (err == ASCIICHAT_OK) {
170 log_info("Seeked forward to %.1f seconds", new_pos);
171 }
172 void *audio_ctx = session_capture_get_audio_context(capture);
173 if (audio_ctx) {
175 }
176 }
177 }
178 }
179 break;
180 }
181
182 // ===== PLAY/PAUSE CONTROL =====
185 if (capture && session_capture_get_audio_context(capture))
187 break;
188 case UI_ACTION_PAUSE: { // Toggle play/pause
189 if (capture) {
194 log_info("Paused");
195 } else {
196 log_info("Playing");
197 }
198 void *audio_ctx = session_capture_get_audio_context(capture);
199 if (audio_ctx) {
201 }
202 }
203 }
204 break;
205 }
206
207 // ===== DIAGNOSTICS =====
208#if defined(ASCIICHAT_DIAGNOSTIC_UI)
209 case UI_ACTION_DEBUG: {
211 break;
212 }
213#endif
214
215 default:
216 // Unknown key - log it for debugging
217 if (key != KEY_NONE) {
218 log_debug("UNKNOWN KEY: code=%d (0x%02x) char='%c'", key, key, (key >= 32 && key < 127) ? key : '?');
219 }
220 break;
221 }
222}
223
225 // Panic-exit bindings are global: they must bypass every modal TUI, prompt,
226 // log search, and media shortcut so they remain available during a hang.
227 if (key == 'Q' || key == KEY_SHIFT_ESCAPE) {
229 }
230
231 bool text_entry = log_search_is_entering();
232 if (!text_entry && (key == '<' || key == '>')) {
233 session_capture_skip_media(capture, key == '>' ? 1 : -1);
234 return;
235 }
236 if (!text_entry && key == KEY_ESCAPE && session_capture_cancel_open(capture))
237 return;
238 if (!text_entry && (key == KEY_MEDIA_SUBMIT || key == KEY_PASTE || tui_shortcut_action(key) == TUI_SHORTCUT_MEDIA || key == 'v' || key == 'V')) {
241 return;
242 }
245 if (key == KEY_MEDIA_SUBMIT)
246 result = ui_input_media_take(input, sizeof(input));
247 else if (key == KEY_PASTE)
248 result = ui_input_take_paste(input, sizeof(input));
249 else if (key == 'v' || key == 'V')
250 result = platform_clipboard_read(input, sizeof(input));
251 if (result == ASCIICHAT_OK) {
252 if (key != KEY_MEDIA_SUBMIT && input[0]) {
253 media_playlist_t list;
254 result = media_playlist_parse(input, &list);
255 if (result == ASCIICHAT_OK)
256 result = media_playlist_format(&list, normalized, sizeof(normalized));
257 if (result == ASCIICHAT_OK)
258 memcpy(input, normalized, strlen(normalized) + 1);
259 }
260 if (result == ASCIICHAT_OK && key != KEY_MEDIA_SUBMIT)
261 result = ui_input_media_begin(input);
262 if (result == ASCIICHAT_OK && key == KEY_MEDIA_SUBMIT)
263 result = session_capture_open_playlist(capture, input);
264 }
265 if (key == KEY_MEDIA_SUBMIT)
267 if (result != ASCIICHAT_OK) {
269 if (HAS_ERRNO(&error))
270 NOTICE(WARNING, "MEDIA INPUT", "%s", error.context_message);
271 CLEAR_ERRNO();
272 }
273 return;
274 }
276 handle_keyboard_input(capture, display, key);
278}
#define HAS_ERRNO(context)
#define CLEAR_ERRNO()
void session_capture_lock_source(session_capture_ctx_t *ctx)
void session_capture_unlock_source(session_capture_ctx_t *ctx)
void session_capture_toggle_media(session_capture_ctx_t *ctx)
void session_capture_skip_media(session_capture_ctx_t *ctx, int direction)
bool session_capture_media_active(session_capture_ctx_t *ctx)
void * session_capture_get_audio_context(session_capture_ctx_t *ctx)
Get the audio context for playback control.
asciichat_error_t session_capture_open_playlist(session_capture_ctx_t *ctx, const char *text)
void session_capture_redraw_media(session_capture_ctx_t *ctx)
bool session_capture_cancel_open(session_capture_ctx_t *ctx)
📹 Unified media capture abstraction for session-based video sources
Dedicated worker for all pending errno stacks and handled history. Signal handlers may only request a...
void ui_errno_stacks_toggle(void)
void ui_errno_stacks_hide(void)
bool ui_errno_stacks_handle_key(keyboard_key_t key)
audio_visualization_source_t source
Definition fft.c:25
void audio_flush_playback_buffers(audio_context_t *ctx)
Flush all audio buffers to synchronize with seek operations.
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 log_error(...)
Log an ERROR message.
Definition log/log.h:628
#define log_info(...)
Log an INFO message.
Definition log/log.h:602
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
bool media_source_is_paused(media_source_t *source)
Check if media source is paused.
Definition source.c:1056
double media_source_get_position(media_source_t *source)
Get current playback position in seconds.
Definition source.c:993
double media_source_get_duration(media_source_t *source)
Get media duration in seconds.
Definition source.c:971
asciichat_error_t media_source_seek(media_source_t *source, double timestamp_sec)
Seek media source to timestamp.
Definition source.c:889
media_source_type_t media_source_get_type(media_source_t *source)
Get media source type.
Definition source.c:967
void media_source_toggle_pause(media_source_t *source)
Toggle pause state of media source.
Definition source.c:1063
@ MEDIA_SOURCE_FILE
Media file (video/audio).
Definition source.h:103
void keyboard_help_toggle(session_display_ctx_t *ctx)
Toggle keyboard help on/off.
bool keyboard_help_is_active(session_display_ctx_t *ctx)
Check if keyboard help is currently active.
struct session_display_ctx session_display_ctx_t
session display config is intentionally a small, concrete record. Fields such as snapshot_mode,...
void session_handle_keyboard_input(session_capture_ctx_t *capture, session_display_ctx_t *display, keyboard_key_t key)
Handle keyboard input in a session.
struct session_capture_ctx session_capture_ctx_t
session capture config keeps the values that move through the subsystem: type, path,...
void * session_capture_get_media_source(session_capture_ctx_t *ctx)
Get the underlying media source from capture context.
🔊 Audio Capture and Playback Interface for ascii-chat
Statistics worker for error totals and memory reports. Signal handlers may only request a report.
#define SESSION_KEYBOARD_HANDLER_INPUT_BUFFER_SIZE
#define SESSION_KEYBOARD_HANDLER_NORMALIZED_BUFFER_SIZE
🎮 Keyboard input handler for interactive session controls
🆘 Interactive keyboard help overlay for session keyboard shortcuts
deferred_action_t action
bool ui_session_logs_key(keyboard_key_t key)
📝 Logging API with multiple log levels and terminal output control
void platform_force_exit(int exit_code)
Definition misc.c:129
#define NOTICE(severity, title,...)
Definition notice.h:56
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
asciichat_error_t media_playlist_format(const media_playlist_t *list, char *text, size_t capacity)
Definition playlist.c:87
asciichat_error_t media_playlist_parse(const char *text, media_playlist_t *list)
Definition playlist.c:32
void ui_prompt_remove(void)
Definition prompt.c:293
bool log_search_is_entering(void)
Check if currently in input mode (typing pattern).
Definition search.c:408
Interactive search filtering for terminal screens.
ui_session_action_t ui_session_key(keyboard_key_t key, double *saved_volume)
tui_shortcut_t tui_shortcut_action(keyboard_key_t key)
Definition shortcuts.c:30
@ TUI_SHORTCUT_MEDIA
Definition shortcuts.h:8
🎬 Unified media source abstraction for webcam, files, and stdin
🖥️ Unified terminal display abstraction for session-based rendering
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with audio device info, it keeps index, max_input_channels,...
Media source for video and audio capture.
Definition source.c:47
void ui_controller_invalidate_media(void)
void ui_controller_redraw(void)
void ui_controller_toggle_logs(void)
int ui_controller_current_screen(void)
void ui_controller_hide_logs(void)
void ui_controller_show_volume(void)
int ui_controller_interactive_screen(void)
@ UI_SCREEN_PROMPT
@ UI_SCREEN_LOGS
asciichat_error_t ui_input_media_take(char *output, size_t capacity)
Definition tui/input.c:765
asciichat_error_t ui_input_take_paste(char *buffer, size_t capacity)
Definition tui/input.c:261
asciichat_error_t ui_input_media_begin(const char *text)
Definition tui/input.c:750