about summary refs log tree commit diff
path: root/src/facility
diff options
context:
space:
mode:
Diffstat (limited to 'src/facility')
-rw-r--r--src/facility/i3.rs94
1 files changed, 94 insertions, 0 deletions
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)
+  }
+
+}
+