Add named switch key support in config

This commit is contained in:
htrefil 2020-10-29 18:10:31 +01:00
parent 8f1037f598
commit 1890bf1429
7 changed files with 1918 additions and 14 deletions

View file

@ -1,5 +1,5 @@
listen-address = "0.0.0.0:5258"
switch-keys = [29, 56, 111]
switch-keys = ["Leftalt"]
identity-path = "identity.p12"
# Doesn't have to be set.
identity-password = "123456789"

View file

@ -1,3 +1,9 @@
mod button;
mod key;
pub use button::Button;
pub use key::Key;
use crate::glue::{self, input_event, timeval};
use serde::{Deserialize, Serialize};
@ -5,7 +11,7 @@ use serde::{Deserialize, Serialize};
pub enum Event {
MouseScroll { delta: i32 },
MouseMove { axis: Axis, delta: i32 },
Key { direction: Direction, code: u16 },
Key { direction: Direction, kind: KeyKind },
Sync,
}
@ -23,12 +29,12 @@ impl Event {
} => (glue::EV_REL as _, glue::REL_Y as _, delta),
Event::Key {
direction: Direction::Up,
code,
} => (glue::EV_KEY as _, code, 0),
kind,
} => (glue::EV_KEY as _, kind.to_raw(), 0),
Event::Key {
direction: Direction::Down,
code,
} => (glue::EV_KEY as _, code, 1),
kind,
} => (glue::EV_KEY as _, kind.to_raw(), 1),
Event::Sync => (glue::EV_SYN as _, glue::SYN_REPORT as _, 0),
};
@ -56,11 +62,11 @@ impl Event {
},
(glue::EV_KEY, code, 0) => Event::Key {
direction: Direction::Up,
code: code as _,
kind: KeyKind::from_raw(code as _)?,
},
(glue::EV_KEY, code, 1) => Event::Key {
direction: Direction::Down,
code: code as _,
kind: KeyKind::from_raw(code as _)?,
},
(glue::EV_SYN, glue::SYN_REPORT, _) => Event::Sync,
_ => return None,
@ -81,3 +87,24 @@ pub enum Direction {
Up, // The key is released.
Down, // The key is pressed.
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum KeyKind {
Key(Key),
Button(Button),
}
impl KeyKind {
pub(crate) fn to_raw(&self) -> u16 {
match self {
KeyKind::Key(key) => key.to_raw(),
KeyKind::Button(button) => button.to_raw(),
}
}
pub(crate) fn from_raw(code: u16) -> Option<KeyKind> {
Key::from_raw(code)
.map(KeyKind::Key)
.or_else(|| Button::from_raw(code).map(KeyKind::Button))
}
}

387
input/src/event/button.rs Normal file
View file

@ -0,0 +1,387 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, Serialize, Deserialize, Hash)]
pub enum Button {
A,
B,
Back,
Base,
Base2,
Base3,
Base4,
Base5,
Base6,
C,
Dead,
Digi,
DpadDown,
DpadLeft,
DpadRight,
DpadUp,
East,
Extra,
Forward,
Gamepad,
GearDown,
GearUp,
Joystick,
Left,
Middle,
Misc,
Mode,
Mouse,
N0,
N1,
N2,
N3,
N4,
N5,
N6,
N7,
N8,
N9,
North,
Pinkie,
Right,
Select,
Side,
South,
Start,
Stylus,
Stylus2,
Stylus3,
Task,
Thumb,
Thumb2,
Thumbl,
Thumbr,
Tl,
Tl2,
ToolAirbrush,
ToolBrush,
ToolDoubletap,
ToolFinger,
ToolLens,
ToolMouse,
ToolPen,
ToolPencil,
ToolQuadtap,
ToolQuinttap,
ToolRubber,
ToolTripletap,
Top,
Top2,
Touch,
Tr,
Tr2,
Trigger,
TriggerHappy,
TriggerHappy1,
TriggerHappy10,
TriggerHappy11,
TriggerHappy12,
TriggerHappy13,
TriggerHappy14,
TriggerHappy15,
TriggerHappy16,
TriggerHappy17,
TriggerHappy18,
TriggerHappy19,
TriggerHappy2,
TriggerHappy20,
TriggerHappy21,
TriggerHappy22,
TriggerHappy23,
TriggerHappy24,
TriggerHappy25,
TriggerHappy26,
TriggerHappy27,
TriggerHappy28,
TriggerHappy29,
TriggerHappy3,
TriggerHappy30,
TriggerHappy31,
TriggerHappy32,
TriggerHappy33,
TriggerHappy34,
TriggerHappy35,
TriggerHappy36,
TriggerHappy37,
TriggerHappy38,
TriggerHappy39,
TriggerHappy4,
TriggerHappy40,
TriggerHappy5,
TriggerHappy6,
TriggerHappy7,
TriggerHappy8,
TriggerHappy9,
West,
Wheel,
X,
Y,
Z,
}
impl Button {
pub(crate) fn to_raw(&self) -> u16 {
use Button::*;
match *self {
A => 0x0130,
B => 0x0131,
Back => 0x0116,
Base => 0x0126,
Base2 => 0x0127,
Base3 => 0x0128,
Base4 => 0x0129,
Base5 => 0x012A,
Base6 => 0x012B,
C => 0x0132,
Dead => 0x012F,
Digi => 0x0140,
DpadDown => 0x0221,
DpadLeft => 0x0222,
DpadRight => 0x0223,
DpadUp => 0x0220,
East => 0x0131,
Extra => 0x0114,
Forward => 0x0115,
Gamepad => 0x0130,
GearDown => 0x0150,
GearUp => 0x0151,
Joystick => 0x0120,
Left => 0x0110,
Middle => 0x0112,
Misc => 0x0100,
Mode => 0x013C,
Mouse => 0x0110,
N0 => 0x0100,
N1 => 0x0101,
N2 => 0x0102,
N3 => 0x0103,
N4 => 0x0104,
N5 => 0x0105,
N6 => 0x0106,
N7 => 0x0107,
N8 => 0x0108,
N9 => 0x0109,
North => 0x0133,
Pinkie => 0x0125,
Right => 0x0111,
Select => 0x013A,
Side => 0x0113,
South => 0x0130,
Start => 0x013B,
Stylus => 0x014B,
Stylus2 => 0x014C,
Stylus3 => 0x0149,
Task => 0x0117,
Thumb => 0x0121,
Thumb2 => 0x0122,
Thumbl => 0x013D,
Thumbr => 0x013E,
Tl => 0x0136,
Tl2 => 0x0138,
ToolAirbrush => 0x0144,
ToolBrush => 0x0142,
ToolDoubletap => 0x014D,
ToolFinger => 0x0145,
ToolLens => 0x0147,
ToolMouse => 0x0146,
ToolPen => 0x0140,
ToolPencil => 0x0143,
ToolQuadtap => 0x014F,
ToolQuinttap => 0x0148,
ToolRubber => 0x0141,
ToolTripletap => 0x014E,
Top => 0x0123,
Top2 => 0x0124,
Touch => 0x014A,
Tr => 0x0137,
Tr2 => 0x0139,
Trigger => 0x0120,
TriggerHappy => 0x02C0,
TriggerHappy1 => 0x02C0,
TriggerHappy10 => 0x02C9,
TriggerHappy11 => 0x02CA,
TriggerHappy12 => 0x02CB,
TriggerHappy13 => 0x02CC,
TriggerHappy14 => 0x02CD,
TriggerHappy15 => 0x02CE,
TriggerHappy16 => 0x02CF,
TriggerHappy17 => 0x02D0,
TriggerHappy18 => 0x02D1,
TriggerHappy19 => 0x02D2,
TriggerHappy2 => 0x02C1,
TriggerHappy20 => 0x02D3,
TriggerHappy21 => 0x02D4,
TriggerHappy22 => 0x02D5,
TriggerHappy23 => 0x02D6,
TriggerHappy24 => 0x02D7,
TriggerHappy25 => 0x02D8,
TriggerHappy26 => 0x02D9,
TriggerHappy27 => 0x02DA,
TriggerHappy28 => 0x02DB,
TriggerHappy29 => 0x02DC,
TriggerHappy3 => 0x02C2,
TriggerHappy30 => 0x02DD,
TriggerHappy31 => 0x02DE,
TriggerHappy32 => 0x02DF,
TriggerHappy33 => 0x02E0,
TriggerHappy34 => 0x02E1,
TriggerHappy35 => 0x02E2,
TriggerHappy36 => 0x02E3,
TriggerHappy37 => 0x02E4,
TriggerHappy38 => 0x02E5,
TriggerHappy39 => 0x02E6,
TriggerHappy4 => 0x02C3,
TriggerHappy40 => 0x02E7,
TriggerHappy5 => 0x02C4,
TriggerHappy6 => 0x02C5,
TriggerHappy7 => 0x02C6,
TriggerHappy8 => 0x02C7,
TriggerHappy9 => 0x02C8,
West => 0x0134,
Wheel => 0x0150,
X => 0x0130,
Y => 0x0150,
Z => 0x0135,
}
}
pub(crate) fn from_raw(code: u16) -> Option<Self> {
use Button::*;
// This is generated from linux headers, some patterns are unreachable, and we don't care.
#[allow(unreachable_patterns)]
let button = match code {
0x0130 => A,
0x0131 => B,
0x0116 => Back,
0x0126 => Base,
0x0127 => Base2,
0x0128 => Base3,
0x0129 => Base4,
0x012A => Base5,
0x012B => Base6,
0x0132 => C,
0x012F => Dead,
0x0140 => Digi,
0x0221 => DpadDown,
0x0222 => DpadLeft,
0x0223 => DpadRight,
0x0220 => DpadUp,
0x0131 => East,
0x0114 => Extra,
0x0115 => Forward,
0x0130 => Gamepad,
0x0150 => GearDown,
0x0151 => GearUp,
0x0120 => Joystick,
0x0110 => Left,
0x0112 => Middle,
0x0100 => Misc,
0x013C => Mode,
0x0110 => Mouse,
0x0100 => N0,
0x0101 => N1,
0x0102 => N2,
0x0103 => N3,
0x0104 => N4,
0x0105 => N5,
0x0106 => N6,
0x0107 => N7,
0x0108 => N8,
0x0109 => N9,
0x0133 => North,
0x0125 => Pinkie,
0x0111 => Right,
0x013A => Select,
0x0113 => Side,
0x0130 => South,
0x013B => Start,
0x014B => Stylus,
0x014C => Stylus2,
0x0149 => Stylus3,
0x0117 => Task,
0x0121 => Thumb,
0x0122 => Thumb2,
0x013D => Thumbl,
0x013E => Thumbr,
0x0136 => Tl,
0x0138 => Tl2,
0x0144 => ToolAirbrush,
0x0142 => ToolBrush,
0x014D => ToolDoubletap,
0x0145 => ToolFinger,
0x0147 => ToolLens,
0x0146 => ToolMouse,
0x0140 => ToolPen,
0x0143 => ToolPencil,
0x014F => ToolQuadtap,
0x0148 => ToolQuinttap,
0x0141 => ToolRubber,
0x014E => ToolTripletap,
0x0123 => Top,
0x0124 => Top2,
0x014A => Touch,
0x0137 => Tr,
0x0139 => Tr2,
0x0120 => Trigger,
0x02C0 => TriggerHappy,
0x02C0 => TriggerHappy1,
0x02C9 => TriggerHappy10,
0x02CA => TriggerHappy11,
0x02CB => TriggerHappy12,
0x02CC => TriggerHappy13,
0x02CD => TriggerHappy14,
0x02CE => TriggerHappy15,
0x02CF => TriggerHappy16,
0x02D0 => TriggerHappy17,
0x02D1 => TriggerHappy18,
0x02D2 => TriggerHappy19,
0x02C1 => TriggerHappy2,
0x02D3 => TriggerHappy20,
0x02D4 => TriggerHappy21,
0x02D5 => TriggerHappy22,
0x02D6 => TriggerHappy23,
0x02D7 => TriggerHappy24,
0x02D8 => TriggerHappy25,
0x02D9 => TriggerHappy26,
0x02DA => TriggerHappy27,
0x02DB => TriggerHappy28,
0x02DC => TriggerHappy29,
0x02C2 => TriggerHappy3,
0x02DD => TriggerHappy30,
0x02DE => TriggerHappy31,
0x02DF => TriggerHappy32,
0x02E0 => TriggerHappy33,
0x02E1 => TriggerHappy34,
0x02E2 => TriggerHappy35,
0x02E3 => TriggerHappy36,
0x02E4 => TriggerHappy37,
0x02E5 => TriggerHappy38,
0x02E6 => TriggerHappy39,
0x02C3 => TriggerHappy4,
0x02E7 => TriggerHappy40,
0x02C4 => TriggerHappy5,
0x02C5 => TriggerHappy6,
0x02C6 => TriggerHappy7,
0x02C7 => TriggerHappy8,
0x02C8 => TriggerHappy9,
0x0134 => West,
0x0150 => Wheel,
0x0135 => Z,
_ => return None,
};
Some(button)
}
}
impl PartialEq for Button {
fn eq(&self, other: &Self) -> bool {
self.to_raw() == other.to_raw()
}
}

1489
input/src/event/key.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -4,6 +4,6 @@ mod event_reader;
mod event_writer;
mod glue;
pub use event::{Axis, Direction, Event};
pub use event::{Axis, Button, Direction, Event, Key, KeyKind};
pub use event_manager::EventManager;
pub use event_writer::EventWriter;

View file

@ -1,3 +1,4 @@
use input::Key;
use serde::Deserialize;
use std::collections::HashSet;
use std::net::SocketAddr;
@ -7,7 +8,7 @@ use std::path::PathBuf;
#[serde(rename_all = "kebab-case")]
pub struct Config {
pub listen_address: SocketAddr,
pub switch_keys: HashSet<u16>,
pub switch_keys: HashSet<Key>,
pub identity_path: PathBuf,
#[serde(default)]
pub identity_password: String,

View file

@ -2,7 +2,7 @@ mod config;
use anyhow::{Context, Error};
use config::Config;
use input::{Direction, Event, EventManager};
use input::{Direction, Event, EventManager, Key, KeyKind};
use net::{self, Message, PROTOCOL_VERSION};
use std::collections::{HashMap, HashSet};
use std::convert::Infallible;
@ -49,7 +49,7 @@ where
async fn run(
listen_address: SocketAddr,
switch_keys: &HashSet<u16>,
switch_keys: &HashSet<Key>,
identity_path: &Path,
identity_password: &str,
) -> Result<Infallible, Error> {
@ -113,8 +113,8 @@ async fn run(
tokio::select! {
event = manager.read() => {
let event = event?;
if let Event::Key { direction, code } = event {
if let Some(state) = key_states.get_mut(&code) {
if let Event::Key { direction, kind: KeyKind::Key(key) } = event {
if let Some(state) = key_states.get_mut(&key) {
*state = if direction == Direction::Down {
true
} else {