example: add C mouse encoding example

Add a new c-vt-mouse-encode example that demonstrates how to use the
mouse encoder C API. The example creates a mouse encoder configured
with SGR format and normal tracking mode, sets up terminal geometry
for pixel-to-cell coordinate mapping, and encodes a left button press
event into a terminal escape sequence.

Mirrors the structure of the existing c-vt-key-encode example. Also
adds the corresponding @example doxygen reference in vt.h.
This commit is contained in:
Mitchell Hashimoto
2026-03-15 20:12:05 -07:00
parent 9b35c2bb65
commit 33b05b9876
6 changed files with 257 additions and 0 deletions

View File

@@ -43,6 +43,7 @@
* Complete working examples:
* - @ref c-vt/src/main.c - OSC parser example
* - @ref c-vt-key-encode/src/main.c - Key encoding example
* - @ref c-vt-mouse-encode/src/main.c - Mouse encoding example
* - @ref c-vt-paste/src/main.c - Paste safety check example
* - @ref c-vt-sgr/src/main.c - SGR parser example
* - @ref c-vt-formatter/src/main.c - Terminal formatter example
@@ -59,6 +60,11 @@
* into terminal escape sequences using the Kitty keyboard protocol.
*/
/** @example c-vt-mouse-encode/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.
*/
/** @example c-vt-paste/src/main.c
* This example demonstrates how to use the paste utilities to check if
* paste data is safe before sending it to the terminal.

View File

@@ -24,6 +24,90 @@
* - Free the event with ghostty_mouse_event_free() or reuse it.
* 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
*
* @code{.c}
* #include <assert.h>
* #include <stdio.h>
* #include <ghostty/vt.h>
*
* int main() {
* // Create encoder
* GhosttyMouseEncoder encoder;
* GhosttyResult result = ghostty_mouse_encoder_new(NULL, &encoder);
* assert(result == GHOSTTY_SUCCESS);
*
* // Configure SGR format with normal tracking
* ghostty_mouse_encoder_setopt(encoder, GHOSTTY_MOUSE_ENCODER_OPT_EVENT,
* &(GhosttyMouseTrackingMode){GHOSTTY_MOUSE_TRACKING_NORMAL});
* ghostty_mouse_encoder_setopt(encoder, GHOSTTY_MOUSE_ENCODER_OPT_FORMAT,
* &(GhosttyMouseFormat){GHOSTTY_MOUSE_FORMAT_SGR});
*
* // Set terminal geometry for coordinate mapping
* ghostty_mouse_encoder_setopt(encoder, GHOSTTY_MOUSE_ENCODER_OPT_SIZE,
* &(GhosttyMouseEncoderSize){
* .size = sizeof(GhosttyMouseEncoderSize),
* .screen_width = 800, .screen_height = 600,
* .cell_width = 10, .cell_height = 20,
* });
*
* // Create and configure a left button press event
* GhosttyMouseEvent event;
* result = ghostty_mouse_event_new(NULL, &event);
* assert(result == GHOSTTY_SUCCESS);
* ghostty_mouse_event_set_action(event, GHOSTTY_MOUSE_ACTION_PRESS);
* ghostty_mouse_event_set_button(event, GHOSTTY_MOUSE_BUTTON_LEFT);
* ghostty_mouse_event_set_position(event,
* (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
* ghostty_mouse_event_free(event);
* ghostty_mouse_encoder_free(encoder);
* return 0;
* }
* @endcode
*
* ## Example: Encoding with Terminal State
*
* When you have a GhosttyTerminal, you can sync its tracking mode and
* output format into the encoder automatically:
*
* @code{.c}
* // 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
* GhosttyMouseEncoder encoder;
* ghostty_mouse_encoder_new(NULL, &encoder);
* ghostty_mouse_encoder_setopt_from_terminal(encoder, terminal);
*
* // 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);
*
* ghostty_mouse_encoder_free(encoder);
* ghostty_terminal_free(terminal);
* @endcode
*
* @{
*/