Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef _UI_INPUT_READER_H
     18 #define _UI_INPUT_READER_H
     19 
     20 #include <ui/EventHub.h>
     21 #include <ui/Input.h>
     22 #include <ui/InputDispatcher.h>
     23 #include <utils/KeyedVector.h>
     24 #include <utils/threads.h>
     25 #include <utils/Timers.h>
     26 #include <utils/RefBase.h>
     27 #include <utils/String8.h>
     28 #include <utils/BitSet.h>
     29 
     30 #include <stddef.h>
     31 #include <unistd.h>
     32 
     33 namespace android {
     34 
     35 class InputDevice;
     36 class InputMapper;
     37 
     38 /* Describes a virtual key. */
     39 struct VirtualKeyDefinition {
     40     int32_t scanCode;
     41 
     42     // configured position data, specified in display coords
     43     int32_t centerX;
     44     int32_t centerY;
     45     int32_t width;
     46     int32_t height;
     47 };
     48 
     49 
     50 /* Specifies input device calibration settings. */
     51 class InputDeviceCalibration {
     52 public:
     53     InputDeviceCalibration();
     54 
     55     void clear();
     56     void addProperty(const String8& key, const String8& value);
     57 
     58     bool tryGetProperty(const String8& key, String8& outValue) const;
     59     bool tryGetProperty(const String8& key, int32_t& outValue) const;
     60     bool tryGetProperty(const String8& key, float& outValue) const;
     61 
     62 private:
     63     KeyedVector<String8, String8> mProperties;
     64 };
     65 
     66 
     67 /*
     68  * Input reader policy interface.
     69  *
     70  * The input reader policy is used by the input reader to interact with the Window Manager
     71  * and other system components.
     72  *
     73  * The actual implementation is partially supported by callbacks into the DVM
     74  * via JNI.  This interface is also mocked in the unit tests.
     75  */
     76 class InputReaderPolicyInterface : public virtual RefBase {
     77 protected:
     78     InputReaderPolicyInterface() { }
     79     virtual ~InputReaderPolicyInterface() { }
     80 
     81 public:
     82     /* Display orientations. */
     83     enum {
     84         ROTATION_0 = 0,
     85         ROTATION_90 = 1,
     86         ROTATION_180 = 2,
     87         ROTATION_270 = 3
     88     };
     89 
     90     /* Gets information about the display with the specified id.
     91      * Returns true if the display info is available, false otherwise.
     92      */
     93     virtual bool getDisplayInfo(int32_t displayId,
     94             int32_t* width, int32_t* height, int32_t* orientation) = 0;
     95 
     96     /* Determines whether to turn on some hacks we have to improve the touch interaction with a
     97      * certain device whose screen currently is not all that good.
     98      */
     99     virtual bool filterTouchEvents() = 0;
    100 
    101     /* Determines whether to turn on some hacks to improve touch interaction with another device
    102      * where touch coordinate data can get corrupted.
    103      */
    104     virtual bool filterJumpyTouchEvents() = 0;
    105 
    106     /* Gets the amount of time to disable virtual keys after the screen is touched
    107      * in order to filter out accidental virtual key presses due to swiping gestures
    108      * or taps near the edge of the display.  May be 0 to disable the feature.
    109      */
    110     virtual nsecs_t getVirtualKeyQuietTime() = 0;
    111 
    112     /* Gets the configured virtual key definitions for an input device. */
    113     virtual void getVirtualKeyDefinitions(const String8& deviceName,
    114             Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) = 0;
    115 
    116     /* Gets the calibration for an input device. */
    117     virtual void getInputDeviceCalibration(const String8& deviceName,
    118             InputDeviceCalibration& outCalibration) = 0;
    119 
    120     /* Gets the excluded device names for the platform. */
    121     virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) = 0;
    122 };
    123 
    124 
    125 /* Processes raw input events and sends cooked event data to an input dispatcher. */
    126 class InputReaderInterface : public virtual RefBase {
    127 protected:
    128     InputReaderInterface() { }
    129     virtual ~InputReaderInterface() { }
    130 
    131 public:
    132     /* Dumps the state of the input reader.
    133      *
    134      * This method may be called on any thread (usually by the input manager). */
    135     virtual void dump(String8& dump) = 0;
    136 
    137     /* Runs a single iteration of the processing loop.
    138      * Nominally reads and processes one incoming message from the EventHub.
    139      *
    140      * This method should be called on the input reader thread.
    141      */
    142     virtual void loopOnce() = 0;
    143 
    144     /* Gets the current input device configuration.
    145      *
    146      * This method may be called on any thread (usually by the input manager).
    147      */
    148     virtual void getInputConfiguration(InputConfiguration* outConfiguration) = 0;
    149 
    150     /* Gets information about the specified input device.
    151      * Returns OK if the device information was obtained or NAME_NOT_FOUND if there
    152      * was no such device.
    153      *
    154      * This method may be called on any thread (usually by the input manager).
    155      */
    156     virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) = 0;
    157 
    158     /* Gets the list of all registered device ids. */
    159     virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds) = 0;
    160 
    161     /* Query current input state. */
    162     virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
    163             int32_t scanCode) = 0;
    164     virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
    165             int32_t keyCode) = 0;
    166     virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
    167             int32_t sw) = 0;
    168 
    169     /* Determine whether physical keys exist for the given framework-domain key codes. */
    170     virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
    171             size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
    172 };
    173 
    174 
    175 /* Internal interface used by individual input devices to access global input device state
    176  * and parameters maintained by the input reader.
    177  */
    178 class InputReaderContext {
    179 public:
    180     InputReaderContext() { }
    181     virtual ~InputReaderContext() { }
    182 
    183     virtual void updateGlobalMetaState() = 0;
    184     virtual int32_t getGlobalMetaState() = 0;
    185 
    186     virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
    187     virtual bool shouldDropVirtualKey(nsecs_t now,
    188             InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
    189 
    190     virtual InputReaderPolicyInterface* getPolicy() = 0;
    191     virtual InputDispatcherInterface* getDispatcher() = 0;
    192     virtual EventHubInterface* getEventHub() = 0;
    193 };
    194 
    195 
    196 /* The input reader reads raw event data from the event hub and processes it into input events
    197  * that it sends to the input dispatcher.  Some functions of the input reader, such as early
    198  * event filtering in low power states, are controlled by a separate policy object.
    199  *
    200  * IMPORTANT INVARIANT:
    201  *     Because the policy and dispatcher can potentially block or cause re-entrance into
    202  *     the input reader, the input reader never calls into other components while holding
    203  *     an exclusive internal lock whenever re-entrance can happen.
    204  */
    205 class InputReader : public InputReaderInterface, protected InputReaderContext {
    206 public:
    207     InputReader(const sp<EventHubInterface>& eventHub,
    208             const sp<InputReaderPolicyInterface>& policy,
    209             const sp<InputDispatcherInterface>& dispatcher);
    210     virtual ~InputReader();
    211 
    212     virtual void dump(String8& dump);
    213 
    214     virtual void loopOnce();
    215 
    216     virtual void getInputConfiguration(InputConfiguration* outConfiguration);
    217 
    218     virtual status_t getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo);
    219     virtual void getInputDeviceIds(Vector<int32_t>& outDeviceIds);
    220 
    221     virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
    222             int32_t scanCode);
    223     virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
    224             int32_t keyCode);
    225     virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
    226             int32_t sw);
    227 
    228     virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
    229             size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
    230 
    231 protected:
    232     // These methods are protected virtual so they can be overridden and instrumented
    233     // by test cases.
    234     virtual InputDevice* createDevice(int32_t deviceId, const String8& name, uint32_t classes);
    235 
    236 private:
    237     sp<EventHubInterface> mEventHub;
    238     sp<InputReaderPolicyInterface> mPolicy;
    239     sp<InputDispatcherInterface> mDispatcher;
    240 
    241     virtual InputReaderPolicyInterface* getPolicy() { return mPolicy.get(); }
    242     virtual InputDispatcherInterface* getDispatcher() { return mDispatcher.get(); }
    243     virtual EventHubInterface* getEventHub() { return mEventHub.get(); }
    244 
    245     // This reader/writer lock guards the list of input devices.
    246     // The writer lock must be held whenever the list of input devices is modified
    247     //   and then promptly released.
    248     // The reader lock must be held whenever the list of input devices is traversed or an
    249     //   input device in the list is accessed.
    250     // This lock only protects the registry and prevents inadvertent deletion of device objects
    251     // that are in use.  Individual devices are responsible for guarding their own internal state
    252     // as needed for concurrent operation.
    253     RWLock mDeviceRegistryLock;
    254     KeyedVector<int32_t, InputDevice*> mDevices;
    255 
    256     // low-level input event decoding and device management
    257     void process(const RawEvent* rawEvent);
    258 
    259     void addDevice(int32_t deviceId);
    260     void removeDevice(int32_t deviceId);
    261     void configureExcludedDevices();
    262 
    263     void consumeEvent(const RawEvent* rawEvent);
    264 
    265     void handleConfigurationChanged(nsecs_t when);
    266 
    267     // state management for all devices
    268     Mutex mStateLock;
    269 
    270     int32_t mGlobalMetaState;
    271     virtual void updateGlobalMetaState();
    272     virtual int32_t getGlobalMetaState();
    273 
    274     InputConfiguration mInputConfiguration;
    275     void updateInputConfiguration();
    276 
    277     nsecs_t mDisableVirtualKeysTimeout;
    278     virtual void disableVirtualKeysUntil(nsecs_t time);
    279     virtual bool shouldDropVirtualKey(nsecs_t now,
    280             InputDevice* device, int32_t keyCode, int32_t scanCode);
    281 
    282     // state queries
    283     typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
    284     int32_t getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
    285             GetStateFunc getStateFunc);
    286     bool markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
    287             const int32_t* keyCodes, uint8_t* outFlags);
    288 };
    289 
    290 
    291 /* Reads raw events from the event hub and processes them, endlessly. */
    292 class InputReaderThread : public Thread {
    293 public:
    294     InputReaderThread(const sp<InputReaderInterface>& reader);
    295     virtual ~InputReaderThread();
    296 
    297 private:
    298     sp<InputReaderInterface> mReader;
    299 
    300     virtual bool threadLoop();
    301 };
    302 
    303 
    304 /* Represents the state of a single input device. */
    305 class InputDevice {
    306 public:
    307     InputDevice(InputReaderContext* context, int32_t id, const String8& name);
    308     ~InputDevice();
    309 
    310     inline InputReaderContext* getContext() { return mContext; }
    311     inline int32_t getId() { return mId; }
    312     inline const String8& getName() { return mName; }
    313     inline uint32_t getSources() { return mSources; }
    314 
    315     inline bool isIgnored() { return mMappers.isEmpty(); }
    316 
    317     void dump(String8& dump);
    318     void addMapper(InputMapper* mapper);
    319     void configure();
    320     void reset();
    321     void process(const RawEvent* rawEvent);
    322 
    323     void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
    324     int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
    325     int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
    326     int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
    327     bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
    328             const int32_t* keyCodes, uint8_t* outFlags);
    329 
    330     int32_t getMetaState();
    331 
    332     inline const InputDeviceCalibration& getCalibration() {
    333         return mCalibration;
    334     }
    335 
    336 private:
    337     InputReaderContext* mContext;
    338     int32_t mId;
    339 
    340     Vector<InputMapper*> mMappers;
    341 
    342     String8 mName;
    343     uint32_t mSources;
    344 
    345     typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
    346     int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
    347 
    348     InputDeviceCalibration mCalibration;
    349 };
    350 
    351 
    352 /* An input mapper transforms raw input events into cooked event data.
    353  * A single input device can have multiple associated input mappers in order to interpret
    354  * different classes of events.
    355  */
    356 class InputMapper {
    357 public:
    358     InputMapper(InputDevice* device);
    359     virtual ~InputMapper();
    360 
    361     inline InputDevice* getDevice() { return mDevice; }
    362     inline int32_t getDeviceId() { return mDevice->getId(); }
    363     inline const String8 getDeviceName() { return mDevice->getName(); }
    364     inline InputReaderContext* getContext() { return mContext; }
    365     inline InputReaderPolicyInterface* getPolicy() { return mContext->getPolicy(); }
    366     inline InputDispatcherInterface* getDispatcher() { return mContext->getDispatcher(); }
    367     inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
    368 
    369     virtual uint32_t getSources() = 0;
    370     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
    371     virtual void dump(String8& dump);
    372     virtual void configure();
    373     virtual void reset();
    374     virtual void process(const RawEvent* rawEvent) = 0;
    375 
    376     virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
    377     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
    378     virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
    379     virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
    380             const int32_t* keyCodes, uint8_t* outFlags);
    381 
    382     virtual int32_t getMetaState();
    383 
    384 protected:
    385     InputDevice* mDevice;
    386     InputReaderContext* mContext;
    387 };
    388 
    389 
    390 class SwitchInputMapper : public InputMapper {
    391 public:
    392     SwitchInputMapper(InputDevice* device);
    393     virtual ~SwitchInputMapper();
    394 
    395     virtual uint32_t getSources();
    396     virtual void process(const RawEvent* rawEvent);
    397 
    398     virtual int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
    399 
    400 private:
    401     void processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue);
    402 };
    403 
    404 
    405 class KeyboardInputMapper : public InputMapper {
    406 public:
    407     KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId, uint32_t sources,
    408             int32_t keyboardType);
    409     virtual ~KeyboardInputMapper();
    410 
    411     virtual uint32_t getSources();
    412     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
    413     virtual void dump(String8& dump);
    414     virtual void reset();
    415     virtual void process(const RawEvent* rawEvent);
    416 
    417     virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
    418     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
    419     virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
    420             const int32_t* keyCodes, uint8_t* outFlags);
    421 
    422     virtual int32_t getMetaState();
    423 
    424 private:
    425     Mutex mLock;
    426 
    427     struct KeyDown {
    428         int32_t keyCode;
    429         int32_t scanCode;
    430     };
    431 
    432     int32_t mAssociatedDisplayId;
    433     uint32_t mSources;
    434     int32_t mKeyboardType;
    435 
    436     struct LockedState {
    437         Vector<KeyDown> keyDowns; // keys that are down
    438         int32_t metaState;
    439         nsecs_t downTime; // time of most recent key down
    440     } mLocked;
    441 
    442     void initializeLocked();
    443 
    444     bool isKeyboardOrGamepadKey(int32_t scanCode);
    445 
    446     void processKey(nsecs_t when, bool down, int32_t keyCode, int32_t scanCode,
    447             uint32_t policyFlags);
    448 
    449     ssize_t findKeyDownLocked(int32_t scanCode);
    450 };
    451 
    452 
    453 class TrackballInputMapper : public InputMapper {
    454 public:
    455     TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId);
    456     virtual ~TrackballInputMapper();
    457 
    458     virtual uint32_t getSources();
    459     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
    460     virtual void dump(String8& dump);
    461     virtual void reset();
    462     virtual void process(const RawEvent* rawEvent);
    463 
    464     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
    465 
    466 private:
    467     // Amount that trackball needs to move in order to generate a key event.
    468     static const int32_t TRACKBALL_MOVEMENT_THRESHOLD = 6;
    469 
    470     Mutex mLock;
    471 
    472     int32_t mAssociatedDisplayId;
    473 
    474     struct Accumulator {
    475         enum {
    476             FIELD_BTN_MOUSE = 1,
    477             FIELD_REL_X = 2,
    478             FIELD_REL_Y = 4
    479         };
    480 
    481         uint32_t fields;
    482 
    483         bool btnMouse;
    484         int32_t relX;
    485         int32_t relY;
    486 
    487         inline void clear() {
    488             fields = 0;
    489         }
    490     } mAccumulator;
    491 
    492     float mXScale;
    493     float mYScale;
    494     float mXPrecision;
    495     float mYPrecision;
    496 
    497     struct LockedState {
    498         bool down;
    499         nsecs_t downTime;
    500     } mLocked;
    501 
    502     void initializeLocked();
    503 
    504     void sync(nsecs_t when);
    505 };
    506 
    507 
    508 class TouchInputMapper : public InputMapper {
    509 public:
    510     TouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
    511     virtual ~TouchInputMapper();
    512 
    513     virtual uint32_t getSources();
    514     virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo);
    515     virtual void dump(String8& dump);
    516     virtual void configure();
    517     virtual void reset();
    518 
    519     virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
    520     virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
    521     virtual bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
    522             const int32_t* keyCodes, uint8_t* outFlags);
    523 
    524 protected:
    525     Mutex mLock;
    526 
    527     struct VirtualKey {
    528         int32_t keyCode;
    529         int32_t scanCode;
    530         uint32_t flags;
    531 
    532         // computed hit box, specified in touch screen coords based on known display size
    533         int32_t hitLeft;
    534         int32_t hitTop;
    535         int32_t hitRight;
    536         int32_t hitBottom;
    537 
    538         inline bool isHit(int32_t x, int32_t y) const {
    539             return x >= hitLeft && x <= hitRight && y >= hitTop && y <= hitBottom;
    540         }
    541     };
    542 
    543     // Raw data for a single pointer.
    544     struct PointerData {
    545         uint32_t id;
    546         int32_t x;
    547         int32_t y;
    548         int32_t pressure;
    549         int32_t touchMajor;
    550         int32_t touchMinor;
    551         int32_t toolMajor;
    552         int32_t toolMinor;
    553         int32_t orientation;
    554 
    555         inline bool operator== (const PointerData& other) const {
    556             return id == other.id
    557                     && x == other.x
    558                     && y == other.y
    559                     && pressure == other.pressure
    560                     && touchMajor == other.touchMajor
    561                     && touchMinor == other.touchMinor
    562                     && toolMajor == other.toolMajor
    563                     && toolMinor == other.toolMinor
    564                     && orientation == other.orientation;
    565         }
    566         inline bool operator!= (const PointerData& other) const {
    567             return !(*this == other);
    568         }
    569     };
    570 
    571     // Raw data for a collection of pointers including a pointer id mapping table.
    572     struct TouchData {
    573         uint32_t pointerCount;
    574         PointerData pointers[MAX_POINTERS];
    575         BitSet32 idBits;
    576         uint32_t idToIndex[MAX_POINTER_ID + 1];
    577 
    578         void copyFrom(const TouchData& other) {
    579             pointerCount = other.pointerCount;
    580             idBits = other.idBits;
    581 
    582             for (uint32_t i = 0; i < pointerCount; i++) {
    583                 pointers[i] = other.pointers[i];
    584 
    585                 int id = pointers[i].id;
    586                 idToIndex[id] = other.idToIndex[id];
    587             }
    588         }
    589 
    590         inline void clear() {
    591             pointerCount = 0;
    592             idBits.clear();
    593         }
    594     };
    595 
    596     int32_t mAssociatedDisplayId;
    597 
    598     // Immutable configuration parameters.
    599     struct Parameters {
    600         bool useBadTouchFilter;
    601         bool useJumpyTouchFilter;
    602         bool useAveragingTouchFilter;
    603         nsecs_t virtualKeyQuietTime;
    604     } mParameters;
    605 
    606     // Immutable calibration parameters in parsed form.
    607     struct Calibration {
    608         // Touch Size
    609         enum TouchSizeCalibration {
    610             TOUCH_SIZE_CALIBRATION_DEFAULT,
    611             TOUCH_SIZE_CALIBRATION_NONE,
    612             TOUCH_SIZE_CALIBRATION_GEOMETRIC,
    613             TOUCH_SIZE_CALIBRATION_PRESSURE,
    614         };
    615 
    616         TouchSizeCalibration touchSizeCalibration;
    617 
    618         // Tool Size
    619         enum ToolSizeCalibration {
    620             TOOL_SIZE_CALIBRATION_DEFAULT,
    621             TOOL_SIZE_CALIBRATION_NONE,
    622             TOOL_SIZE_CALIBRATION_GEOMETRIC,
    623             TOOL_SIZE_CALIBRATION_LINEAR,
    624             TOOL_SIZE_CALIBRATION_AREA,
    625         };
    626 
    627         ToolSizeCalibration toolSizeCalibration;
    628         bool haveToolSizeLinearScale;
    629         float toolSizeLinearScale;
    630         bool haveToolSizeLinearBias;
    631         float toolSizeLinearBias;
    632         bool haveToolSizeAreaScale;
    633         float toolSizeAreaScale;
    634         bool haveToolSizeAreaBias;
    635         float toolSizeAreaBias;
    636         bool haveToolSizeIsSummed;
    637         int32_t toolSizeIsSummed;
    638 
    639         // Pressure
    640         enum PressureCalibration {
    641             PRESSURE_CALIBRATION_DEFAULT,
    642             PRESSURE_CALIBRATION_NONE,
    643             PRESSURE_CALIBRATION_PHYSICAL,
    644             PRESSURE_CALIBRATION_AMPLITUDE,
    645         };
    646         enum PressureSource {
    647             PRESSURE_SOURCE_DEFAULT,
    648             PRESSURE_SOURCE_PRESSURE,
    649             PRESSURE_SOURCE_TOUCH,
    650         };
    651 
    652         PressureCalibration pressureCalibration;
    653         PressureSource pressureSource;
    654         bool havePressureScale;
    655         float pressureScale;
    656 
    657         // Size
    658         enum SizeCalibration {
    659             SIZE_CALIBRATION_DEFAULT,
    660             SIZE_CALIBRATION_NONE,
    661             SIZE_CALIBRATION_NORMALIZED,
    662         };
    663 
    664         SizeCalibration sizeCalibration;
    665 
    666         // Orientation
    667         enum OrientationCalibration {
    668             ORIENTATION_CALIBRATION_DEFAULT,
    669             ORIENTATION_CALIBRATION_NONE,
    670             ORIENTATION_CALIBRATION_INTERPOLATED,
    671         };
    672 
    673         OrientationCalibration orientationCalibration;
    674     } mCalibration;
    675 
    676     // Raw axis information from the driver.
    677     struct RawAxes {
    678         RawAbsoluteAxisInfo x;
    679         RawAbsoluteAxisInfo y;
    680         RawAbsoluteAxisInfo pressure;
    681         RawAbsoluteAxisInfo touchMajor;
    682         RawAbsoluteAxisInfo touchMinor;
    683         RawAbsoluteAxisInfo toolMajor;
    684         RawAbsoluteAxisInfo toolMinor;
    685         RawAbsoluteAxisInfo orientation;
    686     } mRawAxes;
    687 
    688     // Current and previous touch sample data.
    689     TouchData mCurrentTouch;
    690     TouchData mLastTouch;
    691 
    692     // The time the primary pointer last went down.
    693     nsecs_t mDownTime;
    694 
    695     struct LockedState {
    696         Vector<VirtualKey> virtualKeys;
    697 
    698         // The surface orientation and width and height set by configureSurfaceLocked().
    699         int32_t surfaceOrientation;
    700         int32_t surfaceWidth, surfaceHeight;
    701 
    702         // Translation and scaling factors, orientation-independent.
    703         int32_t xOrigin;
    704         float xScale;
    705         float xPrecision;
    706 
    707         int32_t yOrigin;
    708         float yScale;
    709         float yPrecision;
    710 
    711         float geometricScale;
    712 
    713         float toolSizeLinearScale;
    714         float toolSizeLinearBias;
    715         float toolSizeAreaScale;
    716         float toolSizeAreaBias;
    717 
    718         float pressureScale;
    719 
    720         float sizeScale;
    721 
    722         float orientationScale;
    723 
    724         // Oriented motion ranges for input device info.
    725         struct OrientedRanges {
    726             InputDeviceInfo::MotionRange x;
    727             InputDeviceInfo::MotionRange y;
    728 
    729             bool havePressure;
    730             InputDeviceInfo::MotionRange pressure;
    731 
    732             bool haveSize;
    733             InputDeviceInfo::MotionRange size;
    734 
    735             bool haveTouchSize;
    736             InputDeviceInfo::MotionRange touchMajor;
    737             InputDeviceInfo::MotionRange touchMinor;
    738 
    739             bool haveToolSize;
    740             InputDeviceInfo::MotionRange toolMajor;
    741             InputDeviceInfo::MotionRange toolMinor;
    742 
    743             bool haveOrientation;
    744             InputDeviceInfo::MotionRange orientation;
    745         } orientedRanges;
    746 
    747         // Oriented dimensions and precision.
    748         float orientedSurfaceWidth, orientedSurfaceHeight;
    749         float orientedXPrecision, orientedYPrecision;
    750 
    751         struct CurrentVirtualKeyState {
    752             bool down;
    753             nsecs_t downTime;
    754             int32_t keyCode;
    755             int32_t scanCode;
    756         } currentVirtualKey;
    757     } mLocked;
    758 
    759     virtual void configureParameters();
    760     virtual void dumpParameters(String8& dump);
    761     virtual void configureRawAxes();
    762     virtual void dumpRawAxes(String8& dump);
    763     virtual bool configureSurfaceLocked();
    764     virtual void dumpSurfaceLocked(String8& dump);
    765     virtual void configureVirtualKeysLocked();
    766     virtual void dumpVirtualKeysLocked(String8& dump);
    767     virtual void parseCalibration();
    768     virtual void resolveCalibration();
    769     virtual void dumpCalibration(String8& dump);
    770 
    771     enum TouchResult {
    772         // Dispatch the touch normally.
    773         DISPATCH_TOUCH,
    774         // Do not dispatch the touch, but keep tracking the current stroke.
    775         SKIP_TOUCH,
    776         // Do not dispatch the touch, and drop all information associated with the current stoke
    777         // so the next movement will appear as a new down.
    778         DROP_STROKE
    779     };
    780 
    781     void syncTouch(nsecs_t when, bool havePointerIds);
    782 
    783 private:
    784     /* Maximum number of historical samples to average. */
    785     static const uint32_t AVERAGING_HISTORY_SIZE = 5;
    786 
    787     /* Slop distance for jumpy pointer detection.
    788      * The vertical range of the screen divided by this is our epsilon value. */
    789     static const uint32_t JUMPY_EPSILON_DIVISOR = 212;
    790 
    791     /* Number of jumpy points to drop for touchscreens that need it. */
    792     static const uint32_t JUMPY_TRANSITION_DROPS = 3;
    793     static const uint32_t JUMPY_DROP_LIMIT = 3;
    794 
    795     /* Maximum squared distance for averaging.
    796      * If moving farther than this, turn of averaging to avoid lag in response. */
    797     static const uint64_t AVERAGING_DISTANCE_LIMIT = 75 * 75;
    798 
    799     struct AveragingTouchFilterState {
    800         // Individual history tracks are stored by pointer id
    801         uint32_t historyStart[MAX_POINTERS];
    802         uint32_t historyEnd[MAX_POINTERS];
    803         struct {
    804             struct {
    805                 int32_t x;
    806                 int32_t y;
    807                 int32_t pressure;
    808             } pointers[MAX_POINTERS];
    809         } historyData[AVERAGING_HISTORY_SIZE];
    810     } mAveragingTouchFilter;
    811 
    812     struct JumpyTouchFilterState {
    813         uint32_t jumpyPointsDropped;
    814     } mJumpyTouchFilter;
    815 
    816     struct PointerDistanceHeapElement {
    817         uint32_t currentPointerIndex : 8;
    818         uint32_t lastPointerIndex : 8;
    819         uint64_t distance : 48; // squared distance
    820     };
    821 
    822     void initializeLocked();
    823 
    824     TouchResult consumeOffScreenTouches(nsecs_t when, uint32_t policyFlags);
    825     void dispatchTouches(nsecs_t when, uint32_t policyFlags);
    826     void dispatchTouch(nsecs_t when, uint32_t policyFlags, TouchData* touch,
    827             BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
    828             int32_t motionEventAction);
    829     void detectGestures(nsecs_t when);
    830 
    831     bool isPointInsideSurfaceLocked(int32_t x, int32_t y);
    832     const VirtualKey* findVirtualKeyHitLocked(int32_t x, int32_t y);
    833 
    834     bool applyBadTouchFilter();
    835     bool applyJumpyTouchFilter();
    836     void applyAveragingTouchFilter();
    837     void calculatePointerIds();
    838 };
    839 
    840 
    841 class SingleTouchInputMapper : public TouchInputMapper {
    842 public:
    843     SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
    844     virtual ~SingleTouchInputMapper();
    845 
    846     virtual void reset();
    847     virtual void process(const RawEvent* rawEvent);
    848 
    849 protected:
    850     virtual void configureRawAxes();
    851 
    852 private:
    853     struct Accumulator {
    854         enum {
    855             FIELD_BTN_TOUCH = 1,
    856             FIELD_ABS_X = 2,
    857             FIELD_ABS_Y = 4,
    858             FIELD_ABS_PRESSURE = 8,
    859             FIELD_ABS_TOOL_WIDTH = 16
    860         };
    861 
    862         uint32_t fields;
    863 
    864         bool btnTouch;
    865         int32_t absX;
    866         int32_t absY;
    867         int32_t absPressure;
    868         int32_t absToolWidth;
    869 
    870         inline void clear() {
    871             fields = 0;
    872         }
    873     } mAccumulator;
    874 
    875     bool mDown;
    876     int32_t mX;
    877     int32_t mY;
    878     int32_t mPressure;
    879     int32_t mToolWidth;
    880 
    881     void initialize();
    882 
    883     void sync(nsecs_t when);
    884 };
    885 
    886 
    887 class MultiTouchInputMapper : public TouchInputMapper {
    888 public:
    889     MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId);
    890     virtual ~MultiTouchInputMapper();
    891 
    892     virtual void reset();
    893     virtual void process(const RawEvent* rawEvent);
    894 
    895 protected:
    896     virtual void configureRawAxes();
    897 
    898 private:
    899     struct Accumulator {
    900         enum {
    901             FIELD_ABS_MT_POSITION_X = 1,
    902             FIELD_ABS_MT_POSITION_Y = 2,
    903             FIELD_ABS_MT_TOUCH_MAJOR = 4,
    904             FIELD_ABS_MT_TOUCH_MINOR = 8,
    905             FIELD_ABS_MT_WIDTH_MAJOR = 16,
    906             FIELD_ABS_MT_WIDTH_MINOR = 32,
    907             FIELD_ABS_MT_ORIENTATION = 64,
    908             FIELD_ABS_MT_TRACKING_ID = 128,
    909             FIELD_ABS_MT_PRESSURE = 256,
    910         };
    911 
    912         uint32_t pointerCount;
    913         struct Pointer {
    914             uint32_t fields;
    915 
    916             int32_t absMTPositionX;
    917             int32_t absMTPositionY;
    918             int32_t absMTTouchMajor;
    919             int32_t absMTTouchMinor;
    920             int32_t absMTWidthMajor;
    921             int32_t absMTWidthMinor;
    922             int32_t absMTOrientation;
    923             int32_t absMTTrackingId;
    924             int32_t absMTPressure;
    925 
    926             inline void clear() {
    927                 fields = 0;
    928             }
    929         } pointers[MAX_POINTERS + 1]; // + 1 to remove the need for extra range checks
    930 
    931         inline void clear() {
    932             pointerCount = 0;
    933             pointers[0].clear();
    934         }
    935     } mAccumulator;
    936 
    937     void initialize();
    938 
    939     void sync(nsecs_t when);
    940 };
    941 
    942 } // namespace android
    943 
    944 #endif // _UI_INPUT_READER_H
    945