blob: e2db807442940ec91dbdd89c3bce29e25eedb1ee [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputManager-JNI"
18
Michael Wrightbafea6e2017-06-12 15:25:47 +010019#define ATRACE_TAG ATRACE_TAG_INPUT
20
Jeff Brown9c3cda02010-06-15 01:31:58 -070021//#define LOG_NDEBUG 0
22
23// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070024#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070025
26// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070027#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070028
Jeff Brown83c09682010-12-23 17:50:18 -080029
Steven Moreland2279b252017-07-19 09:50:45 -070030#include <nativehelper/JNIHelp.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031#include "jni.h"
Michael Wrighta4051212015-07-23 17:04:40 +010032#include <atomic>
33#include <cinttypes>
Jeff Brown349703e2010-06-22 01:27:15 -070034#include <limits.h>
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080035#include <android-base/stringprintf.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070037#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038
Jeff Brown46b9ac02010-04-22 18:58:52 -070039#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080040#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070041#include <utils/threads.h>
Michael Wrightbafea6e2017-06-12 15:25:47 +010042#include <utils/Trace.h>
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070043#include <utils/SortedVector.h>
Jeff Brown83c09682010-12-23 17:50:18 -080044
Robert Carr788f5742018-07-30 17:46:45 -070045#include <binder/IServiceManager.h>
46
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070048#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080049
Michael Wrightd6b473712014-02-10 15:56:36 -080050#include <inputflinger/InputManager.h>
51
Jeff Brown05dc66a2011-03-02 14:41:58 -080052#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070053#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080054#include <android_view_KeyEvent.h>
55#include <android_view_MotionEvent.h>
56#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070057#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080058#include <android/graphics/GraphicsJNI.h>
59
Steven Moreland2279b252017-07-19 09:50:45 -070060#include <nativehelper/ScopedLocalRef.h>
61#include <nativehelper/ScopedPrimitiveArray.h>
62#include <nativehelper/ScopedUtfChars.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070063
Jeff Brown4f8ecd82012-06-18 18:29:13 -070064#include "com_android_server_power_PowerManagerService.h"
Robert Carre1db3202018-07-23 15:24:59 -070065#include "android_hardware_input_InputApplicationHandle.h"
66#include "android_hardware_input_InputWindowHandle.h"
Santos Cordonee8931e2017-04-05 10:31:15 -070067#include "android_hardware_display_DisplayViewport.h"
Robert Carre0a353c2018-08-02 16:38:04 -070068#include "android_util_Binder.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070069
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010070#include <vector>
71
Michael Wrighta4051212015-07-23 17:04:40 +010072#define INDENT " "
73
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080074using android::base::StringPrintf;
75
Jeff Brown46b9ac02010-04-22 18:58:52 -070076namespace android {
77
Jeff Brown1a84fd12011-06-02 01:26:32 -070078// The exponent used to calculate the pointer speed scaling factor.
79// The scaling factor is calculated as 2 ^ (speed * exponent),
80// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070081static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070082
Jeff Brown46b9ac02010-04-22 18:58:52 -070083static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070084 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070085 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070086 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070087 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070088 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070089 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070090 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070091 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070092 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070093 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070094 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080095 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070096 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080097 jmethodID getKeyRepeatTimeout;
98 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070099 jmethodID getHoverTapTimeout;
100 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -0700101 jmethodID getDoubleTapTimeout;
102 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -0800103 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800104 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700105 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700106 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800107 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -0700108} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700109
110static struct {
111 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700112} gInputDeviceClassInfo;
113
114static struct {
115 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700116} gKeyEventClassInfo;
117
118static struct {
119 jclass clazz;
120} gMotionEventClassInfo;
121
RoboErikfb290df2013-12-16 11:27:55 -0800122static struct {
123 jclass clazz;
124 jmethodID constructor;
125} gInputDeviceIdentifierInfo;
126
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800127static struct {
128 jclass clazz;
129 jmethodID getAffineTransform;
130} gTouchCalibrationClassInfo;
131
RoboErikfb290df2013-12-16 11:27:55 -0800132
Jeff Brown928e0542011-01-10 11:17:36 -0800133
134// --- Global functions ---
135
Jeff Brown214eaf42011-05-26 19:17:02 -0700136template<typename T>
137inline static T min(const T& a, const T& b) {
138 return a < b ? a : b;
139}
140
141template<typename T>
142inline static T max(const T& a, const T& b) {
143 return a > b ? a : b;
144}
145
Michael Wrighta4051212015-07-23 17:04:40 +0100146static inline const char* toString(bool value) {
147 return value ? "true" : "false";
148}
149
Jeff Brown928e0542011-01-10 11:17:36 -0800150static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
151 const sp<InputApplicationHandle>& inputApplicationHandle) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100152 if (inputApplicationHandle == nullptr) {
153 return nullptr;
Jeff Brown928e0542011-01-10 11:17:36 -0800154 }
155 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
156 getInputApplicationHandleObjLocalRef(env);
157}
158
Jun Mukai808196f2015-10-28 16:46:44 -0700159static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
160 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700161 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700162 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700163 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400164 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
165 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
166 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
167 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
168 bitmapCopy->rowBytes(), 0, 0);
169 }
Jun Mukai808196f2015-10-28 16:46:44 -0700170 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
171 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700172 }
173}
174
Jun Mukai808196f2015-10-28 16:46:44 -0700175static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
176 SpriteIcon* outSpriteIcon) {
177 PointerIcon pointerIcon;
178 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
179}
180
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100181static void updatePointerControllerFromViewport(
182 sp<PointerController> controller, const DisplayViewport* const viewport) {
183 if (controller != nullptr && viewport != nullptr) {
184 const int32_t width = viewport->logicalRight - viewport->logicalLeft;
185 const int32_t height = viewport->logicalBottom - viewport->logicalTop;
186 controller->setDisplayViewport(width, height, viewport->orientation);
187 }
188}
189
Jeff Brown905805a2011-10-12 13:57:59 -0700190enum {
191 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700192};
193
Jeff Brown928e0542011-01-10 11:17:36 -0800194
195// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800196
Jeff Brown9c3cda02010-06-15 01:31:58 -0700197class NativeInputManager : public virtual RefBase,
198 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700199 public virtual InputDispatcherPolicyInterface,
200 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700201protected:
202 virtual ~NativeInputManager();
203
204public:
Jeff Brown4532e612012-04-05 14:27:12 -0700205 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700206
207 inline sp<InputManager> getInputManager() const { return mInputManager; }
208
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800209 void dump(std::string& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700210
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100211 void setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700212
Robert Carre0a353c2018-08-02 16:38:04 -0700213 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel, int32_t displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700214 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
215
Arthur Hung39134b22018-08-14 11:58:28 +0800216 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray, int32_t displayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800217 void setFocusedApplication(JNIEnv* env, int32_t displayId, jobject applicationHandleObj);
218 void setFocusedDisplay(JNIEnv* env, int32_t displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700219 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800220 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700221 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700222 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700223 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700224 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800225 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100226 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800227 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700228 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800229 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700230
Jeff Brown9c3cda02010-06-15 01:31:58 -0700231 /* --- InputReaderPolicyInterface implementation --- */
232
Jeff Brown214eaf42011-05-26 19:17:02 -0700233 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800234 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700235 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800236 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100237 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700238 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
239 jfloatArray matrixArr);
240 virtual TouchAffineTransformation getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100241 const std::string& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700242
243 /* --- InputDispatcherPolicyInterface implementation --- */
244
Jeff Brownbcc046a2012-09-27 20:46:43 -0700245 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700246 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700247 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700248 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carre0a353c2018-08-02 16:38:04 -0700249 const sp<IBinder>& token,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800250 const std::string& reason);
Robert Carre0a353c2018-08-02 16:38:04 -0700251 virtual void notifyInputChannelBroken(const sp<IBinder>& token);
Jeff Brown0029c662011-03-30 02:25:18 -0700252 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700253 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800254 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800255 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700256 virtual nsecs_t interceptKeyBeforeDispatching(
Robert Carre0a353c2018-08-02 16:38:04 -0700257 const sp<IBinder>& token,
Jeff Brownb88102f2010-09-08 11:49:43 -0700258 const KeyEvent* keyEvent, uint32_t policyFlags);
Robert Carre0a353c2018-08-02 16:38:04 -0700259 virtual bool dispatchUnhandledKey(const sp<IBinder>& token,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800260 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700261 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700262 virtual bool checkInjectEventsPermissionNonReentrant(
263 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700264
Jeff Brown2352b972011-04-12 22:39:53 -0700265 /* --- PointerControllerPolicyInterface implementation --- */
266
Jun Mukai19a56012015-11-24 11:25:52 -0800267 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700268 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700269 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
270 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900271 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700272 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700273
Jeff Brown9c3cda02010-06-15 01:31:58 -0700274private:
275 sp<InputManager> mInputManager;
276
Jeff Brown2352b972011-04-12 22:39:53 -0700277 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700278 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800279 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700280
Jeff Brown83c09682010-12-23 17:50:18 -0800281 Mutex mLock;
282 struct Locked {
283 // Display size information.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100284 std::vector<DisplayViewport> viewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800285
Jeff Brown05dc66a2011-03-02 14:41:58 -0800286 // System UI visibility.
287 int32_t systemUiVisibility;
288
Jeff Brown1a84fd12011-06-02 01:26:32 -0700289 // Pointer speed.
290 int32_t pointerSpeed;
291
Jeff Brown474dcb52011-06-14 20:22:50 -0700292 // True if pointer gestures are enabled.
293 bool pointerGesturesEnabled;
294
Jeff Browndaf4a122011-08-26 17:14:14 -0700295 // Show touches feature enable/disable.
296 bool showTouches;
297
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800298 // Pointer capture feature enable/disable.
299 bool pointerCapture;
300
Jeff Brown5541de92011-04-11 11:54:25 -0700301 // Sprite controller singleton, created on first use.
302 sp<SpriteController> spriteController;
303
Jeff Brown83c09682010-12-23 17:50:18 -0800304 // Pointer controller singleton, created and destroyed as needed.
305 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700306
307 // Input devices to be disabled
308 SortedVector<int32_t> disabledInputDevices;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100309 } mLocked GUARDED_BY(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700310
Michael Wrighta4051212015-07-23 17:04:40 +0100311 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700312
Jeff Brown2352b972011-04-12 22:39:53 -0700313 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800314 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700315 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800316
Jeff Brownb88102f2010-09-08 11:49:43 -0700317 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700318
Jeff Brown9c3cda02010-06-15 01:31:58 -0700319 static inline JNIEnv* jniEnv() {
320 return AndroidRuntime::getJNIEnv();
321 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700322};
323
Jeff Brown928e0542011-01-10 11:17:36 -0800324
Jeff Brown9c3cda02010-06-15 01:31:58 -0700325
Jeff Brown2352b972011-04-12 22:39:53 -0700326NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700327 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700328 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700329 JNIEnv* env = jniEnv();
330
Jeff Brown2352b972011-04-12 22:39:53 -0700331 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700332 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700333
Jeff Brown83c09682010-12-23 17:50:18 -0800334 {
335 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800336 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700337 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700338 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700339 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800340 mLocked.pointerCapture = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800341 }
Michael Wrighta4051212015-07-23 17:04:40 +0100342 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800343
Jeff Brown9c3cda02010-06-15 01:31:58 -0700344 sp<EventHub> eventHub = new EventHub();
345 mInputManager = new InputManager(eventHub, this, this);
346}
347
348NativeInputManager::~NativeInputManager() {
349 JNIEnv* env = jniEnv();
350
Jeff Brown2352b972011-04-12 22:39:53 -0700351 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700352 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700353}
354
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800355void NativeInputManager::dump(std::string& dump) {
356 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100357 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800358 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100359 }
360 {
361 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800362 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100363 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800364 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
365 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100366 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800367 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
368 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100369 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800370 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100371
Jeff Brownb88102f2010-09-08 11:49:43 -0700372 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800373 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700374
Jeff Brownb88102f2010-09-08 11:49:43 -0700375 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800376 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700377}
378
Jeff Brown7fbdc842010-06-17 20:52:56 -0700379bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700380 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000381 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700382 LOGE_EX(env);
383 env->ExceptionClear();
384 return true;
385 }
386 return false;
387}
388
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100389static const DisplayViewport* findInternalViewport(const std::vector<DisplayViewport>& viewports) {
390 for (const DisplayViewport& v : viewports) {
391 if (v.type == ViewportType::VIEWPORT_INTERNAL) {
392 return &v;
393 }
394 }
395 return nullptr;
396}
397
398void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
399 std::vector<DisplayViewport> viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700400
401 if (viewportObjArray) {
402 jsize length = env->GetArrayLength(viewportObjArray);
403 for (jsize i = 0; i < length; i++) {
404 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
405 if (! viewportObj) {
406 break; // found null element indicating end of used portion of the array
407 }
408
409 DisplayViewport viewport;
410 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100411 ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
412 viewports.push_back(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -0700413
414 env->DeleteLocalRef(viewportObj);
415 }
416 }
417
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100418 const DisplayViewport* newInternalViewport = findInternalViewport(viewports);
Santos Cordonee8931e2017-04-05 10:31:15 -0700419 {
420 AutoMutex _l(mLock);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100421 const DisplayViewport* oldInternalViewport = findInternalViewport(mLocked.viewports);
422 // Internal viewport has changed if there wasn't one earlier, and there is one now, or,
423 // if they are different.
424 const bool internalViewportChanged = (newInternalViewport != nullptr) &&
Siarhei Vishniakoue5bf8662018-10-04 09:33:24 -0700425 (oldInternalViewport == nullptr || (*oldInternalViewport != *newInternalViewport));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100426 if (internalViewportChanged) {
427 sp<PointerController> controller = mLocked.pointerController.promote();
428 updatePointerControllerFromViewport(controller, newInternalViewport);
429 }
430 mLocked.viewports = viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700431 }
432
433 mInputManager->getReader()->requestRefreshConfiguration(
434 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
435}
436
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700437status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Robert Carre0a353c2018-08-02 16:38:04 -0700438 const sp<InputChannel>& inputChannel, int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100439 ATRACE_CALL();
Robert Carre0a353c2018-08-02 16:38:04 -0700440 return mInputManager->getDispatcher()->registerInputChannel(
441 inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700442}
443
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700444status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700445 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100446 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700447 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700448}
449
Jeff Brown214eaf42011-05-26 19:17:02 -0700450void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100451 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700452 JNIEnv* env = jniEnv();
453
Jeff Brown4532e612012-04-05 14:27:12 -0700454 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
455 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700456 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
457 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
458 }
459
460 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700461 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
462 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700463 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
464 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700465 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700466 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100467 const char* deviceNameChars = env->GetStringUTFChars(item, nullptr);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100468 outConfig->excludedDeviceNames.push_back(deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700469 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700470 env->DeleteLocalRef(item);
471 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700472 env->DeleteLocalRef(excludedDeviceNames);
473 }
474
Jeff Brown4532e612012-04-05 14:27:12 -0700475 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
476 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700477 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700478 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
479 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700480 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700481 jint longPressTimeout = env->CallIntMethod(mServiceObj,
482 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700483 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700484 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700485
486 // We must ensure that the tap-drag interval is significantly shorter than
487 // the long-press timeout because the tap is held down for the entire duration
488 // of the double-tap timeout.
489 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700490 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700491 outConfig->pointerGestureTapDragInterval =
492 milliseconds_to_nanoseconds(tapDragInterval);
493 }
494 }
495 }
496
Jeff Brown4532e612012-04-05 14:27:12 -0700497 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
498 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700499 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
500 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700501 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700502
503 { // acquire lock
504 AutoMutex _l(mLock);
505
506 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
507 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700508 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700509
Jeff Browndaf4a122011-08-26 17:14:14 -0700510 outConfig->showTouches = mLocked.showTouches;
511
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800512 outConfig->pointerCapture = mLocked.pointerCapture;
513
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100514 outConfig->setDisplayViewports(mLocked.viewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700515
516 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700517 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700518}
519
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700520sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100521 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800522 AutoMutex _l(mLock);
523
524 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100525 if (controller == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700526 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800527
Jeff Brown2352b972011-04-12 22:39:53 -0700528 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800529 mLocked.pointerController = controller;
530
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100531 const DisplayViewport* internalViewport = findInternalViewport(mLocked.viewports);
532 updatePointerControllerFromViewport(controller, internalViewport);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800533
Jeff Brown2352b972011-04-12 22:39:53 -0700534 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800535 }
536 return controller;
537}
538
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100539void NativeInputManager::ensureSpriteControllerLocked() REQUIRES(mLock) {
540 if (mLocked.spriteController == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700541 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700542 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700543 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
544 layer = -1;
545 }
546 mLocked.spriteController = new SpriteController(mLooper, layer);
547 }
548}
549
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700550void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100551 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700552 JNIEnv* env = jniEnv();
553
554 size_t count = inputDevices.size();
555 jobjectArray inputDevicesObjArray = env->NewObjectArray(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100556 count, gInputDeviceClassInfo.clazz, nullptr);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700557 if (inputDevicesObjArray) {
558 bool error = false;
559 for (size_t i = 0; i < count; i++) {
560 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
561 if (!inputDeviceObj) {
562 error = true;
563 break;
564 }
565
566 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
567 env->DeleteLocalRef(inputDeviceObj);
568 }
569
570 if (!error) {
571 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
572 inputDevicesObjArray);
573 }
574
575 env->DeleteLocalRef(inputDevicesObjArray);
576 }
577
578 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
579}
580
Jeff Brown6ec6f792012-04-17 16:52:41 -0700581sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800582 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100583 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700584 JNIEnv* env = jniEnv();
585
586 sp<KeyCharacterMap> result;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100587 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
RoboErikfb290df2013-12-16 11:27:55 -0800588 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
589 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
590 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700591 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800592 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700593 if (arrayObj.get()) {
594 ScopedLocalRef<jstring> filenameObj(env,
595 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
596 ScopedLocalRef<jstring> contentsObj(env,
597 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
598 ScopedUtfChars filenameChars(env, filenameObj.get());
599 ScopedUtfChars contentsChars(env, contentsObj.get());
600
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100601 KeyCharacterMap::loadContents(filenameChars.c_str(),
602 contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700603 }
604 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
605 return result;
606}
607
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100608std::string NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100609 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700610 JNIEnv* env = jniEnv();
611
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100612 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.c_str()));
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700613 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
614 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100615 std::string result;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700616 if (aliasObj.get()) {
617 ScopedUtfChars aliasChars(env, aliasObj.get());
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100618 result = aliasChars.c_str();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700619 }
620 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
621 return result;
622}
623
Jeff Brownbcc046a2012-09-27 20:46:43 -0700624void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700625 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700626#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700627 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
628 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700629#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100630 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700631
632 JNIEnv* env = jniEnv();
633
Jeff Brown53384282012-08-20 20:16:01 -0700634 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700635 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700636 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700637}
638
Jeff Brown9c3cda02010-06-15 01:31:58 -0700639void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
640#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000641 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700642#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100643 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700644
645 JNIEnv* env = jniEnv();
646
Jeff Brown4532e612012-04-05 14:27:12 -0700647 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700648 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700649}
650
Jeff Brown519e0242010-09-15 15:18:56 -0700651nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carre0a353c2018-08-02 16:38:04 -0700652 const sp<IBinder>& token, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700653#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000654 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700655#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100656 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700657
658 JNIEnv* env = jniEnv();
659
Jeff Brown928e0542011-01-10 11:17:36 -0800660 jobject inputApplicationHandleObj =
661 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
Robert Carre0a353c2018-08-02 16:38:04 -0700662 jobject tokenObj = javaObjectForIBinder(env, token);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800663 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700664
Jeff Brown4532e612012-04-05 14:27:12 -0700665 jlong newTimeout = env->CallLongMethod(mServiceObj,
Robert Carre0a353c2018-08-02 16:38:04 -0700666 gServiceClassInfo.notifyANR, inputApplicationHandleObj, tokenObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700667 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700668 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
669 newTimeout = 0; // abort dispatch
670 } else {
671 assert(newTimeout >= 0);
672 }
673
Jeff Brownbd181bb2013-09-10 16:44:24 -0700674 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800675 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700676 return newTimeout;
677}
678
Robert Carre0a353c2018-08-02 16:38:04 -0700679void NativeInputManager::notifyInputChannelBroken(const sp<IBinder>& token) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700680#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000681 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700682#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100683 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700684
Jeff Brown7fbdc842010-06-17 20:52:56 -0700685 JNIEnv* env = jniEnv();
686
Robert Carre0a353c2018-08-02 16:38:04 -0700687 jobject tokenObj = javaObjectForIBinder(env, token);
688 if (tokenObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700689 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Robert Carre0a353c2018-08-02 16:38:04 -0700690 tokenObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700691 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700692 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700693}
694
Jeff Brown214eaf42011-05-26 19:17:02 -0700695void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100696 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700697 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800698
Jeff Brown4532e612012-04-05 14:27:12 -0700699 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
700 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700701 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
702 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
703 }
Jeff Browna4547672011-03-02 21:38:11 -0800704
Jeff Brown4532e612012-04-05 14:27:12 -0700705 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
706 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700707 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
708 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
709 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700710}
711
Arthur Hung39134b22018-08-14 11:58:28 +0800712void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
713 int32_t displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700714 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700715
Jeff Brown9302c872011-07-13 22:51:29 -0700716 if (windowHandleObjArray) {
717 jsize length = env->GetArrayLength(windowHandleObjArray);
718 for (jsize i = 0; i < length; i++) {
719 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
720 if (! windowHandleObj) {
721 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700722 }
Jeff Brown9302c872011-07-13 22:51:29 -0700723
724 sp<InputWindowHandle> windowHandle =
725 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100726 if (windowHandle != nullptr) {
Jeff Brown9302c872011-07-13 22:51:29 -0700727 windowHandles.push(windowHandle);
728 }
729 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700730 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700731 }
Jeff Brown349703e2010-06-22 01:27:15 -0700732
Arthur Hung39134b22018-08-14 11:58:28 +0800733 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700734
735 // Do this after the dispatcher has updated the window handle state.
736 bool newPointerGesturesEnabled = true;
737 size_t numWindows = windowHandles.size();
738 for (size_t i = 0; i < numWindows; i++) {
739 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700740 const InputWindowInfo* windowInfo = windowHandle->getInfo();
741 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
742 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700743 newPointerGesturesEnabled = false;
744 }
745 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700746
747 uint32_t changes = 0;
748 { // acquire lock
749 AutoMutex _l(mLock);
750
751 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
752 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
753 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
754 }
755 } // release lock
756
757 if (changes) {
758 mInputManager->getReader()->requestRefreshConfiguration(changes);
759 }
Jeff Brown349703e2010-06-22 01:27:15 -0700760}
761
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800762void NativeInputManager::setFocusedApplication(JNIEnv* env, int32_t displayId,
763 jobject applicationHandleObj) {
Jeff Brown9302c872011-07-13 22:51:29 -0700764 sp<InputApplicationHandle> applicationHandle =
765 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800766 mInputManager->getDispatcher()->setFocusedApplication(displayId, applicationHandle);
767}
768
769void NativeInputManager::setFocusedDisplay(JNIEnv* env, int32_t displayId) {
770 mInputManager->getDispatcher()->setFocusedDisplay(displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700771}
772
773void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700774 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700775}
776
Jeff Brown05dc66a2011-03-02 14:41:58 -0800777void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
778 AutoMutex _l(mLock);
779
780 if (mLocked.systemUiVisibility != visibility) {
781 mLocked.systemUiVisibility = visibility;
782
783 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100784 if (controller != nullptr) {
Jeff Brown2352b972011-04-12 22:39:53 -0700785 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800786 }
787 }
788}
789
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100790void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller)
791 REQUIRES(mLock) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800792 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700793 controller->setInactivityTimeout(lightsOut
794 ? PointerController::INACTIVITY_TIMEOUT_SHORT
795 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800796}
797
Jeff Brown1a84fd12011-06-02 01:26:32 -0700798void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700799 { // acquire lock
800 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700801
Jeff Brown474dcb52011-06-14 20:22:50 -0700802 if (mLocked.pointerSpeed == speed) {
803 return;
804 }
805
Steve Block6215d3f2012-01-04 20:05:49 +0000806 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700807 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700808 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700809
Jeff Brown474dcb52011-06-14 20:22:50 -0700810 mInputManager->getReader()->requestRefreshConfiguration(
811 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700812}
813
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700814void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
815 { // acquire lock
816 AutoMutex _l(mLock);
817
818 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
819 bool currentlyEnabled = index < 0;
820 if (!enabled && currentlyEnabled) {
821 mLocked.disabledInputDevices.add(deviceId);
822 }
823 if (enabled && !currentlyEnabled) {
824 mLocked.disabledInputDevices.remove(deviceId);
825 }
826 } // release lock
827
828 mInputManager->getReader()->requestRefreshConfiguration(
829 InputReaderConfiguration::CHANGE_ENABLED_STATE);
830}
831
Jeff Browndaf4a122011-08-26 17:14:14 -0700832void NativeInputManager::setShowTouches(bool enabled) {
833 { // acquire lock
834 AutoMutex _l(mLock);
835
836 if (mLocked.showTouches == enabled) {
837 return;
838 }
839
Steve Block6215d3f2012-01-04 20:05:49 +0000840 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700841 mLocked.showTouches = enabled;
842 } // release lock
843
844 mInputManager->getReader()->requestRefreshConfiguration(
845 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
846}
847
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800848void NativeInputManager::setPointerCapture(bool enabled) {
849 { // acquire lock
850 AutoMutex _l(mLock);
851
852 if (mLocked.pointerCapture == enabled) {
853 return;
854 }
855
856 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
857 mLocked.pointerCapture = enabled;
858 } // release lock
859
860 mInputManager->getReader()->requestRefreshConfiguration(
861 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
862}
863
Jeff Brown037c33e2014-04-09 00:31:55 -0700864void NativeInputManager::setInteractive(bool interactive) {
865 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700866}
867
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800868void NativeInputManager::reloadCalibration() {
869 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100870 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800871}
872
Michael Wrighte051f6f2016-05-13 17:44:16 +0100873void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800874 AutoMutex _l(mLock);
875 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100876 if (controller != nullptr) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100877 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800878 }
879}
880
881void NativeInputManager::reloadPointerIcons() {
882 AutoMutex _l(mLock);
883 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100884 if (controller != nullptr) {
Jun Mukai19a56012015-11-24 11:25:52 -0800885 controller->reloadPointerResources();
886 }
Jun Mukai1db53972015-09-11 18:08:31 -0700887}
888
Jun Mukaid4eaef72015-10-30 15:54:33 -0700889void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
890 AutoMutex _l(mLock);
891 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100892 if (controller != nullptr) {
Jun Mukaid4eaef72015-10-30 15:54:33 -0700893 controller->setCustomPointerIcon(icon);
894 }
895}
896
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800897TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
898 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100899 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800900 ScopedFloatArrayRO matrix(env, matrixArr);
901 assert(matrix.size() == 6);
902
903 TouchAffineTransformation transform;
904 transform.x_scale = matrix[0];
905 transform.x_ymix = matrix[1];
906 transform.x_offset = matrix[2];
907 transform.y_xmix = matrix[3];
908 transform.y_scale = matrix[4];
909 transform.y_offset = matrix[5];
910
911 return transform;
912}
913
914TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100915 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800916 JNIEnv* env = jniEnv();
917
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100918 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.c_str()));
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800919
920 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700921 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
922 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800923
924 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
925 gTouchCalibrationClassInfo.getAffineTransform));
926
927 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
928
929 env->DeleteLocalRef(matrixArr);
930 env->DeleteLocalRef(cal);
931
932 return transform;
933}
934
Jeff Brown0029c662011-03-30 02:25:18 -0700935bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100936 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700937 jobject inputEventObj;
938
939 JNIEnv* env = jniEnv();
940 switch (inputEvent->getType()) {
941 case AINPUT_EVENT_TYPE_KEY:
942 inputEventObj = android_view_KeyEvent_fromNative(env,
943 static_cast<const KeyEvent*>(inputEvent));
944 break;
945 case AINPUT_EVENT_TYPE_MOTION:
946 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
947 static_cast<const MotionEvent*>(inputEvent));
948 break;
949 default:
950 return true; // dispatch the event normally
951 }
952
953 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000954 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700955 return true; // dispatch the event normally
956 }
957
958 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700959 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700960 inputEventObj, policyFlags);
961 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
962 pass = true;
963 }
964 env->DeleteLocalRef(inputEventObj);
965 return pass;
966}
967
Jeff Brown1f245102010-11-18 20:53:46 -0800968void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
969 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100970 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700971 // Policy:
972 // - Ignore untrusted events and pass them along.
973 // - Ask the window manager what to do with normal events and trusted injected events.
974 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100975 bool interactive = mInteractive.load();
976 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700977 policyFlags |= POLICY_FLAG_INTERACTIVE;
978 }
Jeff Brown3122e442010-10-11 23:32:49 -0700979 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800980 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700981 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800982 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
983 jint wmActions;
984 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700985 wmActions = env->CallIntMethod(mServiceObj,
986 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700987 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800988 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
989 wmActions = 0;
990 }
991 android_view_KeyEvent_recycle(env, keyEventObj);
992 env->DeleteLocalRef(keyEventObj);
993 } else {
Steve Block3762c312012-01-06 19:20:56 +0000994 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700995 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700996 }
997
Jeff Brown56194eb2011-03-02 19:23:13 -0800998 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700999 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001000 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001001 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1002 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001003 }
1004}
1005
Jeff Brown56194eb2011-03-02 19:23:13 -08001006void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001007 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001008 // Policy:
1009 // - Ignore untrusted events and pass them along.
1010 // - No special filtering for injected events required at this time.
1011 // - Filter normal events based on screen state.
1012 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001013 bool interactive = mInteractive.load();
1014 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001015 policyFlags |= POLICY_FLAG_INTERACTIVE;
1016 }
Jeff Brown3122e442010-10-11 23:32:49 -07001017 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001018 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001019 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001020 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001021 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001022 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001023 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001024 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001025 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001026 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001027 wmActions = 0;
1028 }
1029
Jeff Brown56194eb2011-03-02 19:23:13 -08001030 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001031 }
Jeff Brown3122e442010-10-11 23:32:49 -07001032 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001033 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001034 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1035 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001036 }
1037}
1038
Jeff Brown56194eb2011-03-02 19:23:13 -08001039void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1040 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001041 if (wmActions & WM_ACTION_PASS_TO_USER) {
1042 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1043 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001044#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001045 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001046#endif
1047 }
1048}
1049
Jeff Brown905805a2011-10-12 13:57:59 -07001050nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Robert Carre0a353c2018-08-02 16:38:04 -07001051 const sp<IBinder>& token,
Jeff Browne20c9e02010-10-11 14:20:19 -07001052 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001053 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001054 // Policy:
1055 // - Ignore untrusted events and pass them along.
1056 // - Filter normal events and trusted injected events through the window manager policy to
1057 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001058 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001059 if (policyFlags & POLICY_FLAG_TRUSTED) {
1060 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001061
Robert Carre0a353c2018-08-02 16:38:04 -07001062 // Token may be null
1063 jobject tokenObj = javaObjectForIBinder(env, token);
1064
Jeff Brown1f245102010-11-18 20:53:46 -08001065 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1066 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001067 jlong delayMillis = env->CallLongMethod(mServiceObj,
1068 gServiceClassInfo.interceptKeyBeforeDispatching,
Robert Carre0a353c2018-08-02 16:38:04 -07001069 tokenObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001070 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1071 android_view_KeyEvent_recycle(env, keyEventObj);
1072 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001073 if (!error) {
1074 if (delayMillis < 0) {
1075 result = -1;
1076 } else if (delayMillis > 0) {
1077 result = milliseconds_to_nanoseconds(delayMillis);
1078 }
1079 }
Jeff Brown1f245102010-11-18 20:53:46 -08001080 } else {
Steve Block3762c312012-01-06 19:20:56 +00001081 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001082 }
Jeff Brown3122e442010-10-11 23:32:49 -07001083 }
Jeff Brown1f245102010-11-18 20:53:46 -08001084 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001085}
1086
Robert Carre0a353c2018-08-02 16:38:04 -07001087bool NativeInputManager::dispatchUnhandledKey(const sp<IBinder>& token,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001088 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001089 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001090 // Policy:
1091 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001092 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001093 if (policyFlags & POLICY_FLAG_TRUSTED) {
1094 JNIEnv* env = jniEnv();
1095
Robert Carre0a353c2018-08-02 16:38:04 -07001096 // Note: tokenObj may be null.
1097 jobject tokenObj = javaObjectForIBinder(env, token);
Jeff Brown1f245102010-11-18 20:53:46 -08001098 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1099 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001100 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1101 gServiceClassInfo.dispatchUnhandledKey,
Robert Carre0a353c2018-08-02 16:38:04 -07001102 tokenObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001103 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001104 fallbackKeyEventObj = nullptr;
Jeff Brownda3d5a92011-03-29 15:11:34 -07001105 }
Jeff Brown1f245102010-11-18 20:53:46 -08001106 android_view_KeyEvent_recycle(env, keyEventObj);
1107 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001108
1109 if (fallbackKeyEventObj) {
1110 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1111 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1112 outFallbackKeyEvent)) {
1113 result = true;
1114 }
1115 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1116 env->DeleteLocalRef(fallbackKeyEventObj);
1117 }
Jeff Brown1f245102010-11-18 20:53:46 -08001118 } else {
Steve Block3762c312012-01-06 19:20:56 +00001119 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001120 }
Jeff Brown3915bb82010-11-05 15:02:16 -07001121 }
Jeff Brown1f245102010-11-18 20:53:46 -08001122 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001123}
1124
Jeff Brown01ce2e92010-09-26 22:20:12 -07001125void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001126 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001127 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001128}
1129
Jeff Brown349703e2010-06-22 01:27:15 -07001130
Jeff Brownb88102f2010-09-08 11:49:43 -07001131bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1132 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001133 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001134 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001135 jboolean result = env->CallBooleanMethod(mServiceObj,
1136 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001137 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1138 result = false;
1139 }
Jeff Brown349703e2010-06-22 01:27:15 -07001140 return result;
1141}
1142
Jun Mukai19a56012015-11-24 11:25:52 -08001143void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001144 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001145 JNIEnv* env = jniEnv();
1146
1147 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1148 mServiceObj, gServiceClassInfo.getPointerIcon));
1149 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1150 return;
1151 }
1152
1153 PointerIcon pointerIcon;
1154 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1155 mContextObj, &pointerIcon);
1156 if (!status && !pointerIcon.isNullIcon()) {
1157 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1158 } else {
1159 *icon = SpriteIcon();
1160 }
1161}
1162
Jeff Brown2352b972011-04-12 22:39:53 -07001163void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001164 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001165 JNIEnv* env = jniEnv();
1166
1167 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1168 &outResources->spotHover);
1169 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1170 &outResources->spotTouch);
1171 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1172 &outResources->spotAnchor);
1173}
1174
Jun Mukai808196f2015-10-28 16:46:44 -07001175void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1176 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001177 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001178 JNIEnv* env = jniEnv();
1179
1180 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1181 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001182 PointerIcon pointerIcon;
1183 loadSystemIconAsSpriteWithPointerIcon(
1184 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1185 if (!pointerIcon.bitmapFrames.empty()) {
1186 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1187 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1188 animationData.durationPerFrame =
1189 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1190 animationData.animationFrames.reserve(numFrames);
1191 animationData.animationFrames.push_back(SpriteIcon(
1192 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1193 for (size_t i = 0; i < numFrames - 1; ++i) {
1194 animationData.animationFrames.push_back(SpriteIcon(
1195 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1196 }
1197 }
Jun Mukai1db53972015-09-11 18:08:31 -07001198 }
Jun Mukai808196f2015-10-28 16:46:44 -07001199 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1200 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001201}
1202
Jun Mukai5ec74202015-10-07 16:58:09 +09001203int32_t NativeInputManager::getDefaultPointerIconId() {
1204 return POINTER_ICON_STYLE_ARROW;
1205}
Jeff Brown83c09682010-12-23 17:50:18 -08001206
Jun Mukaid4eaef72015-10-30 15:54:33 -07001207int32_t NativeInputManager::getCustomPointerIconId() {
1208 return POINTER_ICON_STYLE_CUSTOM;
1209}
1210
Jeff Brown9c3cda02010-06-15 01:31:58 -07001211// ----------------------------------------------------------------------------
1212
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001213static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001214 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001215 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001216 if (messageQueue == nullptr) {
Jeff Brown864693462013-01-28 14:25:53 -08001217 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1218 return 0;
1219 }
1220
Jeff Brown603b4452012-04-06 17:39:41 -07001221 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1222 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001223 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001224 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001225}
1226
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001227static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001228 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001229
Jeff Brown4532e612012-04-05 14:27:12 -07001230 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001231 if (result) {
1232 jniThrowRuntimeException(env, "Input manager could not be started.");
1233 }
1234}
1235
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001236static void nativeSetDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
Santos Cordonee8931e2017-04-05 10:31:15 -07001237 jobjectArray viewportObjArray) {
1238 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001239 im->setDisplayViewports(env, viewportObjArray);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001240}
1241
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001242static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001243 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001244 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001245
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001246 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001247 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001248}
1249
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001250static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001251 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001252 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001253
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001254 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001255 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001256}
1257
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001258static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001259 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001260 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001261
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001262 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001263 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001264}
1265
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001266static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001267 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001268 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001269
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001270 int32_t* codes = env->GetIntArrayElements(keyCodes, nullptr);
1271 uint8_t* flags = env->GetBooleanArrayElements(outFlags, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001272 jsize numCodes = env->GetArrayLength(keyCodes);
1273 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001274 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001275 if (im->getInputManager()->getReader()->hasKeys(
1276 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1277 result = JNI_TRUE;
1278 } else {
1279 result = JNI_FALSE;
1280 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001281 } else {
1282 result = JNI_FALSE;
1283 }
1284
1285 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1286 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1287 return result;
1288}
1289
1290static void throwInputChannelNotInitialized(JNIEnv* env) {
1291 jniThrowException(env, "java/lang/IllegalStateException",
1292 "inputChannel is not initialized");
1293}
1294
Jeff Brown4532e612012-04-05 14:27:12 -07001295static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001296 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001297 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1298
Steve Block8564c8d2012-01-05 23:22:43 +00001299 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001300 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001301 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001302}
1303
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001304static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Robert Carre0a353c2018-08-02 16:38:04 -07001305 jlong ptr, jobject inputChannelObj, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001306 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001307
1308 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1309 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001310 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001311 throwInputChannelNotInitialized(env);
1312 return;
1313 }
Robert Carre0a353c2018-08-02 16:38:04 -07001314 bool monitor = inputChannel->getToken() == nullptr && displayId != ADISPLAY_ID_NONE;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001315
Robert Carre0a353c2018-08-02 16:38:04 -07001316 status_t status = im->registerInputChannel(env, inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001317
Jeff Brown46b9ac02010-04-22 18:58:52 -07001318 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001319 std::string message;
1320 message += StringPrintf("Failed to register input channel. status=%d", status);
1321 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001322 return;
1323 }
1324
Arthur Hungbe5ce212018-09-13 18:41:56 +08001325 // If inputWindowHandle is null and displayId >= 0, treat inputChannel as monitor.
Robert Carre0a353c2018-08-02 16:38:04 -07001326 if (!monitor) {
Jeff Browna41ca772010-08-11 14:46:32 -07001327 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001328 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001329 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001330}
1331
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001332static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001333 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001334 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001335
1336 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1337 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001338 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001339 throwInputChannelNotInitialized(env);
1340 return;
1341 }
1342
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001343 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001344
Jeff Brown4532e612012-04-05 14:27:12 -07001345 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001346 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001347 std::string message;
1348 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1349 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001350 }
1351}
1352
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001353static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001354 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001355 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001356
Jeff Brown4532e612012-04-05 14:27:12 -07001357 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001358}
1359
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001360static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001361 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001362 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001363 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001364
Jeff Brown6ec402b2010-07-28 15:48:59 -07001365 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1366 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001367 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1368 if (status) {
1369 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1370 return INPUT_EVENT_INJECTION_FAILED;
1371 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001372
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001373 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001374 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001375 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001376 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001377 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1378 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001379 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1380 return INPUT_EVENT_INJECTION_FAILED;
1381 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001382
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001383 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001384 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001385 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001386 } else {
1387 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001388 return INPUT_EVENT_INJECTION_FAILED;
1389 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001390}
1391
Andrii Kulian112d0562016-03-08 10:44:22 -08001392static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1393 jlong ptr, jint deviceId) {
1394 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001395
Andrii Kulian112d0562016-03-08 10:44:22 -08001396 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1397}
1398
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001399static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hung39134b22018-08-14 11:58:28 +08001400 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001401 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001402
Arthur Hung39134b22018-08-14 11:58:28 +08001403 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001404}
1405
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001406static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001407 jlong ptr, jint displayId, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001408 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001409
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001410 im->setFocusedApplication(env, displayId, applicationHandleObj);
1411}
1412
1413static void nativeSetFocusedDisplay(JNIEnv* env, jclass /* clazz */,
1414 jlong ptr, jint displayId) {
1415 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1416
1417 im->setFocusedDisplay(env, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001418}
1419
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001420static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1421 jboolean enabled) {
1422 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001423
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001424 im->setPointerCapture(enabled);
1425}
1426
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001427static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1428 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001429 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001430
Jeff Brown4532e612012-04-05 14:27:12 -07001431 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001432}
1433
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001434static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1435 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001436 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001437
Jeff Brown4532e612012-04-05 14:27:12 -07001438 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001439}
1440
Jeff Brown4532e612012-04-05 14:27:12 -07001441static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001442 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001443 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001444
1445 sp<InputChannel> fromChannel =
1446 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1447 sp<InputChannel> toChannel =
1448 android_view_InputChannel_getInputChannel(env, toChannelObj);
1449
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001450 if (fromChannel == nullptr || toChannel == nullptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001451 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001452 }
1453
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001454 if (im->getInputManager()->getDispatcher()->
1455 transferTouchFocus(fromChannel, toChannel)) {
1456 return JNI_TRUE;
1457 } else {
1458 return JNI_FALSE;
1459 }
Jeff Browne6504122010-09-27 14:52:15 -07001460}
1461
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001462static void nativeSetPointerSpeed(JNIEnv* /* env */,
1463 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001464 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001465
Jeff Brown4532e612012-04-05 14:27:12 -07001466 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001467}
1468
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001469static void nativeSetShowTouches(JNIEnv* /* env */,
1470 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001471 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001472
Jeff Brown4532e612012-04-05 14:27:12 -07001473 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001474}
1475
Jeff Brown037c33e2014-04-09 00:31:55 -07001476static void nativeSetInteractive(JNIEnv* env,
1477 jclass clazz, jlong ptr, jboolean interactive) {
1478 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1479
1480 im->setInteractive(interactive);
1481}
1482
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001483static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1484 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001485
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001486 im->reloadCalibration();
1487}
1488
Jeff Browna47425a2012-04-13 04:09:27 -07001489static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001490 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001491 jint repeat, jint token) {
1492 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1493
1494 size_t patternSize = env->GetArrayLength(patternObj);
1495 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001496 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001497 "which is more than the maximum supported size of %d.",
1498 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1499 return; // limit to reasonable size
1500 }
1501
1502 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001503 patternObj, nullptr));
Jeff Browna47425a2012-04-13 04:09:27 -07001504 nsecs_t pattern[patternSize];
1505 for (size_t i = 0; i < patternSize; i++) {
1506 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001507 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001508 }
1509 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1510
1511 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1512}
1513
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001514static void nativeCancelVibrate(JNIEnv* /* env */,
1515 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001516 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1517
1518 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1519}
1520
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001521static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1522 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001523 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1524
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001525 im->getInputManager()->getReader()->requestRefreshConfiguration(
1526 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1527}
1528
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001529static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1530 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001531 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1532
1533 im->getInputManager()->getReader()->requestRefreshConfiguration(
1534 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001535}
1536
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001537static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001538 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001539
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001540 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001541 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001542 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001543}
1544
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001545static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001546 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001547
Jeff Brown4532e612012-04-05 14:27:12 -07001548 im->getInputManager()->getReader()->monitor();
1549 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001550}
1551
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001552static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1553 jclass /* clazz */, jlong ptr, jint deviceId) {
1554 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1555
1556 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1557}
1558
1559static void nativeEnableInputDevice(JNIEnv* /* env */,
1560 jclass /* clazz */, jlong ptr, jint deviceId) {
1561 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1562
1563 im->setInputDeviceEnabled(deviceId, true);
1564}
1565
1566static void nativeDisableInputDevice(JNIEnv* /* env */,
1567 jclass /* clazz */, jlong ptr, jint deviceId) {
1568 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1569
1570 im->setInputDeviceEnabled(deviceId, false);
1571}
1572
Michael Wrighte051f6f2016-05-13 17:44:16 +01001573static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001574 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001575
Michael Wrighte051f6f2016-05-13 17:44:16 +01001576 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001577}
1578
Jun Mukai19a56012015-11-24 11:25:52 -08001579static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1580 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001581
Jun Mukai19a56012015-11-24 11:25:52 -08001582 im->reloadPointerIcons();
1583}
1584
Jun Mukaid4eaef72015-10-30 15:54:33 -07001585static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1586 jlong ptr, jobject iconObj) {
1587 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1588
1589 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001590 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1591 if (result) {
1592 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1593 return;
1594 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001595
1596 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001597 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1598 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1599 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1600 spriteIcon.bitmap.rowBytes(), 0, 0);
1601 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001602 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1603 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1604 im->setCustomPointerIcon(spriteIcon);
1605}
1606
Jeff Brown9c3cda02010-06-15 01:31:58 -07001607// ----------------------------------------------------------------------------
1608
Daniel Micay76f6a862015-09-19 17:31:01 -04001609static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001610 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001611 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001612 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001613 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001614 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001615 (void*) nativeStart },
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001616 { "nativeSetDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1617 (void*) nativeSetDisplayViewports },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001618 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001619 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001620 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001621 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001622 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001623 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001624 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001625 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001626 { "nativeRegisterInputChannel",
Robert Carre0a353c2018-08-02 16:38:04 -07001627 "(JLandroid/view/InputChannel;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001628 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001629 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001630 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001631 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001632 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001633 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001634 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001635 { "nativeToggleCapsLock", "(JI)V",
1636 (void*) nativeToggleCapsLock },
Robert Carr788f5742018-07-30 17:46:45 -07001637 { "nativeSetInputWindows", "(J[Landroid/view/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001638 (void*) nativeSetInputWindows },
Robert Carr788f5742018-07-30 17:46:45 -07001639 { "nativeSetFocusedApplication", "(JILandroid/view/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001640 (void*) nativeSetFocusedApplication },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001641 { "nativeSetFocusedDisplay", "(JI)V",
1642 (void*) nativeSetFocusedDisplay },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001643 { "nativeSetPointerCapture", "(JZ)V",
1644 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001645 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001646 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001647 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001648 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001649 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001650 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001651 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001652 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001653 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001654 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001655 { "nativeSetInteractive", "(JZ)V",
1656 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001657 { "nativeReloadCalibration", "(J)V",
1658 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001659 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001660 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001661 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001662 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001663 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001664 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001665 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001666 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001667 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001668 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001669 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001670 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001671 { "nativeIsInputDeviceEnabled", "(JI)Z",
1672 (void*) nativeIsInputDeviceEnabled },
1673 { "nativeEnableInputDevice", "(JI)V",
1674 (void*) nativeEnableInputDevice },
1675 { "nativeDisableInputDevice", "(JI)V",
1676 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001677 { "nativeSetPointerIconType", "(JI)V",
1678 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001679 { "nativeReloadPointerIcons", "(J)V",
1680 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001681 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1682 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001683};
1684
1685#define FIND_CLASS(var, className) \
1686 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001687 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001688
1689#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1690 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001691 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001692
1693#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1694 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001695 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001696
1697int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001698 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001699 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001700 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001701 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1702
Jeff Brown9c3cda02010-06-15 01:31:58 -07001703 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001704
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001705 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001706 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001707
Jeff Brown4532e612012-04-05 14:27:12 -07001708 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001709 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001710
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001711 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1712 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1713
Jeff Brown53384282012-08-20 20:16:01 -07001714 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1715 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001716
Jeff Brown4532e612012-04-05 14:27:12 -07001717 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
Robert Carre0a353c2018-08-02 16:38:04 -07001718 "notifyInputChannelBroken", "(Landroid/os/IBinder;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001719
Jeff Brown4532e612012-04-05 14:27:12 -07001720 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001721 "notifyANR",
Robert Carre0a353c2018-08-02 16:38:04 -07001722 "(Landroid/view/InputApplicationHandle;Landroid/os/IBinder;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001723
Jeff Brown4532e612012-04-05 14:27:12 -07001724 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001725 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1726
Jeff Brown4532e612012-04-05 14:27:12 -07001727 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001728 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001729
Michael Wright70af00a2014-09-03 19:30:20 -07001730 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1731 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001732
Jeff Brown4532e612012-04-05 14:27:12 -07001733 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001734 "interceptKeyBeforeDispatching",
Robert Carre0a353c2018-08-02 16:38:04 -07001735 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001736
Jeff Brown4532e612012-04-05 14:27:12 -07001737 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001738 "dispatchUnhandledKey",
Robert Carre0a353c2018-08-02 16:38:04 -07001739 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001740
Jeff Brown4532e612012-04-05 14:27:12 -07001741 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001742 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001743
Jeff Brown4532e612012-04-05 14:27:12 -07001744 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001745 "getVirtualKeyQuietTimeMillis", "()I");
1746
Jeff Brown4532e612012-04-05 14:27:12 -07001747 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001748 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1749
Jeff Brown4532e612012-04-05 14:27:12 -07001750 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001751 "getKeyRepeatTimeout", "()I");
1752
Jeff Brown4532e612012-04-05 14:27:12 -07001753 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001754 "getKeyRepeatDelay", "()I");
1755
Jeff Brown4532e612012-04-05 14:27:12 -07001756 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001757 "getHoverTapTimeout", "()I");
1758
Jeff Brown4532e612012-04-05 14:27:12 -07001759 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001760 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001761
Jeff Brown4532e612012-04-05 14:27:12 -07001762 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001763 "getDoubleTapTimeout", "()I");
1764
Jeff Brown4532e612012-04-05 14:27:12 -07001765 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001766 "getLongPressTimeout", "()I");
1767
Jeff Brown4532e612012-04-05 14:27:12 -07001768 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001769 "getPointerLayer", "()I");
1770
Jeff Brown4532e612012-04-05 14:27:12 -07001771 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001772 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001773
Jeff Brown6ec6f792012-04-17 16:52:41 -07001774 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001775 "getKeyboardLayoutOverlay",
1776 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001777
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001778 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1779 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1780
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001781 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1782 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001783 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001784
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001785 // InputDevice
1786
1787 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1788 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1789
Jeff Brown6ec402b2010-07-28 15:48:59 -07001790 // KeyEvent
1791
1792 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001793 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1794
Jeff Brown8d608662010-08-30 03:02:23 -07001795 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001796
1797 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001798 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001799
RoboErikfb290df2013-12-16 11:27:55 -08001800 // InputDeviceIdentifier
1801
1802 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1803 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1804 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1805 "<init>", "(Ljava/lang/String;II)V");
1806
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001807 // TouchCalibration
1808
1809 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1810 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1811
1812 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1813 "getAffineTransform", "()[F");
1814
Jeff Brown46b9ac02010-04-22 18:58:52 -07001815 return 0;
1816}
1817
Jeff Brown46b9ac02010-04-22 18:58:52 -07001818} /* namespace android */