Skip to content

Commit

Permalink
Add XDG_SESSION_TYPE for X11/Wayland sessions
Browse files Browse the repository at this point in the history
Resolves #31.
  • Loading branch information
rharish101 committed Dec 22, 2024
1 parent 0a44393 commit 09382b8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
45 changes: 33 additions & 12 deletions src/gui/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use tokio::{sync::Mutex, time::sleep};
use crate::cache::Cache;
use crate::client::{AuthStatus, GreetdClient};
use crate::config::Config;
use crate::sysutil::SysUtil;
use crate::sysutil::{SessionInfo, SessionType, SysUtil};

use super::messages::{CommandMsg, UserSessInfo};

Expand Down Expand Up @@ -455,18 +455,24 @@ impl Greeter {
}

/// Get the currently selected session name (if available) and command.
fn get_current_session_cmd(
fn get_current_session_info(
&mut self,
sender: &AsyncComponentSender<Self>,
) -> (Option<String>, Option<Vec<String>>) {
) -> (Option<String>, Option<SessionInfo>) {
let info = self.sess_info.as_ref().expect("No session info set yet");
if self.updates.manual_sess_mode {
debug!(
"Retrieved session command '{}' through manual entry",
info.sess_text
);
if let Some(cmd) = shlex::split(info.sess_text.as_str()) {
(None, Some(cmd))
(
None,
Some(SessionInfo {
command: cmd,
sess_type: SessionType::Unknown,
}),
)
} else {
// This must be an invalid command.
self.display_error(
Expand All @@ -479,8 +485,8 @@ impl Greeter {
} else if let Some(session) = &info.sess_id {
// Get the currently selected session.
debug!("Retrieved current session: {session}");
if let Some(cmd) = self.sys_util.get_sessions().get(session.as_str()) {
(Some(session.to_string()), Some(cmd.clone()))
if let Some(sess_info) = self.sys_util.get_sessions().get(session.as_str()) {
(Some(session.to_string()), Some(sess_info.clone()))
} else {
// Shouldn't happen, unless there are no sessions available.
let error_msg = format!("Session '{session}' not found");
Expand All @@ -496,7 +502,13 @@ impl Greeter {
};
warn!("No entry found; using default login shell of user: {username}",);
if let Some(cmd) = self.sys_util.get_shells().get(username.as_str()) {
(None, Some(cmd.clone()))
(
None,
Some(SessionInfo {
command: cmd.clone(),
sess_type: SessionType::Unknown,
}),
)
} else {
// No login shell exists.
let error_msg = "No session or login shell found";
Expand All @@ -509,16 +521,25 @@ impl Greeter {
/// Start the session for the selected user.
async fn start_session(&mut self, sender: &AsyncComponentSender<Self>) {
// Get the session command.
let (session, cmd) = if let (session, Some(cmd)) = self.get_current_session_cmd(sender) {
(session, cmd)
let (session, info) = if let (session, Some(info)) = self.get_current_session_info(sender) {
(session, info)
} else {
// Error handling should be inside `get_current_session_cmd`, so simply return.
// Error handling should be inside `get_current_session_info`, so simply return.
return;
};

// Generate env string that will be passed to greetd when starting the session
let env = self.config.get_env();
let mut environment = Vec::with_capacity(env.len());
let mut environment = Vec::with_capacity(env.len() + 1);
match info.sess_type {
SessionType::X11 => {
environment.push("XDG_SESSION_TYPE=x11".to_string());
}
SessionType::Wayland => {
environment.push("XDG_SESSION_TYPE=wayland".to_string());
}
SessionType::Unknown => {}
};
for (k, v) in env {
environment.push(format!("{}={}", k, v));
}
Expand All @@ -543,7 +564,7 @@ impl Greeter {
.greetd_client
.lock()
.await
.start_session(cmd, environment)
.start_session(info.command, environment)
.await
.unwrap_or_else(|err| panic!("Failed to start session: {err}"));

Expand Down
27 changes: 25 additions & 2 deletions src/sysutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,23 @@ use crate::constants::{LOGIN_DEFS_PATHS, LOGIN_DEFS_UID_MAX, LOGIN_DEFS_UID_MIN,
/// XDG data directory variable name (parent directory for X11/Wayland sessions)
const XDG_DIR_ENV_VAR: &str = "XDG_DATA_DIRS";

#[derive(Clone, Copy)]
pub enum SessionType {
X11,
Wayland,
Unknown,
}

#[derive(Clone)]
pub struct SessionInfo {
pub command: Vec<String>,
pub sess_type: SessionType,
}

// Convenient aliases for used maps
type UserMap = HashMap<String, String>;
type ShellMap = HashMap<String, Vec<String>>;
type SessionMap = HashMap<String, Vec<String>>;
type SessionMap = HashMap<String, SessionInfo>;

/// Stores info of all regular users and sessions
pub struct SysUtil {
Expand Down Expand Up @@ -290,7 +303,17 @@ impl SysUtil {
continue;
};
found_session_names.insert(fname_and_type);
sessions.insert(name.to_string(), cmd);
sessions.insert(
name.to_string(),
SessionInfo {
command: cmd,
sess_type: if is_x11 {
SessionType::X11
} else {
SessionType::Wayland
},
},
);
}
}

Expand Down

0 comments on commit 09382b8

Please sign in to comment.