blob: 0929e20c9187cca13e375e211432bf34ea5f9fd2 [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 Vishniakou15a412d2018-10-04 19:01:04 -070035#include <android-base/parseint.h>
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080036#include <android-base/stringprintf.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070037#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070038#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039
Jeff Brown46b9ac02010-04-22 18:58:52 -070040#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080041#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070042#include <utils/threads.h>
Michael Wrightbafea6e2017-06-12 15:25:47 +010043#include <utils/Trace.h>
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070044#include <utils/SortedVector.h>
Jeff Brown83c09682010-12-23 17:50:18 -080045
Robert Carr788f5742018-07-30 17:46:45 -070046#include <binder/IServiceManager.h>
47
Jeff Brownb4ff35d2011-01-02 16:37:43 -080048#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070049#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080050
Michael Wrightd6b473712014-02-10 15:56:36 -080051#include <inputflinger/InputManager.h>
52
Jeff Brown05dc66a2011-03-02 14:41:58 -080053#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070054#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080055#include <android_view_KeyEvent.h>
56#include <android_view_MotionEvent.h>
57#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070058#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080059#include <android/graphics/GraphicsJNI.h>
60
Steven Moreland2279b252017-07-19 09:50:45 -070061#include <nativehelper/ScopedLocalRef.h>
62#include <nativehelper/ScopedPrimitiveArray.h>
63#include <nativehelper/ScopedUtfChars.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070064
Jeff Brown4f8ecd82012-06-18 18:29:13 -070065#include "com_android_server_power_PowerManagerService.h"
Robert Carre1db3202018-07-23 15:24:59 -070066#include "android_hardware_input_InputApplicationHandle.h"
67#include "android_hardware_input_InputWindowHandle.h"
Santos Cordonee8931e2017-04-05 10:31:15 -070068#include "android_hardware_display_DisplayViewport.h"
Robert Carre0a353c2018-08-02 16:38:04 -070069#include "android_util_Binder.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070070
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +010071#include <vector>
72
Michael Wrighta4051212015-07-23 17:04:40 +010073#define INDENT " "
74
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -070075using android::base::ParseUint;
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -080076using android::base::StringPrintf;
77
Jeff Brown46b9ac02010-04-22 18:58:52 -070078namespace android {
79
Jeff Brown1a84fd12011-06-02 01:26:32 -070080// The exponent used to calculate the pointer speed scaling factor.
81// The scaling factor is calculated as 2 ^ (speed * exponent),
82// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070083static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070084
Jeff Brown46b9ac02010-04-22 18:58:52 -070085static struct {
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -070086 jclass clazz;
Jeff Brown46b9ac02010-04-22 18:58:52 -070087 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070088 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070089 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070090 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070091 jmethodID notifyANR;
Robert Carrbd26e652018-08-21 15:46:28 -070092 jmethodID notifyFocusChanged;
Jeff Brown0029c662011-03-30 02:25:18 -070093 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070094 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070095 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070096 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070097 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070098 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080099 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700100 jmethodID getExcludedDeviceNames;
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700101 jmethodID getInputPortAssociations;
Jeff Browna4547672011-03-02 21:38:11 -0800102 jmethodID getKeyRepeatTimeout;
103 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700104 jmethodID getHoverTapTimeout;
105 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -0700106 jmethodID getDoubleTapTimeout;
107 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -0800108 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800109 jmethodID getPointerIcon;
Arthur Hungb9b32002018-12-18 17:39:43 +0800110 jmethodID getPointerDisplayId;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700111 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700112 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800113 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -0700114} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700115
116static struct {
117 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700118} gInputDeviceClassInfo;
119
120static struct {
121 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700122} gKeyEventClassInfo;
123
124static struct {
125 jclass clazz;
126} gMotionEventClassInfo;
127
RoboErikfb290df2013-12-16 11:27:55 -0800128static struct {
129 jclass clazz;
130 jmethodID constructor;
131} gInputDeviceIdentifierInfo;
132
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800133static struct {
134 jclass clazz;
135 jmethodID getAffineTransform;
136} gTouchCalibrationClassInfo;
137
RoboErikfb290df2013-12-16 11:27:55 -0800138
Jeff Brown928e0542011-01-10 11:17:36 -0800139
140// --- Global functions ---
141
Jeff Brown214eaf42011-05-26 19:17:02 -0700142template<typename T>
143inline static T min(const T& a, const T& b) {
144 return a < b ? a : b;
145}
146
147template<typename T>
148inline static T max(const T& a, const T& b) {
149 return a > b ? a : b;
150}
151
Michael Wrighta4051212015-07-23 17:04:40 +0100152static inline const char* toString(bool value) {
153 return value ? "true" : "false";
154}
155
Jun Mukai808196f2015-10-28 16:46:44 -0700156static void loadSystemIconAsSpriteWithPointerIcon(JNIEnv* env, jobject contextObj, int32_t style,
157 PointerIcon* outPointerIcon, SpriteIcon* outSpriteIcon) {
Jeff Brown2352b972011-04-12 22:39:53 -0700158 status_t status = android_view_PointerIcon_loadSystemIcon(env,
Jun Mukai808196f2015-10-28 16:46:44 -0700159 contextObj, style, outPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700160 if (!status) {
Matt Sarett155d5212017-04-25 17:32:34 -0400161 SkBitmap* bitmapCopy = &outSpriteIcon->bitmap;
162 SkImageInfo bitmapCopyInfo = outPointerIcon->bitmap.info().makeColorType(kN32_SkColorType);
163 if (bitmapCopy->tryAllocPixels(bitmapCopyInfo)) {
164 outPointerIcon->bitmap.readPixels(bitmapCopy->info(), bitmapCopy->getPixels(),
165 bitmapCopy->rowBytes(), 0, 0);
166 }
Jun Mukai808196f2015-10-28 16:46:44 -0700167 outSpriteIcon->hotSpotX = outPointerIcon->hotSpotX;
168 outSpriteIcon->hotSpotY = outPointerIcon->hotSpotY;
Jeff Brown2352b972011-04-12 22:39:53 -0700169 }
170}
171
Jun Mukai808196f2015-10-28 16:46:44 -0700172static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
173 SpriteIcon* outSpriteIcon) {
174 PointerIcon pointerIcon;
175 loadSystemIconAsSpriteWithPointerIcon(env, contextObj, style, &pointerIcon, outSpriteIcon);
176}
177
Jeff Brown905805a2011-10-12 13:57:59 -0700178enum {
179 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700180};
181
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700182static std::string getStringElementFromJavaArray(JNIEnv* env, jobjectArray array, jsize index) {
183 jstring item = jstring(env->GetObjectArrayElement(array, index));
184 ScopedUtfChars chars(env, item);
185 std::string result(chars.c_str());
186 return result;
187}
188
Jeff Brown928e0542011-01-10 11:17:36 -0800189
190// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800191
Jeff Brown9c3cda02010-06-15 01:31:58 -0700192class NativeInputManager : public virtual RefBase,
193 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700194 public virtual InputDispatcherPolicyInterface,
195 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700196protected:
197 virtual ~NativeInputManager();
198
199public:
Jeff Brown4532e612012-04-05 14:27:12 -0700200 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700201
202 inline sp<InputManager> getInputManager() const { return mInputManager; }
203
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800204 void dump(std::string& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700205
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100206 void setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700207
Robert Carre0a353c2018-08-02 16:38:04 -0700208 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel, int32_t displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700209 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
210
Arthur Hung39134b22018-08-14 11:58:28 +0800211 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray, int32_t displayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800212 void setFocusedApplication(JNIEnv* env, int32_t displayId, jobject applicationHandleObj);
213 void setFocusedDisplay(JNIEnv* env, int32_t displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700214 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800215 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700216 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700217 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700218 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700219 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800220 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100221 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800222 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700223 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800224 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700225
Jeff Brown9c3cda02010-06-15 01:31:58 -0700226 /* --- InputReaderPolicyInterface implementation --- */
227
Jeff Brown214eaf42011-05-26 19:17:02 -0700228 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800229 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700230 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800231 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100232 virtual std::string getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700233 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
234 jfloatArray matrixArr);
235 virtual TouchAffineTransformation getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100236 const std::string& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237
238 /* --- InputDispatcherPolicyInterface implementation --- */
239
Jeff Brownbcc046a2012-09-27 20:46:43 -0700240 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700241 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700242 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700243 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carre0a353c2018-08-02 16:38:04 -0700244 const sp<IBinder>& token,
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800245 const std::string& reason);
Robert Carre0a353c2018-08-02 16:38:04 -0700246 virtual void notifyInputChannelBroken(const sp<IBinder>& token);
Robert Carrbd26e652018-08-21 15:46:28 -0700247 virtual void notifyFocusChanged(const sp<IBinder>& token);
Jeff Brown0029c662011-03-30 02:25:18 -0700248 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700249 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800250 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800251 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700252 virtual nsecs_t interceptKeyBeforeDispatching(
Robert Carre0a353c2018-08-02 16:38:04 -0700253 const sp<IBinder>& token,
Jeff Brownb88102f2010-09-08 11:49:43 -0700254 const KeyEvent* keyEvent, uint32_t policyFlags);
Robert Carre0a353c2018-08-02 16:38:04 -0700255 virtual bool dispatchUnhandledKey(const sp<IBinder>& token,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800256 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700257 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700258 virtual bool checkInjectEventsPermissionNonReentrant(
259 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700260
Jeff Brown2352b972011-04-12 22:39:53 -0700261 /* --- PointerControllerPolicyInterface implementation --- */
262
Jun Mukai19a56012015-11-24 11:25:52 -0800263 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700264 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700265 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
266 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900267 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700268 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700269
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270private:
271 sp<InputManager> mInputManager;
272
Jeff Brown2352b972011-04-12 22:39:53 -0700273 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700274 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800275 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700276
Jeff Brown83c09682010-12-23 17:50:18 -0800277 Mutex mLock;
278 struct Locked {
279 // Display size information.
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100280 std::vector<DisplayViewport> viewports;
Jeff Brown83c09682010-12-23 17:50:18 -0800281
Jeff Brown05dc66a2011-03-02 14:41:58 -0800282 // System UI visibility.
283 int32_t systemUiVisibility;
284
Jeff Brown1a84fd12011-06-02 01:26:32 -0700285 // Pointer speed.
286 int32_t pointerSpeed;
287
Jeff Brown474dcb52011-06-14 20:22:50 -0700288 // True if pointer gestures are enabled.
289 bool pointerGesturesEnabled;
290
Jeff Browndaf4a122011-08-26 17:14:14 -0700291 // Show touches feature enable/disable.
292 bool showTouches;
293
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800294 // Pointer capture feature enable/disable.
295 bool pointerCapture;
296
Jeff Brown5541de92011-04-11 11:54:25 -0700297 // Sprite controller singleton, created on first use.
298 sp<SpriteController> spriteController;
299
Jeff Brown83c09682010-12-23 17:50:18 -0800300 // Pointer controller singleton, created and destroyed as needed.
301 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700302
303 // Input devices to be disabled
304 SortedVector<int32_t> disabledInputDevices;
Arthur Hungb9b32002018-12-18 17:39:43 +0800305
306 // Associated Pointer controller display.
307 int32_t pointerDisplayId;
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100308 } mLocked GUARDED_BY(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700309
Michael Wrighta4051212015-07-23 17:04:40 +0100310 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700311
Arthur Hungb9b32002018-12-18 17:39:43 +0800312 void updateInactivityTimeoutLocked();
Jeff Brown56194eb2011-03-02 19:23:13 -0800313 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700314 void ensureSpriteControllerLocked();
Arthur Hungb9b32002018-12-18 17:39:43 +0800315 const DisplayViewport* findDisplayViewportLocked(int32_t displayId);
316 int32_t getPointerDisplayId();
317 void updatePointerDisplayLocked();
Jeff Brownb88102f2010-09-08 11:49:43 -0700318 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700319
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320 static inline JNIEnv* jniEnv() {
321 return AndroidRuntime::getJNIEnv();
322 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700323};
324
Jeff Brown928e0542011-01-10 11:17:36 -0800325
Jeff Brown9c3cda02010-06-15 01:31:58 -0700326
Jeff Brown2352b972011-04-12 22:39:53 -0700327NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700328 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700329 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700330 JNIEnv* env = jniEnv();
331
Jeff Brown2352b972011-04-12 22:39:53 -0700332 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700333 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700334
Jeff Brown83c09682010-12-23 17:50:18 -0800335 {
336 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800337 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700338 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700339 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700340 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800341 mLocked.pointerCapture = false;
Arthur Hungb9b32002018-12-18 17:39:43 +0800342 mLocked.pointerDisplayId = ADISPLAY_ID_DEFAULT;
Jeff Brown83c09682010-12-23 17:50:18 -0800343 }
Michael Wrighta4051212015-07-23 17:04:40 +0100344 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800345
Prabir Pradhane5696a52018-11-14 19:55:21 -0800346 mInputManager = new InputManager(this, this);
Robert Carr679ccb02018-08-08 15:32:35 -0700347 defaultServiceManager()->addService(String16("inputflinger"),
348 mInputManager, false);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700349}
350
351NativeInputManager::~NativeInputManager() {
352 JNIEnv* env = jniEnv();
353
Jeff Brown2352b972011-04-12 22:39:53 -0700354 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700355 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700356}
357
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800358void NativeInputManager::dump(std::string& dump) {
359 dump += "Input Manager State:\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100360 {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800361 dump += StringPrintf(INDENT "Interactive: %s\n", toString(mInteractive.load()));
Michael Wrighta4051212015-07-23 17:04:40 +0100362 }
363 {
364 AutoMutex _l(mLock);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800365 dump += StringPrintf(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100366 mLocked.systemUiVisibility);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800367 dump += StringPrintf(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
368 dump += StringPrintf(INDENT "Pointer Gestures Enabled: %s\n",
Michael Wrighta4051212015-07-23 17:04:40 +0100369 toString(mLocked.pointerGesturesEnabled));
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800370 dump += StringPrintf(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
371 dump += StringPrintf(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100372 }
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800373 dump += "\n";
Michael Wrighta4051212015-07-23 17:04:40 +0100374
Jeff Brownb88102f2010-09-08 11:49:43 -0700375 mInputManager->getReader()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800376 dump += "\n";
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377
Jeff Brownb88102f2010-09-08 11:49:43 -0700378 mInputManager->getDispatcher()->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800379 dump += "\n";
Jeff Brown9c3cda02010-06-15 01:31:58 -0700380}
381
Jeff Brown7fbdc842010-06-17 20:52:56 -0700382bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700383 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000384 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700385 LOGE_EX(env);
386 env->ExceptionClear();
387 return true;
388 }
389 return false;
390}
391
Arthur Hungb9b32002018-12-18 17:39:43 +0800392const DisplayViewport* NativeInputManager::findDisplayViewportLocked(int32_t displayId)
393 REQUIRES(mLock) {
394 for (const DisplayViewport& v : mLocked.viewports) {
395 if (v.displayId == displayId) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100396 return &v;
397 }
398 }
399 return nullptr;
400}
401
402void NativeInputManager::setDisplayViewports(JNIEnv* env, jobjectArray viewportObjArray) {
403 std::vector<DisplayViewport> viewports;
Santos Cordonee8931e2017-04-05 10:31:15 -0700404
405 if (viewportObjArray) {
406 jsize length = env->GetArrayLength(viewportObjArray);
407 for (jsize i = 0; i < length; i++) {
408 jobject viewportObj = env->GetObjectArrayElement(viewportObjArray, i);
409 if (! viewportObj) {
410 break; // found null element indicating end of used portion of the array
411 }
412
413 DisplayViewport viewport;
414 android_hardware_display_DisplayViewport_toNative(env, viewportObj, &viewport);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100415 ALOGI("Viewport [%d] to add: %s", (int) i, viewport.uniqueId.c_str());
416 viewports.push_back(viewport);
Santos Cordonee8931e2017-04-05 10:31:15 -0700417
418 env->DeleteLocalRef(viewportObj);
419 }
420 }
421
Arthur Hungb9b32002018-12-18 17:39:43 +0800422 // Get the preferred pointer controller displayId.
423 int32_t pointerDisplayId = getPointerDisplayId();
424
425 { // acquire lock
Santos Cordonee8931e2017-04-05 10:31:15 -0700426 AutoMutex _l(mLock);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100427 mLocked.viewports = viewports;
Arthur Hungb9b32002018-12-18 17:39:43 +0800428 mLocked.pointerDisplayId = pointerDisplayId;
429 } // release lock
Santos Cordonee8931e2017-04-05 10:31:15 -0700430
431 mInputManager->getReader()->requestRefreshConfiguration(
432 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
433}
434
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700435status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Robert Carre0a353c2018-08-02 16:38:04 -0700436 const sp<InputChannel>& inputChannel, int32_t displayId) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100437 ATRACE_CALL();
Robert Carre0a353c2018-08-02 16:38:04 -0700438 return mInputManager->getDispatcher()->registerInputChannel(
439 inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700440}
441
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700442status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700443 const sp<InputChannel>& inputChannel) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100444 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700445 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700446}
447
Jeff Brown214eaf42011-05-26 19:17:02 -0700448void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100449 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700450 JNIEnv* env = jniEnv();
451
Jeff Brown4532e612012-04-05 14:27:12 -0700452 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
453 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700454 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
455 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
456 }
457
458 outConfig->excludedDeviceNames.clear();
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700459 jobjectArray excludedDeviceNames = jobjectArray(env->CallStaticObjectMethod(
460 gServiceClassInfo.clazz, gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700461 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
462 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700463 for (jsize i = 0; i < length; i++) {
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700464 std::string deviceName = getStringElementFromJavaArray(env, excludedDeviceNames, i);
465 outConfig->excludedDeviceNames.push_back(deviceName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700466 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700467 env->DeleteLocalRef(excludedDeviceNames);
468 }
469
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -0700470 // Associations between input ports and display ports
471 // The java method packs the information in the following manner:
472 // Original data: [{'inputPort1': '1'}, {'inputPort2': '2'}]
473 // Received data: ['inputPort1', '1', 'inputPort2', '2']
474 // So we unpack accordingly here.
475 outConfig->portAssociations.clear();
476 jobjectArray portAssociations = jobjectArray(env->CallStaticObjectMethod(
477 gServiceClassInfo.clazz, gServiceClassInfo.getInputPortAssociations));
478 if (!checkAndClearExceptionFromCallback(env, "getInputPortAssociations") && portAssociations) {
479 jsize length = env->GetArrayLength(portAssociations);
480 for (jsize i = 0; i < length / 2; i++) {
481 std::string inputPort = getStringElementFromJavaArray(env, portAssociations, 2 * i);
482 std::string displayPortStr =
483 getStringElementFromJavaArray(env, portAssociations, 2 * i + 1);
484 uint8_t displayPort;
485 // Should already have been validated earlier, but do it here for safety.
486 bool success = ParseUint(displayPortStr, &displayPort);
487 if (!success) {
488 ALOGE("Could not parse entry in port configuration file, received: %s",
489 displayPortStr.c_str());
490 continue;
491 }
492 outConfig->portAssociations.insert({inputPort, displayPort});
493 }
494 env->DeleteLocalRef(portAssociations);
495 }
496
Jeff Brown4532e612012-04-05 14:27:12 -0700497 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
498 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700499 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700500 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
501 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700502 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700503 jint longPressTimeout = env->CallIntMethod(mServiceObj,
504 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700505 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700506 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700507
508 // We must ensure that the tap-drag interval is significantly shorter than
509 // the long-press timeout because the tap is held down for the entire duration
510 // of the double-tap timeout.
511 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700512 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700513 outConfig->pointerGestureTapDragInterval =
514 milliseconds_to_nanoseconds(tapDragInterval);
515 }
516 }
517 }
518
Jeff Brown4532e612012-04-05 14:27:12 -0700519 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
520 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700521 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
522 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700523 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700524
525 { // acquire lock
526 AutoMutex _l(mLock);
527
528 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
529 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700530 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700531
Jeff Browndaf4a122011-08-26 17:14:14 -0700532 outConfig->showTouches = mLocked.showTouches;
533
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800534 outConfig->pointerCapture = mLocked.pointerCapture;
535
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100536 outConfig->setDisplayViewports(mLocked.viewports);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700537
538 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700539 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700540}
541
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700542sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100543 ATRACE_CALL();
Jeff Brown83c09682010-12-23 17:50:18 -0800544 AutoMutex _l(mLock);
545
546 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100547 if (controller == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700548 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800549
Jeff Brown2352b972011-04-12 22:39:53 -0700550 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800551 mLocked.pointerController = controller;
Arthur Hungb9b32002018-12-18 17:39:43 +0800552 updateInactivityTimeoutLocked();
Andrii Kuliand44026f2018-12-17 18:59:36 +0000553 }
Arthur Hungb9b32002018-12-18 17:39:43 +0800554
555 updatePointerDisplayLocked();
556
Jeff Brown83c09682010-12-23 17:50:18 -0800557 return controller;
558}
559
Arthur Hungb9b32002018-12-18 17:39:43 +0800560int32_t NativeInputManager::getPointerDisplayId() {
561 JNIEnv* env = jniEnv();
562 jint pointerDisplayId = env->CallIntMethod(mServiceObj,
563 gServiceClassInfo.getPointerDisplayId);
564 if (checkAndClearExceptionFromCallback(env, "getPointerDisplayId")) {
565 pointerDisplayId = ADISPLAY_ID_DEFAULT;
566 }
567
568 return pointerDisplayId;
569}
570
571void NativeInputManager::updatePointerDisplayLocked() REQUIRES(mLock) {
572 ATRACE_CALL();
573
574 sp<PointerController> controller = mLocked.pointerController.promote();
575 if (controller != nullptr) {
576 const DisplayViewport* viewport = findDisplayViewportLocked(mLocked.pointerDisplayId);
577 if (viewport == nullptr) {
578 ALOGW("Can't find pointer display viewport, fallback to default display.");
579 viewport = findDisplayViewportLocked(ADISPLAY_ID_DEFAULT);
580 }
581
582 if (viewport != nullptr) {
583 controller->setDisplayViewport(*viewport);
584 }
585 }
586}
587
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100588void NativeInputManager::ensureSpriteControllerLocked() REQUIRES(mLock) {
589 if (mLocked.spriteController == nullptr) {
Jeff Brown5541de92011-04-11 11:54:25 -0700590 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700591 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700592 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
593 layer = -1;
594 }
595 mLocked.spriteController = new SpriteController(mLooper, layer);
596 }
597}
598
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700599void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100600 ATRACE_CALL();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700601 JNIEnv* env = jniEnv();
602
603 size_t count = inputDevices.size();
604 jobjectArray inputDevicesObjArray = env->NewObjectArray(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100605 count, gInputDeviceClassInfo.clazz, nullptr);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700606 if (inputDevicesObjArray) {
607 bool error = false;
608 for (size_t i = 0; i < count; i++) {
609 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
610 if (!inputDeviceObj) {
611 error = true;
612 break;
613 }
614
615 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
616 env->DeleteLocalRef(inputDeviceObj);
617 }
618
619 if (!error) {
620 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
621 inputDevicesObjArray);
622 }
623
624 env->DeleteLocalRef(inputDevicesObjArray);
625 }
626
627 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
628}
629
Jeff Brown6ec6f792012-04-17 16:52:41 -0700630sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800631 const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100632 ATRACE_CALL();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700633 JNIEnv* env = jniEnv();
634
635 sp<KeyCharacterMap> result;
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100636 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.c_str()));
RoboErikfb290df2013-12-16 11:27:55 -0800637 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
638 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
639 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700640 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800641 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700642 if (arrayObj.get()) {
643 ScopedLocalRef<jstring> filenameObj(env,
644 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
645 ScopedLocalRef<jstring> contentsObj(env,
646 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
647 ScopedUtfChars filenameChars(env, filenameObj.get());
648 ScopedUtfChars contentsChars(env, contentsObj.get());
649
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100650 KeyCharacterMap::loadContents(filenameChars.c_str(),
651 contentsChars.c_str(), KeyCharacterMap::FORMAT_OVERLAY, &result);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700652 }
653 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
654 return result;
655}
656
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100657std::string NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100658 ATRACE_CALL();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700659 JNIEnv* env = jniEnv();
660
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100661 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.c_str()));
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700662 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
663 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100664 std::string result;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700665 if (aliasObj.get()) {
666 ScopedUtfChars aliasChars(env, aliasObj.get());
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100667 result = aliasChars.c_str();
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700668 }
669 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
670 return result;
671}
672
Jeff Brownbcc046a2012-09-27 20:46:43 -0700673void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700674 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700675#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700676 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
677 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700678#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100679 ATRACE_CALL();
Jeff Browne20c9e02010-10-11 14:20:19 -0700680
681 JNIEnv* env = jniEnv();
682
Jeff Brown53384282012-08-20 20:16:01 -0700683 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700684 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700685 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700686}
687
Jeff Brown9c3cda02010-06-15 01:31:58 -0700688void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
689#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000690 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700691#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100692 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700693
694 JNIEnv* env = jniEnv();
695
Jeff Brown4532e612012-04-05 14:27:12 -0700696 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700697 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700698}
699
Jeff Brown519e0242010-09-15 15:18:56 -0700700nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Robert Carre0a353c2018-08-02 16:38:04 -0700701 const sp<IBinder>& token, const std::string& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700702#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000703 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700704#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100705 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -0700706
707 JNIEnv* env = jniEnv();
708
Robert Carre0a353c2018-08-02 16:38:04 -0700709 jobject tokenObj = javaObjectForIBinder(env, token);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -0800710 jstring reasonObj = env->NewStringUTF(reason.c_str());
Jeff Brownb88102f2010-09-08 11:49:43 -0700711
Jeff Brown4532e612012-04-05 14:27:12 -0700712 jlong newTimeout = env->CallLongMethod(mServiceObj,
Robert Carr679ccb02018-08-08 15:32:35 -0700713 gServiceClassInfo.notifyANR, tokenObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700714 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700715 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
716 newTimeout = 0; // abort dispatch
717 } else {
718 assert(newTimeout >= 0);
719 }
720
Jeff Brownbd181bb2013-09-10 16:44:24 -0700721 env->DeleteLocalRef(reasonObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700722 return newTimeout;
723}
724
Robert Carre0a353c2018-08-02 16:38:04 -0700725void NativeInputManager::notifyInputChannelBroken(const sp<IBinder>& token) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700726#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000727 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700728#endif
Michael Wrightbafea6e2017-06-12 15:25:47 +0100729 ATRACE_CALL();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700730
Jeff Brown7fbdc842010-06-17 20:52:56 -0700731 JNIEnv* env = jniEnv();
732
Robert Carre0a353c2018-08-02 16:38:04 -0700733 jobject tokenObj = javaObjectForIBinder(env, token);
734 if (tokenObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700735 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Robert Carre0a353c2018-08-02 16:38:04 -0700736 tokenObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700737 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700738 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700739}
740
Robert Carrbd26e652018-08-21 15:46:28 -0700741void NativeInputManager::notifyFocusChanged(const sp<IBinder>& token) {
742#if DEBUG_INPUT_DISPATCHER_POLICY
743 ALOGD("notifyFocusChanged");
744#endif
745 ATRACE_CALL();
746
747 JNIEnv* env = jniEnv();
748
749 jobject tokenObj = javaObjectForIBinder(env, token);
750 if (tokenObj) {
751 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyFocusChanged,
752 tokenObj);
753 checkAndClearExceptionFromCallback(env, "notifyFocusChanged");
754 }
755}
756
Jeff Brown214eaf42011-05-26 19:17:02 -0700757void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100758 ATRACE_CALL();
Jeff Brown214eaf42011-05-26 19:17:02 -0700759 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800760
Jeff Brown4532e612012-04-05 14:27:12 -0700761 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
762 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700763 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
764 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
765 }
Jeff Browna4547672011-03-02 21:38:11 -0800766
Jeff Brown4532e612012-04-05 14:27:12 -0700767 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
768 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700769 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
770 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
771 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700772}
773
Arthur Hung39134b22018-08-14 11:58:28 +0800774void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray,
775 int32_t displayId) {
Jeff Brown9302c872011-07-13 22:51:29 -0700776 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700777
Jeff Brown9302c872011-07-13 22:51:29 -0700778 if (windowHandleObjArray) {
779 jsize length = env->GetArrayLength(windowHandleObjArray);
780 for (jsize i = 0; i < length; i++) {
781 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
782 if (! windowHandleObj) {
783 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700784 }
Jeff Brown9302c872011-07-13 22:51:29 -0700785
786 sp<InputWindowHandle> windowHandle =
787 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100788 if (windowHandle != nullptr) {
Jeff Brown9302c872011-07-13 22:51:29 -0700789 windowHandles.push(windowHandle);
790 }
791 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700792 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700793 }
Jeff Brown349703e2010-06-22 01:27:15 -0700794
Arthur Hung39134b22018-08-14 11:58:28 +0800795 mInputManager->getDispatcher()->setInputWindows(windowHandles, displayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700796
797 // Do this after the dispatcher has updated the window handle state.
798 bool newPointerGesturesEnabled = true;
799 size_t numWindows = windowHandles.size();
800 for (size_t i = 0; i < numWindows; i++) {
801 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700802 const InputWindowInfo* windowInfo = windowHandle->getInfo();
803 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
804 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700805 newPointerGesturesEnabled = false;
806 }
807 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700808
809 uint32_t changes = 0;
810 { // acquire lock
811 AutoMutex _l(mLock);
812
813 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
814 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
815 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
816 }
817 } // release lock
818
819 if (changes) {
820 mInputManager->getReader()->requestRefreshConfiguration(changes);
821 }
Jeff Brown349703e2010-06-22 01:27:15 -0700822}
823
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800824void NativeInputManager::setFocusedApplication(JNIEnv* env, int32_t displayId,
825 jobject applicationHandleObj) {
Jeff Brown9302c872011-07-13 22:51:29 -0700826 sp<InputApplicationHandle> applicationHandle =
827 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800828 mInputManager->getDispatcher()->setFocusedApplication(displayId, applicationHandle);
829}
830
831void NativeInputManager::setFocusedDisplay(JNIEnv* env, int32_t displayId) {
832 mInputManager->getDispatcher()->setFocusedDisplay(displayId);
Jeff Brown349703e2010-06-22 01:27:15 -0700833}
834
835void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700836 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700837}
838
Jeff Brown05dc66a2011-03-02 14:41:58 -0800839void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
840 AutoMutex _l(mLock);
841
842 if (mLocked.systemUiVisibility != visibility) {
843 mLocked.systemUiVisibility = visibility;
Arthur Hungb9b32002018-12-18 17:39:43 +0800844 updateInactivityTimeoutLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800845 }
846}
847
Arthur Hungb9b32002018-12-18 17:39:43 +0800848void NativeInputManager::updateInactivityTimeoutLocked() REQUIRES(mLock) {
849 sp<PointerController> controller = mLocked.pointerController.promote();
850 if (controller == nullptr) {
851 return;
852 }
853
Jeff Brown05dc66a2011-03-02 14:41:58 -0800854 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700855 controller->setInactivityTimeout(lightsOut
856 ? PointerController::INACTIVITY_TIMEOUT_SHORT
857 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800858}
859
Jeff Brown1a84fd12011-06-02 01:26:32 -0700860void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700861 { // acquire lock
862 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700863
Jeff Brown474dcb52011-06-14 20:22:50 -0700864 if (mLocked.pointerSpeed == speed) {
865 return;
866 }
867
Steve Block6215d3f2012-01-04 20:05:49 +0000868 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700869 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700870 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700871
Jeff Brown474dcb52011-06-14 20:22:50 -0700872 mInputManager->getReader()->requestRefreshConfiguration(
873 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700874}
875
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700876void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
877 { // acquire lock
878 AutoMutex _l(mLock);
879
880 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
881 bool currentlyEnabled = index < 0;
882 if (!enabled && currentlyEnabled) {
883 mLocked.disabledInputDevices.add(deviceId);
884 }
885 if (enabled && !currentlyEnabled) {
886 mLocked.disabledInputDevices.remove(deviceId);
887 }
888 } // release lock
889
890 mInputManager->getReader()->requestRefreshConfiguration(
891 InputReaderConfiguration::CHANGE_ENABLED_STATE);
892}
893
Jeff Browndaf4a122011-08-26 17:14:14 -0700894void NativeInputManager::setShowTouches(bool enabled) {
895 { // acquire lock
896 AutoMutex _l(mLock);
897
898 if (mLocked.showTouches == enabled) {
899 return;
900 }
901
Steve Block6215d3f2012-01-04 20:05:49 +0000902 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700903 mLocked.showTouches = enabled;
904 } // release lock
905
906 mInputManager->getReader()->requestRefreshConfiguration(
907 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
908}
909
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800910void NativeInputManager::setPointerCapture(bool enabled) {
911 { // acquire lock
912 AutoMutex _l(mLock);
913
914 if (mLocked.pointerCapture == enabled) {
915 return;
916 }
917
918 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
919 mLocked.pointerCapture = enabled;
920 } // release lock
921
922 mInputManager->getReader()->requestRefreshConfiguration(
923 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
924}
925
Jeff Brown037c33e2014-04-09 00:31:55 -0700926void NativeInputManager::setInteractive(bool interactive) {
927 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700928}
929
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800930void NativeInputManager::reloadCalibration() {
931 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100932 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800933}
934
Michael Wrighte051f6f2016-05-13 17:44:16 +0100935void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800936 AutoMutex _l(mLock);
937 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100938 if (controller != nullptr) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100939 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800940 }
941}
942
943void NativeInputManager::reloadPointerIcons() {
944 AutoMutex _l(mLock);
945 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100946 if (controller != nullptr) {
Jun Mukai19a56012015-11-24 11:25:52 -0800947 controller->reloadPointerResources();
948 }
Jun Mukai1db53972015-09-11 18:08:31 -0700949}
950
Jun Mukaid4eaef72015-10-30 15:54:33 -0700951void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
952 AutoMutex _l(mLock);
953 sp<PointerController> controller = mLocked.pointerController.promote();
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +0100954 if (controller != nullptr) {
Jun Mukaid4eaef72015-10-30 15:54:33 -0700955 controller->setCustomPointerIcon(icon);
956 }
957}
958
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800959TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
960 JNIEnv *env, jfloatArray matrixArr) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100961 ATRACE_CALL();
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800962 ScopedFloatArrayRO matrix(env, matrixArr);
963 assert(matrix.size() == 6);
964
965 TouchAffineTransformation transform;
966 transform.x_scale = matrix[0];
967 transform.x_ymix = matrix[1];
968 transform.x_offset = matrix[2];
969 transform.y_xmix = matrix[3];
970 transform.y_scale = matrix[4];
971 transform.y_offset = matrix[5];
972
973 return transform;
974}
975
976TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100977 const std::string& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800978 JNIEnv* env = jniEnv();
979
Siarhei Vishniakou80278762018-07-06 11:50:18 +0100980 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.c_str()));
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800981
982 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700983 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
984 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800985
986 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
987 gTouchCalibrationClassInfo.getAffineTransform));
988
989 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
990
991 env->DeleteLocalRef(matrixArr);
992 env->DeleteLocalRef(cal);
993
994 return transform;
995}
996
Jeff Brown0029c662011-03-30 02:25:18 -0700997bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +0100998 ATRACE_CALL();
Jeff Brown0029c662011-03-30 02:25:18 -0700999 jobject inputEventObj;
1000
1001 JNIEnv* env = jniEnv();
1002 switch (inputEvent->getType()) {
1003 case AINPUT_EVENT_TYPE_KEY:
1004 inputEventObj = android_view_KeyEvent_fromNative(env,
1005 static_cast<const KeyEvent*>(inputEvent));
1006 break;
1007 case AINPUT_EVENT_TYPE_MOTION:
1008 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
1009 static_cast<const MotionEvent*>(inputEvent));
1010 break;
1011 default:
1012 return true; // dispatch the event normally
1013 }
1014
1015 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +00001016 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -07001017 return true; // dispatch the event normally
1018 }
1019
1020 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -07001021 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -07001022 inputEventObj, policyFlags);
1023 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
1024 pass = true;
1025 }
1026 env->DeleteLocalRef(inputEventObj);
1027 return pass;
1028}
1029
Jeff Brown1f245102010-11-18 20:53:46 -08001030void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
1031 uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001032 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001033 // Policy:
1034 // - Ignore untrusted events and pass them along.
1035 // - Ask the window manager what to do with normal events and trusted injected events.
1036 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001037 bool interactive = mInteractive.load();
1038 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001039 policyFlags |= POLICY_FLAG_INTERACTIVE;
1040 }
Jeff Brown3122e442010-10-11 23:32:49 -07001041 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -08001042 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -07001043 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -08001044 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1045 jint wmActions;
1046 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001047 wmActions = env->CallIntMethod(mServiceObj,
1048 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -07001049 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001050 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
1051 wmActions = 0;
1052 }
1053 android_view_KeyEvent_recycle(env, keyEventObj);
1054 env->DeleteLocalRef(keyEventObj);
1055 } else {
Steve Block3762c312012-01-06 19:20:56 +00001056 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -07001057 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -07001058 }
1059
Jeff Brown56194eb2011-03-02 19:23:13 -08001060 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -07001061 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001062 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001063 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1064 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001065 }
1066}
1067
Jeff Brown56194eb2011-03-02 19:23:13 -08001068void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001069 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001070 // Policy:
1071 // - Ignore untrusted events and pass them along.
1072 // - No special filtering for injected events required at this time.
1073 // - Filter normal events based on screen state.
1074 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +01001075 bool interactive = mInteractive.load();
1076 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001077 policyFlags |= POLICY_FLAG_INTERACTIVE;
1078 }
Jeff Brown3122e442010-10-11 23:32:49 -07001079 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -07001080 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -07001081 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -07001082 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -08001083 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001084 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -07001085 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -08001086 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -08001087 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -07001088 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001089 wmActions = 0;
1090 }
1091
Jeff Brown56194eb2011-03-02 19:23:13 -08001092 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -07001093 }
Jeff Brown3122e442010-10-11 23:32:49 -07001094 } else {
Michael Wrighta4051212015-07-23 17:04:40 +01001095 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -07001096 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1097 }
Jeff Browne20c9e02010-10-11 14:20:19 -07001098 }
1099}
1100
Jeff Brown56194eb2011-03-02 19:23:13 -08001101void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
1102 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -08001103 if (wmActions & WM_ACTION_PASS_TO_USER) {
1104 policyFlags |= POLICY_FLAG_PASS_TO_USER;
1105 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001106#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001107 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001108#endif
1109 }
1110}
1111
Jeff Brown905805a2011-10-12 13:57:59 -07001112nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Robert Carre0a353c2018-08-02 16:38:04 -07001113 const sp<IBinder>& token,
Jeff Browne20c9e02010-10-11 14:20:19 -07001114 const KeyEvent* keyEvent, uint32_t policyFlags) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001115 ATRACE_CALL();
Jeff Brown3122e442010-10-11 23:32:49 -07001116 // Policy:
1117 // - Ignore untrusted events and pass them along.
1118 // - Filter normal events and trusted injected events through the window manager policy to
1119 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001120 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001121 if (policyFlags & POLICY_FLAG_TRUSTED) {
1122 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001123
Robert Carre0a353c2018-08-02 16:38:04 -07001124 // Token may be null
1125 jobject tokenObj = javaObjectForIBinder(env, token);
1126
Jeff Brown1f245102010-11-18 20:53:46 -08001127 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1128 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001129 jlong delayMillis = env->CallLongMethod(mServiceObj,
1130 gServiceClassInfo.interceptKeyBeforeDispatching,
Robert Carre0a353c2018-08-02 16:38:04 -07001131 tokenObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001132 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1133 android_view_KeyEvent_recycle(env, keyEventObj);
1134 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001135 if (!error) {
1136 if (delayMillis < 0) {
1137 result = -1;
1138 } else if (delayMillis > 0) {
1139 result = milliseconds_to_nanoseconds(delayMillis);
1140 }
1141 }
Jeff Brown1f245102010-11-18 20:53:46 -08001142 } else {
Steve Block3762c312012-01-06 19:20:56 +00001143 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001144 }
Jeff Brown3122e442010-10-11 23:32:49 -07001145 }
Jeff Brown1f245102010-11-18 20:53:46 -08001146 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001147}
1148
Robert Carre0a353c2018-08-02 16:38:04 -07001149bool NativeInputManager::dispatchUnhandledKey(const sp<IBinder>& token,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001150 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001151 ATRACE_CALL();
Jeff Brown3915bb82010-11-05 15:02:16 -07001152 // Policy:
1153 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001154 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001155 if (policyFlags & POLICY_FLAG_TRUSTED) {
1156 JNIEnv* env = jniEnv();
1157
Robert Carre0a353c2018-08-02 16:38:04 -07001158 // Note: tokenObj may be null.
1159 jobject tokenObj = javaObjectForIBinder(env, token);
Jeff Brown1f245102010-11-18 20:53:46 -08001160 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1161 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001162 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1163 gServiceClassInfo.dispatchUnhandledKey,
Robert Carre0a353c2018-08-02 16:38:04 -07001164 tokenObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001165 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001166 fallbackKeyEventObj = nullptr;
Jeff Brownda3d5a92011-03-29 15:11:34 -07001167 }
Jeff Brown1f245102010-11-18 20:53:46 -08001168 android_view_KeyEvent_recycle(env, keyEventObj);
1169 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001170
1171 if (fallbackKeyEventObj) {
1172 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1173 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1174 outFallbackKeyEvent)) {
1175 result = true;
1176 }
1177 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1178 env->DeleteLocalRef(fallbackKeyEventObj);
1179 }
Jeff Brown1f245102010-11-18 20:53:46 -08001180 } else {
Steve Block3762c312012-01-06 19:20:56 +00001181 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001182 }
Jeff Brown3915bb82010-11-05 15:02:16 -07001183 }
Jeff Brown1f245102010-11-18 20:53:46 -08001184 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001185}
1186
Jeff Brown01ce2e92010-09-26 22:20:12 -07001187void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001188 ATRACE_CALL();
Jeff Brown01ce2e92010-09-26 22:20:12 -07001189 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001190}
1191
Jeff Brown349703e2010-06-22 01:27:15 -07001192
Jeff Brownb88102f2010-09-08 11:49:43 -07001193bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1194 int32_t injectorPid, int32_t injectorUid) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001195 ATRACE_CALL();
Jeff Brownb88102f2010-09-08 11:49:43 -07001196 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001197 jboolean result = env->CallBooleanMethod(mServiceObj,
1198 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001199 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1200 result = false;
1201 }
Jeff Brown349703e2010-06-22 01:27:15 -07001202 return result;
1203}
1204
Jun Mukai19a56012015-11-24 11:25:52 -08001205void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001206 ATRACE_CALL();
Jun Mukai19a56012015-11-24 11:25:52 -08001207 JNIEnv* env = jniEnv();
1208
1209 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1210 mServiceObj, gServiceClassInfo.getPointerIcon));
1211 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1212 return;
1213 }
1214
1215 PointerIcon pointerIcon;
1216 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1217 mContextObj, &pointerIcon);
1218 if (!status && !pointerIcon.isNullIcon()) {
1219 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1220 } else {
1221 *icon = SpriteIcon();
1222 }
1223}
1224
Jeff Brown2352b972011-04-12 22:39:53 -07001225void NativeInputManager::loadPointerResources(PointerResources* outResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001226 ATRACE_CALL();
Jeff Brown2352b972011-04-12 22:39:53 -07001227 JNIEnv* env = jniEnv();
1228
1229 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1230 &outResources->spotHover);
1231 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1232 &outResources->spotTouch);
1233 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1234 &outResources->spotAnchor);
1235}
1236
Jun Mukai808196f2015-10-28 16:46:44 -07001237void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1238 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Michael Wrightbafea6e2017-06-12 15:25:47 +01001239 ATRACE_CALL();
Jun Mukai1db53972015-09-11 18:08:31 -07001240 JNIEnv* env = jniEnv();
1241
1242 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1243 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001244 PointerIcon pointerIcon;
1245 loadSystemIconAsSpriteWithPointerIcon(
1246 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1247 if (!pointerIcon.bitmapFrames.empty()) {
1248 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1249 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1250 animationData.durationPerFrame =
1251 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1252 animationData.animationFrames.reserve(numFrames);
1253 animationData.animationFrames.push_back(SpriteIcon(
1254 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1255 for (size_t i = 0; i < numFrames - 1; ++i) {
1256 animationData.animationFrames.push_back(SpriteIcon(
1257 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1258 }
1259 }
Jun Mukai1db53972015-09-11 18:08:31 -07001260 }
Jun Mukai808196f2015-10-28 16:46:44 -07001261 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1262 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001263}
1264
Jun Mukai5ec74202015-10-07 16:58:09 +09001265int32_t NativeInputManager::getDefaultPointerIconId() {
1266 return POINTER_ICON_STYLE_ARROW;
1267}
Jeff Brown83c09682010-12-23 17:50:18 -08001268
Jun Mukaid4eaef72015-10-30 15:54:33 -07001269int32_t NativeInputManager::getCustomPointerIconId() {
1270 return POINTER_ICON_STYLE_CUSTOM;
1271}
1272
Jeff Brown9c3cda02010-06-15 01:31:58 -07001273// ----------------------------------------------------------------------------
1274
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001275static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001276 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001277 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001278 if (messageQueue == nullptr) {
Jeff Brown864693462013-01-28 14:25:53 -08001279 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1280 return 0;
1281 }
1282
Jeff Brown603b4452012-04-06 17:39:41 -07001283 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1284 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001285 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001286 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001287}
1288
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001289static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001290 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001291
Jeff Brown4532e612012-04-05 14:27:12 -07001292 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001293 if (result) {
1294 jniThrowRuntimeException(env, "Input manager could not be started.");
1295 }
1296}
1297
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001298static void nativeSetDisplayViewports(JNIEnv* env, jclass /* clazz */, jlong ptr,
Santos Cordonee8931e2017-04-05 10:31:15 -07001299 jobjectArray viewportObjArray) {
1300 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001301 im->setDisplayViewports(env, viewportObjArray);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001302}
1303
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001304static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001305 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001306 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001307
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001308 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001309 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001310}
1311
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001312static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001313 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001314 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001315
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001316 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001317 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001318}
1319
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001320static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001321 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001322 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001323
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001324 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001325 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001326}
1327
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001328static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001329 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001330 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001331
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001332 int32_t* codes = env->GetIntArrayElements(keyCodes, nullptr);
1333 uint8_t* flags = env->GetBooleanArrayElements(outFlags, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001334 jsize numCodes = env->GetArrayLength(keyCodes);
1335 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001336 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001337 if (im->getInputManager()->getReader()->hasKeys(
1338 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1339 result = JNI_TRUE;
1340 } else {
1341 result = JNI_FALSE;
1342 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001343 } else {
1344 result = JNI_FALSE;
1345 }
1346
1347 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1348 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1349 return result;
1350}
1351
1352static void throwInputChannelNotInitialized(JNIEnv* env) {
1353 jniThrowException(env, "java/lang/IllegalStateException",
1354 "inputChannel is not initialized");
1355}
1356
Jeff Brown4532e612012-04-05 14:27:12 -07001357static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001358 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001359 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1360
Steve Block8564c8d2012-01-05 23:22:43 +00001361 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001362 "the input manager!", inputChannel->getName().c_str());
Jeff Brown4532e612012-04-05 14:27:12 -07001363 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001364}
1365
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001366static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Robert Carre0a353c2018-08-02 16:38:04 -07001367 jlong ptr, jobject inputChannelObj, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001368 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001369
1370 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1371 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001372 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001373 throwInputChannelNotInitialized(env);
1374 return;
1375 }
Robert Carre0a353c2018-08-02 16:38:04 -07001376 bool monitor = inputChannel->getToken() == nullptr && displayId != ADISPLAY_ID_NONE;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001377
Robert Carre0a353c2018-08-02 16:38:04 -07001378 status_t status = im->registerInputChannel(env, inputChannel, displayId);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001379
Jeff Brown46b9ac02010-04-22 18:58:52 -07001380 if (status) {
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001381 std::string message;
1382 message += StringPrintf("Failed to register input channel. status=%d", status);
1383 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001384 return;
1385 }
1386
Arthur Hungbe5ce212018-09-13 18:41:56 +08001387 // If inputWindowHandle is null and displayId >= 0, treat inputChannel as monitor.
Robert Carre0a353c2018-08-02 16:38:04 -07001388 if (!monitor) {
Jeff Browna41ca772010-08-11 14:46:32 -07001389 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001390 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001391 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001392}
1393
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001394static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001395 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001396 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001397
1398 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1399 inputChannelObj);
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001400 if (inputChannel == nullptr) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001401 throwInputChannelNotInitialized(env);
1402 return;
1403 }
1404
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001405 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, nullptr, nullptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001406
Jeff Brown4532e612012-04-05 14:27:12 -07001407 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001408 if (status && status != BAD_VALUE) { // ignore already unregistered channel
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001409 std::string message;
1410 message += StringPrintf("Failed to unregister input channel. status=%d", status);
1411 jniThrowRuntimeException(env, message.c_str());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001412 }
1413}
1414
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001415static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001416 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001417 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001418
Jeff Brown4532e612012-04-05 14:27:12 -07001419 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001420}
1421
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001422static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001423 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001424 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001425 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001426
Jeff Brown6ec402b2010-07-28 15:48:59 -07001427 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1428 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001429 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1430 if (status) {
1431 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1432 return INPUT_EVENT_INJECTION_FAILED;
1433 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001434
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001435 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001436 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001437 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001438 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001439 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1440 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001441 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1442 return INPUT_EVENT_INJECTION_FAILED;
1443 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001444
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001445 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001446 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001447 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001448 } else {
1449 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001450 return INPUT_EVENT_INJECTION_FAILED;
1451 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001452}
1453
Andrii Kulian112d0562016-03-08 10:44:22 -08001454static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1455 jlong ptr, jint deviceId) {
1456 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001457
Andrii Kulian112d0562016-03-08 10:44:22 -08001458 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1459}
1460
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001461static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Arthur Hung39134b22018-08-14 11:58:28 +08001462 jlong ptr, jobjectArray windowHandleObjArray, jint displayId) {
Jeff Brown4532e612012-04-05 14:27:12 -07001463 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001464
Arthur Hung39134b22018-08-14 11:58:28 +08001465 im->setInputWindows(env, windowHandleObjArray, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001466}
1467
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001468static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001469 jlong ptr, jint displayId, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001470 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001471
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001472 im->setFocusedApplication(env, displayId, applicationHandleObj);
1473}
1474
1475static void nativeSetFocusedDisplay(JNIEnv* env, jclass /* clazz */,
1476 jlong ptr, jint displayId) {
1477 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1478
1479 im->setFocusedDisplay(env, displayId);
Jeff Brown349703e2010-06-22 01:27:15 -07001480}
1481
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001482static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1483 jboolean enabled) {
1484 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001485
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001486 im->setPointerCapture(enabled);
1487}
1488
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001489static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1490 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001491 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001492
Jeff Brown4532e612012-04-05 14:27:12 -07001493 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001494}
1495
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001496static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1497 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001498 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001499
Jeff Brown4532e612012-04-05 14:27:12 -07001500 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001501}
1502
Jeff Brown4532e612012-04-05 14:27:12 -07001503static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001504 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001505 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001506
1507 sp<InputChannel> fromChannel =
1508 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1509 sp<InputChannel> toChannel =
1510 android_view_InputChannel_getInputChannel(env, toChannelObj);
1511
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001512 if (fromChannel == nullptr || toChannel == nullptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001513 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001514 }
1515
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001516 if (im->getInputManager()->getDispatcher()->
1517 transferTouchFocus(fromChannel, toChannel)) {
1518 return JNI_TRUE;
1519 } else {
1520 return JNI_FALSE;
1521 }
Jeff Browne6504122010-09-27 14:52:15 -07001522}
1523
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001524static void nativeSetPointerSpeed(JNIEnv* /* env */,
1525 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001526 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001527
Jeff Brown4532e612012-04-05 14:27:12 -07001528 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001529}
1530
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001531static void nativeSetShowTouches(JNIEnv* /* env */,
1532 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001533 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001534
Jeff Brown4532e612012-04-05 14:27:12 -07001535 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001536}
1537
Jeff Brown037c33e2014-04-09 00:31:55 -07001538static void nativeSetInteractive(JNIEnv* env,
1539 jclass clazz, jlong ptr, jboolean interactive) {
1540 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1541
1542 im->setInteractive(interactive);
1543}
1544
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001545static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1546 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001547
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001548 im->reloadCalibration();
1549}
1550
Jeff Browna47425a2012-04-13 04:09:27 -07001551static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001552 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001553 jint repeat, jint token) {
1554 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1555
1556 size_t patternSize = env->GetArrayLength(patternObj);
1557 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001558 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001559 "which is more than the maximum supported size of %d.",
1560 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1561 return; // limit to reasonable size
1562 }
1563
1564 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001565 patternObj, nullptr));
Jeff Browna47425a2012-04-13 04:09:27 -07001566 nsecs_t pattern[patternSize];
1567 for (size_t i = 0; i < patternSize; i++) {
1568 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001569 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001570 }
1571 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1572
1573 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1574}
1575
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001576static void nativeCancelVibrate(JNIEnv* /* env */,
1577 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001578 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1579
1580 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1581}
1582
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001583static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1584 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001585 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1586
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001587 im->getInputManager()->getReader()->requestRefreshConfiguration(
1588 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1589}
1590
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001591static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1592 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001593 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1594
1595 im->getInputManager()->getReader()->requestRefreshConfiguration(
1596 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001597}
1598
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001599static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001600 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001601
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001602 std::string dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001603 im->dump(dump);
Siarhei Vishniakou8e960d72017-11-22 19:12:22 -08001604 return env->NewStringUTF(dump.c_str());
Jeff Browne33348b2010-07-15 23:54:05 -07001605}
1606
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001607static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001608 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001609
Jeff Brown4532e612012-04-05 14:27:12 -07001610 im->getInputManager()->getReader()->monitor();
1611 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001612}
1613
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001614static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1615 jclass /* clazz */, jlong ptr, jint deviceId) {
1616 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1617
1618 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1619}
1620
1621static void nativeEnableInputDevice(JNIEnv* /* env */,
1622 jclass /* clazz */, jlong ptr, jint deviceId) {
1623 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1624
1625 im->setInputDeviceEnabled(deviceId, true);
1626}
1627
1628static void nativeDisableInputDevice(JNIEnv* /* env */,
1629 jclass /* clazz */, jlong ptr, jint deviceId) {
1630 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1631
1632 im->setInputDeviceEnabled(deviceId, false);
1633}
1634
Michael Wrighte051f6f2016-05-13 17:44:16 +01001635static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001636 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001637
Michael Wrighte051f6f2016-05-13 17:44:16 +01001638 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001639}
1640
Jun Mukai19a56012015-11-24 11:25:52 -08001641static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1642 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001643
Jun Mukai19a56012015-11-24 11:25:52 -08001644 im->reloadPointerIcons();
1645}
1646
Jun Mukaid4eaef72015-10-30 15:54:33 -07001647static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1648 jlong ptr, jobject iconObj) {
1649 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1650
1651 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001652 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1653 if (result) {
1654 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1655 return;
1656 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001657
1658 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001659 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1660 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1661 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1662 spriteIcon.bitmap.rowBytes(), 0, 0);
1663 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001664 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1665 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1666 im->setCustomPointerIcon(spriteIcon);
1667}
1668
Jeff Brown9c3cda02010-06-15 01:31:58 -07001669// ----------------------------------------------------------------------------
1670
Daniel Micay76f6a862015-09-19 17:31:01 -04001671static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001672 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001673 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001674 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001675 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001676 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001677 (void*) nativeStart },
Siarhei Vishniakou2eb0f8f2018-07-06 23:30:12 +01001678 { "nativeSetDisplayViewports", "(J[Landroid/hardware/display/DisplayViewport;)V",
1679 (void*) nativeSetDisplayViewports },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001680 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001681 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001682 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001683 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001684 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001685 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001686 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001687 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001688 { "nativeRegisterInputChannel",
Robert Carre0a353c2018-08-02 16:38:04 -07001689 "(JLandroid/view/InputChannel;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001690 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001691 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001692 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001693 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001694 (void*) nativeSetInputFilterEnabled },
Siarhei Vishniakou85ddfff2018-01-31 16:49:36 -08001695 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001696 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001697 { "nativeToggleCapsLock", "(JI)V",
1698 (void*) nativeToggleCapsLock },
Robert Carr788f5742018-07-30 17:46:45 -07001699 { "nativeSetInputWindows", "(J[Landroid/view/InputWindowHandle;I)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001700 (void*) nativeSetInputWindows },
Robert Carr788f5742018-07-30 17:46:45 -07001701 { "nativeSetFocusedApplication", "(JILandroid/view/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001702 (void*) nativeSetFocusedApplication },
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001703 { "nativeSetFocusedDisplay", "(JI)V",
1704 (void*) nativeSetFocusedDisplay },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001705 { "nativeSetPointerCapture", "(JZ)V",
1706 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001707 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001708 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001709 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001710 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001711 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001712 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001713 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001714 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001715 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001716 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001717 { "nativeSetInteractive", "(JZ)V",
1718 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001719 { "nativeReloadCalibration", "(J)V",
1720 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001721 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001722 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001723 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001724 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001725 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001726 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001727 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001728 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001729 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001730 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001731 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001732 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001733 { "nativeIsInputDeviceEnabled", "(JI)Z",
1734 (void*) nativeIsInputDeviceEnabled },
1735 { "nativeEnableInputDevice", "(JI)V",
1736 (void*) nativeEnableInputDevice },
1737 { "nativeDisableInputDevice", "(JI)V",
1738 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001739 { "nativeSetPointerIconType", "(JI)V",
1740 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001741 { "nativeReloadPointerIcons", "(J)V",
1742 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001743 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1744 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001745};
1746
1747#define FIND_CLASS(var, className) \
1748 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001749 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001750
1751#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1752 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001753 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001754
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -07001755#define GET_STATIC_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1756 var = env->GetStaticMethodID(clazz, methodName, methodDescriptor); \
1757 LOG_FATAL_IF(! (var), "Unable to find static method " methodName);
1758
Jeff Brown46b9ac02010-04-22 18:58:52 -07001759#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1760 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001761 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001762
1763int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001764 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001765 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001766 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001767 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1768
Jeff Brown9c3cda02010-06-15 01:31:58 -07001769 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001770
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001771 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001772 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Siarhei Vishniakouf9868962018-11-30 09:45:52 -08001773 gServiceClassInfo.clazz = reinterpret_cast<jclass>(env->NewGlobalRef(clazz));
Jeff Brown46b9ac02010-04-22 18:58:52 -07001774
Jeff Brown4532e612012-04-05 14:27:12 -07001775 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001776 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001777
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001778 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1779 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1780
Jeff Brown53384282012-08-20 20:16:01 -07001781 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1782 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001783
Jeff Brown4532e612012-04-05 14:27:12 -07001784 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
Robert Carre0a353c2018-08-02 16:38:04 -07001785 "notifyInputChannelBroken", "(Landroid/os/IBinder;)V");
Robert Carrbd26e652018-08-21 15:46:28 -07001786
1787 GET_METHOD_ID(gServiceClassInfo.notifyFocusChanged, clazz,
1788 "notifyFocusChanged", "(Landroid/os/IBinder;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001789
Jeff Brown4532e612012-04-05 14:27:12 -07001790 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001791 "notifyANR",
Robert Carr679ccb02018-08-08 15:32:35 -07001792 "(Landroid/os/IBinder;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001793
Jeff Brown4532e612012-04-05 14:27:12 -07001794 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001795 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1796
Jeff Brown4532e612012-04-05 14:27:12 -07001797 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001798 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001799
Michael Wright70af00a2014-09-03 19:30:20 -07001800 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1801 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001802
Jeff Brown4532e612012-04-05 14:27:12 -07001803 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001804 "interceptKeyBeforeDispatching",
Robert Carre0a353c2018-08-02 16:38:04 -07001805 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001806
Jeff Brown4532e612012-04-05 14:27:12 -07001807 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001808 "dispatchUnhandledKey",
Robert Carre0a353c2018-08-02 16:38:04 -07001809 "(Landroid/os/IBinder;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001810
Jeff Brown4532e612012-04-05 14:27:12 -07001811 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001812 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001813
Jeff Brown4532e612012-04-05 14:27:12 -07001814 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001815 "getVirtualKeyQuietTimeMillis", "()I");
1816
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -07001817 GET_STATIC_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001818 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1819
Siarhei Vishniakou15a412d2018-10-04 19:01:04 -07001820 GET_STATIC_METHOD_ID(gServiceClassInfo.getInputPortAssociations, clazz,
1821 "getInputPortAssociations", "()[Ljava/lang/String;");
1822
Jeff Brown4532e612012-04-05 14:27:12 -07001823 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001824 "getKeyRepeatTimeout", "()I");
1825
Jeff Brown4532e612012-04-05 14:27:12 -07001826 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001827 "getKeyRepeatDelay", "()I");
1828
Jeff Brown4532e612012-04-05 14:27:12 -07001829 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001830 "getHoverTapTimeout", "()I");
1831
Jeff Brown4532e612012-04-05 14:27:12 -07001832 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001833 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001834
Jeff Brown4532e612012-04-05 14:27:12 -07001835 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001836 "getDoubleTapTimeout", "()I");
1837
Jeff Brown4532e612012-04-05 14:27:12 -07001838 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001839 "getLongPressTimeout", "()I");
1840
Jeff Brown4532e612012-04-05 14:27:12 -07001841 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001842 "getPointerLayer", "()I");
1843
Jeff Brown4532e612012-04-05 14:27:12 -07001844 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001845 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001846
Arthur Hungb9b32002018-12-18 17:39:43 +08001847 GET_METHOD_ID(gServiceClassInfo.getPointerDisplayId, clazz,
1848 "getPointerDisplayId", "()I");
1849
Jeff Brown6ec6f792012-04-17 16:52:41 -07001850 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001851 "getKeyboardLayoutOverlay",
1852 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001853
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001854 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1855 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1856
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001857 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1858 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001859 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001860
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001861 // InputDevice
1862
1863 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1864 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1865
Jeff Brown6ec402b2010-07-28 15:48:59 -07001866 // KeyEvent
1867
1868 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001869 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1870
Jeff Brown8d608662010-08-30 03:02:23 -07001871 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001872
1873 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001874 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001875
RoboErikfb290df2013-12-16 11:27:55 -08001876 // InputDeviceIdentifier
1877
1878 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1879 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1880 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1881 "<init>", "(Ljava/lang/String;II)V");
1882
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001883 // TouchCalibration
1884
1885 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1886 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1887
1888 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1889 "getAffineTransform", "()[F");
1890
Jeff Brown46b9ac02010-04-22 18:58:52 -07001891 return 0;
1892}
1893
Jeff Brown46b9ac02010-04-22 18:58:52 -07001894} /* namespace android */