1
1
// Mostly taken from https://github.com/diwic/dbus-rs/blob/366a6dca3d20745f5dcfa006b1b1311c376d420e/dbus/examples/monitor.rs
2
2
3
3
// 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
+
5
7
use dbus:: blocking:: Connection ;
6
8
use dbus:: channel:: MatchingReceiver ;
7
9
use dbus:: message:: MatchRule ;
@@ -17,6 +19,40 @@ use inputmodule_control::inputmodule::serial_commands;
17
19
18
20
use log:: debug;
19
21
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
+
20
56
fn handle_message ( msg : & Message ) {
21
57
debug ! ( "Got message from DBus: {:?}" , msg) ;
22
58
@@ -26,15 +62,11 @@ fn handle_message(msg: &Message) {
26
62
let string_value: String = string_ref. to_string ( ) ;
27
63
debug ! ( "String value: {}" , string_value) ;
28
64
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
+ }
38
70
}
39
71
}
40
72
iter. next ( ) ;
@@ -56,12 +88,11 @@ pub fn run_dbus_monitor() {
56
88
let conn = Connection :: new_session ( ) . expect ( "D-Bus connection failed" ) ;
57
89
debug ! ( "Connection to DBus session monitor opened" ) ;
58
90
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
61
92
let rule = MatchRule :: new ( )
62
93
. 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 ) ;
65
96
66
97
// Try matching using new scheme
67
98
let proxy = conn. with_proxy (
0 commit comments