This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swaypanion.go
101 lines (76 loc) · 2.03 KB
/
swaypanion.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
package swaypanion
import (
"github.com/willoma/swaypanion/config"
"github.com/willoma/swaypanion/modules"
"github.com/willoma/swaypanion/notification"
socketserver "github.com/willoma/swaypanion/socket/server"
"github.com/willoma/swaypanion/sway"
)
type Swaypanion struct {
config *config.Config
sway *sway.Client
notification *notification.Notification
socketserver *socketserver.Server
coreNotifier *notification.MessageNotifier
stopConfigReload func()
modules []any
}
func New() (*Swaypanion, error) {
sock, err := socketserver.New()
if err != nil {
return nil, err
}
notif, err := notification.New()
if err != nil {
return nil, err
}
coreNotifier := notif.MessageNotifier()
conf, err := config.New(coreNotifier)
if err != nil {
return nil, err
}
s := &Swaypanion{
config: conf,
sway: sway.NewClient(),
notification: notif,
socketserver: sock,
coreNotifier: coreNotifier,
}
s.socketserver.AddCommands(conf.SocketCommands())
s.register(modules.NewBacklight(conf.Backlight, notif))
s.register(modules.NewPlayer(conf.Player, s.sway))
s.register(modules.NewVolume(conf.Volume, notif))
s.register(modules.NewSwayNodes(conf.SwayNodes, s.sway))
s.reloadConfig(conf)
s.stopConfigReload = conf.ListenReload(s.reloadConfig)
coreNotifier.Notify("Swaypanion started")
return s, nil
}
func (s *Swaypanion) Stop() error {
if s.stopConfigReload != nil {
s.stopConfigReload()
}
if err := s.config.Close(); err != nil {
return err
}
for _, module := range s.modules {
if stopperModule, ok := module.(Stopper); ok {
stopperModule.Stop()
}
}
s.sway.Close()
if err := s.socketserver.Close(); err != nil {
return err
}
s.coreNotifier.Notify("Swaypanion stopped")
return nil
}
func (s *Swaypanion) reloadConfig(conf *config.Config) {
s.coreNotifier.Reconfigure(conf.CoreMessages)
}
func (s *Swaypanion) register(module any) {
s.modules = append(s.modules, module)
if mod, ok := module.(socketCommander); ok {
s.socketserver.AddCommands(mod.SocketCommands())
}
}