fotos_lib/capture/
detect.rs1#[derive(Debug, Clone, PartialEq)]
2pub enum Platform {
3 LinuxWaylandGnome,
4 LinuxWaylandKde,
5 LinuxWaylandOther,
6 LinuxX11,
7 Windows,
8}
9
10pub fn detect_platform() -> Platform {
11 #[cfg(target_os = "windows")]
12 {
13 return Platform::Windows;
14 }
15
16 #[cfg(target_os = "linux")]
17 {
18 let session_type = std::env::var("XDG_SESSION_TYPE").unwrap_or_default();
19 let desktop = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default();
20 let wayland_display = std::env::var("WAYLAND_DISPLAY").unwrap_or_default();
21 detect_linux(&session_type, &desktop, &wayland_display)
22 }
23
24 #[cfg(not(any(target_os = "linux", target_os = "windows")))]
25 Platform::LinuxX11
26}
27
28fn detect_linux(session_type: &str, desktop: &str, wayland_display: &str) -> Platform {
30 let is_wayland =
31 session_type == "wayland" || (session_type.is_empty() && !wayland_display.is_empty());
32
33 if is_wayland {
34 if desktop.contains("GNOME") {
35 Platform::LinuxWaylandGnome
36 } else if desktop.contains("KDE") {
37 Platform::LinuxWaylandKde
38 } else {
39 Platform::LinuxWaylandOther
40 }
41 } else {
42 Platform::LinuxX11
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn wayland_session_type_gnome() {
52 assert_eq!(
53 detect_linux("wayland", "GNOME", ""),
54 Platform::LinuxWaylandGnome
55 );
56 }
57
58 #[test]
59 fn wayland_session_type_kde() {
60 assert_eq!(
61 detect_linux("wayland", "KDE", ""),
62 Platform::LinuxWaylandKde
63 );
64 }
65
66 #[test]
67 fn wayland_session_type_other() {
68 assert_eq!(
69 detect_linux("wayland", "Sway", ""),
70 Platform::LinuxWaylandOther
71 );
72 }
73
74 #[test]
75 fn unset_session_with_wayland_display_is_wayland() {
76 assert_eq!(
77 detect_linux("", "Sway", "wayland-0"),
78 Platform::LinuxWaylandOther
79 );
80 }
81
82 #[test]
83 fn unset_session_without_wayland_display_is_x11() {
84 assert_eq!(detect_linux("", "", ""), Platform::LinuxX11);
85 }
86
87 #[test]
88 fn x11_session_type_is_x11() {
89 assert_eq!(detect_linux("x11", "GNOME", ""), Platform::LinuxX11);
90 }
91}