⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

veridian_kernel/desktop/
session_config.rs

1//! Session configuration reader
2//!
3//! Reads `/etc/veridian/session.conf` to determine the preferred desktop
4//! session type (KDE Plasma 6 or built-in DE). Falls back to Plasma if
5//! the config file is missing or unreadable.
6
7#[cfg(feature = "alloc")]
8extern crate alloc;
9
10/// Preferred session type read from config or CLI argument.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum SessionPreference {
13    /// KDE Plasma 6 desktop session.
14    Plasma,
15    /// Built-in kernel-space desktop environment.
16    Builtin,
17}
18
19/// Default session config path.
20const SESSION_CONF_PATH: &str = "/etc/veridian/session.conf";
21
22/// Path to KDE init script (existence check for KDE availability).
23const KDE_INIT_SCRIPT: &str = "/usr/share/veridian/veridian-kde-init.sh";
24
25/// Read the session preference from `/etc/veridian/session.conf`.
26///
27/// The config file format is simple key=value lines. We look for
28/// `session_type=plasma` or `session_type=builtin`. Missing file or
29/// unrecognized value defaults to `Plasma`.
30#[cfg(feature = "alloc")]
31pub fn read_session_preference() -> SessionPreference {
32    match crate::fs::read_file(SESSION_CONF_PATH) {
33        Ok(data) => parse_session_config(&data),
34        Err(_) => {
35            // No config file -- default to Plasma
36            SessionPreference::Plasma
37        }
38    }
39}
40
41/// Parse session config bytes into a preference.
42#[cfg(feature = "alloc")]
43fn parse_session_config(data: &[u8]) -> SessionPreference {
44    let text = match core::str::from_utf8(data) {
45        Ok(s) => s,
46        Err(_) => return SessionPreference::Plasma,
47    };
48
49    for line in text.lines() {
50        let trimmed = line.trim();
51        if trimmed.starts_with('#') || trimmed.is_empty() {
52            continue;
53        }
54        if let Some(value) = trimmed.strip_prefix("session_type=") {
55            return match value.trim() {
56                "plasma" | "kde" => SessionPreference::Plasma,
57                "builtin" | "default" => SessionPreference::Builtin,
58                _ => SessionPreference::Plasma,
59            };
60        }
61    }
62
63    // No session_type line found -- default to Plasma
64    SessionPreference::Plasma
65}
66
67/// Check whether KDE binaries are available on the filesystem.
68///
69/// Returns `true` if the KDE init script exists at the expected path.
70#[cfg(feature = "alloc")]
71pub fn kde_binaries_available() -> bool {
72    crate::fs::read_file(KDE_INIT_SCRIPT).is_ok()
73}
74
75// =========================================================================
76// Tests
77// =========================================================================
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_parse_plasma() {
85        let data = b"session_type=plasma\n";
86        assert_eq!(parse_session_config(data), SessionPreference::Plasma);
87    }
88
89    #[test]
90    fn test_parse_kde_alias() {
91        let data = b"session_type=kde\n";
92        assert_eq!(parse_session_config(data), SessionPreference::Plasma);
93    }
94
95    #[test]
96    fn test_parse_builtin() {
97        let data = b"session_type=builtin\n";
98        assert_eq!(parse_session_config(data), SessionPreference::Builtin);
99    }
100
101    #[test]
102    fn test_parse_default_alias() {
103        let data = b"session_type=default\n";
104        assert_eq!(parse_session_config(data), SessionPreference::Builtin);
105    }
106
107    #[test]
108    fn test_parse_empty_file() {
109        let data = b"";
110        assert_eq!(parse_session_config(data), SessionPreference::Plasma);
111    }
112
113    #[test]
114    fn test_parse_comments_only() {
115        let data = b"# This is a comment\n# Another comment\n";
116        assert_eq!(parse_session_config(data), SessionPreference::Plasma);
117    }
118
119    #[test]
120    fn test_parse_unknown_value() {
121        let data = b"session_type=gnome\n";
122        assert_eq!(parse_session_config(data), SessionPreference::Plasma);
123    }
124
125    #[test]
126    fn test_parse_with_comments_and_whitespace() {
127        let data = b"# Session configuration\n\nsession_type=builtin\n";
128        assert_eq!(parse_session_config(data), SessionPreference::Builtin);
129    }
130
131    #[test]
132    fn test_parse_invalid_utf8() {
133        let data = &[0xFF, 0xFE, 0x00];
134        assert_eq!(parse_session_config(data), SessionPreference::Plasma);
135    }
136}