File: monitor.rs

package info (click to toggle)
rust-dbus 0.9.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,632 bytes parent folder | download | duplicates (10)
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
use std::time::Duration;

use dbus::blocking::Connection;
use dbus::channel::MatchingReceiver;
use dbus::Message;
use dbus::message::MatchRule;

// This programs implements the equivalent of running the "dbus-monitor" tool
fn main() {
    // First open up a connection to the session bus.
    let conn = Connection::new_session().expect("D-Bus connection failed");

    // Second create a rule to match messages we want to receive; in this example we add no
    // further requirements, so all messages will match
    let mut rule = MatchRule::new();

    // Try matching using new scheme
    let proxy = conn.with_proxy("org.freedesktop.DBus", "/org/freedesktop/DBus", Duration::from_millis(5000));
    let result: Result<(), dbus::Error> = proxy.method_call("org.freedesktop.DBus.Monitoring", "BecomeMonitor", (vec!(rule.match_str()), 0u32));

    if result.is_ok() {
        // Start matching using new scheme
        conn.start_receive(rule, Box::new(|msg, _| {
            handle_message(&msg);
            true
        }));
    } else {
        // Start matching using old scheme
        rule.eavesdrop = true; // this lets us eavesdrop on *all* session messages, not just ours
        conn.add_match(rule, |_: (), _, msg| {
            handle_message(&msg);
            true
        }).expect("add_match failed");
    }

    // Loop and print out all messages received (using handle_message()) as they come.
    // Some can be quite large, e.g. if they contain embedded images..
    loop { conn.process(Duration::from_millis(1000)).unwrap(); };
}

fn handle_message(msg: &Message) {
    println!("Got message: {:?}", msg);
}