ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
terminal.h
Go to the documentation of this file.
1#pragma once
2
72#include <stdbool.h>
73#include <stdint.h>
74#include "../common.h"
75#include "../log/types.h"
76
77// ============================================================================
78// Cross-platform CLI Utilities
79// ============================================================================
80
90#ifdef _WIN32
91#include "windows/getopt.h"
92#else
93#include <getopt.h>
94#endif
95
96// ============================================================================
97// Platform-Specific Signal Definitions
98// ============================================================================
99
100#ifdef _WIN32
101// Windows doesn't have these signals natively
102#define SIGWINCH 28 // Window size change (not supported on Windows)
103#define SIGTERM 15 // Termination signal (limited support on Windows)
104#endif
105
106// ============================================================================
107// Theme-Aware Default Colors
108// ============================================================================
109
116#define TERMINAL_COLOR_THEME_LIGHT_FG_R 65
117#define TERMINAL_COLOR_THEME_LIGHT_FG_G 61
118#define TERMINAL_COLOR_THEME_LIGHT_FG_B 61
119
126#define TERMINAL_COLOR_THEME_DARK_FG_R 204
127#define TERMINAL_COLOR_THEME_DARK_FG_G 204
128#define TERMINAL_COLOR_THEME_DARK_FG_B 204
129
135#define TERMINAL_COLOR_THEME_LIGHT_BG_R 255
136#define TERMINAL_COLOR_THEME_LIGHT_BG_G 255
137#define TERMINAL_COLOR_THEME_LIGHT_BG_B 255
138
144#define TERMINAL_COLOR_THEME_DARK_BG_R 0
145#define TERMINAL_COLOR_THEME_DARK_BG_G 0
146#define TERMINAL_COLOR_THEME_DARK_BG_B 0
147
148/* ============================================================================
149 * Terminal Data Structures
150 * ============================================================================
151 */
152
163typedef struct {
164 int rows;
165 int cols;
167
168/* ============================================================================
169 * Terminal Control Functions
170 * @{
171 */
172
187
207
223
238
252
265bool terminal_supports_utf8(void);
266
282
300
316
331
351
366
383
398
412
428
442
459
471
490
510
526
543
546/* ============================================================================
547 * Terminal Detection and Capabilities
548 * @{
549 */
550
590
629
653
670
741
753typedef struct {
755 int fd;
757 const char *path;
760} tty_info_t;
761
762/* ============================================================================
763 * Terminal Capability Detection Functions
764 * @{
765 */
766
790
805
819bool is_valid_tty_path(const char *path);
820
838asciichat_error_t get_terminal_size(unsigned short int *width, unsigned short int *height);
839
854
870
884
903
920
934
957
976
979/* ============================================================================
980 * Windows Console Resize Detection
981 * @{
982 *
983 * Windows-specific functionality for detecting console window resize events.
984 * Unix systems use SIGWINCH signal, but Windows requires polling or event
985 * monitoring, so a background thread is used.
986 */
987
1011
1012/* ============================================================================
1013 * Interactive Mode and TTY State Detection
1014 * @{
1015 *
1016 * Centralized functions for detecting TTY status and interactive mode.
1017 * These functions consolidate scattered isatty() checks throughout the codebase.
1018 */
1019
1034bool terminal_is_stdin_tty(void);
1035
1053bool terminal_is_stdout_tty(void);
1054
1069bool terminal_is_stderr_tty(void);
1070
1096bool terminal_is_interactive(void);
1097
1120bool terminal_is_piped_output(void);
1121
1148
1167
1200bool terminal_can_prompt_user(void);
1201
1202/* ============================================================================
1203 * Console Control and TTY Operations
1204 * ============================================================================ */
1205
1218
1230
1249
1257int platform_isatty(int fd);
1258
1270const char *platform_ttyname(int fd);
1271
1272/* ============================================================================
1273 * Renderer Color Selection (Cross-Platform Abstraction)
1274 * ============================================================================ */
1275
1288void terminal_get_default_foreground_color(int theme, uint8_t *out_r, uint8_t *out_g, uint8_t *out_b);
1289
1302void terminal_get_default_background_color(int theme, uint8_t *out_r, uint8_t *out_g, uint8_t *out_b);
1303
1340unsigned short int terminal_get_effective_width(void);
1341
1378unsigned short int terminal_get_effective_height(void);
1379
1382#ifdef _WIN32
1397typedef void (*terminal_resize_callback_t)(int cols, int rows);
1398
1417int terminal_start_resize_detection(terminal_resize_callback_t callback);
1418
1431
1433#else
1438#endif
unsigned int uint32_t
Definition common.h:58
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
log_level_t
Logging levels enumeration.
Definition types.h:29
asciichat_error_t terminal_reset(int fd)
Reset terminal to default state.
Definition misc.c:81
asciichat_error_t terminal_clear_scrollback(int fd)
Clear terminal scrollback buffer.
Definition misc.c:76
asciichat_error_t terminal_move_cursor_relative(int offset)
Move cursor relative to current position.
asciichat_error_t terminal_get_cursor_position(int *row, int *col)
Get current cursor position.
asciichat_error_t terminal_cursor_home(int fd)
Move cursor to home position (top-left)
asciichat_error_t terminal_cursor_hide(void)
Hide terminal cursor.
bool terminal_supports_unicode(void)
Check if terminal supports unicode.
bool terminal_supports_utf8(void)
Check if terminal supports UTF-8.
asciichat_error_t terminal_set_title(const char *title)
Set terminal window title.
asciichat_error_t terminal_clear_screen(void)
Clear the terminal screen.
asciichat_error_t terminal_set_raw_mode(bool enable)
Set terminal to raw mode.
asciichat_error_t terminal_save_cursor(void)
Save cursor position.
asciichat_error_t terminal_get_size(terminal_size_t *size)
Get terminal size.
asciichat_error_t terminal_move_cursor(int row, int col)
Move cursor to specified position.
asciichat_error_t terminal_restore_cursor(void)
Restore saved cursor position.
asciichat_error_t terminal_set_scroll_region(int top, int bottom)
Set scroll region.
void terminal_enable_ansi(void)
Enable ANSI escape sequences.
asciichat_error_t terminal_ring_bell(void)
Ring terminal bell.
asciichat_error_t terminal_flush(int fd)
Flush terminal output.
asciichat_error_t terminal_set_buffering(bool line_buffered)
Set terminal buffering mode.
bool terminal_supports_color(void)
Check if terminal supports color.
asciichat_error_t terminal_set_echo(bool enable)
Set terminal echo mode.
asciichat_error_t terminal_cursor_show(void)
Show terminal cursor.
ClangTool/LibTooling compatibility shim for stdbool.h.
#define bool
Definition stdbool.h:61
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
bool wants_padding
Whether client wants frame padding (centering) - false for snapshot/piped modes.
Definition terminal.h:737
uint32_t capabilities
Capability flags bitmask (terminal_capability_flags_t)
Definition terminal.h:713
size_t pad_height
Number of top padding lines when wants_padding is true.
Definition terminal.h:739
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
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
Terminal size structure.
Definition terminal.h:163
int cols
Number of columns (width) in terminal.
Definition terminal.h:165
int rows
Number of rows (height) in terminal.
Definition terminal.h:164
TTY detection and management structure.
Definition terminal.h:753
bool owns_fd
True if we opened the FD and should close it, false otherwise.
Definition terminal.h:759
int fd
File descriptor for TTY access.
Definition terminal.h:755
const char * path
Path to TTY device (e.g., "/dev/tty", "CON", etc.)
Definition terminal.h:757
bool is_valid_tty_path(const char *path)
Check if a TTY path is valid.
terminal_capabilities_t apply_color_mode_override(terminal_capabilities_t caps)
Apply command-line overrides to detected capabilities.
Definition misc.c:29
const char * terminal_color_level_name(terminal_color_mode_t level)
Get name of color level.
bool terminal_can_prompt_user(void)
Determine if interactive user prompts are appropriate.
int platform_isatty(int fd)
Check if a file descriptor is a terminal.
Definition util.c:63
tty_info_t get_current_tty(void)
Get current TTY information.
Definition misc.c:24
unsigned short int terminal_get_effective_width(void)
Get effective terminal width with fallback priority.
void terminal_get_default_background_color(int theme, uint8_t *out_r, uint8_t *out_g, uint8_t *out_b)
Get theme-aware default background color for pixel renderers.
color_filter_t
Monochromatic color filter enumeration.
Definition terminal.h:599
@ COLOR_FILTER_WHITE
White content on black background.
Definition terminal.h:605
@ COLOR_FILTER_FUCHSIA
Fuchsia (#FF00AA)
Definition terminal.h:611
@ COLOR_FILTER_NONE
No filtering (default)
Definition terminal.h:601
@ COLOR_FILTER_TEAL
Teal (#00DDDD)
Definition terminal.h:615
@ COLOR_FILTER_CYAN
Cyan (#00FFFF)
Definition terminal.h:617
@ COLOR_FILTER_GREEN
Green (#00FF41)
Definition terminal.h:607
@ COLOR_FILTER_COUNT
Total count of filters (not a valid filter)
Definition terminal.h:627
@ COLOR_FILTER_MAGENTA
Magenta (#FF00FF)
Definition terminal.h:609
@ COLOR_FILTER_RAINBOW
Rainbow (cycles through spectrum over 3.5s)
Definition terminal.h:625
@ COLOR_FILTER_RED
Red (#FF3333)
Definition terminal.h:621
@ COLOR_FILTER_YELLOW
Yellow (#FFEB99)
Definition terminal.h:623
@ COLOR_FILTER_PINK
Pink (#FFB6C1)
Definition terminal.h:619
@ COLOR_FILTER_BLACK
Dark content on white background.
Definition terminal.h:603
@ COLOR_FILTER_ORANGE
Orange (#FF8800)
Definition terminal.h:613
void terminal_stop_resize_detection(void)
No-op stub on POSIX platforms (no resize detection thread)
Definition misc.c:91
bool(* console_ctrl_handler_t)(console_ctrl_event_t event)
Console control handler callback type.
Definition terminal.h:1229
bool terminal_is_interactive(void)
Check if the session is fully interactive.
bool platform_set_console_ctrl_handler(console_ctrl_handler_t handler)
Register a console control handler (for Ctrl+C, etc.)
terminal_capability_flags_t
Terminal capability flags (bitmask)
Definition terminal.h:639
@ TERM_CAP_COLOR_256
256-color support (TERM_CAP_COLOR_256)
Definition terminal.h:643
@ TERM_CAP_COLOR_16
16-color support (TERM_CAP_COLOR_16)
Definition terminal.h:641
@ TERM_CAP_UTF8
UTF-8 encoding support (TERM_CAP_UTF8)
Definition terminal.h:647
@ TERM_CAP_MATRIX_RAIN
Client requests Matrix digital-rain post-processing.
Definition terminal.h:651
@ TERM_CAP_BACKGROUND
Background color support (TERM_CAP_BACKGROUND)
Definition terminal.h:649
@ TERM_CAP_COLOR_TRUE
Truecolor support (TERM_CAP_COLOR_TRUE)
Definition terminal.h:645
bool terminal_is_stdin_tty(void)
Check if stdin is connected to a TTY.
int terminal_choose_log_fd(log_level_t level)
Choose output file descriptor for logging based on level and interactivity.
const char * platform_ttyname(int fd)
Get the name of the terminal associated with a file descriptor.
void test_terminal_output_modes(void)
Test terminal output modes.
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.
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.
render_mode_t
Render mode preferences.
Definition terminal.h:662
@ RENDER_MODE_FOREGROUND
Foreground colors only (text color)
Definition terminal.h:664
@ RENDER_MODE_BACKGROUND
Background colors (block colors)
Definition terminal.h:666
@ RENDER_MODE_HALF_BLOCK
Unicode half-block characters (mixed foreground/background)
Definition terminal.h:668
terminal_color_mode_t
Terminal color support levels.
Definition terminal.h:578
@ TERM_COLOR_NONE
No color support (monochrome terminal)
Definition terminal.h:582
@ TERM_COLOR_16
16-color support (standard ANSI colors)
Definition terminal.h:584
@ TERM_COLOR_256
256-color support (extended ANSI palette)
Definition terminal.h:586
@ TERM_COLOR_AUTO
Auto-detect color support from terminal capabilities.
Definition terminal.h:580
@ TERM_COLOR_TRUECOLOR
24-bit truecolor support (RGB colors)
Definition terminal.h:588
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.
unsigned short int terminal_get_effective_height(void)
Get effective terminal height with fallback priority.
bool terminal_is_piped_output(void)
Check if stdout is piped or redirected.
const char * terminal_capabilities_summary(const terminal_capabilities_t *caps)
Get summary string of terminal capabilities.
console_ctrl_event_t
Console control event types (cross-platform Ctrl+C handling)
Definition terminal.h:1211
@ CONSOLE_CTRL_BREAK
Definition terminal.h:1213
@ CONSOLE_SHUTDOWN
Definition terminal.h:1216
@ CONSOLE_LOGOFF
Definition terminal.h:1215
@ CONSOLE_CTRL_C
Definition terminal.h:1212
@ CONSOLE_CLOSE
Definition terminal.h:1214
terminal_color_mode_t terminal_get_effective_color_mode(void)
Get current color mode considering all overrides.
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.
void terminal_get_default_foreground_color(int theme, uint8_t *out_r, uint8_t *out_g, uint8_t *out_b)
Get theme-aware default foreground color for pixel renderers.