blob: 41f86c9c60b87516a3e2a3741c8cbc23c6ace93b [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
Jeff Brown9c3cda02010-06-15 01:31:58 -070019//#define LOG_NDEBUG 0
20
21// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070022#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070023
24// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070026
Jeff Brown83c09682010-12-23 17:50:18 -080027
Jeff Brown46b9ac02010-04-22 18:58:52 -070028#include "JNIHelp.h"
29#include "jni.h"
Michael Wrighta4051212015-07-23 17:04:40 +010030#include <atomic>
31#include <cinttypes>
Jeff Brown349703e2010-06-22 01:27:15 -070032#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070033#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070034#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080035
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080037#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070038#include <utils/threads.h>
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070039#include <utils/SortedVector.h>
Jeff Brown83c09682010-12-23 17:50:18 -080040
Jeff Brownb4ff35d2011-01-02 16:37:43 -080041#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070042#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080043
Michael Wrightd6b473712014-02-10 15:56:36 -080044#include <inputflinger/InputManager.h>
45
Jeff Brown05dc66a2011-03-02 14:41:58 -080046#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070047#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080048#include <android_view_KeyEvent.h>
49#include <android_view_MotionEvent.h>
50#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070051#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080052#include <android/graphics/GraphicsJNI.h>
53
Jeff Brown6ec6f792012-04-17 16:52:41 -070054#include <ScopedLocalRef.h>
Jason Gerecke857aa7b2014-01-27 18:34:20 -080055#include <ScopedPrimitiveArray.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070056#include <ScopedUtfChars.h>
57
Jeff Brown4f8ecd82012-06-18 18:29:13 -070058#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070059#include "com_android_server_input_InputApplicationHandle.h"
60#include "com_android_server_input_InputWindowHandle.h"
Santos Cordonee8931e2017-04-05 10:31:15 -070061#include "android_hardware_display_DisplayViewport.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070062
Michael Wrighta4051212015-07-23 17:04:40 +010063#define INDENT " "
64
Jeff Brown46b9ac02010-04-22 18:58:52 -070065namespace android {
66
Jeff Brown1a84fd12011-06-02 01:26:32 -070067// The exponent used to calculate the pointer speed scaling factor.
68// The scaling factor is calculated as 2 ^ (speed * exponent),
69// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070070static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070071
Jeff Brown46b9ac02010-04-22 18:58:52 -070072static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070073 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070074 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070075 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070076 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070077 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070078 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070079 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070080 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070081 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070082 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070083 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080084 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070085 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080086 jmethodID getKeyRepeatTimeout;
87 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070088 jmethodID getHoverTapTimeout;
89 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070090 jmethodID getDoubleTapTimeout;
91 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080092 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080093 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070094 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070095 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -080096 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -070097} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070098
99static struct {
100 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700101} gInputDeviceClassInfo;
102
103static struct {
104 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700105} gKeyEventClassInfo;
106
107static struct {
108 jclass clazz;
109} gMotionEventClassInfo;
110
RoboErikfb290df2013-12-16 11:27:55 -0800111static struct {
112 jclass clazz;
113 jmethodID constructor;
114} gInputDeviceIdentifierInfo;
115
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800116static struct {
117 jclass clazz;
118 jmethodID getAffineTransform;
119} gTouchCalibrationClassInfo;
120
RoboErikfb290df2013-12-16 11:27:55 -0800121
Jeff Brown928e0542011-01-10 11:17:36 -0800122
123// --- Global functions ---
124
Jeff Brown214eaf42011-05-26 19:17:02 -0700125template<typename T>
126inline static T min(const T& a, const T& b) {
127 return a < b ? a : b;
128}
129
130template<typename T>
131inline static T max(const T& a, const T& b) {
132 return a > b ? a : b;
133}
134
Michael Wrighta4051212015-07-23 17:04:40 +0100135static inline const char* toString(bool value) {
136 return value ? "true" : "false";
137}
138
Jeff Brown928e0542011-01-10 11:17:36 -0800139static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
140 const sp<InputApplicationHandle>& inputApplicationHandle) {
141 if (inputApplicationHandle == NULL) {
142 return NULL;
143 }
144 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
145 getInputApplicationHandleObjLocalRef(env);
146}
147
148static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
149 const sp<InputWindowHandle>& inputWindowHandle) {
150 if (inputWindowHandle == NULL) {
151 return NULL;
152 }
153 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
154 getInputWindowHandleObjLocalRef(env);
155}
156
Jun Mukai808196f2015-10-28 16:46:44 -0700157static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
158 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700159 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700160 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700161 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400162 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
163 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
164 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
165 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
166 bitmapCopy->rowBytes(), 0, 0);
167 }
Jun Mukai808196f2015-10-28 16:46:44 -0700168 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
169 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700170 }
171}
172
Jun Mukai808196f2015-10-28 16:46:44 -0700173static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
174 SpriteIcon* outSpriteIcon) {
175 PointerIcon pointerIcon;
176 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
177}
178
Jeff Brown905805a2011-10-12 13:57:59 -0700179enum {
180 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700181};
182
Jeff Brown928e0542011-01-10 11:17:36 -0800183
184// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800185
Jeff Brown9c3cda02010-06-15 01:31:58 -0700186class NativeInputManager : public virtual RefBase,
187 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700188 public virtual InputDispatcherPolicyInterface,
189 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700190protected:
191 virtual ~NativeInputManager();
192
193public:
Jeff Brown4532e612012-04-05 14:27:12 -0700194 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700195
196 inline sp<InputManager> getInputManager() const { return mInputManager; }
197
Jeff Brownb88102f2010-09-08 11:49:43 -0700198 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700199
Santos Cordonee8931e2017-04-05 10:31:15 -0700200 void setVirtualDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
201 void setDisplayViewport(int32_t viewportType, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700202
Jeff Brown7fbdc842010-06-17 20:52:56 -0700203 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800204 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700205 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
206
Jeff Brown9302c872011-07-13 22:51:29 -0700207 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
208 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700209 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800210 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700211 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700212 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700213 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700214 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800215 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100216 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800217 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700218 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800219 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700220
Jeff Brown9c3cda02010-06-15 01:31:58 -0700221 /* --- InputReaderPolicyInterface implementation --- */
222
Jeff Brown214eaf42011-05-26 19:17:02 -0700223 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800224 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700225 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800226 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700227 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700228 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
229 jfloatArray matrixArr);
230 virtual TouchAffineTransformation getTouchAffineTransformation(
231 const String8& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700232
233 /* --- InputDispatcherPolicyInterface implementation --- */
234
Jeff Brownbcc046a2012-09-27 20:46:43 -0700235 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700236 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700238 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700239 const sp<InputWindowHandle>& inputWindowHandle,
240 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800241 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700242 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700243 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800244 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800245 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700246 virtual nsecs_t interceptKeyBeforeDispatching(
247 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700248 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800249 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800250 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700251 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700252 virtual bool checkInjectEventsPermissionNonReentrant(
253 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700254
Jeff Brown2352b972011-04-12 22:39:53 -0700255 /* --- PointerControllerPolicyInterface implementation --- */
256
Jun Mukai19a56012015-11-24 11:25:52 -0800257 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700258 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700259 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
260 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900261 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700262 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700263
Jeff Brown9c3cda02010-06-15 01:31:58 -0700264private:
265 sp<InputManager> mInputManager;
266
Jeff Brown2352b972011-04-12 22:39:53 -0700267 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700268 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800269 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270
Jeff Brown83c09682010-12-23 17:50:18 -0800271 Mutex mLock;
272 struct Locked {
273 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700274 DisplayViewport internalViewport;
275 DisplayViewport externalViewport;
Santos Cordonee8931e2017-04-05 10:31:15 -0700276 Vector<DisplayViewport> virtualViewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800277
Jeff Brown05dc66a2011-03-02 14:41:58 -0800278 // System UI visibility.
279 int32_t systemUiVisibility;
280
Jeff Brown1a84fd12011-06-02 01:26:32 -0700281 // Pointer speed.
282 int32_t pointerSpeed;
283
Jeff Brown474dcb52011-06-14 20:22:50 -0700284 // True if pointer gestures are enabled.
285 bool pointerGesturesEnabled;
286
Jeff Browndaf4a122011-08-26 17:14:14 -0700287 // Show touches feature enable/disable.
288 bool showTouches;
289
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800290 // Pointer capture feature enable/disable.
291 bool pointerCapture;
292
Jeff Brown5541de92011-04-11 11:54:25 -0700293 // Sprite controller singleton, created on first use.
294 sp<SpriteController> spriteController;
295
Jeff Brown83c09682010-12-23 17:50:18 -0800296 // Pointer controller singleton, created and destroyed as needed.
297 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700298
299 // Input devices to be disabled
300 SortedVector<int32_t> disabledInputDevices;
Jeff Brown83c09682010-12-23 17:50:18 -0800301 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700302
Michael Wrighta4051212015-07-23 17:04:40 +0100303 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700304
Jeff Brown2352b972011-04-12 22:39:53 -0700305 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800306 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700307 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800308
Jeff Brownb88102f2010-09-08 11:49:43 -0700309 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700310
Jeff Brown9c3cda02010-06-15 01:31:58 -0700311 static inline JNIEnv* jniEnv() {
312 return AndroidRuntime::getJNIEnv();
313 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700314};
315
Jeff Brown928e0542011-01-10 11:17:36 -0800316
Jeff Brown9c3cda02010-06-15 01:31:58 -0700317
Jeff Brown2352b972011-04-12 22:39:53 -0700318NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700319 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700320 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700321 JNIEnv* env = jniEnv();
322
Jeff Brown2352b972011-04-12 22:39:53 -0700323 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700324 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700325
Jeff Brown83c09682010-12-23 17:50:18 -0800326 {
327 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800328 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700329 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700330 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700331 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800332 mLocked.pointerCapture = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800333 }
Michael Wrighta4051212015-07-23 17:04:40 +0100334 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800335
Jeff Brown9c3cda02010-06-15 01:31:58 -0700336 sp<EventHub> eventHub = new EventHub();
337 mInputManager = new InputManager(eventHub, this, this);
338}
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
Jeff Brownb88102f2010-09-08 11:49:43 -0700347void NativeInputManager::dump(String8& dump) {
Michael Wrighta4051212015-07-23 17:04:40 +0100348 dump.append("Input Manager State:\n");
349 {
350 dump.appendFormat(INDENT "Interactive: %s\n", toString(mInteractive.load()));
351 }
352 {
353 AutoMutex _l(mLock);
354 dump.appendFormat(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
355 mLocked.systemUiVisibility);
356 dump.appendFormat(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
357 dump.appendFormat(INDENT "Pointer Gestures Enabled: %s\n",
358 toString(mLocked.pointerGesturesEnabled));
359 dump.appendFormat(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800360 dump.appendFormat(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100361 }
362 dump.append("\n");
363
Jeff Brownb88102f2010-09-08 11:49:43 -0700364 mInputManager->getReader()->dump(dump);
365 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700366
Jeff Brownb88102f2010-09-08 11:49:43 -0700367 mInputManager->getDispatcher()->dump(dump);
368 dump.append("\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
Santos Cordonee8931e2017-04-05 10:31:15 -0700381void NativeInputManager::setVirtualDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
382 Vector<DisplayViewport> viewports;
383
384 if (viewportObjArray) {
385 jsize length = env->GetArrayLength(viewportObjArray);
386 for (jsize i = 0; i < length; i++) {
387 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
388 if (! viewportObj) {
389 break; // found null element indicating end of used portion of the array
390 }
391
392 DisplayViewport viewport;
393 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
394 ALOGI("Viewport [%d] to add: %s", (int) length, viewport.uniqueId.c_str());
395 viewports.push(viewport);
396
397 env->DeleteLocalRef(viewportObj);
398 }
399 }
400
401 {
402 AutoMutex _l(mLock);
403 mLocked.virtualViewports = viewports;
404 }
405
406 mInputManager->getReader()->requestRefreshConfiguration(
407 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
408}
409
410void NativeInputManager::setDisplayViewport(int32_t type, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700411 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700412 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700413 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700414
Santos Cordonee8931e2017-04-05 10:31:15 -0700415 ViewportType viewportType = static_cast<ViewportType>(type);
416 DisplayViewport* v = NULL;
417 if (viewportType == ViewportType::VIEWPORT_EXTERNAL) {
418 v = &mLocked.externalViewport;
419 } else if (viewportType == ViewportType::VIEWPORT_INTERNAL) {
420 v = &mLocked.internalViewport;
421 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700422
Santos Cordonee8931e2017-04-05 10:31:15 -0700423 if (v != NULL && *v != viewport) {
424 changed = true;
425 *v = viewport;
426
427 if (viewportType == ViewportType::VIEWPORT_INTERNAL) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700428 sp<PointerController> controller = mLocked.pointerController.promote();
429 if (controller != NULL) {
430 controller->setDisplayViewport(
431 viewport.logicalRight - viewport.logicalLeft,
432 viewport.logicalBottom - viewport.logicalTop,
433 viewport.orientation);
434 }
Jeff Brown2352b972011-04-12 22:39:53 -0700435 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700436 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700437 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700438
439 if (changed) {
440 mInputManager->getReader()->requestRefreshConfiguration(
441 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
442 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700443}
444
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700445status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Jeff Brown928e0542011-01-10 11:17:36 -0800446 const sp<InputChannel>& inputChannel,
447 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
448 return mInputManager->getDispatcher()->registerInputChannel(
449 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700450}
451
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700452status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700453 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700454 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700455}
456
Jeff Brown214eaf42011-05-26 19:17:02 -0700457void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700458 JNIEnv* env = jniEnv();
459
Jeff Brown4532e612012-04-05 14:27:12 -0700460 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
461 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700462 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
463 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
464 }
465
466 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700467 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
468 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700469 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
470 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700471 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700472 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700473 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700474 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700475 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700476 env->DeleteLocalRef(item);
477 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700478 env->DeleteLocalRef(excludedDeviceNames);
479 }
480
Jeff Brown4532e612012-04-05 14:27:12 -0700481 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
482 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700483 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700484 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
485 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700486 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700487 jint longPressTimeout = env->CallIntMethod(mServiceObj,
488 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700489 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700490 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700491
492 // We must ensure that the tap-drag interval is significantly shorter than
493 // the long-press timeout because the tap is held down for the entire duration
494 // of the double-tap timeout.
495 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700496 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700497 outConfig->pointerGestureTapDragInterval =
498 milliseconds_to_nanoseconds(tapDragInterval);
499 }
500 }
501 }
502
Jeff Brown4532e612012-04-05 14:27:12 -0700503 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
504 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700505 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
506 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700507 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700508
509 { // acquire lock
510 AutoMutex _l(mLock);
511
512 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
513 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700514 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700515
Jeff Browndaf4a122011-08-26 17:14:14 -0700516 outConfig->showTouches = mLocked.showTouches;
517
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800518 outConfig->pointerCapture = mLocked.pointerCapture;
519
Santos Cordonee8931e2017-04-05 10:31:15 -0700520 outConfig->setPhysicalDisplayViewport(ViewportType::VIEWPORT_INTERNAL,
521 mLocked.internalViewport);
522 outConfig->setPhysicalDisplayViewport(ViewportType::VIEWPORT_EXTERNAL,
523 mLocked.externalViewport);
524 outConfig->setVirtualDisplayViewports(mLocked.virtualViewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700525
526 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700527 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700528}
529
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700530sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Jeff Brown83c09682010-12-23 17:50:18 -0800531 AutoMutex _l(mLock);
532
533 sp<PointerController> controller = mLocked.pointerController.promote();
534 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700535 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800536
Jeff Brown2352b972011-04-12 22:39:53 -0700537 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800538 mLocked.pointerController = controller;
539
Jeff Brownd728bf52012-09-08 18:05:28 -0700540 DisplayViewport& v = mLocked.internalViewport;
541 controller->setDisplayViewport(
542 v.logicalRight - v.logicalLeft,
543 v.logicalBottom - v.logicalTop,
544 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800545
Jeff Brown2352b972011-04-12 22:39:53 -0700546 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800547 }
548 return controller;
549}
550
Jeff Brown5541de92011-04-11 11:54:25 -0700551void NativeInputManager::ensureSpriteControllerLocked() {
552 if (mLocked.spriteController == NULL) {
553 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700554 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700555 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
556 layer = -1;
557 }
558 mLocked.spriteController = new SpriteController(mLooper, layer);
559 }
560}
561
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700562void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
563 JNIEnv* env = jniEnv();
564
565 size_t count = inputDevices.size();
566 jobjectArray inputDevicesObjArray = env->NewObjectArray(
567 count, gInputDeviceClassInfo.clazz, NULL);
568 if (inputDevicesObjArray) {
569 bool error = false;
570 for (size_t i = 0; i < count; i++) {
571 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
572 if (!inputDeviceObj) {
573 error = true;
574 break;
575 }
576
577 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
578 env->DeleteLocalRef(inputDeviceObj);
579 }
580
581 if (!error) {
582 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
583 inputDevicesObjArray);
584 }
585
586 env->DeleteLocalRef(inputDevicesObjArray);
587 }
588
589 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
590}
591
Jeff Brown6ec6f792012-04-17 16:52:41 -0700592sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800593 const InputDeviceIdentifier& identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700594 JNIEnv* env = jniEnv();
595
596 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800597 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
598 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
599 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
600 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700601 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800602 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700603 if (arrayObj.get()) {
604 ScopedLocalRef<jstring> filenameObj(env,
605 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
606 ScopedLocalRef<jstring> contentsObj(env,
607 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
608 ScopedUtfChars filenameChars(env, filenameObj.get());
609 ScopedUtfChars contentsChars(env, contentsObj.get());
610
611 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
612 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
613 }
614 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
615 return result;
616}
617
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700618String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
619 JNIEnv* env = jniEnv();
620
621 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
622 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
623 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
624 String8 result;
625 if (aliasObj.get()) {
626 ScopedUtfChars aliasChars(env, aliasObj.get());
627 result.setTo(aliasChars.c_str());
628 }
629 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
630 return result;
631}
632
Jeff Brownbcc046a2012-09-27 20:46:43 -0700633void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700634 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700635#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700636 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
637 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700638#endif
639
640 JNIEnv* env = jniEnv();
641
Jeff Brown53384282012-08-20 20:16:01 -0700642 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700643 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700644 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700645}
646
Jeff Brown9c3cda02010-06-15 01:31:58 -0700647void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
648#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000649 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700650#endif
651
652 JNIEnv* env = jniEnv();
653
Jeff Brown4532e612012-04-05 14:27:12 -0700654 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700655 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700656}
657
Jeff Brown519e0242010-09-15 15:18:56 -0700658nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700659 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700660#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000661 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700662#endif
663
664 JNIEnv* env = jniEnv();
665
Jeff Brown928e0542011-01-10 11:17:36 -0800666 jobject inputApplicationHandleObj =
667 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
668 jobject inputWindowHandleObj =
669 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700670 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700671
Jeff Brown4532e612012-04-05 14:27:12 -0700672 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700673 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
674 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700675 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
676 newTimeout = 0; // abort dispatch
677 } else {
678 assert(newTimeout >= 0);
679 }
680
Jeff Brownbd181bb2013-09-10 16:44:24 -0700681 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800682 env->DeleteLocalRef(inputWindowHandleObj);
683 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700684 return newTimeout;
685}
686
Jeff Brown928e0542011-01-10 11:17:36 -0800687void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700688#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000689 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700690#endif
691
Jeff Brown7fbdc842010-06-17 20:52:56 -0700692 JNIEnv* env = jniEnv();
693
Jeff Brown928e0542011-01-10 11:17:36 -0800694 jobject inputWindowHandleObj =
695 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
696 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700697 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800698 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700699 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
700
Jeff Brown928e0542011-01-10 11:17:36 -0800701 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700702 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700703}
704
Jeff Brown214eaf42011-05-26 19:17:02 -0700705void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
706 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800707
Jeff Brown4532e612012-04-05 14:27:12 -0700708 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
709 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700710 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
711 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
712 }
Jeff Browna4547672011-03-02 21:38:11 -0800713
Jeff Brown4532e612012-04-05 14:27:12 -0700714 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
715 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700716 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
717 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
718 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700719}
720
Jeff Brown9302c872011-07-13 22:51:29 -0700721void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
722 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700723
Jeff Brown9302c872011-07-13 22:51:29 -0700724 if (windowHandleObjArray) {
725 jsize length = env->GetArrayLength(windowHandleObjArray);
726 for (jsize i = 0; i < length; i++) {
727 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
728 if (! windowHandleObj) {
729 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700730 }
Jeff Brown9302c872011-07-13 22:51:29 -0700731
732 sp<InputWindowHandle> windowHandle =
733 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
734 if (windowHandle != NULL) {
735 windowHandles.push(windowHandle);
736 }
737 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700738 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700739 }
Jeff Brown349703e2010-06-22 01:27:15 -0700740
Jeff Brown9302c872011-07-13 22:51:29 -0700741 mInputManager->getDispatcher()->setInputWindows(windowHandles);
742
743 // Do this after the dispatcher has updated the window handle state.
744 bool newPointerGesturesEnabled = true;
745 size_t numWindows = windowHandles.size();
746 for (size_t i = 0; i < numWindows; i++) {
747 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700748 const InputWindowInfo* windowInfo = windowHandle->getInfo();
749 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
750 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700751 newPointerGesturesEnabled = false;
752 }
753 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700754
755 uint32_t changes = 0;
756 { // acquire lock
757 AutoMutex _l(mLock);
758
759 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
760 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
761 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
762 }
763 } // release lock
764
765 if (changes) {
766 mInputManager->getReader()->requestRefreshConfiguration(changes);
767 }
Jeff Brown349703e2010-06-22 01:27:15 -0700768}
769
Jeff Brown9302c872011-07-13 22:51:29 -0700770void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
771 sp<InputApplicationHandle> applicationHandle =
772 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
773 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700774}
775
776void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700777 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700778}
779
Jeff Brown05dc66a2011-03-02 14:41:58 -0800780void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
781 AutoMutex _l(mLock);
782
783 if (mLocked.systemUiVisibility != visibility) {
784 mLocked.systemUiVisibility = visibility;
785
786 sp<PointerController> controller = mLocked.pointerController.promote();
787 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700788 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800789 }
790 }
791}
792
Jeff Brown2352b972011-04-12 22:39:53 -0700793void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800794 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700795 controller->setInactivityTimeout(lightsOut
796 ? PointerController::INACTIVITY_TIMEOUT_SHORT
797 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800798}
799
Jeff Brown1a84fd12011-06-02 01:26:32 -0700800void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700801 { // acquire lock
802 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700803
Jeff Brown474dcb52011-06-14 20:22:50 -0700804 if (mLocked.pointerSpeed == speed) {
805 return;
806 }
807
Steve Block6215d3f2012-01-04 20:05:49 +0000808 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700809 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700810 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700811
Jeff Brown474dcb52011-06-14 20:22:50 -0700812 mInputManager->getReader()->requestRefreshConfiguration(
813 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700814}
815
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700816void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
817 { // acquire lock
818 AutoMutex _l(mLock);
819
820 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
821 bool currentlyEnabled = index < 0;
822 if (!enabled && currentlyEnabled) {
823 mLocked.disabledInputDevices.add(deviceId);
824 }
825 if (enabled && !currentlyEnabled) {
826 mLocked.disabledInputDevices.remove(deviceId);
827 }
828 } // release lock
829
830 mInputManager->getReader()->requestRefreshConfiguration(
831 InputReaderConfiguration::CHANGE_ENABLED_STATE);
832}
833
Jeff Browndaf4a122011-08-26 17:14:14 -0700834void NativeInputManager::setShowTouches(bool enabled) {
835 { // acquire lock
836 AutoMutex _l(mLock);
837
838 if (mLocked.showTouches == enabled) {
839 return;
840 }
841
Steve Block6215d3f2012-01-04 20:05:49 +0000842 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700843 mLocked.showTouches = enabled;
844 } // release lock
845
846 mInputManager->getReader()->requestRefreshConfiguration(
847 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
848}
849
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800850void NativeInputManager::setPointerCapture(bool enabled) {
851 { // acquire lock
852 AutoMutex _l(mLock);
853
854 if (mLocked.pointerCapture == enabled) {
855 return;
856 }
857
858 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
859 mLocked.pointerCapture = enabled;
860 } // release lock
861
862 mInputManager->getReader()->requestRefreshConfiguration(
863 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
864}
865
Jeff Brown037c33e2014-04-09 00:31:55 -0700866void NativeInputManager::setInteractive(bool interactive) {
867 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700868}
869
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800870void NativeInputManager::reloadCalibration() {
871 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100872 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800873}
874
Michael Wrighte051f6f2016-05-13 17:44:16 +0100875void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800876 AutoMutex _l(mLock);
877 sp<PointerController> controller = mLocked.pointerController.promote();
878 if (controller != NULL) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100879 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800880 }
881}
882
883void NativeInputManager::reloadPointerIcons() {
884 AutoMutex _l(mLock);
885 sp<PointerController> controller = mLocked.pointerController.promote();
886 if (controller != NULL) {
887 controller->reloadPointerResources();
888 }
Jun Mukai1db53972015-09-11 18:08:31 -0700889}
890
Jun Mukaid4eaef72015-10-30 15:54:33 -0700891void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
892 AutoMutex _l(mLock);
893 sp<PointerController> controller = mLocked.pointerController.promote();
894 if (controller != NULL) {
895 controller->setCustomPointerIcon(icon);
896 }
897}
898
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800899TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
900 JNIEnv *env, jfloatArray matrixArr) {
901 ScopedFloatArrayRO matrix(env, matrixArr);
902 assert(matrix.size() == 6);
903
904 TouchAffineTransformation transform;
905 transform.x_scale = matrix[0];
906 transform.x_ymix = matrix[1];
907 transform.x_offset = matrix[2];
908 transform.y_xmix = matrix[3];
909 transform.y_scale = matrix[4];
910 transform.y_offset = matrix[5];
911
912 return transform;
913}
914
915TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700916 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800917 JNIEnv* env = jniEnv();
918
919 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
920
921 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700922 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
923 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800924
925 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
926 gTouchCalibrationClassInfo.getAffineTransform));
927
928 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
929
930 env->DeleteLocalRef(matrixArr);
931 env->DeleteLocalRef(cal);
932
933 return transform;
934}
935
Jeff Brown0029c662011-03-30 02:25:18 -0700936bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
937 jobject inputEventObj;
938
939 JNIEnv* env = jniEnv();
940 switch (inputEvent->getType()) {
941 case AINPUT_EVENT_TYPE_KEY:
942 inputEventObj = android_view_KeyEvent_fromNative(env,
943 static_cast<const KeyEvent*>(inputEvent));
944 break;
945 case AINPUT_EVENT_TYPE_MOTION:
946 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
947 static_cast<const MotionEvent*>(inputEvent));
948 break;
949 default:
950 return true; // dispatch the event normally
951 }
952
953 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000954 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700955 return true; // dispatch the event normally
956 }
957
958 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700959 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700960 inputEventObj, policyFlags);
961 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
962 pass = true;
963 }
964 env->DeleteLocalRef(inputEventObj);
965 return pass;
966}
967
Jeff Brown1f245102010-11-18 20:53:46 -0800968void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
969 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700970 // Policy:
971 // - Ignore untrusted events and pass them along.
972 // - Ask the window manager what to do with normal events and trusted injected events.
973 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100974 bool interactive = mInteractive.load();
975 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700976 policyFlags |= POLICY_FLAG_INTERACTIVE;
977 }
Jeff Brown3122e442010-10-11 23:32:49 -0700978 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800979 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700980 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800981 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
982 jint wmActions;
983 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700984 wmActions = env->CallIntMethod(mServiceObj,
985 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700986 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800987 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
988 wmActions = 0;
989 }
990 android_view_KeyEvent_recycle(env, keyEventObj);
991 env->DeleteLocalRef(keyEventObj);
992 } else {
Steve Block3762c312012-01-06 19:20:56 +0000993 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700994 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700995 }
996
Jeff Brown56194eb2011-03-02 19:23:13 -0800997 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700998 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100999 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001000 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1001 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001002 }
1003}
1004
Jeff Brown56194eb2011-03-02 19:23:13 -08001005void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -07001006 // Policy:
1007 // - Ignore untrusted events and pass them along.
1008 // - No special filtering for injected events required at this time.
1009 // - Filter normal events based on screen state.
1010 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001011 bool interactive = mInteractive.load();
1012 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001013 policyFlags |= POLICY_FLAG_INTERACTIVE;
1014 }
Jeff Brown3122e442010-10-11 23:32:49 -07001015 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001016 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001017 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001018 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001019 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001020 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001021 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001022 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001023 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001024 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001025 wmActions = 0;
1026 }
1027
Jeff Brown56194eb2011-03-02 19:23:13 -08001028 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001029 }
Jeff Brown3122e442010-10-11 23:32:49 -07001030 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001031 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001032 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1033 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001034 }
1035}
1036
Jeff Brown56194eb2011-03-02 19:23:13 -08001037void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1038 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001039 if (wmActions & WM_ACTION_PASS_TO_USER) {
1040 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1041 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001042#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001043 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001044#endif
1045 }
1046}
1047
Jeff Brown905805a2011-10-12 13:57:59 -07001048nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -08001049 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -07001050 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -07001051 // Policy:
1052 // - Ignore untrusted events and pass them along.
1053 // - Filter normal events and trusted injected events through the window manager policy to
1054 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001055 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001056 if (policyFlags & POLICY_FLAG_TRUSTED) {
1057 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001058
Jeff Brown928e0542011-01-10 11:17:36 -08001059 // Note: inputWindowHandle may be null.
1060 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001061 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1062 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001063 jlong delayMillis = env->CallLongMethod(mServiceObj,
1064 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -08001065 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001066 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1067 android_view_KeyEvent_recycle(env, keyEventObj);
1068 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001069 if (!error) {
1070 if (delayMillis < 0) {
1071 result = -1;
1072 } else if (delayMillis > 0) {
1073 result = milliseconds_to_nanoseconds(delayMillis);
1074 }
1075 }
Jeff Brown1f245102010-11-18 20:53:46 -08001076 } else {
Steve Block3762c312012-01-06 19:20:56 +00001077 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001078 }
Jeff Brown928e0542011-01-10 11:17:36 -08001079 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -07001080 }
Jeff Brown1f245102010-11-18 20:53:46 -08001081 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001082}
1083
Jeff Brown928e0542011-01-10 11:17:36 -08001084bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001085 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -07001086 // Policy:
1087 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001088 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001089 if (policyFlags & POLICY_FLAG_TRUSTED) {
1090 JNIEnv* env = jniEnv();
1091
Jeff Brown928e0542011-01-10 11:17:36 -08001092 // Note: inputWindowHandle may be null.
1093 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001094 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1095 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001096 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1097 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -08001098 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001099 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
1100 fallbackKeyEventObj = NULL;
1101 }
Jeff Brown1f245102010-11-18 20:53:46 -08001102 android_view_KeyEvent_recycle(env, keyEventObj);
1103 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001104
1105 if (fallbackKeyEventObj) {
1106 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1107 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1108 outFallbackKeyEvent)) {
1109 result = true;
1110 }
1111 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1112 env->DeleteLocalRef(fallbackKeyEventObj);
1113 }
Jeff Brown1f245102010-11-18 20:53:46 -08001114 } else {
Steve Block3762c312012-01-06 19:20:56 +00001115 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001116 }
Jeff Brown928e0542011-01-10 11:17:36 -08001117 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001118 }
Jeff Brown1f245102010-11-18 20:53:46 -08001119 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001120}
1121
Jeff Brown01ce2e92010-09-26 22:20:12 -07001122void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
1123 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001124}
1125
Jeff Brown349703e2010-06-22 01:27:15 -07001126
Jeff Brownb88102f2010-09-08 11:49:43 -07001127bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1128 int32_t injectorPid, int32_t injectorUid) {
1129 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001130 jboolean result = env->CallBooleanMethod(mServiceObj,
1131 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001132 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1133 result = false;
1134 }
Jeff Brown349703e2010-06-22 01:27:15 -07001135 return result;
1136}
1137
Jun Mukai19a56012015-11-24 11:25:52 -08001138void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
1139 JNIEnv* env = jniEnv();
1140
1141 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1142 mServiceObj, gServiceClassInfo.getPointerIcon));
1143 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1144 return;
1145 }
1146
1147 PointerIcon pointerIcon;
1148 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1149 mContextObj, &pointerIcon);
1150 if (!status && !pointerIcon.isNullIcon()) {
1151 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1152 } else {
1153 *icon = SpriteIcon();
1154 }
1155}
1156
Jeff Brown2352b972011-04-12 22:39:53 -07001157void NativeInputManager::loadPointerResources(PointerResources* outResources) {
1158 JNIEnv* env = jniEnv();
1159
1160 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1161 &outResources->spotHover);
1162 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1163 &outResources->spotTouch);
1164 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1165 &outResources->spotAnchor);
1166}
1167
Jun Mukai808196f2015-10-28 16:46:44 -07001168void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1169 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Jun Mukai1db53972015-09-11 18:08:31 -07001170 JNIEnv* env = jniEnv();
1171
1172 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1173 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001174 PointerIcon pointerIcon;
1175 loadSystemIconAsSpriteWithPointerIcon(
1176 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1177 if (!pointerIcon.bitmapFrames.empty()) {
1178 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1179 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1180 animationData.durationPerFrame =
1181 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1182 animationData.animationFrames.reserve(numFrames);
1183 animationData.animationFrames.push_back(SpriteIcon(
1184 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1185 for (size_t i = 0; i < numFrames - 1; ++i) {
1186 animationData.animationFrames.push_back(SpriteIcon(
1187 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1188 }
1189 }
Jun Mukai1db53972015-09-11 18:08:31 -07001190 }
Jun Mukai808196f2015-10-28 16:46:44 -07001191 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1192 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001193}
1194
Jun Mukai5ec74202015-10-07 16:58:09 +09001195int32_t NativeInputManager::getDefaultPointerIconId() {
1196 return POINTER_ICON_STYLE_ARROW;
1197}
Jeff Brown83c09682010-12-23 17:50:18 -08001198
Jun Mukaid4eaef72015-10-30 15:54:33 -07001199int32_t NativeInputManager::getCustomPointerIconId() {
1200 return POINTER_ICON_STYLE_CUSTOM;
1201}
1202
Jeff Brown9c3cda02010-06-15 01:31:58 -07001203// ----------------------------------------------------------------------------
1204
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001205static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001206 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001207 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001208 if (messageQueue == NULL) {
1209 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1210 return 0;
1211 }
1212
Jeff Brown603b4452012-04-06 17:39:41 -07001213 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1214 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001215 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001216 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001217}
1218
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001219static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001220 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001221
Jeff Brown4532e612012-04-05 14:27:12 -07001222 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001223 if (result) {
1224 jniThrowRuntimeException(env, "Input manager could not be started.");
1225 }
1226}
1227
Santos Cordonee8931e2017-04-05 10:31:15 -07001228static void nativeSetVirtualDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
1229 jobjectArray viewportObjArray) {
1230 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1231 im->setVirtualDisplayViewports(env, viewportObjArray);
1232}
1233
1234static void nativeSetDisplayViewport(JNIEnv* env, jclass /* clazz */, jlong ptr,
1235 jint viewportType, jint displayId, jint orientation,
Jeff Brownd728bf52012-09-08 18:05:28 -07001236 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001237 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
Santos Cordonee8931e2017-04-05 10:31:15 -07001238 jint deviceWidth, jint deviceHeight, jstring uniqueId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001239 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001240
Jeff Brownd728bf52012-09-08 18:05:28 -07001241 DisplayViewport v;
1242 v.displayId = displayId;
1243 v.orientation = orientation;
1244 v.logicalLeft = logicalLeft;
1245 v.logicalTop = logicalTop;
1246 v.logicalRight = logicalRight;
1247 v.logicalBottom = logicalBottom;
1248 v.physicalLeft = physicalLeft;
1249 v.physicalTop = physicalTop;
1250 v.physicalRight = physicalRight;
1251 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001252 v.deviceWidth = deviceWidth;
1253 v.deviceHeight = deviceHeight;
Santos Cordonee8931e2017-04-05 10:31:15 -07001254 if (uniqueId != nullptr) {
1255 v.uniqueId.setTo(ScopedUtfChars(env, uniqueId).c_str());
1256 }
1257
1258 im->setDisplayViewport(viewportType, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001259}
1260
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001261static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001262 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001263 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001264
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001265 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001266 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001267}
1268
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001269static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001270 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001271 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001272
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001273 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001274 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001275}
1276
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001277static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001278 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001279 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001280
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001281 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001282 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001283}
1284
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001285static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001286 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001287 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001288
1289 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1290 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1291 jsize numCodes = env->GetArrayLength(keyCodes);
1292 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001293 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001294 if (im->getInputManager()->getReader()->hasKeys(
1295 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1296 result = JNI_TRUE;
1297 } else {
1298 result = JNI_FALSE;
1299 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001300 } else {
1301 result = JNI_FALSE;
1302 }
1303
1304 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1305 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1306 return result;
1307}
1308
1309static void throwInputChannelNotInitialized(JNIEnv* env) {
1310 jniThrowException(env, "java/lang/IllegalStateException",
1311 "inputChannel is not initialized");
1312}
1313
Jeff Brown4532e612012-04-05 14:27:12 -07001314static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001315 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001316 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1317
Steve Block8564c8d2012-01-05 23:22:43 +00001318 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001319 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001320 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001321}
1322
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001323static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001324 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001325 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001326
1327 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1328 inputChannelObj);
1329 if (inputChannel == NULL) {
1330 throwInputChannelNotInitialized(env);
1331 return;
1332 }
1333
Jeff Brown928e0542011-01-10 11:17:36 -08001334 sp<InputWindowHandle> inputWindowHandle =
1335 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001336
Jeff Brown4532e612012-04-05 14:27:12 -07001337 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001338 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001339 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001340 String8 message;
1341 message.appendFormat("Failed to register input channel. status=%d", status);
1342 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001343 return;
1344 }
1345
Jeff Browna41ca772010-08-11 14:46:32 -07001346 if (! monitor) {
1347 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001348 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001349 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001350}
1351
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001352static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001353 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001354 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001355
1356 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1357 inputChannelObj);
1358 if (inputChannel == NULL) {
1359 throwInputChannelNotInitialized(env);
1360 return;
1361 }
1362
1363 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1364
Jeff Brown4532e612012-04-05 14:27:12 -07001365 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001366 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1367 String8 message;
1368 message.appendFormat("Failed to unregister input channel. status=%d", status);
1369 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001370 }
1371}
1372
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001373static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001374 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001375 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001376
Jeff Brown4532e612012-04-05 14:27:12 -07001377 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001378}
1379
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001380static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Jeff Brownca9bc702014-02-11 14:32:56 -08001381 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001382 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001383 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001384
Jeff Brown6ec402b2010-07-28 15:48:59 -07001385 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1386 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001387 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1388 if (status) {
1389 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1390 return INPUT_EVENT_INJECTION_FAILED;
1391 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001392
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001393 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001394 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001395 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001396 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001397 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1398 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001399 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1400 return INPUT_EVENT_INJECTION_FAILED;
1401 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001402
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001403 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001404 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001405 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001406 } else {
1407 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001408 return INPUT_EVENT_INJECTION_FAILED;
1409 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001410}
1411
Andrii Kulian112d0562016-03-08 10:44:22 -08001412static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1413 jlong ptr, jint deviceId) {
1414 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001415
Andrii Kulian112d0562016-03-08 10:44:22 -08001416 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1417}
1418
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001419static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001420 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001421 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001422
Jeff Brown4532e612012-04-05 14:27:12 -07001423 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001424}
1425
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001426static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001427 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001428 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001429
Jeff Brown4532e612012-04-05 14:27:12 -07001430 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001431}
1432
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001433static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1434 jboolean enabled) {
1435 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001436
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001437 im->setPointerCapture(enabled);
1438}
1439
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001440static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1441 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001442 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001443
Jeff Brown4532e612012-04-05 14:27:12 -07001444 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001445}
1446
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001447static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1448 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001449 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001450
Jeff Brown4532e612012-04-05 14:27:12 -07001451 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001452}
1453
Jeff Brown4532e612012-04-05 14:27:12 -07001454static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001455 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001456 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001457
1458 sp<InputChannel> fromChannel =
1459 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1460 sp<InputChannel> toChannel =
1461 android_view_InputChannel_getInputChannel(env, toChannelObj);
1462
1463 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001464 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001465 }
1466
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001467 if (im->getInputManager()->getDispatcher()->
1468 transferTouchFocus(fromChannel, toChannel)) {
1469 return JNI_TRUE;
1470 } else {
1471 return JNI_FALSE;
1472 }
Jeff Browne6504122010-09-27 14:52:15 -07001473}
1474
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001475static void nativeSetPointerSpeed(JNIEnv* /* env */,
1476 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001477 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001478
Jeff Brown4532e612012-04-05 14:27:12 -07001479 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001480}
1481
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001482static void nativeSetShowTouches(JNIEnv* /* env */,
1483 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001484 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001485
Jeff Brown4532e612012-04-05 14:27:12 -07001486 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001487}
1488
Jeff Brown037c33e2014-04-09 00:31:55 -07001489static void nativeSetInteractive(JNIEnv* env,
1490 jclass clazz, jlong ptr, jboolean interactive) {
1491 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1492
1493 im->setInteractive(interactive);
1494}
1495
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001496static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1497 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001498
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001499 im->reloadCalibration();
1500}
1501
Jeff Browna47425a2012-04-13 04:09:27 -07001502static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001503 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001504 jint repeat, jint token) {
1505 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1506
1507 size_t patternSize = env->GetArrayLength(patternObj);
1508 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001509 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001510 "which is more than the maximum supported size of %d.",
1511 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1512 return; // limit to reasonable size
1513 }
1514
1515 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1516 patternObj, NULL));
1517 nsecs_t pattern[patternSize];
1518 for (size_t i = 0; i < patternSize; i++) {
1519 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001520 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001521 }
1522 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1523
1524 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1525}
1526
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001527static void nativeCancelVibrate(JNIEnv* /* env */,
1528 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001529 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1530
1531 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1532}
1533
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001534static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1535 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001536 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1537
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001538 im->getInputManager()->getReader()->requestRefreshConfiguration(
1539 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1540}
1541
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001542static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1543 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001544 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1545
1546 im->getInputManager()->getReader()->requestRefreshConfiguration(
1547 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001548}
1549
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001550static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001551 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001552
Jeff Brownb88102f2010-09-08 11:49:43 -07001553 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001554 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001555 return env->NewStringUTF(dump.string());
1556}
1557
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001558static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001559 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001560
Jeff Brown4532e612012-04-05 14:27:12 -07001561 im->getInputManager()->getReader()->monitor();
1562 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001563}
1564
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001565static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1566 jclass /* clazz */, jlong ptr, jint deviceId) {
1567 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1568
1569 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1570}
1571
1572static void nativeEnableInputDevice(JNIEnv* /* env */,
1573 jclass /* clazz */, jlong ptr, jint deviceId) {
1574 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1575
1576 im->setInputDeviceEnabled(deviceId, true);
1577}
1578
1579static void nativeDisableInputDevice(JNIEnv* /* env */,
1580 jclass /* clazz */, jlong ptr, jint deviceId) {
1581 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1582
1583 im->setInputDeviceEnabled(deviceId, false);
1584}
1585
Michael Wrighte051f6f2016-05-13 17:44:16 +01001586static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001587 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001588
Michael Wrighte051f6f2016-05-13 17:44:16 +01001589 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001590}
1591
Jun Mukai19a56012015-11-24 11:25:52 -08001592static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1593 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001594
Jun Mukai19a56012015-11-24 11:25:52 -08001595 im->reloadPointerIcons();
1596}
1597
Jun Mukaid4eaef72015-10-30 15:54:33 -07001598static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1599 jlong ptr, jobject iconObj) {
1600 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1601
1602 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001603 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1604 if (result) {
1605 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1606 return;
1607 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001608
1609 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001610 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1611 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1612 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1613 spriteIcon.bitmap.rowBytes(), 0, 0);
1614 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001615 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1616 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1617 im->setCustomPointerIcon(spriteIcon);
1618}
1619
Jeff Brown9c3cda02010-06-15 01:31:58 -07001620// ----------------------------------------------------------------------------
1621
Daniel Micay76f6a862015-09-19 17:31:01 -04001622static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001623 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001624 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001625 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001626 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001627 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001628 (void*) nativeStart },
Santos Cordonee8931e2017-04-05 10:31:15 -07001629 { "nativeSetVirtualDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1630 (void*) nativeSetVirtualDisplayViewports },
1631 { "nativeSetDisplayViewport", "(JIIIIIIIIIIIIILjava/lang/String;)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001632 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001633 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001634 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001635 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001636 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001637 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001638 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001639 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001640 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001641 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001642 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001643 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001644 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001645 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001646 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001647 (void*) nativeSetInputFilterEnabled },
Jeff Brownca9bc702014-02-11 14:32:56 -08001648 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001649 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001650 { "nativeToggleCapsLock", "(JI)V",
1651 (void*) nativeToggleCapsLock },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001652 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001653 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001654 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001655 (void*) nativeSetFocusedApplication },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001656 { "nativeSetPointerCapture", "(JZ)V",
1657 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001658 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001659 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001660 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001661 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001662 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001663 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001664 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001665 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001666 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001667 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001668 { "nativeSetInteractive", "(JZ)V",
1669 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001670 { "nativeReloadCalibration", "(J)V",
1671 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001672 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001673 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001674 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001675 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001676 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001677 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001678 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001679 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001680 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001681 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001682 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001683 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001684 { "nativeIsInputDeviceEnabled", "(JI)Z",
1685 (void*) nativeIsInputDeviceEnabled },
1686 { "nativeEnableInputDevice", "(JI)V",
1687 (void*) nativeEnableInputDevice },
1688 { "nativeDisableInputDevice", "(JI)V",
1689 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001690 { "nativeSetPointerIconType", "(JI)V",
1691 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001692 { "nativeReloadPointerIcons", "(J)V",
1693 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001694 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1695 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001696};
1697
1698#define FIND_CLASS(var, className) \
1699 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001700 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001701
1702#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1703 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001704 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001705
1706#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1707 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001708 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001709
1710int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001711 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001712 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001713 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001714 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1715
Jeff Brown9c3cda02010-06-15 01:31:58 -07001716 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001717
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001718 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001719 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001720
Jeff Brown4532e612012-04-05 14:27:12 -07001721 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001722 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001723
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001724 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1725 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1726
Jeff Brown53384282012-08-20 20:16:01 -07001727 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1728 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001729
Jeff Brown4532e612012-04-05 14:27:12 -07001730 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1731 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001732
Jeff Brown4532e612012-04-05 14:27:12 -07001733 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001734 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001735 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001736
Jeff Brown4532e612012-04-05 14:27:12 -07001737 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001738 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1739
Jeff Brown4532e612012-04-05 14:27:12 -07001740 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001741 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001742
Michael Wright70af00a2014-09-03 19:30:20 -07001743 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1744 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001745
Jeff Brown4532e612012-04-05 14:27:12 -07001746 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001747 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001748 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001749
Jeff Brown4532e612012-04-05 14:27:12 -07001750 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001751 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001752 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001753
Jeff Brown4532e612012-04-05 14:27:12 -07001754 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001755 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001756
Jeff Brown4532e612012-04-05 14:27:12 -07001757 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001758 "getVirtualKeyQuietTimeMillis", "()I");
1759
Jeff Brown4532e612012-04-05 14:27:12 -07001760 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001761 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1762
Jeff Brown4532e612012-04-05 14:27:12 -07001763 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001764 "getKeyRepeatTimeout", "()I");
1765
Jeff Brown4532e612012-04-05 14:27:12 -07001766 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001767 "getKeyRepeatDelay", "()I");
1768
Jeff Brown4532e612012-04-05 14:27:12 -07001769 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001770 "getHoverTapTimeout", "()I");
1771
Jeff Brown4532e612012-04-05 14:27:12 -07001772 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001773 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001774
Jeff Brown4532e612012-04-05 14:27:12 -07001775 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001776 "getDoubleTapTimeout", "()I");
1777
Jeff Brown4532e612012-04-05 14:27:12 -07001778 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001779 "getLongPressTimeout", "()I");
1780
Jeff Brown4532e612012-04-05 14:27:12 -07001781 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001782 "getPointerLayer", "()I");
1783
Jeff Brown4532e612012-04-05 14:27:12 -07001784 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001785 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001786
Jeff Brown6ec6f792012-04-17 16:52:41 -07001787 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001788 "getKeyboardLayoutOverlay",
1789 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001790
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001791 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1792 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1793
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001794 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1795 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001796 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001797
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001798 // InputDevice
1799
1800 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1801 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1802
Jeff Brown6ec402b2010-07-28 15:48:59 -07001803 // KeyEvent
1804
1805 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001806 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1807
Jeff Brown8d608662010-08-30 03:02:23 -07001808 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001809
1810 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001811 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001812
RoboErikfb290df2013-12-16 11:27:55 -08001813 // InputDeviceIdentifier
1814
1815 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1816 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1817 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1818 "<init>", "(Ljava/lang/String;II)V");
1819
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001820 // TouchCalibration
1821
1822 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1823 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1824
1825 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1826 "getAffineTransform", "()[F");
1827
Jeff Brown46b9ac02010-04-22 18:58:52 -07001828 return 0;
1829}
1830
Jeff Brown46b9ac02010-04-22 18:58:52 -07001831} /* namespace android */