blob: ee8a08b2fbdbe9f094f3e9cc2248b71c3814596f [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
Prabir Pradhane5696a52018-11-14 19:55:21 -0800344 mInputManager = new InputManager(this, this);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700345}
346
347NativeInputManager::~NativeInputManager() {
348 JNIEnv* env = jniEnv();
349
Jeff Brown2352b972011-04-12 22:39:53 -0700350 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700351 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700352}
353
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800354void NativeInputManager::dump(std::string& dump) {
355 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100356 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800357 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100358 }
359 {
360 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800361 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100362 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800363 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
364 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100365 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800366 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
367 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100368 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800369 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100370
Jeff Brownb88102f2010-09-08 11:49:43 -0700371 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800372 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700373
Jeff Brownb88102f2010-09-08 11:49:43 -0700374 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800375 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700376}
377
Jeff Brown7fbdc842010-06-17 20:52:56 -0700378bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700379 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000380 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700381 LOGE_EX(env);
382 env->ExceptionClear();
383 return true;
384 }
385 return false;
386}
387
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100388static const DisplayViewport* findInternalViewport(const std::vector<DisplayViewport>& viewports) {
389 for (const DisplayViewport& v : viewports) {
390 if (v.type == ViewportType::VIEWPORT_INTERNAL) {
391 return &v;
392 }
393 }
394 return nullptr;
395}
396
397void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
398 std::vector<DisplayViewport> viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700399
400 if (viewportObjArray) {
401 jsize length = env->GetArrayLength(viewportObjArray);
402 for (jsize i = 0; i < length; i++) {
403 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
404 if (! viewportObj) {
405 break; // found null element indicating end of used portion of the array
406 }
407
408 DisplayViewport viewport;
409 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100410 ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
411 viewports.push_back(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -0700412
413 env->DeleteLocalRef(viewportObj);
414 }
415 }
416
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100417 const DisplayViewport* newInternalViewport = findInternalViewport(viewports);
Santos Cordonee8931e2017-04-05 10:31:15 -0700418 {
419 AutoMutex _l(mLock);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100420 const DisplayViewport* oldInternalViewport = findInternalViewport(mLocked.viewports);
421 // Internal viewport has changed if there wasn't one earlier, and there is one now, or,
422 // if they are different.
423 const bool internalViewportChanged = (newInternalViewport != nullptr) &&
Siarhei Vishniakoue5bf8662018-10-04 09:33:24 -0700424 (oldInternalViewport == nullptr || (*oldInternalViewport != *newInternalViewport));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100425 if (internalViewportChanged) {
426 sp<PointerController> controller = mLocked.pointerController.promote();
427 updatePointerControllerFromViewport(controller, newInternalViewport);
428 }
429 mLocked.viewports = viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700430 }
431
432 mInputManager->getReader()->requestRefreshConfiguration(
433 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
434}
435
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700436status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Robert Carre0a353c2018-08-02 16:38:04 -0700437 const sp<InputChannel>& inputChannel, int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100438 ATRACE_CALL();
Robert Carre0a353c2018-08-02 16:38:04 -0700439 return mInputManager->getDispatcher()->registerInputChannel(
440 inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700441}
442
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700443status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700444 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100445 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700446 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700447}
448
Jeff Brown214eaf42011-05-26 19:17:02 -0700449void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100450 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700451 JNIEnv* env = jniEnv();
452
Jeff Brown4532e612012-04-05 14:27:12 -0700453 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
454 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700455 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
456 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
457 }
458
459 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700460 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
461 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700462 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
463 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700464 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700465 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100466 const char* deviceNameChars = env->GetStringUTFChars(item, nullptr);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100467 outConfig->excludedDeviceNames.push_back(deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700468 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700469 env->DeleteLocalRef(item);
470 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700471 env->DeleteLocalRef(excludedDeviceNames);
472 }
473
Jeff Brown4532e612012-04-05 14:27:12 -0700474 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
475 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700476 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700477 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
478 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700479 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700480 jint longPressTimeout = env->CallIntMethod(mServiceObj,
481 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700482 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700483 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700484
485 // We must ensure that the tap-drag interval is significantly shorter than
486 // the long-press timeout because the tap is held down for the entire duration
487 // of the double-tap timeout.
488 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700489 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700490 outConfig->pointerGestureTapDragInterval =
491 milliseconds_to_nanoseconds(tapDragInterval);
492 }
493 }
494 }
495
Jeff Brown4532e612012-04-05 14:27:12 -0700496 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
497 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700498 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
499 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700500 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700501
502 { // acquire lock
503 AutoMutex _l(mLock);
504
505 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
506 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700507 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700508
Jeff Browndaf4a122011-08-26 17:14:14 -0700509 outConfig->showTouches = mLocked.showTouches;
510
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800511 outConfig->pointerCapture = mLocked.pointerCapture;
512
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100513 outConfig->setDisplayViewports(mLocked.viewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700514
515 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700516 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700517}
518
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700519sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100520 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800521 AutoMutex _l(mLock);
522
523 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100524 if (controller == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700525 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800526
Jeff Brown2352b972011-04-12 22:39:53 -0700527 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800528 mLocked.pointerController = controller;
529
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100530 const DisplayViewport* internalViewport = findInternalViewport(mLocked.viewports);
531 updatePointerControllerFromViewport(controller, internalViewport);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800532
Jeff Brown2352b972011-04-12 22:39:53 -0700533 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800534 }
535 return controller;
536}
537
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100538void NativeInputManager::ensureSpriteControllerLocked() REQUIRES(mLock) {
539 if (mLocked.spriteController == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700540 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700541 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700542 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
543 layer = -1;
544 }
545 mLocked.spriteController = new SpriteController(mLooper, layer);
546 }
547}
548
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700549void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100550 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700551 JNIEnv* env = jniEnv();
552
553 size_t count = inputDevices.size();
554 jobjectArray inputDevicesObjArray = env->NewObjectArray(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100555 count, gInputDeviceClassInfo.clazz, nullptr);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700556 if (inputDevicesObjArray) {
557 bool error = false;
558 for (size_t i = 0; i < count; i++) {
559 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
560 if (!inputDeviceObj) {
561 error = true;
562 break;
563 }
564
565 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
566 env->DeleteLocalRef(inputDeviceObj);
567 }
568
569 if (!error) {
570 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
571 inputDevicesObjArray);
572 }
573
574 env->DeleteLocalRef(inputDevicesObjArray);
575 }
576
577 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
578}
579
Jeff Brown6ec6f792012-04-17 16:52:41 -0700580sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800581 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100582 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700583 JNIEnv* env = jniEnv();
584
585 sp<KeyCharacterMap> result;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100586 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
RoboErikfb290df2013-12-16 11:27:55 -0800587 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
588 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
589 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700590 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800591 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700592 if (arrayObj.get()) {
593 ScopedLocalRef<jstring> filenameObj(env,
594 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
595 ScopedLocalRef<jstring> contentsObj(env,
596 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
597 ScopedUtfChars filenameChars(env, filenameObj.get());
598 ScopedUtfChars contentsChars(env, contentsObj.get());
599
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100600 KeyCharacterMap::loadContents(filenameChars.c_str(),
601 contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700602 }
603 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
604 return result;
605}
606
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100607std::string NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100608 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700609 JNIEnv* env = jniEnv();
610
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100611 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.c_str()));
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700612 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
613 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100614 std::string result;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700615 if (aliasObj.get()) {
616 ScopedUtfChars aliasChars(env, aliasObj.get());
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100617 result = aliasChars.c_str();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700618 }
619 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
620 return result;
621}
622
Jeff Brownbcc046a2012-09-27 20:46:43 -0700623void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700624 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700625#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700626 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
627 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700628#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100629 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700630
631 JNIEnv* env = jniEnv();
632
Jeff Brown53384282012-08-20 20:16:01 -0700633 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700634 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700635 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700636}
637
Jeff Brown9c3cda02010-06-15 01:31:58 -0700638void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
639#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000640 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700641#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100642 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700643
644 JNIEnv* env = jniEnv();
645
Jeff Brown4532e612012-04-05 14:27:12 -0700646 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700647 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700648}
649
Jeff Brown519e0242010-09-15 15:18:56 -0700650nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carre0a353c2018-08-02 16:38:04 -0700651 const sp<IBinder>& token, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700652#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000653 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700654#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100655 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700656
657 JNIEnv* env = jniEnv();
658
Jeff Brown928e0542011-01-10 11:17:36 -0800659 jobject inputApplicationHandleObj =
660 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
Robert Carre0a353c2018-08-02 16:38:04 -0700661 jobject tokenObj = javaObjectForIBinder(env, token);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800662 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700663
Jeff Brown4532e612012-04-05 14:27:12 -0700664 jlong newTimeout = env->CallLongMethod(mServiceObj,
Robert Carre0a353c2018-08-02 16:38:04 -0700665 gServiceClassInfo.notifyANR, inputApplicationHandleObj, tokenObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700666 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700667 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
668 newTimeout = 0; // abort dispatch
669 } else {
670 assert(newTimeout >= 0);
671 }
672
Jeff Brownbd181bb2013-09-10 16:44:24 -0700673 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800674 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700675 return newTimeout;
676}
677
Robert Carre0a353c2018-08-02 16:38:04 -0700678void NativeInputManager::notifyInputChannelBroken(const sp<IBinder>& token) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700679#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000680 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700681#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100682 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700683
Jeff Brown7fbdc842010-06-17 20:52:56 -0700684 JNIEnv* env = jniEnv();
685
Robert Carre0a353c2018-08-02 16:38:04 -0700686 jobject tokenObj = javaObjectForIBinder(env, token);
687 if (tokenObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700688 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Robert Carre0a353c2018-08-02 16:38:04 -0700689 tokenObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700690 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700691 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700692}
693
Jeff Brown214eaf42011-05-26 19:17:02 -0700694void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100695 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700696 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800697
Jeff Brown4532e612012-04-05 14:27:12 -0700698 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
699 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700700 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
701 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
702 }
Jeff Browna4547672011-03-02 21:38:11 -0800703
Jeff Brown4532e612012-04-05 14:27:12 -0700704 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
705 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700706 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
707 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
708 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700709}
710
Arthur Hung39134b22018-08-14 11:58:28 +0800711void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
712 int32_t displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700713 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700714
Jeff Brown9302c872011-07-13 22:51:29 -0700715 if (windowHandleObjArray) {
716 jsize length = env->GetArrayLength(windowHandleObjArray);
717 for (jsize i = 0; i < length; i++) {
718 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
719 if (! windowHandleObj) {
720 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700721 }
Jeff Brown9302c872011-07-13 22:51:29 -0700722
723 sp<InputWindowHandle> windowHandle =
724 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100725 if (windowHandle != nullptr) {
Jeff Brown9302c872011-07-13 22:51:29 -0700726 windowHandles.push(windowHandle);
727 }
728 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700729 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700730 }
Jeff Brown349703e2010-06-22 01:27:15 -0700731
Arthur Hung39134b22018-08-14 11:58:28 +0800732 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700733
734 // Do this after the dispatcher has updated the window handle state.
735 bool newPointerGesturesEnabled = true;
736 size_t numWindows = windowHandles.size();
737 for (size_t i = 0; i < numWindows; i++) {
738 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700739 const InputWindowInfo* windowInfo = windowHandle->getInfo();
740 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
741 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700742 newPointerGesturesEnabled = false;
743 }
744 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700745
746 uint32_t changes = 0;
747 { // acquire lock
748 AutoMutex _l(mLock);
749
750 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
751 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
752 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
753 }
754 } // release lock
755
756 if (changes) {
757 mInputManager->getReader()->requestRefreshConfiguration(changes);
758 }
Jeff Brown349703e2010-06-22 01:27:15 -0700759}
760
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800761void NativeInputManager::setFocusedApplication(JNIEnv* env, int32_t displayId,
762 jobject applicationHandleObj) {
Jeff Brown9302c872011-07-13 22:51:29 -0700763 sp<InputApplicationHandle> applicationHandle =
764 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800765 mInputManager->getDispatcher()->setFocusedApplication(displayId, applicationHandle);
766}
767
768void NativeInputManager::setFocusedDisplay(JNIEnv* env, int32_t displayId) {
769 mInputManager->getDispatcher()->setFocusedDisplay(displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700770}
771
772void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700773 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700774}
775
Jeff Brown05dc66a2011-03-02 14:41:58 -0800776void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
777 AutoMutex _l(mLock);
778
779 if (mLocked.systemUiVisibility != visibility) {
780 mLocked.systemUiVisibility = visibility;
781
782 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100783 if (controller != nullptr) {
Jeff Brown2352b972011-04-12 22:39:53 -0700784 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800785 }
786 }
787}
788
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100789void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller)
790 REQUIRES(mLock) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800791 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700792 controller->setInactivityTimeout(lightsOut
793 ? PointerController::INACTIVITY_TIMEOUT_SHORT
794 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800795}
796
Jeff Brown1a84fd12011-06-02 01:26:32 -0700797void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700798 { // acquire lock
799 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700800
Jeff Brown474dcb52011-06-14 20:22:50 -0700801 if (mLocked.pointerSpeed == speed) {
802 return;
803 }
804
Steve Block6215d3f2012-01-04 20:05:49 +0000805 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700806 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700807 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700808
Jeff Brown474dcb52011-06-14 20:22:50 -0700809 mInputManager->getReader()->requestRefreshConfiguration(
810 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700811}
812
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700813void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
814 { // acquire lock
815 AutoMutex _l(mLock);
816
817 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
818 bool currentlyEnabled = index < 0;
819 if (!enabled && currentlyEnabled) {
820 mLocked.disabledInputDevices.add(deviceId);
821 }
822 if (enabled && !currentlyEnabled) {
823 mLocked.disabledInputDevices.remove(deviceId);
824 }
825 } // release lock
826
827 mInputManager->getReader()->requestRefreshConfiguration(
828 InputReaderConfiguration::CHANGE_ENABLED_STATE);
829}
830
Jeff Browndaf4a122011-08-26 17:14:14 -0700831void NativeInputManager::setShowTouches(bool enabled) {
832 { // acquire lock
833 AutoMutex _l(mLock);
834
835 if (mLocked.showTouches == enabled) {
836 return;
837 }
838
Steve Block6215d3f2012-01-04 20:05:49 +0000839 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700840 mLocked.showTouches = enabled;
841 } // release lock
842
843 mInputManager->getReader()->requestRefreshConfiguration(
844 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
845}
846
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800847void NativeInputManager::setPointerCapture(bool enabled) {
848 { // acquire lock
849 AutoMutex _l(mLock);
850
851 if (mLocked.pointerCapture == enabled) {
852 return;
853 }
854
855 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
856 mLocked.pointerCapture = enabled;
857 } // release lock
858
859 mInputManager->getReader()->requestRefreshConfiguration(
860 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
861}
862
Jeff Brown037c33e2014-04-09 00:31:55 -0700863void NativeInputManager::setInteractive(bool interactive) {
864 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700865}
866
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800867void NativeInputManager::reloadCalibration() {
868 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100869 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800870}
871
Michael Wrighte051f6f2016-05-13 17:44:16 +0100872void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800873 AutoMutex _l(mLock);
874 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100875 if (controller != nullptr) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100876 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800877 }
878}
879
880void NativeInputManager::reloadPointerIcons() {
881 AutoMutex _l(mLock);
882 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100883 if (controller != nullptr) {
Jun Mukai19a56012015-11-24 11:25:52 -0800884 controller->reloadPointerResources();
885 }
Jun Mukai1db53972015-09-11 18:08:31 -0700886}
887
Jun Mukaid4eaef72015-10-30 15:54:33 -0700888void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
889 AutoMutex _l(mLock);
890 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100891 if (controller != nullptr) {
Jun Mukaid4eaef72015-10-30 15:54:33 -0700892 controller->setCustomPointerIcon(icon);
893 }
894}
895
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800896TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
897 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100898 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800899 ScopedFloatArrayRO matrix(env, matrixArr);
900 assert(matrix.size() == 6);
901
902 TouchAffineTransformation transform;
903 transform.x_scale = matrix[0];
904 transform.x_ymix = matrix[1];
905 transform.x_offset = matrix[2];
906 transform.y_xmix = matrix[3];
907 transform.y_scale = matrix[4];
908 transform.y_offset = matrix[5];
909
910 return transform;
911}
912
913TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100914 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800915 JNIEnv* env = jniEnv();
916
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100917 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.c_str()));
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800918
919 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700920 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
921 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800922
923 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
924 gTouchCalibrationClassInfo.getAffineTransform));
925
926 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
927
928 env->DeleteLocalRef(matrixArr);
929 env->DeleteLocalRef(cal);
930
931 return transform;
932}
933
Jeff Brown0029c662011-03-30 02:25:18 -0700934bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100935 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700936 jobject inputEventObj;
937
938 JNIEnv* env = jniEnv();
939 switch (inputEvent->getType()) {
940 case AINPUT_EVENT_TYPE_KEY:
941 inputEventObj = android_view_KeyEvent_fromNative(env,
942 static_cast<const KeyEvent*>(inputEvent));
943 break;
944 case AINPUT_EVENT_TYPE_MOTION:
945 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
946 static_cast<const MotionEvent*>(inputEvent));
947 break;
948 default:
949 return true; // dispatch the event normally
950 }
951
952 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000953 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700954 return true; // dispatch the event normally
955 }
956
957 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700958 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700959 inputEventObj, policyFlags);
960 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
961 pass = true;
962 }
963 env->DeleteLocalRef(inputEventObj);
964 return pass;
965}
966
Jeff Brown1f245102010-11-18 20:53:46 -0800967void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
968 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100969 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -0700970 // Policy:
971 // - Ignore untrusted events and pass them along.
972 // - Ask the window manager what to do with normal events and trusted injected events.
973 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100974 bool interactive = mInteractive.load();
975 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700976 policyFlags |= POLICY_FLAG_INTERACTIVE;
977 }
Jeff Brown3122e442010-10-11 23:32:49 -0700978 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800979 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700980 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800981 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
982 jint wmActions;
983 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700984 wmActions = env->CallIntMethod(mServiceObj,
985 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700986 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800987 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
988 wmActions = 0;
989 }
990 android_view_KeyEvent_recycle(env, keyEventObj);
991 env->DeleteLocalRef(keyEventObj);
992 } else {
Steve Block3762c312012-01-06 19:20:56 +0000993 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700994 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700995 }
996
Jeff Brown56194eb2011-03-02 19:23:13 -0800997 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700998 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100999 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001000 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1001 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001002 }
1003}
1004
Jeff Brown56194eb2011-03-02 19:23:13 -08001005void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001006 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001007 // Policy:
1008 // - Ignore untrusted events and pass them along.
1009 // - No special filtering for injected events required at this time.
1010 // - Filter normal events based on screen state.
1011 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001012 bool interactive = mInteractive.load();
1013 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001014 policyFlags |= POLICY_FLAG_INTERACTIVE;
1015 }
Jeff Brown3122e442010-10-11 23:32:49 -07001016 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001017 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001018 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001019 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001020 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001021 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001022 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001023 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001024 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001025 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001026 wmActions = 0;
1027 }
1028
Jeff Brown56194eb2011-03-02 19:23:13 -08001029 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001030 }
Jeff Brown3122e442010-10-11 23:32:49 -07001031 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001032 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001033 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1034 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001035 }
1036}
1037
Jeff Brown56194eb2011-03-02 19:23:13 -08001038void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1039 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001040 if (wmActions & WM_ACTION_PASS_TO_USER) {
1041 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1042 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001043#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001044 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001045#endif
1046 }
1047}
1048
Jeff Brown905805a2011-10-12 13:57:59 -07001049nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Robert Carre0a353c2018-08-02 16:38:04 -07001050 const sp<IBinder>& token,
Jeff Browne20c9e02010-10-11 14:20:19 -07001051 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001052 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001053 // Policy:
1054 // - Ignore untrusted events and pass them along.
1055 // - Filter normal events and trusted injected events through the window manager policy to
1056 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001057 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001058 if (policyFlags & POLICY_FLAG_TRUSTED) {
1059 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001060
Robert Carre0a353c2018-08-02 16:38:04 -07001061 // Token may be null
1062 jobject tokenObj = javaObjectForIBinder(env, token);
1063
Jeff Brown1f245102010-11-18 20:53:46 -08001064 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1065 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001066 jlong delayMillis = env->CallLongMethod(mServiceObj,
1067 gServiceClassInfo.interceptKeyBeforeDispatching,
Robert Carre0a353c2018-08-02 16:38:04 -07001068 tokenObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001069 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1070 android_view_KeyEvent_recycle(env, keyEventObj);
1071 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001072 if (!error) {
1073 if (delayMillis < 0) {
1074 result = -1;
1075 } else if (delayMillis > 0) {
1076 result = milliseconds_to_nanoseconds(delayMillis);
1077 }
1078 }
Jeff Brown1f245102010-11-18 20:53:46 -08001079 } else {
Steve Block3762c312012-01-06 19:20:56 +00001080 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001081 }
Jeff Brown3122e442010-10-11 23:32:49 -07001082 }
Jeff Brown1f245102010-11-18 20:53:46 -08001083 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001084}
1085
Robert Carre0a353c2018-08-02 16:38:04 -07001086bool NativeInputManager::dispatchUnhandledKey(const sp<IBinder>& token,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001087 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001088 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001089 // Policy:
1090 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001091 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001092 if (policyFlags & POLICY_FLAG_TRUSTED) {
1093 JNIEnv* env = jniEnv();
1094
Robert Carre0a353c2018-08-02 16:38:04 -07001095 // Note: tokenObj may be null.
1096 jobject tokenObj = javaObjectForIBinder(env, token);
Jeff Brown1f245102010-11-18 20:53:46 -08001097 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1098 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001099 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1100 gServiceClassInfo.dispatchUnhandledKey,
Robert Carre0a353c2018-08-02 16:38:04 -07001101 tokenObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001102 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001103 fallbackKeyEventObj = nullptr;
Jeff Brownda3d5a92011-03-29 15:11:34 -07001104 }
Jeff Brown1f245102010-11-18 20:53:46 -08001105 android_view_KeyEvent_recycle(env, keyEventObj);
1106 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001107
1108 if (fallbackKeyEventObj) {
1109 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1110 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1111 outFallbackKeyEvent)) {
1112 result = true;
1113 }
1114 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1115 env->DeleteLocalRef(fallbackKeyEventObj);
1116 }
Jeff Brown1f245102010-11-18 20:53:46 -08001117 } else {
Steve Block3762c312012-01-06 19:20:56 +00001118 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001119 }
Jeff Brown3915bb82010-11-05 15:02:16 -07001120 }
Jeff Brown1f245102010-11-18 20:53:46 -08001121 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001122}
1123
Jeff Brown01ce2e92010-09-26 22:20:12 -07001124void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001125 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001126 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001127}
1128
Jeff Brown349703e2010-06-22 01:27:15 -07001129
Jeff Brownb88102f2010-09-08 11:49:43 -07001130bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1131 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001132 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001133 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001134 jboolean result = env->CallBooleanMethod(mServiceObj,
1135 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001136 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1137 result = false;
1138 }
Jeff Brown349703e2010-06-22 01:27:15 -07001139 return result;
1140}
1141
Jun Mukai19a56012015-11-24 11:25:52 -08001142void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001143 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001144 JNIEnv* env = jniEnv();
1145
1146 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1147 mServiceObj, gServiceClassInfo.getPointerIcon));
1148 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1149 return;
1150 }
1151
1152 PointerIcon pointerIcon;
1153 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1154 mContextObj, &pointerIcon);
1155 if (!status && !pointerIcon.isNullIcon()) {
1156 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1157 } else {
1158 *icon = SpriteIcon();
1159 }
1160}
1161
Jeff Brown2352b972011-04-12 22:39:53 -07001162void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001163 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001164 JNIEnv* env = jniEnv();
1165
1166 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1167 &outResources->spotHover);
1168 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1169 &outResources->spotTouch);
1170 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1171 &outResources->spotAnchor);
1172}
1173
Jun Mukai808196f2015-10-28 16:46:44 -07001174void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1175 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001176 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001177 JNIEnv* env = jniEnv();
1178
1179 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1180 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001181 PointerIcon pointerIcon;
1182 loadSystemIconAsSpriteWithPointerIcon(
1183 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1184 if (!pointerIcon.bitmapFrames.empty()) {
1185 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1186 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1187 animationData.durationPerFrame =
1188 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1189 animationData.animationFrames.reserve(numFrames);
1190 animationData.animationFrames.push_back(SpriteIcon(
1191 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1192 for (size_t i = 0; i < numFrames - 1; ++i) {
1193 animationData.animationFrames.push_back(SpriteIcon(
1194 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1195 }
1196 }
Jun Mukai1db53972015-09-11 18:08:31 -07001197 }
Jun Mukai808196f2015-10-28 16:46:44 -07001198 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1199 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001200}
1201
Jun Mukai5ec74202015-10-07 16:58:09 +09001202int32_t NativeInputManager::getDefaultPointerIconId() {
1203 return POINTER_ICON_STYLE_ARROW;
1204}
Jeff Brown83c09682010-12-23 17:50:18 -08001205
Jun Mukaid4eaef72015-10-30 15:54:33 -07001206int32_t NativeInputManager::getCustomPointerIconId() {
1207 return POINTER_ICON_STYLE_CUSTOM;
1208}
1209
Jeff Brown9c3cda02010-06-15 01:31:58 -07001210// ----------------------------------------------------------------------------
1211
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001212static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001213 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001214 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001215 if (messageQueue == nullptr) {
Jeff Brown864693462013-01-28 14:25:53 -08001216 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1217 return 0;
1218 }
1219
Jeff Brown603b4452012-04-06 17:39:41 -07001220 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1221 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001222 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001223 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001224}
1225
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001226static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001227 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001228
Jeff Brown4532e612012-04-05 14:27:12 -07001229 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001230 if (result) {
1231 jniThrowRuntimeException(env, "Input manager could not be started.");
1232 }
1233}
1234
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001235static void nativeSetDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
Santos Cordonee8931e2017-04-05 10:31:15 -07001236 jobjectArray viewportObjArray) {
1237 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001238 im->setDisplayViewports(env, viewportObjArray);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001239}
1240
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001241static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001242 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001243 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001244
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001245 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001246 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001247}
1248
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001249static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001250 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001251 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001252
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001253 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001254 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001255}
1256
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001257static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001258 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001259 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001260
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001261 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001262 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001263}
1264
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001265static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001266 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001267 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001268
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001269 int32_t* codes = env->GetIntArrayElements(keyCodes, nullptr);
1270 uint8_t* flags = env->GetBooleanArrayElements(outFlags, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001271 jsize numCodes = env->GetArrayLength(keyCodes);
1272 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001273 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001274 if (im->getInputManager()->getReader()->hasKeys(
1275 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1276 result = JNI_TRUE;
1277 } else {
1278 result = JNI_FALSE;
1279 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001280 } else {
1281 result = JNI_FALSE;
1282 }
1283
1284 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1285 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1286 return result;
1287}
1288
1289static void throwInputChannelNotInitialized(JNIEnv* env) {
1290 jniThrowException(env, "java/lang/IllegalStateException",
1291 "inputChannel is not initialized");
1292}
1293
Jeff Brown4532e612012-04-05 14:27:12 -07001294static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001295 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001296 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1297
Steve Block8564c8d2012-01-05 23:22:43 +00001298 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001299 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001300 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001301}
1302
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001303static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Robert Carre0a353c2018-08-02 16:38:04 -07001304 jlong ptr, jobject inputChannelObj, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001305 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001306
1307 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1308 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001309 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001310 throwInputChannelNotInitialized(env);
1311 return;
1312 }
Robert Carre0a353c2018-08-02 16:38:04 -07001313 bool monitor = inputChannel->getToken() == nullptr && displayId != ADISPLAY_ID_NONE;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001314
Robert Carre0a353c2018-08-02 16:38:04 -07001315 status_t status = im->registerInputChannel(env, inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001316
Jeff Brown46b9ac02010-04-22 18:58:52 -07001317 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001318 std::string message;
1319 message += StringPrintf("Failed to register input channel. status=%d", status);
1320 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001321 return;
1322 }
1323
Arthur Hungbe5ce212018-09-13 18:41:56 +08001324 // If inputWindowHandle is null and displayId >= 0, treat inputChannel as monitor.
Robert Carre0a353c2018-08-02 16:38:04 -07001325 if (!monitor) {
Jeff Browna41ca772010-08-11 14:46:32 -07001326 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001327 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001328 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001329}
1330
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001331static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001332 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001333 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001334
1335 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1336 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001337 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001338 throwInputChannelNotInitialized(env);
1339 return;
1340 }
1341
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001342 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001343
Jeff Brown4532e612012-04-05 14:27:12 -07001344 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001345 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001346 std::string message;
1347 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1348 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001349 }
1350}
1351
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001352static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001353 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001354 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001355
Jeff Brown4532e612012-04-05 14:27:12 -07001356 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001357}
1358
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001359static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001360 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001361 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001362 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001363
Jeff Brown6ec402b2010-07-28 15:48:59 -07001364 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1365 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001366 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1367 if (status) {
1368 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1369 return INPUT_EVENT_INJECTION_FAILED;
1370 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001371
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001372 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001373 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001374 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001375 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001376 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1377 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001378 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1379 return INPUT_EVENT_INJECTION_FAILED;
1380 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001381
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001382 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001383 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001384 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001385 } else {
1386 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001387 return INPUT_EVENT_INJECTION_FAILED;
1388 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001389}
1390
Andrii Kulian112d0562016-03-08 10:44:22 -08001391static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1392 jlong ptr, jint deviceId) {
1393 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001394
Andrii Kulian112d0562016-03-08 10:44:22 -08001395 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1396}
1397
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001398static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hung39134b22018-08-14 11:58:28 +08001399 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001400 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001401
Arthur Hung39134b22018-08-14 11:58:28 +08001402 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001403}
1404
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001405static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001406 jlong ptr, jint displayId, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001407 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001408
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001409 im->setFocusedApplication(env, displayId, applicationHandleObj);
1410}
1411
1412static void nativeSetFocusedDisplay(JNIEnv* env, jclass /* clazz */,
1413 jlong ptr, jint displayId) {
1414 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1415
1416 im->setFocusedDisplay(env, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001417}
1418
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001419static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1420 jboolean enabled) {
1421 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001422
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001423 im->setPointerCapture(enabled);
1424}
1425
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001426static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1427 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001428 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001429
Jeff Brown4532e612012-04-05 14:27:12 -07001430 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001431}
1432
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001433static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1434 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001435 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001436
Jeff Brown4532e612012-04-05 14:27:12 -07001437 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001438}
1439
Jeff Brown4532e612012-04-05 14:27:12 -07001440static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001441 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001442 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001443
1444 sp<InputChannel> fromChannel =
1445 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1446 sp<InputChannel> toChannel =
1447 android_view_InputChannel_getInputChannel(env, toChannelObj);
1448
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001449 if (fromChannel == nullptr || toChannel == nullptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001450 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001451 }
1452
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001453 if (im->getInputManager()->getDispatcher()->
1454 transferTouchFocus(fromChannel, toChannel)) {
1455 return JNI_TRUE;
1456 } else {
1457 return JNI_FALSE;
1458 }
Jeff Browne6504122010-09-27 14:52:15 -07001459}
1460
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001461static void nativeSetPointerSpeed(JNIEnv* /* env */,
1462 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001463 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001464
Jeff Brown4532e612012-04-05 14:27:12 -07001465 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001466}
1467
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001468static void nativeSetShowTouches(JNIEnv* /* env */,
1469 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001470 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001471
Jeff Brown4532e612012-04-05 14:27:12 -07001472 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001473}
1474
Jeff Brown037c33e2014-04-09 00:31:55 -07001475static void nativeSetInteractive(JNIEnv* env,
1476 jclass clazz, jlong ptr, jboolean interactive) {
1477 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1478
1479 im->setInteractive(interactive);
1480}
1481
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001482static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1483 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001484
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001485 im->reloadCalibration();
1486}
1487
Jeff Browna47425a2012-04-13 04:09:27 -07001488static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001489 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001490 jint repeat, jint token) {
1491 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1492
1493 size_t patternSize = env->GetArrayLength(patternObj);
1494 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001495 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001496 "which is more than the maximum supported size of %d.",
1497 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1498 return; // limit to reasonable size
1499 }
1500
1501 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001502 patternObj, nullptr));
Jeff Browna47425a2012-04-13 04:09:27 -07001503 nsecs_t pattern[patternSize];
1504 for (size_t i = 0; i < patternSize; i++) {
1505 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001506 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001507 }
1508 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1509
1510 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1511}
1512
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001513static void nativeCancelVibrate(JNIEnv* /* env */,
1514 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001515 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1516
1517 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1518}
1519
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001520static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1521 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001522 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1523
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001524 im->getInputManager()->getReader()->requestRefreshConfiguration(
1525 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1526}
1527
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001528static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1529 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001530 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1531
1532 im->getInputManager()->getReader()->requestRefreshConfiguration(
1533 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001534}
1535
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001536static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001537 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001538
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001539 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001540 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001541 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001542}
1543
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001544static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001545 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001546
Jeff Brown4532e612012-04-05 14:27:12 -07001547 im->getInputManager()->getReader()->monitor();
1548 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001549}
1550
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001551static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1552 jclass /* clazz */, jlong ptr, jint deviceId) {
1553 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1554
1555 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1556}
1557
1558static void nativeEnableInputDevice(JNIEnv* /* env */,
1559 jclass /* clazz */, jlong ptr, jint deviceId) {
1560 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1561
1562 im->setInputDeviceEnabled(deviceId, true);
1563}
1564
1565static void nativeDisableInputDevice(JNIEnv* /* env */,
1566 jclass /* clazz */, jlong ptr, jint deviceId) {
1567 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1568
1569 im->setInputDeviceEnabled(deviceId, false);
1570}
1571
Michael Wrighte051f6f2016-05-13 17:44:16 +01001572static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001573 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001574
Michael Wrighte051f6f2016-05-13 17:44:16 +01001575 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001576}
1577
Jun Mukai19a56012015-11-24 11:25:52 -08001578static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1579 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001580
Jun Mukai19a56012015-11-24 11:25:52 -08001581 im->reloadPointerIcons();
1582}
1583
Jun Mukaid4eaef72015-10-30 15:54:33 -07001584static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1585 jlong ptr, jobject iconObj) {
1586 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1587
1588 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001589 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1590 if (result) {
1591 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1592 return;
1593 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001594
1595 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001596 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1597 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1598 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1599 spriteIcon.bitmap.rowBytes(), 0, 0);
1600 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001601 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1602 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1603 im->setCustomPointerIcon(spriteIcon);
1604}
1605
Jeff Brown9c3cda02010-06-15 01:31:58 -07001606// ----------------------------------------------------------------------------
1607
Daniel Micay76f6a862015-09-19 17:31:01 -04001608static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001609 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001610 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001611 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001612 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001613 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001614 (void*) nativeStart },
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001615 { "nativeSetDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1616 (void*) nativeSetDisplayViewports },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001617 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001618 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001619 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001620 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001621 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001622 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001623 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001624 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001625 { "nativeRegisterInputChannel",
Robert Carre0a353c2018-08-02 16:38:04 -07001626 "(JLandroid/view/InputChannel;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001627 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001628 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001629 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001630 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001631 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001632 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001633 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001634 { "nativeToggleCapsLock", "(JI)V",
1635 (void*) nativeToggleCapsLock },
Robert Carr788f5742018-07-30 17:46:45 -07001636 { "nativeSetInputWindows", "(J[Landroid/view/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001637 (void*) nativeSetInputWindows },
Robert Carr788f5742018-07-30 17:46:45 -07001638 { "nativeSetFocusedApplication", "(JILandroid/view/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001639 (void*) nativeSetFocusedApplication },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001640 { "nativeSetFocusedDisplay", "(JI)V",
1641 (void*) nativeSetFocusedDisplay },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001642 { "nativeSetPointerCapture", "(JZ)V",
1643 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001644 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001645 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001646 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001647 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001648 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001649 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001650 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001651 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001652 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001653 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001654 { "nativeSetInteractive", "(JZ)V",
1655 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001656 { "nativeReloadCalibration", "(J)V",
1657 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001658 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001659 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001660 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001661 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001662 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001663 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001664 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001665 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001666 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001667 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001668 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001669 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001670 { "nativeIsInputDeviceEnabled", "(JI)Z",
1671 (void*) nativeIsInputDeviceEnabled },
1672 { "nativeEnableInputDevice", "(JI)V",
1673 (void*) nativeEnableInputDevice },
1674 { "nativeDisableInputDevice", "(JI)V",
1675 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001676 { "nativeSetPointerIconType", "(JI)V",
1677 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001678 { "nativeReloadPointerIcons", "(J)V",
1679 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001680 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1681 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001682};
1683
1684#define FIND_CLASS(var, className) \
1685 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001686 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001687
1688#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1689 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001690 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001691
1692#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1693 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001694 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001695
1696int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001697 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001698 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001699 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001700 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1701
Jeff Brown9c3cda02010-06-15 01:31:58 -07001702 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001703
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001704 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001705 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001706
Jeff Brown4532e612012-04-05 14:27:12 -07001707 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001708 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001709
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001710 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1711 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1712
Jeff Brown53384282012-08-20 20:16:01 -07001713 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1714 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001715
Jeff Brown4532e612012-04-05 14:27:12 -07001716 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
Robert Carre0a353c2018-08-02 16:38:04 -07001717 "notifyInputChannelBroken", "(Landroid/os/IBinder;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001718
Jeff Brown4532e612012-04-05 14:27:12 -07001719 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001720 "notifyANR",
Robert Carre0a353c2018-08-02 16:38:04 -07001721 "(Landroid/view/InputApplicationHandle;Landroid/os/IBinder;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001722
Jeff Brown4532e612012-04-05 14:27:12 -07001723 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001724 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1725
Jeff Brown4532e612012-04-05 14:27:12 -07001726 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001727 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001728
Michael Wright70af00a2014-09-03 19:30:20 -07001729 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1730 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001731
Jeff Brown4532e612012-04-05 14:27:12 -07001732 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001733 "interceptKeyBeforeDispatching",
Robert Carre0a353c2018-08-02 16:38:04 -07001734 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001735
Jeff Brown4532e612012-04-05 14:27:12 -07001736 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001737 "dispatchUnhandledKey",
Robert Carre0a353c2018-08-02 16:38:04 -07001738 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001739
Jeff Brown4532e612012-04-05 14:27:12 -07001740 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001741 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001742
Jeff Brown4532e612012-04-05 14:27:12 -07001743 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001744 "getVirtualKeyQuietTimeMillis", "()I");
1745
Jeff Brown4532e612012-04-05 14:27:12 -07001746 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001747 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1748
Jeff Brown4532e612012-04-05 14:27:12 -07001749 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001750 "getKeyRepeatTimeout", "()I");
1751
Jeff Brown4532e612012-04-05 14:27:12 -07001752 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001753 "getKeyRepeatDelay", "()I");
1754
Jeff Brown4532e612012-04-05 14:27:12 -07001755 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001756 "getHoverTapTimeout", "()I");
1757
Jeff Brown4532e612012-04-05 14:27:12 -07001758 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001759 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001760
Jeff Brown4532e612012-04-05 14:27:12 -07001761 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001762 "getDoubleTapTimeout", "()I");
1763
Jeff Brown4532e612012-04-05 14:27:12 -07001764 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001765 "getLongPressTimeout", "()I");
1766
Jeff Brown4532e612012-04-05 14:27:12 -07001767 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001768 "getPointerLayer", "()I");
1769
Jeff Brown4532e612012-04-05 14:27:12 -07001770 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001771 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001772
Jeff Brown6ec6f792012-04-17 16:52:41 -07001773 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001774 "getKeyboardLayoutOverlay",
1775 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001776
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001777 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1778 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1779
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001780 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1781 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001782 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001783
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001784 // InputDevice
1785
1786 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1787 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1788
Jeff Brown6ec402b2010-07-28 15:48:59 -07001789 // KeyEvent
1790
1791 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001792 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1793
Jeff Brown8d608662010-08-30 03:02:23 -07001794 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001795
1796 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001797 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001798
RoboErikfb290df2013-12-16 11:27:55 -08001799 // InputDeviceIdentifier
1800
1801 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1802 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1803 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1804 "<init>", "(Ljava/lang/String;II)V");
1805
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001806 // TouchCalibration
1807
1808 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1809 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1810
1811 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1812 "getAffineTransform", "()[F");
1813
Jeff Brown46b9ac02010-04-22 18:58:52 -07001814 return 0;
1815}
1816
Jeff Brown46b9ac02010-04-22 18:58:52 -07001817} /* namespace android */