-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaemon.go
More file actions
155 lines (145 loc) · 3.57 KB
/
Copy pathdaemon.go
File metadata and controls
155 lines (145 loc) · 3.57 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
package main
import (
_ "embed"
"encoding/json"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"time"
"fyne.io/systray"
)
//go:embed assets/menubar-glyph.png
var menubarGlyph []byte // black elephant silhouette (macOS template — auto light/dark)
//go:embed assets/tray-glyph.ico
var trayGlyphICO []byte // teal elephant silhouette (Windows taskbar)
// runDaemon is the default (headless) mode: it owns the bridge, serves a local
// control API for the window, and shows a tray/menubar icon. No WebView.
func runDaemon() {
// Single instance: grab the control port. If it's already taken, a daemon is
// running — just open the config window and exit.
ln, err := net.Listen("tcp", daemonAddr)
if err != nil {
openWindow()
return
}
app := NewApp()
go app.serveControl(ln)
app.autoStart()
runTray(app) // blocks on the main thread (required for the menubar)
}
// serveControl exposes the bound methods to the window over localhost.
func (a *App) serveControl(ln net.Listener) {
mux := http.NewServeMux()
mux.HandleFunc("/rpc", func(w http.ResponseWriter, r *http.Request) {
var req struct {
M string `json:"M"`
A []json.RawMessage `json:"A"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{"r": a.dispatch(req.M, req.A)})
})
http.Serve(ln, mux)
}
// dispatch maps an RPC method name + JSON args to the real App method.
func (a *App) dispatch(m string, args []json.RawMessage) any {
s := func(i int) (v string) {
if i < len(args) {
json.Unmarshal(args[i], &v)
}
return
}
n := func(i int) (v int) {
if i < len(args) {
json.Unmarshal(args[i], &v)
}
return
}
b := func(i int) (v bool) {
if i < len(args) {
json.Unmarshal(args[i], &v)
}
return
}
switch m {
case "GetStatus":
return a.GetStatus()
case "GetConfig":
return a.GetConfig()
case "SetPeerIP":
return a.SetPeerIP(s(0))
case "SetRole":
return a.SetRole(s(0))
case "SetDirection":
return a.SetDirection(s(0))
case "SetVolume":
return a.SetVolume(n(0))
case "ApplyParams":
return a.ApplyParams(n(0), n(1), n(2), n(3))
case "Toggle":
return a.Toggle()
case "Verify":
return a.Verify()
case "DiscoverPeers":
return a.DiscoverPeers()
case "SetAutoStart":
return a.SetAutoStart(b(0))
}
return nil
}
// openWindow launches this same binary in window mode (the Wails config window).
func openWindow() {
exe, err := os.Executable()
if err != nil {
return
}
_ = exec.Command(exe, "--window").Start()
}
func runTray(app *App) {
onReady := func() {
if runtime.GOOS == "windows" {
systray.SetIcon(trayGlyphICO)
} else {
systray.SetTemplateIcon(menubarGlyph, menubarGlyph) // macOS adapts to light/dark
}
systray.SetTooltip("hearken")
mStatus := systray.AddMenuItem("starting…", "")
mStatus.Disable()
mOpen := systray.AddMenuItem("Open hearken", "Open the config window")
systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit hearken", "Stop the bridge and quit")
go func() {
for {
select {
case <-mOpen.ClickedCh:
openWindow()
case <-mQuit.ClickedCh:
app.Stop()
systray.Quit()
return
}
}
}()
go func() {
for {
s := app.GetStatus()
t := "Idle"
if s.Active {
if s.PeerConnected {
t = "Streaming · peer connected"
} else {
t = "Streaming · waiting for peer"
}
}
mStatus.SetTitle(t)
time.Sleep(2 * time.Second)
}
}()
}
systray.Run(onReady, func() { os.Exit(0) })
}