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
use std::sync::mpsc::TryIter;
use devices::DevicesDisplayInfo;
use devices::{Devices, JoystickState};
use event::RawEvent;
use std::sync::mpsc::TryRecvError;
use rawinput::{get_event, get_joystick_state};
use registrar;
use std::time::{SystemTime, UNIX_EPOCH};
use winapi::shared::minwindef::UINT;
use winapi::shared::windef::HWND;
use winapi::um::libloaderapi::GetModuleHandleW;
use winapi::um::winuser::{
    CreateWindowExW, DefWindowProcW, RegisterClassExW, CW_USEDEFAULT, HWND_MESSAGE, WNDCLASSEXW,
};

use std::collections::VecDeque;
use std::ffi::OsStr;
use std::mem;
use std::os::windows::ffi::OsStrExt;
use std::ptr;
use std::thread;
use std::thread::JoinHandle;
use std::collections::HashSet;
use std::iter::FromIterator;

use std::sync::mpsc::{channel, Receiver, Sender};

enum Command {
    Register(DeviceType),
    FilterDevices(Vec<String>),
    UnfilterDevices,
    GetEvent,
    GetJoystickState(usize),
    Finish,
    PrintDeviceList,
    GetDeviceList,
    GetDeviceStats,
}

/// Types of Raw Input Device
#[derive(PartialEq, Eq, Clone, Hash)]
pub enum DeviceType {
    Mice,
    Keyboards,
    Joysticks(XInputInclude),
}

/// Denotes if Xbox360 controllers should be used
/// Please Note: Rawinput support for official Xbox360 controllers
/// is very limited (triggers share same axis, no support for
/// rumble or the central X button)
/// Please see https://en.wikipedia.org/wiki/DirectInput#Xbox_360_Controller_support
/// for more details
#[derive(PartialEq, Eq, Clone, Hash)]
pub enum XInputInclude {
    True,
    False,
}

#[derive(Default)]
pub struct DeviceStats {
    pub number_of_mice: usize,
    pub number_of_keyboards: usize,
    pub number_of_joysticks: usize,
}

/// Manages Raw Input Processing
pub struct RawInputManager {
    joiner: Option<JoinHandle<()>>,
    sender: Sender<Command>,
    receiver: Receiver<RawEvent>,
    joystick_receiver: Receiver<Option<JoystickState>>,
    device_info_receiver: Receiver<DevicesDisplayInfo>,
    device_stats_receiver: Receiver<DeviceStats>,
}

impl RawInputManager {
    pub fn new() -> Result<RawInputManager, &'static str> {
        let (tx, rx) = channel();
        let (tx2, rx2) = channel();
        let (tx_joy, rx_joy) = channel();
        let (tx_devices, rx_devices) = channel();
        let (tx_stats, rx_stats) = channel();

        let joiner = thread::spawn(move || {
            let hwnd = setup_message_window();
            let mut event_queue = VecDeque::new();
            let mut devices = Devices::new();
            let mut exit = false;
            let mut registrar = registrar::RawInputRegistrar::new();
            while !exit {
                match rx.try_recv() {
                    Err(TryRecvError::Disconnected) => {
                        panic!("Multinput Thread Unexpectedly Disconnected!")
                    }
                    Err(TryRecvError::Empty) => {
                        std::thread::sleep(std::time::Duration::from_nanos(1));
                    }
                    Ok(Command::Register(thing)) => {
                        devices = registrar.register_devices(hwnd, thing).unwrap();
                    }
                    Ok(Command::FilterDevices(strings)) => {
                        devices.filter_device_map(HashSet::from_iter(strings.into_iter()));
                    }
                    Ok(Command::UnfilterDevices) => {
                        devices.reset_device_map();
                    }
                    Ok(Command::GetEvent) => {
                        if let Some(event) = get_event(&mut event_queue, &mut devices) {
                            tx2.send(event).unwrap()
                        }
                    }
                    Ok(Command::Finish) => {
                        exit = true;
                    }
                    Ok(Command::GetJoystickState(id)) => {
                        tx_joy.send(get_joystick_state(&devices, id)).unwrap()
                    }
                    Ok(Command::PrintDeviceList) => print_raw_device_list(&devices),
                    Ok(Command::GetDeviceList) => tx_devices.send(devices.clone().into()).unwrap(),
                    Ok(Command::GetDeviceStats) => tx_stats.send(get_device_stats(&devices)).unwrap(),
                };
            }
        });
        Ok(RawInputManager {
            joiner: Some(joiner),
            sender: tx,
            receiver: rx2,
            joystick_receiver: rx_joy,
            device_stats_receiver: rx_stats,
            device_info_receiver: rx_devices
        })
    }

    /// Allows Raw Input devices of type device_type to be received from the Input Manager
    pub fn register_devices(&mut self, device_type: DeviceType) {
        self.sender.send(Command::Register(device_type)).unwrap();
    }

    /// Filters events returned to the list of names provided by the device_names list
    /// Warning: you still need to register the corresponding device types beforehand for this to work!
    pub fn filter_devices(&mut self, device_names: Vec<String>) {
        self.sender.send(Command::FilterDevices(device_names)).unwrap();
    }

    /// Undoes the application of filter_devices()
    pub fn unfilter_devices(&mut self) {
        self.sender.send(Command::UnfilterDevices).unwrap();
    }

    /// Get Event from the Input Manager
    pub fn get_event(&mut self) -> Option<RawEvent> {
        self.sender.send(Command::GetEvent).unwrap();
        match self.receiver.try_recv() {
            Ok(event) => Some(event),
            _ => None 
        }
    }

    /// Get All Events from the Input Manager
    pub fn get_events(&mut self) -> TryIter<RawEvent> {
        self.sender.send(Command::GetEvent).unwrap();
        self.receiver.try_iter()
    }

    /// Get Joystick State from the Input Manager
    pub fn get_joystick_state(&mut self, id: usize) -> Option<JoystickState> {
        self.sender.send(Command::GetJoystickState(id)).unwrap();
        self.joystick_receiver.recv().unwrap()
    }

    /// Print List of Potential Input Devices
    pub fn print_device_list(&self) {
        self.sender.send(Command::PrintDeviceList).unwrap();
    }

    /// Get Device Stats (number of connected devices)
    pub fn get_device_stats(&self) -> DeviceStats {
        self.sender.send(Command::GetDeviceStats).unwrap();
        self.device_stats_receiver.recv().unwrap()
    }

    /// Get Device list
    pub fn get_device_list(&self) -> DevicesDisplayInfo {
            self.sender.send(Command::GetDeviceList).unwrap();
            self.device_info_receiver.recv().unwrap()
    }
}

impl Drop for RawInputManager {
    fn drop(&mut self) {
        self.sender.send(Command::Finish).unwrap();
        self.joiner.take().unwrap().join().unwrap();
    }
}

fn setup_message_window() -> HWND {
    let hwnd: HWND;
    unsafe {
        let hinstance = GetModuleHandleW(ptr::null());
        if hinstance == ptr::null_mut() {
            panic!("Instance Generation Failed");
        }

        let current_time = SystemTime::now();
        let classname_str = format!(
            "RawInput Hidden Window - {:?}",
            current_time.duration_since(UNIX_EPOCH).unwrap()
        );

        let classname = OsStr::new(&classname_str)
            .encode_wide()
            .chain(Some(0).into_iter())
            .collect::<Vec<_>>();

        let wcex = WNDCLASSEXW {
            cbSize: (mem::size_of::<WNDCLASSEXW>()) as UINT,
            cbClsExtra: 0,
            cbWndExtra: 0,
            hbrBackground: ptr::null_mut(),
            hCursor: ptr::null_mut(),
            hIcon: ptr::null_mut(),
            hIconSm: ptr::null_mut(),
            hInstance: hinstance,
            lpfnWndProc: Some(DefWindowProcW),
            lpszClassName: classname.as_ptr(),
            lpszMenuName: ptr::null_mut(),
            style: 0,
        };
        let a = RegisterClassExW(&wcex);
        if a == 0 {
            panic!("Registering WindowClass Failed!");
        }

        hwnd = CreateWindowExW(
            0,
            classname.as_ptr(),
            classname.as_ptr(),
            0,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            HWND_MESSAGE,
            ptr::null_mut(),
            hinstance,
            ptr::null_mut(),
        );
        if hwnd.is_null() {
            panic!("Window Creation Failed!");
        }
    }
    hwnd
}

/// Prints a list of all available raw input devices
fn print_raw_device_list(devices: &Devices) {
    println!("Mice:");
    for mouse in &devices.mice {
        println!("{:?}", mouse);
    }
    println!("Keyboards:");
    for keyboard in &devices.keyboards {
        println!("{:?}", keyboard);
    }
    println!("Hids:");
    for joystick in &devices.joysticks {
        println!("{:?}", joystick);
    }
}

fn get_device_stats(devices: &Devices) -> DeviceStats {
    DeviceStats {
        number_of_mice: devices.mice.len(),
        number_of_keyboards: devices.keyboards.len(),
        number_of_joysticks: devices.joysticks.len(),
    }
}