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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// 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 main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"danix.xyz/notifyd/internal/notify"
"github.com/godbus/dbus/v5"
)
func usage() {
fmt.Fprintf(os.Stderr, `usage: %s <verb>
list the live queue as JSON
history [n] the history ring as JSON, default 20
close <id> dismiss one notification
close-all dismiss every notification
action <id> <key> invoke an action on a live notification
clear-history empty the history ring
`, filepath.Base(os.Args[0]))
os.Exit(2)
}
func main() {
if len(os.Args) < 2 {
usage()
}
verb := os.Args[1]
switch verb {
case "list":
printFile("queue.json", 0)
case "history":
limit := 20
if len(os.Args) >= 3 {
n, err := strconv.Atoi(os.Args[2])
if err != nil {
usage()
}
limit = n
}
if limit < 1 {
fmt.Println("[]")
return
}
printFile("history.json", limit)
case "close":
need(3)
id := parseID(os.Args[2])
callControl("Dismiss", id)
case "close-all":
callControl("CloseAll")
case "action":
need(4)
id := parseID(os.Args[2])
callControl("InvokeAction", id, os.Args[3])
case "clear-history":
callControl("ClearHistory")
default:
usage()
}
}
func need(n int) {
if len(os.Args) < n {
usage()
}
}
func parseID(s string) uint32 {
id, err := strconv.ParseUint(s, 10, 32)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: bad id %q\n", filepath.Base(os.Args[0]), s)
os.Exit(1)
}
return uint32(id)
}
// printFile reads a published file, because the files are the interface.
func printFile(name string, limit int) {
data, err := os.ReadFile(filepath.Join(notify.RuntimeDir(), name))
if err != nil {
fmt.Fprintf(os.Stderr, "notifyctl: %v\n", err)
os.Exit(1)
}
if limit > 0 {
var all []notify.Popup
if err := json.Unmarshal(data, &all); err != nil {
fmt.Fprintf(os.Stderr, "notifyctl: %v\n", err)
os.Exit(1)
}
if len(all) > limit {
all = all[:limit]
}
out, _ := json.MarshalIndent(all, "", " ")
fmt.Println(string(out))
return
}
var pretty any
if err := json.Unmarshal(data, &pretty); err != nil {
fmt.Fprintf(os.Stderr, "notifyctl: %v\n", err)
os.Exit(1)
}
out, _ := json.MarshalIndent(pretty, "", " ")
fmt.Println(string(out))
}
// callControl drives the daemon over the private interface. It is the only
// thing that mutates state.
func callControl(method string, args ...any) {
conn, err := dbus.SessionBus()
if err != nil {
fmt.Fprintf(os.Stderr, "notifyctl: session bus: %v\n", err)
os.Exit(1)
}
obj := conn.Object(notify.Name, dbus.ObjectPath("/xyz/danix/Notifyd"))
if err := obj.Call("xyz.danix.Notifyd."+method, 0, args...).Err; err != nil {
fmt.Fprintf(os.Stderr, "notifyctl: %v\n", err)
os.Exit(1)
}
}
|