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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use crate::utils::Dbg;
use std::sync::Mutex;
use std::sync::Arc;
use dbus::channel::Sender;
use std::collections::HashMap;
use crate::{IfaceToken, Crossroads, Context, MethodErr};
use dbus::arg::{Variant, RefArg, Arg, Append, PropMap};
use std::marker::PhantomData;
use crate::ifacedesc::EMITS_CHANGED;
fn introspect(cr: &Crossroads, path: &dbus::Path<'static>) -> String {
let mut children = cr.get_children(path, true);
let mut childstr = String::new();
children.sort_unstable();
for c in children {
childstr += &format!(" <node name=\"{}\"/>\n", c);
}
let (reg, ifaces) = cr.registry_and_ifaces(path);
let ifacestr = reg.introspect(ifaces);
let nodestr = format!(
r##"<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="{}">
{}{}</node>"##, path, ifacestr, childstr);
nodestr
}
pub fn introspectable(cr: &mut Crossroads) -> IfaceToken<()> {
cr.register("org.freedesktop.DBus.Introspectable", |b| {
b.method_with_cr("Introspect", (), ("xml_data",), |ctx, cr, _: ()| {
Ok((introspect(cr, ctx.path()),))
});
})
}
pub (crate) fn make_emits_message<V: dbus::arg::Arg + dbus::arg::Append>(prop_name: &str, emits_changed: &str, ctx: &Context, v: &V) -> Option<dbus::Message> {
let arr = [prop_name];
let (d, i) = match emits_changed {
"false" => return None,
"invalidates" => (None, &arr[..]),
"true" => (Some((arr[0], Variant(v))), &[][..]),
_ => panic!("Invalid value of EmitsChangedSignal: {:?}", emits_changed)
};
use dbus::message::SignalArgs;
use dbus::blocking::stdintf::org_freedesktop_dbus::PropertiesPropertiesChanged as PPC;
let s: &str = ctx.message().read1().unwrap();
Some(dbus::Message::signal(ctx.path(), &PPC::INTERFACE.into(), &PPC::NAME.into())
.append3(s, dbus::arg::Dict::new(d), i))
}
#[derive(Debug)]
pub struct PropContext {
path: dbus::Path<'static>,
interface: dbus::strings::Interface<'static>,
name: String,
context: Option<Context>,
iface_token: usize,
emits_changed: Option<&'static str>,
get_all: Option<Arc<Mutex<PropAllCtx>>>,
}
impl PropContext {
pub fn path(&self) -> &dbus::Path<'static> { &self.path }
pub fn interface(&self) -> &dbus::strings::Interface<'static> { &self.interface }
pub fn name(&self) -> &str { &self.name }
pub fn message(&self) -> Option<&dbus::Message> { self.context.as_ref().map(|ctx| ctx.message()) }
pub fn reply<A: Arg + RefArg + Send + Append + 'static>(&mut self, reply: Result<A, MethodErr>) -> PhantomData<A> {
if let Some(ec) = &self.emits_changed {
let mut emit_msg = None;
if let Ok(v) = &reply {
if let Some(ctx) = &self.context {
emit_msg = make_emits_message(&self.name, ec, &ctx, v);
}
}
self.reply_noemit(reply.map(|_| ()));
emit_msg.map(|emit_msg| self.context.as_mut().map(|ctx| { ctx.push_msg(emit_msg) }));
} else {
if let Some(ga) = &self.get_all {
ga.lock().unwrap().add_reply(self.name.clone(), reply.ok().map(|a| Box::new(a) as Box<(dyn RefArg + Send)>));
} else {
self.context.as_mut().map(|ctx| ctx.reply(reply.map(|a| (Variant(a),))));
}
}
PhantomData
}
pub fn reply_noemit(&mut self, reply: Result<(), MethodErr>) {
debug_assert!(self.emits_changed.is_some());
self.context.as_mut().map(|ctx| ctx.reply(reply));
}
pub (crate) fn set_send_on_drop(&mut self, value: Arc<dyn Sender + Send + Sync>) {
if let Some(get_all) = &self.get_all {
let mut pactx = get_all.lock().unwrap();
if let Some(ref mut propctx) = pactx.propctx {
propctx.context.as_mut().map(|ctx| ctx.set_send_on_drop(value));
return;
}
}
self.context.as_mut().map(|ctx| ctx.set_send_on_drop(value));
}
fn new(cr: &Crossroads, path: dbus::Path<'static>, interface: String, name: String) -> Result<Self, MethodErr> {
let interface = dbus::strings::Interface::new(interface).map_err(|s| MethodErr::no_interface(&s))?;
let iface_token = cr.find_iface_token(&path, Some(&interface))?;
Ok(PropContext {
path,
iface_token,
interface,
name,
get_all: None,
context: None,
emits_changed: None
})
}
fn call_prop(mut self, cr: &mut Crossroads, is_set: bool) -> Option<Self> {
let token = self.iface_token;
let name = self.name.clone();
let mut cb = match self.check(|_| {
cr.registry().take_prop(token, &name, is_set)
}) {
Ok(cb) => cb,
Err(_) => return Some(self)
};
let octx = cb(self, cr);
cr.registry().give_prop(token, &name, cb, is_set);
octx
}
fn call_all_props<F: FnOnce(&mut PropAllCtx) + Send + 'static>(self, cr: &mut Crossroads, f: F) -> Option<Self> {
let token = self.iface_token;
let pactx = Arc::new(Mutex::new(PropAllCtx {
remaining: 0,
answers: HashMap::new(),
donefn: Some(Dbg(Box::new(f))),
propctx: Some(self),
}));
let mut pb = pactx.lock().unwrap();
let pctxs: Vec<_> = cr.registry().prop_names_readable(token).map(|prop_name| {
pb.remaining += 1;
let parent = pb.propctx.as_ref().unwrap();
PropContext {
path: parent.path.clone(),
iface_token: parent.iface_token,
interface: parent.interface().clone(),
name: prop_name.into(),
get_all: Some(pactx.clone()),
context: None,
emits_changed: None
}
}).collect();
drop(pb);
for pctx in pctxs {
pctx.call_prop(cr, false);
}
let mut temp = pactx.lock().unwrap();
if temp.check_finished() { Some(temp.propctx.take().unwrap()) } else { None }
}
pub fn check<R, F: FnOnce(Option<&mut Context>) -> Result<R, MethodErr>>(&mut self, f: F) -> Result<R, ()> {
match &mut self.context {
Some(ctx) => ctx.check(|ctx| f(Some(ctx))),
None => match f(None) {
Ok(r) => Ok(r),
Err(_) => { todo!() },
},
}
}
}
#[derive(Debug)]
struct PropAllCtx {
remaining: usize,
answers: PropMap,
donefn: Option<Dbg<Box<dyn FnOnce(&mut Self) + Send + 'static>>>,
propctx: Option<PropContext>,
}
impl PropAllCtx {
fn check_finished(&mut self) -> bool {
if self.remaining > 0 { return false; }
if let Some(donefn) = self.donefn.take() {
(donefn.0)(self);
}
true
}
fn add_reply(&mut self, prop_name: String, prop_value: Option<Box<dyn RefArg + Send>>) {
if let Some(v) = prop_value {
self.answers.insert(prop_name, Variant(v));
}
self.remaining -= 1;
self.check_finished();
}
}
fn get(mut ctx: Context, cr: &mut Crossroads, (interface_name, property_name): (String, String)) -> Option<Context> {
let mut propctx = match ctx.check(|ctx| { PropContext::new(cr, ctx.path().clone(), interface_name, property_name)}) {
Ok(p) => p,
Err(_) => return Some(ctx),
};
propctx.context = Some(ctx);
propctx.call_prop(cr, false).map(|propctx| { propctx.context.unwrap() })
}
fn getall_all(ctx: Context, cr: &mut Crossroads) -> Option<Context> {
get_all_for_path(&ctx.path().clone(), cr, Some(ctx), move |ictx, octx| {
let props: HashMap<_, _> = ictx.ifaces.values().flatten().collect();
octx.as_mut().unwrap().do_reply(|msg| {
msg.append_all((props,));
});
})
}
fn getall(mut ctx: Context, cr: &mut Crossroads, (interface_name,): (String,)) -> Option<Context> {
if interface_name == "" {
return getall_all(ctx, cr);
}
let mut propctx = match ctx.check(|ctx| { PropContext::new(cr, ctx.path().clone(), interface_name, "".into())}) {
Ok(p) => p,
Err(_) => return Some(ctx),
};
propctx.context = Some(ctx);
propctx.call_all_props(cr, move |pactx| {
let pctx = pactx.propctx.as_mut().unwrap();
let answers = &pactx.answers;
pctx.context.as_mut().unwrap().do_reply(|msg| {
msg.append_all((answers,));
});
}).map(|propctx| { propctx.context.unwrap() })
}
fn set(mut ctx: Context, cr: &mut Crossroads, (interface_name, property_name, _value): (String, String, Variant<Box<dyn RefArg>>)) -> Option<Context> {
let mut propctx = match ctx.check(|ctx| { PropContext::new(cr, ctx.path().clone(), interface_name, property_name) }) {
Ok(p) => p,
Err(_) => return Some(ctx),
};
let ann = cr.registry()
.find_annotation(propctx.iface_token, EMITS_CHANGED, Some(&propctx.name));
propctx.emits_changed = match ann {
Some("const") => Some("const"),
Some("false") => Some("false"),
Some("invalidates") => Some("invalidates"),
_ => Some("true"),
};
propctx.context = Some(ctx);
propctx.call_prop(cr, true).map(|propctx| { propctx.context.unwrap() })
}
pub fn properties(cr: &mut Crossroads) -> IfaceToken<()> {
cr.register("org.freedesktop.DBus.Properties", |b| {
b.method_with_cr_custom::<_, (Variant<u8>,), _, _>("Get", ("interface_name", "property_name"), ("value",), get);
b.method_with_cr_custom::<_, (PropMap,), _, _>("GetAll", ("interface_name",), ("properties",), getall);
b.method_with_cr_custom::<_, (), _, _>("Set", ("interface_name", "property_name", "value"), (), set);
b.signal::<(String, PropMap, Vec<String>), _>("PropertiesChanged",
("interface_name", "changed_properties", "invalidated_properties"));
})
}
#[derive(Debug, Default)]
struct IfaceContext {
remaining: usize,
ifaces: IfacePropMap,
donefn: Option<Dbg<Box<dyn FnOnce(&mut IfaceContext, &mut Option<Context>) + Send + 'static>>>,
}
type IfacePropMap = HashMap<String, PropMap>;
type PathPropMap = HashMap<dbus::Path<'static>, IfacePropMap>;
fn get_all_for_path<F>(path: &dbus::Path<'static>, cr: &mut Crossroads, octx: Option<Context>, f: F) -> Option<Context>
where F: FnOnce(&mut IfaceContext, &mut Option<Context>) + Send + 'static {
let (_reg, ifaces) = cr.registry_and_ifaces(&path);
let all: Vec<usize> = ifaces.into_iter().map(|token| *token).collect();
for_each_interface_with_properties(path, all, cr, octx, f)
}
fn for_each_interface_with_properties<F: FnOnce(&mut IfaceContext, &mut Option<Context>) + Send + 'static, I: IntoIterator<Item = usize>>
(path: &dbus::Path<'static>, ifaces: I, cr: &mut Crossroads, mut octx: Option<Context>, f: F) -> Option<Context> {
let ictx: Arc<Mutex<IfaceContext>> = Default::default();
let (reg, _all_ifaces) = cr.registry_and_ifaces(&path);
let all: Vec<_> = ifaces.into_iter().filter_map(|token| {
let iface_name = reg.get_intf_name(token)?;
Some(PropContext {
context: None,
emits_changed: None,
get_all: None,
iface_token: token,
interface: iface_name.clone(),
path: path.clone(),
name: "".into(),
})
}).collect();
if all.len() == 0 {
f(&mut *ictx.lock().unwrap(), &mut octx);
return octx;
}
let mut ic = ictx.lock().unwrap();
ic.remaining = all.len();
ic.donefn = Some(Dbg(Box::new(f)));
drop(ic);
let amoctx = Arc::new(Mutex::new(octx));
for mut pctx in all.into_iter() {
let iclone = ictx.clone();
let amoclone = amoctx.clone();
pctx.context = amoctx.lock().unwrap().take();
pctx.call_all_props(cr, move |pactx| {
let mut ic = iclone.lock().unwrap();
let mut amoctx = amoclone.lock().unwrap();
if amoctx.is_none() {
*amoctx = pactx.propctx.as_mut().unwrap().context.take();
}
let answers = std::mem::replace(&mut pactx.answers, HashMap::new());
ic.ifaces.insert(pactx.propctx.as_ref().unwrap().interface.to_string(), answers);
ic.remaining -= 1;
if ic.remaining == 0 {
let donefn = ic.donefn.take().unwrap().0;
(donefn)(&mut *ic, &mut amoctx)
}
}).map(|mut pctx| {
let mut amoctx = amoctx.lock().unwrap();
if amoctx.is_none() {
*amoctx = pctx.context.take();
}
});
}
let mut amoctx = amoctx.lock().unwrap();
amoctx.take()
}
fn get_managed_objects(mut ctx: Context, cr: &mut Crossroads, _: ()) -> Option<Context> {
let parent = ctx.path();
let children: Vec<dbus::Path<'static>> =
cr.get_children(ctx.path(), false).into_iter().map(|child_path| {
let mut x = String::from(&**parent);
if !x.ends_with('/') {
x.push_str("/");
}
x.push_str(child_path);
dbus::Path::from(x).into_static()
}).collect();
if children.len() == 0 {
ctx.do_reply(|msg| {
let x: PathPropMap = Default::default();
msg.append_all((x,));
});
return Some(ctx);
}
#[derive(Debug)]
struct Temp {
remaining: usize,
temp_map: PathPropMap,
octx: Option<Context>,
}
let r = Arc::new(Mutex::new(Temp {
remaining: children.len(),
temp_map: HashMap::new(),
octx: Some(ctx),
}));
for subpath in children {
let rclone = r.clone();
let subpath_clone = subpath.clone();
let octx = r.lock().unwrap().octx.take();
get_all_for_path(&subpath, cr, octx, move |ictx, octx| {
let mut rr = rclone.lock().unwrap();
if rr.octx.is_none() { rr.octx = octx.take(); }
let ifaces = std::mem::replace(&mut ictx.ifaces, HashMap::new());
rr.temp_map.insert(subpath_clone, ifaces);
rr.remaining -= 1;
if rr.remaining > 0 { return; }
let mut octx = rr.octx.take().unwrap();
octx.do_reply(|msg| {
msg.append_all((&rr.temp_map,));
});
rr.octx = Some(octx);
}).map(|octx| {
let mut rr = r.lock().unwrap();
if rr.octx.is_none() { rr.octx = Some(octx); }
});
}
let mut rr = r.lock().unwrap();
rr.octx.take()
}
pub fn object_manager(cr: &mut Crossroads) -> IfaceToken<()> {
cr.register("org.freedesktop.DBus.ObjectManager", |b| {
b.method_with_cr_custom::<(), (PathPropMap,), _, _>
("GetManagedObjects", (), ("objpath_interfaces_and_properties",), get_managed_objects);
b.signal::<(dbus::Path<'static>, IfacePropMap), _>("InterfacesAdded",
("object_path", "interfaces_and_properties"));
b.signal::<(dbus::Path<'static>, Vec<String>), _>("InterfacesRemoved",
("object_path", "interfaces"));
})
}
fn object_manager_parents<F: FnMut(dbus::Path<'static>, &mut Crossroads)>(name: &dbus::Path<'static>, cr: &mut Crossroads, mut f: F) {
for idx in 0..name.len()-1 {
if name.as_bytes()[idx] != b'/' { continue; }
let parent = dbus::Path::from(&name[0..(if idx == 0 { idx + 1 } else {idx})]).into_static();
if !cr.has_interface(&parent, cr.object_manager::<()>()) { continue; }
f(parent, cr)
}
}
pub fn object_manager_path_added(sender: Arc<dyn Sender + Send + Sync>, name: &dbus::Path<'static>, cr: &mut Crossroads) {
object_manager_parents(name, cr, |parent, cr| {
let n = name.clone();
let s = sender.clone();
get_all_for_path(&name, cr, None, move |ictx, _| {
let x = dbus::blocking::stdintf::org_freedesktop_dbus::ObjectManagerInterfacesAdded {
object: n,
interfaces: std::mem::replace(&mut ictx.ifaces, HashMap::new()),
};
let _ = s.send(dbus::message::SignalArgs::to_emit_message(&x, &parent));
});
});
}
pub fn object_manager_path_removed(sender: Arc<dyn Sender + Send + Sync>, name: &dbus::Path<'static>, cr: &mut Crossroads) {
object_manager_parents(name, cr, |parent, cr| {
let (reg, ifaces) = cr.registry_and_ifaces(&name);
let x = dbus::blocking::stdintf::org_freedesktop_dbus::ObjectManagerInterfacesRemoved {
object: name.clone(),
interfaces: ifaces.into_iter()
.filter_map(|iface| reg.get_intf_name(*iface))
.map(|iface| String::from(&**iface))
.collect(),
};
let _ = sender.send(dbus::message::SignalArgs::to_emit_message(&x, &parent));
});
}
pub fn object_manager_interface_added(sender: Arc<dyn Sender + Send + Sync>, name: &dbus::Path<'static>, itoken: usize, cr: &mut Crossroads) {
object_manager_parents(name, cr, |parent, cr| {
let n = name.clone();
let s = sender.clone();
for_each_interface_with_properties(&name, vec![itoken], cr, None, move |ictx, _| {
let x = dbus::blocking::stdintf::org_freedesktop_dbus::ObjectManagerInterfacesAdded {
object: n,
interfaces: std::mem::replace(&mut ictx.ifaces, HashMap::new()),
};
let _ = s.send(dbus::message::SignalArgs::to_emit_message(&x, &parent));
});
});
}
pub fn object_manager_interface_removed(sender: Arc<dyn Sender + Send + Sync>, name: &dbus::Path<'static>, itoken: usize, cr: &mut Crossroads) {
object_manager_parents(name, cr, |parent, cr| {
let (reg, _ifaces) = cr.registry_and_ifaces(&name);
if let Some(iface) = reg.get_intf_name(itoken) {
let x = dbus::blocking::stdintf::org_freedesktop_dbus::ObjectManagerInterfacesRemoved {
object: name.clone(),
interfaces: vec![iface.to_string()],
};
let _ = sender.send(dbus::message::SignalArgs::to_emit_message(&x, &parent));
}
});
}