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
#![doc = include_str!("../README.md")]
#![cfg(unix)]
#![warn(missing_docs, rust_2018_idioms)]
// buggy: https://github.com/rust-lang/rust-clippy/issues?q=is%3Aissue+derive_partial_eq_without_eq
#![allow(clippy::derive_partial_eq_without_eq)]

use std::{
    collections::HashMap,
    io::{self, Read, Write},
    sync::{
        atomic::{AtomicBool, Ordering::Relaxed},
        Arc,
    },
};

use arci::{gamepad::*, *};
use termios::{tcsetattr, Termios};
use tracing::{debug, error};

#[derive(Debug)]
struct State {
    sender: flume::Sender<GamepadEvent>,
    key_state: HashMap<char, bool>,
    button_map: HashMap<char, Button>,
}

#[rustfmt::skip]
const LEFT_STICK_KEYS: &[char] = &[
    'q', 'w', 'e',
    'a', 's', 'd',
    'z', 'x', 'c',
];
#[rustfmt::skip]
const RIGHT_STICK_KEYS: &[char] = &[
    'u', 'i', 'o',
    'j', 'k', 'l',
    'm', ',', '.',
];
const DEFAULT_AXIS_VALUE: f64 = 0.3;

impl State {
    fn new(sender: flume::Sender<GamepadEvent>) -> Self {
        let mut key_state = HashMap::new();
        for ch in ('0'..='9').chain('a'..='z') {
            key_state.insert(ch, false);
        }
        key_state.insert(',', false);
        key_state.insert('.', false);

        Self {
            sender,
            key_state,
            button_map: button_map(),
        }
    }

    fn right_stick(&mut self, sign_x: i8, sign_y: i8) {
        self.send(GamepadEvent::AxisChanged(
            Axis::RightStickX,
            sign_x as f64 * DEFAULT_AXIS_VALUE,
        ));
        self.send(GamepadEvent::AxisChanged(
            Axis::RightStickY,
            sign_y as f64 * DEFAULT_AXIS_VALUE,
        ));
    }

    fn left_stick(&mut self, sign_x: i8, sign_y: i8) {
        self.send(GamepadEvent::AxisChanged(
            Axis::LeftStickX,
            sign_x as f64 * DEFAULT_AXIS_VALUE,
        ));
        self.send(GamepadEvent::AxisChanged(
            Axis::LeftStickY,
            sign_y as f64 * DEFAULT_AXIS_VALUE,
        ));
    }

    fn send_left_stick(&mut self, ch: char) {
        match ch {
            'q' => {
                self.left_stick(1, 1);
            }
            'w' => {
                self.left_stick(0, 1);
            }
            'e' => {
                self.left_stick(-1, 1);
            }
            'a' => {
                self.left_stick(1, 0);
            }
            's' => {
                self.left_stick(0, 0);
            }
            'd' => {
                self.left_stick(-1, 0);
            }
            'z' => {
                self.left_stick(1, -1);
            }
            'x' => {
                self.left_stick(0, -1);
            }
            'c' => {
                self.left_stick(-1, -1);
            }
            _ => unreachable!(),
        }
    }

    fn send_right_stick(&mut self, ch: char) {
        match ch {
            'u' => {
                self.right_stick(1, 1);
            }
            'i' => {
                self.right_stick(0, 1);
            }
            'o' => {
                self.right_stick(-1, 1);
            }
            'j' => {
                self.right_stick(1, 0);
            }
            'k' => {
                self.right_stick(0, 0);
            }
            'l' => {
                self.right_stick(-1, 0);
            }
            'm' => {
                self.right_stick(1, -1);
            }
            ',' => {
                self.right_stick(0, -1);
            }
            '.' => {
                self.right_stick(-1, -1);
            }
            _ => unreachable!(),
        }
    }

    fn send_button(&mut self, ch: char) {
        if let Some(&button) = self.button_map.get(&ch) {
            let active = self.key_state.get_mut(&ch).unwrap();
            *active = !*active;
            if *active {
                self.send(GamepadEvent::ButtonPressed(button));
            } else {
                self.send(GamepadEvent::ButtonReleased(button));
            }
        }
    }

    fn send(&self, event: GamepadEvent) {
        debug!("sending {event:?}");
        if let Err(e) = self.sender.send(event) {
            error!("{e}");
        }
    }

    fn send_event(&mut self, ch: char) {
        if LEFT_STICK_KEYS.contains(&ch) {
            self.send_left_stick(ch);
        } else if RIGHT_STICK_KEYS.contains(&ch) {
            self.send_right_stick(ch);
        } else {
            self.send_button(ch);
        }
    }
}

fn button_map() -> HashMap<char, Button> {
    let mut map = HashMap::new();
    map.insert('1', Button::LeftTrigger);
    map.insert('2', Button::LeftTrigger2);
    map.insert('3', Button::LeftThumb);

    map.insert('6', Button::Select);
    map.insert('7', Button::Start);

    map.insert('8', Button::RightTrigger);
    map.insert('9', Button::RightTrigger2);
    map.insert('0', Button::RightThumb);

    // <^>v
    map.insert('5', Button::DPadUp);
    map.insert('r', Button::DPadLeft);
    map.insert('t', Button::DPadRight);
    map.insert('f', Button::DPadDown);

    // △○□x
    map.insert('y', Button::North);
    map.insert('g', Button::West);
    map.insert('h', Button::East);
    map.insert('b', Button::South);

    map
}

/// [`arci::Gamepad`] implementation for keyboard.
pub struct KeyboardGamepad {
    receiver: flume::Receiver<GamepadEvent>,
    is_running: Arc<AtomicBool>,
}

impl KeyboardGamepad {
    /// Creates a new `KeyboardGamepad`.
    pub fn new() -> Self {
        let (sender, receiver) = flume::unbounded();
        let is_running = Arc::new(AtomicBool::new(true));
        let is_running_cloned = is_running.clone();

        // Based on https://stackoverflow.com/questions/26321592/how-can-i-read-one-character-from-stdin-without-having-to-hit-enter
        let stdin = 0; // couldn't get std::os::unix::io::FromRawFd to work
                       // on /dev/stdin or /dev/tty
        let termios = Termios::from_fd(stdin).unwrap();
        let mut new_termios = termios; // make a mutable copy of termios
                                       // that we will modify
        new_termios.c_lflag &= !(termios::ICANON | termios::ECHO); // no echo and canonical mode
        tcsetattr(stdin, termios::TCSANOW, &new_termios).unwrap();
        let mut reader = io::stdin();
        io::stdout().lock().flush().unwrap();
        std::thread::spawn(move || {
            let mut state = State::new(sender);
            while is_running_cloned.load(Relaxed) {
                let mut buffer = [0; 1]; // read exactly one byte
                reader.read_exact(&mut buffer).unwrap();
                let b = buffer[0];
                if b.is_ascii() {
                    state.send_event(b as char);
                    continue;
                }
                debug!("non-ascii input: {b}");
            }
            tcsetattr(stdin, termios::TCSANOW, &termios).unwrap(); // reset the stdin to
        });

        Self {
            receiver,
            is_running,
        }
    }
}

impl Default for KeyboardGamepad {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Gamepad for KeyboardGamepad {
    async fn next_event(&self) -> GamepadEvent {
        match self.receiver.recv_async().await {
            Ok(e) => e,
            Err(e) => {
                error!("recv error: {e}");
                GamepadEvent::Unknown
            }
        }
    }

    fn stop(&self) {
        self.is_running.store(false, Relaxed);
    }
}

impl Drop for KeyboardGamepad {
    fn drop(&mut self) {
        self.stop();
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;

    const TIMEOUT: Duration = Duration::from_secs(1);

    #[test]
    fn button() {
        let (sender, receiver) = flume::unbounded();
        let mut state = State::new(sender);
        let button_map = button_map();

        for (&ch, button) in &button_map {
            state.send_event(ch);
            assert!(
                matches!(receiver.recv_timeout(TIMEOUT).unwrap(), GamepadEvent::ButtonPressed(b) if b == *button)
            );
        }
        for (&ch, button) in &button_map {
            state.send_event(ch);
            assert!(
                matches!(receiver.recv_timeout(TIMEOUT).unwrap(), GamepadEvent::ButtonReleased(b) if b == *button)
            );
        }
    }

    #[test]
    fn axis() {
        let (sender, receiver) = flume::unbounded();
        let mut state = State::new(sender);

        for &ch in LEFT_STICK_KEYS {
            state.send_event(ch);
            assert!(matches!(
                receiver.recv_timeout(TIMEOUT).unwrap(),
                GamepadEvent::AxisChanged(Axis::LeftStickX, _)
            ));
            assert!(matches!(
                receiver.recv_timeout(TIMEOUT).unwrap(),
                GamepadEvent::AxisChanged(Axis::LeftStickY, _)
            ));
        }
        for &ch in RIGHT_STICK_KEYS {
            state.send_event(ch);
            assert!(matches!(
                receiver.recv_timeout(TIMEOUT).unwrap(),
                GamepadEvent::AxisChanged(Axis::RightStickX, _)
            ));
            assert!(matches!(
                receiver.recv_timeout(TIMEOUT).unwrap(),
                GamepadEvent::AxisChanged(Axis::RightStickY, _)
            ));
        }
    }
}