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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#![deny(missing_docs)]
//! Desktop environment detection
//!
//! This crate implements automatic detection for the current desktop environment.
//!
//! See [`DesktopEnvironment`] for supported desktop environments.
//!
//! The environment can be detected using [`DesktopEnvironment::detect`]:
//!
//! ```rust
//! use detect_desktop_environment::DesktopEnvironment;
//!
//! match DesktopEnvironment::detect() {
//!   Some(de) => println!("detected desktop environment: {de:?}"),
//!   None => println!("failed to detect desktop environment"),
//! }
//! ```

/// Desktop environments supported by `detect-desktop-environment`.
// If adding new environment, please keep them sorted alphabetically and use `PascalCase`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum DesktopEnvironment {
  /// Cinnamon, the default desktop environment for Linux Mint.
  ///
  /// - <https://en.wikipedia.org/wiki/Cinnamon_(desktop_environment)>
  Cinnamon,
  /// COSMIC, the default desktop environment for Linux Pop!_OS.
  ///
  /// Note: This corresponds to the classic COSMIC based on GNOME, not the Rust
  /// [COSMIC-epoch](https://github.com/pop-os/cosmic-epoch). Please send a PR if you can
  /// test how to detect cosmic-epoch.
  ///
  /// - <https://github.com/pop-os/cosmic>
  Cosmic,
  /// Deepin desktop environment
  ///
  /// - <https://www.deepin.org/index/en>
  Dde,
  /// EDE Desktop
  ///
  /// - <https://edeproject.org/>
  Ede,
  /// Endless OS desktop
  ///
  /// - <https://www.endlessos.org/os>
  Endless,
  /// Enlightenment desktop environment.
  ///
  /// - <https://en.wikipedia.org/wiki/Enlightenment_(software)>
  Enlightenment,
  /// Gnome, the default environment for many major Linux distributions.
  ///
  /// - <https://en.wikipedia.org/wiki/GNOME>
  Gnome,
  /// Hyprland tiling window manager
  ///
  /// - <https://hyprland.org/>
  Hyprland,
  /// KDE Plasma, the Kool Desktop Environment.
  ///
  /// - <https://kde.org/plasma-desktop/>
  Kde,
  /// LXDE
  ///
  /// - <https://www.lxde.org/>
  Lxde,
  /// LXQt
  ///
  /// - <https://lxqt-project.org/>
  Lxqt,
  /// MacOs, the environment for Apple's OS
  MacOs,
  /// MATE
  ///
  /// - <https://mate-desktop.org/>
  Mate,
  /// Legacy menu systems
  ///
  /// Listed in [Freedesktop Desktop Environments](https://specifications.freedesktop.org/menu-spec/latest/apb.html).
  // Please send a PR if you have more details or better ideas about how to handle this value.
  Old,
  /// Elementary OS Desktop Environment
  ///
  /// - <https://elementary.io/>
  Pantheon,
  /// Razor-qt
  ///
  /// Discontinued Desktop Environment, this is an ancestor of LXQt.
  ///
  ///
  /// - <https://github.com/Razor-qt/razor-qt>
  Razor,
  /// ROX Desktop
  ///
  /// - <https://rox.sourceforge.net/desktop/>
  Rox,
  /// Sway tiling window manager
  ///
  /// - <https://swaywm.org/>
  Sway,
  /// TrinityDesktopEnvironment
  ///
  /// - <https://www.trinitydesktop.org/>
  Tde,
  /// Unity, the legacy desktop environment for Ubuntu
  ///
  /// - <https://en.wikipedia.org/wiki/Unity_%28user_interface%29>
  Unity,
  /// Windows, the environments for Microsoft's OS
  Windows,
  /// Xfce
  ///
  /// - <https://xfce.org/>
  Xfce,
}

impl DesktopEnvironment {
  /// Detect the current desktop environment
  ///
  /// If the current desktop environment can't be detected, `None` is returned.
  pub fn detect() -> Option<Self> {
    Self::detect_impl()
  }

  /// Test if the desktop environment is based on the GTK framework
  ///
  /// See <https://en.wikipedia.org/wiki/Category:Desktop_environments_based_on_GTK>
  ///
  /// ```
  /// use detect_desktop_environment::DesktopEnvironment;
  ///
  /// // All matching desktop environments:
  /// assert!(DesktopEnvironment::Cinnamon.gtk());
  /// assert!(DesktopEnvironment::Cosmic.gtk());
  /// assert!(DesktopEnvironment::Gnome.gtk());
  /// assert!(DesktopEnvironment::Lxde.gtk());
  /// assert!(DesktopEnvironment::Mate.gtk());
  /// assert!(DesktopEnvironment::Unity.gtk());
  /// assert!(DesktopEnvironment::Xfce.gtk());
  /// assert!(DesktopEnvironment::Pantheon.gtk());
  /// assert!(DesktopEnvironment::Dde.gtk());
  ///
  /// // Non-GTK examples
  /// assert!(!DesktopEnvironment::Kde.gtk());
  /// assert!(!DesktopEnvironment::Windows.gtk());
  /// ```
  pub const fn gtk(self) -> bool {
    use DesktopEnvironment::*;
    matches!(self, Cinnamon | Cosmic | Dde | Gnome | Lxde | Mate | Pantheon | Unity | Xfce)
  }

  /// Test if the desktop environment is based on the Qt framework
  ///
  /// ```
  /// use detect_desktop_environment::DesktopEnvironment;
  ///
  /// // All matching desktop environments:
  /// assert!(DesktopEnvironment::Kde.qt());
  /// assert!(DesktopEnvironment::Lxqt.qt());
  /// assert!(DesktopEnvironment::Razor.qt());
  /// assert!(DesktopEnvironment::Tde.qt());
  ///
  /// // Non-Qt examples
  /// assert!(!DesktopEnvironment::Gnome.qt());
  /// assert!(!DesktopEnvironment::Windows.qt());
  /// ```
  pub const fn qt(self) -> bool {
    use DesktopEnvironment::*;
    matches!(self, Kde | Lxqt | Razor | Tde)
  }

  #[cfg(target_os = "macos")]
  fn detect_impl() -> Option<Self> {
    Some(DesktopEnvironment::MacOs)
  }

  #[cfg(target_os = "windows")]
  fn detect_impl() -> Option<Self> {
    Some(DesktopEnvironment::Windows)
  }

  #[cfg(not(any(target_os = "macos", target_os = "windows")))]
  fn detect_impl() -> Option<Self> {
    std::env::var("XDG_CURRENT_DESKTOP").ok().as_deref().and_then(Self::from_xdg_current_desktop)
  }

  /// Parse the desktop environment from the name registered with Freedesktop.org
  ///
  /// See <https://specifications.freedesktop.org/menu-spec/latest/apb.html>
  ///
  /// Returns `None` if the desktop is not registered.
  ///
  /// This function is strictly restricted to the DEs registered with Freedesktop, for a more
  /// complete list use [`DesktopEnvironment::from_xdg_name`]. Note that the check follows the
  /// spec and is case-sensitive.
  ///
  /// ```
  /// use detect_desktop_environment::DesktopEnvironment;
  ///
  /// assert_eq!("KDE", Some(DesktopEnvironment::Kde));
  /// assert_eq!("kde", None); // must be uppercase
  /// assert_eq!("SWAY", None); // not registered
  /// assert_eq!("unknown_de", None);
  /// ```
  pub fn from_freedesktop(name: &str) -> Option<Self> {
    // the patterns in the match below are ordered to match the order in the freedesktop table
    match name {
      "GNOME" => Some(DesktopEnvironment::Gnome),
      "GNOME-Classice" => Some(DesktopEnvironment::Gnome),
      "GNOME-Flashback" => Some(DesktopEnvironment::Gnome),
      "KDE" => Some(DesktopEnvironment::Kde),
      "LXDE" => Some(DesktopEnvironment::Lxde),
      "LXQt" => Some(DesktopEnvironment::Lxqt),
      "MATE" => Some(DesktopEnvironment::Mate),
      "Razor" => Some(DesktopEnvironment::Razor),
      "ROX" => Some(DesktopEnvironment::Rox),
      "TDE" => Some(DesktopEnvironment::Tde),
      "Unity" => Some(DesktopEnvironment::Unity),
      "XFCE" => Some(DesktopEnvironment::Xfce),
      "EDE" => Some(DesktopEnvironment::Ede),
      "Cinnamon" => Some(DesktopEnvironment::Cinnamon),
      "Pantheon" => Some(DesktopEnvironment::Pantheon),
      "DDE" => Some(DesktopEnvironment::Dde),
      "Endless" =>Some(DesktopEnvironment::Endless),
      "Old" =>Some(DesktopEnvironment::Old),
      _ => None,
    }
  }

  /// Parse the XDG desktop environment name
  ///
  /// This is an extended variant of [`DesktopEnvironment::from_freedesktop`]. It supports all
  /// registered Freedesktop names, as well as some extra unregistered names. This is the
  /// recommended method to parse names from the list in the env var `XDG_CURRENT_DESKTOP`.
  ///
  /// Returns `None` if the name is unknown.
  ///
  /// ```
  /// use detect_desktop_environment::DesktopEnvironment;
  ///
  /// assert_eq!("KDE", Some(DesktopEnvironment::Kde)); // freedesktop DE
  /// assert_eq!("kde", None); // must be uppercase
  /// assert_eq!("SWAY", Some(DesktopEnvironment::Sway)); // not registered
  /// assert_eq!("unknown_de", None);
  /// ```
  pub fn from_xdg_name(name: &str) -> Option<Self> {
    if let Some(de) = Self::from_freedesktop(name) {
      return Some(de);
    }

    // keep the patterns sorted alphabetically
    match name {
      "ENLIGHTENMENT" => Some(DesktopEnvironment::Enlightenment),
      "Hyprland" => Some(DesktopEnvironment::Hyprland),
      "SWAY" => Some(DesktopEnvironment::Sway),
      "X-Cinnamon" => Some(DesktopEnvironment::Cinnamon),
      _ => None,
    }
  }

  /// Retrieve the desktop environment from the format used by `XDG_CURRENT_DESKTOP`.
  ///
  /// `XDG_CURRENT_DESKTOP` is a colon separated list of information about the current desktop
  /// environment.
  /// See: <https://specifications.freedesktop.org/mime-apps-spec/1.0.1/ar01s02.html>
  ///
  /// Returns `None` if the resolution fails.
  /// Duplicate entries are allowed as long as they correspond to same Desktop Environment.
  pub fn from_xdg_current_desktop(xdg_current_desktop: &str) -> Option<Self> {
    let mut resolved: Option<DesktopEnvironment> = None;

    for part in xdg_current_desktop.split(':') {
      let de = match Self::from_xdg_name(part) {
        Some(de) => de,
        None => {
          // We ignore parsing errors as we don't really control which values are possible.
          // Some of the entries don't even represent a DE (e.g. `ubuntu:GNOME`, where `ubuntu` is
          // a distro, not a DE)
          // If you want more control over this, open an issue to discuss it.
          continue
        },
      };
      match resolved {
        None => {
          // first successfully parsed DE, store it but keep iterating to check for conflicts
          resolved = Some(de)
        },
        Some(prev) => {
          // a DE was already parsed previously, duplicates are allowed but a conflict causes
          // immediate rejection with `None`.
          // If you want more control over this, open an issue to discuss it.
          if de != prev {
            return None
          }
        }
      }
    }

    resolved
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn linux_tests() {
    // Cases without colon
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("Cinnamon"),
      Some(DesktopEnvironment::Cinnamon)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("ENLIGHTENMENT"),
      Some(DesktopEnvironment::Enlightenment)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("GNOME"),
      Some(DesktopEnvironment::Gnome)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("KDE"),
      Some(DesktopEnvironment::Kde)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("LXDE"),
      Some(DesktopEnvironment::Lxde)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("LXQt"),
      Some(DesktopEnvironment::Lxqt)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("MATE"),
      Some(DesktopEnvironment::Mate)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("Unity"),
      Some(DesktopEnvironment::Unity)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("X-Cinnamon"),
      Some(DesktopEnvironment::Cinnamon)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("XFCE"),
      Some(DesktopEnvironment::Xfce)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("TDE"),
      Some(DesktopEnvironment::Tde)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("DDE"),
      Some(DesktopEnvironment::Dde)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("Pantheon"),
      Some(DesktopEnvironment::Pantheon)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("SWAY"),
      Some(DesktopEnvironment::Sway)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("Hyprland"),
      Some(DesktopEnvironment::Hyprland)
    );

    // Colon splitting
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("ubuntu:GNOME"),
      Some(DesktopEnvironment::Gnome)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("ubuntu:KDE"),
      Some(DesktopEnvironment::Kde)
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("pop:GNOME"),
      Some(DesktopEnvironment::Gnome)
    );

    // Mixed messages
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("KDE:GNOME"),
      None
    );
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("ubuntu:KDE:GNOME"),
      None
    );

    // Strange cases
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("GNOME:GNOME"),
      Some(DesktopEnvironment::Gnome)
    );

    // Empty string
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop(""),
      None
    );

    // Unknown Desktop Environment
    assert_eq!(
      DesktopEnvironment::from_xdg_current_desktop("foo"),
      None
    );
  }
}