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>
int main() {
.screen_width = 800,
.screen_height = 600,
.cell_width = 10,
.cell_height = 20,
.padding_top = 0,
.padding_bottom = 0,
.padding_right = 0,
.padding_left = 0,
});
printf("Encoding event: left button press at (50, 40) in SGR format\n");
size_t required = 0;
printf("Required buffer size: %zu bytes\n", required);
char buf[128];
size_t written = 0;
printf("Encoded %zu bytes\n", written);
printf("Hex: ");
for (size_t i = 0; i < written; i++) printf("%02x ", (unsigned char)buf[i]);
printf("\n");
printf("String: ");
for (size_t i = 0; i < written; i++) {
if (buf[i] == 0x1b) {
printf("\\x1b");
} else {
printf("%c", buf[i]);
}
}
printf("\n");
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)
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)
struct GhosttyMouseEvent * GhosttyMouseEvent
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