libghostty
Loading...
Searching...
No Matches
c-vt-encode-mouse/src/main.c

This example demonstrates how to use the mouse encoder to convert mouse events into terminal escape sequences using the SGR mouse format.

#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <string.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