blob: 3b0f2ac5a2bbc240a78aef5181bbbf6036a55925 [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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Prabir Pradhan29c95332018-11-14 20:14:11 -080018
Christine Franks46d8a1e2022-01-05 16:11:48 -080019#include <android/os/IInputConstants.h>
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 */
Siarhei Vishniakou18050092021-09-01 13:32:49 -070054class InputReaderInterface {
55public:
Prabir Pradhan29c95332018-11-14 20:14:11 -080056 InputReaderInterface() { }
57 virtual ~InputReaderInterface() { }
58
Prabir Pradhan29c95332018-11-14 20:14:11 -080059 /* Dumps the state of the input reader.
60 *
61 * This method may be called on any thread (usually by the input manager). */
62 virtual void dump(std::string& dump) = 0;
63
Prabir Pradhan28efc192019-11-05 01:10:04 +000064 /* Called by the heartbeat to ensures that the reader has not deadlocked. */
Prabir Pradhan29c95332018-11-14 20:14:11 -080065 virtual void monitor() = 0;
66
67 /* Returns true if the input device is enabled. */
68 virtual bool isInputDeviceEnabled(int32_t deviceId) = 0;
69
Prabir Pradhan28efc192019-11-05 01:10:04 +000070 /* Makes the reader start processing events from the kernel. */
71 virtual status_t start() = 0;
72
73 /* Makes the reader stop processing any more events. */
74 virtual status_t stop() = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -080075
76 /* Gets information about all input devices.
77 *
78 * This method may be called on any thread (usually by the input manager).
79 */
Chris Ye98d3f532020-10-01 21:48:59 -070080 virtual std::vector<InputDeviceInfo> getInputDevices() const = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -080081
82 /* Query current input state. */
83 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
84 int32_t scanCode) = 0;
85 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
86 int32_t keyCode) = 0;
87 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
88 int32_t sw) = 0;
89
Philip Junker4af3b3d2021-12-14 10:36:55 +010090 virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
91
Prabir Pradhan29c95332018-11-14 20:14:11 -080092 /* Toggle Caps Lock */
93 virtual void toggleCapsLockState(int32_t deviceId) = 0;
94
95 /* Determine whether physical keys exist for the given framework-domain key codes. */
96 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
Siarhei Vishniakou74007942022-06-13 13:57:47 -070097 const std::vector<int32_t>& keyCodes, uint8_t* outFlags) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -080098
99 /* Requests that a reconfiguration of all input devices.
100 * The changes flag is a bitfield that indicates what has changed and whether
101 * the input devices must all be reopened. */
102 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
103
104 /* Controls the vibrator of a particular input device. */
Chris Ye87143712020-11-10 05:05:58 +0000105 virtual void vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
106 int32_t token) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800107 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
Arthur Hungc23540e2018-11-29 20:42:11 +0800108
Chris Ye87143712020-11-10 05:05:58 +0000109 virtual bool isVibrating(int32_t deviceId) = 0;
110
111 virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0;
Kim Low03ea0352020-11-06 12:45:07 -0800112 /* Get battery level of a particular input device. */
113 virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId) = 0;
114 /* Get battery status of a particular input device. */
115 virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId) = 0;
Prabir Pradhane287ecd2022-09-07 21:18:05 +0000116 /* Get the device path for the battery of an input device. */
117 virtual std::optional<std::string> getBatteryDevicePath(int32_t deviceId) = 0;
Chris Ye87143712020-11-10 05:05:58 +0000118
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000119 virtual std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800120
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000121 virtual std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800122
Arthur Hungc23540e2018-11-29 20:42:11 +0800123 /* Return true if the device can send input events to the specified display. */
124 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) = 0;
Chris Yef59a2f42020-10-16 12:55:26 -0700125
126 /* Enable sensor in input reader mapper. */
127 virtual bool enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
128 std::chrono::microseconds samplingPeriod,
129 std::chrono::microseconds maxBatchReportLatency) = 0;
130
131 /* Disable sensor in input reader mapper. */
132 virtual void disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) = 0;
133
134 /* Flush sensor data in input reader mapper. */
135 virtual void flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) = 0;
Chris Ye3fdbfef2021-01-06 18:45:18 -0800136
137 /* Set color for the light */
138 virtual bool setLightColor(int32_t deviceId, int32_t lightId, int32_t color) = 0;
139 /* Set player ID for the light */
140 virtual bool setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) = 0;
141 /* Get light color */
142 virtual std::optional<int32_t> getLightColor(int32_t deviceId, int32_t lightId) = 0;
143 /* Get light player ID */
144 virtual std::optional<int32_t> getLightPlayerId(int32_t deviceId, int32_t lightId) = 0;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +0000145
146 /* Get the Bluetooth address of an input device, if known. */
147 virtual std::optional<std::string> getBluetoothAddress(int32_t deviceId) const = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800148};
149
Prabir Pradhan28efc192019-11-05 01:10:04 +0000150// --- InputReaderConfiguration ---
Prabir Pradhan29c95332018-11-14 20:14:11 -0800151
152/*
153 * Input reader configuration.
154 *
155 * Specifies various options that modify the behavior of the input reader.
156 */
157struct InputReaderConfiguration {
158 // Describes changes that have occurred.
159 enum {
160 // The pointer speed changed.
161 CHANGE_POINTER_SPEED = 1 << 0,
162
163 // The pointer gesture control changed.
164 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
165
166 // The display size or orientation changed.
167 CHANGE_DISPLAY_INFO = 1 << 2,
168
169 // The visible touches option changed.
170 CHANGE_SHOW_TOUCHES = 1 << 3,
171
172 // The keyboard layouts must be reloaded.
173 CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
174
175 // The device name alias supplied by the may have changed for some devices.
176 CHANGE_DEVICE_ALIAS = 1 << 5,
177
178 // The location calibration matrix changed.
179 CHANGE_TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
180
181 // The presence of an external stylus has changed.
182 CHANGE_EXTERNAL_STYLUS_PRESENCE = 1 << 7,
183
184 // The pointer capture mode has changed.
185 CHANGE_POINTER_CAPTURE = 1 << 8,
186
187 // The set of disabled input devices (disabledDevices) has changed.
188 CHANGE_ENABLED_STATE = 1 << 9,
189
190 // All devices must be reopened.
191 CHANGE_MUST_REOPEN = 1 << 31,
192 };
193
194 // Gets the amount of time to disable virtual keys after the screen is touched
195 // in order to filter out accidental virtual key presses due to swiping gestures
196 // or taps near the edge of the display. May be 0 to disable the feature.
197 nsecs_t virtualKeyQuietTime;
198
199 // The excluded device names for the platform.
200 // Devices with these names will be ignored.
201 std::vector<std::string> excludedDeviceNames;
202
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700203 // The associations between input ports and display ports.
204 // Used to determine which DisplayViewport should be tied to which InputDevice.
205 std::unordered_map<std::string, uint8_t> portAssociations;
206
Christine Franks1ba71cc2021-04-07 14:37:42 -0700207 // The associations between input device names and display unique ids.
208 // Used to determine which DisplayViewport should be tied to which InputDevice.
209 std::unordered_map<std::string, std::string> uniqueIdAssociations;
210
Garfield Tan888a6a42020-01-09 11:39:16 -0800211 // The suggested display ID to show the cursor.
212 int32_t defaultPointerDisplayId;
213
Prabir Pradhan29c95332018-11-14 20:14:11 -0800214 // Velocity control parameters for mouse pointer movements.
215 VelocityControlParameters pointerVelocityControlParameters;
216
217 // Velocity control parameters for mouse wheel movements.
218 VelocityControlParameters wheelVelocityControlParameters;
219
220 // True if pointer gestures are enabled.
221 bool pointerGesturesEnabled;
222
223 // Quiet time between certain pointer gesture transitions.
224 // Time to allow for all fingers or buttons to settle into a stable state before
225 // starting a new gesture.
226 nsecs_t pointerGestureQuietInterval;
227
228 // The minimum speed that a pointer must travel for us to consider switching the active
229 // touch pointer to it during a drag. This threshold is set to avoid switching due
230 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
231 float pointerGestureDragMinSwitchSpeed; // in pixels per second
232
233 // Tap gesture delay time.
234 // The time between down and up must be less than this to be considered a tap.
235 nsecs_t pointerGestureTapInterval;
236
237 // Tap drag gesture delay time.
238 // The time between the previous tap's up and the next down must be less than
239 // this to be considered a drag. Otherwise, the previous tap is finished and a
240 // new tap begins.
241 //
242 // Note that the previous tap will be held down for this entire duration so this
243 // interval must be shorter than the long press timeout.
244 nsecs_t pointerGestureTapDragInterval;
245
246 // The distance in pixels that the pointer is allowed to move from initial down
247 // to up and still be called a tap.
248 float pointerGestureTapSlop; // in pixels
249
250 // Time after the first touch points go down to settle on an initial centroid.
251 // This is intended to be enough time to handle cases where the user puts down two
252 // fingers at almost but not quite exactly the same time.
253 nsecs_t pointerGestureMultitouchSettleInterval;
254
255 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
256 // at least two pointers have moved at least this far from their starting place.
257 float pointerGestureMultitouchMinDistance; // in pixels
258
259 // The transition from PRESS to SWIPE gesture mode can only occur when the
260 // cosine of the angle between the two vectors is greater than or equal to than this value
261 // which indicates that the vectors are oriented in the same direction.
262 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
263 // (In exactly opposite directions, the cosine is -1.0.)
264 float pointerGestureSwipeTransitionAngleCosine;
265
266 // The transition from PRESS to SWIPE gesture mode can only occur when the
267 // fingers are no more than this far apart relative to the diagonal size of
268 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
269 // no more than half the diagonal size of the touch pad apart.
270 float pointerGestureSwipeMaxWidthRatio;
271
272 // The gesture movement speed factor relative to the size of the display.
273 // Movement speed applies when the fingers are moving in the same direction.
274 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
275 // will cover this portion of the display diagonal.
276 float pointerGestureMovementSpeedRatio;
277
278 // The gesture zoom speed factor relative to the size of the display.
279 // Zoom speed applies when the fingers are mostly moving relative to each other
280 // to execute a scale gesture or similar.
281 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
282 // will cover this portion of the display diagonal.
283 float pointerGestureZoomSpeedRatio;
284
285 // True to show the location of touches on the touch screen as spots.
286 bool showTouches;
287
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000288 // The latest request to enable or disable Pointer Capture.
289 PointerCaptureRequest pointerCaptureRequest;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800290
291 // The set of currently disabled input devices.
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000292 std::set<int32_t> disabledDevices;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800293
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000294 InputReaderConfiguration()
295 : virtualKeyQuietTime(0),
Christine Franks46d8a1e2022-01-05 16:11:48 -0800296 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f,
297 static_cast<float>(
298 android::os::IInputConstants::
299 DEFAULT_POINTER_ACCELERATION)),
Prabir Pradhan29c95332018-11-14 20:14:11 -0800300 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
301 pointerGesturesEnabled(true),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000302 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
303 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
304 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
305 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
306 pointerGestureTapSlop(10.0f), // 10 pixels
Prabir Pradhan29c95332018-11-14 20:14:11 -0800307 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000308 pointerGestureMultitouchMinDistance(15), // 15 pixels
309 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
Prabir Pradhan29c95332018-11-14 20:14:11 -0800310 pointerGestureSwipeMaxWidthRatio(0.25f),
311 pointerGestureMovementSpeedRatio(0.8f),
312 pointerGestureZoomSpeedRatio(0.3f),
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000313 showTouches(false),
314 pointerCaptureRequest() {}
Prabir Pradhan29c95332018-11-14 20:14:11 -0800315
Siarhei Vishniakouc5ae0dc2019-07-10 15:51:18 -0700316 static std::string changesToString(uint32_t changes);
317
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700318 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const;
319 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueDisplayId)
320 const;
321 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t physicalPort) const;
Garfield Tan888a6a42020-01-09 11:39:16 -0800322 std::optional<DisplayViewport> getDisplayViewportById(int32_t displayId) const;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800323 void setDisplayViewports(const std::vector<DisplayViewport>& viewports);
324
325
326 void dump(std::string& dump) const;
327 void dumpViewport(std::string& dump, const DisplayViewport& viewport) const;
328
329private:
330 std::vector<DisplayViewport> mDisplays;
331};
332
Prabir Pradhan28efc192019-11-05 01:10:04 +0000333// --- TouchAffineTransformation ---
334
Prabir Pradhan29c95332018-11-14 20:14:11 -0800335struct TouchAffineTransformation {
336 float x_scale;
337 float x_ymix;
338 float x_offset;
339 float y_xmix;
340 float y_scale;
341 float y_offset;
342
343 TouchAffineTransformation() :
344 x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
345 y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
346 }
347
348 TouchAffineTransformation(float xscale, float xymix, float xoffset,
349 float yxmix, float yscale, float yoffset) :
350 x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
351 y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
352 }
353
354 void applyTo(float& x, float& y) const;
355};
356
Prabir Pradhan28efc192019-11-05 01:10:04 +0000357// --- InputReaderPolicyInterface ---
358
Prabir Pradhan29c95332018-11-14 20:14:11 -0800359/*
360 * Input reader policy interface.
361 *
362 * The input reader policy is used by the input reader to interact with the Window Manager
363 * and other system components.
364 *
365 * The actual implementation is partially supported by callbacks into the DVM
366 * via JNI. This interface is also mocked in the unit tests.
367 *
Prabir Pradhan28efc192019-11-05 01:10:04 +0000368 * These methods will NOT re-enter the input reader interface, so they may be called from
369 * any method in the input reader interface.
Prabir Pradhan29c95332018-11-14 20:14:11 -0800370 */
371class InputReaderPolicyInterface : public virtual RefBase {
372protected:
373 InputReaderPolicyInterface() { }
374 virtual ~InputReaderPolicyInterface() { }
375
376public:
377 /* Gets the input reader configuration. */
378 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
379
380 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
Michael Wright17db18e2020-06-26 20:51:44 +0100381 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(
382 int32_t deviceId) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800383
384 /* Notifies the input reader policy that some input devices have changed
385 * and provides information about all current input devices.
386 */
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800387 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800388
389 /* Gets the keyboard layout for a particular input device. */
Chris Ye3a1e4462020-08-12 10:13:15 -0700390 virtual std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
Prabir Pradhan29c95332018-11-14 20:14:11 -0800391 const InputDeviceIdentifier& identifier) = 0;
392
393 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
394 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
395
396 /* Gets the affine calibration associated with the specified device. */
397 virtual TouchAffineTransformation getTouchAffineTransformation(
398 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000399 /* Notifies the input reader policy that a stylus gesture has started. */
400 virtual void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800401};
402
403} // namespace android