blob: 655f0f04a79d921dbad29fe335a0975341329c1b [file] [log] [blame]
Jeff Brown46b9ac0a2010-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
Jeff Brownb4ff35d2011-01-02 16:37:43 -080020#include "EventHub.h"
21#include "InputDispatcher.h"
22#include "PointerController.h"
23
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070024#include <ui/Input.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080025#include <ui/DisplayInfo.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026#include <utils/KeyedVector.h>
27#include <utils/threads.h>
28#include <utils/Timers.h>
29#include <utils/RefBase.h>
30#include <utils/String8.h>
31#include <utils/BitSet.h>
32
33#include <stddef.h>
34#include <unistd.h>
35
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070036namespace android {
37
Jeff Brown6d0fec22010-07-23 21:28:06 -070038class InputDevice;
39class InputMapper;
40
Jeff Brown8d608662010-08-30 03:02:23 -070041
Jeff Brown9c3cda02010-06-15 01:31:58 -070042/*
43 * Input reader policy interface.
44 *
45 * The input reader policy is used by the input reader to interact with the Window Manager
46 * and other system components.
47 *
48 * The actual implementation is partially supported by callbacks into the DVM
49 * via JNI. This interface is also mocked in the unit tests.
50 */
51class InputReaderPolicyInterface : public virtual RefBase {
52protected:
53 InputReaderPolicyInterface() { }
54 virtual ~InputReaderPolicyInterface() { }
55
56public:
57 /* Display orientations. */
58 enum {
59 ROTATION_0 = 0,
60 ROTATION_90 = 1,
61 ROTATION_180 = 2,
62 ROTATION_270 = 3
63 };
64
Jeff Brown9c3cda02010-06-15 01:31:58 -070065 /* Gets information about the display with the specified id.
66 * Returns true if the display info is available, false otherwise.
67 */
68 virtual bool getDisplayInfo(int32_t displayId,
69 int32_t* width, int32_t* height, int32_t* orientation) = 0;
70
Jeff Brown9c3cda02010-06-15 01:31:58 -070071 /* Determines whether to turn on some hacks we have to improve the touch interaction with a
72 * certain device whose screen currently is not all that good.
73 */
74 virtual bool filterTouchEvents() = 0;
75
76 /* Determines whether to turn on some hacks to improve touch interaction with another device
77 * where touch coordinate data can get corrupted.
78 */
79 virtual bool filterJumpyTouchEvents() = 0;
80
Jeff Brownfe508922011-01-18 15:10:10 -080081 /* Gets the amount of time to disable virtual keys after the screen is touched
82 * in order to filter out accidental virtual key presses due to swiping gestures
83 * or taps near the edge of the display. May be 0 to disable the feature.
84 */
85 virtual nsecs_t getVirtualKeyQuietTime() = 0;
86
Jeff Brown9c3cda02010-06-15 01:31:58 -070087 /* Gets the excluded device names for the platform. */
88 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
Jeff Brown83c09682010-12-23 17:50:18 -080089
90 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
91 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -070092};
93
94
95/* Processes raw input events and sends cooked event data to an input dispatcher. */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070096class InputReaderInterface : public virtual RefBase {
97protected:
98 InputReaderInterface() { }
99 virtual ~InputReaderInterface() { }
100
101public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700102 /* Dumps the state of the input reader.
103 *
104 * This method may be called on any thread (usually by the input manager). */
105 virtual void dump(String8& dump) = 0;
106
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700107 /* Runs a single iteration of the processing loop.
108 * Nominally reads and processes one incoming message from the EventHub.
109 *
110 * This method should be called on the input reader thread.
111 */
112 virtual void loopOnce() = 0;
113
Jeff Brown9c3cda02010-06-15 01:31:58 -0700114 /* Gets the current input device configuration.
115 *
116 * This method may be called on any thread (usually by the input manager).
117 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700118 virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700119
Jeff Brown6d0fec22010-07-23 21:28:06 -0700120 /* Gets information about the specified input device.
121 * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
122 * was no such device.
123 *
124 * This method may be called on any thread (usually by the input manager).
Jeff Brown9c3cda02010-06-15 01:31:58 -0700125 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700126 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
127
128 /* Gets the list of all registered device ids. */
129 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
130
131 /* Query current input state. */
132 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
133 int32_t scanCode) = 0;
134 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
135 int32_t keyCode) = 0;
136 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
137 int32_t sw) = 0;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700138
139 /* Determine whether physical keys exist for the given framework-domain key codes. */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700140 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
141 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
142};
143
144
145/* Internal interface used by individual input devices to access global input device state
146 * and parameters maintained by the input reader.
147 */
148class InputReaderContext {
Jeff Brownc3db8582010-10-20 15:33:38 -0700149public:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150 InputReaderContext() { }
151 virtual ~InputReaderContext() { }
152
Jeff Brown6d0fec22010-07-23 21:28:06 -0700153 virtual void updateGlobalMetaState() = 0;
154 virtual int32_t getGlobalMetaState() = 0;
155
Jeff Brownfe508922011-01-18 15:10:10 -0800156 virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
157 virtual bool shouldDropVirtualKey(nsecs_t now,
158 InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
159
Jeff Brown05dc66a2011-03-02 14:41:58 -0800160 virtual void fadePointer() = 0;
161
Jeff Brown6d0fec22010-07-23 21:28:06 -0700162 virtual InputReaderPolicyInterface* getPolicy() = 0;
163 virtual InputDispatcherInterface* getDispatcher() = 0;
164 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700165};
166
Jeff Brown9c3cda02010-06-15 01:31:58 -0700167
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700168/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700169 * that it sends to the input dispatcher. Some functions of the input reader, such as early
170 * event filtering in low power states, are controlled by a separate policy object.
171 *
172 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700173 * Because the policy and dispatcher can potentially block or cause re-entrance into
174 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700175 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700176 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700177class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700178public:
179 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700180 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700181 const sp<InputDispatcherInterface>& dispatcher);
182 virtual ~InputReader();
183
Jeff Brownb88102f2010-09-08 11:49:43 -0700184 virtual void dump(String8& dump);
185
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700186 virtual void loopOnce();
187
Jeff Brown6d0fec22010-07-23 21:28:06 -0700188 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700189
Jeff Brown6d0fec22010-07-23 21:28:06 -0700190 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
191 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700192
Jeff Brown6d0fec22010-07-23 21:28:06 -0700193 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
194 int32_t scanCode);
195 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
196 int32_t keyCode);
197 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
198 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700199
Jeff Brown6d0fec22010-07-23 21:28:06 -0700200 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
201 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700202
Jeff Brownc3db8582010-10-20 15:33:38 -0700203protected:
204 // These methods are protected virtual so they can be overridden and instrumented
205 // by test cases.
206 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
207
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700208private:
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700209 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700210 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700211 sp<InputDispatcherInterface> mDispatcher;
212
Jeff Brown6d0fec22010-07-23 21:28:06 -0700213 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
214 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
215 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
216
217 // This reader/writer lock guards the list of input devices.
218 // The writer lock must be held whenever the list of input devices is modified
219 // and then promptly released.
220 // The reader lock must be held whenever the list of input devices is traversed or an
221 // input device in the list is accessed.
222 // This lock only protects the registry and prevents inadvertent deletion of device objects
223 // that are in use. Individual devices are responsible for guarding their own internal state
224 // as needed for concurrent operation.
225 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700226 KeyedVector<int32_t, InputDevice*> mDevices;
227
Jeff Brown6d0fec22010-07-23 21:28:06 -0700228 // low-level input event decoding and device management
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700229 void process(const RawEvent* rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700230
Jeff Brown7342bb92010-10-01 18:55:43 -0700231 void addDevice(int32_t deviceId);
232 void removeDevice(int32_t deviceId);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700233 void configureExcludedDevices();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234
Jeff Brown6d0fec22010-07-23 21:28:06 -0700235 void consumeEvent(const RawEvent* rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700236
Jeff Brownc3db8582010-10-20 15:33:38 -0700237 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700238
Jeff Brown6d0fec22010-07-23 21:28:06 -0700239 // state management for all devices
240 Mutex mStateLock;
241
242 int32_t mGlobalMetaState;
243 virtual void updateGlobalMetaState();
244 virtual int32_t getGlobalMetaState();
245
Jeff Brown05dc66a2011-03-02 14:41:58 -0800246 virtual void fadePointer();
247
Jeff Brown6d0fec22010-07-23 21:28:06 -0700248 InputConfiguration mInputConfiguration;
249 void updateInputConfiguration();
250
Jeff Brownfe508922011-01-18 15:10:10 -0800251 nsecs_t mDisableVirtualKeysTimeout;
252 virtual void disableVirtualKeysUntil(nsecs_t time);
253 virtual bool shouldDropVirtualKey(nsecs_t now,
254 InputDevice* device, int32_t keyCode, int32_t scanCode);
255
Jeff Brown6d0fec22010-07-23 21:28:06 -0700256 // state queries
257 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
258 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
259 GetStateFunc getStateFunc);
260 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
261 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700262};
263
264
265/* Reads raw events from the event hub and processes them, endlessly. */
266class InputReaderThread : public Thread {
267public:
268 InputReaderThread(const sp<InputReaderInterface>& reader);
269 virtual ~InputReaderThread();
270
271private:
272 sp<InputReaderInterface> mReader;
273
274 virtual bool threadLoop();
275};
276
Jeff Brown6d0fec22010-07-23 21:28:06 -0700277
278/* Represents the state of a single input device. */
279class InputDevice {
280public:
281 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
282 ~InputDevice();
283
284 inline InputReaderContext* getContext() { return mContext; }
285 inline int32_t getId() { return mId; }
286 inline const String8& getName() { return mName; }
287 inline uint32_t getSources() { return mSources; }
288
Jeff Brown56194eb2011-03-02 19:23:13 -0800289 inline bool isExternal() { return mIsExternal; }
290 inline void setExternal(bool external) { mIsExternal = external; }
291
Jeff Brown6d0fec22010-07-23 21:28:06 -0700292 inline bool isIgnored() { return mMappers.isEmpty(); }
293
Jeff Brownef3d7e82010-09-30 14:33:04 -0700294 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700295 void addMapper(InputMapper* mapper);
296 void configure();
297 void reset();
298 void process(const RawEvent* rawEvent);
299
300 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
301 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
302 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
303 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
304 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
305 const int32_t* keyCodes, uint8_t* outFlags);
306
307 int32_t getMetaState();
308
Jeff Brown05dc66a2011-03-02 14:41:58 -0800309 void fadePointer();
310
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800311 inline const PropertyMap& getConfiguration() {
312 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700313 }
314
Jeff Brown6d0fec22010-07-23 21:28:06 -0700315private:
316 InputReaderContext* mContext;
317 int32_t mId;
318
319 Vector<InputMapper*> mMappers;
320
321 String8 mName;
322 uint32_t mSources;
Jeff Brown56194eb2011-03-02 19:23:13 -0800323 bool mIsExternal;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700324
325 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
326 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700327
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800328 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700329};
330
331
332/* An input mapper transforms raw input events into cooked event data.
333 * A single input device can have multiple associated input mappers in order to interpret
334 * different classes of events.
335 */
336class InputMapper {
337public:
338 InputMapper(InputDevice* device);
339 virtual ~InputMapper();
340
341 inline InputDevice* getDevice() { return mDevice; }
342 inline int32_t getDeviceId() { return mDevice->getId(); }
343 inline const String8 getDeviceName() { return mDevice->getName(); }
344 inline InputReaderContext* getContext() { return mContext; }
345 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
346 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
347 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
348
349 virtual uint32_t getSources() = 0;
350 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700351 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700352 virtual void configure();
353 virtual void reset();
354 virtual void process(const RawEvent* rawEvent) = 0;
355
356 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
357 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
358 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
359 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
360 const int32_t* keyCodes, uint8_t* outFlags);
361
362 virtual int32_t getMetaState();
363
Jeff Brown05dc66a2011-03-02 14:41:58 -0800364 virtual void fadePointer();
365
Jeff Brown6d0fec22010-07-23 21:28:06 -0700366protected:
367 InputDevice* mDevice;
368 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800369
370 static void dumpRawAbsoluteAxisInfo(String8& dump,
371 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700372};
373
374
375class SwitchInputMapper : public InputMapper {
376public:
377 SwitchInputMapper(InputDevice* device);
378 virtual ~SwitchInputMapper();
379
380 virtual uint32_t getSources();
381 virtual void process(const RawEvent* rawEvent);
382
383 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
384
385private:
386 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
387};
388
389
390class KeyboardInputMapper : public InputMapper {
391public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800392 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700393 virtual ~KeyboardInputMapper();
394
395 virtual uint32_t getSources();
396 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700397 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800398 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700399 virtual void reset();
400 virtual void process(const RawEvent* rawEvent);
401
402 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
403 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
404 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
405 const int32_t* keyCodes, uint8_t* outFlags);
406
407 virtual int32_t getMetaState();
408
409private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700410 Mutex mLock;
411
Jeff Brown6d0fec22010-07-23 21:28:06 -0700412 struct KeyDown {
413 int32_t keyCode;
414 int32_t scanCode;
415 };
416
Jeff Brown6d0fec22010-07-23 21:28:06 -0700417 uint32_t mSources;
418 int32_t mKeyboardType;
419
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800420 // Immutable configuration parameters.
421 struct Parameters {
422 int32_t associatedDisplayId;
423 bool orientationAware;
424 } mParameters;
425
Jeff Brown6328cdc2010-07-29 18:18:33 -0700426 struct LockedState {
427 Vector<KeyDown> keyDowns; // keys that are down
428 int32_t metaState;
429 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700430
431 struct LedState {
432 bool avail; // led is available
433 bool on; // we think the led is currently on
434 };
435 LedState capsLockLedState;
436 LedState numLockLedState;
437 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700438 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439
Jeff Brown6328cdc2010-07-29 18:18:33 -0700440 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700441
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800442 void configureParameters();
443 void dumpParameters(String8& dump);
444
Jeff Brown6d0fec22010-07-23 21:28:06 -0700445 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700446
Jeff Brown6d0fec22010-07-23 21:28:06 -0700447 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
448 uint32_t policyFlags);
449
Jeff Brown6328cdc2010-07-29 18:18:33 -0700450 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700451
Jeff Brown49ed71d2010-12-06 17:13:33 -0800452 void resetLedStateLocked();
453 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700454 void updateLedStateLocked(bool reset);
455 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
456 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700457};
458
459
Jeff Brown83c09682010-12-23 17:50:18 -0800460class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700461public:
Jeff Brown83c09682010-12-23 17:50:18 -0800462 CursorInputMapper(InputDevice* device);
463 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700464
465 virtual uint32_t getSources();
466 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700467 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800468 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700469 virtual void reset();
470 virtual void process(const RawEvent* rawEvent);
471
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700472 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
473
Jeff Brown05dc66a2011-03-02 14:41:58 -0800474 virtual void fadePointer();
475
Jeff Brown6d0fec22010-07-23 21:28:06 -0700476private:
477 // Amount that trackball needs to move in order to generate a key event.
478 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
479
Jeff Brown6328cdc2010-07-29 18:18:33 -0700480 Mutex mLock;
481
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800482 // Immutable configuration parameters.
483 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800484 enum Mode {
485 MODE_POINTER,
486 MODE_NAVIGATION,
487 };
488
489 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800490 int32_t associatedDisplayId;
491 bool orientationAware;
492 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700493
494 struct Accumulator {
495 enum {
496 FIELD_BTN_MOUSE = 1,
497 FIELD_REL_X = 2,
Jeff Brown6f2fba42011-02-19 01:08:02 -0800498 FIELD_REL_Y = 4,
499 FIELD_REL_WHEEL = 8,
500 FIELD_REL_HWHEEL = 16,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700501 };
502
503 uint32_t fields;
504
505 bool btnMouse;
506 int32_t relX;
507 int32_t relY;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800508 int32_t relWheel;
509 int32_t relHWheel;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700510
511 inline void clear() {
512 fields = 0;
513 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700514 } mAccumulator;
515
Jeff Brown83c09682010-12-23 17:50:18 -0800516 int32_t mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700517 float mXScale;
518 float mYScale;
519 float mXPrecision;
520 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800521
522 bool mHaveVWheel;
523 bool mHaveHWheel;
524 float mVWheelScale;
525 float mHWheelScale;
526
Jeff Brown83c09682010-12-23 17:50:18 -0800527 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700528
Jeff Brown6328cdc2010-07-29 18:18:33 -0700529 struct LockedState {
530 bool down;
531 nsecs_t downTime;
532 } mLocked;
533
534 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700535
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800536 void configureParameters();
537 void dumpParameters(String8& dump);
538
Jeff Brown6d0fec22010-07-23 21:28:06 -0700539 void sync(nsecs_t when);
540};
541
542
543class TouchInputMapper : public InputMapper {
544public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800545 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700546 virtual ~TouchInputMapper();
547
548 virtual uint32_t getSources();
549 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700550 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700551 virtual void configure();
552 virtual void reset();
553
554 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
555 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
556 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
557 const int32_t* keyCodes, uint8_t* outFlags);
558
559protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700560 Mutex mLock;
561
Jeff Brown6d0fec22010-07-23 21:28:06 -0700562 struct VirtualKey {
563 int32_t keyCode;
564 int32_t scanCode;
565 uint32_t flags;
566
567 // computed hit box, specified in touch screen coords based on known display size
568 int32_t hitLeft;
569 int32_t hitTop;
570 int32_t hitRight;
571 int32_t hitBottom;
572
573 inline bool isHit(int32_t x, int32_t y) const {
574 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
575 }
576 };
577
Jeff Brown8d608662010-08-30 03:02:23 -0700578 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700579 struct PointerData {
580 uint32_t id;
581 int32_t x;
582 int32_t y;
583 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700584 int32_t touchMajor;
585 int32_t touchMinor;
586 int32_t toolMajor;
587 int32_t toolMinor;
588 int32_t orientation;
Jeff Brownc3db8582010-10-20 15:33:38 -0700589
590 inline bool operator== (const PointerData& other) const {
591 return id == other.id
592 && x == other.x
593 && y == other.y
594 && pressure == other.pressure
595 && touchMajor == other.touchMajor
596 && touchMinor == other.touchMinor
597 && toolMajor == other.toolMajor
598 && toolMinor == other.toolMinor
599 && orientation == other.orientation;
600 }
601 inline bool operator!= (const PointerData& other) const {
602 return !(*this == other);
603 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700604 };
605
Jeff Brown8d608662010-08-30 03:02:23 -0700606 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700607 struct TouchData {
608 uint32_t pointerCount;
609 PointerData pointers[MAX_POINTERS];
610 BitSet32 idBits;
611 uint32_t idToIndex[MAX_POINTER_ID + 1];
612
613 void copyFrom(const TouchData& other) {
614 pointerCount = other.pointerCount;
615 idBits = other.idBits;
616
617 for (uint32_t i = 0; i < pointerCount; i++) {
618 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700619
620 int id = pointers[i].id;
621 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700622 }
623 }
624
625 inline void clear() {
626 pointerCount = 0;
627 idBits.clear();
628 }
629 };
630
Jeff Brown83c09682010-12-23 17:50:18 -0800631 // Input sources supported by the device.
632 int32_t mSources;
633
Jeff Brown6d0fec22010-07-23 21:28:06 -0700634 // Immutable configuration parameters.
635 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800636 enum DeviceType {
637 DEVICE_TYPE_TOUCH_SCREEN,
638 DEVICE_TYPE_TOUCH_PAD,
639 };
640
641 DeviceType deviceType;
642 int32_t associatedDisplayId;
643 bool orientationAware;
644
Jeff Brown6d0fec22010-07-23 21:28:06 -0700645 bool useBadTouchFilter;
646 bool useJumpyTouchFilter;
647 bool useAveragingTouchFilter;
Jeff Brownfe508922011-01-18 15:10:10 -0800648 nsecs_t virtualKeyQuietTime;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700649 } mParameters;
650
Jeff Brown8d608662010-08-30 03:02:23 -0700651 // Immutable calibration parameters in parsed form.
652 struct Calibration {
Jeff Brown511ee5f2010-10-18 13:32:20 -0700653 // Position
654 bool haveXOrigin;
655 int32_t xOrigin;
656 bool haveYOrigin;
657 int32_t yOrigin;
658 bool haveXScale;
659 float xScale;
660 bool haveYScale;
661 float yScale;
662
Jeff Brownc6d282b2010-10-14 21:42:15 -0700663 // Touch Size
664 enum TouchSizeCalibration {
665 TOUCH_SIZE_CALIBRATION_DEFAULT,
666 TOUCH_SIZE_CALIBRATION_NONE,
667 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
668 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700669 };
670
Jeff Brownc6d282b2010-10-14 21:42:15 -0700671 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700672
Jeff Brownc6d282b2010-10-14 21:42:15 -0700673 // Tool Size
674 enum ToolSizeCalibration {
675 TOOL_SIZE_CALIBRATION_DEFAULT,
676 TOOL_SIZE_CALIBRATION_NONE,
677 TOOL_SIZE_CALIBRATION_GEOMETRIC,
678 TOOL_SIZE_CALIBRATION_LINEAR,
679 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700680 };
681
Jeff Brownc6d282b2010-10-14 21:42:15 -0700682 ToolSizeCalibration toolSizeCalibration;
683 bool haveToolSizeLinearScale;
684 float toolSizeLinearScale;
685 bool haveToolSizeLinearBias;
686 float toolSizeLinearBias;
687 bool haveToolSizeAreaScale;
688 float toolSizeAreaScale;
689 bool haveToolSizeAreaBias;
690 float toolSizeAreaBias;
691 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800692 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700693
694 // Pressure
695 enum PressureCalibration {
696 PRESSURE_CALIBRATION_DEFAULT,
697 PRESSURE_CALIBRATION_NONE,
698 PRESSURE_CALIBRATION_PHYSICAL,
699 PRESSURE_CALIBRATION_AMPLITUDE,
700 };
701 enum PressureSource {
702 PRESSURE_SOURCE_DEFAULT,
703 PRESSURE_SOURCE_PRESSURE,
704 PRESSURE_SOURCE_TOUCH,
705 };
706
707 PressureCalibration pressureCalibration;
708 PressureSource pressureSource;
709 bool havePressureScale;
710 float pressureScale;
711
712 // Size
713 enum SizeCalibration {
714 SIZE_CALIBRATION_DEFAULT,
715 SIZE_CALIBRATION_NONE,
716 SIZE_CALIBRATION_NORMALIZED,
717 };
718
719 SizeCalibration sizeCalibration;
720
721 // Orientation
722 enum OrientationCalibration {
723 ORIENTATION_CALIBRATION_DEFAULT,
724 ORIENTATION_CALIBRATION_NONE,
725 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -0800726 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -0700727 };
728
729 OrientationCalibration orientationCalibration;
730 } mCalibration;
731
732 // Raw axis information from the driver.
733 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700734 RawAbsoluteAxisInfo x;
735 RawAbsoluteAxisInfo y;
736 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700737 RawAbsoluteAxisInfo touchMajor;
738 RawAbsoluteAxisInfo touchMinor;
739 RawAbsoluteAxisInfo toolMajor;
740 RawAbsoluteAxisInfo toolMinor;
741 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700742 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700743
Jeff Brown6328cdc2010-07-29 18:18:33 -0700744 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700745 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700746 TouchData mLastTouch;
747
748 // The time the primary pointer last went down.
749 nsecs_t mDownTime;
750
Jeff Brown6328cdc2010-07-29 18:18:33 -0700751 struct LockedState {
752 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700753
Jeff Brown6328cdc2010-07-29 18:18:33 -0700754 // The surface orientation and width and height set by configureSurfaceLocked().
755 int32_t surfaceOrientation;
756 int32_t surfaceWidth, surfaceHeight;
757
758 // Translation and scaling factors, orientation-independent.
759 int32_t xOrigin;
760 float xScale;
761 float xPrecision;
762
763 int32_t yOrigin;
764 float yScale;
765 float yPrecision;
766
Jeff Brown8d608662010-08-30 03:02:23 -0700767 float geometricScale;
768
Jeff Brownc6d282b2010-10-14 21:42:15 -0700769 float toolSizeLinearScale;
770 float toolSizeLinearBias;
771 float toolSizeAreaScale;
772 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700773
Jeff Brown6328cdc2010-07-29 18:18:33 -0700774 float pressureScale;
775
Jeff Brown6328cdc2010-07-29 18:18:33 -0700776 float sizeScale;
777
778 float orientationScale;
779
780 // Oriented motion ranges for input device info.
781 struct OrientedRanges {
782 InputDeviceInfo::MotionRange x;
783 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700784
785 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700786 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700787
788 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700789 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700790
Jeff Brownc6d282b2010-10-14 21:42:15 -0700791 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700792 InputDeviceInfo::MotionRange touchMajor;
793 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700794
Jeff Brownc6d282b2010-10-14 21:42:15 -0700795 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700796 InputDeviceInfo::MotionRange toolMajor;
797 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700798
799 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700800 InputDeviceInfo::MotionRange orientation;
801 } orientedRanges;
802
803 // Oriented dimensions and precision.
804 float orientedSurfaceWidth, orientedSurfaceHeight;
805 float orientedXPrecision, orientedYPrecision;
806
807 struct CurrentVirtualKeyState {
808 bool down;
809 nsecs_t downTime;
810 int32_t keyCode;
811 int32_t scanCode;
812 } currentVirtualKey;
813 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700814
Jeff Brown8d608662010-08-30 03:02:23 -0700815 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700816 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700817 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700818 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700819 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700820 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700821 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700822 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700823 virtual void parseCalibration();
824 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700825 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700826
827 enum TouchResult {
828 // Dispatch the touch normally.
829 DISPATCH_TOUCH,
830 // Do not dispatch the touch, but keep tracking the current stroke.
831 SKIP_TOUCH,
832 // Do not dispatch the touch, and drop all information associated with the current stoke
833 // so the next movement will appear as a new down.
834 DROP_STROKE
835 };
836
837 void syncTouch(nsecs_t when, bool havePointerIds);
838
839private:
840 /* Maximum number of historical samples to average. */
841 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
842
843 /* Slop distance for jumpy pointer detection.
844 * The vertical range of the screen divided by this is our epsilon value. */
845 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
846
847 /* Number of jumpy points to drop for touchscreens that need it. */
848 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
849 static const uint32_t JUMPY_DROP_LIMIT = 3;
850
851 /* Maximum squared distance for averaging.
852 * If moving farther than this, turn of averaging to avoid lag in response. */
853 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
854
855 struct AveragingTouchFilterState {
856 // Individual history tracks are stored by pointer id
857 uint32_t historyStart[MAX_POINTERS];
858 uint32_t historyEnd[MAX_POINTERS];
859 struct {
860 struct {
861 int32_t x;
862 int32_t y;
863 int32_t pressure;
864 } pointers[MAX_POINTERS];
865 } historyData[AVERAGING_HISTORY_SIZE];
866 } mAveragingTouchFilter;
867
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700868 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700869 uint32_t jumpyPointsDropped;
870 } mJumpyTouchFilter;
871
872 struct PointerDistanceHeapElement {
873 uint32_t currentPointerIndex : 8;
874 uint32_t lastPointerIndex : 8;
875 uint64_t distance : 48; // squared distance
876 };
877
Jeff Brown6328cdc2010-07-29 18:18:33 -0700878 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700879
880 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
881 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
882 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700883 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
884 int32_t motionEventAction);
Jeff Brownfe508922011-01-18 15:10:10 -0800885 void detectGestures(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700886
Jeff Brown6328cdc2010-07-29 18:18:33 -0700887 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
888 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700889
890 bool applyBadTouchFilter();
891 bool applyJumpyTouchFilter();
892 void applyAveragingTouchFilter();
893 void calculatePointerIds();
894};
895
896
897class SingleTouchInputMapper : public TouchInputMapper {
898public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800899 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700900 virtual ~SingleTouchInputMapper();
901
902 virtual void reset();
903 virtual void process(const RawEvent* rawEvent);
904
905protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700906 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700907
908private:
909 struct Accumulator {
910 enum {
911 FIELD_BTN_TOUCH = 1,
912 FIELD_ABS_X = 2,
913 FIELD_ABS_Y = 4,
914 FIELD_ABS_PRESSURE = 8,
915 FIELD_ABS_TOOL_WIDTH = 16
916 };
917
918 uint32_t fields;
919
920 bool btnTouch;
921 int32_t absX;
922 int32_t absY;
923 int32_t absPressure;
924 int32_t absToolWidth;
925
926 inline void clear() {
927 fields = 0;
928 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700929 } mAccumulator;
930
931 bool mDown;
932 int32_t mX;
933 int32_t mY;
934 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700935 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700936
937 void initialize();
938
939 void sync(nsecs_t when);
940};
941
942
943class MultiTouchInputMapper : public TouchInputMapper {
944public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800945 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700946 virtual ~MultiTouchInputMapper();
947
948 virtual void reset();
949 virtual void process(const RawEvent* rawEvent);
950
951protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700952 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700953
954private:
955 struct Accumulator {
956 enum {
957 FIELD_ABS_MT_POSITION_X = 1,
958 FIELD_ABS_MT_POSITION_Y = 2,
959 FIELD_ABS_MT_TOUCH_MAJOR = 4,
960 FIELD_ABS_MT_TOUCH_MINOR = 8,
961 FIELD_ABS_MT_WIDTH_MAJOR = 16,
962 FIELD_ABS_MT_WIDTH_MINOR = 32,
963 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700964 FIELD_ABS_MT_TRACKING_ID = 128,
965 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700966 };
967
968 uint32_t pointerCount;
969 struct Pointer {
970 uint32_t fields;
971
972 int32_t absMTPositionX;
973 int32_t absMTPositionY;
974 int32_t absMTTouchMajor;
975 int32_t absMTTouchMinor;
976 int32_t absMTWidthMajor;
977 int32_t absMTWidthMinor;
978 int32_t absMTOrientation;
979 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700980 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700981
982 inline void clear() {
983 fields = 0;
984 }
985 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
986
987 inline void clear() {
988 pointerCount = 0;
989 pointers[0].clear();
990 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700991 } mAccumulator;
992
993 void initialize();
994
995 void sync(nsecs_t when);
996};
997
Jeff Browncb1404e2011-01-15 18:14:15 -0800998
999class JoystickInputMapper : public InputMapper {
1000public:
1001 JoystickInputMapper(InputDevice* device);
1002 virtual ~JoystickInputMapper();
1003
1004 virtual uint32_t getSources();
1005 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1006 virtual void dump(String8& dump);
1007 virtual void configure();
1008 virtual void reset();
1009 virtual void process(const RawEvent* rawEvent);
1010
1011private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001012 struct Axis {
1013 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001014
Jeff Brown6f2fba42011-02-19 01:08:02 -08001015 int32_t axis; // axis id
1016 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001017
Jeff Brown6f2fba42011-02-19 01:08:02 -08001018 float scale; // scale factor from raw to normalized values
1019 float offset; // offset to add after scaling for normalization
Jeff Browncb1404e2011-01-15 18:14:15 -08001020
Jeff Brown6f2fba42011-02-19 01:08:02 -08001021 float min; // normalized inclusive minimum
1022 float max; // normalized inclusive maximum
1023 float flat; // normalized flat region size
1024 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001025
Jeff Brown6f2fba42011-02-19 01:08:02 -08001026 float oldValue; // previous value
1027 float newValue; // most recent value
Jeff Browncb1404e2011-01-15 18:14:15 -08001028
Jeff Brown6f2fba42011-02-19 01:08:02 -08001029 float filter; // filter out small variations of this size
Jeff Browncb1404e2011-01-15 18:14:15 -08001030
Jeff Brown6f2fba42011-02-19 01:08:02 -08001031 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo,
1032 int32_t axis, bool explicitlyMapped, float scale, float offset,
1033 float min, float max, float flat, float fuzz) {
1034 this->rawAxisInfo = rawAxisInfo;
1035 this->axis = axis;
1036 this->explicitlyMapped = explicitlyMapped;
1037 this->scale = scale;
1038 this->offset = offset;
1039 this->min = min;
1040 this->max = max;
1041 this->flat = flat;
1042 this->fuzz = fuzz;
1043 this->filter = 0;
1044 this->oldValue = 0;
1045 this->newValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001046 }
1047 };
1048
Jeff Brown6f2fba42011-02-19 01:08:02 -08001049 // Axes indexed by raw ABS_* axis index.
1050 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001051
Jeff Brown6f2fba42011-02-19 01:08:02 -08001052 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001053
Jeff Brown6f2fba42011-02-19 01:08:02 -08001054 bool haveAxis(int32_t axis);
1055 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1056 bool haveAxesChangedSignificantly();
Jeff Browncb1404e2011-01-15 18:14:15 -08001057
Jeff Brown6f2fba42011-02-19 01:08:02 -08001058 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001059};
1060
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001061} // namespace android
1062
1063#endif // _UI_INPUT_READER_H