From 1324be0da86dad53922679d7f9ec1dce028b6fd3 Mon Sep 17 00:00:00 2001 From: Raffaele Meloni Date: Sun, 24 Mar 2024 19:50:02 +0100 Subject: [PATCH] Use the lock crate instead of keyboard shortcuts to lock the screen --- .gitignore | 1 + Cargo.lock | 82 +++++------------------------------------------------- Cargo.toml | 3 +- src/app.rs | 30 +++++++++----------- 4 files changed, 22 insertions(+), 94 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..3a8cabc 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.idea diff --git a/Cargo.lock b/Cargo.lock index e83b947..aa722f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -499,46 +499,12 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "core-foundation" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" -dependencies = [ - "core-foundation-sys", - "libc", -] - [[package]] name = "core-foundation-sys" version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" -[[package]] -name = "core-graphics" -version = "0.23.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "970a29baf4110c26fedbc7f82107d42c23f7e88e404c4577ed73fe99ff85a212" -dependencies = [ - "bitflags 1.3.2", - "core-foundation", - "core-graphics-types", - "foreign-types", - "libc", -] - -[[package]] -name = "core-graphics-types" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf" -dependencies = [ - "bitflags 1.3.2", - "core-foundation", - "libc", -] - [[package]] name = "coreaudio-rs" version = "0.11.3" @@ -721,19 +687,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "enigo" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "802e4b2ae123615659085369b453cba87c5562e46ed8050a909fee18a9bc3157" -dependencies = [ - "core-graphics", - "libc", - "objc", - "pkg-config", - "windows 0.51.1", -] - [[package]] name = "enumflags2" version = "0.7.8" @@ -824,33 +777,6 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" -[[package]] -name = "foreign-types" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" -dependencies = [ - "foreign-types-macros", - "foreign-types-shared", -] - -[[package]] -name = "foreign-types-macros" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.48", -] - -[[package]] -name = "foreign-types-shared" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" - [[package]] name = "futures-core" version = "0.3.30" @@ -1149,6 +1075,12 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" +[[package]] +name = "lock" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "058cf9c2b0daa80dafc6d65b594ea04db8050522cfbf39f5d5aef2184f2a5a55" + [[package]] name = "lock_api" version = "0.4.11" @@ -1539,7 +1471,7 @@ dependencies = [ "clap", "crossterm", "dirs", - "enigo", + "lock", "notify-rust", "rodio", "serde", diff --git a/Cargo.toml b/Cargo.toml index d01dd77..776e528 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,8 +23,7 @@ dirs = "5.0.1" clap = { version = "4.4.16", features = ["derive", "cargo"] } rodio = "0.17.3" crossterm = "0.27.0" -enigo = "0.1.3" - +lock = "0.1.0" [profile.release] strip = true # Automatically strip symbols from the binary. diff --git a/src/app.rs b/src/app.rs index 1e29670..af8ff91 100644 --- a/src/app.rs +++ b/src/app.rs @@ -6,7 +6,7 @@ use crate::utils::*; use crossterm::cursor::{MoveToColumn, MoveToPreviousLine}; use crossterm::execute; use crossterm::terminal::{Clear, ClearType}; -use enigo::*; +use lock::FailureReason; use notify_rust::{Notification, Timeout}; use rodio::{Decoder, OutputStream, Sink}; use std::thread; @@ -128,25 +128,21 @@ pub fn stop_timer() -> AppResult<()> { Ok(()) } +/// Lock the screen. fn lock_screen() -> AppResult<()> { println!("Locking screen..."); - let mut enigo = Enigo::new(); - enigo.key_down(Key::Meta); // The meta key is meta key (also known as “windows”, “super”, and “command”) - - #[cfg(any(target_os = "linux", target_os = "windows"))] - enigo.key_click(Key::Layout('l')); - - #[cfg(target_os = "macos")] - { - enigo.key_down(Key::Control); - enigo.key_click(Key::Layout('q')); - enigo.key_up(Key::Control); - } - - enigo.key_up(Key::Meta); - - Ok(()) + lock::lock().map_err(|fail| { + AppError::new(match fail { + FailureReason::CannotExecute => "Cannot execute the lock command.", + FailureReason::LinuxCommandNotFound => { + "Linux command not found. The following commands are supported\ + \n- xdg-screensaver\ + \n- gnome-screensaver\ + \n- dm-tool" + } + }) + }) } /// Trigger the alarm sound and/or the system notification.