blob: 27c2faca9d3363c4e7867dea1fd7ac090bcce20e [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
Jeff Brown9302c872011-07-13 22:51:29 -0700213 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
214 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
Jeff Brown9302c872011-07-13 22:51:29 -0700739void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
740 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700741
Jeff Brown9302c872011-07-13 22:51:29 -0700742 if (windowHandleObjArray) {
743 jsize length = env->GetArrayLength(windowHandleObjArray);
744 for (jsize i = 0; i < length; i++) {
745 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
746 if (! windowHandleObj) {
747 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700748 }
Jeff Brown9302c872011-07-13 22:51:29 -0700749
750 sp<InputWindowHandle> windowHandle =
751 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
752 if (windowHandle != NULL) {
753 windowHandles.push(windowHandle);
754 }
755 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700756 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700757 }
Jeff Brown349703e2010-06-22 01:27:15 -0700758
Jeff Brown9302c872011-07-13 22:51:29 -0700759 mInputManager->getDispatcher()->setInputWindows(windowHandles);
760
761 // Do this after the dispatcher has updated the window handle state.
762 bool newPointerGesturesEnabled = true;
763 size_t numWindows = windowHandles.size();
764 for (size_t i = 0; i < numWindows; i++) {
765 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700766 const InputWindowInfo* windowInfo = windowHandle->getInfo();
767 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
768 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700769 newPointerGesturesEnabled = false;
770 }
771 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700772
773 uint32_t changes = 0;
774 { // acquire lock
775 AutoMutex _l(mLock);
776
777 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
778 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
779 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
780 }
781 } // release lock
782
783 if (changes) {
784 mInputManager->getReader()->requestRefreshConfiguration(changes);
785 }
Jeff Brown349703e2010-06-22 01:27:15 -0700786}
787
Jeff Brown9302c872011-07-13 22:51:29 -0700788void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
789 sp<InputApplicationHandle> applicationHandle =
790 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
791 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700792}
793
794void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700795 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700796}
797
Jeff Brown05dc66a2011-03-02 14:41:58 -0800798void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
799 AutoMutex _l(mLock);
800
801 if (mLocked.systemUiVisibility != visibility) {
802 mLocked.systemUiVisibility = visibility;
803
804 sp<PointerController> controller = mLocked.pointerController.promote();
805 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700806 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800807 }
808 }
809}
810
Jeff Brown2352b972011-04-12 22:39:53 -0700811void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800812 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700813 controller->setInactivityTimeout(lightsOut
814 ? PointerController::INACTIVITY_TIMEOUT_SHORT
815 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800816}
817
Jeff Brown1a84fd12011-06-02 01:26:32 -0700818void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700819 { // acquire lock
820 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700821
Jeff Brown474dcb52011-06-14 20:22:50 -0700822 if (mLocked.pointerSpeed == speed) {
823 return;
824 }
825
Steve Block6215d3f2012-01-04 20:05:49 +0000826 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700827 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700828 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700829
Jeff Brown474dcb52011-06-14 20:22:50 -0700830 mInputManager->getReader()->requestRefreshConfiguration(
831 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700832}
833
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700834void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
835 { // acquire lock
836 AutoMutex _l(mLock);
837
838 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
839 bool currentlyEnabled = index < 0;
840 if (!enabled && currentlyEnabled) {
841 mLocked.disabledInputDevices.add(deviceId);
842 }
843 if (enabled && !currentlyEnabled) {
844 mLocked.disabledInputDevices.remove(deviceId);
845 }
846 } // release lock
847
848 mInputManager->getReader()->requestRefreshConfiguration(
849 InputReaderConfiguration::CHANGE_ENABLED_STATE);
850}
851
Jeff Browndaf4a122011-08-26 17:14:14 -0700852void NativeInputManager::setShowTouches(bool enabled) {
853 { // acquire lock
854 AutoMutex _l(mLock);
855
856 if (mLocked.showTouches == enabled) {
857 return;
858 }
859
Steve Block6215d3f2012-01-04 20:05:49 +0000860 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700861 mLocked.showTouches = enabled;
862 } // release lock
863
864 mInputManager->getReader()->requestRefreshConfiguration(
865 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
866}
867
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800868void NativeInputManager::setPointerCapture(bool enabled) {
869 { // acquire lock
870 AutoMutex _l(mLock);
871
872 if (mLocked.pointerCapture == enabled) {
873 return;
874 }
875
876 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
877 mLocked.pointerCapture = enabled;
878 } // release lock
879
880 mInputManager->getReader()->requestRefreshConfiguration(
881 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
882}
883
Jeff Brown037c33e2014-04-09 00:31:55 -0700884void NativeInputManager::setInteractive(bool interactive) {
885 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700886}
887
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800888void NativeInputManager::reloadCalibration() {
889 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100890 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800891}
892
Michael Wrighte051f6f2016-05-13 17:44:16 +0100893void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800894 AutoMutex _l(mLock);
895 sp<PointerController> controller = mLocked.pointerController.promote();
896 if (controller != NULL) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100897 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800898 }
899}
900
901void NativeInputManager::reloadPointerIcons() {
902 AutoMutex _l(mLock);
903 sp<PointerController> controller = mLocked.pointerController.promote();
904 if (controller != NULL) {
905 controller->reloadPointerResources();
906 }
Jun Mukai1db53972015-09-11 18:08:31 -0700907}
908
Jun Mukaid4eaef72015-10-30 15:54:33 -0700909void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
910 AutoMutex _l(mLock);
911 sp<PointerController> controller = mLocked.pointerController.promote();
912 if (controller != NULL) {
913 controller->setCustomPointerIcon(icon);
914 }
915}
916
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800917TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
918 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100919 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800920 ScopedFloatArrayRO matrix(env, matrixArr);
921 assert(matrix.size() == 6);
922
923 TouchAffineTransformation transform;
924 transform.x_scale = matrix[0];
925 transform.x_ymix = matrix[1];
926 transform.x_offset = matrix[2];
927 transform.y_xmix = matrix[3];
928 transform.y_scale = matrix[4];
929 transform.y_offset = matrix[5];
930
931 return transform;
932}
933
934TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700935 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800936 JNIEnv* env = jniEnv();
937
938 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
939
940 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700941 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
942 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800943
944 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
945 gTouchCalibrationClassInfo.getAffineTransform));
946
947 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
948
949 env->DeleteLocalRef(matrixArr);
950 env->DeleteLocalRef(cal);
951
952 return transform;
953}
954
Jeff Brown0029c662011-03-30 02:25:18 -0700955bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100956 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700957 jobject inputEventObj;
958
959 JNIEnv* env = jniEnv();
960 switch (inputEvent->getType()) {
961 case AINPUT_EVENT_TYPE_KEY:
962 inputEventObj = android_view_KeyEvent_fromNative(env,
963 static_cast<const KeyEvent*>(inputEvent));
964 break;
965 case AINPUT_EVENT_TYPE_MOTION:
966 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
967 static_cast<const MotionEvent*>(inputEvent));
968 break;
969 default:
970 return true; // dispatch the event normally
971 }
972
973 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000974 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700975 return true; // dispatch the event normally
976 }
977
978 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700979 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700980 inputEventObj, policyFlags);
981 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
982 pass = true;
983 }
984 env->DeleteLocalRef(inputEventObj);
985 return pass;
986}
987
Jeff Brown1f245102010-11-18 20:53:46 -0800988void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
989 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100990 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700991 // Policy:
992 // - Ignore untrusted events and pass them along.
993 // - Ask the window manager what to do with normal events and trusted injected events.
994 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100995 bool interactive = mInteractive.load();
996 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700997 policyFlags |= POLICY_FLAG_INTERACTIVE;
998 }
Jeff Brown3122e442010-10-11 23:32:49 -0700999 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -08001000 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -07001001 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -08001002 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1003 jint wmActions;
1004 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001005 wmActions = env->CallIntMethod(mServiceObj,
1006 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -07001007 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001008 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
1009 wmActions = 0;
1010 }
1011 android_view_KeyEvent_recycle(env, keyEventObj);
1012 env->DeleteLocalRef(keyEventObj);
1013 } else {
Steve Block3762c312012-01-06 19:20:56 +00001014 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -07001015 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -07001016 }
1017
Jeff Brown56194eb2011-03-02 19:23:13 -08001018 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -07001019 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001020 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001021 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1022 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001023 }
1024}
1025
Jeff Brown56194eb2011-03-02 19:23:13 -08001026void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001027 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001028 // Policy:
1029 // - Ignore untrusted events and pass them along.
1030 // - No special filtering for injected events required at this time.
1031 // - Filter normal events based on screen state.
1032 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001033 bool interactive = mInteractive.load();
1034 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001035 policyFlags |= POLICY_FLAG_INTERACTIVE;
1036 }
Jeff Brown3122e442010-10-11 23:32:49 -07001037 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001038 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001039 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001040 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001041 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001042 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001043 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001044 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001045 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001046 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001047 wmActions = 0;
1048 }
1049
Jeff Brown56194eb2011-03-02 19:23:13 -08001050 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001051 }
Jeff Brown3122e442010-10-11 23:32:49 -07001052 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001053 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001054 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1055 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001056 }
1057}
1058
Jeff Brown56194eb2011-03-02 19:23:13 -08001059void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1060 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001061 if (wmActions & WM_ACTION_PASS_TO_USER) {
1062 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1063 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001064#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001065 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001066#endif
1067 }
1068}
1069
Jeff Brown905805a2011-10-12 13:57:59 -07001070nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -08001071 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -07001072 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001073 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001074 // Policy:
1075 // - Ignore untrusted events and pass them along.
1076 // - Filter normal events and trusted injected events through the window manager policy to
1077 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001078 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001079 if (policyFlags & POLICY_FLAG_TRUSTED) {
1080 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001081
Jeff Brown928e0542011-01-10 11:17:36 -08001082 // Note: inputWindowHandle may be null.
1083 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001084 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1085 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001086 jlong delayMillis = env->CallLongMethod(mServiceObj,
1087 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -08001088 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001089 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1090 android_view_KeyEvent_recycle(env, keyEventObj);
1091 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001092 if (!error) {
1093 if (delayMillis < 0) {
1094 result = -1;
1095 } else if (delayMillis > 0) {
1096 result = milliseconds_to_nanoseconds(delayMillis);
1097 }
1098 }
Jeff Brown1f245102010-11-18 20:53:46 -08001099 } else {
Steve Block3762c312012-01-06 19:20:56 +00001100 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001101 }
Jeff Brown928e0542011-01-10 11:17:36 -08001102 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -07001103 }
Jeff Brown1f245102010-11-18 20:53:46 -08001104 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001105}
1106
Jeff Brown928e0542011-01-10 11:17:36 -08001107bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001108 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001109 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001110 // Policy:
1111 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001112 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001113 if (policyFlags & POLICY_FLAG_TRUSTED) {
1114 JNIEnv* env = jniEnv();
1115
Jeff Brown928e0542011-01-10 11:17:36 -08001116 // Note: inputWindowHandle may be null.
1117 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001118 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1119 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001120 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1121 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -08001122 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001123 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
1124 fallbackKeyEventObj = NULL;
1125 }
Jeff Brown1f245102010-11-18 20:53:46 -08001126 android_view_KeyEvent_recycle(env, keyEventObj);
1127 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001128
1129 if (fallbackKeyEventObj) {
1130 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1131 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1132 outFallbackKeyEvent)) {
1133 result = true;
1134 }
1135 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1136 env->DeleteLocalRef(fallbackKeyEventObj);
1137 }
Jeff Brown1f245102010-11-18 20:53:46 -08001138 } else {
Steve Block3762c312012-01-06 19:20:56 +00001139 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001140 }
Jeff Brown928e0542011-01-10 11:17:36 -08001141 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001142 }
Jeff Brown1f245102010-11-18 20:53:46 -08001143 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001144}
1145
Jeff Brown01ce2e92010-09-26 22:20:12 -07001146void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001147 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001148 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001149}
1150
Jeff Brown349703e2010-06-22 01:27:15 -07001151
Jeff Brownb88102f2010-09-08 11:49:43 -07001152bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1153 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001154 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001155 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001156 jboolean result = env->CallBooleanMethod(mServiceObj,
1157 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001158 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1159 result = false;
1160 }
Jeff Brown349703e2010-06-22 01:27:15 -07001161 return result;
1162}
1163
Jun Mukai19a56012015-11-24 11:25:52 -08001164void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001165 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001166 JNIEnv* env = jniEnv();
1167
1168 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1169 mServiceObj, gServiceClassInfo.getPointerIcon));
1170 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1171 return;
1172 }
1173
1174 PointerIcon pointerIcon;
1175 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1176 mContextObj, &pointerIcon);
1177 if (!status && !pointerIcon.isNullIcon()) {
1178 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1179 } else {
1180 *icon = SpriteIcon();
1181 }
1182}
1183
Jeff Brown2352b972011-04-12 22:39:53 -07001184void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001185 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001186 JNIEnv* env = jniEnv();
1187
1188 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1189 &outResources->spotHover);
1190 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1191 &outResources->spotTouch);
1192 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1193 &outResources->spotAnchor);
1194}
1195
Jun Mukai808196f2015-10-28 16:46:44 -07001196void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1197 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001198 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001199 JNIEnv* env = jniEnv();
1200
1201 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1202 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001203 PointerIcon pointerIcon;
1204 loadSystemIconAsSpriteWithPointerIcon(
1205 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1206 if (!pointerIcon.bitmapFrames.empty()) {
1207 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1208 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1209 animationData.durationPerFrame =
1210 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1211 animationData.animationFrames.reserve(numFrames);
1212 animationData.animationFrames.push_back(SpriteIcon(
1213 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1214 for (size_t i = 0; i < numFrames - 1; ++i) {
1215 animationData.animationFrames.push_back(SpriteIcon(
1216 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1217 }
1218 }
Jun Mukai1db53972015-09-11 18:08:31 -07001219 }
Jun Mukai808196f2015-10-28 16:46:44 -07001220 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1221 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001222}
1223
Jun Mukai5ec74202015-10-07 16:58:09 +09001224int32_t NativeInputManager::getDefaultPointerIconId() {
1225 return POINTER_ICON_STYLE_ARROW;
1226}
Jeff Brown83c09682010-12-23 17:50:18 -08001227
Jun Mukaid4eaef72015-10-30 15:54:33 -07001228int32_t NativeInputManager::getCustomPointerIconId() {
1229 return POINTER_ICON_STYLE_CUSTOM;
1230}
1231
Jeff Brown9c3cda02010-06-15 01:31:58 -07001232// ----------------------------------------------------------------------------
1233
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001234static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001235 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001236 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001237 if (messageQueue == NULL) {
1238 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1239 return 0;
1240 }
1241
Jeff Brown603b4452012-04-06 17:39:41 -07001242 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1243 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001244 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001245 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001246}
1247
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001248static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001249 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001250
Jeff Brown4532e612012-04-05 14:27:12 -07001251 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001252 if (result) {
1253 jniThrowRuntimeException(env, "Input manager could not be started.");
1254 }
1255}
1256
Santos Cordonee8931e2017-04-05 10:31:15 -07001257static void nativeSetVirtualDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
1258 jobjectArray viewportObjArray) {
1259 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1260 im->setVirtualDisplayViewports(env, viewportObjArray);
1261}
1262
1263static void nativeSetDisplayViewport(JNIEnv* env, jclass /* clazz */, jlong ptr,
1264 jint viewportType, jint displayId, jint orientation,
Jeff Brownd728bf52012-09-08 18:05:28 -07001265 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001266 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
Santos Cordonee8931e2017-04-05 10:31:15 -07001267 jint deviceWidth, jint deviceHeight, jstring uniqueId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001268 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001269
Jeff Brownd728bf52012-09-08 18:05:28 -07001270 DisplayViewport v;
1271 v.displayId = displayId;
1272 v.orientation = orientation;
1273 v.logicalLeft = logicalLeft;
1274 v.logicalTop = logicalTop;
1275 v.logicalRight = logicalRight;
1276 v.logicalBottom = logicalBottom;
1277 v.physicalLeft = physicalLeft;
1278 v.physicalTop = physicalTop;
1279 v.physicalRight = physicalRight;
1280 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001281 v.deviceWidth = deviceWidth;
1282 v.deviceHeight = deviceHeight;
Santos Cordonee8931e2017-04-05 10:31:15 -07001283 if (uniqueId != nullptr) {
1284 v.uniqueId.setTo(ScopedUtfChars(env, uniqueId).c_str());
1285 }
1286
1287 im->setDisplayViewport(viewportType, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001288}
1289
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001290static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001291 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001292 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001293
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001294 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001295 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001296}
1297
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001298static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001299 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001300 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001301
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001302 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001303 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001304}
1305
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001306static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001307 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001308 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001309
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001310 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001311 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001312}
1313
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001314static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001315 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001316 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001317
1318 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1319 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1320 jsize numCodes = env->GetArrayLength(keyCodes);
1321 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001322 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001323 if (im->getInputManager()->getReader()->hasKeys(
1324 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1325 result = JNI_TRUE;
1326 } else {
1327 result = JNI_FALSE;
1328 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001329 } else {
1330 result = JNI_FALSE;
1331 }
1332
1333 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1334 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1335 return result;
1336}
1337
1338static void throwInputChannelNotInitialized(JNIEnv* env) {
1339 jniThrowException(env, "java/lang/IllegalStateException",
1340 "inputChannel is not initialized");
1341}
1342
Jeff Brown4532e612012-04-05 14:27:12 -07001343static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001344 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001345 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1346
Steve Block8564c8d2012-01-05 23:22:43 +00001347 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001348 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001349 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001350}
1351
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001352static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001353 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001354 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001355
1356 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1357 inputChannelObj);
1358 if (inputChannel == NULL) {
1359 throwInputChannelNotInitialized(env);
1360 return;
1361 }
1362
Jeff Brown928e0542011-01-10 11:17:36 -08001363 sp<InputWindowHandle> inputWindowHandle =
1364 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001365
Jeff Brown4532e612012-04-05 14:27:12 -07001366 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001367 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001368 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001369 std::string message;
1370 message += StringPrintf("Failed to register input channel. status=%d", status);
1371 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001372 return;
1373 }
1374
Jeff Browna41ca772010-08-11 14:46:32 -07001375 if (! monitor) {
1376 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001377 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001378 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001379}
1380
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001381static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001382 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001383 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001384
1385 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1386 inputChannelObj);
1387 if (inputChannel == NULL) {
1388 throwInputChannelNotInitialized(env);
1389 return;
1390 }
1391
1392 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1393
Jeff Brown4532e612012-04-05 14:27:12 -07001394 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001395 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001396 std::string message;
1397 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1398 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001399 }
1400}
1401
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001402static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001403 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001404 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001405
Jeff Brown4532e612012-04-05 14:27:12 -07001406 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001407}
1408
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001409static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Jeff Brownca9bc702014-02-11 14:32:56 -08001410 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001411 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001412 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001413
Jeff Brown6ec402b2010-07-28 15:48:59 -07001414 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1415 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001416 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1417 if (status) {
1418 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1419 return INPUT_EVENT_INJECTION_FAILED;
1420 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001421
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001422 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001423 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001424 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001425 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001426 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1427 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001428 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1429 return INPUT_EVENT_INJECTION_FAILED;
1430 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001431
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001432 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001433 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001434 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001435 } else {
1436 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001437 return INPUT_EVENT_INJECTION_FAILED;
1438 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001439}
1440
Andrii Kulian112d0562016-03-08 10:44:22 -08001441static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1442 jlong ptr, jint deviceId) {
1443 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001444
Andrii Kulian112d0562016-03-08 10:44:22 -08001445 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1446}
1447
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001448static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001449 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001450 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001451
Jeff Brown4532e612012-04-05 14:27:12 -07001452 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001453}
1454
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001455static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001456 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001457 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001458
Jeff Brown4532e612012-04-05 14:27:12 -07001459 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001460}
1461
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001462static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1463 jboolean enabled) {
1464 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001465
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001466 im->setPointerCapture(enabled);
1467}
1468
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001469static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1470 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001471 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001472
Jeff Brown4532e612012-04-05 14:27:12 -07001473 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001474}
1475
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001476static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1477 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001478 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001479
Jeff Brown4532e612012-04-05 14:27:12 -07001480 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001481}
1482
Jeff Brown4532e612012-04-05 14:27:12 -07001483static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001484 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001485 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001486
1487 sp<InputChannel> fromChannel =
1488 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1489 sp<InputChannel> toChannel =
1490 android_view_InputChannel_getInputChannel(env, toChannelObj);
1491
1492 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001493 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001494 }
1495
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001496 if (im->getInputManager()->getDispatcher()->
1497 transferTouchFocus(fromChannel, toChannel)) {
1498 return JNI_TRUE;
1499 } else {
1500 return JNI_FALSE;
1501 }
Jeff Browne6504122010-09-27 14:52:15 -07001502}
1503
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001504static void nativeSetPointerSpeed(JNIEnv* /* env */,
1505 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001506 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001507
Jeff Brown4532e612012-04-05 14:27:12 -07001508 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001509}
1510
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001511static void nativeSetShowTouches(JNIEnv* /* env */,
1512 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001513 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001514
Jeff Brown4532e612012-04-05 14:27:12 -07001515 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001516}
1517
Jeff Brown037c33e2014-04-09 00:31:55 -07001518static void nativeSetInteractive(JNIEnv* env,
1519 jclass clazz, jlong ptr, jboolean interactive) {
1520 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1521
1522 im->setInteractive(interactive);
1523}
1524
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001525static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1526 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001527
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001528 im->reloadCalibration();
1529}
1530
Jeff Browna47425a2012-04-13 04:09:27 -07001531static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001532 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001533 jint repeat, jint token) {
1534 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1535
1536 size_t patternSize = env->GetArrayLength(patternObj);
1537 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001538 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001539 "which is more than the maximum supported size of %d.",
1540 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1541 return; // limit to reasonable size
1542 }
1543
1544 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1545 patternObj, NULL));
1546 nsecs_t pattern[patternSize];
1547 for (size_t i = 0; i < patternSize; i++) {
1548 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001549 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001550 }
1551 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1552
1553 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1554}
1555
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001556static void nativeCancelVibrate(JNIEnv* /* env */,
1557 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001558 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1559
1560 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1561}
1562
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001563static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1564 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001565 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1566
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001567 im->getInputManager()->getReader()->requestRefreshConfiguration(
1568 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1569}
1570
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001571static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1572 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001573 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1574
1575 im->getInputManager()->getReader()->requestRefreshConfiguration(
1576 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001577}
1578
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001579static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001580 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001581
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001582 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001583 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001584 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001585}
1586
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001587static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001588 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001589
Jeff Brown4532e612012-04-05 14:27:12 -07001590 im->getInputManager()->getReader()->monitor();
1591 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001592}
1593
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001594static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1595 jclass /* clazz */, jlong ptr, jint deviceId) {
1596 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1597
1598 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1599}
1600
1601static void nativeEnableInputDevice(JNIEnv* /* env */,
1602 jclass /* clazz */, jlong ptr, jint deviceId) {
1603 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1604
1605 im->setInputDeviceEnabled(deviceId, true);
1606}
1607
1608static void nativeDisableInputDevice(JNIEnv* /* env */,
1609 jclass /* clazz */, jlong ptr, jint deviceId) {
1610 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1611
1612 im->setInputDeviceEnabled(deviceId, false);
1613}
1614
Michael Wrighte051f6f2016-05-13 17:44:16 +01001615static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001616 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001617
Michael Wrighte051f6f2016-05-13 17:44:16 +01001618 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001619}
1620
Jun Mukai19a56012015-11-24 11:25:52 -08001621static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1622 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001623
Jun Mukai19a56012015-11-24 11:25:52 -08001624 im->reloadPointerIcons();
1625}
1626
Jun Mukaid4eaef72015-10-30 15:54:33 -07001627static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1628 jlong ptr, jobject iconObj) {
1629 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1630
1631 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001632 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1633 if (result) {
1634 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1635 return;
1636 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001637
1638 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001639 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1640 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1641 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1642 spriteIcon.bitmap.rowBytes(), 0, 0);
1643 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001644 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1645 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1646 im->setCustomPointerIcon(spriteIcon);
1647}
1648
Jeff Brown9c3cda02010-06-15 01:31:58 -07001649// ----------------------------------------------------------------------------
1650
Daniel Micay76f6a862015-09-19 17:31:01 -04001651static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001652 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001653 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001654 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001655 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001656 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001657 (void*) nativeStart },
Santos Cordonee8931e2017-04-05 10:31:15 -07001658 { "nativeSetVirtualDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1659 (void*) nativeSetVirtualDisplayViewports },
1660 { "nativeSetDisplayViewport", "(JIIIIIIIIIIIIILjava/lang/String;)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001661 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001662 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001663 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001664 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001665 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001666 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001667 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001668 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001669 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001670 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001671 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001672 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001673 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001674 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001675 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001676 (void*) nativeSetInputFilterEnabled },
Jeff Brownca9bc702014-02-11 14:32:56 -08001677 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001678 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001679 { "nativeToggleCapsLock", "(JI)V",
1680 (void*) nativeToggleCapsLock },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001681 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001682 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001683 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001684 (void*) nativeSetFocusedApplication },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001685 { "nativeSetPointerCapture", "(JZ)V",
1686 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001687 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001688 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001689 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001690 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001691 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001692 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001693 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001694 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001695 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001696 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001697 { "nativeSetInteractive", "(JZ)V",
1698 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001699 { "nativeReloadCalibration", "(J)V",
1700 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001701 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001702 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001703 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001704 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001705 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001706 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001707 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001708 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001709 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001710 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001711 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001712 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001713 { "nativeIsInputDeviceEnabled", "(JI)Z",
1714 (void*) nativeIsInputDeviceEnabled },
1715 { "nativeEnableInputDevice", "(JI)V",
1716 (void*) nativeEnableInputDevice },
1717 { "nativeDisableInputDevice", "(JI)V",
1718 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001719 { "nativeSetPointerIconType", "(JI)V",
1720 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001721 { "nativeReloadPointerIcons", "(J)V",
1722 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001723 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1724 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001725};
1726
1727#define FIND_CLASS(var, className) \
1728 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001729 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001730
1731#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1732 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001733 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001734
1735#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1736 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001737 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001738
1739int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001740 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001741 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001742 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001743 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1744
Jeff Brown9c3cda02010-06-15 01:31:58 -07001745 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001746
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001747 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001748 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001749
Jeff Brown4532e612012-04-05 14:27:12 -07001750 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001751 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001752
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001753 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1754 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1755
Jeff Brown53384282012-08-20 20:16:01 -07001756 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1757 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001758
Jeff Brown4532e612012-04-05 14:27:12 -07001759 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1760 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001761
Jeff Brown4532e612012-04-05 14:27:12 -07001762 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001763 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001764 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001765
Jeff Brown4532e612012-04-05 14:27:12 -07001766 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001767 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1768
Jeff Brown4532e612012-04-05 14:27:12 -07001769 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001770 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001771
Michael Wright70af00a2014-09-03 19:30:20 -07001772 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1773 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001774
Jeff Brown4532e612012-04-05 14:27:12 -07001775 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001776 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001777 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001778
Jeff Brown4532e612012-04-05 14:27:12 -07001779 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001780 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001781 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001782
Jeff Brown4532e612012-04-05 14:27:12 -07001783 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001784 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001785
Jeff Brown4532e612012-04-05 14:27:12 -07001786 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001787 "getVirtualKeyQuietTimeMillis", "()I");
1788
Jeff Brown4532e612012-04-05 14:27:12 -07001789 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001790 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1791
Jeff Brown4532e612012-04-05 14:27:12 -07001792 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001793 "getKeyRepeatTimeout", "()I");
1794
Jeff Brown4532e612012-04-05 14:27:12 -07001795 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001796 "getKeyRepeatDelay", "()I");
1797
Jeff Brown4532e612012-04-05 14:27:12 -07001798 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001799 "getHoverTapTimeout", "()I");
1800
Jeff Brown4532e612012-04-05 14:27:12 -07001801 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001802 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001803
Jeff Brown4532e612012-04-05 14:27:12 -07001804 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001805 "getDoubleTapTimeout", "()I");
1806
Jeff Brown4532e612012-04-05 14:27:12 -07001807 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001808 "getLongPressTimeout", "()I");
1809
Jeff Brown4532e612012-04-05 14:27:12 -07001810 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001811 "getPointerLayer", "()I");
1812
Jeff Brown4532e612012-04-05 14:27:12 -07001813 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001814 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001815
Jeff Brown6ec6f792012-04-17 16:52:41 -07001816 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001817 "getKeyboardLayoutOverlay",
1818 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001819
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001820 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1821 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1822
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001823 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1824 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001825 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001826
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001827 // InputDevice
1828
1829 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1830 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1831
Jeff Brown6ec402b2010-07-28 15:48:59 -07001832 // KeyEvent
1833
1834 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001835 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1836
Jeff Brown8d608662010-08-30 03:02:23 -07001837 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001838
1839 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001840 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001841
RoboErikfb290df2013-12-16 11:27:55 -08001842 // InputDeviceIdentifier
1843
1844 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1845 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1846 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1847 "<init>", "(Ljava/lang/String;II)V");
1848
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001849 // TouchCalibration
1850
1851 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1852 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1853
1854 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1855 "getAffineTransform", "()[F");
1856
Jeff Brown46b9ac02010-04-22 18:58:52 -07001857 return 0;
1858}
1859
Jeff Brown46b9ac02010-04-22 18:58:52 -07001860} /* namespace android */