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
use crate::handlers::{OnOff, ProcessKeys, HandlerResult};
#[allow(unused_imports)]
use crate::key_codes::{AcceptsKeycode, KeyCode};
#[allow(unused_imports)]
use crate::Keyboard;
use crate::{iter_unhandled_mut, Event, EventStatus, KeyboardState, USBKeyOut};
use alloc::sync::Arc;
use no_std_compat::prelude::v1::*;
use spin::RwLock;
#[derive(Default)]
pub struct KeyOutCatcher {
    keys_registered: Vec<u8>,
    pub reports: Vec<Vec<u8>>,
    state: KeyboardState,
}
impl KeyOutCatcher {
    pub fn new() -> KeyOutCatcher {
        KeyOutCatcher {
            keys_registered: Vec::new(),
            reports: Vec::new(),
            state: KeyboardState::new(),
        }
    }
    // for testing, clear the catcher of everything
    pub fn clear(&mut self) {
        self.keys_registered.clear();
        self.reports.clear();
    }
}
impl USBKeyOut for KeyOutCatcher {
    fn state(&mut self) -> &mut KeyboardState {
        return &mut self.state;
    }

    fn ro_state(&self) -> &KeyboardState {
        return &self.state;
    }

    #[allow(unused_variables)]
    fn debug(&mut self, s: &str) {
        #[cfg(test)]
        println!("{}", s);
    }

    fn bootloader(&mut self) {}

    fn send_keys(&mut self, keys: &[KeyCode]) {
        self.reports.push(keys.iter().map(|&x| x.to_u8()).collect());
    }
    fn register_key(&mut self, key: KeyCode) {
        if !self.keys_registered.iter().any(|x| *x == key.to_u8()) {
            self.keys_registered.push(key.to_u8());
        }
    }
    fn send_registered(&mut self) {
        self.reports.push(self.keys_registered.clone());
        self.keys_registered.clear();
    }
    fn send_empty(&mut self) {
        self.reports.push(Vec::new());
    }
}
#[cfg(test)]
pub fn check_output(keyboard: &Keyboard<KeyOutCatcher>, should: &[&[KeyCode]]) {
    if !(should.len() == keyboard.output.reports.len()) {
        dbg!(&keyboard.output.reports);
        dbg!(&should);
    }
    assert!(should.len() == keyboard.output.reports.len());
    for (ii, report) in should.iter().enumerate() {
        if !(keyboard.output.reports[ii].len() == report.len()) {
            dbg!(&keyboard.output.reports);
            dbg!(&should);
        }
        assert!(keyboard.output.reports[ii].len() == report.len());
        for k in report.iter() {
            let kcu: u8 = (*k).to_u8();
            if !(keyboard.output.reports[ii].contains(&kcu)) {
                dbg!(&keyboard.output.reports);
                dbg!(&should);
            }
            assert!(keyboard.output.reports[ii].contains(&kcu));
        }
    }
}
/// send a key whenever a time out occurs
pub struct TimeoutLogger {
    keycode: KeyCode,
    min_timeout_ms: u16,
}
impl TimeoutLogger {
    pub fn new(keycode: KeyCode, min_timeout_ms: u16) -> TimeoutLogger {
        TimeoutLogger {
            keycode,
            min_timeout_ms,
        }
    }
}
impl<T: USBKeyOut> ProcessKeys<T> for TimeoutLogger {
    fn process_keys(&mut self, events: &mut Vec<(Event, EventStatus)>, output: &mut T) -> HandlerResult {
        for (event, _status) in iter_unhandled_mut(events) {
            if let Event::TimeOut(ms_since_last) = event {
                if *ms_since_last > self.min_timeout_ms {
                    output.send_keys(&[self.keycode]);
                }
            }
        }
        HandlerResult::NoOp
    }
}
#[derive(Debug)]
pub struct PressCounter {
    pub down_counter: u8,
    pub up_counter: u8,
}
impl OnOff for Arc<RwLock<PressCounter>> {
    fn on_activate(&mut self, output: &mut dyn USBKeyOut) {
        self.write().down_counter += 1;
        output.send_keys(&[KeyCode::H]);
    }
    fn on_deactivate(&mut self, output: &mut dyn USBKeyOut) {
        self.write().up_counter += 1;
        output.send_keys(&[KeyCode::I]);
    }
}
impl OnOff for PressCounter {
    fn on_activate(&mut self, output: &mut dyn USBKeyOut) {
        self.down_counter += 1;
        output.send_keys(&[KeyCode::H]);
    }
    fn on_deactivate(&mut self, output: &mut dyn USBKeyOut) {
        self.up_counter += 1;
        output.send_keys(&[KeyCode::I]);
    }
}
#[cfg(test)]
pub struct Debugger {
    s: String,
}
#[cfg(test)]
impl Debugger {
    pub fn new(s: &str) -> Debugger {
        Debugger { s: s.to_string() }
    }
}
#[cfg(test)]
impl<T: USBKeyOut> ProcessKeys<T> for Debugger {
    fn process_keys(&mut self, events: &mut Vec<(Event, EventStatus)>, _output: &mut T) -> HandlerResult {
        println!("{}, {:?}", self.s, events);
        HandlerResult::NoOp
    }
}

#[cfg(test)]
pub trait Checks {
    /// press check
    fn pc(&mut self, key: impl AcceptsKeycode, should: &[&[KeyCode]]);
    /// release and check
    fn rc(&mut self, key: impl AcceptsKeycode, should: &[&[KeyCode]]);
    /// timeout and check
    fn tc(&mut self, ms_since_last: u16, should: &[&[KeyCode]]);
    /// 
    /// press check with defined ms_since
    fn pct(&mut self, key: impl AcceptsKeycode, ms_since_last: u16, should: &[&[KeyCode]]);
    /// release check with defined ms_since
    fn rct(&mut self, key: impl AcceptsKeycode, ms_since_last: u16, should: &[&[KeyCode]]);
}

#[cfg(test)]
impl Checks for Keyboard<'_, KeyOutCatcher> {
    fn pc(&mut self, key: impl AcceptsKeycode, should: &[&[KeyCode]]) {
        self.add_keypress(key, 50);
        self.handle_keys().unwrap();
        check_output(self, should);
        self.output.clear();
    }
    fn rc(&mut self, key: impl AcceptsKeycode, should: &[&[KeyCode]]) {
        self.add_keyrelease(key, 50);
        self.handle_keys().unwrap();
        check_output(self, should);
        self.output.clear();
    }
    fn tc(&mut self, ms_since_last: u16, should: &[&[KeyCode]]) {
        self.add_timeout(ms_since_last);
        self.handle_keys().unwrap();
        check_output(self, should);
        self.output.clear();
    }
    fn pct(&mut self, key: impl AcceptsKeycode, ms_since_last: u16, should: &[&[KeyCode]]) {
        self.add_keypress(key, ms_since_last);
        self.handle_keys().unwrap();
        check_output(self, should);
        self.output.clear();
    }
    fn rct(&mut self, key: impl AcceptsKeycode, ms_since_last: u16, should: &[&[KeyCode]]) {
        self.add_keyrelease(key, ms_since_last);
        self.handle_keys().unwrap();
        check_output(self, should);
        self.output.clear();
    }
}