1d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/*
2d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Copyright (C) 2010 The Android Open Source Project
3d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
4d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Licensed under the Apache License, Version 2.0 (the "License");
5d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * you may not use this file except in compliance with the License.
6d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * You may obtain a copy of the License at
7d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
8d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *      http://www.apache.org/licenses/LICENSE-2.0
9d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
10d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Unless required by applicable law or agreed to in writing, software
11d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * distributed under the License is distributed on an "AS IS" BASIS,
12d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * See the License for the specific language governing permissions and
14d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * limitations under the License.
15d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright */
16d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
17d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#ifndef _UI_INPUT_READER_H
18d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define _UI_INPUT_READER_H
19d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
20d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include "EventHub.h"
21d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include "PointerControllerInterface.h"
22d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include "InputListener.h"
23d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
24d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <input/Input.h>
25d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <input/VelocityControl.h>
26d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <input/VelocityTracker.h>
27d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <ui/DisplayInfo.h>
28d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <utils/KeyedVector.h>
29d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <utils/threads.h>
30d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <utils/Timers.h>
31d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <utils/RefBase.h>
32d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <utils/String8.h>
33d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <utils/BitSet.h>
34d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
35d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <stddef.h>
36d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#include <unistd.h>
37d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
38d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Maximum supported size of a vibration pattern.
39d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Must be at least 2.
40d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define MAX_VIBRATE_PATTERN_SIZE 100
41d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
42d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// Maximum allowable delay value in a vibration pattern before
43d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright// which the delay will be truncated.
44d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright#define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL)
45d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
46d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightnamespace android {
47d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
48d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass InputDevice;
49d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass InputMapper;
50d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
51d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/*
52d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Describes how coordinates are mapped on a physical display.
53d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * See com.android.server.display.DisplayViewport.
54d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright */
55d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstruct DisplayViewport {
56d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t displayId; // -1 if invalid
57d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t orientation;
58d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t logicalLeft;
59d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t logicalTop;
60d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t logicalRight;
61d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t logicalBottom;
62d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t physicalLeft;
63d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t physicalTop;
64d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t physicalRight;
65d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t physicalBottom;
66d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t deviceWidth;
67d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t deviceHeight;
68d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
69d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    DisplayViewport() :
70d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            displayId(ADISPLAY_ID_NONE), orientation(DISPLAY_ORIENTATION_0),
71d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            logicalLeft(0), logicalTop(0), logicalRight(0), logicalBottom(0),
72d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            physicalLeft(0), physicalTop(0), physicalRight(0), physicalBottom(0),
73d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            deviceWidth(0), deviceHeight(0) {
74d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
75d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
76d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool operator==(const DisplayViewport& other) const {
77d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return displayId == other.displayId
78d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && orientation == other.orientation
79d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && logicalLeft == other.logicalLeft
80d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && logicalTop == other.logicalTop
81d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && logicalRight == other.logicalRight
82d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && logicalBottom == other.logicalBottom
83d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && physicalLeft == other.physicalLeft
84d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && physicalTop == other.physicalTop
85d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && physicalRight == other.physicalRight
86d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && physicalBottom == other.physicalBottom
87d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && deviceWidth == other.deviceWidth
88d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                && deviceHeight == other.deviceHeight;
89d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
90d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
91d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool operator!=(const DisplayViewport& other) const {
92d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return !(*this == other);
93d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
94d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
95d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline bool isValid() const {
96d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return displayId >= 0;
97d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
98d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
99d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void setNonDisplayViewport(int32_t width, int32_t height) {
100d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        displayId = ADISPLAY_ID_NONE;
101d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        orientation = DISPLAY_ORIENTATION_0;
102d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        logicalLeft = 0;
103d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        logicalTop = 0;
104d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        logicalRight = width;
105d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        logicalBottom = height;
106d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        physicalLeft = 0;
107d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        physicalTop = 0;
108d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        physicalRight = width;
109d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        physicalBottom = height;
110d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        deviceWidth = width;
111d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        deviceHeight = height;
112d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
113d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
114d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
115d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/*
116d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Input reader configuration.
117d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
118d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Specifies various options that modify the behavior of the input reader.
119d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright */
120d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightstruct InputReaderConfiguration {
121d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Describes changes that have occurred.
122d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    enum {
123d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // The pointer speed changed.
124d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        CHANGE_POINTER_SPEED = 1 << 0,
125d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
126d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // The pointer gesture control changed.
127d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
128d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
129d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // The display size or orientation changed.
130d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        CHANGE_DISPLAY_INFO = 1 << 2,
131d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
132d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // The visible touches option changed.
133d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        CHANGE_SHOW_TOUCHES = 1 << 3,
134d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
135d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // The keyboard layouts must be reloaded.
136d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
137d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
138d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // The device name alias supplied by the may have changed for some devices.
139d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        CHANGE_DEVICE_ALIAS = 1 << 5,
140d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
14112d6baa9b832f16a28f048ed5ffab75a76ed9c41Jason Gerecke        // The location calibration matrix changed.
14212d6baa9b832f16a28f048ed5ffab75a76ed9c41Jason Gerecke        TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
14312d6baa9b832f16a28f048ed5ffab75a76ed9c41Jason Gerecke
144d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        // All devices must be reopened.
145d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        CHANGE_MUST_REOPEN = 1 << 31,
146d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    };
147d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
148d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Gets the amount of time to disable virtual keys after the screen is touched
149d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // in order to filter out accidental virtual key presses due to swiping gestures
150d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // or taps near the edge of the display.  May be 0 to disable the feature.
151d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t virtualKeyQuietTime;
152d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
153d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The excluded device names for the platform.
154d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Devices with these names will be ignored.
155d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Vector<String8> excludedDeviceNames;
156d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
157d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Velocity control parameters for mouse pointer movements.
158d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    VelocityControlParameters pointerVelocityControlParameters;
159d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
160d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Velocity control parameters for mouse wheel movements.
161d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    VelocityControlParameters wheelVelocityControlParameters;
162d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
163d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // True if pointer gestures are enabled.
164d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool pointerGesturesEnabled;
165d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
166d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Quiet time between certain pointer gesture transitions.
167d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Time to allow for all fingers or buttons to settle into a stable state before
168d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // starting a new gesture.
169d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t pointerGestureQuietInterval;
170d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
171d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The minimum speed that a pointer must travel for us to consider switching the active
172d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // touch pointer to it during a drag.  This threshold is set to avoid switching due
173d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // to noise from a finger resting on the touch pad (perhaps just pressing it down).
174d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    float pointerGestureDragMinSwitchSpeed; // in pixels per second
175d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
176d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Tap gesture delay time.
177d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The time between down and up must be less than this to be considered a tap.
178d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t pointerGestureTapInterval;
179d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
180d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Tap drag gesture delay time.
181d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The time between the previous tap's up and the next down must be less than
182d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // this to be considered a drag.  Otherwise, the previous tap is finished and a
183d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // new tap begins.
184d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    //
185d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Note that the previous tap will be held down for this entire duration so this
186d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // interval must be shorter than the long press timeout.
187d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t pointerGestureTapDragInterval;
188d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
189d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The distance in pixels that the pointer is allowed to move from initial down
190d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // to up and still be called a tap.
191d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    float pointerGestureTapSlop; // in pixels
192d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
193d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Time after the first touch points go down to settle on an initial centroid.
194d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // This is intended to be enough time to handle cases where the user puts down two
195d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // fingers at almost but not quite exactly the same time.
196d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t pointerGestureMultitouchSettleInterval;
197d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
198d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
199d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // at least two pointers have moved at least this far from their starting place.
200d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    float pointerGestureMultitouchMinDistance; // in pixels
201d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
202d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The transition from PRESS to SWIPE gesture mode can only occur when the
203d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // cosine of the angle between the two vectors is greater than or equal to than this value
204d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // which indicates that the vectors are oriented in the same direction.
205d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
206d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // (In exactly opposite directions, the cosine is -1.0.)
207d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    float pointerGestureSwipeTransitionAngleCosine;
208d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
209d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The transition from PRESS to SWIPE gesture mode can only occur when the
210d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // fingers are no more than this far apart relative to the diagonal size of
211d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // the touch pad.  For example, a ratio of 0.5 means that the fingers must be
212d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // no more than half the diagonal size of the touch pad apart.
213d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    float pointerGestureSwipeMaxWidthRatio;
214d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
215d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The gesture movement speed factor relative to the size of the display.
216d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Movement speed applies when the fingers are moving in the same direction.
217d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Without acceleration, a full swipe of the touch pad diagonal in movement mode
218d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // will cover this portion of the display diagonal.
219d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    float pointerGestureMovementSpeedRatio;
220d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
221d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The gesture zoom speed factor relative to the size of the display.
222d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Zoom speed applies when the fingers are mostly moving relative to each other
223d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // to execute a scale gesture or similar.
224d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
225d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // will cover this portion of the display diagonal.
226d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    float pointerGestureZoomSpeedRatio;
227d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
228d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // True to show the location of touches on the touch screen as spots.
229d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool showTouches;
230d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
231d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputReaderConfiguration() :
232d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            virtualKeyQuietTime(0),
233d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
234d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
235d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGesturesEnabled(true),
236d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
237d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
238d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureTapInterval(150 * 1000000LL), // 150 ms
239d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
240d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureTapSlop(10.0f), // 10 pixels
241d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
242d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureMultitouchMinDistance(15), // 15 pixels
243d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
244d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureSwipeMaxWidthRatio(0.25f),
245d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureMovementSpeedRatio(0.8f),
246d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            pointerGestureZoomSpeedRatio(0.3f),
247d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            showTouches(false) { }
248d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
249d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool getDisplayInfo(bool external, DisplayViewport* outViewport) const;
250d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void setDisplayInfo(bool external, const DisplayViewport& viewport);
251d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
252d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
253d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    DisplayViewport mInternalDisplay;
254d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    DisplayViewport mExternalDisplay;
255d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
256d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
257d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
258af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gereckestruct TouchAffineTransformation {
259af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke    float x_scale;
260af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke    float x_ymix;
261af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke    float x_offset;
262af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke    float y_xmix;
263af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke    float y_scale;
264af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke    float y_offset;
265af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke
266af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke    TouchAffineTransformation() :
267af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke        x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
268af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke        y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
269af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke    }
270af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke
271489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke    TouchAffineTransformation(float xscale, float xymix, float xoffset,
272489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke            float yxmix, float yscale, float yoffset) :
273489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke        x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
274489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke        y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
275489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke    }
276489fda8d476f3fc10b0251263cc69528b736257bJason Gerecke
277af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke    void applyTo(float& x, float& y) const;
278af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke};
279af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke
280af126fb538c39d5488d62695b1bfb2990a3ef7dbJason Gerecke
281d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/*
282d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * Input reader policy interface.
283d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
284d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * The input reader policy is used by the input reader to interact with the Window Manager
285d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * and other system components.
286d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
287d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * The actual implementation is partially supported by callbacks into the DVM
288d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * via JNI.  This interface is also mocked in the unit tests.
289d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
290d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * These methods must NOT re-enter the input reader since they may be called while
291d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * holding the input reader lock.
292d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright */
293d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass InputReaderPolicyInterface : public virtual RefBase {
294d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprotected:
295d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputReaderPolicyInterface() { }
296d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual ~InputReaderPolicyInterface() { }
297d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
298d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
299d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Gets the input reader configuration. */
300d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
301d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
302d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
303d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
304d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
305d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Notifies the input reader policy that some input devices have changed
306d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     * and provides information about all current input devices.
307d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     */
308d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
309d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
310d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Gets the keyboard layout for a particular input device. */
311d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(
312d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            const InputDeviceIdentifier& identifier) = 0;
313d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
314d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
315d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
31612d6baa9b832f16a28f048ed5ffab75a76ed9c41Jason Gerecke
31712d6baa9b832f16a28f048ed5ffab75a76ed9c41Jason Gerecke    /* Gets the affine calibration associated with the specified device. */
31812d6baa9b832f16a28f048ed5ffab75a76ed9c41Jason Gerecke    virtual TouchAffineTransformation getTouchAffineTransformation(
31971b16e81f9cbf2e288611f32c43ea7fb4a331fcfJason Gerecke            const String8& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
320d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
321d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
322d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
323d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/* Processes raw input events and sends cooked event data to an input listener. */
324d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass InputReaderInterface : public virtual RefBase {
325d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprotected:
326d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputReaderInterface() { }
327d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual ~InputReaderInterface() { }
328d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
329d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
330d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Dumps the state of the input reader.
331d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     *
332d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     * This method may be called on any thread (usually by the input manager). */
333d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void dump(String8& dump) = 0;
334d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
335d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Called by the heatbeat to ensures that the reader has not deadlocked. */
336d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void monitor() = 0;
337d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
338d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Runs a single iteration of the processing loop.
339d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     * Nominally reads and processes one incoming message from the EventHub.
340d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     *
341d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     * This method should be called on the input reader thread.
342d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     */
343d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void loopOnce() = 0;
344d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
345d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Gets information about all input devices.
346d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     *
347d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     * This method may be called on any thread (usually by the input manager).
348d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     */
349d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
350d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
351d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Query current input state. */
352d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
353d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t scanCode) = 0;
354d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
355d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t keyCode) = 0;
356d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
357d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t sw) = 0;
358d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
359d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Determine whether physical keys exist for the given framework-domain key codes. */
360d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
361d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
362d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
363d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Requests that a reconfiguration of all input devices.
364d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     * The changes flag is a bitfield that indicates what has changed and whether
365d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright     * the input devices must all be reopened. */
366d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void requestRefreshConfiguration(uint32_t changes) = 0;
367d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
368d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    /* Controls the vibrator of a particular input device. */
369d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
370d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t repeat, int32_t token) = 0;
371d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
372d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
373d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
374d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
375d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/* Internal interface used by individual input devices to access global input device state
376d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * and parameters maintained by the input reader.
377d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright */
378d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass InputReaderContext {
379d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
380d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputReaderContext() { }
381d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual ~InputReaderContext() { }
382d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
383d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void updateGlobalMetaState() = 0;
384d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getGlobalMetaState() = 0;
385d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
386d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void disableVirtualKeysUntil(nsecs_t time) = 0;
387d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool shouldDropVirtualKey(nsecs_t now,
388d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            InputDevice* device, int32_t keyCode, int32_t scanCode) = 0;
389d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
390d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void fadePointer() = 0;
391d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
392d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void requestTimeoutAtTime(nsecs_t when) = 0;
393d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t bumpGeneration() = 0;
394d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
395d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual InputReaderPolicyInterface* getPolicy() = 0;
396d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual InputListenerInterface* getListener() = 0;
397d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual EventHubInterface* getEventHub() = 0;
398d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
399d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
400d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
401d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/* The input reader reads raw event data from the event hub and processes it into input events
402d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * that it sends to the input listener.  Some functions of the input reader, such as early
403d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * event filtering in low power states, are controlled by a separate policy object.
404d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright *
405d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * The InputReader owns a collection of InputMappers.  Most of the work it does happens
406d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * on the input reader thread but the InputReader can receive queries from other system
407d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * components running on arbitrary threads.  To keep things manageable, the InputReader
408d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * uses a single Mutex to guard its state.  The Mutex may be held while calling into the
409d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * EventHub or the InputReaderPolicy but it is never held while calling into the
410d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright * InputListener.
411d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright */
412d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass InputReader : public InputReaderInterface {
413d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
414d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputReader(const sp<EventHubInterface>& eventHub,
415d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            const sp<InputReaderPolicyInterface>& policy,
416d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            const sp<InputListenerInterface>& listener);
417d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual ~InputReader();
418d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
419d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void dump(String8& dump);
420d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void monitor();
421d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
422d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void loopOnce();
423d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
424d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices);
425d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
426d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
427d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t scanCode);
428d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
429d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t keyCode);
430d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
431d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            int32_t sw);
432d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
433d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
434d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags);
435d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
436d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void requestRefreshConfiguration(uint32_t changes);
437d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
438d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
439d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            ssize_t repeat, int32_t token);
440d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual void cancelVibrate(int32_t deviceId, int32_t token);
441d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
442d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprotected:
443d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // These members are protected so they can be instrumented by test cases.
444d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual InputDevice* createDeviceLocked(int32_t deviceId, int32_t controllerNumber,
445d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            const InputDeviceIdentifier& identifier, uint32_t classes);
446d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
447d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    class ContextImpl : public InputReaderContext {
448d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        InputReader* mReader;
449d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
450d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    public:
451d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        ContextImpl(InputReader* reader);
452d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
453d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual void updateGlobalMetaState();
454d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual int32_t getGlobalMetaState();
455d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual void disableVirtualKeysUntil(nsecs_t time);
456d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual bool shouldDropVirtualKey(nsecs_t now,
457d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright                InputDevice* device, int32_t keyCode, int32_t scanCode);
458d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual void fadePointer();
459d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual void requestTimeoutAtTime(nsecs_t when);
460d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual int32_t bumpGeneration();
461d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual InputReaderPolicyInterface* getPolicy();
462d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual InputListenerInterface* getListener();
463d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        virtual EventHubInterface* getEventHub();
464d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    } mContext;
465d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
466d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    friend class ContextImpl;
467d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
468d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
469d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Mutex mLock;
470d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
471d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Condition mReaderIsAliveCondition;
472d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
473d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    sp<EventHubInterface> mEventHub;
474d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    sp<InputReaderPolicyInterface> mPolicy;
475d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    sp<QueuedInputListener> mQueuedListener;
476d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
477d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputReaderConfiguration mConfig;
478d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
479d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // The event queue.
480d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    static const int EVENT_BUFFER_SIZE = 256;
481d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    RawEvent mEventBuffer[EVENT_BUFFER_SIZE];
482d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
483d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    KeyedVector<int32_t, InputDevice*> mDevices;
484d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
485d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // low-level input event decoding and device management
486d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void processEventsLocked(const RawEvent* rawEvents, size_t count);
487d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
488d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addDeviceLocked(nsecs_t when, int32_t deviceId);
489d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void removeDeviceLocked(nsecs_t when, int32_t deviceId);
490d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void processEventsForDeviceLocked(int32_t deviceId, const RawEvent* rawEvents, size_t count);
491d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void timeoutExpiredLocked(nsecs_t when);
492d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
493d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void handleConfigurationChangedLocked(nsecs_t when);
494d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
495d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mGlobalMetaState;
496d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void updateGlobalMetaStateLocked();
497d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t getGlobalMetaStateLocked();
498d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
499d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void fadePointerLocked();
500d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
501d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mGeneration;
502d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t bumpGenerationLocked();
503d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
504d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices);
505d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
506d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t mDisableVirtualKeysTimeout;
507d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void disableVirtualKeysUntilLocked(nsecs_t time);
508d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool shouldDropVirtualKeyLocked(nsecs_t now,
509d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            InputDevice* device, int32_t keyCode, int32_t scanCode);
510d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
511d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    nsecs_t mNextTimeout;
512d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void requestTimeoutAtTimeLocked(nsecs_t when);
513d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
514d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    uint32_t mConfigurationChangesToRefresh;
515d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void refreshConfigurationLocked(uint32_t changes);
516d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
517d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    // state queries
518d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    typedef int32_t (InputDevice::*GetStateFunc)(uint32_t sourceMask, int32_t code);
519d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
520d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            GetStateFunc getStateFunc);
521d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
522d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            const int32_t* keyCodes, uint8_t* outFlags);
523d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
524d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
525d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
526d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/* Reads raw events from the event hub and processes them, endlessly. */
527d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass InputReaderThread : public Thread {
528d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
529d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputReaderThread(const sp<InputReaderInterface>& reader);
530d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual ~InputReaderThread();
531d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
532d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
533d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    sp<InputReaderInterface> mReader;
534d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
535d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    virtual bool threadLoop();
536d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
537d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
538d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
539d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/* Represents the state of a single input device. */
540d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass InputDevice {
541d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
542d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputDevice(InputReaderContext* context, int32_t id, int32_t generation, int32_t
543d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            controllerNumber, const InputDeviceIdentifier& identifier, uint32_t classes);
544d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    ~InputDevice();
545d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
546d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline InputReaderContext* getContext() { return mContext; }
547d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline int32_t getId() const { return mId; }
548d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline int32_t getControllerNumber() const { return mControllerNumber; }
549d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline int32_t getGeneration() const { return mGeneration; }
550d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline const String8& getName() const { return mIdentifier.name; }
55112d6baa9b832f16a28f048ed5ffab75a76ed9c41Jason Gerecke    inline const String8& getDescriptor() { return mIdentifier.descriptor; }
552d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline uint32_t getClasses() const { return mClasses; }
553d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline uint32_t getSources() const { return mSources; }
554d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
555d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline bool isExternal() { return mIsExternal; }
556d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline void setExternal(bool external) { mIsExternal = external; }
557d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
558d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline bool isIgnored() { return mMappers.isEmpty(); }
559d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
560d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void dump(String8& dump);
561d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void addMapper(InputMapper* mapper);
562d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes);
563d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void reset(nsecs_t when);
564d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void process(const RawEvent* rawEvents, size_t count);
565d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void timeoutExpired(nsecs_t when);
566d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
567d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void getDeviceInfo(InputDeviceInfo* outDeviceInfo);
568d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode);
569d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode);
570d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t getSwitchState(uint32_t sourceMask, int32_t switchCode);
571d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
572d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright            const int32_t* keyCodes, uint8_t* outFlags);
573d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, int32_t token);
574d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void cancelVibrate(int32_t token);
575d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
576d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t getMetaState();
577d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
578d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void fadePointer();
579d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
580d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void bumpGeneration();
581d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
582d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void notifyReset(nsecs_t when);
583d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
584d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline const PropertyMap& getConfiguration() { return mConfiguration; }
585d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline EventHubInterface* getEventHub() { return mContext->getEventHub(); }
586d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
587d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool hasKey(int32_t code) {
588d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return getEventHub()->hasScanCode(mId, code);
589d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
590d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
591d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool hasAbsoluteAxis(int32_t code) {
592d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        RawAbsoluteAxisInfo info;
593d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        getEventHub()->getAbsoluteAxisInfo(mId, code, &info);
594d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return info.valid;
595d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
596d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
597d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool isKeyPressed(int32_t code) {
598d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return getEventHub()->getScanCodeState(mId, code) == AKEY_STATE_DOWN;
599d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
600d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
601d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t getAbsoluteAxisValue(int32_t code) {
602d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        int32_t value;
603d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        getEventHub()->getAbsoluteAxisValue(mId, code, &value);
604d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright        return value;
605d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    }
606d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
607d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
608d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputReaderContext* mContext;
609d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mId;
610d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mGeneration;
611d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mControllerNumber;
612d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    InputDeviceIdentifier mIdentifier;
613d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    String8 mAlias;
614d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    uint32_t mClasses;
615d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
616d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    Vector<InputMapper*> mMappers;
617d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
618d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    uint32_t mSources;
619d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mIsExternal;
620d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mDropUntilNextSync;
621d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
622d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    typedef int32_t (InputMapper::*GetStateFunc)(uint32_t sourceMask, int32_t code);
623d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc);
624d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
625d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    PropertyMap mConfiguration;
626d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
627d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
628d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
629d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/* Keeps track of the state of mouse or touch pad buttons. */
630d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass CursorButtonAccumulator {
631d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
632d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    CursorButtonAccumulator();
633d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void reset(InputDevice* device);
634d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
635d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void process(const RawEvent* rawEvent);
636d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
637d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    uint32_t getButtonState() const;
638d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
639d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
640d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mBtnLeft;
641d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mBtnRight;
642d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mBtnMiddle;
643d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mBtnBack;
644d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mBtnSide;
645d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mBtnForward;
646d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mBtnExtra;
647d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mBtnTask;
648d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
649d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void clearButtons();
650d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
651d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
652d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
653d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/* Keeps track of cursor movements. */
654d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
655d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass CursorMotionAccumulator {
656d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
657d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    CursorMotionAccumulator();
658d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void reset(InputDevice* device);
659d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
660d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void process(const RawEvent* rawEvent);
661d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void finishSync();
662d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
663d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline int32_t getRelativeX() const { return mRelX; }
664d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline int32_t getRelativeY() const { return mRelY; }
665d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
666d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
667d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mRelX;
668d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mRelY;
669d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
670d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void clearRelativeAxes();
671d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
672d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
673d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
674d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/* Keeps track of cursor scrolling motions. */
675d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
676d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass CursorScrollAccumulator {
677d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
678d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    CursorScrollAccumulator();
679d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void configure(InputDevice* device);
680d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void reset(InputDevice* device);
681d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
682d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void process(const RawEvent* rawEvent);
683d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void finishSync();
684d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
685d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline bool haveRelativeVWheel() const { return mHaveRelWheel; }
686d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline bool haveRelativeHWheel() const { return mHaveRelHWheel; }
687d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
688d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline int32_t getRelativeX() const { return mRelX; }
689d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline int32_t getRelativeY() const { return mRelY; }
690d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline int32_t getRelativeVWheel() const { return mRelWheel; }
691d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    inline int32_t getRelativeHWheel() const { return mRelHWheel; }
692d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
693d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
694d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mHaveRelWheel;
695d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mHaveRelHWheel;
696d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
697d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mRelX;
698d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mRelY;
699d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mRelWheel;
700d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t mRelHWheel;
701d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
702d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void clearRelativeAxes();
703d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright};
704d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
705d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
706d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright/* Keeps track of the state of touch, stylus and tool buttons. */
707d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightclass TouchButtonAccumulator {
708d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightpublic:
709d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    TouchButtonAccumulator();
710d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void configure(InputDevice* device);
711d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void reset(InputDevice* device);
712d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
713d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    void process(const RawEvent* rawEvent);
714d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
715d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    uint32_t getButtonState() const;
716d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    int32_t getToolType() const;
717d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool isToolActive() const;
718d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool isHovering() const;
719d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool hasStylus() const;
720d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright
721d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wrightprivate:
722d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mHaveBtnTouch;
723d02c5b6aace05d9fd938e2d03705ac4f60f8da19Michael Wright    bool mHaveStylus;
724