Refactor get led state

This commit is contained in:
asur4s 2022-09-05 08:07:13 -04:00
parent a105aff2aa
commit 72d357e14b
2 changed files with 27 additions and 37 deletions

View File

@ -1,5 +1,6 @@
use super::{xdo::EnigoXdo};
use crate::{Key, KeyboardControllable, MouseButton, MouseControllable};
use std::io::Read;
/// The main struct for handling the event emitting
// #[derive(Default)]
@ -111,6 +112,30 @@ impl MouseControllable for Enigo {
}
}
fn get_led_state(key: Key) -> bool{
let led_file = match key{
Key::CapsLock => {
"/sys/class/leds/input1::capslock/brightness"
}
Key::NumLock => {
"/sys/class/leds/input1::numlock/brightness"
}
_ => {
return false;
}
};
let status = if let Ok(mut file) = std::fs::File::open(&led_file) {
let mut content = String::new();
file.read_to_string(&mut content).ok();
let status = content.trim_end().to_string().parse::<i32>().unwrap_or(0);
status
}else{
0
};
status == 1
}
impl KeyboardControllable for Enigo {
fn get_key_state(&mut self, key: Key) -> bool {
if self.is_x11 {
@ -119,7 +144,7 @@ impl KeyboardControllable for Enigo {
if let Some(keyboard) = &mut self.uinput_keyboard {
keyboard.get_key_state(key)
} else {
false
get_led_state(key)
}
}
}

View File

@ -330,7 +330,6 @@ impl<T: InvokeUi> Session<T> {
if get_key_state(enigo::Key::NumLock) {
key_event.modifiers.push(ControlKey::NumLock.into());
}
log::info!("{:?}", get_key_state(enigo::Key::NumLock));
self.send_key_event(key_event, KeyboardMode::Map);
}
@ -416,20 +415,11 @@ impl<T: InvokeUi> Session<T> {
{
key_event.modifiers.push(ControlKey::Meta.into());
}
#[cfg(target_os = "linux")]
if get_led_state(enigo::Key::CapsLock){
key_event.modifiers.push(ControlKey::CapsLock.into());
}
#[cfg(not(target_os = "linux"))]
if get_key_state(enigo::Key::CapsLock) {
key_event.modifiers.push(ControlKey::CapsLock.into());
}
if self.peer_platform() != "Mac OS" {
#[cfg(target_os = "linux")]
if get_led_state(enigo::Key::NumLock){
key_event.modifiers.push(ControlKey::NumLock.into());
}
#[cfg(not(target_os = "linux"))]
if get_key_state(enigo::Key::NumLock) {
key_event.modifiers.push(ControlKey::NumLock.into());
}
@ -1371,29 +1361,4 @@ async fn start_one_port_forward<T: InvokeUi>(
async fn send_note(url: String, id: String, conn_id: i32, note: String) {
let body = serde_json::json!({ "id": id, "Id": conn_id, "note": note });
allow_err!(crate::post_request(url, body.to_string(), "").await);
}
fn get_led_state(key: enigo::Key) -> bool{
let led_file = match key{
enigo::Key::CapsLock => {
"/sys/class/leds/input1::capslock/brightness"
}
enigo::Key::NumLock => {
"/sys/class/leds/input1::numlock/brightness"
}
_ => {
return false;
}
};
let status = if let Ok(mut file) = std::fs::File::open(&led_file) {
let mut content = String::new();
file.read_to_string(&mut content).ok();
let status = content.trim_end().to_string().parse::<i32>().unwrap_or(0);
status
}else{
0
};
log::info!("get led state: {}", status);
status == 1
}