-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
83 lines (70 loc) · 2.31 KB
/
api.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
package usrCmds
import (
"log"
"github.com/pancsta/gosway/ipc"
"github.com/pancsta/sway-yasm/internal/types"
)
type DaemonAPI interface {
FocusedWindow() types.WindowData
ListSpaces(skipOutputs []string) ([]string, error)
GetWinTreePath(id int) ([]*ipc.Node, error)
PrevWindow() types.WindowData
SwayMsgs(msgs []string) error
SwayMsg(msg string, args ...any) error
MoveWinToSpaceNum(winID, spaceNum int) error
MoveWinToSpace(winID int, space string) error
MoveSpaceToOutput(space, output string, focusedWinData types.WindowData) error
ListWindows() map[string]types.WindowData
MouseToOutput(output string) error
FocusWinID(id int) error
WinMatchApp(win types.WindowData, match string) bool
WinMatchTitle(win types.WindowData, match string) bool
}
type UserFunc func(DaemonAPI, map[string]string) (string, error)
type ListenerFuncs struct {
WinListenerFunc WinListenerFunc
ClipListenerFunc ClipListenerFunc
}
type WinListenerFunc func(DaemonAPI, types.WindowData)
type ClipListenerFunc func(DaemonAPI, string) string
var Registered map[string]UserFunc
var Listeners map[string][]*ListenerFuncs
// register registers a new user command function.
func register(name string, fn UserFunc) {
if Registered == nil {
Registered = make(map[string]UserFunc)
}
Registered[name] = fn
}
// listener registers a new event listener.
func listener(event string, fn *ListenerFuncs) {
if Listeners == nil {
Listeners = make(map[string][]*ListenerFuncs)
}
Listeners[event] = append(Listeners[event], fn)
}
// onClose registers a window "close" event listener.
func onClose(fn WinListenerFunc) {
listener("close", &ListenerFuncs{WinListenerFunc: fn})
}
// onFocus registers a window "focus" event listener.
func onFocus(fn WinListenerFunc) {
listener("focus", &ListenerFuncs{WinListenerFunc: fn})
}
// onNew registers a window "new" event listener.
func onNew(fn WinListenerFunc) {
listener("new", &ListenerFuncs{WinListenerFunc: fn})
}
// onCopy registers a clipboard event listener, triggered when a history item is
// copied.
func onCopy(fn ClipListenerFunc) {
listener("copy", &ListenerFuncs{ClipListenerFunc: fn})
}
// inspect prints the value to the daemon's log.
func inspect(val any) {
log.Printf("Inspect: %+v\n", val)
}
// inspect prints the value to the daemon's log.
func p(msg string, vals ...any) {
log.Printf(msg, vals...)
}