blob: b344ffe1005f46b63b5804d6d5111d3ab592bb2e [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
289 inline bool isIgnored() { return mMappers.isEmpty(); }
290
Jeff Brownef3d7e82010-09-30 14:33:04 -0700291 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700292 void addMapper(InputMapper* mapper);
293 void configure();
294 void reset();
295 void process(const RawEvent* rawEvent);
296
297 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
298 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
299 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
300 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
301 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
302 const int32_t* keyCodes, uint8_t* outFlags);
303
304 int32_t getMetaState();
305
Jeff Brown05dc66a2011-03-02 14:41:58 -0800306 void fadePointer();
307
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800308 inline const PropertyMap& getConfiguration() {
309 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700310 }
311
Jeff Brown6d0fec22010-07-23 21:28:06 -0700312private:
313 InputReaderContext* mContext;
314 int32_t mId;
315
316 Vector<InputMapper*> mMappers;
317
318 String8 mName;
319 uint32_t mSources;
320
321 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
322 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700323
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800324 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700325};
326
327
328/* An input mapper transforms raw input events into cooked event data.
329 * A single input device can have multiple associated input mappers in order to interpret
330 * different classes of events.
331 */
332class InputMapper {
333public:
334 InputMapper(InputDevice* device);
335 virtual ~InputMapper();
336
337 inline InputDevice* getDevice() { return mDevice; }
338 inline int32_t getDeviceId() { return mDevice->getId(); }
339 inline const String8 getDeviceName() { return mDevice->getName(); }
340 inline InputReaderContext* getContext() { return mContext; }
341 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
342 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
343 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
344
345 virtual uint32_t getSources() = 0;
346 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700347 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700348 virtual void configure();
349 virtual void reset();
350 virtual void process(const RawEvent* rawEvent) = 0;
351
352 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
353 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
354 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
355 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
356 const int32_t* keyCodes, uint8_t* outFlags);
357
358 virtual int32_t getMetaState();
359
Jeff Brown05dc66a2011-03-02 14:41:58 -0800360 virtual void fadePointer();
361
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362protected:
363 InputDevice* mDevice;
364 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800365
366 static void dumpRawAbsoluteAxisInfo(String8& dump,
367 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700368};
369
370
371class SwitchInputMapper : public InputMapper {
372public:
373 SwitchInputMapper(InputDevice* device);
374 virtual ~SwitchInputMapper();
375
376 virtual uint32_t getSources();
377 virtual void process(const RawEvent* rawEvent);
378
379 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
380
381private:
382 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
383};
384
385
386class KeyboardInputMapper : public InputMapper {
387public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800388 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700389 virtual ~KeyboardInputMapper();
390
391 virtual uint32_t getSources();
392 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700393 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800394 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 virtual void reset();
396 virtual void process(const RawEvent* rawEvent);
397
398 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
399 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
400 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
401 const int32_t* keyCodes, uint8_t* outFlags);
402
403 virtual int32_t getMetaState();
404
405private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700406 Mutex mLock;
407
Jeff Brown6d0fec22010-07-23 21:28:06 -0700408 struct KeyDown {
409 int32_t keyCode;
410 int32_t scanCode;
411 };
412
Jeff Brown6d0fec22010-07-23 21:28:06 -0700413 uint32_t mSources;
414 int32_t mKeyboardType;
415
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800416 // Immutable configuration parameters.
417 struct Parameters {
418 int32_t associatedDisplayId;
419 bool orientationAware;
420 } mParameters;
421
Jeff Brown6328cdc2010-07-29 18:18:33 -0700422 struct LockedState {
423 Vector<KeyDown> keyDowns; // keys that are down
424 int32_t metaState;
425 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700426
427 struct LedState {
428 bool avail; // led is available
429 bool on; // we think the led is currently on
430 };
431 LedState capsLockLedState;
432 LedState numLockLedState;
433 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700434 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435
Jeff Brown6328cdc2010-07-29 18:18:33 -0700436 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700437
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800438 void configureParameters();
439 void dumpParameters(String8& dump);
440
Jeff Brown6d0fec22010-07-23 21:28:06 -0700441 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700442
Jeff Brown6d0fec22010-07-23 21:28:06 -0700443 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
444 uint32_t policyFlags);
445
Jeff Brown6328cdc2010-07-29 18:18:33 -0700446 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700447
Jeff Brown49ed71d2010-12-06 17:13:33 -0800448 void resetLedStateLocked();
449 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700450 void updateLedStateLocked(bool reset);
451 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
452 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700453};
454
455
Jeff Brown83c09682010-12-23 17:50:18 -0800456class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700457public:
Jeff Brown83c09682010-12-23 17:50:18 -0800458 CursorInputMapper(InputDevice* device);
459 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700460
461 virtual uint32_t getSources();
462 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700463 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800464 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465 virtual void reset();
466 virtual void process(const RawEvent* rawEvent);
467
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700468 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
469
Jeff Brown05dc66a2011-03-02 14:41:58 -0800470 virtual void fadePointer();
471
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472private:
473 // Amount that trackball needs to move in order to generate a key event.
474 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
475
Jeff Brown6328cdc2010-07-29 18:18:33 -0700476 Mutex mLock;
477
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800478 // Immutable configuration parameters.
479 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800480 enum Mode {
481 MODE_POINTER,
482 MODE_NAVIGATION,
483 };
484
485 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800486 int32_t associatedDisplayId;
487 bool orientationAware;
488 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700489
490 struct Accumulator {
491 enum {
492 FIELD_BTN_MOUSE = 1,
493 FIELD_REL_X = 2,
Jeff Brown6f2fba42011-02-19 01:08:02 -0800494 FIELD_REL_Y = 4,
495 FIELD_REL_WHEEL = 8,
496 FIELD_REL_HWHEEL = 16,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700497 };
498
499 uint32_t fields;
500
501 bool btnMouse;
502 int32_t relX;
503 int32_t relY;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800504 int32_t relWheel;
505 int32_t relHWheel;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700506
507 inline void clear() {
508 fields = 0;
509 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700510 } mAccumulator;
511
Jeff Brown83c09682010-12-23 17:50:18 -0800512 int32_t mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700513 float mXScale;
514 float mYScale;
515 float mXPrecision;
516 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800517
518 bool mHaveVWheel;
519 bool mHaveHWheel;
520 float mVWheelScale;
521 float mHWheelScale;
522
Jeff Brown83c09682010-12-23 17:50:18 -0800523 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700524
Jeff Brown6328cdc2010-07-29 18:18:33 -0700525 struct LockedState {
526 bool down;
527 nsecs_t downTime;
528 } mLocked;
529
530 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700531
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800532 void configureParameters();
533 void dumpParameters(String8& dump);
534
Jeff Brown6d0fec22010-07-23 21:28:06 -0700535 void sync(nsecs_t when);
536};
537
538
539class TouchInputMapper : public InputMapper {
540public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800541 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700542 virtual ~TouchInputMapper();
543
544 virtual uint32_t getSources();
545 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700546 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700547 virtual void configure();
548 virtual void reset();
549
550 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
551 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
552 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
553 const int32_t* keyCodes, uint8_t* outFlags);
554
555protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700556 Mutex mLock;
557
Jeff Brown6d0fec22010-07-23 21:28:06 -0700558 struct VirtualKey {
559 int32_t keyCode;
560 int32_t scanCode;
561 uint32_t flags;
562
563 // computed hit box, specified in touch screen coords based on known display size
564 int32_t hitLeft;
565 int32_t hitTop;
566 int32_t hitRight;
567 int32_t hitBottom;
568
569 inline bool isHit(int32_t x, int32_t y) const {
570 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
571 }
572 };
573
Jeff Brown8d608662010-08-30 03:02:23 -0700574 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575 struct PointerData {
576 uint32_t id;
577 int32_t x;
578 int32_t y;
579 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700580 int32_t touchMajor;
581 int32_t touchMinor;
582 int32_t toolMajor;
583 int32_t toolMinor;
584 int32_t orientation;
Jeff Brownc3db8582010-10-20 15:33:38 -0700585
586 inline bool operator== (const PointerData& other) const {
587 return id == other.id
588 && x == other.x
589 && y == other.y
590 && pressure == other.pressure
591 && touchMajor == other.touchMajor
592 && touchMinor == other.touchMinor
593 && toolMajor == other.toolMajor
594 && toolMinor == other.toolMinor
595 && orientation == other.orientation;
596 }
597 inline bool operator!= (const PointerData& other) const {
598 return !(*this == other);
599 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600 };
601
Jeff Brown8d608662010-08-30 03:02:23 -0700602 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700603 struct TouchData {
604 uint32_t pointerCount;
605 PointerData pointers[MAX_POINTERS];
606 BitSet32 idBits;
607 uint32_t idToIndex[MAX_POINTER_ID + 1];
608
609 void copyFrom(const TouchData& other) {
610 pointerCount = other.pointerCount;
611 idBits = other.idBits;
612
613 for (uint32_t i = 0; i < pointerCount; i++) {
614 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700615
616 int id = pointers[i].id;
617 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700618 }
619 }
620
621 inline void clear() {
622 pointerCount = 0;
623 idBits.clear();
624 }
625 };
626
Jeff Brown83c09682010-12-23 17:50:18 -0800627 // Input sources supported by the device.
628 int32_t mSources;
629
Jeff Brown6d0fec22010-07-23 21:28:06 -0700630 // Immutable configuration parameters.
631 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800632 enum DeviceType {
633 DEVICE_TYPE_TOUCH_SCREEN,
634 DEVICE_TYPE_TOUCH_PAD,
635 };
636
637 DeviceType deviceType;
638 int32_t associatedDisplayId;
639 bool orientationAware;
640
Jeff Brown6d0fec22010-07-23 21:28:06 -0700641 bool useBadTouchFilter;
642 bool useJumpyTouchFilter;
643 bool useAveragingTouchFilter;
Jeff Brownfe508922011-01-18 15:10:10 -0800644 nsecs_t virtualKeyQuietTime;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700645 } mParameters;
646
Jeff Brown8d608662010-08-30 03:02:23 -0700647 // Immutable calibration parameters in parsed form.
648 struct Calibration {
Jeff Brown511ee5f2010-10-18 13:32:20 -0700649 // Position
650 bool haveXOrigin;
651 int32_t xOrigin;
652 bool haveYOrigin;
653 int32_t yOrigin;
654 bool haveXScale;
655 float xScale;
656 bool haveYScale;
657 float yScale;
658
Jeff Brownc6d282b2010-10-14 21:42:15 -0700659 // Touch Size
660 enum TouchSizeCalibration {
661 TOUCH_SIZE_CALIBRATION_DEFAULT,
662 TOUCH_SIZE_CALIBRATION_NONE,
663 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
664 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700665 };
666
Jeff Brownc6d282b2010-10-14 21:42:15 -0700667 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700668
Jeff Brownc6d282b2010-10-14 21:42:15 -0700669 // Tool Size
670 enum ToolSizeCalibration {
671 TOOL_SIZE_CALIBRATION_DEFAULT,
672 TOOL_SIZE_CALIBRATION_NONE,
673 TOOL_SIZE_CALIBRATION_GEOMETRIC,
674 TOOL_SIZE_CALIBRATION_LINEAR,
675 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700676 };
677
Jeff Brownc6d282b2010-10-14 21:42:15 -0700678 ToolSizeCalibration toolSizeCalibration;
679 bool haveToolSizeLinearScale;
680 float toolSizeLinearScale;
681 bool haveToolSizeLinearBias;
682 float toolSizeLinearBias;
683 bool haveToolSizeAreaScale;
684 float toolSizeAreaScale;
685 bool haveToolSizeAreaBias;
686 float toolSizeAreaBias;
687 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800688 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700689
690 // Pressure
691 enum PressureCalibration {
692 PRESSURE_CALIBRATION_DEFAULT,
693 PRESSURE_CALIBRATION_NONE,
694 PRESSURE_CALIBRATION_PHYSICAL,
695 PRESSURE_CALIBRATION_AMPLITUDE,
696 };
697 enum PressureSource {
698 PRESSURE_SOURCE_DEFAULT,
699 PRESSURE_SOURCE_PRESSURE,
700 PRESSURE_SOURCE_TOUCH,
701 };
702
703 PressureCalibration pressureCalibration;
704 PressureSource pressureSource;
705 bool havePressureScale;
706 float pressureScale;
707
708 // Size
709 enum SizeCalibration {
710 SIZE_CALIBRATION_DEFAULT,
711 SIZE_CALIBRATION_NONE,
712 SIZE_CALIBRATION_NORMALIZED,
713 };
714
715 SizeCalibration sizeCalibration;
716
717 // Orientation
718 enum OrientationCalibration {
719 ORIENTATION_CALIBRATION_DEFAULT,
720 ORIENTATION_CALIBRATION_NONE,
721 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -0800722 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -0700723 };
724
725 OrientationCalibration orientationCalibration;
726 } mCalibration;
727
728 // Raw axis information from the driver.
729 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700730 RawAbsoluteAxisInfo x;
731 RawAbsoluteAxisInfo y;
732 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700733 RawAbsoluteAxisInfo touchMajor;
734 RawAbsoluteAxisInfo touchMinor;
735 RawAbsoluteAxisInfo toolMajor;
736 RawAbsoluteAxisInfo toolMinor;
737 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700738 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700739
Jeff Brown6328cdc2010-07-29 18:18:33 -0700740 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700741 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700742 TouchData mLastTouch;
743
744 // The time the primary pointer last went down.
745 nsecs_t mDownTime;
746
Jeff Brown6328cdc2010-07-29 18:18:33 -0700747 struct LockedState {
748 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700749
Jeff Brown6328cdc2010-07-29 18:18:33 -0700750 // The surface orientation and width and height set by configureSurfaceLocked().
751 int32_t surfaceOrientation;
752 int32_t surfaceWidth, surfaceHeight;
753
754 // Translation and scaling factors, orientation-independent.
755 int32_t xOrigin;
756 float xScale;
757 float xPrecision;
758
759 int32_t yOrigin;
760 float yScale;
761 float yPrecision;
762
Jeff Brown8d608662010-08-30 03:02:23 -0700763 float geometricScale;
764
Jeff Brownc6d282b2010-10-14 21:42:15 -0700765 float toolSizeLinearScale;
766 float toolSizeLinearBias;
767 float toolSizeAreaScale;
768 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700769
Jeff Brown6328cdc2010-07-29 18:18:33 -0700770 float pressureScale;
771
Jeff Brown6328cdc2010-07-29 18:18:33 -0700772 float sizeScale;
773
774 float orientationScale;
775
776 // Oriented motion ranges for input device info.
777 struct OrientedRanges {
778 InputDeviceInfo::MotionRange x;
779 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700780
781 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700782 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700783
784 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700785 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700786
Jeff Brownc6d282b2010-10-14 21:42:15 -0700787 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700788 InputDeviceInfo::MotionRange touchMajor;
789 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700790
Jeff Brownc6d282b2010-10-14 21:42:15 -0700791 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700792 InputDeviceInfo::MotionRange toolMajor;
793 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700794
795 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700796 InputDeviceInfo::MotionRange orientation;
797 } orientedRanges;
798
799 // Oriented dimensions and precision.
800 float orientedSurfaceWidth, orientedSurfaceHeight;
801 float orientedXPrecision, orientedYPrecision;
802
803 struct CurrentVirtualKeyState {
804 bool down;
805 nsecs_t downTime;
806 int32_t keyCode;
807 int32_t scanCode;
808 } currentVirtualKey;
809 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700810
Jeff Brown8d608662010-08-30 03:02:23 -0700811 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700812 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700813 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700814 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700815 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700816 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700817 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700818 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700819 virtual void parseCalibration();
820 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700821 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700822
823 enum TouchResult {
824 // Dispatch the touch normally.
825 DISPATCH_TOUCH,
826 // Do not dispatch the touch, but keep tracking the current stroke.
827 SKIP_TOUCH,
828 // Do not dispatch the touch, and drop all information associated with the current stoke
829 // so the next movement will appear as a new down.
830 DROP_STROKE
831 };
832
833 void syncTouch(nsecs_t when, bool havePointerIds);
834
835private:
836 /* Maximum number of historical samples to average. */
837 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
838
839 /* Slop distance for jumpy pointer detection.
840 * The vertical range of the screen divided by this is our epsilon value. */
841 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
842
843 /* Number of jumpy points to drop for touchscreens that need it. */
844 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
845 static const uint32_t JUMPY_DROP_LIMIT = 3;
846
847 /* Maximum squared distance for averaging.
848 * If moving farther than this, turn of averaging to avoid lag in response. */
849 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
850
851 struct AveragingTouchFilterState {
852 // Individual history tracks are stored by pointer id
853 uint32_t historyStart[MAX_POINTERS];
854 uint32_t historyEnd[MAX_POINTERS];
855 struct {
856 struct {
857 int32_t x;
858 int32_t y;
859 int32_t pressure;
860 } pointers[MAX_POINTERS];
861 } historyData[AVERAGING_HISTORY_SIZE];
862 } mAveragingTouchFilter;
863
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700864 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700865 uint32_t jumpyPointsDropped;
866 } mJumpyTouchFilter;
867
868 struct PointerDistanceHeapElement {
869 uint32_t currentPointerIndex : 8;
870 uint32_t lastPointerIndex : 8;
871 uint64_t distance : 48; // squared distance
872 };
873
Jeff Brown6328cdc2010-07-29 18:18:33 -0700874 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700875
876 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
877 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
878 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700879 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
880 int32_t motionEventAction);
Jeff Brownfe508922011-01-18 15:10:10 -0800881 void detectGestures(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700882
Jeff Brown6328cdc2010-07-29 18:18:33 -0700883 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
884 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700885
886 bool applyBadTouchFilter();
887 bool applyJumpyTouchFilter();
888 void applyAveragingTouchFilter();
889 void calculatePointerIds();
890};
891
892
893class SingleTouchInputMapper : public TouchInputMapper {
894public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800895 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700896 virtual ~SingleTouchInputMapper();
897
898 virtual void reset();
899 virtual void process(const RawEvent* rawEvent);
900
901protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700902 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700903
904private:
905 struct Accumulator {
906 enum {
907 FIELD_BTN_TOUCH = 1,
908 FIELD_ABS_X = 2,
909 FIELD_ABS_Y = 4,
910 FIELD_ABS_PRESSURE = 8,
911 FIELD_ABS_TOOL_WIDTH = 16
912 };
913
914 uint32_t fields;
915
916 bool btnTouch;
917 int32_t absX;
918 int32_t absY;
919 int32_t absPressure;
920 int32_t absToolWidth;
921
922 inline void clear() {
923 fields = 0;
924 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700925 } mAccumulator;
926
927 bool mDown;
928 int32_t mX;
929 int32_t mY;
930 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700931 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700932
933 void initialize();
934
935 void sync(nsecs_t when);
936};
937
938
939class MultiTouchInputMapper : public TouchInputMapper {
940public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800941 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700942 virtual ~MultiTouchInputMapper();
943
944 virtual void reset();
945 virtual void process(const RawEvent* rawEvent);
946
947protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700948 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700949
950private:
951 struct Accumulator {
952 enum {
953 FIELD_ABS_MT_POSITION_X = 1,
954 FIELD_ABS_MT_POSITION_Y = 2,
955 FIELD_ABS_MT_TOUCH_MAJOR = 4,
956 FIELD_ABS_MT_TOUCH_MINOR = 8,
957 FIELD_ABS_MT_WIDTH_MAJOR = 16,
958 FIELD_ABS_MT_WIDTH_MINOR = 32,
959 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700960 FIELD_ABS_MT_TRACKING_ID = 128,
961 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700962 };
963
964 uint32_t pointerCount;
965 struct Pointer {
966 uint32_t fields;
967
968 int32_t absMTPositionX;
969 int32_t absMTPositionY;
970 int32_t absMTTouchMajor;
971 int32_t absMTTouchMinor;
972 int32_t absMTWidthMajor;
973 int32_t absMTWidthMinor;
974 int32_t absMTOrientation;
975 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700976 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700977
978 inline void clear() {
979 fields = 0;
980 }
981 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
982
983 inline void clear() {
984 pointerCount = 0;
985 pointers[0].clear();
986 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700987 } mAccumulator;
988
989 void initialize();
990
991 void sync(nsecs_t when);
992};
993
Jeff Browncb1404e2011-01-15 18:14:15 -0800994
995class JoystickInputMapper : public InputMapper {
996public:
997 JoystickInputMapper(InputDevice* device);
998 virtual ~JoystickInputMapper();
999
1000 virtual uint32_t getSources();
1001 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
1002 virtual void dump(String8& dump);
1003 virtual void configure();
1004 virtual void reset();
1005 virtual void process(const RawEvent* rawEvent);
1006
1007private:
Jeff Brown6f2fba42011-02-19 01:08:02 -08001008 struct Axis {
1009 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001010
Jeff Brown6f2fba42011-02-19 01:08:02 -08001011 int32_t axis; // axis id
1012 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001013
Jeff Brown6f2fba42011-02-19 01:08:02 -08001014 float scale; // scale factor from raw to normalized values
1015 float offset; // offset to add after scaling for normalization
Jeff Browncb1404e2011-01-15 18:14:15 -08001016
Jeff Brown6f2fba42011-02-19 01:08:02 -08001017 float min; // normalized inclusive minimum
1018 float max; // normalized inclusive maximum
1019 float flat; // normalized flat region size
1020 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001021
Jeff Brown6f2fba42011-02-19 01:08:02 -08001022 float oldValue; // previous value
1023 float newValue; // most recent value
Jeff Browncb1404e2011-01-15 18:14:15 -08001024
Jeff Brown6f2fba42011-02-19 01:08:02 -08001025 float filter; // filter out small variations of this size
Jeff Browncb1404e2011-01-15 18:14:15 -08001026
Jeff Brown6f2fba42011-02-19 01:08:02 -08001027 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo,
1028 int32_t axis, bool explicitlyMapped, float scale, float offset,
1029 float min, float max, float flat, float fuzz) {
1030 this->rawAxisInfo = rawAxisInfo;
1031 this->axis = axis;
1032 this->explicitlyMapped = explicitlyMapped;
1033 this->scale = scale;
1034 this->offset = offset;
1035 this->min = min;
1036 this->max = max;
1037 this->flat = flat;
1038 this->fuzz = fuzz;
1039 this->filter = 0;
1040 this->oldValue = 0;
1041 this->newValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001042 }
1043 };
1044
Jeff Brown6f2fba42011-02-19 01:08:02 -08001045 // Axes indexed by raw ABS_* axis index.
1046 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001047
Jeff Brown6f2fba42011-02-19 01:08:02 -08001048 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001049
Jeff Brown6f2fba42011-02-19 01:08:02 -08001050 bool haveAxis(int32_t axis);
1051 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1052 bool haveAxesChangedSignificantly();
Jeff Browncb1404e2011-01-15 18:14:15 -08001053
Jeff Brown6f2fba42011-02-19 01:08:02 -08001054 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001055};
1056
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001057} // namespace android
1058
1059#endif // _UI_INPUT_READER_H