1 /*
2  * Copyright (C) 2022 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 package com.android.server.input;
18 
19 import android.annotation.Nullable;
20 import android.hardware.display.DisplayViewport;
21 import android.hardware.input.InputSensorInfo;
22 import android.hardware.lights.Light;
23 import android.os.IBinder;
24 import android.os.MessageQueue;
25 import android.util.SparseArray;
26 import android.view.InputApplicationHandle;
27 import android.view.InputChannel;
28 import android.view.InputEvent;
29 import android.view.PointerIcon;
30 import android.view.VerifiedInputEvent;
31 import android.view.ViewConfiguration;
32 
33 import java.util.List;
34 
35 /**
36  * An interface for the native methods of InputManagerService. We use a public interface so that
37  * this can be mocked for testing by Mockito.
38  */
39 interface NativeInputManagerService {
40 
start()41     void start();
42 
setDisplayViewports(DisplayViewport[] viewports)43     void setDisplayViewports(DisplayViewport[] viewports);
44 
getScanCodeState(int deviceId, int sourceMask, int scanCode)45     int getScanCodeState(int deviceId, int sourceMask, int scanCode);
46 
getKeyCodeState(int deviceId, int sourceMask, int keyCode)47     int getKeyCodeState(int deviceId, int sourceMask, int keyCode);
48 
getSwitchState(int deviceId, int sourceMask, int sw)49     int getSwitchState(int deviceId, int sourceMask, int sw);
50 
addKeyRemapping(int deviceId, int fromKeyCode, int toKeyCode)51     void addKeyRemapping(int deviceId, int fromKeyCode, int toKeyCode);
52 
hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists)53     boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists);
54 
getKeyCodeForKeyLocation(int deviceId, int locationKeyCode)55     int getKeyCodeForKeyLocation(int deviceId, int locationKeyCode);
56 
createInputChannel(String name)57     InputChannel createInputChannel(String name);
58 
createInputMonitor(int displayId, String name, int pid)59     InputChannel createInputMonitor(int displayId, String name, int pid);
60 
removeInputChannel(IBinder connectionToken)61     void removeInputChannel(IBinder connectionToken);
62 
pilferPointers(IBinder token)63     void pilferPointers(IBinder token);
64 
setInputFilterEnabled(boolean enable)65     void setInputFilterEnabled(boolean enable);
66 
67     /**
68      * Set the touch mode state for the display passed as argument.
69      *
70      * @param inTouchMode   true if the device is in touch mode
71      * @param pid           the pid of the process that requested to switch touch mode state
72      * @param uid           the uid of the process that requested to switch touch mode state
73      * @param hasPermission if set to {@code true} then no further authorization will be performed
74      * @param displayId     the target display (ignored if device is configured with per display
75      *                      touch mode enabled)
76      * @return {@code true} if the touch mode was successfully changed, {@code false} otherwise
77      */
setInTouchMode(boolean inTouchMode, int pid, int uid, boolean hasPermission, int displayId)78     boolean setInTouchMode(boolean inTouchMode, int pid, int uid, boolean hasPermission,
79             int displayId);
80 
setMaximumObscuringOpacityForTouch(float opacity)81     void setMaximumObscuringOpacityForTouch(float opacity);
82 
83     /**
84      * Inject an input event into the system.
85      *
86      * @param event         the input event to inject
87      * @param injectIntoUid true if the event should target windows owned by uid, false otherwise
88      * @param uid           the uid whose windows should be targeted, if any
89      * @param syncMode      {@link android.os.InputEventInjectionSync}
90      * @param timeoutMillis timeout to wait for input injection to complete, in milliseconds
91      * @param policyFlags   defined in {@link android.view.WindowManagerPolicyConstants}
92      * @return {@link android.os.InputEventInjectionResult}
93      */
injectInputEvent(InputEvent event, boolean injectIntoUid, int uid, int syncMode, int timeoutMillis, int policyFlags)94     int injectInputEvent(InputEvent event, boolean injectIntoUid, int uid, int syncMode,
95             int timeoutMillis, int policyFlags);
96 
verifyInputEvent(InputEvent event)97     VerifiedInputEvent verifyInputEvent(InputEvent event);
98 
toggleCapsLock(int deviceId)99     void toggleCapsLock(int deviceId);
100 
displayRemoved(int displayId)101     void displayRemoved(int displayId);
102 
setInputDispatchMode(boolean enabled, boolean frozen)103     void setInputDispatchMode(boolean enabled, boolean frozen);
104 
setSystemUiLightsOut(boolean lightsOut)105     void setSystemUiLightsOut(boolean lightsOut);
106 
setFocusedApplication(int displayId, InputApplicationHandle application)107     void setFocusedApplication(int displayId, InputApplicationHandle application);
108 
setFocusedDisplay(int displayId)109     void setFocusedDisplay(int displayId);
110 
transferTouchFocus(IBinder fromChannelToken, IBinder toChannelToken, boolean isDragDrop)111     boolean transferTouchFocus(IBinder fromChannelToken, IBinder toChannelToken,
112             boolean isDragDrop);
113 
114     /**
115      * Transfer the current touch gesture to the window identified by 'destChannelToken' positioned
116      * on display with id 'displayId'.
117      */
transferTouch(IBinder destChannelToken, int displayId)118     boolean transferTouch(IBinder destChannelToken, int displayId);
119 
setPointerSpeed(int speed)120     void setPointerSpeed(int speed);
121 
setPointerAcceleration(float acceleration)122     void setPointerAcceleration(float acceleration);
123 
setTouchpadPointerSpeed(int speed)124     void setTouchpadPointerSpeed(int speed);
125 
setTouchpadNaturalScrollingEnabled(boolean enabled)126     void setTouchpadNaturalScrollingEnabled(boolean enabled);
127 
setTouchpadTapToClickEnabled(boolean enabled)128     void setTouchpadTapToClickEnabled(boolean enabled);
129 
setTouchpadRightClickZoneEnabled(boolean enabled)130     void setTouchpadRightClickZoneEnabled(boolean enabled);
131 
setShowTouches(boolean enabled)132     void setShowTouches(boolean enabled);
133 
setInteractive(boolean interactive)134     void setInteractive(boolean interactive);
135 
reloadCalibration()136     void reloadCalibration();
137 
vibrate(int deviceId, long[] pattern, int[] amplitudes, int repeat, int token)138     void vibrate(int deviceId, long[] pattern, int[] amplitudes, int repeat, int token);
139 
vibrateCombined(int deviceId, long[] pattern, SparseArray<int[]> amplitudes, int repeat, int token)140     void vibrateCombined(int deviceId, long[] pattern, SparseArray<int[]> amplitudes,
141             int repeat, int token);
142 
cancelVibrate(int deviceId, int token)143     void cancelVibrate(int deviceId, int token);
144 
isVibrating(int deviceId)145     boolean isVibrating(int deviceId);
146 
getVibratorIds(int deviceId)147     int[] getVibratorIds(int deviceId);
148 
getBatteryCapacity(int deviceId)149     int getBatteryCapacity(int deviceId);
150 
getBatteryStatus(int deviceId)151     int getBatteryStatus(int deviceId);
152 
153     /**
154      * Get the device path of the battery for an input device.
155      * @return the path for the input device battery, or null if there is none.
156      */
157     @Nullable
getBatteryDevicePath(int deviceId)158     String getBatteryDevicePath(int deviceId);
159 
getLights(int deviceId)160     List<Light> getLights(int deviceId);
161 
getLightPlayerId(int deviceId, int lightId)162     int getLightPlayerId(int deviceId, int lightId);
163 
getLightColor(int deviceId, int lightId)164     int getLightColor(int deviceId, int lightId);
165 
setLightPlayerId(int deviceId, int lightId, int playerId)166     void setLightPlayerId(int deviceId, int lightId, int playerId);
167 
setLightColor(int deviceId, int lightId, int color)168     void setLightColor(int deviceId, int lightId, int color);
169 
reloadKeyboardLayouts()170     void reloadKeyboardLayouts();
171 
reloadDeviceAliases()172     void reloadDeviceAliases();
173 
dump()174     String dump();
175 
monitor()176     void monitor();
177 
isInputDeviceEnabled(int deviceId)178     boolean isInputDeviceEnabled(int deviceId);
179 
enableInputDevice(int deviceId)180     void enableInputDevice(int deviceId);
181 
disableInputDevice(int deviceId)182     void disableInputDevice(int deviceId);
183 
setPointerIconType(int iconId)184     void setPointerIconType(int iconId);
185 
reloadPointerIcons()186     void reloadPointerIcons();
187 
setCustomPointerIcon(PointerIcon icon)188     void setCustomPointerIcon(PointerIcon icon);
189 
requestPointerCapture(IBinder windowToken, boolean enabled)190     void requestPointerCapture(IBinder windowToken, boolean enabled);
191 
canDispatchToDisplay(int deviceId, int displayId)192     boolean canDispatchToDisplay(int deviceId, int displayId);
193 
notifyPortAssociationsChanged()194     void notifyPortAssociationsChanged();
195 
changeUniqueIdAssociation()196     void changeUniqueIdAssociation();
197 
changeTypeAssociation()198     void changeTypeAssociation();
199 
changeKeyboardLayoutAssociation()200     void changeKeyboardLayoutAssociation();
201 
setDisplayEligibilityForPointerCapture(int displayId, boolean enabled)202     void setDisplayEligibilityForPointerCapture(int displayId, boolean enabled);
203 
setMotionClassifierEnabled(boolean enabled)204     void setMotionClassifierEnabled(boolean enabled);
205 
getSensorList(int deviceId)206     InputSensorInfo[] getSensorList(int deviceId);
207 
flushSensor(int deviceId, int sensorType)208     boolean flushSensor(int deviceId, int sensorType);
209 
enableSensor(int deviceId, int sensorType, int samplingPeriodUs, int maxBatchReportLatencyUs)210     boolean enableSensor(int deviceId, int sensorType, int samplingPeriodUs,
211             int maxBatchReportLatencyUs);
212 
disableSensor(int deviceId, int sensorType)213     void disableSensor(int deviceId, int sensorType);
214 
cancelCurrentTouch()215     void cancelCurrentTouch();
216 
217     /** Set the displayId on which the mouse cursor should be shown. */
setPointerDisplayId(int displayId)218     void setPointerDisplayId(int displayId);
219 
220     /** Get the bluetooth address of an input device if known, otherwise return null. */
getBluetoothAddress(int deviceId)221     String getBluetoothAddress(int deviceId);
222 
223     /** Set whether stylus button reporting through motion events should be enabled. */
setStylusButtonMotionEventsEnabled(boolean enabled)224     void setStylusButtonMotionEventsEnabled(boolean enabled);
225 
226     /**
227      * Get the current position of the mouse cursor.
228      *
229      * If the mouse cursor is not currently shown, the coordinate values will be NaN-s.
230      *
231      * NOTE: This will grab the PointerController's lock, so we must be careful about calling this
232      * from the InputReader or Display threads, which may result in a deadlock.
233      */
getMouseCursorPosition()234     float[] getMouseCursorPosition();
235 
236     /** Set whether showing a pointer icon for styluses is enabled. */
setStylusPointerIconEnabled(boolean enabled)237     void setStylusPointerIconEnabled(boolean enabled);
238 
239     /**
240      * Report sysfs node changes. This may result in recreation of the corresponding InputDevice.
241      * The recreated device may contain new associated peripheral devices like Light, Battery, etc.
242      */
sysfsNodeChanged(String sysfsNodePath)243     void sysfsNodeChanged(String sysfsNodePath);
244 
245     /**
246      * Notify there is a change in any of the key gesture timeouts, such as the key
247      * repeat timeout or key repeat delay.
248      *
249      * @see ViewConfiguration#getKeyRepeatTimeout()
250      * @see ViewConfiguration#getKeyRepeatDelay()
251      */
notifyKeyGestureTimeoutsChanged()252     void notifyKeyGestureTimeoutsChanged();
253 
254     /** The native implementation of InputManagerService methods. */
255     class NativeImpl implements NativeInputManagerService {
256         /** Pointer to native input manager service object, used by native code. */
257         @SuppressWarnings({"unused", "FieldCanBeLocal"})
258         private final long mPtr;
259 
NativeImpl(InputManagerService service, MessageQueue messageQueue)260         NativeImpl(InputManagerService service, MessageQueue messageQueue) {
261             mPtr = init(service, messageQueue);
262         }
263 
init(InputManagerService service, MessageQueue messageQueue)264         private native long init(InputManagerService service, MessageQueue messageQueue);
265 
266         @Override
start()267         public native void start();
268 
269         @Override
setDisplayViewports(DisplayViewport[] viewports)270         public native void setDisplayViewports(DisplayViewport[] viewports);
271 
272         @Override
getScanCodeState(int deviceId, int sourceMask, int scanCode)273         public native int getScanCodeState(int deviceId, int sourceMask, int scanCode);
274 
275         @Override
getKeyCodeState(int deviceId, int sourceMask, int keyCode)276         public native int getKeyCodeState(int deviceId, int sourceMask, int keyCode);
277 
278         @Override
getSwitchState(int deviceId, int sourceMask, int sw)279         public native int getSwitchState(int deviceId, int sourceMask, int sw);
280 
281         @Override
addKeyRemapping(int deviceId, int fromKeyCode, int toKeyCode)282         public native void addKeyRemapping(int deviceId, int fromKeyCode, int toKeyCode);
283 
284         @Override
hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists)285         public native boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes,
286                 boolean[] keyExists);
287 
288         @Override
getKeyCodeForKeyLocation(int deviceId, int locationKeyCode)289         public native int getKeyCodeForKeyLocation(int deviceId, int locationKeyCode);
290 
291         @Override
createInputChannel(String name)292         public native InputChannel createInputChannel(String name);
293 
294         @Override
createInputMonitor(int displayId, String name, int pid)295         public native InputChannel createInputMonitor(int displayId, String name, int pid);
296 
297         @Override
removeInputChannel(IBinder connectionToken)298         public native void removeInputChannel(IBinder connectionToken);
299 
300         @Override
pilferPointers(IBinder token)301         public native void pilferPointers(IBinder token);
302 
303         @Override
setInputFilterEnabled(boolean enable)304         public native void setInputFilterEnabled(boolean enable);
305 
306         @Override
setInTouchMode(boolean inTouchMode, int pid, int uid, boolean hasPermission, int displayId)307         public native boolean setInTouchMode(boolean inTouchMode, int pid, int uid,
308                 boolean hasPermission, int displayId);
309 
310         @Override
setMaximumObscuringOpacityForTouch(float opacity)311         public native void setMaximumObscuringOpacityForTouch(float opacity);
312 
313         @Override
injectInputEvent(InputEvent event, boolean injectIntoUid, int uid, int syncMode, int timeoutMillis, int policyFlags)314         public native int injectInputEvent(InputEvent event, boolean injectIntoUid, int uid,
315                 int syncMode, int timeoutMillis, int policyFlags);
316 
317         @Override
verifyInputEvent(InputEvent event)318         public native VerifiedInputEvent verifyInputEvent(InputEvent event);
319 
320         @Override
toggleCapsLock(int deviceId)321         public native void toggleCapsLock(int deviceId);
322 
323         @Override
displayRemoved(int displayId)324         public native void displayRemoved(int displayId);
325 
326         @Override
setInputDispatchMode(boolean enabled, boolean frozen)327         public native void setInputDispatchMode(boolean enabled, boolean frozen);
328 
329         @Override
setSystemUiLightsOut(boolean lightsOut)330         public native void setSystemUiLightsOut(boolean lightsOut);
331 
332         @Override
setFocusedApplication(int displayId, InputApplicationHandle application)333         public native void setFocusedApplication(int displayId, InputApplicationHandle application);
334 
335         @Override
setFocusedDisplay(int displayId)336         public native void setFocusedDisplay(int displayId);
337 
338         @Override
transferTouchFocus(IBinder fromChannelToken, IBinder toChannelToken, boolean isDragDrop)339         public native boolean transferTouchFocus(IBinder fromChannelToken, IBinder toChannelToken,
340                 boolean isDragDrop);
341 
342         @Override
transferTouch(IBinder destChannelToken, int displayId)343         public native boolean transferTouch(IBinder destChannelToken, int displayId);
344 
345         @Override
setPointerSpeed(int speed)346         public native void setPointerSpeed(int speed);
347 
348         @Override
setPointerAcceleration(float acceleration)349         public native void setPointerAcceleration(float acceleration);
350 
351         @Override
setTouchpadPointerSpeed(int speed)352         public native void setTouchpadPointerSpeed(int speed);
353 
354         @Override
setTouchpadNaturalScrollingEnabled(boolean enabled)355         public native void setTouchpadNaturalScrollingEnabled(boolean enabled);
356 
357         @Override
setTouchpadTapToClickEnabled(boolean enabled)358         public native void setTouchpadTapToClickEnabled(boolean enabled);
359 
360         @Override
setTouchpadRightClickZoneEnabled(boolean enabled)361         public native void setTouchpadRightClickZoneEnabled(boolean enabled);
362 
363         @Override
setShowTouches(boolean enabled)364         public native void setShowTouches(boolean enabled);
365 
366         @Override
setInteractive(boolean interactive)367         public native void setInteractive(boolean interactive);
368 
369         @Override
reloadCalibration()370         public native void reloadCalibration();
371 
372         @Override
vibrate(int deviceId, long[] pattern, int[] amplitudes, int repeat, int token)373         public native void vibrate(int deviceId, long[] pattern, int[] amplitudes, int repeat,
374                 int token);
375 
376         @Override
vibrateCombined(int deviceId, long[] pattern, SparseArray<int[]> amplitudes, int repeat, int token)377         public native void vibrateCombined(int deviceId, long[] pattern,
378                 SparseArray<int[]> amplitudes,
379                 int repeat, int token);
380 
381         @Override
cancelVibrate(int deviceId, int token)382         public native void cancelVibrate(int deviceId, int token);
383 
384         @Override
isVibrating(int deviceId)385         public native boolean isVibrating(int deviceId);
386 
387         @Override
getVibratorIds(int deviceId)388         public native int[] getVibratorIds(int deviceId);
389 
390         @Override
getBatteryCapacity(int deviceId)391         public native int getBatteryCapacity(int deviceId);
392 
393         @Override
getBatteryStatus(int deviceId)394         public native int getBatteryStatus(int deviceId);
395 
396         @Override
getBatteryDevicePath(int deviceId)397         public native String getBatteryDevicePath(int deviceId);
398 
399         @Override
getLights(int deviceId)400         public native List<Light> getLights(int deviceId);
401 
402         @Override
getLightPlayerId(int deviceId, int lightId)403         public native int getLightPlayerId(int deviceId, int lightId);
404 
405         @Override
getLightColor(int deviceId, int lightId)406         public native int getLightColor(int deviceId, int lightId);
407 
408         @Override
setLightPlayerId(int deviceId, int lightId, int playerId)409         public native void setLightPlayerId(int deviceId, int lightId, int playerId);
410 
411         @Override
setLightColor(int deviceId, int lightId, int color)412         public native void setLightColor(int deviceId, int lightId, int color);
413 
414         @Override
reloadKeyboardLayouts()415         public native void reloadKeyboardLayouts();
416 
417         @Override
reloadDeviceAliases()418         public native void reloadDeviceAliases();
419 
420         @Override
dump()421         public native String dump();
422 
423         @Override
monitor()424         public native void monitor();
425 
426         @Override
isInputDeviceEnabled(int deviceId)427         public native boolean isInputDeviceEnabled(int deviceId);
428 
429         @Override
enableInputDevice(int deviceId)430         public native void enableInputDevice(int deviceId);
431 
432         @Override
disableInputDevice(int deviceId)433         public native void disableInputDevice(int deviceId);
434 
435         @Override
setPointerIconType(int iconId)436         public native void setPointerIconType(int iconId);
437 
438         @Override
reloadPointerIcons()439         public native void reloadPointerIcons();
440 
441         @Override
setCustomPointerIcon(PointerIcon icon)442         public native void setCustomPointerIcon(PointerIcon icon);
443 
444         @Override
requestPointerCapture(IBinder windowToken, boolean enabled)445         public native void requestPointerCapture(IBinder windowToken, boolean enabled);
446 
447         @Override
canDispatchToDisplay(int deviceId, int displayId)448         public native boolean canDispatchToDisplay(int deviceId, int displayId);
449 
450         @Override
notifyPortAssociationsChanged()451         public native void notifyPortAssociationsChanged();
452 
453         @Override
changeUniqueIdAssociation()454         public native void changeUniqueIdAssociation();
455 
456         @Override
changeTypeAssociation()457         public native void changeTypeAssociation();
458 
459         @Override
changeKeyboardLayoutAssociation()460         public native void changeKeyboardLayoutAssociation();
461 
462         @Override
setDisplayEligibilityForPointerCapture(int displayId, boolean enabled)463         public native void setDisplayEligibilityForPointerCapture(int displayId, boolean enabled);
464 
465         @Override
setMotionClassifierEnabled(boolean enabled)466         public native void setMotionClassifierEnabled(boolean enabled);
467 
468         @Override
getSensorList(int deviceId)469         public native InputSensorInfo[] getSensorList(int deviceId);
470 
471         @Override
flushSensor(int deviceId, int sensorType)472         public native boolean flushSensor(int deviceId, int sensorType);
473 
474         @Override
enableSensor(int deviceId, int sensorType, int samplingPeriodUs, int maxBatchReportLatencyUs)475         public native boolean enableSensor(int deviceId, int sensorType, int samplingPeriodUs,
476                 int maxBatchReportLatencyUs);
477 
478         @Override
disableSensor(int deviceId, int sensorType)479         public native void disableSensor(int deviceId, int sensorType);
480 
481         @Override
cancelCurrentTouch()482         public native void cancelCurrentTouch();
483 
484         @Override
setPointerDisplayId(int displayId)485         public native void setPointerDisplayId(int displayId);
486 
487         @Override
getBluetoothAddress(int deviceId)488         public native String getBluetoothAddress(int deviceId);
489 
490         @Override
setStylusButtonMotionEventsEnabled(boolean enabled)491         public native void setStylusButtonMotionEventsEnabled(boolean enabled);
492 
493         @Override
getMouseCursorPosition()494         public native float[] getMouseCursorPosition();
495 
496         @Override
setStylusPointerIconEnabled(boolean enabled)497         public native void setStylusPointerIconEnabled(boolean enabled);
498 
499         @Override
sysfsNodeChanged(String sysfsNodePath)500         public native void sysfsNodeChanged(String sysfsNodePath);
501 
502         @Override
notifyKeyGestureTimeoutsChanged()503         public native void notifyKeyGestureTimeoutsChanged();
504     }
505 }
506