feat: notice daemon implementation
This commit is contained in:
commit
549b520a4c
5
go.mod
Normal file
5
go.mod
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
module git.meatbag.se/varl/notice
|
||||
|
||||
go 1.24.1
|
||||
|
||||
require github.com/godbus/dbus/v5 v5.1.0 // indirect
|
||||
2
go.sum
Normal file
2
go.sum
Normal file
|
|
@ -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=
|
||||
114
main.go
Normal file
114
main.go
Normal file
|
|
@ -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 {}
|
||||
}
|
||||
Loading…
Reference in a new issue