Skip to content

Commit

Permalink
Add deentry for desktop entry sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite committed Apr 6, 2024
1 parent 465566f commit 74e1122
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ unicode-width = "0.1"

mio = { version = "0.8.8", features = [ "os-poll", "os-ext" ] }

deentry = "0.0.1"

# Interacting with the kernel interfaces
rand = "0.8.4"
nix = "0.23.1"
Expand Down
6 changes: 6 additions & 0 deletions extra/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,13 @@ scripts_path = "/etc/lemurs/wms"
# window manager.
xsetup_path = "/etc/lemurs/xsetup.sh"

# The directory to use for desktop entries X11 sessions.
xsessions_path = "/usr/share/xsessions"

[wayland]
# Path to the directory where the startup scripts for the Wayland sessions are
# found
scripts_path = "/etc/lemurs/wayland"

# The directory to use for desktop entries wayland sessions.
wayland_sessions_path = "/usr/share/wayland-sessions"
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,12 @@ toml_config_struct! { X11Config, PartialX11Config, RoughX11Config,

scripts_path => String,
xsetup_path => String,
xsessions_path => String,
}

toml_config_struct! { WaylandConfig, PartialWaylandConfig, RoughWaylandConfig,
scripts_path => String,
wayland_sessions_path => String,
}

#[derive(Debug, Clone, Deserialize)]
Expand Down
107 changes: 107 additions & 0 deletions src/post_login/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,111 @@ impl PostLoginEnvironment {
}
}

fn parse_desktop_entry(path: &Path, _: &Config) -> Result<(String, String), String> {
let content = match fs::read_to_string(path) {
Ok(content) => content,
Err(err) => {
return Err(format!("file cannot be read. Reason: {err}"));
}
};

let desktop_entry = match deentry::DesktopEntry::try_from(&content[..]) {
Ok(v) => v,
Err(err) => {
return Err(format!("file cannot be parsed. Reason: {err}"));
}
};

let Some(desktop_entry) = desktop_entry
.groups()
.iter()
.find(|g| g.name() == "Desktop Entry")
else {
return Err(format!("file does not contain 'Desktop Entry' group"));
};

let Some(exec) = desktop_entry.get("Exec") else {
return Err(format!("'Exec' key cannot be found"));
};

let exec = match exec.value().as_string() {
Ok(v) => v,
Err(err) => {
return Err(format!(
"'Exec' key does not contain a string. Reason: {err}"
));
}
};

let name = match desktop_entry.get("Name") {
Some(name) => match name.value().as_string() {
Ok(v) => v,
Err(err) => {
warn!(
"Cannot use 'Name' in '{}' because it does not contain a string. Reason: {err}",
path.display()
);

exec
}
},
None => exec,
};

Ok((name.to_string(), exec.to_string()))
}

pub fn get_envs(config: &Config) -> Vec<(String, PostLoginEnvironment)> {
// NOTE: Maybe we can do something smart with `with_capacity` here.
let mut envs = Vec::new();

match fs::read_dir(&config.x11.xsessions_path) {
Ok(paths) => {
for path in paths {
let Ok(path) = path else {
continue;
};

let path = path.path();

match parse_desktop_entry(&path, config) {
Ok((name, exec)) => {
info!("Added environment '{name}' from xsessions");
envs.push((name, PostLoginEnvironment::X { xinitrc_path: exec }));
}
Err(err) => warn!("Skipping '{}', because {err}", path.display()),
}
}
}
Err(err) => {
warn!("Failed to read from the xsessions folder '{err}'",);
}
}

match fs::read_dir(&config.wayland.wayland_sessions_path
) {
Ok(paths) => {
for path in paths {
let Ok(path) = path else {
continue;
};

let path = path.path();

match parse_desktop_entry(&path, config) {
Ok((name, exec)) => {
info!("Added environment '{name}' from wayland sessions");
envs.push((name, PostLoginEnvironment::Wayland { script_path: exec }))
}
Err(err) => warn!("Skipping '{}', because {err}", path.display()),
}
}
}
Err(err) => {
warn!("Failed to read from the wayland sessions folder '{err}'",);
}
}

match fs::read_dir(&config.x11.scripts_path) {
Ok(paths) => {
for path in paths {
Expand All @@ -255,6 +356,7 @@ pub fn get_envs(config: &Config) -> Vec<(String, PostLoginEnvironment)> {
}
}

info!("Added environment '{file_name}' from lemurs x11 scripts");
envs.push((
file_name,
PostLoginEnvironment::X {
Expand Down Expand Up @@ -303,6 +405,7 @@ pub fn get_envs(config: &Config) -> Vec<(String, PostLoginEnvironment)> {
}
}

info!("Added environment '{file_name}' from lemurs wayland scripts");
envs.push((
file_name,
PostLoginEnvironment::Wayland {
Expand Down Expand Up @@ -334,6 +437,10 @@ pub fn get_envs(config: &Config) -> Vec<(String, PostLoginEnvironment)> {
}

if envs.is_empty() || config.environment_switcher.include_tty_shell {
if envs.is_empty() {
info!("Added TTY SHELL because no other environments were found");
}

envs.push(("TTYSHELL".to_string(), PostLoginEnvironment::Shell));
}

Expand Down

0 comments on commit 74e1122

Please sign in to comment.