Fix wayland input after Lock and Wake

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2022-11-25 15:22:44 +08:00
parent e31130d4f8
commit 9dfa02a702
9 changed files with 159 additions and 36 deletions

View File

@@ -117,6 +117,7 @@ pub enum DataMouse {
Click(enigo::MouseButton),
ScrollX(i32),
ScrollY(i32),
Refresh,
}
#[derive(Debug, Serialize, Deserialize, Clone)]

View File

@@ -1,6 +1,8 @@
use crate::ipc::Data;
use bytes::Bytes;
pub use connection::*;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::config::Config2;
use hbb_common::{
allow_err,
anyhow::{anyhow, Context},
@@ -15,8 +17,6 @@ use hbb_common::{
timeout, tokio, ResultType, Stream,
};
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::config::Config2;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use service::ServiceTmpl;
use service::{GenericService, Service, Subscriber};
use std::{
@@ -377,6 +377,7 @@ pub async fn start_server(is_server: bool) {
#[cfg(windows)]
crate::platform::windows::bootstrap();
input_service::fix_key_down_timeout_loop();
allow_err!(video_service::check_init().await);
#[cfg(target_os = "macos")]
tokio::spawn(async { sync_and_watch_config_dir().await });
crate::RendezvousMediator::start_all().await;

View File

@@ -219,19 +219,43 @@ lazy_static::lazy_static! {
static ref IS_SERVER: bool = std::env::args().nth(1) == Some("--server".to_owned());
}
// First call set_uinput() will create keyboard and mouse clients.
// The clients are ipc connections that must live shorter than tokio runtime.
// Thus this funtion must not be called in a temporary runtime.
#[cfg(target_os = "linux")]
pub async fn set_uinput() -> ResultType<()> {
// Keyboard and mouse both open /dev/uinput
// TODO: Make sure there's no race
let keyboard = super::uinput::client::UInputKeyboard::new().await?;
log::info!("UInput keyboard created");
let mouse = super::uinput::client::UInputMouse::new().await?;
log::info!("UInput mouse created");
let xxx = ENIGO.lock();
let mut en = xxx.unwrap();
en.set_uinput_keyboard(Some(Box::new(keyboard)));
en.set_uinput_mouse(Some(Box::new(mouse)));
if ENIGO.lock().unwrap().get_custom_keyboard().is_none() {
let keyboard = super::uinput::client::UInputKeyboard::new().await?;
log::info!("UInput keyboard created");
ENIGO
.lock()
.unwrap()
.set_custom_keyboard(Box::new(keyboard));
}
let mouse_created = ENIGO.lock().unwrap().get_custom_mouse().is_some();
if mouse_created {
std::thread::spawn(|| {
if let Some(mouse) = ENIGO.lock().unwrap().get_custom_mouse() {
if let Some(mouse) = mouse
.as_mut_any()
.downcast_mut::<super::uinput::client::UInputMouse>()
{
allow_err!(mouse.send_refresh());
} else {
log::error!("failed downcast uinput mouse");
}
}
});
} else {
let mouse = super::uinput::client::UInputMouse::new().await?;
log::info!("UInput mouse created");
ENIGO.lock().unwrap().set_custom_mouse(Box::new(mouse));
}
Ok(())
}

View File

@@ -63,6 +63,14 @@ pub mod client {
}
impl KeyboardControllable for UInputKeyboard {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self
}
fn get_key_state(&mut self, key: Key) -> bool {
match self.send_get_key_state(Data::Keyboard(DataKeyboard::GetKeyState(key))) {
Ok(state) => state,
@@ -105,9 +113,21 @@ pub mod client {
async fn send(&mut self, data: Data) -> ResultType<()> {
self.conn.send(&data).await
}
pub fn send_refresh(&mut self) -> ResultType<()> {
self.send(Data::Mouse(DataMouse::Refresh))
}
}
impl MouseControllable for UInputMouse {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn std::any::Any {
self
}
fn mouse_move_to(&mut self, x: i32, y: i32) {
allow_err!(self.send(Data::Mouse(DataMouse::MoveTo(x, y))));
}
@@ -492,6 +512,9 @@ pub mod service {
allow_err!(mouse.scroll_wheel(&scroll))
}
}
DataMouse::Refresh => {
// unreachable!()
}
}
}
@@ -562,7 +585,17 @@ pub mod service {
Ok(Some(data)) => {
match data {
Data::Mouse(data) => {
handle_mouse(&mut mouse, &data);
if let DataMouse::Refresh = data {
mouse = match mouce::Mouse::new_uinput(rng_x, rng_y) {
Ok(mouse) => mouse,
Err(e) => {
log::error!("Failed to create mouse, {}", e);
return;
}
}
} else {
handle_mouse(&mut mouse, &data);
}
}
_ => {
}

View File

@@ -759,6 +759,16 @@ fn get_display_num() -> usize {
}
}
pub async fn check_init() -> ResultType<()> {
#[cfg(target_os = "linux")]
{
if !scrap::is_x11() {
return super::wayland::check_init().await;
}
}
Ok(())
}
pub(super) fn get_displays_2(all: &Vec<Display>) -> (usize, Vec<DisplayInfo>) {
let mut displays = Vec::new();
let mut primary = 0;

View File

@@ -115,7 +115,7 @@ pub(super) fn is_inited() -> Option<Message> {
}
}
async fn check_init() -> ResultType<()> {
pub(super) async fn check_init() -> ResultType<()> {
if !scrap::is_x11() {
let mut minx = 0;
let mut maxx = 0;