libghostty
Loading...
Searching...
No Matches
Mouse Encoding

Detailed Description

Utilities for encoding mouse events into terminal escape sequences, supporting X10, UTF-8, SGR, URxvt, and SGR-Pixels mouse protocols.

Basic Usage

  1. Create an encoder instance with ghostty_mouse_encoder_new().
  2. Configure encoder options with ghostty_mouse_encoder_setopt() or ghostty_mouse_encoder_setopt_from_terminal().
  3. For each mouse event:
  4. Free the encoder with ghostty_mouse_encoder_free() when done.

For a complete working example, see example/c-vt-encode-mouse in the repository.

Example

int main() {
// Create encoder
GhosttyResult result = ghostty_mouse_encoder_new(NULL, &encoder);
assert(result == GHOSTTY_SUCCESS);
// Configure SGR format with normal tracking
&(GhosttyMouseFormat){GHOSTTY_MOUSE_FORMAT_SGR});
// Set terminal geometry for coordinate mapping
.size = sizeof(GhosttyMouseEncoderSize),
.screen_width = 800, .screen_height = 600,
.cell_width = 10, .cell_height = 20,
});
// Create and configure a left button press event
result = ghostty_mouse_event_new(NULL, &event);
assert(result == GHOSTTY_SUCCESS);
ghostty_mouse_event_set_button(event, GHOSTTY_MOUSE_BUTTON_LEFT);
(GhosttyMousePosition){.x = 50.0f, .y = 40.0f});
// Encode the mouse event
char buf[128];
size_t written = 0;
result = ghostty_mouse_encoder_encode(encoder, event,
buf, sizeof(buf), &written);
assert(result == GHOSTTY_SUCCESS);
// Use the encoded sequence (e.g., write to terminal)
fwrite(buf, 1, written, stdout);
// Cleanup
return 0;
}

Example: Encoding with Terminal State

When you have a GhosttyTerminal, you can sync its tracking mode and output format into the encoder automatically:

// Create a terminal and feed it some VT data that enables mouse tracking
GhosttyTerminal terminal;
ghostty_terminal_new(NULL, &terminal,
(GhosttyTerminalOptions){.cols = 80, .rows = 24, .max_scrollback = 0});
// Application might write data that enables mouse reporting, etc.
ghostty_terminal_vt_write(terminal, vt_data, vt_len);
// Create an encoder and sync its options from the terminal
ghostty_mouse_encoder_new(NULL, &encoder);
// Encode a mouse event using the terminal-derived options
char buf[128];
size_t written = 0;
ghostty_mouse_encoder_encode(encoder, event, buf, sizeof(buf), &written);
struct GhosttyMouseEncoderImpl * GhosttyMouseEncoder
GHOSTTY_API GhosttyResult ghostty_mouse_encoder_encode(GhosttyMouseEncoder encoder, GhosttyMouseEvent event, char *out_buf, size_t out_buf_size, size_t *out_len)
GHOSTTY_API void ghostty_mouse_encoder_setopt_from_terminal(GhosttyMouseEncoder encoder, GhosttyTerminal terminal)
GHOSTTY_API GhosttyResult ghostty_mouse_encoder_new(const GhosttyAllocator *allocator, GhosttyMouseEncoder *encoder)
GHOSTTY_API void ghostty_mouse_encoder_free(GhosttyMouseEncoder encoder)
struct GhosttyTerminalImpl * GhosttyTerminal
Definition types.h:95
GHOSTTY_API GhosttyResult ghostty_terminal_new(const GhosttyAllocator *allocator, GhosttyTerminal *terminal, GhosttyTerminalOptions options)
GHOSTTY_API void ghostty_terminal_free(GhosttyTerminal terminal)
GHOSTTY_API void ghostty_terminal_vt_write(GhosttyTerminal terminal, const uint8_t *data, size_t len)

Typedefs

typedef struct GhosttyMouseEncoderImpl * GhosttyMouseEncoder
typedef struct GhosttyMouseEventImpl * GhosttyMouseEvent

Enumerations

enum  GhosttyMouseTrackingMode {
  GHOSTTY_MOUSE_TRACKING_NONE = 0 , GHOSTTY_MOUSE_TRACKING_X10 = 1 , GHOSTTY_MOUSE_TRACKING_NORMAL = 2 , GHOSTTY_MOUSE_TRACKING_BUTTON = 3 ,
  GHOSTTY_MOUSE_TRACKING_ANY = 4 , GHOSTTY_MOUSE_TRACKING_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE
}
enum  GhosttyMouseFormat
enum  GhosttyMouseEncoderOption {
  GHOSTTY_MOUSE_ENCODER_OPT_EVENT = 0 , GHOSTTY_MOUSE_ENCODER_OPT_FORMAT = 1 , GHOSTTY_MOUSE_ENCODER_OPT_SIZE = 2 , GHOSTTY_MOUSE_ENCODER_OPT_ANY_BUTTON_PRESSED = 3 ,
  GHOSTTY_MOUSE_ENCODER_OPT_TRACK_LAST_CELL = 4 , GHOSTTY_MOUSE_ENCODER_OPT_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE
}
enum  GhosttyMouseAction { GHOSTTY_MOUSE_ACTION_PRESS = 0 , GHOSTTY_MOUSE_ACTION_RELEASE = 1 , GHOSTTY_MOUSE_ACTION_MOTION = 2 , GHOSTTY_MOUSE_ACTION_MAX_VALUE = GHOSTTY_ENUM_MAX_VALUE }
enum  GhosttyMouseButton

Functions

GHOSTTY_API GhosttyResult ghostty_mouse_encoder_new (const GhosttyAllocator *allocator, GhosttyMouseEncoder *encoder)
GHOSTTY_API void ghostty_mouse_encoder_free (GhosttyMouseEncoder encoder)
GHOSTTY_API void ghostty_mouse_encoder_setopt (GhosttyMouseEncoder encoder, GhosttyMouseEncoderOption option, const void *value)
GHOSTTY_API void ghostty_mouse_encoder_setopt_from_terminal (GhosttyMouseEncoder encoder, GhosttyTerminal terminal)
GHOSTTY_API void ghostty_mouse_encoder_reset (GhosttyMouseEncoder encoder)
GHOSTTY_API GhosttyResult ghostty_mouse_encoder_encode (GhosttyMouseEncoder encoder, GhosttyMouseEvent event, char *out_buf, size_t out_buf_size, size_t *out_len)
GHOSTTY_API GhosttyResult ghostty_mouse_event_new (const GhosttyAllocator *allocator, GhosttyMouseEvent *event)
GHOSTTY_API void ghostty_mouse_event_free (GhosttyMouseEvent event)
GHOSTTY_API void ghostty_mouse_event_set_action (GhosttyMouseEvent event, GhosttyMouseAction action)
GHOSTTY_API GhosttyMouseAction ghostty_mouse_event_get_action (GhosttyMouseEvent event)
GHOSTTY_API void ghostty_mouse_event_set_button (GhosttyMouseEvent event, GhosttyMouseButton button)
GHOSTTY_API void ghostty_mouse_event_clear_button (GhosttyMouseEvent event)
GHOSTTY_API bool ghostty_mouse_event_get_button (GhosttyMouseEvent event, GhosttyMouseButton *out_button)
GHOSTTY_API void ghostty_mouse_event_set_mods (GhosttyMouseEvent event, GhosttyMods mods)
GHOSTTY_API GhosttyMods ghostty_mouse_event_get_mods (GhosttyMouseEvent event)
GHOSTTY_API void ghostty_mouse_event_set_position (GhosttyMouseEvent event, GhosttyMousePosition position)
GHOSTTY_API GhosttyMousePosition ghostty_mouse_event_get_position (GhosttyMouseEvent event)

Data Structures

struct  GhosttyMouseEncoderSize
struct  GhosttyMousePosition

Typedef Documentation

◆ GhosttyMouseEncoder

typedef struct GhosttyMouseEncoderImpl* GhosttyMouseEncoder

Opaque handle to a mouse encoder instance.

This handle represents a mouse encoder that converts normalized mouse events into terminal escape sequences.

Examples
c-vt-encode-mouse/src/main.c.

Definition at line 26 of file mouse/encoder.h.

◆ GhosttyMouseEvent

typedef struct GhosttyMouseEventImpl* GhosttyMouseEvent

Opaque handle to a mouse event.

This handle represents a normalized mouse input event containing action, button, modifiers, and surface-space position.

Examples
c-vt-encode-mouse/src/main.c.

Definition at line 23 of file mouse/event.h.

Enumeration Type Documentation

◆ GhosttyMouseAction

Mouse event action type.

Enumerator
GHOSTTY_MOUSE_ACTION_PRESS 

Mouse button was pressed.

GHOSTTY_MOUSE_ACTION_RELEASE 

Mouse button was released.

GHOSTTY_MOUSE_ACTION_MOTION 

Mouse moved.

Definition at line 30 of file mouse/event.h.

◆ GhosttyMouseButton

Mouse button identity.

Definition at line 47 of file mouse/event.h.

◆ GhosttyMouseEncoderOption

Mouse encoder option identifiers.

These values are used with ghostty_mouse_encoder_setopt() to configure the behavior of the mouse encoder.

Enumerator
GHOSTTY_MOUSE_ENCODER_OPT_EVENT 

Mouse tracking mode (value: GhosttyMouseTrackingMode).

GHOSTTY_MOUSE_ENCODER_OPT_FORMAT 

Mouse output format (value: GhosttyMouseFormat).

GHOSTTY_MOUSE_ENCODER_OPT_SIZE 

Renderer size context (value: GhosttyMouseEncoderSize).

GHOSTTY_MOUSE_ENCODER_OPT_ANY_BUTTON_PRESSED 

Whether any mouse button is currently pressed (value: bool).

GHOSTTY_MOUSE_ENCODER_OPT_TRACK_LAST_CELL 

Whether to enable motion deduplication by last cell (value: bool).

Definition at line 110 of file mouse/encoder.h.

◆ GhosttyMouseFormat

Mouse output format.

Examples
c-vt-encode-mouse/src/main.c.

Definition at line 56 of file mouse/encoder.h.

◆ GhosttyMouseTrackingMode

Mouse tracking mode.

Enumerator
GHOSTTY_MOUSE_TRACKING_NONE 

Mouse reporting disabled.

GHOSTTY_MOUSE_TRACKING_X10 

X10 mouse mode.

GHOSTTY_MOUSE_TRACKING_NORMAL 

Normal mouse mode (button press/release only).

GHOSTTY_MOUSE_TRACKING_BUTTON 

Button-event tracking mode.

GHOSTTY_MOUSE_TRACKING_ANY 

Any-event tracking mode.

Examples
c-vt-encode-mouse/src/main.c.

Definition at line 33 of file mouse/encoder.h.

Function Documentation

◆ ghostty_mouse_encoder_encode()

GHOSTTY_API GhosttyResult ghostty_mouse_encoder_encode ( GhosttyMouseEncoder encoder,
GhosttyMouseEvent event,
char * out_buf,
size_t out_buf_size,
size_t * out_len )

Encode a mouse event into a terminal escape sequence.

Not all mouse events produce output. In such cases this returns GHOSTTY_SUCCESS with out_len set to 0.

If the output buffer is too small, this returns GHOSTTY_OUT_OF_SPACE and out_len contains the required size.

Parameters
encoderThe encoder handle, must not be NULL
eventThe mouse event to encode, must not be NULL
out_bufBuffer to write encoded bytes to, or NULL to query required size
out_buf_sizeSize of out_buf in bytes
out_lenPointer to store bytes written (or required bytes on failure)
Returns
GHOSTTY_SUCCESS on success, GHOSTTY_OUT_OF_SPACE if buffer is too small, or another error code
Examples
c-vt-encode-mouse/src/main.c.

◆ ghostty_mouse_encoder_free()

GHOSTTY_API void ghostty_mouse_encoder_free ( GhosttyMouseEncoder encoder)

Free a mouse encoder instance.

Parameters
encoderThe encoder handle to free (may be NULL)
Examples
c-vt-encode-mouse/src/main.c.

◆ ghostty_mouse_encoder_new()

GHOSTTY_API GhosttyResult ghostty_mouse_encoder_new ( const GhosttyAllocator * allocator,
GhosttyMouseEncoder * encoder )

Create a new mouse encoder instance.

Parameters
Memory ManagementPointer to allocator, or NULL to use the default allocator
encoderPointer to store the created encoder handle
Returns
GHOSTTY_SUCCESS on success, or an error code on failure
Examples
c-vt-encode-mouse/src/main.c.

◆ ghostty_mouse_encoder_reset()

GHOSTTY_API void ghostty_mouse_encoder_reset ( GhosttyMouseEncoder encoder)

Reset internal encoder state.

This clears motion deduplication state (last tracked cell).

Parameters
encoderThe encoder handle (may be NULL)

◆ ghostty_mouse_encoder_setopt()

GHOSTTY_API void ghostty_mouse_encoder_setopt ( GhosttyMouseEncoder encoder,
GhosttyMouseEncoderOption option,
const void * value )

Set an option on the mouse encoder.

A null pointer value does nothing. It does not reset to defaults.

Parameters
encoderThe encoder handle, must not be NULL
optionThe option to set
valuePointer to option value (type depends on option)
Examples
c-vt-encode-mouse/src/main.c.

◆ ghostty_mouse_encoder_setopt_from_terminal()

GHOSTTY_API void ghostty_mouse_encoder_setopt_from_terminal ( GhosttyMouseEncoder encoder,
GhosttyTerminal terminal )

Set encoder options from a terminal's current state.

This sets tracking mode and output format from terminal state. It does not modify size or any-button state.

Parameters
encoderThe encoder handle, must not be NULL
TerminalThe terminal handle, must not be NULL

◆ ghostty_mouse_event_clear_button()

GHOSTTY_API void ghostty_mouse_event_clear_button ( GhosttyMouseEvent event)

Clear the event button.

This sets the event button to "none".

Parameters
eventThe event handle, must not be NULL

◆ ghostty_mouse_event_free()

GHOSTTY_API void ghostty_mouse_event_free ( GhosttyMouseEvent event)

Free a mouse event instance.

Parameters
eventThe mouse event handle to free (may be NULL)
Examples
c-vt-encode-mouse/src/main.c.

◆ ghostty_mouse_event_get_action()

GHOSTTY_API GhosttyMouseAction ghostty_mouse_event_get_action ( GhosttyMouseEvent event)

Get the event action.

Parameters
eventThe event handle, must not be NULL
Returns
The event action

◆ ghostty_mouse_event_get_button()

GHOSTTY_API bool ghostty_mouse_event_get_button ( GhosttyMouseEvent event,
GhosttyMouseButton * out_button )

Get the event button.

Parameters
eventThe event handle, must not be NULL
out_buttonOutput pointer for the button value (may be NULL)
Returns
true if a button is set, false if no button is set

◆ ghostty_mouse_event_get_mods()

GHOSTTY_API GhosttyMods ghostty_mouse_event_get_mods ( GhosttyMouseEvent event)

Get keyboard modifiers held during the event.

Parameters
eventThe event handle, must not be NULL
Returns
Modifier bitmask

◆ ghostty_mouse_event_get_position()

GHOSTTY_API GhosttyMousePosition ghostty_mouse_event_get_position ( GhosttyMouseEvent event)

Get the event position in surface-space pixels.

Parameters
eventThe event handle, must not be NULL
Returns
The current event position

◆ ghostty_mouse_event_new()

GHOSTTY_API GhosttyResult ghostty_mouse_event_new ( const GhosttyAllocator * allocator,
GhosttyMouseEvent * event )

Create a new mouse event instance.

Parameters
Memory ManagementPointer to allocator, or NULL to use the default allocator
eventPointer to store the created event handle
Returns
GHOSTTY_SUCCESS on success, or an error code on failure
Examples
c-vt-encode-mouse/src/main.c.

◆ ghostty_mouse_event_set_action()

GHOSTTY_API void ghostty_mouse_event_set_action ( GhosttyMouseEvent event,
GhosttyMouseAction action )

Set the event action.

Parameters
eventThe event handle, must not be NULL
actionThe action to set
Examples
c-vt-encode-mouse/src/main.c.

◆ ghostty_mouse_event_set_button()

GHOSTTY_API void ghostty_mouse_event_set_button ( GhosttyMouseEvent event,
GhosttyMouseButton button )

Set the event button.

This sets a concrete button identity for the event. To represent "no button" (for motion events), use ghostty_mouse_event_clear_button().

Parameters
eventThe event handle, must not be NULL
buttonThe button to set
Examples
c-vt-encode-mouse/src/main.c.

◆ ghostty_mouse_event_set_mods()

GHOSTTY_API void ghostty_mouse_event_set_mods ( GhosttyMouseEvent event,
GhosttyMods mods )

Set keyboard modifiers held during the event.

Parameters
eventThe event handle, must not be NULL
modsModifier bitmask

◆ ghostty_mouse_event_set_position()

GHOSTTY_API void ghostty_mouse_event_set_position ( GhosttyMouseEvent event,
GhosttyMousePosition position )

Set the event position in surface-space pixels.

Parameters
eventThe event handle, must not be NULL
positionThe position to set
Examples
c-vt-encode-mouse/src/main.c.