-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathextui_host.go
More file actions
608 lines (548 loc) · 13.7 KB
/
Copy pathextui_host.go
File metadata and controls
608 lines (548 loc) · 13.7 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
package main
import (
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/unxed/vtinput"
"github.com/unxed/vtui"
"github.com/vmihailenco/msgpack/v5"
)
const (
extUiProtocolVersion = 2
extUiMaxMessageSize = 64 * 1024 * 1024
)
func extUiNewNonce() (string, error) {
var buf [16]byte
if _, err := rand.Read(buf[:]); err != nil {
return "", err
}
return fmt.Sprintf("%x", buf[:]), nil
}
func extUiSendMessage(w io.Writer, msg map[string]any) error {
payload, err := msgpack.Marshal(msg)
if err != nil {
return err
}
if len(payload) > extUiMaxMessageSize {
return fmt.Errorf("extui message too large: %d bytes", len(payload))
}
var hdr [4]byte
binary.BigEndian.PutUint32(hdr[:], uint32(len(payload)))
if _, err := w.Write(hdr[:]); err != nil {
return err
}
_, err = w.Write(payload)
return err
}
type extUiMessageSender struct {
mu sync.Mutex
w io.Writer
}
func (s *extUiMessageSender) Send(msg map[string]any) error {
s.mu.Lock()
defer s.mu.Unlock()
return extUiSendMessage(s.w, msg)
}
func extUiReadMessage(r io.Reader) (map[string]any, error) {
var hdr [4]byte
if _, err := io.ReadFull(r, hdr[:]); err != nil {
return nil, err
}
n := binary.BigEndian.Uint32(hdr[:])
if n == 0 {
return nil, fmt.Errorf("empty extui message")
}
if n > extUiMaxMessageSize {
return nil, fmt.Errorf("extui message too large: %d bytes", n)
}
payload := make([]byte, n)
if _, err := io.ReadFull(r, payload); err != nil {
return nil, err
}
var msg map[string]any
if err := msgpack.Unmarshal(payload, &msg); err != nil {
return nil, err
}
return msg, nil
}
func extUiString(msg map[string]any, key string) string {
if v, ok := msg[key].(string); ok {
return v
}
return ""
}
func extUiBool(msg map[string]any, key string) bool {
if v, ok := msg[key].(bool); ok {
return v
}
return false
}
func extUiInt(msg map[string]any, key string) int {
return extUiAnyInt(msg[key])
}
func extUiAnyInt(v any) int {
switch n := v.(type) {
case int:
return n
case int8:
return int(n)
case int16:
return int(n)
case int32:
return int(n)
case int64:
return int(n)
case uint:
return int(n)
case uint8:
return int(n)
case uint16:
return int(n)
case uint32:
return int(n)
case uint64:
return int(n)
}
return 0
}
type ExtUiRenderer struct {
mu sync.Mutex
conn net.Conn
send *extUiMessageSender
cursorX, cursorY int
cursorVis bool
cursorShape vtui.CursorShape
cursorDirty bool
palette [256]uint32
paletteValid bool
pendingPalette []uint32
pendingFrame map[string]any
pendingScene map[string]any
closed bool
}
func NewExtUiRenderer(conn net.Conn, sender *extUiMessageSender) *ExtUiRenderer {
return &ExtUiRenderer{conn: conn, send: sender, cursorDirty: true}
}
func (r *ExtUiRenderer) SetPalette(pal *[256]uint32) {
if pal == nil {
return
}
r.mu.Lock()
defer r.mu.Unlock()
changed := !r.paletteValid
if !changed {
for i, c := range pal {
if r.palette[i] != c {
changed = true
break
}
}
}
if !changed {
return
}
r.palette = *pal
r.paletteValid = true
colors := make([]uint32, len(pal))
copy(colors, pal[:])
r.pendingPalette = colors
}
func (r *ExtUiRenderer) SetCursor(x, y int, visible bool, shape vtui.CursorShape) {
r.mu.Lock()
defer r.mu.Unlock()
if r.cursorX == x && r.cursorY == y && r.cursorVis == visible && r.cursorShape == shape {
return
}
r.cursorX, r.cursorY = x, y
r.cursorVis = visible
r.cursorShape = shape
r.cursorDirty = true
}
func (r *ExtUiRenderer) SetWindowTitle(title string) {
_ = r.send.Send(map[string]any{
"type": "title",
"title": title,
})
}
func (r *ExtUiRenderer) Render(buf, shadow []vtui.CharInfo, width, height int, forceRedraw bool) {
if width <= 0 || height <= 0 || len(buf) == 0 {
return
}
needsRedraw := forceRedraw
if !needsRedraw {
for i := 0; i < width*height && i < len(buf) && i < len(shadow); i++ {
if buf[i] != shadow[i] {
needsRedraw = true
break
}
}
}
if !needsRedraw {
return
}
limit := width * height
if limit > len(buf) {
limit = len(buf)
}
cells := make([][3]uint64, 0, limit)
if forceRedraw || len(shadow) < limit {
for i := 0; i < limit; i++ {
c := buf[i]
cells = append(cells, [3]uint64{uint64(i), c.Char, c.Attributes})
}
} else {
for i := 0; i < limit; i++ {
c := buf[i]
if c != shadow[i] {
cells = append(cells, [3]uint64{uint64(i), c.Char, c.Attributes})
}
}
}
r.mu.Lock()
r.pendingFrame = map[string]any{
"type": "frame",
"width": width,
"height": height,
"full": forceRedraw || len(shadow) < limit,
"cells": cells,
}
r.mu.Unlock()
}
func (r *ExtUiRenderer) SetSemanticScene(scene map[string]any) {
r.mu.Lock()
defer r.mu.Unlock()
r.pendingScene = scene
}
func (r *ExtUiRenderer) Flush() {
var messages []map[string]any
r.mu.Lock()
if r.closed {
r.mu.Unlock()
return
}
if r.pendingPalette != nil {
messages = append(messages, map[string]any{
"type": "palette",
"colors": r.pendingPalette,
})
r.pendingPalette = nil
}
if r.pendingFrame != nil {
messages = append(messages, r.pendingFrame)
r.pendingFrame = nil
}
if r.pendingScene != nil {
messages = append(messages, r.pendingScene)
r.pendingScene = nil
}
if r.cursorDirty {
messages = append(messages, map[string]any{
"type": "cursor",
"x": r.cursorX,
"y": r.cursorY,
"visible": r.cursorVis,
"shape": int(r.cursorShape),
})
r.cursorDirty = false
}
r.mu.Unlock()
for _, msg := range messages {
if err := r.send.Send(msg); err != nil {
vtui.DebugLog("EXTUI_RENDERER: send failed: %v", err)
r.mu.Lock()
r.closed = true
r.mu.Unlock()
return
}
}
}
type ExtUiHost struct {
mu sync.Mutex
conn net.Conn
send *extUiMessageSender
reader *vtinput.Reader
cols int
rows int
}
func RunExternalUI(cols, rows int, execPath string, args []string) error {
nonce, err := extUiNewNonce()
if err != nil {
return err
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return fmt.Errorf("failed to create extui listener: %w", err)
}
defer listener.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cmdArgs := append([]string{}, args...)
cmdArgs = append(cmdArgs,
"--f4-ext-connect="+listener.Addr().String(),
"--f4-ext-nonce="+nonce,
"--f4-ext-cols="+strconv.Itoa(cols),
"--f4-ext-rows="+strconv.Itoa(rows),
)
cmd := exec.CommandContext(ctx, execPath, cmdArgs...)
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start external UI %q: %w", execPath, err)
}
defer func() {
cancel()
_ = cmd.Wait()
}()
connCh := make(chan net.Conn, 1)
errCh := make(chan error, 1)
go func() {
conn, err := listener.Accept()
if err != nil {
errCh <- err
return
}
connCh <- conn
}()
var conn net.Conn
select {
case conn = <-connCh:
case err := <-errCh:
return fmt.Errorf("extui connection failed: %w", err)
case <-time.After(10 * time.Second):
return fmt.Errorf("extui did not connect within 10s")
}
defer conn.Close()
if err := conn.SetDeadline(time.Now().Add(10 * time.Second)); err != nil {
return err
}
hello, err := extUiReadMessage(conn)
if err != nil {
return fmt.Errorf("failed to read extui hello: %w", err)
}
if extUiString(hello, "type") != "hello" || extUiString(hello, "nonce") != nonce {
return fmt.Errorf("invalid extui hello")
}
clientCols := extUiInt(hello, "cols")
clientRows := extUiInt(hello, "rows")
pixelW := extUiInt(hello, "pixelWidth")
pixelH := extUiInt(hello, "pixelHeight")
cellW := extUiInt(hello, "cellWidth")
cellH := extUiInt(hello, "cellHeight")
if pixelW > 0 && cellW > 0 {
clientCols = pixelW / cellW
}
if pixelH > 0 && cellH > 0 {
clientRows = pixelH / cellH
}
if clientCols > 0 {
cols = clientCols
}
if clientRows > 0 {
rows = clientRows
}
sender := &extUiMessageSender{w: conn}
if err := sender.Send(map[string]any{
"type": "hello",
"nonce": nonce,
"protocol": extUiProtocolVersion,
"cols": cols,
"rows": rows,
"app": vtui.AppName,
}); err != nil {
return fmt.Errorf("failed to send extui hello: %w", err)
}
if err := conn.SetDeadline(time.Time{}); err != nil {
return err
}
host := &ExtUiHost{conn: conn, send: sender, cols: cols, rows: rows}
scr := vtui.NewScreenBuf()
scr.AllocBuf(cols, rows)
scr.Renderer = NewExtUiRenderer(conn, sender)
vtui.FrameManager.Init(scr)
pr, _ := io.Pipe()
reader := vtinput.NewReader(pr, true)
host.reader = reader
vtui.GetTerminalSize = func() (int, int, error) {
host.mu.Lock()
defer host.mu.Unlock()
return host.cols, host.rows, nil
}
go host.readLoop()
SetupUI()
vtui.FrameManager.Run(reader)
_ = sender.Send(map[string]any{"type": "quit"})
return nil
}
func (h *ExtUiHost) readLoop() {
for {
msg, err := extUiReadMessage(h.conn)
if err != nil {
vtui.DebugLog("EXTUI_HOST: read loop stopped: %v", err)
if h.reader != nil {
h.reader.Close()
}
return
}
h.handleMessage(msg)
}
}
func (h *ExtUiHost) handleMessage(msg map[string]any) {
switch extUiString(msg, "type") {
case "resize":
cols, rows := extUiInt(msg, "cols"), extUiInt(msg, "rows")
if cols <= 0 || rows <= 0 {
return
}
h.mu.Lock()
changed := h.cols != cols || h.rows != rows
h.cols, h.rows = cols, rows
h.mu.Unlock()
if changed {
h.sendEvent(&vtinput.InputEvent{Type: vtinput.ResizeEventType, InputSource: "extui"})
}
case "key":
h.sendEvent(&vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: extUiBool(msg, "down"),
VirtualKeyCode: uint16(extUiInt(msg, "vk")),
Char: rune(extUiInt(msg, "char")),
ControlKeyState: vtinput.ControlKeyState(uint32(extUiInt(msg, "mods"))),
InputSource: "extui",
})
case "text":
for _, r := range extUiString(msg, "text") {
h.sendEvent(&vtinput.InputEvent{
Type: vtinput.KeyEventType,
KeyDown: true,
Char: r,
ControlKeyState: vtinput.ControlKeyState(uint32(extUiInt(msg, "mods"))),
InputSource: "extui_text",
})
}
case "mouse":
h.sendEvent(&vtinput.InputEvent{
Type: vtinput.MouseEventType,
MouseX: uint16(extUiInt(msg, "x")),
MouseY: uint16(extUiInt(msg, "y")),
ButtonState: uint32(extUiInt(msg, "button")),
MouseEventFlags: uint32(extUiInt(msg, "flags")),
KeyDown: extUiBool(msg, "down"),
ControlKeyState: vtinput.ControlKeyState(uint32(extUiInt(msg, "mods"))),
InputSource: "extui",
})
case "wheel":
h.sendEvent(&vtinput.InputEvent{
Type: vtinput.MouseEventType,
MouseX: uint16(extUiInt(msg, "x")),
MouseY: uint16(extUiInt(msg, "y")),
WheelDirection: extUiInt(msg, "dir"),
ControlKeyState: vtinput.ControlKeyState(uint32(extUiInt(msg, "mods"))),
InputSource: "extui",
})
case "paste":
text := extUiString(msg, "text")
h.sendEvent(&vtinput.InputEvent{Type: vtinput.PasteEventType, PasteStart: true, InputSource: "extui"})
for _, r := range text {
h.sendEvent(&vtinput.InputEvent{Type: vtinput.KeyEventType, KeyDown: true, Char: r, InputSource: "extui_paste"})
}
h.sendEvent(&vtinput.InputEvent{Type: vtinput.PasteEventType, PasteStart: false, InputSource: "extui"})
case "clipboard_get":
_ = h.send.Send(map[string]any{
"type": "clipboard_data",
"text": vtui.GetClipboard(),
})
case "clipboard_set":
vtui.SetClipboard(extUiString(msg, "text"))
case "ui_action":
action := msg
if nested, ok := msg["action"].(map[string]any); ok {
action = nested
}
if vtui.FrameManager != nil {
vtui.FrameManager.PostTask(func() {
if HandleSemanticAction(action) {
vtui.FrameManager.Redraw()
}
})
}
case "quit":
if vtui.FrameManager != nil {
vtui.FrameManager.PostTask(func() {
vtui.FrameManager.EmitCommand(vtui.CmQuit, nil)
vtui.FrameManager.Stop()
})
}
}
}
func (h *ExtUiHost) sendEvent(ev *vtinput.InputEvent) {
if h.reader == nil {
return
}
select {
case h.reader.EventChan <- ev:
case <-time.After(500 * time.Millisecond):
vtui.DebugLog("EXTUI_HOST: dropped event after blocked queue: %s", ev.String())
}
}
func RunExternalUIWithMapping(backend string) error {
path, err := findExtUiPath(backend)
if err != nil {
return err
}
return RunExternalUI(100, 30, path, nil)
}
func findExtUiPath(backend string) (string, error) {
binName := "f4-qt-host"
if backend != "qt" {
binName = strings.TrimPrefix(backend, "ext:")
}
if runtime.GOOS == "windows" && !strings.HasSuffix(binName, ".exe") {
binName += ".exe"
}
if env := os.Getenv("F4_EXT_UI_PATH"); env != "" {
if extUiFileExists(env) {
return env, nil
}
}
var candidates []string
if exe, err := os.Executable(); err == nil {
exeDir := filepath.Dir(exe)
candidates = append(candidates, filepath.Join(exeDir, binName))
if runtime.GOOS == "darwin" && strings.HasSuffix(exeDir, ".app/Contents/MacOS") {
candidates = append(candidates, filepath.Join(filepath.Dir(filepath.Dir(exeDir)), "Resources", binName))
}
}
if cwd, err := os.Getwd(); err == nil {
for _, cfg := range []string{"RelWithDebInfo", "Release", "Debug"} {
candidates = append(candidates, filepath.Join(cwd, "qt", "host", "build", "bin", cfg, binName))
}
candidates = append(candidates,
filepath.Join(cwd, "qt", "host", "build", "bin", binName),
filepath.Join(cwd, "qt", "host", "build", binName),
)
}
for _, path := range candidates {
if extUiFileExists(path) {
return path, nil
}
}
return "", fmt.Errorf("external UI executable %q not found", binName)
}
func extUiFileExists(path string) bool {
if path == "" {
return false
}
st, err := os.Stat(path)
return err == nil && !st.IsDir()
}