blob: 6bf1bfaeb681595afdd495bd61dae7c1a70498a5 [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
38
Jeff Brown9c3cda02010-06-15 01:31:58 -070039/*
40 * Input reader policy interface.
41 *
42 * The input reader policy is used by the input reader to interact with the Window Manager
43 * and other system components.
44 *
45 * The actual implementation is partially supported by callbacks into the DVM
46 * via JNI. This interface is also mocked in the unit tests.
47 */
48class InputReaderPolicyInterface : public virtual RefBase {
49protected:
50 InputReaderPolicyInterface() { }
51 virtual ~InputReaderPolicyInterface() { }
52
53public:
54 /* Display orientations. */
55 enum {
56 ROTATION_0 = 0,
57 ROTATION_90 = 1,
58 ROTATION_180 = 2,
59 ROTATION_270 = 3
60 };
61
62 /* Actions returned by interceptXXX methods. */
63 enum {
64 // The input dispatcher should do nothing and discard the input unless other
65 // flags are set.
66 ACTION_NONE = 0,
67
68 // The input dispatcher should dispatch the input to the application.
69 ACTION_DISPATCH = 0x00000001,
70
71 // The input dispatcher should perform special filtering in preparation for
72 // a pending app switch.
73 ACTION_APP_SWITCH_COMING = 0x00000002,
Jeff Brown9c3cda02010-06-15 01:31:58 -070074 };
75
76 /* Describes a virtual key. */
77 struct VirtualKeyDefinition {
78 int32_t scanCode;
79
80 // configured position data, specified in display coords
81 int32_t centerX;
82 int32_t centerY;
83 int32_t width;
84 int32_t height;
85 };
86
87 /* Gets information about the display with the specified id.
88 * Returns true if the display info is available, false otherwise.
89 */
90 virtual bool getDisplayInfo(int32_t displayId,
91 int32_t* width, int32_t* height, int32_t* orientation) = 0;
92
Jeff Brown00fa7bd2010-07-02 15:37:36 -070093 /* Provides feedback for a virtual key down.
Jeff Brown9c3cda02010-06-15 01:31:58 -070094 */
Jeff Brown00fa7bd2010-07-02 15:37:36 -070095 virtual void virtualKeyDownFeedback() = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -070096
97 /* Intercepts a key event.
98 * The policy can use this method as an opportunity to perform power management functions
Jeff Brown6d0fec22010-07-23 21:28:06 -070099 * and early event preprocessing such as updating policy flags.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700100 *
101 * Returns a policy action constant such as ACTION_DISPATCH.
102 */
103 virtual int32_t interceptKey(nsecs_t when, int32_t deviceId,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700104 bool down, int32_t keyCode, int32_t scanCode, uint32_t& policyFlags) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700105
106 /* Intercepts a switch event.
107 * The policy can use this method as an opportunity to perform power management functions
Jeff Brown6d0fec22010-07-23 21:28:06 -0700108 * and early event preprocessing such as updating policy flags.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700109 *
110 * Switches are not dispatched to applications so this method should
111 * usually return ACTION_NONE.
112 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700113 virtual int32_t interceptSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
114 uint32_t& policyFlags) = 0;
115
116 /* Intercepts a generic touch, trackball or other event.
117 * The policy can use this method as an opportunity to perform power management functions
118 * and early event preprocessing such as updating policy flags.
119 *
120 * Returns a policy action constant such as ACTION_DISPATCH.
121 */
122 virtual int32_t interceptGeneric(nsecs_t when, uint32_t& policyFlags) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700123
124 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
125 * certain device whose screen currently is not all that good.
126 */
127 virtual bool filterTouchEvents() = 0;
128
129 /* Determines whether to turn on some hacks to improve touch interaction with another device
130 * where touch coordinate data can get corrupted.
131 */
132 virtual bool filterJumpyTouchEvents() = 0;
133
134 /* Gets the configured virtual key definitions for an input device. */
135 virtual void getVirtualKeyDefinitions(const String8& deviceName,
136 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
137
138 /* Gets the excluded device names for the platform. */
139 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
140};
141
142
143/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700144class InputReaderInterface : public virtual RefBase {
145protected:
146 InputReaderInterface() { }
147 virtual ~InputReaderInterface() { }
148
149public:
150 /* Runs a single iteration of the processing loop.
151 * Nominally reads and processes one incoming message from the EventHub.
152 *
153 * This method should be called on the input reader thread.
154 */
155 virtual void loopOnce() = 0;
156
Jeff Brown9c3cda02010-06-15 01:31:58 -0700157 /* Gets the current input device configuration.
158 *
159 * This method may be called on any thread (usually by the input manager).
160 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700161 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700162
Jeff Brown6d0fec22010-07-23 21:28:06 -0700163 /* Gets information about the specified input device.
164 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
165 * was no such device.
166 *
167 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700168 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700169 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
170
171 /* Gets the list of all registered device ids. */
172 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
173
174 /* Query current input state. */
175 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
176 int32_t scanCode) = 0;
177 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
178 int32_t keyCode) = 0;
179 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
180 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700181
182 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700183 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
184 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
185};
186
187
188/* Internal interface used by individual input devices to access global input device state
189 * and parameters maintained by the input reader.
190 */
191class InputReaderContext {
192protected:
193 InputReaderContext() { }
194 virtual ~InputReaderContext() { }
195
196public:
197 virtual void updateGlobalMetaState() = 0;
198 virtual int32_t getGlobalMetaState() = 0;
199
200 virtual InputReaderPolicyInterface* getPolicy() = 0;
201 virtual InputDispatcherInterface* getDispatcher() = 0;
202 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700203};
204
Jeff Brown9c3cda02010-06-15 01:31:58 -0700205
Jeff Brown46b9ac02010-04-22 18:58:52 -0700206/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700207 * that it sends to the input dispatcher. Some functions of the input reader, such as early
208 * event filtering in low power states, are controlled by a separate policy object.
209 *
210 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700211 * Because the policy and dispatcher can potentially block or cause re-entrance into
212 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700213 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700214 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700215class InputReader : public InputReaderInterface, private InputReaderContext {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700216public:
217 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700218 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700219 const sp<InputDispatcherInterface>& dispatcher);
220 virtual ~InputReader();
221
222 virtual void loopOnce();
223
Jeff Brown6d0fec22010-07-23 21:28:06 -0700224 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700225
Jeff Brown6d0fec22010-07-23 21:28:06 -0700226 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
227 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700228
Jeff Brown6d0fec22010-07-23 21:28:06 -0700229 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
230 int32_t scanCode);
231 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
232 int32_t keyCode);
233 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
234 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700235
Jeff Brown6d0fec22010-07-23 21:28:06 -0700236 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
237 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700238
Jeff Brown46b9ac02010-04-22 18:58:52 -0700239private:
Jeff Brown46b9ac02010-04-22 18:58:52 -0700240 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700241 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700242 sp<InputDispatcherInterface> mDispatcher;
243
Jeff Brown6d0fec22010-07-23 21:28:06 -0700244 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
245 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
246 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
247
248 // This reader/writer lock guards the list of input devices.
249 // The writer lock must be held whenever the list of input devices is modified
250 // and then promptly released.
251 // The reader lock must be held whenever the list of input devices is traversed or an
252 // input device in the list is accessed.
253 // This lock only protects the registry and prevents inadvertent deletion of device objects
254 // that are in use. Individual devices are responsible for guarding their own internal state
255 // as needed for concurrent operation.
256 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700257 KeyedVector<int32_t, InputDevice*> mDevices;
258
Jeff Brown6d0fec22010-07-23 21:28:06 -0700259 // low-level input event decoding and device management
Jeff Brown46b9ac02010-04-22 18:58:52 -0700260 void process(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700261
Jeff Brown46b9ac02010-04-22 18:58:52 -0700262 void addDevice(nsecs_t when, int32_t deviceId);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700263 void removeDevice(nsecs_t when, int32_t deviceId);
264 InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700265 void configureExcludedDevices();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700266
Jeff Brown6d0fec22010-07-23 21:28:06 -0700267 void consumeEvent(const RawEvent* rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700268
Jeff Brown6d0fec22010-07-23 21:28:06 -0700269 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270
Jeff Brown6d0fec22010-07-23 21:28:06 -0700271 // state management for all devices
272 Mutex mStateLock;
273
274 int32_t mGlobalMetaState;
275 virtual void updateGlobalMetaState();
276 virtual int32_t getGlobalMetaState();
277
278 InputConfiguration mInputConfiguration;
279 void updateInputConfiguration();
280
281 // state queries
282 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
283 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
284 GetStateFunc getStateFunc);
285 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
286 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700287};
288
289
290/* Reads raw events from the event hub and processes them, endlessly. */
291class InputReaderThread : public Thread {
292public:
293 InputReaderThread(const sp<InputReaderInterface>& reader);
294 virtual ~InputReaderThread();
295
296private:
297 sp<InputReaderInterface> mReader;
298
299 virtual bool threadLoop();
300};
301
Jeff Brown6d0fec22010-07-23 21:28:06 -0700302
303/* Represents the state of a single input device. */
304class InputDevice {
305public:
306 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
307 ~InputDevice();
308
309 inline InputReaderContext* getContext() { return mContext; }
310 inline int32_t getId() { return mId; }
311 inline const String8& getName() { return mName; }
312 inline uint32_t getSources() { return mSources; }
313
314 inline bool isIgnored() { return mMappers.isEmpty(); }
315
316 void addMapper(InputMapper* mapper);
317 void configure();
318 void reset();
319 void process(const RawEvent* rawEvent);
320
321 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
322 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
323 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
324 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
325 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
326 const int32_t* keyCodes, uint8_t* outFlags);
327
328 int32_t getMetaState();
329
330private:
331 InputReaderContext* mContext;
332 int32_t mId;
333
334 Vector<InputMapper*> mMappers;
335
336 String8 mName;
337 uint32_t mSources;
338
339 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
340 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
341};
342
343
344/* An input mapper transforms raw input events into cooked event data.
345 * A single input device can have multiple associated input mappers in order to interpret
346 * different classes of events.
347 */
348class InputMapper {
349public:
350 InputMapper(InputDevice* device);
351 virtual ~InputMapper();
352
353 inline InputDevice* getDevice() { return mDevice; }
354 inline int32_t getDeviceId() { return mDevice->getId(); }
355 inline const String8 getDeviceName() { return mDevice->getName(); }
356 inline InputReaderContext* getContext() { return mContext; }
357 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
358 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
359 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
360
361 virtual uint32_t getSources() = 0;
362 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
363 virtual void configure();
364 virtual void reset();
365 virtual void process(const RawEvent* rawEvent) = 0;
366
367 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
368 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
369 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
370 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
371 const int32_t* keyCodes, uint8_t* outFlags);
372
373 virtual int32_t getMetaState();
374
375protected:
376 InputDevice* mDevice;
377 InputReaderContext* mContext;
378
379 bool applyStandardPolicyActions(nsecs_t when, int32_t policyActions);
380};
381
382
383class SwitchInputMapper : public InputMapper {
384public:
385 SwitchInputMapper(InputDevice* device);
386 virtual ~SwitchInputMapper();
387
388 virtual uint32_t getSources();
389 virtual void process(const RawEvent* rawEvent);
390
391 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
392
393private:
394 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
395};
396
397
398class KeyboardInputMapper : public InputMapper {
399public:
400 KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
401 int32_t keyboardType);
402 virtual ~KeyboardInputMapper();
403
404 virtual uint32_t getSources();
405 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
406 virtual void reset();
407 virtual void process(const RawEvent* rawEvent);
408
409 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
410 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
411 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
412 const int32_t* keyCodes, uint8_t* outFlags);
413
414 virtual int32_t getMetaState();
415
416private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700417 Mutex mLock;
418
Jeff Brown6d0fec22010-07-23 21:28:06 -0700419 struct KeyDown {
420 int32_t keyCode;
421 int32_t scanCode;
422 };
423
424 int32_t mAssociatedDisplayId;
425 uint32_t mSources;
426 int32_t mKeyboardType;
427
Jeff Brown6328cdc2010-07-29 18:18:33 -0700428 struct LockedState {
429 Vector<KeyDown> keyDowns; // keys that are down
430 int32_t metaState;
431 nsecs_t downTime; // time of most recent key down
432 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700433
Jeff Brown6328cdc2010-07-29 18:18:33 -0700434 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435
436 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700437
Jeff Brown6d0fec22010-07-23 21:28:06 -0700438 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
439 uint32_t policyFlags);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700440 void applyPolicyAndDispatch(nsecs_t when, uint32_t policyFlags,
441 bool down, int32_t keyCode, int32_t scanCode, int32_t metaState, nsecs_t downTime);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442
Jeff Brown6328cdc2010-07-29 18:18:33 -0700443 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444};
445
446
447class TrackballInputMapper : public InputMapper {
448public:
449 TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
450 virtual ~TrackballInputMapper();
451
452 virtual uint32_t getSources();
453 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
454 virtual void reset();
455 virtual void process(const RawEvent* rawEvent);
456
457private:
458 // Amount that trackball needs to move in order to generate a key event.
459 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
460
Jeff Brown6328cdc2010-07-29 18:18:33 -0700461 Mutex mLock;
462
Jeff Brown6d0fec22010-07-23 21:28:06 -0700463 int32_t mAssociatedDisplayId;
464
465 struct Accumulator {
466 enum {
467 FIELD_BTN_MOUSE = 1,
468 FIELD_REL_X = 2,
469 FIELD_REL_Y = 4
470 };
471
472 uint32_t fields;
473
474 bool btnMouse;
475 int32_t relX;
476 int32_t relY;
477
478 inline void clear() {
479 fields = 0;
480 }
481
482 inline bool isDirty() {
483 return fields != 0;
484 }
485 } mAccumulator;
486
Jeff Brown6d0fec22010-07-23 21:28:06 -0700487 float mXScale;
488 float mYScale;
489 float mXPrecision;
490 float mYPrecision;
491
Jeff Brown6328cdc2010-07-29 18:18:33 -0700492 struct LockedState {
493 bool down;
494 nsecs_t downTime;
495 } mLocked;
496
497 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700498
499 void sync(nsecs_t when);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700500 void applyPolicyAndDispatch(nsecs_t when, int32_t motionEventAction,
501 PointerCoords* pointerCoords, nsecs_t downTime);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700502};
503
504
505class TouchInputMapper : public InputMapper {
506public:
507 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
508 virtual ~TouchInputMapper();
509
510 virtual uint32_t getSources();
511 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
512 virtual void configure();
513 virtual void reset();
514
515 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
516 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
517 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
518 const int32_t* keyCodes, uint8_t* outFlags);
519
520protected:
521 /* Maximum pointer id value supported.
522 * (This is limited by our use of BitSet32 to track pointer assignments.) */
523 static const uint32_t MAX_POINTER_ID = 31;
524
Jeff Brown6328cdc2010-07-29 18:18:33 -0700525 Mutex mLock;
526
Jeff Brown6d0fec22010-07-23 21:28:06 -0700527 struct VirtualKey {
528 int32_t keyCode;
529 int32_t scanCode;
530 uint32_t flags;
531
532 // computed hit box, specified in touch screen coords based on known display size
533 int32_t hitLeft;
534 int32_t hitTop;
535 int32_t hitRight;
536 int32_t hitBottom;
537
538 inline bool isHit(int32_t x, int32_t y) const {
539 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
540 }
541 };
542
543 struct PointerData {
544 uint32_t id;
545 int32_t x;
546 int32_t y;
547 int32_t pressure;
548 int32_t size;
549 int32_t touchMajor;
550 int32_t touchMinor;
551 int32_t toolMajor;
552 int32_t toolMinor;
553 int32_t orientation;
554 };
555
556 struct TouchData {
557 uint32_t pointerCount;
558 PointerData pointers[MAX_POINTERS];
559 BitSet32 idBits;
560 uint32_t idToIndex[MAX_POINTER_ID + 1];
561
562 void copyFrom(const TouchData& other) {
563 pointerCount = other.pointerCount;
564 idBits = other.idBits;
565
566 for (uint32_t i = 0; i < pointerCount; i++) {
567 pointers[i] = other.pointers[i];
568 idToIndex[i] = other.idToIndex[i];
569 }
570 }
571
572 inline void clear() {
573 pointerCount = 0;
574 idBits.clear();
575 }
576 };
577
578 int32_t mAssociatedDisplayId;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700579
580 // Immutable configuration parameters.
581 struct Parameters {
582 bool useBadTouchFilter;
583 bool useJumpyTouchFilter;
584 bool useAveragingTouchFilter;
585 } mParameters;
586
587 // Raw axis information.
588 struct Axes {
589 RawAbsoluteAxisInfo x;
590 RawAbsoluteAxisInfo y;
591 RawAbsoluteAxisInfo pressure;
592 RawAbsoluteAxisInfo size;
593 RawAbsoluteAxisInfo touchMajor;
594 RawAbsoluteAxisInfo touchMinor;
595 RawAbsoluteAxisInfo toolMajor;
596 RawAbsoluteAxisInfo toolMinor;
597 RawAbsoluteAxisInfo orientation;
598 } mAxes;
599
Jeff Brown6328cdc2010-07-29 18:18:33 -0700600 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700601 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700602 TouchData mLastTouch;
603
604 // The time the primary pointer last went down.
605 nsecs_t mDownTime;
606
Jeff Brown6328cdc2010-07-29 18:18:33 -0700607 struct LockedState {
608 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700609
Jeff Brown6328cdc2010-07-29 18:18:33 -0700610 // The surface orientation and width and height set by configureSurfaceLocked().
611 int32_t surfaceOrientation;
612 int32_t surfaceWidth, surfaceHeight;
613
614 // Translation and scaling factors, orientation-independent.
615 int32_t xOrigin;
616 float xScale;
617 float xPrecision;
618
619 int32_t yOrigin;
620 float yScale;
621 float yPrecision;
622
623 int32_t pressureOrigin;
624 float pressureScale;
625
626 int32_t sizeOrigin;
627 float sizeScale;
628
629 float orientationScale;
630
631 // Oriented motion ranges for input device info.
632 struct OrientedRanges {
633 InputDeviceInfo::MotionRange x;
634 InputDeviceInfo::MotionRange y;
635 InputDeviceInfo::MotionRange pressure;
636 InputDeviceInfo::MotionRange size;
637 InputDeviceInfo::MotionRange touchMajor;
638 InputDeviceInfo::MotionRange touchMinor;
639 InputDeviceInfo::MotionRange toolMajor;
640 InputDeviceInfo::MotionRange toolMinor;
641 InputDeviceInfo::MotionRange orientation;
642 } orientedRanges;
643
644 // Oriented dimensions and precision.
645 float orientedSurfaceWidth, orientedSurfaceHeight;
646 float orientedXPrecision, orientedYPrecision;
647
648 struct CurrentVirtualKeyState {
649 bool down;
650 nsecs_t downTime;
651 int32_t keyCode;
652 int32_t scanCode;
653 } currentVirtualKey;
654 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700655
656 virtual void configureAxes();
Jeff Brown6328cdc2010-07-29 18:18:33 -0700657 virtual bool configureSurfaceLocked();
658 virtual void configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700659
660 enum TouchResult {
661 // Dispatch the touch normally.
662 DISPATCH_TOUCH,
663 // Do not dispatch the touch, but keep tracking the current stroke.
664 SKIP_TOUCH,
665 // Do not dispatch the touch, and drop all information associated with the current stoke
666 // so the next movement will appear as a new down.
667 DROP_STROKE
668 };
669
670 void syncTouch(nsecs_t when, bool havePointerIds);
671
672private:
673 /* Maximum number of historical samples to average. */
674 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
675
676 /* Slop distance for jumpy pointer detection.
677 * The vertical range of the screen divided by this is our epsilon value. */
678 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
679
680 /* Number of jumpy points to drop for touchscreens that need it. */
681 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
682 static const uint32_t JUMPY_DROP_LIMIT = 3;
683
684 /* Maximum squared distance for averaging.
685 * If moving farther than this, turn of averaging to avoid lag in response. */
686 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
687
688 struct AveragingTouchFilterState {
689 // Individual history tracks are stored by pointer id
690 uint32_t historyStart[MAX_POINTERS];
691 uint32_t historyEnd[MAX_POINTERS];
692 struct {
693 struct {
694 int32_t x;
695 int32_t y;
696 int32_t pressure;
697 } pointers[MAX_POINTERS];
698 } historyData[AVERAGING_HISTORY_SIZE];
699 } mAveragingTouchFilter;
700
701 struct JumpTouchFilterState {
702 uint32_t jumpyPointsDropped;
703 } mJumpyTouchFilter;
704
705 struct PointerDistanceHeapElement {
706 uint32_t currentPointerIndex : 8;
707 uint32_t lastPointerIndex : 8;
708 uint64_t distance : 48; // squared distance
709 };
710
Jeff Brown6328cdc2010-07-29 18:18:33 -0700711 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700712
713 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
714 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
715 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
716 BitSet32 idBits, uint32_t changedId, int32_t motionEventAction);
717
Jeff Brown6328cdc2010-07-29 18:18:33 -0700718 void applyPolicyAndDispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
719 int32_t keyEventAction, int32_t keyEventFlags,
720 int32_t keyCode, int32_t scanCode, nsecs_t downTime);
721
722 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
723 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700724
725 bool applyBadTouchFilter();
726 bool applyJumpyTouchFilter();
727 void applyAveragingTouchFilter();
728 void calculatePointerIds();
729};
730
731
732class SingleTouchInputMapper : public TouchInputMapper {
733public:
734 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
735 virtual ~SingleTouchInputMapper();
736
737 virtual void reset();
738 virtual void process(const RawEvent* rawEvent);
739
740protected:
741 virtual void configureAxes();
742
743private:
744 struct Accumulator {
745 enum {
746 FIELD_BTN_TOUCH = 1,
747 FIELD_ABS_X = 2,
748 FIELD_ABS_Y = 4,
749 FIELD_ABS_PRESSURE = 8,
750 FIELD_ABS_TOOL_WIDTH = 16
751 };
752
753 uint32_t fields;
754
755 bool btnTouch;
756 int32_t absX;
757 int32_t absY;
758 int32_t absPressure;
759 int32_t absToolWidth;
760
761 inline void clear() {
762 fields = 0;
763 }
764
765 inline bool isDirty() {
766 return fields != 0;
767 }
768 } mAccumulator;
769
770 bool mDown;
771 int32_t mX;
772 int32_t mY;
773 int32_t mPressure;
774 int32_t mSize;
775
776 void initialize();
777
778 void sync(nsecs_t when);
779};
780
781
782class MultiTouchInputMapper : public TouchInputMapper {
783public:
784 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
785 virtual ~MultiTouchInputMapper();
786
787 virtual void reset();
788 virtual void process(const RawEvent* rawEvent);
789
790protected:
791 virtual void configureAxes();
792
793private:
794 struct Accumulator {
795 enum {
796 FIELD_ABS_MT_POSITION_X = 1,
797 FIELD_ABS_MT_POSITION_Y = 2,
798 FIELD_ABS_MT_TOUCH_MAJOR = 4,
799 FIELD_ABS_MT_TOUCH_MINOR = 8,
800 FIELD_ABS_MT_WIDTH_MAJOR = 16,
801 FIELD_ABS_MT_WIDTH_MINOR = 32,
802 FIELD_ABS_MT_ORIENTATION = 64,
803 FIELD_ABS_MT_TRACKING_ID = 128
804 };
805
806 uint32_t pointerCount;
807 struct Pointer {
808 uint32_t fields;
809
810 int32_t absMTPositionX;
811 int32_t absMTPositionY;
812 int32_t absMTTouchMajor;
813 int32_t absMTTouchMinor;
814 int32_t absMTWidthMajor;
815 int32_t absMTWidthMinor;
816 int32_t absMTOrientation;
817 int32_t absMTTrackingId;
818
819 inline void clear() {
820 fields = 0;
821 }
822 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
823
824 inline void clear() {
825 pointerCount = 0;
826 pointers[0].clear();
827 }
828
829 inline bool isDirty() {
830 return pointerCount != 0;
831 }
832 } mAccumulator;
833
834 void initialize();
835
836 void sync(nsecs_t when);
837};
838
Jeff Brown46b9ac02010-04-22 18:58:52 -0700839} // namespace android
840
841#endif // _UI_INPUT_READER_H