From c3c33008bc0aae4971d854e6edcd48144d593bfb Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Tue, 15 Sep 2026 13:49:33 +0200 Subject: feat: add the D-Bus service and the daemon Notify assigns an id and publishes; a replaces_id or stack tag reuses the id. CloseNotification closes with reason 3. The daemon claims org.freedesktop.Notifications and exits non-zero if it cannot, which is what happens while dunst still holds it. --- internal/notify/control.go | 24 +++++++ internal/notify/service.go | 134 +++++++++++++++++++++++++++++++++++++ internal/notify/service_test.go | 145 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 303 insertions(+) create mode 100644 internal/notify/control.go create mode 100644 internal/notify/service.go create mode 100644 internal/notify/service_test.go (limited to 'internal/notify') diff --git a/internal/notify/control.go b/internal/notify/control.go new file mode 100644 index 0000000..4783711 --- /dev/null +++ b/internal/notify/control.go @@ -0,0 +1,24 @@ +// Copyright (C) 2026 Danilo M. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +package notify + +import "github.com/godbus/dbus/v5" + +// control is the private interface notifyctl drives. +type control struct { + svc *Service +} + +func (c *control) CloseAll() *dbus.Error { + c.svc.store.DismissAll() + return nil +} diff --git a/internal/notify/service.go b/internal/notify/service.go new file mode 100644 index 0000000..d775aa6 --- /dev/null +++ b/internal/notify/service.go @@ -0,0 +1,134 @@ +// Copyright (C) 2026 Danilo M. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +package notify + +import ( + "log" + "sync" + "time" + + "github.com/godbus/dbus/v5" +) + +// Name is the well-known bus name the daemon owns. +const Name = "org.freedesktop.Notifications" + +const ( + objPath = "/org/freedesktop/Notifications" + iface = "org.freedesktop.Notifications" + ctrlPath = "/xyz/danix/Notifyd" + ctrlIface = "xyz.danix.Notifyd" +) + +// Service is the org.freedesktop.Notifications object. +type Service struct { + conn *dbus.Conn + store *Store + dir string + + mu sync.Mutex + timers map[uint32]*time.Timer +} + +func NewService(conn *dbus.Conn, dir string) *Service { + s := &Service{conn: conn, dir: dir, timers: map[uint32]*time.Timer{}} + s.store = NewStore(s.emitClosed, func(live, history []Popup) { + if err := Publish(s.dir, live, history); err != nil { + log.Printf("notifyd: publish: %v", err) + } + }) + return s +} + +// Start exports the interfaces and empties the state. Nothing from a previous +// run is resurrected. +func (s *Service) Start() error { + if err := s.conn.Export(s, dbus.ObjectPath(objPath), iface); err != nil { + return err + } + if err := s.conn.Export(&control{s}, dbus.ObjectPath(ctrlPath), ctrlIface); err != nil { + return err + } + s.store.Reset() + return nil +} + +func (s *Service) emitClosed(id, reason uint32) { + s.conn.Emit(dbus.ObjectPath(objPath), iface+".NotificationClosed", id, reason) +} + +// GetCapabilities tells clients what the daemon understands. actions and +// body-markup are load-bearing: mail-notify sends actions and escapes its body +// because the running dunst advertises markup. +func (s *Service) GetCapabilities() ([]string, *dbus.Error) { + return []string{"actions", "body-markup", "icon-static", "persistence"}, nil +} + +func (s *Service) GetServerInformation() (string, string, string, string, *dbus.Error) { + return "danix", "danix", "0.1", "1.2", nil +} + +// Notify is the spec's entry point. The id is returned to the client; a +// replace reuses the id of what it replaced. +func (s *Service) Notify(appName string, replacesID uint32, appIcon, summary, body string, actions []string, hints map[string]dbus.Variant, expireTimeout int32) (uint32, *dbus.Error) { + u := UrgencyFromHints(hints) + tag := StackTagFromHints(hints) + now := time.Now().UnixMilli() + ms := EffectiveTimeoutMS(expireTimeout, u) + n := &Popup{ + App: appName, + Summary: summary, + Body: body, + Urgency: u, + Icon: appIcon, + Actions: ParseActions(actions), + Created: now, + } + if ms > 0 { + n.Expires = now + ms + } + id, _ := s.store.Add(n, tag, replacesID) + s.arm(id, ms) + return id, nil +} + +// CloseNotification is the spec's programmatic close, reason 3. +func (s *Service) CloseNotification(id uint32) *dbus.Error { + s.stopTimer(id) + s.store.Dismiss(id, 3) + return nil +} + +func (s *Service) arm(id uint32, ms int64) { + s.mu.Lock() + defer s.mu.Unlock() + s.stopTimerLocked(id) + if ms <= 0 { + return + } + s.timers[id] = time.AfterFunc(time.Duration(ms)*time.Millisecond, func() { + s.store.Expire(id) + }) +} + +func (s *Service) stopTimer(id uint32) { + s.mu.Lock() + defer s.mu.Unlock() + s.stopTimerLocked(id) +} + +func (s *Service) stopTimerLocked(id uint32) { + if t, ok := s.timers[id]; ok { + t.Stop() + delete(s.timers, id) + } +} diff --git a/internal/notify/service_test.go b/internal/notify/service_test.go new file mode 100644 index 0000000..e5395db --- /dev/null +++ b/internal/notify/service_test.go @@ -0,0 +1,145 @@ +// Copyright (C) 2026 Danilo M. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +package notify + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + "time" + + "github.com/godbus/dbus/v5" +) + +// The integration tests need a session bus this process can own the name on. +// Run them with dbus-run-session -- go test ./internal/notify +func busOrSkip(t *testing.T) *dbus.Conn { + t.Helper() + conn, err := dbus.SessionBus() + if err != nil { + t.Skipf("no session bus: %v", err) + } + reply, err := conn.RequestName(Name, dbus.NameFlagDoNotQueue) + if err != nil { + t.Skipf("cannot request the name: %v", err) + } + // AlreadyOwner happens on the second bus test in one process, because + // dbus.SessionBus is a shared connection. That is fine: the name is ours. + if reply != dbus.RequestNameReplyPrimaryOwner && reply != dbus.RequestNameReplyAlreadyOwner { + t.Skipf("cannot own the name, run under dbus-run-session (reply %v)", reply) + } + return conn +} + +func TestIdentityAndCapabilities(t *testing.T) { + s := &Service{} + name, vendor, _, spec, err := s.GetServerInformation() + if err != nil { + t.Fatalf("GetServerInformation: %v", err) + } + if name != "danix" || vendor != "danix" || spec != "1.2" { + t.Errorf("identity = %q/%q spec %q, want danix/danix 1.2", name, vendor, spec) + } + caps, err := s.GetCapabilities() + if err != nil { + t.Fatalf("GetCapabilities: %v", err) + } + for _, want := range []string{"actions", "body-markup", "icon-static", "persistence"} { + found := false + for _, c := range caps { + if c == want { + found = true + } + } + if !found { + t.Errorf("missing capability %q in %v", want, caps) + } + } +} + +func TestNotifyReturnsAnIDAndPublishes(t *testing.T) { + conn := busOrSkip(t) + dir := t.TempDir() + svc := NewService(conn, dir) + if err := svc.Start(); err != nil { + t.Fatalf("Start: %v", err) + } + + obj := conn.Object(Name, dbus.ObjectPath(objPath)) + call := obj.Call(iface+".Notify", 0, + "app", uint32(0), "icon", "summary", "body", + []string{"default", "open"}, + map[string]dbus.Variant{"urgency": dbus.MakeVariant(byte(1))}, + int32(0)) + if call.Err != nil { + t.Fatalf("Notify: %v", call.Err) + } + var id uint32 + if err := call.Store(&id); err != nil { + t.Fatalf("Notify reply: %v", err) + } + if id == 0 { + t.Fatal("Notify returned id 0") + } + + data, err := os.ReadFile(filepath.Join(dir, "queue.json")) + if err != nil { + t.Fatalf("read queue: %v", err) + } + var live []Popup + if err := json.Unmarshal(data, &live); err != nil { + t.Fatalf("queue not JSON: %v", err) + } + if len(live) != 1 || live[0].Summary != "summary" { + t.Fatalf("queue = %+v, want one summary", live) + } + if live[0].Expires == 0 { + t.Error("expires is zero, want a normal timeout") + } +} + +func TestCloseNotificationEmitsReasonThree(t *testing.T) { + conn := busOrSkip(t) + dir := t.TempDir() + svc := NewService(conn, dir) + if err := svc.Start(); err != nil { + t.Fatalf("Start: %v", err) + } + signals := make(chan *dbus.Signal, 4) + conn.Signal(signals) + if err := conn.AddMatchSignal(dbus.WithMatchObjectPath(dbus.ObjectPath(objPath))); err != nil { + t.Fatalf("AddMatchSignal: %v", err) + } + + obj := conn.Object(Name, dbus.ObjectPath(objPath)) + call := obj.Call(iface+".Notify", 0, "app", uint32(0), "", "s", "b", []string{}, map[string]dbus.Variant{}, int32(-1)) + var id uint32 + if err := call.Store(&id); err != nil { + t.Fatalf("Notify reply: %v", err) + } + if err := obj.Call(iface+".CloseNotification", 0, id).Err; err != nil { + t.Fatalf("CloseNotification: %v", err) + } + + select { + case sig := <-signals: + if sig.Name != iface+".NotificationClosed" { + t.Fatalf("signal %q, want NotificationClosed", sig.Name) + } + if sig.Body[0].(uint32) != id || sig.Body[1].(uint32) != 3 { + t.Errorf("signal body = %v, want id %d reason 3", sig.Body, id) + } + case <-time.After(2 * time.Second): + t.Fatal("no NotificationClosed signal") + } +} -- cgit v1.2.3