Skip to content

Commit 17670df

Browse files
committed
added an option to tesselate mouse movement server-side
1 parent c4ccd89 commit 17670df

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ During the first run the client will generate a config file called `inputshare-c
7373
* `hotkey`: The hotkey that toggles input between the local and remote pc. The hotkey has two parts: the trigger key which triggers the swap and a variable amount of modifier keys which also have to be pressed for the trigger to work. Supported values: [Possible key codes](#appendix-a-possible-key-codes).
7474
* `backlist`: All keys included in in this list will be ignored by the client. Supported values: [Possible key codes](#appendix-a-possible-key-codes).
7575
* `show_network_info`: When set to `true` the client will display the round-trip-time and packet loss to the server. This call also be toggled at runtime by pressing `F1`.
76-
* `network_send_rate`: The number of packets per second that the client will send to the server while transmitting. Higher values mean lower latency and smoother mouse movement while lower values mean less network activity. Note that if the send rate is set to high it will flood the connection and cause massive delays / packet loss.
76+
* `network_send_rate`: The number of packets per second that the client will send to the server while transmitting. Higher values mean lower latency and smoother mouse movement while lower values mean less network activity. Note that if the send rate is set to high it will flood the connection and cause massive delays / packet loss. Consider that the `mouse-tesselation-factor` option of the server has a similar effect and should be tuned in tandem.
7777

7878

7979

inputshare-server/src/main.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
mod receiver;
22
mod configfs;
33

4+
use std::convert::TryInto;
45
use std::fmt::{Debug, Formatter};
56
use std::fs::{File, OpenOptions};
67
use std::io::Write;
78
use std::net::{SocketAddr};
9+
use std::num::NonZeroU8;
810
use std::time::{Duration, Instant};
911
use mio::{Events, Interest, Poll, Token};
1012
use anyhow::Result;
@@ -24,6 +26,11 @@ struct Args {
2426
#[clap(short, long)]
2527
auto_movement_timeout: Option<u64>,
2628

29+
/// Split each mouse movement command in up to x usb packets
30+
/// Higher values mean smoother movement but carry a higher risk of saturating the usb connection
31+
#[clap(short, long, default_value_t = 5)]
32+
mouse_tesselation_factor: u8,
33+
2734
/// The interface that should be bound
2835
#[clap(short, long, default_value = "0.0.0.0:60067")]
2936
interface: String,
@@ -44,7 +51,7 @@ fn main() -> Result<()>{
4451
fn server(args: Args) -> Result<()> {
4552
println!("Opening HID devices");
4653

47-
let mut mouse = Mouse::new()?;
54+
let mut mouse = Mouse::new(args.mouse_tesselation_factor.try_into()?)?;
4855
let mut keyboard = Keyboard::new()?;
4956

5057
const SERVER: Token = Token(0);
@@ -254,16 +261,18 @@ impl Keyboard {
254261
#[derive(Debug)]
255262
pub struct Mouse {
256263
device: File,
257-
pressed_buttons: HidMouseButton
264+
pressed_buttons: HidMouseButton,
265+
tess_factor: i16
258266
}
259267

260268
impl Mouse {
261269

262-
pub fn new() -> std::io::Result<Self> {
270+
pub fn new(tess_factor: NonZeroU8) -> std::io::Result<Self> {
263271
let device = OpenOptions::new().write(true).append(true).open("/dev/hidg1")?;
264272
Ok(Self {
265273
device,
266-
pressed_buttons: HidMouseButton::empty()
274+
pressed_buttons: HidMouseButton::empty(),
275+
tess_factor: i16::from(tess_factor.get())
267276
})
268277
}
269278

@@ -294,8 +303,17 @@ impl Mouse {
294303
self.send_report(0, 0,0,0)
295304
}
296305

297-
pub fn move_by(&mut self, dx: i16, dy: i16) -> std::io::Result<()> {
298-
self.send_report(dx, dy, 0,0)
306+
pub fn move_by(&mut self, mut dx: i16, mut dy: i16) -> std::io::Result<()> {
307+
let sx = abs_max(dx / self.tess_factor, dx.signum());
308+
let sy = abs_max(dy / self.tess_factor, dy.signum());
309+
while dx != 0 || dy != 0 {
310+
let tx = abs_min(dx, sx);
311+
let ty = abs_min(dy, sy);
312+
self.send_report(tx, ty, 0,0)?;
313+
dx -= tx;
314+
dy -= ty;
315+
}
316+
Ok(())
299317
}
300318

301319
pub fn scroll_vertical(&mut self, amount: i8) -> std::io::Result<()> {
@@ -306,4 +324,20 @@ impl Mouse {
306324
self.send_report(0, 0, 0, amount)
307325
}
308326

309-
}
327+
}
328+
329+
fn abs_max(a: i16, b: i16) -> i16 {
330+
if a.abs() >= b.abs() {
331+
a
332+
} else {
333+
b
334+
}
335+
}
336+
337+
fn abs_min(a: i16, b: i16) -> i16 {
338+
if a.abs() <= b.abs() {
339+
a
340+
} else {
341+
b
342+
}
343+
}

0 commit comments

Comments
 (0)