1 use std::time::Duration;
2 
3 use dbus::blocking::Connection;
4 use dbus::channel::MatchingReceiver;
5 use dbus::Message;
6 use dbus::message::MatchRule;
7 
8 // This programs implements the equivalent of running the "dbus-monitor" tool
main()9 fn main() {
10     // First open up a connection to the session bus.
11     let conn = Connection::new_session().expect("D-Bus connection failed");
12 
13     // Second create a rule to match messages we want to receive; in this example we add no
14     // further requirements, so all messages will match
15     let mut rule = MatchRule::new();
16 
17     // Try matching using new scheme
18     let proxy = conn.with_proxy("org.freedesktop.DBus", "/org/freedesktop/DBus", Duration::from_millis(5000));
19     let result: Result<(), dbus::Error> = proxy.method_call("org.freedesktop.DBus.Monitoring", "BecomeMonitor", (vec!(rule.match_str()), 0u32));
20 
21     if result.is_ok() {
22         // Start matching using new scheme
23         conn.start_receive(rule, Box::new(|msg, _| {
24             handle_message(&msg);
25             true
26         }));
27     } else {
28         // Start matching using old scheme
29         rule.eavesdrop = true; // this lets us eavesdrop on *all* session messages, not just ours
30         conn.add_match(rule, |_: (), _, msg| {
31             handle_message(&msg);
32             true
33         }).expect("add_match failed");
34     }
35 
36     // Loop and print out all messages received (using handle_message()) as they come.
37     // Some can be quite large, e.g. if they contain embedded images..
38     loop { conn.process(Duration::from_millis(1000)).unwrap(); };
39 }
40 
handle_message(msg: &Message)41 fn handle_message(msg: &Message) {
42     println!("Got message: {:?}", msg);
43 }
44