blob: f5f19f644e258fa600dea8c0b601c12849a5f341 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputManager-JNI"
18
Michael Wrightbafea6e2017-06-12 15:25:47 +010019#define ATRACE_TAG ATRACE_TAG_INPUT
20
Jeff Brown9c3cda02010-06-15 01:31:58 -070021//#define LOG_NDEBUG 0
22
23// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070024#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070025
26// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070027#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070028
Jeff Brown83c09682010-12-23 17:50:18 -080029
Steven Moreland2279b252017-07-19 09:50:45 -070030#include <nativehelper/JNIHelp.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031#include "jni.h"
Michael Wrighta4051212015-07-23 17:04:40 +010032#include <atomic>
33#include <cinttypes>
Jeff Brown349703e2010-06-22 01:27:15 -070034#include <limits.h>
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080035#include <android-base/stringprintf.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070037#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038
Jeff Brown46b9ac02010-04-22 18:58:52 -070039#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080040#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070041#include <utils/threads.h>
Michael Wrightbafea6e2017-06-12 15:25:47 +010042#include <utils/Trace.h>
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070043#include <utils/SortedVector.h>
Jeff Brown83c09682010-12-23 17:50:18 -080044
Jeff Brownb4ff35d2011-01-02 16:37:43 -080045#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070046#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047
Michael Wrightd6b473712014-02-10 15:56:36 -080048#include <inputflinger/InputManager.h>
49
Jeff Brown05dc66a2011-03-02 14:41:58 -080050#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070051#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080052#include <android_view_KeyEvent.h>
53#include <android_view_MotionEvent.h>
54#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070055#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080056#include <android/graphics/GraphicsJNI.h>
57
Steven Moreland2279b252017-07-19 09:50:45 -070058#include <nativehelper/ScopedLocalRef.h>
59#include <nativehelper/ScopedPrimitiveArray.h>
60#include <nativehelper/ScopedUtfChars.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070061
Jeff Brown4f8ecd82012-06-18 18:29:13 -070062#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070063#include "com_android_server_input_InputApplicationHandle.h"
64#include "com_android_server_input_InputWindowHandle.h"
Santos Cordonee8931e2017-04-05 10:31:15 -070065#include "android_hardware_display_DisplayViewport.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070066
Michael Wrighta4051212015-07-23 17:04:40 +010067#define INDENT " "
68
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080069using android::base::StringPrintf;
70
Jeff Brown46b9ac02010-04-22 18:58:52 -070071namespace android {
72
Jeff Brown1a84fd12011-06-02 01:26:32 -070073// The exponent used to calculate the pointer speed scaling factor.
74// The scaling factor is calculated as 2 ^ (speed * exponent),
75// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070076static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070077
Jeff Brown46b9ac02010-04-22 18:58:52 -070078static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070079 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070080 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070081 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070082 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070083 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070084 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070085 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070086 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070087 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070088 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070089 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080090 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070091 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080092 jmethodID getKeyRepeatTimeout;
93 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070094 jmethodID getHoverTapTimeout;
95 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070096 jmethodID getDoubleTapTimeout;
97 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080098 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080099 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700100 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700101 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800102 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -0700103} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700104
105static struct {
106 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700107} gInputDeviceClassInfo;
108
109static struct {
110 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700111} gKeyEventClassInfo;
112
113static struct {
114 jclass clazz;
115} gMotionEventClassInfo;
116
RoboErikfb290df2013-12-16 11:27:55 -0800117static struct {
118 jclass clazz;
119 jmethodID constructor;
120} gInputDeviceIdentifierInfo;
121
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800122static struct {
123 jclass clazz;
124 jmethodID getAffineTransform;
125} gTouchCalibrationClassInfo;
126
RoboErikfb290df2013-12-16 11:27:55 -0800127
Jeff Brown928e0542011-01-10 11:17:36 -0800128
129// --- Global functions ---
130
Jeff Brown214eaf42011-05-26 19:17:02 -0700131template<typename T>
132inline static T min(const T& a, const T& b) {
133 return a < b ? a : b;
134}
135
136template<typename T>
137inline static T max(const T& a, const T& b) {
138 return a > b ? a : b;
139}
140
Michael Wrighta4051212015-07-23 17:04:40 +0100141static inline const char* toString(bool value) {
142 return value ? "true" : "false";
143}
144
Jeff Brown928e0542011-01-10 11:17:36 -0800145static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
146 const sp<InputApplicationHandle>& inputApplicationHandle) {
147 if (inputApplicationHandle == NULL) {
148 return NULL;
149 }
150 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
151 getInputApplicationHandleObjLocalRef(env);
152}
153
154static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
155 const sp<InputWindowHandle>& inputWindowHandle) {
156 if (inputWindowHandle == NULL) {
157 return NULL;
158 }
159 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
160 getInputWindowHandleObjLocalRef(env);
161}
162
Jun Mukai808196f2015-10-28 16:46:44 -0700163static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
164 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700165 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700166 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700167 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400168 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
169 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
170 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
171 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
172 bitmapCopy->rowBytes(), 0, 0);
173 }
Jun Mukai808196f2015-10-28 16:46:44 -0700174 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
175 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700176 }
177}
178
Jun Mukai808196f2015-10-28 16:46:44 -0700179static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
180 SpriteIcon* outSpriteIcon) {
181 PointerIcon pointerIcon;
182 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
183}
184
Jeff Brown905805a2011-10-12 13:57:59 -0700185enum {
186 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700187};
188
Jeff Brown928e0542011-01-10 11:17:36 -0800189
190// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800191
Jeff Brown9c3cda02010-06-15 01:31:58 -0700192class NativeInputManager : public virtual RefBase,
193 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700194 public virtual InputDispatcherPolicyInterface,
195 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700196protected:
197 virtual ~NativeInputManager();
198
199public:
Jeff Brown4532e612012-04-05 14:27:12 -0700200 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700201
202 inline sp<InputManager> getInputManager() const { return mInputManager; }
203
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800204 void dump(std::string& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700205
Santos Cordonee8931e2017-04-05 10:31:15 -0700206 void setVirtualDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
207 void setDisplayViewport(int32_t viewportType, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700208
Jeff Brown7fbdc842010-06-17 20:52:56 -0700209 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800210 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700211 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
212
Arthur Hungb37e35e2018-07-30 15:32:11 +0800213 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray, int displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700214 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700215 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800216 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700217 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700218 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700219 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700220 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800221 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100222 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800223 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700224 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800225 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700226
Jeff Brown9c3cda02010-06-15 01:31:58 -0700227 /* --- InputReaderPolicyInterface implementation --- */
228
Jeff Brown214eaf42011-05-26 19:17:02 -0700229 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800230 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700231 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800232 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700233 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700234 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
235 jfloatArray matrixArr);
236 virtual TouchAffineTransformation getTouchAffineTransformation(
237 const String8& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700238
239 /* --- InputDispatcherPolicyInterface implementation --- */
240
Jeff Brownbcc046a2012-09-27 20:46:43 -0700241 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700242 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700243 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700244 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700245 const sp<InputWindowHandle>& inputWindowHandle,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800246 const std::string& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800247 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700248 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700249 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800250 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800251 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700252 virtual nsecs_t interceptKeyBeforeDispatching(
253 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700254 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800255 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800256 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700257 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700258 virtual bool checkInjectEventsPermissionNonReentrant(
259 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700260
Jeff Brown2352b972011-04-12 22:39:53 -0700261 /* --- PointerControllerPolicyInterface implementation --- */
262
Jun Mukai19a56012015-11-24 11:25:52 -0800263 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700264 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700265 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
266 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900267 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700268 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700269
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270private:
271 sp<InputManager> mInputManager;
272
Jeff Brown2352b972011-04-12 22:39:53 -0700273 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700274 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800275 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700276
Jeff Brown83c09682010-12-23 17:50:18 -0800277 Mutex mLock;
278 struct Locked {
279 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700280 DisplayViewport internalViewport;
281 DisplayViewport externalViewport;
Santos Cordonee8931e2017-04-05 10:31:15 -0700282 Vector<DisplayViewport> virtualViewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800283
Jeff Brown05dc66a2011-03-02 14:41:58 -0800284 // System UI visibility.
285 int32_t systemUiVisibility;
286
Jeff Brown1a84fd12011-06-02 01:26:32 -0700287 // Pointer speed.
288 int32_t pointerSpeed;
289
Jeff Brown474dcb52011-06-14 20:22:50 -0700290 // True if pointer gestures are enabled.
291 bool pointerGesturesEnabled;
292
Jeff Browndaf4a122011-08-26 17:14:14 -0700293 // Show touches feature enable/disable.
294 bool showTouches;
295
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800296 // Pointer capture feature enable/disable.
297 bool pointerCapture;
298
Jeff Brown5541de92011-04-11 11:54:25 -0700299 // Sprite controller singleton, created on first use.
300 sp<SpriteController> spriteController;
301
Jeff Brown83c09682010-12-23 17:50:18 -0800302 // Pointer controller singleton, created and destroyed as needed.
303 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700304
305 // Input devices to be disabled
306 SortedVector<int32_t> disabledInputDevices;
Jeff Brown83c09682010-12-23 17:50:18 -0800307 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700308
Michael Wrighta4051212015-07-23 17:04:40 +0100309 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700310
Jeff Brown2352b972011-04-12 22:39:53 -0700311 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800312 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700313 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800314
Jeff Brownb88102f2010-09-08 11:49:43 -0700315 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700316
Jeff Brown9c3cda02010-06-15 01:31:58 -0700317 static inline JNIEnv* jniEnv() {
318 return AndroidRuntime::getJNIEnv();
319 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320};
321
Jeff Brown928e0542011-01-10 11:17:36 -0800322
Jeff Brown9c3cda02010-06-15 01:31:58 -0700323
Jeff Brown2352b972011-04-12 22:39:53 -0700324NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700325 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700326 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700327 JNIEnv* env = jniEnv();
328
Jeff Brown2352b972011-04-12 22:39:53 -0700329 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700330 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700331
Jeff Brown83c09682010-12-23 17:50:18 -0800332 {
333 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800334 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700335 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700336 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700337 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800338 mLocked.pointerCapture = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800339 }
Michael Wrighta4051212015-07-23 17:04:40 +0100340 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800341
Jeff Brown9c3cda02010-06-15 01:31:58 -0700342 sp<EventHub> eventHub = new EventHub();
343 mInputManager = new InputManager(eventHub, this, this);
344}
345
346NativeInputManager::~NativeInputManager() {
347 JNIEnv* env = jniEnv();
348
Jeff Brown2352b972011-04-12 22:39:53 -0700349 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700350 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700351}
352
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800353void NativeInputManager::dump(std::string& dump) {
354 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100355 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800356 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100357 }
358 {
359 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800360 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100361 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800362 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
363 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100364 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800365 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
366 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100367 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800368 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100369
Jeff Brownb88102f2010-09-08 11:49:43 -0700370 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800371 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700372
Jeff Brownb88102f2010-09-08 11:49:43 -0700373 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800374 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700375}
376
Jeff Brown7fbdc842010-06-17 20:52:56 -0700377bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700378 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000379 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700380 LOGE_EX(env);
381 env->ExceptionClear();
382 return true;
383 }
384 return false;
385}
386
Santos Cordonee8931e2017-04-05 10:31:15 -0700387void NativeInputManager::setVirtualDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
388 Vector<DisplayViewport> viewports;
389
390 if (viewportObjArray) {
391 jsize length = env->GetArrayLength(viewportObjArray);
392 for (jsize i = 0; i < length; i++) {
393 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
394 if (! viewportObj) {
395 break; // found null element indicating end of used portion of the array
396 }
397
398 DisplayViewport viewport;
399 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
400 ALOGI("Viewport [%d] to add: %s", (int) length, viewport.uniqueId.c_str());
401 viewports.push(viewport);
402
403 env->DeleteLocalRef(viewportObj);
404 }
405 }
406
407 {
408 AutoMutex _l(mLock);
409 mLocked.virtualViewports = viewports;
410 }
411
412 mInputManager->getReader()->requestRefreshConfiguration(
413 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
414}
415
416void NativeInputManager::setDisplayViewport(int32_t type, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700417 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700418 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700419 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700420
Santos Cordonee8931e2017-04-05 10:31:15 -0700421 ViewportType viewportType = static_cast<ViewportType>(type);
422 DisplayViewport* v = NULL;
423 if (viewportType == ViewportType::VIEWPORT_EXTERNAL) {
424 v = &mLocked.externalViewport;
425 } else if (viewportType == ViewportType::VIEWPORT_INTERNAL) {
426 v = &mLocked.internalViewport;
427 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700428
Santos Cordonee8931e2017-04-05 10:31:15 -0700429 if (v != NULL && *v != viewport) {
430 changed = true;
431 *v = viewport;
432
433 if (viewportType == ViewportType::VIEWPORT_INTERNAL) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700434 sp<PointerController> controller = mLocked.pointerController.promote();
435 if (controller != NULL) {
436 controller->setDisplayViewport(
437 viewport.logicalRight - viewport.logicalLeft,
438 viewport.logicalBottom - viewport.logicalTop,
439 viewport.orientation);
440 }
Jeff Brown2352b972011-04-12 22:39:53 -0700441 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700442 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700443 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700444
445 if (changed) {
446 mInputManager->getReader()->requestRefreshConfiguration(
447 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
448 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700449}
450
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700451status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Jeff Brown928e0542011-01-10 11:17:36 -0800452 const sp<InputChannel>& inputChannel,
453 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100454 ATRACE_CALL();
Jeff Brown928e0542011-01-10 11:17:36 -0800455 return mInputManager->getDispatcher()->registerInputChannel(
456 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700457}
458
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700459status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700460 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100461 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700462 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700463}
464
Jeff Brown214eaf42011-05-26 19:17:02 -0700465void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100466 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700467 JNIEnv* env = jniEnv();
468
Jeff Brown4532e612012-04-05 14:27:12 -0700469 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
470 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700471 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
472 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
473 }
474
475 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700476 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
477 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700478 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
479 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700480 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700481 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700482 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700483 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700484 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700485 env->DeleteLocalRef(item);
486 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700487 env->DeleteLocalRef(excludedDeviceNames);
488 }
489
Jeff Brown4532e612012-04-05 14:27:12 -0700490 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
491 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700492 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700493 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
494 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700495 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700496 jint longPressTimeout = env->CallIntMethod(mServiceObj,
497 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700498 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700499 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700500
501 // We must ensure that the tap-drag interval is significantly shorter than
502 // the long-press timeout because the tap is held down for the entire duration
503 // of the double-tap timeout.
504 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700505 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700506 outConfig->pointerGestureTapDragInterval =
507 milliseconds_to_nanoseconds(tapDragInterval);
508 }
509 }
510 }
511
Jeff Brown4532e612012-04-05 14:27:12 -0700512 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
513 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700514 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
515 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700516 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700517
518 { // acquire lock
519 AutoMutex _l(mLock);
520
521 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
522 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700523 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700524
Jeff Browndaf4a122011-08-26 17:14:14 -0700525 outConfig->showTouches = mLocked.showTouches;
526
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800527 outConfig->pointerCapture = mLocked.pointerCapture;
528
Santos Cordonee8931e2017-04-05 10:31:15 -0700529 outConfig->setPhysicalDisplayViewport(ViewportType::VIEWPORT_INTERNAL,
530 mLocked.internalViewport);
531 outConfig->setPhysicalDisplayViewport(ViewportType::VIEWPORT_EXTERNAL,
532 mLocked.externalViewport);
533 outConfig->setVirtualDisplayViewports(mLocked.virtualViewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700534
535 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700536 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700537}
538
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700539sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100540 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800541 AutoMutex _l(mLock);
542
543 sp<PointerController> controller = mLocked.pointerController.promote();
544 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700545 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800546
Jeff Brown2352b972011-04-12 22:39:53 -0700547 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800548 mLocked.pointerController = controller;
549
Jeff Brownd728bf52012-09-08 18:05:28 -0700550 DisplayViewport& v = mLocked.internalViewport;
551 controller->setDisplayViewport(
552 v.logicalRight - v.logicalLeft,
553 v.logicalBottom - v.logicalTop,
554 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800555
Jeff Brown2352b972011-04-12 22:39:53 -0700556 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800557 }
558 return controller;
559}
560
Jeff Brown5541de92011-04-11 11:54:25 -0700561void NativeInputManager::ensureSpriteControllerLocked() {
562 if (mLocked.spriteController == NULL) {
563 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700564 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700565 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
566 layer = -1;
567 }
568 mLocked.spriteController = new SpriteController(mLooper, layer);
569 }
570}
571
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700572void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100573 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700574 JNIEnv* env = jniEnv();
575
576 size_t count = inputDevices.size();
577 jobjectArray inputDevicesObjArray = env->NewObjectArray(
578 count, gInputDeviceClassInfo.clazz, NULL);
579 if (inputDevicesObjArray) {
580 bool error = false;
581 for (size_t i = 0; i < count; i++) {
582 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
583 if (!inputDeviceObj) {
584 error = true;
585 break;
586 }
587
588 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
589 env->DeleteLocalRef(inputDeviceObj);
590 }
591
592 if (!error) {
593 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
594 inputDevicesObjArray);
595 }
596
597 env->DeleteLocalRef(inputDevicesObjArray);
598 }
599
600 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
601}
602
Jeff Brown6ec6f792012-04-17 16:52:41 -0700603sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800604 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100605 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700606 JNIEnv* env = jniEnv();
607
608 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800609 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
610 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
611 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
612 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700613 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800614 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700615 if (arrayObj.get()) {
616 ScopedLocalRef<jstring> filenameObj(env,
617 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
618 ScopedLocalRef<jstring> contentsObj(env,
619 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
620 ScopedUtfChars filenameChars(env, filenameObj.get());
621 ScopedUtfChars contentsChars(env, contentsObj.get());
622
623 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
624 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
625 }
626 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
627 return result;
628}
629
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700630String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100631 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700632 JNIEnv* env = jniEnv();
633
634 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
635 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
636 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
637 String8 result;
638 if (aliasObj.get()) {
639 ScopedUtfChars aliasChars(env, aliasObj.get());
640 result.setTo(aliasChars.c_str());
641 }
642 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
643 return result;
644}
645
Jeff Brownbcc046a2012-09-27 20:46:43 -0700646void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700647 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700648#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700649 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
650 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700651#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100652 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700653
654 JNIEnv* env = jniEnv();
655
Jeff Brown53384282012-08-20 20:16:01 -0700656 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700657 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700658 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700659}
660
Jeff Brown9c3cda02010-06-15 01:31:58 -0700661void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
662#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000663 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700664#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100665 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700666
667 JNIEnv* env = jniEnv();
668
Jeff Brown4532e612012-04-05 14:27:12 -0700669 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700670 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700671}
672
Jeff Brown519e0242010-09-15 15:18:56 -0700673nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800674 const sp<InputWindowHandle>& inputWindowHandle, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700675#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000676 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700677#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100678 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700679
680 JNIEnv* env = jniEnv();
681
Jeff Brown928e0542011-01-10 11:17:36 -0800682 jobject inputApplicationHandleObj =
683 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
684 jobject inputWindowHandleObj =
685 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800686 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700687
Jeff Brown4532e612012-04-05 14:27:12 -0700688 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700689 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
690 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700691 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
692 newTimeout = 0; // abort dispatch
693 } else {
694 assert(newTimeout >= 0);
695 }
696
Jeff Brownbd181bb2013-09-10 16:44:24 -0700697 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800698 env->DeleteLocalRef(inputWindowHandleObj);
699 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700700 return newTimeout;
701}
702
Jeff Brown928e0542011-01-10 11:17:36 -0800703void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700704#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000705 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700706#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100707 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700708
Jeff Brown7fbdc842010-06-17 20:52:56 -0700709 JNIEnv* env = jniEnv();
710
Jeff Brown928e0542011-01-10 11:17:36 -0800711 jobject inputWindowHandleObj =
712 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
713 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700714 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800715 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700716 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
717
Jeff Brown928e0542011-01-10 11:17:36 -0800718 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700719 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700720}
721
Jeff Brown214eaf42011-05-26 19:17:02 -0700722void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100723 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700724 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800725
Jeff Brown4532e612012-04-05 14:27:12 -0700726 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
727 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700728 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
729 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
730 }
Jeff Browna4547672011-03-02 21:38:11 -0800731
Jeff Brown4532e612012-04-05 14:27:12 -0700732 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
733 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700734 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
735 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
736 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700737}
738
Arthur Hungb37e35e2018-07-30 15:32:11 +0800739void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
740 int displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700741 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700742
Jeff Brown9302c872011-07-13 22:51:29 -0700743 if (windowHandleObjArray) {
744 jsize length = env->GetArrayLength(windowHandleObjArray);
745 for (jsize i = 0; i < length; i++) {
746 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
747 if (! windowHandleObj) {
748 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700749 }
Jeff Brown9302c872011-07-13 22:51:29 -0700750
751 sp<InputWindowHandle> windowHandle =
752 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
753 if (windowHandle != NULL) {
754 windowHandles.push(windowHandle);
755 }
756 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700757 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700758 }
Jeff Brown349703e2010-06-22 01:27:15 -0700759
Arthur Hungb37e35e2018-07-30 15:32:11 +0800760 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700761
762 // Do this after the dispatcher has updated the window handle state.
763 bool newPointerGesturesEnabled = true;
764 size_t numWindows = windowHandles.size();
765 for (size_t i = 0; i < numWindows; i++) {
766 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700767 const InputWindowInfo* windowInfo = windowHandle->getInfo();
768 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
769 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700770 newPointerGesturesEnabled = false;
771 }
772 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700773
774 uint32_t changes = 0;
775 { // acquire lock
776 AutoMutex _l(mLock);
777
778 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
779 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
780 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
781 }
782 } // release lock
783
784 if (changes) {
785 mInputManager->getReader()->requestRefreshConfiguration(changes);
786 }
Jeff Brown349703e2010-06-22 01:27:15 -0700787}
788
Jeff Brown9302c872011-07-13 22:51:29 -0700789void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
790 sp<InputApplicationHandle> applicationHandle =
791 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
792 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700793}
794
795void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700796 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700797}
798
Jeff Brown05dc66a2011-03-02 14:41:58 -0800799void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
800 AutoMutex _l(mLock);
801
802 if (mLocked.systemUiVisibility != visibility) {
803 mLocked.systemUiVisibility = visibility;
804
805 sp<PointerController> controller = mLocked.pointerController.promote();
806 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700807 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800808 }
809 }
810}
811
Jeff Brown2352b972011-04-12 22:39:53 -0700812void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800813 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700814 controller->setInactivityTimeout(lightsOut
815 ? PointerController::INACTIVITY_TIMEOUT_SHORT
816 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800817}
818
Jeff Brown1a84fd12011-06-02 01:26:32 -0700819void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700820 { // acquire lock
821 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700822
Jeff Brown474dcb52011-06-14 20:22:50 -0700823 if (mLocked.pointerSpeed == speed) {
824 return;
825 }
826
Steve Block6215d3f2012-01-04 20:05:49 +0000827 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700828 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700829 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700830
Jeff Brown474dcb52011-06-14 20:22:50 -0700831 mInputManager->getReader()->requestRefreshConfiguration(
832 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700833}
834
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700835void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
836 { // acquire lock
837 AutoMutex _l(mLock);
838
839 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
840 bool currentlyEnabled = index < 0;
841 if (!enabled && currentlyEnabled) {
842 mLocked.disabledInputDevices.add(deviceId);
843 }
844 if (enabled && !currentlyEnabled) {
845 mLocked.disabledInputDevices.remove(deviceId);
846 }
847 } // release lock
848
849 mInputManager->getReader()->requestRefreshConfiguration(
850 InputReaderConfiguration::CHANGE_ENABLED_STATE);
851}
852
Jeff Browndaf4a122011-08-26 17:14:14 -0700853void NativeInputManager::setShowTouches(bool enabled) {
854 { // acquire lock
855 AutoMutex _l(mLock);
856
857 if (mLocked.showTouches == enabled) {
858 return;
859 }
860
Steve Block6215d3f2012-01-04 20:05:49 +0000861 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700862 mLocked.showTouches = enabled;
863 } // release lock
864
865 mInputManager->getReader()->requestRefreshConfiguration(
866 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
867}
868
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800869void NativeInputManager::setPointerCapture(bool enabled) {
870 { // acquire lock
871 AutoMutex _l(mLock);
872
873 if (mLocked.pointerCapture == enabled) {
874 return;
875 }
876
877 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
878 mLocked.pointerCapture = enabled;
879 } // release lock
880
881 mInputManager->getReader()->requestRefreshConfiguration(
882 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
883}
884
Jeff Brown037c33e2014-04-09 00:31:55 -0700885void NativeInputManager::setInteractive(bool interactive) {
886 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700887}
888
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800889void NativeInputManager::reloadCalibration() {
890 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100891 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800892}
893
Michael Wrighte051f6f2016-05-13 17:44:16 +0100894void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800895 AutoMutex _l(mLock);
896 sp<PointerController> controller = mLocked.pointerController.promote();
897 if (controller != NULL) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100898 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800899 }
900}
901
902void NativeInputManager::reloadPointerIcons() {
903 AutoMutex _l(mLock);
904 sp<PointerController> controller = mLocked.pointerController.promote();
905 if (controller != NULL) {
906 controller->reloadPointerResources();
907 }
Jun Mukai1db53972015-09-11 18:08:31 -0700908}
909
Jun Mukaid4eaef72015-10-30 15:54:33 -0700910void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
911 AutoMutex _l(mLock);
912 sp<PointerController> controller = mLocked.pointerController.promote();
913 if (controller != NULL) {
914 controller->setCustomPointerIcon(icon);
915 }
916}
917
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800918TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
919 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100920 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800921 ScopedFloatArrayRO matrix(env, matrixArr);
922 assert(matrix.size() == 6);
923
924 TouchAffineTransformation transform;
925 transform.x_scale = matrix[0];
926 transform.x_ymix = matrix[1];
927 transform.x_offset = matrix[2];
928 transform.y_xmix = matrix[3];
929 transform.y_scale = matrix[4];
930 transform.y_offset = matrix[5];
931
932 return transform;
933}
934
935TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700936 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800937 JNIEnv* env = jniEnv();
938
939 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
940
941 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700942 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
943 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800944
945 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
946 gTouchCalibrationClassInfo.getAffineTransform));
947
948 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
949
950 env->DeleteLocalRef(matrixArr);
951 env->DeleteLocalRef(cal);
952
953 return transform;
954}
955
Jeff Brown0029c662011-03-30 02:25:18 -0700956bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100957 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700958 jobject inputEventObj;
959
960 JNIEnv* env = jniEnv();
961 switch (inputEvent->getType()) {
962 case AINPUT_EVENT_TYPE_KEY:
963 inputEventObj = android_view_KeyEvent_fromNative(env,
964 static_cast<const KeyEvent*>(inputEvent));
965 break;
966 case AINPUT_EVENT_TYPE_MOTION:
967 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
968 static_cast<const MotionEvent*>(inputEvent));
969 break;
970 default:
971 return true; // dispatch the event normally
972 }
973
974 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000975 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700976 return true; // dispatch the event normally
977 }
978
979 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700980 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700981 inputEventObj, policyFlags);
982 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
983 pass = true;
984 }
985 env->DeleteLocalRef(inputEventObj);
986 return pass;
987}
988
Jeff Brown1f245102010-11-18 20:53:46 -0800989void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
990 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100991 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700992 // Policy:
993 // - Ignore untrusted events and pass them along.
994 // - Ask the window manager what to do with normal events and trusted injected events.
995 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100996 bool interactive = mInteractive.load();
997 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700998 policyFlags |= POLICY_FLAG_INTERACTIVE;
999 }
Jeff Brown3122e442010-10-11 23:32:49 -07001000 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -08001001 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -07001002 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -08001003 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1004 jint wmActions;
1005 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001006 wmActions = env->CallIntMethod(mServiceObj,
1007 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -07001008 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001009 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
1010 wmActions = 0;
1011 }
1012 android_view_KeyEvent_recycle(env, keyEventObj);
1013 env->DeleteLocalRef(keyEventObj);
1014 } else {
Steve Block3762c312012-01-06 19:20:56 +00001015 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -07001016 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -07001017 }
1018
Jeff Brown56194eb2011-03-02 19:23:13 -08001019 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -07001020 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001021 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001022 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1023 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001024 }
1025}
1026
Jeff Brown56194eb2011-03-02 19:23:13 -08001027void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001028 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001029 // Policy:
1030 // - Ignore untrusted events and pass them along.
1031 // - No special filtering for injected events required at this time.
1032 // - Filter normal events based on screen state.
1033 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001034 bool interactive = mInteractive.load();
1035 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001036 policyFlags |= POLICY_FLAG_INTERACTIVE;
1037 }
Jeff Brown3122e442010-10-11 23:32:49 -07001038 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001039 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001040 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001041 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001042 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001043 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001044 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001045 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001046 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001047 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001048 wmActions = 0;
1049 }
1050
Jeff Brown56194eb2011-03-02 19:23:13 -08001051 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001052 }
Jeff Brown3122e442010-10-11 23:32:49 -07001053 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001054 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001055 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1056 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001057 }
1058}
1059
Jeff Brown56194eb2011-03-02 19:23:13 -08001060void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1061 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001062 if (wmActions & WM_ACTION_PASS_TO_USER) {
1063 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1064 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001065#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001066 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001067#endif
1068 }
1069}
1070
Jeff Brown905805a2011-10-12 13:57:59 -07001071nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -08001072 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -07001073 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001074 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001075 // Policy:
1076 // - Ignore untrusted events and pass them along.
1077 // - Filter normal events and trusted injected events through the window manager policy to
1078 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001079 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001080 if (policyFlags & POLICY_FLAG_TRUSTED) {
1081 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001082
Jeff Brown928e0542011-01-10 11:17:36 -08001083 // Note: inputWindowHandle may be null.
1084 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001085 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1086 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001087 jlong delayMillis = env->CallLongMethod(mServiceObj,
1088 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -08001089 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001090 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1091 android_view_KeyEvent_recycle(env, keyEventObj);
1092 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001093 if (!error) {
1094 if (delayMillis < 0) {
1095 result = -1;
1096 } else if (delayMillis > 0) {
1097 result = milliseconds_to_nanoseconds(delayMillis);
1098 }
1099 }
Jeff Brown1f245102010-11-18 20:53:46 -08001100 } else {
Steve Block3762c312012-01-06 19:20:56 +00001101 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001102 }
Jeff Brown928e0542011-01-10 11:17:36 -08001103 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -07001104 }
Jeff Brown1f245102010-11-18 20:53:46 -08001105 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001106}
1107
Jeff Brown928e0542011-01-10 11:17:36 -08001108bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001109 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001110 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001111 // Policy:
1112 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001113 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001114 if (policyFlags & POLICY_FLAG_TRUSTED) {
1115 JNIEnv* env = jniEnv();
1116
Jeff Brown928e0542011-01-10 11:17:36 -08001117 // Note: inputWindowHandle may be null.
1118 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001119 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1120 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001121 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1122 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -08001123 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001124 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
1125 fallbackKeyEventObj = NULL;
1126 }
Jeff Brown1f245102010-11-18 20:53:46 -08001127 android_view_KeyEvent_recycle(env, keyEventObj);
1128 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001129
1130 if (fallbackKeyEventObj) {
1131 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1132 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1133 outFallbackKeyEvent)) {
1134 result = true;
1135 }
1136 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1137 env->DeleteLocalRef(fallbackKeyEventObj);
1138 }
Jeff Brown1f245102010-11-18 20:53:46 -08001139 } else {
Steve Block3762c312012-01-06 19:20:56 +00001140 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001141 }
Jeff Brown928e0542011-01-10 11:17:36 -08001142 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001143 }
Jeff Brown1f245102010-11-18 20:53:46 -08001144 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001145}
1146
Jeff Brown01ce2e92010-09-26 22:20:12 -07001147void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001148 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001149 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001150}
1151
Jeff Brown349703e2010-06-22 01:27:15 -07001152
Jeff Brownb88102f2010-09-08 11:49:43 -07001153bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1154 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001155 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001156 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001157 jboolean result = env->CallBooleanMethod(mServiceObj,
1158 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001159 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1160 result = false;
1161 }
Jeff Brown349703e2010-06-22 01:27:15 -07001162 return result;
1163}
1164
Jun Mukai19a56012015-11-24 11:25:52 -08001165void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001166 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001167 JNIEnv* env = jniEnv();
1168
1169 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1170 mServiceObj, gServiceClassInfo.getPointerIcon));
1171 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1172 return;
1173 }
1174
1175 PointerIcon pointerIcon;
1176 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1177 mContextObj, &pointerIcon);
1178 if (!status && !pointerIcon.isNullIcon()) {
1179 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1180 } else {
1181 *icon = SpriteIcon();
1182 }
1183}
1184
Jeff Brown2352b972011-04-12 22:39:53 -07001185void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001186 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001187 JNIEnv* env = jniEnv();
1188
1189 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1190 &outResources->spotHover);
1191 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1192 &outResources->spotTouch);
1193 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1194 &outResources->spotAnchor);
1195}
1196
Jun Mukai808196f2015-10-28 16:46:44 -07001197void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1198 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001199 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001200 JNIEnv* env = jniEnv();
1201
1202 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1203 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001204 PointerIcon pointerIcon;
1205 loadSystemIconAsSpriteWithPointerIcon(
1206 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1207 if (!pointerIcon.bitmapFrames.empty()) {
1208 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1209 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1210 animationData.durationPerFrame =
1211 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1212 animationData.animationFrames.reserve(numFrames);
1213 animationData.animationFrames.push_back(SpriteIcon(
1214 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1215 for (size_t i = 0; i < numFrames - 1; ++i) {
1216 animationData.animationFrames.push_back(SpriteIcon(
1217 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1218 }
1219 }
Jun Mukai1db53972015-09-11 18:08:31 -07001220 }
Jun Mukai808196f2015-10-28 16:46:44 -07001221 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1222 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001223}
1224
Jun Mukai5ec74202015-10-07 16:58:09 +09001225int32_t NativeInputManager::getDefaultPointerIconId() {
1226 return POINTER_ICON_STYLE_ARROW;
1227}
Jeff Brown83c09682010-12-23 17:50:18 -08001228
Jun Mukaid4eaef72015-10-30 15:54:33 -07001229int32_t NativeInputManager::getCustomPointerIconId() {
1230 return POINTER_ICON_STYLE_CUSTOM;
1231}
1232
Jeff Brown9c3cda02010-06-15 01:31:58 -07001233// ----------------------------------------------------------------------------
1234
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001235static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001236 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001237 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001238 if (messageQueue == NULL) {
1239 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1240 return 0;
1241 }
1242
Jeff Brown603b4452012-04-06 17:39:41 -07001243 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1244 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001245 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001246 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001247}
1248
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001249static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001250 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001251
Jeff Brown4532e612012-04-05 14:27:12 -07001252 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001253 if (result) {
1254 jniThrowRuntimeException(env, "Input manager could not be started.");
1255 }
1256}
1257
Santos Cordonee8931e2017-04-05 10:31:15 -07001258static void nativeSetVirtualDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
1259 jobjectArray viewportObjArray) {
1260 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1261 im->setVirtualDisplayViewports(env, viewportObjArray);
1262}
1263
1264static void nativeSetDisplayViewport(JNIEnv* env, jclass /* clazz */, jlong ptr,
1265 jint viewportType, jint displayId, jint orientation,
Jeff Brownd728bf52012-09-08 18:05:28 -07001266 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001267 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
Santos Cordonee8931e2017-04-05 10:31:15 -07001268 jint deviceWidth, jint deviceHeight, jstring uniqueId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001269 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001270
Jeff Brownd728bf52012-09-08 18:05:28 -07001271 DisplayViewport v;
1272 v.displayId = displayId;
1273 v.orientation = orientation;
1274 v.logicalLeft = logicalLeft;
1275 v.logicalTop = logicalTop;
1276 v.logicalRight = logicalRight;
1277 v.logicalBottom = logicalBottom;
1278 v.physicalLeft = physicalLeft;
1279 v.physicalTop = physicalTop;
1280 v.physicalRight = physicalRight;
1281 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001282 v.deviceWidth = deviceWidth;
1283 v.deviceHeight = deviceHeight;
Santos Cordonee8931e2017-04-05 10:31:15 -07001284 if (uniqueId != nullptr) {
1285 v.uniqueId.setTo(ScopedUtfChars(env, uniqueId).c_str());
1286 }
1287
1288 im->setDisplayViewport(viewportType, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001289}
1290
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001291static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001292 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001293 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001294
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001295 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001296 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001297}
1298
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001299static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001300 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001301 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001302
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001303 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001304 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001305}
1306
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001307static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001308 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001309 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001310
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001311 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001312 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001313}
1314
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001315static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001316 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001317 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001318
1319 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1320 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1321 jsize numCodes = env->GetArrayLength(keyCodes);
1322 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001323 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001324 if (im->getInputManager()->getReader()->hasKeys(
1325 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1326 result = JNI_TRUE;
1327 } else {
1328 result = JNI_FALSE;
1329 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001330 } else {
1331 result = JNI_FALSE;
1332 }
1333
1334 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1335 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1336 return result;
1337}
1338
1339static void throwInputChannelNotInitialized(JNIEnv* env) {
1340 jniThrowException(env, "java/lang/IllegalStateException",
1341 "inputChannel is not initialized");
1342}
1343
Jeff Brown4532e612012-04-05 14:27:12 -07001344static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001345 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001346 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1347
Steve Block8564c8d2012-01-05 23:22:43 +00001348 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001349 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001350 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001351}
1352
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001353static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001354 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001355 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001356
1357 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1358 inputChannelObj);
1359 if (inputChannel == NULL) {
1360 throwInputChannelNotInitialized(env);
1361 return;
1362 }
1363
Jeff Brown928e0542011-01-10 11:17:36 -08001364 sp<InputWindowHandle> inputWindowHandle =
1365 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001366
Jeff Brown4532e612012-04-05 14:27:12 -07001367 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001368 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001369 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001370 std::string message;
1371 message += StringPrintf("Failed to register input channel. status=%d", status);
1372 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001373 return;
1374 }
1375
Jeff Browna41ca772010-08-11 14:46:32 -07001376 if (! monitor) {
1377 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001378 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001379 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001380}
1381
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001382static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001383 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001384 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001385
1386 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1387 inputChannelObj);
1388 if (inputChannel == NULL) {
1389 throwInputChannelNotInitialized(env);
1390 return;
1391 }
1392
1393 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1394
Jeff Brown4532e612012-04-05 14:27:12 -07001395 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001396 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001397 std::string message;
1398 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1399 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001400 }
1401}
1402
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001403static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001404 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001405 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001406
Jeff Brown4532e612012-04-05 14:27:12 -07001407 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001408}
1409
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001410static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001411 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001412 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001413 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001414
Jeff Brown6ec402b2010-07-28 15:48:59 -07001415 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1416 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001417 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1418 if (status) {
1419 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1420 return INPUT_EVENT_INJECTION_FAILED;
1421 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001422
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001423 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001424 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001425 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001426 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001427 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1428 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001429 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1430 return INPUT_EVENT_INJECTION_FAILED;
1431 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001432
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001433 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001434 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001435 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001436 } else {
1437 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001438 return INPUT_EVENT_INJECTION_FAILED;
1439 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001440}
1441
Andrii Kulian112d0562016-03-08 10:44:22 -08001442static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1443 jlong ptr, jint deviceId) {
1444 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001445
Andrii Kulian112d0562016-03-08 10:44:22 -08001446 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1447}
1448
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001449static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hungb37e35e2018-07-30 15:32:11 +08001450 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001451 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001452
Arthur Hungb37e35e2018-07-30 15:32:11 +08001453 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001454}
1455
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001456static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001457 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001458 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001459
Jeff Brown4532e612012-04-05 14:27:12 -07001460 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001461}
1462
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001463static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1464 jboolean enabled) {
1465 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001466
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001467 im->setPointerCapture(enabled);
1468}
1469
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001470static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1471 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001472 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001473
Jeff Brown4532e612012-04-05 14:27:12 -07001474 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001475}
1476
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001477static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1478 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001479 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001480
Jeff Brown4532e612012-04-05 14:27:12 -07001481 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001482}
1483
Jeff Brown4532e612012-04-05 14:27:12 -07001484static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001485 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001486 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001487
1488 sp<InputChannel> fromChannel =
1489 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1490 sp<InputChannel> toChannel =
1491 android_view_InputChannel_getInputChannel(env, toChannelObj);
1492
1493 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001494 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001495 }
1496
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001497 if (im->getInputManager()->getDispatcher()->
1498 transferTouchFocus(fromChannel, toChannel)) {
1499 return JNI_TRUE;
1500 } else {
1501 return JNI_FALSE;
1502 }
Jeff Browne6504122010-09-27 14:52:15 -07001503}
1504
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001505static void nativeSetPointerSpeed(JNIEnv* /* env */,
1506 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001507 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001508
Jeff Brown4532e612012-04-05 14:27:12 -07001509 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001510}
1511
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001512static void nativeSetShowTouches(JNIEnv* /* env */,
1513 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001514 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001515
Jeff Brown4532e612012-04-05 14:27:12 -07001516 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001517}
1518
Jeff Brown037c33e2014-04-09 00:31:55 -07001519static void nativeSetInteractive(JNIEnv* env,
1520 jclass clazz, jlong ptr, jboolean interactive) {
1521 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1522
1523 im->setInteractive(interactive);
1524}
1525
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001526static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1527 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001528
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001529 im->reloadCalibration();
1530}
1531
Jeff Browna47425a2012-04-13 04:09:27 -07001532static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001533 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001534 jint repeat, jint token) {
1535 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1536
1537 size_t patternSize = env->GetArrayLength(patternObj);
1538 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001539 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001540 "which is more than the maximum supported size of %d.",
1541 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1542 return; // limit to reasonable size
1543 }
1544
1545 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1546 patternObj, NULL));
1547 nsecs_t pattern[patternSize];
1548 for (size_t i = 0; i < patternSize; i++) {
1549 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001550 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001551 }
1552 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1553
1554 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1555}
1556
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001557static void nativeCancelVibrate(JNIEnv* /* env */,
1558 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001559 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1560
1561 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1562}
1563
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001564static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1565 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001566 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1567
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001568 im->getInputManager()->getReader()->requestRefreshConfiguration(
1569 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1570}
1571
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001572static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1573 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001574 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1575
1576 im->getInputManager()->getReader()->requestRefreshConfiguration(
1577 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001578}
1579
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001580static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001581 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001582
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001583 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001584 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001585 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001586}
1587
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001588static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001589 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001590
Jeff Brown4532e612012-04-05 14:27:12 -07001591 im->getInputManager()->getReader()->monitor();
1592 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001593}
1594
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001595static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1596 jclass /* clazz */, jlong ptr, jint deviceId) {
1597 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1598
1599 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1600}
1601
1602static void nativeEnableInputDevice(JNIEnv* /* env */,
1603 jclass /* clazz */, jlong ptr, jint deviceId) {
1604 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1605
1606 im->setInputDeviceEnabled(deviceId, true);
1607}
1608
1609static void nativeDisableInputDevice(JNIEnv* /* env */,
1610 jclass /* clazz */, jlong ptr, jint deviceId) {
1611 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1612
1613 im->setInputDeviceEnabled(deviceId, false);
1614}
1615
Michael Wrighte051f6f2016-05-13 17:44:16 +01001616static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001617 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001618
Michael Wrighte051f6f2016-05-13 17:44:16 +01001619 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001620}
1621
Jun Mukai19a56012015-11-24 11:25:52 -08001622static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1623 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001624
Jun Mukai19a56012015-11-24 11:25:52 -08001625 im->reloadPointerIcons();
1626}
1627
Jun Mukaid4eaef72015-10-30 15:54:33 -07001628static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1629 jlong ptr, jobject iconObj) {
1630 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1631
1632 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001633 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1634 if (result) {
1635 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1636 return;
1637 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001638
1639 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001640 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1641 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1642 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1643 spriteIcon.bitmap.rowBytes(), 0, 0);
1644 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001645 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1646 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1647 im->setCustomPointerIcon(spriteIcon);
1648}
1649
Jeff Brown9c3cda02010-06-15 01:31:58 -07001650// ----------------------------------------------------------------------------
1651
Daniel Micay76f6a862015-09-19 17:31:01 -04001652static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001653 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001654 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001655 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001656 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001657 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001658 (void*) nativeStart },
Santos Cordonee8931e2017-04-05 10:31:15 -07001659 { "nativeSetVirtualDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1660 (void*) nativeSetVirtualDisplayViewports },
1661 { "nativeSetDisplayViewport", "(JIIIIIIIIIIIIILjava/lang/String;)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001662 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001663 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001664 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001665 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001666 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001667 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001668 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001669 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001670 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001671 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001672 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001673 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001674 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001675 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001676 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001677 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001678 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001679 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001680 { "nativeToggleCapsLock", "(JI)V",
1681 (void*) nativeToggleCapsLock },
Arthur Hungb37e35e2018-07-30 15:32:11 +08001682 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001683 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001684 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001685 (void*) nativeSetFocusedApplication },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001686 { "nativeSetPointerCapture", "(JZ)V",
1687 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001688 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001689 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001690 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001691 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001692 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001693 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001694 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001695 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001696 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001697 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001698 { "nativeSetInteractive", "(JZ)V",
1699 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001700 { "nativeReloadCalibration", "(J)V",
1701 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001702 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001703 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001704 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001705 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001706 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001707 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001708 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001709 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001710 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001711 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001712 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001713 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001714 { "nativeIsInputDeviceEnabled", "(JI)Z",
1715 (void*) nativeIsInputDeviceEnabled },
1716 { "nativeEnableInputDevice", "(JI)V",
1717 (void*) nativeEnableInputDevice },
1718 { "nativeDisableInputDevice", "(JI)V",
1719 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001720 { "nativeSetPointerIconType", "(JI)V",
1721 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001722 { "nativeReloadPointerIcons", "(J)V",
1723 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001724 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1725 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001726};
1727
1728#define FIND_CLASS(var, className) \
1729 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001730 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001731
1732#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1733 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001734 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001735
1736#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1737 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001738 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001739
1740int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001741 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001742 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001743 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001744 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1745
Jeff Brown9c3cda02010-06-15 01:31:58 -07001746 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001747
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001748 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001749 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001750
Jeff Brown4532e612012-04-05 14:27:12 -07001751 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001752 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001753
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001754 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1755 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1756
Jeff Brown53384282012-08-20 20:16:01 -07001757 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1758 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001759
Jeff Brown4532e612012-04-05 14:27:12 -07001760 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1761 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001762
Jeff Brown4532e612012-04-05 14:27:12 -07001763 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001764 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001765 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001766
Jeff Brown4532e612012-04-05 14:27:12 -07001767 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001768 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1769
Jeff Brown4532e612012-04-05 14:27:12 -07001770 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001771 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001772
Michael Wright70af00a2014-09-03 19:30:20 -07001773 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1774 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001775
Jeff Brown4532e612012-04-05 14:27:12 -07001776 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001777 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001778 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001779
Jeff Brown4532e612012-04-05 14:27:12 -07001780 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001781 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001782 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001783
Jeff Brown4532e612012-04-05 14:27:12 -07001784 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001785 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001786
Jeff Brown4532e612012-04-05 14:27:12 -07001787 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001788 "getVirtualKeyQuietTimeMillis", "()I");
1789
Jeff Brown4532e612012-04-05 14:27:12 -07001790 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001791 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1792
Jeff Brown4532e612012-04-05 14:27:12 -07001793 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001794 "getKeyRepeatTimeout", "()I");
1795
Jeff Brown4532e612012-04-05 14:27:12 -07001796 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001797 "getKeyRepeatDelay", "()I");
1798
Jeff Brown4532e612012-04-05 14:27:12 -07001799 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001800 "getHoverTapTimeout", "()I");
1801
Jeff Brown4532e612012-04-05 14:27:12 -07001802 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001803 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001804
Jeff Brown4532e612012-04-05 14:27:12 -07001805 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001806 "getDoubleTapTimeout", "()I");
1807
Jeff Brown4532e612012-04-05 14:27:12 -07001808 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001809 "getLongPressTimeout", "()I");
1810
Jeff Brown4532e612012-04-05 14:27:12 -07001811 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001812 "getPointerLayer", "()I");
1813
Jeff Brown4532e612012-04-05 14:27:12 -07001814 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001815 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001816
Jeff Brown6ec6f792012-04-17 16:52:41 -07001817 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001818 "getKeyboardLayoutOverlay",
1819 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001820
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001821 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1822 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1823
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001824 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1825 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001826 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001827
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001828 // InputDevice
1829
1830 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1831 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1832
Jeff Brown6ec402b2010-07-28 15:48:59 -07001833 // KeyEvent
1834
1835 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001836 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1837
Jeff Brown8d608662010-08-30 03:02:23 -07001838 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001839
1840 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001841 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001842
RoboErikfb290df2013-12-16 11:27:55 -08001843 // InputDeviceIdentifier
1844
1845 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1846 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1847 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1848 "<init>", "(Ljava/lang/String;II)V");
1849
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001850 // TouchCalibration
1851
1852 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1853 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1854
1855 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1856 "getAffineTransform", "()[F");
1857
Jeff Brown46b9ac02010-04-22 18:58:52 -07001858 return 0;
1859}
1860
Jeff Brown46b9ac02010-04-22 18:58:52 -07001861} /* namespace android */