ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
platform/wasm/terminal.c
Go to the documentation of this file.
1
11#include <stdbool.h>
12#include <stddef.h>
13#include <stdint.h>
14
15// ============================================================================
16// Terminal Bridge Functions
17// ============================================================================
18
19static int js_get_terminal_cols(void) {
20 return 80;
21}
22
23static int js_get_terminal_rows(void) {
24 return 24;
25}
26
27static void js_terminal_write(const char *data, int len) {
28 (void)data;
29 (void)len;
30}
31
32// ============================================================================
33// Platform API Implementation
34// ============================================================================
35
38 if (r)
39 *r = value;
40 if (g)
41 *g = value;
42 if (b)
43 *b = value;
44}
45
48 if (r)
49 *r = value;
50 if (g)
51 *g = value;
52 if (b)
53 *b = value;
54}
55
56int platform_get_terminal_size(int *cols, int *rows) {
57 if (!cols || !rows) {
58 return -1;
59 }
60 *cols = js_get_terminal_cols();
61 *rows = js_get_terminal_rows();
62 return 0;
63}
64
65// Color mode detection - not needed for WASM mirror mode
66// Terminal capabilities are managed via JavaScript
67
68int platform_write_terminal(const char *data, size_t len) {
69 js_terminal_write(data, (int)len);
70 return (int)len;
71}
72
73// Stub implementations (not needed for mirror mode)
75 (void)enable;
76 return 0;
77}
78
79int platform_read_keyboard(char *buffer, size_t len) {
80 (void)buffer;
81 (void)len;
82 return -1; // Not supported: use JavaScript event listeners
83}
84
86 (void)fd;
87 return true; // Always true for WASM terminal
88}
89
90int platform_get_cursor_position(int *row, int *col) {
91 (void)row;
92 (void)col;
93 return -1; // Not supported in WASM
94}
95
96int platform_set_cursor_position(int row, int col) {
97 (void)row;
98 (void)col;
99 return -1; // Not supported in WASM
100}
101
102// get_terminal_size wrapper
103asciichat_error_t get_terminal_size(unsigned short int *width, unsigned short int *height) {
104 if (!width || !height) {
105 return ERROR_INVALID_PARAM;
106 }
107 int cols, rows;
108 if (platform_get_terminal_size(&cols, &rows) != 0) {
109 return ERROR_PLATFORM_INIT;
110 }
111 *width = (unsigned short int)cols;
112 *height = (unsigned short int)rows;
113 return ASCIICHAT_OK;
114}
115
116// Terminal UTF-8 support detection
118 return true;
119}
120
121// Piped output detection
123 return false; // WASM mirror mode is not piped
124}
125
126// Color output detection
128 (void)fd;
129 return true; // WASM mirror mode supports color
130}
131
132// TTY detection stubs
134 return false; // No stdin in WASM
135}
136
138 return true;
139}
140
142 return true;
143}
144
146 return false; // WASM mirror mode is not interactive
147}
148
150 return false; // Don't force stderr in WASM
151}
152
154 return false; // Cannot prompt in WASM
155}
156
157// Terminal background detection
159 return true; // Default to dark background for terminals
160}
161
162// Terminal flush stub
164 (void)fd;
165 return ASCIICHAT_OK;
166}
167
168// Terminal capabilities detection stub
170 terminal_capabilities_t caps = {0};
172 caps.capabilities = 0;
173 caps.color_count = 16777216;
174 caps.utf8_support = true;
175 caps.detection_reliable = true;
176 caps.render_mode = RENDER_MODE_FOREGROUND; // Default to foreground mode
177 platform_strlcpy(caps.term_type, "xterm-256color", sizeof(caps.term_type));
178 platform_strlcpy(caps.colorterm, "truecolor", sizeof(caps.colorterm));
179 caps.wants_background = true;
180 caps.palette_type = 0; // Default palette
181 // Browser rendering uses requestAnimationFrame and supports the same
182 // 60 FPS default as the native terminal paths.
185 return caps;
186}
187
188// Terminal background color query stub
190 // Return dark background (black)
191 if (bg_r)
192 *bg_r = 0;
193 if (bg_g)
194 *bg_g = 0;
195 if (bg_b)
196 *bg_b = 0;
197 return true;
198}
199
200// ============================================================================
201// Terminal Control Implementation (ANSI escape sequences)
202// ============================================================================
203
204#include <unistd.h>
205#include <stdio.h>
206
208 // Send ANSI escape sequence for echo control (xterm ignores but passes through)
209 if (!enabled) {
210 write(STDOUT_FILENO, "\033[?25l", 6); // Hide cursor
211 } else {
212 write(STDOUT_FILENO, "\033[?25h", 6); // Show cursor
213 }
214 return ASCIICHAT_OK;
215}
216
218 (void)fd;
219 return true; // Always use ANSI sequences in browser
220}
221
223 // Send ANSI clear screen escape sequence
224 write(STDOUT_FILENO, "\033[2J", 4);
225 return ASCIICHAT_OK;
226}
227
229 (void)fd;
230 // Send ANSI cursor home escape sequence
231 write(STDOUT_FILENO, "\033[H", 3);
232 return ASCIICHAT_OK;
233}
234
236 if (offset == 0) {
237 return ASCIICHAT_OK;
238 }
239
240 if (offset > 0) {
241 // Move right: \x1b[<n>C
242 char buf[32];
243 int len = snprintf(buf, sizeof(buf), "\033[%dC", offset);
244 if (len > 0 && len < (int)sizeof(buf)) {
245 write(STDOUT_FILENO, buf, len);
246 }
247 } else {
248 // Move left: \x1b[<n>D
249 char buf[32];
250 int len = snprintf(buf, sizeof(buf), "\033[%dD", -offset);
251 if (len > 0 && len < (int)sizeof(buf)) {
252 write(STDOUT_FILENO, buf, len);
253 }
254 }
255
256 return ASCIICHAT_OK;
257}
Platform abstraction layer umbrella header providing unified cross-platform API.
⚠️‼️ Comprehensive thread-local error context system for ascii-chat
bool enabled
Is filtering active?
Definition grep.c:84
unsigned char uint8_t
Definition common.h:56
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:49
@ ERROR_PLATFORM_INIT
Definition error_codes.h:60
@ ASCIICHAT_OK
Definition error_codes.h:51
@ ERROR_INVALID_PARAM
#define DEFAULT_MAX_FPS
Default maximum frame rate (frames per second)
Definition limits.h:29
asciichat_error_t terminal_move_cursor_relative(int offset)
Move cursor relative to current position.
asciichat_error_t terminal_cursor_home(int fd)
Move cursor to home position (top-left)
#define TERMINAL_COLOR_THEME_LIGHT_BG_R
Default background color for light theme (RGB)
Definition terminal.h:135
bool terminal_supports_utf8(void)
Check if terminal supports UTF-8.
asciichat_error_t terminal_clear_screen(void)
Clear the terminal screen.
#define TERMINAL_COLOR_THEME_DARK_FG_R
Default text color for dark theme (RGB)
Definition terminal.h:126
#define TERMINAL_COLOR_THEME_DARK_BG_R
Default background color for dark theme (RGB)
Definition terminal.h:144
asciichat_error_t terminal_flush(int fd)
Flush terminal output.
size_t platform_strlcpy(char *dst, const char *src, size_t size)
Safe string copy with size tracking (strlcpy)
#define TERMINAL_COLOR_THEME_LIGHT_FG_R
Platform-specific getopt include.
Definition terminal.h:116
asciichat_error_t terminal_set_echo(bool enabled)
Set terminal echo mode.
Application limits and constraints.
bool terminal_can_prompt_user(void)
Determine if interactive user prompts are appropriate.
int platform_set_terminal_raw_mode(bool enable)
int platform_get_cursor_position(int *row, int *col)
bool terminal_is_interactive(void)
Check if the session is fully interactive.
void terminal_get_default_background_color(int theme, uint8_t *r, uint8_t *g, uint8_t *b)
Get theme-aware default background color for pixel renderers.
int platform_write_terminal(const char *data, size_t len)
int platform_read_keyboard(char *buffer, size_t len)
bool platform_is_terminal(int fd)
int platform_set_cursor_position(int row, int col)
bool terminal_is_stdin_tty(void)
Check if stdin is connected to a TTY.
bool terminal_is_stderr_tty(void)
Check if stderr is connected to a TTY.
bool terminal_is_stdout_tty(void)
Check if stdout is connected to a TTY.
int platform_get_terminal_size(int *cols, int *rows)
terminal_capabilities_t detect_terminal_capabilities(void)
Detect terminal capabilities.
bool terminal_has_dark_background(void)
Detect if terminal theme is dark.
bool terminal_should_force_stderr(void)
Determine if logs should be forced to stderr.
void terminal_get_default_foreground_color(int theme, uint8_t *r, uint8_t *g, uint8_t *b)
Get theme-aware default foreground color for pixel renderers.
bool terminal_query_background_color(uint8_t *bg_r, uint8_t *bg_g, uint8_t *bg_b)
Query terminal background color using OSC 11 escape sequence.
bool terminal_is_piped_output(void)
Check if stdout is piped or redirected.
bool terminal_should_use_control_sequences(int fd)
Check if terminal control sequences should be used for the given fd.
asciichat_error_t get_terminal_size(unsigned short int *width, unsigned short int *height)
Get terminal size with multiple fallback methods.
bool terminal_should_color_output(int fd)
Determine if color output should be used.
ClangTool/LibTooling compatibility shim for stdbool.h.
Complete terminal capabilities structure.
Definition terminal.h:709
int palette_type
Palette type enum value (palette_type_t)
Definition terminal.h:729
terminal_color_mode_t color_level
Detected color support level (terminal_color_mode_t)
Definition terminal.h:711
uint8_t desired_fps
Client's desired frame rate (1-144 FPS)
Definition terminal.h:733
render_mode_t render_mode
Preferred rendering mode (render_mode_t)
Definition terminal.h:721
bool utf8_support
True if terminal supports UTF-8 encoding.
Definition terminal.h:717
uint32_t capabilities
Capability flags bitmask (terminal_capability_flags_t)
Definition terminal.h:713
uint32_t color_count
Maximum number of colors (2, 16, 256, or 16777216)
Definition terminal.h:715
color_filter_t color_filter
Monochromatic color filter enum value (color_filter_t)
Definition terminal.h:735
char term_type[64]
$TERM environment variable value (for debugging)
Definition terminal.h:723
char colorterm[64]
$COLORTERM environment variable value (for debugging)
Definition terminal.h:725
bool detection_reliable
True if detection is confident (reliable detection)
Definition terminal.h:719
bool wants_background
True if background colors are preferred.
Definition terminal.h:727
🖥️ Cross-platform terminal interface for ascii-chat
@ COLOR_FILTER_NONE
No filtering (default)
Definition terminal.h:601
@ RENDER_MODE_FOREGROUND
Foreground colors only (text color)
Definition terminal.h:664
@ TERM_COLOR_TRUECOLOR
24-bit truecolor support (RGB colors)
Definition terminal.h:588