ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
terminal.c File Reference

Go to the source code of this file.

Macros

#define PLATFORM_WASM_TERMINAL_BUF_BUFFER_SIZE   32

Functions

void terminal_get_default_foreground_color (int theme, uint8_t *r, uint8_t *g, uint8_t *b)
void terminal_get_default_background_color (int theme, uint8_t *r, uint8_t *g, uint8_t *b)
int platform_get_terminal_size (int *cols, int *rows)
int terminal_get_size_fd (int fd, terminal_size_t *size)
int terminal_get_size (terminal_size_t *size)
int terminal_cursor_hide (void)
int terminal_cursor_show (void)
unsigned short int terminal_get_effective_width (int width_option, unsigned short fallback_width)
unsigned short int terminal_get_effective_height (int height_option, unsigned short fallback_height)
int platform_write_terminal (const char *data, size_t len)
int platform_set_terminal_raw_mode (bool enable)
int platform_read_keyboard (char *buffer, size_t len)
bool platform_is_terminal (int fd)
int platform_get_cursor_position (int *row, int *col)
int platform_set_cursor_position (int row, int col)
int get_terminal_size (unsigned short int *width, unsigned short int *height)
bool terminal_should_use_utf8 (terminal_utf8_setting_t user_setting)
bool terminal_supports_utf8 (void)
bool terminal_is_piped_output (void)
bool terminal_should_color_output (int fd, const terminal_options_t *options)
terminal_color_mode_t terminal_get_effective_color_mode (const terminal_options_t *options)
bool terminal_is_stdin_tty (void)
bool terminal_is_stdout_tty (void)
bool terminal_is_stderr_tty (void)
bool terminal_is_interactive (void)
bool terminal_should_force_stderr (void)
bool terminal_can_prompt_user (bool user_setting)
bool terminal_has_dark_background (void)
int terminal_flush (int fd)
terminal_capabilities_t detect_terminal_capabilities (const terminal_options_t *options)
bool terminal_query_background_color (uint8_t *bg_r, uint8_t *bg_g, uint8_t *bg_b)
void detect_terminal_capabilities_into (const terminal_options_t *options, terminal_capabilities_t *out)
void apply_color_mode_override_into (const terminal_capabilities_t *caps, const terminal_options_t *options, terminal_capabilities_t *out)
int terminal_set_echo (bool enabled)
bool terminal_should_use_control_sequences (int fd, bool user_setting)
int terminal_clear_screen (void)
int terminal_cursor_home (int fd)
int terminal_move_cursor_relative (int offset)
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.
void terminal_fd_reader_destroy (terminal_fd_reader_t *reader)
 Destroy terminal FD reader.
asciichat_error_t terminal_fd_reader_next (terminal_fd_reader_t *reader, char **out_frame)
 Read next frame from file descriptor.
bool ui_controller_is_presenting (void)
bool ui_controller_is_owner (void)
asciichat_error_t ui_controller_write (int fd, const char *data, size_t len)
void ui_controller_title_ready (void)
void ui_controller_title_activity (ui_title_activity_t activity)

Macro Definition Documentation

◆ PLATFORM_WASM_TERMINAL_BUF_BUFFER_SIZE

#define PLATFORM_WASM_TERMINAL_BUF_BUFFER_SIZE   32

Definition at line 8 of file src/web/platform/terminal.c.

Referenced by terminal_move_cursor_relative().

Function Documentation

◆ apply_color_mode_override_into()

void apply_color_mode_override_into ( const terminal_capabilities_t * caps,
const terminal_options_t * options,
terminal_capabilities_t * out )

Definition at line 274 of file src/web/platform/terminal.c.

274 {
275 if (caps && out)
276 *out = apply_color_mode_override(*caps, options);
277}
terminal_capabilities_t apply_color_mode_override(terminal_capabilities_t caps, const terminal_options_t *options)
Definition misc.c:40
static bool size_t gotel_query_t * out
Definition queries.h:8

References apply_color_mode_override(), and out.

Referenced by main().

◆ detect_terminal_capabilities()

terminal_capabilities_t detect_terminal_capabilities ( const terminal_options_t * options)

Definition at line 230 of file src/web/platform/terminal.c.

230 {
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}
#define DEFAULT_MAX_FPS
Default maximum frame rate (frames per second).
size_t platform_strlcpy(char *dst, const char *src, size_t size)

References DEFAULT_MAX_FPS, and platform_strlcpy().

Referenced by detect_terminal_capabilities_into(), session_display_create(), session_participant_request_settings(), session_protocol_build_capabilities(), session_protocol_update_settings(), tui_stats_render(), ui_session_render(), and ui_sync_render().

◆ detect_terminal_capabilities_into()

void detect_terminal_capabilities_into ( const terminal_options_t * options,
terminal_capabilities_t * out )

Definition at line 269 of file src/web/platform/terminal.c.

269 {
270 if (out)
272}
terminal_capabilities_t detect_terminal_capabilities(const terminal_options_t *options)

References detect_terminal_capabilities(), and out.

Referenced by action_show_capabilities_immediate(), log_redetect_terminal_capabilities(), and main().

◆ get_terminal_size()

int get_terminal_size ( unsigned short int * width,
unsigned short int * height )

Definition at line 150 of file src/web/platform/terminal.c.

150 {
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}
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
@ ERROR_PLATFORM_INIT
Definition error_codes.h:59
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
int platform_get_terminal_size(int *cols, int *rows)

References ASCIICHAT_OK, ERROR_INVALID_PARAM, ERROR_PLATFORM_INIT, height, platform_get_terminal_size(), and width.

Referenced by update_dimensions_for_full_height(), and update_dimensions_to_terminal_size().

◆ platform_get_cursor_position()

int platform_get_cursor_position ( int * row,
int * col )

Definition at line 137 of file src/web/platform/terminal.c.

137 {
138 (void)row;
139 (void)col;
140 return -1; // Not supported in WASM
141}

◆ platform_get_terminal_size()

int platform_get_terminal_size ( int * cols,
int * rows )

Definition at line 61 of file src/web/platform/terminal.c.

61 {
62 if (!cols || !rows) {
63 return -1;
64 }
65 *cols = js_get_terminal_cols();
66 *rows = js_get_terminal_rows();
67 return 0;
68}

Referenced by get_terminal_size().

◆ platform_is_terminal()

bool platform_is_terminal ( int fd)

Definition at line 132 of file src/web/platform/terminal.c.

132 {
133 (void)fd;
134 return true; // Always true for WASM terminal
135}

◆ platform_read_keyboard()

int platform_read_keyboard ( char * buffer,
size_t len )

Definition at line 126 of file src/web/platform/terminal.c.

126 {
127 (void)buffer;
128 (void)len;
129 return -1; // Not supported: use JavaScript event listeners
130}

◆ platform_set_cursor_position()

int platform_set_cursor_position ( int row,
int col )

Definition at line 143 of file src/web/platform/terminal.c.

143 {
144 (void)row;
145 (void)col;
146 return -1; // Not supported in WASM
147}

◆ platform_set_terminal_raw_mode()

int platform_set_terminal_raw_mode ( bool enable)

Definition at line 121 of file src/web/platform/terminal.c.

121 {
122 (void)enable;
123 return 0;
124}

◆ platform_write_terminal()

int platform_write_terminal ( const char * data,
size_t len )

Definition at line 115 of file src/web/platform/terminal.c.

115 {
116 js_terminal_write(data, (int)len);
117 return (int)len;
118}

◆ terminal_can_prompt_user()

bool terminal_can_prompt_user ( bool user_setting)

Definition at line 213 of file src/web/platform/terminal.c.

213 {
214 (void)user_setting;
215 return false; // Cannot prompt in WASM
216}

Referenced by prompt_unknown_host(), ui_mdns_prompt_selection(), ui_notice_confirm_kind(), and update_banner_show_prompt().

◆ terminal_clear_screen()

int terminal_clear_screen ( void )

Definition at line 295 of file src/web/platform/terminal.c.

295 {
296 // Send ANSI clear screen escape sequence
297 write(STDOUT_FILENO, "\033[2J", 4);
298 return ASCIICHAT_OK;
299}

References ASCIICHAT_OK.

◆ terminal_cursor_hide()

int terminal_cursor_hide ( void )

Definition at line 80 of file src/web/platform/terminal.c.

80 {
81 return ASCIICHAT_OK;
82}

References ASCIICHAT_OK.

◆ terminal_cursor_home()

int terminal_cursor_home ( int fd)

Definition at line 301 of file src/web/platform/terminal.c.

301 {
302 (void)fd;
303 // Send ANSI cursor home escape sequence
304 write(STDOUT_FILENO, "\033[H", 3);
305 return ASCIICHAT_OK;
306}

References ASCIICHAT_OK.

◆ terminal_cursor_show()

int terminal_cursor_show ( void )

Definition at line 84 of file src/web/platform/terminal.c.

84 {
85 return ASCIICHAT_OK;
86}

References ASCIICHAT_OK.

Referenced by log_search_enter_mode().

◆ terminal_fd_reader_create()

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.

Parameters
fdFile descriptor to read from (e.g., STDIN_FILENO, open file)
frame_heightNumber of lines per frame (required - used for frame boundaries)
outPointer to store reader context
Returns
ASCIICHAT_OK on success

Width is auto-detected from the first frame's line length.

Definition at line 332 of file src/web/platform/terminal.c.

332 {
333 (void)fd;
334 (void)frame_height;
335 if (out)
336 *out = NULL;
337 return ASCIICHAT_OK;
338}

References ASCIICHAT_OK, and out.

◆ terminal_fd_reader_destroy()

void terminal_fd_reader_destroy ( terminal_fd_reader_t * reader)

Destroy terminal FD reader.

Parameters
readerReader context to destroy

Definition at line 340 of file src/web/platform/terminal.c.

340 {
341 (void)reader;
342}

◆ terminal_fd_reader_next()

asciichat_error_t terminal_fd_reader_next ( terminal_fd_reader_t * reader,
char ** out_frame )

Read next frame from file descriptor.

Reads frame_height lines from the FD and returns them as a single frame string. Returns NULL in out_frame when EOF is reached.

Parameters
readerReader context
out_framePointer to store frame data (NULL when EOF, must free with SAFE_FREE)
Returns
ASCIICHAT_OK on success (including EOF)

Definition at line 344 of file src/web/platform/terminal.c.

344 {
345 (void)reader;
346 if (out_frame)
347 *out_frame = NULL;
348 return ASCIICHAT_OK;
349}

References ASCIICHAT_OK.

◆ terminal_flush()

int terminal_flush ( int fd)

Definition at line 224 of file src/web/platform/terminal.c.

224 {
225 (void)fd;
226 return ASCIICHAT_OK;
227}

References ASCIICHAT_OK.

Referenced by config_create_default(), session_display_write_ascii_frame(), and session_display_write_raw().

◆ terminal_get_default_background_color()

void terminal_get_default_background_color ( int theme,
uint8_t * r,
uint8_t * g,
uint8_t * b )

Definition at line 51 of file src/web/platform/terminal.c.

51 {
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}
unsigned char uint8_t

Referenced by term_renderer_feed().

◆ terminal_get_default_foreground_color()

void terminal_get_default_foreground_color ( int theme,
uint8_t * r,
uint8_t * g,
uint8_t * b )

Definition at line 41 of file src/web/platform/terminal.c.

41 {
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}

Referenced by term_renderer_feed().

◆ terminal_get_effective_color_mode()

terminal_color_mode_t terminal_get_effective_color_mode ( const terminal_options_t * options)

Definition at line 185 of file src/web/platform/terminal.c.

185 {
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}
int color_mode
Definition fft.c:26
#define GET_OPTION(field)
Definition options.h:1333

References color_mode, and GET_OPTION.

◆ terminal_get_effective_height()

unsigned short int terminal_get_effective_height ( int height_option,
unsigned short fallback_height )

Definition at line 100 of file src/web/platform/terminal.c.

100 {
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}
#define OPT_HEIGHT_DEFAULT
Default terminal height in characters.
Definition options.h:419

References GET_OPTION, height, and OPT_HEIGHT_DEFAULT.

Referenced by client_protocol_start(), mirror_convert_frame(), and session_display_create().

◆ terminal_get_effective_width()

unsigned short int terminal_get_effective_width ( int width_option,
unsigned short fallback_width )

Definition at line 88 of file src/web/platform/terminal.c.

88 {
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}
#define OPT_WIDTH_DEFAULT
Default terminal width in characters.
Definition options.h:408

References GET_OPTION, OPT_WIDTH_DEFAULT, and width.

Referenced by client_protocol_start(), mirror_convert_frame(), and session_display_create().

◆ terminal_get_size()

int terminal_get_size ( terminal_size_t * size)

Definition at line 75 of file src/web/platform/terminal.c.

75 {
76 (void)size;
77 return SET_ERRNO(ERROR_NOT_SUPPORTED, "Terminal operations not supported in WASM");
78}
#define SET_ERRNO(code,...)
@ ERROR_NOT_SUPPORTED
Definition error_codes.h:73

References ERROR_NOT_SUPPORTED, and SET_ERRNO.

Referenced by action_show_capabilities_immediate(), sdp_detect_terminal_capabilities(), and terminal_get_size_fd().

◆ terminal_get_size_fd()

int terminal_get_size_fd ( int fd,
terminal_size_t * size )

Definition at line 70 of file src/web/platform/terminal.c.

70 {
71 (void)fd;
72 return terminal_get_size(size);
73}
int terminal_get_size(terminal_size_t *size)

References terminal_get_size().

Referenced by session_display_write_ascii_frame(), and ui_notice_flush_snapshot().

◆ terminal_has_dark_background()

bool terminal_has_dark_background ( void )

Definition at line 219 of file src/web/platform/terminal.c.

219 {
220 return true; // Default to dark background for terminals
221}

Referenced by detect_terminal_background().

◆ terminal_is_interactive()

◆ terminal_is_piped_output()

bool terminal_is_piped_output ( void )

Definition at line 174 of file src/web/platform/terminal.c.

174 {
175 return false; // WASM mirror mode is not piped
176}

Referenced by asciichat_shared_init(), and log_init().

◆ terminal_is_stderr_tty()

bool terminal_is_stderr_tty ( void )

Definition at line 201 of file src/web/platform/terminal.c.

201 {
202 return true;
203}

◆ terminal_is_stdin_tty()

bool terminal_is_stdin_tty ( void )

◆ terminal_is_stdout_tty()

bool terminal_is_stdout_tty ( void )

◆ terminal_move_cursor_relative()

int terminal_move_cursor_relative ( int offset)

Definition at line 308 of file src/web/platform/terminal.c.

308 {
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}
#define PLATFORM_WASM_TERMINAL_BUF_BUFFER_SIZE

References ASCIICHAT_OK, and PLATFORM_WASM_TERMINAL_BUF_BUFFER_SIZE.

◆ terminal_query_background_color()

bool terminal_query_background_color ( uint8_t * bg_r,
uint8_t * bg_g,
uint8_t * bg_b )

Definition at line 251 of file src/web/platform/terminal.c.

251 {
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}

◆ terminal_set_echo()

int terminal_set_echo ( bool enabled)

Definition at line 279 of file src/web/platform/terminal.c.

279 {
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}
bool enabled
Is filtering active?
Definition grep.c:62

References ASCIICHAT_OK, and enabled.

Referenced by ascii_write_destroy(), and ascii_write_init().

◆ terminal_should_color_output()

bool terminal_should_color_output ( int fd,
const terminal_options_t * options )

◆ terminal_should_force_stderr()

bool terminal_should_force_stderr ( void )

Definition at line 209 of file src/web/platform/terminal.c.

209 {
210 return false; // Don't force stderr in WASM
211}

Referenced by main(), and session_display_create().

◆ terminal_should_use_control_sequences()

bool terminal_should_use_control_sequences ( int fd,
bool user_setting )

Definition at line 289 of file src/web/platform/terminal.c.

289 {
290 (void)fd;
291 (void)user_setting;
292 return true; // Always use ANSI sequences in browser
293}

Referenced by ascii_write(), ascii_write_destroy(), and ascii_write_init().

◆ terminal_should_use_utf8()

◆ terminal_supports_utf8()

bool terminal_supports_utf8 ( void )

Definition at line 169 of file src/web/platform/terminal.c.

169 {
170 return true;
171}

Referenced by detect_client_utf8_support(), and utf8_output_enabled().

◆ ui_controller_is_owner()

bool ui_controller_is_owner ( void )

Definition at line 355 of file src/web/platform/terminal.c.

355 {
356 return false;
357}

◆ ui_controller_is_presenting()

bool ui_controller_is_presenting ( void )

Lock-free query for console logging, including logs from rendering callbacks.

Definition at line 351 of file src/web/platform/terminal.c.

351 {
352 return false;
353}

◆ ui_controller_title_activity()

void ui_controller_title_activity ( ui_title_activity_t activity)

Definition at line 368 of file src/web/platform/terminal.c.

368 {
369 (void)activity;
370}

◆ ui_controller_title_ready()

void ui_controller_title_ready ( void )

Definition at line 367 of file src/web/platform/terminal.c.

367{}

◆ ui_controller_write()

asciichat_error_t ui_controller_write ( int fd,
const char * data,
size_t len )

Final output sink. Live screen writes must originate in a presentation callback. Other terminal text is suppressed while a screen owns the terminal; non-TTY output remains synchronous.

Definition at line 359 of file src/web/platform/terminal.c.

359 {
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}
@ ERROR_FILE_OPERATION

References ASCIICHAT_OK, ERROR_FILE_OPERATION, and SET_ERRNO.