The Pedigree Project 0.1
InputManager.h
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#ifndef MACHINE_INPUT_MANAGER_H
21#define MACHINE_INPUT_MANAGER_H
22#include "pedigree/kernel/Atomic.h"
23#include "pedigree/kernel/Spinlock.h"
24#include "pedigree/kernel/compiler.h"
25#include "pedigree/kernel/process/Semaphore.h"
26#include "pedigree/kernel/process/WaitQueue.h"
27#include "pedigree/kernel/processor/types.h"
28#include "pedigree/kernel/utilities/List.h"
29#include "pedigree/kernel/utilities/Vector.h"
30#include "pedigree/kernel/utilities/new"
31
32#include <config.h>
33
34class Thread;
35
39class EXPORTED_PUBLIC InputManager {
40 public:
43 const static int Key = 1;
44 const static int Mouse = 2;
45 const static int Joystick = 4;
46 const static int RawKey = 8;
47 const static int MachineKey = 16; // machine-specific key codes
48 const static int AbsoluteMouse = 32;
49 const static int Unknown = 255;
50
51 typedef int CallbackType;
52
57 CallbackType type;
58 void* meta;
59
60 union {
61 struct {
62 uint64_t key;
63 } key;
64 struct {
65 ssize_t relx;
66 ssize_t rely;
67 ssize_t relz;
68
69 bool buttons[64];
70 } pointy;
71 struct {
72 uint32_t x;
73 uint32_t y;
74 ssize_t wheel;
75
76 bool buttons[64];
77 } absolute;
78 struct {
81 uint8_t scancode;
82
84 bool keyUp;
85 } rawkey;
86 struct {
88 uint8_t scancode;
89
91 bool keyUp;
92 } machinekey;
93 } data;
94 };
95
97 typedef void (*callback_t)(InputNotification&);
98
100 InputManager();
101
103 virtual ~InputManager();
104
106 void initialise();
107
109 void shutdown();
110
113 return m_Instance;
114 }
115
117 void keyPressed(uint64_t key);
118
121 void rawKeyUpdate(uint8_t scancode, bool bKeyUp);
122
124 void machineKeyUpdate(uint8_t scancode, bool bKeyUp);
125
127 void mouseUpdate(ssize_t relX, ssize_t relY, ssize_t relZ, uint32_t buttonBitmap);
128
130 void absoluteMouseUpdate(uint32_t x, uint32_t y, ssize_t wheel, uint32_t buttonBitmap);
131
133 void joystickUpdate(ssize_t relX, ssize_t relY, ssize_t relZ, uint32_t buttonBitmap);
134
136 void installCallback(CallbackType filter, callback_t callback, void* meta = 0);
137
139 void removeCallback(callback_t callback, void* meta = 0);
140
142 static int trampoline(void* ptr);
143
145 void mainThread();
146
148 bool isActive() const {
149 return m_bActive;
150 }
151
152#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
153 using CallbackPinHook = void (*)(callback_t callback, void* meta);
154
155 static void setCallbackPinHook(CallbackPinHook hook);
156#endif
157
158 private:
161
162 struct CallbackItem;
163
166 void putNotification(InputNotification* note);
167
168#if THREADS
169 void removeCallbacks(callback_t callback, void* meta);
170 void drainCallback(CallbackItem* item);
171#endif
172
176 callback_t func;
177
179 CallbackType filter;
180
182 void* meta;
183
184#if THREADS
186 size_t inFlight;
187
190
193
195 size_t removers;
196
199
200 WaitQueue drainWaiters;
201#endif
202 };
203
206
212
215
216#if THREADS
219
222
225#endif
226
229
230#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
231 static CallbackPinHook m_CallbackPinHook;
232#endif
233};
234
235#endif
Spinlock m_QueueLock
static InputManager & instance()
Singleton design.
Thread * m_pCallbackDispatchThread
Set only while the input worker is executing a committed callback.
static InputManager m_Instance
Static instance.
Atomic< bool > m_bActive
Are we active?
bool isActive() const
Returns whether the instance is creating notifications.
Semaphore m_InputQueueSize
Key press queue Semaphore.
Thread * m_pThread
Thread object for our worker thread.
List< InputNotification * > m_InputQueue
Input queue (for distribution to applications)
List< CallbackItem * > m_Callbacks
Callback list.
Definition List.h:61
Item in the kernel callback list.
void * meta
Meta pointer for the InputNotifications we generate.
bool draining
An external remover owns final deletion after draining inFlight.
callback_t func
The handler function.
CallbackType filter
Filter for this callback.
size_t inFlight
Pins held by a worker snapshot or an active callback invocation.
size_t removers
External removers which must all finish before final deletion.
bool deferredRemoval
The input worker must delete this item after self-removal drains.
bool enabled
No new invocation may begin after this becomes false.
bool keyUp
Whether this is a keyUp event or not.
uint8_t scancode
Machine-specific scancode for the key.