-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnotify.go
More file actions
233 lines (213 loc) · 5.54 KB
/
snotify.go
File metadata and controls
233 lines (213 loc) · 5.54 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"errors"
"log"
"os"
"sync"
"github.com/godbus/dbus/v5"
"github.com/jfreymuth/oggvorbis"
"github.com/jfreymuth/pulse"
)
const (
version float64 = 1.0
)
var (
busSigChan = make(chan notif, 16)
soundAllowed bool
dndLock sync.RWMutex
oggFile string
)
type notif struct {
Type string
ID string
}
func getConn() (conn *dbus.Conn) {
conn, err := dbus.ConnectSessionBus()
if err != nil {
log.Fatalln("Could not connect to session bus:", err)
}
return conn
}
type NotificationSound struct {
FileDescriptor dbus.UnixFD `db:"file-descriptor,omitempty"`
}
type PortalNotification struct {
Title string
Body string
Sound bool
ID string
}
func dndWatcher() () {
//conn := getConn()
env := os.Getenv("XDG_CURRENT_DESKTOP")
switch env {
case "GNOME":
monitorGNOMEDND()
default:
soundAllowed = true
log.Println("Do not disturb unsupported:", env)
}
}
func legacyNotifWatcher() () {
conn := getConn()
monitorObj := conn.Object("org.freedesktop.DBus", "/org/freedesktop/DBus")
ruleSlice := []string{
"type='method_call',interface='org.freedesktop.Notifications',member='Notify',path='/org/freedesktop/Notifications',destination='org.freedesktop.Notifications'",
//"type='method_call',interface='org.gtk.Notifications',member='AddNotification',path='/org/gtk/Notifications',destination='org.gtk.Notifications'", // GTK's notif API, do we really need those?
"type='method_call',interface='org.freedesktop.portal.Notification',member='AddNotification',path='/org/freedesktop/portal/desktop',destination='org.freedesktop.portal.Desktop'",
"type='method_call',interface='org.gtk.Notifications',member='AddNotification',path='/org/gtk/Notifications',destination='org.gtk.Notifications'",
}
arg2 := uint(0)
call := monitorObj.Call("org.freedesktop.DBus.Monitoring.BecomeMonitor", 0, ruleSlice, arg2)
if call.Err != nil {
log.Fatalln("Could not become bus monitor:", call.Err)
} else {
log.Println("Bus replied:", call.Body)
}
var sigChan = make(chan *dbus.Message, 16)
conn.Eavesdrop(sigChan)
var lastMsg classicNotifBody
log.Println("Initialized D-Bus connection")
for sig := range sigChan {
var con notif
var body classicNotifBody
err := dbus.Store(sig.Body,
&body.App,
&body.ReplaceID,
&body.Icon,
&body.Summary,
&body.Body,
&body.Actions,
&body.Hints,
&body.Expire,
)
if err != nil {
notif, err := decodePortalNotif(sig)
if err != nil {
notif, err := decodeGTKNotif(sig)
if err != nil {
log.Println("Could not decode notification: tried Legacy, Portal and GTK:", err)
continue
}
con.ID = notif.ID
con.Type = "GTK"
} else {
con.Type = "Portal"
con.ID = notif.ID
if notif.Sound {
log.Println("Portal notification has sound, suppressing ours")
continue
}
}
} else {
con.Type = "legacy"
con.ID = body.App
if lastMsg.Body == body.Body && lastMsg.App == body.App && lastMsg.Summary == body.Summary {
log.Println("Skipping duplicate notification")
lastMsg = classicNotifBody{}
continue
}
lastMsg = body
}
log.Println(con.ID, "sent", con.Type,"notification:", body)
busSigChan <- con
}
}
func decodeGTKNotif(msg *dbus.Message) (PortalNotification, error) {
var m = make(map[string]dbus.Variant)
var notif PortalNotification
var title string
err := dbus.Store(msg.Body, ¬if.ID, &title, &m)
if err != nil {
return notif, errors.New("Could not store message: " + err.Error())
}
notif.Title = title
return notif, nil
}
func decodePortalNotif(con *dbus.Message) (PortalNotification, error) {
var m = make(map[string]dbus.Variant)
var notif PortalNotification
err := dbus.Store(con.Body, ¬if.ID, &m)
if err != nil {
return notif, errors.New("Could not store message: " + err.Error())
}
val, ok := m["priority"]
if ok {
log.Println("Portal notification priority:", val)
}
_, ok = m["sound"]
if ok {
notif.Sound = true
}
return notif, nil
}
type classicNotifBody struct {
App string
ReplaceID uint32
Icon string
Summary string
Body string
Actions []string
Hints map[string]dbus.Variant
Expire int32
}
func audioController() {
client, err := pulse.NewClient(
pulse.ClientApplicationName("Snotify Notification Sounds"),
pulse.ClientApplicationIconName("notifications-new-symbolic"),
)
if err != nil {
log.Fatalln("Could not connect to PulseAudio:", err)
}
file, err := os.Open(oggFile)
if err != nil {
log.Fatalln("Could not open audio message file:", err)
}
defer file.Close()
readerFile, err := oggvorbis.NewReader(file)
if err != nil {
log.Fatalln("Could not read audio message file:", err)
}
reader := pulse.Float32Reader(func(f []float32) (int, error) {
return readerFile.Read(f)
})
playback, err := client.NewPlayback(
reader,
pulse.PlaybackSampleRate(readerFile.SampleRate()),
//pulse.PlaybackStereo,
pulse.PlaybackLatency(0.5),
)
if err != nil {
log.Fatalln("Could not request PulseAudio playback:", err)
}
defer playback.Close()
for sig := range busSigChan {
dndLock.RLock()
if soundAllowed == false {
dndLock.RUnlock()
log.Println("Not playing sound with DnD")
continue
}
dndLock.RUnlock()
playback.Stop()
readerFile.SetPosition(0)
log.Println("Playing sound for:", sig)
go playback.Start()
}
}
func main() {
log.Println("Starting snotify, version", version)
envFile := os.Getenv("SNOTIFY_OGG_FILE")
if len(envFile) == 0 {
oggFile = "/opt/snotify/message.ogg"
} else {
oggFile = envFile
}
go audioController()
go dndWatcher()
var wg sync.WaitGroup
wg.Go(func() {
legacyNotifWatcher()
})
wg.Wait()
}