Register callbacks for what a program asks of the terminal: pty replies, the bell, title changes, OSC 52 clipboard reads and writes, and unknown sequences. You need these as soon as a real program runs in the terminal.
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
void* userdata,
const uint8_t* data,
size_t len) {
(void)terminal;
(void)userdata;
printf(" write_pty (%zu bytes): ", len);
fwrite(data, 1, len, stdout);
printf("\n");
}
(void)terminal;
int* count = (int*)userdata;
(*count)++;
printf(" bell! (count=%d)\n", *count);
}
(void)userdata;
uint16_t col = 0;
printf(" title changed (cursor at col %u)\n", col);
}
void on_clipboard_write(
void* userdata,
(void)terminal;
(void)userdata;
printf(" clipboard write (location=%d, contents=%zu)\n",
printf(" clear\n");
}
printf(" ");
}
printf(
" (%zu bytes): ", content->
data.
len);
}
printf("\n");
}
.size = sizeof(reply),
.remember = false,
};
write->
reply(write, &reply);
}
void on_clipboard_read(
void* userdata,
(void)terminal;
(void)userdata;
printf(" clipboard read (location=%d, mimes=%zu)\n",
for (
size_t i = 0; i < read->
mimes_len; i++) {
printf(" ");
printf("\n");
}
const char* text = "Hello from the clipboard";
.mime = {.ptr = (const uint8_t*)"text/plain", .len = 10},
.data = {.ptr = (const uint8_t*)text, .len = strlen(text)},
};
.size = sizeof(reply),
.contents = &content,
.contents_len = 1,
.available = NULL,
.available_len = 0,
.remember = false,
};
read->
reply(read, &reply);
}
void on_unknown_sequence(
void* userdata,
(void)terminal;
(void)userdata;
printf(" unknown APC (truncated=%s, content=%zu bytes): ",
}
printf("\n");
break;
}
printf(" unknown OSC (truncated=%s, terminator=%s, content=%zu bytes): ",
}
printf("\n");
break;
}
default:
break;
}
}
int main() {
fprintf(stderr, "Failed to create terminal\n");
return 1;
}
int bell_count = 0;
(const void *)on_write_pty);
(const void *)on_bell);
(const void *)on_title_changed);
(const void *)on_clipboard_write);
(const void *)on_clipboard_read);
(const void *)on_unknown_sequence);
size_t unknown_max_bytes = 256;
&unknown_max_bytes);
printf("Sending BEL:\n");
const uint8_t bel = 0x07;
printf("Sending title change:\n");
const char* title_seq = "\x1B]2;Hello Effects\x1B\\";
strlen(title_seq));
printf("Sending DECRQM query:\n");
const char* decrqm = "\x1B[?7$p";
strlen(decrqm));
printf("Sending clipboard write:\n");
const char* clipboard_seq =
"\x1B]52;c;SGVsbG8gY2xpcGJvYXJk\x1B\\";
strlen(clipboard_seq));
printf("Sending clipboard read:\n");
const char* clipboard_read_seq = "\x1B]52;c;?\x1B\\";
strlen(clipboard_read_seq));
printf("Sending unknown APC:\n");
const char* unknown_apc = "\x1B_private-command;payload\x1B\\";
strlen(unknown_apc));
printf("Sending unknown OSC:\n");
const char* unknown_osc = "\x1B]7400;status=busy\x07";
strlen(unknown_osc));
printf("Sending another BEL:\n");
printf("Total bells: %d\n", bell_count);
return 0;
}
@ GHOSTTY_CLIPBOARD_READ_RESULT_SUCCESS
@ GHOSTTY_CLIPBOARD_WRITE_RESULT_SUCCESS
@ GHOSTTY_TERMINAL_UNKNOWN_SEQUENCE_OSC
@ GHOSTTY_TERMINAL_UNKNOWN_SEQUENCE_APC
@ GHOSTTY_OSC_TERMINATOR_BEL
GHOSTTY_API GhosttyResult ghostty_terminal_set(GhosttyTerminal terminal, GhosttyTerminalOption option, const void *value)
GHOSTTY_API GhosttyResult ghostty_terminal_get(GhosttyTerminal terminal, GhosttyTerminalData data, void *out)
struct GhosttyTerminalImpl * GhosttyTerminal
GHOSTTY_API void ghostty_terminal_free(GhosttyTerminal terminal)
GHOSTTY_API void ghostty_terminal_vt_write(GhosttyTerminal terminal, const uint8_t *data, size_t len)
GHOSTTY_API GhosttyResult ghostty_terminal_new(const GhosttyAllocator *allocator, GhosttyTerminal *terminal, uint16_t cols, uint16_t rows)
@ GHOSTTY_TERMINAL_DATA_CURSOR_X
@ GHOSTTY_TERMINAL_OPT_TITLE_CHANGED
@ GHOSTTY_TERMINAL_OPT_UNKNOWN_MAX_BYTES
@ GHOSTTY_TERMINAL_OPT_USERDATA
@ GHOSTTY_TERMINAL_OPT_CLIPBOARD_WRITE
@ GHOSTTY_TERMINAL_OPT_WRITE_PTY
@ GHOSTTY_TERMINAL_OPT_BELL
@ GHOSTTY_TERMINAL_OPT_UNKNOWN_SEQUENCE
@ GHOSTTY_TERMINAL_OPT_CLIPBOARD_READ
GhosttyClipboardReadReplyFn reply
GhosttyClipboardLocation location
const GhosttyString * mimes
GhosttyClipboardWriteReplyFn reply
const GhosttyClipboardContent * contents
GhosttyClipboardLocation location
GhosttyOscTerminator terminator
GhosttyTerminalUnknownOscSequence osc
GhosttyTerminalUnknownStringSequence apc