Skip to content

Commit

Permalink
fix: GNotifications not being detected & better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimiblock committed Feb 17, 2024
1 parent 8e7893c commit 6ae844f
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions snotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"os/exec"
"strings"
"sync"
"time"
)
Expand All @@ -13,9 +12,9 @@ var (
lastLine string
)

func monitorDbus() {
func monitorDbus(path, member string) {
for {
cmd := exec.Command("dbus-monitor", "path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications',member='Notify'")
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)
Expand All @@ -37,12 +36,11 @@ func monitorDbus() {
}

line := string(buf[:n])
if strings.Contains(line, "member=Notify") {
// Lock the mutex to safely update lastLine
mu.Lock()
lastLine = line
mu.Unlock()
}

// Lock the mutex to safely update lastLine
mu.Lock()
lastLine = line
mu.Unlock()
}

// Wait for the command to finish
Expand All @@ -51,12 +49,12 @@ func monitorDbus() {
}

// Sleep for a while before restarting dbus-monitor
time.Sleep(2 * time.Second)
time.Sleep(5 * time.Second)
}
}

func playSoundOnNewLine() {
ticker := time.NewTicker(400 * time.Millisecond)
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()

for range ticker.C {
Expand All @@ -66,7 +64,7 @@ func playSoundOnNewLine() {
mu.Unlock()

if currentLine != "" {
fmt.Println("Received a Notify event. Playing sound...")
fmt.Println("Received a Notify or AddNotification event. Playing sound...")
playSound()

// Clear lastLine to prevent repeated execution
Expand All @@ -78,7 +76,7 @@ func playSoundOnNewLine() {
}

func playSound() {
soundCmd := exec.Command("/usr/bin/paplay", "/opt/snotify/message.ogg")
soundCmd := exec.Command("mpv", "--no-config", "--really-quiet", "/opt/snotify/message.ogg")
if err := soundCmd.Start(); err != nil {
fmt.Println("Error playing sound:", err)
return
Expand All @@ -90,8 +88,9 @@ func playSound() {
}

func main() {
go monitorDbus() // Start monitoring dbus in a goroutine
go playSoundOnNewLine() // Start checking for new lines and playing sound in a goroutine
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 {}
Expand Down

0 comments on commit 6ae844f

Please sign in to comment.