cognitive_qualia/input.rs
1// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
2// the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/
3
4//! Definitions for various input related functionalities.
5
6// -------------------------------------------------------------------------------------------------
7
8use timing;
9use defs::{Slide, Vector};
10
11// -------------------------------------------------------------------------------------------------
12
13pub type Key = Button;
14
15pub type InputCode = u16;
16pub type InputValue = i32;
17
18// -------------------------------------------------------------------------------------------------
19
20/// These flags describe key modifiers.
21pub mod modifier {
22 pub type ModifierType = u16;
23 pub const NONE: ModifierType = 0b00000000;
24 pub const LCTL: ModifierType = 0b00000001;
25 pub const RCTL: ModifierType = 0b00000010;
26 pub const LSHF: ModifierType = 0b00000100;
27 pub const RSHF: ModifierType = 0b00001000;
28 pub const LALT: ModifierType = 0b00010000;
29 pub const RALT: ModifierType = 0b00100000;
30 pub const LMTA: ModifierType = 0b01000000;
31 pub const RMTA: ModifierType = 0b10000000;
32 pub const CTRL: ModifierType = LCTL | RCTL;
33 pub const SHIFT: ModifierType = LSHF | RSHF;
34 pub const ALT: ModifierType = LALT | RALT;
35 pub const META: ModifierType = LMTA | RMTA;
36}
37
38// -------------------------------------------------------------------------------------------------
39
40/// Enumeration for possible results of catching key.
41#[derive(PartialEq)]
42pub enum CatchResult {
43 Caught,
44 Passed,
45}
46
47// -------------------------------------------------------------------------------------------------
48
49/// Structure for identifying key binding.
50///
51/// Used as key in hash maps.
52#[derive(PartialEq, Eq, Hash, Clone, Debug)]
53pub struct Binding {
54 code: InputCode,
55 modifiers: modifier::ModifierType,
56}
57
58// -------------------------------------------------------------------------------------------------
59
60impl Binding {
61 /// Constructs new `Binding`.
62 ///
63 /// `uinput_sys` defines codes as `i32` so second constructor added to avoid casting in other
64 /// places.
65 pub fn new(code: i32, modifiers: modifier::ModifierType) -> Self {
66 Binding {
67 code: code as InputCode,
68 modifiers: modifiers,
69 }
70 }
71
72 /// `Binding` constructor.
73 pub fn create(code: InputCode, modifiers: modifier::ModifierType) -> Self {
74 Binding {
75 code: code,
76 modifiers: modifiers,
77 }
78 }
79}
80
81// -------------------------------------------------------------------------------------------------
82
83/// Data for button event.
84#[derive(Clone, Copy, Debug)]
85pub struct Button {
86 pub code: u16,
87 pub value: i32,
88 pub time: timing::Milliseconds,
89}
90
91// -------------------------------------------------------------------------------------------------
92
93impl Button {
94 /// Constructs `Button`.
95 pub fn new(code: u16, value: i32, milliseconds: timing::Milliseconds) -> Self {
96 Button {
97 code: code,
98 value: value,
99 time: milliseconds,
100 }
101 }
102
103 /// Constructs `Button` with current time.
104 pub fn new_now(code: u16, value: i32) -> Self {
105 Button {
106 code: code,
107 value: value,
108 time: timing::Milliseconds::now(),
109 }
110 }
111}
112
113// -------------------------------------------------------------------------------------------------
114
115/// Data for axis event.
116#[derive(Clone, Copy, Debug)]
117pub struct Axis {
118 pub discrete: Vector,
119 pub continuous: Slide,
120 pub time: timing::Milliseconds,
121}
122
123// -------------------------------------------------------------------------------------------------
124
125impl Axis {
126 pub fn new(discrete: Vector, continuous: Slide, time: timing::Milliseconds) -> Self {
127 Axis {
128 discrete: discrete,
129 continuous: continuous,
130 time: time,
131 }
132 }
133
134 pub fn new_now(discrete: Vector, continuous: Slide) -> Self {
135 Axis {
136 discrete: discrete,
137 continuous: continuous,
138 time: timing::Milliseconds::now(),
139 }
140 }
141}
142
143// -------------------------------------------------------------------------------------------------
144
145/// Trait implemented by handlers of input events like key strokes.
146pub trait InputHandling: Send {
147 /// Catches and processes the keyboard event.
148 fn catch_key(&mut self,
149 code: InputCode,
150 value: InputValue,
151 modifiers: modifier::ModifierType)
152 -> CatchResult;
153
154 /// Catches and processes the button event.
155 fn catch_button(&mut self,
156 code: InputCode,
157 value: InputValue,
158 modifiers: modifier::ModifierType)
159 -> CatchResult;
160
161 /// Clones the instance of `InputHandling`.
162 fn duplicate(&self) -> Box<InputHandling>;
163}
164
165// -------------------------------------------------------------------------------------------------
166
167/// Trait defining interface for input drivers to access the application.
168pub trait InputForwarding: Send {
169 /// Emits key event.
170 fn emit_key(&mut self, code: u16, value: i32);
171
172 /// Emits pointer motion event.
173 fn emit_motion(&mut self, x: isize, y: isize);
174
175 /// Emits pointer position event.
176 fn emit_position(&mut self, x: Option<isize>, y: Option<isize>);
177
178 /// Emits button event.
179 fn emit_button(&mut self, code: u16, value: i32);
180
181 /// Emits exist event.
182 fn emit_axis(&mut self, horizontal: isize, vertical: isize);
183
184 /// Emits position reset event.
185 fn emit_position_reset(&mut self);
186
187 /// Emits system activity event.
188 fn emit_system_activity_event(&mut self);
189}
190
191// -------------------------------------------------------------------------------------------------