11// Mostly taken from https://github.com/diwic/dbus-rs/blob/366a6dca3d20745f5dcfa006b1b1311c376d420e/dbus/examples/monitor.rs
22
33// This programs implements the equivalent of running the "dbus-monitor" tool
4- // modified to only search for messages in the org.freedesktop.Notifications interface
4+ // modified to only search for messages in the interface specificied in config.json,
5+ // and then run arbitary inputmodule-rs commands to react to them
6+
57use dbus:: blocking:: Connection ;
68use dbus:: channel:: MatchingReceiver ;
79use dbus:: message:: MatchRule ;
@@ -17,6 +19,40 @@ use inputmodule_control::inputmodule::serial_commands;
1719
1820use log:: debug;
1921
22+ use serde:: { Deserialize , Serialize } ;
23+ use serde_json;
24+ use std:: fs:: File ;
25+ use std:: io:: Read ;
26+
27+ use lazy_static:: lazy_static;
28+
29+ #[ derive( Debug , Deserialize , Serialize ) ]
30+ pub struct Config {
31+ dbus_interface : String ,
32+ dbus_member : String ,
33+ scan_args_for : String ,
34+ run_inputmodule_commands : Vec < String > ,
35+ }
36+
37+ fn read_config ( file_path : & str ) -> Result < Config , Box < dyn std:: error:: Error > > {
38+ let mut file = File :: open ( file_path) ?;
39+ let mut config_data = String :: new ( ) ;
40+ file. read_to_string ( & mut config_data) ?;
41+
42+ let config: Config = serde_json:: from_str ( & config_data) ?;
43+
44+ Ok ( config)
45+ }
46+
47+ lazy_static ! {
48+ pub static ref CONFIG : Config = {
49+ // Read and deserialize the JSON configuration
50+ let config_file = "dbus-monitor/src/config.json" ;
51+ let config = read_config( config_file) . expect( "Failed to read config" ) ;
52+ config
53+ } ;
54+ }
55+
2056fn handle_message ( msg : & Message ) {
2157 debug ! ( "Got message from DBus: {:?}" , msg) ;
2258
@@ -26,15 +62,11 @@ fn handle_message(msg: &Message) {
2662 let string_value: String = string_ref. to_string ( ) ;
2763 debug ! ( "String value: {}" , string_value) ;
2864
29- if string_value. contains ( "calendar.google.com" ) {
30- run_inputmodule_command ( vec ! [
31- "led-matrix" ,
32- "--pattern" ,
33- "all-on" ,
34- "--blink-n-times" ,
35- "3" ,
36- ] ) ;
37- run_inputmodule_command ( vec ! [ "led-matrix" , "--brightness" , "0" ] ) ;
65+ if string_value. contains ( & CONFIG . scan_args_for ) {
66+ for command in & CONFIG . run_inputmodule_commands {
67+ let command_vec: Vec < & str > = command. split_whitespace ( ) . collect ( ) ;
68+ run_inputmodule_command ( command_vec) ;
69+ }
3870 }
3971 }
4072 iter. next ( ) ;
@@ -56,12 +88,11 @@ pub fn run_dbus_monitor() {
5688 let conn = Connection :: new_session ( ) . expect ( "D-Bus connection failed" ) ;
5789 debug ! ( "Connection to DBus session monitor opened" ) ;
5890
59- // Second create a rule to match messages we want to receive; in this example we add no
60- // further requirements, so all messages will match
91+ // Second create a rule to match messages we want to receive
6192 let rule = MatchRule :: new ( )
6293 . with_type ( MessageType :: MethodCall )
63- . with_interface ( "org.freedesktop.Notifications" )
64- . with_member ( "Notify" ) ;
94+ . with_interface ( & CONFIG . dbus_interface )
95+ . with_member ( & CONFIG . dbus_member ) ;
6596
6697 // Try matching using new scheme
6798 let proxy = conn. with_proxy (
0 commit comments