blob: d400a928bc775ec91452bccc0a85e8e62244e921 [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
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010067#include <vector>
68
Michael Wrighta4051212015-07-23 17:04:40 +010069#define INDENT " "
70
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080071using android::base::StringPrintf;
72
Jeff Brown46b9ac02010-04-22 18:58:52 -070073namespace android {
74
Jeff Brown1a84fd12011-06-02 01:26:32 -070075// The exponent used to calculate the pointer speed scaling factor.
76// The scaling factor is calculated as 2 ^ (speed * exponent),
77// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070078static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070079
Jeff Brown46b9ac02010-04-22 18:58:52 -070080static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070081 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070082 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070083 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070084 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070085 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070086 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070087 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070088 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070089 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070090 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070091 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080092 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070093 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080094 jmethodID getKeyRepeatTimeout;
95 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070096 jmethodID getHoverTapTimeout;
97 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070098 jmethodID getDoubleTapTimeout;
99 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -0800100 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800101 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700102 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700103 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800104 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -0700105} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700106
107static struct {
108 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700109} gInputDeviceClassInfo;
110
111static struct {
112 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700113} gKeyEventClassInfo;
114
115static struct {
116 jclass clazz;
117} gMotionEventClassInfo;
118
RoboErikfb290df2013-12-16 11:27:55 -0800119static struct {
120 jclass clazz;
121 jmethodID constructor;
122} gInputDeviceIdentifierInfo;
123
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800124static struct {
125 jclass clazz;
126 jmethodID getAffineTransform;
127} gTouchCalibrationClassInfo;
128
RoboErikfb290df2013-12-16 11:27:55 -0800129
Jeff Brown928e0542011-01-10 11:17:36 -0800130
131// --- Global functions ---
132
Jeff Brown214eaf42011-05-26 19:17:02 -0700133template<typename T>
134inline static T min(const T& a, const T& b) {
135 return a < b ? a : b;
136}
137
138template<typename T>
139inline static T max(const T& a, const T& b) {
140 return a > b ? a : b;
141}
142
Michael Wrighta4051212015-07-23 17:04:40 +0100143static inline const char* toString(bool value) {
144 return value ? "true" : "false";
145}
146
Jeff Brown928e0542011-01-10 11:17:36 -0800147static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
148 const sp<InputApplicationHandle>& inputApplicationHandle) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100149 if (inputApplicationHandle == nullptr) {
150 return nullptr;
Jeff Brown928e0542011-01-10 11:17:36 -0800151 }
152 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
153 getInputApplicationHandleObjLocalRef(env);
154}
155
156static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
157 const sp<InputWindowHandle>& inputWindowHandle) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100158 if (inputWindowHandle == nullptr) {
159 return nullptr;
Jeff Brown928e0542011-01-10 11:17:36 -0800160 }
161 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
162 getInputWindowHandleObjLocalRef(env);
163}
164
Jun Mukai808196f2015-10-28 16:46:44 -0700165static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
166 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700167 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700168 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700169 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400170 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
171 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
172 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
173 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
174 bitmapCopy->rowBytes(), 0, 0);
175 }
Jun Mukai808196f2015-10-28 16:46:44 -0700176 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
177 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700178 }
179}
180
Jun Mukai808196f2015-10-28 16:46:44 -0700181static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
182 SpriteIcon* outSpriteIcon) {
183 PointerIcon pointerIcon;
184 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
185}
186
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100187static void updatePointerControllerFromViewport(
188 sp<PointerController> controller, const DisplayViewport* const viewport) {
189 if (controller != nullptr && viewport != nullptr) {
190 const int32_t width = viewport->logicalRight - viewport->logicalLeft;
191 const int32_t height = viewport->logicalBottom - viewport->logicalTop;
192 controller->setDisplayViewport(width, height, viewport->orientation);
193 }
194}
195
Jeff Brown905805a2011-10-12 13:57:59 -0700196enum {
197 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700198};
199
Jeff Brown928e0542011-01-10 11:17:36 -0800200
201// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800202
Jeff Brown9c3cda02010-06-15 01:31:58 -0700203class NativeInputManager : public virtual RefBase,
204 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700205 public virtual InputDispatcherPolicyInterface,
206 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700207protected:
208 virtual ~NativeInputManager();
209
210public:
Jeff Brown4532e612012-04-05 14:27:12 -0700211 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700212
213 inline sp<InputManager> getInputManager() const { return mInputManager; }
214
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800215 void dump(std::string& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700216
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100217 void setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700218
Jeff Brown7fbdc842010-06-17 20:52:56 -0700219 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800220 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700221 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
222
Arthur Hung39134b22018-08-14 11:58:28 +0800223 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray, int32_t displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700224 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700225 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800226 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700227 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700228 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700229 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700230 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800231 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100232 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800233 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700234 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800235 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700236
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237 /* --- InputReaderPolicyInterface implementation --- */
238
Jeff Brown214eaf42011-05-26 19:17:02 -0700239 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800240 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700241 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800242 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100243 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700244 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
245 jfloatArray matrixArr);
246 virtual TouchAffineTransformation getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100247 const std::string& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700248
249 /* --- InputDispatcherPolicyInterface implementation --- */
250
Jeff Brownbcc046a2012-09-27 20:46:43 -0700251 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700252 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700253 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700254 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700255 const sp<InputWindowHandle>& inputWindowHandle,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800256 const std::string& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800257 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700258 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700259 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800260 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800261 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700262 virtual nsecs_t interceptKeyBeforeDispatching(
263 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700264 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800265 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800266 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700267 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700268 virtual bool checkInjectEventsPermissionNonReentrant(
269 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270
Jeff Brown2352b972011-04-12 22:39:53 -0700271 /* --- PointerControllerPolicyInterface implementation --- */
272
Jun Mukai19a56012015-11-24 11:25:52 -0800273 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700274 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700275 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
276 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900277 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700278 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700279
Jeff Brown9c3cda02010-06-15 01:31:58 -0700280private:
281 sp<InputManager> mInputManager;
282
Jeff Brown2352b972011-04-12 22:39:53 -0700283 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700284 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800285 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700286
Jeff Brown83c09682010-12-23 17:50:18 -0800287 Mutex mLock;
288 struct Locked {
289 // Display size information.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100290 std::vector<DisplayViewport> viewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800291
Jeff Brown05dc66a2011-03-02 14:41:58 -0800292 // System UI visibility.
293 int32_t systemUiVisibility;
294
Jeff Brown1a84fd12011-06-02 01:26:32 -0700295 // Pointer speed.
296 int32_t pointerSpeed;
297
Jeff Brown474dcb52011-06-14 20:22:50 -0700298 // True if pointer gestures are enabled.
299 bool pointerGesturesEnabled;
300
Jeff Browndaf4a122011-08-26 17:14:14 -0700301 // Show touches feature enable/disable.
302 bool showTouches;
303
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800304 // Pointer capture feature enable/disable.
305 bool pointerCapture;
306
Jeff Brown5541de92011-04-11 11:54:25 -0700307 // Sprite controller singleton, created on first use.
308 sp<SpriteController> spriteController;
309
Jeff Brown83c09682010-12-23 17:50:18 -0800310 // Pointer controller singleton, created and destroyed as needed.
311 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700312
313 // Input devices to be disabled
314 SortedVector<int32_t> disabledInputDevices;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100315 } mLocked GUARDED_BY(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700316
Michael Wrighta4051212015-07-23 17:04:40 +0100317 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700318
Jeff Brown2352b972011-04-12 22:39:53 -0700319 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800320 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700321 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800322
Jeff Brownb88102f2010-09-08 11:49:43 -0700323 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700324
Jeff Brown9c3cda02010-06-15 01:31:58 -0700325 static inline JNIEnv* jniEnv() {
326 return AndroidRuntime::getJNIEnv();
327 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700328};
329
Jeff Brown928e0542011-01-10 11:17:36 -0800330
Jeff Brown9c3cda02010-06-15 01:31:58 -0700331
Jeff Brown2352b972011-04-12 22:39:53 -0700332NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700333 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700334 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700335 JNIEnv* env = jniEnv();
336
Jeff Brown2352b972011-04-12 22:39:53 -0700337 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700338 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700339
Jeff Brown83c09682010-12-23 17:50:18 -0800340 {
341 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800342 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700343 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700344 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700345 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800346 mLocked.pointerCapture = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800347 }
Michael Wrighta4051212015-07-23 17:04:40 +0100348 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800349
Jeff Brown9c3cda02010-06-15 01:31:58 -0700350 sp<EventHub> eventHub = new EventHub();
351 mInputManager = new InputManager(eventHub, this, this);
352}
353
354NativeInputManager::~NativeInputManager() {
355 JNIEnv* env = jniEnv();
356
Jeff Brown2352b972011-04-12 22:39:53 -0700357 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700358 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700359}
360
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800361void NativeInputManager::dump(std::string& dump) {
362 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100363 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800364 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100365 }
366 {
367 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800368 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100369 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800370 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
371 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100372 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800373 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
374 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100375 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800376 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100377
Jeff Brownb88102f2010-09-08 11:49:43 -0700378 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800379 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700380
Jeff Brownb88102f2010-09-08 11:49:43 -0700381 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800382 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700383}
384
Jeff Brown7fbdc842010-06-17 20:52:56 -0700385bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700386 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000387 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700388 LOGE_EX(env);
389 env->ExceptionClear();
390 return true;
391 }
392 return false;
393}
394
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100395static const DisplayViewport* findInternalViewport(const std::vector<DisplayViewport>& viewports) {
396 for (const DisplayViewport& v : viewports) {
397 if (v.type == ViewportType::VIEWPORT_INTERNAL) {
398 return &v;
399 }
400 }
401 return nullptr;
402}
403
404void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
405 std::vector<DisplayViewport> viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700406
407 if (viewportObjArray) {
408 jsize length = env->GetArrayLength(viewportObjArray);
409 for (jsize i = 0; i < length; i++) {
410 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
411 if (! viewportObj) {
412 break; // found null element indicating end of used portion of the array
413 }
414
415 DisplayViewport viewport;
416 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100417 ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
418 viewports.push_back(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -0700419
420 env->DeleteLocalRef(viewportObj);
421 }
422 }
423
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100424 const DisplayViewport* newInternalViewport = findInternalViewport(viewports);
Santos Cordonee8931e2017-04-05 10:31:15 -0700425 {
426 AutoMutex _l(mLock);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100427 const DisplayViewport* oldInternalViewport = findInternalViewport(mLocked.viewports);
428 // Internal viewport has changed if there wasn't one earlier, and there is one now, or,
429 // if they are different.
430 const bool internalViewportChanged = (newInternalViewport != nullptr) &&
Siarhei Vishniakoue5bf8662018-10-04 09:33:24 -0700431 (oldInternalViewport == nullptr || (*oldInternalViewport != *newInternalViewport));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100432 if (internalViewportChanged) {
433 sp<PointerController> controller = mLocked.pointerController.promote();
434 updatePointerControllerFromViewport(controller, newInternalViewport);
435 }
436 mLocked.viewports = viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700437 }
438
439 mInputManager->getReader()->requestRefreshConfiguration(
440 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
441}
442
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700443status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Jeff Brown928e0542011-01-10 11:17:36 -0800444 const sp<InputChannel>& inputChannel,
445 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100446 ATRACE_CALL();
Jeff Brown928e0542011-01-10 11:17:36 -0800447 return mInputManager->getDispatcher()->registerInputChannel(
448 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700449}
450
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700451status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700452 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100453 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700454 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700455}
456
Jeff Brown214eaf42011-05-26 19:17:02 -0700457void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100458 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700459 JNIEnv* env = jniEnv();
460
Jeff Brown4532e612012-04-05 14:27:12 -0700461 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
462 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700463 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
464 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
465 }
466
467 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700468 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
469 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700470 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
471 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700472 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700473 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100474 const char* deviceNameChars = env->GetStringUTFChars(item, nullptr);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100475 outConfig->excludedDeviceNames.push_back(deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700476 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700477 env->DeleteLocalRef(item);
478 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700479 env->DeleteLocalRef(excludedDeviceNames);
480 }
481
Jeff Brown4532e612012-04-05 14:27:12 -0700482 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
483 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700484 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700485 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
486 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700487 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700488 jint longPressTimeout = env->CallIntMethod(mServiceObj,
489 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700490 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700491 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700492
493 // We must ensure that the tap-drag interval is significantly shorter than
494 // the long-press timeout because the tap is held down for the entire duration
495 // of the double-tap timeout.
496 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700497 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700498 outConfig->pointerGestureTapDragInterval =
499 milliseconds_to_nanoseconds(tapDragInterval);
500 }
501 }
502 }
503
Jeff Brown4532e612012-04-05 14:27:12 -0700504 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
505 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700506 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
507 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700508 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700509
510 { // acquire lock
511 AutoMutex _l(mLock);
512
513 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
514 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700515 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700516
Jeff Browndaf4a122011-08-26 17:14:14 -0700517 outConfig->showTouches = mLocked.showTouches;
518
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800519 outConfig->pointerCapture = mLocked.pointerCapture;
520
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100521 outConfig->setDisplayViewports(mLocked.viewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700522
523 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700524 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700525}
526
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700527sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100528 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800529 AutoMutex _l(mLock);
530
531 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100532 if (controller == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700533 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800534
Jeff Brown2352b972011-04-12 22:39:53 -0700535 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800536 mLocked.pointerController = controller;
537
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100538 const DisplayViewport* internalViewport = findInternalViewport(mLocked.viewports);
539 updatePointerControllerFromViewport(controller, internalViewport);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800540
Jeff Brown2352b972011-04-12 22:39:53 -0700541 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800542 }
543 return controller;
544}
545
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100546void NativeInputManager::ensureSpriteControllerLocked() REQUIRES(mLock) {
547 if (mLocked.spriteController == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700548 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700549 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700550 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
551 layer = -1;
552 }
553 mLocked.spriteController = new SpriteController(mLooper, layer);
554 }
555}
556
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700557void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100558 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700559 JNIEnv* env = jniEnv();
560
561 size_t count = inputDevices.size();
562 jobjectArray inputDevicesObjArray = env->NewObjectArray(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100563 count, gInputDeviceClassInfo.clazz, nullptr);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700564 if (inputDevicesObjArray) {
565 bool error = false;
566 for (size_t i = 0; i < count; i++) {
567 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
568 if (!inputDeviceObj) {
569 error = true;
570 break;
571 }
572
573 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
574 env->DeleteLocalRef(inputDeviceObj);
575 }
576
577 if (!error) {
578 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
579 inputDevicesObjArray);
580 }
581
582 env->DeleteLocalRef(inputDevicesObjArray);
583 }
584
585 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
586}
587
Jeff Brown6ec6f792012-04-17 16:52:41 -0700588sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800589 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100590 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700591 JNIEnv* env = jniEnv();
592
593 sp<KeyCharacterMap> result;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100594 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
RoboErikfb290df2013-12-16 11:27:55 -0800595 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
596 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
597 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700598 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800599 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700600 if (arrayObj.get()) {
601 ScopedLocalRef<jstring> filenameObj(env,
602 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
603 ScopedLocalRef<jstring> contentsObj(env,
604 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
605 ScopedUtfChars filenameChars(env, filenameObj.get());
606 ScopedUtfChars contentsChars(env, contentsObj.get());
607
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100608 KeyCharacterMap::loadContents(filenameChars.c_str(),
609 contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700610 }
611 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
612 return result;
613}
614
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100615std::string NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100616 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700617 JNIEnv* env = jniEnv();
618
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100619 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.c_str()));
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700620 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
621 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100622 std::string result;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700623 if (aliasObj.get()) {
624 ScopedUtfChars aliasChars(env, aliasObj.get());
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100625 result = aliasChars.c_str();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700626 }
627 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
628 return result;
629}
630
Jeff Brownbcc046a2012-09-27 20:46:43 -0700631void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700632 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700633#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700634 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
635 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700636#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100637 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700638
639 JNIEnv* env = jniEnv();
640
Jeff Brown53384282012-08-20 20:16:01 -0700641 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700642 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700643 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700644}
645
Jeff Brown9c3cda02010-06-15 01:31:58 -0700646void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
647#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000648 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700649#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100650 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700651
652 JNIEnv* env = jniEnv();
653
Jeff Brown4532e612012-04-05 14:27:12 -0700654 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700655 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700656}
657
Jeff Brown519e0242010-09-15 15:18:56 -0700658nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800659 const sp<InputWindowHandle>& inputWindowHandle, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700660#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000661 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700662#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100663 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700664
665 JNIEnv* env = jniEnv();
666
Jeff Brown928e0542011-01-10 11:17:36 -0800667 jobject inputApplicationHandleObj =
668 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
669 jobject inputWindowHandleObj =
670 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800671 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700672
Jeff Brown4532e612012-04-05 14:27:12 -0700673 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700674 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
675 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700676 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
677 newTimeout = 0; // abort dispatch
678 } else {
679 assert(newTimeout >= 0);
680 }
681
Jeff Brownbd181bb2013-09-10 16:44:24 -0700682 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800683 env->DeleteLocalRef(inputWindowHandleObj);
684 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700685 return newTimeout;
686}
687
Jeff Brown928e0542011-01-10 11:17:36 -0800688void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700689#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000690 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700691#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100692 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700693
Jeff Brown7fbdc842010-06-17 20:52:56 -0700694 JNIEnv* env = jniEnv();
695
Jeff Brown928e0542011-01-10 11:17:36 -0800696 jobject inputWindowHandleObj =
697 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
698 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700699 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800700 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700701 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
702
Jeff Brown928e0542011-01-10 11:17:36 -0800703 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700704 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700705}
706
Jeff Brown214eaf42011-05-26 19:17:02 -0700707void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100708 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700709 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800710
Jeff Brown4532e612012-04-05 14:27:12 -0700711 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
712 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700713 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
714 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
715 }
Jeff Browna4547672011-03-02 21:38:11 -0800716
Jeff Brown4532e612012-04-05 14:27:12 -0700717 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
718 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700719 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
720 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
721 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700722}
723
Arthur Hung39134b22018-08-14 11:58:28 +0800724void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
725 int32_t displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700726 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700727
Jeff Brown9302c872011-07-13 22:51:29 -0700728 if (windowHandleObjArray) {
729 jsize length = env->GetArrayLength(windowHandleObjArray);
730 for (jsize i = 0; i < length; i++) {
731 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
732 if (! windowHandleObj) {
733 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700734 }
Jeff Brown9302c872011-07-13 22:51:29 -0700735
736 sp<InputWindowHandle> windowHandle =
737 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100738 if (windowHandle != nullptr) {
Jeff Brown9302c872011-07-13 22:51:29 -0700739 windowHandles.push(windowHandle);
740 }
741 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700742 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700743 }
Jeff Brown349703e2010-06-22 01:27:15 -0700744
Arthur Hung39134b22018-08-14 11:58:28 +0800745 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700746
747 // Do this after the dispatcher has updated the window handle state.
748 bool newPointerGesturesEnabled = true;
749 size_t numWindows = windowHandles.size();
750 for (size_t i = 0; i < numWindows; i++) {
751 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700752 const InputWindowInfo* windowInfo = windowHandle->getInfo();
753 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
754 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700755 newPointerGesturesEnabled = false;
756 }
757 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700758
759 uint32_t changes = 0;
760 { // acquire lock
761 AutoMutex _l(mLock);
762
763 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
764 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
765 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
766 }
767 } // release lock
768
769 if (changes) {
770 mInputManager->getReader()->requestRefreshConfiguration(changes);
771 }
Jeff Brown349703e2010-06-22 01:27:15 -0700772}
773
Jeff Brown9302c872011-07-13 22:51:29 -0700774void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
775 sp<InputApplicationHandle> applicationHandle =
776 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
777 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700778}
779
780void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700781 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700782}
783
Jeff Brown05dc66a2011-03-02 14:41:58 -0800784void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
785 AutoMutex _l(mLock);
786
787 if (mLocked.systemUiVisibility != visibility) {
788 mLocked.systemUiVisibility = visibility;
789
790 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100791 if (controller != nullptr) {
Jeff Brown2352b972011-04-12 22:39:53 -0700792 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800793 }
794 }
795}
796
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100797void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller)
798 REQUIRES(mLock) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800799 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700800 controller->setInactivityTimeout(lightsOut
801 ? PointerController::INACTIVITY_TIMEOUT_SHORT
802 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800803}
804
Jeff Brown1a84fd12011-06-02 01:26:32 -0700805void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700806 { // acquire lock
807 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700808
Jeff Brown474dcb52011-06-14 20:22:50 -0700809 if (mLocked.pointerSpeed == speed) {
810 return;
811 }
812
Steve Block6215d3f2012-01-04 20:05:49 +0000813 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700814 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700815 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700816
Jeff Brown474dcb52011-06-14 20:22:50 -0700817 mInputManager->getReader()->requestRefreshConfiguration(
818 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700819}
820
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700821void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
822 { // acquire lock
823 AutoMutex _l(mLock);
824
825 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
826 bool currentlyEnabled = index < 0;
827 if (!enabled && currentlyEnabled) {
828 mLocked.disabledInputDevices.add(deviceId);
829 }
830 if (enabled && !currentlyEnabled) {
831 mLocked.disabledInputDevices.remove(deviceId);
832 }
833 } // release lock
834
835 mInputManager->getReader()->requestRefreshConfiguration(
836 InputReaderConfiguration::CHANGE_ENABLED_STATE);
837}
838
Jeff Browndaf4a122011-08-26 17:14:14 -0700839void NativeInputManager::setShowTouches(bool enabled) {
840 { // acquire lock
841 AutoMutex _l(mLock);
842
843 if (mLocked.showTouches == enabled) {
844 return;
845 }
846
Steve Block6215d3f2012-01-04 20:05:49 +0000847 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700848 mLocked.showTouches = enabled;
849 } // release lock
850
851 mInputManager->getReader()->requestRefreshConfiguration(
852 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
853}
854
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800855void NativeInputManager::setPointerCapture(bool enabled) {
856 { // acquire lock
857 AutoMutex _l(mLock);
858
859 if (mLocked.pointerCapture == enabled) {
860 return;
861 }
862
863 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
864 mLocked.pointerCapture = enabled;
865 } // release lock
866
867 mInputManager->getReader()->requestRefreshConfiguration(
868 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
869}
870
Jeff Brown037c33e2014-04-09 00:31:55 -0700871void NativeInputManager::setInteractive(bool interactive) {
872 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700873}
874
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800875void NativeInputManager::reloadCalibration() {
876 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100877 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800878}
879
Michael Wrighte051f6f2016-05-13 17:44:16 +0100880void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800881 AutoMutex _l(mLock);
882 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100883 if (controller != nullptr) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100884 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800885 }
886}
887
888void NativeInputManager::reloadPointerIcons() {
889 AutoMutex _l(mLock);
890 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100891 if (controller != nullptr) {
Jun Mukai19a56012015-11-24 11:25:52 -0800892 controller->reloadPointerResources();
893 }
Jun Mukai1db53972015-09-11 18:08:31 -0700894}
895
Jun Mukaid4eaef72015-10-30 15:54:33 -0700896void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
897 AutoMutex _l(mLock);
898 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100899 if (controller != nullptr) {
Jun Mukaid4eaef72015-10-30 15:54:33 -0700900 controller->setCustomPointerIcon(icon);
901 }
902}
903
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800904TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
905 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100906 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800907 ScopedFloatArrayRO matrix(env, matrixArr);
908 assert(matrix.size() == 6);
909
910 TouchAffineTransformation transform;
911 transform.x_scale = matrix[0];
912 transform.x_ymix = matrix[1];
913 transform.x_offset = matrix[2];
914 transform.y_xmix = matrix[3];
915 transform.y_scale = matrix[4];
916 transform.y_offset = matrix[5];
917
918 return transform;
919}
920
921TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100922 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800923 JNIEnv* env = jniEnv();
924
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100925 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.c_str()));
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800926
927 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700928 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
929 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800930
931 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
932 gTouchCalibrationClassInfo.getAffineTransform));
933
934 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
935
936 env->DeleteLocalRef(matrixArr);
937 env->DeleteLocalRef(cal);
938
939 return transform;
940}
941
Jeff Brown0029c662011-03-30 02:25:18 -0700942bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100943 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700944 jobject inputEventObj;
945
946 JNIEnv* env = jniEnv();
947 switch (inputEvent->getType()) {
948 case AINPUT_EVENT_TYPE_KEY:
949 inputEventObj = android_view_KeyEvent_fromNative(env,
950 static_cast<const KeyEvent*>(inputEvent));
951 break;
952 case AINPUT_EVENT_TYPE_MOTION:
953 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
954 static_cast<const MotionEvent*>(inputEvent));
955 break;
956 default:
957 return true; // dispatch the event normally
958 }
959
960 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000961 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700962 return true; // dispatch the event normally
963 }
964
965 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700966 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700967 inputEventObj, policyFlags);
968 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
969 pass = true;
970 }
971 env->DeleteLocalRef(inputEventObj);
972 return pass;
973}
974
Jeff Brown1f245102010-11-18 20:53:46 -0800975void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
976 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100977 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700978 // Policy:
979 // - Ignore untrusted events and pass them along.
980 // - Ask the window manager what to do with normal events and trusted injected events.
981 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100982 bool interactive = mInteractive.load();
983 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700984 policyFlags |= POLICY_FLAG_INTERACTIVE;
985 }
Jeff Brown3122e442010-10-11 23:32:49 -0700986 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800987 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700988 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800989 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
990 jint wmActions;
991 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700992 wmActions = env->CallIntMethod(mServiceObj,
993 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700994 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800995 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
996 wmActions = 0;
997 }
998 android_view_KeyEvent_recycle(env, keyEventObj);
999 env->DeleteLocalRef(keyEventObj);
1000 } else {
Steve Block3762c312012-01-06 19:20:56 +00001001 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -07001002 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -07001003 }
1004
Jeff Brown56194eb2011-03-02 19:23:13 -08001005 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -07001006 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001007 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001008 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1009 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001010 }
1011}
1012
Jeff Brown56194eb2011-03-02 19:23:13 -08001013void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001014 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001015 // Policy:
1016 // - Ignore untrusted events and pass them along.
1017 // - No special filtering for injected events required at this time.
1018 // - Filter normal events based on screen state.
1019 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001020 bool interactive = mInteractive.load();
1021 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001022 policyFlags |= POLICY_FLAG_INTERACTIVE;
1023 }
Jeff Brown3122e442010-10-11 23:32:49 -07001024 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001025 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001026 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001027 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001028 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001029 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001030 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001031 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001032 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001033 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001034 wmActions = 0;
1035 }
1036
Jeff Brown56194eb2011-03-02 19:23:13 -08001037 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001038 }
Jeff Brown3122e442010-10-11 23:32:49 -07001039 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001040 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001041 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1042 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001043 }
1044}
1045
Jeff Brown56194eb2011-03-02 19:23:13 -08001046void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1047 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001048 if (wmActions & WM_ACTION_PASS_TO_USER) {
1049 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1050 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001051#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001052 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001053#endif
1054 }
1055}
1056
Jeff Brown905805a2011-10-12 13:57:59 -07001057nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -08001058 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -07001059 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001060 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001061 // Policy:
1062 // - Ignore untrusted events and pass them along.
1063 // - Filter normal events and trusted injected events through the window manager policy to
1064 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001065 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001066 if (policyFlags & POLICY_FLAG_TRUSTED) {
1067 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001068
Jeff Brown928e0542011-01-10 11:17:36 -08001069 // Note: inputWindowHandle may be null.
1070 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001071 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1072 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001073 jlong delayMillis = env->CallLongMethod(mServiceObj,
1074 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -08001075 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001076 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1077 android_view_KeyEvent_recycle(env, keyEventObj);
1078 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001079 if (!error) {
1080 if (delayMillis < 0) {
1081 result = -1;
1082 } else if (delayMillis > 0) {
1083 result = milliseconds_to_nanoseconds(delayMillis);
1084 }
1085 }
Jeff Brown1f245102010-11-18 20:53:46 -08001086 } else {
Steve Block3762c312012-01-06 19:20:56 +00001087 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001088 }
Jeff Brown928e0542011-01-10 11:17:36 -08001089 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -07001090 }
Jeff Brown1f245102010-11-18 20:53:46 -08001091 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001092}
1093
Jeff Brown928e0542011-01-10 11:17:36 -08001094bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001095 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001096 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001097 // Policy:
1098 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001099 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001100 if (policyFlags & POLICY_FLAG_TRUSTED) {
1101 JNIEnv* env = jniEnv();
1102
Jeff Brown928e0542011-01-10 11:17:36 -08001103 // Note: inputWindowHandle may be null.
1104 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001105 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1106 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001107 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1108 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -08001109 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001110 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001111 fallbackKeyEventObj = nullptr;
Jeff Brownda3d5a92011-03-29 15:11:34 -07001112 }
Jeff Brown1f245102010-11-18 20:53:46 -08001113 android_view_KeyEvent_recycle(env, keyEventObj);
1114 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001115
1116 if (fallbackKeyEventObj) {
1117 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1118 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1119 outFallbackKeyEvent)) {
1120 result = true;
1121 }
1122 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1123 env->DeleteLocalRef(fallbackKeyEventObj);
1124 }
Jeff Brown1f245102010-11-18 20:53:46 -08001125 } else {
Steve Block3762c312012-01-06 19:20:56 +00001126 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001127 }
Jeff Brown928e0542011-01-10 11:17:36 -08001128 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001129 }
Jeff Brown1f245102010-11-18 20:53:46 -08001130 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001131}
1132
Jeff Brown01ce2e92010-09-26 22:20:12 -07001133void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001134 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001135 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001136}
1137
Jeff Brown349703e2010-06-22 01:27:15 -07001138
Jeff Brownb88102f2010-09-08 11:49:43 -07001139bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1140 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001141 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001142 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001143 jboolean result = env->CallBooleanMethod(mServiceObj,
1144 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001145 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1146 result = false;
1147 }
Jeff Brown349703e2010-06-22 01:27:15 -07001148 return result;
1149}
1150
Jun Mukai19a56012015-11-24 11:25:52 -08001151void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001152 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001153 JNIEnv* env = jniEnv();
1154
1155 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1156 mServiceObj, gServiceClassInfo.getPointerIcon));
1157 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1158 return;
1159 }
1160
1161 PointerIcon pointerIcon;
1162 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1163 mContextObj, &pointerIcon);
1164 if (!status && !pointerIcon.isNullIcon()) {
1165 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1166 } else {
1167 *icon = SpriteIcon();
1168 }
1169}
1170
Jeff Brown2352b972011-04-12 22:39:53 -07001171void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001172 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001173 JNIEnv* env = jniEnv();
1174
1175 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1176 &outResources->spotHover);
1177 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1178 &outResources->spotTouch);
1179 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1180 &outResources->spotAnchor);
1181}
1182
Jun Mukai808196f2015-10-28 16:46:44 -07001183void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1184 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001185 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001186 JNIEnv* env = jniEnv();
1187
1188 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1189 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001190 PointerIcon pointerIcon;
1191 loadSystemIconAsSpriteWithPointerIcon(
1192 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1193 if (!pointerIcon.bitmapFrames.empty()) {
1194 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1195 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1196 animationData.durationPerFrame =
1197 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1198 animationData.animationFrames.reserve(numFrames);
1199 animationData.animationFrames.push_back(SpriteIcon(
1200 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1201 for (size_t i = 0; i < numFrames - 1; ++i) {
1202 animationData.animationFrames.push_back(SpriteIcon(
1203 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1204 }
1205 }
Jun Mukai1db53972015-09-11 18:08:31 -07001206 }
Jun Mukai808196f2015-10-28 16:46:44 -07001207 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1208 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001209}
1210
Jun Mukai5ec74202015-10-07 16:58:09 +09001211int32_t NativeInputManager::getDefaultPointerIconId() {
1212 return POINTER_ICON_STYLE_ARROW;
1213}
Jeff Brown83c09682010-12-23 17:50:18 -08001214
Jun Mukaid4eaef72015-10-30 15:54:33 -07001215int32_t NativeInputManager::getCustomPointerIconId() {
1216 return POINTER_ICON_STYLE_CUSTOM;
1217}
1218
Jeff Brown9c3cda02010-06-15 01:31:58 -07001219// ----------------------------------------------------------------------------
1220
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001221static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001222 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001223 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001224 if (messageQueue == nullptr) {
Jeff Brown864693462013-01-28 14:25:53 -08001225 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1226 return 0;
1227 }
1228
Jeff Brown603b4452012-04-06 17:39:41 -07001229 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1230 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001231 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001232 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001233}
1234
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001235static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001236 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001237
Jeff Brown4532e612012-04-05 14:27:12 -07001238 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001239 if (result) {
1240 jniThrowRuntimeException(env, "Input manager could not be started.");
1241 }
1242}
1243
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001244static void nativeSetDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
Santos Cordonee8931e2017-04-05 10:31:15 -07001245 jobjectArray viewportObjArray) {
1246 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001247 im->setDisplayViewports(env, viewportObjArray);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001248}
1249
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001250static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001251 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001252 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001253
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001254 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001255 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001256}
1257
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001258static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001259 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001260 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001261
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001262 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001263 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001264}
1265
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001266static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001267 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001268 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001269
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001270 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001271 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001272}
1273
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001274static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001275 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001276 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001277
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001278 int32_t* codes = env->GetIntArrayElements(keyCodes, nullptr);
1279 uint8_t* flags = env->GetBooleanArrayElements(outFlags, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001280 jsize numCodes = env->GetArrayLength(keyCodes);
1281 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001282 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001283 if (im->getInputManager()->getReader()->hasKeys(
1284 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1285 result = JNI_TRUE;
1286 } else {
1287 result = JNI_FALSE;
1288 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001289 } else {
1290 result = JNI_FALSE;
1291 }
1292
1293 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1294 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1295 return result;
1296}
1297
1298static void throwInputChannelNotInitialized(JNIEnv* env) {
1299 jniThrowException(env, "java/lang/IllegalStateException",
1300 "inputChannel is not initialized");
1301}
1302
Jeff Brown4532e612012-04-05 14:27:12 -07001303static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001304 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001305 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1306
Steve Block8564c8d2012-01-05 23:22:43 +00001307 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001308 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001309 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001310}
1311
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001312static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001313 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001314 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001315
1316 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1317 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001318 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001319 throwInputChannelNotInitialized(env);
1320 return;
1321 }
1322
Jeff Brown928e0542011-01-10 11:17:36 -08001323 sp<InputWindowHandle> inputWindowHandle =
1324 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001325
Jeff Brown4532e612012-04-05 14:27:12 -07001326 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001327 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001328 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001329 std::string message;
1330 message += StringPrintf("Failed to register input channel. status=%d", status);
1331 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001332 return;
1333 }
1334
Jeff Browna41ca772010-08-11 14:46:32 -07001335 if (! monitor) {
1336 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001337 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001338 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001339}
1340
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001341static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001342 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001343 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001344
1345 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1346 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001347 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001348 throwInputChannelNotInitialized(env);
1349 return;
1350 }
1351
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001352 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001353
Jeff Brown4532e612012-04-05 14:27:12 -07001354 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001355 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001356 std::string message;
1357 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1358 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001359 }
1360}
1361
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001362static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001363 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001364 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001365
Jeff Brown4532e612012-04-05 14:27:12 -07001366 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001367}
1368
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001369static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001370 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001371 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001372 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001373
Jeff Brown6ec402b2010-07-28 15:48:59 -07001374 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1375 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001376 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1377 if (status) {
1378 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1379 return INPUT_EVENT_INJECTION_FAILED;
1380 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001381
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001382 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001383 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001384 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001385 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001386 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1387 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001388 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1389 return INPUT_EVENT_INJECTION_FAILED;
1390 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001391
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001392 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001393 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001394 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001395 } else {
1396 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001397 return INPUT_EVENT_INJECTION_FAILED;
1398 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001399}
1400
Andrii Kulian112d0562016-03-08 10:44:22 -08001401static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1402 jlong ptr, jint deviceId) {
1403 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001404
Andrii Kulian112d0562016-03-08 10:44:22 -08001405 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1406}
1407
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001408static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hung39134b22018-08-14 11:58:28 +08001409 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001410 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001411
Arthur Hung39134b22018-08-14 11:58:28 +08001412 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001413}
1414
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001415static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001416 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001417 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001418
Jeff Brown4532e612012-04-05 14:27:12 -07001419 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001420}
1421
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001422static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1423 jboolean enabled) {
1424 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001425
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001426 im->setPointerCapture(enabled);
1427}
1428
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001429static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1430 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001431 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001432
Jeff Brown4532e612012-04-05 14:27:12 -07001433 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001434}
1435
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001436static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1437 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001438 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001439
Jeff Brown4532e612012-04-05 14:27:12 -07001440 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001441}
1442
Jeff Brown4532e612012-04-05 14:27:12 -07001443static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001444 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001445 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001446
1447 sp<InputChannel> fromChannel =
1448 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1449 sp<InputChannel> toChannel =
1450 android_view_InputChannel_getInputChannel(env, toChannelObj);
1451
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001452 if (fromChannel == nullptr || toChannel == nullptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001453 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001454 }
1455
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001456 if (im->getInputManager()->getDispatcher()->
1457 transferTouchFocus(fromChannel, toChannel)) {
1458 return JNI_TRUE;
1459 } else {
1460 return JNI_FALSE;
1461 }
Jeff Browne6504122010-09-27 14:52:15 -07001462}
1463
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001464static void nativeSetPointerSpeed(JNIEnv* /* env */,
1465 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001466 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001467
Jeff Brown4532e612012-04-05 14:27:12 -07001468 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001469}
1470
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001471static void nativeSetShowTouches(JNIEnv* /* env */,
1472 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001473 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001474
Jeff Brown4532e612012-04-05 14:27:12 -07001475 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001476}
1477
Jeff Brown037c33e2014-04-09 00:31:55 -07001478static void nativeSetInteractive(JNIEnv* env,
1479 jclass clazz, jlong ptr, jboolean interactive) {
1480 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1481
1482 im->setInteractive(interactive);
1483}
1484
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001485static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1486 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001487
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001488 im->reloadCalibration();
1489}
1490
Jeff Browna47425a2012-04-13 04:09:27 -07001491static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001492 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001493 jint repeat, jint token) {
1494 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1495
1496 size_t patternSize = env->GetArrayLength(patternObj);
1497 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001498 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001499 "which is more than the maximum supported size of %d.",
1500 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1501 return; // limit to reasonable size
1502 }
1503
1504 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001505 patternObj, nullptr));
Jeff Browna47425a2012-04-13 04:09:27 -07001506 nsecs_t pattern[patternSize];
1507 for (size_t i = 0; i < patternSize; i++) {
1508 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001509 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001510 }
1511 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1512
1513 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1514}
1515
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001516static void nativeCancelVibrate(JNIEnv* /* env */,
1517 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001518 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1519
1520 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1521}
1522
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001523static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1524 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001525 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1526
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001527 im->getInputManager()->getReader()->requestRefreshConfiguration(
1528 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1529}
1530
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001531static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1532 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001533 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1534
1535 im->getInputManager()->getReader()->requestRefreshConfiguration(
1536 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001537}
1538
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001539static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001540 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001541
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001542 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001543 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001544 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001545}
1546
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001547static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001548 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001549
Jeff Brown4532e612012-04-05 14:27:12 -07001550 im->getInputManager()->getReader()->monitor();
1551 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001552}
1553
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001554static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1555 jclass /* clazz */, jlong ptr, jint deviceId) {
1556 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1557
1558 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1559}
1560
1561static void nativeEnableInputDevice(JNIEnv* /* env */,
1562 jclass /* clazz */, jlong ptr, jint deviceId) {
1563 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1564
1565 im->setInputDeviceEnabled(deviceId, true);
1566}
1567
1568static void nativeDisableInputDevice(JNIEnv* /* env */,
1569 jclass /* clazz */, jlong ptr, jint deviceId) {
1570 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1571
1572 im->setInputDeviceEnabled(deviceId, false);
1573}
1574
Michael Wrighte051f6f2016-05-13 17:44:16 +01001575static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001576 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001577
Michael Wrighte051f6f2016-05-13 17:44:16 +01001578 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001579}
1580
Jun Mukai19a56012015-11-24 11:25:52 -08001581static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1582 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001583
Jun Mukai19a56012015-11-24 11:25:52 -08001584 im->reloadPointerIcons();
1585}
1586
Jun Mukaid4eaef72015-10-30 15:54:33 -07001587static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1588 jlong ptr, jobject iconObj) {
1589 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1590
1591 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001592 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1593 if (result) {
1594 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1595 return;
1596 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001597
1598 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001599 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1600 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1601 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1602 spriteIcon.bitmap.rowBytes(), 0, 0);
1603 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001604 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1605 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1606 im->setCustomPointerIcon(spriteIcon);
1607}
1608
Jeff Brown9c3cda02010-06-15 01:31:58 -07001609// ----------------------------------------------------------------------------
1610
Daniel Micay76f6a862015-09-19 17:31:01 -04001611static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001612 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001613 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001614 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001615 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001616 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001617 (void*) nativeStart },
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001618 { "nativeSetDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1619 (void*) nativeSetDisplayViewports },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001620 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001621 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001622 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001623 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001624 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001625 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001626 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001627 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001628 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001629 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001630 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001631 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001632 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001633 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001634 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001635 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001636 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001637 { "nativeToggleCapsLock", "(JI)V",
1638 (void*) nativeToggleCapsLock },
Arthur Hung39134b22018-08-14 11:58:28 +08001639 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001640 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001641 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001642 (void*) nativeSetFocusedApplication },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001643 { "nativeSetPointerCapture", "(JZ)V",
1644 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001645 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001646 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001647 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001648 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001649 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001650 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001651 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001652 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001653 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001654 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001655 { "nativeSetInteractive", "(JZ)V",
1656 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001657 { "nativeReloadCalibration", "(J)V",
1658 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001659 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001660 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001661 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001662 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001663 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001664 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001665 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001666 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001667 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001668 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001669 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001670 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001671 { "nativeIsInputDeviceEnabled", "(JI)Z",
1672 (void*) nativeIsInputDeviceEnabled },
1673 { "nativeEnableInputDevice", "(JI)V",
1674 (void*) nativeEnableInputDevice },
1675 { "nativeDisableInputDevice", "(JI)V",
1676 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001677 { "nativeSetPointerIconType", "(JI)V",
1678 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001679 { "nativeReloadPointerIcons", "(J)V",
1680 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001681 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1682 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001683};
1684
1685#define FIND_CLASS(var, className) \
1686 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001687 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001688
1689#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1690 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001691 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001692
1693#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1694 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001695 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001696
1697int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001698 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001699 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001700 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001701 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1702
Jeff Brown9c3cda02010-06-15 01:31:58 -07001703 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001704
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001705 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001706 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001707
Jeff Brown4532e612012-04-05 14:27:12 -07001708 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001709 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001710
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001711 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1712 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1713
Jeff Brown53384282012-08-20 20:16:01 -07001714 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1715 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001716
Jeff Brown4532e612012-04-05 14:27:12 -07001717 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1718 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001719
Jeff Brown4532e612012-04-05 14:27:12 -07001720 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001721 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001722 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001723
Jeff Brown4532e612012-04-05 14:27:12 -07001724 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001725 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1726
Jeff Brown4532e612012-04-05 14:27:12 -07001727 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001728 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001729
Michael Wright70af00a2014-09-03 19:30:20 -07001730 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1731 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001732
Jeff Brown4532e612012-04-05 14:27:12 -07001733 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001734 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001735 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001736
Jeff Brown4532e612012-04-05 14:27:12 -07001737 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001738 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001739 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001740
Jeff Brown4532e612012-04-05 14:27:12 -07001741 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001742 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001743
Jeff Brown4532e612012-04-05 14:27:12 -07001744 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001745 "getVirtualKeyQuietTimeMillis", "()I");
1746
Jeff Brown4532e612012-04-05 14:27:12 -07001747 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001748 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1749
Jeff Brown4532e612012-04-05 14:27:12 -07001750 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001751 "getKeyRepeatTimeout", "()I");
1752
Jeff Brown4532e612012-04-05 14:27:12 -07001753 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001754 "getKeyRepeatDelay", "()I");
1755
Jeff Brown4532e612012-04-05 14:27:12 -07001756 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001757 "getHoverTapTimeout", "()I");
1758
Jeff Brown4532e612012-04-05 14:27:12 -07001759 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001760 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001761
Jeff Brown4532e612012-04-05 14:27:12 -07001762 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001763 "getDoubleTapTimeout", "()I");
1764
Jeff Brown4532e612012-04-05 14:27:12 -07001765 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001766 "getLongPressTimeout", "()I");
1767
Jeff Brown4532e612012-04-05 14:27:12 -07001768 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001769 "getPointerLayer", "()I");
1770
Jeff Brown4532e612012-04-05 14:27:12 -07001771 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001772 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001773
Jeff Brown6ec6f792012-04-17 16:52:41 -07001774 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001775 "getKeyboardLayoutOverlay",
1776 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001777
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001778 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1779 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1780
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001781 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1782 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001783 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001784
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001785 // InputDevice
1786
1787 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1788 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1789
Jeff Brown6ec402b2010-07-28 15:48:59 -07001790 // KeyEvent
1791
1792 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001793 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1794
Jeff Brown8d608662010-08-30 03:02:23 -07001795 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001796
1797 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001798 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001799
RoboErikfb290df2013-12-16 11:27:55 -08001800 // InputDeviceIdentifier
1801
1802 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1803 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1804 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1805 "<init>", "(Ljava/lang/String;II)V");
1806
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001807 // TouchCalibration
1808
1809 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1810 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1811
1812 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1813 "getAffineTransform", "()[F");
1814
Jeff Brown46b9ac02010-04-22 18:58:52 -07001815 return 0;
1816}
1817
Jeff Brown46b9ac02010-04-22 18:58:52 -07001818} /* namespace android */