blob: fff8480f578168aab35d64d75a5a05239f3d7572 [file] [log] [blame]
Prabir Pradhan29c95332018-11-14 20:14:11 -08001/*
2 * Copyright (C) 2018 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_BASE_H
18#define _UI_INPUT_READER_BASE_H
19
20#include "PointerControllerInterface.h"
21
22#include <input/Input.h>
23#include <input/InputDevice.h>
24#include <input/DisplayViewport.h>
25#include <input/VelocityControl.h>
26#include <input/VelocityTracker.h>
27#include <utils/KeyedVector.h>
28#include <utils/Thread.h>
29#include <utils/RefBase.h>
30#include <utils/SortedVector.h>
31
32#include <optional>
33#include <stddef.h>
34#include <unistd.h>
35#include <vector>
36
37// Maximum supported size of a vibration pattern.
38// Must be at least 2.
39#define MAX_VIBRATE_PATTERN_SIZE 100
40
41// Maximum allowable delay value in a vibration pattern before
42// which the delay will be truncated.
43#define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL)
44
45namespace android {
46
47/* Processes raw input events and sends cooked event data to an input listener. */
48class InputReaderInterface : public virtual RefBase {
49protected:
50 InputReaderInterface() { }
51 virtual ~InputReaderInterface() { }
52
53public:
54 /* Dumps the state of the input reader.
55 *
56 * This method may be called on any thread (usually by the input manager). */
57 virtual void dump(std::string& dump) = 0;
58
59 /* Called by the heatbeat to ensures that the reader has not deadlocked. */
60 virtual void monitor() = 0;
61
62 /* Returns true if the input device is enabled. */
63 virtual bool isInputDeviceEnabled(int32_t deviceId) = 0;
64
65 /* Runs a single iteration of the processing loop.
66 * Nominally reads and processes one incoming message from the EventHub.
67 *
68 * This method should be called on the input reader thread.
69 */
70 virtual void loopOnce() = 0;
71
72 /* Gets information about all input devices.
73 *
74 * This method may be called on any thread (usually by the input manager).
75 */
76 virtual void getInputDevices(Vector<InputDeviceInfo>& outInputDevices) = 0;
77
78 /* Query current input state. */
79 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
80 int32_t scanCode) = 0;
81 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
82 int32_t keyCode) = 0;
83 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
84 int32_t sw) = 0;
85
86 /* Toggle Caps Lock */
87 virtual void toggleCapsLockState(int32_t deviceId) = 0;
88
89 /* Determine whether physical keys exist for the given framework-domain key codes. */
90 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
91 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
92
93 /* Requests that a reconfiguration of all input devices.
94 * The changes flag is a bitfield that indicates what has changed and whether
95 * the input devices must all be reopened. */
96 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
97
98 /* Controls the vibrator of a particular input device. */
99 virtual void vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize,
100 ssize_t repeat, int32_t token) = 0;
101 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
102};
103
104/* Reads raw events from the event hub and processes them, endlessly. */
105class InputReaderThread : public Thread {
106public:
107 explicit InputReaderThread(const sp<InputReaderInterface>& reader);
108 virtual ~InputReaderThread();
109
110private:
111 sp<InputReaderInterface> mReader;
112
113 virtual bool threadLoop();
114};
115
116/*
117 * Input reader configuration.
118 *
119 * Specifies various options that modify the behavior of the input reader.
120 */
121struct InputReaderConfiguration {
122 // Describes changes that have occurred.
123 enum {
124 // The pointer speed changed.
125 CHANGE_POINTER_SPEED = 1 << 0,
126
127 // The pointer gesture control changed.
128 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
129
130 // The display size or orientation changed.
131 CHANGE_DISPLAY_INFO = 1 << 2,
132
133 // The visible touches option changed.
134 CHANGE_SHOW_TOUCHES = 1 << 3,
135
136 // The keyboard layouts must be reloaded.
137 CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
138
139 // The device name alias supplied by the may have changed for some devices.
140 CHANGE_DEVICE_ALIAS = 1 << 5,
141
142 // The location calibration matrix changed.
143 CHANGE_TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
144
145 // The presence of an external stylus has changed.
146 CHANGE_EXTERNAL_STYLUS_PRESENCE = 1 << 7,
147
148 // The pointer capture mode has changed.
149 CHANGE_POINTER_CAPTURE = 1 << 8,
150
151 // The set of disabled input devices (disabledDevices) has changed.
152 CHANGE_ENABLED_STATE = 1 << 9,
153
154 // All devices must be reopened.
155 CHANGE_MUST_REOPEN = 1 << 31,
156 };
157
158 // Gets the amount of time to disable virtual keys after the screen is touched
159 // in order to filter out accidental virtual key presses due to swiping gestures
160 // or taps near the edge of the display. May be 0 to disable the feature.
161 nsecs_t virtualKeyQuietTime;
162
163 // The excluded device names for the platform.
164 // Devices with these names will be ignored.
165 std::vector<std::string> excludedDeviceNames;
166
167 // Velocity control parameters for mouse pointer movements.
168 VelocityControlParameters pointerVelocityControlParameters;
169
170 // Velocity control parameters for mouse wheel movements.
171 VelocityControlParameters wheelVelocityControlParameters;
172
173 // True if pointer gestures are enabled.
174 bool pointerGesturesEnabled;
175
176 // Quiet time between certain pointer gesture transitions.
177 // Time to allow for all fingers or buttons to settle into a stable state before
178 // starting a new gesture.
179 nsecs_t pointerGestureQuietInterval;
180
181 // The minimum speed that a pointer must travel for us to consider switching the active
182 // touch pointer to it during a drag. This threshold is set to avoid switching due
183 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
184 float pointerGestureDragMinSwitchSpeed; // in pixels per second
185
186 // Tap gesture delay time.
187 // The time between down and up must be less than this to be considered a tap.
188 nsecs_t pointerGestureTapInterval;
189
190 // Tap drag gesture delay time.
191 // The time between the previous tap's up and the next down must be less than
192 // this to be considered a drag. Otherwise, the previous tap is finished and a
193 // new tap begins.
194 //
195 // Note that the previous tap will be held down for this entire duration so this
196 // interval must be shorter than the long press timeout.
197 nsecs_t pointerGestureTapDragInterval;
198
199 // The distance in pixels that the pointer is allowed to move from initial down
200 // to up and still be called a tap.
201 float pointerGestureTapSlop; // in pixels
202
203 // Time after the first touch points go down to settle on an initial centroid.
204 // This is intended to be enough time to handle cases where the user puts down two
205 // fingers at almost but not quite exactly the same time.
206 nsecs_t pointerGestureMultitouchSettleInterval;
207
208 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
209 // at least two pointers have moved at least this far from their starting place.
210 float pointerGestureMultitouchMinDistance; // in pixels
211
212 // The transition from PRESS to SWIPE gesture mode can only occur when the
213 // cosine of the angle between the two vectors is greater than or equal to than this value
214 // which indicates that the vectors are oriented in the same direction.
215 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
216 // (In exactly opposite directions, the cosine is -1.0.)
217 float pointerGestureSwipeTransitionAngleCosine;
218
219 // The transition from PRESS to SWIPE gesture mode can only occur when the
220 // fingers are no more than this far apart relative to the diagonal size of
221 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
222 // no more than half the diagonal size of the touch pad apart.
223 float pointerGestureSwipeMaxWidthRatio;
224
225 // The gesture movement speed factor relative to the size of the display.
226 // Movement speed applies when the fingers are moving in the same direction.
227 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
228 // will cover this portion of the display diagonal.
229 float pointerGestureMovementSpeedRatio;
230
231 // The gesture zoom speed factor relative to the size of the display.
232 // Zoom speed applies when the fingers are mostly moving relative to each other
233 // to execute a scale gesture or similar.
234 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
235 // will cover this portion of the display diagonal.
236 float pointerGestureZoomSpeedRatio;
237
238 // True to show the location of touches on the touch screen as spots.
239 bool showTouches;
240
241 // True if pointer capture is enabled.
242 bool pointerCapture;
243
244 // The set of currently disabled input devices.
245 SortedVector<int32_t> disabledDevices;
246
247 InputReaderConfiguration() :
248 virtualKeyQuietTime(0),
249 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
250 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
251 pointerGesturesEnabled(true),
252 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
253 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
254 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
255 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
256 pointerGestureTapSlop(10.0f), // 10 pixels
257 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
258 pointerGestureMultitouchMinDistance(15), // 15 pixels
259 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
260 pointerGestureSwipeMaxWidthRatio(0.25f),
261 pointerGestureMovementSpeedRatio(0.8f),
262 pointerGestureZoomSpeedRatio(0.3f),
263 showTouches(false) { }
264
265 std::optional<DisplayViewport> getDisplayViewport(ViewportType viewportType,
266 const std::string& uniqueDisplayId) const;
267 void setDisplayViewports(const std::vector<DisplayViewport>& viewports);
268
269
270 void dump(std::string& dump) const;
271 void dumpViewport(std::string& dump, const DisplayViewport& viewport) const;
272
273private:
274 std::vector<DisplayViewport> mDisplays;
275};
276
277struct TouchAffineTransformation {
278 float x_scale;
279 float x_ymix;
280 float x_offset;
281 float y_xmix;
282 float y_scale;
283 float y_offset;
284
285 TouchAffineTransformation() :
286 x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
287 y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
288 }
289
290 TouchAffineTransformation(float xscale, float xymix, float xoffset,
291 float yxmix, float yscale, float yoffset) :
292 x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
293 y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
294 }
295
296 void applyTo(float& x, float& y) const;
297};
298
299/*
300 * Input reader policy interface.
301 *
302 * The input reader policy is used by the input reader to interact with the Window Manager
303 * and other system components.
304 *
305 * The actual implementation is partially supported by callbacks into the DVM
306 * via JNI. This interface is also mocked in the unit tests.
307 *
308 * These methods must NOT re-enter the input reader since they may be called while
309 * holding the input reader lock.
310 */
311class InputReaderPolicyInterface : public virtual RefBase {
312protected:
313 InputReaderPolicyInterface() { }
314 virtual ~InputReaderPolicyInterface() { }
315
316public:
317 /* Gets the input reader configuration. */
318 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
319
320 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
321 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
322
323 /* Notifies the input reader policy that some input devices have changed
324 * and provides information about all current input devices.
325 */
326 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) = 0;
327
328 /* Gets the keyboard layout for a particular input device. */
329 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(
330 const InputDeviceIdentifier& identifier) = 0;
331
332 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
333 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
334
335 /* Gets the affine calibration associated with the specified device. */
336 virtual TouchAffineTransformation getTouchAffineTransformation(
337 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
338};
339
340} // namespace android
341
342#endif // _UI_INPUT_READER_COMMON_H