blob: 85a0084720ac570c465b4c5836b506fa2c643d1f [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _UI_INPUT_READER_H
18#define _UI_INPUT_READER_H
19
20#include <ui/EventHub.h>
21#include <ui/Input.h>
Jeff Brown8575a872010-06-30 16:10:35 -070022#include <ui/InputDevice.h>
Jeff Browne839a582010-04-22 18:58:52 -070023#include <ui/InputDispatcher.h>
24#include <utils/KeyedVector.h>
25#include <utils/threads.h>
26#include <utils/Timers.h>
27#include <utils/RefBase.h>
28#include <utils/String8.h>
29#include <utils/BitSet.h>
30
31#include <stddef.h>
32#include <unistd.h>
33
Jeff Browne839a582010-04-22 18:58:52 -070034namespace android {
35
Jeff Brown54bc2812010-06-15 01:31:58 -070036/*
37 * Input reader policy interface.
38 *
39 * The input reader policy is used by the input reader to interact with the Window Manager
40 * and other system components.
41 *
42 * The actual implementation is partially supported by callbacks into the DVM
43 * via JNI. This interface is also mocked in the unit tests.
44 */
45class InputReaderPolicyInterface : public virtual RefBase {
46protected:
47 InputReaderPolicyInterface() { }
48 virtual ~InputReaderPolicyInterface() { }
49
50public:
51 /* Display orientations. */
52 enum {
53 ROTATION_0 = 0,
54 ROTATION_90 = 1,
55 ROTATION_180 = 2,
56 ROTATION_270 = 3
57 };
58
59 /* Actions returned by interceptXXX methods. */
60 enum {
61 // The input dispatcher should do nothing and discard the input unless other
62 // flags are set.
63 ACTION_NONE = 0,
64
65 // The input dispatcher should dispatch the input to the application.
66 ACTION_DISPATCH = 0x00000001,
67
68 // The input dispatcher should perform special filtering in preparation for
69 // a pending app switch.
70 ACTION_APP_SWITCH_COMING = 0x00000002,
71
72 // The input dispatcher should add POLICY_FLAG_WOKE_HERE to the policy flags it
73 // passes through the dispatch pipeline.
74 ACTION_WOKE_HERE = 0x00000004,
75
76 // The input dispatcher should add POLICY_FLAG_BRIGHT_HERE to the policy flags it
77 // passes through the dispatch pipeline.
Jeff Brown50de30a2010-06-22 01:27:15 -070078 ACTION_BRIGHT_HERE = 0x00000008,
Jeff Brown54bc2812010-06-15 01:31:58 -070079 };
80
81 /* Describes a virtual key. */
82 struct VirtualKeyDefinition {
83 int32_t scanCode;
84
85 // configured position data, specified in display coords
86 int32_t centerX;
87 int32_t centerY;
88 int32_t width;
89 int32_t height;
90 };
91
92 /* Gets information about the display with the specified id.
93 * Returns true if the display info is available, false otherwise.
94 */
95 virtual bool getDisplayInfo(int32_t displayId,
96 int32_t* width, int32_t* height, int32_t* orientation) = 0;
97
Jeff Brownf16c26d2010-07-02 15:37:36 -070098 /* Provides feedback for a virtual key down.
Jeff Brown54bc2812010-06-15 01:31:58 -070099 */
Jeff Brownf16c26d2010-07-02 15:37:36 -0700100 virtual void virtualKeyDownFeedback() = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700101
102 /* Intercepts a key event.
103 * The policy can use this method as an opportunity to perform power management functions
104 * and early event preprocessing.
105 *
106 * Returns a policy action constant such as ACTION_DISPATCH.
107 */
108 virtual int32_t interceptKey(nsecs_t when, int32_t deviceId,
109 bool down, int32_t keyCode, int32_t scanCode, uint32_t policyFlags) = 0;
110
111 /* Intercepts a trackball event.
112 * The policy can use this method as an opportunity to perform power management functions
113 * and early event preprocessing.
114 *
115 * Returns a policy action constant such as ACTION_DISPATCH.
116 */
117 virtual int32_t interceptTrackball(nsecs_t when, bool buttonChanged, bool buttonDown,
118 bool rolled) = 0;
119
120 /* Intercepts a touch event.
121 * The policy can use this method as an opportunity to perform power management functions
122 * and early event preprocessing.
123 *
124 * Returns a policy action constant such as ACTION_DISPATCH.
125 */
126 virtual int32_t interceptTouch(nsecs_t when) = 0;
127
128 /* Intercepts a switch event.
129 * The policy can use this method as an opportunity to perform power management functions
130 * and early event preprocessing.
131 *
132 * Switches are not dispatched to applications so this method should
133 * usually return ACTION_NONE.
134 */
135 virtual int32_t interceptSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) = 0;
136
137 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
138 * certain device whose screen currently is not all that good.
139 */
140 virtual bool filterTouchEvents() = 0;
141
142 /* Determines whether to turn on some hacks to improve touch interaction with another device
143 * where touch coordinate data can get corrupted.
144 */
145 virtual bool filterJumpyTouchEvents() = 0;
146
147 /* Gets the configured virtual key definitions for an input device. */
148 virtual void getVirtualKeyDefinitions(const String8& deviceName,
149 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
150
151 /* Gets the excluded device names for the platform. */
152 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
153};
154
155
156/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Browne839a582010-04-22 18:58:52 -0700157class InputReaderInterface : public virtual RefBase {
158protected:
159 InputReaderInterface() { }
160 virtual ~InputReaderInterface() { }
161
162public:
163 /* Runs a single iteration of the processing loop.
164 * Nominally reads and processes one incoming message from the EventHub.
165 *
166 * This method should be called on the input reader thread.
167 */
168 virtual void loopOnce() = 0;
169
170 /* Gets the current virtual key. Returns false if not down.
171 *
172 * This method may be called on any thread (usually by the input manager).
173 */
174 virtual bool getCurrentVirtualKey(int32_t* outKeyCode, int32_t* outScanCode) const = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700175
176 /* Gets the current input device configuration.
177 *
178 * This method may be called on any thread (usually by the input manager).
179 */
180 virtual void getCurrentInputConfiguration(InputConfiguration* outConfiguration) const = 0;
181
182 /*
183 * Query current input state.
184 * deviceId may be -1 to search for the device automatically, filtered by class.
185 * deviceClasses may be -1 to ignore device class while searching.
186 */
187 virtual int32_t getCurrentScanCodeState(int32_t deviceId, int32_t deviceClasses,
188 int32_t scanCode) const = 0;
189 virtual int32_t getCurrentKeyCodeState(int32_t deviceId, int32_t deviceClasses,
190 int32_t keyCode) const = 0;
191 virtual int32_t getCurrentSwitchState(int32_t deviceId, int32_t deviceClasses,
192 int32_t sw) const = 0;
193
194 /* Determine whether physical keys exist for the given framework-domain key codes. */
195 virtual bool hasKeys(size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const = 0;
Jeff Browne839a582010-04-22 18:58:52 -0700196};
197
Jeff Brown54bc2812010-06-15 01:31:58 -0700198
Jeff Browne839a582010-04-22 18:58:52 -0700199/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-06-15 01:31:58 -0700200 * that it sends to the input dispatcher. Some functions of the input reader, such as early
201 * event filtering in low power states, are controlled by a separate policy object.
202 *
203 * IMPORTANT INVARIANT:
204 * Because the policy can potentially block or cause re-entrance into the input reader,
205 * the input reader never calls into the policy while holding its internal locks.
Jeff Browne839a582010-04-22 18:58:52 -0700206 */
207class InputReader : public InputReaderInterface {
208public:
209 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700210 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700211 const sp<InputDispatcherInterface>& dispatcher);
212 virtual ~InputReader();
213
214 virtual void loopOnce();
215
216 virtual bool getCurrentVirtualKey(int32_t* outKeyCode, int32_t* outScanCode) const;
217
Jeff Brown54bc2812010-06-15 01:31:58 -0700218 virtual void getCurrentInputConfiguration(InputConfiguration* outConfiguration) const;
219
220 virtual int32_t getCurrentScanCodeState(int32_t deviceId, int32_t deviceClasses,
221 int32_t scanCode) const;
222 virtual int32_t getCurrentKeyCodeState(int32_t deviceId, int32_t deviceClasses,
223 int32_t keyCode) const;
224 virtual int32_t getCurrentSwitchState(int32_t deviceId, int32_t deviceClasses,
225 int32_t sw) const;
226
227 virtual bool hasKeys(size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) const;
228
Jeff Browne839a582010-04-22 18:58:52 -0700229private:
230 // Lock that must be acquired while manipulating state that may be concurrently accessed
231 // from other threads by input state query methods. It should be held for as short a
232 // time as possible.
233 //
234 // Exported state:
235 // - global virtual key code and scan code
236 // - device list and immutable properties of devices such as id, name, and class
237 // (but not other internal device state)
238 mutable Mutex mExportedStateLock;
239
Jeff Brown54bc2812010-06-15 01:31:58 -0700240 // current virtual key information (lock mExportedStateLock)
241 int32_t mExportedVirtualKeyCode;
242 int32_t mExportedVirtualScanCode;
243
244 // current input configuration (lock mExportedStateLock)
245 InputConfiguration mExportedInputConfiguration;
Jeff Browne839a582010-04-22 18:58:52 -0700246
247 // combined key meta state
248 int32_t mGlobalMetaState;
249
250 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700251 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700252 sp<InputDispatcherInterface> mDispatcher;
253
254 KeyedVector<int32_t, InputDevice*> mDevices;
255
256 // display properties needed to translate touch screen coordinates into display coordinates
257 int32_t mDisplayOrientation;
258 int32_t mDisplayWidth;
259 int32_t mDisplayHeight;
260
261 // low-level input event decoding
262 void process(const RawEvent* rawEvent);
263 void handleDeviceAdded(const RawEvent* rawEvent);
264 void handleDeviceRemoved(const RawEvent* rawEvent);
265 void handleSync(const RawEvent* rawEvent);
266 void handleKey(const RawEvent* rawEvent);
267 void handleRelativeMotion(const RawEvent* rawEvent);
268 void handleAbsoluteMotion(const RawEvent* rawEvent);
269 void handleSwitch(const RawEvent* rawEvent);
270
271 // input policy processing and dispatch
272 void onKey(nsecs_t when, InputDevice* device, bool down,
273 int32_t keyCode, int32_t scanCode, uint32_t policyFlags);
Jeff Brown54bc2812010-06-15 01:31:58 -0700274 void onSwitch(nsecs_t when, InputDevice* device, int32_t switchCode, int32_t switchValue);
Jeff Browne839a582010-04-22 18:58:52 -0700275 void onSingleTouchScreenStateChanged(nsecs_t when, InputDevice* device);
276 void onMultiTouchScreenStateChanged(nsecs_t when, InputDevice* device);
277 void onTouchScreenChanged(nsecs_t when, InputDevice* device, bool havePointerIds);
278 void onTrackballStateChanged(nsecs_t when, InputDevice* device);
279 void onConfigurationChanged(nsecs_t when);
280
281 bool applyStandardInputDispatchPolicyActions(nsecs_t when,
282 int32_t policyActions, uint32_t* policyFlags);
283
284 bool consumeVirtualKeyTouches(nsecs_t when, InputDevice* device, uint32_t policyFlags);
285 void dispatchVirtualKey(nsecs_t when, InputDevice* device, uint32_t policyFlags,
286 int32_t keyEventAction, int32_t keyEventFlags);
287 void dispatchTouches(nsecs_t when, InputDevice* device, uint32_t policyFlags);
288 void dispatchTouch(nsecs_t when, InputDevice* device, uint32_t policyFlags,
289 InputDevice::TouchData* touch, BitSet32 idBits, int32_t motionEventAction);
290
291 // display
292 void resetDisplayProperties();
293 bool refreshDisplayProperties();
294
295 // device management
296 InputDevice* getDevice(int32_t deviceId);
297 InputDevice* getNonIgnoredDevice(int32_t deviceId);
298 void addDevice(nsecs_t when, int32_t deviceId);
299 void removeDevice(nsecs_t when, InputDevice* device);
300 void configureDevice(InputDevice* device);
301 void configureDeviceForCurrentDisplaySize(InputDevice* device);
302 void configureVirtualKeys(InputDevice* device);
303 void configureAbsoluteAxisInfo(InputDevice* device, int axis, const char* name,
304 InputDevice::AbsoluteAxisInfo* out);
Jeff Brown54bc2812010-06-15 01:31:58 -0700305 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700306
307 // global meta state management for all devices
308 void resetGlobalMetaState();
309 int32_t globalMetaState();
310
311 // virtual key management
Jeff Brown54bc2812010-06-15 01:31:58 -0700312 void updateExportedVirtualKeyState();
313
314 // input configuration management
315 void updateExportedInputConfiguration();
Jeff Browne839a582010-04-22 18:58:52 -0700316};
317
318
319/* Reads raw events from the event hub and processes them, endlessly. */
320class InputReaderThread : public Thread {
321public:
322 InputReaderThread(const sp<InputReaderInterface>& reader);
323 virtual ~InputReaderThread();
324
325private:
326 sp<InputReaderInterface> mReader;
327
328 virtual bool threadLoop();
329};
330
331} // namespace android
332
333#endif // _UI_INPUT_READER_H