| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use evdev::{uinput::VirtualDevice, AttributeSet, EventType, InputEvent, RelativeAxisCode}; |
| 5 |
use std::thread::sleep; |
| 6 |
use std::time::Duration; |
| 7 |
use MoveDirection::*; |
| 8 |
|
| 9 |
fn main() -> std::io::Result<()> { |
| 10 |
let mut device = VirtualDevice::builder()? |
| 11 |
.name("fake-mouse") |
| 12 |
.with_relative_axes(&AttributeSet::from_iter([ |
| 13 |
RelativeAxisCode::REL_X, |
| 14 |
RelativeAxisCode::REL_Y, |
| 15 |
RelativeAxisCode::REL_WHEEL, |
| 16 |
RelativeAxisCode::REL_HWHEEL, |
| 17 |
]))? |
| 18 |
.build() |
| 19 |
.unwrap(); |
| 20 |
|
| 21 |
for path in device.enumerate_dev_nodes_blocking()? { |
| 22 |
let path = path?; |
| 23 |
println!("Available as {}", path.display()); |
| 24 |
} |
| 25 |
|
| 26 |
println!("Waiting for Ctrl-C..."); |
| 27 |
loop { |
| 28 |
let ev = new_move_mouse_event(Up, 50); |
| 29 |
device.emit(&[ev]).unwrap(); |
| 30 |
println!("Moved mouse up"); |
| 31 |
sleep(Duration::from_millis(100)); |
| 32 |
|
| 33 |
let ev = new_move_mouse_event(Down, 50); |
| 34 |
device.emit(&[ev]).unwrap(); |
| 35 |
println!("Moved mouse down"); |
| 36 |
sleep(Duration::from_millis(100)); |
| 37 |
|
| 38 |
let ev = new_move_mouse_event(Left, 50); |
| 39 |
device.emit(&[ev]).unwrap(); |
| 40 |
println!("Moved mouse left"); |
| 41 |
sleep(Duration::from_millis(100)); |
| 42 |
|
| 43 |
let ev = new_move_mouse_event(Right, 50); |
| 44 |
device.emit(&[ev]).unwrap(); |
| 45 |
println!("Moved mouse right"); |
| 46 |
sleep(Duration::from_millis(100)); |
| 47 |
|
| 48 |
let ev = new_scroll_mouse_event(Up, 1); |
| 49 |
device.emit(&[ev]).unwrap(); |
| 50 |
println!("Scrolled mouse up"); |
| 51 |
sleep(Duration::from_millis(100)); |
| 52 |
|
| 53 |
let ev = new_scroll_mouse_event(Down, 1); |
| 54 |
device.emit(&[ev]).unwrap(); |
| 55 |
println!("Scrolled mouse down"); |
| 56 |
sleep(Duration::from_millis(100)); |
| 57 |
|
| 58 |
let ev = new_scroll_mouse_event(Left, 1); |
| 59 |
device.emit(&[ev]).unwrap(); |
| 60 |
println!("Scrolled mouse left"); |
| 61 |
sleep(Duration::from_millis(100)); |
| 62 |
|
| 63 |
let ev = new_scroll_mouse_event(Right, 1); |
| 64 |
device.emit(&[ev]).unwrap(); |
| 65 |
println!("Scrolled mouse right"); |
| 66 |
sleep(Duration::from_millis(100)); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
enum MoveDirection { |
| 71 |
Up, |
| 72 |
Down, |
| 73 |
Left, |
| 74 |
Right, |
| 75 |
} |
| 76 |
|
| 77 |
fn new_move_mouse_event(direction: MoveDirection, distance: u16) -> InputEvent { |
| 78 |
let (axis, distance) = match direction { |
| 79 |
MoveDirection::Up => (RelativeAxisCode::REL_Y, -i32::from(distance)), |
| 80 |
MoveDirection::Down => (RelativeAxisCode::REL_Y, i32::from(distance)), |
| 81 |
MoveDirection::Left => (RelativeAxisCode::REL_X, -i32::from(distance)), |
| 82 |
MoveDirection::Right => (RelativeAxisCode::REL_X, i32::from(distance)), |
| 83 |
}; |
| 84 |
InputEvent::new_now(EventType::RELATIVE.0, axis.0, distance) |
| 85 |
} |
| 86 |
|
| 87 |
fn new_scroll_mouse_event(direction: MoveDirection, distance: u16) -> InputEvent { |
| 88 |
let (axis, distance) = match direction { |
| 89 |
MoveDirection::Up => (RelativeAxisCode::REL_WHEEL.0, i32::from(distance)), |
| 90 |
MoveDirection::Down => (RelativeAxisCode::REL_WHEEL.0, -i32::from(distance)), |
| 91 |
MoveDirection::Left => (RelativeAxisCode::REL_HWHEEL.0, -i32::from(distance)), |
| 92 |
MoveDirection::Right => (RelativeAxisCode::REL_HWHEEL.0, i32::from(distance)), |
| 93 |
}; |
| 94 |
InputEvent::new_now(EventType::RELATIVE.0, axis, distance) |
| 95 |
} |