-
Notifications
You must be signed in to change notification settings - Fork 0
/
snotify.go
97 lines (79 loc) · 2.16 KB
/
snotify.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
package main
import (
"fmt"
"os/exec"
"sync"
"time"
)
var (
mu sync.Mutex
lastLine string
)
func monitorDbus(path, member string) {
for {
cmd := exec.Command("dbus-monitor", fmt.Sprintf("path='%s',member='%s'", path, member))
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("Error creating stdout pipe:", err)
return
}
if err := cmd.Start(); err != nil {
fmt.Println("Error starting dbus-monitor:", err)
return
}
buf := make([]byte, 512) // Limit the line length to 512 bytes
for {
n, err := stdout.Read(buf)
if err != nil {
fmt.Println("Error reading from dbus-monitor:", err)
break
}
line := string(buf[:n])
// Lock the mutex to safely update lastLine
mu.Lock()
lastLine = line
mu.Unlock()
}
// Wait for the command to finish
if err := cmd.Wait(); err != nil {
fmt.Println("dbus-monitor exited with an error:", err)
}
// Sleep for a while before restarting dbus-monitor
time.Sleep(5 * time.Second)
}
}
func playSoundOnNewLine() {
ticker := time.NewTicker(900 * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
// Lock the mutex to safely read lastLine
mu.Lock()
currentLine := lastLine
mu.Unlock()
if currentLine != "" {
fmt.Println("Received a Notify or AddNotification event. Playing sound...")
playSound()
// Clear lastLine to prevent repeated execution
mu.Lock()
lastLine = ""
mu.Unlock()
}
}
}
func playSound() {
soundCmd := exec.Command("paplay", "/opt/snotify/message.ogg", "--client-name=snotify")
if err := soundCmd.Start(); err != nil {
fmt.Println("Error playing sound:", err)
return
}
if err := soundCmd.Wait(); err != nil {
fmt.Println("Error waiting for sound:", err)
}
}
func main() {
go monitorDbus("/org/freedesktop/Notifications", "Notify") // Start monitoring dbus for the first path and member
go monitorDbus("/org/gtk/Notifications", "AddNotification") // Start monitoring dbus for the second path and member
go playSoundOnNewLine() // Start checking for new lines and playing sound in a goroutine
// The program will run indefinitely without waiting for Enter key input
select {}
}