Prevent small race when both track and artist change
[xsetrootd.git] / src / main.rs
index a5b3c32..d2d1df2 100644 (file)
@@ -5,6 +5,7 @@ use dbus::message::Message;
 use std::collections::HashMap;
 use std::process::Command;
 use std::sync::{Arc, Mutex};
+use std::thread;
 use std::time::Duration;
 
 // Custom signal type impl
@@ -33,17 +34,37 @@ impl dbus::message::SignalArgs for ComJacobCasperMailUnreadCount {
     const INTERFACE: &'static str = "com.jacobcasper.Mail";
 }
 
+fn get_local_time_string() -> std::string::String {
+    use chrono::prelude::*;
+    let local_time = chrono::prelude::Local::now();
+    return format!(
+        "{}-{}-{:02} {}:{:02}",
+        local_time.year(),
+        local_time.month(),
+        local_time.day(),
+        local_time.hour(),
+        local_time.minute(),
+    );
+}
+
 fn update_map(arc_map: Arc<Mutex<HashMap<String, String>>>, key: &str, val: &str) {
     let clone_arc = arc_map.clone();
     let mut map = clone_arc.lock().unwrap();
     map.insert(String::from(key), String::from(val));
+}
+
+fn update_xsetroot(arc_map: Arc<Mutex<HashMap<String, String>>>) {
+    let clone_arc = arc_map.clone();
+    let map = clone_arc.lock().unwrap();
     let _ = Command::new("xsetroot")
         .arg("-name")
         .arg(format!(
-            "[🔊 {title} - {artist}] | âœ‰ {unread_count}",
+            "[{playback_status} {title} - {artist}] | âœ‰ {unread_count} | {date_time}",
+            playback_status = map.get("playback_status").unwrap_or(&String::from("🔈")),
             title = map.get("title").unwrap_or(&String::from("")),
             artist = map.get("artist").unwrap_or(&String::from("")),
             unread_count = map.get("unread_count").unwrap_or(&String::from("?")),
+            date_time = map.get("date_time").unwrap_or(&get_local_time_string()),
         ))
         .spawn();
 }
@@ -66,6 +87,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
                 "unread_count",
                 m.count.to_string().as_str(),
             );
+            update_xsetroot(mail_match_map.clone());
             true
         },
     );
@@ -79,36 +101,74 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
     let spotify_match_map = arc_locked_xset_map.clone();
     let _ = spotify_proxy.match_signal(
         move |pc: PropertiesPropertiesChanged, _: &Connection, _: &Message| {
-            pc.changed_properties["Metadata"]
-                .0
-                .as_iter()
-                .and_then(|mut iter| {
-                    // Iterating over a RefArg goes key, value, key, value... Insane honestly.
-                    while let Some(key) = iter.next() {
-                        let key_str = key.as_str()?;
-                        let value = iter.next();
+            match pc.changed_properties.get("Metadata") {
+                Some(variant) => {
+                    variant.0.as_iter().and_then(|mut iter| {
+                        // Iterating over a RefArg goes key, value, key, value... Insane honestly.
+                        while let Some(key) = iter.next() {
+                            let key_str = key.as_str()?;
+                            let value = iter.next();
 
-                        match key_str {
-                            "xesam:artist" => {
-                                // Variant holding a variant that should just be a Vec<&str> I
-                                // believe. This is _the recommended_ way to do this by the author.
-                                let inner_value = value?.as_iter()?.next()?.as_iter()?.next()?;
-                                let artist = inner_value.as_str()?;
-                                update_map(spotify_match_map.clone(), "artist", artist);
+                            match key_str {
+                                "xesam:artist" => {
+                                    // Variant holding a variant that should just be a Vec<&str> I
+                                    // believe. This is _the recommended_ way to do this by the author.
+                                    let inner_value =
+                                        value?.as_iter()?.next()?.as_iter()?.next()?;
+                                    let artist = inner_value.as_str()?;
+                                    update_map(spotify_match_map.clone(), "artist", artist);
+                                }
+                                "xesam:title" => {
+                                    let title = value?.as_iter()?.next()?.as_str()?;
+                                    update_map(spotify_match_map.clone(), "title", title);
+                                }
+                                _ => (),
                             }
-                            "xesam:title" => {
-                                let title = value?.as_iter()?.next()?.as_str()?;
-                                update_map(spotify_match_map.clone(), "title", title);
-                            }
-                            _ => (),
                         }
-                    }
-                    Some(())
-                });
+                        update_xsetroot(spotify_match_map.clone());
+                        Some(())
+                    });
+                }
+                None => (),
+            }
+            match &pc.changed_properties.get("PlaybackStatus") {
+                Some(variant) => {
+                    let playback_status = &variant.0;
+                    update_map(
+                        spotify_match_map.clone(),
+                        "playback_status",
+                        match &*playback_status.as_str().unwrap_or("") {
+                            "Playing" => "🔊",
+                            _ => "🔈",
+                        },
+                    );
+                    update_xsetroot(spotify_match_map.clone());
+                }
+                None => (),
+            }
             true
         },
     );
 
+    let date_time_map = arc_locked_xset_map.clone();
+    let _ = thread::spawn(move || -> Result<(), std::time::SystemTimeError> {
+        let mut last_run_at = std::time::SystemTime::now();
+        loop {
+            let sys_time = std::time::SystemTime::now();
+            let elapsed_time = sys_time.duration_since(last_run_at)?;
+            if elapsed_time.as_secs() > 60 {
+                last_run_at = sys_time;
+                update_map(
+                    date_time_map.clone(),
+                    "date_time",
+                    get_local_time_string().as_str(),
+                );
+                update_xsetroot(date_time_map.clone());
+            }
+            thread::sleep(Duration::from_millis(5000));
+        }
+    });
+
     loop {
         conn.process(Duration::from_millis(1000))?;
     }