ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
platform/wasm/terminal.c
Go to the documentation of this file.
1
7#include <emscripten.h>
8#include <ascii-chat/platform/abstraction.h>
9#include <ascii-chat/platform/terminal.h>
10#include <ascii-chat/asciichat_errno.h>
11#include <stdbool.h>
12#include <stddef.h>
13#include <stdint.h>
14
15// ============================================================================
16// EM_JS: JavaScript bridge functions
17// ============================================================================
18
19EM_JS(int, js_get_terminal_cols, (), { return Module.xterm ? Module.xterm.cols : 80; });
20
21EM_JS(int, js_get_terminal_rows, (), { return Module.xterm ? Module.xterm.rows : 24; });
22
23EM_JS(void, js_terminal_write, (const char *data, int len), {
24 if (Module.xterm) {
25 Module.xterm.write(UTF8ToString(data, len));
26 }
27});
28
29// ============================================================================
30// Platform API Implementation
31// ============================================================================
32
33int platform_get_terminal_size(int *cols, int *rows) {
34 if (!cols || !rows) {
35 return -1;
36 }
37 *cols = js_get_terminal_cols();
38 *rows = js_get_terminal_rows();
39 return 0;
40}
41
42// Color mode detection - not needed for WASM mirror mode
43// Terminal capabilities are managed via JavaScript xterm.js settings
44
45int platform_write_terminal(const char *data, size_t len) {
46 js_terminal_write(data, (int)len);
47 return (int)len;
48}
49
50// Stub implementations (not needed for mirror mode)
52 (void)enable;
53 return 0; // No-op: xterm.js handles this
54}
55
56int platform_read_keyboard(char *buffer, size_t len) {
57 (void)buffer;
58 (void)len;
59 return -1; // Not supported: use JavaScript event listeners
60}
61
63 (void)fd;
64 return true; // Always true for WASM terminal
65}
66
67int platform_get_cursor_position(int *row, int *col) {
68 (void)row;
69 (void)col;
70 return -1; // Not supported in WASM
71}
72
73int platform_set_cursor_position(int row, int col) {
74 (void)row;
75 (void)col;
76 return -1; // Not supported in WASM
77}
78
79// get_terminal_size wrapper
80asciichat_error_t get_terminal_size(unsigned short int *width, unsigned short int *height) {
81 if (!width || !height) {
82 return ERROR_INVALID_PARAM;
83 }
84 int cols, rows;
85 if (platform_get_terminal_size(&cols, &rows) != 0) {
86 return ERROR_PLATFORM_INIT;
87 }
88 *width = (unsigned short int)cols;
89 *height = (unsigned short int)rows;
90 return ASCIICHAT_OK;
91}
92
93// Terminal UTF-8 support detection
95 return true; // xterm.js supports UTF-8
96}
97
98// Piped output detection
100 return false; // WASM mirror mode is not piped
101}
102
103// Color output detection
105 (void)fd;
106 return true; // WASM mirror mode supports color
107}
108
109// TTY detection stubs
111 return false; // No stdin in WASM
112}
113
115 return true; // xterm.js output is a TTY
116}
117
119 return true; // xterm.js output is a TTY
120}
121
123 return false; // WASM mirror mode is not interactive
124}
125
127 return false; // Don't force stderr in WASM
128}
129
131 return false; // Cannot prompt in WASM
132}
133
134// Terminal background detection
136 return true; // Default to dark background for xterm.js
137}
138
139// Terminal flush stub
140asciichat_error_t terminal_flush(int fd) {
141 (void)fd;
142 return ASCIICHAT_OK; // No-op - xterm.js handles flushing
143}
144
145// Terminal capabilities detection stub
146terminal_capabilities_t detect_terminal_capabilities(void) {
147 terminal_capabilities_t caps = {0};
148 caps.color_level = TERM_COLOR_256; // xterm.js supports 256 colors
149 caps.capabilities = 0;
150 caps.color_count = 256;
151 caps.utf8_support = true; // xterm.js supports UTF-8
152 caps.detection_reliable = true;
153 caps.render_mode = RENDER_MODE_FOREGROUND; // Default to foreground mode
154 platform_strlcpy(caps.term_type, "xterm-256color", sizeof(caps.term_type));
155 platform_strlcpy(caps.colorterm, "truecolor", sizeof(caps.colorterm));
156 caps.wants_background = true;
157 caps.palette_type = 0; // Default palette
158 caps.desired_fps = 30;
159 caps.color_filter = COLOR_FILTER_NONE;
160 return caps;
161}
162
163// Terminal background color query stub
164bool terminal_query_background_color(uint8_t *bg_r, uint8_t *bg_g, uint8_t *bg_b) {
165 // Return dark background (black) for xterm.js
166 if (bg_r)
167 *bg_r = 0;
168 if (bg_g)
169 *bg_g = 0;
170 if (bg_b)
171 *bg_b = 0;
172 return true;
173}
size_t platform_strlcpy(char *dst, const char *src, size_t size)
bool terminal_can_prompt_user(void)
int platform_set_terminal_raw_mode(bool enable)
int platform_get_cursor_position(int *row, int *col)
bool terminal_supports_utf8(void)
bool terminal_is_interactive(void)
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)
EM_JS(int, js_get_terminal_cols,(), { return Module.xterm ? Module.xterm.cols :80;})
int platform_set_cursor_position(int row, int col)
bool terminal_is_stdin_tty(void)
bool terminal_is_stderr_tty(void)
bool terminal_is_stdout_tty(void)
int platform_get_terminal_size(int *cols, int *rows)
terminal_capabilities_t detect_terminal_capabilities(void)
bool terminal_has_dark_background(void)
bool terminal_should_force_stderr(void)
asciichat_error_t terminal_flush(int fd)
bool terminal_query_background_color(uint8_t *bg_r, uint8_t *bg_g, uint8_t *bg_b)
bool terminal_is_piped_output(void)
asciichat_error_t get_terminal_size(unsigned short int *width, unsigned short int *height)
bool terminal_should_color_output(int fd)