From 98bb47a81ddf751b16d87ccaa4f6da77ff4eda67 Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 14 Nov 2022 15:05:44 +0800 Subject: [PATCH] fix cursor lost control sometime && refactor some Camel-Case flutter Signed-off-by: fufesou --- flutter/lib/models/input_model.dart | 22 +++++++++++----------- flutter/lib/models/model.dart | 14 +++++++------- src/server/input_service.rs | 20 ++++++++++++++++---- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/flutter/lib/models/input_model.dart b/flutter/lib/models/input_model.dart index 30a01cda7..83171514d 100644 --- a/flutter/lib/models/input_model.dart +++ b/flutter/lib/models/input_model.dart @@ -42,7 +42,7 @@ class InputModel { // mouse final isPhysicalMouse = false.obs; int _lastMouseDownButtons = 0; - Offset last_mouse_pos = Offset.zero; + Offset lastMousePos = Offset.zero; get id => parent.target?.id ?? ""; @@ -308,23 +308,23 @@ class InputModel { double y = max(0.0, evt['y']); final cursorModel = parent.target!.cursorModel; - if (cursorModel.is_peer_control_protected) { - last_mouse_pos = ui.Offset(x, y); + if (cursorModel.isPeerControlProtected) { + lastMousePos = ui.Offset(x, y); return; } - if (!cursorModel.got_mouse_control) { - bool self_get_control = - (x - last_mouse_pos.dx).abs() > kMouseControlDistance || - (y - last_mouse_pos.dy).abs() > kMouseControlDistance; - if (self_get_control) { - cursorModel.got_mouse_control = true; + if (!cursorModel.gotMouseControl) { + bool selfGetControl = + (x - lastMousePos.dx).abs() > kMouseControlDistance || + (y - lastMousePos.dy).abs() > kMouseControlDistance; + if (selfGetControl) { + cursorModel.gotMouseControl = true; } else { - last_mouse_pos = ui.Offset(x, y); + lastMousePos = ui.Offset(x, y); return; } } - last_mouse_pos = ui.Offset(x, y); + lastMousePos = ui.Offset(x, y); var type = ''; var isMove = false; diff --git a/flutter/lib/models/model.dart b/flutter/lib/models/model.dart index cf4c63952..d7fc414d5 100644 --- a/flutter/lib/models/model.dart +++ b/flutter/lib/models/model.dart @@ -753,8 +753,8 @@ class CursorModel with ChangeNotifier { double _hoty = 0; double _displayOriginX = 0; double _displayOriginY = 0; - bool got_mouse_control = true; - DateTime _last_peer_mouse = DateTime.now() + bool gotMouseControl = true; + DateTime _lastPeerMouse = DateTime.now() .subtract(Duration(milliseconds: 2 * kMouseControlTimeoutMSec)); String id = ''; WeakReference parent; @@ -772,8 +772,8 @@ class CursorModel with ChangeNotifier { double get hotx => _hotx; double get hoty => _hoty; - bool get is_peer_control_protected => - DateTime.now().difference(_last_peer_mouse).inMilliseconds < + bool get isPeerControlProtected => + DateTime.now().difference(_lastPeerMouse).inMilliseconds < kMouseControlTimeoutMSec; CursorModel(this.parent) { @@ -806,7 +806,7 @@ class CursorModel with ChangeNotifier { } else { data = Uint8List.fromList(img2.encodePng(defaultCursorImage!)); } - + _defaultCache = CursorData( peerId: id, id: _defaultCacheId, @@ -1012,8 +1012,8 @@ class CursorModel with ChangeNotifier { /// Update the cursor position. updateCursorPosition(Map evt, String id) async { - got_mouse_control = false; - _last_peer_mouse = DateTime.now(); + gotMouseControl = false; + _lastPeerMouse = DateTime.now(); _x = double.parse(evt['x']); _y = double.parse(evt['y']); try { diff --git a/src/server/input_service.rs b/src/server/input_service.rs index 780c1106a..653187bc3 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -209,6 +209,7 @@ lazy_static::lazy_static! { static EXITING: AtomicBool = AtomicBool::new(false); const MOUSE_MOVE_PROTECTION_TIMEOUT: Duration = Duration::from_millis(1_000); +// Actual diff of (x,y) is (1,1) here. But 5 may be tolerant. const MOUSE_ACTIVE_DISTANCE: i32 = 5; // mac key input must be run in main thread, otherwise crash on >= osx 10.15 @@ -406,14 +407,25 @@ fn active_mouse_(conn: i32) -> bool { return false; } + let in_actived_dist = |a: i32, b: i32| -> bool { (a - b).abs() < MOUSE_ACTIVE_DISTANCE }; + // check if input is in valid range match crate::get_cursor_pos() { Some((x, y)) => { - let can_active = (last_input.x - x).abs() < MOUSE_ACTIVE_DISTANCE - && (last_input.y - y).abs() < MOUSE_ACTIVE_DISTANCE; + let mut can_active = + in_actived_dist(last_input.x, x) && in_actived_dist(last_input.y, y); if !can_active { - last_input.x = -MOUSE_ACTIVE_DISTANCE * 2; - last_input.y = -MOUSE_ACTIVE_DISTANCE * 2; + // Try agin + // No need to care about sleep here. It's not a common case. + std::thread::sleep(std::time::Duration::from_micros(10)); + if let Some((x2, y2)) = crate::get_cursor_pos() { + can_active = + in_actived_dist(last_input.x, x2) && in_actived_dist(last_input.y, y2); + } + } + if !can_active { + last_input.x = INVALID_CURSOR_POS / 2; + last_input.y = INVALID_CURSOR_POS / 2; } can_active }