blob: 14a2381c2b7eb6fed752b38573a3b703273dca3c [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
Jeff Brown9c3cda02010-06-15 01:31:58 -070019//#define LOG_NDEBUG 0
20
21// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070022#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070023
24// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070026
Jeff Brown83c09682010-12-23 17:50:18 -080027
Jeff Brown46b9ac02010-04-22 18:58:52 -070028#include "JNIHelp.h"
29#include "jni.h"
Michael Wrighta4051212015-07-23 17:04:40 +010030#include <atomic>
31#include <cinttypes>
Jeff Brown349703e2010-06-22 01:27:15 -070032#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070033#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070034#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080035
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080037#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070038#include <utils/threads.h>
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -070039#include <utils/SortedVector.h>
Jeff Brown83c09682010-12-23 17:50:18 -080040
Jeff Brownb4ff35d2011-01-02 16:37:43 -080041#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070042#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080043
Michael Wrightd6b473712014-02-10 15:56:36 -080044#include <inputflinger/InputManager.h>
45
Jeff Brown05dc66a2011-03-02 14:41:58 -080046#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070047#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080048#include <android_view_KeyEvent.h>
49#include <android_view_MotionEvent.h>
50#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070051#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080052#include <android/graphics/GraphicsJNI.h>
53
Jeff Brown6ec6f792012-04-17 16:52:41 -070054#include <ScopedLocalRef.h>
Jason Gerecke857aa7b2014-01-27 18:34:20 -080055#include <ScopedPrimitiveArray.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070056#include <ScopedUtfChars.h>
57
Jeff Brown4f8ecd82012-06-18 18:29:13 -070058#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070059#include "com_android_server_input_InputApplicationHandle.h"
60#include "com_android_server_input_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070061
Michael Wrighta4051212015-07-23 17:04:40 +010062#define INDENT " "
63
Jeff Brown46b9ac02010-04-22 18:58:52 -070064namespace android {
65
Jeff Brown1a84fd12011-06-02 01:26:32 -070066// The exponent used to calculate the pointer speed scaling factor.
67// The scaling factor is calculated as 2 ^ (speed * exponent),
68// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070069static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070070
Jeff Brown46b9ac02010-04-22 18:58:52 -070071static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070072 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070073 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070074 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070075 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070076 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070077 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070078 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070079 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070080 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070081 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070082 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080083 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070084 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080085 jmethodID getKeyRepeatTimeout;
86 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070087 jmethodID getHoverTapTimeout;
88 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070089 jmethodID getDoubleTapTimeout;
90 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080091 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080092 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070093 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070094 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -080095 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -070096} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070097
98static struct {
99 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700100} gInputDeviceClassInfo;
101
102static struct {
103 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700104} gKeyEventClassInfo;
105
106static struct {
107 jclass clazz;
108} gMotionEventClassInfo;
109
RoboErikfb290df2013-12-16 11:27:55 -0800110static struct {
111 jclass clazz;
112 jmethodID constructor;
113} gInputDeviceIdentifierInfo;
114
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800115static struct {
116 jclass clazz;
117 jmethodID getAffineTransform;
118} gTouchCalibrationClassInfo;
119
RoboErikfb290df2013-12-16 11:27:55 -0800120
Jeff Brown928e0542011-01-10 11:17:36 -0800121
122// --- Global functions ---
123
Jeff Brown214eaf42011-05-26 19:17:02 -0700124template<typename T>
125inline static T min(const T& a, const T& b) {
126 return a < b ? a : b;
127}
128
129template<typename T>
130inline static T max(const T& a, const T& b) {
131 return a > b ? a : b;
132}
133
Michael Wrighta4051212015-07-23 17:04:40 +0100134static inline const char* toString(bool value) {
135 return value ? "true" : "false";
136}
137
Jeff Brown928e0542011-01-10 11:17:36 -0800138static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
139 const sp<InputApplicationHandle>& inputApplicationHandle) {
140 if (inputApplicationHandle == NULL) {
141 return NULL;
142 }
143 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
144 getInputApplicationHandleObjLocalRef(env);
145}
146
147static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
148 const sp<InputWindowHandle>& inputWindowHandle) {
149 if (inputWindowHandle == NULL) {
150 return NULL;
151 }
152 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
153 getInputWindowHandleObjLocalRef(env);
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
Jeff Brown928e0542011-01-10 11:17:36 -0800182
183// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800184
Jeff Brown9c3cda02010-06-15 01:31:58 -0700185class NativeInputManager : public virtual RefBase,
186 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700187 public virtual InputDispatcherPolicyInterface,
188 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700189protected:
190 virtual ~NativeInputManager();
191
192public:
Jeff Brown4532e612012-04-05 14:27:12 -0700193 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700194
195 inline sp<InputManager> getInputManager() const { return mInputManager; }
196
Jeff Brownb88102f2010-09-08 11:49:43 -0700197 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700198
Jeff Brownd728bf52012-09-08 18:05:28 -0700199 void setDisplayViewport(bool external, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700200
Jeff Brown7fbdc842010-06-17 20:52:56 -0700201 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800202 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700203 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
204
Jeff Brown9302c872011-07-13 22:51:29 -0700205 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
206 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700207 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800208 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700209 void setPointerSpeed(int32_t speed);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700210 void setInputDeviceEnabled(uint32_t deviceId, bool enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -0700211 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700212 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800213 void reloadCalibration();
Michael Wrighte051f6f2016-05-13 17:44:16 +0100214 void setPointerIconType(int32_t iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800215 void reloadPointerIcons();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700216 void setCustomPointerIcon(const SpriteIcon& icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800217 void setPointerCapture(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700218
Jeff Brown9c3cda02010-06-15 01:31:58 -0700219 /* --- InputReaderPolicyInterface implementation --- */
220
Jeff Brown214eaf42011-05-26 19:17:02 -0700221 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800222 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700223 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800224 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700225 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700226 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
227 jfloatArray matrixArr);
228 virtual TouchAffineTransformation getTouchAffineTransformation(
229 const String8& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700230
231 /* --- InputDispatcherPolicyInterface implementation --- */
232
Jeff Brownbcc046a2012-09-27 20:46:43 -0700233 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700234 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700235 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700236 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700237 const sp<InputWindowHandle>& inputWindowHandle,
238 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800239 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700240 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700241 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800242 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800243 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700244 virtual nsecs_t interceptKeyBeforeDispatching(
245 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700246 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800247 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800248 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700249 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700250 virtual bool checkInjectEventsPermissionNonReentrant(
251 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700252
Jeff Brown2352b972011-04-12 22:39:53 -0700253 /* --- PointerControllerPolicyInterface implementation --- */
254
Jun Mukai19a56012015-11-24 11:25:52 -0800255 virtual void loadPointerIcon(SpriteIcon* icon);
Jeff Brown2352b972011-04-12 22:39:53 -0700256 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai808196f2015-10-28 16:46:44 -0700257 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
258 std::map<int32_t, PointerAnimation>* outAnimationResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900259 virtual int32_t getDefaultPointerIconId();
Jun Mukaid4eaef72015-10-30 15:54:33 -0700260 virtual int32_t getCustomPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700261
Jeff Brown9c3cda02010-06-15 01:31:58 -0700262private:
263 sp<InputManager> mInputManager;
264
Jeff Brown2352b972011-04-12 22:39:53 -0700265 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700266 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800267 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700268
Jeff Brown83c09682010-12-23 17:50:18 -0800269 Mutex mLock;
270 struct Locked {
271 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700272 DisplayViewport internalViewport;
273 DisplayViewport externalViewport;
Jeff Brown83c09682010-12-23 17:50:18 -0800274
Jeff Brown05dc66a2011-03-02 14:41:58 -0800275 // System UI visibility.
276 int32_t systemUiVisibility;
277
Jeff Brown1a84fd12011-06-02 01:26:32 -0700278 // Pointer speed.
279 int32_t pointerSpeed;
280
Jeff Brown474dcb52011-06-14 20:22:50 -0700281 // True if pointer gestures are enabled.
282 bool pointerGesturesEnabled;
283
Jeff Browndaf4a122011-08-26 17:14:14 -0700284 // Show touches feature enable/disable.
285 bool showTouches;
286
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800287 // Pointer capture feature enable/disable.
288 bool pointerCapture;
289
Jeff Brown5541de92011-04-11 11:54:25 -0700290 // Sprite controller singleton, created on first use.
291 sp<SpriteController> spriteController;
292
Jeff Brown83c09682010-12-23 17:50:18 -0800293 // Pointer controller singleton, created and destroyed as needed.
294 wp<PointerController> pointerController;
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700295
296 // Input devices to be disabled
297 SortedVector<int32_t> disabledInputDevices;
Jeff Brown83c09682010-12-23 17:50:18 -0800298 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700299
Michael Wrighta4051212015-07-23 17:04:40 +0100300 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700301
Jeff Brown2352b972011-04-12 22:39:53 -0700302 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800303 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700304 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800305
Jeff Brownb88102f2010-09-08 11:49:43 -0700306 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700307
Jeff Brown9c3cda02010-06-15 01:31:58 -0700308 static inline JNIEnv* jniEnv() {
309 return AndroidRuntime::getJNIEnv();
310 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700311};
312
Jeff Brown928e0542011-01-10 11:17:36 -0800313
Jeff Brown9c3cda02010-06-15 01:31:58 -0700314
Jeff Brown2352b972011-04-12 22:39:53 -0700315NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700316 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700317 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700318 JNIEnv* env = jniEnv();
319
Jeff Brown2352b972011-04-12 22:39:53 -0700320 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700321 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700322
Jeff Brown83c09682010-12-23 17:50:18 -0800323 {
324 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800325 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700326 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700327 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700328 mLocked.showTouches = false;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800329 mLocked.pointerCapture = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800330 }
Michael Wrighta4051212015-07-23 17:04:40 +0100331 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800332
Jeff Brown9c3cda02010-06-15 01:31:58 -0700333 sp<EventHub> eventHub = new EventHub();
334 mInputManager = new InputManager(eventHub, this, this);
335}
336
337NativeInputManager::~NativeInputManager() {
338 JNIEnv* env = jniEnv();
339
Jeff Brown2352b972011-04-12 22:39:53 -0700340 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700341 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700342}
343
Jeff Brownb88102f2010-09-08 11:49:43 -0700344void NativeInputManager::dump(String8& dump) {
Michael Wrighta4051212015-07-23 17:04:40 +0100345 dump.append("Input Manager State:\n");
346 {
347 dump.appendFormat(INDENT "Interactive: %s\n", toString(mInteractive.load()));
348 }
349 {
350 AutoMutex _l(mLock);
351 dump.appendFormat(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
352 mLocked.systemUiVisibility);
353 dump.appendFormat(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
354 dump.appendFormat(INDENT "Pointer Gestures Enabled: %s\n",
355 toString(mLocked.pointerGesturesEnabled));
356 dump.appendFormat(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800357 dump.appendFormat(INDENT "Pointer Capture Enabled: %s\n", toString(mLocked.pointerCapture));
Michael Wrighta4051212015-07-23 17:04:40 +0100358 }
359 dump.append("\n");
360
Jeff Brownb88102f2010-09-08 11:49:43 -0700361 mInputManager->getReader()->dump(dump);
362 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700363
Jeff Brownb88102f2010-09-08 11:49:43 -0700364 mInputManager->getDispatcher()->dump(dump);
365 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700366}
367
Jeff Brown7fbdc842010-06-17 20:52:56 -0700368bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700369 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000370 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700371 LOGE_EX(env);
372 env->ExceptionClear();
373 return true;
374 }
375 return false;
376}
377
Jeff Brownd728bf52012-09-08 18:05:28 -0700378void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700379 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700380 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700381 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700382
Jeff Brownd728bf52012-09-08 18:05:28 -0700383 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
384 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700385 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700386 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700387
Jeff Brownd728bf52012-09-08 18:05:28 -0700388 if (!external) {
389 sp<PointerController> controller = mLocked.pointerController.promote();
390 if (controller != NULL) {
391 controller->setDisplayViewport(
392 viewport.logicalRight - viewport.logicalLeft,
393 viewport.logicalBottom - viewport.logicalTop,
394 viewport.orientation);
395 }
Jeff Brown2352b972011-04-12 22:39:53 -0700396 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700397 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700398 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700399
400 if (changed) {
401 mInputManager->getReader()->requestRefreshConfiguration(
402 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
403 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700404}
405
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700406status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Jeff Brown928e0542011-01-10 11:17:36 -0800407 const sp<InputChannel>& inputChannel,
408 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
409 return mInputManager->getDispatcher()->registerInputChannel(
410 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700411}
412
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700413status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700414 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700415 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700416}
417
Jeff Brown214eaf42011-05-26 19:17:02 -0700418void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700419 JNIEnv* env = jniEnv();
420
Jeff Brown4532e612012-04-05 14:27:12 -0700421 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
422 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700423 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
424 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
425 }
426
427 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700428 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
429 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700430 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
431 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700432 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700433 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700434 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700435 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700436 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700437 env->DeleteLocalRef(item);
438 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700439 env->DeleteLocalRef(excludedDeviceNames);
440 }
441
Jeff Brown4532e612012-04-05 14:27:12 -0700442 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
443 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700444 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700445 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
446 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700447 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700448 jint longPressTimeout = env->CallIntMethod(mServiceObj,
449 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700450 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700451 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700452
453 // We must ensure that the tap-drag interval is significantly shorter than
454 // the long-press timeout because the tap is held down for the entire duration
455 // of the double-tap timeout.
456 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700457 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700458 outConfig->pointerGestureTapDragInterval =
459 milliseconds_to_nanoseconds(tapDragInterval);
460 }
461 }
462 }
463
Jeff Brown4532e612012-04-05 14:27:12 -0700464 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
465 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700466 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
467 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700468 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700469
470 { // acquire lock
471 AutoMutex _l(mLock);
472
473 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
474 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700475 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700476
Jeff Browndaf4a122011-08-26 17:14:14 -0700477 outConfig->showTouches = mLocked.showTouches;
478
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800479 outConfig->pointerCapture = mLocked.pointerCapture;
480
Jeff Brownd728bf52012-09-08 18:05:28 -0700481 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
482 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700483
484 outConfig->disabledDevices = mLocked.disabledInputDevices;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700485 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700486}
487
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700488sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Jeff Brown83c09682010-12-23 17:50:18 -0800489 AutoMutex _l(mLock);
490
491 sp<PointerController> controller = mLocked.pointerController.promote();
492 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700493 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800494
Jeff Brown2352b972011-04-12 22:39:53 -0700495 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800496 mLocked.pointerController = controller;
497
Jeff Brownd728bf52012-09-08 18:05:28 -0700498 DisplayViewport& v = mLocked.internalViewport;
499 controller->setDisplayViewport(
500 v.logicalRight - v.logicalLeft,
501 v.logicalBottom - v.logicalTop,
502 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800503
Jeff Brown2352b972011-04-12 22:39:53 -0700504 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800505 }
506 return controller;
507}
508
Jeff Brown5541de92011-04-11 11:54:25 -0700509void NativeInputManager::ensureSpriteControllerLocked() {
510 if (mLocked.spriteController == NULL) {
511 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700512 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700513 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
514 layer = -1;
515 }
516 mLocked.spriteController = new SpriteController(mLooper, layer);
517 }
518}
519
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700520void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
521 JNIEnv* env = jniEnv();
522
523 size_t count = inputDevices.size();
524 jobjectArray inputDevicesObjArray = env->NewObjectArray(
525 count, gInputDeviceClassInfo.clazz, NULL);
526 if (inputDevicesObjArray) {
527 bool error = false;
528 for (size_t i = 0; i < count; i++) {
529 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
530 if (!inputDeviceObj) {
531 error = true;
532 break;
533 }
534
535 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
536 env->DeleteLocalRef(inputDeviceObj);
537 }
538
539 if (!error) {
540 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
541 inputDevicesObjArray);
542 }
543
544 env->DeleteLocalRef(inputDevicesObjArray);
545 }
546
547 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
548}
549
Jeff Brown6ec6f792012-04-17 16:52:41 -0700550sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800551 const InputDeviceIdentifier& identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700552 JNIEnv* env = jniEnv();
553
554 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800555 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
556 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
557 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
558 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700559 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800560 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700561 if (arrayObj.get()) {
562 ScopedLocalRef<jstring> filenameObj(env,
563 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
564 ScopedLocalRef<jstring> contentsObj(env,
565 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
566 ScopedUtfChars filenameChars(env, filenameObj.get());
567 ScopedUtfChars contentsChars(env, contentsObj.get());
568
569 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
570 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
571 }
572 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
573 return result;
574}
575
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700576String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
577 JNIEnv* env = jniEnv();
578
579 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
580 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
581 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
582 String8 result;
583 if (aliasObj.get()) {
584 ScopedUtfChars aliasChars(env, aliasObj.get());
585 result.setTo(aliasChars.c_str());
586 }
587 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
588 return result;
589}
590
Jeff Brownbcc046a2012-09-27 20:46:43 -0700591void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700592 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700593#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700594 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
595 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700596#endif
597
598 JNIEnv* env = jniEnv();
599
Jeff Brown53384282012-08-20 20:16:01 -0700600 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700601 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700602 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700603}
604
Jeff Brown9c3cda02010-06-15 01:31:58 -0700605void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
606#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000607 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700608#endif
609
610 JNIEnv* env = jniEnv();
611
Jeff Brown4532e612012-04-05 14:27:12 -0700612 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700613 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700614}
615
Jeff Brown519e0242010-09-15 15:18:56 -0700616nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700617 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700618#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000619 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700620#endif
621
622 JNIEnv* env = jniEnv();
623
Jeff Brown928e0542011-01-10 11:17:36 -0800624 jobject inputApplicationHandleObj =
625 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
626 jobject inputWindowHandleObj =
627 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700628 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700629
Jeff Brown4532e612012-04-05 14:27:12 -0700630 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700631 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
632 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700633 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
634 newTimeout = 0; // abort dispatch
635 } else {
636 assert(newTimeout >= 0);
637 }
638
Jeff Brownbd181bb2013-09-10 16:44:24 -0700639 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800640 env->DeleteLocalRef(inputWindowHandleObj);
641 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700642 return newTimeout;
643}
644
Jeff Brown928e0542011-01-10 11:17:36 -0800645void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700646#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000647 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700648#endif
649
Jeff Brown7fbdc842010-06-17 20:52:56 -0700650 JNIEnv* env = jniEnv();
651
Jeff Brown928e0542011-01-10 11:17:36 -0800652 jobject inputWindowHandleObj =
653 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
654 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700655 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800656 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700657 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
658
Jeff Brown928e0542011-01-10 11:17:36 -0800659 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700660 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700661}
662
Jeff Brown214eaf42011-05-26 19:17:02 -0700663void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
664 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800665
Jeff Brown4532e612012-04-05 14:27:12 -0700666 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
667 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700668 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
669 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
670 }
Jeff Browna4547672011-03-02 21:38:11 -0800671
Jeff Brown4532e612012-04-05 14:27:12 -0700672 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
673 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700674 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
675 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
676 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700677}
678
Jeff Brown9302c872011-07-13 22:51:29 -0700679void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
680 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700681
Jeff Brown9302c872011-07-13 22:51:29 -0700682 if (windowHandleObjArray) {
683 jsize length = env->GetArrayLength(windowHandleObjArray);
684 for (jsize i = 0; i < length; i++) {
685 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
686 if (! windowHandleObj) {
687 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700688 }
Jeff Brown9302c872011-07-13 22:51:29 -0700689
690 sp<InputWindowHandle> windowHandle =
691 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
692 if (windowHandle != NULL) {
693 windowHandles.push(windowHandle);
694 }
695 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700696 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700697 }
Jeff Brown349703e2010-06-22 01:27:15 -0700698
Jeff Brown9302c872011-07-13 22:51:29 -0700699 mInputManager->getDispatcher()->setInputWindows(windowHandles);
700
701 // Do this after the dispatcher has updated the window handle state.
702 bool newPointerGesturesEnabled = true;
703 size_t numWindows = windowHandles.size();
704 for (size_t i = 0; i < numWindows; i++) {
705 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700706 const InputWindowInfo* windowInfo = windowHandle->getInfo();
707 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
708 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700709 newPointerGesturesEnabled = false;
710 }
711 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700712
713 uint32_t changes = 0;
714 { // acquire lock
715 AutoMutex _l(mLock);
716
717 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
718 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
719 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
720 }
721 } // release lock
722
723 if (changes) {
724 mInputManager->getReader()->requestRefreshConfiguration(changes);
725 }
Jeff Brown349703e2010-06-22 01:27:15 -0700726}
727
Jeff Brown9302c872011-07-13 22:51:29 -0700728void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
729 sp<InputApplicationHandle> applicationHandle =
730 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
731 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700732}
733
734void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700735 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700736}
737
Jeff Brown05dc66a2011-03-02 14:41:58 -0800738void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
739 AutoMutex _l(mLock);
740
741 if (mLocked.systemUiVisibility != visibility) {
742 mLocked.systemUiVisibility = visibility;
743
744 sp<PointerController> controller = mLocked.pointerController.promote();
745 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700746 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800747 }
748 }
749}
750
Jeff Brown2352b972011-04-12 22:39:53 -0700751void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800752 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700753 controller->setInactivityTimeout(lightsOut
754 ? PointerController::INACTIVITY_TIMEOUT_SHORT
755 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800756}
757
Jeff Brown1a84fd12011-06-02 01:26:32 -0700758void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700759 { // acquire lock
760 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700761
Jeff Brown474dcb52011-06-14 20:22:50 -0700762 if (mLocked.pointerSpeed == speed) {
763 return;
764 }
765
Steve Block6215d3f2012-01-04 20:05:49 +0000766 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700767 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700768 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700769
Jeff Brown474dcb52011-06-14 20:22:50 -0700770 mInputManager->getReader()->requestRefreshConfiguration(
771 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700772}
773
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700774void NativeInputManager::setInputDeviceEnabled(uint32_t deviceId, bool enabled) {
775 { // acquire lock
776 AutoMutex _l(mLock);
777
778 ssize_t index = mLocked.disabledInputDevices.indexOf(deviceId);
779 bool currentlyEnabled = index < 0;
780 if (!enabled && currentlyEnabled) {
781 mLocked.disabledInputDevices.add(deviceId);
782 }
783 if (enabled && !currentlyEnabled) {
784 mLocked.disabledInputDevices.remove(deviceId);
785 }
786 } // release lock
787
788 mInputManager->getReader()->requestRefreshConfiguration(
789 InputReaderConfiguration::CHANGE_ENABLED_STATE);
790}
791
Jeff Browndaf4a122011-08-26 17:14:14 -0700792void NativeInputManager::setShowTouches(bool enabled) {
793 { // acquire lock
794 AutoMutex _l(mLock);
795
796 if (mLocked.showTouches == enabled) {
797 return;
798 }
799
Steve Block6215d3f2012-01-04 20:05:49 +0000800 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700801 mLocked.showTouches = enabled;
802 } // release lock
803
804 mInputManager->getReader()->requestRefreshConfiguration(
805 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
806}
807
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800808void NativeInputManager::setPointerCapture(bool enabled) {
809 { // acquire lock
810 AutoMutex _l(mLock);
811
812 if (mLocked.pointerCapture == enabled) {
813 return;
814 }
815
816 ALOGI("Setting pointer capture to %s.", enabled ? "enabled" : "disabled");
817 mLocked.pointerCapture = enabled;
818 } // release lock
819
820 mInputManager->getReader()->requestRefreshConfiguration(
821 InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
822}
823
Jeff Brown037c33e2014-04-09 00:31:55 -0700824void NativeInputManager::setInteractive(bool interactive) {
825 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700826}
827
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800828void NativeInputManager::reloadCalibration() {
829 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100830 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800831}
832
Michael Wrighte051f6f2016-05-13 17:44:16 +0100833void NativeInputManager::setPointerIconType(int32_t iconId) {
Jun Mukai19a56012015-11-24 11:25:52 -0800834 AutoMutex _l(mLock);
835 sp<PointerController> controller = mLocked.pointerController.promote();
836 if (controller != NULL) {
Michael Wrighte051f6f2016-05-13 17:44:16 +0100837 controller->updatePointerIcon(iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800838 }
839}
840
841void NativeInputManager::reloadPointerIcons() {
842 AutoMutex _l(mLock);
843 sp<PointerController> controller = mLocked.pointerController.promote();
844 if (controller != NULL) {
845 controller->reloadPointerResources();
846 }
Jun Mukai1db53972015-09-11 18:08:31 -0700847}
848
Jun Mukaid4eaef72015-10-30 15:54:33 -0700849void NativeInputManager::setCustomPointerIcon(const SpriteIcon& icon) {
850 AutoMutex _l(mLock);
851 sp<PointerController> controller = mLocked.pointerController.promote();
852 if (controller != NULL) {
853 controller->setCustomPointerIcon(icon);
854 }
855}
856
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800857TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
858 JNIEnv *env, jfloatArray matrixArr) {
859 ScopedFloatArrayRO matrix(env, matrixArr);
860 assert(matrix.size() == 6);
861
862 TouchAffineTransformation transform;
863 transform.x_scale = matrix[0];
864 transform.x_ymix = matrix[1];
865 transform.x_offset = matrix[2];
866 transform.y_xmix = matrix[3];
867 transform.y_scale = matrix[4];
868 transform.y_offset = matrix[5];
869
870 return transform;
871}
872
873TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700874 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800875 JNIEnv* env = jniEnv();
876
877 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
878
879 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700880 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
881 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800882
883 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
884 gTouchCalibrationClassInfo.getAffineTransform));
885
886 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
887
888 env->DeleteLocalRef(matrixArr);
889 env->DeleteLocalRef(cal);
890
891 return transform;
892}
893
Jeff Brown0029c662011-03-30 02:25:18 -0700894bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
895 jobject inputEventObj;
896
897 JNIEnv* env = jniEnv();
898 switch (inputEvent->getType()) {
899 case AINPUT_EVENT_TYPE_KEY:
900 inputEventObj = android_view_KeyEvent_fromNative(env,
901 static_cast<const KeyEvent*>(inputEvent));
902 break;
903 case AINPUT_EVENT_TYPE_MOTION:
904 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
905 static_cast<const MotionEvent*>(inputEvent));
906 break;
907 default:
908 return true; // dispatch the event normally
909 }
910
911 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000912 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700913 return true; // dispatch the event normally
914 }
915
916 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700917 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700918 inputEventObj, policyFlags);
919 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
920 pass = true;
921 }
922 env->DeleteLocalRef(inputEventObj);
923 return pass;
924}
925
Jeff Brown1f245102010-11-18 20:53:46 -0800926void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
927 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700928 // Policy:
929 // - Ignore untrusted events and pass them along.
930 // - Ask the window manager what to do with normal events and trusted injected events.
931 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100932 bool interactive = mInteractive.load();
933 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700934 policyFlags |= POLICY_FLAG_INTERACTIVE;
935 }
Jeff Brown3122e442010-10-11 23:32:49 -0700936 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800937 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700938 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800939 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
940 jint wmActions;
941 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700942 wmActions = env->CallIntMethod(mServiceObj,
943 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700944 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800945 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
946 wmActions = 0;
947 }
948 android_view_KeyEvent_recycle(env, keyEventObj);
949 env->DeleteLocalRef(keyEventObj);
950 } else {
Steve Block3762c312012-01-06 19:20:56 +0000951 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700952 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700953 }
954
Jeff Brown56194eb2011-03-02 19:23:13 -0800955 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700956 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100957 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -0700958 policyFlags |= POLICY_FLAG_PASS_TO_USER;
959 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700960 }
961}
962
Jeff Brown56194eb2011-03-02 19:23:13 -0800963void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700964 // Policy:
965 // - Ignore untrusted events and pass them along.
966 // - No special filtering for injected events required at this time.
967 // - Filter normal events based on screen state.
968 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100969 bool interactive = mInteractive.load();
970 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700971 policyFlags |= POLICY_FLAG_INTERACTIVE;
972 }
Jeff Brown3122e442010-10-11 23:32:49 -0700973 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700974 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -0700975 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -0700976 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -0800977 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700978 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -0700979 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -0800980 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800981 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -0700982 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800983 wmActions = 0;
984 }
985
Jeff Brown56194eb2011-03-02 19:23:13 -0800986 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700987 }
Jeff Brown3122e442010-10-11 23:32:49 -0700988 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100989 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -0700990 policyFlags |= POLICY_FLAG_PASS_TO_USER;
991 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700992 }
993}
994
Jeff Brown56194eb2011-03-02 19:23:13 -0800995void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
996 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800997 if (wmActions & WM_ACTION_PASS_TO_USER) {
998 policyFlags |= POLICY_FLAG_PASS_TO_USER;
999 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -08001000#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +00001001 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -08001002#endif
1003 }
1004}
1005
Jeff Brown905805a2011-10-12 13:57:59 -07001006nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -08001007 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -07001008 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -07001009 // Policy:
1010 // - Ignore untrusted events and pass them along.
1011 // - Filter normal events and trusted injected events through the window manager policy to
1012 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -07001013 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -07001014 if (policyFlags & POLICY_FLAG_TRUSTED) {
1015 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -07001016
Jeff Brown928e0542011-01-10 11:17:36 -08001017 // Note: inputWindowHandle may be null.
1018 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001019 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1020 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001021 jlong delayMillis = env->CallLongMethod(mServiceObj,
1022 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -08001023 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -08001024 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
1025 android_view_KeyEvent_recycle(env, keyEventObj);
1026 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -07001027 if (!error) {
1028 if (delayMillis < 0) {
1029 result = -1;
1030 } else if (delayMillis > 0) {
1031 result = milliseconds_to_nanoseconds(delayMillis);
1032 }
1033 }
Jeff Brown1f245102010-11-18 20:53:46 -08001034 } else {
Steve Block3762c312012-01-06 19:20:56 +00001035 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -08001036 }
Jeff Brown928e0542011-01-10 11:17:36 -08001037 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -07001038 }
Jeff Brown1f245102010-11-18 20:53:46 -08001039 return result;
Jeff Brownd0097872010-06-30 14:41:59 -07001040}
1041
Jeff Brown928e0542011-01-10 11:17:36 -08001042bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001043 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -07001044 // Policy:
1045 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -08001046 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -07001047 if (policyFlags & POLICY_FLAG_TRUSTED) {
1048 JNIEnv* env = jniEnv();
1049
Jeff Brown928e0542011-01-10 11:17:36 -08001050 // Note: inputWindowHandle may be null.
1051 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -08001052 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
1053 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001054 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
1055 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -08001056 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001057 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
1058 fallbackKeyEventObj = NULL;
1059 }
Jeff Brown1f245102010-11-18 20:53:46 -08001060 android_view_KeyEvent_recycle(env, keyEventObj);
1061 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001062
1063 if (fallbackKeyEventObj) {
1064 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1065 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1066 outFallbackKeyEvent)) {
1067 result = true;
1068 }
1069 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1070 env->DeleteLocalRef(fallbackKeyEventObj);
1071 }
Jeff Brown1f245102010-11-18 20:53:46 -08001072 } else {
Steve Block3762c312012-01-06 19:20:56 +00001073 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001074 }
Jeff Brown928e0542011-01-10 11:17:36 -08001075 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001076 }
Jeff Brown1f245102010-11-18 20:53:46 -08001077 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001078}
1079
Jeff Brown01ce2e92010-09-26 22:20:12 -07001080void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
1081 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001082}
1083
Jeff Brown349703e2010-06-22 01:27:15 -07001084
Jeff Brownb88102f2010-09-08 11:49:43 -07001085bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1086 int32_t injectorPid, int32_t injectorUid) {
1087 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001088 jboolean result = env->CallBooleanMethod(mServiceObj,
1089 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001090 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1091 result = false;
1092 }
Jeff Brown349703e2010-06-22 01:27:15 -07001093 return result;
1094}
1095
Jun Mukai19a56012015-11-24 11:25:52 -08001096void NativeInputManager::loadPointerIcon(SpriteIcon* icon) {
1097 JNIEnv* env = jniEnv();
1098
1099 ScopedLocalRef<jobject> pointerIconObj(env, env->CallObjectMethod(
1100 mServiceObj, gServiceClassInfo.getPointerIcon));
1101 if (checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
1102 return;
1103 }
1104
1105 PointerIcon pointerIcon;
1106 status_t status = android_view_PointerIcon_load(env, pointerIconObj.get(),
1107 mContextObj, &pointerIcon);
1108 if (!status && !pointerIcon.isNullIcon()) {
1109 *icon = SpriteIcon(pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY);
1110 } else {
1111 *icon = SpriteIcon();
1112 }
1113}
1114
Jeff Brown2352b972011-04-12 22:39:53 -07001115void NativeInputManager::loadPointerResources(PointerResources* outResources) {
1116 JNIEnv* env = jniEnv();
1117
1118 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1119 &outResources->spotHover);
1120 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1121 &outResources->spotTouch);
1122 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1123 &outResources->spotAnchor);
1124}
1125
Jun Mukai808196f2015-10-28 16:46:44 -07001126void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources,
1127 std::map<int32_t, PointerAnimation>* outAnimationResources) {
Jun Mukai1db53972015-09-11 18:08:31 -07001128 JNIEnv* env = jniEnv();
1129
1130 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1131 ++iconId) {
Jun Mukai808196f2015-10-28 16:46:44 -07001132 PointerIcon pointerIcon;
1133 loadSystemIconAsSpriteWithPointerIcon(
1134 env, mContextObj, iconId, &pointerIcon, &((*outResources)[iconId]));
1135 if (!pointerIcon.bitmapFrames.empty()) {
1136 PointerAnimation& animationData = (*outAnimationResources)[iconId];
1137 size_t numFrames = pointerIcon.bitmapFrames.size() + 1;
1138 animationData.durationPerFrame =
1139 milliseconds_to_nanoseconds(pointerIcon.durationPerFrame);
1140 animationData.animationFrames.reserve(numFrames);
1141 animationData.animationFrames.push_back(SpriteIcon(
1142 pointerIcon.bitmap, pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1143 for (size_t i = 0; i < numFrames - 1; ++i) {
1144 animationData.animationFrames.push_back(SpriteIcon(
1145 pointerIcon.bitmapFrames[i], pointerIcon.hotSpotX, pointerIcon.hotSpotY));
1146 }
1147 }
Jun Mukai1db53972015-09-11 18:08:31 -07001148 }
Jun Mukai808196f2015-10-28 16:46:44 -07001149 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL,
1150 &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001151}
1152
Jun Mukai5ec74202015-10-07 16:58:09 +09001153int32_t NativeInputManager::getDefaultPointerIconId() {
1154 return POINTER_ICON_STYLE_ARROW;
1155}
Jeff Brown83c09682010-12-23 17:50:18 -08001156
Jun Mukaid4eaef72015-10-30 15:54:33 -07001157int32_t NativeInputManager::getCustomPointerIconId() {
1158 return POINTER_ICON_STYLE_CUSTOM;
1159}
1160
Jeff Brown9c3cda02010-06-15 01:31:58 -07001161// ----------------------------------------------------------------------------
1162
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001163static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001164 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001165 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001166 if (messageQueue == NULL) {
1167 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1168 return 0;
1169 }
1170
Jeff Brown603b4452012-04-06 17:39:41 -07001171 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1172 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001173 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001174 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001175}
1176
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001177static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001178 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001179
Jeff Brown4532e612012-04-05 14:27:12 -07001180 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001181 if (result) {
1182 jniThrowRuntimeException(env, "Input manager could not be started.");
1183 }
1184}
1185
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001186static void nativeSetDisplayViewport(JNIEnv* /* env */, jclass /* clazz */, jlong ptr,
1187 jboolean external, jint displayId, jint orientation,
Jeff Brownd728bf52012-09-08 18:05:28 -07001188 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001189 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
1190 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001191 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001192
Jeff Brownd728bf52012-09-08 18:05:28 -07001193 DisplayViewport v;
1194 v.displayId = displayId;
1195 v.orientation = orientation;
1196 v.logicalLeft = logicalLeft;
1197 v.logicalTop = logicalTop;
1198 v.logicalRight = logicalRight;
1199 v.logicalBottom = logicalBottom;
1200 v.physicalLeft = physicalLeft;
1201 v.physicalTop = physicalTop;
1202 v.physicalRight = physicalRight;
1203 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001204 v.deviceWidth = deviceWidth;
1205 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -07001206 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001207}
1208
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001209static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001210 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001211 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001212
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001213 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001214 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001215}
1216
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001217static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001218 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001219 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001220
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001221 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001222 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001223}
1224
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001225static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001226 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001227 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001228
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001229 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001230 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001231}
1232
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001233static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001234 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001235 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001236
1237 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1238 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1239 jsize numCodes = env->GetArrayLength(keyCodes);
1240 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001241 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001242 if (im->getInputManager()->getReader()->hasKeys(
1243 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1244 result = JNI_TRUE;
1245 } else {
1246 result = JNI_FALSE;
1247 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001248 } else {
1249 result = JNI_FALSE;
1250 }
1251
1252 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1253 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1254 return result;
1255}
1256
1257static void throwInputChannelNotInitialized(JNIEnv* env) {
1258 jniThrowException(env, "java/lang/IllegalStateException",
1259 "inputChannel is not initialized");
1260}
1261
Jeff Brown4532e612012-04-05 14:27:12 -07001262static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001263 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001264 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1265
Steve Block8564c8d2012-01-05 23:22:43 +00001266 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001267 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001268 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001269}
1270
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001271static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001272 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001273 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001274
1275 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1276 inputChannelObj);
1277 if (inputChannel == NULL) {
1278 throwInputChannelNotInitialized(env);
1279 return;
1280 }
1281
Jeff Brown928e0542011-01-10 11:17:36 -08001282 sp<InputWindowHandle> inputWindowHandle =
1283 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001284
Jeff Brown4532e612012-04-05 14:27:12 -07001285 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001286 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001287 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001288 String8 message;
1289 message.appendFormat("Failed to register input channel. status=%d", status);
1290 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001291 return;
1292 }
1293
Jeff Browna41ca772010-08-11 14:46:32 -07001294 if (! monitor) {
1295 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001296 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001297 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001298}
1299
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001300static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001301 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001302 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001303
1304 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1305 inputChannelObj);
1306 if (inputChannel == NULL) {
1307 throwInputChannelNotInitialized(env);
1308 return;
1309 }
1310
1311 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1312
Jeff Brown4532e612012-04-05 14:27:12 -07001313 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001314 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1315 String8 message;
1316 message.appendFormat("Failed to unregister input channel. status=%d", status);
1317 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001318 }
1319}
1320
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001321static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001322 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001323 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001324
Jeff Brown4532e612012-04-05 14:27:12 -07001325 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001326}
1327
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001328static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Jeff Brownca9bc702014-02-11 14:32:56 -08001329 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001330 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001331 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001332
Jeff Brown6ec402b2010-07-28 15:48:59 -07001333 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1334 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001335 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1336 if (status) {
1337 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1338 return INPUT_EVENT_INJECTION_FAILED;
1339 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001340
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001341 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001342 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001343 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001344 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001345 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1346 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001347 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1348 return INPUT_EVENT_INJECTION_FAILED;
1349 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001350
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001351 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001352 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001353 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001354 } else {
1355 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001356 return INPUT_EVENT_INJECTION_FAILED;
1357 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001358}
1359
Andrii Kulian112d0562016-03-08 10:44:22 -08001360static void nativeToggleCapsLock(JNIEnv* env, jclass /* clazz */,
1361 jlong ptr, jint deviceId) {
1362 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001363
Andrii Kulian112d0562016-03-08 10:44:22 -08001364 im->getInputManager()->getReader()->toggleCapsLockState(deviceId);
1365}
1366
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001367static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001368 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001369 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001370
Jeff Brown4532e612012-04-05 14:27:12 -07001371 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001372}
1373
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001374static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001375 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001376 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001377
Jeff Brown4532e612012-04-05 14:27:12 -07001378 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001379}
1380
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001381static void nativeSetPointerCapture(JNIEnv* env, jclass /* clazz */, jlong ptr,
1382 jboolean enabled) {
1383 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001384
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001385 im->setPointerCapture(enabled);
1386}
1387
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001388static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1389 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001390 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001391
Jeff Brown4532e612012-04-05 14:27:12 -07001392 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001393}
1394
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001395static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1396 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001397 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001398
Jeff Brown4532e612012-04-05 14:27:12 -07001399 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001400}
1401
Jeff Brown4532e612012-04-05 14:27:12 -07001402static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001403 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001404 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001405
1406 sp<InputChannel> fromChannel =
1407 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1408 sp<InputChannel> toChannel =
1409 android_view_InputChannel_getInputChannel(env, toChannelObj);
1410
1411 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001412 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001413 }
1414
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001415 if (im->getInputManager()->getDispatcher()->
1416 transferTouchFocus(fromChannel, toChannel)) {
1417 return JNI_TRUE;
1418 } else {
1419 return JNI_FALSE;
1420 }
Jeff Browne6504122010-09-27 14:52:15 -07001421}
1422
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001423static void nativeSetPointerSpeed(JNIEnv* /* env */,
1424 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001425 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001426
Jeff Brown4532e612012-04-05 14:27:12 -07001427 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001428}
1429
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001430static void nativeSetShowTouches(JNIEnv* /* env */,
1431 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001432 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001433
Jeff Brown4532e612012-04-05 14:27:12 -07001434 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001435}
1436
Jeff Brown037c33e2014-04-09 00:31:55 -07001437static void nativeSetInteractive(JNIEnv* env,
1438 jclass clazz, jlong ptr, jboolean interactive) {
1439 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1440
1441 im->setInteractive(interactive);
1442}
1443
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001444static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1445 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001446
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001447 im->reloadCalibration();
1448}
1449
Jeff Browna47425a2012-04-13 04:09:27 -07001450static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001451 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001452 jint repeat, jint token) {
1453 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1454
1455 size_t patternSize = env->GetArrayLength(patternObj);
1456 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001457 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001458 "which is more than the maximum supported size of %d.",
1459 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1460 return; // limit to reasonable size
1461 }
1462
1463 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1464 patternObj, NULL));
1465 nsecs_t pattern[patternSize];
1466 for (size_t i = 0; i < patternSize; i++) {
1467 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001468 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001469 }
1470 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1471
1472 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1473}
1474
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001475static void nativeCancelVibrate(JNIEnv* /* env */,
1476 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001477 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1478
1479 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1480}
1481
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001482static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1483 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001484 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1485
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001486 im->getInputManager()->getReader()->requestRefreshConfiguration(
1487 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1488}
1489
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001490static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1491 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001492 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1493
1494 im->getInputManager()->getReader()->requestRefreshConfiguration(
1495 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001496}
1497
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001498static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001499 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001500
Jeff Brownb88102f2010-09-08 11:49:43 -07001501 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001502 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001503 return env->NewStringUTF(dump.string());
1504}
1505
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001506static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001507 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001508
Jeff Brown4532e612012-04-05 14:27:12 -07001509 im->getInputManager()->getReader()->monitor();
1510 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001511}
1512
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001513static jboolean nativeIsInputDeviceEnabled(JNIEnv* env /* env */,
1514 jclass /* clazz */, jlong ptr, jint deviceId) {
1515 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1516
1517 return im->getInputManager()->getReader()->isInputDeviceEnabled(deviceId);
1518}
1519
1520static void nativeEnableInputDevice(JNIEnv* /* env */,
1521 jclass /* clazz */, jlong ptr, jint deviceId) {
1522 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1523
1524 im->setInputDeviceEnabled(deviceId, true);
1525}
1526
1527static void nativeDisableInputDevice(JNIEnv* /* env */,
1528 jclass /* clazz */, jlong ptr, jint deviceId) {
1529 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1530
1531 im->setInputDeviceEnabled(deviceId, false);
1532}
1533
Michael Wrighte051f6f2016-05-13 17:44:16 +01001534static void nativeSetPointerIconType(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
Jun Mukai1db53972015-09-11 18:08:31 -07001535 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001536
Michael Wrighte051f6f2016-05-13 17:44:16 +01001537 im->setPointerIconType(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -07001538}
1539
Jun Mukai19a56012015-11-24 11:25:52 -08001540static void nativeReloadPointerIcons(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
1541 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001542
Jun Mukai19a56012015-11-24 11:25:52 -08001543 im->reloadPointerIcons();
1544}
1545
Jun Mukaid4eaef72015-10-30 15:54:33 -07001546static void nativeSetCustomPointerIcon(JNIEnv* env, jclass /* clazz */,
1547 jlong ptr, jobject iconObj) {
1548 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1549
1550 PointerIcon pointerIcon;
Michael Wrightb004b512017-01-18 18:09:29 +00001551 status_t result = android_view_PointerIcon_getLoadedIcon(env, iconObj, &pointerIcon);
1552 if (result) {
1553 jniThrowRuntimeException(env, "Failed to load custom pointer icon.");
1554 return;
1555 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001556
1557 SpriteIcon spriteIcon;
Matt Sarett1350a5f2017-04-27 16:47:10 -04001558 SkImageInfo spriteInfo = pointerIcon.bitmap.info().makeColorType(kN32_SkColorType);
1559 if (spriteIcon.bitmap.tryAllocPixels(spriteInfo)) {
1560 pointerIcon.bitmap.readPixels(spriteInfo, spriteIcon.bitmap.getPixels(),
1561 spriteIcon.bitmap.rowBytes(), 0, 0);
1562 }
Jun Mukaid4eaef72015-10-30 15:54:33 -07001563 spriteIcon.hotSpotX = pointerIcon.hotSpotX;
1564 spriteIcon.hotSpotY = pointerIcon.hotSpotY;
1565 im->setCustomPointerIcon(spriteIcon);
1566}
1567
Jeff Brown9c3cda02010-06-15 01:31:58 -07001568// ----------------------------------------------------------------------------
1569
Daniel Micay76f6a862015-09-19 17:31:01 -04001570static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001571 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001572 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001573 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001574 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001575 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001576 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001577 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001578 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001579 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001580 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001581 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001582 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001583 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001584 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001585 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001586 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001587 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001588 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001589 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001590 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001591 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001592 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001593 (void*) nativeSetInputFilterEnabled },
Jeff Brownca9bc702014-02-11 14:32:56 -08001594 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001595 (void*) nativeInjectInputEvent },
Andrii Kulian112d0562016-03-08 10:44:22 -08001596 { "nativeToggleCapsLock", "(JI)V",
1597 (void*) nativeToggleCapsLock },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001598 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001599 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001600 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001601 (void*) nativeSetFocusedApplication },
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001602 { "nativeSetPointerCapture", "(JZ)V",
1603 (void*) nativeSetPointerCapture },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001604 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001605 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001606 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001607 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001608 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001609 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001610 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001611 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001612 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001613 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001614 { "nativeSetInteractive", "(JZ)V",
1615 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001616 { "nativeReloadCalibration", "(J)V",
1617 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001618 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001619 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001620 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001621 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001622 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001623 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001624 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001625 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001626 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001627 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001628 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001629 (void*) nativeMonitor },
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -07001630 { "nativeIsInputDeviceEnabled", "(JI)Z",
1631 (void*) nativeIsInputDeviceEnabled },
1632 { "nativeEnableInputDevice", "(JI)V",
1633 (void*) nativeEnableInputDevice },
1634 { "nativeDisableInputDevice", "(JI)V",
1635 (void*) nativeDisableInputDevice },
Michael Wrighte051f6f2016-05-13 17:44:16 +01001636 { "nativeSetPointerIconType", "(JI)V",
1637 (void*) nativeSetPointerIconType },
Jun Mukai19a56012015-11-24 11:25:52 -08001638 { "nativeReloadPointerIcons", "(J)V",
1639 (void*) nativeReloadPointerIcons },
Jun Mukaid4eaef72015-10-30 15:54:33 -07001640 { "nativeSetCustomPointerIcon", "(JLandroid/view/PointerIcon;)V",
1641 (void*) nativeSetCustomPointerIcon },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001642};
1643
1644#define FIND_CLASS(var, className) \
1645 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001646 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001647
1648#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1649 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001650 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001651
1652#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1653 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -07001654 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001655
1656int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001657 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001658 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001659 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001660 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1661
Jeff Brown9c3cda02010-06-15 01:31:58 -07001662 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001663
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001664 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001665 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001666
Jeff Brown4532e612012-04-05 14:27:12 -07001667 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001668 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001669
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001670 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1671 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1672
Jeff Brown53384282012-08-20 20:16:01 -07001673 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1674 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001675
Jeff Brown4532e612012-04-05 14:27:12 -07001676 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1677 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001678
Jeff Brown4532e612012-04-05 14:27:12 -07001679 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001680 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001681 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001682
Jeff Brown4532e612012-04-05 14:27:12 -07001683 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001684 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1685
Jeff Brown4532e612012-04-05 14:27:12 -07001686 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001687 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001688
Michael Wright70af00a2014-09-03 19:30:20 -07001689 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1690 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001691
Jeff Brown4532e612012-04-05 14:27:12 -07001692 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001693 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001694 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001695
Jeff Brown4532e612012-04-05 14:27:12 -07001696 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001697 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001698 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001699
Jeff Brown4532e612012-04-05 14:27:12 -07001700 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001701 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001702
Jeff Brown4532e612012-04-05 14:27:12 -07001703 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001704 "getVirtualKeyQuietTimeMillis", "()I");
1705
Jeff Brown4532e612012-04-05 14:27:12 -07001706 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001707 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1708
Jeff Brown4532e612012-04-05 14:27:12 -07001709 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001710 "getKeyRepeatTimeout", "()I");
1711
Jeff Brown4532e612012-04-05 14:27:12 -07001712 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001713 "getKeyRepeatDelay", "()I");
1714
Jeff Brown4532e612012-04-05 14:27:12 -07001715 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001716 "getHoverTapTimeout", "()I");
1717
Jeff Brown4532e612012-04-05 14:27:12 -07001718 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001719 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001720
Jeff Brown4532e612012-04-05 14:27:12 -07001721 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001722 "getDoubleTapTimeout", "()I");
1723
Jeff Brown4532e612012-04-05 14:27:12 -07001724 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001725 "getLongPressTimeout", "()I");
1726
Jeff Brown4532e612012-04-05 14:27:12 -07001727 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001728 "getPointerLayer", "()I");
1729
Jeff Brown4532e612012-04-05 14:27:12 -07001730 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001731 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001732
Jeff Brown6ec6f792012-04-17 16:52:41 -07001733 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001734 "getKeyboardLayoutOverlay",
1735 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001736
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001737 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1738 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1739
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001740 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1741 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001742 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001743
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001744 // InputDevice
1745
1746 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1747 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1748
Jeff Brown6ec402b2010-07-28 15:48:59 -07001749 // KeyEvent
1750
1751 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001752 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1753
Jeff Brown8d608662010-08-30 03:02:23 -07001754 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001755
1756 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001757 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001758
RoboErikfb290df2013-12-16 11:27:55 -08001759 // InputDeviceIdentifier
1760
1761 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1762 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1763 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1764 "<init>", "(Ljava/lang/String;II)V");
1765
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001766 // TouchCalibration
1767
1768 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1769 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1770
1771 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1772 "getAffineTransform", "()[F");
1773
Jeff Brown46b9ac02010-04-22 18:58:52 -07001774 return 0;
1775}
1776
Jeff Brown46b9ac02010-04-22 18:58:52 -07001777} /* namespace android */