From 7bbc5183db4c72a33bf466815e31435103af04fd Mon Sep 17 00:00:00 2001 From: dignow Date: Tue, 18 Jul 2023 21:04:12 +0800 Subject: [PATCH] trackpad scale, mid commit Signed-off-by: dignow --- flutter/lib/models/input_model.dart | 11 ++++++++--- libs/hbb_common/protos/message.proto | 1 + src/client.rs | 13 ++++++++----- src/flutter_ffi.rs | 6 +++++- src/server/input_service.rs | 11 ++++++++++- src/ui_session_interface.rs | 26 ++++++++++++++++++++------ 6 files changed, 52 insertions(+), 16 deletions(-) diff --git a/flutter/lib/models/input_model.dart b/flutter/lib/models/input_model.dart index 1032bbe9d..44a39ffbc 100644 --- a/flutter/lib/models/input_model.dart +++ b/flutter/lib/models/input_model.dart @@ -55,6 +55,8 @@ class InputModel { final _trackpadSpeed = 0.06; var _trackpadScrollUnsent = Offset.zero; + var _lastScale = 1.0; + // mouse final isPhysicalMouse = false.obs; int _lastButtons = 0; @@ -272,7 +274,7 @@ class InputModel { sendMouse('down', button); } - void tapUp(MouseButtons button) { + void tapUp(MouseButtons button) { sendMouse('up', button); } @@ -337,12 +339,15 @@ class InputModel { } void onPointerPanZoomStart(PointerPanZoomStartEvent e) { + _lastScale = 1.0; _stopFling = true; } // https://docs.flutter.dev/release/breaking-changes/trackpad-gestures - // TODO(support zoom in/out) void onPointerPanZoomUpdate(PointerPanZoomUpdateEvent e) { + final scale = ((e.scale - _lastScale) * 100).toInt(); + _lastScale = e.scale; + final delta = e.panDelta; _trackpadLastDelta = delta; @@ -366,7 +371,7 @@ class InputModel { if (x != 0 || y != 0) { bind.sessionSendMouse( sessionId: sessionId, - msg: '{"type": "trackpad", "x": "$x", "y": "$y"}'); + msg: '{"type": "trackpad", "x": "$x", "y": "$y", "scale": "$scale"}'); } } diff --git a/libs/hbb_common/protos/message.proto b/libs/hbb_common/protos/message.proto index e67a5d3b1..20eb08ba5 100644 --- a/libs/hbb_common/protos/message.proto +++ b/libs/hbb_common/protos/message.proto @@ -116,6 +116,7 @@ message MouseEvent { sint32 x = 2; sint32 y = 3; repeated ControlKey modifiers = 4; + sint32 scale = 5; } enum KeyboardMode{ diff --git a/src/client.rs b/src/client.rs index 85047ee29..6937816a4 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1109,7 +1109,8 @@ impl LoginConfigHandler { self.remember = !config.password.is_empty(); self.config = config; let mut sid = rand::random(); - if sid == 0 { // you won the lottery + if sid == 0 { + // you won the lottery sid = 1; } self.session_id = sid; @@ -1928,6 +1929,7 @@ pub fn send_mouse( mask: i32, x: i32, y: i32, + scale: i32, alt: bool, ctrl: bool, shift: bool, @@ -1939,6 +1941,7 @@ pub fn send_mouse( mask, x, y, + scale, ..Default::default() }; if alt { @@ -1971,12 +1974,12 @@ pub fn send_mouse( /// /// * `interface` - The interface for sending data. fn activate_os(interface: &impl Interface) { - send_mouse(0, 0, 0, false, false, false, false, interface); + send_mouse(0, 0, 0, 0, false, false, false, false, interface); std::thread::sleep(Duration::from_millis(50)); - send_mouse(0, 3, 3, false, false, false, false, interface); + send_mouse(0, 3, 3, 0, false, false, false, false, interface); std::thread::sleep(Duration::from_millis(50)); - send_mouse(1 | 1 << 3, 0, 0, false, false, false, false, interface); - send_mouse(2 | 1 << 3, 0, 0, false, false, false, false, interface); + send_mouse(1 | 1 << 3, 0, 0, 0, false, false, false, false, interface); + send_mouse(2 | 1 << 3, 0, 0, 0, false, false, false, false, interface); /* let mut key_event = KeyEvent::new(); // do not use Esc, which has problem with Linux diff --git a/src/flutter_ffi.rs b/src/flutter_ffi.rs index c8de03862..e4564f144 100644 --- a/src/flutter_ffi.rs +++ b/src/flutter_ffi.rs @@ -1103,8 +1103,12 @@ pub fn session_send_mouse(session_id: SessionID, msg: String) { _ => 0, } << 3; } + let scale = m + .get("scale") + .map(|x| x.parse::().unwrap_or(0)) + .unwrap_or(0); if let Some(session) = SESSIONS.read().unwrap().get(&session_id) { - session.send_mouse(mask, x, y, alt, ctrl, shift, command); + session.send_mouse(mask, x, y, scale, alt, ctrl, shift, command); } } } diff --git a/src/server/input_service.rs b/src/server/input_service.rs index e71d166f5..476aa03b2 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -759,7 +759,7 @@ pub fn handle_mouse_(evt: &MouseEvent, conn: i32) { let mut en = ENIGO.lock().unwrap(); #[cfg(not(target_os = "macos"))] let mut to_release = Vec::new(); - if evt_type == 1 { + if evt_type == MOUSE_TYPE_DOWN { fix_modifiers(&evt.modifiers[..], &mut en, 0); #[cfg(target_os = "macos")] en.reset_flag(); @@ -883,6 +883,15 @@ pub fn handle_mouse_(evt: &MouseEvent, conn: i32) { for key in to_release { en.key_up(key.clone()); } + handle_mouse_scale(evt.scale); +} + +#[cfg(target_os = "windows")] +fn handle_mouse_scale(scale: i32) { + let mut en = ENIGO.lock().unwrap(); + en.key_down(Key::Control); + en.mouse_scroll_y(scale); + en.key_up(Key::Control); } pub fn is_enter(evt: &KeyEvent) -> bool { diff --git a/src/ui_session_interface.rs b/src/ui_session_interface.rs index ed6052d39..773eee374 100644 --- a/src/ui_session_interface.rs +++ b/src/ui_session_interface.rs @@ -1,3 +1,4 @@ +use crate::input::{MOUSE_BUTTON_LEFT, MOUSE_TYPE_DOWN, MOUSE_TYPE_UP}; #[cfg(not(any(target_os = "android", target_os = "ios")))] use std::{collections::HashMap, sync::atomic::AtomicBool}; use std::{ @@ -50,7 +51,7 @@ const CHANGE_RESOLUTION_VALID_TIMEOUT_SECS: u64 = 15; #[derive(Clone, Default)] pub struct Session { pub session_id: SessionID, // different from the one in LoginConfigHandler, used for flutter UI message pass - pub id: String, // peer id + pub id: String, // peer id pub password: String, pub args: Vec, pub lc: Arc>, @@ -306,8 +307,7 @@ impl Session { } pub fn get_audit_server(&self, typ: String) -> String { - if LocalConfig::get_option("access_token").is_empty() - { + if LocalConfig::get_option("access_token").is_empty() { return "".to_owned(); } crate::get_audit_server( @@ -695,6 +695,7 @@ impl Session { mask: i32, x: i32, y: i32, + scale: i32, alt: bool, ctrl: bool, shift: bool, @@ -713,15 +714,28 @@ impl Session { let (alt, ctrl, shift, command) = keyboard::client::get_modifiers_state(alt, ctrl, shift, command); - send_mouse(mask, x, y, alt, ctrl, shift, command, self); + send_mouse(mask, x, y, scale, alt, ctrl, shift, command, self); // on macos, ctrl + left button down = right button down, up won't emit, so we need to // emit up myself if peer is not macos // to-do: how about ctrl + left from win to macos if cfg!(target_os = "macos") { let buttons = mask >> 3; let evt_type = mask & 0x7; - if buttons == 1 && evt_type == 1 && ctrl && self.peer_platform() != "Mac OS" { - self.send_mouse((1 << 3 | 2) as _, x, y, alt, ctrl, shift, command); + if buttons == MOUSE_BUTTON_LEFT + && evt_type == MOUSE_TYPE_DOWN + && ctrl + && self.peer_platform() != "Mac OS" + { + self.send_mouse( + (MOUSE_BUTTON_LEFT << 3 | MOUSE_TYPE_UP) as _, + x, + y, + scale, + alt, + ctrl, + shift, + command, + ); } } }