blob: b67e1da795faaeda66a99b59001cfb8de4e287a6 [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
Robert Carr788f5742018-07-30 17:46:45 -070045#include <binder/IServiceManager.h>
46
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070048#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080049
Michael Wrightd6b473712014-02-10 15:56:36 -080050#include <inputflinger/InputManager.h>
51
Jeff Brown05dc66a2011-03-02 14:41:58 -080052#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070053#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080054#include <android_view_KeyEvent.h>
55#include <android_view_MotionEvent.h>
56#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070057#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080058#include <android/graphics/GraphicsJNI.h>
59
Steven Moreland2279b252017-07-19 09:50:45 -070060#include <nativehelper/ScopedLocalRef.h>
61#include <nativehelper/ScopedPrimitiveArray.h>
62#include <nativehelper/ScopedUtfChars.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070063
Jeff Brown4f8ecd82012-06-18 18:29:13 -070064#include "com_android_server_power_PowerManagerService.h"
Robert Carre1db3202018-07-23 15:24:59 -070065#include "android_hardware_input_InputApplicationHandle.h"
66#include "android_hardware_input_InputWindowHandle.h"
Santos Cordonee8931e2017-04-05 10:31:15 -070067#include "android_hardware_display_DisplayViewport.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070068
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010069#include <vector>
70
Michael Wrighta4051212015-07-23 17:04:40 +010071#define INDENT " "
72
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080073using android::base::StringPrintf;
74
Jeff Brown46b9ac02010-04-22 18:58:52 -070075namespace android {
76
Jeff Brown1a84fd12011-06-02 01:26:32 -070077// The exponent used to calculate the pointer speed scaling factor.
78// The scaling factor is calculated as 2 ^ (speed * exponent),
79// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070080static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070081
Jeff Brown46b9ac02010-04-22 18:58:52 -070082static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070083 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070084 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070085 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070086 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070087 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070088 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070089 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070090 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070091 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070092 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070093 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080094 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070095 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080096 jmethodID getKeyRepeatTimeout;
97 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070098 jmethodID getHoverTapTimeout;
99 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -0700100 jmethodID getDoubleTapTimeout;
101 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -0800102 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800103 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700104 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700105 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800106 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -0700107} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700108
109static struct {
110 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700111} gInputDeviceClassInfo;
112
113static struct {
114 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700115} gKeyEventClassInfo;
116
117static struct {
118 jclass clazz;
119} gMotionEventClassInfo;
120
RoboErikfb290df2013-12-16 11:27:55 -0800121static struct {
122 jclass clazz;
123 jmethodID constructor;
124} gInputDeviceIdentifierInfo;
125
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800126static struct {
127 jclass clazz;
128 jmethodID getAffineTransform;
129} gTouchCalibrationClassInfo;
130
RoboErikfb290df2013-12-16 11:27:55 -0800131
Jeff Brown928e0542011-01-10 11:17:36 -0800132
133// --- Global functions ---
134
Jeff Brown214eaf42011-05-26 19:17:02 -0700135template<typename T>
136inline static T min(const T& a, const T& b) {
137 return a < b ? a : b;
138}
139
140template<typename T>
141inline static T max(const T& a, const T& b) {
142 return a > b ? a : b;
143}
144
Michael Wrighta4051212015-07-23 17:04:40 +0100145static inline const char* toString(bool value) {
146 return value ? "true" : "false";
147}
148
Jeff Brown928e0542011-01-10 11:17:36 -0800149static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
150 const sp<InputApplicationHandle>& inputApplicationHandle) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100151 if (inputApplicationHandle == nullptr) {
152 return nullptr;
Jeff Brown928e0542011-01-10 11:17:36 -0800153 }
154 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
155 getInputApplicationHandleObjLocalRef(env);
156}
157
158static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
159 const sp<InputWindowHandle>& inputWindowHandle) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100160 if (inputWindowHandle == nullptr) {
161 return nullptr;
Jeff Brown928e0542011-01-10 11:17:36 -0800162 }
163 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
164 getInputWindowHandleObjLocalRef(env);
165}
166
Jun Mukai808196f2015-10-28 16:46:44 -0700167static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
168 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700169 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700170 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700171 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400172 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
173 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
174 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
175 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
176 bitmapCopy->rowBytes(), 0, 0);
177 }
Jun Mukai808196f2015-10-28 16:46:44 -0700178 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
179 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700180 }
181}
182
Jun Mukai808196f2015-10-28 16:46:44 -0700183static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
184 SpriteIcon* outSpriteIcon) {
185 PointerIcon pointerIcon;
186 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
187}
188
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100189static void updatePointerControllerFromViewport(
190 sp<PointerController> controller, const DisplayViewport* const viewport) {
191 if (controller != nullptr && viewport != nullptr) {
192 const int32_t width = viewport->logicalRight - viewport->logicalLeft;
193 const int32_t height = viewport->logicalBottom - viewport->logicalTop;
194 controller->setDisplayViewport(width, height, viewport->orientation);
195 }
196}
197
Jeff Brown905805a2011-10-12 13:57:59 -0700198enum {
199 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700200};
201
Jeff Brown928e0542011-01-10 11:17:36 -0800202
203// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800204
Jeff Brown9c3cda02010-06-15 01:31:58 -0700205class NativeInputManager : public virtual RefBase,
206 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700207 public virtual InputDispatcherPolicyInterface,
208 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700209protected:
210 virtual ~NativeInputManager();
211
212public:
Jeff Brown4532e612012-04-05 14:27:12 -0700213 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700214
215 inline sp<InputManager> getInputManager() const { return mInputManager; }
216
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800217 void dump(std::string& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700218
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100219 void setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700220
Jeff Brown7fbdc842010-06-17 20:52:56 -0700221 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Arthur Hungbe5ce212018-09-13 18:41:56 +0800222 const sp<InputWindowHandle>& inputWindowHandle, int32_t displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700223 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
224
Arthur Hung39134b22018-08-14 11:58:28 +0800225 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray, int32_t displayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800226 void setFocusedApplication(JNIEnv* env, int32_t displayId, jobject applicationHandleObj);
227 void setFocusedDisplay(JNIEnv* env, int32_t displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700228 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800229 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700230 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700231 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700232 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700233 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800234 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100235 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800236 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700237 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800238 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700239
Jeff Brown9c3cda02010-06-15 01:31:58 -0700240 /* --- InputReaderPolicyInterface implementation --- */
241
Jeff Brown214eaf42011-05-26 19:17:02 -0700242 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800243 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700244 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800245 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100246 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700247 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
248 jfloatArray matrixArr);
249 virtual TouchAffineTransformation getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100250 const std::string& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700251
252 /* --- InputDispatcherPolicyInterface implementation --- */
253
Jeff Brownbcc046a2012-09-27 20:46:43 -0700254 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700255 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700256 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700257 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700258 const sp<InputWindowHandle>& inputWindowHandle,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800259 const std::string& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800260 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700261 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700262 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800263 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800264 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700265 virtual nsecs_t interceptKeyBeforeDispatching(
266 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700267 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800268 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800269 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700270 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700271 virtual bool checkInjectEventsPermissionNonReentrant(
272 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700273
Jeff Brown2352b972011-04-12 22:39:53 -0700274 /* --- PointerControllerPolicyInterface implementation --- */
275
Jun Mukai19a56012015-11-24 11:25:52 -0800276 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700277 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700278 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
279 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900280 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700281 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700282
Jeff Brown9c3cda02010-06-15 01:31:58 -0700283private:
284 sp<InputManager> mInputManager;
285
Jeff Brown2352b972011-04-12 22:39:53 -0700286 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700287 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800288 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700289
Jeff Brown83c09682010-12-23 17:50:18 -0800290 Mutex mLock;
291 struct Locked {
292 // Display size information.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100293 std::vector<DisplayViewport> viewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800294
Jeff Brown05dc66a2011-03-02 14:41:58 -0800295 // System UI visibility.
296 int32_t systemUiVisibility;
297
Jeff Brown1a84fd12011-06-02 01:26:32 -0700298 // Pointer speed.
299 int32_t pointerSpeed;
300
Jeff Brown474dcb52011-06-14 20:22:50 -0700301 // True if pointer gestures are enabled.
302 bool pointerGesturesEnabled;
303
Jeff Browndaf4a122011-08-26 17:14:14 -0700304 // Show touches feature enable/disable.
305 bool showTouches;
306
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800307 // Pointer capture feature enable/disable.
308 bool pointerCapture;
309
Jeff Brown5541de92011-04-11 11:54:25 -0700310 // Sprite controller singleton, created on first use.
311 sp<SpriteController> spriteController;
312
Jeff Brown83c09682010-12-23 17:50:18 -0800313 // Pointer controller singleton, created and destroyed as needed.
314 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700315
316 // Input devices to be disabled
317 SortedVector<int32_t> disabledInputDevices;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100318 } mLocked GUARDED_BY(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700319
Michael Wrighta4051212015-07-23 17:04:40 +0100320 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700321
Jeff Brown2352b972011-04-12 22:39:53 -0700322 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800323 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700324 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800325
Jeff Brownb88102f2010-09-08 11:49:43 -0700326 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700327
Jeff Brown9c3cda02010-06-15 01:31:58 -0700328 static inline JNIEnv* jniEnv() {
329 return AndroidRuntime::getJNIEnv();
330 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700331};
332
Jeff Brown928e0542011-01-10 11:17:36 -0800333
Jeff Brown9c3cda02010-06-15 01:31:58 -0700334
Jeff Brown2352b972011-04-12 22:39:53 -0700335NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700336 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700337 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700338 JNIEnv* env = jniEnv();
339
Jeff Brown2352b972011-04-12 22:39:53 -0700340 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700341 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700342
Jeff Brown83c09682010-12-23 17:50:18 -0800343 {
344 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800345 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700346 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700347 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700348 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800349 mLocked.pointerCapture = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800350 }
Michael Wrighta4051212015-07-23 17:04:40 +0100351 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800352
Jeff Brown9c3cda02010-06-15 01:31:58 -0700353 sp<EventHub> eventHub = new EventHub();
354 mInputManager = new InputManager(eventHub, this, this);
355}
356
357NativeInputManager::~NativeInputManager() {
358 JNIEnv* env = jniEnv();
359
Jeff Brown2352b972011-04-12 22:39:53 -0700360 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700361 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700362}
363
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800364void NativeInputManager::dump(std::string& dump) {
365 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100366 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800367 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100368 }
369 {
370 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800371 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100372 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800373 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
374 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100375 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800376 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
377 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100378 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800379 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100380
Jeff Brownb88102f2010-09-08 11:49:43 -0700381 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800382 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383
Jeff Brownb88102f2010-09-08 11:49:43 -0700384 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800385 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700386}
387
Jeff Brown7fbdc842010-06-17 20:52:56 -0700388bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700389 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000390 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700391 LOGE_EX(env);
392 env->ExceptionClear();
393 return true;
394 }
395 return false;
396}
397
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100398static const DisplayViewport* findInternalViewport(const std::vector<DisplayViewport>& viewports) {
399 for (const DisplayViewport& v : viewports) {
400 if (v.type == ViewportType::VIEWPORT_INTERNAL) {
401 return &v;
402 }
403 }
404 return nullptr;
405}
406
407void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
408 std::vector<DisplayViewport> viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700409
410 if (viewportObjArray) {
411 jsize length = env->GetArrayLength(viewportObjArray);
412 for (jsize i = 0; i < length; i++) {
413 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
414 if (! viewportObj) {
415 break; // found null element indicating end of used portion of the array
416 }
417
418 DisplayViewport viewport;
419 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100420 ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
421 viewports.push_back(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -0700422
423 env->DeleteLocalRef(viewportObj);
424 }
425 }
426
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100427 const DisplayViewport* newInternalViewport = findInternalViewport(viewports);
Santos Cordonee8931e2017-04-05 10:31:15 -0700428 {
429 AutoMutex _l(mLock);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100430 const DisplayViewport* oldInternalViewport = findInternalViewport(mLocked.viewports);
431 // Internal viewport has changed if there wasn't one earlier, and there is one now, or,
432 // if they are different.
433 const bool internalViewportChanged = (newInternalViewport != nullptr) &&
Siarhei Vishniakoue5bf8662018-10-04 09:33:24 -0700434 (oldInternalViewport == nullptr || (*oldInternalViewport != *newInternalViewport));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100435 if (internalViewportChanged) {
436 sp<PointerController> controller = mLocked.pointerController.promote();
437 updatePointerControllerFromViewport(controller, newInternalViewport);
438 }
439 mLocked.viewports = viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700440 }
441
442 mInputManager->getReader()->requestRefreshConfiguration(
443 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
444}
445
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700446status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Arthur Hungbe5ce212018-09-13 18:41:56 +0800447 const sp<InputChannel>& inputChannel, const sp<InputWindowHandle>& inputWindowHandle,
448 int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100449 ATRACE_CALL();
Arthur Hungbe5ce212018-09-13 18:41:56 +0800450 return mInputManager->getDispatcher()->registerInputChannel(inputChannel, inputWindowHandle,
451 displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700452}
453
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700454status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700455 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100456 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700457 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700458}
459
Jeff Brown214eaf42011-05-26 19:17:02 -0700460void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100461 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700462 JNIEnv* env = jniEnv();
463
Jeff Brown4532e612012-04-05 14:27:12 -0700464 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
465 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700466 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
467 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
468 }
469
470 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700471 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
472 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700473 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
474 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700475 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700476 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100477 const char* deviceNameChars = env->GetStringUTFChars(item, nullptr);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100478 outConfig->excludedDeviceNames.push_back(deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700479 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700480 env->DeleteLocalRef(item);
481 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700482 env->DeleteLocalRef(excludedDeviceNames);
483 }
484
Jeff Brown4532e612012-04-05 14:27:12 -0700485 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
486 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700487 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700488 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
489 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700490 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700491 jint longPressTimeout = env->CallIntMethod(mServiceObj,
492 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700493 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700494 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700495
496 // We must ensure that the tap-drag interval is significantly shorter than
497 // the long-press timeout because the tap is held down for the entire duration
498 // of the double-tap timeout.
499 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700500 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700501 outConfig->pointerGestureTapDragInterval =
502 milliseconds_to_nanoseconds(tapDragInterval);
503 }
504 }
505 }
506
Jeff Brown4532e612012-04-05 14:27:12 -0700507 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
508 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700509 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
510 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700511 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700512
513 { // acquire lock
514 AutoMutex _l(mLock);
515
516 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
517 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700518 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700519
Jeff Browndaf4a122011-08-26 17:14:14 -0700520 outConfig->showTouches = mLocked.showTouches;
521
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800522 outConfig->pointerCapture = mLocked.pointerCapture;
523
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100524 outConfig->setDisplayViewports(mLocked.viewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700525
526 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700527 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700528}
529
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700530sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100531 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800532 AutoMutex _l(mLock);
533
534 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100535 if (controller == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700536 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800537
Jeff Brown2352b972011-04-12 22:39:53 -0700538 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800539 mLocked.pointerController = controller;
540
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100541 const DisplayViewport* internalViewport = findInternalViewport(mLocked.viewports);
542 updatePointerControllerFromViewport(controller, internalViewport);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800543
Jeff Brown2352b972011-04-12 22:39:53 -0700544 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800545 }
546 return controller;
547}
548
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100549void NativeInputManager::ensureSpriteControllerLocked() REQUIRES(mLock) {
550 if (mLocked.spriteController == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700551 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700552 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700553 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
554 layer = -1;
555 }
556 mLocked.spriteController = new SpriteController(mLooper, layer);
557 }
558}
559
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700560void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100561 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700562 JNIEnv* env = jniEnv();
563
564 size_t count = inputDevices.size();
565 jobjectArray inputDevicesObjArray = env->NewObjectArray(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100566 count, gInputDeviceClassInfo.clazz, nullptr);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700567 if (inputDevicesObjArray) {
568 bool error = false;
569 for (size_t i = 0; i < count; i++) {
570 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
571 if (!inputDeviceObj) {
572 error = true;
573 break;
574 }
575
576 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
577 env->DeleteLocalRef(inputDeviceObj);
578 }
579
580 if (!error) {
581 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
582 inputDevicesObjArray);
583 }
584
585 env->DeleteLocalRef(inputDevicesObjArray);
586 }
587
588 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
589}
590
Jeff Brown6ec6f792012-04-17 16:52:41 -0700591sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800592 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100593 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700594 JNIEnv* env = jniEnv();
595
596 sp<KeyCharacterMap> result;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100597 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
RoboErikfb290df2013-12-16 11:27:55 -0800598 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
599 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
600 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700601 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800602 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700603 if (arrayObj.get()) {
604 ScopedLocalRef<jstring> filenameObj(env,
605 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
606 ScopedLocalRef<jstring> contentsObj(env,
607 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
608 ScopedUtfChars filenameChars(env, filenameObj.get());
609 ScopedUtfChars contentsChars(env, contentsObj.get());
610
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100611 KeyCharacterMap::loadContents(filenameChars.c_str(),
612 contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700613 }
614 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
615 return result;
616}
617
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100618std::string NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100619 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700620 JNIEnv* env = jniEnv();
621
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100622 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.c_str()));
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700623 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
624 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100625 std::string result;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700626 if (aliasObj.get()) {
627 ScopedUtfChars aliasChars(env, aliasObj.get());
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100628 result = aliasChars.c_str();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700629 }
630 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
631 return result;
632}
633
Jeff Brownbcc046a2012-09-27 20:46:43 -0700634void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700635 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700636#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700637 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
638 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700639#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100640 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700641
642 JNIEnv* env = jniEnv();
643
Jeff Brown53384282012-08-20 20:16:01 -0700644 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700645 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700646 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700647}
648
Jeff Brown9c3cda02010-06-15 01:31:58 -0700649void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
650#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000651 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700652#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100653 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700654
655 JNIEnv* env = jniEnv();
656
Jeff Brown4532e612012-04-05 14:27:12 -0700657 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700658 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700659}
660
Jeff Brown519e0242010-09-15 15:18:56 -0700661nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800662 const sp<InputWindowHandle>& inputWindowHandle, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700663#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000664 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700665#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100666 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700667
668 JNIEnv* env = jniEnv();
669
Jeff Brown928e0542011-01-10 11:17:36 -0800670 jobject inputApplicationHandleObj =
671 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
672 jobject inputWindowHandleObj =
673 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800674 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700675
Jeff Brown4532e612012-04-05 14:27:12 -0700676 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700677 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
678 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700679 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
680 newTimeout = 0; // abort dispatch
681 } else {
682 assert(newTimeout >= 0);
683 }
684
Jeff Brownbd181bb2013-09-10 16:44:24 -0700685 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800686 env->DeleteLocalRef(inputWindowHandleObj);
687 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700688 return newTimeout;
689}
690
Jeff Brown928e0542011-01-10 11:17:36 -0800691void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700692#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000693 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700694#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100695 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700696
Jeff Brown7fbdc842010-06-17 20:52:56 -0700697 JNIEnv* env = jniEnv();
698
Jeff Brown928e0542011-01-10 11:17:36 -0800699 jobject inputWindowHandleObj =
700 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
701 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700702 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800703 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700704 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
705
Jeff Brown928e0542011-01-10 11:17:36 -0800706 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700707 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700708}
709
Jeff Brown214eaf42011-05-26 19:17:02 -0700710void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100711 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700712 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800713
Jeff Brown4532e612012-04-05 14:27:12 -0700714 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
715 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700716 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
717 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
718 }
Jeff Browna4547672011-03-02 21:38:11 -0800719
Jeff Brown4532e612012-04-05 14:27:12 -0700720 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
721 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700722 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
723 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
724 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700725}
726
Arthur Hung39134b22018-08-14 11:58:28 +0800727void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
728 int32_t displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700729 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700730
Jeff Brown9302c872011-07-13 22:51:29 -0700731 if (windowHandleObjArray) {
732 jsize length = env->GetArrayLength(windowHandleObjArray);
733 for (jsize i = 0; i < length; i++) {
734 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
735 if (! windowHandleObj) {
736 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700737 }
Jeff Brown9302c872011-07-13 22:51:29 -0700738
739 sp<InputWindowHandle> windowHandle =
740 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100741 if (windowHandle != nullptr) {
Jeff Brown9302c872011-07-13 22:51:29 -0700742 windowHandles.push(windowHandle);
743 }
744 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700745 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700746 }
Jeff Brown349703e2010-06-22 01:27:15 -0700747
Arthur Hung39134b22018-08-14 11:58:28 +0800748 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700749
750 // Do this after the dispatcher has updated the window handle state.
751 bool newPointerGesturesEnabled = true;
752 size_t numWindows = windowHandles.size();
753 for (size_t i = 0; i < numWindows; i++) {
754 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700755 const InputWindowInfo* windowInfo = windowHandle->getInfo();
756 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
757 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700758 newPointerGesturesEnabled = false;
759 }
760 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700761
762 uint32_t changes = 0;
763 { // acquire lock
764 AutoMutex _l(mLock);
765
766 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
767 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
768 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
769 }
770 } // release lock
771
772 if (changes) {
773 mInputManager->getReader()->requestRefreshConfiguration(changes);
774 }
Jeff Brown349703e2010-06-22 01:27:15 -0700775}
776
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800777void NativeInputManager::setFocusedApplication(JNIEnv* env, int32_t displayId,
778 jobject applicationHandleObj) {
Jeff Brown9302c872011-07-13 22:51:29 -0700779 sp<InputApplicationHandle> applicationHandle =
780 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800781 mInputManager->getDispatcher()->setFocusedApplication(displayId, applicationHandle);
782}
783
784void NativeInputManager::setFocusedDisplay(JNIEnv* env, int32_t displayId) {
785 mInputManager->getDispatcher()->setFocusedDisplay(displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700786}
787
788void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700789 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700790}
791
Jeff Brown05dc66a2011-03-02 14:41:58 -0800792void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
793 AutoMutex _l(mLock);
794
795 if (mLocked.systemUiVisibility != visibility) {
796 mLocked.systemUiVisibility = visibility;
797
798 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100799 if (controller != nullptr) {
Jeff Brown2352b972011-04-12 22:39:53 -0700800 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800801 }
802 }
803}
804
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100805void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller)
806 REQUIRES(mLock) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800807 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700808 controller->setInactivityTimeout(lightsOut
809 ? PointerController::INACTIVITY_TIMEOUT_SHORT
810 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800811}
812
Jeff Brown1a84fd12011-06-02 01:26:32 -0700813void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700814 { // acquire lock
815 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700816
Jeff Brown474dcb52011-06-14 20:22:50 -0700817 if (mLocked.pointerSpeed == speed) {
818 return;
819 }
820
Steve Block6215d3f2012-01-04 20:05:49 +0000821 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700822 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700823 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700824
Jeff Brown474dcb52011-06-14 20:22:50 -0700825 mInputManager->getReader()->requestRefreshConfiguration(
826 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700827}
828
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700829void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
830 { // acquire lock
831 AutoMutex _l(mLock);
832
833 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
834 bool currentlyEnabled = index < 0;
835 if (!enabled && currentlyEnabled) {
836 mLocked.disabledInputDevices.add(deviceId);
837 }
838 if (enabled && !currentlyEnabled) {
839 mLocked.disabledInputDevices.remove(deviceId);
840 }
841 } // release lock
842
843 mInputManager->getReader()->requestRefreshConfiguration(
844 InputReaderConfiguration::CHANGE_ENABLED_STATE);
845}
846
Jeff Browndaf4a122011-08-26 17:14:14 -0700847void NativeInputManager::setShowTouches(bool enabled) {
848 { // acquire lock
849 AutoMutex _l(mLock);
850
851 if (mLocked.showTouches == enabled) {
852 return;
853 }
854
Steve Block6215d3f2012-01-04 20:05:49 +0000855 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700856 mLocked.showTouches = enabled;
857 } // release lock
858
859 mInputManager->getReader()->requestRefreshConfiguration(
860 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
861}
862
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800863void NativeInputManager::setPointerCapture(bool enabled) {
864 { // acquire lock
865 AutoMutex _l(mLock);
866
867 if (mLocked.pointerCapture == enabled) {
868 return;
869 }
870
871 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
872 mLocked.pointerCapture = enabled;
873 } // release lock
874
875 mInputManager->getReader()->requestRefreshConfiguration(
876 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
877}
878
Jeff Brown037c33e2014-04-09 00:31:55 -0700879void NativeInputManager::setInteractive(bool interactive) {
880 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700881}
882
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800883void NativeInputManager::reloadCalibration() {
884 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100885 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800886}
887
Michael Wrighte051f6f2016-05-13 17:44:16 +0100888void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800889 AutoMutex _l(mLock);
890 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100891 if (controller != nullptr) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100892 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800893 }
894}
895
896void NativeInputManager::reloadPointerIcons() {
897 AutoMutex _l(mLock);
898 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100899 if (controller != nullptr) {
Jun Mukai19a56012015-11-24 11:25:52 -0800900 controller->reloadPointerResources();
901 }
Jun Mukai1db53972015-09-11 18:08:31 -0700902}
903
Jun Mukaid4eaef72015-10-30 15:54:33 -0700904void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
905 AutoMutex _l(mLock);
906 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100907 if (controller != nullptr) {
Jun Mukaid4eaef72015-10-30 15:54:33 -0700908 controller->setCustomPointerIcon(icon);
909 }
910}
911
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800912TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
913 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100914 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800915 ScopedFloatArrayRO matrix(env, matrixArr);
916 assert(matrix.size() == 6);
917
918 TouchAffineTransformation transform;
919 transform.x_scale = matrix[0];
920 transform.x_ymix = matrix[1];
921 transform.x_offset = matrix[2];
922 transform.y_xmix = matrix[3];
923 transform.y_scale = matrix[4];
924 transform.y_offset = matrix[5];
925
926 return transform;
927}
928
929TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100930 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800931 JNIEnv* env = jniEnv();
932
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100933 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.c_str()));
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800934
935 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700936 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
937 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800938
939 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
940 gTouchCalibrationClassInfo.getAffineTransform));
941
942 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
943
944 env->DeleteLocalRef(matrixArr);
945 env->DeleteLocalRef(cal);
946
947 return transform;
948}
949
Jeff Brown0029c662011-03-30 02:25:18 -0700950bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100951 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700952 jobject inputEventObj;
953
954 JNIEnv* env = jniEnv();
955 switch (inputEvent->getType()) {
956 case AINPUT_EVENT_TYPE_KEY:
957 inputEventObj = android_view_KeyEvent_fromNative(env,
958 static_cast<const KeyEvent*>(inputEvent));
959 break;
960 case AINPUT_EVENT_TYPE_MOTION:
961 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
962 static_cast<const MotionEvent*>(inputEvent));
963 break;
964 default:
965 return true; // dispatch the event normally
966 }
967
968 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000969 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700970 return true; // dispatch the event normally
971 }
972
973 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700974 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700975 inputEventObj, policyFlags);
976 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
977 pass = true;
978 }
979 env->DeleteLocalRef(inputEventObj);
980 return pass;
981}
982
Jeff Brown1f245102010-11-18 20:53:46 -0800983void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
984 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100985 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700986 // Policy:
987 // - Ignore untrusted events and pass them along.
988 // - Ask the window manager what to do with normal events and trusted injected events.
989 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100990 bool interactive = mInteractive.load();
991 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700992 policyFlags |= POLICY_FLAG_INTERACTIVE;
993 }
Jeff Brown3122e442010-10-11 23:32:49 -0700994 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800995 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700996 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800997 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
998 jint wmActions;
999 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001000 wmActions = env->CallIntMethod(mServiceObj,
1001 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -07001002 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001003 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
1004 wmActions = 0;
1005 }
1006 android_view_KeyEvent_recycle(env, keyEventObj);
1007 env->DeleteLocalRef(keyEventObj);
1008 } else {
Steve Block3762c312012-01-06 19:20:56 +00001009 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -07001010 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -07001011 }
1012
Jeff Brown56194eb2011-03-02 19:23:13 -08001013 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -07001014 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001015 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001016 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1017 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001018 }
1019}
1020
Jeff Brown56194eb2011-03-02 19:23:13 -08001021void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001022 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001023 // Policy:
1024 // - Ignore untrusted events and pass them along.
1025 // - No special filtering for injected events required at this time.
1026 // - Filter normal events based on screen state.
1027 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001028 bool interactive = mInteractive.load();
1029 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001030 policyFlags |= POLICY_FLAG_INTERACTIVE;
1031 }
Jeff Brown3122e442010-10-11 23:32:49 -07001032 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001033 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001034 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001035 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001036 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001037 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001038 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001039 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001040 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001041 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001042 wmActions = 0;
1043 }
1044
Jeff Brown56194eb2011-03-02 19:23:13 -08001045 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001046 }
Jeff Brown3122e442010-10-11 23:32:49 -07001047 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001048 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001049 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1050 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001051 }
1052}
1053
Jeff Brown56194eb2011-03-02 19:23:13 -08001054void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1055 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001056 if (wmActions & WM_ACTION_PASS_TO_USER) {
1057 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1058 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001059#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001060 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001061#endif
1062 }
1063}
1064
Jeff Brown905805a2011-10-12 13:57:59 -07001065nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -08001066 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -07001067 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001068 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001069 // Policy:
1070 // - Ignore untrusted events and pass them along.
1071 // - Filter normal events and trusted injected events through the window manager policy to
1072 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001073 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001074 if (policyFlags & POLICY_FLAG_TRUSTED) {
1075 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001076
Jeff Brown928e0542011-01-10 11:17:36 -08001077 // Note: inputWindowHandle may be null.
1078 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001079 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1080 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001081 jlong delayMillis = env->CallLongMethod(mServiceObj,
1082 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -08001083 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001084 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1085 android_view_KeyEvent_recycle(env, keyEventObj);
1086 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001087 if (!error) {
1088 if (delayMillis < 0) {
1089 result = -1;
1090 } else if (delayMillis > 0) {
1091 result = milliseconds_to_nanoseconds(delayMillis);
1092 }
1093 }
Jeff Brown1f245102010-11-18 20:53:46 -08001094 } else {
Steve Block3762c312012-01-06 19:20:56 +00001095 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001096 }
Jeff Brown928e0542011-01-10 11:17:36 -08001097 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -07001098 }
Jeff Brown1f245102010-11-18 20:53:46 -08001099 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001100}
1101
Jeff Brown928e0542011-01-10 11:17:36 -08001102bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001103 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001104 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001105 // Policy:
1106 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001107 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001108 if (policyFlags & POLICY_FLAG_TRUSTED) {
1109 JNIEnv* env = jniEnv();
1110
Jeff Brown928e0542011-01-10 11:17:36 -08001111 // Note: inputWindowHandle may be null.
1112 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001113 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1114 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001115 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1116 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -08001117 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001118 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001119 fallbackKeyEventObj = nullptr;
Jeff Brownda3d5a92011-03-29 15:11:34 -07001120 }
Jeff Brown1f245102010-11-18 20:53:46 -08001121 android_view_KeyEvent_recycle(env, keyEventObj);
1122 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001123
1124 if (fallbackKeyEventObj) {
1125 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1126 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1127 outFallbackKeyEvent)) {
1128 result = true;
1129 }
1130 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1131 env->DeleteLocalRef(fallbackKeyEventObj);
1132 }
Jeff Brown1f245102010-11-18 20:53:46 -08001133 } else {
Steve Block3762c312012-01-06 19:20:56 +00001134 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001135 }
Jeff Brown928e0542011-01-10 11:17:36 -08001136 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001137 }
Jeff Brown1f245102010-11-18 20:53:46 -08001138 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001139}
1140
Jeff Brown01ce2e92010-09-26 22:20:12 -07001141void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001142 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001143 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001144}
1145
Jeff Brown349703e2010-06-22 01:27:15 -07001146
Jeff Brownb88102f2010-09-08 11:49:43 -07001147bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1148 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001149 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001150 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001151 jboolean result = env->CallBooleanMethod(mServiceObj,
1152 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001153 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1154 result = false;
1155 }
Jeff Brown349703e2010-06-22 01:27:15 -07001156 return result;
1157}
1158
Jun Mukai19a56012015-11-24 11:25:52 -08001159void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001160 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001161 JNIEnv* env = jniEnv();
1162
1163 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1164 mServiceObj, gServiceClassInfo.getPointerIcon));
1165 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1166 return;
1167 }
1168
1169 PointerIcon pointerIcon;
1170 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1171 mContextObj, &pointerIcon);
1172 if (!status && !pointerIcon.isNullIcon()) {
1173 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1174 } else {
1175 *icon = SpriteIcon();
1176 }
1177}
1178
Jeff Brown2352b972011-04-12 22:39:53 -07001179void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001180 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001181 JNIEnv* env = jniEnv();
1182
1183 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1184 &outResources->spotHover);
1185 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1186 &outResources->spotTouch);
1187 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1188 &outResources->spotAnchor);
1189}
1190
Jun Mukai808196f2015-10-28 16:46:44 -07001191void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1192 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001193 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001194 JNIEnv* env = jniEnv();
1195
1196 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1197 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001198 PointerIcon pointerIcon;
1199 loadSystemIconAsSpriteWithPointerIcon(
1200 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1201 if (!pointerIcon.bitmapFrames.empty()) {
1202 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1203 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1204 animationData.durationPerFrame =
1205 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1206 animationData.animationFrames.reserve(numFrames);
1207 animationData.animationFrames.push_back(SpriteIcon(
1208 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1209 for (size_t i = 0; i < numFrames - 1; ++i) {
1210 animationData.animationFrames.push_back(SpriteIcon(
1211 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1212 }
1213 }
Jun Mukai1db53972015-09-11 18:08:31 -07001214 }
Jun Mukai808196f2015-10-28 16:46:44 -07001215 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1216 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001217}
1218
Jun Mukai5ec74202015-10-07 16:58:09 +09001219int32_t NativeInputManager::getDefaultPointerIconId() {
1220 return POINTER_ICON_STYLE_ARROW;
1221}
Jeff Brown83c09682010-12-23 17:50:18 -08001222
Jun Mukaid4eaef72015-10-30 15:54:33 -07001223int32_t NativeInputManager::getCustomPointerIconId() {
1224 return POINTER_ICON_STYLE_CUSTOM;
1225}
1226
Jeff Brown9c3cda02010-06-15 01:31:58 -07001227// ----------------------------------------------------------------------------
1228
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001229static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001230 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001231 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001232 if (messageQueue == nullptr) {
Jeff Brown864693462013-01-28 14:25:53 -08001233 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1234 return 0;
1235 }
1236
Jeff Brown603b4452012-04-06 17:39:41 -07001237 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1238 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001239 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001240 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001241}
1242
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001243static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001244 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001245
Jeff Brown4532e612012-04-05 14:27:12 -07001246 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001247 if (result) {
1248 jniThrowRuntimeException(env, "Input manager could not be started.");
1249 }
1250}
1251
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001252static void nativeSetDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
Santos Cordonee8931e2017-04-05 10:31:15 -07001253 jobjectArray viewportObjArray) {
1254 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001255 im->setDisplayViewports(env, viewportObjArray);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001256}
1257
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001258static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001259 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
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()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001263 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001264}
1265
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001266static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001267 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
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()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001271 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001272}
1273
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001274static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001275 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001276 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001277
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001278 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001279 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001280}
1281
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001282static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001283 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001284 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001285
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001286 int32_t* codes = env->GetIntArrayElements(keyCodes, nullptr);
1287 uint8_t* flags = env->GetBooleanArrayElements(outFlags, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001288 jsize numCodes = env->GetArrayLength(keyCodes);
1289 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001290 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001291 if (im->getInputManager()->getReader()->hasKeys(
1292 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1293 result = JNI_TRUE;
1294 } else {
1295 result = JNI_FALSE;
1296 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001297 } else {
1298 result = JNI_FALSE;
1299 }
1300
1301 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1302 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1303 return result;
1304}
1305
1306static void throwInputChannelNotInitialized(JNIEnv* env) {
1307 jniThrowException(env, "java/lang/IllegalStateException",
1308 "inputChannel is not initialized");
1309}
1310
Jeff Brown4532e612012-04-05 14:27:12 -07001311static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001312 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001313 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1314
Steve Block8564c8d2012-01-05 23:22:43 +00001315 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001316 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001317 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001318}
1319
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001320static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Arthur Hungbe5ce212018-09-13 18:41:56 +08001321 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001322 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001323
1324 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1325 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001326 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001327 throwInputChannelNotInitialized(env);
1328 return;
1329 }
1330
Jeff Brown928e0542011-01-10 11:17:36 -08001331 sp<InputWindowHandle> inputWindowHandle =
1332 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001333
Jeff Brown4532e612012-04-05 14:27:12 -07001334 status_t status = im->registerInputChannel(
Arthur Hungbe5ce212018-09-13 18:41:56 +08001335 env, inputChannel, inputWindowHandle, displayId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001336 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001337 std::string message;
1338 message += StringPrintf("Failed to register input channel. status=%d", status);
1339 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001340 return;
1341 }
1342
Arthur Hungbe5ce212018-09-13 18:41:56 +08001343 // If inputWindowHandle is null and displayId >= 0, treat inputChannel as monitor.
1344 if (inputWindowHandle != nullptr || displayId == ADISPLAY_ID_NONE) {
Jeff Browna41ca772010-08-11 14:46:32 -07001345 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001346 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001347 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001348}
1349
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001350static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001351 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001352 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001353
1354 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1355 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001356 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001357 throwInputChannelNotInitialized(env);
1358 return;
1359 }
1360
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001361 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001362
Jeff Brown4532e612012-04-05 14:27:12 -07001363 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001364 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001365 std::string message;
1366 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1367 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001368 }
1369}
1370
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001371static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001372 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001373 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001374
Jeff Brown4532e612012-04-05 14:27:12 -07001375 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001376}
1377
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001378static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001379 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001380 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001381 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001382
Jeff Brown6ec402b2010-07-28 15:48:59 -07001383 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1384 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001385 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1386 if (status) {
1387 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1388 return INPUT_EVENT_INJECTION_FAILED;
1389 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001390
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001391 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001392 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001393 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001394 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001395 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1396 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001397 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1398 return INPUT_EVENT_INJECTION_FAILED;
1399 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001400
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001401 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001402 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001403 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001404 } else {
1405 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001406 return INPUT_EVENT_INJECTION_FAILED;
1407 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001408}
1409
Andrii Kulian112d0562016-03-08 10:44:22 -08001410static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1411 jlong ptr, jint deviceId) {
1412 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001413
Andrii Kulian112d0562016-03-08 10:44:22 -08001414 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1415}
1416
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001417static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hung39134b22018-08-14 11:58:28 +08001418 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001419 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001420
Arthur Hung39134b22018-08-14 11:58:28 +08001421 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001422}
1423
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001424static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001425 jlong ptr, jint displayId, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001426 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001427
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001428 im->setFocusedApplication(env, displayId, applicationHandleObj);
1429}
1430
1431static void nativeSetFocusedDisplay(JNIEnv* env, jclass /* clazz */,
1432 jlong ptr, jint displayId) {
1433 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1434
1435 im->setFocusedDisplay(env, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001436}
1437
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001438static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1439 jboolean enabled) {
1440 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001441
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001442 im->setPointerCapture(enabled);
1443}
1444
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001445static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1446 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001447 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001448
Jeff Brown4532e612012-04-05 14:27:12 -07001449 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001450}
1451
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001452static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1453 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001454 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001455
Jeff Brown4532e612012-04-05 14:27:12 -07001456 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001457}
1458
Jeff Brown4532e612012-04-05 14:27:12 -07001459static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001460 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001461 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001462
1463 sp<InputChannel> fromChannel =
1464 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1465 sp<InputChannel> toChannel =
1466 android_view_InputChannel_getInputChannel(env, toChannelObj);
1467
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001468 if (fromChannel == nullptr || toChannel == nullptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001469 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001470 }
1471
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001472 if (im->getInputManager()->getDispatcher()->
1473 transferTouchFocus(fromChannel, toChannel)) {
1474 return JNI_TRUE;
1475 } else {
1476 return JNI_FALSE;
1477 }
Jeff Browne6504122010-09-27 14:52:15 -07001478}
1479
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001480static void nativeSetPointerSpeed(JNIEnv* /* env */,
1481 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001482 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001483
Jeff Brown4532e612012-04-05 14:27:12 -07001484 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001485}
1486
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001487static void nativeSetShowTouches(JNIEnv* /* env */,
1488 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001489 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001490
Jeff Brown4532e612012-04-05 14:27:12 -07001491 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001492}
1493
Jeff Brown037c33e2014-04-09 00:31:55 -07001494static void nativeSetInteractive(JNIEnv* env,
1495 jclass clazz, jlong ptr, jboolean interactive) {
1496 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1497
1498 im->setInteractive(interactive);
1499}
1500
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001501static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1502 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001503
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001504 im->reloadCalibration();
1505}
1506
Jeff Browna47425a2012-04-13 04:09:27 -07001507static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001508 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001509 jint repeat, jint token) {
1510 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1511
1512 size_t patternSize = env->GetArrayLength(patternObj);
1513 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001514 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001515 "which is more than the maximum supported size of %d.",
1516 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1517 return; // limit to reasonable size
1518 }
1519
1520 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001521 patternObj, nullptr));
Jeff Browna47425a2012-04-13 04:09:27 -07001522 nsecs_t pattern[patternSize];
1523 for (size_t i = 0; i < patternSize; i++) {
1524 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001525 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001526 }
1527 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1528
1529 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1530}
1531
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001532static void nativeCancelVibrate(JNIEnv* /* env */,
1533 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001534 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1535
1536 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1537}
1538
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001539static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1540 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001541 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1542
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001543 im->getInputManager()->getReader()->requestRefreshConfiguration(
1544 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1545}
1546
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001547static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1548 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001549 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1550
1551 im->getInputManager()->getReader()->requestRefreshConfiguration(
1552 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001553}
1554
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001555static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001556 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001557
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001558 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001559 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001560 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001561}
1562
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001563static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001564 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001565
Jeff Brown4532e612012-04-05 14:27:12 -07001566 im->getInputManager()->getReader()->monitor();
1567 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001568}
1569
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001570static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1571 jclass /* clazz */, jlong ptr, jint deviceId) {
1572 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1573
1574 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1575}
1576
1577static void nativeEnableInputDevice(JNIEnv* /* env */,
1578 jclass /* clazz */, jlong ptr, jint deviceId) {
1579 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1580
1581 im->setInputDeviceEnabled(deviceId, true);
1582}
1583
1584static void nativeDisableInputDevice(JNIEnv* /* env */,
1585 jclass /* clazz */, jlong ptr, jint deviceId) {
1586 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1587
1588 im->setInputDeviceEnabled(deviceId, false);
1589}
1590
Michael Wrighte051f6f2016-05-13 17:44:16 +01001591static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001592 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001593
Michael Wrighte051f6f2016-05-13 17:44:16 +01001594 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001595}
1596
Jun Mukai19a56012015-11-24 11:25:52 -08001597static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1598 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001599
Jun Mukai19a56012015-11-24 11:25:52 -08001600 im->reloadPointerIcons();
1601}
1602
Jun Mukaid4eaef72015-10-30 15:54:33 -07001603static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1604 jlong ptr, jobject iconObj) {
1605 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1606
1607 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001608 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1609 if (result) {
1610 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1611 return;
1612 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001613
1614 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001615 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1616 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1617 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1618 spriteIcon.bitmap.rowBytes(), 0, 0);
1619 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001620 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1621 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1622 im->setCustomPointerIcon(spriteIcon);
1623}
1624
Jeff Brown9c3cda02010-06-15 01:31:58 -07001625// ----------------------------------------------------------------------------
1626
Daniel Micay76f6a862015-09-19 17:31:01 -04001627static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001628 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001629 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001630 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001631 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001632 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001633 (void*) nativeStart },
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001634 { "nativeSetDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1635 (void*) nativeSetDisplayViewports },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001636 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001637 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001638 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001639 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001640 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001641 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001642 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001643 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001644 { "nativeRegisterInputChannel",
Robert Carr788f5742018-07-30 17:46:45 -07001645 "(JLandroid/view/InputChannel;Landroid/view/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001646 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001647 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001648 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001649 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001650 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001651 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001652 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001653 { "nativeToggleCapsLock", "(JI)V",
1654 (void*) nativeToggleCapsLock },
Robert Carr788f5742018-07-30 17:46:45 -07001655 { "nativeSetInputWindows", "(J[Landroid/view/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001656 (void*) nativeSetInputWindows },
Robert Carr788f5742018-07-30 17:46:45 -07001657 { "nativeSetFocusedApplication", "(JILandroid/view/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001658 (void*) nativeSetFocusedApplication },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001659 { "nativeSetFocusedDisplay", "(JI)V",
1660 (void*) nativeSetFocusedDisplay },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001661 { "nativeSetPointerCapture", "(JZ)V",
1662 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001663 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001664 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001665 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001666 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001667 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001668 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001669 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001670 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001671 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001672 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001673 { "nativeSetInteractive", "(JZ)V",
1674 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001675 { "nativeReloadCalibration", "(J)V",
1676 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001677 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001678 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001679 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001680 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001681 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001682 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001683 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001684 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001685 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001686 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001687 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001688 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001689 { "nativeIsInputDeviceEnabled", "(JI)Z",
1690 (void*) nativeIsInputDeviceEnabled },
1691 { "nativeEnableInputDevice", "(JI)V",
1692 (void*) nativeEnableInputDevice },
1693 { "nativeDisableInputDevice", "(JI)V",
1694 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001695 { "nativeSetPointerIconType", "(JI)V",
1696 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001697 { "nativeReloadPointerIcons", "(J)V",
1698 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001699 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1700 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001701};
1702
1703#define FIND_CLASS(var, className) \
1704 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001705 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001706
1707#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1708 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001709 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001710
1711#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1712 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001713 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001714
1715int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001716 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001717 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001718 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001719 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1720
Jeff Brown9c3cda02010-06-15 01:31:58 -07001721 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001722
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001723 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001724 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001725
Jeff Brown4532e612012-04-05 14:27:12 -07001726 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001727 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001728
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001729 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1730 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1731
Jeff Brown53384282012-08-20 20:16:01 -07001732 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1733 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001734
Jeff Brown4532e612012-04-05 14:27:12 -07001735 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
Robert Carr788f5742018-07-30 17:46:45 -07001736 "notifyInputChannelBroken", "(Landroid/view/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001737
Jeff Brown4532e612012-04-05 14:27:12 -07001738 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001739 "notifyANR",
Robert Carr788f5742018-07-30 17:46:45 -07001740 "(Landroid/view/InputApplicationHandle;Landroid/view/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001741
Jeff Brown4532e612012-04-05 14:27:12 -07001742 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001743 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1744
Jeff Brown4532e612012-04-05 14:27:12 -07001745 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001746 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001747
Michael Wright70af00a2014-09-03 19:30:20 -07001748 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1749 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001750
Jeff Brown4532e612012-04-05 14:27:12 -07001751 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001752 "interceptKeyBeforeDispatching",
Robert Carr788f5742018-07-30 17:46:45 -07001753 "(Landroid/view/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001754
Jeff Brown4532e612012-04-05 14:27:12 -07001755 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001756 "dispatchUnhandledKey",
Robert Carr788f5742018-07-30 17:46:45 -07001757 "(Landroid/view/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001758
Jeff Brown4532e612012-04-05 14:27:12 -07001759 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001760 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001761
Jeff Brown4532e612012-04-05 14:27:12 -07001762 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001763 "getVirtualKeyQuietTimeMillis", "()I");
1764
Jeff Brown4532e612012-04-05 14:27:12 -07001765 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001766 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1767
Jeff Brown4532e612012-04-05 14:27:12 -07001768 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001769 "getKeyRepeatTimeout", "()I");
1770
Jeff Brown4532e612012-04-05 14:27:12 -07001771 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001772 "getKeyRepeatDelay", "()I");
1773
Jeff Brown4532e612012-04-05 14:27:12 -07001774 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001775 "getHoverTapTimeout", "()I");
1776
Jeff Brown4532e612012-04-05 14:27:12 -07001777 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001778 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001779
Jeff Brown4532e612012-04-05 14:27:12 -07001780 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001781 "getDoubleTapTimeout", "()I");
1782
Jeff Brown4532e612012-04-05 14:27:12 -07001783 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001784 "getLongPressTimeout", "()I");
1785
Jeff Brown4532e612012-04-05 14:27:12 -07001786 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001787 "getPointerLayer", "()I");
1788
Jeff Brown4532e612012-04-05 14:27:12 -07001789 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001790 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001791
Jeff Brown6ec6f792012-04-17 16:52:41 -07001792 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001793 "getKeyboardLayoutOverlay",
1794 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001795
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001796 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1797 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1798
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001799 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1800 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001801 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001802
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001803 // InputDevice
1804
1805 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1806 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1807
Jeff Brown6ec402b2010-07-28 15:48:59 -07001808 // KeyEvent
1809
1810 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001811 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1812
Jeff Brown8d608662010-08-30 03:02:23 -07001813 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001814
1815 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001816 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001817
RoboErikfb290df2013-12-16 11:27:55 -08001818 // InputDeviceIdentifier
1819
1820 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1821 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1822 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1823 "<init>", "(Ljava/lang/String;II)V");
1824
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001825 // TouchCalibration
1826
1827 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1828 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1829
1830 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1831 "getAffineTransform", "()[F");
1832
Jeff Brown46b9ac02010-04-22 18:58:52 -07001833 return 0;
1834}
1835
Jeff Brown46b9ac02010-04-22 18:58:52 -07001836} /* namespace android */