From 02b4d7f1d9dc755d4e55f403479dbb76b4a82f5c Mon Sep 17 00:00:00 2001 From: Asura Date: Tue, 19 Jul 2022 15:43:13 +0800 Subject: [PATCH] Refactor keyboard mode by enum --- libs/hbb_common/protos/message.proto | 9 +++++++- src/server/connection.rs | 4 ++-- src/server/input_service.rs | 10 ++++---- src/ui/remote.rs | 34 +++++++++++++++++----------- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/libs/hbb_common/protos/message.proto b/libs/hbb_common/protos/message.proto index 645930eb4..2282bf43a 100644 --- a/libs/hbb_common/protos/message.proto +++ b/libs/hbb_common/protos/message.proto @@ -97,6 +97,13 @@ message MouseEvent { repeated ControlKey modifiers = 4; } +enum KeyboardMode{ + Legacy = 0; + Map = 1; + Translate = 2; + Auto = 3; +} + enum ControlKey { Unknown = 0; Alt = 1; @@ -190,7 +197,7 @@ message KeyEvent { string seq = 6; } repeated ControlKey modifiers = 8; - uint32 mode = 9; + KeyboardMode mode = 9; } message CursorData { diff --git a/src/server/connection.rs b/src/server/connection.rs index 854191c95..fdeaac106 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -420,11 +420,11 @@ impl Connection { } MessageInput::Key((mut msg, press)) => { // todo: press and down have similar meanings. - if press && msg.mode == 0 { + if press && msg.mode.unwrap() == KeyboardMode::Legacy { msg.down = true; } handle_key(&msg); - if press && msg.mode == 0 { + if press && msg.mode.unwrap() == KeyboardMode::Legacy { msg.down = false; handle_key(&msg); } diff --git a/src/server/input_service.rs b/src/server/input_service.rs index 4563ef465..02f50abff 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -450,7 +450,7 @@ pub fn lock_screen() { key_event.down = true; key_event.set_chr('l' as _); key_event.modifiers.push(ControlKey::Meta.into()); - key_event.mode = 0; + key_event.mode = KeyboardMode::Legacy; handle_key(&key_event); key_event.down = false; handle_key(&key_event); @@ -463,7 +463,7 @@ pub fn lock_screen() { key_event.set_chr('q' as _); key_event.modifiers.push(ControlKey::Meta.into()); key_event.modifiers.push(ControlKey::Control.into()); - key_event.mode = 0; + key_event.mode = KeyboardMode::Legacy; handle_key(&key_event); key_event.down = false; handle_key(&key_event); @@ -787,11 +787,11 @@ fn handle_key_(evt: &KeyEvent) { return; } - match evt.mode { - 0 => { + match evt.mode.unwrap() { + KeyboardMode::Legacy => { legacy_keyboard_map(evt); } - 1 => { + KeyboardMode::Map => { map_keyboard_map(evt); } _ => { diff --git a/src/ui/remote.rs b/src/ui/remote.rs index 0b96b3c26..4deaf05d8 100644 --- a/src/ui/remote.rs +++ b/src/ui/remote.rs @@ -41,6 +41,7 @@ use hbb_common::{ time::{self, Duration, Instant, Interval}, }, Stream, + protobuf::ProtobufEnumOrUnknown, }; use hbb_common::{config::TransferSerde, fs::TransferJobMeta}; use rdev::{Event, EventType::*, Key as RdevKey}; @@ -921,14 +922,14 @@ impl Handler { key_event.set_control_key(ControlKey::CtrlAltDel); // todo key_event.down = true; - self.send_key_event(key_event, 0); + self.send_key_event(key_event, KeyboardMode::Legacy); } else { let mut key_event = KeyEvent::new(); key_event.set_control_key(ControlKey::Delete); self.legacy_modifiers(&mut key_event, true, true, false, false); // todo key_event.press = true; - self.send_key_event(key_event, 0); + self.send_key_event(key_event, KeyboardMode::Legacy); } } @@ -937,7 +938,7 @@ impl Handler { key_event.set_control_key(ControlKey::LockScreen); // todo key_event.down = true; - self.send_key_event(key_event, 0); + self.send_key_event(key_event, KeyboardMode::Legacy); } fn transfer_file(&mut self) { @@ -956,9 +957,9 @@ impl Handler { } } - fn send_key_event(&mut self, mut evt: KeyEvent, keyboard_mode: u32) { - // mode: map(1), translate(2), legacy(3), auto(4) - evt.mode = keyboard_mode; + fn send_key_event(&mut self, mut evt: KeyEvent, keyboard_mode: KeyboardMode) { + // mode: legacy(0), map(1), translate(2), auto(3) + evt.mode = ProtobufEnumOrUnknown::new(keyboard_mode); let mut msg_out = Message::new(); msg_out.set_key_event(evt); log::info!("{:?}", msg_out); @@ -1012,7 +1013,7 @@ impl Handler { key_event.modifiers.push(ControlKey::NumLock.into()); } - self.send_key_event(key_event, 1); + self.send_key_event(key_event, KeyboardMode::Map); } // fn translate_keyboard_mode(&mut self, down_or_up: bool, key: RdevKey) { @@ -1269,15 +1270,22 @@ impl Handler { if down_or_up == true { key_event.down = true; } - dbg!(&key_event); - self.send_key_event(key_event, 0) + self.send_key_event(key_event, KeyboardMode::Legacy) } fn key_down_or_up(&mut self, down_or_up: bool, key: RdevKey, evt: Event) { // Call different functions according to keyboard mode. - let mode = std::env::var("KEYBOARD_MOAD").unwrap_or(String::from("legacy")); - match mode.as_str() { - "map" => { + let mode = match std::env::var("KEYBOARD_MOAD") + .unwrap_or(String::from("legacy")) + .as_str() + { + "map" => KeyboardMode::Map, + "legacy" => KeyboardMode::Legacy, + _ => KeyboardMode::Legacy, + }; + + match mode { + KeyboardMode::Map => { if down_or_up == true { TO_RELEASE.lock().unwrap().insert(key); } else { @@ -1285,7 +1293,7 @@ impl Handler { } self.map_keyboard_mode(down_or_up, key); } - "legacy" => self.legacy_keyboard_mode(down_or_up, key, evt), + KeyboardMode::Legacy => self.legacy_keyboard_mode(down_or_up, key, evt), _ => self.legacy_keyboard_mode(down_or_up, key, evt), } }