mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-05-09 05:36:28 +02:00
Compare commits
4 Commits
accept_win
...
1e6a3dc644
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e6a3dc644 | ||
|
|
5b7ad339b8 | ||
|
|
7308c448f1 | ||
|
|
c8ba99d1a1 |
@@ -62,7 +62,13 @@ class AudioRecordHandle(private var context: Context, private var isVideoStart:
|
||||
return false
|
||||
}
|
||||
}
|
||||
audioRecorder = builder.build()
|
||||
val recorder = try {
|
||||
builder.build()
|
||||
} catch (e: Exception) {
|
||||
Log.e(logTag, "createAudioRecorder failed", e)
|
||||
return false
|
||||
}
|
||||
audioRecorder = recorder
|
||||
Log.d(logTag, "createAudioRecorder done,minBufferSize:$minBufferSize")
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -532,7 +532,9 @@ class _RawTouchGestureDetectorRegionState
|
||||
// Official
|
||||
TapGestureRecognizer:
|
||||
GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
|
||||
() => TapGestureRecognizer(), (instance) {
|
||||
() => TapGestureRecognizer(
|
||||
supportedDevices: kTouchBasedDeviceKinds,
|
||||
), (instance) {
|
||||
instance
|
||||
..onTapDown = onTapDown
|
||||
..onTapUp = onTapUp
|
||||
@@ -540,14 +542,18 @@ class _RawTouchGestureDetectorRegionState
|
||||
}),
|
||||
DoubleTapGestureRecognizer:
|
||||
GestureRecognizerFactoryWithHandlers<DoubleTapGestureRecognizer>(
|
||||
() => DoubleTapGestureRecognizer(), (instance) {
|
||||
() => DoubleTapGestureRecognizer(
|
||||
supportedDevices: kTouchBasedDeviceKinds,
|
||||
), (instance) {
|
||||
instance
|
||||
..onDoubleTapDown = onDoubleTapDown
|
||||
..onDoubleTap = onDoubleTap;
|
||||
}),
|
||||
LongPressGestureRecognizer:
|
||||
GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
|
||||
() => LongPressGestureRecognizer(), (instance) {
|
||||
() => LongPressGestureRecognizer(
|
||||
supportedDevices: kTouchBasedDeviceKinds,
|
||||
), (instance) {
|
||||
instance
|
||||
..onLongPressDown = onLongPressDown
|
||||
..onLongPressUp = onLongPressUp
|
||||
@@ -557,7 +563,9 @@ class _RawTouchGestureDetectorRegionState
|
||||
// Customized
|
||||
HoldTapMoveGestureRecognizer:
|
||||
GestureRecognizerFactoryWithHandlers<HoldTapMoveGestureRecognizer>(
|
||||
() => HoldTapMoveGestureRecognizer(),
|
||||
() => HoldTapMoveGestureRecognizer(
|
||||
supportedDevices: kTouchBasedDeviceKinds,
|
||||
),
|
||||
(instance) => instance
|
||||
..onHoldDragStart = onHoldDragStart
|
||||
..onHoldDragUpdate = onHoldDragUpdate
|
||||
@@ -565,14 +573,18 @@ class _RawTouchGestureDetectorRegionState
|
||||
..onHoldDragEnd = onHoldDragEnd),
|
||||
DoubleFinerTapGestureRecognizer:
|
||||
GestureRecognizerFactoryWithHandlers<DoubleFinerTapGestureRecognizer>(
|
||||
() => DoubleFinerTapGestureRecognizer(), (instance) {
|
||||
() => DoubleFinerTapGestureRecognizer(
|
||||
supportedDevices: kTouchBasedDeviceKinds,
|
||||
), (instance) {
|
||||
instance
|
||||
..onDoubleFinerTap = onDoubleFinerTap
|
||||
..onDoubleFinerTapDown = onDoubleFinerTapDown;
|
||||
}),
|
||||
CustomTouchGestureRecognizer:
|
||||
GestureRecognizerFactoryWithHandlers<CustomTouchGestureRecognizer>(
|
||||
() => CustomTouchGestureRecognizer(), (instance) {
|
||||
() => CustomTouchGestureRecognizer(
|
||||
supportedDevices: kTouchBasedDeviceKinds,
|
||||
), (instance) {
|
||||
instance.onOneFingerPanStart =
|
||||
(DragStartDetails d) => onOneFingerPanStart(context, d);
|
||||
instance
|
||||
|
||||
@@ -426,12 +426,10 @@ class _RemotePageState extends State<RemotePage> with WidgetsBindingObserver {
|
||||
}
|
||||
return Container(
|
||||
color: MyTheme.canvasColor,
|
||||
child: inputModel.isPhysicalMouse.value
|
||||
? getBodyForMobile()
|
||||
: RawTouchGestureDetectorRegion(
|
||||
child: getBodyForMobile(),
|
||||
ffi: gFFI,
|
||||
),
|
||||
child: RawTouchGestureDetectorRegion(
|
||||
child: getBodyForMobile(),
|
||||
ffi: gFFI,
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
|
||||
@@ -259,13 +259,11 @@ class _ViewCameraPageState extends State<ViewCameraPage>
|
||||
}
|
||||
return Container(
|
||||
color: MyTheme.canvasColor,
|
||||
child: inputModel.isPhysicalMouse.value
|
||||
? getBodyForMobile()
|
||||
: RawTouchGestureDetectorRegion(
|
||||
child: getBodyForMobile(),
|
||||
ffi: gFFI,
|
||||
isCamera: true,
|
||||
),
|
||||
child: RawTouchGestureDetectorRegion(
|
||||
child: getBodyForMobile(),
|
||||
ffi: gFFI,
|
||||
isCamera: true,
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:desktop_multi_window/desktop_multi_window.dart';
|
||||
@@ -15,6 +16,7 @@ import 'package:get/get.dart';
|
||||
import '../../models/model.dart';
|
||||
import '../../models/platform_model.dart';
|
||||
import '../../models/state_model.dart';
|
||||
import 'input_modifier_utils.dart';
|
||||
import 'relative_mouse_model.dart';
|
||||
import '../common.dart';
|
||||
import '../consts.dart';
|
||||
@@ -697,6 +699,38 @@ class InputModel {
|
||||
}
|
||||
}
|
||||
|
||||
// Safe: this only re-dispatches synthesized Shift key-up events.
|
||||
// The key-up path clears the tracked Shift state so this does not loop.
|
||||
void _releaseTrackedShiftKeyEventIfNeeded() {
|
||||
final leftShift = toReleaseKeys.lastLShiftKeyEvent;
|
||||
final rightShift = toReleaseKeys.lastRShiftKeyEvent;
|
||||
if (leftShift != null) {
|
||||
handleKeyEvent(leftShift);
|
||||
}
|
||||
if (rightShift != null) {
|
||||
handleKeyEvent(rightShift);
|
||||
}
|
||||
}
|
||||
|
||||
// Safe: this only re-dispatches synthesized Shift key-up events.
|
||||
// The raw key-up path clears the tracked Shift state so this does not loop.
|
||||
void _releaseTrackedRawShiftKeyEventIfNeeded() {
|
||||
final leftShift = toReleaseRawKeys.lastLShiftKeyEvent;
|
||||
final rightShift = toReleaseRawKeys.lastRShiftKeyEvent;
|
||||
if (leftShift != null) {
|
||||
handleRawKeyEvent(RawKeyUpEvent(
|
||||
data: leftShift.data,
|
||||
character: leftShift.character,
|
||||
));
|
||||
}
|
||||
if (rightShift != null) {
|
||||
handleRawKeyEvent(RawKeyUpEvent(
|
||||
data: rightShift.data,
|
||||
character: rightShift.character,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
KeyEventResult handleRawKeyEvent(RawKeyEvent e) {
|
||||
if (isViewOnly) return KeyEventResult.handled;
|
||||
if (isViewCamera) return KeyEventResult.handled;
|
||||
@@ -751,6 +785,27 @@ class InputModel {
|
||||
toReleaseRawKeys.updateKeyUp(key, e);
|
||||
}
|
||||
|
||||
// On some mobile soft-keyboard paths, Flutter may leave cached Shift state
|
||||
// set even though the current raw key event is not shifted anymore.
|
||||
if (e is RawKeyDownEvent &&
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: isMobile,
|
||||
cachedShiftPressed: shift,
|
||||
actualShiftPressed: e.isShiftPressed,
|
||||
logicalKey: e.logicalKey,
|
||||
hasTrackedShiftKeyDown: toReleaseRawKeys.lastLShiftKeyEvent != null ||
|
||||
toReleaseRawKeys.lastRShiftKeyEvent != null,
|
||||
)) {
|
||||
if (kDebugMode) {
|
||||
debugPrint(
|
||||
'input: releasing stale mobile Shift before replaying tracked raw '
|
||||
'key-up (logicalKey=${e.logicalKey.keyLabel}, '
|
||||
'actualShiftPressed=${e.isShiftPressed}, cachedShiftPressed=$shift)',
|
||||
);
|
||||
}
|
||||
_releaseTrackedRawShiftKeyEventIfNeeded();
|
||||
}
|
||||
|
||||
// * Currently mobile does not enable map mode
|
||||
if ((isDesktop || isWebDesktop) && keyboardMode == kKeyMapMode) {
|
||||
mapKeyboardModeRaw(e, iosCapsLock);
|
||||
@@ -794,6 +849,8 @@ class InputModel {
|
||||
iosCapsLock = _getIosCapsFromCharacter(e);
|
||||
}
|
||||
|
||||
// Update cached modifier state before sending the event. The stale mobile
|
||||
// Shift release check below relies on this cached state.
|
||||
if (e is KeyUpEvent) {
|
||||
handleKeyUpEventModifiers(e);
|
||||
} else if (e is KeyDownEvent) {
|
||||
@@ -831,6 +888,21 @@ class InputModel {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// On some mobile soft-keyboard paths, Flutter may leave cached Shift state
|
||||
// set even though the current key event is not shifted anymore.
|
||||
if (e is KeyDownEvent &&
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: isMobile,
|
||||
cachedShiftPressed: shift,
|
||||
actualShiftPressed: HardwareKeyboard.instance.isShiftPressed,
|
||||
logicalKey: e.logicalKey,
|
||||
hasTrackedShiftKeyDown: toReleaseKeys.lastLShiftKeyEvent != null ||
|
||||
toReleaseKeys.lastRShiftKeyEvent != null,
|
||||
)) {
|
||||
_releaseTrackedShiftKeyEventIfNeeded();
|
||||
}
|
||||
|
||||
final isDesktopAndMapMode =
|
||||
isDesktop || (isWebDesktop && keyboardMode == kKeyMapMode);
|
||||
if (isMobileAndMapMode || isDesktopAndMapMode) {
|
||||
|
||||
38
flutter/lib/models/input_modifier_utils.dart
Normal file
38
flutter/lib/models/input_modifier_utils.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// Returns true when a stale mobile one-shot Shift state should be released
|
||||
/// by replaying a tracked Shift key-down as a synthesized key-up.
|
||||
///
|
||||
/// This is only valid on mobile when Flutter's cached Shift state is still on
|
||||
/// (`cachedShiftPressed == true`) but the current hardware/raw event reports
|
||||
/// Shift as off (`actualShiftPressed == false`).
|
||||
///
|
||||
/// A tracked Shift key-down is required so the caller can safely synthesize the
|
||||
/// matching key-up. Both `shiftLeft` and `shiftRight` are excluded because the
|
||||
/// Shift key event itself must be processed first; otherwise we could release
|
||||
/// the tracked key while still handling the original Shift press/release.
|
||||
/// Callers should evaluate this only after their cached modifier state has been
|
||||
/// updated for the current event.
|
||||
///
|
||||
/// When this returns true, the caller logs a line like:
|
||||
/// `input: releasing stale mobile Shift before replaying tracked raw key-up`
|
||||
/// immediately before calling `_releaseTrackedRawShiftKeyEventIfNeeded()`.
|
||||
bool shouldReleaseStaleMobileShift({
|
||||
required bool isMobile,
|
||||
required bool cachedShiftPressed,
|
||||
required bool actualShiftPressed,
|
||||
required LogicalKeyboardKey logicalKey,
|
||||
required bool hasTrackedShiftKeyDown,
|
||||
}) {
|
||||
if (!isMobile || !cachedShiftPressed || actualShiftPressed) {
|
||||
return false;
|
||||
}
|
||||
if (!hasTrackedShiftKeyDown) {
|
||||
return false;
|
||||
}
|
||||
if (logicalKey == LogicalKeyboardKey.shiftLeft ||
|
||||
logicalKey == LogicalKeyboardKey.shiftRight) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -113,8 +113,8 @@ dependencies:
|
||||
|
||||
dev_dependencies:
|
||||
icons_launcher: ^2.0.4
|
||||
#flutter_test:
|
||||
#sdk: flutter
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
build_runner: ^2.4.6
|
||||
freezed: ^2.4.2
|
||||
flutter_lints: ^2.0.2
|
||||
|
||||
125
flutter/test/input_modifier_utils_test.dart
Normal file
125
flutter/test/input_modifier_utils_test.dart
Normal file
@@ -0,0 +1,125 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter_hbb/models/input_modifier_utils.dart';
|
||||
|
||||
void main() {
|
||||
group('shouldReleaseStaleMobileShift', () {
|
||||
test('does not release when cached shift is already false', () {
|
||||
expect(
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: true,
|
||||
cachedShiftPressed: false,
|
||||
actualShiftPressed: false,
|
||||
logicalKey: LogicalKeyboardKey.keyD,
|
||||
hasTrackedShiftKeyDown: true,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('releases one-shot mobile shift after a text key', () {
|
||||
expect(
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: true,
|
||||
cachedShiftPressed: true,
|
||||
actualShiftPressed: false,
|
||||
logicalKey: LogicalKeyboardKey.keyD,
|
||||
hasTrackedShiftKeyDown: true,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('does not release manually toggled shift without tracked key down',
|
||||
() {
|
||||
expect(
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: true,
|
||||
cachedShiftPressed: true,
|
||||
actualShiftPressed: false,
|
||||
logicalKey: LogicalKeyboardKey.keyD,
|
||||
hasTrackedShiftKeyDown: false,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('does not release when shift is still physically pressed', () {
|
||||
expect(
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: true,
|
||||
cachedShiftPressed: true,
|
||||
actualShiftPressed: true,
|
||||
logicalKey: LogicalKeyboardKey.keyD,
|
||||
hasTrackedShiftKeyDown: true,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('does not release on non-mobile platforms', () {
|
||||
expect(
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: false,
|
||||
cachedShiftPressed: true,
|
||||
actualShiftPressed: false,
|
||||
logicalKey: LogicalKeyboardKey.keyD,
|
||||
hasTrackedShiftKeyDown: true,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('releases on enter key', () {
|
||||
expect(
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: true,
|
||||
cachedShiftPressed: true,
|
||||
actualShiftPressed: false,
|
||||
logicalKey: LogicalKeyboardKey.enter,
|
||||
hasTrackedShiftKeyDown: true,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('releases on arrow key', () {
|
||||
expect(
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: true,
|
||||
cachedShiftPressed: true,
|
||||
actualShiftPressed: false,
|
||||
logicalKey: LogicalKeyboardKey.arrowLeft,
|
||||
hasTrackedShiftKeyDown: true,
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
});
|
||||
|
||||
test('does not release on modifier events', () {
|
||||
expect(
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: true,
|
||||
cachedShiftPressed: true,
|
||||
actualShiftPressed: false,
|
||||
logicalKey: LogicalKeyboardKey.shiftLeft,
|
||||
hasTrackedShiftKeyDown: true,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
test('does not release on shiftRight modifier events', () {
|
||||
expect(
|
||||
shouldReleaseStaleMobileShift(
|
||||
isMobile: true,
|
||||
cachedShiftPressed: true,
|
||||
actualShiftPressed: false,
|
||||
logicalKey: LogicalKeyboardKey.shiftRight,
|
||||
hasTrackedShiftKeyDown: true,
|
||||
),
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -605,21 +605,30 @@ pub fn session_handle_flutter_raw_key_event(
|
||||
}
|
||||
}
|
||||
|
||||
// SyncReturn<()> is used to make sure enter() and leave() are executed in the sequence this function is called.
|
||||
//
|
||||
// If the cursor jumps between remote page of two connections, leave view and enter view will be called.
|
||||
// session_enter_or_leave() will be called then.
|
||||
// As rust is multi-thread, it is possible that enter() is called before leave().
|
||||
// This will cause the keyboard input to take no effect.
|
||||
// As Rust is multi-threaded, enter() can be called before leave().
|
||||
// The Rust-side grab ownership state filters stale transitions.
|
||||
pub fn session_enter_or_leave(_session_id: SessionID, _enter: bool) -> SyncReturn<()> {
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
if let Some(session) = sessions::get_session_by_session_id(&_session_id) {
|
||||
let keyboard_mode = session.get_keyboard_mode();
|
||||
// Use the full per-window UUID (not lc.session_id which is per-connection)
|
||||
// so that two windows viewing the same peer get distinct grab owners.
|
||||
let window_id = _session_id.as_u128();
|
||||
if _enter {
|
||||
set_cur_session_id_(_session_id, &keyboard_mode);
|
||||
session.enter(keyboard_mode);
|
||||
crate::keyboard::client::change_grab_status(
|
||||
crate::common::GrabState::Run,
|
||||
&keyboard_mode,
|
||||
window_id,
|
||||
);
|
||||
} else {
|
||||
session.leave(keyboard_mode);
|
||||
crate::keyboard::client::change_grab_status(
|
||||
crate::common::GrabState::Wait,
|
||||
&keyboard_mode,
|
||||
window_id,
|
||||
);
|
||||
}
|
||||
}
|
||||
SyncReturn(())
|
||||
|
||||
223
src/keyboard.rs
223
src/keyboard.rs
@@ -82,8 +82,67 @@ lazy_static::lazy_static! {
|
||||
pub mod client {
|
||||
use super::*;
|
||||
|
||||
/// Tracks grab ownership and serializes transitions across threads.
|
||||
///
|
||||
/// Multiple Flutter isolates (one per session window) call
|
||||
/// `change_grab_status(Run/Wait)` concurrently. Without serialization a
|
||||
/// stale `Wait` from session A can clobber session B's freshly acquired
|
||||
/// grab on any desktop OS.
|
||||
///
|
||||
/// Windows and macOS are less susceptible in practice because the Flutter
|
||||
/// side triggers `enterView` only after a mouse click inside the window,
|
||||
/// but we cannot rely on that. On Linux/X11, `XGrabKeyboard` can also
|
||||
/// cause a focus-change feedback loop (~10 Hz), so `last_grab` debounces
|
||||
/// spurious `Wait` events that arrive shortly after a `Run`.
|
||||
#[derive(Default)]
|
||||
struct GrabOwnerState {
|
||||
owner: Option<u128>,
|
||||
last_grab: Option<std::time::Instant>,
|
||||
/// True while a deferred-release thread is in flight. Prevents
|
||||
/// spawning redundant threads during the X11 feedback loop.
|
||||
deferred_pending: bool,
|
||||
}
|
||||
|
||||
/// How long after a grab acquisition we suppress Wait from the same session.
|
||||
/// Must exceed one full X11 feedback cycle (~100 ms: 50 ms enable + 50 ms disable).
|
||||
#[cfg(target_os = "linux")]
|
||||
const GRAB_DEBOUNCE_MS: u128 = 300;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref IS_GRAB_STARTED: Arc<Mutex<bool>> = Arc::new(Mutex::new(false));
|
||||
static ref GRAB_STATE: Arc<Mutex<GrabOwnerState>> = Arc::new(Mutex::new(GrabOwnerState::default()));
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
lazy_static::lazy_static! {
|
||||
static ref GRAB_OP_LOCK: Mutex<()> = Mutex::new(());
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn apply_run_grab_if_owner(session_id: u128, disable_first: bool) {
|
||||
let _lock = GRAB_OP_LOCK.lock().unwrap();
|
||||
let gs = GRAB_STATE.lock().unwrap();
|
||||
if gs.owner != Some(session_id) {
|
||||
return;
|
||||
}
|
||||
drop(gs);
|
||||
if disable_first {
|
||||
log::debug!("[grab] handoff: disable_grab before re-grab");
|
||||
rdev::disable_grab();
|
||||
}
|
||||
rdev::enable_grab();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn disable_grab_if_released() {
|
||||
let _lock = GRAB_OP_LOCK.lock().unwrap();
|
||||
let should_disable = {
|
||||
let gs = GRAB_STATE.lock().unwrap();
|
||||
gs.owner.is_none() && gs.last_grab.is_none()
|
||||
};
|
||||
if should_disable {
|
||||
rdev::disable_grab();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_grab_loop() {
|
||||
@@ -96,36 +155,167 @@ pub mod client {
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
pub fn change_grab_status(state: GrabState, keyboard_mode: &str) {
|
||||
pub fn change_grab_status(state: GrabState, keyboard_mode: &str, session_id: u128) {
|
||||
#[cfg(feature = "flutter")]
|
||||
if !IS_RDEV_ENABLED.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
// Serialize transitions so a stale `Wait` from a previous owner cannot
|
||||
// clobber a fresh `Run` from a different session window.
|
||||
let mut release_after_unlock = None;
|
||||
#[cfg(target_os = "linux")]
|
||||
let mut run_grab_after_unlock = None;
|
||||
#[cfg(target_os = "linux")]
|
||||
let mut disable_after_unlock = false;
|
||||
let mut gs = GRAB_STATE.lock().unwrap();
|
||||
match state {
|
||||
GrabState::Ready => {}
|
||||
GrabState::Run => {
|
||||
#[cfg(windows)]
|
||||
update_grab_get_key_name(keyboard_mode);
|
||||
|
||||
// Idempotent: if this session already owns the grab, just
|
||||
// refresh the debounce timer (proves the session is still
|
||||
// actively focused) and skip the actual grab call.
|
||||
if gs.owner == Some(session_id) {
|
||||
gs.last_grab = Some(std::time::Instant::now());
|
||||
// Reset so the next Wait can spawn a fresh deferred-release
|
||||
// timer with an up-to-date snapshot of last_grab.
|
||||
gs.deferred_pending = false;
|
||||
log::debug!(
|
||||
"[grab] Run(0x{:x}): already owner, refresh debounce",
|
||||
session_id
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
log::debug!(
|
||||
"[grab] Run(0x{:x}): prev_owner={}, mode={}",
|
||||
session_id,
|
||||
gs.owner
|
||||
.map_or("none".to_string(), |id| format!("0x{:x}", id)),
|
||||
keyboard_mode,
|
||||
);
|
||||
|
||||
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
|
||||
KEYBOARD_HOOKED.swap(true, Ordering::SeqCst);
|
||||
KEYBOARD_HOOKED.store(true, Ordering::SeqCst);
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
rdev::enable_grab();
|
||||
let had_owner = gs.owner.is_some();
|
||||
gs.owner = Some(session_id);
|
||||
gs.last_grab = Some(std::time::Instant::now());
|
||||
// Invalidate any in-flight deferred release from the previous
|
||||
// owner so it cannot suppress a fresh timer for the new owner.
|
||||
gs.deferred_pending = false;
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
run_grab_after_unlock = Some(had_owner);
|
||||
}
|
||||
}
|
||||
GrabState::Wait => {
|
||||
// Drop stale `Wait` events that do not correspond to the
|
||||
// current grab owner. This prevents a late PointerExit from
|
||||
// session A from releasing session B's freshly acquired grab.
|
||||
if gs.owner != Some(session_id) {
|
||||
log::debug!(
|
||||
"[grab] Wait(0x{:x}): ignored, owner={}",
|
||||
session_id,
|
||||
gs.owner
|
||||
.map_or("none".to_string(), |id| format!("0x{:x}", id)),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Debounce: on Linux/X11, XGrabKeyboard causes a focus-change
|
||||
// feedback loop (grab -> PointerExit -> ungrab -> PointerEnter ->
|
||||
// grab -> ...). Suppress Wait if the grab was acquired recently
|
||||
// by this same session -- it is X11 feedback, not a real leave.
|
||||
// A deferred release is scheduled so that a genuine leave within
|
||||
// the debounce window is not permanently lost.
|
||||
#[cfg(target_os = "linux")]
|
||||
if let Some(t) = gs.last_grab {
|
||||
let elapsed = t.elapsed().as_millis();
|
||||
if elapsed < GRAB_DEBOUNCE_MS {
|
||||
if !gs.deferred_pending {
|
||||
log::debug!(
|
||||
"[grab] Wait(0x{:x}): debounced ({}ms < {}ms), scheduling deferred release",
|
||||
session_id, elapsed, GRAB_DEBOUNCE_MS,
|
||||
);
|
||||
gs.deferred_pending = true;
|
||||
let remaining = (GRAB_DEBOUNCE_MS - elapsed) as u64 + 50;
|
||||
let snapshot = gs.last_grab;
|
||||
let mode = keyboard_mode.to_string();
|
||||
std::thread::spawn(move || {
|
||||
std::thread::sleep(std::time::Duration::from_millis(remaining));
|
||||
let release_keys = {
|
||||
let mut gs = GRAB_STATE.lock().unwrap();
|
||||
// Release only if no new Run has refreshed the grab since.
|
||||
if gs.owner == Some(session_id) && gs.last_grab == snapshot {
|
||||
let to_release = take_remote_keys();
|
||||
gs.deferred_pending = false;
|
||||
log::debug!(
|
||||
"[grab] Wait(0x{:x}): deferred release",
|
||||
session_id
|
||||
);
|
||||
KEYBOARD_HOOKED.store(false, Ordering::SeqCst);
|
||||
gs.owner = None;
|
||||
gs.last_grab = None;
|
||||
Some(to_release)
|
||||
} else {
|
||||
log::debug!(
|
||||
"[grab] Wait(0x{:x}): deferred release cancelled (grab refreshed)",
|
||||
session_id,
|
||||
);
|
||||
None
|
||||
}
|
||||
};
|
||||
if let Some(to_release) = release_keys {
|
||||
disable_grab_if_released();
|
||||
release_remote_keys_for_events(&mode, to_release);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
log::debug!(
|
||||
"[grab] Wait(0x{:x}): debounced, deferred release already pending",
|
||||
session_id,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
log::debug!("[grab] Wait(0x{:x}): releasing grab", session_id);
|
||||
|
||||
#[cfg(windows)]
|
||||
rdev::set_get_key_unicode(false);
|
||||
|
||||
release_remote_keys(keyboard_mode);
|
||||
|
||||
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
|
||||
KEYBOARD_HOOKED.swap(false, Ordering::SeqCst);
|
||||
KEYBOARD_HOOKED.store(false, Ordering::SeqCst);
|
||||
|
||||
gs.owner = None;
|
||||
gs.last_grab = None;
|
||||
gs.deferred_pending = false;
|
||||
release_after_unlock = Some(take_remote_keys());
|
||||
#[cfg(target_os = "linux")]
|
||||
rdev::disable_grab();
|
||||
{
|
||||
disable_after_unlock = true;
|
||||
}
|
||||
}
|
||||
GrabState::Exit => {}
|
||||
}
|
||||
drop(gs);
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
if disable_after_unlock {
|
||||
disable_grab_if_released();
|
||||
}
|
||||
if let Some(disable_first) = run_grab_after_unlock {
|
||||
apply_run_grab_if_owner(session_id, disable_first);
|
||||
}
|
||||
}
|
||||
if let Some(to_release) = release_after_unlock {
|
||||
release_remote_keys_for_events(keyboard_mode, to_release);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process_event(keyboard_mode: &str, event: &Event, lock_modes: Option<i32>) {
|
||||
@@ -341,7 +531,6 @@ fn notify_exit_relative_mouse_mode() {
|
||||
flutter::push_session_event(&session_id, "exit_relative_mouse_mode", vec![]);
|
||||
}
|
||||
|
||||
|
||||
/// Handle relative mouse mode shortcuts in the rdev grab loop.
|
||||
/// Returns true if the event should be blocked from being sent to the peer.
|
||||
#[cfg(feature = "flutter")]
|
||||
@@ -540,10 +729,12 @@ pub fn is_long_press(event: &Event) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn release_remote_keys(keyboard_mode: &str) {
|
||||
// todo!: client quit suddenly, how to release keys?
|
||||
let to_release = TO_RELEASE.lock().unwrap().clone();
|
||||
TO_RELEASE.lock().unwrap().clear();
|
||||
fn take_remote_keys() -> HashMap<Key, Event> {
|
||||
let mut to_release = TO_RELEASE.lock().unwrap();
|
||||
std::mem::take(&mut *to_release)
|
||||
}
|
||||
|
||||
fn release_remote_keys_for_events(keyboard_mode: &str, to_release: HashMap<Key, Event>) {
|
||||
for (key, mut event) in to_release.into_iter() {
|
||||
event.event_type = EventType::KeyRelease(key);
|
||||
client::process_event(keyboard_mode, &event, None);
|
||||
@@ -558,6 +749,12 @@ pub fn release_remote_keys(keyboard_mode: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn release_remote_keys(keyboard_mode: &str) {
|
||||
// todo!: client quit suddenly, how to release keys?
|
||||
release_remote_keys_for_events(keyboard_mode, take_remote_keys());
|
||||
}
|
||||
|
||||
pub fn get_keyboard_mode_enum(keyboard_mode: &str) -> KeyboardMode {
|
||||
match keyboard_mode {
|
||||
"map" => KeyboardMode::Map,
|
||||
@@ -748,7 +945,6 @@ pub fn event_to_key_events(
|
||||
) -> Vec<KeyEvent> {
|
||||
peer.retain(|c| !c.is_whitespace());
|
||||
|
||||
let mut key_event = KeyEvent::new();
|
||||
update_modifiers_state(event);
|
||||
|
||||
match event.event_type {
|
||||
@@ -761,6 +957,7 @@ pub fn event_to_key_events(
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let mut key_event = KeyEvent::new();
|
||||
key_event.mode = keyboard_mode.into();
|
||||
|
||||
let mut key_events = match keyboard_mode {
|
||||
|
||||
@@ -870,12 +870,14 @@ impl<T: InvokeUiSession> Session<T> {
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
pub fn enter(&self, keyboard_mode: String) {
|
||||
keyboard::client::change_grab_status(GrabState::Run, &keyboard_mode);
|
||||
let session_id = self.lc.read().unwrap().session_id as u128;
|
||||
keyboard::client::change_grab_status(GrabState::Run, &keyboard_mode, session_id);
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
||||
pub fn leave(&self, keyboard_mode: String) {
|
||||
keyboard::client::change_grab_status(GrabState::Wait, &keyboard_mode);
|
||||
let session_id = self.lc.read().unwrap().session_id as u128;
|
||||
keyboard::client::change_grab_status(GrabState::Wait, &keyboard_mode, session_id);
|
||||
}
|
||||
|
||||
// flutter only TODO new input
|
||||
|
||||
Reference in New Issue
Block a user