• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #pragma once
18 
19 #include <android/os/IInputConstants.h>
20 #include <input/DisplayViewport.h>
21 #include <input/Input.h>
22 #include <input/InputDevice.h>
23 #include <input/VelocityControl.h>
24 #include <input/VelocityTracker.h>
25 #include <stddef.h>
26 #include <ui/Rotation.h>
27 #include <unistd.h>
28 #include <utils/Errors.h>
29 #include <utils/RefBase.h>
30 
31 #include <optional>
32 #include <set>
33 #include <unordered_map>
34 #include <vector>
35 
36 #include "PointerControllerInterface.h"
37 #include "VibrationElement.h"
38 
39 // Maximum supported size of a vibration pattern.
40 // Must be at least 2.
41 #define MAX_VIBRATE_PATTERN_SIZE 100
42 
43 namespace android {
44 
45 // --- InputReaderConfiguration ---
46 
47 /*
48  * Input reader configuration.
49  *
50  * Specifies various options that modify the behavior of the input reader.
51  */
52 struct InputReaderConfiguration {
53     // Describes changes that have occurred.
54     enum class Change : uint32_t {
55         // The mouse pointer speed changed.
56         POINTER_SPEED = 1u << 0,
57 
58         // The pointer gesture control changed.
59         POINTER_GESTURE_ENABLEMENT = 1u << 1,
60 
61         // The display size or orientation changed.
62         DISPLAY_INFO = 1u << 2,
63 
64         // The visible touches option changed.
65         SHOW_TOUCHES = 1u << 3,
66 
67         // The keyboard layouts must be reloaded.
68         KEYBOARD_LAYOUTS = 1u << 4,
69 
70         // The device name alias supplied by the may have changed for some devices.
71         DEVICE_ALIAS = 1u << 5,
72 
73         // The location calibration matrix changed.
74         TOUCH_AFFINE_TRANSFORMATION = 1u << 6,
75 
76         // The presence of an external stylus has changed.
77         EXTERNAL_STYLUS_PRESENCE = 1u << 7,
78 
79         // The pointer capture mode has changed.
80         POINTER_CAPTURE = 1u << 8,
81 
82         // The set of disabled input devices (disabledDevices) has changed.
83         ENABLED_STATE = 1u << 9,
84 
85         // The device type has been updated.
86         DEVICE_TYPE = 1u << 10,
87 
88         // The keyboard layout association has changed.
89         KEYBOARD_LAYOUT_ASSOCIATION = 1u << 11,
90 
91         // The stylus button reporting configurations has changed.
92         STYLUS_BUTTON_REPORTING = 1u << 12,
93 
94         // The touchpad settings changed.
95         TOUCHPAD_SETTINGS = 1u << 13,
96 
97         // All devices must be reopened.
98         MUST_REOPEN = 1u << 31,
99     };
100 
101     // Gets the amount of time to disable virtual keys after the screen is touched
102     // in order to filter out accidental virtual key presses due to swiping gestures
103     // or taps near the edge of the display.  May be 0 to disable the feature.
104     nsecs_t virtualKeyQuietTime;
105 
106     // The excluded device names for the platform.
107     // Devices with these names will be ignored.
108     std::vector<std::string> excludedDeviceNames;
109 
110     // The associations between input ports and display ports.
111     // Used to determine which DisplayViewport should be tied to which InputDevice.
112     std::unordered_map<std::string, uint8_t> portAssociations;
113 
114     // The associations between input device physical port locations and display unique ids.
115     // Used to determine which DisplayViewport should be tied to which InputDevice.
116     std::unordered_map<std::string, std::string> uniqueIdAssociations;
117 
118     // The associations between input device ports device types.
119     // This is used to determine which device type and source should be tied to which InputDevice.
120     std::unordered_map<std::string, std::string> deviceTypeAssociations;
121 
122     // The map from the input device physical port location to the input device layout info.
123     // Can be used to determine the layout of the keyboard device.
124     std::unordered_map<std::string, KeyboardLayoutInfo> keyboardLayoutAssociations;
125 
126     // The suggested display ID to show the cursor.
127     int32_t defaultPointerDisplayId;
128 
129     // Velocity control parameters for mouse pointer movements.
130     VelocityControlParameters pointerVelocityControlParameters;
131 
132     // Velocity control parameters for mouse wheel movements.
133     VelocityControlParameters wheelVelocityControlParameters;
134 
135     // True if pointer gestures are enabled.
136     bool pointerGesturesEnabled;
137 
138     // Quiet time between certain pointer gesture transitions.
139     // Time to allow for all fingers or buttons to settle into a stable state before
140     // starting a new gesture.
141     nsecs_t pointerGestureQuietInterval;
142 
143     // The minimum speed that a pointer must travel for us to consider switching the active
144     // touch pointer to it during a drag.  This threshold is set to avoid switching due
145     // to noise from a finger resting on the touch pad (perhaps just pressing it down).
146     float pointerGestureDragMinSwitchSpeed; // in pixels per second
147 
148     // Tap gesture delay time.
149     // The time between down and up must be less than this to be considered a tap.
150     nsecs_t pointerGestureTapInterval;
151 
152     // Tap drag gesture delay time.
153     // The time between the previous tap's up and the next down must be less than
154     // this to be considered a drag.  Otherwise, the previous tap is finished and a
155     // new tap begins.
156     //
157     // Note that the previous tap will be held down for this entire duration so this
158     // interval must be shorter than the long press timeout.
159     nsecs_t pointerGestureTapDragInterval;
160 
161     // The distance in pixels that the pointer is allowed to move from initial down
162     // to up and still be called a tap.
163     float pointerGestureTapSlop; // in pixels
164 
165     // Time after the first touch points go down to settle on an initial centroid.
166     // This is intended to be enough time to handle cases where the user puts down two
167     // fingers at almost but not quite exactly the same time.
168     nsecs_t pointerGestureMultitouchSettleInterval;
169 
170     // The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
171     // at least two pointers have moved at least this far from their starting place.
172     float pointerGestureMultitouchMinDistance; // in pixels
173 
174     // The transition from PRESS to SWIPE gesture mode can only occur when the
175     // cosine of the angle between the two vectors is greater than or equal to than this value
176     // which indicates that the vectors are oriented in the same direction.
177     // When the vectors are oriented in the exactly same direction, the cosine is 1.0.
178     // (In exactly opposite directions, the cosine is -1.0.)
179     float pointerGestureSwipeTransitionAngleCosine;
180 
181     // The transition from PRESS to SWIPE gesture mode can only occur when the
182     // fingers are no more than this far apart relative to the diagonal size of
183     // the touch pad.  For example, a ratio of 0.5 means that the fingers must be
184     // no more than half the diagonal size of the touch pad apart.
185     float pointerGestureSwipeMaxWidthRatio;
186 
187     // The gesture movement speed factor relative to the size of the display.
188     // Movement speed applies when the fingers are moving in the same direction.
189     // Without acceleration, a full swipe of the touch pad diagonal in movement mode
190     // will cover this portion of the display diagonal.
191     float pointerGestureMovementSpeedRatio;
192 
193     // The gesture zoom speed factor relative to the size of the display.
194     // Zoom speed applies when the fingers are mostly moving relative to each other
195     // to execute a scale gesture or similar.
196     // Without acceleration, a full swipe of the touch pad diagonal in zoom mode
197     // will cover this portion of the display diagonal.
198     float pointerGestureZoomSpeedRatio;
199 
200     // True to show the location of touches on the touch screen as spots.
201     bool showTouches;
202 
203     // The latest request to enable or disable Pointer Capture.
204     PointerCaptureRequest pointerCaptureRequest;
205 
206     // The touchpad pointer speed, as a number from -7 (slowest) to 7 (fastest).
207     int32_t touchpadPointerSpeed;
208 
209     // True to invert the touchpad scrolling direction, so that moving two fingers downwards on the
210     // touchpad scrolls the content upwards.
211     bool touchpadNaturalScrollingEnabled;
212 
213     // True to enable tap-to-click on touchpads.
214     bool touchpadTapToClickEnabled;
215 
216     // True to enable a zone on the right-hand side of touchpads where clicks will be turned into
217     // context (a.k.a. "right") clicks.
218     bool touchpadRightClickZoneEnabled;
219 
220     // The set of currently disabled input devices.
221     std::set<int32_t> disabledDevices;
222 
223     // True if stylus button reporting through motion events should be enabled, in which case
224     // stylus button state changes are reported through motion events.
225     bool stylusButtonMotionEventsEnabled;
226 
227     // True if a pointer icon should be shown for direct stylus pointers.
228     bool stylusPointerIconEnabled;
229 
InputReaderConfigurationInputReaderConfiguration230     InputReaderConfiguration()
231           : virtualKeyQuietTime(0),
232             pointerVelocityControlParameters(1.0f, 500.0f, 3000.0f,
233                                              static_cast<float>(
234                                                      android::os::IInputConstants::
235                                                              DEFAULT_POINTER_ACCELERATION)),
236             wheelVelocityControlParameters(1.0f, 15.0f, 50.0f, 4.0f),
237             pointerGesturesEnabled(true),
238             pointerGestureQuietInterval(100 * 1000000LL),            // 100 ms
239             pointerGestureDragMinSwitchSpeed(50),                    // 50 pixels per second
240             pointerGestureTapInterval(150 * 1000000LL),              // 150 ms
241             pointerGestureTapDragInterval(150 * 1000000LL),          // 150 ms
242             pointerGestureTapSlop(10.0f),                            // 10 pixels
243             pointerGestureMultitouchSettleInterval(100 * 1000000LL), // 100 ms
244             pointerGestureMultitouchMinDistance(15),                 // 15 pixels
245             pointerGestureSwipeTransitionAngleCosine(0.2588f),       // cosine of 75 degrees
246             pointerGestureSwipeMaxWidthRatio(0.25f),
247             pointerGestureMovementSpeedRatio(0.8f),
248             pointerGestureZoomSpeedRatio(0.3f),
249             showTouches(false),
250             pointerCaptureRequest(),
251             touchpadPointerSpeed(0),
252             touchpadNaturalScrollingEnabled(true),
253             touchpadTapToClickEnabled(true),
254             touchpadRightClickZoneEnabled(false),
255             stylusButtonMotionEventsEnabled(true),
256             stylusPointerIconEnabled(false) {}
257 
258     std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const;
259     std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueDisplayId)
260             const;
261     std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t physicalPort) const;
262     std::optional<DisplayViewport> getDisplayViewportById(int32_t displayId) const;
263     void setDisplayViewports(const std::vector<DisplayViewport>& viewports);
264 
265     void dump(std::string& dump) const;
266     void dumpViewport(std::string& dump, const DisplayViewport& viewport) const;
267 
268 private:
269     std::vector<DisplayViewport> mDisplays;
270 };
271 
272 using ConfigurationChanges = ftl::Flags<InputReaderConfiguration::Change>;
273 
274 // --- InputReaderInterface ---
275 
276 /* The interface for the InputReader shared library.
277  *
278  * Manages one or more threads that process raw input events and sends cooked event data to an
279  * input listener.
280  *
281  * The implementation must guarantee thread safety for this interface. However, since the input
282  * listener is NOT thread safe, all calls to the listener must happen from the same thread.
283  */
284 class InputReaderInterface {
285 public:
InputReaderInterface()286     InputReaderInterface() {}
~InputReaderInterface()287     virtual ~InputReaderInterface() {}
288     /* Dumps the state of the input reader.
289      *
290      * This method may be called on any thread (usually by the input manager). */
291     virtual void dump(std::string& dump) = 0;
292 
293     /* Called by the heartbeat to ensures that the reader has not deadlocked. */
294     virtual void monitor() = 0;
295 
296     /* Returns true if the input device is enabled. */
297     virtual bool isInputDeviceEnabled(int32_t deviceId) = 0;
298 
299     /* Makes the reader start processing events from the kernel. */
300     virtual status_t start() = 0;
301 
302     /* Makes the reader stop processing any more events. */
303     virtual status_t stop() = 0;
304 
305     /* Gets information about all input devices.
306      *
307      * This method may be called on any thread (usually by the input manager).
308      */
309     virtual std::vector<InputDeviceInfo> getInputDevices() const = 0;
310 
311     /* Query current input state. */
312     virtual int32_t getScanCodeState(int32_t deviceId, uint32_t sourceMask, int32_t scanCode) = 0;
313     virtual int32_t getKeyCodeState(int32_t deviceId, uint32_t sourceMask, int32_t keyCode) = 0;
314     virtual int32_t getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t sw) = 0;
315 
316     virtual void addKeyRemapping(int32_t deviceId, int32_t fromKeyCode,
317                                  int32_t toKeyCode) const = 0;
318 
319     virtual int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const = 0;
320 
321     /* Toggle Caps Lock */
322     virtual void toggleCapsLockState(int32_t deviceId) = 0;
323 
324     /* Determine whether physical keys exist for the given framework-domain key codes. */
325     virtual bool hasKeys(int32_t deviceId, uint32_t sourceMask,
326                          const std::vector<int32_t>& keyCodes, uint8_t* outFlags) = 0;
327 
328     /* Requests that a reconfiguration of all input devices.
329      * The changes flag is a bitfield that indicates what has changed and whether
330      * the input devices must all be reopened. */
331     virtual void requestRefreshConfiguration(ConfigurationChanges changes) = 0;
332 
333     /* Controls the vibrator of a particular input device. */
334     virtual void vibrate(int32_t deviceId, const VibrationSequence& sequence, ssize_t repeat,
335                          int32_t token) = 0;
336     virtual void cancelVibrate(int32_t deviceId, int32_t token) = 0;
337 
338     virtual bool isVibrating(int32_t deviceId) = 0;
339 
340     virtual std::vector<int32_t> getVibratorIds(int32_t deviceId) = 0;
341     /* Get battery level of a particular input device. */
342     virtual std::optional<int32_t> getBatteryCapacity(int32_t deviceId) = 0;
343     /* Get battery status of a particular input device. */
344     virtual std::optional<int32_t> getBatteryStatus(int32_t deviceId) = 0;
345     /* Get the device path for the battery of an input device. */
346     virtual std::optional<std::string> getBatteryDevicePath(int32_t deviceId) = 0;
347 
348     virtual std::vector<InputDeviceLightInfo> getLights(int32_t deviceId) = 0;
349 
350     virtual std::vector<InputDeviceSensorInfo> getSensors(int32_t deviceId) = 0;
351 
352     /* Return true if the device can send input events to the specified display. */
353     virtual bool canDispatchToDisplay(int32_t deviceId, int32_t displayId) = 0;
354 
355     /* Enable sensor in input reader mapper. */
356     virtual bool enableSensor(int32_t deviceId, InputDeviceSensorType sensorType,
357                               std::chrono::microseconds samplingPeriod,
358                               std::chrono::microseconds maxBatchReportLatency) = 0;
359 
360     /* Disable sensor in input reader mapper. */
361     virtual void disableSensor(int32_t deviceId, InputDeviceSensorType sensorType) = 0;
362 
363     /* Flush sensor data in input reader mapper. */
364     virtual void flushSensor(int32_t deviceId, InputDeviceSensorType sensorType) = 0;
365 
366     /* Set color for the light */
367     virtual bool setLightColor(int32_t deviceId, int32_t lightId, int32_t color) = 0;
368     /* Set player ID for the light */
369     virtual bool setLightPlayerId(int32_t deviceId, int32_t lightId, int32_t playerId) = 0;
370     /* Get light color */
371     virtual std::optional<int32_t> getLightColor(int32_t deviceId, int32_t lightId) = 0;
372     /* Get light player ID */
373     virtual std::optional<int32_t> getLightPlayerId(int32_t deviceId, int32_t lightId) = 0;
374 
375     /* Get the Bluetooth address of an input device, if known. */
376     virtual std::optional<std::string> getBluetoothAddress(int32_t deviceId) const = 0;
377 
378     /* Sysfs node change reported. Recreate device if required to incorporate the new sysfs nodes */
379     virtual void sysfsNodeChanged(const std::string& sysfsNodePath) = 0;
380 };
381 
382 // --- TouchAffineTransformation ---
383 
384 struct TouchAffineTransformation {
385     float x_scale;
386     float x_ymix;
387     float x_offset;
388     float y_xmix;
389     float y_scale;
390     float y_offset;
391 
TouchAffineTransformationTouchAffineTransformation392     TouchAffineTransformation() :
393         x_scale(1.0f), x_ymix(0.0f), x_offset(0.0f),
394         y_xmix(0.0f), y_scale(1.0f), y_offset(0.0f) {
395     }
396 
TouchAffineTransformationTouchAffineTransformation397     TouchAffineTransformation(float xscale, float xymix, float xoffset,
398             float yxmix, float yscale, float yoffset) :
399         x_scale(xscale), x_ymix(xymix), x_offset(xoffset),
400         y_xmix(yxmix), y_scale(yscale), y_offset(yoffset) {
401     }
402 
403     void applyTo(float& x, float& y) const;
404 };
405 
406 // --- InputReaderPolicyInterface ---
407 
408 /*
409  * Input reader policy interface.
410  *
411  * The input reader policy is used by the input reader to interact with the Window Manager
412  * and other system components.
413  *
414  * The actual implementation is partially supported by callbacks into the DVM
415  * via JNI.  This interface is also mocked in the unit tests.
416  *
417  * These methods will NOT re-enter the input reader interface, so they may be called from
418  * any method in the input reader interface.
419  */
420 class InputReaderPolicyInterface : public virtual RefBase {
421 protected:
InputReaderPolicyInterface()422     InputReaderPolicyInterface() { }
~InputReaderPolicyInterface()423     virtual ~InputReaderPolicyInterface() { }
424 
425 public:
426     /* Gets the input reader configuration. */
427     virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
428 
429     /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
430     virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(
431             int32_t deviceId) = 0;
432 
433     /* Notifies the input reader policy that some input devices have changed
434      * and provides information about all current input devices.
435      */
436     virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) = 0;
437 
438     /* Gets the keyboard layout for a particular input device. */
439     virtual std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
440             const InputDeviceIdentifier& identifier) = 0;
441 
442     /* Gets a user-supplied alias for a particular input device, or an empty string if none. */
443     virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier) = 0;
444 
445     /* Gets the affine calibration associated with the specified device. */
446     virtual TouchAffineTransformation getTouchAffineTransformation(
447             const std::string& inputDeviceDescriptor, ui::Rotation surfaceRotation) = 0;
448     /* Notifies the input reader policy that a stylus gesture has started. */
449     virtual void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) = 0;
450 
451     /* Returns true if any InputConnection is currently active. */
452     virtual bool isInputMethodConnectionActive() = 0;
453 };
454 
455 } // namespace android
456