blob: d3fbaf75794f8eb1b0ca7764ce800434a9f638c2 [file] [log] [blame]
Jeff Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070022#include <ui/InputDispatcher.h>
23#include <utils/KeyedVector.h>
24#include <utils/threads.h>
25#include <utils/Timers.h>
26#include <utils/RefBase.h>
27#include <utils/String8.h>
28#include <utils/BitSet.h>
29
30#include <stddef.h>
31#include <unistd.h>
32
Jeff Brown46b9ac02010-04-22 18:58:52 -070033namespace android {
34
Jeff Brown6d0fec22010-07-23 21:28:06 -070035class InputDevice;
36class InputMapper;
37
Jeff Brown8d608662010-08-30 03:02:23 -070038/* Describes a virtual key. */
39struct VirtualKeyDefinition {
40 int32_t scanCode;
41
42 // configured position data, specified in display coords
43 int32_t centerX;
44 int32_t centerY;
45 int32_t width;
46 int32_t height;
47};
48
49
50/* Specifies input device calibration settings. */
51class InputDeviceCalibration {
52public:
53 InputDeviceCalibration();
54
55 void clear();
56 void addProperty(const String8& key, const String8& value);
57
58 bool tryGetProperty(const String8& key, String8& outValue) const;
59 bool tryGetProperty(const String8& key, int32_t& outValue) const;
60 bool tryGetProperty(const String8& key, float& outValue) const;
61
62private:
63 KeyedVector<String8, String8> mProperties;
64};
65
Jeff Brown6d0fec22010-07-23 21:28:06 -070066
Jeff Brown9c3cda02010-06-15 01:31:58 -070067/*
68 * Input reader policy interface.
69 *
70 * The input reader policy is used by the input reader to interact with the Window Manager
71 * and other system components.
72 *
73 * The actual implementation is partially supported by callbacks into the DVM
74 * via JNI. This interface is also mocked in the unit tests.
75 */
76class InputReaderPolicyInterface : public virtual RefBase {
77protected:
78 InputReaderPolicyInterface() { }
79 virtual ~InputReaderPolicyInterface() { }
80
81public:
82 /* Display orientations. */
83 enum {
84 ROTATION_0 = 0,
85 ROTATION_90 = 1,
86 ROTATION_180 = 2,
87 ROTATION_270 = 3
88 };
89
90 /* Actions returned by interceptXXX methods. */
91 enum {
92 // The input dispatcher should do nothing and discard the input unless other
93 // flags are set.
94 ACTION_NONE = 0,
95
96 // The input dispatcher should dispatch the input to the application.
97 ACTION_DISPATCH = 0x00000001,
Jeff Brown9c3cda02010-06-15 01:31:58 -070098 };
99
Jeff Brown9c3cda02010-06-15 01:31:58 -0700100 /* Gets information about the display with the specified id.
101 * Returns true if the display info is available, false otherwise.
102 */
103 virtual bool getDisplayInfo(int32_t displayId,
104 int32_t* width, int32_t* height, int32_t* orientation) = 0;
105
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700106 /* Provides feedback for a virtual key down.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700107 */
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700108 virtual void virtualKeyDownFeedback() = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700109
110 /* Intercepts a key event.
111 * The policy can use this method as an opportunity to perform power management functions
Jeff Brown6d0fec22010-07-23 21:28:06 -0700112 * and early event preprocessing such as updating policy flags.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700113 *
114 * Returns a policy action constant such as ACTION_DISPATCH.
115 */
116 virtual int32_t interceptKey(nsecs_t when, int32_t deviceId,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700117 bool down, int32_t keyCode, int32_t scanCode, uint32_t& policyFlags) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700118
119 /* Intercepts a switch event.
120 * The policy can use this method as an opportunity to perform power management functions
Jeff Brown6d0fec22010-07-23 21:28:06 -0700121 * and early event preprocessing such as updating policy flags.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700122 *
123 * Switches are not dispatched to applications so this method should
124 * usually return ACTION_NONE.
125 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700126 virtual int32_t interceptSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
127 uint32_t& policyFlags) = 0;
128
129 /* Intercepts a generic touch, trackball or other event.
130 * The policy can use this method as an opportunity to perform power management functions
131 * and early event preprocessing such as updating policy flags.
132 *
133 * Returns a policy action constant such as ACTION_DISPATCH.
134 */
135 virtual int32_t interceptGeneric(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700136
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
Jeff Brown8d608662010-08-30 03:02:23 -0700151 /* Gets the calibration for an input device. */
152 virtual void getInputDeviceCalibration(const String8& deviceName,
153 InputDeviceCalibration& outCalibration) = 0;
154
Jeff Brown9c3cda02010-06-15 01:31:58 -0700155 /* Gets the excluded device names for the platform. */
156 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
157};
158
159
160/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700161class InputReaderInterface : public virtual RefBase {
162protected:
163 InputReaderInterface() { }
164 virtual ~InputReaderInterface() { }
165
166public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700167 /* Dumps the state of the input reader.
168 *
169 * This method may be called on any thread (usually by the input manager). */
170 virtual void dump(String8& dump) = 0;
171
Jeff Brown46b9ac02010-04-22 18:58:52 -0700172 /* Runs a single iteration of the processing loop.
173 * Nominally reads and processes one incoming message from the EventHub.
174 *
175 * This method should be called on the input reader thread.
176 */
177 virtual void loopOnce() = 0;
178
Jeff Brown9c3cda02010-06-15 01:31:58 -0700179 /* Gets the current input device configuration.
180 *
181 * This method may be called on any thread (usually by the input manager).
182 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700183 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700184
Jeff Brown6d0fec22010-07-23 21:28:06 -0700185 /* Gets information about the specified input device.
186 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
187 * was no such device.
188 *
189 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700190 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700191 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
192
193 /* Gets the list of all registered device ids. */
194 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
195
196 /* Query current input state. */
197 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
198 int32_t scanCode) = 0;
199 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
200 int32_t keyCode) = 0;
201 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
202 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700203
204 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700205 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
206 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
207};
208
209
210/* Internal interface used by individual input devices to access global input device state
211 * and parameters maintained by the input reader.
212 */
213class InputReaderContext {
214protected:
215 InputReaderContext() { }
216 virtual ~InputReaderContext() { }
217
218public:
219 virtual void updateGlobalMetaState() = 0;
220 virtual int32_t getGlobalMetaState() = 0;
221
222 virtual InputReaderPolicyInterface* getPolicy() = 0;
223 virtual InputDispatcherInterface* getDispatcher() = 0;
224 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700225};
226
Jeff Brown9c3cda02010-06-15 01:31:58 -0700227
Jeff Brown46b9ac02010-04-22 18:58:52 -0700228/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700229 * that it sends to the input dispatcher. Some functions of the input reader, such as early
230 * event filtering in low power states, are controlled by a separate policy object.
231 *
232 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700233 * Because the policy and dispatcher can potentially block or cause re-entrance into
234 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700235 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700236 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700237class InputReader : public InputReaderInterface, private InputReaderContext {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700238public:
239 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700240 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700241 const sp<InputDispatcherInterface>& dispatcher);
242 virtual ~InputReader();
243
Jeff Brownb88102f2010-09-08 11:49:43 -0700244 virtual void dump(String8& dump);
245
Jeff Brown46b9ac02010-04-22 18:58:52 -0700246 virtual void loopOnce();
247
Jeff Brown6d0fec22010-07-23 21:28:06 -0700248 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700249
Jeff Brown6d0fec22010-07-23 21:28:06 -0700250 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
251 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700252
Jeff Brown6d0fec22010-07-23 21:28:06 -0700253 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
254 int32_t scanCode);
255 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
256 int32_t keyCode);
257 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
258 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700259
Jeff Brown6d0fec22010-07-23 21:28:06 -0700260 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
261 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700262
Jeff Brown46b9ac02010-04-22 18:58:52 -0700263private:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700264 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700265 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700266 sp<InputDispatcherInterface> mDispatcher;
267
Jeff Brown6d0fec22010-07-23 21:28:06 -0700268 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
269 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
270 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
271
272 // This reader/writer lock guards the list of input devices.
273 // The writer lock must be held whenever the list of input devices is modified
274 // and then promptly released.
275 // The reader lock must be held whenever the list of input devices is traversed or an
276 // input device in the list is accessed.
277 // This lock only protects the registry and prevents inadvertent deletion of device objects
278 // that are in use. Individual devices are responsible for guarding their own internal state
279 // as needed for concurrent operation.
280 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700281 KeyedVector<int32_t, InputDevice*> mDevices;
282
Jeff Brown6d0fec22010-07-23 21:28:06 -0700283 // low-level input event decoding and device management
Jeff Brown46b9ac02010-04-22 18:58:52 -0700284 void process(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700285
Jeff Brown46b9ac02010-04-22 18:58:52 -0700286 void addDevice(nsecs_t when, int32_t deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700287 void removeDevice(nsecs_t when, int32_t deviceId);
288 InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700289 void configureExcludedDevices();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700290
Jeff Brown6d0fec22010-07-23 21:28:06 -0700291 void consumeEvent(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292
Jeff Brown6d0fec22010-07-23 21:28:06 -0700293 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700294
Jeff Brown6d0fec22010-07-23 21:28:06 -0700295 // state management for all devices
296 Mutex mStateLock;
297
298 int32_t mGlobalMetaState;
299 virtual void updateGlobalMetaState();
300 virtual int32_t getGlobalMetaState();
301
302 InputConfiguration mInputConfiguration;
303 void updateInputConfiguration();
304
305 // state queries
306 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
307 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
308 GetStateFunc getStateFunc);
309 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
310 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700311};
312
313
314/* Reads raw events from the event hub and processes them, endlessly. */
315class InputReaderThread : public Thread {
316public:
317 InputReaderThread(const sp<InputReaderInterface>& reader);
318 virtual ~InputReaderThread();
319
320private:
321 sp<InputReaderInterface> mReader;
322
323 virtual bool threadLoop();
324};
325
Jeff Brown6d0fec22010-07-23 21:28:06 -0700326
327/* Represents the state of a single input device. */
328class InputDevice {
329public:
330 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
331 ~InputDevice();
332
333 inline InputReaderContext* getContext() { return mContext; }
334 inline int32_t getId() { return mId; }
335 inline const String8& getName() { return mName; }
336 inline uint32_t getSources() { return mSources; }
337
338 inline bool isIgnored() { return mMappers.isEmpty(); }
339
Jeff Brownef3d7e82010-09-30 14:33:04 -0700340 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700341 void addMapper(InputMapper* mapper);
342 void configure();
343 void reset();
344 void process(const RawEvent* rawEvent);
345
346 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
347 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
348 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
349 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
350 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
351 const int32_t* keyCodes, uint8_t* outFlags);
352
353 int32_t getMetaState();
354
Jeff Brown8d608662010-08-30 03:02:23 -0700355 inline const InputDeviceCalibration& getCalibration() {
356 return mCalibration;
357 }
358
Jeff Brown6d0fec22010-07-23 21:28:06 -0700359private:
360 InputReaderContext* mContext;
361 int32_t mId;
362
363 Vector<InputMapper*> mMappers;
364
365 String8 mName;
366 uint32_t mSources;
367
368 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
369 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700370
371 InputDeviceCalibration mCalibration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700372};
373
374
375/* An input mapper transforms raw input events into cooked event data.
376 * A single input device can have multiple associated input mappers in order to interpret
377 * different classes of events.
378 */
379class InputMapper {
380public:
381 InputMapper(InputDevice* device);
382 virtual ~InputMapper();
383
384 inline InputDevice* getDevice() { return mDevice; }
385 inline int32_t getDeviceId() { return mDevice->getId(); }
386 inline const String8 getDeviceName() { return mDevice->getName(); }
387 inline InputReaderContext* getContext() { return mContext; }
388 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
389 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
390 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
391
392 virtual uint32_t getSources() = 0;
393 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700394 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 virtual void configure();
396 virtual void reset();
397 virtual void process(const RawEvent* rawEvent) = 0;
398
399 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
400 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
401 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
402 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
403 const int32_t* keyCodes, uint8_t* outFlags);
404
405 virtual int32_t getMetaState();
406
407protected:
408 InputDevice* mDevice;
409 InputReaderContext* mContext;
410
411 bool applyStandardPolicyActions(nsecs_t when, int32_t policyActions);
412};
413
414
415class SwitchInputMapper : public InputMapper {
416public:
417 SwitchInputMapper(InputDevice* device);
418 virtual ~SwitchInputMapper();
419
420 virtual uint32_t getSources();
421 virtual void process(const RawEvent* rawEvent);
422
423 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
424
425private:
426 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
427};
428
429
430class KeyboardInputMapper : public InputMapper {
431public:
432 KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
433 int32_t keyboardType);
434 virtual ~KeyboardInputMapper();
435
436 virtual uint32_t getSources();
437 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700438 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439 virtual void reset();
440 virtual void process(const RawEvent* rawEvent);
441
442 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
443 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
444 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
445 const int32_t* keyCodes, uint8_t* outFlags);
446
447 virtual int32_t getMetaState();
448
449private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700450 Mutex mLock;
451
Jeff Brown6d0fec22010-07-23 21:28:06 -0700452 struct KeyDown {
453 int32_t keyCode;
454 int32_t scanCode;
455 };
456
457 int32_t mAssociatedDisplayId;
458 uint32_t mSources;
459 int32_t mKeyboardType;
460
Jeff Brown6328cdc2010-07-29 18:18:33 -0700461 struct LockedState {
462 Vector<KeyDown> keyDowns; // keys that are down
463 int32_t metaState;
464 nsecs_t downTime; // time of most recent key down
465 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700466
Jeff Brown6328cdc2010-07-29 18:18:33 -0700467 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700468
469 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700470
Jeff Brown6d0fec22010-07-23 21:28:06 -0700471 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
472 uint32_t policyFlags);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700473 void applyPolicyAndDispatch(nsecs_t when, uint32_t policyFlags,
474 bool down, int32_t keyCode, int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700475
Jeff Brown6328cdc2010-07-29 18:18:33 -0700476 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700477};
478
479
480class TrackballInputMapper : public InputMapper {
481public:
482 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
483 virtual ~TrackballInputMapper();
484
485 virtual uint32_t getSources();
486 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700487 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700488 virtual void reset();
489 virtual void process(const RawEvent* rawEvent);
490
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700491 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
492
Jeff Brown6d0fec22010-07-23 21:28:06 -0700493private:
494 // Amount that trackball needs to move in order to generate a key event.
495 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
496
Jeff Brown6328cdc2010-07-29 18:18:33 -0700497 Mutex mLock;
498
Jeff Brown6d0fec22010-07-23 21:28:06 -0700499 int32_t mAssociatedDisplayId;
500
501 struct Accumulator {
502 enum {
503 FIELD_BTN_MOUSE = 1,
504 FIELD_REL_X = 2,
505 FIELD_REL_Y = 4
506 };
507
508 uint32_t fields;
509
510 bool btnMouse;
511 int32_t relX;
512 int32_t relY;
513
514 inline void clear() {
515 fields = 0;
516 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700517 } mAccumulator;
518
Jeff Brown6d0fec22010-07-23 21:28:06 -0700519 float mXScale;
520 float mYScale;
521 float mXPrecision;
522 float mYPrecision;
523
Jeff Brown6328cdc2010-07-29 18:18:33 -0700524 struct LockedState {
525 bool down;
526 nsecs_t downTime;
527 } mLocked;
528
529 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700530
531 void sync(nsecs_t when);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700532 void applyPolicyAndDispatch(nsecs_t when, int32_t motionEventAction,
533 PointerCoords* pointerCoords, nsecs_t downTime);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700534};
535
536
537class TouchInputMapper : public InputMapper {
538public:
539 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
540 virtual ~TouchInputMapper();
541
542 virtual uint32_t getSources();
543 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700544 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700545 virtual void configure();
546 virtual void reset();
547
548 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
549 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
550 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
551 const int32_t* keyCodes, uint8_t* outFlags);
552
553protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700554 Mutex mLock;
555
Jeff Brown6d0fec22010-07-23 21:28:06 -0700556 struct VirtualKey {
557 int32_t keyCode;
558 int32_t scanCode;
559 uint32_t flags;
560
561 // computed hit box, specified in touch screen coords based on known display size
562 int32_t hitLeft;
563 int32_t hitTop;
564 int32_t hitRight;
565 int32_t hitBottom;
566
567 inline bool isHit(int32_t x, int32_t y) const {
568 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
569 }
570 };
571
Jeff Brown8d608662010-08-30 03:02:23 -0700572 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700573 struct PointerData {
574 uint32_t id;
575 int32_t x;
576 int32_t y;
577 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700578 int32_t touchMajor;
579 int32_t touchMinor;
580 int32_t toolMajor;
581 int32_t toolMinor;
582 int32_t orientation;
583 };
584
Jeff Brown8d608662010-08-30 03:02:23 -0700585 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700586 struct TouchData {
587 uint32_t pointerCount;
588 PointerData pointers[MAX_POINTERS];
589 BitSet32 idBits;
590 uint32_t idToIndex[MAX_POINTER_ID + 1];
591
592 void copyFrom(const TouchData& other) {
593 pointerCount = other.pointerCount;
594 idBits = other.idBits;
595
596 for (uint32_t i = 0; i < pointerCount; i++) {
597 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700598
599 int id = pointers[i].id;
600 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700601 }
602 }
603
604 inline void clear() {
605 pointerCount = 0;
606 idBits.clear();
607 }
608 };
609
610 int32_t mAssociatedDisplayId;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700611
612 // Immutable configuration parameters.
613 struct Parameters {
614 bool useBadTouchFilter;
615 bool useJumpyTouchFilter;
616 bool useAveragingTouchFilter;
617 } mParameters;
618
Jeff Brown8d608662010-08-30 03:02:23 -0700619 // Immutable calibration parameters in parsed form.
620 struct Calibration {
621 // Touch Area
622 enum TouchAreaCalibration {
623 TOUCH_AREA_CALIBRATION_DEFAULT,
624 TOUCH_AREA_CALIBRATION_NONE,
625 TOUCH_AREA_CALIBRATION_GEOMETRIC,
626 TOUCH_AREA_CALIBRATION_PRESSURE,
627 };
628
629 TouchAreaCalibration touchAreaCalibration;
630
631 // Tool Area
632 enum ToolAreaCalibration {
633 TOOL_AREA_CALIBRATION_DEFAULT,
634 TOOL_AREA_CALIBRATION_NONE,
635 TOOL_AREA_CALIBRATION_GEOMETRIC,
636 TOOL_AREA_CALIBRATION_LINEAR,
637 };
638
639 ToolAreaCalibration toolAreaCalibration;
640 bool haveToolAreaLinearScale;
641 float toolAreaLinearScale;
642 bool haveToolAreaLinearBias;
643 float toolAreaLinearBias;
644 bool haveToolAreaIsSummed;
645 int32_t toolAreaIsSummed;
646
647 // Pressure
648 enum PressureCalibration {
649 PRESSURE_CALIBRATION_DEFAULT,
650 PRESSURE_CALIBRATION_NONE,
651 PRESSURE_CALIBRATION_PHYSICAL,
652 PRESSURE_CALIBRATION_AMPLITUDE,
653 };
654 enum PressureSource {
655 PRESSURE_SOURCE_DEFAULT,
656 PRESSURE_SOURCE_PRESSURE,
657 PRESSURE_SOURCE_TOUCH,
658 };
659
660 PressureCalibration pressureCalibration;
661 PressureSource pressureSource;
662 bool havePressureScale;
663 float pressureScale;
664
665 // Size
666 enum SizeCalibration {
667 SIZE_CALIBRATION_DEFAULT,
668 SIZE_CALIBRATION_NONE,
669 SIZE_CALIBRATION_NORMALIZED,
670 };
671
672 SizeCalibration sizeCalibration;
673
674 // Orientation
675 enum OrientationCalibration {
676 ORIENTATION_CALIBRATION_DEFAULT,
677 ORIENTATION_CALIBRATION_NONE,
678 ORIENTATION_CALIBRATION_INTERPOLATED,
679 };
680
681 OrientationCalibration orientationCalibration;
682 } mCalibration;
683
684 // Raw axis information from the driver.
685 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700686 RawAbsoluteAxisInfo x;
687 RawAbsoluteAxisInfo y;
688 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700689 RawAbsoluteAxisInfo touchMajor;
690 RawAbsoluteAxisInfo touchMinor;
691 RawAbsoluteAxisInfo toolMajor;
692 RawAbsoluteAxisInfo toolMinor;
693 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700694 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700695
Jeff Brown6328cdc2010-07-29 18:18:33 -0700696 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700697 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700698 TouchData mLastTouch;
699
700 // The time the primary pointer last went down.
701 nsecs_t mDownTime;
702
Jeff Brown6328cdc2010-07-29 18:18:33 -0700703 struct LockedState {
704 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700705
Jeff Brown6328cdc2010-07-29 18:18:33 -0700706 // The surface orientation and width and height set by configureSurfaceLocked().
707 int32_t surfaceOrientation;
708 int32_t surfaceWidth, surfaceHeight;
709
710 // Translation and scaling factors, orientation-independent.
711 int32_t xOrigin;
712 float xScale;
713 float xPrecision;
714
715 int32_t yOrigin;
716 float yScale;
717 float yPrecision;
718
Jeff Brown8d608662010-08-30 03:02:23 -0700719 float geometricScale;
720
721 float toolAreaLinearScale;
722 float toolAreaLinearBias;
723
Jeff Brown6328cdc2010-07-29 18:18:33 -0700724 float pressureScale;
725
Jeff Brown6328cdc2010-07-29 18:18:33 -0700726 float sizeScale;
727
728 float orientationScale;
729
730 // Oriented motion ranges for input device info.
731 struct OrientedRanges {
732 InputDeviceInfo::MotionRange x;
733 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700734
735 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700736 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700737
738 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700739 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700740
741 bool haveTouchArea;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700742 InputDeviceInfo::MotionRange touchMajor;
743 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700744
745 bool haveToolArea;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700746 InputDeviceInfo::MotionRange toolMajor;
747 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700748
749 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700750 InputDeviceInfo::MotionRange orientation;
751 } orientedRanges;
752
753 // Oriented dimensions and precision.
754 float orientedSurfaceWidth, orientedSurfaceHeight;
755 float orientedXPrecision, orientedYPrecision;
756
757 struct CurrentVirtualKeyState {
758 bool down;
759 nsecs_t downTime;
760 int32_t keyCode;
761 int32_t scanCode;
762 } currentVirtualKey;
763 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700764
Jeff Brown8d608662010-08-30 03:02:23 -0700765 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700766 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700767 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700768 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700769 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700770 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700771 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700772 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700773 virtual void parseCalibration();
774 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700775 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700776
777 enum TouchResult {
778 // Dispatch the touch normally.
779 DISPATCH_TOUCH,
780 // Do not dispatch the touch, but keep tracking the current stroke.
781 SKIP_TOUCH,
782 // Do not dispatch the touch, and drop all information associated with the current stoke
783 // so the next movement will appear as a new down.
784 DROP_STROKE
785 };
786
787 void syncTouch(nsecs_t when, bool havePointerIds);
788
789private:
790 /* Maximum number of historical samples to average. */
791 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
792
793 /* Slop distance for jumpy pointer detection.
794 * The vertical range of the screen divided by this is our epsilon value. */
795 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
796
797 /* Number of jumpy points to drop for touchscreens that need it. */
798 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
799 static const uint32_t JUMPY_DROP_LIMIT = 3;
800
801 /* Maximum squared distance for averaging.
802 * If moving farther than this, turn of averaging to avoid lag in response. */
803 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
804
805 struct AveragingTouchFilterState {
806 // Individual history tracks are stored by pointer id
807 uint32_t historyStart[MAX_POINTERS];
808 uint32_t historyEnd[MAX_POINTERS];
809 struct {
810 struct {
811 int32_t x;
812 int32_t y;
813 int32_t pressure;
814 } pointers[MAX_POINTERS];
815 } historyData[AVERAGING_HISTORY_SIZE];
816 } mAveragingTouchFilter;
817
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700818 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700819 uint32_t jumpyPointsDropped;
820 } mJumpyTouchFilter;
821
822 struct PointerDistanceHeapElement {
823 uint32_t currentPointerIndex : 8;
824 uint32_t lastPointerIndex : 8;
825 uint64_t distance : 48; // squared distance
826 };
827
Jeff Brown6328cdc2010-07-29 18:18:33 -0700828 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700829
830 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
831 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
832 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700833 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
834 int32_t motionEventAction);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700835
Jeff Brown6328cdc2010-07-29 18:18:33 -0700836 void applyPolicyAndDispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
837 int32_t keyEventAction, int32_t keyEventFlags,
838 int32_t keyCode, int32_t scanCode, nsecs_t downTime);
839
840 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
841 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700842
843 bool applyBadTouchFilter();
844 bool applyJumpyTouchFilter();
845 void applyAveragingTouchFilter();
846 void calculatePointerIds();
847};
848
849
850class SingleTouchInputMapper : public TouchInputMapper {
851public:
852 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
853 virtual ~SingleTouchInputMapper();
854
855 virtual void reset();
856 virtual void process(const RawEvent* rawEvent);
857
858protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700859 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700860
861private:
862 struct Accumulator {
863 enum {
864 FIELD_BTN_TOUCH = 1,
865 FIELD_ABS_X = 2,
866 FIELD_ABS_Y = 4,
867 FIELD_ABS_PRESSURE = 8,
868 FIELD_ABS_TOOL_WIDTH = 16
869 };
870
871 uint32_t fields;
872
873 bool btnTouch;
874 int32_t absX;
875 int32_t absY;
876 int32_t absPressure;
877 int32_t absToolWidth;
878
879 inline void clear() {
880 fields = 0;
881 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700882 } mAccumulator;
883
884 bool mDown;
885 int32_t mX;
886 int32_t mY;
887 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700888 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700889
890 void initialize();
891
892 void sync(nsecs_t when);
893};
894
895
896class MultiTouchInputMapper : public TouchInputMapper {
897public:
898 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
899 virtual ~MultiTouchInputMapper();
900
901 virtual void reset();
902 virtual void process(const RawEvent* rawEvent);
903
904protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700905 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700906
907private:
908 struct Accumulator {
909 enum {
910 FIELD_ABS_MT_POSITION_X = 1,
911 FIELD_ABS_MT_POSITION_Y = 2,
912 FIELD_ABS_MT_TOUCH_MAJOR = 4,
913 FIELD_ABS_MT_TOUCH_MINOR = 8,
914 FIELD_ABS_MT_WIDTH_MAJOR = 16,
915 FIELD_ABS_MT_WIDTH_MINOR = 32,
916 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700917 FIELD_ABS_MT_TRACKING_ID = 128,
918 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700919 };
920
921 uint32_t pointerCount;
922 struct Pointer {
923 uint32_t fields;
924
925 int32_t absMTPositionX;
926 int32_t absMTPositionY;
927 int32_t absMTTouchMajor;
928 int32_t absMTTouchMinor;
929 int32_t absMTWidthMajor;
930 int32_t absMTWidthMinor;
931 int32_t absMTOrientation;
932 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700933 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700934
935 inline void clear() {
936 fields = 0;
937 }
938 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
939
940 inline void clear() {
941 pointerCount = 0;
942 pointers[0].clear();
943 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700944 } mAccumulator;
945
946 void initialize();
947
948 void sync(nsecs_t when);
949};
950
Jeff Brown46b9ac02010-04-22 18:58:52 -0700951} // namespace android
952
953#endif // _UI_INPUT_READER_H