| 1 |
|
| 2 |
|
| 3 |
use evdev::{ |
| 4 |
uinput::VirtualDevice, AttributeSet, EventSummary, FFEffectCode, FFStatusCode, InputEvent, |
| 5 |
UInputCode, |
| 6 |
}; |
| 7 |
use std::collections::BTreeSet; |
| 8 |
|
| 9 |
fn main() -> std::io::Result<()> { |
| 10 |
let mut device = VirtualDevice::builder()? |
| 11 |
.name("Fake Force Feedback") |
| 12 |
.with_ff(&AttributeSet::from_iter([FFEffectCode::FF_RUMBLE]))? |
| 13 |
.with_ff_effects_max(16) |
| 14 |
.build()?; |
| 15 |
|
| 16 |
for path in device.enumerate_dev_nodes_blocking()? { |
| 17 |
let path = path?; |
| 18 |
println!("Available as {}", path.display()); |
| 19 |
} |
| 20 |
|
| 21 |
let mut ids: BTreeSet<u16> = (0..16).collect(); |
| 22 |
|
| 23 |
println!("Waiting for Ctrl-C..."); |
| 24 |
loop { |
| 25 |
let events: Vec<InputEvent> = device.fetch_events()?.collect(); |
| 26 |
|
| 27 |
const STOPPED: i32 = FFStatusCode::FF_STATUS_STOPPED.0 as i32; |
| 28 |
const PLAYING: i32 = FFStatusCode::FF_STATUS_PLAYING.0 as i32; |
| 29 |
|
| 30 |
for event in events { |
| 31 |
match event.destructure() { |
| 32 |
EventSummary::UInput(event, UInputCode::UI_FF_UPLOAD, ..) => { |
| 33 |
let mut event = device.process_ff_upload(event)?; |
| 34 |
let id = ids.iter().next().copied(); |
| 35 |
match id { |
| 36 |
Some(id) => { |
| 37 |
ids.remove(&id); |
| 38 |
event.set_effect_id(id as i16); |
| 39 |
event.set_retval(0); |
| 40 |
} |
| 41 |
None => { |
| 42 |
event.set_retval(-1); |
| 43 |
} |
| 44 |
} |
| 45 |
println!("upload effect {:?}", event.effect()); |
| 46 |
} |
| 47 |
EventSummary::UInput(event, UInputCode::UI_FF_ERASE, ..) => { |
| 48 |
let event = device.process_ff_erase(event)?; |
| 49 |
ids.insert(event.effect_id() as u16); |
| 50 |
println!("erase effect ID = {}", event.effect_id()); |
| 51 |
} |
| 52 |
EventSummary::ForceFeedback(.., effect_id, STOPPED) => { |
| 53 |
println!("stopped effect ID = {}", effect_id.0); |
| 54 |
} |
| 55 |
EventSummary::ForceFeedback(.., effect_id, PLAYING) => { |
| 56 |
println!("playing effect ID = {}", effect_id.0); |
| 57 |
} |
| 58 |
_ => { |
| 59 |
println!("event = {:?}", event); |
| 60 |
} |
| 61 |
}; |
| 62 |
} |
| 63 |
} |
| 64 |
} |