blob: ff0b0d6f6eaf1e716682839010b8e380b4a184ab [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 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#define LOG_TAG "InputManager-JNI"
18
Michael Wrightbafea6e2017-06-12 15:25:47 +010019#define ATRACE_TAG ATRACE_TAG_INPUT
20
Jeff Brown9c3cda02010-06-15 01:31:58 -070021//#define LOG_NDEBUG 0
22
23// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070024#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070025
26// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070027#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070028
Jeff Brown83c09682010-12-23 17:50:18 -080029
Steven Moreland2279b252017-07-19 09:50:45 -070030#include <nativehelper/JNIHelp.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031#include "jni.h"
Michael Wrighta4051212015-07-23 17:04:40 +010032#include <atomic>
33#include <cinttypes>
Jeff Brown349703e2010-06-22 01:27:15 -070034#include <limits.h>
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -070035#include <android-base/parseint.h>
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080036#include <android-base/stringprintf.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070037#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070038#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039
Jeff Brown46b9ac02010-04-22 18:58:52 -070040#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080041#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070042#include <utils/threads.h>
Michael Wrightbafea6e2017-06-12 15:25:47 +010043#include <utils/Trace.h>
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070044#include <utils/SortedVector.h>
Jeff Brown83c09682010-12-23 17:50:18 -080045
Robert Carr788f5742018-07-30 17:46:45 -070046#include <binder/IServiceManager.h>
47
Jeff Brownb4ff35d2011-01-02 16:37:43 -080048#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070049#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080050
Michael Wrightd6b473712014-02-10 15:56:36 -080051#include <inputflinger/InputManager.h>
52
Jeff Brown05dc66a2011-03-02 14:41:58 -080053#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070054#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080055#include <android_view_KeyEvent.h>
56#include <android_view_MotionEvent.h>
57#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070058#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080059#include <android/graphics/GraphicsJNI.h>
60
Steven Moreland2279b252017-07-19 09:50:45 -070061#include <nativehelper/ScopedLocalRef.h>
62#include <nativehelper/ScopedPrimitiveArray.h>
63#include <nativehelper/ScopedUtfChars.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070064
Jeff Brown4f8ecd82012-06-18 18:29:13 -070065#include "com_android_server_power_PowerManagerService.h"
Robert Carre1db3202018-07-23 15:24:59 -070066#include "android_hardware_input_InputApplicationHandle.h"
67#include "android_hardware_input_InputWindowHandle.h"
Santos Cordonee8931e2017-04-05 10:31:15 -070068#include "android_hardware_display_DisplayViewport.h"
Robert Carre0a353c2018-08-02 16:38:04 -070069#include "android_util_Binder.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070070
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010071#include <vector>
72
Michael Wrighta4051212015-07-23 17:04:40 +010073#define INDENT " "
74
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -070075using android::base::ParseUint;
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080076using android::base::StringPrintf;
77
Jeff Brown46b9ac02010-04-22 18:58:52 -070078namespace android {
79
Jeff Brown1a84fd12011-06-02 01:26:32 -070080// The exponent used to calculate the pointer speed scaling factor.
81// The scaling factor is calculated as 2 ^ (speed * exponent),
82// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070083static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070084
Jeff Brown46b9ac02010-04-22 18:58:52 -070085static struct {
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -070086 jclass clazz;
Jeff Brown46b9ac02010-04-22 18:58:52 -070087 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070088 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070089 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070090 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070091 jmethodID notifyANR;
Robert Carrbd26e652018-08-21 15:46:28 -070092 jmethodID notifyFocusChanged;
Jeff Brown0029c662011-03-30 02:25:18 -070093 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070094 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070095 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070096 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070097 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070098 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080099 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700100 jmethodID getExcludedDeviceNames;
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700101 jmethodID getInputPortAssociations;
Jeff Browna4547672011-03-02 21:38:11 -0800102 jmethodID getKeyRepeatTimeout;
103 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700104 jmethodID getHoverTapTimeout;
105 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -0700106 jmethodID getDoubleTapTimeout;
107 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -0800108 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800109 jmethodID getPointerIcon;
Arthur Hungb9b32002018-12-18 17:39:43 +0800110 jmethodID getPointerDisplayId;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700111 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700112 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800113 jmethodID getTouchCalibrationForInputDevice;
Andrii Kulianfd8666d2018-10-05 16:58:39 -0700114 jmethodID getContextForDisplay;
Jeff Brown4532e612012-04-05 14:27:12 -0700115} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700116
117static struct {
118 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700119} gInputDeviceClassInfo;
120
121static struct {
122 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700123} gKeyEventClassInfo;
124
125static struct {
126 jclass clazz;
127} gMotionEventClassInfo;
128
RoboErikfb290df2013-12-16 11:27:55 -0800129static struct {
130 jclass clazz;
131 jmethodID constructor;
132} gInputDeviceIdentifierInfo;
133
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800134static struct {
135 jclass clazz;
136 jmethodID getAffineTransform;
137} gTouchCalibrationClassInfo;
138
RoboErikfb290df2013-12-16 11:27:55 -0800139
Jeff Brown928e0542011-01-10 11:17:36 -0800140
141// --- Global functions ---
142
Jeff Brown214eaf42011-05-26 19:17:02 -0700143template<typename T>
144inline static T min(const T& a, const T& b) {
145 return a < b ? a : b;
146}
147
148template<typename T>
149inline static T max(const T& a, const T& b) {
150 return a > b ? a : b;
151}
152
Michael Wrighta4051212015-07-23 17:04:40 +0100153static inline const char* toString(bool value) {
154 return value ? "true" : "false";
155}
156
Jun Mukai808196f2015-10-28 16:46:44 -0700157static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
158 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700159 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700160 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700161 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400162 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
163 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
164 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
165 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
166 bitmapCopy->rowBytes(), 0, 0);
167 }
Jun Mukai808196f2015-10-28 16:46:44 -0700168 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
169 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700170 }
171}
172
Jun Mukai808196f2015-10-28 16:46:44 -0700173static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
174 SpriteIcon* outSpriteIcon) {
175 PointerIcon pointerIcon;
176 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
177}
178
Jeff Brown905805a2011-10-12 13:57:59 -0700179enum {
180 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700181};
182
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700183static std::string getStringElementFromJavaArray(JNIEnv* env, jobjectArray array, jsize index) {
184 jstring item = jstring(env->GetObjectArrayElement(array, index));
185 ScopedUtfChars chars(env, item);
186 std::string result(chars.c_str());
187 return result;
188}
189
Jeff Brown928e0542011-01-10 11:17:36 -0800190
191// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800192
Jeff Brown9c3cda02010-06-15 01:31:58 -0700193class NativeInputManager : public virtual RefBase,
194 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700195 public virtual InputDispatcherPolicyInterface,
196 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700197protected:
198 virtual ~NativeInputManager();
199
200public:
Jeff Brown4532e612012-04-05 14:27:12 -0700201 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700202
203 inline sp<InputManager> getInputManager() const { return mInputManager; }
204
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800205 void dump(std::string& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700206
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100207 void setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700208
Robert Carre0a353c2018-08-02 16:38:04 -0700209 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel, int32_t displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700210 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
211
Arthur Hung39134b22018-08-14 11:58:28 +0800212 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray, int32_t displayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800213 void setFocusedApplication(JNIEnv* env, int32_t displayId, jobject applicationHandleObj);
214 void setFocusedDisplay(JNIEnv* env, int32_t displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700215 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800216 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700217 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700218 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700219 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700220 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800221 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100222 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800223 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700224 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800225 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700226
Jeff Brown9c3cda02010-06-15 01:31:58 -0700227 /* --- InputReaderPolicyInterface implementation --- */
228
Jeff Brown214eaf42011-05-26 19:17:02 -0700229 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800230 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700231 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800232 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100233 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700234 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
235 jfloatArray matrixArr);
236 virtual TouchAffineTransformation getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100237 const std::string& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700238
239 /* --- InputDispatcherPolicyInterface implementation --- */
240
Jeff Brownbcc046a2012-09-27 20:46:43 -0700241 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700242 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700243 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700244 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carre0a353c2018-08-02 16:38:04 -0700245 const sp<IBinder>& token,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800246 const std::string& reason);
Robert Carre0a353c2018-08-02 16:38:04 -0700247 virtual void notifyInputChannelBroken(const sp<IBinder>& token);
chaviw91089862019-01-09 15:49:11 -0800248 virtual void notifyFocusChanged(const sp<IBinder>& oldToken, const sp<IBinder>& newToken);
Jeff Brown0029c662011-03-30 02:25:18 -0700249 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700250 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800251 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800252 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700253 virtual nsecs_t interceptKeyBeforeDispatching(
Robert Carre0a353c2018-08-02 16:38:04 -0700254 const sp<IBinder>& token,
Jeff Brownb88102f2010-09-08 11:49:43 -0700255 const KeyEvent* keyEvent, uint32_t policyFlags);
Robert Carre0a353c2018-08-02 16:38:04 -0700256 virtual bool dispatchUnhandledKey(const sp<IBinder>& token,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800257 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700258 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700259 virtual bool checkInjectEventsPermissionNonReentrant(
260 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700261
Jeff Brown2352b972011-04-12 22:39:53 -0700262 /* --- PointerControllerPolicyInterface implementation --- */
263
Andrii Kulianfd8666d2018-10-05 16:58:39 -0700264 virtual void loadPointerIcon(SpriteIcon* icon, int32_t displayId);
265 virtual void loadPointerResources(PointerResources* outResources, int32_t displayId);
Jun Mukai808196f2015-10-28 16:46:44 -0700266 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
Andrii Kulianfd8666d2018-10-05 16:58:39 -0700267 std::map<int32_t, PointerAnimation>* outAnimationResources, int32_t displayId);
Jun Mukai5ec74202015-10-07 16:58:09 +0900268 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700269 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700270
Jeff Brown9c3cda02010-06-15 01:31:58 -0700271private:
272 sp<InputManager> mInputManager;
273
Jeff Brown4532e612012-04-05 14:27:12 -0700274 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800275 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700276
Jeff Brown83c09682010-12-23 17:50:18 -0800277 Mutex mLock;
278 struct Locked {
279 // Display size information.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100280 std::vector<DisplayViewport> viewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800281
Jeff Brown05dc66a2011-03-02 14:41:58 -0800282 // System UI visibility.
283 int32_t systemUiVisibility;
284
Jeff Brown1a84fd12011-06-02 01:26:32 -0700285 // Pointer speed.
286 int32_t pointerSpeed;
287
Jeff Brown474dcb52011-06-14 20:22:50 -0700288 // True if pointer gestures are enabled.
289 bool pointerGesturesEnabled;
290
Jeff Browndaf4a122011-08-26 17:14:14 -0700291 // Show touches feature enable/disable.
292 bool showTouches;
293
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800294 // Pointer capture feature enable/disable.
295 bool pointerCapture;
296
Jeff Brown5541de92011-04-11 11:54:25 -0700297 // Sprite controller singleton, created on first use.
298 sp<SpriteController> spriteController;
299
Jeff Brown83c09682010-12-23 17:50:18 -0800300 // Pointer controller singleton, created and destroyed as needed.
301 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700302
303 // Input devices to be disabled
304 SortedVector<int32_t> disabledInputDevices;
Arthur Hungb9b32002018-12-18 17:39:43 +0800305
306 // Associated Pointer controller display.
307 int32_t pointerDisplayId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100308 } mLocked GUARDED_BY(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700309
Michael Wrighta4051212015-07-23 17:04:40 +0100310 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700311
Arthur Hungb9b32002018-12-18 17:39:43 +0800312 void updateInactivityTimeoutLocked();
Jeff Brown56194eb2011-03-02 19:23:13 -0800313 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700314 void ensureSpriteControllerLocked();
Arthur Hungb9b32002018-12-18 17:39:43 +0800315 const DisplayViewport* findDisplayViewportLocked(int32_t displayId);
316 int32_t getPointerDisplayId();
317 void updatePointerDisplayLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700318 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700319
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320 static inline JNIEnv* jniEnv() {
321 return AndroidRuntime::getJNIEnv();
322 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700323};
324
Jeff Brown928e0542011-01-10 11:17:36 -0800325
Jeff Brown9c3cda02010-06-15 01:31:58 -0700326
Jeff Brown2352b972011-04-12 22:39:53 -0700327NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700328 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700329 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700330 JNIEnv* env = jniEnv();
331
Jeff Brown4532e612012-04-05 14:27:12 -0700332 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700333
Jeff Brown83c09682010-12-23 17:50:18 -0800334 {
335 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800336 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700337 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700338 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700339 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800340 mLocked.pointerCapture = false;
Arthur Hungb9b32002018-12-18 17:39:43 +0800341 mLocked.pointerDisplayId = ADISPLAY_ID_DEFAULT;
Jeff Brown83c09682010-12-23 17:50:18 -0800342 }
Michael Wrighta4051212015-07-23 17:04:40 +0100343 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800344
Prabir Pradhane5696a52018-11-14 19:55:21 -0800345 mInputManager = new InputManager(this, this);
Robert Carr679ccb02018-08-08 15:32:35 -0700346 defaultServiceManager()->addService(String16("inputflinger"),
347 mInputManager, false);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700348}
349
350NativeInputManager::~NativeInputManager() {
351 JNIEnv* env = jniEnv();
352
Jeff Brown4532e612012-04-05 14:27:12 -0700353 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700354}
355
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800356void NativeInputManager::dump(std::string& dump) {
357 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100358 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800359 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100360 }
361 {
362 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800363 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100364 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800365 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
366 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100367 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800368 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
369 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100370 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800371 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100372
Jeff Brownb88102f2010-09-08 11:49:43 -0700373 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800374 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375
Jeff Brownb88102f2010-09-08 11:49:43 -0700376 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800377 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700378}
379
Jeff Brown7fbdc842010-06-17 20:52:56 -0700380bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700381 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000382 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700383 LOGE_EX(env);
384 env->ExceptionClear();
385 return true;
386 }
387 return false;
388}
389
Arthur Hungb9b32002018-12-18 17:39:43 +0800390const DisplayViewport* NativeInputManager::findDisplayViewportLocked(int32_t displayId)
391 REQUIRES(mLock) {
392 for (const DisplayViewport& v : mLocked.viewports) {
393 if (v.displayId == displayId) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100394 return &v;
395 }
396 }
397 return nullptr;
398}
399
400void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
401 std::vector<DisplayViewport> viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700402
403 if (viewportObjArray) {
404 jsize length = env->GetArrayLength(viewportObjArray);
405 for (jsize i = 0; i < length; i++) {
406 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
407 if (! viewportObj) {
408 break; // found null element indicating end of used portion of the array
409 }
410
411 DisplayViewport viewport;
412 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100413 ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
414 viewports.push_back(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -0700415
416 env->DeleteLocalRef(viewportObj);
417 }
418 }
419
Arthur Hungb9b32002018-12-18 17:39:43 +0800420 // Get the preferred pointer controller displayId.
421 int32_t pointerDisplayId = getPointerDisplayId();
422
423 { // acquire lock
Santos Cordonee8931e2017-04-05 10:31:15 -0700424 AutoMutex _l(mLock);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100425 mLocked.viewports = viewports;
Arthur Hungb9b32002018-12-18 17:39:43 +0800426 mLocked.pointerDisplayId = pointerDisplayId;
427 } // release lock
Santos Cordonee8931e2017-04-05 10:31:15 -0700428
429 mInputManager->getReader()->requestRefreshConfiguration(
430 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
431}
432
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700433status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Robert Carre0a353c2018-08-02 16:38:04 -0700434 const sp<InputChannel>& inputChannel, int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100435 ATRACE_CALL();
Robert Carre0a353c2018-08-02 16:38:04 -0700436 return mInputManager->getDispatcher()->registerInputChannel(
437 inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700438}
439
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700440status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700441 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100442 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700443 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700444}
445
Jeff Brown214eaf42011-05-26 19:17:02 -0700446void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100447 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700448 JNIEnv* env = jniEnv();
449
Jeff Brown4532e612012-04-05 14:27:12 -0700450 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
451 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700452 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
453 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
454 }
455
456 outConfig->excludedDeviceNames.clear();
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700457 jobjectArray excludedDeviceNames = jobjectArray(env->CallStaticObjectMethod(
458 gServiceClassInfo.clazz, gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700459 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
460 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700461 for (jsize i = 0; i < length; i++) {
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700462 std::string deviceName = getStringElementFromJavaArray(env, excludedDeviceNames, i);
463 outConfig->excludedDeviceNames.push_back(deviceName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700464 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700465 env->DeleteLocalRef(excludedDeviceNames);
466 }
467
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700468 // Associations between input ports and display ports
469 // The java method packs the information in the following manner:
470 // Original data: [{'inputPort1': '1'}, {'inputPort2': '2'}]
471 // Received data: ['inputPort1', '1', 'inputPort2', '2']
472 // So we unpack accordingly here.
473 outConfig->portAssociations.clear();
474 jobjectArray portAssociations = jobjectArray(env->CallStaticObjectMethod(
475 gServiceClassInfo.clazz, gServiceClassInfo.getInputPortAssociations));
476 if (!checkAndClearExceptionFromCallback(env, "getInputPortAssociations") && portAssociations) {
477 jsize length = env->GetArrayLength(portAssociations);
478 for (jsize i = 0; i < length / 2; i++) {
479 std::string inputPort = getStringElementFromJavaArray(env, portAssociations, 2 * i);
480 std::string displayPortStr =
481 getStringElementFromJavaArray(env, portAssociations, 2 * i + 1);
482 uint8_t displayPort;
483 // Should already have been validated earlier, but do it here for safety.
484 bool success = ParseUint(displayPortStr, &displayPort);
485 if (!success) {
486 ALOGE("Could not parse entry in port configuration file, received: %s",
487 displayPortStr.c_str());
488 continue;
489 }
490 outConfig->portAssociations.insert({inputPort, displayPort});
491 }
492 env->DeleteLocalRef(portAssociations);
493 }
494
Jeff Brown4532e612012-04-05 14:27:12 -0700495 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
496 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700497 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700498 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
499 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700500 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700501 jint longPressTimeout = env->CallIntMethod(mServiceObj,
502 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700503 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700504 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700505
506 // We must ensure that the tap-drag interval is significantly shorter than
507 // the long-press timeout because the tap is held down for the entire duration
508 // of the double-tap timeout.
509 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700510 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700511 outConfig->pointerGestureTapDragInterval =
512 milliseconds_to_nanoseconds(tapDragInterval);
513 }
514 }
515 }
516
Jeff Brown4532e612012-04-05 14:27:12 -0700517 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
518 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700519 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
520 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700521 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700522
523 { // acquire lock
524 AutoMutex _l(mLock);
525
526 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
527 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700528 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700529
Jeff Browndaf4a122011-08-26 17:14:14 -0700530 outConfig->showTouches = mLocked.showTouches;
531
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800532 outConfig->pointerCapture = mLocked.pointerCapture;
533
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100534 outConfig->setDisplayViewports(mLocked.viewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700535
536 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700537 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700538}
539
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700540sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100541 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800542 AutoMutex _l(mLock);
543
544 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100545 if (controller == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700546 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800547
Jeff Brown2352b972011-04-12 22:39:53 -0700548 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800549 mLocked.pointerController = controller;
Arthur Hungb9b32002018-12-18 17:39:43 +0800550 updateInactivityTimeoutLocked();
Andrii Kuliand44026f2018-12-17 18:59:36 +0000551 }
Arthur Hungb9b32002018-12-18 17:39:43 +0800552
553 updatePointerDisplayLocked();
554
Jeff Brown83c09682010-12-23 17:50:18 -0800555 return controller;
556}
557
Arthur Hungb9b32002018-12-18 17:39:43 +0800558int32_t NativeInputManager::getPointerDisplayId() {
559 JNIEnv* env = jniEnv();
560 jint pointerDisplayId = env->CallIntMethod(mServiceObj,
561 gServiceClassInfo.getPointerDisplayId);
562 if (checkAndClearExceptionFromCallback(env, "getPointerDisplayId")) {
563 pointerDisplayId = ADISPLAY_ID_DEFAULT;
564 }
565
566 return pointerDisplayId;
567}
568
569void NativeInputManager::updatePointerDisplayLocked() REQUIRES(mLock) {
570 ATRACE_CALL();
571
572 sp<PointerController> controller = mLocked.pointerController.promote();
573 if (controller != nullptr) {
574 const DisplayViewport* viewport = findDisplayViewportLocked(mLocked.pointerDisplayId);
575 if (viewport == nullptr) {
576 ALOGW("Can't find pointer display viewport, fallback to default display.");
577 viewport = findDisplayViewportLocked(ADISPLAY_ID_DEFAULT);
578 }
579
580 if (viewport != nullptr) {
581 controller->setDisplayViewport(*viewport);
582 }
583 }
584}
585
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100586void NativeInputManager::ensureSpriteControllerLocked() REQUIRES(mLock) {
587 if (mLocked.spriteController == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700588 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700589 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700590 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
591 layer = -1;
592 }
593 mLocked.spriteController = new SpriteController(mLooper, layer);
594 }
595}
596
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700597void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100598 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700599 JNIEnv* env = jniEnv();
600
601 size_t count = inputDevices.size();
602 jobjectArray inputDevicesObjArray = env->NewObjectArray(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100603 count, gInputDeviceClassInfo.clazz, nullptr);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700604 if (inputDevicesObjArray) {
605 bool error = false;
606 for (size_t i = 0; i < count; i++) {
607 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
608 if (!inputDeviceObj) {
609 error = true;
610 break;
611 }
612
613 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
614 env->DeleteLocalRef(inputDeviceObj);
615 }
616
617 if (!error) {
618 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
619 inputDevicesObjArray);
620 }
621
622 env->DeleteLocalRef(inputDevicesObjArray);
623 }
624
625 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
626}
627
Jeff Brown6ec6f792012-04-17 16:52:41 -0700628sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800629 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100630 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700631 JNIEnv* env = jniEnv();
632
633 sp<KeyCharacterMap> result;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100634 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
RoboErikfb290df2013-12-16 11:27:55 -0800635 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
636 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
637 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700638 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800639 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700640 if (arrayObj.get()) {
641 ScopedLocalRef<jstring> filenameObj(env,
642 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
643 ScopedLocalRef<jstring> contentsObj(env,
644 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
645 ScopedUtfChars filenameChars(env, filenameObj.get());
646 ScopedUtfChars contentsChars(env, contentsObj.get());
647
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100648 KeyCharacterMap::loadContents(filenameChars.c_str(),
649 contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700650 }
651 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
652 return result;
653}
654
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100655std::string NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100656 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700657 JNIEnv* env = jniEnv();
658
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100659 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.c_str()));
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700660 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
661 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100662 std::string result;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700663 if (aliasObj.get()) {
664 ScopedUtfChars aliasChars(env, aliasObj.get());
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100665 result = aliasChars.c_str();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700666 }
667 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
668 return result;
669}
670
Jeff Brownbcc046a2012-09-27 20:46:43 -0700671void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700672 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700673#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700674 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
675 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700676#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100677 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700678
679 JNIEnv* env = jniEnv();
680
Jeff Brown53384282012-08-20 20:16:01 -0700681 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700682 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700683 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700684}
685
Jeff Brown9c3cda02010-06-15 01:31:58 -0700686void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
687#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000688 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700689#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100690 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700691
692 JNIEnv* env = jniEnv();
693
Jeff Brown4532e612012-04-05 14:27:12 -0700694 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700695 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700696}
697
Jeff Brown519e0242010-09-15 15:18:56 -0700698nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carre0a353c2018-08-02 16:38:04 -0700699 const sp<IBinder>& token, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700700#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000701 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700702#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100703 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700704
705 JNIEnv* env = jniEnv();
706
Robert Carre0a353c2018-08-02 16:38:04 -0700707 jobject tokenObj = javaObjectForIBinder(env, token);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800708 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700709
Jeff Brown4532e612012-04-05 14:27:12 -0700710 jlong newTimeout = env->CallLongMethod(mServiceObj,
Robert Carr679ccb02018-08-08 15:32:35 -0700711 gServiceClassInfo.notifyANR, tokenObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700712 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700713 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
714 newTimeout = 0; // abort dispatch
715 } else {
716 assert(newTimeout >= 0);
717 }
718
Jeff Brownbd181bb2013-09-10 16:44:24 -0700719 env->DeleteLocalRef(reasonObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700720 return newTimeout;
721}
722
Robert Carre0a353c2018-08-02 16:38:04 -0700723void NativeInputManager::notifyInputChannelBroken(const sp<IBinder>& token) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700724#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000725 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700726#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100727 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700728
Jeff Brown7fbdc842010-06-17 20:52:56 -0700729 JNIEnv* env = jniEnv();
730
Robert Carre0a353c2018-08-02 16:38:04 -0700731 jobject tokenObj = javaObjectForIBinder(env, token);
732 if (tokenObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700733 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Robert Carre0a353c2018-08-02 16:38:04 -0700734 tokenObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700735 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700736 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700737}
738
chaviw91089862019-01-09 15:49:11 -0800739void NativeInputManager::notifyFocusChanged(const sp<IBinder>& oldToken,
740 const sp<IBinder>& newToken) {
Robert Carrbd26e652018-08-21 15:46:28 -0700741#if DEBUG_INPUT_DISPATCHER_POLICY
742 ALOGD("notifyFocusChanged");
743#endif
744 ATRACE_CALL();
745
746 JNIEnv* env = jniEnv();
747
chaviw91089862019-01-09 15:49:11 -0800748 jobject oldTokenObj = javaObjectForIBinder(env, oldToken);
749 jobject newTokenObj = javaObjectForIBinder(env, newToken);
750 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyFocusChanged,
751 oldTokenObj, newTokenObj);
752 checkAndClearExceptionFromCallback(env, "notifyFocusChanged");
Robert Carrbd26e652018-08-21 15:46:28 -0700753}
754
Jeff Brown214eaf42011-05-26 19:17:02 -0700755void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100756 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700757 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800758
Jeff Brown4532e612012-04-05 14:27:12 -0700759 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
760 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700761 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
762 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
763 }
Jeff Browna4547672011-03-02 21:38:11 -0800764
Jeff Brown4532e612012-04-05 14:27:12 -0700765 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
766 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700767 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
768 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
769 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700770}
771
Arthur Hung39134b22018-08-14 11:58:28 +0800772void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
773 int32_t displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700774 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700775
Jeff Brown9302c872011-07-13 22:51:29 -0700776 if (windowHandleObjArray) {
777 jsize length = env->GetArrayLength(windowHandleObjArray);
778 for (jsize i = 0; i < length; i++) {
779 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
780 if (! windowHandleObj) {
781 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700782 }
Jeff Brown9302c872011-07-13 22:51:29 -0700783
784 sp<InputWindowHandle> windowHandle =
Riddle Hsucd958bc2019-01-23 15:40:26 +0800785 android_view_InputWindowHandle_getHandle(env, windowHandleObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100786 if (windowHandle != nullptr) {
Jeff Brown9302c872011-07-13 22:51:29 -0700787 windowHandles.push(windowHandle);
788 }
789 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700790 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700791 }
Jeff Brown349703e2010-06-22 01:27:15 -0700792
Arthur Hung39134b22018-08-14 11:58:28 +0800793 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700794
795 // Do this after the dispatcher has updated the window handle state.
796 bool newPointerGesturesEnabled = true;
797 size_t numWindows = windowHandles.size();
798 for (size_t i = 0; i < numWindows; i++) {
799 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700800 const InputWindowInfo* windowInfo = windowHandle->getInfo();
801 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
802 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700803 newPointerGesturesEnabled = false;
804 }
805 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700806
807 uint32_t changes = 0;
808 { // acquire lock
809 AutoMutex _l(mLock);
810
811 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
812 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
813 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
814 }
815 } // release lock
816
817 if (changes) {
818 mInputManager->getReader()->requestRefreshConfiguration(changes);
819 }
Jeff Brown349703e2010-06-22 01:27:15 -0700820}
821
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800822void NativeInputManager::setFocusedApplication(JNIEnv* env, int32_t displayId,
823 jobject applicationHandleObj) {
Jeff Brown9302c872011-07-13 22:51:29 -0700824 sp<InputApplicationHandle> applicationHandle =
Riddle Hsucd958bc2019-01-23 15:40:26 +0800825 android_view_InputApplicationHandle_getHandle(env, applicationHandleObj);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800826 mInputManager->getDispatcher()->setFocusedApplication(displayId, applicationHandle);
827}
828
829void NativeInputManager::setFocusedDisplay(JNIEnv* env, int32_t displayId) {
830 mInputManager->getDispatcher()->setFocusedDisplay(displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700831}
832
833void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700834 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700835}
836
Jeff Brown05dc66a2011-03-02 14:41:58 -0800837void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
838 AutoMutex _l(mLock);
839
840 if (mLocked.systemUiVisibility != visibility) {
841 mLocked.systemUiVisibility = visibility;
Arthur Hungb9b32002018-12-18 17:39:43 +0800842 updateInactivityTimeoutLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800843 }
844}
845
Arthur Hungb9b32002018-12-18 17:39:43 +0800846void NativeInputManager::updateInactivityTimeoutLocked() REQUIRES(mLock) {
847 sp<PointerController> controller = mLocked.pointerController.promote();
848 if (controller == nullptr) {
849 return;
850 }
851
Jeff Brown05dc66a2011-03-02 14:41:58 -0800852 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700853 controller->setInactivityTimeout(lightsOut
854 ? PointerController::INACTIVITY_TIMEOUT_SHORT
855 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800856}
857
Jeff Brown1a84fd12011-06-02 01:26:32 -0700858void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700859 { // acquire lock
860 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700861
Jeff Brown474dcb52011-06-14 20:22:50 -0700862 if (mLocked.pointerSpeed == speed) {
863 return;
864 }
865
Steve Block6215d3f2012-01-04 20:05:49 +0000866 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700867 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700868 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700869
Jeff Brown474dcb52011-06-14 20:22:50 -0700870 mInputManager->getReader()->requestRefreshConfiguration(
871 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700872}
873
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700874void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
875 { // acquire lock
876 AutoMutex _l(mLock);
877
878 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
879 bool currentlyEnabled = index < 0;
880 if (!enabled && currentlyEnabled) {
881 mLocked.disabledInputDevices.add(deviceId);
882 }
883 if (enabled && !currentlyEnabled) {
884 mLocked.disabledInputDevices.remove(deviceId);
885 }
886 } // release lock
887
888 mInputManager->getReader()->requestRefreshConfiguration(
889 InputReaderConfiguration::CHANGE_ENABLED_STATE);
890}
891
Jeff Browndaf4a122011-08-26 17:14:14 -0700892void NativeInputManager::setShowTouches(bool enabled) {
893 { // acquire lock
894 AutoMutex _l(mLock);
895
896 if (mLocked.showTouches == enabled) {
897 return;
898 }
899
Steve Block6215d3f2012-01-04 20:05:49 +0000900 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700901 mLocked.showTouches = enabled;
902 } // release lock
903
904 mInputManager->getReader()->requestRefreshConfiguration(
905 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
906}
907
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800908void NativeInputManager::setPointerCapture(bool enabled) {
909 { // acquire lock
910 AutoMutex _l(mLock);
911
912 if (mLocked.pointerCapture == enabled) {
913 return;
914 }
915
916 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
917 mLocked.pointerCapture = enabled;
918 } // release lock
919
920 mInputManager->getReader()->requestRefreshConfiguration(
921 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
922}
923
Jeff Brown037c33e2014-04-09 00:31:55 -0700924void NativeInputManager::setInteractive(bool interactive) {
925 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700926}
927
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800928void NativeInputManager::reloadCalibration() {
929 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100930 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800931}
932
Michael Wrighte051f6f2016-05-13 17:44:16 +0100933void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800934 AutoMutex _l(mLock);
935 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100936 if (controller != nullptr) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100937 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800938 }
939}
940
941void NativeInputManager::reloadPointerIcons() {
942 AutoMutex _l(mLock);
943 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100944 if (controller != nullptr) {
Jun Mukai19a56012015-11-24 11:25:52 -0800945 controller->reloadPointerResources();
946 }
Jun Mukai1db53972015-09-11 18:08:31 -0700947}
948
Jun Mukaid4eaef72015-10-30 15:54:33 -0700949void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
950 AutoMutex _l(mLock);
951 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100952 if (controller != nullptr) {
Jun Mukaid4eaef72015-10-30 15:54:33 -0700953 controller->setCustomPointerIcon(icon);
954 }
955}
956
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800957TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
958 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100959 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800960 ScopedFloatArrayRO matrix(env, matrixArr);
961 assert(matrix.size() == 6);
962
963 TouchAffineTransformation transform;
964 transform.x_scale = matrix[0];
965 transform.x_ymix = matrix[1];
966 transform.x_offset = matrix[2];
967 transform.y_xmix = matrix[3];
968 transform.y_scale = matrix[4];
969 transform.y_offset = matrix[5];
970
971 return transform;
972}
973
974TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100975 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800976 JNIEnv* env = jniEnv();
977
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100978 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.c_str()));
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800979
980 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700981 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
982 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800983
984 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
985 gTouchCalibrationClassInfo.getAffineTransform));
986
987 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
988
989 env->DeleteLocalRef(matrixArr);
990 env->DeleteLocalRef(cal);
991
992 return transform;
993}
994
Jeff Brown0029c662011-03-30 02:25:18 -0700995bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100996 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700997 jobject inputEventObj;
998
999 JNIEnv* env = jniEnv();
1000 switch (inputEvent->getType()) {
1001 case AINPUT_EVENT_TYPE_KEY:
1002 inputEventObj = android_view_KeyEvent_fromNative(env,
1003 static_cast<const KeyEvent*>(inputEvent));
1004 break;
1005 case AINPUT_EVENT_TYPE_MOTION:
1006 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
1007 static_cast<const MotionEvent*>(inputEvent));
1008 break;
1009 default:
1010 return true; // dispatch the event normally
1011 }
1012
1013 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +00001014 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -07001015 return true; // dispatch the event normally
1016 }
1017
1018 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -07001019 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -07001020 inputEventObj, policyFlags);
1021 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
1022 pass = true;
1023 }
1024 env->DeleteLocalRef(inputEventObj);
1025 return pass;
1026}
1027
Jeff Brown1f245102010-11-18 20:53:46 -08001028void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
1029 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001030 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001031 // Policy:
1032 // - Ignore untrusted events and pass them along.
1033 // - Ask the window manager what to do with normal events and trusted injected events.
1034 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001035 bool interactive = mInteractive.load();
1036 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001037 policyFlags |= POLICY_FLAG_INTERACTIVE;
1038 }
Jeff Brown3122e442010-10-11 23:32:49 -07001039 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -08001040 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -07001041 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -08001042 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1043 jint wmActions;
1044 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001045 wmActions = env->CallIntMethod(mServiceObj,
1046 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -07001047 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001048 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
1049 wmActions = 0;
1050 }
1051 android_view_KeyEvent_recycle(env, keyEventObj);
1052 env->DeleteLocalRef(keyEventObj);
1053 } else {
Steve Block3762c312012-01-06 19:20:56 +00001054 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -07001055 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -07001056 }
1057
Jeff Brown56194eb2011-03-02 19:23:13 -08001058 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -07001059 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001060 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001061 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1062 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001063 }
1064}
1065
Jeff Brown56194eb2011-03-02 19:23:13 -08001066void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001067 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001068 // Policy:
1069 // - Ignore untrusted events and pass them along.
1070 // - No special filtering for injected events required at this time.
1071 // - Filter normal events based on screen state.
1072 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001073 bool interactive = mInteractive.load();
1074 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001075 policyFlags |= POLICY_FLAG_INTERACTIVE;
1076 }
Jeff Brown3122e442010-10-11 23:32:49 -07001077 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001078 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001079 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001080 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001081 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001082 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001083 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001084 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001085 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001086 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001087 wmActions = 0;
1088 }
1089
Jeff Brown56194eb2011-03-02 19:23:13 -08001090 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001091 }
Jeff Brown3122e442010-10-11 23:32:49 -07001092 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001093 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001094 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1095 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001096 }
1097}
1098
Jeff Brown56194eb2011-03-02 19:23:13 -08001099void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1100 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001101 if (wmActions & WM_ACTION_PASS_TO_USER) {
1102 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1103 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001104#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001105 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001106#endif
1107 }
1108}
1109
Jeff Brown905805a2011-10-12 13:57:59 -07001110nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Robert Carre0a353c2018-08-02 16:38:04 -07001111 const sp<IBinder>& token,
Jeff Browne20c9e02010-10-11 14:20:19 -07001112 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001113 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001114 // Policy:
1115 // - Ignore untrusted events and pass them along.
1116 // - Filter normal events and trusted injected events through the window manager policy to
1117 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001118 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001119 if (policyFlags & POLICY_FLAG_TRUSTED) {
1120 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001121
Robert Carre0a353c2018-08-02 16:38:04 -07001122 // Token may be null
1123 jobject tokenObj = javaObjectForIBinder(env, token);
1124
Jeff Brown1f245102010-11-18 20:53:46 -08001125 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1126 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001127 jlong delayMillis = env->CallLongMethod(mServiceObj,
1128 gServiceClassInfo.interceptKeyBeforeDispatching,
Robert Carre0a353c2018-08-02 16:38:04 -07001129 tokenObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001130 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1131 android_view_KeyEvent_recycle(env, keyEventObj);
1132 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001133 if (!error) {
1134 if (delayMillis < 0) {
1135 result = -1;
1136 } else if (delayMillis > 0) {
1137 result = milliseconds_to_nanoseconds(delayMillis);
1138 }
1139 }
Jeff Brown1f245102010-11-18 20:53:46 -08001140 } else {
Steve Block3762c312012-01-06 19:20:56 +00001141 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001142 }
Jeff Brown3122e442010-10-11 23:32:49 -07001143 }
Jeff Brown1f245102010-11-18 20:53:46 -08001144 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001145}
1146
Robert Carre0a353c2018-08-02 16:38:04 -07001147bool NativeInputManager::dispatchUnhandledKey(const sp<IBinder>& token,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001148 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001149 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001150 // Policy:
1151 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001152 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001153 if (policyFlags & POLICY_FLAG_TRUSTED) {
1154 JNIEnv* env = jniEnv();
1155
Robert Carre0a353c2018-08-02 16:38:04 -07001156 // Note: tokenObj may be null.
1157 jobject tokenObj = javaObjectForIBinder(env, token);
Jeff Brown1f245102010-11-18 20:53:46 -08001158 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1159 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001160 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1161 gServiceClassInfo.dispatchUnhandledKey,
Robert Carre0a353c2018-08-02 16:38:04 -07001162 tokenObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001163 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001164 fallbackKeyEventObj = nullptr;
Jeff Brownda3d5a92011-03-29 15:11:34 -07001165 }
Jeff Brown1f245102010-11-18 20:53:46 -08001166 android_view_KeyEvent_recycle(env, keyEventObj);
1167 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001168
1169 if (fallbackKeyEventObj) {
1170 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1171 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1172 outFallbackKeyEvent)) {
1173 result = true;
1174 }
1175 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1176 env->DeleteLocalRef(fallbackKeyEventObj);
1177 }
Jeff Brown1f245102010-11-18 20:53:46 -08001178 } else {
Steve Block3762c312012-01-06 19:20:56 +00001179 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001180 }
Jeff Brown3915bb82010-11-05 15:02:16 -07001181 }
Jeff Brown1f245102010-11-18 20:53:46 -08001182 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001183}
1184
Jeff Brown01ce2e92010-09-26 22:20:12 -07001185void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001186 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001187 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001188}
1189
Jeff Brown349703e2010-06-22 01:27:15 -07001190
Jeff Brownb88102f2010-09-08 11:49:43 -07001191bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1192 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001193 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001194 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001195 jboolean result = env->CallBooleanMethod(mServiceObj,
1196 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001197 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1198 result = false;
1199 }
Jeff Brown349703e2010-06-22 01:27:15 -07001200 return result;
1201}
1202
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001203void NativeInputManager::loadPointerIcon(SpriteIcon* icon, int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001204 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001205 JNIEnv* env = jniEnv();
1206
1207 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001208 mServiceObj, gServiceClassInfo.getPointerIcon, displayId));
Jun Mukai19a56012015-11-24 11:25:52 -08001209 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1210 return;
1211 }
1212
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001213 ScopedLocalRef<jobject> displayContext(env, env->CallObjectMethod(
1214 mServiceObj, gServiceClassInfo.getContextForDisplay, displayId));
1215
Jun Mukai19a56012015-11-24 11:25:52 -08001216 PointerIcon pointerIcon;
1217 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001218 displayContext.get(), &pointerIcon);
Jun Mukai19a56012015-11-24 11:25:52 -08001219 if (!status && !pointerIcon.isNullIcon()) {
1220 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1221 } else {
1222 *icon = SpriteIcon();
1223 }
1224}
1225
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001226void NativeInputManager::loadPointerResources(PointerResources* outResources, int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001227 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001228 JNIEnv* env = jniEnv();
1229
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001230 ScopedLocalRef<jobject> displayContext(env, env->CallObjectMethod(
1231 mServiceObj, gServiceClassInfo.getContextForDisplay, displayId));
1232
1233 loadSystemIconAsSprite(env, displayContext.get(), POINTER_ICON_STYLE_SPOT_HOVER,
Jeff Brown2352b972011-04-12 22:39:53 -07001234 &outResources->spotHover);
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001235 loadSystemIconAsSprite(env, displayContext.get(), POINTER_ICON_STYLE_SPOT_TOUCH,
Jeff Brown2352b972011-04-12 22:39:53 -07001236 &outResources->spotTouch);
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001237 loadSystemIconAsSprite(env, displayContext.get(), POINTER_ICON_STYLE_SPOT_ANCHOR,
Jeff Brown2352b972011-04-12 22:39:53 -07001238 &outResources->spotAnchor);
1239}
1240
Jun Mukai808196f2015-10-28 16:46:44 -07001241void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001242 std::map<int32_t, PointerAnimation>* outAnimationResources, int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001243 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001244 JNIEnv* env = jniEnv();
1245
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001246 ScopedLocalRef<jobject> displayContext(env, env->CallObjectMethod(
1247 mServiceObj, gServiceClassInfo.getContextForDisplay, displayId));
1248
Jun Mukai1db53972015-09-11 18:08:31 -07001249 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1250 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001251 PointerIcon pointerIcon;
1252 loadSystemIconAsSpriteWithPointerIcon(
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001253 env, displayContext.get(), iconId, &pointerIcon, &((*outResources)[iconId]));
Jun Mukai808196f2015-10-28 16:46:44 -07001254 if (!pointerIcon.bitmapFrames.empty()) {
1255 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1256 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1257 animationData.durationPerFrame =
1258 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1259 animationData.animationFrames.reserve(numFrames);
1260 animationData.animationFrames.push_back(SpriteIcon(
1261 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1262 for (size_t i = 0; i < numFrames - 1; ++i) {
1263 animationData.animationFrames.push_back(SpriteIcon(
1264 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1265 }
1266 }
Jun Mukai1db53972015-09-11 18:08:31 -07001267 }
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001268 loadSystemIconAsSprite(env, displayContext.get(), POINTER_ICON_STYLE_NULL,
Jun Mukai808196f2015-10-28 16:46:44 -07001269 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001270}
1271
Jun Mukai5ec74202015-10-07 16:58:09 +09001272int32_t NativeInputManager::getDefaultPointerIconId() {
1273 return POINTER_ICON_STYLE_ARROW;
1274}
Jeff Brown83c09682010-12-23 17:50:18 -08001275
Jun Mukaid4eaef72015-10-30 15:54:33 -07001276int32_t NativeInputManager::getCustomPointerIconId() {
1277 return POINTER_ICON_STYLE_CUSTOM;
1278}
1279
Jeff Brown9c3cda02010-06-15 01:31:58 -07001280// ----------------------------------------------------------------------------
1281
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001282static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001283 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001284 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001285 if (messageQueue == nullptr) {
Jeff Brown864693462013-01-28 14:25:53 -08001286 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1287 return 0;
1288 }
1289
Jeff Brown603b4452012-04-06 17:39:41 -07001290 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1291 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001292 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001293 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001294}
1295
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001296static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001297 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001298
Jeff Brown4532e612012-04-05 14:27:12 -07001299 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001300 if (result) {
1301 jniThrowRuntimeException(env, "Input manager could not be started.");
1302 }
1303}
1304
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001305static void nativeSetDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
Santos Cordonee8931e2017-04-05 10:31:15 -07001306 jobjectArray viewportObjArray) {
1307 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001308 im->setDisplayViewports(env, viewportObjArray);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001309}
1310
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001311static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001312 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001313 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001314
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001315 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001316 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001317}
1318
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001319static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001320 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001321 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001322
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001323 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001324 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001325}
1326
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001327static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001328 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001329 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001330
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001331 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001332 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001333}
1334
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001335static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001336 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001337 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001338
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001339 int32_t* codes = env->GetIntArrayElements(keyCodes, nullptr);
1340 uint8_t* flags = env->GetBooleanArrayElements(outFlags, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001341 jsize numCodes = env->GetArrayLength(keyCodes);
1342 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001343 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001344 if (im->getInputManager()->getReader()->hasKeys(
1345 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1346 result = JNI_TRUE;
1347 } else {
1348 result = JNI_FALSE;
1349 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001350 } else {
1351 result = JNI_FALSE;
1352 }
1353
1354 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1355 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1356 return result;
1357}
1358
1359static void throwInputChannelNotInitialized(JNIEnv* env) {
1360 jniThrowException(env, "java/lang/IllegalStateException",
1361 "inputChannel is not initialized");
1362}
1363
Jeff Brown4532e612012-04-05 14:27:12 -07001364static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001365 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001366 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1367
Steve Block8564c8d2012-01-05 23:22:43 +00001368 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001369 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001370 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001371}
1372
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001373static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Robert Carre0a353c2018-08-02 16:38:04 -07001374 jlong ptr, jobject inputChannelObj, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001375 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001376
1377 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1378 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001379 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001380 throwInputChannelNotInitialized(env);
1381 return;
1382 }
Robert Carre0a353c2018-08-02 16:38:04 -07001383 bool monitor = inputChannel->getToken() == nullptr && displayId != ADISPLAY_ID_NONE;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001384
Robert Carre0a353c2018-08-02 16:38:04 -07001385 status_t status = im->registerInputChannel(env, inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001386
Jeff Brown46b9ac02010-04-22 18:58:52 -07001387 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001388 std::string message;
1389 message += StringPrintf("Failed to register input channel. status=%d", status);
1390 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001391 return;
1392 }
1393
Arthur Hungbe5ce212018-09-13 18:41:56 +08001394 // If inputWindowHandle is null and displayId >= 0, treat inputChannel as monitor.
Robert Carre0a353c2018-08-02 16:38:04 -07001395 if (!monitor) {
Jeff Browna41ca772010-08-11 14:46:32 -07001396 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001397 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001398 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001399}
1400
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001401static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001402 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001403 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001404
1405 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1406 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001407 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001408 throwInputChannelNotInitialized(env);
1409 return;
1410 }
1411
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001412 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001413
Jeff Brown4532e612012-04-05 14:27:12 -07001414 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001415 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001416 std::string message;
1417 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1418 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001419 }
1420}
1421
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001422static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001423 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001424 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001425
Jeff Brown4532e612012-04-05 14:27:12 -07001426 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001427}
1428
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001429static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001430 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001431 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001432 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001433
Jeff Brown6ec402b2010-07-28 15:48:59 -07001434 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1435 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001436 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1437 if (status) {
1438 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1439 return INPUT_EVENT_INJECTION_FAILED;
1440 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001441
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001442 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001443 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001444 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001445 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001446 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1447 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001448 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1449 return INPUT_EVENT_INJECTION_FAILED;
1450 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001451
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001452 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001453 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001454 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001455 } else {
1456 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001457 return INPUT_EVENT_INJECTION_FAILED;
1458 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001459}
1460
Andrii Kulian112d0562016-03-08 10:44:22 -08001461static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1462 jlong ptr, jint deviceId) {
1463 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001464
Andrii Kulian112d0562016-03-08 10:44:22 -08001465 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1466}
1467
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001468static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hung39134b22018-08-14 11:58:28 +08001469 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001470 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001471
Arthur Hung39134b22018-08-14 11:58:28 +08001472 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001473}
1474
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001475static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001476 jlong ptr, jint displayId, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001477 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001478
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001479 im->setFocusedApplication(env, displayId, applicationHandleObj);
1480}
1481
1482static void nativeSetFocusedDisplay(JNIEnv* env, jclass /* clazz */,
1483 jlong ptr, jint displayId) {
1484 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1485
1486 im->setFocusedDisplay(env, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001487}
1488
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001489static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1490 jboolean enabled) {
1491 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001492
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001493 im->setPointerCapture(enabled);
1494}
1495
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001496static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1497 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001498 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001499
Jeff Brown4532e612012-04-05 14:27:12 -07001500 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001501}
1502
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001503static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1504 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001505 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001506
Jeff Brown4532e612012-04-05 14:27:12 -07001507 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001508}
1509
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001510static void nativeSetPointerSpeed(JNIEnv* /* env */,
1511 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001512 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001513
Jeff Brown4532e612012-04-05 14:27:12 -07001514 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001515}
1516
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001517static void nativeSetShowTouches(JNIEnv* /* env */,
1518 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001519 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001520
Jeff Brown4532e612012-04-05 14:27:12 -07001521 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001522}
1523
Jeff Brown037c33e2014-04-09 00:31:55 -07001524static void nativeSetInteractive(JNIEnv* env,
1525 jclass clazz, jlong ptr, jboolean interactive) {
1526 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1527
1528 im->setInteractive(interactive);
1529}
1530
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001531static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1532 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001533
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001534 im->reloadCalibration();
1535}
1536
Jeff Browna47425a2012-04-13 04:09:27 -07001537static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001538 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001539 jint repeat, jint token) {
1540 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1541
1542 size_t patternSize = env->GetArrayLength(patternObj);
1543 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001544 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001545 "which is more than the maximum supported size of %d.",
1546 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1547 return; // limit to reasonable size
1548 }
1549
1550 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001551 patternObj, nullptr));
Jeff Browna47425a2012-04-13 04:09:27 -07001552 nsecs_t pattern[patternSize];
1553 for (size_t i = 0; i < patternSize; i++) {
1554 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001555 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001556 }
1557 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1558
1559 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1560}
1561
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001562static void nativeCancelVibrate(JNIEnv* /* env */,
1563 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001564 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1565
1566 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1567}
1568
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001569static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1570 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001571 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1572
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001573 im->getInputManager()->getReader()->requestRefreshConfiguration(
1574 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1575}
1576
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001577static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1578 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001579 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1580
1581 im->getInputManager()->getReader()->requestRefreshConfiguration(
1582 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001583}
1584
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001585static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001586 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001587
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001588 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001589 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001590 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001591}
1592
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001593static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001594 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001595
Jeff Brown4532e612012-04-05 14:27:12 -07001596 im->getInputManager()->getReader()->monitor();
1597 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001598}
1599
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001600static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1601 jclass /* clazz */, jlong ptr, jint deviceId) {
1602 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1603
1604 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1605}
1606
1607static void nativeEnableInputDevice(JNIEnv* /* env */,
1608 jclass /* clazz */, jlong ptr, jint deviceId) {
1609 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1610
1611 im->setInputDeviceEnabled(deviceId, true);
1612}
1613
1614static void nativeDisableInputDevice(JNIEnv* /* env */,
1615 jclass /* clazz */, jlong ptr, jint deviceId) {
1616 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1617
1618 im->setInputDeviceEnabled(deviceId, false);
1619}
1620
Michael Wrighte051f6f2016-05-13 17:44:16 +01001621static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001622 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001623
Michael Wrighte051f6f2016-05-13 17:44:16 +01001624 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001625}
1626
Jun Mukai19a56012015-11-24 11:25:52 -08001627static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1628 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001629
Jun Mukai19a56012015-11-24 11:25:52 -08001630 im->reloadPointerIcons();
1631}
1632
Jun Mukaid4eaef72015-10-30 15:54:33 -07001633static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1634 jlong ptr, jobject iconObj) {
1635 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1636
1637 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001638 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1639 if (result) {
1640 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1641 return;
1642 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001643
1644 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001645 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1646 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1647 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1648 spriteIcon.bitmap.rowBytes(), 0, 0);
1649 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001650 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1651 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1652 im->setCustomPointerIcon(spriteIcon);
1653}
1654
Jeff Brown9c3cda02010-06-15 01:31:58 -07001655// ----------------------------------------------------------------------------
1656
Daniel Micay76f6a862015-09-19 17:31:01 -04001657static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001658 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001659 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001660 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001661 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001662 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001663 (void*) nativeStart },
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001664 { "nativeSetDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1665 (void*) nativeSetDisplayViewports },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001666 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001667 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001668 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001669 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001670 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001671 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001672 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001673 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001674 { "nativeRegisterInputChannel",
Robert Carre0a353c2018-08-02 16:38:04 -07001675 "(JLandroid/view/InputChannel;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001676 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001677 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001678 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001679 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001680 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001681 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001682 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001683 { "nativeToggleCapsLock", "(JI)V",
1684 (void*) nativeToggleCapsLock },
Robert Carr788f5742018-07-30 17:46:45 -07001685 { "nativeSetInputWindows", "(J[Landroid/view/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001686 (void*) nativeSetInputWindows },
Robert Carr788f5742018-07-30 17:46:45 -07001687 { "nativeSetFocusedApplication", "(JILandroid/view/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001688 (void*) nativeSetFocusedApplication },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001689 { "nativeSetFocusedDisplay", "(JI)V",
1690 (void*) nativeSetFocusedDisplay },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001691 { "nativeSetPointerCapture", "(JZ)V",
1692 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001693 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001694 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001695 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001696 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001697 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001698 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001699 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001700 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001701 { "nativeSetInteractive", "(JZ)V",
1702 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001703 { "nativeReloadCalibration", "(J)V",
1704 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001705 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001706 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001707 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001708 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001709 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001710 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001711 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001712 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001713 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001714 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001715 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001716 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001717 { "nativeIsInputDeviceEnabled", "(JI)Z",
1718 (void*) nativeIsInputDeviceEnabled },
1719 { "nativeEnableInputDevice", "(JI)V",
1720 (void*) nativeEnableInputDevice },
1721 { "nativeDisableInputDevice", "(JI)V",
1722 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001723 { "nativeSetPointerIconType", "(JI)V",
1724 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001725 { "nativeReloadPointerIcons", "(J)V",
1726 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001727 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1728 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001729};
1730
1731#define FIND_CLASS(var, className) \
1732 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001733 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001734
1735#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1736 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001737 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001738
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -07001739#define GET_STATIC_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1740 var = env->GetStaticMethodID(clazz, methodName, methodDescriptor); \
1741 LOG_FATAL_IF(! (var), "Unable to find static method " methodName);
1742
Jeff Brown46b9ac02010-04-22 18:58:52 -07001743#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1744 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001745 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001746
1747int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001748 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001749 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001750 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001751 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1752
Jeff Brown9c3cda02010-06-15 01:31:58 -07001753 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001754
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001755 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001756 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Siarhei Vishniakouf9868962018-11-30 09:45:52 -08001757 gServiceClassInfo.clazz = reinterpret_cast<jclass>(env->NewGlobalRef(clazz));
Jeff Brown46b9ac02010-04-22 18:58:52 -07001758
Jeff Brown4532e612012-04-05 14:27:12 -07001759 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001760 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001761
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001762 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1763 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1764
Jeff Brown53384282012-08-20 20:16:01 -07001765 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1766 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001767
Jeff Brown4532e612012-04-05 14:27:12 -07001768 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
Robert Carre0a353c2018-08-02 16:38:04 -07001769 "notifyInputChannelBroken", "(Landroid/os/IBinder;)V");
Robert Carrbd26e652018-08-21 15:46:28 -07001770
1771 GET_METHOD_ID(gServiceClassInfo.notifyFocusChanged, clazz,
chaviw91089862019-01-09 15:49:11 -08001772 "notifyFocusChanged", "(Landroid/os/IBinder;Landroid/os/IBinder;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001773
Jeff Brown4532e612012-04-05 14:27:12 -07001774 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001775 "notifyANR",
Robert Carr679ccb02018-08-08 15:32:35 -07001776 "(Landroid/os/IBinder;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001777
Jeff Brown4532e612012-04-05 14:27:12 -07001778 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001779 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1780
Jeff Brown4532e612012-04-05 14:27:12 -07001781 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001782 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001783
Michael Wright70af00a2014-09-03 19:30:20 -07001784 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1785 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001786
Jeff Brown4532e612012-04-05 14:27:12 -07001787 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001788 "interceptKeyBeforeDispatching",
Robert Carre0a353c2018-08-02 16:38:04 -07001789 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001790
Jeff Brown4532e612012-04-05 14:27:12 -07001791 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001792 "dispatchUnhandledKey",
Robert Carre0a353c2018-08-02 16:38:04 -07001793 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001794
Jeff Brown4532e612012-04-05 14:27:12 -07001795 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001796 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001797
Jeff Brown4532e612012-04-05 14:27:12 -07001798 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001799 "getVirtualKeyQuietTimeMillis", "()I");
1800
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -07001801 GET_STATIC_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001802 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1803
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -07001804 GET_STATIC_METHOD_ID(gServiceClassInfo.getInputPortAssociations, clazz,
1805 "getInputPortAssociations", "()[Ljava/lang/String;");
1806
Jeff Brown4532e612012-04-05 14:27:12 -07001807 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001808 "getKeyRepeatTimeout", "()I");
1809
Jeff Brown4532e612012-04-05 14:27:12 -07001810 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001811 "getKeyRepeatDelay", "()I");
1812
Jeff Brown4532e612012-04-05 14:27:12 -07001813 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001814 "getHoverTapTimeout", "()I");
1815
Jeff Brown4532e612012-04-05 14:27:12 -07001816 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001817 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001818
Jeff Brown4532e612012-04-05 14:27:12 -07001819 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001820 "getDoubleTapTimeout", "()I");
1821
Jeff Brown4532e612012-04-05 14:27:12 -07001822 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001823 "getLongPressTimeout", "()I");
1824
Jeff Brown4532e612012-04-05 14:27:12 -07001825 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001826 "getPointerLayer", "()I");
1827
Jeff Brown4532e612012-04-05 14:27:12 -07001828 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001829 "getPointerIcon", "(I)Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001830
Arthur Hungb9b32002018-12-18 17:39:43 +08001831 GET_METHOD_ID(gServiceClassInfo.getPointerDisplayId, clazz,
1832 "getPointerDisplayId", "()I");
1833
Jeff Brown6ec6f792012-04-17 16:52:41 -07001834 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001835 "getKeyboardLayoutOverlay",
1836 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001837
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001838 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1839 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1840
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001841 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1842 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001843 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001844
Andrii Kulianfd8666d2018-10-05 16:58:39 -07001845 GET_METHOD_ID(gServiceClassInfo.getContextForDisplay, clazz,
1846 "getContextForDisplay",
1847 "(I)Landroid/content/Context;")
1848
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001849 // InputDevice
1850
1851 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1852 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1853
Jeff Brown6ec402b2010-07-28 15:48:59 -07001854 // KeyEvent
1855
1856 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001857 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1858
Jeff Brown8d608662010-08-30 03:02:23 -07001859 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001860
1861 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001862 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001863
RoboErikfb290df2013-12-16 11:27:55 -08001864 // InputDeviceIdentifier
1865
1866 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1867 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1868 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1869 "<init>", "(Ljava/lang/String;II)V");
1870
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001871 // TouchCalibration
1872
1873 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1874 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1875
1876 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1877 "getAffineTransform", "()[F");
1878
Jeff Brown46b9ac02010-04-22 18:58:52 -07001879 return 0;
1880}
1881
Jeff Brown46b9ac02010-04-22 18:58:52 -07001882} /* namespace android */