blob: cf41535e097cddbad10029d13e7a23dcb15dbd10 [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 Brown6d0fec22010-07-23 21:28:06 -0700160 virtual InputReaderPolicyInterface* getPolicy() = 0;
161 virtual InputDispatcherInterface* getDispatcher() = 0;
162 virtual EventHubInterface* getEventHub() = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700163};
164
Jeff Brown9c3cda02010-06-15 01:31:58 -0700165
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700166/* The input reader reads raw event data from the event hub and processes it into input events
Jeff Brown9c3cda02010-06-15 01:31:58 -0700167 * that it sends to the input dispatcher. Some functions of the input reader, such as early
168 * event filtering in low power states, are controlled by a separate policy object.
169 *
170 * IMPORTANT INVARIANT:
Jeff Brown6d0fec22010-07-23 21:28:06 -0700171 * Because the policy and dispatcher can potentially block or cause re-entrance into
172 * the input reader, the input reader never calls into other components while holding
Jeff Brown6328cdc2010-07-29 18:18:33 -0700173 * an exclusive internal lock whenever re-entrance can happen.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700174 */
Jeff Brownc3db8582010-10-20 15:33:38 -0700175class InputReader : public InputReaderInterface, protected InputReaderContext {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700176public:
177 InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700178 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700179 const sp<InputDispatcherInterface>& dispatcher);
180 virtual ~InputReader();
181
Jeff Brownb88102f2010-09-08 11:49:43 -0700182 virtual void dump(String8& dump);
183
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700184 virtual void loopOnce();
185
Jeff Brown6d0fec22010-07-23 21:28:06 -0700186 virtual void getInputConfiguration(InputConfiguration* outConfiguration);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700187
Jeff Brown6d0fec22010-07-23 21:28:06 -0700188 virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
189 virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700190
Jeff Brown6d0fec22010-07-23 21:28:06 -0700191 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
192 int32_t scanCode);
193 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
194 int32_t keyCode);
195 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
196 int32_t sw);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700197
Jeff Brown6d0fec22010-07-23 21:28:06 -0700198 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
199 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700200
Jeff Brownc3db8582010-10-20 15:33:38 -0700201protected:
202 // These methods are protected virtual so they can be overridden and instrumented
203 // by test cases.
204 virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
205
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700206private:
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700207 sp<EventHubInterface> mEventHub;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700208 sp<InputReaderPolicyInterface> mPolicy;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700209 sp<InputDispatcherInterface> mDispatcher;
210
Jeff Brown6d0fec22010-07-23 21:28:06 -0700211 virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
212 virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
213 virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
214
215 // This reader/writer lock guards the list of input devices.
216 // The writer lock must be held whenever the list of input devices is modified
217 // and then promptly released.
218 // The reader lock must be held whenever the list of input devices is traversed or an
219 // input device in the list is accessed.
220 // This lock only protects the registry and prevents inadvertent deletion of device objects
221 // that are in use. Individual devices are responsible for guarding their own internal state
222 // as needed for concurrent operation.
223 RWLock mDeviceRegistryLock;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700224 KeyedVector<int32_t, InputDevice*> mDevices;
225
Jeff Brown6d0fec22010-07-23 21:28:06 -0700226 // low-level input event decoding and device management
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700227 void process(const RawEvent* rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700228
Jeff Brown7342bb92010-10-01 18:55:43 -0700229 void addDevice(int32_t deviceId);
230 void removeDevice(int32_t deviceId);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700231 void configureExcludedDevices();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700232
Jeff Brown6d0fec22010-07-23 21:28:06 -0700233 void consumeEvent(const RawEvent* rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700234
Jeff Brownc3db8582010-10-20 15:33:38 -0700235 void handleConfigurationChanged(nsecs_t when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700236
Jeff Brown6d0fec22010-07-23 21:28:06 -0700237 // state management for all devices
238 Mutex mStateLock;
239
240 int32_t mGlobalMetaState;
241 virtual void updateGlobalMetaState();
242 virtual int32_t getGlobalMetaState();
243
244 InputConfiguration mInputConfiguration;
245 void updateInputConfiguration();
246
Jeff Brownfe508922011-01-18 15:10:10 -0800247 nsecs_t mDisableVirtualKeysTimeout;
248 virtual void disableVirtualKeysUntil(nsecs_t time);
249 virtual bool shouldDropVirtualKey(nsecs_t now,
250 InputDevice* device, int32_t keyCode, int32_t scanCode);
251
Jeff Brown6d0fec22010-07-23 21:28:06 -0700252 // state queries
253 typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
254 int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
255 GetStateFunc getStateFunc);
256 bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
257 const int32_t* keyCodes, uint8_t* outFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700258};
259
260
261/* Reads raw events from the event hub and processes them, endlessly. */
262class InputReaderThread : public Thread {
263public:
264 InputReaderThread(const sp<InputReaderInterface>& reader);
265 virtual ~InputReaderThread();
266
267private:
268 sp<InputReaderInterface> mReader;
269
270 virtual bool threadLoop();
271};
272
Jeff Brown6d0fec22010-07-23 21:28:06 -0700273
274/* Represents the state of a single input device. */
275class InputDevice {
276public:
277 InputDevice(InputReaderContext* context, int32_t id, const String8& name);
278 ~InputDevice();
279
280 inline InputReaderContext* getContext() { return mContext; }
281 inline int32_t getId() { return mId; }
282 inline const String8& getName() { return mName; }
283 inline uint32_t getSources() { return mSources; }
284
285 inline bool isIgnored() { return mMappers.isEmpty(); }
286
Jeff Brownef3d7e82010-09-30 14:33:04 -0700287 void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700288 void addMapper(InputMapper* mapper);
289 void configure();
290 void reset();
291 void process(const RawEvent* rawEvent);
292
293 void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
294 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
295 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
296 int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
297 bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
298 const int32_t* keyCodes, uint8_t* outFlags);
299
300 int32_t getMetaState();
301
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800302 inline const PropertyMap& getConfiguration() {
303 return mConfiguration;
Jeff Brown8d608662010-08-30 03:02:23 -0700304 }
305
Jeff Brown6d0fec22010-07-23 21:28:06 -0700306private:
307 InputReaderContext* mContext;
308 int32_t mId;
309
310 Vector<InputMapper*> mMappers;
311
312 String8 mName;
313 uint32_t mSources;
314
315 typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
316 int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
Jeff Brown8d608662010-08-30 03:02:23 -0700317
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800318 PropertyMap mConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700319};
320
321
322/* An input mapper transforms raw input events into cooked event data.
323 * A single input device can have multiple associated input mappers in order to interpret
324 * different classes of events.
325 */
326class InputMapper {
327public:
328 InputMapper(InputDevice* device);
329 virtual ~InputMapper();
330
331 inline InputDevice* getDevice() { return mDevice; }
332 inline int32_t getDeviceId() { return mDevice->getId(); }
333 inline const String8 getDeviceName() { return mDevice->getName(); }
334 inline InputReaderContext* getContext() { return mContext; }
335 inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
336 inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
337 inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
338
339 virtual uint32_t getSources() = 0;
340 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700341 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700342 virtual void configure();
343 virtual void reset();
344 virtual void process(const RawEvent* rawEvent) = 0;
345
346 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
347 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
348 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
349 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
350 const int32_t* keyCodes, uint8_t* outFlags);
351
352 virtual int32_t getMetaState();
353
354protected:
355 InputDevice* mDevice;
356 InputReaderContext* mContext;
Jeff Browncb1404e2011-01-15 18:14:15 -0800357
358 static void dumpRawAbsoluteAxisInfo(String8& dump,
359 const RawAbsoluteAxisInfo& axis, const char* name);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700360};
361
362
363class SwitchInputMapper : public InputMapper {
364public:
365 SwitchInputMapper(InputDevice* device);
366 virtual ~SwitchInputMapper();
367
368 virtual uint32_t getSources();
369 virtual void process(const RawEvent* rawEvent);
370
371 virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
372
373private:
374 void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
375};
376
377
378class KeyboardInputMapper : public InputMapper {
379public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800380 KeyboardInputMapper(InputDevice* device, uint32_t sources, int32_t keyboardType);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381 virtual ~KeyboardInputMapper();
382
383 virtual uint32_t getSources();
384 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700385 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800386 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387 virtual void reset();
388 virtual void process(const RawEvent* rawEvent);
389
390 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
391 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
392 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
393 const int32_t* keyCodes, uint8_t* outFlags);
394
395 virtual int32_t getMetaState();
396
397private:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700398 Mutex mLock;
399
Jeff Brown6d0fec22010-07-23 21:28:06 -0700400 struct KeyDown {
401 int32_t keyCode;
402 int32_t scanCode;
403 };
404
Jeff Brown6d0fec22010-07-23 21:28:06 -0700405 uint32_t mSources;
406 int32_t mKeyboardType;
407
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800408 // Immutable configuration parameters.
409 struct Parameters {
410 int32_t associatedDisplayId;
411 bool orientationAware;
412 } mParameters;
413
Jeff Brown6328cdc2010-07-29 18:18:33 -0700414 struct LockedState {
415 Vector<KeyDown> keyDowns; // keys that are down
416 int32_t metaState;
417 nsecs_t downTime; // time of most recent key down
Jeff Brown497a92c2010-09-12 17:55:08 -0700418
419 struct LedState {
420 bool avail; // led is available
421 bool on; // we think the led is currently on
422 };
423 LedState capsLockLedState;
424 LedState numLockLedState;
425 LedState scrollLockLedState;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700426 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700427
Jeff Brown6328cdc2010-07-29 18:18:33 -0700428 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700429
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800430 void configureParameters();
431 void dumpParameters(String8& dump);
432
Jeff Brown6d0fec22010-07-23 21:28:06 -0700433 bool isKeyboardOrGamepadKey(int32_t scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700434
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435 void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
436 uint32_t policyFlags);
437
Jeff Brown6328cdc2010-07-29 18:18:33 -0700438 ssize_t findKeyDownLocked(int32_t scanCode);
Jeff Brown497a92c2010-09-12 17:55:08 -0700439
Jeff Brown49ed71d2010-12-06 17:13:33 -0800440 void resetLedStateLocked();
441 void initializeLedStateLocked(LockedState::LedState& ledState, int32_t led);
Jeff Brown497a92c2010-09-12 17:55:08 -0700442 void updateLedStateLocked(bool reset);
443 void updateLedStateForModifierLocked(LockedState::LedState& ledState, int32_t led,
444 int32_t modifier, bool reset);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700445};
446
447
Jeff Brown83c09682010-12-23 17:50:18 -0800448class CursorInputMapper : public InputMapper {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700449public:
Jeff Brown83c09682010-12-23 17:50:18 -0800450 CursorInputMapper(InputDevice* device);
451 virtual ~CursorInputMapper();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700452
453 virtual uint32_t getSources();
454 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700455 virtual void dump(String8& dump);
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800456 virtual void configure();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700457 virtual void reset();
458 virtual void process(const RawEvent* rawEvent);
459
Jeff Brownc3fc2d02010-08-10 15:47:53 -0700460 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
461
Jeff Brown6d0fec22010-07-23 21:28:06 -0700462private:
463 // Amount that trackball needs to move in order to generate a key event.
464 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
465
Jeff Brown6328cdc2010-07-29 18:18:33 -0700466 Mutex mLock;
467
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800468 // Immutable configuration parameters.
469 struct Parameters {
Jeff Brown83c09682010-12-23 17:50:18 -0800470 enum Mode {
471 MODE_POINTER,
472 MODE_NAVIGATION,
473 };
474
475 Mode mode;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800476 int32_t associatedDisplayId;
477 bool orientationAware;
478 } mParameters;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700479
480 struct Accumulator {
481 enum {
482 FIELD_BTN_MOUSE = 1,
483 FIELD_REL_X = 2,
Jeff Brown6f2fba42011-02-19 01:08:02 -0800484 FIELD_REL_Y = 4,
485 FIELD_REL_WHEEL = 8,
486 FIELD_REL_HWHEEL = 16,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700487 };
488
489 uint32_t fields;
490
491 bool btnMouse;
492 int32_t relX;
493 int32_t relY;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800494 int32_t relWheel;
495 int32_t relHWheel;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700496
497 inline void clear() {
498 fields = 0;
499 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700500 } mAccumulator;
501
Jeff Brown83c09682010-12-23 17:50:18 -0800502 int32_t mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700503 float mXScale;
504 float mYScale;
505 float mXPrecision;
506 float mYPrecision;
Jeff Brown6f2fba42011-02-19 01:08:02 -0800507
508 bool mHaveVWheel;
509 bool mHaveHWheel;
510 float mVWheelScale;
511 float mHWheelScale;
512
Jeff Brown83c09682010-12-23 17:50:18 -0800513 sp<PointerControllerInterface> mPointerController;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700514
Jeff Brown6328cdc2010-07-29 18:18:33 -0700515 struct LockedState {
516 bool down;
517 nsecs_t downTime;
518 } mLocked;
519
520 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700521
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800522 void configureParameters();
523 void dumpParameters(String8& dump);
524
Jeff Brown6d0fec22010-07-23 21:28:06 -0700525 void sync(nsecs_t when);
526};
527
528
529class TouchInputMapper : public InputMapper {
530public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800531 TouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700532 virtual ~TouchInputMapper();
533
534 virtual uint32_t getSources();
535 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700536 virtual void dump(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700537 virtual void configure();
538 virtual void reset();
539
540 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
541 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
542 virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
543 const int32_t* keyCodes, uint8_t* outFlags);
544
545protected:
Jeff Brown6328cdc2010-07-29 18:18:33 -0700546 Mutex mLock;
547
Jeff Brown6d0fec22010-07-23 21:28:06 -0700548 struct VirtualKey {
549 int32_t keyCode;
550 int32_t scanCode;
551 uint32_t flags;
552
553 // computed hit box, specified in touch screen coords based on known display size
554 int32_t hitLeft;
555 int32_t hitTop;
556 int32_t hitRight;
557 int32_t hitBottom;
558
559 inline bool isHit(int32_t x, int32_t y) const {
560 return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
561 }
562 };
563
Jeff Brown8d608662010-08-30 03:02:23 -0700564 // Raw data for a single pointer.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700565 struct PointerData {
566 uint32_t id;
567 int32_t x;
568 int32_t y;
569 int32_t pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700570 int32_t touchMajor;
571 int32_t touchMinor;
572 int32_t toolMajor;
573 int32_t toolMinor;
574 int32_t orientation;
Jeff Brownc3db8582010-10-20 15:33:38 -0700575
576 inline bool operator== (const PointerData& other) const {
577 return id == other.id
578 && x == other.x
579 && y == other.y
580 && pressure == other.pressure
581 && touchMajor == other.touchMajor
582 && touchMinor == other.touchMinor
583 && toolMajor == other.toolMajor
584 && toolMinor == other.toolMinor
585 && orientation == other.orientation;
586 }
587 inline bool operator!= (const PointerData& other) const {
588 return !(*this == other);
589 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700590 };
591
Jeff Brown8d608662010-08-30 03:02:23 -0700592 // Raw data for a collection of pointers including a pointer id mapping table.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700593 struct TouchData {
594 uint32_t pointerCount;
595 PointerData pointers[MAX_POINTERS];
596 BitSet32 idBits;
597 uint32_t idToIndex[MAX_POINTER_ID + 1];
598
599 void copyFrom(const TouchData& other) {
600 pointerCount = other.pointerCount;
601 idBits = other.idBits;
602
603 for (uint32_t i = 0; i < pointerCount; i++) {
604 pointers[i] = other.pointers[i];
Jeff Brown9e2ad362010-07-30 19:20:11 -0700605
606 int id = pointers[i].id;
607 idToIndex[id] = other.idToIndex[id];
Jeff Brown6d0fec22010-07-23 21:28:06 -0700608 }
609 }
610
611 inline void clear() {
612 pointerCount = 0;
613 idBits.clear();
614 }
615 };
616
Jeff Brown83c09682010-12-23 17:50:18 -0800617 // Input sources supported by the device.
618 int32_t mSources;
619
Jeff Brown6d0fec22010-07-23 21:28:06 -0700620 // Immutable configuration parameters.
621 struct Parameters {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800622 enum DeviceType {
623 DEVICE_TYPE_TOUCH_SCREEN,
624 DEVICE_TYPE_TOUCH_PAD,
625 };
626
627 DeviceType deviceType;
628 int32_t associatedDisplayId;
629 bool orientationAware;
630
Jeff Brown6d0fec22010-07-23 21:28:06 -0700631 bool useBadTouchFilter;
632 bool useJumpyTouchFilter;
633 bool useAveragingTouchFilter;
Jeff Brownfe508922011-01-18 15:10:10 -0800634 nsecs_t virtualKeyQuietTime;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700635 } mParameters;
636
Jeff Brown8d608662010-08-30 03:02:23 -0700637 // Immutable calibration parameters in parsed form.
638 struct Calibration {
Jeff Brown511ee5f2010-10-18 13:32:20 -0700639 // Position
640 bool haveXOrigin;
641 int32_t xOrigin;
642 bool haveYOrigin;
643 int32_t yOrigin;
644 bool haveXScale;
645 float xScale;
646 bool haveYScale;
647 float yScale;
648
Jeff Brownc6d282b2010-10-14 21:42:15 -0700649 // Touch Size
650 enum TouchSizeCalibration {
651 TOUCH_SIZE_CALIBRATION_DEFAULT,
652 TOUCH_SIZE_CALIBRATION_NONE,
653 TOUCH_SIZE_CALIBRATION_GEOMETRIC,
654 TOUCH_SIZE_CALIBRATION_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -0700655 };
656
Jeff Brownc6d282b2010-10-14 21:42:15 -0700657 TouchSizeCalibration touchSizeCalibration;
Jeff Brown8d608662010-08-30 03:02:23 -0700658
Jeff Brownc6d282b2010-10-14 21:42:15 -0700659 // Tool Size
660 enum ToolSizeCalibration {
661 TOOL_SIZE_CALIBRATION_DEFAULT,
662 TOOL_SIZE_CALIBRATION_NONE,
663 TOOL_SIZE_CALIBRATION_GEOMETRIC,
664 TOOL_SIZE_CALIBRATION_LINEAR,
665 TOOL_SIZE_CALIBRATION_AREA,
Jeff Brown8d608662010-08-30 03:02:23 -0700666 };
667
Jeff Brownc6d282b2010-10-14 21:42:15 -0700668 ToolSizeCalibration toolSizeCalibration;
669 bool haveToolSizeLinearScale;
670 float toolSizeLinearScale;
671 bool haveToolSizeLinearBias;
672 float toolSizeLinearBias;
673 bool haveToolSizeAreaScale;
674 float toolSizeAreaScale;
675 bool haveToolSizeAreaBias;
676 float toolSizeAreaBias;
677 bool haveToolSizeIsSummed;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800678 bool toolSizeIsSummed;
Jeff Brown8d608662010-08-30 03:02:23 -0700679
680 // Pressure
681 enum PressureCalibration {
682 PRESSURE_CALIBRATION_DEFAULT,
683 PRESSURE_CALIBRATION_NONE,
684 PRESSURE_CALIBRATION_PHYSICAL,
685 PRESSURE_CALIBRATION_AMPLITUDE,
686 };
687 enum PressureSource {
688 PRESSURE_SOURCE_DEFAULT,
689 PRESSURE_SOURCE_PRESSURE,
690 PRESSURE_SOURCE_TOUCH,
691 };
692
693 PressureCalibration pressureCalibration;
694 PressureSource pressureSource;
695 bool havePressureScale;
696 float pressureScale;
697
698 // Size
699 enum SizeCalibration {
700 SIZE_CALIBRATION_DEFAULT,
701 SIZE_CALIBRATION_NONE,
702 SIZE_CALIBRATION_NORMALIZED,
703 };
704
705 SizeCalibration sizeCalibration;
706
707 // Orientation
708 enum OrientationCalibration {
709 ORIENTATION_CALIBRATION_DEFAULT,
710 ORIENTATION_CALIBRATION_NONE,
711 ORIENTATION_CALIBRATION_INTERPOLATED,
Jeff Brown517bb4c2011-01-14 19:09:23 -0800712 ORIENTATION_CALIBRATION_VECTOR,
Jeff Brown8d608662010-08-30 03:02:23 -0700713 };
714
715 OrientationCalibration orientationCalibration;
716 } mCalibration;
717
718 // Raw axis information from the driver.
719 struct RawAxes {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700720 RawAbsoluteAxisInfo x;
721 RawAbsoluteAxisInfo y;
722 RawAbsoluteAxisInfo pressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700723 RawAbsoluteAxisInfo touchMajor;
724 RawAbsoluteAxisInfo touchMinor;
725 RawAbsoluteAxisInfo toolMajor;
726 RawAbsoluteAxisInfo toolMinor;
727 RawAbsoluteAxisInfo orientation;
Jeff Brown8d608662010-08-30 03:02:23 -0700728 } mRawAxes;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700729
Jeff Brown6328cdc2010-07-29 18:18:33 -0700730 // Current and previous touch sample data.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700731 TouchData mCurrentTouch;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700732 TouchData mLastTouch;
733
734 // The time the primary pointer last went down.
735 nsecs_t mDownTime;
736
Jeff Brown6328cdc2010-07-29 18:18:33 -0700737 struct LockedState {
738 Vector<VirtualKey> virtualKeys;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700739
Jeff Brown6328cdc2010-07-29 18:18:33 -0700740 // The surface orientation and width and height set by configureSurfaceLocked().
741 int32_t surfaceOrientation;
742 int32_t surfaceWidth, surfaceHeight;
743
744 // Translation and scaling factors, orientation-independent.
745 int32_t xOrigin;
746 float xScale;
747 float xPrecision;
748
749 int32_t yOrigin;
750 float yScale;
751 float yPrecision;
752
Jeff Brown8d608662010-08-30 03:02:23 -0700753 float geometricScale;
754
Jeff Brownc6d282b2010-10-14 21:42:15 -0700755 float toolSizeLinearScale;
756 float toolSizeLinearBias;
757 float toolSizeAreaScale;
758 float toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -0700759
Jeff Brown6328cdc2010-07-29 18:18:33 -0700760 float pressureScale;
761
Jeff Brown6328cdc2010-07-29 18:18:33 -0700762 float sizeScale;
763
764 float orientationScale;
765
766 // Oriented motion ranges for input device info.
767 struct OrientedRanges {
768 InputDeviceInfo::MotionRange x;
769 InputDeviceInfo::MotionRange y;
Jeff Brown8d608662010-08-30 03:02:23 -0700770
771 bool havePressure;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700772 InputDeviceInfo::MotionRange pressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700773
774 bool haveSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700775 InputDeviceInfo::MotionRange size;
Jeff Brown8d608662010-08-30 03:02:23 -0700776
Jeff Brownc6d282b2010-10-14 21:42:15 -0700777 bool haveTouchSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700778 InputDeviceInfo::MotionRange touchMajor;
779 InputDeviceInfo::MotionRange touchMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700780
Jeff Brownc6d282b2010-10-14 21:42:15 -0700781 bool haveToolSize;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700782 InputDeviceInfo::MotionRange toolMajor;
783 InputDeviceInfo::MotionRange toolMinor;
Jeff Brown8d608662010-08-30 03:02:23 -0700784
785 bool haveOrientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700786 InputDeviceInfo::MotionRange orientation;
787 } orientedRanges;
788
789 // Oriented dimensions and precision.
790 float orientedSurfaceWidth, orientedSurfaceHeight;
791 float orientedXPrecision, orientedYPrecision;
792
793 struct CurrentVirtualKeyState {
794 bool down;
795 nsecs_t downTime;
796 int32_t keyCode;
797 int32_t scanCode;
798 } currentVirtualKey;
799 } mLocked;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700800
Jeff Brown8d608662010-08-30 03:02:23 -0700801 virtual void configureParameters();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700802 virtual void dumpParameters(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700803 virtual void configureRawAxes();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700804 virtual void dumpRawAxes(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700805 virtual bool configureSurfaceLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700806 virtual void dumpSurfaceLocked(String8& dump);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700807 virtual void configureVirtualKeysLocked();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700808 virtual void dumpVirtualKeysLocked(String8& dump);
Jeff Brown8d608662010-08-30 03:02:23 -0700809 virtual void parseCalibration();
810 virtual void resolveCalibration();
Jeff Brownef3d7e82010-09-30 14:33:04 -0700811 virtual void dumpCalibration(String8& dump);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700812
813 enum TouchResult {
814 // Dispatch the touch normally.
815 DISPATCH_TOUCH,
816 // Do not dispatch the touch, but keep tracking the current stroke.
817 SKIP_TOUCH,
818 // Do not dispatch the touch, and drop all information associated with the current stoke
819 // so the next movement will appear as a new down.
820 DROP_STROKE
821 };
822
823 void syncTouch(nsecs_t when, bool havePointerIds);
824
825private:
826 /* Maximum number of historical samples to average. */
827 static const uint32_t AVERAGING_HISTORY_SIZE = 5;
828
829 /* Slop distance for jumpy pointer detection.
830 * The vertical range of the screen divided by this is our epsilon value. */
831 static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
832
833 /* Number of jumpy points to drop for touchscreens that need it. */
834 static const uint32_t JUMPY_TRANSITION_DROPS = 3;
835 static const uint32_t JUMPY_DROP_LIMIT = 3;
836
837 /* Maximum squared distance for averaging.
838 * If moving farther than this, turn of averaging to avoid lag in response. */
839 static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
840
841 struct AveragingTouchFilterState {
842 // Individual history tracks are stored by pointer id
843 uint32_t historyStart[MAX_POINTERS];
844 uint32_t historyEnd[MAX_POINTERS];
845 struct {
846 struct {
847 int32_t x;
848 int32_t y;
849 int32_t pressure;
850 } pointers[MAX_POINTERS];
851 } historyData[AVERAGING_HISTORY_SIZE];
852 } mAveragingTouchFilter;
853
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700854 struct JumpyTouchFilterState {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700855 uint32_t jumpyPointsDropped;
856 } mJumpyTouchFilter;
857
858 struct PointerDistanceHeapElement {
859 uint32_t currentPointerIndex : 8;
860 uint32_t lastPointerIndex : 8;
861 uint64_t distance : 48; // squared distance
862 };
863
Jeff Brown6328cdc2010-07-29 18:18:33 -0700864 void initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700865
866 TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
867 void dispatchTouches(nsecs_t when, uint32_t policyFlags);
868 void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
Jeff Brown8d608662010-08-30 03:02:23 -0700869 BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
870 int32_t motionEventAction);
Jeff Brownfe508922011-01-18 15:10:10 -0800871 void detectGestures(nsecs_t when);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700872
Jeff Brown6328cdc2010-07-29 18:18:33 -0700873 bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
874 const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700875
876 bool applyBadTouchFilter();
877 bool applyJumpyTouchFilter();
878 void applyAveragingTouchFilter();
879 void calculatePointerIds();
880};
881
882
883class SingleTouchInputMapper : public TouchInputMapper {
884public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800885 SingleTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700886 virtual ~SingleTouchInputMapper();
887
888 virtual void reset();
889 virtual void process(const RawEvent* rawEvent);
890
891protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700892 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700893
894private:
895 struct Accumulator {
896 enum {
897 FIELD_BTN_TOUCH = 1,
898 FIELD_ABS_X = 2,
899 FIELD_ABS_Y = 4,
900 FIELD_ABS_PRESSURE = 8,
901 FIELD_ABS_TOOL_WIDTH = 16
902 };
903
904 uint32_t fields;
905
906 bool btnTouch;
907 int32_t absX;
908 int32_t absY;
909 int32_t absPressure;
910 int32_t absToolWidth;
911
912 inline void clear() {
913 fields = 0;
914 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700915 } mAccumulator;
916
917 bool mDown;
918 int32_t mX;
919 int32_t mY;
920 int32_t mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -0700921 int32_t mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700922
923 void initialize();
924
925 void sync(nsecs_t when);
926};
927
928
929class MultiTouchInputMapper : public TouchInputMapper {
930public:
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800931 MultiTouchInputMapper(InputDevice* device);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700932 virtual ~MultiTouchInputMapper();
933
934 virtual void reset();
935 virtual void process(const RawEvent* rawEvent);
936
937protected:
Jeff Brown8d608662010-08-30 03:02:23 -0700938 virtual void configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700939
940private:
941 struct Accumulator {
942 enum {
943 FIELD_ABS_MT_POSITION_X = 1,
944 FIELD_ABS_MT_POSITION_Y = 2,
945 FIELD_ABS_MT_TOUCH_MAJOR = 4,
946 FIELD_ABS_MT_TOUCH_MINOR = 8,
947 FIELD_ABS_MT_WIDTH_MAJOR = 16,
948 FIELD_ABS_MT_WIDTH_MINOR = 32,
949 FIELD_ABS_MT_ORIENTATION = 64,
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700950 FIELD_ABS_MT_TRACKING_ID = 128,
951 FIELD_ABS_MT_PRESSURE = 256,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700952 };
953
954 uint32_t pointerCount;
955 struct Pointer {
956 uint32_t fields;
957
958 int32_t absMTPositionX;
959 int32_t absMTPositionY;
960 int32_t absMTTouchMajor;
961 int32_t absMTTouchMinor;
962 int32_t absMTWidthMajor;
963 int32_t absMTWidthMinor;
964 int32_t absMTOrientation;
965 int32_t absMTTrackingId;
Jeff Brown2dfd7a72010-08-17 20:38:35 -0700966 int32_t absMTPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700967
968 inline void clear() {
969 fields = 0;
970 }
971 } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
972
973 inline void clear() {
974 pointerCount = 0;
975 pointers[0].clear();
976 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700977 } mAccumulator;
978
979 void initialize();
980
981 void sync(nsecs_t when);
982};
983
Jeff Browncb1404e2011-01-15 18:14:15 -0800984
985class JoystickInputMapper : public InputMapper {
986public:
987 JoystickInputMapper(InputDevice* device);
988 virtual ~JoystickInputMapper();
989
990 virtual uint32_t getSources();
991 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
992 virtual void dump(String8& dump);
993 virtual void configure();
994 virtual void reset();
995 virtual void process(const RawEvent* rawEvent);
996
997private:
Jeff Brown6f2fba42011-02-19 01:08:02 -0800998 struct Axis {
999 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Browncb1404e2011-01-15 18:14:15 -08001000
Jeff Brown6f2fba42011-02-19 01:08:02 -08001001 int32_t axis; // axis id
1002 bool explicitlyMapped; // true if the axis was explicitly assigned an axis id
Jeff Browncb1404e2011-01-15 18:14:15 -08001003
Jeff Brown6f2fba42011-02-19 01:08:02 -08001004 float scale; // scale factor from raw to normalized values
1005 float offset; // offset to add after scaling for normalization
Jeff Browncb1404e2011-01-15 18:14:15 -08001006
Jeff Brown6f2fba42011-02-19 01:08:02 -08001007 float min; // normalized inclusive minimum
1008 float max; // normalized inclusive maximum
1009 float flat; // normalized flat region size
1010 float fuzz; // normalized error tolerance
Jeff Browncb1404e2011-01-15 18:14:15 -08001011
Jeff Brown6f2fba42011-02-19 01:08:02 -08001012 float oldValue; // previous value
1013 float newValue; // most recent value
Jeff Browncb1404e2011-01-15 18:14:15 -08001014
Jeff Brown6f2fba42011-02-19 01:08:02 -08001015 float filter; // filter out small variations of this size
Jeff Browncb1404e2011-01-15 18:14:15 -08001016
Jeff Brown6f2fba42011-02-19 01:08:02 -08001017 void initialize(const RawAbsoluteAxisInfo& rawAxisInfo,
1018 int32_t axis, bool explicitlyMapped, float scale, float offset,
1019 float min, float max, float flat, float fuzz) {
1020 this->rawAxisInfo = rawAxisInfo;
1021 this->axis = axis;
1022 this->explicitlyMapped = explicitlyMapped;
1023 this->scale = scale;
1024 this->offset = offset;
1025 this->min = min;
1026 this->max = max;
1027 this->flat = flat;
1028 this->fuzz = fuzz;
1029 this->filter = 0;
1030 this->oldValue = 0;
1031 this->newValue = 0;
Jeff Browncb1404e2011-01-15 18:14:15 -08001032 }
1033 };
1034
Jeff Brown6f2fba42011-02-19 01:08:02 -08001035 // Axes indexed by raw ABS_* axis index.
1036 KeyedVector<int32_t, Axis> mAxes;
Jeff Browncb1404e2011-01-15 18:14:15 -08001037
Jeff Brown6f2fba42011-02-19 01:08:02 -08001038 void sync(nsecs_t when, bool force);
Jeff Browncb1404e2011-01-15 18:14:15 -08001039
Jeff Brown6f2fba42011-02-19 01:08:02 -08001040 bool haveAxis(int32_t axis);
1041 void pruneAxes(bool ignoreExplicitlyMappedAxes);
1042 bool haveAxesChangedSignificantly();
Jeff Browncb1404e2011-01-15 18:14:15 -08001043
Jeff Brown6f2fba42011-02-19 01:08:02 -08001044 static bool isCenteredAxis(int32_t axis);
Jeff Browncb1404e2011-01-15 18:14:15 -08001045};
1046
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001047} // namespace android
1048
1049#endif // _UI_INPUT_READER_H