Skip to content

Commit cfc2983

Browse files
committed
Add example for a virtual touchscreen
1 parent 42b58ee commit cfc2983

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

examples/virtual_touch_screen.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use evdev::{
2+
uinput::VirtualDeviceBuilder, AbsInfo, AbsoluteAxisCode, AttributeSet, EventType, InputEvent,
3+
};
4+
use evdev::{KeyCode, KeyEvent, UinputAbsSetup};
5+
use std::thread::sleep;
6+
use std::time::Duration;
7+
8+
fn main() -> std::io::Result<()> {
9+
10+
// Size of the touch screen
11+
let max_x = 1080;
12+
let max_y = 1920;
13+
14+
let abs_setup_x = AbsInfo::new(0, 0, max_x, 0, 0, 0);
15+
let abs_setup_y = AbsInfo::new(0, 0, max_y, 0, 0, 0);
16+
17+
// see https://www.kernel.org/doc/html/v4.17/input/event-codes.html
18+
let mut buttons = AttributeSet::<KeyCode>::new();
19+
buttons.insert(KeyCode::BTN_TOUCH);
20+
21+
let mut device = VirtualDeviceBuilder::new()?
22+
.name("Fake TouchScreen")
23+
.with_keys(&buttons)?
24+
.with_absolute_axis(&UinputAbsSetup::new(AbsoluteAxisCode::ABS_X, abs_setup_x))?
25+
.with_absolute_axis(&UinputAbsSetup::new(AbsoluteAxisCode::ABS_Y, abs_setup_y))?
26+
.build()?;
27+
28+
for path in device.enumerate_dev_nodes_blocking()? {
29+
let path = path?;
30+
println!("Available as {}", path.display());
31+
}
32+
33+
println!("Waiting for Ctrl-C...");
34+
35+
// Emit some touch events
36+
for i in 0..10 {
37+
for j in 0..10 {
38+
let move_x = InputEvent::new_now(EventType::ABSOLUTE.0, AbsoluteAxisCode::ABS_X.0, i);
39+
let move_y = InputEvent::new_now(EventType::ABSOLUTE.0, AbsoluteAxisCode::ABS_Y.0, j);
40+
let down_event = *KeyEvent::new(KeyCode(KeyCode::BTN_TOUCH.0), 1);
41+
42+
device.emit(&[down_event, move_x, move_y]).unwrap();
43+
println!("touching {i}, {j}");
44+
45+
let up_event = *KeyEvent::new(KeyCode(KeyCode::BTN_TOUCH.0), 0);
46+
device.emit(&[up_event])?;
47+
println!("releasing {i}, {j}");
48+
sleep(Duration::from_millis(100));
49+
}
50+
}
51+
52+
Ok(())
53+
}

0 commit comments

Comments
 (0)