diff --git a/Cargo.lock b/Cargo.lock index 6a3a0e5..5dbaf95 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,16 +95,15 @@ dependencies = [ [[package]] name = "anstream" -version = "0.3.2" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", - "is-terminal", "utf8parse", ] @@ -134,9 +133,9 @@ dependencies = [ [[package]] name = "anstyle-wincon" -version = "1.0.1" +version = "3.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" dependencies = [ "anstyle", "windows-sys", @@ -390,33 +389,31 @@ dependencies = [ [[package]] name = "clap" -version = "4.3.3" +version = "4.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca8f255e4b8027970e78db75e78831229c9815fdbfa67eb1a1b777a62e24b4a0" +checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956" dependencies = [ "clap_builder", "clap_derive", - "once_cell 1.18.0", ] [[package]] name = "clap_builder" -version = "4.3.3" +version = "4.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acd4f3c17c83b0ba34ffbc4f8bbd74f079413f747f84a6f89292f138057e36ab" +checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45" dependencies = [ "anstream", "anstyle", - "bitflags 1.3.2", "clap_lex", "strsim", ] [[package]] name = "clap_derive" -version = "4.3.2" +version = "4.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8cd2b2a819ad6eec39e8f1d6b53001af1e5469f8c177579cdaeb313115b825f" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" dependencies = [ "heck", "proc-macro2", @@ -839,8 +836,11 @@ dependencies = [ "dbus", "env_logger", "inputmodule-control", + "lazy_static", "libdbus-sys", "log", + "serde", + "serde_json", ] [[package]] @@ -1060,6 +1060,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + [[package]] name = "jni" version = "0.19.0" @@ -1989,6 +1995,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ryu" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + [[package]] name = "same-file" version = "1.0.6" @@ -2036,6 +2048,31 @@ name = "serde" version = "1.0.164" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.164" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.18", +] + +[[package]] +name = "serde_json" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46266871c240a00b8f503b877622fe33430b3c7d963bdc0f2adc511e54a1eae3" +dependencies = [ + "itoa", + "ryu", + "serde", +] [[package]] name = "serialport" diff --git a/dbus-monitor/Cargo.toml b/dbus-monitor/Cargo.toml index 9f09a53..ae43a17 100644 --- a/dbus-monitor/Cargo.toml +++ b/dbus-monitor/Cargo.toml @@ -5,9 +5,12 @@ version = "0.0.1" [dependencies] dbus = { version = "0.9.7", features = ["vendored"] } -clap = { version = "4.0", features = ["derive"] } +clap = { version = "4.4.6", features = ["derive"] } log = "0.4" env_logger = "0.10.0" +serde = { version="1.0", features = ["derive"] } +serde_json = "1.0" +lazy_static = "1.4" [dependencies.libdbus-sys] default-features = false diff --git a/dbus-monitor/src/config.json b/dbus-monitor/src/config.json new file mode 100644 index 0000000..f838111 --- /dev/null +++ b/dbus-monitor/src/config.json @@ -0,0 +1,10 @@ +{ + "dbus_interface": "org.freedesktop.Notifications", + "dbus_member": "Notify", + "scan_args_for": "calendar.google.com", + "run_inputmodule_commands": [ + "led-matrix --pattern all-on --blink-n-times 3", + "led-matrix --brightness 0" + ] +} + diff --git a/dbus-monitor/src/dbus_monitor.rs b/dbus-monitor/src/dbus_monitor.rs index 271bcac..05212a4 100644 --- a/dbus-monitor/src/dbus_monitor.rs +++ b/dbus-monitor/src/dbus_monitor.rs @@ -1,7 +1,9 @@ // Mostly taken from https://github.com/diwic/dbus-rs/blob/366a6dca3d20745f5dcfa006b1b1311c376d420e/dbus/examples/monitor.rs // This programs implements the equivalent of running the "dbus-monitor" tool -// modified to only search for messages in the org.freedesktop.Notifications interface +// modified to only search for messages in the interface specificied in config.json, +// and then run arbitary inputmodule-rs commands to react to them + use dbus::blocking::Connection; use dbus::channel::MatchingReceiver; use dbus::message::MatchRule; @@ -17,6 +19,40 @@ use inputmodule_control::inputmodule::serial_commands; use log::debug; +use serde::{Deserialize, Serialize}; +use serde_json; +use std::fs::File; +use std::io::Read; + +use lazy_static::lazy_static; + +#[derive(Debug, Deserialize, Serialize)] +pub struct Config { + dbus_interface: String, + dbus_member: String, + scan_args_for: String, + run_inputmodule_commands: Vec, +} + +fn read_config(file_path: &str) -> Result> { + let mut file = File::open(file_path)?; + let mut config_data = String::new(); + file.read_to_string(&mut config_data)?; + + let config: Config = serde_json::from_str(&config_data)?; + + Ok(config) +} + +lazy_static! { + pub static ref CONFIG: Config = { + // Read and deserialize the JSON configuration + let config_file = "dbus-monitor/src/config.json"; + let config = read_config(config_file).expect("Failed to read config"); + config + }; +} + fn handle_message(msg: &Message) { debug!("Got message from DBus: {:?}", msg); @@ -26,15 +62,11 @@ fn handle_message(msg: &Message) { let string_value: String = string_ref.to_string(); debug!("String value: {}", string_value); - if string_value.contains("calendar.google.com") { - run_inputmodule_command(vec![ - "led-matrix", - "--pattern", - "all-on", - "--blink-n-times", - "3", - ]); - run_inputmodule_command(vec!["led-matrix", "--brightness", "0"]); + if string_value.contains(&CONFIG.scan_args_for) { + for command in &CONFIG.run_inputmodule_commands { + let command_vec: Vec<&str> = command.split_whitespace().collect(); + run_inputmodule_command(command_vec); + } } } iter.next(); @@ -56,12 +88,11 @@ pub fn run_dbus_monitor() { let conn = Connection::new_session().expect("D-Bus connection failed"); debug!("Connection to DBus session monitor opened"); - // Second create a rule to match messages we want to receive; in this example we add no - // further requirements, so all messages will match + // Second create a rule to match messages we want to receive let rule = MatchRule::new() .with_type(MessageType::MethodCall) - .with_interface("org.freedesktop.Notifications") - .with_member("Notify"); + .with_interface(&CONFIG.dbus_interface) + .with_member(&CONFIG.dbus_member); // Try matching using new scheme let proxy = conn.with_proxy(