feat: mobile map mode (#9717)

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou 2024-10-23 09:28:39 +08:00 committed by GitHub
parent 2cdaca0fa3
commit cc6f919080
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 64 additions and 44 deletions

2
Cargo.lock generated
View File

@ -5205,7 +5205,7 @@ dependencies = [
[[package]] [[package]]
name = "rdev" name = "rdev"
version = "0.5.0-2" version = "0.5.0-2"
source = "git+https://github.com/rustdesk-org/rdev#d4c1759926d693ba269e2cb8cf9f87b13e424e4e" source = "git+https://github.com/rustdesk-org/rdev#961d25cc00c6b3ef80f444e6a7bed9872e2c35ea"
dependencies = [ dependencies = [
"cocoa 0.24.1", "cocoa 0.24.1",
"core-foundation 0.9.4", "core-foundation 0.9.4",

View File

@ -544,8 +544,7 @@ class InputModel {
handleKeyDownEventModifiers(e); handleKeyDownEventModifiers(e);
} }
// * Currently mobile does not enable map mode if (isMobile || (isDesktop || isWebDesktop) && keyboardMode == kKeyMapMode) {
if ((isDesktop || isWebDesktop) && keyboardMode == kKeyMapMode) {
// FIXME: e.character is wrong for dead keys, eg: ^ in de // FIXME: e.character is wrong for dead keys, eg: ^ in de
newKeyboardMode( newKeyboardMode(
e.character ?? '', e.character ?? '',

View File

@ -222,6 +222,10 @@ pub fn session_get_enable_trusted_devices(session_id: SessionID) -> SyncReturn<b
pub fn session_close(session_id: SessionID) { pub fn session_close(session_id: SessionID) {
if let Some(session) = sessions::remove_session_by_session_id(&session_id) { if let Some(session) = sessions::remove_session_by_session_id(&session_id) {
// `release_remote_keys` is not required for mobile platforms in common cases.
// But we still call it to make the code more stable.
#[cfg(any(target_os = "android", target_os = "ios"))]
crate::keyboard::release_remote_keys("map");
session.close_event_stream(session_id); session.close_event_stream(session_id);
session.close(); session.close();
} }

View File

@ -4,6 +4,7 @@ use crate::flutter;
use crate::platform::windows::{get_char_from_vk, get_unicode_from_vk}; use crate::platform::windows::{get_char_from_vk, get_unicode_from_vk};
#[cfg(not(any(feature = "flutter", feature = "cli")))] #[cfg(not(any(feature = "flutter", feature = "cli")))]
use crate::ui::CUR_SESSION; use crate::ui::CUR_SESSION;
use crate::ui_session_interface::{InvokeUiSession, Session};
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
use crate::{client::get_key_state, common::GrabState}; use crate::{client::get_key_state, common::GrabState};
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
@ -105,16 +106,31 @@ pub mod client {
pub fn process_event(keyboard_mode: &str, event: &Event, lock_modes: Option<i32>) { pub fn process_event(keyboard_mode: &str, event: &Event, lock_modes: Option<i32>) {
let keyboard_mode = get_keyboard_mode_enum(keyboard_mode); let keyboard_mode = get_keyboard_mode_enum(keyboard_mode);
if is_long_press(&event) { if is_long_press(&event) {
return; return;
} }
let peer = get_peer_platform().to_lowercase();
for key_event in event_to_key_events(&event, keyboard_mode, lock_modes) { for key_event in event_to_key_events(peer, &event, keyboard_mode, lock_modes) {
send_key_event(&key_event); send_key_event(&key_event);
} }
} }
pub fn process_event_with_session<T: InvokeUiSession>(
keyboard_mode: &str,
event: &Event,
lock_modes: Option<i32>,
session: &Session<T>,
) {
let keyboard_mode = get_keyboard_mode_enum(keyboard_mode);
if is_long_press(&event) {
return;
}
let peer = session.peer_platform().to_lowercase();
for key_event in event_to_key_events(peer, &event, keyboard_mode, lock_modes) {
session.send_key_event(&key_event);
}
}
pub fn get_modifiers_state( pub fn get_modifiers_state(
alt: bool, alt: bool,
ctrl: bool, ctrl: bool,
@ -171,6 +187,7 @@ pub mod client {
} }
} }
#[cfg(target_os = "android")]
pub fn map_key_to_control_key(key: &rdev::Key) -> Option<ControlKey> { pub fn map_key_to_control_key(key: &rdev::Key) -> Option<ControlKey> {
match key { match key {
Key::Alt => Some(ControlKey::Alt), Key::Alt => Some(ControlKey::Alt),
@ -358,7 +375,6 @@ pub fn is_long_press(event: &Event) -> bool {
return false; return false;
} }
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn release_remote_keys(keyboard_mode: &str) { pub fn release_remote_keys(keyboard_mode: &str) {
// todo!: client quit suddenly, how to release keys? // todo!: client quit suddenly, how to release keys?
let to_release = TO_RELEASE.lock().unwrap().clone(); let to_release = TO_RELEASE.lock().unwrap().clone();
@ -387,7 +403,6 @@ pub fn get_keyboard_mode_enum(keyboard_mode: &str) -> KeyboardMode {
} }
#[inline] #[inline]
#[cfg(not(any(target_os = "ios")))]
pub fn is_modifier(key: &rdev::Key) -> bool { pub fn is_modifier(key: &rdev::Key) -> bool {
matches!( matches!(
key, key,
@ -403,7 +418,6 @@ pub fn is_modifier(key: &rdev::Key) -> bool {
} }
#[inline] #[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn is_numpad_rdev_key(key: &rdev::Key) -> bool { pub fn is_numpad_rdev_key(key: &rdev::Key) -> bool {
matches!( matches!(
key, key,
@ -426,7 +440,6 @@ pub fn is_numpad_rdev_key(key: &rdev::Key) -> bool {
} }
#[inline] #[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn is_letter_rdev_key(key: &rdev::Key) -> bool { pub fn is_letter_rdev_key(key: &rdev::Key) -> bool {
matches!( matches!(
key, key,
@ -462,7 +475,6 @@ pub fn is_letter_rdev_key(key: &rdev::Key) -> bool {
// https://github.com/rustdesk/rustdesk/issues/8599 // https://github.com/rustdesk/rustdesk/issues/8599
// We just add these keys as letter keys. // We just add these keys as letter keys.
#[inline] #[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn is_letter_rdev_key_ex(key: &rdev::Key) -> bool { pub fn is_letter_rdev_key_ex(key: &rdev::Key) -> bool {
matches!( matches!(
key, key,
@ -471,7 +483,6 @@ pub fn is_letter_rdev_key_ex(key: &rdev::Key) -> bool {
} }
#[inline] #[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn is_numpad_key(event: &Event) -> bool { fn is_numpad_key(event: &Event) -> bool {
matches!(event.event_type, EventType::KeyPress(key) | EventType::KeyRelease(key) if is_numpad_rdev_key(&key)) matches!(event.event_type, EventType::KeyPress(key) | EventType::KeyRelease(key) if is_numpad_rdev_key(&key))
} }
@ -479,12 +490,10 @@ fn is_numpad_key(event: &Event) -> bool {
// Check is letter key for lock modes. // Check is letter key for lock modes.
// Only letter keys need to check and send Lock key state. // Only letter keys need to check and send Lock key state.
#[inline] #[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn is_letter_key_4_lock_modes(event: &Event) -> bool { fn is_letter_key_4_lock_modes(event: &Event) -> bool {
matches!(event.event_type, EventType::KeyPress(key) | EventType::KeyRelease(key) if (is_letter_rdev_key(&key) || is_letter_rdev_key_ex(&key))) matches!(event.event_type, EventType::KeyPress(key) | EventType::KeyRelease(key) if (is_letter_rdev_key(&key) || is_letter_rdev_key_ex(&key)))
} }
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn parse_add_lock_modes_modifiers( fn parse_add_lock_modes_modifiers(
key_event: &mut KeyEvent, key_event: &mut KeyEvent,
lock_modes: i32, lock_modes: i32,
@ -555,10 +564,13 @@ fn update_modifiers_state(event: &Event) {
} }
pub fn event_to_key_events( pub fn event_to_key_events(
mut peer: String,
event: &Event, event: &Event,
keyboard_mode: KeyboardMode, keyboard_mode: KeyboardMode,
_lock_modes: Option<i32>, _lock_modes: Option<i32>,
) -> Vec<KeyEvent> { ) -> Vec<KeyEvent> {
peer.retain(|c| !c.is_whitespace());
let mut key_event = KeyEvent::new(); let mut key_event = KeyEvent::new();
update_modifiers_state(event); update_modifiers_state(event);
@ -572,16 +584,9 @@ pub fn event_to_key_events(
_ => {} _ => {}
} }
let mut peer = get_peer_platform().to_lowercase();
peer.retain(|c| !c.is_whitespace());
key_event.mode = keyboard_mode.into(); key_event.mode = keyboard_mode.into();
#[cfg(not(any(target_os = "android", target_os = "ios")))] let mut key_events = match keyboard_mode {
let mut key_events;
#[cfg(any(target_os = "android", target_os = "ios"))]
let key_events;
key_events = match keyboard_mode {
KeyboardMode::Map => map_keyboard_mode(peer.as_str(), event, key_event), KeyboardMode::Map => map_keyboard_mode(peer.as_str(), event, key_event),
KeyboardMode::Translate => translate_keyboard_mode(peer.as_str(), event, key_event), KeyboardMode::Translate => translate_keyboard_mode(peer.as_str(), event, key_event),
_ => { _ => {
@ -596,15 +601,14 @@ pub fn event_to_key_events(
} }
}; };
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let is_numpad_key = is_numpad_key(&event); let is_numpad_key = is_numpad_key(&event);
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if keyboard_mode != KeyboardMode::Translate || is_numpad_key { if keyboard_mode != KeyboardMode::Translate || is_numpad_key {
let is_letter_key = is_letter_key_4_lock_modes(&event); let is_letter_key = is_letter_key_4_lock_modes(&event);
for key_event in &mut key_events { for key_event in &mut key_events {
if let Some(lock_modes) = _lock_modes { if let Some(lock_modes) = _lock_modes {
parse_add_lock_modes_modifiers(key_event, lock_modes, is_numpad_key, is_letter_key); parse_add_lock_modes_modifiers(key_event, lock_modes, is_numpad_key, is_letter_key);
} else { } else {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
add_lock_modes_modifiers(key_event, is_numpad_key, is_letter_key); add_lock_modes_modifiers(key_event, is_numpad_key, is_letter_key);
} }
} }
@ -617,6 +621,7 @@ pub fn send_key_event(key_event: &KeyEvent) {
if let Some(session) = CUR_SESSION.lock().unwrap().as_ref() { if let Some(session) = CUR_SESSION.lock().unwrap().as_ref() {
session.send_key_event(key_event); session.send_key_event(key_event);
} }
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
if let Some(session) = flutter::get_cur_session() { if let Some(session) = flutter::get_cur_session() {
session.send_key_event(key_event); session.send_key_event(key_event);
@ -936,8 +941,19 @@ fn _map_keyboard_mode(_peer: &str, event: &Event, mut key_event: KeyEvent) -> Op
_ => event.position_code as _, _ => event.position_code as _,
}; };
#[cfg(any(target_os = "android", target_os = "ios"))] #[cfg(any(target_os = "android", target_os = "ios"))]
let keycode = 0; let keycode = match _peer {
OS_LOWER_WINDOWS => rdev::usb_hid_code_to_win_scancode(event.usb_hid as _)?,
OS_LOWER_LINUX => rdev::usb_hid_code_to_linux_code(event.usb_hid as _)?,
OS_LOWER_MACOS => {
if hbb_common::config::LocalConfig::get_kb_layout_type() == "ISO" {
rdev::usb_hid_code_to_macos_iso_code(event.usb_hid as _)?
} else {
rdev::usb_hid_code_to_macos_code(event.usb_hid as _)?
}
}
OS_LOWER_ANDROID => rdev::usb_hid_code_to_android_key_code(event.usb_hid as _)?,
_ => event.usb_hid as _,
};
key_event.set_chr(keycode as _); key_event.set_chr(keycode as _);
Some(key_event) Some(key_event)
} }

View File

@ -861,24 +861,13 @@ impl<T: InvokeUiSession> Session<T> {
platform_code, platform_code,
position_code: position_code as _, position_code: position_code as _,
event_type, event_type,
usb_hid: 0,
#[cfg(any(target_os = "windows", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "macos"))]
extra_data: 0, extra_data: 0,
}; };
keyboard::client::process_event(keyboard_mode, &event, Some(lock_modes)); keyboard::client::process_event_with_session(keyboard_mode, &event, Some(lock_modes), self);
} }
#[cfg(any(target_os = "ios"))]
pub fn handle_flutter_key_event(
&self,
_keyboard_mode: &str,
_character: &str,
_usb_hid: i32,
_lock_modes: i32,
_down_or_up: bool,
) {
}
#[cfg(not(any(target_os = "ios")))]
pub fn handle_flutter_key_event( pub fn handle_flutter_key_event(
&self, &self,
keyboard_mode: &str, keyboard_mode: &str,
@ -900,7 +889,6 @@ impl<T: InvokeUiSession> Session<T> {
} }
} }
#[cfg(not(any(target_os = "ios")))]
fn _handle_key_flutter_simulation( fn _handle_key_flutter_simulation(
&self, &self,
_keyboard_mode: &str, _keyboard_mode: &str,
@ -925,7 +913,6 @@ impl<T: InvokeUiSession> Session<T> {
self.send_key_event(&key_event); self.send_key_event(&key_event);
} }
#[cfg(not(any(target_os = "ios")))]
fn _handle_key_non_flutter_simulation( fn _handle_key_non_flutter_simulation(
&self, &self,
keyboard_mode: &str, keyboard_mode: &str,
@ -936,14 +923,24 @@ impl<T: InvokeUiSession> Session<T> {
) { ) {
let key = rdev::usb_hid_key_from_code(usb_hid as _); let key = rdev::usb_hid_key_from_code(usb_hid as _);
#[cfg(any(target_os = "android", target_os = "ios"))]
let position_code: KeyCode = 0;
#[cfg(any(target_os = "android", target_os = "ios"))]
let platform_code: KeyCode = 0;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let platform_code: u32 = rdev::win_code_from_key(key).unwrap_or(0); let platform_code: u32 = rdev::win_code_from_key(key).unwrap_or(0);
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let position_code: KeyCode = rdev::win_scancode_from_key(key).unwrap_or(0) as _; let position_code: KeyCode = rdev::win_scancode_from_key(key).unwrap_or(0) as _;
#[cfg(not(target_os = "windows"))] #[cfg(not(any(target_os = "windows", target_os = "android", target_os = "ios")))]
let position_code: KeyCode = rdev::code_from_key(key).unwrap_or(0) as _; let position_code: KeyCode = rdev::code_from_key(key).unwrap_or(0) as _;
#[cfg(not(any(target_os = "windows", target_os = "linux")))] #[cfg(not(any(
target_os = "windows",
target_os = "android",
target_os = "ios",
target_os = "linux"
)))]
let platform_code: u32 = position_code as _; let platform_code: u32 = position_code as _;
// For translate mode. // For translate mode.
// We need to set the platform code (keysym) if is AltGr. // We need to set the platform code (keysym) if is AltGr.
@ -972,10 +969,14 @@ impl<T: InvokeUiSession> Session<T> {
platform_code, platform_code,
position_code: position_code as _, position_code: position_code as _,
event_type, event_type,
#[cfg(any(target_os = "android", target_os = "ios"))]
usb_hid: usb_hid as _,
#[cfg(not(any(target_os = "android", target_os = "ios")))]
usb_hid: 0,
#[cfg(any(target_os = "windows", target_os = "macos"))] #[cfg(any(target_os = "windows", target_os = "macos"))]
extra_data: 0, extra_data: 0,
}; };
keyboard::client::process_event(keyboard_mode, &event, Some(lock_modes)); keyboard::client::process_event_with_session(keyboard_mode, &event, Some(lock_modes), self);
} }
// flutter only TODO new input // flutter only TODO new input