blob: 5a832e7a8e97ce7ef4cf4a0a068a7a5c2ab1c35d [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
42// Maximum allowable delay value in a vibration pattern before
43// which the delay will be truncated.
44#define MAX_VIBRATE_PATTERN_DELAY_NSECS (1000000 * 1000000000LL)
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +000045#define MAX_VIBRATE_PATTERN_DELAY_MSECS (1000000 * 1000LL)
Prabir Pradhan29c95332018-11-14 20:14:11 -080046
47namespace android {
48
Prabir Pradhan28efc192019-11-05 01:10:04 +000049// --- InputReaderInterface ---
50
51/* The interface for the InputReader shared library.
52 *
53 * Manages one or more threads that process raw input events and sends cooked event data to an
54 * input listener.
55 *
56 * The implementation must guarantee thread safety for this interface. However, since the input
57 * listener is NOT thread safe, all calls to the listener must happen from the same thread.
58 */
Prabir Pradhan29c95332018-11-14 20:14:11 -080059class InputReaderInterface : public virtual RefBase {
60protected:
61 InputReaderInterface() { }
62 virtual ~InputReaderInterface() { }
63
64public:
65 /* Dumps the state of the input reader.
66 *
67 * This method may be called on any thread (usually by the input manager). */
68 virtual void dump(std::string& dump) = 0;
69
Prabir Pradhan28efc192019-11-05 01:10:04 +000070 /* Called by the heartbeat to ensures that the reader has not deadlocked. */
Prabir Pradhan29c95332018-11-14 20:14:11 -080071 virtual void monitor() = 0;
72
73 /* Returns true if the input device is enabled. */
74 virtual bool isInputDeviceEnabled(int32_t deviceId) = 0;
75
Prabir Pradhan28efc192019-11-05 01:10:04 +000076 /* Makes the reader start processing events from the kernel. */
77 virtual status_t start() = 0;
78
79 /* Makes the reader stop processing any more events. */
80 virtual status_t stop() = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -080081
82 /* Gets information about all input devices.
83 *
84 * This method may be called on any thread (usually by the input manager).
85 */
Arthur Hung7c3ae9c2019-03-11 11:23:03 +080086 virtual void getInputDevices(std::vector<InputDeviceInfo>& outInputDevices) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -080087
88 /* Query current input state. */
89 virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask,
90 int32_t scanCode) = 0;
91 virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
92 int32_t keyCode) = 0;
93 virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask,
94 int32_t sw) = 0;
95
96 /* Toggle Caps Lock */
97 virtual void toggleCapsLockState(int32_t deviceId) = 0;
98
99 /* Determine whether physical keys exist for the given framework-domain key codes. */
100 virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
101 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) = 0;
102
103 /* Requests that a reconfiguration of all input devices.
104 * The changes flag is a bitfield that indicates what has changed and whether
105 * the input devices must all be reopened. */
106 virtual void requestRefreshConfiguration(uint32_t changes) = 0;
107
108 /* Controls the vibrator of a particular input device. */
Nathaniel R. Lewiscacd69a2019-08-12 22:07:00 +0000109 virtual void vibrate(int32_t deviceId, const std::vector<VibrationElement>& pattern,
110 ssize_t repeat, int32_t token) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800111 virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
Arthur Hungc23540e2018-11-29 20:42:11 +0800112
113 /* Return true if the device can send input events to the specified display. */
114 virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800115};
116
Prabir Pradhan28efc192019-11-05 01:10:04 +0000117// --- InputReaderConfiguration ---
Prabir Pradhan29c95332018-11-14 20:14:11 -0800118
119/*
120 * Input reader configuration.
121 *
122 * Specifies various options that modify the behavior of the input reader.
123 */
124struct InputReaderConfiguration {
125 // Describes changes that have occurred.
126 enum {
127 // The pointer speed changed.
128 CHANGE_POINTER_SPEED = 1 << 0,
129
130 // The pointer gesture control changed.
131 CHANGE_POINTER_GESTURE_ENABLEMENT = 1 << 1,
132
133 // The display size or orientation changed.
134 CHANGE_DISPLAY_INFO = 1 << 2,
135
136 // The visible touches option changed.
137 CHANGE_SHOW_TOUCHES = 1 << 3,
138
139 // The keyboard layouts must be reloaded.
140 CHANGE_KEYBOARD_LAYOUTS = 1 << 4,
141
142 // The device name alias supplied by the may have changed for some devices.
143 CHANGE_DEVICE_ALIAS = 1 << 5,
144
145 // The location calibration matrix changed.
146 CHANGE_TOUCH_AFFINE_TRANSFORMATION = 1 << 6,
147
148 // The presence of an external stylus has changed.
149 CHANGE_EXTERNAL_STYLUS_PRESENCE = 1 << 7,
150
151 // The pointer capture mode has changed.
152 CHANGE_POINTER_CAPTURE = 1 << 8,
153
154 // The set of disabled input devices (disabledDevices) has changed.
155 CHANGE_ENABLED_STATE = 1 << 9,
156
157 // All devices must be reopened.
158 CHANGE_MUST_REOPEN = 1 << 31,
159 };
160
161 // Gets the amount of time to disable virtual keys after the screen is touched
162 // in order to filter out accidental virtual key presses due to swiping gestures
163 // or taps near the edge of the display. May be 0 to disable the feature.
164 nsecs_t virtualKeyQuietTime;
165
166 // The excluded device names for the platform.
167 // Devices with these names will be ignored.
168 std::vector<std::string> excludedDeviceNames;
169
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700170 // The associations between input ports and display ports.
171 // Used to determine which DisplayViewport should be tied to which InputDevice.
172 std::unordered_map<std::string, uint8_t> portAssociations;
173
Garfield Tan888a6a42020-01-09 11:39:16 -0800174 // The suggested display ID to show the cursor.
175 int32_t defaultPointerDisplayId;
176
Prabir Pradhan29c95332018-11-14 20:14:11 -0800177 // Velocity control parameters for mouse pointer movements.
178 VelocityControlParameters pointerVelocityControlParameters;
179
180 // Velocity control parameters for mouse wheel movements.
181 VelocityControlParameters wheelVelocityControlParameters;
182
183 // True if pointer gestures are enabled.
184 bool pointerGesturesEnabled;
185
186 // Quiet time between certain pointer gesture transitions.
187 // Time to allow for all fingers or buttons to settle into a stable state before
188 // starting a new gesture.
189 nsecs_t pointerGestureQuietInterval;
190
191 // The minimum speed that a pointer must travel for us to consider switching the active
192 // touch pointer to it during a drag. This threshold is set to avoid switching due
193 // to noise from a finger resting on the touch pad (perhaps just pressing it down).
194 float pointerGestureDragMinSwitchSpeed; // in pixels per second
195
196 // Tap gesture delay time.
197 // The time between down and up must be less than this to be considered a tap.
198 nsecs_t pointerGestureTapInterval;
199
200 // Tap drag gesture delay time.
201 // The time between the previous tap's up and the next down must be less than
202 // this to be considered a drag. Otherwise, the previous tap is finished and a
203 // new tap begins.
204 //
205 // Note that the previous tap will be held down for this entire duration so this
206 // interval must be shorter than the long press timeout.
207 nsecs_t pointerGestureTapDragInterval;
208
209 // The distance in pixels that the pointer is allowed to move from initial down
210 // to up and still be called a tap.
211 float pointerGestureTapSlop; // in pixels
212
213 // Time after the first touch points go down to settle on an initial centroid.
214 // This is intended to be enough time to handle cases where the user puts down two
215 // fingers at almost but not quite exactly the same time.
216 nsecs_t pointerGestureMultitouchSettleInterval;
217
218 // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
219 // at least two pointers have moved at least this far from their starting place.
220 float pointerGestureMultitouchMinDistance; // in pixels
221
222 // The transition from PRESS to SWIPE gesture mode can only occur when the
223 // cosine of the angle between the two vectors is greater than or equal to than this value
224 // which indicates that the vectors are oriented in the same direction.
225 // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
226 // (In exactly opposite directions, the cosine is -1.0.)
227 float pointerGestureSwipeTransitionAngleCosine;
228
229 // The transition from PRESS to SWIPE gesture mode can only occur when the
230 // fingers are no more than this far apart relative to the diagonal size of
231 // the touch pad. For example, a ratio of 0.5 means that the fingers must be
232 // no more than half the diagonal size of the touch pad apart.
233 float pointerGestureSwipeMaxWidthRatio;
234
235 // The gesture movement speed factor relative to the size of the display.
236 // Movement speed applies when the fingers are moving in the same direction.
237 // Without acceleration, a full swipe of the touch pad diagonal in movement mode
238 // will cover this portion of the display diagonal.
239 float pointerGestureMovementSpeedRatio;
240
241 // The gesture zoom speed factor relative to the size of the display.
242 // Zoom speed applies when the fingers are mostly moving relative to each other
243 // to execute a scale gesture or similar.
244 // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
245 // will cover this portion of the display diagonal.
246 float pointerGestureZoomSpeedRatio;
247
248 // True to show the location of touches on the touch screen as spots.
249 bool showTouches;
250
251 // True if pointer capture is enabled.
252 bool pointerCapture;
253
254 // The set of currently disabled input devices.
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000255 std::set<int32_t> disabledDevices;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800256
257 InputReaderConfiguration() :
258 virtualKeyQuietTime(0),
259 pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f, 3.0f),
260 wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
261 pointerGesturesEnabled(true),
262 pointerGestureQuietInterval(100 * 1000000LL), // 100 ms
263 pointerGestureDragMinSwitchSpeed(50), // 50 pixels per second
264 pointerGestureTapInterval(150 * 1000000LL), // 150 ms
265 pointerGestureTapDragInterval(150 * 1000000LL), // 150 ms
266 pointerGestureTapSlop(10.0f), // 10 pixels
267 pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
268 pointerGestureMultitouchMinDistance(15), // 15 pixels
269 pointerGestureSwipeTransitionAngleCosine(0.2588f), // cosine of 75 degrees
270 pointerGestureSwipeMaxWidthRatio(0.25f),
271 pointerGestureMovementSpeedRatio(0.8f),
272 pointerGestureZoomSpeedRatio(0.3f),
Siarhei Vishniakou4b057032019-01-30 19:43:49 -0800273 showTouches(false), pointerCapture(false) { }
Prabir Pradhan29c95332018-11-14 20:14:11 -0800274
Siarhei Vishniakouc5ae0dc2019-07-10 15:51:18 -0700275 static std::string changesToString(uint32_t changes);
276
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700277 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const;
278 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueDisplayId)
279 const;
280 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t physicalPort) const;
Garfield Tan888a6a42020-01-09 11:39:16 -0800281 std::optional<DisplayViewport> getDisplayViewportById(int32_t displayId) const;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800282 void setDisplayViewports(const std::vector<DisplayViewport>& viewports);
283
284
285 void dump(std::string& dump) const;
286 void dumpViewport(std::string& dump, const DisplayViewport& viewport) const;
287
288private:
289 std::vector<DisplayViewport> mDisplays;
290};
291
Prabir Pradhan28efc192019-11-05 01:10:04 +0000292// --- TouchAffineTransformation ---
293
Prabir Pradhan29c95332018-11-14 20:14:11 -0800294struct TouchAffineTransformation {
295 float x_scale;
296 float x_ymix;
297 float x_offset;
298 float y_xmix;
299 float y_scale;
300 float y_offset;
301
302 TouchAffineTransformation() :
303 x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
304 y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
305 }
306
307 TouchAffineTransformation(float xscale, float xymix, float xoffset,
308 float yxmix, float yscale, float yoffset) :
309 x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
310 y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
311 }
312
313 void applyTo(float& x, float& y) const;
314};
315
Prabir Pradhan28efc192019-11-05 01:10:04 +0000316// --- InputReaderPolicyInterface ---
317
Prabir Pradhan29c95332018-11-14 20:14:11 -0800318/*
319 * Input reader policy interface.
320 *
321 * The input reader policy is used by the input reader to interact with the Window Manager
322 * and other system components.
323 *
324 * The actual implementation is partially supported by callbacks into the DVM
325 * via JNI. This interface is also mocked in the unit tests.
326 *
Prabir Pradhan28efc192019-11-05 01:10:04 +0000327 * These methods will NOT re-enter the input reader interface, so they may be called from
328 * any method in the input reader interface.
Prabir Pradhan29c95332018-11-14 20:14:11 -0800329 */
330class InputReaderPolicyInterface : public virtual RefBase {
331protected:
332 InputReaderPolicyInterface() { }
333 virtual ~InputReaderPolicyInterface() { }
334
335public:
336 /* Gets the input reader configuration. */
337 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
338
339 /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
Michael Wright17db18e2020-06-26 20:51:44 +0100340 virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(
341 int32_t deviceId) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800342
343 /* Notifies the input reader policy that some input devices have changed
344 * and provides information about all current input devices.
345 */
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800346 virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800347
348 /* Gets the keyboard layout for a particular input device. */
349 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(
350 const InputDeviceIdentifier& identifier) = 0;
351
352 /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
353 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
354
355 /* Gets the affine calibration associated with the specified device. */
356 virtual TouchAffineTransformation getTouchAffineTransformation(
357 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) = 0;
Prabir Pradhan29c95332018-11-14 20:14:11 -0800358};
359
360} // namespace android
361
Garfield Tan888a6a42020-01-09 11:39:16 -0800362#endif // _UI_INPUT_READER_COMMON_H