This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (80 loc) · 2.11 KB
/
main.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
// This file is a part of git.thekondor.net/zvuchno.git (mirror: github.com/thekondor/zvuchno)
package main
import (
"github.com/godbus/dbus"
"github.com/sqp/pulseaudio"
"log"
)
type AppSettings struct {
Timeout uint32
Title string
OnMuteText string
OnUnmuteText string
}
type App struct {
deviceEventCh chan struct{}
volumeNotification VolumeNotification
bar VolumeBar
settings AppSettings
}
func (app *App) DeviceVolumeUpdated(path dbus.ObjectPath, values []uint32) {
app.lockPulseAudio(true)
defer app.lockPulseAudio(false)
if values[0] == values[1] {
app.volumeNotification.Show(app.settings.Title, app.bar.Update(int(values[0])), app.settings.Timeout)
}
}
func (app *App) DeviceMuteUpdated(path dbus.ObjectPath, value bool) {
app.lockPulseAudio(true)
defer app.lockPulseAudio(false)
var message string
if value {
message = app.settings.OnMuteText
} else {
message = app.settings.OnUnmuteText
}
app.volumeNotification.Show(app.settings.Title, message, app.settings.Timeout)
}
func (app *App) Loop(pa *pulseaudio.Client) {
app.lockPulseAudio(false)
pa.Register(app)
pa.Listen()
}
func (app *App) lockPulseAudio(state bool) {
if state {
<-app.deviceEventCh
} else {
app.deviceEventCh <- struct{}{}
}
}
func mustPulseAudio() *pulseaudio.Client {
pa, err := pulseaudio.New()
if err != nil {
log.Panicf("Failed to connect to PulseAudio: %s", err)
}
return pa
}
func mustDbus() *dbus.Conn {
sessionBus, err := dbus.SessionBus()
if err != nil {
log.Panicf("Failed to connect to DBUS session bus: %s", err)
}
return sessionBus
}
func main() {
pa := mustPulseAudio()
dbusConn := mustDbus()
config := NewConfig()
app := &App{
deviceEventCh: make(chan struct{}, 1),
volumeNotification: NewVolumeNotification(dbusConn),
bar: NewVolumeBar(config.Appearance.Width,
config.Appearance.Format.Full,
config.Appearance.Format.Bar),
settings: AppSettings{config.Notification.Timeout,
config.Appearance.Text.Title,
config.Appearance.Text.OnMute,
config.Appearance.Text.OnUnmute}}
log.Printf("Serving PA events...")
app.Loop(pa)
}