ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
src/web/platform/terminal.c
Go to the documentation of this file.
1
6
7/* Storage capacities in elements; text buffers include the terminating NUL. */
8#define PLATFORM_WASM_TERMINAL_BUF_BUFFER_SIZE 32
9
10#include <libplatform/terminal.h>
13#include <options/options.h>
16#include <stdbool.h>
17#include <stddef.h>
18#include <stdint.h>
19
20// ============================================================================
21// Terminal Bridge Functions
22// ============================================================================
23
24static int js_get_terminal_cols(void) {
25 return 80;
26}
27
28static int js_get_terminal_rows(void) {
29 return 24;
30}
31
32static void js_terminal_write(const char *data, int len) {
33 (void)data;
34 (void)len;
35}
36
37// ============================================================================
38// Platform API Implementation
39// ============================================================================
40
42 uint8_t value = theme == 1 ? TERMINAL_COLOR_THEME_LIGHT_FG_R : TERMINAL_COLOR_THEME_DARK_FG_R;
43 if (r)
44 *r = value;
45 if (g)
46 *g = value;
47 if (b)
48 *b = value;
49}
50
52 uint8_t value = theme == 1 ? TERMINAL_COLOR_THEME_LIGHT_BG_R : TERMINAL_COLOR_THEME_DARK_BG_R;
53 if (r)
54 *r = value;
55 if (g)
56 *g = value;
57 if (b)
58 *b = value;
59}
60
61int platform_get_terminal_size(int *cols, int *rows) {
62 if (!cols || !rows) {
63 return -1;
64 }
65 *cols = js_get_terminal_cols();
66 *rows = js_get_terminal_rows();
67 return 0;
68}
69
70int terminal_get_size_fd(int fd, terminal_size_t *size) {
71 (void)fd;
72 return terminal_get_size(size);
73}
74
75int terminal_get_size(terminal_size_t *size) {
76 (void)size;
77 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Terminal operations not supported in WASM");
78}
79
81 return ASCIICHAT_OK;
82}
83
85 return ASCIICHAT_OK;
86}
87
88unsigned short int terminal_get_effective_width(int width_option, unsigned short fallback_width) {
89 (void)fallback_width;
90 if (width_option > 0) {
91 return (unsigned short int)width_option;
92 }
93 int width = GET_OPTION(width);
94 if (width > 0) {
95 return (unsigned short int)width;
96 }
97 return OPT_WIDTH_DEFAULT;
98}
99
100unsigned short int terminal_get_effective_height(int height_option, unsigned short fallback_height) {
101 (void)fallback_height;
102 if (height_option > 0) {
103 return (unsigned short int)height_option;
104 }
105 int height = GET_OPTION(height);
106 if (height > 0) {
107 return (unsigned short int)height;
108 }
109 return OPT_HEIGHT_DEFAULT;
110}
111
112// Color mode detection - not needed for WASM mirror mode
113// Terminal capabilities are managed via JavaScript
114
115int platform_write_terminal(const char *data, size_t len) {
116 js_terminal_write(data, (int)len);
117 return (int)len;
118}
119
120// Stub implementations (not needed for mirror mode)
122 (void)enable;
123 return 0;
124}
125
126int platform_read_keyboard(char *buffer, size_t len) {
127 (void)buffer;
128 (void)len;
129 return -1; // Not supported: use JavaScript event listeners
130}
131
133 (void)fd;
134 return true; // Always true for WASM terminal
135}
136
137int platform_get_cursor_position(int *row, int *col) {
138 (void)row;
139 (void)col;
140 return -1; // Not supported in WASM
141}
142
143int platform_set_cursor_position(int row, int col) {
144 (void)row;
145 (void)col;
146 return -1; // Not supported in WASM
147}
148
149// get_terminal_size wrapper
150int get_terminal_size(unsigned short int *width, unsigned short int *height) {
151 if (!width || !height) {
152 return ERROR_INVALID_PARAM;
153 }
154 int cols, rows;
155 if (platform_get_terminal_size(&cols, &rows) != 0) {
156 return ERROR_PLATFORM_INIT;
157 }
158 *width = (unsigned short int)cols;
159 *height = (unsigned short int)rows;
160 return ASCIICHAT_OK;
161}
162
163// Terminal UTF-8 support detection
164bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting) {
165 (void)user_setting;
166 return GET_OPTION(force_utf8) != UTF8_SETTING_FALSE;
167}
168
170 return true;
171}
172
173// Piped output detection
175 return false; // WASM mirror mode is not piped
176}
177
178// Color output detection
179bool terminal_should_color_output(int fd, const terminal_options_t *options) {
180 (void)options;
181 (void)fd;
182 return true; // WASM mirror mode supports color
183}
184
185terminal_color_mode_t terminal_get_effective_color_mode(const terminal_options_t *options) {
186 (void)options;
187 // Browser output supports color; retain the explicit option or AUTO so the
188 // caller can resolve it using the JavaScript-managed terminal capabilities.
189 return GET_OPTION(color_mode);
190}
191
192// TTY detection stubs
194 return false; // No stdin in WASM
195}
196
198 return true;
199}
200
202 return true;
203}
204
206 return false; // WASM mirror mode is not interactive
207}
208
210 return false; // Don't force stderr in WASM
211}
212
213bool terminal_can_prompt_user(bool user_setting) {
214 (void)user_setting;
215 return false; // Cannot prompt in WASM
216}
217
218// Terminal background detection
220 return true; // Default to dark background for terminals
221}
222
223// Terminal flush stub
224int terminal_flush(int fd) {
225 (void)fd;
226 return ASCIICHAT_OK;
227}
228
229// Terminal capabilities detection stub
230terminal_capabilities_t detect_terminal_capabilities(const terminal_options_t *options) {
231 (void)options;
232 terminal_capabilities_t caps = {0};
233 caps.color_level = TERM_COLOR_TRUECOLOR;
234 caps.capabilities = 0;
235 caps.color_count = 16777216;
236 caps.utf8_support = true;
237 caps.detection_reliable = true;
238 caps.render_mode = RENDER_MODE_FOREGROUND; // Default to foreground mode
239 platform_strlcpy(caps.term_type, "xterm-256color", sizeof(caps.term_type));
240 platform_strlcpy(caps.colorterm, "truecolor", sizeof(caps.colorterm));
241 caps.wants_background = true;
242 caps.palette_type = 0; // Default palette
243 // Browser rendering uses requestAnimationFrame and supports the same
244 // 60 FPS default as the native terminal paths.
245 caps.desired_fps = DEFAULT_MAX_FPS;
246 caps.color_filter = COLOR_FILTER_NONE;
247 return caps;
248}
249
250// Terminal background color query stub
252 // Return dark background (black)
253 if (bg_r)
254 *bg_r = 0;
255 if (bg_g)
256 *bg_g = 0;
257 if (bg_b)
258 *bg_b = 0;
259 return true;
260}
261
262// ============================================================================
263// Terminal Control Implementation (ANSI escape sequences)
264// ============================================================================
265
266#include <unistd.h>
267#include <stdio.h>
268
269void detect_terminal_capabilities_into(const terminal_options_t *options, terminal_capabilities_t *out) {
270 if (out)
272}
273
274void apply_color_mode_override_into(const terminal_capabilities_t *caps, const terminal_options_t *options, terminal_capabilities_t *out) {
275 if (caps && out)
276 *out = apply_color_mode_override(*caps, options);
277}
278
280 // Send ANSI escape sequence for echo control (xterm ignores but passes through)
281 if (!enabled) {
282 write(STDOUT_FILENO, "\033[?25l", 6); // Hide cursor
283 } else {
284 write(STDOUT_FILENO, "\033[?25h", 6); // Show cursor
285 }
286 return ASCIICHAT_OK;
287}
288
289bool terminal_should_use_control_sequences(int fd, bool user_setting) {
290 (void)fd;
291 (void)user_setting;
292 return true; // Always use ANSI sequences in browser
293}
294
296 // Send ANSI clear screen escape sequence
297 write(STDOUT_FILENO, "\033[2J", 4);
298 return ASCIICHAT_OK;
299}
300
302 (void)fd;
303 // Send ANSI cursor home escape sequence
304 write(STDOUT_FILENO, "\033[H", 3);
305 return ASCIICHAT_OK;
306}
307
309 if (offset == 0) {
310 return ASCIICHAT_OK;
311 }
312
313 if (offset > 0) {
314 // Move right: \x1b[<n>C
316 int len = snprintf(buf, sizeof(buf), "\033[%dC", offset);
317 if (len > 0 && len < (int)sizeof(buf)) {
318 write(STDOUT_FILENO, buf, len);
319 }
320 } else {
321 // Move left: \x1b[<n>D
323 int len = snprintf(buf, sizeof(buf), "\033[%dD", -offset);
324 if (len > 0 && len < (int)sizeof(buf)) {
325 write(STDOUT_FILENO, buf, len);
326 }
327 }
328
329 return ASCIICHAT_OK;
330}
331
333 (void)fd;
334 (void)frame_height;
335 if (out)
336 *out = NULL;
337 return ASCIICHAT_OK;
338}
339
341 (void)reader;
342}
343
345 (void)reader;
346 if (out_frame)
347 *out_frame = NULL;
348 return ASCIICHAT_OK;
349}
350
352 return false;
353}
354
356 return false;
357}
358
359asciichat_error_t ui_controller_write(int fd, const char *data, size_t len) {
360 if (!data || !len)
361 return ASCIICHAT_OK;
362 if (platform_write_all(fd, data, len) != len)
363 return SET_ERRNO(ERROR_FILE_OPERATION, "Failed to write browser log output");
364 return ASCIICHAT_OK;
365}
366
369 (void)activity;
370}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
Application limits and constraints.
int color_mode
Definition fft.c:26
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
bool enabled
Is filtering active?
Definition grep.c:62
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_PLATFORM_INIT
Definition error_codes.h:59
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_FILE_OPERATION
@ ERROR_INVALID_PARAM
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73
#define DEFAULT_MAX_FPS
Default maximum frame rate (frames per second).
#define GET_OPTION(field)
Definition options.h:1333
#define OPT_HEIGHT_DEFAULT
Default terminal height in characters.
Definition options.h:419
#define OPT_WIDTH_DEFAULT
Default terminal width in characters.
Definition options.h:408
@ UTF8_SETTING_FALSE
Force UTF-8 OFF: disable UTF-8 support.
Definition options.h:269
terminal_capabilities_t apply_color_mode_override(terminal_capabilities_t caps, const terminal_options_t *options)
Definition misc.c:40
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
static bool size_t gotel_query_t * out
Definition queries.h:8
struct terminal_fd_reader_s terminal_fd_reader_t
Read ANSI ASCII frames from file descriptor, chunked by height.
size_t platform_strlcpy(char *dst, const char *src, size_t size)
int terminal_flush(int fd)
int terminal_clear_screen(void)
terminal_capabilities_t detect_terminal_capabilities(const terminal_options_t *options)
#define PLATFORM_WASM_TERMINAL_BUF_BUFFER_SIZE
int terminal_get_size_fd(int fd, terminal_size_t *size)
unsigned short int terminal_get_effective_height(int height_option, unsigned short fallback_height)
bool terminal_should_color_output(int fd, const terminal_options_t *options)
int platform_set_terminal_raw_mode(bool enable)
int platform_get_cursor_position(int *row, int *col)
bool terminal_supports_utf8(void)
void apply_color_mode_override_into(const terminal_capabilities_t *caps, const terminal_options_t *options, terminal_capabilities_t *out)
terminal_color_mode_t terminal_get_effective_color_mode(const terminal_options_t *options)
void terminal_fd_reader_destroy(terminal_fd_reader_t *reader)
Destroy terminal FD reader.
bool terminal_is_interactive(void)
bool ui_controller_is_presenting(void)
void terminal_get_default_background_color(int theme, uint8_t *r, uint8_t *g, uint8_t *b)
int platform_write_terminal(const char *data, size_t len)
int platform_read_keyboard(char *buffer, size_t len)
bool terminal_can_prompt_user(bool user_setting)
bool platform_is_terminal(int fd)
bool ui_controller_is_owner(void)
int platform_set_cursor_position(int row, int col)
bool terminal_is_stdin_tty(void)
void ui_controller_title_activity(ui_title_activity_t activity)
bool terminal_is_stderr_tty(void)
void detect_terminal_capabilities_into(const terminal_options_t *options, terminal_capabilities_t *out)
int terminal_cursor_hide(void)
bool terminal_is_stdout_tty(void)
void ui_controller_title_ready(void)
int terminal_cursor_home(int fd)
int platform_get_terminal_size(int *cols, int *rows)
int terminal_move_cursor_relative(int offset)
unsigned short int terminal_get_effective_width(int width_option, unsigned short fallback_width)
int terminal_get_size(terminal_size_t *size)
bool terminal_has_dark_background(void)
bool terminal_should_force_stderr(void)
int terminal_cursor_show(void)
void terminal_get_default_foreground_color(int theme, uint8_t *r, uint8_t *g, uint8_t *b)
asciichat_error_t terminal_fd_reader_next(terminal_fd_reader_t *reader, char **out_frame)
Read next frame from file descriptor.
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
asciichat_error_t terminal_fd_reader_create(int fd, int frame_height, terminal_fd_reader_t **out)
Create a terminal FD reader from a file descriptor.
bool terminal_query_background_color(uint8_t *bg_r, uint8_t *bg_g, uint8_t *bg_b)
bool terminal_is_piped_output(void)
bool terminal_should_use_utf8(terminal_utf8_setting_t user_setting)
int terminal_set_echo(bool enabled)
int get_terminal_size(unsigned short int *width, unsigned short int *height)
bool terminal_should_use_control_sequences(int fd, bool user_setting)
ui_title_activity_t