blob: 6fabadc5011fd2b41775d96d04455d7c3084ce16 [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 Vishniakou8e960d72017-11-22 19:12:22 -080035#include <android-base/stringprintf.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070037#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038
Jeff Brown46b9ac02010-04-22 18:58:52 -070039#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080040#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070041#include <utils/threads.h>
Michael Wrightbafea6e2017-06-12 15:25:47 +010042#include <utils/Trace.h>
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070043#include <utils/SortedVector.h>
Jeff Brown83c09682010-12-23 17:50:18 -080044
Robert Carr788f5742018-07-30 17:46:45 -070045#include <binder/IServiceManager.h>
46
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070048#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080049
Michael Wrightd6b473712014-02-10 15:56:36 -080050#include <inputflinger/InputManager.h>
51
Jeff Brown05dc66a2011-03-02 14:41:58 -080052#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070053#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080054#include <android_view_KeyEvent.h>
55#include <android_view_MotionEvent.h>
56#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070057#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080058#include <android/graphics/GraphicsJNI.h>
59
Steven Moreland2279b252017-07-19 09:50:45 -070060#include <nativehelper/ScopedLocalRef.h>
61#include <nativehelper/ScopedPrimitiveArray.h>
62#include <nativehelper/ScopedUtfChars.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070063
Jeff Brown4f8ecd82012-06-18 18:29:13 -070064#include "com_android_server_power_PowerManagerService.h"
Robert Carre1db3202018-07-23 15:24:59 -070065#include "android_hardware_input_InputApplicationHandle.h"
66#include "android_hardware_input_InputWindowHandle.h"
Santos Cordonee8931e2017-04-05 10:31:15 -070067#include "android_hardware_display_DisplayViewport.h"
Robert Carre0a353c2018-08-02 16:38:04 -070068#include "android_util_Binder.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070069
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010070#include <vector>
71
Michael Wrighta4051212015-07-23 17:04:40 +010072#define INDENT " "
73
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080074using android::base::StringPrintf;
75
Jeff Brown46b9ac02010-04-22 18:58:52 -070076namespace android {
77
Jeff Brown1a84fd12011-06-02 01:26:32 -070078// The exponent used to calculate the pointer speed scaling factor.
79// The scaling factor is calculated as 2 ^ (speed * exponent),
80// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070081static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070082
Jeff Brown46b9ac02010-04-22 18:58:52 -070083static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070084 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070085 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070086 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070087 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070088 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070089 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070090 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070091 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070092 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070093 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070094 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080095 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070096 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080097 jmethodID getKeyRepeatTimeout;
98 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070099 jmethodID getHoverTapTimeout;
100 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -0700101 jmethodID getDoubleTapTimeout;
102 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -0800103 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800104 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700105 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700106 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800107 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -0700108} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700109
110static struct {
111 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700112} gInputDeviceClassInfo;
113
114static struct {
115 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700116} gKeyEventClassInfo;
117
118static struct {
119 jclass clazz;
120} gMotionEventClassInfo;
121
RoboErikfb290df2013-12-16 11:27:55 -0800122static struct {
123 jclass clazz;
124 jmethodID constructor;
125} gInputDeviceIdentifierInfo;
126
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800127static struct {
128 jclass clazz;
129 jmethodID getAffineTransform;
130} gTouchCalibrationClassInfo;
131
RoboErikfb290df2013-12-16 11:27:55 -0800132
Jeff Brown928e0542011-01-10 11:17:36 -0800133
134// --- Global functions ---
135
Jeff Brown214eaf42011-05-26 19:17:02 -0700136template<typename T>
137inline static T min(const T& a, const T& b) {
138 return a < b ? a : b;
139}
140
141template<typename T>
142inline static T max(const T& a, const T& b) {
143 return a > b ? a : b;
144}
145
Michael Wrighta4051212015-07-23 17:04:40 +0100146static inline const char* toString(bool value) {
147 return value ? "true" : "false";
148}
149
Jun Mukai808196f2015-10-28 16:46:44 -0700150static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
151 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700152 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700153 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700154 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400155 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
156 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
157 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
158 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
159 bitmapCopy->rowBytes(), 0, 0);
160 }
Jun Mukai808196f2015-10-28 16:46:44 -0700161 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
162 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700163 }
164}
165
Jun Mukai808196f2015-10-28 16:46:44 -0700166static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
167 SpriteIcon* outSpriteIcon) {
168 PointerIcon pointerIcon;
169 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
170}
171
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100172static void updatePointerControllerFromViewport(
173 sp<PointerController> controller, const DisplayViewport* const viewport) {
174 if (controller != nullptr && viewport != nullptr) {
175 const int32_t width = viewport->logicalRight - viewport->logicalLeft;
176 const int32_t height = viewport->logicalBottom - viewport->logicalTop;
177 controller->setDisplayViewport(width, height, viewport->orientation);
178 }
179}
180
Jeff Brown905805a2011-10-12 13:57:59 -0700181enum {
182 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700183};
184
Jeff Brown928e0542011-01-10 11:17:36 -0800185
186// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800187
Jeff Brown9c3cda02010-06-15 01:31:58 -0700188class NativeInputManager : public virtual RefBase,
189 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700190 public virtual InputDispatcherPolicyInterface,
191 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700192protected:
193 virtual ~NativeInputManager();
194
195public:
Jeff Brown4532e612012-04-05 14:27:12 -0700196 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700197
198 inline sp<InputManager> getInputManager() const { return mInputManager; }
199
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800200 void dump(std::string& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700201
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100202 void setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700203
Robert Carre0a353c2018-08-02 16:38:04 -0700204 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel, int32_t displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700205 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
206
Arthur Hung39134b22018-08-14 11:58:28 +0800207 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray, int32_t displayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800208 void setFocusedApplication(JNIEnv* env, int32_t displayId, jobject applicationHandleObj);
209 void setFocusedDisplay(JNIEnv* env, int32_t displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700210 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800211 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700212 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700213 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700214 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700215 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800216 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100217 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800218 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700219 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800220 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700221
Jeff Brown9c3cda02010-06-15 01:31:58 -0700222 /* --- InputReaderPolicyInterface implementation --- */
223
Jeff Brown214eaf42011-05-26 19:17:02 -0700224 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800225 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700226 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800227 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100228 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700229 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
230 jfloatArray matrixArr);
231 virtual TouchAffineTransformation getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100232 const std::string& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700233
234 /* --- InputDispatcherPolicyInterface implementation --- */
235
Jeff Brownbcc046a2012-09-27 20:46:43 -0700236 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700237 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700238 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700239 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carre0a353c2018-08-02 16:38:04 -0700240 const sp<IBinder>& token,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800241 const std::string& reason);
Robert Carre0a353c2018-08-02 16:38:04 -0700242 virtual void notifyInputChannelBroken(const sp<IBinder>& token);
Jeff Brown0029c662011-03-30 02:25:18 -0700243 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700244 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800245 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800246 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700247 virtual nsecs_t interceptKeyBeforeDispatching(
Robert Carre0a353c2018-08-02 16:38:04 -0700248 const sp<IBinder>& token,
Jeff Brownb88102f2010-09-08 11:49:43 -0700249 const KeyEvent* keyEvent, uint32_t policyFlags);
Robert Carre0a353c2018-08-02 16:38:04 -0700250 virtual bool dispatchUnhandledKey(const sp<IBinder>& token,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800251 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700252 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700253 virtual bool checkInjectEventsPermissionNonReentrant(
254 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700255
Jeff Brown2352b972011-04-12 22:39:53 -0700256 /* --- PointerControllerPolicyInterface implementation --- */
257
Jun Mukai19a56012015-11-24 11:25:52 -0800258 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700259 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700260 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
261 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900262 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700263 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700264
Jeff Brown9c3cda02010-06-15 01:31:58 -0700265private:
266 sp<InputManager> mInputManager;
267
Jeff Brown2352b972011-04-12 22:39:53 -0700268 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700269 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800270 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700271
Jeff Brown83c09682010-12-23 17:50:18 -0800272 Mutex mLock;
273 struct Locked {
274 // Display size information.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100275 std::vector<DisplayViewport> viewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800276
Jeff Brown05dc66a2011-03-02 14:41:58 -0800277 // System UI visibility.
278 int32_t systemUiVisibility;
279
Jeff Brown1a84fd12011-06-02 01:26:32 -0700280 // Pointer speed.
281 int32_t pointerSpeed;
282
Jeff Brown474dcb52011-06-14 20:22:50 -0700283 // True if pointer gestures are enabled.
284 bool pointerGesturesEnabled;
285
Jeff Browndaf4a122011-08-26 17:14:14 -0700286 // Show touches feature enable/disable.
287 bool showTouches;
288
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800289 // Pointer capture feature enable/disable.
290 bool pointerCapture;
291
Jeff Brown5541de92011-04-11 11:54:25 -0700292 // Sprite controller singleton, created on first use.
293 sp<SpriteController> spriteController;
294
Jeff Brown83c09682010-12-23 17:50:18 -0800295 // Pointer controller singleton, created and destroyed as needed.
296 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700297
298 // Input devices to be disabled
299 SortedVector<int32_t> disabledInputDevices;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100300 } mLocked GUARDED_BY(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700301
Michael Wrighta4051212015-07-23 17:04:40 +0100302 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700303
Jeff Brown2352b972011-04-12 22:39:53 -0700304 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800305 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700306 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800307
Jeff Brownb88102f2010-09-08 11:49:43 -0700308 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700309
Jeff Brown9c3cda02010-06-15 01:31:58 -0700310 static inline JNIEnv* jniEnv() {
311 return AndroidRuntime::getJNIEnv();
312 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700313};
314
Jeff Brown928e0542011-01-10 11:17:36 -0800315
Jeff Brown9c3cda02010-06-15 01:31:58 -0700316
Jeff Brown2352b972011-04-12 22:39:53 -0700317NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700318 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700319 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320 JNIEnv* env = jniEnv();
321
Jeff Brown2352b972011-04-12 22:39:53 -0700322 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700323 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700324
Jeff Brown83c09682010-12-23 17:50:18 -0800325 {
326 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800327 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700328 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700329 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700330 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800331 mLocked.pointerCapture = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800332 }
Michael Wrighta4051212015-07-23 17:04:40 +0100333 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800334
Prabir Pradhane5696a52018-11-14 19:55:21 -0800335 mInputManager = new InputManager(this, this);
Robert Carr679ccb02018-08-08 15:32:35 -0700336 defaultServiceManager()->addService(String16("inputflinger"),
337 mInputManager, false);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700338}
339
340NativeInputManager::~NativeInputManager() {
341 JNIEnv* env = jniEnv();
342
Jeff Brown2352b972011-04-12 22:39:53 -0700343 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700344 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700345}
346
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800347void NativeInputManager::dump(std::string& dump) {
348 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100349 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800350 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100351 }
352 {
353 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800354 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100355 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800356 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
357 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100358 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800359 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
360 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100361 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800362 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100363
Jeff Brownb88102f2010-09-08 11:49:43 -0700364 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800365 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700366
Jeff Brownb88102f2010-09-08 11:49:43 -0700367 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800368 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700369}
370
Jeff Brown7fbdc842010-06-17 20:52:56 -0700371bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700372 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000373 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700374 LOGE_EX(env);
375 env->ExceptionClear();
376 return true;
377 }
378 return false;
379}
380
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100381static const DisplayViewport* findInternalViewport(const std::vector<DisplayViewport>& viewports) {
382 for (const DisplayViewport& v : viewports) {
383 if (v.type == ViewportType::VIEWPORT_INTERNAL) {
384 return &v;
385 }
386 }
387 return nullptr;
388}
389
390void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
391 std::vector<DisplayViewport> viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700392
393 if (viewportObjArray) {
394 jsize length = env->GetArrayLength(viewportObjArray);
395 for (jsize i = 0; i < length; i++) {
396 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
397 if (! viewportObj) {
398 break; // found null element indicating end of used portion of the array
399 }
400
401 DisplayViewport viewport;
402 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100403 ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
404 viewports.push_back(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -0700405
406 env->DeleteLocalRef(viewportObj);
407 }
408 }
409
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100410 const DisplayViewport* newInternalViewport = findInternalViewport(viewports);
Santos Cordonee8931e2017-04-05 10:31:15 -0700411 {
412 AutoMutex _l(mLock);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100413 const DisplayViewport* oldInternalViewport = findInternalViewport(mLocked.viewports);
414 // Internal viewport has changed if there wasn't one earlier, and there is one now, or,
415 // if they are different.
416 const bool internalViewportChanged = (newInternalViewport != nullptr) &&
Siarhei Vishniakoue5bf8662018-10-04 09:33:24 -0700417 (oldInternalViewport == nullptr || (*oldInternalViewport != *newInternalViewport));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100418 if (internalViewportChanged) {
419 sp<PointerController> controller = mLocked.pointerController.promote();
420 updatePointerControllerFromViewport(controller, newInternalViewport);
421 }
422 mLocked.viewports = viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700423 }
424
425 mInputManager->getReader()->requestRefreshConfiguration(
426 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
427}
428
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700429status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Robert Carre0a353c2018-08-02 16:38:04 -0700430 const sp<InputChannel>& inputChannel, int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100431 ATRACE_CALL();
Robert Carre0a353c2018-08-02 16:38:04 -0700432 return mInputManager->getDispatcher()->registerInputChannel(
433 inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700434}
435
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700436status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700437 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100438 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700439 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700440}
441
Jeff Brown214eaf42011-05-26 19:17:02 -0700442void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100443 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700444 JNIEnv* env = jniEnv();
445
Jeff Brown4532e612012-04-05 14:27:12 -0700446 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
447 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700448 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
449 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
450 }
451
452 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700453 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
454 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700455 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
456 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700457 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700458 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100459 const char* deviceNameChars = env->GetStringUTFChars(item, nullptr);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100460 outConfig->excludedDeviceNames.push_back(deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700461 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700462 env->DeleteLocalRef(item);
463 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700464 env->DeleteLocalRef(excludedDeviceNames);
465 }
466
Jeff Brown4532e612012-04-05 14:27:12 -0700467 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
468 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700469 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700470 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
471 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700472 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700473 jint longPressTimeout = env->CallIntMethod(mServiceObj,
474 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700475 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700476 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700477
478 // We must ensure that the tap-drag interval is significantly shorter than
479 // the long-press timeout because the tap is held down for the entire duration
480 // of the double-tap timeout.
481 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700482 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700483 outConfig->pointerGestureTapDragInterval =
484 milliseconds_to_nanoseconds(tapDragInterval);
485 }
486 }
487 }
488
Jeff Brown4532e612012-04-05 14:27:12 -0700489 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
490 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700491 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
492 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700493 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700494
495 { // acquire lock
496 AutoMutex _l(mLock);
497
498 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
499 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700500 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700501
Jeff Browndaf4a122011-08-26 17:14:14 -0700502 outConfig->showTouches = mLocked.showTouches;
503
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800504 outConfig->pointerCapture = mLocked.pointerCapture;
505
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100506 outConfig->setDisplayViewports(mLocked.viewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700507
508 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700509 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700510}
511
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700512sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100513 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800514 AutoMutex _l(mLock);
515
516 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100517 if (controller == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700518 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800519
Jeff Brown2352b972011-04-12 22:39:53 -0700520 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800521 mLocked.pointerController = controller;
522
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100523 const DisplayViewport* internalViewport = findInternalViewport(mLocked.viewports);
524 updatePointerControllerFromViewport(controller, internalViewport);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800525
Jeff Brown2352b972011-04-12 22:39:53 -0700526 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800527 }
528 return controller;
529}
530
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100531void NativeInputManager::ensureSpriteControllerLocked() REQUIRES(mLock) {
532 if (mLocked.spriteController == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700533 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700534 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700535 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
536 layer = -1;
537 }
538 mLocked.spriteController = new SpriteController(mLooper, layer);
539 }
540}
541
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700542void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100543 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700544 JNIEnv* env = jniEnv();
545
546 size_t count = inputDevices.size();
547 jobjectArray inputDevicesObjArray = env->NewObjectArray(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100548 count, gInputDeviceClassInfo.clazz, nullptr);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700549 if (inputDevicesObjArray) {
550 bool error = false;
551 for (size_t i = 0; i < count; i++) {
552 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
553 if (!inputDeviceObj) {
554 error = true;
555 break;
556 }
557
558 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
559 env->DeleteLocalRef(inputDeviceObj);
560 }
561
562 if (!error) {
563 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
564 inputDevicesObjArray);
565 }
566
567 env->DeleteLocalRef(inputDevicesObjArray);
568 }
569
570 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
571}
572
Jeff Brown6ec6f792012-04-17 16:52:41 -0700573sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800574 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100575 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700576 JNIEnv* env = jniEnv();
577
578 sp<KeyCharacterMap> result;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100579 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
RoboErikfb290df2013-12-16 11:27:55 -0800580 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
581 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
582 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700583 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800584 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700585 if (arrayObj.get()) {
586 ScopedLocalRef<jstring> filenameObj(env,
587 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
588 ScopedLocalRef<jstring> contentsObj(env,
589 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
590 ScopedUtfChars filenameChars(env, filenameObj.get());
591 ScopedUtfChars contentsChars(env, contentsObj.get());
592
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100593 KeyCharacterMap::loadContents(filenameChars.c_str(),
594 contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700595 }
596 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
597 return result;
598}
599
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100600std::string NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100601 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700602 JNIEnv* env = jniEnv();
603
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100604 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.c_str()));
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700605 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
606 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100607 std::string result;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700608 if (aliasObj.get()) {
609 ScopedUtfChars aliasChars(env, aliasObj.get());
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100610 result = aliasChars.c_str();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700611 }
612 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
613 return result;
614}
615
Jeff Brownbcc046a2012-09-27 20:46:43 -0700616void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700617 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700618#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700619 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
620 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700621#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100622 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700623
624 JNIEnv* env = jniEnv();
625
Jeff Brown53384282012-08-20 20:16:01 -0700626 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700627 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700628 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700629}
630
Jeff Brown9c3cda02010-06-15 01:31:58 -0700631void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
632#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000633 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700634#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100635 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700636
637 JNIEnv* env = jniEnv();
638
Jeff Brown4532e612012-04-05 14:27:12 -0700639 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700640 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700641}
642
Jeff Brown519e0242010-09-15 15:18:56 -0700643nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carre0a353c2018-08-02 16:38:04 -0700644 const sp<IBinder>& token, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700645#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000646 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700647#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100648 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700649
650 JNIEnv* env = jniEnv();
651
Robert Carre0a353c2018-08-02 16:38:04 -0700652 jobject tokenObj = javaObjectForIBinder(env, token);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800653 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700654
Jeff Brown4532e612012-04-05 14:27:12 -0700655 jlong newTimeout = env->CallLongMethod(mServiceObj,
Robert Carr679ccb02018-08-08 15:32:35 -0700656 gServiceClassInfo.notifyANR, tokenObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700657 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700658 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
659 newTimeout = 0; // abort dispatch
660 } else {
661 assert(newTimeout >= 0);
662 }
663
Jeff Brownbd181bb2013-09-10 16:44:24 -0700664 env->DeleteLocalRef(reasonObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700665 return newTimeout;
666}
667
Robert Carre0a353c2018-08-02 16:38:04 -0700668void NativeInputManager::notifyInputChannelBroken(const sp<IBinder>& token) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700669#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000670 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700671#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100672 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700673
Jeff Brown7fbdc842010-06-17 20:52:56 -0700674 JNIEnv* env = jniEnv();
675
Robert Carre0a353c2018-08-02 16:38:04 -0700676 jobject tokenObj = javaObjectForIBinder(env, token);
677 if (tokenObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700678 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Robert Carre0a353c2018-08-02 16:38:04 -0700679 tokenObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700680 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700681 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700682}
683
Jeff Brown214eaf42011-05-26 19:17:02 -0700684void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100685 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700686 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800687
Jeff Brown4532e612012-04-05 14:27:12 -0700688 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
689 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700690 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
691 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
692 }
Jeff Browna4547672011-03-02 21:38:11 -0800693
Jeff Brown4532e612012-04-05 14:27:12 -0700694 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
695 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700696 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
697 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
698 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700699}
700
Arthur Hung39134b22018-08-14 11:58:28 +0800701void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
702 int32_t displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700703 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700704
Jeff Brown9302c872011-07-13 22:51:29 -0700705 if (windowHandleObjArray) {
706 jsize length = env->GetArrayLength(windowHandleObjArray);
707 for (jsize i = 0; i < length; i++) {
708 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
709 if (! windowHandleObj) {
710 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700711 }
Jeff Brown9302c872011-07-13 22:51:29 -0700712
713 sp<InputWindowHandle> windowHandle =
714 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100715 if (windowHandle != nullptr) {
Jeff Brown9302c872011-07-13 22:51:29 -0700716 windowHandles.push(windowHandle);
717 }
718 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700719 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700720 }
Jeff Brown349703e2010-06-22 01:27:15 -0700721
Arthur Hung39134b22018-08-14 11:58:28 +0800722 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700723
724 // Do this after the dispatcher has updated the window handle state.
725 bool newPointerGesturesEnabled = true;
726 size_t numWindows = windowHandles.size();
727 for (size_t i = 0; i < numWindows; i++) {
728 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700729 const InputWindowInfo* windowInfo = windowHandle->getInfo();
730 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
731 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700732 newPointerGesturesEnabled = false;
733 }
734 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700735
736 uint32_t changes = 0;
737 { // acquire lock
738 AutoMutex _l(mLock);
739
740 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
741 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
742 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
743 }
744 } // release lock
745
746 if (changes) {
747 mInputManager->getReader()->requestRefreshConfiguration(changes);
748 }
Jeff Brown349703e2010-06-22 01:27:15 -0700749}
750
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800751void NativeInputManager::setFocusedApplication(JNIEnv* env, int32_t displayId,
752 jobject applicationHandleObj) {
Jeff Brown9302c872011-07-13 22:51:29 -0700753 sp<InputApplicationHandle> applicationHandle =
754 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800755 mInputManager->getDispatcher()->setFocusedApplication(displayId, applicationHandle);
756}
757
758void NativeInputManager::setFocusedDisplay(JNIEnv* env, int32_t displayId) {
759 mInputManager->getDispatcher()->setFocusedDisplay(displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700760}
761
762void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700763 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700764}
765
Jeff Brown05dc66a2011-03-02 14:41:58 -0800766void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
767 AutoMutex _l(mLock);
768
769 if (mLocked.systemUiVisibility != visibility) {
770 mLocked.systemUiVisibility = visibility;
771
772 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100773 if (controller != nullptr) {
Jeff Brown2352b972011-04-12 22:39:53 -0700774 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800775 }
776 }
777}
778
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100779void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller)
780 REQUIRES(mLock) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800781 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700782 controller->setInactivityTimeout(lightsOut
783 ? PointerController::INACTIVITY_TIMEOUT_SHORT
784 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800785}
786
Jeff Brown1a84fd12011-06-02 01:26:32 -0700787void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700788 { // acquire lock
789 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700790
Jeff Brown474dcb52011-06-14 20:22:50 -0700791 if (mLocked.pointerSpeed == speed) {
792 return;
793 }
794
Steve Block6215d3f2012-01-04 20:05:49 +0000795 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700796 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700797 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700798
Jeff Brown474dcb52011-06-14 20:22:50 -0700799 mInputManager->getReader()->requestRefreshConfiguration(
800 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700801}
802
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700803void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
804 { // acquire lock
805 AutoMutex _l(mLock);
806
807 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
808 bool currentlyEnabled = index < 0;
809 if (!enabled && currentlyEnabled) {
810 mLocked.disabledInputDevices.add(deviceId);
811 }
812 if (enabled && !currentlyEnabled) {
813 mLocked.disabledInputDevices.remove(deviceId);
814 }
815 } // release lock
816
817 mInputManager->getReader()->requestRefreshConfiguration(
818 InputReaderConfiguration::CHANGE_ENABLED_STATE);
819}
820
Jeff Browndaf4a122011-08-26 17:14:14 -0700821void NativeInputManager::setShowTouches(bool enabled) {
822 { // acquire lock
823 AutoMutex _l(mLock);
824
825 if (mLocked.showTouches == enabled) {
826 return;
827 }
828
Steve Block6215d3f2012-01-04 20:05:49 +0000829 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700830 mLocked.showTouches = enabled;
831 } // release lock
832
833 mInputManager->getReader()->requestRefreshConfiguration(
834 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
835}
836
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800837void NativeInputManager::setPointerCapture(bool enabled) {
838 { // acquire lock
839 AutoMutex _l(mLock);
840
841 if (mLocked.pointerCapture == enabled) {
842 return;
843 }
844
845 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
846 mLocked.pointerCapture = enabled;
847 } // release lock
848
849 mInputManager->getReader()->requestRefreshConfiguration(
850 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
851}
852
Jeff Brown037c33e2014-04-09 00:31:55 -0700853void NativeInputManager::setInteractive(bool interactive) {
854 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700855}
856
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800857void NativeInputManager::reloadCalibration() {
858 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100859 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800860}
861
Michael Wrighte051f6f2016-05-13 17:44:16 +0100862void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800863 AutoMutex _l(mLock);
864 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100865 if (controller != nullptr) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100866 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800867 }
868}
869
870void NativeInputManager::reloadPointerIcons() {
871 AutoMutex _l(mLock);
872 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100873 if (controller != nullptr) {
Jun Mukai19a56012015-11-24 11:25:52 -0800874 controller->reloadPointerResources();
875 }
Jun Mukai1db53972015-09-11 18:08:31 -0700876}
877
Jun Mukaid4eaef72015-10-30 15:54:33 -0700878void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
879 AutoMutex _l(mLock);
880 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100881 if (controller != nullptr) {
Jun Mukaid4eaef72015-10-30 15:54:33 -0700882 controller->setCustomPointerIcon(icon);
883 }
884}
885
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800886TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
887 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100888 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800889 ScopedFloatArrayRO matrix(env, matrixArr);
890 assert(matrix.size() == 6);
891
892 TouchAffineTransformation transform;
893 transform.x_scale = matrix[0];
894 transform.x_ymix = matrix[1];
895 transform.x_offset = matrix[2];
896 transform.y_xmix = matrix[3];
897 transform.y_scale = matrix[4];
898 transform.y_offset = matrix[5];
899
900 return transform;
901}
902
903TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100904 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800905 JNIEnv* env = jniEnv();
906
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100907 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.c_str()));
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800908
909 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700910 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
911 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800912
913 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
914 gTouchCalibrationClassInfo.getAffineTransform));
915
916 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
917
918 env->DeleteLocalRef(matrixArr);
919 env->DeleteLocalRef(cal);
920
921 return transform;
922}
923
Jeff Brown0029c662011-03-30 02:25:18 -0700924bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100925 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700926 jobject inputEventObj;
927
928 JNIEnv* env = jniEnv();
929 switch (inputEvent->getType()) {
930 case AINPUT_EVENT_TYPE_KEY:
931 inputEventObj = android_view_KeyEvent_fromNative(env,
932 static_cast<const KeyEvent*>(inputEvent));
933 break;
934 case AINPUT_EVENT_TYPE_MOTION:
935 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
936 static_cast<const MotionEvent*>(inputEvent));
937 break;
938 default:
939 return true; // dispatch the event normally
940 }
941
942 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000943 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700944 return true; // dispatch the event normally
945 }
946
947 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700948 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700949 inputEventObj, policyFlags);
950 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
951 pass = true;
952 }
953 env->DeleteLocalRef(inputEventObj);
954 return pass;
955}
956
Jeff Brown1f245102010-11-18 20:53:46 -0800957void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
958 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100959 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700960 // Policy:
961 // - Ignore untrusted events and pass them along.
962 // - Ask the window manager what to do with normal events and trusted injected events.
963 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100964 bool interactive = mInteractive.load();
965 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700966 policyFlags |= POLICY_FLAG_INTERACTIVE;
967 }
Jeff Brown3122e442010-10-11 23:32:49 -0700968 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800969 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700970 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800971 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
972 jint wmActions;
973 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700974 wmActions = env->CallIntMethod(mServiceObj,
975 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700976 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800977 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
978 wmActions = 0;
979 }
980 android_view_KeyEvent_recycle(env, keyEventObj);
981 env->DeleteLocalRef(keyEventObj);
982 } else {
Steve Block3762c312012-01-06 19:20:56 +0000983 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700984 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700985 }
986
Jeff Brown56194eb2011-03-02 19:23:13 -0800987 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700988 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100989 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -0700990 policyFlags |= POLICY_FLAG_PASS_TO_USER;
991 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700992 }
993}
994
Jeff Brown56194eb2011-03-02 19:23:13 -0800995void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100996 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700997 // Policy:
998 // - Ignore untrusted events and pass them along.
999 // - No special filtering for injected events required at this time.
1000 // - Filter normal events based on screen state.
1001 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001002 bool interactive = mInteractive.load();
1003 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001004 policyFlags |= POLICY_FLAG_INTERACTIVE;
1005 }
Jeff Brown3122e442010-10-11 23:32:49 -07001006 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001007 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001008 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001009 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001010 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001011 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001012 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001013 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001014 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001015 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001016 wmActions = 0;
1017 }
1018
Jeff Brown56194eb2011-03-02 19:23:13 -08001019 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001020 }
Jeff Brown3122e442010-10-11 23:32:49 -07001021 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001022 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001023 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1024 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001025 }
1026}
1027
Jeff Brown56194eb2011-03-02 19:23:13 -08001028void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1029 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001030 if (wmActions & WM_ACTION_PASS_TO_USER) {
1031 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1032 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001033#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001034 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001035#endif
1036 }
1037}
1038
Jeff Brown905805a2011-10-12 13:57:59 -07001039nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Robert Carre0a353c2018-08-02 16:38:04 -07001040 const sp<IBinder>& token,
Jeff Browne20c9e02010-10-11 14:20:19 -07001041 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001042 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001043 // Policy:
1044 // - Ignore untrusted events and pass them along.
1045 // - Filter normal events and trusted injected events through the window manager policy to
1046 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001047 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001048 if (policyFlags & POLICY_FLAG_TRUSTED) {
1049 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001050
Robert Carre0a353c2018-08-02 16:38:04 -07001051 // Token may be null
1052 jobject tokenObj = javaObjectForIBinder(env, token);
1053
Jeff Brown1f245102010-11-18 20:53:46 -08001054 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1055 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001056 jlong delayMillis = env->CallLongMethod(mServiceObj,
1057 gServiceClassInfo.interceptKeyBeforeDispatching,
Robert Carre0a353c2018-08-02 16:38:04 -07001058 tokenObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001059 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1060 android_view_KeyEvent_recycle(env, keyEventObj);
1061 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001062 if (!error) {
1063 if (delayMillis < 0) {
1064 result = -1;
1065 } else if (delayMillis > 0) {
1066 result = milliseconds_to_nanoseconds(delayMillis);
1067 }
1068 }
Jeff Brown1f245102010-11-18 20:53:46 -08001069 } else {
Steve Block3762c312012-01-06 19:20:56 +00001070 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001071 }
Jeff Brown3122e442010-10-11 23:32:49 -07001072 }
Jeff Brown1f245102010-11-18 20:53:46 -08001073 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001074}
1075
Robert Carre0a353c2018-08-02 16:38:04 -07001076bool NativeInputManager::dispatchUnhandledKey(const sp<IBinder>& token,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001077 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001078 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001079 // Policy:
1080 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001081 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001082 if (policyFlags & POLICY_FLAG_TRUSTED) {
1083 JNIEnv* env = jniEnv();
1084
Robert Carre0a353c2018-08-02 16:38:04 -07001085 // Note: tokenObj may be null.
1086 jobject tokenObj = javaObjectForIBinder(env, token);
Jeff Brown1f245102010-11-18 20:53:46 -08001087 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1088 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001089 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1090 gServiceClassInfo.dispatchUnhandledKey,
Robert Carre0a353c2018-08-02 16:38:04 -07001091 tokenObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001092 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001093 fallbackKeyEventObj = nullptr;
Jeff Brownda3d5a92011-03-29 15:11:34 -07001094 }
Jeff Brown1f245102010-11-18 20:53:46 -08001095 android_view_KeyEvent_recycle(env, keyEventObj);
1096 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001097
1098 if (fallbackKeyEventObj) {
1099 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1100 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1101 outFallbackKeyEvent)) {
1102 result = true;
1103 }
1104 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1105 env->DeleteLocalRef(fallbackKeyEventObj);
1106 }
Jeff Brown1f245102010-11-18 20:53:46 -08001107 } else {
Steve Block3762c312012-01-06 19:20:56 +00001108 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001109 }
Jeff Brown3915bb82010-11-05 15:02:16 -07001110 }
Jeff Brown1f245102010-11-18 20:53:46 -08001111 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001112}
1113
Jeff Brown01ce2e92010-09-26 22:20:12 -07001114void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001115 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001116 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001117}
1118
Jeff Brown349703e2010-06-22 01:27:15 -07001119
Jeff Brownb88102f2010-09-08 11:49:43 -07001120bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1121 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001122 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001123 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001124 jboolean result = env->CallBooleanMethod(mServiceObj,
1125 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001126 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1127 result = false;
1128 }
Jeff Brown349703e2010-06-22 01:27:15 -07001129 return result;
1130}
1131
Jun Mukai19a56012015-11-24 11:25:52 -08001132void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001133 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001134 JNIEnv* env = jniEnv();
1135
1136 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1137 mServiceObj, gServiceClassInfo.getPointerIcon));
1138 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1139 return;
1140 }
1141
1142 PointerIcon pointerIcon;
1143 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1144 mContextObj, &pointerIcon);
1145 if (!status && !pointerIcon.isNullIcon()) {
1146 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1147 } else {
1148 *icon = SpriteIcon();
1149 }
1150}
1151
Jeff Brown2352b972011-04-12 22:39:53 -07001152void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001153 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001154 JNIEnv* env = jniEnv();
1155
1156 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1157 &outResources->spotHover);
1158 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1159 &outResources->spotTouch);
1160 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1161 &outResources->spotAnchor);
1162}
1163
Jun Mukai808196f2015-10-28 16:46:44 -07001164void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1165 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001166 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001167 JNIEnv* env = jniEnv();
1168
1169 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1170 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001171 PointerIcon pointerIcon;
1172 loadSystemIconAsSpriteWithPointerIcon(
1173 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1174 if (!pointerIcon.bitmapFrames.empty()) {
1175 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1176 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1177 animationData.durationPerFrame =
1178 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1179 animationData.animationFrames.reserve(numFrames);
1180 animationData.animationFrames.push_back(SpriteIcon(
1181 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1182 for (size_t i = 0; i < numFrames - 1; ++i) {
1183 animationData.animationFrames.push_back(SpriteIcon(
1184 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1185 }
1186 }
Jun Mukai1db53972015-09-11 18:08:31 -07001187 }
Jun Mukai808196f2015-10-28 16:46:44 -07001188 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1189 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001190}
1191
Jun Mukai5ec74202015-10-07 16:58:09 +09001192int32_t NativeInputManager::getDefaultPointerIconId() {
1193 return POINTER_ICON_STYLE_ARROW;
1194}
Jeff Brown83c09682010-12-23 17:50:18 -08001195
Jun Mukaid4eaef72015-10-30 15:54:33 -07001196int32_t NativeInputManager::getCustomPointerIconId() {
1197 return POINTER_ICON_STYLE_CUSTOM;
1198}
1199
Jeff Brown9c3cda02010-06-15 01:31:58 -07001200// ----------------------------------------------------------------------------
1201
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001202static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001203 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001204 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001205 if (messageQueue == nullptr) {
Jeff Brown864693462013-01-28 14:25:53 -08001206 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1207 return 0;
1208 }
1209
Jeff Brown603b4452012-04-06 17:39:41 -07001210 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1211 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001212 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001213 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001214}
1215
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001216static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001217 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001218
Jeff Brown4532e612012-04-05 14:27:12 -07001219 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001220 if (result) {
1221 jniThrowRuntimeException(env, "Input manager could not be started.");
1222 }
1223}
1224
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001225static void nativeSetDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
Santos Cordonee8931e2017-04-05 10:31:15 -07001226 jobjectArray viewportObjArray) {
1227 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001228 im->setDisplayViewports(env, viewportObjArray);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001229}
1230
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001231static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001232 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001233 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001234
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001235 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001236 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001237}
1238
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001239static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001240 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001241 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001242
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001243 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001244 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001245}
1246
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001247static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001248 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001249 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001250
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001251 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001252 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001253}
1254
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001255static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001256 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001257 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001258
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001259 int32_t* codes = env->GetIntArrayElements(keyCodes, nullptr);
1260 uint8_t* flags = env->GetBooleanArrayElements(outFlags, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001261 jsize numCodes = env->GetArrayLength(keyCodes);
1262 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001263 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001264 if (im->getInputManager()->getReader()->hasKeys(
1265 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1266 result = JNI_TRUE;
1267 } else {
1268 result = JNI_FALSE;
1269 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001270 } else {
1271 result = JNI_FALSE;
1272 }
1273
1274 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1275 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1276 return result;
1277}
1278
1279static void throwInputChannelNotInitialized(JNIEnv* env) {
1280 jniThrowException(env, "java/lang/IllegalStateException",
1281 "inputChannel is not initialized");
1282}
1283
Jeff Brown4532e612012-04-05 14:27:12 -07001284static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001285 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001286 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1287
Steve Block8564c8d2012-01-05 23:22:43 +00001288 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001289 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001290 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001291}
1292
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001293static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Robert Carre0a353c2018-08-02 16:38:04 -07001294 jlong ptr, jobject inputChannelObj, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001295 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001296
1297 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1298 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001299 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001300 throwInputChannelNotInitialized(env);
1301 return;
1302 }
Robert Carre0a353c2018-08-02 16:38:04 -07001303 bool monitor = inputChannel->getToken() == nullptr && displayId != ADISPLAY_ID_NONE;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001304
Robert Carre0a353c2018-08-02 16:38:04 -07001305 status_t status = im->registerInputChannel(env, inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001306
Jeff Brown46b9ac02010-04-22 18:58:52 -07001307 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001308 std::string message;
1309 message += StringPrintf("Failed to register input channel. status=%d", status);
1310 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001311 return;
1312 }
1313
Arthur Hungbe5ce212018-09-13 18:41:56 +08001314 // If inputWindowHandle is null and displayId >= 0, treat inputChannel as monitor.
Robert Carre0a353c2018-08-02 16:38:04 -07001315 if (!monitor) {
Jeff Browna41ca772010-08-11 14:46:32 -07001316 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001317 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001318 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001319}
1320
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001321static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001322 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001323 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001324
1325 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1326 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001327 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001328 throwInputChannelNotInitialized(env);
1329 return;
1330 }
1331
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001332 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001333
Jeff Brown4532e612012-04-05 14:27:12 -07001334 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001335 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001336 std::string message;
1337 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1338 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001339 }
1340}
1341
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001342static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001343 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001344 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001345
Jeff Brown4532e612012-04-05 14:27:12 -07001346 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001347}
1348
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001349static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001350 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001351 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001352 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001353
Jeff Brown6ec402b2010-07-28 15:48:59 -07001354 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1355 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001356 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1357 if (status) {
1358 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1359 return INPUT_EVENT_INJECTION_FAILED;
1360 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001361
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001362 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001363 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001364 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001365 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001366 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1367 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001368 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1369 return INPUT_EVENT_INJECTION_FAILED;
1370 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001371
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001372 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001373 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001374 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001375 } else {
1376 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001377 return INPUT_EVENT_INJECTION_FAILED;
1378 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001379}
1380
Andrii Kulian112d0562016-03-08 10:44:22 -08001381static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1382 jlong ptr, jint deviceId) {
1383 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001384
Andrii Kulian112d0562016-03-08 10:44:22 -08001385 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1386}
1387
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001388static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hung39134b22018-08-14 11:58:28 +08001389 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001390 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001391
Arthur Hung39134b22018-08-14 11:58:28 +08001392 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001393}
1394
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001395static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001396 jlong ptr, jint displayId, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001397 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001398
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001399 im->setFocusedApplication(env, displayId, applicationHandleObj);
1400}
1401
1402static void nativeSetFocusedDisplay(JNIEnv* env, jclass /* clazz */,
1403 jlong ptr, jint displayId) {
1404 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1405
1406 im->setFocusedDisplay(env, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001407}
1408
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001409static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1410 jboolean enabled) {
1411 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001412
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001413 im->setPointerCapture(enabled);
1414}
1415
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001416static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1417 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001418 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001419
Jeff Brown4532e612012-04-05 14:27:12 -07001420 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001421}
1422
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001423static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1424 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001425 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001426
Jeff Brown4532e612012-04-05 14:27:12 -07001427 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001428}
1429
Jeff Brown4532e612012-04-05 14:27:12 -07001430static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001431 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001432 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001433
1434 sp<InputChannel> fromChannel =
1435 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1436 sp<InputChannel> toChannel =
1437 android_view_InputChannel_getInputChannel(env, toChannelObj);
1438
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001439 if (fromChannel == nullptr || toChannel == nullptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001440 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001441 }
1442
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001443 if (im->getInputManager()->getDispatcher()->
1444 transferTouchFocus(fromChannel, toChannel)) {
1445 return JNI_TRUE;
1446 } else {
1447 return JNI_FALSE;
1448 }
Jeff Browne6504122010-09-27 14:52:15 -07001449}
1450
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001451static void nativeSetPointerSpeed(JNIEnv* /* env */,
1452 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001453 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001454
Jeff Brown4532e612012-04-05 14:27:12 -07001455 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001456}
1457
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001458static void nativeSetShowTouches(JNIEnv* /* env */,
1459 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001460 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001461
Jeff Brown4532e612012-04-05 14:27:12 -07001462 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001463}
1464
Jeff Brown037c33e2014-04-09 00:31:55 -07001465static void nativeSetInteractive(JNIEnv* env,
1466 jclass clazz, jlong ptr, jboolean interactive) {
1467 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1468
1469 im->setInteractive(interactive);
1470}
1471
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001472static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1473 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001474
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001475 im->reloadCalibration();
1476}
1477
Jeff Browna47425a2012-04-13 04:09:27 -07001478static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001479 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001480 jint repeat, jint token) {
1481 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1482
1483 size_t patternSize = env->GetArrayLength(patternObj);
1484 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001485 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001486 "which is more than the maximum supported size of %d.",
1487 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1488 return; // limit to reasonable size
1489 }
1490
1491 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001492 patternObj, nullptr));
Jeff Browna47425a2012-04-13 04:09:27 -07001493 nsecs_t pattern[patternSize];
1494 for (size_t i = 0; i < patternSize; i++) {
1495 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001496 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001497 }
1498 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1499
1500 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1501}
1502
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001503static void nativeCancelVibrate(JNIEnv* /* env */,
1504 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001505 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1506
1507 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1508}
1509
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001510static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1511 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001512 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1513
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001514 im->getInputManager()->getReader()->requestRefreshConfiguration(
1515 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1516}
1517
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001518static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1519 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001520 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1521
1522 im->getInputManager()->getReader()->requestRefreshConfiguration(
1523 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001524}
1525
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001526static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001527 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001528
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001529 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001530 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001531 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001532}
1533
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001534static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001535 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001536
Jeff Brown4532e612012-04-05 14:27:12 -07001537 im->getInputManager()->getReader()->monitor();
1538 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001539}
1540
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001541static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1542 jclass /* clazz */, jlong ptr, jint deviceId) {
1543 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1544
1545 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1546}
1547
1548static void nativeEnableInputDevice(JNIEnv* /* env */,
1549 jclass /* clazz */, jlong ptr, jint deviceId) {
1550 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1551
1552 im->setInputDeviceEnabled(deviceId, true);
1553}
1554
1555static void nativeDisableInputDevice(JNIEnv* /* env */,
1556 jclass /* clazz */, jlong ptr, jint deviceId) {
1557 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1558
1559 im->setInputDeviceEnabled(deviceId, false);
1560}
1561
Michael Wrighte051f6f2016-05-13 17:44:16 +01001562static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001563 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001564
Michael Wrighte051f6f2016-05-13 17:44:16 +01001565 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001566}
1567
Jun Mukai19a56012015-11-24 11:25:52 -08001568static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1569 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001570
Jun Mukai19a56012015-11-24 11:25:52 -08001571 im->reloadPointerIcons();
1572}
1573
Jun Mukaid4eaef72015-10-30 15:54:33 -07001574static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1575 jlong ptr, jobject iconObj) {
1576 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1577
1578 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001579 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1580 if (result) {
1581 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1582 return;
1583 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001584
1585 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001586 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1587 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1588 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1589 spriteIcon.bitmap.rowBytes(), 0, 0);
1590 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001591 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1592 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1593 im->setCustomPointerIcon(spriteIcon);
1594}
1595
Jeff Brown9c3cda02010-06-15 01:31:58 -07001596// ----------------------------------------------------------------------------
1597
Daniel Micay76f6a862015-09-19 17:31:01 -04001598static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001599 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001600 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001601 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001602 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001603 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001604 (void*) nativeStart },
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001605 { "nativeSetDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1606 (void*) nativeSetDisplayViewports },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001607 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001608 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001609 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001610 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001611 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001612 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001613 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001614 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001615 { "nativeRegisterInputChannel",
Robert Carre0a353c2018-08-02 16:38:04 -07001616 "(JLandroid/view/InputChannel;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001617 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001618 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001619 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001620 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001621 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001622 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001623 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001624 { "nativeToggleCapsLock", "(JI)V",
1625 (void*) nativeToggleCapsLock },
Robert Carr788f5742018-07-30 17:46:45 -07001626 { "nativeSetInputWindows", "(J[Landroid/view/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001627 (void*) nativeSetInputWindows },
Robert Carr788f5742018-07-30 17:46:45 -07001628 { "nativeSetFocusedApplication", "(JILandroid/view/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001629 (void*) nativeSetFocusedApplication },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001630 { "nativeSetFocusedDisplay", "(JI)V",
1631 (void*) nativeSetFocusedDisplay },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001632 { "nativeSetPointerCapture", "(JZ)V",
1633 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001634 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001635 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001636 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001637 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001638 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001639 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001640 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001641 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001642 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001643 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001644 { "nativeSetInteractive", "(JZ)V",
1645 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001646 { "nativeReloadCalibration", "(J)V",
1647 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001648 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001649 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001650 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001651 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001652 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001653 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001654 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001655 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001656 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001657 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001658 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001659 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001660 { "nativeIsInputDeviceEnabled", "(JI)Z",
1661 (void*) nativeIsInputDeviceEnabled },
1662 { "nativeEnableInputDevice", "(JI)V",
1663 (void*) nativeEnableInputDevice },
1664 { "nativeDisableInputDevice", "(JI)V",
1665 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001666 { "nativeSetPointerIconType", "(JI)V",
1667 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001668 { "nativeReloadPointerIcons", "(J)V",
1669 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001670 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1671 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001672};
1673
1674#define FIND_CLASS(var, className) \
1675 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001676 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001677
1678#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1679 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001680 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001681
1682#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1683 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001684 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001685
1686int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001687 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001688 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001689 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001690 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1691
Jeff Brown9c3cda02010-06-15 01:31:58 -07001692 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001693
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001694 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001695 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001696
Jeff Brown4532e612012-04-05 14:27:12 -07001697 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001698 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001699
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001700 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1701 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1702
Jeff Brown53384282012-08-20 20:16:01 -07001703 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1704 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001705
Jeff Brown4532e612012-04-05 14:27:12 -07001706 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
Robert Carre0a353c2018-08-02 16:38:04 -07001707 "notifyInputChannelBroken", "(Landroid/os/IBinder;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001708
Jeff Brown4532e612012-04-05 14:27:12 -07001709 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001710 "notifyANR",
Robert Carr679ccb02018-08-08 15:32:35 -07001711 "(Landroid/os/IBinder;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001712
Jeff Brown4532e612012-04-05 14:27:12 -07001713 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001714 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1715
Jeff Brown4532e612012-04-05 14:27:12 -07001716 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001717 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001718
Michael Wright70af00a2014-09-03 19:30:20 -07001719 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1720 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001721
Jeff Brown4532e612012-04-05 14:27:12 -07001722 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001723 "interceptKeyBeforeDispatching",
Robert Carre0a353c2018-08-02 16:38:04 -07001724 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001725
Jeff Brown4532e612012-04-05 14:27:12 -07001726 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001727 "dispatchUnhandledKey",
Robert Carre0a353c2018-08-02 16:38:04 -07001728 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001729
Jeff Brown4532e612012-04-05 14:27:12 -07001730 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001731 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001732
Jeff Brown4532e612012-04-05 14:27:12 -07001733 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001734 "getVirtualKeyQuietTimeMillis", "()I");
1735
Jeff Brown4532e612012-04-05 14:27:12 -07001736 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001737 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1738
Jeff Brown4532e612012-04-05 14:27:12 -07001739 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001740 "getKeyRepeatTimeout", "()I");
1741
Jeff Brown4532e612012-04-05 14:27:12 -07001742 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001743 "getKeyRepeatDelay", "()I");
1744
Jeff Brown4532e612012-04-05 14:27:12 -07001745 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001746 "getHoverTapTimeout", "()I");
1747
Jeff Brown4532e612012-04-05 14:27:12 -07001748 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001749 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001750
Jeff Brown4532e612012-04-05 14:27:12 -07001751 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001752 "getDoubleTapTimeout", "()I");
1753
Jeff Brown4532e612012-04-05 14:27:12 -07001754 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001755 "getLongPressTimeout", "()I");
1756
Jeff Brown4532e612012-04-05 14:27:12 -07001757 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001758 "getPointerLayer", "()I");
1759
Jeff Brown4532e612012-04-05 14:27:12 -07001760 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001761 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001762
Jeff Brown6ec6f792012-04-17 16:52:41 -07001763 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001764 "getKeyboardLayoutOverlay",
1765 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001766
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001767 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1768 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1769
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001770 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1771 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001772 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001773
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001774 // InputDevice
1775
1776 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1777 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1778
Jeff Brown6ec402b2010-07-28 15:48:59 -07001779 // KeyEvent
1780
1781 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001782 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1783
Jeff Brown8d608662010-08-30 03:02:23 -07001784 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001785
1786 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001787 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001788
RoboErikfb290df2013-12-16 11:27:55 -08001789 // InputDeviceIdentifier
1790
1791 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1792 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1793 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1794 "<init>", "(Ljava/lang/String;II)V");
1795
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001796 // TouchCalibration
1797
1798 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1799 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1800
1801 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1802 "getAffineTransform", "()[F");
1803
Jeff Brown46b9ac02010-04-22 18:58:52 -07001804 return 0;
1805}
1806
Jeff Brown46b9ac02010-04-22 18:58:52 -07001807} /* namespace android */