-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnext-vnc.go
104 lines (86 loc) · 2.09 KB
/
next-vnc.go
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
package usrCmds
import (
"fmt"
"log"
"os"
"os/exec"
"slices"
"strings"
)
// TODO config
var skipOutputs = []string{}
func init() {
register("next-vnc", NextVnc)
}
func getOutputNum(name string) string {
output := strings.Split(name, "-")
if len(output) < 2 {
return ""
}
return output[1]
}
func NextVnc(d DaemonAPI, args map[string]string) (string, error) {
// current output
win := d.FocusedWindow()
// other visible outputs for this vnc session
visibleOutputs := []string{}
for _, name := range d.Outputs() {
if vncOutputVisible(getOutputNum(name)) &&
!slices.Contains(skipOutputs, name) {
visibleOutputs = append(visibleOutputs, name)
}
}
var nextOutput string
if len(visibleOutputs) == 0 {
return "", fmt.Errorf("no visible vnc outputs")
} else if len(visibleOutputs) <= 1 {
if os.Getenv("YASM_LOG") != "" {
log.Printf("only 1 output vnc visible (%s)", visibleOutputs[0])
}
nextOutput = visibleOutputs[0]
} else {
log.Printf("visible outputs: %s", visibleOutputs)
idx := slices.Index(visibleOutputs, win.Output)
if idx == -1 {
// current output not vnc visible, pick 1st visible one
idx = 0
}
if _, ok := args["--back"]; ok {
idx = (idx - 1)
if idx < 0 {
idx = len(visibleOutputs) - 1
}
} else {
// fwd
idx = (idx + 1) % len(visibleOutputs)
}
nextOutput = visibleOutputs[idx]
}
// find the MRU window on next output to get its workspace
for _, winId := range d.MruList() {
win := d.WindowById(winId)
if win.Output != nextOutput {
continue
}
err := d.FocusSpace(win.Workspace, win.Output)
// end
return nextOutput + " - " + win.Title + " (" + win.Workspace + ")\n", err
}
return "", fmt.Errorf("no windows found for output %s", nextOutput)
}
func vncOutputVisible(outputNum string) bool {
shell := os.Getenv("SHELL")
if len(shell) == 0 {
shell = "sh"
}
cmd := `wayvncctl -S /tmp/wayvnc-` + outputNum + ` client-list`
// TODO d.Log
if os.Getenv("YASM_LOG") != "" {
log.Println(cmd)
}
out, err := exec.Command(shell, "-c", cmd).Output()
if err != nil || len(out) == 0 {
return false
}
return true
}