blob: 56d27651528a084674a0ba3b6bb65c432b5438ad [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 }
Jeff Browne57e8952010-07-23 21:28:06 -0700483 } mAccumulator;
484
Jeff Browne57e8952010-07-23 21:28:06 -0700485 float mXScale;
486 float mYScale;
487 float mXPrecision;
488 float mYPrecision;
489
Jeff Brownb51719b2010-07-29 18:18:33 -0700490 struct LockedState {
491 bool down;
492 nsecs_t downTime;
493 } mLocked;
494
495 void initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700496
497 void sync(nsecs_t when);
Jeff Brownb51719b2010-07-29 18:18:33 -0700498 void applyPolicyAndDispatch(nsecs_t when, int32_t motionEventAction,
499 PointerCoords* pointerCoords, nsecs_t downTime);
Jeff Browne57e8952010-07-23 21:28:06 -0700500};
501
502
503class TouchInputMapper : public InputMapper {
504public:
505 TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
506 virtual ~TouchInputMapper();
507
508 virtual uint32_t getSources();
509 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
510 virtual void configure();
511 virtual void reset();
512
513 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
514 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
515 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
516 const int32_t* keyCodes, uint8_t* outFlags);
517
518protected:
519 /* Maximum pointer id value supported.
520 * (This is limited by our use of BitSet32 to track pointer assignments.) */
521 static const uint32_t MAX_POINTER_ID = 31;
522
Jeff Brownb51719b2010-07-29 18:18:33 -0700523 Mutex mLock;
524
Jeff Browne57e8952010-07-23 21:28:06 -0700525 struct VirtualKey {
526 int32_t keyCode;
527 int32_t scanCode;
528 uint32_t flags;
529
530 // computed hit box, specified in touch screen coords based on known display size
531 int32_t hitLeft;
532 int32_t hitTop;
533 int32_t hitRight;
534 int32_t hitBottom;
535
536 inline bool isHit(int32_t x, int32_t y) const {
537 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
538 }
539 };
540
541 struct PointerData {
542 uint32_t id;
543 int32_t x;
544 int32_t y;
545 int32_t pressure;
546 int32_t size;
547 int32_t touchMajor;
548 int32_t touchMinor;
549 int32_t toolMajor;
550 int32_t toolMinor;
551 int32_t orientation;
552 };
553
554 struct TouchData {
555 uint32_t pointerCount;
556 PointerData pointers[MAX_POINTERS];
557 BitSet32 idBits;
558 uint32_t idToIndex[MAX_POINTER_ID + 1];
559
560 void copyFrom(const TouchData& other) {
561 pointerCount = other.pointerCount;
562 idBits = other.idBits;
563
564 for (uint32_t i = 0; i < pointerCount; i++) {
565 pointers[i] = other.pointers[i];
Jeff Brownb183fbd2010-07-30 19:20:11 -0700566
567 int id = pointers[i].id;
568 idToIndex[id] = other.idToIndex[id];
Jeff Browne57e8952010-07-23 21:28:06 -0700569 }
570 }
571
572 inline void clear() {
573 pointerCount = 0;
574 idBits.clear();
575 }
576 };
577
578 int32_t mAssociatedDisplayId;
Jeff Browne57e8952010-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 Brownb51719b2010-07-29 18:18:33 -0700600 // Current and previous touch sample data.
Jeff Browne57e8952010-07-23 21:28:06 -0700601 TouchData mCurrentTouch;
Jeff Browne57e8952010-07-23 21:28:06 -0700602 TouchData mLastTouch;
603
604 // The time the primary pointer last went down.
605 nsecs_t mDownTime;
606
Jeff Brownb51719b2010-07-29 18:18:33 -0700607 struct LockedState {
608 Vector<VirtualKey> virtualKeys;
Jeff Browne57e8952010-07-23 21:28:06 -0700609
Jeff Brownb51719b2010-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 Browne57e8952010-07-23 21:28:06 -0700655
656 virtual void configureAxes();
Jeff Brownb51719b2010-07-29 18:18:33 -0700657 virtual bool configureSurfaceLocked();
658 virtual void configureVirtualKeysLocked();
Jeff Browne57e8952010-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
Jeff Brownd64c8552010-08-17 20:38:35 -0700701 struct JumpyTouchFilterState {
Jeff Browne57e8952010-07-23 21:28:06 -0700702 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 Brownb51719b2010-07-29 18:18:33 -0700711 void initializeLocked();
Jeff Browne57e8952010-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 Brownb51719b2010-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 Browne57e8952010-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 }
Jeff Browne57e8952010-07-23 21:28:06 -0700764 } mAccumulator;
765
766 bool mDown;
767 int32_t mX;
768 int32_t mY;
769 int32_t mPressure;
770 int32_t mSize;
771
772 void initialize();
773
774 void sync(nsecs_t when);
775};
776
777
778class MultiTouchInputMapper : public TouchInputMapper {
779public:
780 MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
781 virtual ~MultiTouchInputMapper();
782
783 virtual void reset();
784 virtual void process(const RawEvent* rawEvent);
785
786protected:
787 virtual void configureAxes();
788
789private:
790 struct Accumulator {
791 enum {
792 FIELD_ABS_MT_POSITION_X = 1,
793 FIELD_ABS_MT_POSITION_Y = 2,
794 FIELD_ABS_MT_TOUCH_MAJOR = 4,
795 FIELD_ABS_MT_TOUCH_MINOR = 8,
796 FIELD_ABS_MT_WIDTH_MAJOR = 16,
797 FIELD_ABS_MT_WIDTH_MINOR = 32,
798 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brownd64c8552010-08-17 20:38:35 -0700799 FIELD_ABS_MT_TRACKING_ID = 128,
800 FIELD_ABS_MT_PRESSURE = 256,
Jeff Browne57e8952010-07-23 21:28:06 -0700801 };
802
803 uint32_t pointerCount;
804 struct Pointer {
805 uint32_t fields;
806
807 int32_t absMTPositionX;
808 int32_t absMTPositionY;
809 int32_t absMTTouchMajor;
810 int32_t absMTTouchMinor;
811 int32_t absMTWidthMajor;
812 int32_t absMTWidthMinor;
813 int32_t absMTOrientation;
814 int32_t absMTTrackingId;
Jeff Brownd64c8552010-08-17 20:38:35 -0700815 int32_t absMTPressure;
Jeff Browne57e8952010-07-23 21:28:06 -0700816
817 inline void clear() {
818 fields = 0;
819 }
820 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
821
822 inline void clear() {
823 pointerCount = 0;
824 pointers[0].clear();
825 }
Jeff Browne57e8952010-07-23 21:28:06 -0700826 } mAccumulator;
827
828 void initialize();
829
830 void sync(nsecs_t when);
831};
832
Jeff Browne839a582010-04-22 18:58:52 -0700833} // namespace android
834
835#endif // _UI_INPUT_READER_H