diff --git a/Cargo.lock b/Cargo.lock index 3b75e00a..cf375dda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -951,6 +951,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.177" @@ -1131,6 +1137,7 @@ dependencies = [ "greetd_ipc", "gtk", "input-event-codes", + "lazy_static", "libhandy", "libphosh", "log", diff --git a/Cargo.toml b/Cargo.toml index d618c0cf..61ebbb7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ nix = { version = "0.30", features = ["signal"] } async-global-executor = "3.0.0" futures-util = "0.3.30" log = "0.4.22" +lazy_static = "^1.4" [dependencies.glib] version = "0.18" diff --git a/src/sessions.rs b/src/sessions.rs index d12198c9..6bbb4f84 100644 --- a/src/sessions.rs +++ b/src/sessions.rs @@ -3,25 +3,58 @@ use glib::warn; use glob::glob; use gtk::gio::DesktopAppInfo; use gtk::prelude::*; -use std::collections::HashMap; +use lazy_static::lazy_static; +use std::{ + collections::HashMap, + path::PathBuf, + path::Path, + env, +}; static G_LOG_DOMAIN: &str = "phrog-sessions"; +lazy_static! { + // Snippet copied from https://github.com/apognu/tuigreet + + static ref XDG_DATA_DIRS: Vec = { + let envvar = env::var("XDG_DATA_DIRS"); + + let value = match envvar { + Ok(var) if !var.is_empty() => var, + _ => "/usr/local/share:/usr/share".to_string(), + }; + + env::split_paths(&value).filter(|p| p.is_absolute()).collect() + }; +} + pub fn sessions() -> Vec { let mut sessions = HashMap::new(); - session_list( - "/usr/share/wayland-sessions/*.desktop", - "wayland", - &mut sessions, - ); - session_list("/usr/share/xsessions/*.desktop", "x11", &mut sessions); + + for dir in XDG_DATA_DIRS.iter() { + let wl_dir = dir.join("wayland-sessions/*.desktop"); + let x11_dir = dir.join("xsessions/*.desktop"); + + session_list( + &wl_dir, + "wayland", + &mut sessions, + ); + + session_list( + &x11_dir, + "x11", + &mut sessions, + ); + }; + sessions.values().cloned().collect() } -fn session_list(path: &str, session_type: &str, sessions: &mut HashMap) { - for f in match glob(path) { +fn session_list(path: &Path, session_type: &str, sessions: &mut HashMap) { + for f in match glob(path.to_str().unwrap()) { Err(e) => { - warn!("couldn't check sessions in {}: {}", path, e); + warn!("couldn't check sessions in {}: {}", path.display(), e); return; } Ok(iter) => iter,