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
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/

//! Definitions for various input related functionalities.

// -------------------------------------------------------------------------------------------------

use timing;
use defs::{Slide, Vector};

// -------------------------------------------------------------------------------------------------

pub type Key = Button;

pub type KeyCode = u16;
pub type KeyValue = i32;

// -------------------------------------------------------------------------------------------------

/// These flags describe key modifiers.
pub mod modifier {
    pub type ModifierType = u16;
    pub const NONE: ModifierType = 0b00000000;
    pub const LCTL: ModifierType = 0b00000001;
    pub const RCTL: ModifierType = 0b00000010;
    pub const LSHF: ModifierType = 0b00000100;
    pub const RSHF: ModifierType = 0b00001000;
    pub const LALT: ModifierType = 0b00010000;
    pub const RALT: ModifierType = 0b00100000;
    pub const LMTA: ModifierType = 0b01000000;
    pub const RMTA: ModifierType = 0b10000000;
    pub const CTRL: ModifierType = LCTL | RCTL;
    pub const SHIFT: ModifierType = LSHF | RSHF;
    pub const ALT: ModifierType = LALT | RALT;
    pub const META: ModifierType = LMTA | RMTA;
}

// -------------------------------------------------------------------------------------------------

/// Enumeration for possible results of catching key.
#[derive(PartialEq)]
pub enum KeyCatchResult {
    Caught,
    Passed,
}

// -------------------------------------------------------------------------------------------------

/// Structure for identifying key binding.
///
/// Used as key in hash maps.
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct Binding {
    code: KeyCode,
    modifiers: modifier::ModifierType,
}

// -------------------------------------------------------------------------------------------------

impl Binding {
    /// Constructs new `Binding`.
    ///
    /// `uinput_sys` defines codes as `i32` so second constructor added to avoid casting in other
    /// places.
    pub fn new(code: i32, modifiers: modifier::ModifierType) -> Self {
        Binding {
            code: code as KeyCode,
            modifiers: modifiers,
        }
    }

    /// `Binding` constructor.
    pub fn create(code: KeyCode, modifiers: modifier::ModifierType) -> Self {
        Binding {
            code: code,
            modifiers: modifiers,
        }
    }
}

// -------------------------------------------------------------------------------------------------

/// Data for button event.
#[derive(Clone, Copy, Debug)]
pub struct Button {
    pub code: u16,
    pub value: i32,
    pub time: timing::Milliseconds,
}

// -------------------------------------------------------------------------------------------------

impl Button {
    /// Constructs `Button`.
    pub fn new(code: u16, value: i32, milliseconds: timing::Milliseconds) -> Self {
        Button {
            code: code,
            value: value,
            time: milliseconds,
        }
    }

    /// Constructs `Button` with current time.
    pub fn new_now(code: u16, value: i32) -> Self {
        Button {
            code: code,
            value: value,
            time: timing::Milliseconds::now(),
        }
    }
}

// -------------------------------------------------------------------------------------------------

/// Data for axis event.
#[derive(Clone, Copy, Debug)]
pub struct Axis {
    pub discrete: Vector,
    pub continuous: Slide,
    pub time: timing::Milliseconds,
}

// -------------------------------------------------------------------------------------------------

impl Axis {
    pub fn new(discrete: Vector, continuous: Slide, time: timing::Milliseconds) -> Self {
        Axis {
            discrete: discrete,
            continuous: continuous,
            time: time,
        }
    }

    pub fn new_now(discrete: Vector, continuous: Slide) -> Self {
        Axis {
            discrete: discrete,
            continuous: continuous,
            time: timing::Milliseconds::now(),
        }
    }
}

// -------------------------------------------------------------------------------------------------

/// Trait implemented by handlers of input events like key strokes.
pub trait InputHandling: Send {
    /// Catches and processes the keyboard event.
    fn catch_key(&mut self,
                 code: KeyCode,
                 value: KeyValue,
                 modifiers: modifier::ModifierType)
                 -> KeyCatchResult;

    /// Clones the instance of `InputHandling`.
    fn duplicate(&self) -> Box<InputHandling>;
}

// -------------------------------------------------------------------------------------------------

/// Trait defining interface for input drivers to access the application.
pub trait InputForwarding: Send {
    /// Emits key event.
    fn emit_key(&mut self, code: u16, value: i32);

    /// Emits pointer motion event.
    fn emit_motion(&mut self, x: isize, y: isize);

    /// Emits pointer position event.
    fn emit_position(&mut self, x: Option<isize>, y: Option<isize>);

    /// Emits button event.
    fn emit_button(&mut self, code: u16, value: i32);

    /// Emits exist event.
    fn emit_axis(&mut self, horizontal: isize, vertical: isize);

    /// Emits position reset event.
    fn emit_position_reset(&mut self);

    /// Emits system activity event.
    fn emit_system_activity_event(&mut self);

    /// Clones the instance of `InputForwarding`.
    fn duplicate(&self) -> Box<InputForwarding>;
}

// -------------------------------------------------------------------------------------------------