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-mouse-encode in the repository.

Example

#include <assert.h>
#include <stdio.h>
#include <ghostty/vt.h>
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;
}
GhosttyResult ghostty_mouse_event_new(const GhosttyAllocator *allocator, GhosttyMouseEvent *event)
void ghostty_mouse_event_free(GhosttyMouseEvent event)
void ghostty_mouse_event_set_action(GhosttyMouseEvent event, GhosttyMouseAction action)
void ghostty_mouse_encoder_setopt(GhosttyMouseEncoder encoder, GhosttyMouseEncoderOption option, const void *value)
GhosttyMouseFormat
GhosttyResult ghostty_mouse_encoder_new(const GhosttyAllocator *allocator, GhosttyMouseEncoder *encoder)
void ghostty_mouse_encoder_free(GhosttyMouseEncoder encoder)
GhosttyResult ghostty_mouse_encoder_encode(GhosttyMouseEncoder encoder, GhosttyMouseEvent event, char *out_buf, size_t out_buf_size, size_t *out_len)
GhosttyMouseTrackingMode
struct GhosttyMouseEvent * GhosttyMouseEvent
Definition mouse/event.h:23
void ghostty_mouse_event_set_position(GhosttyMouseEvent event, GhosttyMousePosition position)
void ghostty_mouse_event_set_button(GhosttyMouseEvent event, GhosttyMouseButton button)
struct GhosttyMouseEncoder * GhosttyMouseEncoder
@ GHOSTTY_MOUSE_ENCODER_OPT_SIZE
@ GHOSTTY_MOUSE_ENCODER_OPT_FORMAT
@ GHOSTTY_MOUSE_ENCODER_OPT_EVENT
@ GHOSTTY_MOUSE_TRACKING_NORMAL
@ GHOSTTY_MOUSE_ACTION_PRESS
Definition mouse/event.h:32
GhosttyResult
Definition types.h:13
@ GHOSTTY_SUCCESS
Definition types.h:15

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);
void ghostty_mouse_encoder_setopt_from_terminal(GhosttyMouseEncoder encoder, GhosttyTerminal terminal)
struct GhosttyTerminal * GhosttyTerminal
Definition terminal.h:37
GhosttyResult ghostty_terminal_new(const GhosttyAllocator *allocator, GhosttyTerminal *terminal, GhosttyTerminalOptions options)
void ghostty_terminal_vt_write(GhosttyTerminal terminal, const uint8_t *data, size_t len)
void ghostty_terminal_free(GhosttyTerminal terminal)

Typedefs

typedef struct GhosttyMouseEncoderGhosttyMouseEncoder
typedef struct GhosttyMouseEventGhosttyMouseEvent

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
}
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
}
enum  GhosttyMouseAction { GHOSTTY_MOUSE_ACTION_PRESS = 0 , GHOSTTY_MOUSE_ACTION_RELEASE = 1 , GHOSTTY_MOUSE_ACTION_MOTION = 2 }
enum  GhosttyMouseButton

Functions

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

Data Structures

struct  GhosttyMouseEncoderSize
struct  GhosttyMousePosition

Typedef Documentation

◆ 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-mouse-encode/src/main.c.

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

◆ 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-mouse-encode/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 46 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 108 of file mouse/encoder.h.

◆ GhosttyMouseFormat

Mouse output format.

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

Definition at line 55 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-mouse-encode/src/main.c.

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

Function Documentation

◆ ghostty_mouse_encoder_encode()

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_MEMORY 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_MEMORY if buffer is too small, or another error code
Examples
c-vt-mouse-encode/src/main.c.

◆ ghostty_mouse_encoder_free()

void ghostty_mouse_encoder_free ( GhosttyMouseEncoder encoder)

Free a mouse encoder instance.

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

◆ ghostty_mouse_encoder_new()

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-mouse-encode/src/main.c.

◆ ghostty_mouse_encoder_reset()

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()

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-mouse-encode/src/main.c.

◆ ghostty_mouse_encoder_setopt_from_terminal()

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()

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()

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-mouse-encode/src/main.c.

◆ ghostty_mouse_event_get_action()

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()

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()

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()

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()

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-mouse-encode/src/main.c.

◆ ghostty_mouse_event_set_action()

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-mouse-encode/src/main.c.

◆ ghostty_mouse_event_set_button()

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-mouse-encode/src/main.c.

◆ ghostty_mouse_event_set_mods()

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()

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-mouse-encode/src/main.c.