anybox-rs 0.11.0

one API for the most popular passthrough boxes
Documentation
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::any::Any;
use std::sync::mpsc;
use std::sync::{Arc, Barrier, Mutex};
use std::time::{Duration, Instant};

use crate::backend::{
    Backend, ChordKind, Command, Input, InputKind, InputKinds, LockSlot, Sink, Typing, UNLOCKED,
    Usage,
};
use crate::capability::{Action, Button, Capabilities, Capability, Family, Info, Snapshot};
use crate::click::{ClickTiming, Holds};
use crate::device::{Device, Start};
use crate::error::{Error, Result};
use crate::event::{BoxTime, Clock};
use crate::extension::{Extension, ExtensionReply, Interpolation};
use crate::key::{Key, MediaKey};

#[derive(Default)]
struct State {
    log: Vec<(Instant, Command)>,
    streams: Vec<InputKinds>,
    /// what the fake streams now, so like the real backends it only records a change, and a reconnect forgets it
    streaming: Option<InputKinds>,
    restarted: Vec<InputKind>,
    released_all: Vec<Vec<Usage>>,
    unlocked_all: Vec<Vec<LockSlot>>,
    /// the cleanup calls in the order they came, `release_all`, `restore_all` and `unlock_all`
    cleanup: Vec<&'static str>,
    fail_next: Option<Error>,
    /// the next stream switch fails for these kinds with this error
    fail_streams: Option<(InputKinds, Error)>,
    /// runs when that failing switch is about to return
    before_failing: Option<Box<dyn FnOnce() + Send>>,
    /// the next restart of a stream fails with this error
    fail_restart: Option<Error>,
    shutdowns: usize,
    fail_releases: bool,
    pause_release: Option<Arc<Barrier>>,
    snapshot: Option<Snapshot>,
    /// every piece of text the fake typed
    pieces: Vec<Vec<u8>>,
    /// runs before each piece with its number, and an error it gives fails that piece
    on_piece: Option<Box<dyn FnMut(usize) -> Option<Error> + Send>>,
    /// every extension call that reached the fake
    extensions: Vec<Extension>,
}

/// a box that does whatever its snapshot says and writes down everything it was asked
struct Fake(Arc<Mutex<State>>);

/// the test's side of a fake box
#[derive(Clone)]
pub(crate) struct Handle {
    state: Arc<Mutex<State>>,
    sink: Sink,
}

pub(crate) fn mouse() -> Capabilities {
    Capabilities::new(vec![
        Button::Left,
        Button::Right,
        Button::Middle,
        Button::Side1,
        Button::Side2,
    ])
    .set(Action::Move, Capability::native())
    .set(Action::Press, Capability::native())
    .set(Action::Release, Capability::native())
    .set(Action::Wheel, Capability::native())
}

pub(crate) fn open(capabilities: Capabilities, click_timing: ClickTiming) -> (Device, Handle) {
    open_as(Family::Medius, capabilities, click_timing)
}

/// the fake, saying it's another family, for the calls only that family has
pub(crate) fn open_as(
    family: Family,
    capabilities: Capabilities,
    click_timing: ClickTiming,
) -> (Device, Handle) {
    let snapshot = Snapshot::new(Info::new(family), capabilities);
    let state = Arc::new(Mutex::new(State {
        snapshot: Some(snapshot.clone()),
        ..State::default()
    }));
    let (signals, incoming) = mpsc::channel();
    let handle = Handle {
        state: state.clone(),
        sink: Sink(signals.clone()),
    };
    let device = Device::start(Start {
        backend: Box::new(Fake(state)),
        snapshot,
        signals,
        incoming,
        click_timing,
        holds: Holds::seeded(42),
        port: "fake".into(),
    })
    .unwrap();
    (device, handle)
}

impl Handle {
    pub(crate) fn log(&self) -> Vec<Command> {
        self.state
            .lock()
            .unwrap()
            .log
            .iter()
            .map(|(_, c)| *c)
            .collect()
    }

    /// waits until the box was asked for this, so a test doesn't have to guess with a sleep
    pub(crate) fn wait_for(&self, command: Command) {
        let deadline = Instant::now() + Duration::from_secs(5);
        while !self.log().contains(&command) {
            assert!(Instant::now() < deadline, "the box never got {command:?}");
            std::thread::sleep(Duration::from_millis(1));
        }
    }

    pub(crate) fn timed_log(&self) -> Vec<(Instant, Command)> {
        self.state.lock().unwrap().log.clone()
    }

    pub(crate) fn streams(&self) -> Vec<InputKinds> {
        self.state.lock().unwrap().streams.clone()
    }

    pub(crate) fn restarted(&self) -> Vec<InputKind> {
        self.state.lock().unwrap().restarted.clone()
    }

    pub(crate) fn released_all(&self) -> Vec<Vec<Usage>> {
        self.state.lock().unwrap().released_all.clone()
    }

    pub(crate) fn cleanup(&self) -> Vec<&'static str> {
        self.state.lock().unwrap().cleanup.clone()
    }

    pub(crate) fn unlocked_all(&self) -> Vec<Vec<LockSlot>> {
        self.state.lock().unwrap().unlocked_all.clone()
    }

    pub(crate) fn extensions(&self) -> Vec<Extension> {
        self.state.lock().unwrap().extensions.clone()
    }

    pub(crate) fn pieces(&self) -> Vec<Vec<u8>> {
        self.state.lock().unwrap().pieces.clone()
    }

    pub(crate) fn on_piece(&self, on_piece: impl FnMut(usize) -> Option<Error> + Send + 'static) {
        self.state.lock().unwrap().on_piece = Some(Box::new(on_piece));
    }

    pub(crate) fn fail_next(&self, error: Error) {
        self.state.lock().unwrap().fail_next = Some(error);
    }

    pub(crate) fn fail_restart(&self, error: Error) {
        self.state.lock().unwrap().fail_restart = Some(error);
    }

    pub(crate) fn shutdowns(&self) -> usize {
        self.state.lock().unwrap().shutdowns
    }

    /// a button whose bytes were read at `read`, which a real link takes before anything else
    pub(crate) fn button_read(&self, button: Button, read: crate::transport::os::Read) {
        self.sink.input(
            Input::Button {
                button,
                pressed: true,
            },
            Instant::now(),
            None,
            Some(read),
        );
    }

    pub(crate) fn fail_streams(&self, kinds: InputKinds, error: Error) {
        self.state.lock().unwrap().fail_streams = Some((kinds, error));
    }

    pub(crate) fn fail_releases(&self, fail: bool) {
        self.state.lock().unwrap().fail_releases = fail;
    }

    /// the next release waits at the barrier twice, once when it starts and once right before it goes out, so the
    /// test can step in between
    pub(crate) fn pause_next_release(&self) -> Arc<Barrier> {
        let barrier = Arc::new(Barrier::new(2));
        self.state.lock().unwrap().pause_release = Some(barrier.clone());
        barrier
    }

    /// the box changes without telling the core, like a reconnect the core hasn't heard of yet
    pub(crate) fn change_quietly(&self, capabilities: Capabilities) {
        let snapshot = Snapshot::new(Info::new(Family::Medius), capabilities);
        self.state.lock().unwrap().snapshot = Some(snapshot);
    }

    pub(crate) fn button(&self, button: Button, pressed: bool) {
        self.sink.input(
            Input::Button { button, pressed },
            Instant::now(),
            Some(BoxTime {
                micros: 1000,
                clock: Clock::MediusHostChip,
            }),
            None,
        );
    }

    pub(crate) fn key(&self, key: Key, pressed: bool) {
        self.sink
            .input(Input::Key { key, pressed }, Instant::now(), None, None);
    }

    pub(crate) fn media(&self, key: MediaKey, pressed: bool) {
        self.sink
            .input(Input::Media { key, pressed }, Instant::now(), None, None);
    }

    pub(crate) fn motion(&self, dx: i32) {
        self.sink.input(
            Input::Motion {
                dx,
                dy: 0,
                wheel: 0,
                pan: 0,
            },
            Instant::now(),
            None,
            None,
        );
    }

    pub(crate) fn dropped(&self, count: u64) {
        self.sink.dropped(count, crate::event::DropSource::Device);
    }

    pub(crate) fn disconnect(&self) {
        self.sink.disconnected();
    }

    /// the same device with different details, like a new key limit
    pub(crate) fn changed(&self, capabilities: Capabilities) {
        let snapshot = Snapshot::new(Info::new(Family::Medius), capabilities);
        self.state.lock().unwrap().snapshot = Some(snapshot.clone());
        self.sink.changed(snapshot);
    }

    /// the box is back, maybe with a different mouse behind it
    pub(crate) fn reconnect(&self, capabilities: Capabilities) {
        let snapshot = Snapshot::new(Info::new(Family::Medius), capabilities);
        self.state.lock().unwrap().snapshot = Some(snapshot.clone());
        self.state.lock().unwrap().streaming = None;
        self.sink.reconnected(snapshot);
    }

    pub(crate) fn stream_lost(&self, kind: InputKind) {
        self.sink.stream_lost(kind);
    }

    pub(crate) fn refused(&self, action: Action) {
        self.sink.refused(action);
    }

    /// the next failing stream switch first reports this refusal on its own and waits until the core published it,
    /// the order a MAKCU's reader can give
    pub(crate) fn refuse_before_the_switch_returns(&self, action: Action) {
        let sink = self.sink.clone();
        self.state.lock().unwrap().before_failing = Some(Box::new(move || {
            sink.refused(action);
            let (done, published) = mpsc::channel();
            let _ = sink.0.send(crate::backend::Signal::Barrier(done));
            let _ = published.recv();
        }));
    }

    pub(crate) fn session_reset(&self) {
        self.state.lock().unwrap().streaming = None;
        self.sink.session_reset();
    }
}

impl Backend for Fake {
    fn send(&self, command: Command) -> Result<()> {
        let release = matches!(command.holds(), Some((_, false)))
            || matches!(
                command,
                Command::Lock { keep: UNLOCKED, .. }
                    | Command::KeyForce { force: false, .. }
                    | Command::KeyChord {
                        kind: ChordKind::Release,
                        ..
                    }
            );
        if release {
            let pause = self.0.lock().unwrap().pause_release.take();
            if let Some(barrier) = pause {
                barrier.wait();
                barrier.wait();
            }
        }
        let mut state = self.0.lock().unwrap();
        if let Some(error) = state.fail_next.take() {
            return Err(error);
        }
        if state.fail_releases && release {
            return Err(Error::Disconnected);
        }
        let snapshot = state.snapshot.as_ref().unwrap();
        // a release always goes through, same as on the real boxes
        if !release && !snapshot.capabilities.get(command.action()).is_supported() {
            return Err(Error::Unsupported {
                action: command.action(),
                family: snapshot.info.family,
                reason: "the box changed during this call, check its snapshot again".into(),
            });
        }
        state.log.push((Instant::now(), command));
        Ok(())
    }

    fn set_streams(
        &self,
        kinds: InputKinds,
    ) -> std::result::Result<(), crate::backend::StreamFailure> {
        let mut state = self.0.lock().unwrap();
        if let Some((failed, error)) = state.fail_streams.take() {
            let before = state.before_failing.take();
            drop(state);
            if let Some(before) = before {
                before();
            }
            return Err(crate::backend::StreamFailure { failed, error });
        }
        if state.streaming != Some(kinds) {
            state.streaming = Some(kinds);
            state.streams.push(kinds);
        }
        Ok(())
    }

    fn restart_stream(&self, kind: InputKind) -> Result<()> {
        let mut state = self.0.lock().unwrap();
        state.restarted.push(kind);
        state.fail_restart.take().map_or(Ok(()), Err)
    }

    fn type_piece(&self, piece: &[u8], typing: &mut Typing) -> Result<()> {
        let mut on_piece = self.0.lock().unwrap().on_piece.take();
        let index = self.0.lock().unwrap().pieces.len();
        let failed = on_piece.as_mut().and_then(|on_piece| on_piece(index));
        let mut state = self.0.lock().unwrap();
        if state.on_piece.is_none() {
            state.on_piece = on_piece;
        }
        // a hook's error stands for a box that failed with the piece already sent
        if let Some(error) = failed {
            typing.sent = true;
            return Err(error);
        }
        let snapshot = state.snapshot.as_ref().unwrap();
        if !snapshot.capabilities.get(Action::TypeText).is_supported() {
            return Err(Error::Unsupported {
                action: Action::TypeText,
                family: snapshot.info.family,
                reason: "the box changed during this call, check its snapshot again".into(),
            });
        }
        state.pieces.push(piece.to_vec());
        typing.sent = true;
        Ok(())
    }

    fn unlock_all(&self, slots: &[LockSlot]) -> Result<()> {
        let mut slots = slots.to_vec();
        slots.sort_by_key(|slot| format!("{slot:?}"));
        let mut state = self.0.lock().unwrap();
        state.unlocked_all.push(slots);
        state.cleanup.push("unlock_all");
        Ok(())
    }

    fn release_all(&self, held: &[Usage]) -> Result<()> {
        let mut held = held.to_vec();
        held.sort_by_key(|usage| format!("{usage:?}"));
        let mut state = self.0.lock().unwrap();
        state.released_all.push(held);
        state.cleanup.push("release_all");
        Ok(())
    }

    fn restore_all(&self, keys: &[Key]) -> Result<()> {
        self.0.lock().unwrap().cleanup.push("restore_all");
        let mut first = Ok(());
        for &key in keys {
            let restored = self.send(Command::KeyForce { key, force: false });
            if first.is_ok() {
                first = restored;
            }
        }
        first
    }

    fn shutdown(&self, _deadline: Duration) {
        self.0.lock().unwrap().shutdowns += 1;
    }

    fn extension(&self, request: Extension) -> Result<ExtensionReply> {
        self.0.lock().unwrap().extensions.push(request);
        Ok(match request {
            Extension::MakcuInterpolation => ExtensionReply::Interpolation(Interpolation::Auto),
            Extension::MakcuMoving => ExtensionReply::Moving(false),
            Extension::MakcuAutorelease => ExtensionReply::Autorelease(None),
            Extension::FerrumCatchXy { .. } => ExtensionReply::Xy(0, 0),
            _ => ExtensionReply::Done,
        })
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}