-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathterminal_effect.go
More file actions
417 lines (377 loc) · 13.2 KB
/
Copy pathterminal_effect.go
File metadata and controls
417 lines (377 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package libghostty
// C trampolines for terminal effects.
//
// Each exported Go function here is passed to the C side as a function
// pointer. The C library calls it with a userdata void*, which is a
// cgo.Handle pointing back to the owning *Terminal. The trampoline
// recovers the Terminal and dispatches to the user-supplied Go effect handler.
/*
#include <ghostty/vt.h>
// Forward declarations for the Go trampolines so we can take their
// addresses on the C side.
extern void goWritePtyTrampoline(GhosttyTerminal, void*, uint8_t*, size_t);
extern void goBellTrampoline(GhosttyTerminal, void*);
extern GhosttyClipboardWriteResult goClipboardWriteTrampoline(GhosttyTerminal, void*, GhosttyClipboardWrite*);
extern void goDesktopNotificationTrampoline(GhosttyTerminal, void*, GhosttyTerminalDesktopNotification*);
extern void goTitleChangedTrampoline(GhosttyTerminal, void*);
extern void goPwdChangedTrampoline(GhosttyTerminal, void*);
extern void goProgressReportTrampoline(GhosttyTerminal, void*, GhosttyTerminalProgressReport*);
extern GhosttyString goEnquiryTrampoline(GhosttyTerminal, void*);
extern GhosttyString goXtversionTrampoline(GhosttyTerminal, void*);
extern bool goSizeTrampoline(GhosttyTerminal, void*, GhosttySizeReportSize*);
extern bool goColorSchemeTrampoline(GhosttyTerminal, void*, GhosttyColorScheme*);
extern bool goDeviceAttributesTrampoline(GhosttyTerminal, void*, GhosttyDeviceAttributes*);
// Helpers to set each effect via ghostty_terminal_set.
// We need these because cgo cannot take the address of a Go-exported
// function directly as a C function pointer.
static inline GhosttyResult set_write_pty(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_WRITE_PTY, (const void*)goWritePtyTrampoline);
}
static inline GhosttyResult set_bell(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_BELL, (const void*)goBellTrampoline);
}
static inline GhosttyResult set_clipboard_write(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_CLIPBOARD_WRITE, (const void*)goClipboardWriteTrampoline);
}
static inline GhosttyResult set_desktop_notification(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_DESKTOP_NOTIFICATION, (const void*)goDesktopNotificationTrampoline);
}
static inline GhosttyResult set_title_changed(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_TITLE_CHANGED, (const void*)goTitleChangedTrampoline);
}
static inline GhosttyResult set_pwd_changed(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_PWD_CHANGED, (const void*)goPwdChangedTrampoline);
}
static inline GhosttyResult set_progress_report(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_PROGRESS_REPORT, (const void*)goProgressReportTrampoline);
}
static inline GhosttyResult set_enquiry(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_ENQUIRY, (const void*)goEnquiryTrampoline);
}
static inline GhosttyResult set_xtversion(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_XTVERSION, (const void*)goXtversionTrampoline);
}
static inline GhosttyResult set_size(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_SIZE, (const void*)goSizeTrampoline);
}
static inline GhosttyResult set_color_scheme(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_COLOR_SCHEME, (const void*)goColorSchemeTrampoline);
}
static inline GhosttyResult set_device_attributes(GhosttyTerminal t) {
return ghostty_terminal_set(t, GHOSTTY_TERMINAL_OPT_DEVICE_ATTRIBUTES, (const void*)goDeviceAttributesTrampoline);
}
// Helper to clear an effect by setting it to NULL.
static inline GhosttyResult clear_effect(GhosttyTerminal t, GhosttyTerminalOption opt) {
return ghostty_terminal_set(t, opt, NULL);
}
*/
import "C"
import (
"runtime/cgo"
"unsafe"
)
// syncEffects registers or clears each C effect based on whether
// the corresponding Go effect handler is set.
func (t *Terminal) syncEffects() {
if t.onWritePty != nil {
C.set_write_pty(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_WRITE_PTY)
}
if t.onBell != nil {
C.set_bell(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_BELL)
}
if t.onClipboardWrite != nil {
C.set_clipboard_write(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_CLIPBOARD_WRITE)
}
if t.onDesktopNotification != nil {
C.set_desktop_notification(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_DESKTOP_NOTIFICATION)
}
if t.onTitleChanged != nil {
C.set_title_changed(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_TITLE_CHANGED)
}
if t.onPwdChanged != nil {
C.set_pwd_changed(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_PWD_CHANGED)
}
if t.onProgressReport != nil {
C.set_progress_report(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_PROGRESS_REPORT)
}
if t.onEnquiry != nil {
C.set_enquiry(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_ENQUIRY)
}
if t.onXtversion != nil {
C.set_xtversion(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_XTVERSION)
}
if t.onSize != nil {
C.set_size(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_SIZE)
}
if t.onColorScheme != nil {
C.set_color_scheme(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_COLOR_SCHEME)
}
if t.onDeviceAttributes != nil {
C.set_device_attributes(t.ptr)
} else {
C.clear_effect(t.ptr, C.GHOSTTY_TERMINAL_OPT_DEVICE_ATTRIBUTES)
}
}
// terminalFromUserdata recovers a *Terminal from the C userdata pointer.
func terminalFromUserdata(userdata unsafe.Pointer) *Terminal {
return cgo.Handle(userdata).Value().(*Terminal)
}
//export goWritePtyTrampoline
func goWritePtyTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer, data *C.uint8_t, length C.size_t) {
t := terminalFromUserdata(userdata)
if t.onWritePty != nil {
t.onWritePty(t, C.GoBytes(unsafe.Pointer(data), C.int(length)))
}
}
//export goBellTrampoline
func goBellTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer) {
t := terminalFromUserdata(userdata)
if t.onBell != nil {
t.onBell(t)
}
}
//export goClipboardWriteTrampoline
func goClipboardWriteTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer, write *C.GhosttyClipboardWrite) C.GhosttyClipboardWriteResult {
t := terminalFromUserdata(userdata)
if t.onClipboardWrite == nil {
return C.GHOSTTY_CLIPBOARD_WRITE_RESULT_UNSUPPORTED
}
// GhosttyClipboardWrite is a sized struct so newer libghostty versions
// can extend it without breaking existing callbacks. Only read the fields
// this binding knows about when the descriptor contains the full current
// layout.
if write == nil || write.size < C.size_t(C.sizeof_GhosttyClipboardWrite) {
return C.GHOSTTY_CLIPBOARD_WRITE_RESULT_INVALID_DATA
}
count, ok := ghosttySizeToInt(write.contents_len)
if !ok || (count > 0 && write.contents == nil) {
return C.GHOSTTY_CLIPBOARD_WRITE_RESULT_INVALID_DATA
}
contents := make([]ClipboardContent, count)
if count > 0 {
cContents := unsafe.Slice(write.contents, count)
for i, content := range cContents {
mime, valid := copyGhosttyString(content.mime)
if !valid {
return C.GHOSTTY_CLIPBOARD_WRITE_RESULT_INVALID_DATA
}
data, valid := copyGhosttyString(content.data)
if !valid {
return C.GHOSTTY_CLIPBOARD_WRITE_RESULT_INVALID_DATA
}
contents[i] = ClipboardContent{
MIME: string(mime),
Data: data,
}
}
}
result := t.onClipboardWrite(t, ClipboardWrite{
Location: ClipboardLocation(write.location),
Contents: contents,
})
return C.GhosttyClipboardWriteResult(result)
}
// ghosttySizeToInt converts a C size to a Go slice length without allowing
// an overflowing conversion to produce an invalid unsafe.Slice length.
func ghosttySizeToInt(size C.size_t) (int, bool) {
if uint64(size) > uint64(^uint(0)>>1) {
return 0, false
}
return int(size), true
}
// copyGhosttyString copies a borrowed, binary-safe GhosttyString into Go
// memory. For zero-length strings the pointer is intentionally ignored
// because libghostty does not require it to be valid.
func copyGhosttyString(value C.GhosttyString) ([]byte, bool) {
length, ok := ghosttySizeToInt(value.len)
if !ok {
return nil, false
}
if length == 0 {
return []byte{}, true
}
if value.ptr == nil {
return nil, false
}
return append([]byte(nil), unsafe.Slice((*byte)(unsafe.Pointer(value.ptr)), length)...), true
}
//export goDesktopNotificationTrampoline
func goDesktopNotificationTrampoline(
_ C.GhosttyTerminal,
userdata unsafe.Pointer,
notification *C.GhosttyTerminalDesktopNotification,
) {
t := terminalFromUserdata(userdata)
if t.onDesktopNotification == nil {
return
}
// GhosttyTerminalDesktopNotification is a sized struct so newer
// libghostty versions can extend it without breaking existing callbacks.
// Ignore descriptors that do not contain the full layout this binding
// knows how to read.
if notification == nil ||
notification.size < C.size_t(C.sizeof_GhosttyTerminalDesktopNotification) {
return
}
title, ok := copyGhosttyString(notification.title)
if !ok {
return
}
body, ok := copyGhosttyString(notification.body)
if !ok {
return
}
t.onDesktopNotification(t, TerminalDesktopNotification{
Title: string(title),
Body: string(body),
})
}
//export goTitleChangedTrampoline
func goTitleChangedTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer) {
t := terminalFromUserdata(userdata)
if t.onTitleChanged != nil {
t.onTitleChanged(t)
}
}
//export goPwdChangedTrampoline
func goPwdChangedTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer) {
t := terminalFromUserdata(userdata)
if t.onPwdChanged != nil {
t.onPwdChanged(t)
}
}
//export goProgressReportTrampoline
func goProgressReportTrampoline(
_ C.GhosttyTerminal,
userdata unsafe.Pointer,
report *C.GhosttyTerminalProgressReport,
) {
t := terminalFromUserdata(userdata)
if t.onProgressReport == nil {
return
}
// GhosttyTerminalProgressReport is also a sized struct. Only read the
// fields when the caller supplied the complete layout known here.
if report == nil ||
report.size < C.size_t(C.sizeof_GhosttyTerminalProgressReport) {
return
}
t.onProgressReport(t, TerminalProgressReport{
State: TerminalProgressState(report.state),
Progress: int8(report.progress),
})
}
//export goEnquiryTrampoline
func goEnquiryTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer) C.GhosttyString {
t := terminalFromUserdata(userdata)
if t.onEnquiry == nil {
return C.GhosttyString{}
}
return t.effectString(t.onEnquiry(t))
}
//export goXtversionTrampoline
func goXtversionTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer) C.GhosttyString {
t := terminalFromUserdata(userdata)
if t.onXtversion == nil {
return C.GhosttyString{}
}
return t.effectString([]byte(t.onXtversion(t)))
}
//export goSizeTrampoline
func goSizeTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer, outSize *C.GhosttySizeReportSize) C.bool {
t := terminalFromUserdata(userdata)
if t.onSize == nil {
return C.bool(false)
}
size, ok := t.onSize(t)
if !ok {
return C.bool(false)
}
outSize.rows = C.uint16_t(size.Rows)
outSize.columns = C.uint16_t(size.Columns)
outSize.cell_width = C.uint32_t(size.CellWidth)
outSize.cell_height = C.uint32_t(size.CellHeight)
return C.bool(true)
}
//export goColorSchemeTrampoline
func goColorSchemeTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer, outScheme *C.GhosttyColorScheme) C.bool {
t := terminalFromUserdata(userdata)
if t.onColorScheme == nil {
return C.bool(false)
}
scheme, ok := t.onColorScheme(t)
if !ok {
return C.bool(false)
}
*outScheme = C.GhosttyColorScheme(scheme)
return C.bool(true)
}
//export goDeviceAttributesTrampoline
func goDeviceAttributesTrampoline(_ C.GhosttyTerminal, userdata unsafe.Pointer, outAttrs *C.GhosttyDeviceAttributes) C.bool {
t := terminalFromUserdata(userdata)
if t.onDeviceAttributes == nil {
return C.bool(false)
}
attrs, ok := t.onDeviceAttributes(t)
if !ok {
return C.bool(false)
}
// Primary (DA1).
outAttrs.primary.conformance_level = C.uint16_t(attrs.Primary.ConformanceLevel)
outAttrs.primary.num_features = C.size_t(attrs.Primary.NumFeatures)
for i := 0; i < attrs.Primary.NumFeatures && i < 64; i++ {
outAttrs.primary.features[i] = C.uint16_t(attrs.Primary.Features[i])
}
// Secondary (DA2).
outAttrs.secondary.device_type = C.uint16_t(attrs.Secondary.DeviceType)
outAttrs.secondary.firmware_version = C.uint16_t(attrs.Secondary.FirmwareVersion)
outAttrs.secondary.rom_cartridge = C.uint16_t(attrs.Secondary.ROMCartridge)
// Tertiary (DA3).
outAttrs.tertiary.unit_id = C.uint32_t(attrs.Tertiary.UnitID)
return C.bool(true)
}
// effectString copies data into C memory allocated via the libghostty
// allocator, updates effectBuf/effectBufLen, and returns a
// GhosttyString pointing to it. The previous effectBuf is freed.
// Returns a zero-length GhosttyString if data is empty.
func (t *Terminal) effectString(data []byte) C.GhosttyString {
if t.effectBuf != nil {
Free(t.effectBuf, t.effectBufLen)
t.effectBuf = nil
t.effectBufLen = 0
}
if len(data) == 0 {
return C.GhosttyString{}
}
n := uintptr(len(data))
cmem := Alloc(n)
copy(unsafe.Slice((*byte)(cmem), n), data)
t.effectBuf = cmem
t.effectBufLen = n
return C.GhosttyString{
ptr: (*C.uint8_t)(cmem),
len: C.size_t(n),
}
}