build: PKGBUILD added
[zvuchno.git] / main.go
1 // This file is a part of git.thekondor.net/zvuchno.git (mirror: github.com/thekondor/zvuchno)
2
3 package main
4
5 import (
6         "github.com/godbus/dbus"
7         "github.com/sqp/pulseaudio"
8         "log"
9 )
10
11 type AppSettings struct {
12         Timeout      uint32
13         Title        string
14         OnMuteText   string
15         OnUnmuteText string
16 }
17
18 type App struct {
19         deviceEventCh      chan struct{}
20         volumeNotification VolumeNotification
21         bar                VolumeBar
22         settings           AppSettings
23 }
24
25 func (app *App) DeviceVolumeUpdated(path dbus.ObjectPath, values []uint32) {
26         app.lockPulseAudio(true)
27         defer app.lockPulseAudio(false)
28
29         if values[0] == values[1] {
30                 app.volumeNotification.Show(app.settings.Title, app.bar.Update(int(values[0])), app.settings.Timeout)
31         }
32 }
33
34 func (app *App) DeviceMuteUpdated(path dbus.ObjectPath, value bool) {
35         app.lockPulseAudio(true)
36         defer app.lockPulseAudio(false)
37
38         var message string
39         if value {
40                 message = app.settings.OnMuteText
41         } else {
42                 message = app.settings.OnUnmuteText
43         }
44
45         app.volumeNotification.Show(app.settings.Title, message, app.settings.Timeout)
46 }
47
48 func (app *App) Loop(pa *pulseaudio.Client) {
49         app.lockPulseAudio(false)
50         pa.Register(app)
51         pa.Listen()
52 }
53
54 func (app *App) lockPulseAudio(state bool) {
55         if state {
56                 <-app.deviceEventCh
57         } else {
58                 app.deviceEventCh <- struct{}{}
59         }
60 }
61
62 func mustPulseAudio() *pulseaudio.Client {
63         pa, err := pulseaudio.New()
64         if err != nil {
65                 log.Panicf("Failed to connect to PulseAudio: %s", err)
66         }
67
68         return pa
69 }
70
71 func mustDbus() *dbus.Conn {
72         sessionBus, err := dbus.SessionBus()
73         if err != nil {
74                 log.Panicf("Failed to connect to DBUS session bus: %s", err)
75         }
76
77         return sessionBus
78 }
79
80 func main() {
81         pa := mustPulseAudio()
82         dbusConn := mustDbus()
83
84         config := NewConfig()
85         app := &App{
86                 deviceEventCh:      make(chan struct{}, 1),
87                 volumeNotification: NewVolumeNotification(dbusConn),
88                 bar: NewVolumeBar(config.Appearance.Width,
89                         config.Appearance.Format.Full,
90                         config.Appearance.Format.Bar),
91                 settings: AppSettings{config.Notification.Timeout,
92                         config.Appearance.Text.Title,
93                         config.Appearance.Text.OnMute,
94                         config.Appearance.Text.OnUnmute}}
95
96         log.Printf("Serving PA events...")
97         app.Loop(pa)
98 }