| 1 |
use std::time::Duration; |
| 2 |
|
| 3 |
use ashpd::desktop::input_capture::{ |
| 4 |
Barrier, BarrierID, Capabilities, CreateSessionOptions, InputCapture, ReleaseOptions, |
| 5 |
StartOptions, |
| 6 |
}; |
| 7 |
use futures_util::StreamExt; |
| 8 |
|
| 9 |
#[tokio::main] |
| 10 |
async fn main() -> ashpd::Result<()> { |
| 11 |
let input_capture = InputCapture::new().await?; |
| 12 |
|
| 13 |
let capabilities = Capabilities::Keyboard | Capabilities::Pointer; |
| 14 |
// Try create_session2 + start first, fall back to legacy |
| 15 |
let session = match input_capture.create_session2(Default::default()).await { |
| 16 |
Ok(session) => { |
| 17 |
let start_request = input_capture |
| 18 |
.start( |
| 19 |
&session, |
| 20 |
None, |
| 21 |
StartOptions::default().set_capabilities(capabilities), |
| 22 |
) |
| 23 |
.await?; |
| 24 |
let _start_response = start_request.response()?; |
| 25 |
session |
| 26 |
} |
| 27 |
Err(ashpd::Error::RequiresVersion(_want, _have)) => { |
| 28 |
let (session, _caps) = input_capture |
| 29 |
.create_session( |
| 30 |
None, |
| 31 |
CreateSessionOptions::default().set_capabilities(capabilities), |
| 32 |
) |
| 33 |
.await?; |
| 34 |
session |
| 35 |
} |
| 36 |
Err(err) => { |
| 37 |
return Err(err); |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
let zones_response = input_capture.zones(&session, Default::default()).await?; |
| 42 |
let zones = zones_response.response()?; |
| 43 |
println!("Available Zones: {:?}", zones); |
| 44 |
|
| 45 |
// Set up barriers on all edges of each zone, even the overlapping ones (if we |
| 46 |
// have multiple zones). Those will fail and that's fine. The alternative is |
| 47 |
// a more sophisticated algorithm to only set up barriers on the outside |
| 48 |
// edges. |
| 49 |
let barriers: Vec<Barrier> = |
| 50 |
zones |
| 51 |
.regions() |
| 52 |
.iter() |
| 53 |
.enumerate() |
| 54 |
.flat_map(|(n, region)| { |
| 55 |
let x = region.x_offset(); |
| 56 |
let y = region.y_offset(); |
| 57 |
let width = region.width() as i32; |
| 58 |
let height = region.height() as i32; |
| 59 |
|
| 60 |
// Create barriers for all four edges |
| 61 |
let edges = [ |
| 62 |
("left", (x, y, x, y + height - 1)), |
| 63 |
("right", (x + width - 1, y, x + width - 1, y + height - 1)), |
| 64 |
("top", (x, y, x + width - 1, y)), |
| 65 |
("bottom", (x, y + height - 1, x + width - 1, y + height - 1)), |
| 66 |
]; |
| 67 |
|
| 68 |
edges.into_iter().enumerate().map(move |(edge_idx, (edge_name, position))| { |
| 69 |
// Create unique barrier ID: zone_n * 4 + edge_idx + 1 |
| 70 |
let barrier_id = (n * 4 + edge_idx + 1) as u32; |
| 71 |
let id = BarrierID::new(barrier_id).expect("barrier-id must be non-zero"); |
| 72 |
|
| 73 |
println!( |
| 74 |
"Creating barrier {} ({} edge) at position {:?} for zone ({}, {}, {}x{})", |
| 75 |
id, |
| 76 |
edge_name, |
| 77 |
position, |
| 78 |
region.x_offset(), |
| 79 |
region.y_offset(), |
| 80 |
region.width(), |
| 81 |
region.height() |
| 82 |
); |
| 83 |
|
| 84 |
Barrier::new(id, position) |
| 85 |
}) |
| 86 |
}) |
| 87 |
.collect(); |
| 88 |
|
| 89 |
let barriers_response = input_capture |
| 90 |
.set_pointer_barriers(&session, &barriers, zones.zone_set(), Default::default()) |
| 91 |
.await?; |
| 92 |
|
| 93 |
let result = barriers_response.response()?; |
| 94 |
let failed = result.failed_barriers(); |
| 95 |
|
| 96 |
if !failed.is_empty() { |
| 97 |
println!("Failed barriers: {:?}", failed); |
| 98 |
} else { |
| 99 |
println!("All barriers set successfully"); |
| 100 |
} |
| 101 |
|
| 102 |
// Actual input events will be coming from the EIS file descriptor, once |
| 103 |
// any barrier activates. |
| 104 |
let eifd = input_capture |
| 105 |
.connect_to_eis(&session, Default::default()) |
| 106 |
.await?; |
| 107 |
println!("Connected to EIS, fd: {:?}", eifd); |
| 108 |
|
| 109 |
// Enable input capture so the barriers can trigger |
| 110 |
input_capture.enable(&session, Default::default()).await?; |
| 111 |
println!("Input capture enabled - move cursor to any edge to trigger"); |
| 112 |
|
| 113 |
// Listen for activation events, once we get the event we'll see EI events |
| 114 |
// flow too. |
| 115 |
let mut activated_stream = input_capture.receive_activated().await?; |
| 116 |
let mut deactivated_stream = input_capture.receive_deactivated().await?; |
| 117 |
|
| 118 |
println!("\nWaiting for input capture to be triggered..."); |
| 119 |
println!("Press Ctrl+C to exit\n"); |
| 120 |
|
| 121 |
loop { |
| 122 |
tokio::select! { |
| 123 |
Some(activated) = activated_stream.next() => { |
| 124 |
println!("Input capture ACTIVATED!"); |
| 125 |
println!(" Activation ID: {:?}", activated.activation_id()); |
| 126 |
println!(" Cursor position: {:?}", activated.cursor_position()); |
| 127 |
println!(" Barrier ID: {:?}", activated.barrier_id()); |
| 128 |
println!(" (Input events would now be captured via libei)"); |
| 129 |
|
| 130 |
// Once InputCapture activates, we can't interact with the machine anymore so |
| 131 |
// force a InputCapture.Release after 5s. |
| 132 |
println!(" Waiting 5 seconds before releasing..."); |
| 133 |
tokio::time::sleep(Duration::from_secs(5)).await; |
| 134 |
|
| 135 |
// Release the input capture |
| 136 |
input_capture.release(&session, ReleaseOptions::default().set_activation_id(activated.activation_id())).await?; |
| 137 |
println!(" Released input capture"); |
| 138 |
|
| 139 |
// And re-enable so we can play this game again. But wait a second first |
| 140 |
// to give the user a chance to move away from the barrier. |
| 141 |
tokio::time::sleep(Duration::from_secs(1)).await; |
| 142 |
input_capture.enable(&session, Default::default()).await?; |
| 143 |
} |
| 144 |
Some(deactivated) = deactivated_stream.next() => { |
| 145 |
println!("Input capture DEACTIVATED"); |
| 146 |
println!(" Activation ID: {:?}", deactivated.activation_id()); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |