Skip to main content

pfx_input/
device.rs

1use serde::{Deserialize, Serialize};
2
3use crate::family::Family;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
6pub struct PadId(pub u32);
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
9pub enum DeviceKind {
10    Keyboard,
11    Mouse,
12    Gamepad,
13}
14
15impl DeviceKind {
16    pub fn is_gamepad(self) -> bool {
17        self == DeviceKind::Gamepad
18    }
19}
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
22pub enum Key {
23    A,
24    B,
25    C,
26    D,
27    E,
28    F,
29    G,
30    H,
31    I,
32    J,
33    K,
34    L,
35    M,
36    N,
37    O,
38    P,
39    Q,
40    R,
41    S,
42    T,
43    U,
44    V,
45    W,
46    X,
47    Y,
48    Z,
49    Digit0,
50    Digit1,
51    Digit2,
52    Digit3,
53    Digit4,
54    Digit5,
55    Digit6,
56    Digit7,
57    Digit8,
58    Digit9,
59    F1,
60    F2,
61    F3,
62    F4,
63    F5,
64    F6,
65    F7,
66    F8,
67    F9,
68    F10,
69    F11,
70    F12,
71    Up,
72    Down,
73    Left,
74    Right,
75    Escape,
76    Tab,
77    CapsLock,
78    ShiftLeft,
79    ShiftRight,
80    ControlLeft,
81    ControlRight,
82    AltLeft,
83    AltRight,
84    SuperLeft,
85    SuperRight,
86    ContextMenu,
87    Space,
88    Enter,
89    Backspace,
90    Insert,
91    Delete,
92    Home,
93    End,
94    PageUp,
95    PageDown,
96    PrintScreen,
97    ScrollLock,
98    Pause,
99    NumLock,
100    Minus,
101    Equal,
102    BracketLeft,
103    BracketRight,
104    Backslash,
105    Semicolon,
106    Quote,
107    Backquote,
108    Comma,
109    Period,
110    Slash,
111    Numpad0,
112    Numpad1,
113    Numpad2,
114    Numpad3,
115    Numpad4,
116    Numpad5,
117    Numpad6,
118    Numpad7,
119    Numpad8,
120    Numpad9,
121    NumpadAdd,
122    NumpadSubtract,
123    NumpadMultiply,
124    NumpadDivide,
125    NumpadDecimal,
126    NumpadEnter,
127}
128
129impl Key {
130    pub const ALL: [Key; 104] = [
131        Key::A,
132        Key::B,
133        Key::C,
134        Key::D,
135        Key::E,
136        Key::F,
137        Key::G,
138        Key::H,
139        Key::I,
140        Key::J,
141        Key::K,
142        Key::L,
143        Key::M,
144        Key::N,
145        Key::O,
146        Key::P,
147        Key::Q,
148        Key::R,
149        Key::S,
150        Key::T,
151        Key::U,
152        Key::V,
153        Key::W,
154        Key::X,
155        Key::Y,
156        Key::Z,
157        Key::Digit0,
158        Key::Digit1,
159        Key::Digit2,
160        Key::Digit3,
161        Key::Digit4,
162        Key::Digit5,
163        Key::Digit6,
164        Key::Digit7,
165        Key::Digit8,
166        Key::Digit9,
167        Key::F1,
168        Key::F2,
169        Key::F3,
170        Key::F4,
171        Key::F5,
172        Key::F6,
173        Key::F7,
174        Key::F8,
175        Key::F9,
176        Key::F10,
177        Key::F11,
178        Key::F12,
179        Key::Up,
180        Key::Down,
181        Key::Left,
182        Key::Right,
183        Key::Escape,
184        Key::Tab,
185        Key::CapsLock,
186        Key::ShiftLeft,
187        Key::ShiftRight,
188        Key::ControlLeft,
189        Key::ControlRight,
190        Key::AltLeft,
191        Key::AltRight,
192        Key::SuperLeft,
193        Key::SuperRight,
194        Key::ContextMenu,
195        Key::Space,
196        Key::Enter,
197        Key::Backspace,
198        Key::Insert,
199        Key::Delete,
200        Key::Home,
201        Key::End,
202        Key::PageUp,
203        Key::PageDown,
204        Key::PrintScreen,
205        Key::ScrollLock,
206        Key::Pause,
207        Key::NumLock,
208        Key::Minus,
209        Key::Equal,
210        Key::BracketLeft,
211        Key::BracketRight,
212        Key::Backslash,
213        Key::Semicolon,
214        Key::Quote,
215        Key::Backquote,
216        Key::Comma,
217        Key::Period,
218        Key::Slash,
219        Key::Numpad0,
220        Key::Numpad1,
221        Key::Numpad2,
222        Key::Numpad3,
223        Key::Numpad4,
224        Key::Numpad5,
225        Key::Numpad6,
226        Key::Numpad7,
227        Key::Numpad8,
228        Key::Numpad9,
229        Key::NumpadAdd,
230        Key::NumpadSubtract,
231        Key::NumpadMultiply,
232        Key::NumpadDivide,
233        Key::NumpadDecimal,
234        Key::NumpadEnter,
235    ];
236
237    pub fn follows_layout(self) -> bool {
238        matches!(
239            self,
240            Key::A
241                | Key::B
242                | Key::C
243                | Key::D
244                | Key::E
245                | Key::F
246                | Key::G
247                | Key::H
248                | Key::I
249                | Key::J
250                | Key::K
251                | Key::L
252                | Key::M
253                | Key::N
254                | Key::O
255                | Key::P
256                | Key::Q
257                | Key::R
258                | Key::S
259                | Key::T
260                | Key::U
261                | Key::V
262                | Key::W
263                | Key::X
264                | Key::Y
265                | Key::Z
266                | Key::Minus
267                | Key::Equal
268                | Key::BracketLeft
269                | Key::BracketRight
270                | Key::Backslash
271                | Key::Semicolon
272                | Key::Quote
273                | Key::Backquote
274                | Key::Comma
275                | Key::Period
276                | Key::Slash
277        )
278    }
279
280    pub fn label(self) -> &'static str {
281        match self {
282            Key::A => "A",
283            Key::B => "B",
284            Key::C => "C",
285            Key::D => "D",
286            Key::E => "E",
287            Key::F => "F",
288            Key::G => "G",
289            Key::H => "H",
290            Key::I => "I",
291            Key::J => "J",
292            Key::K => "K",
293            Key::L => "L",
294            Key::M => "M",
295            Key::N => "N",
296            Key::O => "O",
297            Key::P => "P",
298            Key::Q => "Q",
299            Key::R => "R",
300            Key::S => "S",
301            Key::T => "T",
302            Key::U => "U",
303            Key::V => "V",
304            Key::W => "W",
305            Key::X => "X",
306            Key::Y => "Y",
307            Key::Z => "Z",
308            Key::Digit0 | Key::Numpad0 => "0",
309            Key::Digit1 | Key::Numpad1 => "1",
310            Key::Digit2 | Key::Numpad2 => "2",
311            Key::Digit3 | Key::Numpad3 => "3",
312            Key::Digit4 | Key::Numpad4 => "4",
313            Key::Digit5 | Key::Numpad5 => "5",
314            Key::Digit6 | Key::Numpad6 => "6",
315            Key::Digit7 | Key::Numpad7 => "7",
316            Key::Digit8 | Key::Numpad8 => "8",
317            Key::Digit9 | Key::Numpad9 => "9",
318            Key::F1 => "F1",
319            Key::F2 => "F2",
320            Key::F3 => "F3",
321            Key::F4 => "F4",
322            Key::F5 => "F5",
323            Key::F6 => "F6",
324            Key::F7 => "F7",
325            Key::F8 => "F8",
326            Key::F9 => "F9",
327            Key::F10 => "F10",
328            Key::F11 => "F11",
329            Key::F12 => "F12",
330            Key::Up => "^",
331            Key::Down => "v",
332            Key::Left => "<",
333            Key::Right => ">",
334            Key::Escape => "ESC",
335            Key::Tab => "TAB",
336            Key::CapsLock => "CAPS",
337            Key::ShiftLeft | Key::ShiftRight => "SHIFT",
338            Key::ControlLeft | Key::ControlRight => "CTRL",
339            Key::AltLeft | Key::AltRight => "ALT",
340            Key::SuperLeft | Key::SuperRight => "META",
341            Key::ContextMenu => "MENU",
342            Key::Space => "SPACE",
343            Key::Enter | Key::NumpadEnter => "ENTER",
344            Key::Backspace => "BKSP",
345            Key::Insert => "INS",
346            Key::Delete => "DEL",
347            Key::Home => "HOME",
348            Key::End => "END",
349            Key::PageUp => "PGUP",
350            Key::PageDown => "PGDN",
351            Key::PrintScreen => "PRTSC",
352            Key::ScrollLock => "SCRLK",
353            Key::Pause => "PAUSE",
354            Key::NumLock => "NUMLK",
355            Key::Minus | Key::NumpadSubtract => "-",
356            Key::Equal => "=",
357            Key::BracketLeft => "[",
358            Key::BracketRight => "]",
359            Key::Backslash => "\\",
360            Key::Semicolon => ";",
361            Key::Quote => "'",
362            Key::Backquote => "`",
363            Key::Comma => ",",
364            Key::Period | Key::NumpadDecimal => ".",
365            Key::Slash | Key::NumpadDivide => "/",
366            Key::NumpadAdd => "+",
367            Key::NumpadMultiply => "*",
368        }
369    }
370}
371
372#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
373pub enum MouseButton {
374    Left,
375    Right,
376    Middle,
377    Back,
378    Forward,
379}
380
381#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
382pub enum Wheel {
383    Up,
384    Down,
385    Left,
386    Right,
387}
388
389#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
390pub enum Button {
391    South,
392    East,
393    West,
394    North,
395    LeftBumper,
396    RightBumper,
397    LeftTrigger,
398    RightTrigger,
399    LeftStick,
400    RightStick,
401    DPadUp,
402    DPadDown,
403    DPadLeft,
404    DPadRight,
405    Menu,
406    View,
407    Guide,
408    Touchpad,
409    LeftGrip,
410    RightGrip,
411    LeftGripLower,
412    RightGripLower,
413    Misc,
414}
415
416impl Button {
417    pub const ALL: [Button; 23] = [
418        Button::South,
419        Button::East,
420        Button::West,
421        Button::North,
422        Button::LeftBumper,
423        Button::RightBumper,
424        Button::LeftTrigger,
425        Button::RightTrigger,
426        Button::LeftStick,
427        Button::RightStick,
428        Button::DPadUp,
429        Button::DPadDown,
430        Button::DPadLeft,
431        Button::DPadRight,
432        Button::Menu,
433        Button::View,
434        Button::Guide,
435        Button::Touchpad,
436        Button::LeftGrip,
437        Button::RightGrip,
438        Button::LeftGripLower,
439        Button::RightGripLower,
440        Button::Misc,
441    ];
442}
443
444#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
445pub enum Axis {
446    LeftX,
447    LeftY,
448    RightX,
449    RightY,
450}
451
452impl Axis {
453    pub fn stick(self) -> Stick {
454        match self {
455            Axis::LeftX | Axis::LeftY => Stick::Left,
456            Axis::RightX | Axis::RightY => Stick::Right,
457        }
458    }
459}
460
461#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
462pub enum Stick {
463    Left,
464    Right,
465}
466
467impl Stick {
468    pub fn axes(self) -> (Axis, Axis) {
469        match self {
470            Stick::Left => (Axis::LeftX, Axis::LeftY),
471            Stick::Right => (Axis::RightX, Axis::RightY),
472        }
473    }
474}
475
476#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
477pub enum Sign {
478    Negative,
479    Positive,
480}
481
482#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
483pub enum Control {
484    Key(Key),
485    Mouse(MouseButton),
486    Wheel(Wheel),
487    MouseMove,
488    Button(Button),
489    Stick(Stick),
490    DPad,
491}
492
493#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
494pub enum Motors {
495    None,
496    One,
497    Two,
498}
499
500#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
501pub struct PadInfo {
502    pub name: String,
503    pub vendor: Option<u16>,
504    pub product: Option<u16>,
505    pub family: Family,
506    pub motors: Motors,
507    pub resolves_actions: bool,
508}
509
510impl PadInfo {
511    pub fn from_ids(name: &str, vendor: Option<u16>, product: Option<u16>, motors: Motors) -> Self {
512        Self {
513            name: name.to_owned(),
514            vendor,
515            product,
516            family: match (vendor, product) {
517                (Some(vendor), Some(product)) => Family::from_ids(vendor, product),
518                _ => Family::Generic,
519            },
520            motors,
521            resolves_actions: false,
522        }
523    }
524}