blob: 3bf212a51b55294ea10d57c3d264a8227c5c779b [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
Prabir Pradhan28efc192019-11-05 01:10:04 +000020#include <input/DisplayViewport.h>
Prabir Pradhan29c95332018-11-14 20:14:11 -080021#include <input/Input.h>
22#include <input/InputDevice.h>
Prabir Pradhan29c95332018-11-14 20:14:11 -080023#include <input/VelocityControl.h>
24#include <input/VelocityTracker.h>
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000025#include <stddef.h>
26#include <unistd.h>
Prabir Pradhan28efc192019-11-05 01:10:04 +000027#include <utils/Errors.h>
Prabir Pradhan29c95332018-11-14 20:14:11 -080028#include <utils/RefBase.h>
Prabir Pradhan29c95332018-11-14 20:14:11 -080029
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +000030#include <optional>
31#include <set>
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070032#include <unordered_map>
Prabir Pradhan29c95332018-11-14 20:14:11 -080033#include <vector>
34
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000035#include "PointerControllerInterface.h"
36#include "VibrationElement.h"
37
Prabir Pradhan29c95332018-11-14 20:14:11 -080038// Maximum supported size of a vibration pattern.
39// Must be at least 2.
40#define MAX_VIBRATE_PATTERN_SIZE 100
41
Prabir Pradhan29c95332018-11-14 20:14:11 -080042namespace android {
43
Prabir Pradhan28efc192019-11-05 01:10:04 +000044// --- InputReaderInterface ---
45
46/* The interface for the InputReader shared library.
47 *
48 * Manages one or more threads that process raw input events and sends cooked event data to an
49 * input listener.
50 *
51 * The implementation must guarantee thread safety for this interface. However, since the input
52 * listener is NOT thread safe, all calls to the listener must happen from the same thread.
53 */
Prabir Pradhan29c95332018-11-14 20:14:11 -080054class InputReaderInterface : public virtual RefBase {
55protected:
56 InputReaderInterface() { }
57 virtual ~InputReaderInterface() { }
58
59public:
60 /* Dumps the state of the input reader.
61 *
62 * This method may be called on any thread (usually by the input manager). */
63 virtual void dump(std::string& dump) = 0;
64
Prabir Pradhan28efc192019-11-05 01:10:04 +000065 /* Called by the heartbeat to ensures that the reader has not deadlocked. */
Prabir Pradhan29c95332018-11-14 20:14:11 -080066 virtual void monitor() = 0;
67
68 /* Returns true if the input device is enabled. */
69 virtual bool isInputDeviceEnabled(int32_t deviceId) = 0;
70
Prabir Pradhan28efc192019-11-05 01:10:04 +000071 /* Makes the reader start processing events from the kernel. */
72 virtual status_t start() = 0;
73
74 /* Makes the reader stop processing any more events. */
75 virtual status_t stop() = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -080076
77 /* Gets information about all input devices.
78 *
79 * This method may be called on any thread (usually by the input manager).
80 */
Chris Ye98d3f532020-10-01 21:48:59 -070081 virtual std::vector<InputDeviceInfo> getInputDevices() const = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -080082
83 /* Query current input state. */
84 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
85 int32_t scanCode) = 0;
86 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
87 int32_t keyCode) = 0;
88 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
89 int32_t sw) = 0;
90
91 /* Toggle Caps Lock */
92 virtual void toggleCapsLockState(int32_t deviceId) = 0;
93
94 /* Determine whether physical keys exist for the given framework-domain key codes. */
95 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
96 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
97
98 /* Requests that a reconfiguration of all input devices.
99 * The changes flag is a bitfield that indicates what has changed and whether
100 * the input devices must all be reopened. */
101 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
102
103 /* Controls the vibrator of a particular input device. */
Chris Ye87143712020-11-10 05:05:58 +0000104 virtual void vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
105 int32_t token) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800106 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
Arthur Hungc23540e2018-11-29 20:42:11 +0800107
Chris Ye87143712020-11-10 05:05:58 +0000108 virtual bool isVibrating(int32_t deviceId) = 0;
109
110 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800111 /* Get battery level of a particular input device. */
112 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId) = 0;
113 /* Get battery status of a particular input device. */
114 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId) = 0;
Chris Ye87143712020-11-10 05:05:58 +0000115
Chris Ye3fdbfef2021-01-06 18:45:18 -0800116 virtual std::vector<int32_t> getLightIds(int32_t deviceId) = 0;
117
118 virtual const InputDeviceLightInfo* getLightInfo(int32_t deviceId, int32_t lightId) = 0;
119
Arthur Hungc23540e2018-11-29 20:42:11 +0800120 /* Return true if the device can send input events to the specified display. */
121 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) = 0;
Chris Yef59a2f42020-10-16 12:55:26 -0700122
123 /* Enable sensor in input reader mapper. */
124 virtual bool enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
125 std::chrono::microseconds samplingPeriod,
126 std::chrono::microseconds maxBatchReportLatency) = 0;
127
128 /* Disable sensor in input reader mapper. */
129 virtual void disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) = 0;
130
131 /* Flush sensor data in input reader mapper. */
132 virtual void flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800133
134 /* Set color for the light */
135 virtual bool setLightColor(int32_t deviceId, int32_t lightId, int32_t color) = 0;
136 /* Set player ID for the light */
137 virtual bool setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) = 0;
138 /* Get light color */
139 virtual std::optional<int32_t> getLightColor(int32_t deviceId, int32_t lightId) = 0;
140 /* Get light player ID */
141 virtual std::optional<int32_t> getLightPlayerId(int32_t deviceId, int32_t lightId) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800142};
143
Prabir Pradhan28efc192019-11-05 01:10:04 +0000144// --- InputReaderConfiguration ---
Prabir Pradhan29c95332018-11-14 20:14:11 -0800145
146/*
147 * Input reader configuration.
148 *
149 * Specifies various options that modify the behavior of the input reader.
150 */
151struct InputReaderConfiguration {
152 // Describes changes that have occurred.
153 enum {
154 // The pointer speed changed.
155 CHANGE_POINTER_SPEED = 1 << 0,
156
157 // The pointer gesture control changed.
158 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
159
160 // The display size or orientation changed.
161 CHANGE_DISPLAY_INFO = 1 << 2,
162
163 // The visible touches option changed.
164 CHANGE_SHOW_TOUCHES = 1 << 3,
165
166 // The keyboard layouts must be reloaded.
167 CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
168
169 // The device name alias supplied by the may have changed for some devices.
170 CHANGE_DEVICE_ALIAS = 1 << 5,
171
172 // The location calibration matrix changed.
173 CHANGE_TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
174
175 // The presence of an external stylus has changed.
176 CHANGE_EXTERNAL_STYLUS_PRESENCE = 1 << 7,
177
178 // The pointer capture mode has changed.
179 CHANGE_POINTER_CAPTURE = 1 << 8,
180
181 // The set of disabled input devices (disabledDevices) has changed.
182 CHANGE_ENABLED_STATE = 1 << 9,
183
184 // All devices must be reopened.
185 CHANGE_MUST_REOPEN = 1 << 31,
186 };
187
188 // Gets the amount of time to disable virtual keys after the screen is touched
189 // in order to filter out accidental virtual key presses due to swiping gestures
190 // or taps near the edge of the display. May be 0 to disable the feature.
191 nsecs_t virtualKeyQuietTime;
192
193 // The excluded device names for the platform.
194 // Devices with these names will be ignored.
195 std::vector<std::string> excludedDeviceNames;
196
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700197 // The associations between input ports and display ports.
198 // Used to determine which DisplayViewport should be tied to which InputDevice.
199 std::unordered_map<std::string, uint8_t> portAssociations;
200
Garfield Tan888a6a42020-01-09 11:39:16 -0800201 // The suggested display ID to show the cursor.
202 int32_t defaultPointerDisplayId;
203
Prabir Pradhan29c95332018-11-14 20:14:11 -0800204 // Velocity control parameters for mouse pointer movements.
205 VelocityControlParameters pointerVelocityControlParameters;
206
207 // Velocity control parameters for mouse wheel movements.
208 VelocityControlParameters wheelVelocityControlParameters;
209
210 // True if pointer gestures are enabled.
211 bool pointerGesturesEnabled;
212
213 // Quiet time between certain pointer gesture transitions.
214 // Time to allow for all fingers or buttons to settle into a stable state before
215 // starting a new gesture.
216 nsecs_t pointerGestureQuietInterval;
217
218 // The minimum speed that a pointer must travel for us to consider switching the active
219 // touch pointer to it during a drag. This threshold is set to avoid switching due
220 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
221 float pointerGestureDragMinSwitchSpeed; // in pixels per second
222
223 // Tap gesture delay time.
224 // The time between down and up must be less than this to be considered a tap.
225 nsecs_t pointerGestureTapInterval;
226
227 // Tap drag gesture delay time.
228 // The time between the previous tap's up and the next down must be less than
229 // this to be considered a drag. Otherwise, the previous tap is finished and a
230 // new tap begins.
231 //
232 // Note that the previous tap will be held down for this entire duration so this
233 // interval must be shorter than the long press timeout.
234 nsecs_t pointerGestureTapDragInterval;
235
236 // The distance in pixels that the pointer is allowed to move from initial down
237 // to up and still be called a tap.
238 float pointerGestureTapSlop; // in pixels
239
240 // Time after the first touch points go down to settle on an initial centroid.
241 // This is intended to be enough time to handle cases where the user puts down two
242 // fingers at almost but not quite exactly the same time.
243 nsecs_t pointerGestureMultitouchSettleInterval;
244
245 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
246 // at least two pointers have moved at least this far from their starting place.
247 float pointerGestureMultitouchMinDistance; // in pixels
248
249 // The transition from PRESS to SWIPE gesture mode can only occur when the
250 // cosine of the angle between the two vectors is greater than or equal to than this value
251 // which indicates that the vectors are oriented in the same direction.
252 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
253 // (In exactly opposite directions, the cosine is -1.0.)
254 float pointerGestureSwipeTransitionAngleCosine;
255
256 // The transition from PRESS to SWIPE gesture mode can only occur when the
257 // fingers are no more than this far apart relative to the diagonal size of
258 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
259 // no more than half the diagonal size of the touch pad apart.
260 float pointerGestureSwipeMaxWidthRatio;
261
262 // The gesture movement speed factor relative to the size of the display.
263 // Movement speed applies when the fingers are moving in the same direction.
264 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
265 // will cover this portion of the display diagonal.
266 float pointerGestureMovementSpeedRatio;
267
268 // The gesture zoom speed factor relative to the size of the display.
269 // Zoom speed applies when the fingers are mostly moving relative to each other
270 // to execute a scale gesture or similar.
271 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
272 // will cover this portion of the display diagonal.
273 float pointerGestureZoomSpeedRatio;
274
275 // True to show the location of touches on the touch screen as spots.
276 bool showTouches;
277
278 // True if pointer capture is enabled.
279 bool pointerCapture;
280
281 // The set of currently disabled input devices.
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000282 std::set<int32_t> disabledDevices;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800283
284 InputReaderConfiguration() :
285 virtualKeyQuietTime(0),
286 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
287 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
288 pointerGesturesEnabled(true),
289 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
290 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
291 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
292 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
293 pointerGestureTapSlop(10.0f), // 10 pixels
294 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
295 pointerGestureMultitouchMinDistance(15), // 15 pixels
296 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
297 pointerGestureSwipeMaxWidthRatio(0.25f),
298 pointerGestureMovementSpeedRatio(0.8f),
299 pointerGestureZoomSpeedRatio(0.3f),
Siarhei Vishniakou4b057032019-01-30 19:43:49 -0800300 showTouches(false), pointerCapture(false) { }
Prabir Pradhan29c95332018-11-14 20:14:11 -0800301
Siarhei Vishniakouc5ae0dc2019-07-10 15:51:18 -0700302 static std::string changesToString(uint32_t changes);
303
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700304 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const;
305 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueDisplayId)
306 const;
307 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t physicalPort) const;
Garfield Tan888a6a42020-01-09 11:39:16 -0800308 std::optional<DisplayViewport> getDisplayViewportById(int32_t displayId) const;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800309 void setDisplayViewports(const std::vector<DisplayViewport>& viewports);
310
311
312 void dump(std::string& dump) const;
313 void dumpViewport(std::string& dump, const DisplayViewport& viewport) const;
314
315private:
316 std::vector<DisplayViewport> mDisplays;
317};
318
Prabir Pradhan28efc192019-11-05 01:10:04 +0000319// --- TouchAffineTransformation ---
320
Prabir Pradhan29c95332018-11-14 20:14:11 -0800321struct TouchAffineTransformation {
322 float x_scale;
323 float x_ymix;
324 float x_offset;
325 float y_xmix;
326 float y_scale;
327 float y_offset;
328
329 TouchAffineTransformation() :
330 x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
331 y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
332 }
333
334 TouchAffineTransformation(float xscale, float xymix, float xoffset,
335 float yxmix, float yscale, float yoffset) :
336 x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
337 y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
338 }
339
340 void applyTo(float& x, float& y) const;
341};
342
Prabir Pradhan28efc192019-11-05 01:10:04 +0000343// --- InputReaderPolicyInterface ---
344
Prabir Pradhan29c95332018-11-14 20:14:11 -0800345/*
346 * Input reader policy interface.
347 *
348 * The input reader policy is used by the input reader to interact with the Window Manager
349 * and other system components.
350 *
351 * The actual implementation is partially supported by callbacks into the DVM
352 * via JNI. This interface is also mocked in the unit tests.
353 *
Prabir Pradhan28efc192019-11-05 01:10:04 +0000354 * These methods will NOT re-enter the input reader interface, so they may be called from
355 * any method in the input reader interface.
Prabir Pradhan29c95332018-11-14 20:14:11 -0800356 */
357class InputReaderPolicyInterface : public virtual RefBase {
358protected:
359 InputReaderPolicyInterface() { }
360 virtual ~InputReaderPolicyInterface() { }
361
362public:
363 /* Gets the input reader configuration. */
364 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
365
366 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
Michael Wright17db18e2020-06-26 20:51:44 +0100367 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(
368 int32_t deviceId) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800369
370 /* Notifies the input reader policy that some input devices have changed
371 * and provides information about all current input devices.
372 */
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800373 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800374
375 /* Gets the keyboard layout for a particular input device. */
Chris Ye3a1e4462020-08-12 10:13:15 -0700376 virtual std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
Prabir Pradhan29c95332018-11-14 20:14:11 -0800377 const InputDeviceIdentifier& identifier) = 0;
378
379 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
380 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
381
382 /* Gets the affine calibration associated with the specified device. */
383 virtual TouchAffineTransformation getTouchAffineTransformation(
384 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800385};
386
387} // namespace android
388
Garfield Tan888a6a42020-01-09 11:39:16 -0800389#endif // _UI_INPUT_READER_COMMON_H