summaryrefslogtreecommitdiffstats
path: root/src/capture_test.rs
blob: 4eafc056cb7870190d58629f5864eacf8e41ab21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::config::Config;
use futures::StreamExt;
use input_capture::{self, CaptureError, InputCapture, InputCaptureError, Position};
use input_event::{Event, KeyboardEvent};
use tokio::task::LocalSet;

pub fn run() -> anyhow::Result<()> {
    log::info!("running input capture test");
    let runtime = tokio::runtime::Builder::new_current_thread()
        .enable_io()
        .enable_time()
        .build()?;

    let config = Config::new()?;

    Ok(runtime.block_on(LocalSet::new().run_until(input_capture_test(config)))?)
}

async fn input_capture_test(config: Config) -> Result<(), InputCaptureError> {
    log::info!("creating input capture");
    let backend = config.capture_backend.map(|b| b.into());
    loop {
        let mut input_capture = input_capture::create(backend).await?;
        log::info!("creating clients");
        input_capture.create(0, Position::Left).await?;
        input_capture.create(1, Position::Right).await?;
        input_capture.create(2, Position::Top).await?;
        input_capture.create(3, Position::Bottom).await?;
        if let Err(e) = do_capture(&mut input_capture).await {
            log::warn!("{e} - recreating capture");
        }
        let _ = input_capture.terminate().await;
    }
}

async fn do_capture(input_capture: &mut Box<dyn InputCapture>) -> Result<(), CaptureError> {
    loop {
        let (client, event) = input_capture
            .next()
            .await
            .ok_or(CaptureError::EndOfStream)??;
        let pos = match client {
            0 => Position::Left,
            1 => Position::Right,
            2 => Position::Top,
            _ => Position::Bottom,
        };
        log::info!("position: {pos}, event: {event}");
        if let Event::Keyboard(KeyboardEvent::Key { key: 1, .. }) = event {
            input_capture.release().await?;
            break Ok(());
        }
    }
}