blob: 3943dba7092eab4d93dbb5eb6ffc4b48226ba513 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputManager-JNI"
18
Michael Wrightbafea6e2017-06-12 15:25:47 +010019#define ATRACE_TAG ATRACE_TAG_INPUT
20
Jeff Brown9c3cda02010-06-15 01:31:58 -070021//#define LOG_NDEBUG 0
22
23// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070024#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070025
26// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070027#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070028
Jeff Brown83c09682010-12-23 17:50:18 -080029
Steven Moreland2279b252017-07-19 09:50:45 -070030#include <nativehelper/JNIHelp.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031#include "jni.h"
Michael Wrighta4051212015-07-23 17:04:40 +010032#include <atomic>
33#include <cinttypes>
Jeff Brown349703e2010-06-22 01:27:15 -070034#include <limits.h>
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080035#include <android-base/stringprintf.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070037#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038
Jeff Brown46b9ac02010-04-22 18:58:52 -070039#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080040#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070041#include <utils/threads.h>
Michael Wrightbafea6e2017-06-12 15:25:47 +010042#include <utils/Trace.h>
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070043#include <utils/SortedVector.h>
Jeff Brown83c09682010-12-23 17:50:18 -080044
Jeff Brownb4ff35d2011-01-02 16:37:43 -080045#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070046#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047
Michael Wrightd6b473712014-02-10 15:56:36 -080048#include <inputflinger/InputManager.h>
49
Jeff Brown05dc66a2011-03-02 14:41:58 -080050#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070051#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080052#include <android_view_KeyEvent.h>
53#include <android_view_MotionEvent.h>
54#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070055#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080056#include <android/graphics/GraphicsJNI.h>
57
Steven Moreland2279b252017-07-19 09:50:45 -070058#include <nativehelper/ScopedLocalRef.h>
59#include <nativehelper/ScopedPrimitiveArray.h>
60#include <nativehelper/ScopedUtfChars.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070061
Jeff Brown4f8ecd82012-06-18 18:29:13 -070062#include "com_android_server_power_PowerManagerService.h"
Robert Carre1db3202018-07-23 15:24:59 -070063#include "android_hardware_input_InputApplicationHandle.h"
64#include "android_hardware_input_InputWindowHandle.h"
Santos Cordonee8931e2017-04-05 10:31:15 -070065#include "android_hardware_display_DisplayViewport.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070066
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010067#include <vector>
68
Michael Wrighta4051212015-07-23 17:04:40 +010069#define INDENT " "
70
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080071using android::base::StringPrintf;
72
Jeff Brown46b9ac02010-04-22 18:58:52 -070073namespace android {
74
Jeff Brown1a84fd12011-06-02 01:26:32 -070075// The exponent used to calculate the pointer speed scaling factor.
76// The scaling factor is calculated as 2 ^ (speed * exponent),
77// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070078static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070079
Jeff Brown46b9ac02010-04-22 18:58:52 -070080static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070081 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070082 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070083 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070084 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070085 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070086 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070087 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070088 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070089 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070090 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070091 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080092 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070093 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080094 jmethodID getKeyRepeatTimeout;
95 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070096 jmethodID getHoverTapTimeout;
97 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070098 jmethodID getDoubleTapTimeout;
99 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -0800100 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800101 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700102 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700103 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800104 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -0700105} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700106
107static struct {
108 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700109} gInputDeviceClassInfo;
110
111static struct {
112 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700113} gKeyEventClassInfo;
114
115static struct {
116 jclass clazz;
117} gMotionEventClassInfo;
118
RoboErikfb290df2013-12-16 11:27:55 -0800119static struct {
120 jclass clazz;
121 jmethodID constructor;
122} gInputDeviceIdentifierInfo;
123
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800124static struct {
125 jclass clazz;
126 jmethodID getAffineTransform;
127} gTouchCalibrationClassInfo;
128
RoboErikfb290df2013-12-16 11:27:55 -0800129
Jeff Brown928e0542011-01-10 11:17:36 -0800130
131// --- Global functions ---
132
Jeff Brown214eaf42011-05-26 19:17:02 -0700133template<typename T>
134inline static T min(const T& a, const T& b) {
135 return a < b ? a : b;
136}
137
138template<typename T>
139inline static T max(const T& a, const T& b) {
140 return a > b ? a : b;
141}
142
Michael Wrighta4051212015-07-23 17:04:40 +0100143static inline const char* toString(bool value) {
144 return value ? "true" : "false";
145}
146
Jeff Brown928e0542011-01-10 11:17:36 -0800147static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
148 const sp<InputApplicationHandle>& inputApplicationHandle) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100149 if (inputApplicationHandle == nullptr) {
150 return nullptr;
Jeff Brown928e0542011-01-10 11:17:36 -0800151 }
152 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
153 getInputApplicationHandleObjLocalRef(env);
154}
155
156static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
157 const sp<InputWindowHandle>& inputWindowHandle) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100158 if (inputWindowHandle == nullptr) {
159 return nullptr;
Jeff Brown928e0542011-01-10 11:17:36 -0800160 }
161 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
162 getInputWindowHandleObjLocalRef(env);
163}
164
Jun Mukai808196f2015-10-28 16:46:44 -0700165static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
166 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700167 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700168 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700169 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400170 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
171 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
172 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
173 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
174 bitmapCopy->rowBytes(), 0, 0);
175 }
Jun Mukai808196f2015-10-28 16:46:44 -0700176 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
177 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700178 }
179}
180
Jun Mukai808196f2015-10-28 16:46:44 -0700181static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
182 SpriteIcon* outSpriteIcon) {
183 PointerIcon pointerIcon;
184 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
185}
186
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100187static void updatePointerControllerFromViewport(
188 sp<PointerController> controller, const DisplayViewport* const viewport) {
189 if (controller != nullptr && viewport != nullptr) {
190 const int32_t width = viewport->logicalRight - viewport->logicalLeft;
191 const int32_t height = viewport->logicalBottom - viewport->logicalTop;
192 controller->setDisplayViewport(width, height, viewport->orientation);
193 }
194}
195
Jeff Brown905805a2011-10-12 13:57:59 -0700196enum {
197 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700198};
199
Jeff Brown928e0542011-01-10 11:17:36 -0800200
201// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800202
Jeff Brown9c3cda02010-06-15 01:31:58 -0700203class NativeInputManager : public virtual RefBase,
204 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700205 public virtual InputDispatcherPolicyInterface,
206 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700207protected:
208 virtual ~NativeInputManager();
209
210public:
Jeff Brown4532e612012-04-05 14:27:12 -0700211 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700212
213 inline sp<InputManager> getInputManager() const { return mInputManager; }
214
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800215 void dump(std::string& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700216
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100217 void setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700218
Jeff Brown7fbdc842010-06-17 20:52:56 -0700219 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Arthur Hungbe5ce212018-09-13 18:41:56 +0800220 const sp<InputWindowHandle>& inputWindowHandle, int32_t displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700221 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
222
Arthur Hung39134b22018-08-14 11:58:28 +0800223 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray, int32_t displayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800224 void setFocusedApplication(JNIEnv* env, int32_t displayId, jobject applicationHandleObj);
225 void setFocusedDisplay(JNIEnv* env, int32_t displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700226 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800227 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700228 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700229 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700230 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700231 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800232 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100233 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800234 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700235 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800236 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700237
Jeff Brown9c3cda02010-06-15 01:31:58 -0700238 /* --- InputReaderPolicyInterface implementation --- */
239
Jeff Brown214eaf42011-05-26 19:17:02 -0700240 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800241 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700242 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800243 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100244 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700245 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
246 jfloatArray matrixArr);
247 virtual TouchAffineTransformation getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100248 const std::string& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700249
250 /* --- InputDispatcherPolicyInterface implementation --- */
251
Jeff Brownbcc046a2012-09-27 20:46:43 -0700252 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700253 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700254 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700255 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700256 const sp<InputWindowHandle>& inputWindowHandle,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800257 const std::string& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800258 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700259 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700260 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800261 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800262 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700263 virtual nsecs_t interceptKeyBeforeDispatching(
264 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700265 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800266 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800267 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700268 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700269 virtual bool checkInjectEventsPermissionNonReentrant(
270 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700271
Jeff Brown2352b972011-04-12 22:39:53 -0700272 /* --- PointerControllerPolicyInterface implementation --- */
273
Jun Mukai19a56012015-11-24 11:25:52 -0800274 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700275 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700276 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
277 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900278 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700279 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700280
Jeff Brown9c3cda02010-06-15 01:31:58 -0700281private:
282 sp<InputManager> mInputManager;
283
Jeff Brown2352b972011-04-12 22:39:53 -0700284 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700285 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800286 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700287
Jeff Brown83c09682010-12-23 17:50:18 -0800288 Mutex mLock;
289 struct Locked {
290 // Display size information.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100291 std::vector<DisplayViewport> viewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800292
Jeff Brown05dc66a2011-03-02 14:41:58 -0800293 // System UI visibility.
294 int32_t systemUiVisibility;
295
Jeff Brown1a84fd12011-06-02 01:26:32 -0700296 // Pointer speed.
297 int32_t pointerSpeed;
298
Jeff Brown474dcb52011-06-14 20:22:50 -0700299 // True if pointer gestures are enabled.
300 bool pointerGesturesEnabled;
301
Jeff Browndaf4a122011-08-26 17:14:14 -0700302 // Show touches feature enable/disable.
303 bool showTouches;
304
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800305 // Pointer capture feature enable/disable.
306 bool pointerCapture;
307
Jeff Brown5541de92011-04-11 11:54:25 -0700308 // Sprite controller singleton, created on first use.
309 sp<SpriteController> spriteController;
310
Jeff Brown83c09682010-12-23 17:50:18 -0800311 // Pointer controller singleton, created and destroyed as needed.
312 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700313
314 // Input devices to be disabled
315 SortedVector<int32_t> disabledInputDevices;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100316 } mLocked GUARDED_BY(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700317
Michael Wrighta4051212015-07-23 17:04:40 +0100318 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700319
Jeff Brown2352b972011-04-12 22:39:53 -0700320 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800321 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700322 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800323
Jeff Brownb88102f2010-09-08 11:49:43 -0700324 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700325
Jeff Brown9c3cda02010-06-15 01:31:58 -0700326 static inline JNIEnv* jniEnv() {
327 return AndroidRuntime::getJNIEnv();
328 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700329};
330
Jeff Brown928e0542011-01-10 11:17:36 -0800331
Jeff Brown9c3cda02010-06-15 01:31:58 -0700332
Jeff Brown2352b972011-04-12 22:39:53 -0700333NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700334 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700335 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700336 JNIEnv* env = jniEnv();
337
Jeff Brown2352b972011-04-12 22:39:53 -0700338 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700339 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700340
Jeff Brown83c09682010-12-23 17:50:18 -0800341 {
342 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800343 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700344 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700345 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700346 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800347 mLocked.pointerCapture = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800348 }
Michael Wrighta4051212015-07-23 17:04:40 +0100349 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800350
Jeff Brown9c3cda02010-06-15 01:31:58 -0700351 sp<EventHub> eventHub = new EventHub();
352 mInputManager = new InputManager(eventHub, this, this);
353}
354
355NativeInputManager::~NativeInputManager() {
356 JNIEnv* env = jniEnv();
357
Jeff Brown2352b972011-04-12 22:39:53 -0700358 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700359 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700360}
361
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800362void NativeInputManager::dump(std::string& dump) {
363 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100364 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800365 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100366 }
367 {
368 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800369 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100370 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800371 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
372 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100373 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800374 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
375 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100376 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800377 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100378
Jeff Brownb88102f2010-09-08 11:49:43 -0700379 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800380 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381
Jeff Brownb88102f2010-09-08 11:49:43 -0700382 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800383 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700384}
385
Jeff Brown7fbdc842010-06-17 20:52:56 -0700386bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700387 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000388 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700389 LOGE_EX(env);
390 env->ExceptionClear();
391 return true;
392 }
393 return false;
394}
395
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100396static const DisplayViewport* findInternalViewport(const std::vector<DisplayViewport>& viewports) {
397 for (const DisplayViewport& v : viewports) {
398 if (v.type == ViewportType::VIEWPORT_INTERNAL) {
399 return &v;
400 }
401 }
402 return nullptr;
403}
404
405void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
406 std::vector<DisplayViewport> viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700407
408 if (viewportObjArray) {
409 jsize length = env->GetArrayLength(viewportObjArray);
410 for (jsize i = 0; i < length; i++) {
411 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
412 if (! viewportObj) {
413 break; // found null element indicating end of used portion of the array
414 }
415
416 DisplayViewport viewport;
417 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100418 ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
419 viewports.push_back(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -0700420
421 env->DeleteLocalRef(viewportObj);
422 }
423 }
424
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100425 const DisplayViewport* newInternalViewport = findInternalViewport(viewports);
Santos Cordonee8931e2017-04-05 10:31:15 -0700426 {
427 AutoMutex _l(mLock);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100428 const DisplayViewport* oldInternalViewport = findInternalViewport(mLocked.viewports);
429 // Internal viewport has changed if there wasn't one earlier, and there is one now, or,
430 // if they are different.
431 const bool internalViewportChanged = (newInternalViewport != nullptr) &&
Siarhei Vishniakoue5bf8662018-10-04 09:33:24 -0700432 (oldInternalViewport == nullptr || (*oldInternalViewport != *newInternalViewport));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100433 if (internalViewportChanged) {
434 sp<PointerController> controller = mLocked.pointerController.promote();
435 updatePointerControllerFromViewport(controller, newInternalViewport);
436 }
437 mLocked.viewports = viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700438 }
439
440 mInputManager->getReader()->requestRefreshConfiguration(
441 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
442}
443
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700444status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Arthur Hungbe5ce212018-09-13 18:41:56 +0800445 const sp<InputChannel>& inputChannel, const sp<InputWindowHandle>& inputWindowHandle,
446 int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100447 ATRACE_CALL();
Arthur Hungbe5ce212018-09-13 18:41:56 +0800448 return mInputManager->getDispatcher()->registerInputChannel(inputChannel, inputWindowHandle,
449 displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700450}
451
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700452status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700453 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100454 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700455 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700456}
457
Jeff Brown214eaf42011-05-26 19:17:02 -0700458void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100459 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700460 JNIEnv* env = jniEnv();
461
Jeff Brown4532e612012-04-05 14:27:12 -0700462 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
463 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700464 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
465 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
466 }
467
468 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700469 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
470 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700471 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
472 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700473 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700474 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100475 const char* deviceNameChars = env->GetStringUTFChars(item, nullptr);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100476 outConfig->excludedDeviceNames.push_back(deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700477 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700478 env->DeleteLocalRef(item);
479 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700480 env->DeleteLocalRef(excludedDeviceNames);
481 }
482
Jeff Brown4532e612012-04-05 14:27:12 -0700483 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
484 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700485 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700486 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
487 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700488 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700489 jint longPressTimeout = env->CallIntMethod(mServiceObj,
490 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700491 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700492 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700493
494 // We must ensure that the tap-drag interval is significantly shorter than
495 // the long-press timeout because the tap is held down for the entire duration
496 // of the double-tap timeout.
497 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700498 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700499 outConfig->pointerGestureTapDragInterval =
500 milliseconds_to_nanoseconds(tapDragInterval);
501 }
502 }
503 }
504
Jeff Brown4532e612012-04-05 14:27:12 -0700505 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
506 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700507 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
508 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700509 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700510
511 { // acquire lock
512 AutoMutex _l(mLock);
513
514 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
515 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700516 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700517
Jeff Browndaf4a122011-08-26 17:14:14 -0700518 outConfig->showTouches = mLocked.showTouches;
519
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800520 outConfig->pointerCapture = mLocked.pointerCapture;
521
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100522 outConfig->setDisplayViewports(mLocked.viewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700523
524 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700525 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700526}
527
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700528sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100529 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800530 AutoMutex _l(mLock);
531
532 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100533 if (controller == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700534 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800535
Jeff Brown2352b972011-04-12 22:39:53 -0700536 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800537 mLocked.pointerController = controller;
538
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100539 const DisplayViewport* internalViewport = findInternalViewport(mLocked.viewports);
540 updatePointerControllerFromViewport(controller, internalViewport);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800541
Jeff Brown2352b972011-04-12 22:39:53 -0700542 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800543 }
544 return controller;
545}
546
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100547void NativeInputManager::ensureSpriteControllerLocked() REQUIRES(mLock) {
548 if (mLocked.spriteController == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700549 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700550 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700551 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
552 layer = -1;
553 }
554 mLocked.spriteController = new SpriteController(mLooper, layer);
555 }
556}
557
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700558void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100559 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700560 JNIEnv* env = jniEnv();
561
562 size_t count = inputDevices.size();
563 jobjectArray inputDevicesObjArray = env->NewObjectArray(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100564 count, gInputDeviceClassInfo.clazz, nullptr);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700565 if (inputDevicesObjArray) {
566 bool error = false;
567 for (size_t i = 0; i < count; i++) {
568 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
569 if (!inputDeviceObj) {
570 error = true;
571 break;
572 }
573
574 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
575 env->DeleteLocalRef(inputDeviceObj);
576 }
577
578 if (!error) {
579 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
580 inputDevicesObjArray);
581 }
582
583 env->DeleteLocalRef(inputDevicesObjArray);
584 }
585
586 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
587}
588
Jeff Brown6ec6f792012-04-17 16:52:41 -0700589sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800590 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100591 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700592 JNIEnv* env = jniEnv();
593
594 sp<KeyCharacterMap> result;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100595 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
RoboErikfb290df2013-12-16 11:27:55 -0800596 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
597 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
598 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700599 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800600 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700601 if (arrayObj.get()) {
602 ScopedLocalRef<jstring> filenameObj(env,
603 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
604 ScopedLocalRef<jstring> contentsObj(env,
605 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
606 ScopedUtfChars filenameChars(env, filenameObj.get());
607 ScopedUtfChars contentsChars(env, contentsObj.get());
608
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100609 KeyCharacterMap::loadContents(filenameChars.c_str(),
610 contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700611 }
612 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
613 return result;
614}
615
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100616std::string NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100617 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700618 JNIEnv* env = jniEnv();
619
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100620 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.c_str()));
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700621 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
622 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100623 std::string result;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700624 if (aliasObj.get()) {
625 ScopedUtfChars aliasChars(env, aliasObj.get());
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100626 result = aliasChars.c_str();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700627 }
628 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
629 return result;
630}
631
Jeff Brownbcc046a2012-09-27 20:46:43 -0700632void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700633 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700634#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700635 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
636 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700637#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100638 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700639
640 JNIEnv* env = jniEnv();
641
Jeff Brown53384282012-08-20 20:16:01 -0700642 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700643 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700644 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700645}
646
Jeff Brown9c3cda02010-06-15 01:31:58 -0700647void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
648#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000649 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700650#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100651 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700652
653 JNIEnv* env = jniEnv();
654
Jeff Brown4532e612012-04-05 14:27:12 -0700655 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700656 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700657}
658
Jeff Brown519e0242010-09-15 15:18:56 -0700659nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800660 const sp<InputWindowHandle>& inputWindowHandle, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700661#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000662 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700663#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100664 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700665
666 JNIEnv* env = jniEnv();
667
Jeff Brown928e0542011-01-10 11:17:36 -0800668 jobject inputApplicationHandleObj =
669 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
670 jobject inputWindowHandleObj =
671 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800672 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700673
Jeff Brown4532e612012-04-05 14:27:12 -0700674 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700675 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
676 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700677 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
678 newTimeout = 0; // abort dispatch
679 } else {
680 assert(newTimeout >= 0);
681 }
682
Jeff Brownbd181bb2013-09-10 16:44:24 -0700683 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800684 env->DeleteLocalRef(inputWindowHandleObj);
685 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700686 return newTimeout;
687}
688
Jeff Brown928e0542011-01-10 11:17:36 -0800689void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700690#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000691 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700692#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100693 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700694
Jeff Brown7fbdc842010-06-17 20:52:56 -0700695 JNIEnv* env = jniEnv();
696
Jeff Brown928e0542011-01-10 11:17:36 -0800697 jobject inputWindowHandleObj =
698 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
699 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700700 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800701 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700702 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
703
Jeff Brown928e0542011-01-10 11:17:36 -0800704 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700705 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700706}
707
Jeff Brown214eaf42011-05-26 19:17:02 -0700708void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100709 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700710 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800711
Jeff Brown4532e612012-04-05 14:27:12 -0700712 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
713 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700714 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
715 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
716 }
Jeff Browna4547672011-03-02 21:38:11 -0800717
Jeff Brown4532e612012-04-05 14:27:12 -0700718 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
719 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700720 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
721 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
722 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700723}
724
Arthur Hung39134b22018-08-14 11:58:28 +0800725void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
726 int32_t displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700727 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700728
Jeff Brown9302c872011-07-13 22:51:29 -0700729 if (windowHandleObjArray) {
730 jsize length = env->GetArrayLength(windowHandleObjArray);
731 for (jsize i = 0; i < length; i++) {
732 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
733 if (! windowHandleObj) {
734 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700735 }
Jeff Brown9302c872011-07-13 22:51:29 -0700736
737 sp<InputWindowHandle> windowHandle =
738 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100739 if (windowHandle != nullptr) {
Jeff Brown9302c872011-07-13 22:51:29 -0700740 windowHandles.push(windowHandle);
741 }
742 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700743 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700744 }
Jeff Brown349703e2010-06-22 01:27:15 -0700745
Arthur Hung39134b22018-08-14 11:58:28 +0800746 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700747
748 // Do this after the dispatcher has updated the window handle state.
749 bool newPointerGesturesEnabled = true;
750 size_t numWindows = windowHandles.size();
751 for (size_t i = 0; i < numWindows; i++) {
752 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700753 const InputWindowInfo* windowInfo = windowHandle->getInfo();
754 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
755 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700756 newPointerGesturesEnabled = false;
757 }
758 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700759
760 uint32_t changes = 0;
761 { // acquire lock
762 AutoMutex _l(mLock);
763
764 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
765 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
766 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
767 }
768 } // release lock
769
770 if (changes) {
771 mInputManager->getReader()->requestRefreshConfiguration(changes);
772 }
Jeff Brown349703e2010-06-22 01:27:15 -0700773}
774
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800775void NativeInputManager::setFocusedApplication(JNIEnv* env, int32_t displayId,
776 jobject applicationHandleObj) {
Jeff Brown9302c872011-07-13 22:51:29 -0700777 sp<InputApplicationHandle> applicationHandle =
778 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800779 mInputManager->getDispatcher()->setFocusedApplication(displayId, applicationHandle);
780}
781
782void NativeInputManager::setFocusedDisplay(JNIEnv* env, int32_t displayId) {
783 mInputManager->getDispatcher()->setFocusedDisplay(displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700784}
785
786void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700787 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700788}
789
Jeff Brown05dc66a2011-03-02 14:41:58 -0800790void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
791 AutoMutex _l(mLock);
792
793 if (mLocked.systemUiVisibility != visibility) {
794 mLocked.systemUiVisibility = visibility;
795
796 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100797 if (controller != nullptr) {
Jeff Brown2352b972011-04-12 22:39:53 -0700798 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800799 }
800 }
801}
802
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100803void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller)
804 REQUIRES(mLock) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800805 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700806 controller->setInactivityTimeout(lightsOut
807 ? PointerController::INACTIVITY_TIMEOUT_SHORT
808 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800809}
810
Jeff Brown1a84fd12011-06-02 01:26:32 -0700811void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700812 { // acquire lock
813 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700814
Jeff Brown474dcb52011-06-14 20:22:50 -0700815 if (mLocked.pointerSpeed == speed) {
816 return;
817 }
818
Steve Block6215d3f2012-01-04 20:05:49 +0000819 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700820 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700821 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700822
Jeff Brown474dcb52011-06-14 20:22:50 -0700823 mInputManager->getReader()->requestRefreshConfiguration(
824 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700825}
826
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700827void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
828 { // acquire lock
829 AutoMutex _l(mLock);
830
831 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
832 bool currentlyEnabled = index < 0;
833 if (!enabled && currentlyEnabled) {
834 mLocked.disabledInputDevices.add(deviceId);
835 }
836 if (enabled && !currentlyEnabled) {
837 mLocked.disabledInputDevices.remove(deviceId);
838 }
839 } // release lock
840
841 mInputManager->getReader()->requestRefreshConfiguration(
842 InputReaderConfiguration::CHANGE_ENABLED_STATE);
843}
844
Jeff Browndaf4a122011-08-26 17:14:14 -0700845void NativeInputManager::setShowTouches(bool enabled) {
846 { // acquire lock
847 AutoMutex _l(mLock);
848
849 if (mLocked.showTouches == enabled) {
850 return;
851 }
852
Steve Block6215d3f2012-01-04 20:05:49 +0000853 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700854 mLocked.showTouches = enabled;
855 } // release lock
856
857 mInputManager->getReader()->requestRefreshConfiguration(
858 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
859}
860
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800861void NativeInputManager::setPointerCapture(bool enabled) {
862 { // acquire lock
863 AutoMutex _l(mLock);
864
865 if (mLocked.pointerCapture == enabled) {
866 return;
867 }
868
869 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
870 mLocked.pointerCapture = enabled;
871 } // release lock
872
873 mInputManager->getReader()->requestRefreshConfiguration(
874 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
875}
876
Jeff Brown037c33e2014-04-09 00:31:55 -0700877void NativeInputManager::setInteractive(bool interactive) {
878 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700879}
880
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800881void NativeInputManager::reloadCalibration() {
882 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100883 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800884}
885
Michael Wrighte051f6f2016-05-13 17:44:16 +0100886void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800887 AutoMutex _l(mLock);
888 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100889 if (controller != nullptr) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100890 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800891 }
892}
893
894void NativeInputManager::reloadPointerIcons() {
895 AutoMutex _l(mLock);
896 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100897 if (controller != nullptr) {
Jun Mukai19a56012015-11-24 11:25:52 -0800898 controller->reloadPointerResources();
899 }
Jun Mukai1db53972015-09-11 18:08:31 -0700900}
901
Jun Mukaid4eaef72015-10-30 15:54:33 -0700902void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
903 AutoMutex _l(mLock);
904 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100905 if (controller != nullptr) {
Jun Mukaid4eaef72015-10-30 15:54:33 -0700906 controller->setCustomPointerIcon(icon);
907 }
908}
909
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800910TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
911 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100912 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800913 ScopedFloatArrayRO matrix(env, matrixArr);
914 assert(matrix.size() == 6);
915
916 TouchAffineTransformation transform;
917 transform.x_scale = matrix[0];
918 transform.x_ymix = matrix[1];
919 transform.x_offset = matrix[2];
920 transform.y_xmix = matrix[3];
921 transform.y_scale = matrix[4];
922 transform.y_offset = matrix[5];
923
924 return transform;
925}
926
927TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100928 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800929 JNIEnv* env = jniEnv();
930
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100931 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.c_str()));
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800932
933 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700934 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
935 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800936
937 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
938 gTouchCalibrationClassInfo.getAffineTransform));
939
940 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
941
942 env->DeleteLocalRef(matrixArr);
943 env->DeleteLocalRef(cal);
944
945 return transform;
946}
947
Jeff Brown0029c662011-03-30 02:25:18 -0700948bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100949 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700950 jobject inputEventObj;
951
952 JNIEnv* env = jniEnv();
953 switch (inputEvent->getType()) {
954 case AINPUT_EVENT_TYPE_KEY:
955 inputEventObj = android_view_KeyEvent_fromNative(env,
956 static_cast<const KeyEvent*>(inputEvent));
957 break;
958 case AINPUT_EVENT_TYPE_MOTION:
959 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
960 static_cast<const MotionEvent*>(inputEvent));
961 break;
962 default:
963 return true; // dispatch the event normally
964 }
965
966 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000967 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700968 return true; // dispatch the event normally
969 }
970
971 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700972 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700973 inputEventObj, policyFlags);
974 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
975 pass = true;
976 }
977 env->DeleteLocalRef(inputEventObj);
978 return pass;
979}
980
Jeff Brown1f245102010-11-18 20:53:46 -0800981void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
982 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100983 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700984 // Policy:
985 // - Ignore untrusted events and pass them along.
986 // - Ask the window manager what to do with normal events and trusted injected events.
987 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100988 bool interactive = mInteractive.load();
989 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700990 policyFlags |= POLICY_FLAG_INTERACTIVE;
991 }
Jeff Brown3122e442010-10-11 23:32:49 -0700992 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800993 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700994 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800995 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
996 jint wmActions;
997 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700998 wmActions = env->CallIntMethod(mServiceObj,
999 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -07001000 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001001 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
1002 wmActions = 0;
1003 }
1004 android_view_KeyEvent_recycle(env, keyEventObj);
1005 env->DeleteLocalRef(keyEventObj);
1006 } else {
Steve Block3762c312012-01-06 19:20:56 +00001007 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -07001008 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -07001009 }
1010
Jeff Brown56194eb2011-03-02 19:23:13 -08001011 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -07001012 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001013 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001014 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1015 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001016 }
1017}
1018
Jeff Brown56194eb2011-03-02 19:23:13 -08001019void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001020 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001021 // Policy:
1022 // - Ignore untrusted events and pass them along.
1023 // - No special filtering for injected events required at this time.
1024 // - Filter normal events based on screen state.
1025 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001026 bool interactive = mInteractive.load();
1027 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001028 policyFlags |= POLICY_FLAG_INTERACTIVE;
1029 }
Jeff Brown3122e442010-10-11 23:32:49 -07001030 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001031 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001032 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001033 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001034 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001035 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001036 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001037 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001038 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001039 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001040 wmActions = 0;
1041 }
1042
Jeff Brown56194eb2011-03-02 19:23:13 -08001043 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001044 }
Jeff Brown3122e442010-10-11 23:32:49 -07001045 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001046 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001047 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1048 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001049 }
1050}
1051
Jeff Brown56194eb2011-03-02 19:23:13 -08001052void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1053 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001054 if (wmActions & WM_ACTION_PASS_TO_USER) {
1055 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1056 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001057#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001058 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001059#endif
1060 }
1061}
1062
Jeff Brown905805a2011-10-12 13:57:59 -07001063nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -08001064 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -07001065 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001066 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001067 // Policy:
1068 // - Ignore untrusted events and pass them along.
1069 // - Filter normal events and trusted injected events through the window manager policy to
1070 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001071 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001072 if (policyFlags & POLICY_FLAG_TRUSTED) {
1073 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001074
Jeff Brown928e0542011-01-10 11:17:36 -08001075 // Note: inputWindowHandle may be null.
1076 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001077 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1078 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001079 jlong delayMillis = env->CallLongMethod(mServiceObj,
1080 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -08001081 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001082 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1083 android_view_KeyEvent_recycle(env, keyEventObj);
1084 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001085 if (!error) {
1086 if (delayMillis < 0) {
1087 result = -1;
1088 } else if (delayMillis > 0) {
1089 result = milliseconds_to_nanoseconds(delayMillis);
1090 }
1091 }
Jeff Brown1f245102010-11-18 20:53:46 -08001092 } else {
Steve Block3762c312012-01-06 19:20:56 +00001093 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001094 }
Jeff Brown928e0542011-01-10 11:17:36 -08001095 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -07001096 }
Jeff Brown1f245102010-11-18 20:53:46 -08001097 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001098}
1099
Jeff Brown928e0542011-01-10 11:17:36 -08001100bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001101 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001102 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001103 // Policy:
1104 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001105 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001106 if (policyFlags & POLICY_FLAG_TRUSTED) {
1107 JNIEnv* env = jniEnv();
1108
Jeff Brown928e0542011-01-10 11:17:36 -08001109 // Note: inputWindowHandle may be null.
1110 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001111 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1112 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001113 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1114 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -08001115 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001116 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001117 fallbackKeyEventObj = nullptr;
Jeff Brownda3d5a92011-03-29 15:11:34 -07001118 }
Jeff Brown1f245102010-11-18 20:53:46 -08001119 android_view_KeyEvent_recycle(env, keyEventObj);
1120 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001121
1122 if (fallbackKeyEventObj) {
1123 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1124 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1125 outFallbackKeyEvent)) {
1126 result = true;
1127 }
1128 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1129 env->DeleteLocalRef(fallbackKeyEventObj);
1130 }
Jeff Brown1f245102010-11-18 20:53:46 -08001131 } else {
Steve Block3762c312012-01-06 19:20:56 +00001132 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001133 }
Jeff Brown928e0542011-01-10 11:17:36 -08001134 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001135 }
Jeff Brown1f245102010-11-18 20:53:46 -08001136 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001137}
1138
Jeff Brown01ce2e92010-09-26 22:20:12 -07001139void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001140 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001141 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001142}
1143
Jeff Brown349703e2010-06-22 01:27:15 -07001144
Jeff Brownb88102f2010-09-08 11:49:43 -07001145bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1146 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001147 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001148 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001149 jboolean result = env->CallBooleanMethod(mServiceObj,
1150 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001151 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1152 result = false;
1153 }
Jeff Brown349703e2010-06-22 01:27:15 -07001154 return result;
1155}
1156
Jun Mukai19a56012015-11-24 11:25:52 -08001157void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001158 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001159 JNIEnv* env = jniEnv();
1160
1161 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1162 mServiceObj, gServiceClassInfo.getPointerIcon));
1163 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1164 return;
1165 }
1166
1167 PointerIcon pointerIcon;
1168 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1169 mContextObj, &pointerIcon);
1170 if (!status && !pointerIcon.isNullIcon()) {
1171 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1172 } else {
1173 *icon = SpriteIcon();
1174 }
1175}
1176
Jeff Brown2352b972011-04-12 22:39:53 -07001177void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001178 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001179 JNIEnv* env = jniEnv();
1180
1181 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1182 &outResources->spotHover);
1183 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1184 &outResources->spotTouch);
1185 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1186 &outResources->spotAnchor);
1187}
1188
Jun Mukai808196f2015-10-28 16:46:44 -07001189void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1190 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001191 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001192 JNIEnv* env = jniEnv();
1193
1194 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1195 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001196 PointerIcon pointerIcon;
1197 loadSystemIconAsSpriteWithPointerIcon(
1198 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1199 if (!pointerIcon.bitmapFrames.empty()) {
1200 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1201 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1202 animationData.durationPerFrame =
1203 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1204 animationData.animationFrames.reserve(numFrames);
1205 animationData.animationFrames.push_back(SpriteIcon(
1206 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1207 for (size_t i = 0; i < numFrames - 1; ++i) {
1208 animationData.animationFrames.push_back(SpriteIcon(
1209 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1210 }
1211 }
Jun Mukai1db53972015-09-11 18:08:31 -07001212 }
Jun Mukai808196f2015-10-28 16:46:44 -07001213 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1214 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001215}
1216
Jun Mukai5ec74202015-10-07 16:58:09 +09001217int32_t NativeInputManager::getDefaultPointerIconId() {
1218 return POINTER_ICON_STYLE_ARROW;
1219}
Jeff Brown83c09682010-12-23 17:50:18 -08001220
Jun Mukaid4eaef72015-10-30 15:54:33 -07001221int32_t NativeInputManager::getCustomPointerIconId() {
1222 return POINTER_ICON_STYLE_CUSTOM;
1223}
1224
Jeff Brown9c3cda02010-06-15 01:31:58 -07001225// ----------------------------------------------------------------------------
1226
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001227static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001228 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001229 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001230 if (messageQueue == nullptr) {
Jeff Brown864693462013-01-28 14:25:53 -08001231 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1232 return 0;
1233 }
1234
Jeff Brown603b4452012-04-06 17:39:41 -07001235 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1236 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001237 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001238 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001239}
1240
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001241static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001242 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001243
Jeff Brown4532e612012-04-05 14:27:12 -07001244 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001245 if (result) {
1246 jniThrowRuntimeException(env, "Input manager could not be started.");
1247 }
1248}
1249
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001250static void nativeSetDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
Santos Cordonee8931e2017-04-05 10:31:15 -07001251 jobjectArray viewportObjArray) {
1252 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001253 im->setDisplayViewports(env, viewportObjArray);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001254}
1255
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001256static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001257 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001258 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001259
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001260 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001261 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001262}
1263
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001264static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001265 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001266 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001267
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001268 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001269 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001270}
1271
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001272static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001273 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001274 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001275
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001276 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001277 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001278}
1279
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001280static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001281 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001282 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001283
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001284 int32_t* codes = env->GetIntArrayElements(keyCodes, nullptr);
1285 uint8_t* flags = env->GetBooleanArrayElements(outFlags, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001286 jsize numCodes = env->GetArrayLength(keyCodes);
1287 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001288 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001289 if (im->getInputManager()->getReader()->hasKeys(
1290 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1291 result = JNI_TRUE;
1292 } else {
1293 result = JNI_FALSE;
1294 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001295 } else {
1296 result = JNI_FALSE;
1297 }
1298
1299 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1300 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1301 return result;
1302}
1303
1304static void throwInputChannelNotInitialized(JNIEnv* env) {
1305 jniThrowException(env, "java/lang/IllegalStateException",
1306 "inputChannel is not initialized");
1307}
1308
Jeff Brown4532e612012-04-05 14:27:12 -07001309static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001310 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001311 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1312
Steve Block8564c8d2012-01-05 23:22:43 +00001313 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001314 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001315 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001316}
1317
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001318static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Arthur Hungbe5ce212018-09-13 18:41:56 +08001319 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001320 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001321
1322 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1323 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001324 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001325 throwInputChannelNotInitialized(env);
1326 return;
1327 }
1328
Jeff Brown928e0542011-01-10 11:17:36 -08001329 sp<InputWindowHandle> inputWindowHandle =
1330 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001331
Jeff Brown4532e612012-04-05 14:27:12 -07001332 status_t status = im->registerInputChannel(
Arthur Hungbe5ce212018-09-13 18:41:56 +08001333 env, inputChannel, inputWindowHandle, displayId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001334 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001335 std::string message;
1336 message += StringPrintf("Failed to register input channel. status=%d", status);
1337 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001338 return;
1339 }
1340
Arthur Hungbe5ce212018-09-13 18:41:56 +08001341 // If inputWindowHandle is null and displayId >= 0, treat inputChannel as monitor.
1342 if (inputWindowHandle != nullptr || displayId == ADISPLAY_ID_NONE) {
Jeff Browna41ca772010-08-11 14:46:32 -07001343 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001344 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001345 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001346}
1347
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001348static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001349 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001350 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001351
1352 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1353 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001354 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001355 throwInputChannelNotInitialized(env);
1356 return;
1357 }
1358
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001359 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001360
Jeff Brown4532e612012-04-05 14:27:12 -07001361 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001362 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001363 std::string message;
1364 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1365 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001366 }
1367}
1368
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001369static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001370 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001371 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001372
Jeff Brown4532e612012-04-05 14:27:12 -07001373 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001374}
1375
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001376static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001377 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001378 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001379 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001380
Jeff Brown6ec402b2010-07-28 15:48:59 -07001381 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1382 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001383 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1384 if (status) {
1385 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1386 return INPUT_EVENT_INJECTION_FAILED;
1387 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001388
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001389 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001390 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001391 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001392 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001393 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1394 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001395 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1396 return INPUT_EVENT_INJECTION_FAILED;
1397 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001398
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001399 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001400 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001401 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001402 } else {
1403 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001404 return INPUT_EVENT_INJECTION_FAILED;
1405 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001406}
1407
Andrii Kulian112d0562016-03-08 10:44:22 -08001408static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1409 jlong ptr, jint deviceId) {
1410 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001411
Andrii Kulian112d0562016-03-08 10:44:22 -08001412 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1413}
1414
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001415static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hung39134b22018-08-14 11:58:28 +08001416 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001417 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001418
Arthur Hung39134b22018-08-14 11:58:28 +08001419 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001420}
1421
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001422static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001423 jlong ptr, jint displayId, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001424 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001425
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001426 im->setFocusedApplication(env, displayId, applicationHandleObj);
1427}
1428
1429static void nativeSetFocusedDisplay(JNIEnv* env, jclass /* clazz */,
1430 jlong ptr, jint displayId) {
1431 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1432
1433 im->setFocusedDisplay(env, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001434}
1435
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001436static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1437 jboolean enabled) {
1438 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001439
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001440 im->setPointerCapture(enabled);
1441}
1442
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001443static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1444 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001445 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001446
Jeff Brown4532e612012-04-05 14:27:12 -07001447 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001448}
1449
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001450static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1451 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001452 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001453
Jeff Brown4532e612012-04-05 14:27:12 -07001454 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001455}
1456
Jeff Brown4532e612012-04-05 14:27:12 -07001457static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001458 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001459 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001460
1461 sp<InputChannel> fromChannel =
1462 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1463 sp<InputChannel> toChannel =
1464 android_view_InputChannel_getInputChannel(env, toChannelObj);
1465
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001466 if (fromChannel == nullptr || toChannel == nullptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001467 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001468 }
1469
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001470 if (im->getInputManager()->getDispatcher()->
1471 transferTouchFocus(fromChannel, toChannel)) {
1472 return JNI_TRUE;
1473 } else {
1474 return JNI_FALSE;
1475 }
Jeff Browne6504122010-09-27 14:52:15 -07001476}
1477
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001478static void nativeSetPointerSpeed(JNIEnv* /* env */,
1479 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001480 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001481
Jeff Brown4532e612012-04-05 14:27:12 -07001482 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001483}
1484
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001485static void nativeSetShowTouches(JNIEnv* /* env */,
1486 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001487 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001488
Jeff Brown4532e612012-04-05 14:27:12 -07001489 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001490}
1491
Jeff Brown037c33e2014-04-09 00:31:55 -07001492static void nativeSetInteractive(JNIEnv* env,
1493 jclass clazz, jlong ptr, jboolean interactive) {
1494 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1495
1496 im->setInteractive(interactive);
1497}
1498
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001499static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1500 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001501
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001502 im->reloadCalibration();
1503}
1504
Jeff Browna47425a2012-04-13 04:09:27 -07001505static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001506 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001507 jint repeat, jint token) {
1508 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1509
1510 size_t patternSize = env->GetArrayLength(patternObj);
1511 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001512 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001513 "which is more than the maximum supported size of %d.",
1514 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1515 return; // limit to reasonable size
1516 }
1517
1518 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001519 patternObj, nullptr));
Jeff Browna47425a2012-04-13 04:09:27 -07001520 nsecs_t pattern[patternSize];
1521 for (size_t i = 0; i < patternSize; i++) {
1522 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001523 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001524 }
1525 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1526
1527 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1528}
1529
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001530static void nativeCancelVibrate(JNIEnv* /* env */,
1531 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001532 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1533
1534 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1535}
1536
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001537static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1538 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001539 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1540
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001541 im->getInputManager()->getReader()->requestRefreshConfiguration(
1542 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1543}
1544
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001545static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1546 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001547 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1548
1549 im->getInputManager()->getReader()->requestRefreshConfiguration(
1550 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001551}
1552
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001553static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001554 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001555
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001556 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001557 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001558 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001559}
1560
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001561static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001562 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001563
Jeff Brown4532e612012-04-05 14:27:12 -07001564 im->getInputManager()->getReader()->monitor();
1565 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001566}
1567
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001568static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1569 jclass /* clazz */, jlong ptr, jint deviceId) {
1570 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1571
1572 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1573}
1574
1575static void nativeEnableInputDevice(JNIEnv* /* env */,
1576 jclass /* clazz */, jlong ptr, jint deviceId) {
1577 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1578
1579 im->setInputDeviceEnabled(deviceId, true);
1580}
1581
1582static void nativeDisableInputDevice(JNIEnv* /* env */,
1583 jclass /* clazz */, jlong ptr, jint deviceId) {
1584 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1585
1586 im->setInputDeviceEnabled(deviceId, false);
1587}
1588
Michael Wrighte051f6f2016-05-13 17:44:16 +01001589static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001590 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001591
Michael Wrighte051f6f2016-05-13 17:44:16 +01001592 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001593}
1594
Jun Mukai19a56012015-11-24 11:25:52 -08001595static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1596 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001597
Jun Mukai19a56012015-11-24 11:25:52 -08001598 im->reloadPointerIcons();
1599}
1600
Jun Mukaid4eaef72015-10-30 15:54:33 -07001601static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1602 jlong ptr, jobject iconObj) {
1603 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1604
1605 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001606 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1607 if (result) {
1608 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1609 return;
1610 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001611
1612 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001613 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1614 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1615 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1616 spriteIcon.bitmap.rowBytes(), 0, 0);
1617 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001618 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1619 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1620 im->setCustomPointerIcon(spriteIcon);
1621}
1622
Jeff Brown9c3cda02010-06-15 01:31:58 -07001623// ----------------------------------------------------------------------------
1624
Daniel Micay76f6a862015-09-19 17:31:01 -04001625static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001626 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001627 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001628 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001629 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001630 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001631 (void*) nativeStart },
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001632 { "nativeSetDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1633 (void*) nativeSetDisplayViewports },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001634 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001635 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001636 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001637 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001638 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001639 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001640 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001641 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001642 { "nativeRegisterInputChannel",
Arthur Hungbe5ce212018-09-13 18:41:56 +08001643 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001644 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001645 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001646 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001647 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001648 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001649 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001650 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001651 { "nativeToggleCapsLock", "(JI)V",
1652 (void*) nativeToggleCapsLock },
Arthur Hung39134b22018-08-14 11:58:28 +08001653 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001654 (void*) nativeSetInputWindows },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001655 { "nativeSetFocusedApplication", "(JILcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001656 (void*) nativeSetFocusedApplication },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001657 { "nativeSetFocusedDisplay", "(JI)V",
1658 (void*) nativeSetFocusedDisplay },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001659 { "nativeSetPointerCapture", "(JZ)V",
1660 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001661 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001662 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001663 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001664 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001665 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001666 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001667 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001668 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001669 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001670 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001671 { "nativeSetInteractive", "(JZ)V",
1672 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001673 { "nativeReloadCalibration", "(J)V",
1674 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001675 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001676 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001677 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001678 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001679 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001680 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001681 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001682 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001683 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001684 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001685 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001686 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001687 { "nativeIsInputDeviceEnabled", "(JI)Z",
1688 (void*) nativeIsInputDeviceEnabled },
1689 { "nativeEnableInputDevice", "(JI)V",
1690 (void*) nativeEnableInputDevice },
1691 { "nativeDisableInputDevice", "(JI)V",
1692 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001693 { "nativeSetPointerIconType", "(JI)V",
1694 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001695 { "nativeReloadPointerIcons", "(J)V",
1696 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001697 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1698 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001699};
1700
1701#define FIND_CLASS(var, className) \
1702 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001703 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001704
1705#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1706 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001707 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001708
1709#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1710 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001711 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001712
1713int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001714 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001715 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001716 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001717 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1718
Jeff Brown9c3cda02010-06-15 01:31:58 -07001719 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001720
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001721 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001722 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001723
Jeff Brown4532e612012-04-05 14:27:12 -07001724 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001725 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001726
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001727 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1728 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1729
Jeff Brown53384282012-08-20 20:16:01 -07001730 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1731 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001732
Jeff Brown4532e612012-04-05 14:27:12 -07001733 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1734 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001735
Jeff Brown4532e612012-04-05 14:27:12 -07001736 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001737 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001738 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001739
Jeff Brown4532e612012-04-05 14:27:12 -07001740 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001741 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1742
Jeff Brown4532e612012-04-05 14:27:12 -07001743 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001744 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001745
Michael Wright70af00a2014-09-03 19:30:20 -07001746 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1747 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001748
Jeff Brown4532e612012-04-05 14:27:12 -07001749 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001750 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001751 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001752
Jeff Brown4532e612012-04-05 14:27:12 -07001753 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001754 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001755 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001756
Jeff Brown4532e612012-04-05 14:27:12 -07001757 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001758 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001759
Jeff Brown4532e612012-04-05 14:27:12 -07001760 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001761 "getVirtualKeyQuietTimeMillis", "()I");
1762
Jeff Brown4532e612012-04-05 14:27:12 -07001763 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001764 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1765
Jeff Brown4532e612012-04-05 14:27:12 -07001766 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001767 "getKeyRepeatTimeout", "()I");
1768
Jeff Brown4532e612012-04-05 14:27:12 -07001769 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001770 "getKeyRepeatDelay", "()I");
1771
Jeff Brown4532e612012-04-05 14:27:12 -07001772 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001773 "getHoverTapTimeout", "()I");
1774
Jeff Brown4532e612012-04-05 14:27:12 -07001775 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001776 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001777
Jeff Brown4532e612012-04-05 14:27:12 -07001778 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001779 "getDoubleTapTimeout", "()I");
1780
Jeff Brown4532e612012-04-05 14:27:12 -07001781 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001782 "getLongPressTimeout", "()I");
1783
Jeff Brown4532e612012-04-05 14:27:12 -07001784 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001785 "getPointerLayer", "()I");
1786
Jeff Brown4532e612012-04-05 14:27:12 -07001787 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001788 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001789
Jeff Brown6ec6f792012-04-17 16:52:41 -07001790 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001791 "getKeyboardLayoutOverlay",
1792 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001793
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001794 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1795 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1796
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001797 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1798 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001799 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001800
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001801 // InputDevice
1802
1803 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1804 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1805
Jeff Brown6ec402b2010-07-28 15:48:59 -07001806 // KeyEvent
1807
1808 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001809 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1810
Jeff Brown8d608662010-08-30 03:02:23 -07001811 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001812
1813 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001814 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001815
RoboErikfb290df2013-12-16 11:27:55 -08001816 // InputDeviceIdentifier
1817
1818 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1819 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1820 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1821 "<init>", "(Ljava/lang/String;II)V");
1822
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001823 // TouchCalibration
1824
1825 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1826 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1827
1828 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1829 "getAffineTransform", "()[F");
1830
Jeff Brown46b9ac02010-04-22 18:58:52 -07001831 return 0;
1832}
1833
Jeff Brown46b9ac02010-04-22 18:58:52 -07001834} /* namespace android */