about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/error.rs6
-rw-r--r--src/facility.rs4
-rw-r--r--src/facility/i3.rs94
-rw-r--r--src/main.rs1
-rw-r--r--src/xdg.rs27
5 files changed, 132 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
index 390d6da..a0aa68e 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -22,6 +22,12 @@ impl Display for Error {
   }
 }
 
+impl From<std::env::VarError> for Error {
+  fn from(error: std::env::VarError) -> Error {
+    Error(format!("Environment variable: {}", error))
+  }
+}
+
 impl From<std::io::Error> for Error {
   fn from(error: std::io::Error) -> Error {
     Error(format!("I/O: {}", error))
diff --git a/src/facility.rs b/src/facility.rs
index 0c34bf9..56afaf4 100644
--- a/src/facility.rs
+++ b/src/facility.rs
@@ -6,8 +6,10 @@ use std::collections::BTreeMap;
 use bitflags::bitflags;
 
 pub use envsys::EnvSys;
+pub use i3::I3;
 
 pub mod envsys;
+pub mod i3;
 
 
 pub trait Facility: Any {
@@ -69,6 +71,7 @@ bitflags! {
   #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
   pub struct FacilityType: u32 {
     const ENVSYS = 0x00000001;
+    const I3     = 0x00000002;
   }
 }
 
@@ -76,6 +79,7 @@ impl FacilityType {
   pub async fn new(&self) -> Result<Box<dyn Facility>> {
     match self {
       &FacilityType::ENVSYS => Ok(EnvSys::new().await?.as_box()),
+      &FacilityType::I3     => Ok(I3::new().await?.as_box()),
       _ => todo!()
     }
   }
diff --git a/src/facility/i3.rs b/src/facility/i3.rs
new file mode 100644
index 0000000..7ee29dc
--- /dev/null
+++ b/src/facility/i3.rs
@@ -0,0 +1,94 @@
+use crate::error::{ Error, Result };
+use crate::facility::{ CreatableFacility, Facility, FacilityType };
+use crate::xdg::xdg_runtime_directory;
+
+use std::any::Any;
+use std::path::{ Path, PathBuf };
+
+use async_fs::File;
+
+
+pub struct I3 {
+  socket: File,
+}
+
+
+impl Facility for I3 {
+  fn as_any_mut(&mut self) -> &mut dyn Any {
+    self
+  }
+
+  fn update(&mut self) -> Result<()> {
+    Ok(())
+  }
+}
+
+impl CreatableFacility for I3 {
+  const FACILITY_TYPE: FacilityType = FacilityType::I3;
+
+  async fn new() -> Result<Self> {
+    let socket = Self::find_socket().await?;
+
+    Ok(I3 { socket })
+  }
+}
+
+impl I3 {
+  async fn find_socket() -> Result<File> {
+    /*   We save most errors for the end, because they aren't really errors if
+     * a subsequent attempt succeeds.
+     */
+    let mut errors: Vec<Error> = Vec::new();
+
+    /*   According to the i3 documentation, the first-choice path is the one
+     * set in the config file. However, we opt not to parse the config - this
+     * would be the sole benefit we'd get from doing so. So, we skip it. If
+     * this affects you, sorry about that.
+     */
+
+    match std::env::var("I3SOCK") {
+      Ok(path) => {
+        /*   If it's set but invalid, we regard that as a reason to immediately
+         * declare failure.
+         */
+        let path = PathBuf::from(path);
+
+        match Self::try_socket_path(&path).await {
+          Ok(Some(socket)) => { return Ok(socket); }
+          Ok(None) => { }
+          Err(error) => { errors.push(error); }
+        }
+      }
+
+      /*   We don't consider the mere absence of an environment variable to be
+       * an error; the user is perfectly capable of noticing that on their own.
+       */
+      Err(std::env::VarError::NotPresent) => { }
+      Err(error) => { errors.push(error.into()); }
+    }
+
+    if let Some(mut path) = xdg_runtime_directory()? {
+      path.push("i3/");
+
+      match Self::try_socket_directory(&path).await {
+        Ok(Some(socket)) => { return Ok(socket); }
+        Ok(None) => { }
+        Err(error) => { errors.push(error); }
+      }
+    }
+    /* As before, an absent environment variable is not an error. */
+
+    for error in errors { error.log() }
+    Err(Error::custom("i3 window manager socket not found"))
+  }
+
+  async fn try_socket_directory(path: &Path) -> Result<Option<File>> {
+    Ok(None)
+  }
+
+  async fn try_socket_path(path: &Path) -> Result<Option<File>> {
+    Ok(None)
+  }
+
+}
+
diff --git a/src/main.rs b/src/main.rs
index 27c5899..b1b4b0e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -49,6 +49,7 @@ mod layout;
 mod module;
 mod property_list;
 mod x;
+mod xdg;
 
 
 static EXECUTOR: StaticExecutor = StaticExecutor::new();
diff --git a/src/xdg.rs b/src/xdg.rs
new file mode 100644
index 0000000..2c130e7
--- /dev/null
+++ b/src/xdg.rs
@@ -0,0 +1,27 @@
+use crate::Result;
+
+use std::path::PathBuf;
+
+
+/*   The spec does not suggest a default for this one, merely saying that
+ * applications should fall back to something else. So, we return an Option to
+ * allow our caller to worry about that.
+ */
+pub fn xdg_runtime_directory() -> Result<Option<PathBuf>> {
+  if let Ok(mut path) = std::env::var("XDG_RUNTIME_DIR") {
+    /*   As nice as it would be to use a portable interface for this, Rust
+     * doesn't have a stable one yet. In fact, we barely have visibility into
+     * it at all once we convert to a path.
+     *
+     *   Oh well. Fuck Microsoft anyway.
+     */
+    if !path.ends_with("/") {
+      path.push_str("/");
+    }
+
+    Ok(Some(PathBuf::from(path)))
+  } else {
+    Ok(None)
+  }
+}
+