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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
package srv
import (
"encoding/json"
"fmt"
"log"
"os"
"slices"
"sync"
"github.com/godbus/dbus/v5"
)
const DEFAULT_EXPIRATION = 5000
const SORT_DIRECTION = 1 // 1 = newest first, -1 = oldest first
type server struct {
notifications *notificationStack
conn *dbus.Conn
object dbus.ObjectPath
name string
}
func (s server) close(id uint32, reason closeReason) {
s.notifications.mutex.Lock()
defer s.notifications.mutex.Unlock()
n, ok := s.notifications.notifications[id]
if !ok {
return
}
if n.timer != nil {
n.timer.Stop()
}
if n.Image != "" {
os.Remove(n.Image)
}
delete(s.notifications.notifications, n.Id)
err := s.conn.Emit(s.object, s.name+".NotificationClosed", n.Id, reason)
if err != nil {
log.Print(err)
}
}
// TODO: relocate to cmd/corvid
func (s server) output() {
arr := make([]notification, len(s.notifications.notifications))
i := 0
for _, notification := range s.notifications.notifications {
arr[i] = notification
i++
}
slices.SortFunc(arr, func(a, b notification) int {
if a.Timestamp > b.Timestamp {
return SORT_DIRECTION
} else if a.Timestamp < b.Timestamp {
return -SORT_DIRECTION
} else {
if a.Id > b.Id {
return SORT_DIRECTION
} else if a.Id < b.Id {
return -SORT_DIRECTION
}
}
return 0
})
j, err := json.Marshal(arr)
if err != nil {
log.Fatalln(err)
}
fmt.Println(string(j))
}
func Start() {
const NOTIF_DBUS_OBJECT = "/org/freedesktop/Notifications"
const NOTIF_DBUS_NAME = "org.freedesktop.Notifications"
const CORVID_DBUS_OBJECT = "/sh/cxl/Corvid"
const CORVID_DBUS_NAME = "sh.cxl.Corvid"
notifications := notificationStack{
mutex: &sync.Mutex{},
notifications: make(map[uint32]notification),
nextId: 1,
}
conn, err := dbus.SessionBus()
if err != nil {
log.Fatal(err)
}
err = startDBusServer(
conn,
corvidServer{
notifications: ¬ifications,
conn: conn,
object: CORVID_DBUS_OBJECT,
name: CORVID_DBUS_NAME,
},
CORVID_DBUS_OBJECT,
CORVID_DBUS_NAME,
)
if err != nil {
log.Fatal(err)
}
err = startDBusServer(
conn,
notifServer{
notifications: ¬ifications,
conn: conn,
object: NOTIF_DBUS_OBJECT,
name: NOTIF_DBUS_NAME,
},
NOTIF_DBUS_OBJECT,
NOTIF_DBUS_NAME,
)
if err != nil {
log.Fatal(err)
}
fmt.Println("[]")
}
func startDBusServer(conn *dbus.Conn, v interface{}, object dbus.ObjectPath, name string) error {
conn.Export(v, object, name)
reply, err := conn.RequestName(name, dbus.NameFlagReplaceExisting|dbus.NameFlagDoNotQueue)
if err != nil {
return err
}
if reply != dbus.RequestNameReplyPrimaryOwner {
return fmt.Errorf("'%s' already taken", name)
}
log.Printf("connected to dbus as %s @ %s", name, object)
return nil
}
|