about summary refs log tree commit diff
path: root/src/helpers/idle.rs
blob: 074eba9abe8c559e7a0bec7f912c17399d0b3f76 (plain) (blame)
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
// Furtherance - Track your time without being tracked
// Copyright (C) 2024  Ricky Kresslein <rk@unobserved.io>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use std::env;
use std::time::Duration;

#[cfg(target_os = "linux")]
use {
    std::path::Path, std::sync::Arc
};

pub fn get_mac_windows_x11_idle_seconds() -> u64 {
    0 // unimplemented
}

pub fn get_idle_time() -> Result<u64, Box<dyn std::error::Error>> {
    match env::consts::OS {
        "windows" => Ok(get_mac_windows_x11_idle_seconds()),
        "macos" => Ok(get_mac_windows_x11_idle_seconds()),
        #[cfg(target_os = "linux")]
        "linux" => {
            if is_wayland() {
                if is_gnome() {
                    get_gnome_idle_sync()
                } else {
                    get_wayland_idle_sync()
                }
            } else if is_x11() {
                Ok(get_mac_windows_x11_idle_seconds())
            } else {
                Ok(0)
            }
        }
        _ => Ok(0),
    }
}

#[cfg(target_os = "linux")]
fn is_wayland() -> bool {
    if let Ok(_) = env::var("XDG_SESSION_TYPE").map(|v| v == "wayland") {
        return true;
    } else if let Ok(display) = env::var("WAYLAND_DISPLAY") {
        if display.chars().next() == Some('/') {
            return Path::new(&display).exists();
        }
        if let Ok(runtime_dir) = env::var("XDG_RUNTIME_DIR") {
            return Path::new(
                &format!("{}/{}", runtime_dir, display)
            ).exists();
        }
    }
    false
}

#[cfg(target_os = "linux")]
fn is_x11() -> bool {
    x11rb::connect(None).is_ok()
}

#[cfg(target_os = "linux")]
fn get_wayland_idle_sync() -> Result<u64, Box<dyn std::error::Error>> {
    use crate::helpers::wayland_idle;

    wayland_idle::initialize_wayland().unwrap();

    Ok(wayland_idle::get_idle_time())
}

#[cfg(target_os = "linux")]
fn get_gnome_idle_sync() -> Result<u64, Box<dyn std::error::Error>> {
    use dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
    let c = dbus::blocking::Connection::new_session()?;

    let p = c.with_proxy(
        "org.gnome.Mutter.IdleMonitor",
        "/org/gnome/Mutter/IdleMonitor/Core",
        Duration::from_millis(5000),
    );
    let (idle_time,): (u64,) =
        p.method_call("org.gnome.Mutter.IdleMonitor", "GetIdletime", ())?;
    Ok(idle_time / 1000)
}

#[cfg(target_os = "linux")]
pub fn is_kde() -> bool {
    if let Ok(desktop) = std::env::var("XDG_CURRENT_DESKTOP") {
        return desktop.to_uppercase().contains("KDE");
    }
    false
}

#[cfg(target_os = "linux")]
fn is_gnome() -> bool {
    if let Ok(xdg_current_desktop) = env::var("XDG_CURRENT_DESKTOP") {
        if xdg_current_desktop.to_lowercase().contains("gnome") {
            return true;
        }
    }

    if let Ok(gdm_session) = env::var("GDMSESSION") {
        if gdm_session.to_lowercase().contains("gnome") {
            return true;
        }
    }

    false
}