vt: expose terminal modes to C API

Add modes.h with GhosttyModeTag (uint16_t) matching the Zig ModeTag
packed struct layout, along with inline helpers for constructing and
inspecting mode tags. Provide GHOSTTY_MODE_* macros for all 39
built-in modes (4 ANSI, 35 DEC), parenthesized for safety.

Add ghostty_terminal_mode_get and ghostty_terminal_mode_set to
terminal.h, both returning GhosttyResult so that null terminals
and unknown mode tags return GHOSTTY_INVALID_VALUE. The get function
writes its result through a bool out-parameter.

Add a note in the Zig mode entries reminding developers to update
modes.h when adding new modes.
This commit is contained in:
Mitchell Hashimoto
2026-03-16 15:57:39 -07:00
parent 25679f3ae7
commit 1c03770e2b
6 changed files with 203 additions and 1 deletions

View File

@@ -53,6 +53,58 @@
extern "C" {
#endif
/** @name ANSI Mode Tags
* Mode tags for standard ANSI modes.
* @{
*/
#define GHOSTTY_MODE_KAM (ghostty_mode_tag_new(2, true)) /**< Keyboard action (disable keyboard) */
#define GHOSTTY_MODE_INSERT (ghostty_mode_tag_new(4, true)) /**< Insert mode */
#define GHOSTTY_MODE_SRM (ghostty_mode_tag_new(12, true)) /**< Send/receive mode */
#define GHOSTTY_MODE_LINEFEED (ghostty_mode_tag_new(20, true)) /**< Linefeed/new line mode */
/** @} */
/** @name DEC Private Mode Tags
* Mode tags for DEC private modes (?-prefixed).
* @{
*/
#define GHOSTTY_MODE_DECCKM (ghostty_mode_tag_new(1, false)) /**< Cursor keys */
#define GHOSTTY_MODE_132_COLUMN (ghostty_mode_tag_new(3, false)) /**< 132/80 column mode */
#define GHOSTTY_MODE_SLOW_SCROLL (ghostty_mode_tag_new(4, false)) /**< Slow scroll */
#define GHOSTTY_MODE_REVERSE_COLORS (ghostty_mode_tag_new(5, false)) /**< Reverse video */
#define GHOSTTY_MODE_ORIGIN (ghostty_mode_tag_new(6, false)) /**< Origin mode */
#define GHOSTTY_MODE_WRAPAROUND (ghostty_mode_tag_new(7, false)) /**< Auto-wrap mode */
#define GHOSTTY_MODE_AUTOREPEAT (ghostty_mode_tag_new(8, false)) /**< Auto-repeat keys */
#define GHOSTTY_MODE_X10_MOUSE (ghostty_mode_tag_new(9, false)) /**< X10 mouse reporting */
#define GHOSTTY_MODE_CURSOR_BLINKING (ghostty_mode_tag_new(12, false)) /**< Cursor blink */
#define GHOSTTY_MODE_CURSOR_VISIBLE (ghostty_mode_tag_new(25, false)) /**< Cursor visible (DECTCEM) */
#define GHOSTTY_MODE_ENABLE_MODE_3 (ghostty_mode_tag_new(40, false)) /**< Allow 132 column mode */
#define GHOSTTY_MODE_REVERSE_WRAP (ghostty_mode_tag_new(45, false)) /**< Reverse wrap */
#define GHOSTTY_MODE_ALT_SCREEN_LEGACY (ghostty_mode_tag_new(47, false)) /**< Alternate screen (legacy) */
#define GHOSTTY_MODE_KEYPAD_KEYS (ghostty_mode_tag_new(66, false)) /**< Application keypad */
#define GHOSTTY_MODE_LEFT_RIGHT_MARGIN (ghostty_mode_tag_new(69, false)) /**< Left/right margin mode */
#define GHOSTTY_MODE_NORMAL_MOUSE (ghostty_mode_tag_new(1000, false)) /**< Normal mouse tracking */
#define GHOSTTY_MODE_BUTTON_MOUSE (ghostty_mode_tag_new(1002, false)) /**< Button-event mouse tracking */
#define GHOSTTY_MODE_ANY_MOUSE (ghostty_mode_tag_new(1003, false)) /**< Any-event mouse tracking */
#define GHOSTTY_MODE_FOCUS_EVENT (ghostty_mode_tag_new(1004, false)) /**< Focus in/out events */
#define GHOSTTY_MODE_UTF8_MOUSE (ghostty_mode_tag_new(1005, false)) /**< UTF-8 mouse format */
#define GHOSTTY_MODE_SGR_MOUSE (ghostty_mode_tag_new(1006, false)) /**< SGR mouse format */
#define GHOSTTY_MODE_ALT_SCROLL (ghostty_mode_tag_new(1007, false)) /**< Alternate scroll mode */
#define GHOSTTY_MODE_URXVT_MOUSE (ghostty_mode_tag_new(1015, false)) /**< URxvt mouse format */
#define GHOSTTY_MODE_SGR_PIXELS_MOUSE (ghostty_mode_tag_new(1016, false)) /**< SGR-Pixels mouse format */
#define GHOSTTY_MODE_NUMLOCK_KEYPAD (ghostty_mode_tag_new(1035, false)) /**< Ignore keypad with NumLock */
#define GHOSTTY_MODE_ALT_ESC_PREFIX (ghostty_mode_tag_new(1036, false)) /**< Alt key sends ESC prefix */
#define GHOSTTY_MODE_ALT_SENDS_ESC (ghostty_mode_tag_new(1039, false)) /**< Alt sends escape */
#define GHOSTTY_MODE_REVERSE_WRAP_EXT (ghostty_mode_tag_new(1045, false)) /**< Extended reverse wrap */
#define GHOSTTY_MODE_ALT_SCREEN (ghostty_mode_tag_new(1047, false)) /**< Alternate screen */
#define GHOSTTY_MODE_SAVE_CURSOR (ghostty_mode_tag_new(1048, false)) /**< Save cursor (DECSC) */
#define GHOSTTY_MODE_ALT_SCREEN_SAVE (ghostty_mode_tag_new(1049, false)) /**< Alt screen + save cursor + clear */
#define GHOSTTY_MODE_BRACKETED_PASTE (ghostty_mode_tag_new(2004, false)) /**< Bracketed paste mode */
#define GHOSTTY_MODE_SYNC_OUTPUT (ghostty_mode_tag_new(2026, false)) /**< Synchronized output */
#define GHOSTTY_MODE_GRAPHEME_CLUSTER (ghostty_mode_tag_new(2027, false)) /**< Grapheme cluster mode */
#define GHOSTTY_MODE_COLOR_SCHEME_REPORT (ghostty_mode_tag_new(2031, false)) /**< Report color scheme */
#define GHOSTTY_MODE_IN_BAND_RESIZE (ghostty_mode_tag_new(2048, false)) /**< In-band size reports */
/** @} */
/**
* A packed 16-bit terminal mode tag.
*

View File

@@ -7,10 +7,12 @@
#ifndef GHOSTTY_VT_TERMINAL_H
#define GHOSTTY_VT_TERMINAL_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <ghostty/vt/types.h>
#include <ghostty/vt/allocator.h>
#include <ghostty/vt/modes.h>
#ifdef __cplusplus
extern "C" {
@@ -192,7 +194,42 @@ void ghostty_terminal_vt_write(GhosttyTerminal terminal,
* @ingroup terminal
*/
void ghostty_terminal_scroll_viewport(GhosttyTerminal terminal,
GhosttyTerminalScrollViewport behavior);
GhosttyTerminalScrollViewport behavior);
/**
* Get the current value of a terminal mode.
*
* Returns the value of the mode identified by the given mode tag.
*
* @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
* @param tag The mode tag identifying the mode to query
* @param[out] out_value On success, set to true if the mode is set, false
* if it is reset
* @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the terminal
* is NULL or the tag does not correspond to a known mode
*
* @ingroup terminal
*/
GhosttyResult ghostty_terminal_mode_get(GhosttyTerminal terminal,
GhosttyModeTag tag,
bool* out_value);
/**
* Set the value of a terminal mode.
*
* Sets the mode identified by the given mode tag to the specified value.
*
* @param terminal The terminal handle (NULL returns GHOSTTY_INVALID_VALUE)
* @param tag The mode tag identifying the mode to set
* @param value true to set the mode, false to reset it
* @return GHOSTTY_SUCCESS on success, GHOSTTY_INVALID_VALUE if the terminal
* is NULL or the tag does not correspond to a known mode
*
* @ingroup terminal
*/
GhosttyResult ghostty_terminal_mode_set(GhosttyTerminal terminal,
GhosttyModeTag tag,
bool value);
/** @} */