wayland better uinput control

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-11-30 10:28:03 +08:00
parent ee29db888a
commit 60073e037e
3 changed files with 34 additions and 31 deletions

View File

@ -379,6 +379,10 @@ pub async fn start_server(is_server: bool) {
#[cfg(windows)]
crate::platform::windows::bootstrap();
input_service::fix_key_down_timeout_loop();
#[cfg(target_os = "linux")]
if crate::platform::current_is_wayland() {
allow_err!(input_service::setup_uinput(0, 1920, 0, 1080).await);
}
#[cfg(target_os = "macos")]
tokio::spawn(async { sync_and_watch_config_dir().await });
crate::RendezvousMediator::start_all().await;

View File

@ -223,44 +223,44 @@ lazy_static::lazy_static! {
// 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<()> {
pub async fn setup_uinput(minx: i32, maxx: i32, miny: i32, maxy: i32) -> ResultType<()> {
// Keyboard and mouse both open /dev/uinput
// TODO: Make sure there's no race
set_uinput_resolution(minx, maxx, miny, maxy).await?;
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 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 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));
ENIGO
.lock()
.unwrap()
.set_custom_keyboard(Box::new(keyboard));
ENIGO.lock().unwrap().set_custom_mouse(Box::new(mouse));
Ok(())
}
#[cfg(target_os = "linux")]
pub async fn update_mouse_resolution(minx: i32, maxx: i32, miny: i32, maxy: i32) -> ResultType<()> {
set_uinput_resolution(minx, maxx, miny, maxy).await?;
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");
}
}
Ok(())
}
#[cfg(target_os = "linux")]
pub async fn set_uinput_resolution(minx: i32, maxx: i32, miny: i32, maxy: i32) -> ResultType<()> {
async fn set_uinput_resolution(minx: i32, maxx: i32, miny: i32, maxy: i32) -> ResultType<()> {
super::uinput::client::set_resolution(minx, maxx, miny, maxy).await
}

View File

@ -174,14 +174,13 @@ pub(super) async fn check_init() -> ResultType<()> {
if minx != maxx && miny != maxy {
log::info!(
"send uinput resolution: ({}, {}), ({}, {})",
"update mouse resolution: ({}, {}), ({}, {})",
minx,
maxx,
miny,
maxy
);
allow_err!(input_service::set_uinput_resolution(minx, maxx, miny, maxy).await);
allow_err!(input_service::set_uinput().await);
allow_err!(input_service::update_mouse_resolution(minx, maxx, miny, maxy).await);
}
}
Ok(())