blob: 71c6c517692d10424873ff88a0bbaf702097d42c [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 Browne839a582010-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 Browne839a582010-04-22 18:58:52 -070033namespace android {
34
Jeff Browne57e8952010-07-23 21:28:06 -070035class InputDevice;
36class InputMapper;
37
38
Jeff Brown54bc2812010-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 Brown54bc2812010-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 Brownf16c26d2010-07-02 15:37:36 -070093 /* Provides feedback for a virtual key down.
Jeff Brown54bc2812010-06-15 01:31:58 -070094 */
Jeff Brownf16c26d2010-07-02 15:37:36 -070095 virtual void virtualKeyDownFeedback() = 0;
Jeff Brown54bc2812010-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 Browne57e8952010-07-23 21:28:06 -070099 * and early event preprocessing such as updating policy flags.
Jeff Brown54bc2812010-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 Browne57e8952010-07-23 21:28:06 -0700104 bool down, int32_t keyCode, int32_t scanCode, uint32_t& policyFlags) = 0;
Jeff Brown54bc2812010-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 Browne57e8952010-07-23 21:28:06 -0700108 * and early event preprocessing such as updating policy flags.
Jeff Brown54bc2812010-06-15 01:31:58 -0700109 *
110 * Switches are not dispatched to applications so this method should
111 * usually return ACTION_NONE.
112 */
Jeff Browne57e8952010-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 Brown54bc2812010-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 Browne839a582010-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 Brown54bc2812010-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 Browne57e8952010-07-23 21:28:06 -0700161 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown54bc2812010-06-15 01:31:58 -0700162
Jeff Browne57e8952010-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 Brown54bc2812010-06-15 01:31:58 -0700168 */
Jeff Browne57e8952010-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 Brown54bc2812010-06-15 01:31:58 -0700181
182 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700203};
204
Jeff Brown54bc2812010-06-15 01:31:58 -0700205
Jeff Browne839a582010-04-22 18:58:52 -0700206/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown54bc2812010-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 Browne57e8952010-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 Brownb51719b2010-07-29 18:18:33 -0700213 * an exclusive internal lock whenever re-entrance can happen.
Jeff Browne839a582010-04-22 18:58:52 -0700214 */
Jeff Browne57e8952010-07-23 21:28:06 -0700215class InputReader : public InputReaderInterface, private InputReaderContext {
Jeff Browne839a582010-04-22 18:58:52 -0700216public:
217 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700218 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700219 const sp<InputDispatcherInterface>& dispatcher);
220 virtual ~InputReader();
221
222 virtual void loopOnce();
223
Jeff Browne57e8952010-07-23 21:28:06 -0700224 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Browne839a582010-04-22 18:58:52 -0700225
Jeff Browne57e8952010-07-23 21:28:06 -0700226 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
227 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown54bc2812010-06-15 01:31:58 -0700228
Jeff Browne57e8952010-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 Brown54bc2812010-06-15 01:31:58 -0700235
Jeff Browne57e8952010-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 Brown54bc2812010-06-15 01:31:58 -0700238
Jeff Browne839a582010-04-22 18:58:52 -0700239private:
Jeff Browne839a582010-04-22 18:58:52 -0700240 sp<EventHubInterface> mEventHub;
Jeff Brown54bc2812010-06-15 01:31:58 -0700241 sp<InputReaderPolicyInterface> mPolicy;
Jeff Browne839a582010-04-22 18:58:52 -0700242 sp<InputDispatcherInterface> mDispatcher;
243
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700257 KeyedVector<int32_t, InputDevice*> mDevices;
258
Jeff Browne57e8952010-07-23 21:28:06 -0700259 // low-level input event decoding and device management
Jeff Browne839a582010-04-22 18:58:52 -0700260 void process(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700261
Jeff Browne839a582010-04-22 18:58:52 -0700262 void addDevice(nsecs_t when, int32_t deviceId);
Jeff Browne57e8952010-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 Brown54bc2812010-06-15 01:31:58 -0700265 void configureExcludedDevices();
Jeff Browne839a582010-04-22 18:58:52 -0700266
Jeff Browne57e8952010-07-23 21:28:06 -0700267 void consumeEvent(const RawEvent* rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700268
Jeff Browne57e8952010-07-23 21:28:06 -0700269 void handleConfigurationChanged(nsecs_t when);
Jeff Brown54bc2812010-06-15 01:31:58 -0700270
Jeff Browne57e8952010-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 Browne839a582010-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 Browne57e8952010-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 Brownb51719b2010-07-29 18:18:33 -0700417 Mutex mLock;
418
Jeff Browne57e8952010-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 Brownb51719b2010-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 Browne57e8952010-07-23 21:28:06 -0700433
Jeff Brownb51719b2010-07-29 18:18:33 -0700434 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700435
436 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -0700437
Jeff Browne57e8952010-07-23 21:28:06 -0700438 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
439 uint32_t policyFlags);
Jeff Brownb51719b2010-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 Browne57e8952010-07-23 21:28:06 -0700442
Jeff Brownb51719b2010-07-29 18:18:33 -0700443 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Browne57e8952010-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
Jeff Brown8d4dfd22010-08-10 15:47:53 -0700457 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
458
Jeff Browne57e8952010-07-23 21:28:06 -0700459private:
460 // Amount that trackball needs to move in order to generate a key event.
461 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
462
Jeff Brownb51719b2010-07-29 18:18:33 -0700463 Mutex mLock;
464
Jeff Browne57e8952010-07-23 21:28:06 -0700465 int32_t mAssociatedDisplayId;
466
467 struct Accumulator {
468 enum {
469 FIELD_BTN_MOUSE = 1,
470 FIELD_REL_X = 2,
471 FIELD_REL_Y = 4
472 };
473
474 uint32_t fields;
475
476 bool btnMouse;
477 int32_t relX;
478 int32_t relY;
479
480 inline void clear() {
481 fields = 0;
482 }
483
484 inline bool isDirty() {
485 return fields != 0;
486 }
487 } mAccumulator;
488
Jeff Browne57e8952010-07-23 21:28:06 -0700489 float mXScale;
490 float mYScale;
491 float mXPrecision;
492 float mYPrecision;
493
Jeff Brownb51719b2010-07-29 18:18:33 -0700494 struct LockedState {
495 bool down;
496 nsecs_t downTime;
497 } mLocked;
498
499 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700500
501 void sync(nsecs_t when);
Jeff Brownb51719b2010-07-29 18:18:33 -0700502 void applyPolicyAndDispatch(nsecs_t when, int32_t motionEventAction,
503 PointerCoords* pointerCoords, nsecs_t downTime);
Jeff Browne57e8952010-07-23 21:28:06 -0700504};
505
506
507class TouchInputMapper : public InputMapper {
508public:
509 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
510 virtual ~TouchInputMapper();
511
512 virtual uint32_t getSources();
513 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
514 virtual void configure();
515 virtual void reset();
516
517 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
518 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
519 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
520 const int32_t* keyCodes, uint8_t* outFlags);
521
522protected:
523 /* Maximum pointer id value supported.
524 * (This is limited by our use of BitSet32 to track pointer assignments.) */
525 static const uint32_t MAX_POINTER_ID = 31;
526
Jeff Brownb51719b2010-07-29 18:18:33 -0700527 Mutex mLock;
528
Jeff Browne57e8952010-07-23 21:28:06 -0700529 struct VirtualKey {
530 int32_t keyCode;
531 int32_t scanCode;
532 uint32_t flags;
533
534 // computed hit box, specified in touch screen coords based on known display size
535 int32_t hitLeft;
536 int32_t hitTop;
537 int32_t hitRight;
538 int32_t hitBottom;
539
540 inline bool isHit(int32_t x, int32_t y) const {
541 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
542 }
543 };
544
545 struct PointerData {
546 uint32_t id;
547 int32_t x;
548 int32_t y;
549 int32_t pressure;
550 int32_t size;
551 int32_t touchMajor;
552 int32_t touchMinor;
553 int32_t toolMajor;
554 int32_t toolMinor;
555 int32_t orientation;
556 };
557
558 struct TouchData {
559 uint32_t pointerCount;
560 PointerData pointers[MAX_POINTERS];
561 BitSet32 idBits;
562 uint32_t idToIndex[MAX_POINTER_ID + 1];
563
564 void copyFrom(const TouchData& other) {
565 pointerCount = other.pointerCount;
566 idBits = other.idBits;
567
568 for (uint32_t i = 0; i < pointerCount; i++) {
569 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700570
571 int id = pointers[i].id;
572 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700573 }
574 }
575
576 inline void clear() {
577 pointerCount = 0;
578 idBits.clear();
579 }
580 };
581
582 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-07-23 21:28:06 -0700583
584 // Immutable configuration parameters.
585 struct Parameters {
586 bool useBadTouchFilter;
587 bool useJumpyTouchFilter;
588 bool useAveragingTouchFilter;
589 } mParameters;
590
591 // Raw axis information.
592 struct Axes {
593 RawAbsoluteAxisInfo x;
594 RawAbsoluteAxisInfo y;
595 RawAbsoluteAxisInfo pressure;
596 RawAbsoluteAxisInfo size;
597 RawAbsoluteAxisInfo touchMajor;
598 RawAbsoluteAxisInfo touchMinor;
599 RawAbsoluteAxisInfo toolMajor;
600 RawAbsoluteAxisInfo toolMinor;
601 RawAbsoluteAxisInfo orientation;
602 } mAxes;
603
Jeff Brownb51719b2010-07-29 18:18:33 -0700604 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700605 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700606 TouchData mLastTouch;
607
608 // The time the primary pointer last went down.
609 nsecs_t mDownTime;
610
Jeff Brownb51719b2010-07-29 18:18:33 -0700611 struct LockedState {
612 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700613
Jeff Brownb51719b2010-07-29 18:18:33 -0700614 // The surface orientation and width and height set by configureSurfaceLocked().
615 int32_t surfaceOrientation;
616 int32_t surfaceWidth, surfaceHeight;
617
618 // Translation and scaling factors, orientation-independent.
619 int32_t xOrigin;
620 float xScale;
621 float xPrecision;
622
623 int32_t yOrigin;
624 float yScale;
625 float yPrecision;
626
627 int32_t pressureOrigin;
628 float pressureScale;
629
630 int32_t sizeOrigin;
631 float sizeScale;
632
633 float orientationScale;
634
635 // Oriented motion ranges for input device info.
636 struct OrientedRanges {
637 InputDeviceInfo::MotionRange x;
638 InputDeviceInfo::MotionRange y;
639 InputDeviceInfo::MotionRange pressure;
640 InputDeviceInfo::MotionRange size;
641 InputDeviceInfo::MotionRange touchMajor;
642 InputDeviceInfo::MotionRange touchMinor;
643 InputDeviceInfo::MotionRange toolMajor;
644 InputDeviceInfo::MotionRange toolMinor;
645 InputDeviceInfo::MotionRange orientation;
646 } orientedRanges;
647
648 // Oriented dimensions and precision.
649 float orientedSurfaceWidth, orientedSurfaceHeight;
650 float orientedXPrecision, orientedYPrecision;
651
652 struct CurrentVirtualKeyState {
653 bool down;
654 nsecs_t downTime;
655 int32_t keyCode;
656 int32_t scanCode;
657 } currentVirtualKey;
658 } mLocked;
Jeff Browne57e8952010-07-23 21:28:06 -0700659
660 virtual void configureAxes();
Jeff Brownb51719b2010-07-29 18:18:33 -0700661 virtual bool configureSurfaceLocked();
662 virtual void configureVirtualKeysLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700663
664 enum TouchResult {
665 // Dispatch the touch normally.
666 DISPATCH_TOUCH,
667 // Do not dispatch the touch, but keep tracking the current stroke.
668 SKIP_TOUCH,
669 // Do not dispatch the touch, and drop all information associated with the current stoke
670 // so the next movement will appear as a new down.
671 DROP_STROKE
672 };
673
674 void syncTouch(nsecs_t when, bool havePointerIds);
675
676private:
677 /* Maximum number of historical samples to average. */
678 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
679
680 /* Slop distance for jumpy pointer detection.
681 * The vertical range of the screen divided by this is our epsilon value. */
682 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
683
684 /* Number of jumpy points to drop for touchscreens that need it. */
685 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
686 static const uint32_t JUMPY_DROP_LIMIT = 3;
687
688 /* Maximum squared distance for averaging.
689 * If moving farther than this, turn of averaging to avoid lag in response. */
690 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
691
692 struct AveragingTouchFilterState {
693 // Individual history tracks are stored by pointer id
694 uint32_t historyStart[MAX_POINTERS];
695 uint32_t historyEnd[MAX_POINTERS];
696 struct {
697 struct {
698 int32_t x;
699 int32_t y;
700 int32_t pressure;
701 } pointers[MAX_POINTERS];
702 } historyData[AVERAGING_HISTORY_SIZE];
703 } mAveragingTouchFilter;
704
705 struct JumpTouchFilterState {
706 uint32_t jumpyPointsDropped;
707 } mJumpyTouchFilter;
708
709 struct PointerDistanceHeapElement {
710 uint32_t currentPointerIndex : 8;
711 uint32_t lastPointerIndex : 8;
712 uint64_t distance : 48; // squared distance
713 };
714
Jeff Brownb51719b2010-07-29 18:18:33 -0700715 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700716
717 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
718 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
719 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
720 BitSet32 idBits, uint32_t changedId, int32_t motionEventAction);
721
Jeff Brownb51719b2010-07-29 18:18:33 -0700722 void applyPolicyAndDispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
723 int32_t keyEventAction, int32_t keyEventFlags,
724 int32_t keyCode, int32_t scanCode, nsecs_t downTime);
725
726 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
727 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Browne57e8952010-07-23 21:28:06 -0700728
729 bool applyBadTouchFilter();
730 bool applyJumpyTouchFilter();
731 void applyAveragingTouchFilter();
732 void calculatePointerIds();
733};
734
735
736class SingleTouchInputMapper : public TouchInputMapper {
737public:
738 SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
739 virtual ~SingleTouchInputMapper();
740
741 virtual void reset();
742 virtual void process(const RawEvent* rawEvent);
743
744protected:
745 virtual void configureAxes();
746
747private:
748 struct Accumulator {
749 enum {
750 FIELD_BTN_TOUCH = 1,
751 FIELD_ABS_X = 2,
752 FIELD_ABS_Y = 4,
753 FIELD_ABS_PRESSURE = 8,
754 FIELD_ABS_TOOL_WIDTH = 16
755 };
756
757 uint32_t fields;
758
759 bool btnTouch;
760 int32_t absX;
761 int32_t absY;
762 int32_t absPressure;
763 int32_t absToolWidth;
764
765 inline void clear() {
766 fields = 0;
767 }
768
769 inline bool isDirty() {
770 return fields != 0;
771 }
772 } mAccumulator;
773
774 bool mDown;
775 int32_t mX;
776 int32_t mY;
777 int32_t mPressure;
778 int32_t mSize;
779
780 void initialize();
781
782 void sync(nsecs_t when);
783};
784
785
786class MultiTouchInputMapper : public TouchInputMapper {
787public:
788 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
789 virtual ~MultiTouchInputMapper();
790
791 virtual void reset();
792 virtual void process(const RawEvent* rawEvent);
793
794protected:
795 virtual void configureAxes();
796
797private:
798 struct Accumulator {
799 enum {
800 FIELD_ABS_MT_POSITION_X = 1,
801 FIELD_ABS_MT_POSITION_Y = 2,
802 FIELD_ABS_MT_TOUCH_MAJOR = 4,
803 FIELD_ABS_MT_TOUCH_MINOR = 8,
804 FIELD_ABS_MT_WIDTH_MAJOR = 16,
805 FIELD_ABS_MT_WIDTH_MINOR = 32,
806 FIELD_ABS_MT_ORIENTATION = 64,
807 FIELD_ABS_MT_TRACKING_ID = 128
808 };
809
810 uint32_t pointerCount;
811 struct Pointer {
812 uint32_t fields;
813
814 int32_t absMTPositionX;
815 int32_t absMTPositionY;
816 int32_t absMTTouchMajor;
817 int32_t absMTTouchMinor;
818 int32_t absMTWidthMajor;
819 int32_t absMTWidthMinor;
820 int32_t absMTOrientation;
821 int32_t absMTTrackingId;
822
823 inline void clear() {
824 fields = 0;
825 }
826 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
827
828 inline void clear() {
829 pointerCount = 0;
830 pointers[0].clear();
831 }
832
833 inline bool isDirty() {
834 return pointerCount != 0;
835 }
836 } mAccumulator;
837
838 void initialize();
839
840 void sync(nsecs_t when);
841};
842
Jeff Browne839a582010-04-22 18:58:52 -0700843} // namespace android
844
845#endif // _UI_INPUT_READER_H