commit 549b520a4c49edd8e10ecb6579819173cc88c4a03eef505ff4a1d69d4ad39328 Author: Viktor Varland Date: Thu Apr 3 09:37:57 2025 +0200 feat: notice daemon implementation diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..32b57c0 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.meatbag.se/varl/notice + +go 1.24.1 + +require github.com/godbus/dbus/v5 v5.1.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2be4302 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= +github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= diff --git a/main.go b/main.go new file mode 100644 index 0000000..cdbf9f8 --- /dev/null +++ b/main.go @@ -0,0 +1,114 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "sync" + "time" + + "github.com/godbus/dbus/v5" +) + +type notificationServer struct{} + +type Notification struct { + AppName string + ReplacesId uint32 + Icon string + Summary string + Body string + Actions []string + ExpireTimeout int32 +} + +// {"text": "$text", "alt": "$alt", "tooltip": "$tooltip", "class": "$class", "percentage": $percentage } +type Format struct { + Text string `json:"text"` +} + +func (s notificationServer) Notify(appName string, + replacesId uint32, + appIcon string, + summary string, + body string, + actions []string, + hints map[string]dbus.Variant, + expireTimeout int32) (uint32, *dbus.Error) { + + n := Notification{ + AppName: appName, + ReplacesId: replacesId, + Icon: appIcon, + Summary: summary, + Body: body, + Actions: actions, + ExpireTimeout: expireTimeout, + } + + log.Println(n) + + way := Format{ + Text: fmt.Sprintf("%s: %s", n.Summary, n.Body), + } + + res, err := json.Marshal(way) + if err != nil { + panic(err) + } + + defer func() { + timeout, err := time.ParseDuration(fmt.Sprintf("%vms", n.ExpireTimeout)) + if n.ExpireTimeout == -1 || err != nil { + timeout = time.Duration(5000 * time.Millisecond) + } + time.Sleep(timeout) + wg := sync.WaitGroup{} + wg.Add(1) + go s.CloseNotification(n.ReplacesId) + wg.Done() + }() + + //const ClearLine = "\033[2K" + //fmt.Printf(ClearLine) + //fmt.Printf("\r") + fmt.Printf("%s\n", res) + return 0, nil +} + +func (s notificationServer) CloseNotification(id uint32) { + log.Println("close the notification", id) + fmt.Printf("\n") +} + +func (s notificationServer) GetCapabilities() ([]string, *dbus.Error) { + return []string{"action-icons", "actions", "body", "body-hyperlinks", "body-images", "body-markup", "icon-multi", "icon-static", "persistence", "sound"}, nil +} + +func (s notificationServer) GetServerInformation() (string, string, string, string, *dbus.Error) { + return "notice", "meatbag.se", "1.0", "1.2", nil +} + +func main() { + conn, err := dbus.ConnectSessionBus() + + if err != nil { + panic(err) + } + + defer conn.Close() + + f := notificationServer{} + conn.Export(f, "/org/freedesktop/Notifications", "org.freedesktop.Notifications") + + reply, err := conn.RequestName("org.freedesktop.Notifications", dbus.NameFlagDoNotQueue) + if err != nil { + panic(err) + } + + if reply != dbus.RequestNameReplyPrimaryOwner { + log.Fatal("name already taken") + } + + select {} +}