The Pedigree Project  0.1
HidInputManager.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/machine/HidInputManager.h"
21 #include "pedigree/kernel/LockGuard.h"
22 #include "pedigree/kernel/machine/InputManager.h"
23 #include "pedigree/kernel/machine/KeymapManager.h"
24 #include "pedigree/kernel/machine/Machine.h"
25 #include "pedigree/kernel/machine/Timer.h"
26 #include "pedigree/kernel/utilities/Iterator.h"
27 #include "pedigree/kernel/utilities/utility.h"
28 
30 
32 {
33 }
34 
36 {
37 }
38 
39 void HidInputManager::keyDown(uint8_t keyCode)
40 {
41  KeymapManager &keymapManager = KeymapManager::instance();
42 
43  InputManager::instance().rawKeyUpdate(keyCode, false);
44 
45  // Check for modifiers
46  if (keymapManager.handleHidModifier(keyCode, true))
47  {
48  updateKeys();
49  return;
50  }
51 
53 
54  // Is the key already considered "down"?
55  if (!m_KeyStates.lookup(keyCode))
56  {
57  // If there was no key before, register the timer handler
58  if (!m_KeyStates.count())
59  Machine::instance().getTimer()->registerHandler(this);
60 
61  // Resolve the key
62  uint64_t key = keymapManager.resolveHidKeycode(keyCode);
63 
64  // Create a key state structure and fill it
65  KeyState *keyState = new KeyState;
66  keyState->key = key;
67  keyState->nLeftTicks = 600000000;
68 
69  // Insert the key state
70  m_KeyStates.insert(keyCode, keyState);
71 
72  // First keypress always sent straight away, repeating keystrokes
73  // are transferred as necessary
74  if (key)
76  }
77 }
78 
79 void HidInputManager::keyUp(uint8_t keyCode)
80 {
81  KeymapManager &keymapManager = KeymapManager::instance();
82 
83  InputManager::instance().rawKeyUpdate(keyCode, true);
84 
85  // Check for modifiers
86  if (keymapManager.handleHidModifier(keyCode, false))
87  {
88  updateKeys();
89  return;
90  }
91 
93 
94  // Is the key actually pressed?
95  KeyState *keyState = m_KeyStates.lookup(keyCode);
96  if (keyState)
97  {
98  // Remove the key from the key states tree
99  m_KeyStates.remove(keyCode);
100  // Delete the key state structure
101  delete keyState;
102  }
103 }
104 
105 void HidInputManager::timer(uint64_t delta, InterruptState &state)
106 {
108 
110  it != m_KeyStates.end(); ++it)
111  {
112  KeyState *keyState = it.value();
113  if (keyState->nLeftTicks > delta)
114  keyState->nLeftTicks -= delta;
115  else
116  {
117  keyState->nLeftTicks = 40000000;
118  if (keyState->key)
120  }
121  }
122 
123  // If we've got no more keys being held down, release the handler
124  if (!m_KeyStates.count())
125  Machine::instance().getTimer()->unregisterHandler(this);
126 }
127 
129 {
131 
132  KeymapManager &keymapManager = KeymapManager::instance();
134  it != m_KeyStates.end(); ++it)
135  {
136  KeyState *keyState = it.value();
137  keyState->key = keymapManager.resolveHidKeycode(it.key());
138  }
139 }
virtual Timer * getTimer()=0
void timer(uint64_t delta, InterruptState &state)
uint64_t resolveHidKeycode(uint8_t keyCode)
void keyUp(uint8_t keyCode)
Called when a key transitions to the "up" state.
static KeymapManager & instance()
Singleton design.
Definition: KeymapManager.h:41
HidInputManager()
Default constructor.
static HidInputManager m_Instance
Static instance.
void keyDown(uint8_t keyCode)
Called when a key transitions to the "down" state.
void updateKeys()
Apply modifier or keymap changes to all keys in down state.
void keyPressed(uint64_t key)
Called whenever a key is pressed and needs to be added to the queue.
Tree< uint8_t, KeyState * > m_KeyStates
Current key states (for periodic callbacks while a key is down)
virtual ~HidInputManager()
Default destructor.
bool handleHidModifier(uint8_t keyCode, bool bDown)
uint64_t key
The resolved key.
static InputManager & instance()
Singleton design.
Definition: InputManager.h:107
An iterator applicable for many data structures.
Definition: Iterator.h:180
void rawKeyUpdate(uint8_t scancode, bool bKeyUp)
uint64_t nLeftTicks
The time left until the next key repeat.