Skip to content

Commit

Permalink
Bring up to date with main
Browse files Browse the repository at this point in the history
  • Loading branch information
zachfeldman committed Oct 9, 2023
1 parent 9a6d6f7 commit e16a7ce
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ usbd-serial = "0.1.1"
usbd-hid = "0.6.1"
fugit = "0.3.7"
# LED Matrix
is31fl3741 = { git = "https://github.com/FrameworkComputer/is31fl3741-rs", branch = "sw-enablement" }
is31fl3741 = { git = "https://github.com/FrameworkComputer/is31fl3741-rs", branch = "is31fl3743a" }
# B1 Display
st7306 = { git = "https://github.com/FrameworkComputer/st7306-rs", branch = "update-deps" }
embedded-graphics = "0.8"
Expand Down
7 changes: 7 additions & 0 deletions inputmodule-dbus-monitor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
edition = "2021"
name = "inputmodule-dbus-monitor"
version = "0.0.1"

[dependencies]
dbus = { version = "0.9.7", features = ["vendored"] }
82 changes: 82 additions & 0 deletions inputmodule-dbus-monitor/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Source: https://github.com/diwic/dbus-rs/blob/master/dbus/examples/monitor.rs

use std::time::Duration;

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

// This programs implements the equivalent of running the "dbus-monitor" tool
fn main() {
// Very simple argument parsing.
let use_system_bus = std::env::args().into_iter().any(|a| a == "--system");

// First open up a connection to the desired bus.
let conn = (if use_system_bus { Connection::new_system() } else { 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 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));

match result {
// BecomeMonitor was successful, start listening for messages
Ok(_) => {
conn.start_receive(
rule,
Box::new(|msg, _| {
handle_message(&msg);
true
}),
);
}
// BecomeMonitor failed, fallback to using the old scheme
Err(e) => {
eprintln!("Failed to BecomeMonitor: '{}', falling back to eavesdrop", e);

// First, we'll try "eavesdrop", which as the name implies lets us receive
// *all* messages, not just ours.
let rule_with_eavesdrop = {
let mut rule = rule.clone();
rule.eavesdrop = true;
rule
};

let result = conn.add_match(rule_with_eavesdrop, |_: (), _, msg| {
handle_message(&msg);
true
});

match result {
Ok(_) => {
// success, we're now listening
}
// This can sometimes fail, for example when listening to the system bus as a non-root user.
// So, just like `dbus-monitor`, we attempt to fallback without `eavesdrop=true`:
Err(e) => {
eprintln!("Failed to eavesdrop: '{}', trying without it", e);
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);
}

0 comments on commit e16a7ce

Please sign in to comment.