Source file notification.ml
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
open Lwt_react
open Lwt
open OBus_value
let app_name = ref (Filename.basename Sys.argv.(0))
let desktop_entry = ref None
type server_info = {
server_name : string;
server_vendor : string;
server_version : string;
server_spec_version : string;
}
type image = {
img_width : int;
img_height : int;
img_rowstride : int;
img_has_alpha : bool;
img_bits_per_sample : int;
img_channels : int;
img_data : string;
}
type urgency = [ `Low | `Normal | `Critical ]
type id = int32
type notification = {
mutable notif_deleted : bool;
notif_action : string -> unit;
notif_closed : unit -> unit;
}
type 'a t = {
result : 'a Lwt.t;
notification : notification;
peer : OBus_peer.t;
id : id;
}
module Peer_map = Map.Make(OBus_peer)
module Id_map = Map.Make(Int32)
let notifications : notification Id_map.t ref Peer_map.t ref = ref Peer_map.empty
let default_action = "default"
let server_name = "org.freedesktop.Notifications"
let server_path = ["org"; "freedesktop"; "Notifications"]
open Notification_interfaces.Org_freedesktop_Notifications
let proxy = lazy(
let%lwt bus = OBus_bus.session () in
return (OBus_proxy.make (OBus_peer.make bus server_name) server_path)
)
let get_server_information () =
let%lwt proxy = Lazy.force proxy in
let%lwt name, vendor, version, spec_version = OBus_method.call m_GetServerInformation proxy () in
return {
server_name = name;
server_vendor = vendor;
server_version = version;
server_spec_version = spec_version;
}
let get_capabilities () =
let%lwt proxy = Lazy.force proxy in
OBus_method.call m_GetCapabilities proxy ()
let notify proxy ~app_name ~id ~icon ~summary ~body ~actions ~hints ~timeout =
let%lwt context, return_id = OBus_method.call_with_context m_Notify proxy (app_name, id, icon, summary, body, actions, hints, Int32.of_int timeout) in
return (OBus_context.sender context, return_id)
let close_notification proxy id =
OBus_method.call m_CloseNotification proxy id
let s_NotificationClosed =
OBus_member.Signal.make
~interface:"org.freedesktop.Notifications"
~member:"NotificationClosed"
~args:(arg2
(None, C.basic_uint32)
(None, C.basic_uint32))
~annotations:[]
let notification_closed proxy =
OBus_signal.make s_NotificationClosed proxy
let s_ActionInvoked =
OBus_member.Signal.make
~interface:"org.freedesktop.Notifications"
~member:"ActionInvoked"
~args:(arg2
(None, C.basic_uint32)
(None, C.basic_string))
~annotations:[]
let action_invoked proxy =
OBus_signal.make s_ActionInvoked proxy
let monitor_peer peer =
ignore begin
let%lwt () = OBus_peer.wait_for_exit peer in
let m = Peer_map.find peer !notifications in
notifications := Peer_map.remove peer !notifications;
Id_map.iter (fun id notif -> notif.notif_closed ()) !m;
return ()
end
let remove_notification peer id notif =
notif.notif_deleted <- true;
let r = Peer_map.find peer !notifications in
r := Id_map.remove id !r
let init_callbacks = lazy(
let%lwt bus = OBus_bus.session () in
let anonymous_proxy = { OBus_proxy.peer = OBus_peer.anonymous bus;
OBus_proxy.path = server_path } in
let%lwt event =
OBus_signal.connect
(OBus_signal.map_with_context
(fun context (id, reason) -> (OBus_context.sender context, id, reason))
(notification_closed anonymous_proxy))
in
E.keep
(E.map_p
(fun (peer, id, reason) ->
match try Some(Peer_map.find peer !notifications) with Not_found -> None with
| None ->
return ()
| Some m ->
match try Some(Id_map.find id !m) with Not_found -> None with
| None ->
return ()
| Some notif ->
remove_notification peer id notif;
notif.notif_closed ();
return ())
event);
let%lwt event =
OBus_signal.connect
(OBus_signal.map_with_context
(fun context (id, action) -> (OBus_context.sender context, id, action))
(action_invoked anonymous_proxy))
in
E.keep
(E.map_p
(fun (peer, id, action) ->
match try Some(Peer_map.find peer !notifications) with Not_found -> None with
| None ->
return ()
| Some m ->
match try Some(Id_map.find id !m) with Not_found -> None with
| None ->
return ()
| Some notif ->
remove_notification peer id notif;
notif.notif_action action;
return ())
event);
return ()
)
let result n = n.result
let close n =
let notif = n.notification in
if not notif.notif_deleted then begin
remove_notification n.peer n.id notif;
notif.notif_closed ();
close_notification (OBus_proxy.make n.peer server_path) n.id
end else
return ()
let rec filter_opt = function
| [] -> []
| Some x :: l ->
x :: filter_opt l
| None :: l ->
filter_opt l
let default_desktop_entry = desktop_entry
let notify ?(app_name= !app_name) ?desktop_entry
?replace ?(icon="") ?image ~summary ?(body="") ?(actions=[])
?urgency ?category ?sound_file ?suppress_sound ?pos ?(hints=[]) ?(timeout= -1) () =
let desktop_entry =
match desktop_entry with
| None -> !default_desktop_entry
| x -> x
in
let make_hint name x f =
match x with
| Some x -> Some(name, f x)
| None -> None
in
let hints =
filter_opt
[make_hint "desktop-entry" desktop_entry V.basic_string;
make_hint "image_data" image
(fun image ->
V.structure
[V.basic_int32 (Int32.of_int image.img_width);
V.basic_int32 (Int32.of_int image.img_height);
V.basic_int32 (Int32.of_int image.img_rowstride);
V.basic_boolean image.img_has_alpha;
V.basic_int32 (Int32.of_int image.img_bits_per_sample);
V.basic_int32 (Int32.of_int image.img_channels);
V.byte_array image.img_data]);
make_hint "urgency" urgency
(fun urgency ->
V.basic_int32 (match urgency with
| `Low -> 0l
| `Normal -> 1l
| `Critical -> 2l));
make_hint "category" category V.basic_string;
make_hint "sound-file" sound_file V.basic_string;
make_hint "suppress-sound" suppress_sound V.basic_boolean;
make_hint "x" pos (fun (x, y) -> V.basic_int32(Int32.of_int x));
make_hint "y" pos (fun (x, y) -> V.basic_int32(Int32.of_int y))]
@ hints in
let _, actions, actions_map =
List.fold_right
(fun (text, user_key) (acc, al, am) ->
let key = Printf.sprintf "key%d" acc in
(acc + 1, key :: text :: al, (key, user_key) :: am))
actions (0, [], []) in
let actions_map = (default_action, `Default) :: actions_map in
let%lwt () = Lazy.force init_callbacks in
let%lwt daemon = Lazy.force proxy in
let%lwt peer, id =
notify
daemon
~app_name
~id:(match replace with
| Some n -> n.id
| None -> 0l)
~icon
~summary
~body
~actions
~hints
~timeout
in
let waiter, wakener = wait () in
let notif = {
notif_deleted = false;
notif_action = (fun action ->
wakeup wakener (try
List.assoc action actions_map
with Not_found ->
`Default));
notif_closed = (fun () -> wakeup wakener `Closed);
} in
begin
try
let r = Peer_map.find peer !notifications in
r := Id_map.add id notif !r
with Not_found ->
notifications :=
Peer_map.add
peer
(ref (Id_map.add id notif Id_map.empty))
!notifications;
monitor_peer peer
end;
return {
result = waiter;
notification = notif;
peer = peer;
id = id;
}