blob: 59a174e7205d564cc5228e62794cbbf14fdffcbd [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputManager-JNI"
18
Michael Wrightbafea6e2017-06-12 15:25:47 +010019#define ATRACE_TAG ATRACE_TAG_INPUT
20
Jeff Brown9c3cda02010-06-15 01:31:58 -070021//#define LOG_NDEBUG 0
22
23// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070024#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070025
26// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070027#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070028
Jeff Brown83c09682010-12-23 17:50:18 -080029
Jeff Brown46b9ac02010-04-22 18:58:52 -070030#include "JNIHelp.h"
31#include "jni.h"
Michael Wrighta4051212015-07-23 17:04:40 +010032#include <atomic>
33#include <cinttypes>
Jeff Brown349703e2010-06-22 01:27:15 -070034#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070035#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070036#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080037
Jeff Brown46b9ac02010-04-22 18:58:52 -070038#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080039#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070040#include <utils/threads.h>
Michael Wrightbafea6e2017-06-12 15:25:47 +010041#include <utils/Trace.h>
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070042#include <utils/SortedVector.h>
Jeff Brown83c09682010-12-23 17:50:18 -080043
Jeff Brownb4ff35d2011-01-02 16:37:43 -080044#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070045#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080046
Michael Wrightd6b473712014-02-10 15:56:36 -080047#include <inputflinger/InputManager.h>
48
Jeff Brown05dc66a2011-03-02 14:41:58 -080049#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070050#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080051#include <android_view_KeyEvent.h>
52#include <android_view_MotionEvent.h>
53#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070054#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080055#include <android/graphics/GraphicsJNI.h>
56
Jeff Brown6ec6f792012-04-17 16:52:41 -070057#include <ScopedLocalRef.h>
Jason Gerecke857aa7b2014-01-27 18:34:20 -080058#include <ScopedPrimitiveArray.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070059#include <ScopedUtfChars.h>
60
Jeff Brown4f8ecd82012-06-18 18:29:13 -070061#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070062#include "com_android_server_input_InputApplicationHandle.h"
63#include "com_android_server_input_InputWindowHandle.h"
Santos Cordonee8931e2017-04-05 10:31:15 -070064#include "android_hardware_display_DisplayViewport.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070065
Michael Wrighta4051212015-07-23 17:04:40 +010066#define INDENT " "
67
Jeff Brown46b9ac02010-04-22 18:58:52 -070068namespace android {
69
Jeff Brown1a84fd12011-06-02 01:26:32 -070070// The exponent used to calculate the pointer speed scaling factor.
71// The scaling factor is calculated as 2 ^ (speed * exponent),
72// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070073static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070074
Jeff Brown46b9ac02010-04-22 18:58:52 -070075static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070076 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070077 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070078 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070079 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070080 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070081 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070082 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070083 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070084 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070085 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070086 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080087 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070088 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080089 jmethodID getKeyRepeatTimeout;
90 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070091 jmethodID getHoverTapTimeout;
92 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070093 jmethodID getDoubleTapTimeout;
94 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080095 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080096 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070097 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070098 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -080099 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -0700100} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700101
102static struct {
103 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700104} gInputDeviceClassInfo;
105
106static struct {
107 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700108} gKeyEventClassInfo;
109
110static struct {
111 jclass clazz;
112} gMotionEventClassInfo;
113
RoboErikfb290df2013-12-16 11:27:55 -0800114static struct {
115 jclass clazz;
116 jmethodID constructor;
117} gInputDeviceIdentifierInfo;
118
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800119static struct {
120 jclass clazz;
121 jmethodID getAffineTransform;
122} gTouchCalibrationClassInfo;
123
RoboErikfb290df2013-12-16 11:27:55 -0800124
Jeff Brown928e0542011-01-10 11:17:36 -0800125
126// --- Global functions ---
127
Jeff Brown214eaf42011-05-26 19:17:02 -0700128template<typename T>
129inline static T min(const T& a, const T& b) {
130 return a < b ? a : b;
131}
132
133template<typename T>
134inline static T max(const T& a, const T& b) {
135 return a > b ? a : b;
136}
137
Michael Wrighta4051212015-07-23 17:04:40 +0100138static inline const char* toString(bool value) {
139 return value ? "true" : "false";
140}
141
Jeff Brown928e0542011-01-10 11:17:36 -0800142static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
143 const sp<InputApplicationHandle>& inputApplicationHandle) {
144 if (inputApplicationHandle == NULL) {
145 return NULL;
146 }
147 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
148 getInputApplicationHandleObjLocalRef(env);
149}
150
151static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
152 const sp<InputWindowHandle>& inputWindowHandle) {
153 if (inputWindowHandle == NULL) {
154 return NULL;
155 }
156 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
157 getInputWindowHandleObjLocalRef(env);
158}
159
Jun Mukai808196f2015-10-28 16:46:44 -0700160static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
161 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700162 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700163 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700164 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400165 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
166 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
167 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
168 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
169 bitmapCopy->rowBytes(), 0, 0);
170 }
Jun Mukai808196f2015-10-28 16:46:44 -0700171 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
172 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700173 }
174}
175
Jun Mukai808196f2015-10-28 16:46:44 -0700176static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
177 SpriteIcon* outSpriteIcon) {
178 PointerIcon pointerIcon;
179 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
180}
181
Jeff Brown905805a2011-10-12 13:57:59 -0700182enum {
183 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700184};
185
Jeff Brown928e0542011-01-10 11:17:36 -0800186
187// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800188
Jeff Brown9c3cda02010-06-15 01:31:58 -0700189class NativeInputManager : public virtual RefBase,
190 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700191 public virtual InputDispatcherPolicyInterface,
192 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700193protected:
194 virtual ~NativeInputManager();
195
196public:
Jeff Brown4532e612012-04-05 14:27:12 -0700197 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700198
199 inline sp<InputManager> getInputManager() const { return mInputManager; }
200
Jeff Brownb88102f2010-09-08 11:49:43 -0700201 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700202
Santos Cordonee8931e2017-04-05 10:31:15 -0700203 void setVirtualDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
204 void setDisplayViewport(int32_t viewportType, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700205
Jeff Brown7fbdc842010-06-17 20:52:56 -0700206 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800207 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700208 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
209
Jeff Brown9302c872011-07-13 22:51:29 -0700210 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
211 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700212 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800213 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700214 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700215 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700216 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700217 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800218 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100219 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800220 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700221 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800222 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700223
Jeff Brown9c3cda02010-06-15 01:31:58 -0700224 /* --- InputReaderPolicyInterface implementation --- */
225
Jeff Brown214eaf42011-05-26 19:17:02 -0700226 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800227 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700228 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800229 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700230 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700231 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
232 jfloatArray matrixArr);
233 virtual TouchAffineTransformation getTouchAffineTransformation(
234 const String8& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700235
236 /* --- InputDispatcherPolicyInterface implementation --- */
237
Jeff Brownbcc046a2012-09-27 20:46:43 -0700238 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700239 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700240 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700241 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700242 const sp<InputWindowHandle>& inputWindowHandle,
243 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800244 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700245 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700246 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800247 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800248 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700249 virtual nsecs_t interceptKeyBeforeDispatching(
250 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700251 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800252 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800253 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700254 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700255 virtual bool checkInjectEventsPermissionNonReentrant(
256 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700257
Jeff Brown2352b972011-04-12 22:39:53 -0700258 /* --- PointerControllerPolicyInterface implementation --- */
259
Jun Mukai19a56012015-11-24 11:25:52 -0800260 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700261 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700262 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
263 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900264 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700265 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700266
Jeff Brown9c3cda02010-06-15 01:31:58 -0700267private:
268 sp<InputManager> mInputManager;
269
Jeff Brown2352b972011-04-12 22:39:53 -0700270 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700271 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800272 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700273
Jeff Brown83c09682010-12-23 17:50:18 -0800274 Mutex mLock;
275 struct Locked {
276 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700277 DisplayViewport internalViewport;
278 DisplayViewport externalViewport;
Santos Cordonee8931e2017-04-05 10:31:15 -0700279 Vector<DisplayViewport> virtualViewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800280
Jeff Brown05dc66a2011-03-02 14:41:58 -0800281 // System UI visibility.
282 int32_t systemUiVisibility;
283
Jeff Brown1a84fd12011-06-02 01:26:32 -0700284 // Pointer speed.
285 int32_t pointerSpeed;
286
Jeff Brown474dcb52011-06-14 20:22:50 -0700287 // True if pointer gestures are enabled.
288 bool pointerGesturesEnabled;
289
Jeff Browndaf4a122011-08-26 17:14:14 -0700290 // Show touches feature enable/disable.
291 bool showTouches;
292
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800293 // Pointer capture feature enable/disable.
294 bool pointerCapture;
295
Jeff Brown5541de92011-04-11 11:54:25 -0700296 // Sprite controller singleton, created on first use.
297 sp<SpriteController> spriteController;
298
Jeff Brown83c09682010-12-23 17:50:18 -0800299 // Pointer controller singleton, created and destroyed as needed.
300 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700301
302 // Input devices to be disabled
303 SortedVector<int32_t> disabledInputDevices;
Jeff Brown83c09682010-12-23 17:50:18 -0800304 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700305
Michael Wrighta4051212015-07-23 17:04:40 +0100306 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700307
Jeff Brown2352b972011-04-12 22:39:53 -0700308 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800309 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700310 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800311
Jeff Brownb88102f2010-09-08 11:49:43 -0700312 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700313
Jeff Brown9c3cda02010-06-15 01:31:58 -0700314 static inline JNIEnv* jniEnv() {
315 return AndroidRuntime::getJNIEnv();
316 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700317};
318
Jeff Brown928e0542011-01-10 11:17:36 -0800319
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320
Jeff Brown2352b972011-04-12 22:39:53 -0700321NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700322 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700323 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700324 JNIEnv* env = jniEnv();
325
Jeff Brown2352b972011-04-12 22:39:53 -0700326 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700327 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700328
Jeff Brown83c09682010-12-23 17:50:18 -0800329 {
330 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800331 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700332 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700333 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700334 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800335 mLocked.pointerCapture = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800336 }
Michael Wrighta4051212015-07-23 17:04:40 +0100337 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800338
Jeff Brown9c3cda02010-06-15 01:31:58 -0700339 sp<EventHub> eventHub = new EventHub();
340 mInputManager = new InputManager(eventHub, this, this);
341}
342
343NativeInputManager::~NativeInputManager() {
344 JNIEnv* env = jniEnv();
345
Jeff Brown2352b972011-04-12 22:39:53 -0700346 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700347 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700348}
349
Jeff Brownb88102f2010-09-08 11:49:43 -0700350void NativeInputManager::dump(String8& dump) {
Michael Wrighta4051212015-07-23 17:04:40 +0100351 dump.append("Input Manager State:\n");
352 {
353 dump.appendFormat(INDENT "Interactive: %s\n", toString(mInteractive.load()));
354 }
355 {
356 AutoMutex _l(mLock);
357 dump.appendFormat(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
358 mLocked.systemUiVisibility);
359 dump.appendFormat(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
360 dump.appendFormat(INDENT "Pointer Gestures Enabled: %s\n",
361 toString(mLocked.pointerGesturesEnabled));
362 dump.appendFormat(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800363 dump.appendFormat(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100364 }
365 dump.append("\n");
366
Jeff Brownb88102f2010-09-08 11:49:43 -0700367 mInputManager->getReader()->dump(dump);
368 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700369
Jeff Brownb88102f2010-09-08 11:49:43 -0700370 mInputManager->getDispatcher()->dump(dump);
371 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700372}
373
Jeff Brown7fbdc842010-06-17 20:52:56 -0700374bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700375 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000376 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700377 LOGE_EX(env);
378 env->ExceptionClear();
379 return true;
380 }
381 return false;
382}
383
Santos Cordonee8931e2017-04-05 10:31:15 -0700384void NativeInputManager::setVirtualDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
385 Vector<DisplayViewport> viewports;
386
387 if (viewportObjArray) {
388 jsize length = env->GetArrayLength(viewportObjArray);
389 for (jsize i = 0; i < length; i++) {
390 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
391 if (! viewportObj) {
392 break; // found null element indicating end of used portion of the array
393 }
394
395 DisplayViewport viewport;
396 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
397 ALOGI("Viewport [%d] to add: %s", (int) length, viewport.uniqueId.c_str());
398 viewports.push(viewport);
399
400 env->DeleteLocalRef(viewportObj);
401 }
402 }
403
404 {
405 AutoMutex _l(mLock);
406 mLocked.virtualViewports = viewports;
407 }
408
409 mInputManager->getReader()->requestRefreshConfiguration(
410 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
411}
412
413void NativeInputManager::setDisplayViewport(int32_t type, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700414 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700415 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700416 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700417
Santos Cordonee8931e2017-04-05 10:31:15 -0700418 ViewportType viewportType = static_cast<ViewportType>(type);
419 DisplayViewport* v = NULL;
420 if (viewportType == ViewportType::VIEWPORT_EXTERNAL) {
421 v = &mLocked.externalViewport;
422 } else if (viewportType == ViewportType::VIEWPORT_INTERNAL) {
423 v = &mLocked.internalViewport;
424 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700425
Santos Cordonee8931e2017-04-05 10:31:15 -0700426 if (v != NULL && *v != viewport) {
427 changed = true;
428 *v = viewport;
429
430 if (viewportType == ViewportType::VIEWPORT_INTERNAL) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700431 sp<PointerController> controller = mLocked.pointerController.promote();
432 if (controller != NULL) {
433 controller->setDisplayViewport(
434 viewport.logicalRight - viewport.logicalLeft,
435 viewport.logicalBottom - viewport.logicalTop,
436 viewport.orientation);
437 }
Jeff Brown2352b972011-04-12 22:39:53 -0700438 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700439 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700440 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700441
442 if (changed) {
443 mInputManager->getReader()->requestRefreshConfiguration(
444 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
445 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700446}
447
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700448status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Jeff Brown928e0542011-01-10 11:17:36 -0800449 const sp<InputChannel>& inputChannel,
450 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100451 ATRACE_CALL();
Jeff Brown928e0542011-01-10 11:17:36 -0800452 return mInputManager->getDispatcher()->registerInputChannel(
453 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700454}
455
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700456status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700457 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100458 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700459 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700460}
461
Jeff Brown214eaf42011-05-26 19:17:02 -0700462void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100463 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700464 JNIEnv* env = jniEnv();
465
Jeff Brown4532e612012-04-05 14:27:12 -0700466 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
467 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700468 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
469 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
470 }
471
472 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700473 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
474 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700475 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
476 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700477 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700478 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700479 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700480 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700481 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700482 env->DeleteLocalRef(item);
483 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700484 env->DeleteLocalRef(excludedDeviceNames);
485 }
486
Jeff Brown4532e612012-04-05 14:27:12 -0700487 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
488 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700489 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700490 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
491 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700492 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700493 jint longPressTimeout = env->CallIntMethod(mServiceObj,
494 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700495 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700496 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700497
498 // We must ensure that the tap-drag interval is significantly shorter than
499 // the long-press timeout because the tap is held down for the entire duration
500 // of the double-tap timeout.
501 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700502 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700503 outConfig->pointerGestureTapDragInterval =
504 milliseconds_to_nanoseconds(tapDragInterval);
505 }
506 }
507 }
508
Jeff Brown4532e612012-04-05 14:27:12 -0700509 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
510 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700511 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
512 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700513 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700514
515 { // acquire lock
516 AutoMutex _l(mLock);
517
518 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
519 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700520 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700521
Jeff Browndaf4a122011-08-26 17:14:14 -0700522 outConfig->showTouches = mLocked.showTouches;
523
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800524 outConfig->pointerCapture = mLocked.pointerCapture;
525
Santos Cordonee8931e2017-04-05 10:31:15 -0700526 outConfig->setPhysicalDisplayViewport(ViewportType::VIEWPORT_INTERNAL,
527 mLocked.internalViewport);
528 outConfig->setPhysicalDisplayViewport(ViewportType::VIEWPORT_EXTERNAL,
529 mLocked.externalViewport);
530 outConfig->setVirtualDisplayViewports(mLocked.virtualViewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700531
532 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700533 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700534}
535
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700536sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100537 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800538 AutoMutex _l(mLock);
539
540 sp<PointerController> controller = mLocked.pointerController.promote();
541 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700542 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800543
Jeff Brown2352b972011-04-12 22:39:53 -0700544 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800545 mLocked.pointerController = controller;
546
Jeff Brownd728bf52012-09-08 18:05:28 -0700547 DisplayViewport& v = mLocked.internalViewport;
548 controller->setDisplayViewport(
549 v.logicalRight - v.logicalLeft,
550 v.logicalBottom - v.logicalTop,
551 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800552
Jeff Brown2352b972011-04-12 22:39:53 -0700553 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800554 }
555 return controller;
556}
557
Jeff Brown5541de92011-04-11 11:54:25 -0700558void NativeInputManager::ensureSpriteControllerLocked() {
559 if (mLocked.spriteController == NULL) {
560 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700561 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700562 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
563 layer = -1;
564 }
565 mLocked.spriteController = new SpriteController(mLooper, layer);
566 }
567}
568
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700569void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100570 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700571 JNIEnv* env = jniEnv();
572
573 size_t count = inputDevices.size();
574 jobjectArray inputDevicesObjArray = env->NewObjectArray(
575 count, gInputDeviceClassInfo.clazz, NULL);
576 if (inputDevicesObjArray) {
577 bool error = false;
578 for (size_t i = 0; i < count; i++) {
579 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
580 if (!inputDeviceObj) {
581 error = true;
582 break;
583 }
584
585 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
586 env->DeleteLocalRef(inputDeviceObj);
587 }
588
589 if (!error) {
590 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
591 inputDevicesObjArray);
592 }
593
594 env->DeleteLocalRef(inputDevicesObjArray);
595 }
596
597 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
598}
599
Jeff Brown6ec6f792012-04-17 16:52:41 -0700600sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800601 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100602 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700603 JNIEnv* env = jniEnv();
604
605 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800606 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
607 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
608 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
609 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700610 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800611 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700612 if (arrayObj.get()) {
613 ScopedLocalRef<jstring> filenameObj(env,
614 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
615 ScopedLocalRef<jstring> contentsObj(env,
616 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
617 ScopedUtfChars filenameChars(env, filenameObj.get());
618 ScopedUtfChars contentsChars(env, contentsObj.get());
619
620 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
621 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
622 }
623 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
624 return result;
625}
626
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700627String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100628 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700629 JNIEnv* env = jniEnv();
630
631 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
632 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
633 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
634 String8 result;
635 if (aliasObj.get()) {
636 ScopedUtfChars aliasChars(env, aliasObj.get());
637 result.setTo(aliasChars.c_str());
638 }
639 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
640 return result;
641}
642
Jeff Brownbcc046a2012-09-27 20:46:43 -0700643void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700644 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700645#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700646 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
647 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700648#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100649 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700650
651 JNIEnv* env = jniEnv();
652
Jeff Brown53384282012-08-20 20:16:01 -0700653 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700654 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700655 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700656}
657
Jeff Brown9c3cda02010-06-15 01:31:58 -0700658void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
659#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000660 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700661#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100662 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700663
664 JNIEnv* env = jniEnv();
665
Jeff Brown4532e612012-04-05 14:27:12 -0700666 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700667 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700668}
669
Jeff Brown519e0242010-09-15 15:18:56 -0700670nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700671 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700672#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000673 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700674#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100675 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700676
677 JNIEnv* env = jniEnv();
678
Jeff Brown928e0542011-01-10 11:17:36 -0800679 jobject inputApplicationHandleObj =
680 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
681 jobject inputWindowHandleObj =
682 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700683 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700684
Jeff Brown4532e612012-04-05 14:27:12 -0700685 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700686 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
687 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700688 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
689 newTimeout = 0; // abort dispatch
690 } else {
691 assert(newTimeout >= 0);
692 }
693
Jeff Brownbd181bb2013-09-10 16:44:24 -0700694 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800695 env->DeleteLocalRef(inputWindowHandleObj);
696 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700697 return newTimeout;
698}
699
Jeff Brown928e0542011-01-10 11:17:36 -0800700void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700701#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000702 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700703#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100704 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700705
Jeff Brown7fbdc842010-06-17 20:52:56 -0700706 JNIEnv* env = jniEnv();
707
Jeff Brown928e0542011-01-10 11:17:36 -0800708 jobject inputWindowHandleObj =
709 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
710 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700711 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800712 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700713 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
714
Jeff Brown928e0542011-01-10 11:17:36 -0800715 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700716 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700717}
718
Jeff Brown214eaf42011-05-26 19:17:02 -0700719void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100720 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700721 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800722
Jeff Brown4532e612012-04-05 14:27:12 -0700723 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
724 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700725 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
726 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
727 }
Jeff Browna4547672011-03-02 21:38:11 -0800728
Jeff Brown4532e612012-04-05 14:27:12 -0700729 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
730 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700731 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
732 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
733 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700734}
735
Jeff Brown9302c872011-07-13 22:51:29 -0700736void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
737 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700738
Jeff Brown9302c872011-07-13 22:51:29 -0700739 if (windowHandleObjArray) {
740 jsize length = env->GetArrayLength(windowHandleObjArray);
741 for (jsize i = 0; i < length; i++) {
742 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
743 if (! windowHandleObj) {
744 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700745 }
Jeff Brown9302c872011-07-13 22:51:29 -0700746
747 sp<InputWindowHandle> windowHandle =
748 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
749 if (windowHandle != NULL) {
750 windowHandles.push(windowHandle);
751 }
752 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700753 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700754 }
Jeff Brown349703e2010-06-22 01:27:15 -0700755
Jeff Brown9302c872011-07-13 22:51:29 -0700756 mInputManager->getDispatcher()->setInputWindows(windowHandles);
757
758 // Do this after the dispatcher has updated the window handle state.
759 bool newPointerGesturesEnabled = true;
760 size_t numWindows = windowHandles.size();
761 for (size_t i = 0; i < numWindows; i++) {
762 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700763 const InputWindowInfo* windowInfo = windowHandle->getInfo();
764 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
765 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700766 newPointerGesturesEnabled = false;
767 }
768 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700769
770 uint32_t changes = 0;
771 { // acquire lock
772 AutoMutex _l(mLock);
773
774 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
775 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
776 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
777 }
778 } // release lock
779
780 if (changes) {
781 mInputManager->getReader()->requestRefreshConfiguration(changes);
782 }
Jeff Brown349703e2010-06-22 01:27:15 -0700783}
784
Jeff Brown9302c872011-07-13 22:51:29 -0700785void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
786 sp<InputApplicationHandle> applicationHandle =
787 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
788 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700789}
790
791void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700792 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700793}
794
Jeff Brown05dc66a2011-03-02 14:41:58 -0800795void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
796 AutoMutex _l(mLock);
797
798 if (mLocked.systemUiVisibility != visibility) {
799 mLocked.systemUiVisibility = visibility;
800
801 sp<PointerController> controller = mLocked.pointerController.promote();
802 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700803 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800804 }
805 }
806}
807
Jeff Brown2352b972011-04-12 22:39:53 -0700808void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800809 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700810 controller->setInactivityTimeout(lightsOut
811 ? PointerController::INACTIVITY_TIMEOUT_SHORT
812 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800813}
814
Jeff Brown1a84fd12011-06-02 01:26:32 -0700815void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700816 { // acquire lock
817 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700818
Jeff Brown474dcb52011-06-14 20:22:50 -0700819 if (mLocked.pointerSpeed == speed) {
820 return;
821 }
822
Steve Block6215d3f2012-01-04 20:05:49 +0000823 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700824 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700825 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700826
Jeff Brown474dcb52011-06-14 20:22:50 -0700827 mInputManager->getReader()->requestRefreshConfiguration(
828 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700829}
830
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700831void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
832 { // acquire lock
833 AutoMutex _l(mLock);
834
835 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
836 bool currentlyEnabled = index < 0;
837 if (!enabled && currentlyEnabled) {
838 mLocked.disabledInputDevices.add(deviceId);
839 }
840 if (enabled && !currentlyEnabled) {
841 mLocked.disabledInputDevices.remove(deviceId);
842 }
843 } // release lock
844
845 mInputManager->getReader()->requestRefreshConfiguration(
846 InputReaderConfiguration::CHANGE_ENABLED_STATE);
847}
848
Jeff Browndaf4a122011-08-26 17:14:14 -0700849void NativeInputManager::setShowTouches(bool enabled) {
850 { // acquire lock
851 AutoMutex _l(mLock);
852
853 if (mLocked.showTouches == enabled) {
854 return;
855 }
856
Steve Block6215d3f2012-01-04 20:05:49 +0000857 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700858 mLocked.showTouches = enabled;
859 } // release lock
860
861 mInputManager->getReader()->requestRefreshConfiguration(
862 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
863}
864
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800865void NativeInputManager::setPointerCapture(bool enabled) {
866 { // acquire lock
867 AutoMutex _l(mLock);
868
869 if (mLocked.pointerCapture == enabled) {
870 return;
871 }
872
873 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
874 mLocked.pointerCapture = enabled;
875 } // release lock
876
877 mInputManager->getReader()->requestRefreshConfiguration(
878 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
879}
880
Jeff Brown037c33e2014-04-09 00:31:55 -0700881void NativeInputManager::setInteractive(bool interactive) {
882 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700883}
884
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800885void NativeInputManager::reloadCalibration() {
886 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100887 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800888}
889
Michael Wrighte051f6f2016-05-13 17:44:16 +0100890void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800891 AutoMutex _l(mLock);
892 sp<PointerController> controller = mLocked.pointerController.promote();
893 if (controller != NULL) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100894 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800895 }
896}
897
898void NativeInputManager::reloadPointerIcons() {
899 AutoMutex _l(mLock);
900 sp<PointerController> controller = mLocked.pointerController.promote();
901 if (controller != NULL) {
902 controller->reloadPointerResources();
903 }
Jun Mukai1db53972015-09-11 18:08:31 -0700904}
905
Jun Mukaid4eaef72015-10-30 15:54:33 -0700906void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
907 AutoMutex _l(mLock);
908 sp<PointerController> controller = mLocked.pointerController.promote();
909 if (controller != NULL) {
910 controller->setCustomPointerIcon(icon);
911 }
912}
913
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800914TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
915 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100916 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800917 ScopedFloatArrayRO matrix(env, matrixArr);
918 assert(matrix.size() == 6);
919
920 TouchAffineTransformation transform;
921 transform.x_scale = matrix[0];
922 transform.x_ymix = matrix[1];
923 transform.x_offset = matrix[2];
924 transform.y_xmix = matrix[3];
925 transform.y_scale = matrix[4];
926 transform.y_offset = matrix[5];
927
928 return transform;
929}
930
931TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700932 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800933 JNIEnv* env = jniEnv();
934
935 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
936
937 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700938 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
939 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800940
941 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
942 gTouchCalibrationClassInfo.getAffineTransform));
943
944 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
945
946 env->DeleteLocalRef(matrixArr);
947 env->DeleteLocalRef(cal);
948
949 return transform;
950}
951
Jeff Brown0029c662011-03-30 02:25:18 -0700952bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100953 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700954 jobject inputEventObj;
955
956 JNIEnv* env = jniEnv();
957 switch (inputEvent->getType()) {
958 case AINPUT_EVENT_TYPE_KEY:
959 inputEventObj = android_view_KeyEvent_fromNative(env,
960 static_cast<const KeyEvent*>(inputEvent));
961 break;
962 case AINPUT_EVENT_TYPE_MOTION:
963 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
964 static_cast<const MotionEvent*>(inputEvent));
965 break;
966 default:
967 return true; // dispatch the event normally
968 }
969
970 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000971 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700972 return true; // dispatch the event normally
973 }
974
975 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700976 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700977 inputEventObj, policyFlags);
978 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
979 pass = true;
980 }
981 env->DeleteLocalRef(inputEventObj);
982 return pass;
983}
984
Jeff Brown1f245102010-11-18 20:53:46 -0800985void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
986 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100987 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700988 // Policy:
989 // - Ignore untrusted events and pass them along.
990 // - Ask the window manager what to do with normal events and trusted injected events.
991 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100992 bool interactive = mInteractive.load();
993 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700994 policyFlags |= POLICY_FLAG_INTERACTIVE;
995 }
Jeff Brown3122e442010-10-11 23:32:49 -0700996 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800997 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700998 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800999 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1000 jint wmActions;
1001 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001002 wmActions = env->CallIntMethod(mServiceObj,
1003 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -07001004 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001005 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
1006 wmActions = 0;
1007 }
1008 android_view_KeyEvent_recycle(env, keyEventObj);
1009 env->DeleteLocalRef(keyEventObj);
1010 } else {
Steve Block3762c312012-01-06 19:20:56 +00001011 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -07001012 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -07001013 }
1014
Jeff Brown56194eb2011-03-02 19:23:13 -08001015 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -07001016 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001017 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001018 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1019 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001020 }
1021}
1022
Jeff Brown56194eb2011-03-02 19:23:13 -08001023void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001024 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001025 // Policy:
1026 // - Ignore untrusted events and pass them along.
1027 // - No special filtering for injected events required at this time.
1028 // - Filter normal events based on screen state.
1029 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001030 bool interactive = mInteractive.load();
1031 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001032 policyFlags |= POLICY_FLAG_INTERACTIVE;
1033 }
Jeff Brown3122e442010-10-11 23:32:49 -07001034 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001035 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001036 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001037 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001038 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001039 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001040 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001041 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001042 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001043 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001044 wmActions = 0;
1045 }
1046
Jeff Brown56194eb2011-03-02 19:23:13 -08001047 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001048 }
Jeff Brown3122e442010-10-11 23:32:49 -07001049 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001050 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001051 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1052 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001053 }
1054}
1055
Jeff Brown56194eb2011-03-02 19:23:13 -08001056void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1057 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001058 if (wmActions & WM_ACTION_PASS_TO_USER) {
1059 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1060 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001061#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001062 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001063#endif
1064 }
1065}
1066
Jeff Brown905805a2011-10-12 13:57:59 -07001067nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -08001068 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -07001069 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001070 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001071 // Policy:
1072 // - Ignore untrusted events and pass them along.
1073 // - Filter normal events and trusted injected events through the window manager policy to
1074 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001075 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001076 if (policyFlags & POLICY_FLAG_TRUSTED) {
1077 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001078
Jeff Brown928e0542011-01-10 11:17:36 -08001079 // Note: inputWindowHandle may be null.
1080 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001081 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1082 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001083 jlong delayMillis = env->CallLongMethod(mServiceObj,
1084 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -08001085 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001086 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1087 android_view_KeyEvent_recycle(env, keyEventObj);
1088 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001089 if (!error) {
1090 if (delayMillis < 0) {
1091 result = -1;
1092 } else if (delayMillis > 0) {
1093 result = milliseconds_to_nanoseconds(delayMillis);
1094 }
1095 }
Jeff Brown1f245102010-11-18 20:53:46 -08001096 } else {
Steve Block3762c312012-01-06 19:20:56 +00001097 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001098 }
Jeff Brown928e0542011-01-10 11:17:36 -08001099 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -07001100 }
Jeff Brown1f245102010-11-18 20:53:46 -08001101 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001102}
1103
Jeff Brown928e0542011-01-10 11:17:36 -08001104bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001105 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001106 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001107 // Policy:
1108 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001109 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001110 if (policyFlags & POLICY_FLAG_TRUSTED) {
1111 JNIEnv* env = jniEnv();
1112
Jeff Brown928e0542011-01-10 11:17:36 -08001113 // Note: inputWindowHandle may be null.
1114 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001115 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1116 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001117 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1118 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -08001119 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001120 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
1121 fallbackKeyEventObj = NULL;
1122 }
Jeff Brown1f245102010-11-18 20:53:46 -08001123 android_view_KeyEvent_recycle(env, keyEventObj);
1124 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001125
1126 if (fallbackKeyEventObj) {
1127 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1128 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1129 outFallbackKeyEvent)) {
1130 result = true;
1131 }
1132 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1133 env->DeleteLocalRef(fallbackKeyEventObj);
1134 }
Jeff Brown1f245102010-11-18 20:53:46 -08001135 } else {
Steve Block3762c312012-01-06 19:20:56 +00001136 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001137 }
Jeff Brown928e0542011-01-10 11:17:36 -08001138 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001139 }
Jeff Brown1f245102010-11-18 20:53:46 -08001140 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001141}
1142
Jeff Brown01ce2e92010-09-26 22:20:12 -07001143void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001144 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001145 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001146}
1147
Jeff Brown349703e2010-06-22 01:27:15 -07001148
Jeff Brownb88102f2010-09-08 11:49:43 -07001149bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1150 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001151 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001152 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001153 jboolean result = env->CallBooleanMethod(mServiceObj,
1154 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001155 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1156 result = false;
1157 }
Jeff Brown349703e2010-06-22 01:27:15 -07001158 return result;
1159}
1160
Jun Mukai19a56012015-11-24 11:25:52 -08001161void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001162 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001163 JNIEnv* env = jniEnv();
1164
1165 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1166 mServiceObj, gServiceClassInfo.getPointerIcon));
1167 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1168 return;
1169 }
1170
1171 PointerIcon pointerIcon;
1172 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1173 mContextObj, &pointerIcon);
1174 if (!status && !pointerIcon.isNullIcon()) {
1175 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1176 } else {
1177 *icon = SpriteIcon();
1178 }
1179}
1180
Jeff Brown2352b972011-04-12 22:39:53 -07001181void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001182 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001183 JNIEnv* env = jniEnv();
1184
1185 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1186 &outResources->spotHover);
1187 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1188 &outResources->spotTouch);
1189 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1190 &outResources->spotAnchor);
1191}
1192
Jun Mukai808196f2015-10-28 16:46:44 -07001193void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1194 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001195 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001196 JNIEnv* env = jniEnv();
1197
1198 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1199 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001200 PointerIcon pointerIcon;
1201 loadSystemIconAsSpriteWithPointerIcon(
1202 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1203 if (!pointerIcon.bitmapFrames.empty()) {
1204 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1205 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1206 animationData.durationPerFrame =
1207 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1208 animationData.animationFrames.reserve(numFrames);
1209 animationData.animationFrames.push_back(SpriteIcon(
1210 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1211 for (size_t i = 0; i < numFrames - 1; ++i) {
1212 animationData.animationFrames.push_back(SpriteIcon(
1213 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1214 }
1215 }
Jun Mukai1db53972015-09-11 18:08:31 -07001216 }
Jun Mukai808196f2015-10-28 16:46:44 -07001217 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1218 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001219}
1220
Jun Mukai5ec74202015-10-07 16:58:09 +09001221int32_t NativeInputManager::getDefaultPointerIconId() {
1222 return POINTER_ICON_STYLE_ARROW;
1223}
Jeff Brown83c09682010-12-23 17:50:18 -08001224
Jun Mukaid4eaef72015-10-30 15:54:33 -07001225int32_t NativeInputManager::getCustomPointerIconId() {
1226 return POINTER_ICON_STYLE_CUSTOM;
1227}
1228
Jeff Brown9c3cda02010-06-15 01:31:58 -07001229// ----------------------------------------------------------------------------
1230
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001231static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001232 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001233 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001234 if (messageQueue == NULL) {
1235 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1236 return 0;
1237 }
1238
Jeff Brown603b4452012-04-06 17:39:41 -07001239 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1240 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001241 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001242 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001243}
1244
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001245static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001246 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001247
Jeff Brown4532e612012-04-05 14:27:12 -07001248 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001249 if (result) {
1250 jniThrowRuntimeException(env, "Input manager could not be started.");
1251 }
1252}
1253
Santos Cordonee8931e2017-04-05 10:31:15 -07001254static void nativeSetVirtualDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
1255 jobjectArray viewportObjArray) {
1256 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1257 im->setVirtualDisplayViewports(env, viewportObjArray);
1258}
1259
1260static void nativeSetDisplayViewport(JNIEnv* env, jclass /* clazz */, jlong ptr,
1261 jint viewportType, jint displayId, jint orientation,
Jeff Brownd728bf52012-09-08 18:05:28 -07001262 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001263 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
Santos Cordonee8931e2017-04-05 10:31:15 -07001264 jint deviceWidth, jint deviceHeight, jstring uniqueId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001265 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001266
Jeff Brownd728bf52012-09-08 18:05:28 -07001267 DisplayViewport v;
1268 v.displayId = displayId;
1269 v.orientation = orientation;
1270 v.logicalLeft = logicalLeft;
1271 v.logicalTop = logicalTop;
1272 v.logicalRight = logicalRight;
1273 v.logicalBottom = logicalBottom;
1274 v.physicalLeft = physicalLeft;
1275 v.physicalTop = physicalTop;
1276 v.physicalRight = physicalRight;
1277 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001278 v.deviceWidth = deviceWidth;
1279 v.deviceHeight = deviceHeight;
Santos Cordonee8931e2017-04-05 10:31:15 -07001280 if (uniqueId != nullptr) {
1281 v.uniqueId.setTo(ScopedUtfChars(env, uniqueId).c_str());
1282 }
1283
1284 im->setDisplayViewport(viewportType, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001285}
1286
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001287static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001288 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001289 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001290
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001291 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001292 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001293}
1294
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001295static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001296 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001297 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001298
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001299 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001300 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001301}
1302
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001303static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001304 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001305 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001306
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001307 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001308 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001309}
1310
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001311static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001312 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001313 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001314
1315 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1316 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1317 jsize numCodes = env->GetArrayLength(keyCodes);
1318 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001319 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001320 if (im->getInputManager()->getReader()->hasKeys(
1321 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1322 result = JNI_TRUE;
1323 } else {
1324 result = JNI_FALSE;
1325 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001326 } else {
1327 result = JNI_FALSE;
1328 }
1329
1330 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1331 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1332 return result;
1333}
1334
1335static void throwInputChannelNotInitialized(JNIEnv* env) {
1336 jniThrowException(env, "java/lang/IllegalStateException",
1337 "inputChannel is not initialized");
1338}
1339
Jeff Brown4532e612012-04-05 14:27:12 -07001340static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001341 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001342 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1343
Steve Block8564c8d2012-01-05 23:22:43 +00001344 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001345 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001346 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001347}
1348
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001349static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001350 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001351 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001352
1353 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1354 inputChannelObj);
1355 if (inputChannel == NULL) {
1356 throwInputChannelNotInitialized(env);
1357 return;
1358 }
1359
Jeff Brown928e0542011-01-10 11:17:36 -08001360 sp<InputWindowHandle> inputWindowHandle =
1361 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001362
Jeff Brown4532e612012-04-05 14:27:12 -07001363 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001364 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001365 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001366 String8 message;
1367 message.appendFormat("Failed to register input channel. status=%d", status);
1368 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001369 return;
1370 }
1371
Jeff Browna41ca772010-08-11 14:46:32 -07001372 if (! monitor) {
1373 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001374 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001375 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001376}
1377
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001378static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001379 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001380 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001381
1382 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1383 inputChannelObj);
1384 if (inputChannel == NULL) {
1385 throwInputChannelNotInitialized(env);
1386 return;
1387 }
1388
1389 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1390
Jeff Brown4532e612012-04-05 14:27:12 -07001391 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001392 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1393 String8 message;
1394 message.appendFormat("Failed to unregister input channel. status=%d", status);
1395 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001396 }
1397}
1398
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001399static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001400 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001401 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001402
Jeff Brown4532e612012-04-05 14:27:12 -07001403 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001404}
1405
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001406static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Jeff Brownca9bc702014-02-11 14:32:56 -08001407 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001408 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001409 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001410
Jeff Brown6ec402b2010-07-28 15:48:59 -07001411 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1412 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001413 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1414 if (status) {
1415 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1416 return INPUT_EVENT_INJECTION_FAILED;
1417 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001418
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001419 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001420 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001421 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001422 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001423 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1424 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001425 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1426 return INPUT_EVENT_INJECTION_FAILED;
1427 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001428
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001429 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001430 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001431 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001432 } else {
1433 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001434 return INPUT_EVENT_INJECTION_FAILED;
1435 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001436}
1437
Andrii Kulian112d0562016-03-08 10:44:22 -08001438static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1439 jlong ptr, jint deviceId) {
1440 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001441
Andrii Kulian112d0562016-03-08 10:44:22 -08001442 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1443}
1444
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001445static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001446 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001447 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001448
Jeff Brown4532e612012-04-05 14:27:12 -07001449 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001450}
1451
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001452static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001453 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001454 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001455
Jeff Brown4532e612012-04-05 14:27:12 -07001456 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001457}
1458
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001459static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1460 jboolean enabled) {
1461 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001462
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001463 im->setPointerCapture(enabled);
1464}
1465
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001466static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1467 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001468 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001469
Jeff Brown4532e612012-04-05 14:27:12 -07001470 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001471}
1472
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001473static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1474 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001475 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001476
Jeff Brown4532e612012-04-05 14:27:12 -07001477 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001478}
1479
Jeff Brown4532e612012-04-05 14:27:12 -07001480static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001481 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001482 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001483
1484 sp<InputChannel> fromChannel =
1485 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1486 sp<InputChannel> toChannel =
1487 android_view_InputChannel_getInputChannel(env, toChannelObj);
1488
1489 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001490 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001491 }
1492
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001493 if (im->getInputManager()->getDispatcher()->
1494 transferTouchFocus(fromChannel, toChannel)) {
1495 return JNI_TRUE;
1496 } else {
1497 return JNI_FALSE;
1498 }
Jeff Browne6504122010-09-27 14:52:15 -07001499}
1500
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001501static void nativeSetPointerSpeed(JNIEnv* /* env */,
1502 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001503 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001504
Jeff Brown4532e612012-04-05 14:27:12 -07001505 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001506}
1507
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001508static void nativeSetShowTouches(JNIEnv* /* env */,
1509 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001510 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001511
Jeff Brown4532e612012-04-05 14:27:12 -07001512 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001513}
1514
Jeff Brown037c33e2014-04-09 00:31:55 -07001515static void nativeSetInteractive(JNIEnv* env,
1516 jclass clazz, jlong ptr, jboolean interactive) {
1517 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1518
1519 im->setInteractive(interactive);
1520}
1521
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001522static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1523 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001524
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001525 im->reloadCalibration();
1526}
1527
Jeff Browna47425a2012-04-13 04:09:27 -07001528static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001529 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001530 jint repeat, jint token) {
1531 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1532
1533 size_t patternSize = env->GetArrayLength(patternObj);
1534 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001535 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001536 "which is more than the maximum supported size of %d.",
1537 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1538 return; // limit to reasonable size
1539 }
1540
1541 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1542 patternObj, NULL));
1543 nsecs_t pattern[patternSize];
1544 for (size_t i = 0; i < patternSize; i++) {
1545 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001546 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001547 }
1548 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1549
1550 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1551}
1552
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001553static void nativeCancelVibrate(JNIEnv* /* env */,
1554 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001555 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1556
1557 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1558}
1559
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001560static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1561 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001562 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1563
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001564 im->getInputManager()->getReader()->requestRefreshConfiguration(
1565 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1566}
1567
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001568static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1569 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001570 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1571
1572 im->getInputManager()->getReader()->requestRefreshConfiguration(
1573 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001574}
1575
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001576static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001577 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001578
Jeff Brownb88102f2010-09-08 11:49:43 -07001579 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001580 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001581 return env->NewStringUTF(dump.string());
1582}
1583
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001584static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001585 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001586
Jeff Brown4532e612012-04-05 14:27:12 -07001587 im->getInputManager()->getReader()->monitor();
1588 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001589}
1590
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001591static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1592 jclass /* clazz */, jlong ptr, jint deviceId) {
1593 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1594
1595 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1596}
1597
1598static void nativeEnableInputDevice(JNIEnv* /* env */,
1599 jclass /* clazz */, jlong ptr, jint deviceId) {
1600 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1601
1602 im->setInputDeviceEnabled(deviceId, true);
1603}
1604
1605static void nativeDisableInputDevice(JNIEnv* /* env */,
1606 jclass /* clazz */, jlong ptr, jint deviceId) {
1607 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1608
1609 im->setInputDeviceEnabled(deviceId, false);
1610}
1611
Michael Wrighte051f6f2016-05-13 17:44:16 +01001612static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001613 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001614
Michael Wrighte051f6f2016-05-13 17:44:16 +01001615 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001616}
1617
Jun Mukai19a56012015-11-24 11:25:52 -08001618static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1619 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001620
Jun Mukai19a56012015-11-24 11:25:52 -08001621 im->reloadPointerIcons();
1622}
1623
Jun Mukaid4eaef72015-10-30 15:54:33 -07001624static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1625 jlong ptr, jobject iconObj) {
1626 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1627
1628 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001629 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1630 if (result) {
1631 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1632 return;
1633 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001634
1635 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001636 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1637 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1638 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1639 spriteIcon.bitmap.rowBytes(), 0, 0);
1640 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001641 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1642 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1643 im->setCustomPointerIcon(spriteIcon);
1644}
1645
Jeff Brown9c3cda02010-06-15 01:31:58 -07001646// ----------------------------------------------------------------------------
1647
Daniel Micay76f6a862015-09-19 17:31:01 -04001648static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001649 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001650 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001651 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001652 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001653 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001654 (void*) nativeStart },
Santos Cordonee8931e2017-04-05 10:31:15 -07001655 { "nativeSetVirtualDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1656 (void*) nativeSetVirtualDisplayViewports },
1657 { "nativeSetDisplayViewport", "(JIIIIIIIIIIIIILjava/lang/String;)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001658 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001659 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001660 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001661 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001662 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001663 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001664 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001665 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001666 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001667 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001668 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001669 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001670 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001671 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001672 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001673 (void*) nativeSetInputFilterEnabled },
Jeff Brownca9bc702014-02-11 14:32:56 -08001674 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001675 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001676 { "nativeToggleCapsLock", "(JI)V",
1677 (void*) nativeToggleCapsLock },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001678 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001679 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001680 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001681 (void*) nativeSetFocusedApplication },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001682 { "nativeSetPointerCapture", "(JZ)V",
1683 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001684 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001685 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001686 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001687 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001688 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001689 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001690 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001691 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001692 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001693 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001694 { "nativeSetInteractive", "(JZ)V",
1695 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001696 { "nativeReloadCalibration", "(J)V",
1697 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001698 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001699 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001700 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001701 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001702 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001703 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001704 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001705 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001706 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001707 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001708 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001709 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001710 { "nativeIsInputDeviceEnabled", "(JI)Z",
1711 (void*) nativeIsInputDeviceEnabled },
1712 { "nativeEnableInputDevice", "(JI)V",
1713 (void*) nativeEnableInputDevice },
1714 { "nativeDisableInputDevice", "(JI)V",
1715 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001716 { "nativeSetPointerIconType", "(JI)V",
1717 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001718 { "nativeReloadPointerIcons", "(J)V",
1719 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001720 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1721 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001722};
1723
1724#define FIND_CLASS(var, className) \
1725 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001726 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001727
1728#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1729 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001730 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001731
1732#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1733 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001734 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001735
1736int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001737 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001738 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001739 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001740 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1741
Jeff Brown9c3cda02010-06-15 01:31:58 -07001742 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001743
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001744 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001745 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001746
Jeff Brown4532e612012-04-05 14:27:12 -07001747 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001748 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001749
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001750 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1751 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1752
Jeff Brown53384282012-08-20 20:16:01 -07001753 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1754 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001755
Jeff Brown4532e612012-04-05 14:27:12 -07001756 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1757 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001758
Jeff Brown4532e612012-04-05 14:27:12 -07001759 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001760 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001761 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001762
Jeff Brown4532e612012-04-05 14:27:12 -07001763 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001764 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1765
Jeff Brown4532e612012-04-05 14:27:12 -07001766 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001767 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001768
Michael Wright70af00a2014-09-03 19:30:20 -07001769 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1770 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001771
Jeff Brown4532e612012-04-05 14:27:12 -07001772 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001773 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001774 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001775
Jeff Brown4532e612012-04-05 14:27:12 -07001776 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001777 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001778 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001779
Jeff Brown4532e612012-04-05 14:27:12 -07001780 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001781 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001782
Jeff Brown4532e612012-04-05 14:27:12 -07001783 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001784 "getVirtualKeyQuietTimeMillis", "()I");
1785
Jeff Brown4532e612012-04-05 14:27:12 -07001786 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001787 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1788
Jeff Brown4532e612012-04-05 14:27:12 -07001789 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001790 "getKeyRepeatTimeout", "()I");
1791
Jeff Brown4532e612012-04-05 14:27:12 -07001792 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001793 "getKeyRepeatDelay", "()I");
1794
Jeff Brown4532e612012-04-05 14:27:12 -07001795 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001796 "getHoverTapTimeout", "()I");
1797
Jeff Brown4532e612012-04-05 14:27:12 -07001798 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001799 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001800
Jeff Brown4532e612012-04-05 14:27:12 -07001801 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001802 "getDoubleTapTimeout", "()I");
1803
Jeff Brown4532e612012-04-05 14:27:12 -07001804 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001805 "getLongPressTimeout", "()I");
1806
Jeff Brown4532e612012-04-05 14:27:12 -07001807 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001808 "getPointerLayer", "()I");
1809
Jeff Brown4532e612012-04-05 14:27:12 -07001810 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001811 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001812
Jeff Brown6ec6f792012-04-17 16:52:41 -07001813 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001814 "getKeyboardLayoutOverlay",
1815 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001816
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001817 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1818 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1819
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001820 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1821 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001822 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001823
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001824 // InputDevice
1825
1826 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1827 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1828
Jeff Brown6ec402b2010-07-28 15:48:59 -07001829 // KeyEvent
1830
1831 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001832 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1833
Jeff Brown8d608662010-08-30 03:02:23 -07001834 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001835
1836 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001837 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001838
RoboErikfb290df2013-12-16 11:27:55 -08001839 // InputDeviceIdentifier
1840
1841 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1842 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1843 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1844 "<init>", "(Ljava/lang/String;II)V");
1845
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001846 // TouchCalibration
1847
1848 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1849 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1850
1851 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1852 "getAffineTransform", "()[F");
1853
Jeff Brown46b9ac02010-04-22 18:58:52 -07001854 return 0;
1855}
1856
Jeff Brown46b9ac02010-04-22 18:58:52 -07001857} /* namespace android */