add pointer device event

Signed-off-by: dignow <linlong1265@gmail.com>
This commit is contained in:
dignow
2023-07-19 01:18:10 +08:00
parent 780cb37d46
commit c86a8fff03
9 changed files with 204 additions and 31 deletions

View File

@@ -115,6 +115,8 @@ enum MessageInput {
Mouse((MouseEvent, i32)),
#[cfg(not(any(target_os = "android", target_os = "ios")))]
Key((KeyEvent, bool)),
#[cfg(not(any(target_os = "android", target_os = "ios")))]
Pointer((PointerDeviceEvent, i32)),
BlockOn,
BlockOff,
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
@@ -668,6 +670,9 @@ impl Connection {
handle_key(&msg);
}
}
MessageInput::Pointer((msg, id)) => {
handle_pointer(&msg, id);
}
MessageInput::BlockOn => {
if crate::platform::block_input(true) {
block_input_mode = true;
@@ -1179,6 +1184,12 @@ impl Connection {
self.tx_input.send(MessageInput::Mouse((msg, conn_id))).ok();
}
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn input_pointer(&self, msg: PointerDeviceEvent, conn_id: i32) {
self.tx_input.send(MessageInput::Pointer((msg, conn_id))).ok();
}
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
fn input_key(&self, msg: KeyEvent, press: bool) {
@@ -1577,6 +1588,13 @@ impl Connection {
self.input_mouse(me, self.inner.id());
}
}
Some(message::Union::PointerDeviceEvent(pde)) => {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if self.peer_keyboard_enabled() {
MOUSE_MOVE_TIME.store(get_time(), Ordering::SeqCst);
self.input_pointer(pde, self.inner.id());
}
}
#[cfg(any(target_os = "android", target_os = "ios"))]
Some(message::Union::KeyEvent(..)) => {}
#[cfg(not(any(target_os = "android", target_os = "ios")))]

View File

@@ -7,7 +7,11 @@ use crate::input::*;
#[cfg(target_os = "macos")]
use dispatch::Queue;
use enigo::{Enigo, Key, KeyboardControllable, MouseButton, MouseControllable};
use hbb_common::{get_time, protobuf::EnumOrUnknown};
use hbb_common::{
get_time,
message_proto::{pointer_device_event::Union::TouchEvent, touch_event::Union::ScaleUpdate},
protobuf::EnumOrUnknown,
};
use rdev::{self, EventType, Key as RdevKey, KeyCode, RawKey};
#[cfg(target_os = "macos")]
use rdev::{CGEventSourceStateID, CGEventTapLocation, VirtualInput};
@@ -523,6 +527,21 @@ pub fn handle_mouse(evt: &MouseEvent, conn: i32) {
handle_mouse_(evt, conn);
}
// to-do: merge handle_mouse and handle_pointer
pub fn handle_pointer(evt: &PointerDeviceEvent, conn: i32) {
#[cfg(target_os = "macos")]
if !is_server() {
// having GUI, run main GUI thread, otherwise crash
let evt = evt.clone();
QUEUE.exec_async(move || handle_pointer_(&evt, conn));
return;
}
#[cfg(windows)]
crate::portable_service::client::handle_pointer(evt, conn);
#[cfg(not(windows))]
handle_pointer_(evt, conn);
}
pub fn fix_key_down_timeout_loop() {
std::thread::spawn(move || loop {
std::thread::sleep(std::time::Duration::from_millis(10_000));
@@ -743,7 +762,7 @@ fn active_mouse_(conn: i32) -> bool {
}
}
pub fn handle_mouse_(evt: &MouseEvent, conn: i32) {
pub fn handle_pointer_(evt: &PointerDeviceEvent, conn: i32) {
if !active_mouse_(conn) {
return;
}
@@ -752,12 +771,25 @@ pub fn handle_mouse_(evt: &MouseEvent, conn: i32) {
return;
}
if evt.scale != 0 {
#[cfg(target_os = "windows")]
{
handle_scale(evt.scale);
return;
}
match &evt.union {
Some(TouchEvent(evt)) => match &evt.union {
Some(ScaleUpdate(_scale_evt)) => {
#[cfg(target_os = "windows")]
handle_scale(_scale_evt.scale);
}
_ => {}
},
_ => {}
}
}
pub fn handle_mouse_(evt: &MouseEvent, conn: i32) {
if !active_mouse_(conn) {
return;
}
if EXITING.load(Ordering::SeqCst) {
return;
}
#[cfg(windows)]
@@ -896,10 +928,13 @@ pub fn handle_mouse_(evt: &MouseEvent, conn: i32) {
#[cfg(target_os = "windows")]
fn handle_scale(scale: i32) {
let mut en = ENIGO.lock().unwrap();
if en.key_down(Key::Control).is_ok() {
en.mouse_scroll_y(scale);
if scale == 0 {
en.key_up(Key::Control);
} else {
if en.key_down(Key::Control).is_ok() {
en.mouse_scroll_y(scale);
}
}
en.key_up(Key::Control);
}
pub fn is_enter(evt: &KeyEvent) -> bool {

View File

@@ -222,6 +222,8 @@ mod utils {
// functions called in separate SYSTEM user process.
pub mod server {
use hbb_common::message_proto::PointerDeviceEvent;
use super::*;
lazy_static::lazy_static! {
@@ -466,6 +468,11 @@ pub mod server {
crate::input_service::handle_mouse_(&evt, conn);
}
}
Pointer((v, conn)) => {
if let Ok(evt) = PointerDeviceEvent::parse_from_bytes(&v) {
crate::input_service::handle_pointer_(&evt, conn);
}
}
Key(v) => {
if let Ok(evt) = KeyEvent::parse_from_bytes(&v) {
crate::input_service::handle_key_(&evt);
@@ -499,7 +506,7 @@ pub mod server {
// functions called in main process.
pub mod client {
use hbb_common::anyhow::Context;
use hbb_common::{anyhow::Context, message_proto::PointerDeviceEvent};
use super::*;
@@ -864,6 +871,14 @@ pub mod client {
))))
}
fn handle_pointer_(evt: &PointerDeviceEvent, conn: i32) -> ResultType<()> {
let mut v = vec![];
evt.write_to_vec(&mut v)?;
ipc_send(Data::DataPortableService(DataPortableService::Pointer((
v, conn,
))))
}
fn handle_key_(evt: &KeyEvent) -> ResultType<()> {
let mut v = vec![];
evt.write_to_vec(&mut v)?;
@@ -910,6 +925,15 @@ pub mod client {
}
}
pub fn handle_pointer(evt: &PointerDeviceEvent, conn: i32) {
if RUNNING.lock().unwrap().clone() {
crate::input_service::update_latest_input_cursor_time(conn);
handle_pointer_(evt, conn).ok();
} else {
crate::input_service::handle_pointer_(evt, conn);
}
}
pub fn handle_key(evt: &KeyEvent) {
if RUNNING.lock().unwrap().clone() {
handle_key_(evt).ok();