Skip to content

Commit

Permalink
feat(TargetMouse): add MoveCursor method to virtual mouse interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowApex committed Aug 7, 2024
1 parent 2b94b6a commit 94e79a0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
39 changes: 30 additions & 9 deletions src/dbus/interface/target/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use zbus::fdo;
use zbus_macros::interface;

use crate::input::{
capability::{Capability, Mouse},
event::{native::NativeEvent, value::InputValue},
target::client::TargetDeviceClient,
};

/// The [TargetMouseInterface] provides a DBus interface that can be exposed for managing
/// a [MouseDevice]. It works by sending command messages to a channel that the
/// [MouseDevice] is listening on.
pub struct TargetMouseInterface {}

impl TargetMouseInterface {
pub fn new() -> TargetMouseInterface {
TargetMouseInterface {}
}
pub struct TargetMouseInterface {
target_device: TargetDeviceClient,
}

impl Default for TargetMouseInterface {
fn default() -> Self {
Self::new()
impl TargetMouseInterface {
pub fn new(target_device: TargetDeviceClient) -> TargetMouseInterface {
TargetMouseInterface { target_device }
}
}

Expand All @@ -25,4 +27,23 @@ impl TargetMouseInterface {
async fn name(&self) -> fdo::Result<String> {
Ok("Mouse".into())
}

/// Move the virtual mouse by the given amount relative to the cursor's
/// relative position.
async fn move_cursor(&self, x: i32, y: i32) -> fdo::Result<()> {
// Create a mouse motion event
let value = InputValue::Vector2 {
x: Some(x as f64),
y: Some(y as f64),
};
let event = NativeEvent::new(Capability::Mouse(Mouse::Motion), value);

// Write the event to the virtual mouse
self.target_device
.write_event(event)
.await
.map_err(|err| fdo::Error::Failed(err.to_string()))?;

Ok(())
}
}
9 changes: 2 additions & 7 deletions src/input/target/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,10 @@ impl MouseDevice {
}

impl TargetInputDevice for MouseDevice {
fn start_dbus_interface(
&mut self,
dbus: Connection,
path: String,
_client: TargetDeviceClient,
) {
fn start_dbus_interface(&mut self, dbus: Connection, path: String, client: TargetDeviceClient) {
log::debug!("Starting dbus interface: {path}");
tokio::task::spawn(async move {
let iface = TargetMouseInterface::new();
let iface = TargetMouseInterface::new(client);
if let Err(e) = dbus.object_server().at(path.clone(), iface).await {
log::debug!("Failed to start dbus interface {path}: {e:?}");
} else {
Expand Down

0 comments on commit 94e79a0

Please sign in to comment.