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
// Copyright 2017 Mateusz Sieczko and other GilRs Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! Gamepad state and other event related functionality.

use gamepad::NativeEvCode;

use vec_map::{self, VecMap};

use std::iter::Iterator;
use std::time::SystemTime;

pub mod filter;


/// Cached gamepad state.
#[derive(Clone, Debug)]
pub struct GamepadState {
    // Indexed by NativeEvCode (nec)
    buttons: VecMap<ButtonData>,
    // Indexed by NativeEvCode (nec)
    axes: VecMap<AxisData>,
    // Mappings, will be dynamically created while we are processing new events.
}

impl GamepadState {
    pub(crate) fn new() -> Self {
        GamepadState {
            buttons: VecMap::new(),
            axes: VecMap::new(),
        }
    }

    /// Returns `true` if given button is pressed. Returns `false` if there is no information about
    /// `btn` or it is not pressed.
    pub fn is_pressed(&self, btn: NativeEvCode) -> bool {
        self.buttons
            .get(btn as usize)
            .map(|s| s.is_pressed())
            .unwrap_or(false)
    }

    /// Returns value of axis or 0.0 when there is no information about axis.
    pub fn value(&self, axis: NativeEvCode) -> f32 {
        self.axes
            .get(axis as usize)
            .map(|s| s.value())
            .unwrap_or(0.0)
    }

    /// Iterate over buttons data.
    pub fn buttons(&self) -> ButtonDataIter {
        ButtonDataIter(self.buttons.iter())
    }

    /// Iterate over axes data.
    pub fn axes(&self) -> AxisDataIter {
        AxisDataIter(self.axes.iter())
    }

    /// Returns button state and when it changed.
    pub fn button_data(&self, btn: NativeEvCode) -> Option<&ButtonData> {
        self.buttons.get(btn as usize)
    }

    /// Returns axis state and when it changed.
    pub fn axis_data(&self, axis: NativeEvCode) -> Option<&AxisData> {
        self.axes.get(axis as usize)
    }

    pub(crate) fn update_btn(&mut self, btn: NativeEvCode, data: ButtonData) {
        self.buttons.insert(btn as usize, data);
    }

    pub(crate) fn update_axis(&mut self, axis: NativeEvCode, data: AxisData) {
        self.axes.insert(axis as usize, data);
    }
}

/// Iterator over `ButtonData`.
pub struct ButtonDataIter<'a>(vec_map::Iter<'a, ButtonData>);

/// Iterator over `AxisData`.
pub struct AxisDataIter<'a>(vec_map::Iter<'a, AxisData>);

impl<'a> Iterator for ButtonDataIter<'a> {
    type Item = (usize, &'a ButtonData);

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

impl<'a> Iterator for AxisDataIter<'a> {
    type Item = (usize, &'a AxisData);

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

/// Information about button stored in `State`.
#[derive(Clone, Copy, Debug)]
pub struct ButtonData {
    last_event_ts: SystemTime,
    state_and_counter: u64,
    // 2b of state (is pressed, is repeating), 62b of counter
}

impl ButtonData {
    pub(crate) fn new(pressed: bool, repeating: bool, counter: u64, time: SystemTime) -> Self {
        debug_assert!(counter <= 0x3FFF_FFFF_FFFF_FFFF);

        let state = ((pressed as u64) << 63) | ((repeating as u64) << 62);
        ButtonData {
            last_event_ts: time,
            state_and_counter: state | counter,
        }
    }

    /// Returns `true` if button is pressed.
    pub fn is_pressed(&self) -> bool {
        self.state_and_counter >> 63 == 1
    }

    /// Returns `true` if button is repeating.
    pub fn is_repeating(&self) -> bool {
        self.state_and_counter & 0x4000_0000_0000_0000 != 0
    }

    /// Returns value of counter when button state last changed.
    pub fn counter(&self) -> u64 {
        self.state_and_counter & 0x3FFF_FFFF_FFFF_FFFF
    }

    /// Returns when button state last changed.
    pub fn timestamp(&self) -> SystemTime {
        self.last_event_ts
    }
}

/// Information about axis stored in `State`.
#[derive(Clone, Copy, Debug)]
pub struct AxisData {
    last_event_ts: SystemTime,
    last_event_c: u64,
    value: f32,
}

impl AxisData {
    pub(crate) fn new(value: f32, counter: u64, time: SystemTime) -> Self {
        AxisData {
            last_event_ts: time,
            last_event_c: counter,
            value,
        }
    }
    /// Returns value of axis.
    pub fn value(&self) -> f32 {
        self.value
    }

    /// Returns value of counter when axis value last changed.
    pub fn counter(&self) -> u64 {
        self.last_event_c
    }

    /// Returns when axis value last changed.
    pub fn timestamp(&self) -> SystemTime {
        self.last_event_ts
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::SystemTime;

    #[test]
    fn button_data() {
        // Who needs rand crate
        let mut state = 1234567890123456789u64;
        let mut xorshift = || {
            let mut x = state;
            x ^= x >> 12;
            x ^= x << 25;
            x ^= x >> 27;
            state = x;

            x.wrapping_mul(0x2545F4914F6CDD1D)
        };

        for _ in 0..(1024 * 1024 * 16) {
            let counter = xorshift() & 0x3FFF_FFFF_FFFF_FFFF;
            let pressed = xorshift() % 2 == 1;
            let repeating = xorshift() % 2 == 1;
            let btn = ButtonData::new(pressed, repeating, counter, SystemTime::now());
            assert_eq!(btn.is_pressed(), pressed);
            assert_eq!(btn.is_repeating(), repeating);
            assert_eq!(btn.counter(), counter);
        }
    }
}