Commit | Line | Data |
---|---|---|
12bd87b0 | 1 | use dbus::arg; |
baedbc7e JC |
2 | use dbus::blocking::stdintf::org_freedesktop_dbus::PropertiesPropertiesChanged; |
3 | use dbus::blocking::Connection; | |
4 | use dbus::message::Message; | |
1e52c28e | 5 | use std::collections::HashMap; |
42fef3a7 | 6 | use std::process::Command; |
1e52c28e | 7 | use std::sync::{Arc, Mutex}; |
bbc46eb1 | 8 | use std::thread; |
baedbc7e JC |
9 | use std::time::Duration; |
10 | ||
12bd87b0 JC |
11 | // Custom signal type impl |
12 | // Mostly copied from library examples | |
13 | #[derive(Debug)] | |
14 | pub struct ComJacobCasperMailUnreadCount { | |
15 | pub count: u32, | |
16 | } | |
17 | ||
18 | impl arg::AppendAll for ComJacobCasperMailUnreadCount { | |
19 | fn append(&self, iter: &mut arg::IterAppend) { | |
20 | arg::RefArg::append(&self.count, iter); | |
21 | } | |
22 | } | |
23 | ||
24 | impl arg::ReadAll for ComJacobCasperMailUnreadCount { | |
25 | fn read(iter: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> { | |
26 | Ok(ComJacobCasperMailUnreadCount { | |
27 | count: iter.read()?, | |
28 | }) | |
29 | } | |
30 | } | |
31 | ||
32 | impl dbus::message::SignalArgs for ComJacobCasperMailUnreadCount { | |
33 | const NAME: &'static str = "UnreadCount"; | |
34 | const INTERFACE: &'static str = "com.jacobcasper.Mail"; | |
35 | } | |
36 | ||
bbc46eb1 JC |
37 | fn get_local_time_string() -> std::string::String { |
38 | use chrono::prelude::*; | |
39 | let local_time = chrono::prelude::Local::now(); | |
40 | return format!( | |
41 | "{}-{}-{:02} {}:{}", | |
42 | local_time.year(), | |
43 | local_time.month(), | |
44 | local_time.day(), | |
45 | local_time.hour(), | |
46 | local_time.minute(), | |
47 | ); | |
48 | } | |
49 | ||
ddaab88f | 50 | fn update_map(arc_map: Arc<Mutex<HashMap<String, String>>>, key: &str, val: &str) { |
1e52c28e JC |
51 | let clone_arc = arc_map.clone(); |
52 | let mut map = clone_arc.lock().unwrap(); | |
ddaab88f | 53 | map.insert(String::from(key), String::from(val)); |
42fef3a7 JC |
54 | let _ = Command::new("xsetroot") |
55 | .arg("-name") | |
56 | .arg(format!( | |
bbc46eb1 | 57 | "[🔊 {title} - {artist}] | ✉ {unread_count} | {date_time}", |
42fef3a7 JC |
58 | title = map.get("title").unwrap_or(&String::from("")), |
59 | artist = map.get("artist").unwrap_or(&String::from("")), | |
60 | unread_count = map.get("unread_count").unwrap_or(&String::from("?")), | |
bbc46eb1 | 61 | date_time = map.get("date_time").unwrap_or(&get_local_time_string()), |
42fef3a7 JC |
62 | )) |
63 | .spawn(); | |
1e52c28e JC |
64 | } |
65 | ||
baedbc7e | 66 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
1e52c28e | 67 | let arc_locked_xset_map = Arc::new(Mutex::new(HashMap::new())); |
baedbc7e JC |
68 | let conn = Connection::new_session()?; |
69 | ||
12bd87b0 JC |
70 | let mail_proxy = conn.with_proxy( |
71 | "com.jacobcasper.Mail", | |
72 | "/com/jacobcasper/Mail/Unread", | |
73 | Duration::from_millis(5000), | |
74 | ); | |
75 | ||
76 | let mail_match_map = arc_locked_xset_map.clone(); | |
77 | let _ = mail_proxy.match_signal( | |
78 | move |m: ComJacobCasperMailUnreadCount, _: &Connection, _: &Message| { | |
79 | update_map( | |
80 | mail_match_map.clone(), | |
81 | "unread_count", | |
82 | m.count.to_string().as_str(), | |
83 | ); | |
84 | true | |
85 | }, | |
86 | ); | |
87 | ||
88 | let spotify_proxy = conn.with_proxy( | |
baedbc7e JC |
89 | "org.mpris.MediaPlayer2.spotify", |
90 | "/org/mpris/MediaPlayer2", | |
91 | Duration::from_millis(5000), | |
92 | ); | |
93 | ||
16683530 | 94 | let spotify_match_map = arc_locked_xset_map.clone(); |
12bd87b0 | 95 | let _ = spotify_proxy.match_signal( |
1e52c28e | 96 | move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| { |
79b46cd5 JC |
97 | pc.changed_properties["Metadata"] |
98 | .0 | |
99 | .as_iter() | |
100 | .and_then(|mut iter| { | |
101 | // Iterating over a RefArg goes key, value, key, value... Insane honestly. | |
102 | while let Some(key) = iter.next() { | |
103 | let key_str = key.as_str()?; | |
104 | let value = iter.next(); | |
105 | ||
106 | match key_str { | |
107 | "xesam:artist" => { | |
108 | // Variant holding a variant that should just be a Vec<&str> I | |
109 | // believe. This is _the recommended_ way to do this by the author. | |
110 | let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?; | |
ddaab88f | 111 | let artist = inner_value.as_str()?; |
16683530 | 112 | update_map(spotify_match_map.clone(), "artist", artist); |
79b46cd5 | 113 | } |
b710f9dc | 114 | "xesam:title" => { |
1e52c28e | 115 | let title = value?.as_iter()?.next()?.as_str()?; |
16683530 | 116 | update_map(spotify_match_map.clone(), "title", title); |
b710f9dc | 117 | } |
79b46cd5 JC |
118 | _ => (), |
119 | } | |
120 | } | |
121 | Some(()) | |
122 | }); | |
baedbc7e JC |
123 | true |
124 | }, | |
125 | ); | |
126 | ||
bbc46eb1 JC |
127 | let date_time_map = arc_locked_xset_map.clone(); |
128 | let _ = thread::spawn(move || -> Result<(), std::time::SystemTimeError> { | |
129 | let mut last_run_at = std::time::SystemTime::now(); | |
130 | loop { | |
131 | let sys_time = std::time::SystemTime::now(); | |
132 | let elapsed_time = sys_time.duration_since(last_run_at)?; | |
133 | if elapsed_time.as_secs() > 60 { | |
134 | last_run_at = sys_time; | |
135 | update_map( | |
136 | date_time_map.clone(), | |
137 | "date_time", | |
138 | get_local_time_string().as_str(), | |
139 | ); | |
140 | } | |
141 | thread::sleep(Duration::from_millis(5000)); | |
142 | } | |
143 | }); | |
144 | ||
baedbc7e JC |
145 | loop { |
146 | conn.process(Duration::from_millis(1000))?; | |
147 | } | |
148 | } |