From 98740dbfd71c714169ae05ad87de58d86c849063 Mon Sep 17 00:00:00 2001 From: 21pages Date: Mon, 5 Jun 2023 20:24:16 +0800 Subject: [PATCH] keep server device awake if have connection Signed-off-by: 21pages --- Cargo.lock | 275 +++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/platform/linux.rs | 21 ++- src/platform/macos.rs | 15 ++ src/platform/windows.rs | 28 +++- src/server/video_service.rs | 15 ++ 6 files changed, 344 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bec83b664..2c0212b60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,6 +188,43 @@ version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" +[[package]] +name = "apple-bindgen" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38f109ee76f68b4767848cb5dc93bfcc7c425deca849c4c81fa11cdce525e3d2" +dependencies = [ + "apple-sdk", + "bindgen 0.63.0", + "derive_more", + "regex", + "serde 1.0.163", + "thiserror", + "toml 0.6.0", +] + +[[package]] +name = "apple-sdk" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a04f192a700686ee70008ff4e4eb76fe7d11814ab93b7ee9d48c36b9a9f0bd2a" +dependencies = [ + "plist", + "serde 1.0.163", + "serde_json 1.0.96", +] + +[[package]] +name = "apple-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12b3a1c3342678cd72676d0c1644fde496c1f65ea41f51465f54a89cad3bdf34" +dependencies = [ + "apple-bindgen", + "apple-sdk", + "objc", +] + [[package]] name = "arboard" version = "3.2.0" @@ -445,6 +482,28 @@ dependencies = [ "which", ] +[[package]] +name = "bindgen" +version = "0.63.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36d860121800b2a9a94f9b5604b332d5cffb234ce17609ea479d723dbc9d3885" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "proc-macro2 1.0.56", + "quote 1.0.27", + "regex", + "rustc-hash", + "shlex", + "syn 1.0.109", + "which", +] + [[package]] name = "bindgen" version = "0.64.0" @@ -1022,12 +1081,44 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "const_fn" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbdcdcb6d86f71c5e97409ad45898af11cbc995b4ee8112d59095a28d376c935" + +[[package]] +name = "const_format" +version = "0.2.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c990efc7a285731f9a4378d81aff2f0e85a2c8781a05ef0f8baa8dac54d0ff48" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e026b6ce194a874cb9cf32cd5772d1ef9767cc8fcb5765948d74f37a9d8b2bf6" +dependencies = [ + "proc-macro2 1.0.56", + "quote 1.0.27", + "unicode-xid 0.2.4", +] + [[package]] name = "constant_time_eq" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "convert_case" version = "0.5.0" @@ -1565,6 +1656,19 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case 0.4.0", + "proc-macro2 1.0.56", + "quote 1.0.27", + "rustc_version", + "syn 1.0.109", +] + [[package]] name = "detect-desktop-environment" version = "0.2.0" @@ -2122,7 +2226,7 @@ dependencies = [ "cbindgen", "chrono", "clap 4.2.7", - "convert_case", + "convert_case 0.5.0", "delegate", "enum-iterator", "enum_dispatch", @@ -2485,6 +2589,19 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "git2" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf7f68c2995f392c49fffb4f95ae2c873297830eb25c6bc4c114ce8f4562acc" +dependencies = [ + "bitflags", + "libc", + "libgit2-sys", + "log", + "url", +] + [[package]] name = "glib" version = "0.10.3" @@ -3179,6 +3296,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "is_debug" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06d198e9919d9822d5f7083ba8530e04de87841eaf21ead9af8f2304efd57c89" + [[package]] name = "itertools" version = "0.9.0" @@ -3286,6 +3409,21 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "keepawake" +version = "0.4.2" +source = "git+https://github.com/segevfiner/keepawake-rs#ade0b2baa1440807c8089e9ed24e5d722468651e" +dependencies = [ + "anyhow", + "apple-sys", + "cfg-if 1.0.0", + "core-foundation 0.9.3", + "shadow-rs", + "windows 0.48.0", + "winres", + "zbus", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -3364,6 +3502,18 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "libgit2-sys" +version = "0.14.2+1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f3d95f6b51075fe9810a7ae22c7095f12b98005ab364d8544797a825ce946a4" +dependencies = [ + "cc", + "libc", + "libz-sys", + "pkg-config", +] + [[package]] name = "libloading" version = "0.7.4" @@ -3478,6 +3628,18 @@ dependencies = [ "x11 2.21.0", ] +[[package]] +name = "libz-sys" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ee889ecc9568871456d42f603d6a0ce59ff328d291063a45cbdf0036baf6db" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "line-wrap" version = "0.1.1" @@ -3931,6 +4093,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nom8" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae01545c9c7fc4486ab7debaf2aad7003ac19431791868fb2e8066df97fad2f8" +dependencies = [ + "memchr", +] + [[package]] name = "ntapi" version = "0.4.1" @@ -4062,6 +4233,15 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "objc" version = "0.2.7" @@ -4551,7 +4731,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" dependencies = [ "once_cell", - "toml_edit", + "toml_edit 0.19.8", ] [[package]] @@ -4584,7 +4764,7 @@ version = "0.4.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" dependencies = [ - "unicode-xid", + "unicode-xid 0.1.0", ] [[package]] @@ -5222,6 +5402,7 @@ dependencies = [ "impersonate_system", "include_dir", "jni 0.21.1", + "keepawake", "lazy_static", "libloading 0.8.0", "libpulse-binding", @@ -5607,6 +5788,19 @@ dependencies = [ "digest", ] +[[package]] +name = "shadow-rs" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427f07ab5f873000cf55324882e12a88c0a7ea7025df4fc1e7e35e688877a583" +dependencies = [ + "const_format", + "git2", + "is_debug", + "time 0.3.21", + "tzdb", +] + [[package]] name = "shared_memory" version = "0.12.4" @@ -5832,7 +6026,7 @@ checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" dependencies = [ "proc-macro2 0.4.30", "quote 0.6.13", - "unicode-xid", + "unicode-xid 0.1.0", ] [[package]] @@ -6135,6 +6329,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f3403384eaacbca9923fa06940178ac13e4edb725486d70e8e15881d0c836cc" dependencies = [ "itoa 1.0.6", + "libc", + "num_threads", "serde 1.0.163", "time-core", "time-macros", @@ -6254,6 +6450,18 @@ dependencies = [ "serde 1.0.163", ] +[[package]] +name = "toml" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb9d890e4dc9298b70f740f615f2e05b9db37dce531f6b24fb77ac993f9f217" +dependencies = [ + "serde 1.0.163", + "serde_spanned", + "toml_datetime 0.5.1", + "toml_edit 0.18.1", +] + [[package]] name = "toml" version = "0.7.3" @@ -6262,8 +6470,17 @@ checksum = "b403acf6f2bb0859c93c7f0d967cb4a75a7ac552100f9322faf64dc047669b21" dependencies = [ "serde 1.0.163", "serde_spanned", - "toml_datetime", - "toml_edit", + "toml_datetime 0.6.1", + "toml_edit 0.19.8", +] + +[[package]] +name = "toml_datetime" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4553f467ac8e3d374bc9a177a26801e5d0f9b211aa1673fb137a403afd1c9cf5" +dependencies = [ + "serde 1.0.163", ] [[package]] @@ -6275,6 +6492,19 @@ dependencies = [ "serde 1.0.163", ] +[[package]] +name = "toml_edit" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c59d8dd7d0dcbc6428bf7aa2f0e823e26e43b3c9aca15bbc9475d23e5fa12b" +dependencies = [ + "indexmap", + "nom8", + "serde 1.0.163", + "serde_spanned", + "toml_datetime 0.5.1", +] + [[package]] name = "toml_edit" version = "0.19.8" @@ -6284,7 +6514,7 @@ dependencies = [ "indexmap", "serde 1.0.163", "serde_spanned", - "toml_datetime", + "toml_datetime 0.6.1", "winnow", ] @@ -6382,6 +6612,25 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" +[[package]] +name = "tz-rs" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33851b15c848fad2cf4b105c6bb66eb9512b6f6c44a4b13f57c53c73c707e2b4" +dependencies = [ + "const_fn", +] + +[[package]] +name = "tzdb" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec758958f2fb5069cd7fae385be95cc8eceb8cdfd270c7d14de6034f0108d99e" +dependencies = [ + "iana-time-zone", + "tz-rs", +] + [[package]] name = "uds_windows" version = "1.0.2" @@ -6440,6 +6689,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" + [[package]] name = "untrusted" version = "0.7.1" @@ -6493,6 +6748,12 @@ dependencies = [ "getrandom", ] +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "vec_map" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index 9c0834ee5..07016c719 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -117,6 +117,7 @@ objc_id = "0.1" tray-icon = "0.5" tao = { git = "https://github.com/Kingtous/tao", branch = "muda" } image = "0.24" +keepawake = { git = "https://github.com/segevfiner/keepawake-rs" } [target.'cfg(target_os = "linux")'.dependencies] psimple = { package = "libpulse-simple-binding", version = "2.27" } diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 00e09967d..fca9970cb 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -776,9 +776,9 @@ pub fn resolutions(name: &str) -> Vec { Screen 0: minimum 320 x 200, current 1920 x 1080, maximum 16384 x 16384 eDP-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 344mm x 193mm - 1920x1080 60.01*+ 60.01 59.97 59.96 59.93 - 1680x1050 59.95 59.88 - 1600x1024 60.17 + 1920x1080 60.01*+ 60.01 59.97 59.96 59.93 + 1680x1050 59.95 59.88 + 1600x1024 60.17 XWAYLAND0 connected primary 1920x984+0+0 (normal left inverted right x axis y axis) 0mm x 0mm Virtual1 connected primary 1920x984+0+0 (normal left inverted right x axis y axis) 0mm x 0mm @@ -1060,3 +1060,18 @@ mod desktop { } } } + +pub struct WakeLock(Option); + +impl WakeLock { + pub fn new(display: bool, idle: bool, sleep: bool) -> Self { + WakeLock( + keepawake::Builder::new() + .display(display) + .idle(idle) + .sleep(sleep) + .create() + .ok(), + ) + } +} diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 506390506..89e102177 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -701,3 +701,18 @@ pub fn elevate(args: Vec<&str>, prompt: &str) -> ResultType { } } } + +pub struct WakeLock(Option); + +impl WakeLock { + pub fn new(display: bool, idle: bool, sleep: bool) -> Self { + WakeLock( + keepawake::Builder::new() + .display(display) + .idle(idle) + .sleep(sleep) + .create() + .ok(), + ) + } +} diff --git a/src/platform/windows.rs b/src/platform/windows.rs index 8d2555fa7..3e96a2aa9 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -40,7 +40,9 @@ use winapi::{ winbase::*, wingdi::*, winnt::{ - TokenElevation, HANDLE, PROCESS_QUERY_LIMITED_INFORMATION, TOKEN_ELEVATION, TOKEN_QUERY, + TokenElevation, ES_AWAYMODE_REQUIRED, ES_CONTINUOUS, ES_DISPLAY_REQUIRED, + ES_SYSTEM_REQUIRED, HANDLE, PROCESS_QUERY_LIMITED_INFORMATION, TOKEN_ELEVATION, + TOKEN_QUERY, }, winreg::HKEY_CURRENT_USER, winuser::*, @@ -2180,6 +2182,30 @@ pub fn is_process_consent_running() -> ResultType { .output()?; Ok(output.status.success() && !output.stdout.is_empty()) } +pub struct WakeLock; +// Failed to compile keepawake-rs on i686 +impl WakeLock { + pub fn new(display: bool, idle: bool, sleep: bool) -> Self { + let mut flag = ES_CONTINUOUS; + if display { + flag |= ES_DISPLAY_REQUIRED; + } + if idle { + flag |= ES_SYSTEM_REQUIRED; + } + if sleep { + flag |= ES_AWAYMODE_REQUIRED; + } + unsafe { SetThreadExecutionState(flag) }; + WakeLock {} + } +} + +impl Drop for WakeLock { + fn drop(&mut self) { + unsafe { SetThreadExecutionState(ES_CONTINUOUS) }; + } +} #[cfg(test)] mod tests { diff --git a/src/server/video_service.rs b/src/server/video_service.rs index 22ad83001..c1d5d7f18 100644 --- a/src/server/video_service.rs +++ b/src/server/video_service.rs @@ -506,6 +506,8 @@ fn check_get_displays_changed_msg() -> Option { } fn run(sp: GenericService) -> ResultType<()> { + #[cfg(not(any(target_os = "android", target_os = "ios")))] + let _wake_lock = get_wake_lock(); #[cfg(all(windows, feature = "virtual_display_driver"))] ensure_close_virtual_device()?; @@ -1058,3 +1060,16 @@ fn start_uac_elevation_check() { } }); } + +#[cfg(not(any(target_os = "android", target_os = "ios")))] +fn get_wake_lock() -> crate::platform::WakeLock { + let (display, idle, sleep) = if cfg!(windows) { + (true, false, false) + } else if cfg!(linux) { + (false, false, true) + } else { + //macos + (true, false, false) + }; + crate::platform::WakeLock::new(display, idle, sleep) +}