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
use crate::constants::*; use serde::{Deserialize, Serialize}; use std::convert::TryFrom; /// The relevant data involved in a Key event. /// /// A lot of extra information is passed in an evdev::InputEvent, so this structure simplifies the /// data to only the relevant information in a key event: the key and its state. #[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct KeyEvent { key: KeyCode, state: PressState, } impl KeyEvent { /// Creates a new KeyEvent with the specified key and state. /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let ev1 = KeyEvent::new(KeyCode::KEY_A, PressState::Up); /// let ev2 = KeyEvent::new(KeyCode::KEY_A, PressState::Down); /// let ev3 = KeyEvent::new(KeyCode::KEY_A, PressState::Up); /// assert!(ev1 != ev2); /// assert!(ev1 == ev3); /// ``` pub fn new(key: KeyCode, state: PressState) -> Self { Self { key: key, state: state, } } /// Retrieves the KeyCode for the event. /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let ev = KeyEvent::new(KeyCode::KEY_W, PressState::Down); /// assert_eq!(ev.key(), KeyCode::KEY_W); /// ``` pub fn key(&self) -> KeyCode { self.key } /// Retrieves the state of the key reported by the event. /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let ev = KeyEvent::new(KeyCode::KEY_W, PressState::Down); /// assert_eq!(ev.state(), PressState::Down); /// ``` pub fn state(&self) -> PressState { self.state } } impl TryFrom<evdev::InputEvent> for KeyEvent { type Error = &'static str; fn try_from(ev: evdev::InputEvent) -> Result<Self, Self::Error> { let error: &'static str = "Could not convert evdev::InputEvent into KeyEvent"; // If the event is a key event if let evdev::InputEventKind::Key(code) = ev.kind() { // Match the key let key = match num::FromPrimitive::from_u16(code.0) { Some(key) => key, None => return Err(error), }; // Set the PressState match ev.value() { 1 => { return Ok(Self { key: key, state: PressState::Down, }) } 0 => { return Ok(Self { key: key, state: PressState::Up, }) } _ => return Err(error), } } Err(error) } } /// The relevant data involved in a Relative Axis event. /// /// A lot of extra information is passed in an evdev::InputEvent, so this structure simplifies the /// data to only the relevant information in a Relative Axis event: the axis and its value. #[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct RelAxisEvent { axis: RelAxisCode, state: AxisState, } impl RelAxisEvent { /// Creates a new RelAxisEvent with the specified axis and state. /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let ev1 = RelAxisEvent::new(RelAxisCode::REL_X, -5); /// let ev2 = RelAxisEvent::new(RelAxisCode::REL_X, 5); /// let ev3 = RelAxisEvent::new(RelAxisCode::REL_X, -5); /// assert!(ev1 != ev2); /// assert!(ev1 == ev3); /// ``` pub fn new(axis: RelAxisCode, val: AxisState) -> Self { Self { axis: axis, state: val, } } /// Retrieves the axis for the event. /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let ev = RelAxisEvent::new(RelAxisCode::REL_X, -5); /// assert_eq!(ev.axis(), RelAxisCode::REL_X); /// ``` pub fn axis(&self) -> RelAxisCode { self.axis } /// Retrieves the value of the axis for the event /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let ev = RelAxisEvent::new(RelAxisCode::REL_X, -5); /// assert_eq!(ev.state(), -5); /// ``` pub fn state(&self) -> AxisState { self.state } /// Sets the state of the axis for the event /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let mut ev = RelAxisEvent::new(RelAxisCode::REL_X, -5); /// assert_eq!(ev.state(), -5); /// ev.set_state(10); /// assert_eq!(ev.state(), 10); /// ``` pub fn set_state(&mut self, state: AxisState) { self.state = state; } } impl TryFrom<evdev::InputEvent> for RelAxisEvent { type Error = &'static str; fn try_from(ev: evdev::InputEvent) -> Result<Self, Self::Error> { let error: &'static str = "Could not convert evdev::InputEvent into RelAxisEvent"; if let evdev::InputEventKind::RelAxis(code) = ev.kind() { let axis = match num::FromPrimitive::from_u16(code.0) { Some(axis) => axis, None => return Err(error), }; return Ok(Self { axis: axis, state: ev.value(), }); } Err(error) } } /// The relevant data involved in a Absolute Axis event. /// /// A lot of extra information is passed in an evdev::InputEvent, so this structure simplifies the /// data to only the relevant information: the axis and its value. #[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct AbsAxisEvent { axis: AbsAxisCode, state: AxisState, } impl AbsAxisEvent { /// Creates a new AbsAxisEvent with the specified axis and state. /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let ev1 = AbsAxisEvent::new(AbsAxisCode::ABS_X, -5); /// let ev2 = AbsAxisEvent::new(AbsAxisCode::ABS_X, 5); /// let ev3 = AbsAxisEvent::new(AbsAxisCode::ABS_X, -5); /// assert!(ev1 != ev2); /// assert!(ev1 == ev3); /// ``` pub fn new(axis: AbsAxisCode, state: AxisState) -> Self { Self { axis: axis, state: state, } } /// Retrieves the axis for the event. /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let ev = AbsAxisEvent::new(AbsAxisCode::ABS_X, -5); /// assert_eq!(ev.axis(), AbsAxisCode::ABS_X); /// ``` pub fn axis(&self) -> AbsAxisCode { self.axis } /// Retrieves the value of the axis for the event /// /// Example: /// ``` /// use chord2key::constants::*; /// use chord2key::events::*; /// let ev = AbsAxisEvent::new(AbsAxisCode::ABS_X, -5); /// assert_eq!(ev.state(), -5); /// ``` pub fn state(&self) -> AxisState { self.state } } impl TryFrom<evdev::InputEvent> for AbsAxisEvent { type Error = &'static str; fn try_from(ev: evdev::InputEvent) -> Result<Self, Self::Error> { let error: &'static str = "Could not convert evdev::InputEvent into AbsAxisEvent"; if let evdev::InputEventKind::AbsAxis(code) = ev.kind() { let axis = match num::FromPrimitive::from_u16(code.0) { Some(axis) => axis, None => return Err(error), }; return Ok(Self { axis: axis, state: ev.value(), }); } Err(error) } }