1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! Whether the product runs with a window.
//!
//! The service is the product; the window is a layer on top of it that exists only where a
//! graphical system does. This module owns the one decision that follows from that: whether to
//! create a window at all ([`window_mode`]). It is a pure read — no display is touched and no probe
//! is run — so the decision can be exercised without a graphical system, and the boot path can name
//! it in one plain sentence.
/// Where the process runs, as far as window creation is concerned.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Platform {
MacOs,
Windows,
Linux,
/// Any other target: no window layer is built for it.
Other,
}
/// The graphical-system facts a window decision reads. Kept apart from the environment so the
/// decision is a pure function.
#[derive(Clone, Copy, Default)]
struct DisplayEnv<'a> {
pub display: Option<&'a str>,
pub wayland_display: Option<&'a str>,
/// `MAHBOT_HEADLESS`: an operator's own decision to run a host with an unusable graphical
/// system as a service. Not a mode switch — the service is the same either way.
pub headless: bool,
}
/// Whether a window is created, and why not when it is not.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum WindowMode {
/// A graphical system exists; the window is created and drives the same service.
Window,
/// No window: the service runs on its own. The sentence is what the boot logs once.
Headless(&'static str),
}
impl WindowMode {
#[must_use]
pub const fn is_headless(self) -> bool {
matches!(self, Self::Headless(_))
}
}
const NO_DISPLAY: &str =
"no graphical system was found (neither DISPLAY nor WAYLAND_DISPLAY is set)";
const FORCED_HEADLESS: &str = "MAHBOT_HEADLESS is set";
const NO_WINDOW_LAYER: &str = "this platform has no window layer";
/// Decide from facts alone. A graphical system is a platform with a window layer and — on Linux,
/// where the display is named by the environment — a display to put the window on. A host that
/// names no display is a server, not a broken desktop: nothing is guessed from a failed
/// connection, so an SSH install on a desktop is not mistaken for one.
#[must_use]
fn window_mode(platform: Platform, env: DisplayEnv<'_>) -> WindowMode {
if env.headless {
return WindowMode::Headless(FORCED_HEADLESS);
}
match platform {
Platform::MacOs | Platform::Windows => WindowMode::Window,
Platform::Linux => {
if present(env.display) || present(env.wayland_display) {
WindowMode::Window
} else {
WindowMode::Headless(NO_DISPLAY)
}
}
Platform::Other => WindowMode::Headless(NO_WINDOW_LAYER),
}
}
/// The decision this process runs under, read once at the daemon's start — before anything asks
/// for a display, and handed to whatever needs it rather than read a second time.
#[must_use]
pub fn detect_window_mode() -> WindowMode {
let display = std::env::var("DISPLAY").ok();
let wayland_display = std::env::var("WAYLAND_DISPLAY").ok();
window_mode(
platform(),
DisplayEnv {
display: display.as_deref(),
wayland_display: wayland_display.as_deref(),
headless: headless_requested(),
},
)
}
/// Whether the operator asked this host to run without a window.
#[must_use]
fn headless_requested() -> bool {
std::env::var("MAHBOT_HEADLESS").is_ok_and(|value| value != "0" && !value.is_empty())
}
const fn platform() -> Platform {
if cfg!(target_os = "macos") {
Platform::MacOs
} else if cfg!(target_os = "windows") {
Platform::Windows
} else if cfg!(target_os = "linux") {
Platform::Linux
} else {
Platform::Other
}
}
fn present(value: Option<&str>) -> bool {
value.is_some_and(|value| !value.trim().is_empty())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_platform_with_a_window_layer_and_a_display_gets_a_window() {
assert_eq!(
window_mode(
Platform::Linux,
DisplayEnv {
display: Some(":0"),
..DisplayEnv::default()
}
),
WindowMode::Window
);
assert_eq!(
window_mode(
Platform::Linux,
DisplayEnv {
wayland_display: Some("wayland-0"),
..DisplayEnv::default()
}
),
WindowMode::Window
);
assert_eq!(
window_mode(Platform::MacOs, DisplayEnv::default()),
WindowMode::Window
);
assert_eq!(
window_mode(Platform::Windows, DisplayEnv::default()),
WindowMode::Window
);
}
#[test]
fn a_linux_host_with_no_display_runs_without_a_window() {
assert_eq!(
window_mode(Platform::Linux, DisplayEnv::default()),
WindowMode::Headless(NO_DISPLAY)
);
// An empty or blank variable names no display either.
assert_eq!(
window_mode(
Platform::Linux,
DisplayEnv {
display: Some(" "),
wayland_display: Some(""),
..DisplayEnv::default()
}
),
WindowMode::Headless(NO_DISPLAY)
);
}
#[test]
fn the_operators_own_decision_wins_over_the_environment() {
assert_eq!(
window_mode(
Platform::MacOs,
DisplayEnv {
headless: true,
..DisplayEnv::default()
}
),
WindowMode::Headless(FORCED_HEADLESS)
);
assert_eq!(
window_mode(
Platform::Linux,
DisplayEnv {
display: Some(":0"),
headless: true,
..DisplayEnv::default()
}
),
WindowMode::Headless(FORCED_HEADLESS)
);
assert_eq!(
window_mode(Platform::Other, DisplayEnv::default()),
WindowMode::Headless(NO_WINDOW_LAYER)
);
}
}