input.rs (5368B)
1 pub use crate::Result; 2 3 use crate::geometry::Corner; 4 use crate::geometry::Edge; 5 use crate::geometry::Pos; 6 use crate::window::Window; 7 8 use std::collections::HashMap; 9 use std::collections::HashSet; 10 use std::convert::TryFrom; 11 use std::hash::Hash; 12 use std::hash::Hasher; 13 use std::vec::Vec; 14 15 use anyhow::anyhow; 16 use strum::EnumIter; 17 use strum::IntoEnumIterator; 18 19 #[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)] 20 pub enum Grip { 21 Edge(Edge), 22 Corner(Corner), 23 } 24 25 impl Grip { 26 pub fn is_top_grip(&self) -> bool { 27 *self == Grip::Edge(Edge::Top) 28 || *self == Grip::Corner(Corner::TopLeft) 29 || *self == Grip::Corner(Corner::TopRight) 30 } 31 32 pub fn is_left_grip(&self) -> bool { 33 *self == Grip::Edge(Edge::Left) 34 || *self == Grip::Corner(Corner::TopLeft) 35 || *self == Grip::Corner(Corner::BottomLeft) 36 } 37 } 38 39 #[repr(u8)] 40 #[derive(Debug, PartialEq, EnumIter, Eq, Hash, Clone, Copy, PartialOrd, Ord)] 41 pub enum Modifier { 42 Ctrl = 1 << 0, 43 Shift = 1 << 1, 44 Alt = 1 << 2, 45 AltGr = 1 << 3, 46 Super = 1 << 4, 47 NumLock = 1 << 5, 48 ScrollLock = 1 << 6, 49 } 50 51 impl TryFrom<&str> for Modifier { 52 type Error = anyhow::Error; 53 54 fn try_from(val: &str) -> Result<Self> { 55 match val { 56 "C" => Ok(Self::Ctrl), 57 "A" => Ok(Self::Alt), 58 "S" => Ok(Self::Shift), 59 "M" => Ok(Self::Super), 60 "AltGr" => Ok(Self::Alt), 61 "Num" => Ok(Self::NumLock), 62 "Scroll" => Ok(Self::ScrollLock), 63 _ => Err(anyhow!("unable to resolve \"{}\" to modifier", val)), 64 } 65 } 66 } 67 68 #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] 69 pub enum Button { 70 Left, 71 Middle, 72 Right, 73 ScrollUp, 74 ScrollDown, 75 Backward, 76 Forward, 77 } 78 79 #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] 80 pub enum MouseEventKind { 81 Press, 82 Release, 83 Motion, 84 } 85 86 #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] 87 pub enum MouseInputTarget { 88 Global, 89 Root, 90 Client, 91 } 92 93 #[derive(Debug, Clone, PartialEq, Eq)] 94 pub struct MouseInput { 95 pub target: MouseInputTarget, 96 pub button: Button, 97 pub modifiers: HashSet<Modifier>, 98 } 99 100 impl Hash for MouseInput { 101 fn hash<H: Hasher>( 102 &self, 103 state: &mut H, 104 ) { 105 self.button.hash(state); 106 self.modifiers 107 .iter() 108 .fold(0u8, |acc, &m| acc | m as u8) 109 .hash(state); 110 } 111 } 112 113 #[derive(Debug, Clone, Hash, PartialEq, Eq)] 114 pub struct MouseEvent { 115 pub kind: MouseEventKind, 116 pub input: MouseInput, 117 pub window: Option<Window>, 118 pub root_rpos: Pos, 119 } 120 121 #[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] 122 pub enum Key { 123 Any, 124 Backspace, 125 Tab, 126 Clear, 127 Return, 128 Shift, 129 Control, 130 Alt, 131 Super, 132 Menu, 133 Pause, 134 CapsLock, 135 Escape, 136 Space, 137 ExclamationMark, 138 QuotationMark, 139 QuestionMark, 140 NumberSign, 141 DollarSign, 142 PercentSign, 143 AtSign, 144 Ampersand, 145 Apostrophe, 146 LeftParenthesis, 147 RightParenthesis, 148 LeftBracket, 149 RightBracket, 150 LeftBrace, 151 RightBrace, 152 Underscore, 153 Grave, 154 Bar, 155 Tilde, 156 QuoteLeft, 157 Asterisk, 158 Plus, 159 Comma, 160 Minus, 161 Period, 162 Slash, 163 BackSlash, 164 Colon, 165 SemiColon, 166 Less, 167 Equal, 168 Greater, 169 PageUp, 170 PageDown, 171 End, 172 Home, 173 Left, 174 Up, 175 Right, 176 Down, 177 Select, 178 Print, 179 Execute, 180 PrintScreen, 181 Insert, 182 Delete, 183 Help, 184 Zero, 185 One, 186 Two, 187 Three, 188 Four, 189 Five, 190 Six, 191 Seven, 192 Eight, 193 Nine, 194 A, 195 B, 196 C, 197 D, 198 E, 199 F, 200 G, 201 H, 202 I, 203 J, 204 K, 205 L, 206 M, 207 N, 208 O, 209 P, 210 Q, 211 R, 212 S, 213 T, 214 U, 215 V, 216 W, 217 X, 218 Y, 219 Z, 220 NumPad0, 221 NumPad1, 222 NumPad2, 223 NumPad3, 224 NumPad4, 225 NumPad5, 226 NumPad6, 227 NumPad7, 228 NumPad8, 229 NumPad9, 230 Multiply, 231 Add, 232 Seperator, 233 Subtract, 234 Decimal, 235 Divide, 236 F1, 237 F2, 238 F3, 239 F4, 240 F5, 241 F6, 242 F7, 243 F8, 244 F9, 245 F10, 246 F11, 247 F12, 248 F13, 249 F14, 250 F15, 251 F16, 252 F17, 253 F18, 254 F19, 255 F20, 256 F21, 257 F22, 258 F23, 259 F24, 260 Numlock, 261 ScrollLock, 262 LeftShift, 263 RightShift, 264 LeftControl, 265 RightContol, 266 LeftAlt, 267 RightAlt, 268 LeftSuper, 269 RightSuper, 270 BrowserBack, 271 BrowserForward, 272 BrowserRefresh, 273 BrowserStop, 274 BrowserSearch, 275 BrowserFavorites, 276 BrowserHome, 277 VolumeMute, 278 VolumeDown, 279 VolumeUp, 280 NextTrack, 281 PreviousTrack, 282 StopMedia, 283 PlayPause, 284 LaunchMail, 285 SelectMedia, 286 LaunchAppA, 287 LaunchAppB, 288 LaunchAppC, 289 LaunchAppD, 290 LaunchAppE, 291 LaunchAppF, 292 LaunchApp0, 293 LaunchApp1, 294 LaunchApp2, 295 LaunchApp3, 296 LaunchApp4, 297 LaunchApp5, 298 LaunchApp6, 299 LaunchApp7, 300 LaunchApp8, 301 LaunchApp9, 302 } 303 304 #[derive(Debug, Clone, PartialEq, Eq)] 305 pub struct KeyInput { 306 pub key: Key, 307 pub modifiers: HashSet<Modifier>, 308 } 309 310 impl Hash for KeyInput { 311 fn hash<H: Hasher>( 312 &self, 313 state: &mut H, 314 ) { 315 self.key.hash(state); 316 self.modifiers 317 .iter() 318 .fold(0u8, |acc, &m| acc | m as u8) 319 .hash(state); 320 } 321 } 322 323 #[derive(Debug, Clone, Hash, PartialEq, Eq)] 324 pub struct KeyEvent { 325 pub input: KeyInput, 326 pub window: Option<Window>, 327 }