blob: ba6f39f5a8a7db91d3e1c663cb9742f448553dd8 [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
Prabir Pradhane5696a52018-11-14 19:55:21 -0800351 mInputManager = new InputManager(this, this);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700352}
353
354NativeInputManager::~NativeInputManager() {
355 JNIEnv* env = jniEnv();
356
Jeff Brown2352b972011-04-12 22:39:53 -0700357 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700358 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700359}
360
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800361void NativeInputManager::dump(std::string& dump) {
362 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100363 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800364 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100365 }
366 {
367 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800368 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100369 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800370 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
371 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100372 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800373 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
374 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100375 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800376 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100377
Jeff Brownb88102f2010-09-08 11:49:43 -0700378 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800379 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700380
Jeff Brownb88102f2010-09-08 11:49:43 -0700381 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800382 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700383}
384
Jeff Brown7fbdc842010-06-17 20:52:56 -0700385bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700386 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000387 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700388 LOGE_EX(env);
389 env->ExceptionClear();
390 return true;
391 }
392 return false;
393}
394
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100395static const DisplayViewport* findInternalViewport(const std::vector<DisplayViewport>& viewports) {
396 for (const DisplayViewport& v : viewports) {
397 if (v.type == ViewportType::VIEWPORT_INTERNAL) {
398 return &v;
399 }
400 }
401 return nullptr;
402}
403
404void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
405 std::vector<DisplayViewport> viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700406
407 if (viewportObjArray) {
408 jsize length = env->GetArrayLength(viewportObjArray);
409 for (jsize i = 0; i < length; i++) {
410 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
411 if (! viewportObj) {
412 break; // found null element indicating end of used portion of the array
413 }
414
415 DisplayViewport viewport;
416 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100417 ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
418 viewports.push_back(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -0700419
420 env->DeleteLocalRef(viewportObj);
421 }
422 }
423
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100424 const DisplayViewport* newInternalViewport = findInternalViewport(viewports);
Santos Cordonee8931e2017-04-05 10:31:15 -0700425 {
426 AutoMutex _l(mLock);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100427 const DisplayViewport* oldInternalViewport = findInternalViewport(mLocked.viewports);
428 // Internal viewport has changed if there wasn't one earlier, and there is one now, or,
429 // if they are different.
430 const bool internalViewportChanged = (newInternalViewport != nullptr) &&
Siarhei Vishniakoue5bf8662018-10-04 09:33:24 -0700431 (oldInternalViewport == nullptr || (*oldInternalViewport != *newInternalViewport));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100432 if (internalViewportChanged) {
433 sp<PointerController> controller = mLocked.pointerController.promote();
434 updatePointerControllerFromViewport(controller, newInternalViewport);
435 }
436 mLocked.viewports = viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700437 }
438
439 mInputManager->getReader()->requestRefreshConfiguration(
440 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
441}
442
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700443status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Arthur Hungbe5ce212018-09-13 18:41:56 +0800444 const sp<InputChannel>& inputChannel, const sp<InputWindowHandle>& inputWindowHandle,
445 int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100446 ATRACE_CALL();
Arthur Hungbe5ce212018-09-13 18:41:56 +0800447 return mInputManager->getDispatcher()->registerInputChannel(inputChannel, inputWindowHandle,
448 displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700449}
450
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700451status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700452 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100453 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700454 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700455}
456
Jeff Brown214eaf42011-05-26 19:17:02 -0700457void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100458 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700459 JNIEnv* env = jniEnv();
460
Jeff Brown4532e612012-04-05 14:27:12 -0700461 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
462 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700463 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
464 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
465 }
466
467 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700468 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
469 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700470 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
471 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700472 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700473 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100474 const char* deviceNameChars = env->GetStringUTFChars(item, nullptr);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100475 outConfig->excludedDeviceNames.push_back(deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700476 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700477 env->DeleteLocalRef(item);
478 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700479 env->DeleteLocalRef(excludedDeviceNames);
480 }
481
Jeff Brown4532e612012-04-05 14:27:12 -0700482 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
483 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700484 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700485 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
486 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700487 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700488 jint longPressTimeout = env->CallIntMethod(mServiceObj,
489 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700490 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700491 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700492
493 // We must ensure that the tap-drag interval is significantly shorter than
494 // the long-press timeout because the tap is held down for the entire duration
495 // of the double-tap timeout.
496 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700497 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700498 outConfig->pointerGestureTapDragInterval =
499 milliseconds_to_nanoseconds(tapDragInterval);
500 }
501 }
502 }
503
Jeff Brown4532e612012-04-05 14:27:12 -0700504 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
505 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700506 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
507 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700508 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700509
510 { // acquire lock
511 AutoMutex _l(mLock);
512
513 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
514 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700515 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700516
Jeff Browndaf4a122011-08-26 17:14:14 -0700517 outConfig->showTouches = mLocked.showTouches;
518
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800519 outConfig->pointerCapture = mLocked.pointerCapture;
520
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100521 outConfig->setDisplayViewports(mLocked.viewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700522
523 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700524 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700525}
526
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700527sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100528 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800529 AutoMutex _l(mLock);
530
531 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100532 if (controller == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700533 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800534
Jeff Brown2352b972011-04-12 22:39:53 -0700535 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800536 mLocked.pointerController = controller;
537
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100538 const DisplayViewport* internalViewport = findInternalViewport(mLocked.viewports);
539 updatePointerControllerFromViewport(controller, internalViewport);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800540
Jeff Brown2352b972011-04-12 22:39:53 -0700541 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800542 }
543 return controller;
544}
545
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100546void NativeInputManager::ensureSpriteControllerLocked() REQUIRES(mLock) {
547 if (mLocked.spriteController == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700548 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700549 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700550 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
551 layer = -1;
552 }
553 mLocked.spriteController = new SpriteController(mLooper, layer);
554 }
555}
556
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700557void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100558 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700559 JNIEnv* env = jniEnv();
560
561 size_t count = inputDevices.size();
562 jobjectArray inputDevicesObjArray = env->NewObjectArray(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100563 count, gInputDeviceClassInfo.clazz, nullptr);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700564 if (inputDevicesObjArray) {
565 bool error = false;
566 for (size_t i = 0; i < count; i++) {
567 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
568 if (!inputDeviceObj) {
569 error = true;
570 break;
571 }
572
573 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
574 env->DeleteLocalRef(inputDeviceObj);
575 }
576
577 if (!error) {
578 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
579 inputDevicesObjArray);
580 }
581
582 env->DeleteLocalRef(inputDevicesObjArray);
583 }
584
585 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
586}
587
Jeff Brown6ec6f792012-04-17 16:52:41 -0700588sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800589 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100590 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700591 JNIEnv* env = jniEnv();
592
593 sp<KeyCharacterMap> result;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100594 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
RoboErikfb290df2013-12-16 11:27:55 -0800595 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
596 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
597 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700598 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800599 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700600 if (arrayObj.get()) {
601 ScopedLocalRef<jstring> filenameObj(env,
602 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
603 ScopedLocalRef<jstring> contentsObj(env,
604 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
605 ScopedUtfChars filenameChars(env, filenameObj.get());
606 ScopedUtfChars contentsChars(env, contentsObj.get());
607
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100608 KeyCharacterMap::loadContents(filenameChars.c_str(),
609 contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700610 }
611 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
612 return result;
613}
614
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100615std::string NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100616 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700617 JNIEnv* env = jniEnv();
618
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100619 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.c_str()));
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700620 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
621 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100622 std::string result;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700623 if (aliasObj.get()) {
624 ScopedUtfChars aliasChars(env, aliasObj.get());
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100625 result = aliasChars.c_str();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700626 }
627 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
628 return result;
629}
630
Jeff Brownbcc046a2012-09-27 20:46:43 -0700631void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700632 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700633#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700634 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
635 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700636#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100637 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700638
639 JNIEnv* env = jniEnv();
640
Jeff Brown53384282012-08-20 20:16:01 -0700641 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700642 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700643 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700644}
645
Jeff Brown9c3cda02010-06-15 01:31:58 -0700646void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
647#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000648 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700649#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100650 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700651
652 JNIEnv* env = jniEnv();
653
Jeff Brown4532e612012-04-05 14:27:12 -0700654 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700655 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700656}
657
Jeff Brown519e0242010-09-15 15:18:56 -0700658nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800659 const sp<InputWindowHandle>& inputWindowHandle, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700660#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000661 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700662#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100663 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700664
665 JNIEnv* env = jniEnv();
666
Jeff Brown928e0542011-01-10 11:17:36 -0800667 jobject inputApplicationHandleObj =
668 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
669 jobject inputWindowHandleObj =
670 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800671 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700672
Jeff Brown4532e612012-04-05 14:27:12 -0700673 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700674 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
675 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700676 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
677 newTimeout = 0; // abort dispatch
678 } else {
679 assert(newTimeout >= 0);
680 }
681
Jeff Brownbd181bb2013-09-10 16:44:24 -0700682 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800683 env->DeleteLocalRef(inputWindowHandleObj);
684 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700685 return newTimeout;
686}
687
Jeff Brown928e0542011-01-10 11:17:36 -0800688void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700689#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000690 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700691#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100692 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700693
Jeff Brown7fbdc842010-06-17 20:52:56 -0700694 JNIEnv* env = jniEnv();
695
Jeff Brown928e0542011-01-10 11:17:36 -0800696 jobject inputWindowHandleObj =
697 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
698 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700699 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800700 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700701 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
702
Jeff Brown928e0542011-01-10 11:17:36 -0800703 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700704 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700705}
706
Jeff Brown214eaf42011-05-26 19:17:02 -0700707void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100708 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700709 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800710
Jeff Brown4532e612012-04-05 14:27:12 -0700711 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
712 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700713 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
714 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
715 }
Jeff Browna4547672011-03-02 21:38:11 -0800716
Jeff Brown4532e612012-04-05 14:27:12 -0700717 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
718 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700719 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
720 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
721 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700722}
723
Arthur Hung39134b22018-08-14 11:58:28 +0800724void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
725 int32_t displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700726 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700727
Jeff Brown9302c872011-07-13 22:51:29 -0700728 if (windowHandleObjArray) {
729 jsize length = env->GetArrayLength(windowHandleObjArray);
730 for (jsize i = 0; i < length; i++) {
731 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
732 if (! windowHandleObj) {
733 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700734 }
Jeff Brown9302c872011-07-13 22:51:29 -0700735
736 sp<InputWindowHandle> windowHandle =
737 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100738 if (windowHandle != nullptr) {
Jeff Brown9302c872011-07-13 22:51:29 -0700739 windowHandles.push(windowHandle);
740 }
741 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700742 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700743 }
Jeff Brown349703e2010-06-22 01:27:15 -0700744
Arthur Hung39134b22018-08-14 11:58:28 +0800745 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700746
747 // Do this after the dispatcher has updated the window handle state.
748 bool newPointerGesturesEnabled = true;
749 size_t numWindows = windowHandles.size();
750 for (size_t i = 0; i < numWindows; i++) {
751 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700752 const InputWindowInfo* windowInfo = windowHandle->getInfo();
753 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
754 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700755 newPointerGesturesEnabled = false;
756 }
757 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700758
759 uint32_t changes = 0;
760 { // acquire lock
761 AutoMutex _l(mLock);
762
763 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
764 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
765 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
766 }
767 } // release lock
768
769 if (changes) {
770 mInputManager->getReader()->requestRefreshConfiguration(changes);
771 }
Jeff Brown349703e2010-06-22 01:27:15 -0700772}
773
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800774void NativeInputManager::setFocusedApplication(JNIEnv* env, int32_t displayId,
775 jobject applicationHandleObj) {
Jeff Brown9302c872011-07-13 22:51:29 -0700776 sp<InputApplicationHandle> applicationHandle =
777 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800778 mInputManager->getDispatcher()->setFocusedApplication(displayId, applicationHandle);
779}
780
781void NativeInputManager::setFocusedDisplay(JNIEnv* env, int32_t displayId) {
782 mInputManager->getDispatcher()->setFocusedDisplay(displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700783}
784
785void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700786 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700787}
788
Jeff Brown05dc66a2011-03-02 14:41:58 -0800789void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
790 AutoMutex _l(mLock);
791
792 if (mLocked.systemUiVisibility != visibility) {
793 mLocked.systemUiVisibility = visibility;
794
795 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100796 if (controller != nullptr) {
Jeff Brown2352b972011-04-12 22:39:53 -0700797 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800798 }
799 }
800}
801
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100802void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller)
803 REQUIRES(mLock) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800804 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700805 controller->setInactivityTimeout(lightsOut
806 ? PointerController::INACTIVITY_TIMEOUT_SHORT
807 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800808}
809
Jeff Brown1a84fd12011-06-02 01:26:32 -0700810void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700811 { // acquire lock
812 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700813
Jeff Brown474dcb52011-06-14 20:22:50 -0700814 if (mLocked.pointerSpeed == speed) {
815 return;
816 }
817
Steve Block6215d3f2012-01-04 20:05:49 +0000818 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700819 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700820 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700821
Jeff Brown474dcb52011-06-14 20:22:50 -0700822 mInputManager->getReader()->requestRefreshConfiguration(
823 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700824}
825
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700826void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
827 { // acquire lock
828 AutoMutex _l(mLock);
829
830 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
831 bool currentlyEnabled = index < 0;
832 if (!enabled && currentlyEnabled) {
833 mLocked.disabledInputDevices.add(deviceId);
834 }
835 if (enabled && !currentlyEnabled) {
836 mLocked.disabledInputDevices.remove(deviceId);
837 }
838 } // release lock
839
840 mInputManager->getReader()->requestRefreshConfiguration(
841 InputReaderConfiguration::CHANGE_ENABLED_STATE);
842}
843
Jeff Browndaf4a122011-08-26 17:14:14 -0700844void NativeInputManager::setShowTouches(bool enabled) {
845 { // acquire lock
846 AutoMutex _l(mLock);
847
848 if (mLocked.showTouches == enabled) {
849 return;
850 }
851
Steve Block6215d3f2012-01-04 20:05:49 +0000852 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700853 mLocked.showTouches = enabled;
854 } // release lock
855
856 mInputManager->getReader()->requestRefreshConfiguration(
857 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
858}
859
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800860void NativeInputManager::setPointerCapture(bool enabled) {
861 { // acquire lock
862 AutoMutex _l(mLock);
863
864 if (mLocked.pointerCapture == enabled) {
865 return;
866 }
867
868 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
869 mLocked.pointerCapture = enabled;
870 } // release lock
871
872 mInputManager->getReader()->requestRefreshConfiguration(
873 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
874}
875
Jeff Brown037c33e2014-04-09 00:31:55 -0700876void NativeInputManager::setInteractive(bool interactive) {
877 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700878}
879
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800880void NativeInputManager::reloadCalibration() {
881 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100882 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800883}
884
Michael Wrighte051f6f2016-05-13 17:44:16 +0100885void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800886 AutoMutex _l(mLock);
887 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100888 if (controller != nullptr) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100889 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800890 }
891}
892
893void NativeInputManager::reloadPointerIcons() {
894 AutoMutex _l(mLock);
895 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100896 if (controller != nullptr) {
Jun Mukai19a56012015-11-24 11:25:52 -0800897 controller->reloadPointerResources();
898 }
Jun Mukai1db53972015-09-11 18:08:31 -0700899}
900
Jun Mukaid4eaef72015-10-30 15:54:33 -0700901void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
902 AutoMutex _l(mLock);
903 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100904 if (controller != nullptr) {
Jun Mukaid4eaef72015-10-30 15:54:33 -0700905 controller->setCustomPointerIcon(icon);
906 }
907}
908
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800909TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
910 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100911 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800912 ScopedFloatArrayRO matrix(env, matrixArr);
913 assert(matrix.size() == 6);
914
915 TouchAffineTransformation transform;
916 transform.x_scale = matrix[0];
917 transform.x_ymix = matrix[1];
918 transform.x_offset = matrix[2];
919 transform.y_xmix = matrix[3];
920 transform.y_scale = matrix[4];
921 transform.y_offset = matrix[5];
922
923 return transform;
924}
925
926TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100927 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800928 JNIEnv* env = jniEnv();
929
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100930 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.c_str()));
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800931
932 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700933 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
934 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800935
936 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
937 gTouchCalibrationClassInfo.getAffineTransform));
938
939 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
940
941 env->DeleteLocalRef(matrixArr);
942 env->DeleteLocalRef(cal);
943
944 return transform;
945}
946
Jeff Brown0029c662011-03-30 02:25:18 -0700947bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100948 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700949 jobject inputEventObj;
950
951 JNIEnv* env = jniEnv();
952 switch (inputEvent->getType()) {
953 case AINPUT_EVENT_TYPE_KEY:
954 inputEventObj = android_view_KeyEvent_fromNative(env,
955 static_cast<const KeyEvent*>(inputEvent));
956 break;
957 case AINPUT_EVENT_TYPE_MOTION:
958 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
959 static_cast<const MotionEvent*>(inputEvent));
960 break;
961 default:
962 return true; // dispatch the event normally
963 }
964
965 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000966 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700967 return true; // dispatch the event normally
968 }
969
970 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700971 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700972 inputEventObj, policyFlags);
973 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
974 pass = true;
975 }
976 env->DeleteLocalRef(inputEventObj);
977 return pass;
978}
979
Jeff Brown1f245102010-11-18 20:53:46 -0800980void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
981 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100982 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700983 // Policy:
984 // - Ignore untrusted events and pass them along.
985 // - Ask the window manager what to do with normal events and trusted injected events.
986 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100987 bool interactive = mInteractive.load();
988 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700989 policyFlags |= POLICY_FLAG_INTERACTIVE;
990 }
Jeff Brown3122e442010-10-11 23:32:49 -0700991 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800992 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700993 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800994 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
995 jint wmActions;
996 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700997 wmActions = env->CallIntMethod(mServiceObj,
998 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700999 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001000 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
1001 wmActions = 0;
1002 }
1003 android_view_KeyEvent_recycle(env, keyEventObj);
1004 env->DeleteLocalRef(keyEventObj);
1005 } else {
Steve Block3762c312012-01-06 19:20:56 +00001006 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -07001007 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -07001008 }
1009
Jeff Brown56194eb2011-03-02 19:23:13 -08001010 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -07001011 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001012 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001013 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1014 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001015 }
1016}
1017
Jeff Brown56194eb2011-03-02 19:23:13 -08001018void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001019 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001020 // Policy:
1021 // - Ignore untrusted events and pass them along.
1022 // - No special filtering for injected events required at this time.
1023 // - Filter normal events based on screen state.
1024 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001025 bool interactive = mInteractive.load();
1026 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001027 policyFlags |= POLICY_FLAG_INTERACTIVE;
1028 }
Jeff Brown3122e442010-10-11 23:32:49 -07001029 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001030 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001031 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001032 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001033 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001034 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001035 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001036 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001037 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001038 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001039 wmActions = 0;
1040 }
1041
Jeff Brown56194eb2011-03-02 19:23:13 -08001042 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001043 }
Jeff Brown3122e442010-10-11 23:32:49 -07001044 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001045 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001046 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1047 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001048 }
1049}
1050
Jeff Brown56194eb2011-03-02 19:23:13 -08001051void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1052 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001053 if (wmActions & WM_ACTION_PASS_TO_USER) {
1054 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1055 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001056#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001057 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001058#endif
1059 }
1060}
1061
Jeff Brown905805a2011-10-12 13:57:59 -07001062nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -08001063 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -07001064 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001065 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001066 // Policy:
1067 // - Ignore untrusted events and pass them along.
1068 // - Filter normal events and trusted injected events through the window manager policy to
1069 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001070 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001071 if (policyFlags & POLICY_FLAG_TRUSTED) {
1072 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001073
Jeff Brown928e0542011-01-10 11:17:36 -08001074 // Note: inputWindowHandle may be null.
1075 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001076 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1077 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001078 jlong delayMillis = env->CallLongMethod(mServiceObj,
1079 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -08001080 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001081 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1082 android_view_KeyEvent_recycle(env, keyEventObj);
1083 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001084 if (!error) {
1085 if (delayMillis < 0) {
1086 result = -1;
1087 } else if (delayMillis > 0) {
1088 result = milliseconds_to_nanoseconds(delayMillis);
1089 }
1090 }
Jeff Brown1f245102010-11-18 20:53:46 -08001091 } else {
Steve Block3762c312012-01-06 19:20:56 +00001092 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001093 }
Jeff Brown928e0542011-01-10 11:17:36 -08001094 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -07001095 }
Jeff Brown1f245102010-11-18 20:53:46 -08001096 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001097}
1098
Jeff Brown928e0542011-01-10 11:17:36 -08001099bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001100 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001101 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001102 // Policy:
1103 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001104 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001105 if (policyFlags & POLICY_FLAG_TRUSTED) {
1106 JNIEnv* env = jniEnv();
1107
Jeff Brown928e0542011-01-10 11:17:36 -08001108 // Note: inputWindowHandle may be null.
1109 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001110 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1111 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001112 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1113 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -08001114 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001115 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001116 fallbackKeyEventObj = nullptr;
Jeff Brownda3d5a92011-03-29 15:11:34 -07001117 }
Jeff Brown1f245102010-11-18 20:53:46 -08001118 android_view_KeyEvent_recycle(env, keyEventObj);
1119 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001120
1121 if (fallbackKeyEventObj) {
1122 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1123 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1124 outFallbackKeyEvent)) {
1125 result = true;
1126 }
1127 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1128 env->DeleteLocalRef(fallbackKeyEventObj);
1129 }
Jeff Brown1f245102010-11-18 20:53:46 -08001130 } else {
Steve Block3762c312012-01-06 19:20:56 +00001131 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001132 }
Jeff Brown928e0542011-01-10 11:17:36 -08001133 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001134 }
Jeff Brown1f245102010-11-18 20:53:46 -08001135 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001136}
1137
Jeff Brown01ce2e92010-09-26 22:20:12 -07001138void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001139 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001140 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001141}
1142
Jeff Brown349703e2010-06-22 01:27:15 -07001143
Jeff Brownb88102f2010-09-08 11:49:43 -07001144bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1145 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001146 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001147 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001148 jboolean result = env->CallBooleanMethod(mServiceObj,
1149 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001150 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1151 result = false;
1152 }
Jeff Brown349703e2010-06-22 01:27:15 -07001153 return result;
1154}
1155
Jun Mukai19a56012015-11-24 11:25:52 -08001156void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001157 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001158 JNIEnv* env = jniEnv();
1159
1160 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1161 mServiceObj, gServiceClassInfo.getPointerIcon));
1162 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1163 return;
1164 }
1165
1166 PointerIcon pointerIcon;
1167 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1168 mContextObj, &pointerIcon);
1169 if (!status && !pointerIcon.isNullIcon()) {
1170 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1171 } else {
1172 *icon = SpriteIcon();
1173 }
1174}
1175
Jeff Brown2352b972011-04-12 22:39:53 -07001176void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001177 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001178 JNIEnv* env = jniEnv();
1179
1180 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1181 &outResources->spotHover);
1182 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1183 &outResources->spotTouch);
1184 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1185 &outResources->spotAnchor);
1186}
1187
Jun Mukai808196f2015-10-28 16:46:44 -07001188void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1189 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001190 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001191 JNIEnv* env = jniEnv();
1192
1193 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1194 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001195 PointerIcon pointerIcon;
1196 loadSystemIconAsSpriteWithPointerIcon(
1197 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1198 if (!pointerIcon.bitmapFrames.empty()) {
1199 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1200 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1201 animationData.durationPerFrame =
1202 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1203 animationData.animationFrames.reserve(numFrames);
1204 animationData.animationFrames.push_back(SpriteIcon(
1205 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1206 for (size_t i = 0; i < numFrames - 1; ++i) {
1207 animationData.animationFrames.push_back(SpriteIcon(
1208 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1209 }
1210 }
Jun Mukai1db53972015-09-11 18:08:31 -07001211 }
Jun Mukai808196f2015-10-28 16:46:44 -07001212 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1213 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001214}
1215
Jun Mukai5ec74202015-10-07 16:58:09 +09001216int32_t NativeInputManager::getDefaultPointerIconId() {
1217 return POINTER_ICON_STYLE_ARROW;
1218}
Jeff Brown83c09682010-12-23 17:50:18 -08001219
Jun Mukaid4eaef72015-10-30 15:54:33 -07001220int32_t NativeInputManager::getCustomPointerIconId() {
1221 return POINTER_ICON_STYLE_CUSTOM;
1222}
1223
Jeff Brown9c3cda02010-06-15 01:31:58 -07001224// ----------------------------------------------------------------------------
1225
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001226static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001227 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001228 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001229 if (messageQueue == nullptr) {
Jeff Brown864693462013-01-28 14:25:53 -08001230 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1231 return 0;
1232 }
1233
Jeff Brown603b4452012-04-06 17:39:41 -07001234 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1235 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001236 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001237 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001238}
1239
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001240static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001241 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001242
Jeff Brown4532e612012-04-05 14:27:12 -07001243 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001244 if (result) {
1245 jniThrowRuntimeException(env, "Input manager could not be started.");
1246 }
1247}
1248
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001249static void nativeSetDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
Santos Cordonee8931e2017-04-05 10:31:15 -07001250 jobjectArray viewportObjArray) {
1251 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001252 im->setDisplayViewports(env, viewportObjArray);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001253}
1254
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001255static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001256 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001257 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001258
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001259 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001260 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001261}
1262
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001263static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001264 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001265 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001266
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001267 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001268 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001269}
1270
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001271static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001272 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001273 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001274
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001275 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001276 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001277}
1278
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001279static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001280 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001281 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001282
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001283 int32_t* codes = env->GetIntArrayElements(keyCodes, nullptr);
1284 uint8_t* flags = env->GetBooleanArrayElements(outFlags, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001285 jsize numCodes = env->GetArrayLength(keyCodes);
1286 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001287 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001288 if (im->getInputManager()->getReader()->hasKeys(
1289 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1290 result = JNI_TRUE;
1291 } else {
1292 result = JNI_FALSE;
1293 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001294 } else {
1295 result = JNI_FALSE;
1296 }
1297
1298 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1299 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1300 return result;
1301}
1302
1303static void throwInputChannelNotInitialized(JNIEnv* env) {
1304 jniThrowException(env, "java/lang/IllegalStateException",
1305 "inputChannel is not initialized");
1306}
1307
Jeff Brown4532e612012-04-05 14:27:12 -07001308static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001309 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001310 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1311
Steve Block8564c8d2012-01-05 23:22:43 +00001312 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001313 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001314 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001315}
1316
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001317static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Arthur Hungbe5ce212018-09-13 18:41:56 +08001318 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001319 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001320
1321 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1322 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001323 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001324 throwInputChannelNotInitialized(env);
1325 return;
1326 }
1327
Jeff Brown928e0542011-01-10 11:17:36 -08001328 sp<InputWindowHandle> inputWindowHandle =
1329 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001330
Jeff Brown4532e612012-04-05 14:27:12 -07001331 status_t status = im->registerInputChannel(
Arthur Hungbe5ce212018-09-13 18:41:56 +08001332 env, inputChannel, inputWindowHandle, displayId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001333 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001334 std::string message;
1335 message += StringPrintf("Failed to register input channel. status=%d", status);
1336 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001337 return;
1338 }
1339
Arthur Hungbe5ce212018-09-13 18:41:56 +08001340 // If inputWindowHandle is null and displayId >= 0, treat inputChannel as monitor.
1341 if (inputWindowHandle != nullptr || displayId == ADISPLAY_ID_NONE) {
Jeff Browna41ca772010-08-11 14:46:32 -07001342 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001343 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001344 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001345}
1346
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001347static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001348 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001349 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001350
1351 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1352 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001353 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001354 throwInputChannelNotInitialized(env);
1355 return;
1356 }
1357
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001358 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001359
Jeff Brown4532e612012-04-05 14:27:12 -07001360 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001361 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001362 std::string message;
1363 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1364 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001365 }
1366}
1367
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001368static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001369 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001370 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001371
Jeff Brown4532e612012-04-05 14:27:12 -07001372 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001373}
1374
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001375static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001376 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001377 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001378 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001379
Jeff Brown6ec402b2010-07-28 15:48:59 -07001380 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1381 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001382 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1383 if (status) {
1384 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1385 return INPUT_EVENT_INJECTION_FAILED;
1386 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001387
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001388 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001389 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001390 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001391 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001392 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1393 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001394 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1395 return INPUT_EVENT_INJECTION_FAILED;
1396 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001397
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001398 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001399 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001400 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001401 } else {
1402 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001403 return INPUT_EVENT_INJECTION_FAILED;
1404 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001405}
1406
Andrii Kulian112d0562016-03-08 10:44:22 -08001407static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1408 jlong ptr, jint deviceId) {
1409 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001410
Andrii Kulian112d0562016-03-08 10:44:22 -08001411 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1412}
1413
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001414static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hung39134b22018-08-14 11:58:28 +08001415 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001416 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001417
Arthur Hung39134b22018-08-14 11:58:28 +08001418 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001419}
1420
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001421static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001422 jlong ptr, jint displayId, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001423 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001424
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001425 im->setFocusedApplication(env, displayId, applicationHandleObj);
1426}
1427
1428static void nativeSetFocusedDisplay(JNIEnv* env, jclass /* clazz */,
1429 jlong ptr, jint displayId) {
1430 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1431
1432 im->setFocusedDisplay(env, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001433}
1434
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001435static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1436 jboolean enabled) {
1437 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001438
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001439 im->setPointerCapture(enabled);
1440}
1441
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001442static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1443 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001444 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001445
Jeff Brown4532e612012-04-05 14:27:12 -07001446 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001447}
1448
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001449static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1450 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001451 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001452
Jeff Brown4532e612012-04-05 14:27:12 -07001453 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001454}
1455
Jeff Brown4532e612012-04-05 14:27:12 -07001456static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001457 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001458 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001459
1460 sp<InputChannel> fromChannel =
1461 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1462 sp<InputChannel> toChannel =
1463 android_view_InputChannel_getInputChannel(env, toChannelObj);
1464
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001465 if (fromChannel == nullptr || toChannel == nullptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001466 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001467 }
1468
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001469 if (im->getInputManager()->getDispatcher()->
1470 transferTouchFocus(fromChannel, toChannel)) {
1471 return JNI_TRUE;
1472 } else {
1473 return JNI_FALSE;
1474 }
Jeff Browne6504122010-09-27 14:52:15 -07001475}
1476
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001477static void nativeSetPointerSpeed(JNIEnv* /* env */,
1478 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001479 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001480
Jeff Brown4532e612012-04-05 14:27:12 -07001481 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001482}
1483
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001484static void nativeSetShowTouches(JNIEnv* /* env */,
1485 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001486 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001487
Jeff Brown4532e612012-04-05 14:27:12 -07001488 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001489}
1490
Jeff Brown037c33e2014-04-09 00:31:55 -07001491static void nativeSetInteractive(JNIEnv* env,
1492 jclass clazz, jlong ptr, jboolean interactive) {
1493 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1494
1495 im->setInteractive(interactive);
1496}
1497
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001498static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1499 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001500
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001501 im->reloadCalibration();
1502}
1503
Jeff Browna47425a2012-04-13 04:09:27 -07001504static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001505 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001506 jint repeat, jint token) {
1507 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1508
1509 size_t patternSize = env->GetArrayLength(patternObj);
1510 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001511 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001512 "which is more than the maximum supported size of %d.",
1513 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1514 return; // limit to reasonable size
1515 }
1516
1517 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001518 patternObj, nullptr));
Jeff Browna47425a2012-04-13 04:09:27 -07001519 nsecs_t pattern[patternSize];
1520 for (size_t i = 0; i < patternSize; i++) {
1521 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001522 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001523 }
1524 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1525
1526 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1527}
1528
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001529static void nativeCancelVibrate(JNIEnv* /* env */,
1530 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001531 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1532
1533 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1534}
1535
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001536static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1537 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001538 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1539
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001540 im->getInputManager()->getReader()->requestRefreshConfiguration(
1541 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1542}
1543
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001544static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1545 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001546 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1547
1548 im->getInputManager()->getReader()->requestRefreshConfiguration(
1549 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001550}
1551
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001552static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001553 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001554
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001555 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001556 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001557 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001558}
1559
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001560static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001561 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001562
Jeff Brown4532e612012-04-05 14:27:12 -07001563 im->getInputManager()->getReader()->monitor();
1564 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001565}
1566
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001567static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1568 jclass /* clazz */, jlong ptr, jint deviceId) {
1569 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1570
1571 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1572}
1573
1574static void nativeEnableInputDevice(JNIEnv* /* env */,
1575 jclass /* clazz */, jlong ptr, jint deviceId) {
1576 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1577
1578 im->setInputDeviceEnabled(deviceId, true);
1579}
1580
1581static void nativeDisableInputDevice(JNIEnv* /* env */,
1582 jclass /* clazz */, jlong ptr, jint deviceId) {
1583 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1584
1585 im->setInputDeviceEnabled(deviceId, false);
1586}
1587
Michael Wrighte051f6f2016-05-13 17:44:16 +01001588static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001589 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001590
Michael Wrighte051f6f2016-05-13 17:44:16 +01001591 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001592}
1593
Jun Mukai19a56012015-11-24 11:25:52 -08001594static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1595 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001596
Jun Mukai19a56012015-11-24 11:25:52 -08001597 im->reloadPointerIcons();
1598}
1599
Jun Mukaid4eaef72015-10-30 15:54:33 -07001600static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1601 jlong ptr, jobject iconObj) {
1602 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1603
1604 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001605 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1606 if (result) {
1607 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1608 return;
1609 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001610
1611 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001612 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1613 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1614 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1615 spriteIcon.bitmap.rowBytes(), 0, 0);
1616 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001617 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1618 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1619 im->setCustomPointerIcon(spriteIcon);
1620}
1621
Jeff Brown9c3cda02010-06-15 01:31:58 -07001622// ----------------------------------------------------------------------------
1623
Daniel Micay76f6a862015-09-19 17:31:01 -04001624static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001625 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001626 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001627 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001628 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001629 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001630 (void*) nativeStart },
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001631 { "nativeSetDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1632 (void*) nativeSetDisplayViewports },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001633 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001634 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001635 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001636 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001637 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001638 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001639 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001640 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001641 { "nativeRegisterInputChannel",
Arthur Hungbe5ce212018-09-13 18:41:56 +08001642 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001643 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001644 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001645 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001646 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001647 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001648 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001649 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001650 { "nativeToggleCapsLock", "(JI)V",
1651 (void*) nativeToggleCapsLock },
Arthur Hung39134b22018-08-14 11:58:28 +08001652 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001653 (void*) nativeSetInputWindows },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001654 { "nativeSetFocusedApplication", "(JILcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001655 (void*) nativeSetFocusedApplication },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001656 { "nativeSetFocusedDisplay", "(JI)V",
1657 (void*) nativeSetFocusedDisplay },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001658 { "nativeSetPointerCapture", "(JZ)V",
1659 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001660 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001661 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001662 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001663 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001664 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001665 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001666 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001667 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001668 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001669 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001670 { "nativeSetInteractive", "(JZ)V",
1671 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001672 { "nativeReloadCalibration", "(J)V",
1673 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001674 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001675 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001676 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001677 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001678 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001679 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001680 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001681 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001682 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001683 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001684 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001685 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001686 { "nativeIsInputDeviceEnabled", "(JI)Z",
1687 (void*) nativeIsInputDeviceEnabled },
1688 { "nativeEnableInputDevice", "(JI)V",
1689 (void*) nativeEnableInputDevice },
1690 { "nativeDisableInputDevice", "(JI)V",
1691 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001692 { "nativeSetPointerIconType", "(JI)V",
1693 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001694 { "nativeReloadPointerIcons", "(J)V",
1695 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001696 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1697 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001698};
1699
1700#define FIND_CLASS(var, className) \
1701 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001702 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001703
1704#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1705 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001706 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001707
1708#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1709 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001710 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001711
1712int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001713 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001714 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001715 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001716 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1717
Jeff Brown9c3cda02010-06-15 01:31:58 -07001718 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001719
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001720 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001721 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001722
Jeff Brown4532e612012-04-05 14:27:12 -07001723 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001724 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001725
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001726 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1727 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1728
Jeff Brown53384282012-08-20 20:16:01 -07001729 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1730 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001731
Jeff Brown4532e612012-04-05 14:27:12 -07001732 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1733 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001734
Jeff Brown4532e612012-04-05 14:27:12 -07001735 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001736 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001737 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001738
Jeff Brown4532e612012-04-05 14:27:12 -07001739 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001740 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1741
Jeff Brown4532e612012-04-05 14:27:12 -07001742 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001743 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001744
Michael Wright70af00a2014-09-03 19:30:20 -07001745 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1746 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001747
Jeff Brown4532e612012-04-05 14:27:12 -07001748 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001749 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001750 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001751
Jeff Brown4532e612012-04-05 14:27:12 -07001752 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001753 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001754 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001755
Jeff Brown4532e612012-04-05 14:27:12 -07001756 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001757 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001758
Jeff Brown4532e612012-04-05 14:27:12 -07001759 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001760 "getVirtualKeyQuietTimeMillis", "()I");
1761
Jeff Brown4532e612012-04-05 14:27:12 -07001762 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001763 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1764
Jeff Brown4532e612012-04-05 14:27:12 -07001765 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001766 "getKeyRepeatTimeout", "()I");
1767
Jeff Brown4532e612012-04-05 14:27:12 -07001768 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001769 "getKeyRepeatDelay", "()I");
1770
Jeff Brown4532e612012-04-05 14:27:12 -07001771 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001772 "getHoverTapTimeout", "()I");
1773
Jeff Brown4532e612012-04-05 14:27:12 -07001774 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001775 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001776
Jeff Brown4532e612012-04-05 14:27:12 -07001777 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001778 "getDoubleTapTimeout", "()I");
1779
Jeff Brown4532e612012-04-05 14:27:12 -07001780 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001781 "getLongPressTimeout", "()I");
1782
Jeff Brown4532e612012-04-05 14:27:12 -07001783 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001784 "getPointerLayer", "()I");
1785
Jeff Brown4532e612012-04-05 14:27:12 -07001786 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001787 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001788
Jeff Brown6ec6f792012-04-17 16:52:41 -07001789 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001790 "getKeyboardLayoutOverlay",
1791 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001792
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001793 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1794 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1795
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001796 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1797 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001798 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001799
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001800 // InputDevice
1801
1802 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1803 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1804
Jeff Brown6ec402b2010-07-28 15:48:59 -07001805 // KeyEvent
1806
1807 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001808 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1809
Jeff Brown8d608662010-08-30 03:02:23 -07001810 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001811
1812 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001813 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001814
RoboErikfb290df2013-12-16 11:27:55 -08001815 // InputDeviceIdentifier
1816
1817 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1818 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1819 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1820 "<init>", "(Ljava/lang/String;II)V");
1821
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001822 // TouchCalibration
1823
1824 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1825 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1826
1827 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1828 "getAffineTransform", "()[F");
1829
Jeff Brown46b9ac02010-04-22 18:58:52 -07001830 return 0;
1831}
1832
Jeff Brown46b9ac02010-04-22 18:58:52 -07001833} /* namespace android */