blob: be190cbef47d47956191adce23f0f76e35f0cd24 [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>
Jeff Brown83c09682010-12-23 17:50:18 -080039
Jeff Brownb4ff35d2011-01-02 16:37:43 -080040#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070041#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042
Michael Wrightd6b473712014-02-10 15:56:36 -080043#include <inputflinger/InputManager.h>
44
Jeff Brown05dc66a2011-03-02 14:41:58 -080045#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070046#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047#include <android_view_KeyEvent.h>
48#include <android_view_MotionEvent.h>
49#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070050#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080051#include <android/graphics/GraphicsJNI.h>
52
Jeff Brown6ec6f792012-04-17 16:52:41 -070053#include <ScopedLocalRef.h>
Jason Gerecke857aa7b2014-01-27 18:34:20 -080054#include <ScopedPrimitiveArray.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070055#include <ScopedUtfChars.h>
56
Jeff Brown4f8ecd82012-06-18 18:29:13 -070057#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070058#include "com_android_server_input_InputApplicationHandle.h"
59#include "com_android_server_input_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070060
Michael Wrighta4051212015-07-23 17:04:40 +010061#define INDENT " "
62
Jeff Brown46b9ac02010-04-22 18:58:52 -070063namespace android {
64
Jeff Brown1a84fd12011-06-02 01:26:32 -070065// The exponent used to calculate the pointer speed scaling factor.
66// The scaling factor is calculated as 2 ^ (speed * exponent),
67// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070068static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070069
Jeff Brown46b9ac02010-04-22 18:58:52 -070070static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070071 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070072 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070073 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070074 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070075 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070076 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070077 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070078 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070079 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070080 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070081 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080082 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070083 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080084 jmethodID getKeyRepeatTimeout;
85 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070086 jmethodID getHoverTapTimeout;
87 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070088 jmethodID getDoubleTapTimeout;
89 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080090 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080091 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070092 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070093 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -080094 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -070095} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070096
97static struct {
98 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070099} gInputDeviceClassInfo;
100
101static struct {
102 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700103} gKeyEventClassInfo;
104
105static struct {
106 jclass clazz;
107} gMotionEventClassInfo;
108
RoboErikfb290df2013-12-16 11:27:55 -0800109static struct {
110 jclass clazz;
111 jmethodID constructor;
112} gInputDeviceIdentifierInfo;
113
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800114static struct {
115 jclass clazz;
116 jmethodID getAffineTransform;
117} gTouchCalibrationClassInfo;
118
RoboErikfb290df2013-12-16 11:27:55 -0800119
Jeff Brown928e0542011-01-10 11:17:36 -0800120
121// --- Global functions ---
122
Jeff Brown214eaf42011-05-26 19:17:02 -0700123template<typename T>
124inline static T min(const T& a, const T& b) {
125 return a < b ? a : b;
126}
127
128template<typename T>
129inline static T max(const T& a, const T& b) {
130 return a > b ? a : b;
131}
132
Michael Wrighta4051212015-07-23 17:04:40 +0100133static inline const char* toString(bool value) {
134 return value ? "true" : "false";
135}
136
Jeff Brown928e0542011-01-10 11:17:36 -0800137static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
138 const sp<InputApplicationHandle>& inputApplicationHandle) {
139 if (inputApplicationHandle == NULL) {
140 return NULL;
141 }
142 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
143 getInputApplicationHandleObjLocalRef(env);
144}
145
146static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
147 const sp<InputWindowHandle>& inputWindowHandle) {
148 if (inputWindowHandle == NULL) {
149 return NULL;
150 }
151 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
152 getInputWindowHandleObjLocalRef(env);
153}
154
Jeff Brown2352b972011-04-12 22:39:53 -0700155static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
156 SpriteIcon* outSpriteIcon) {
157 PointerIcon pointerIcon;
158 status_t status = android_view_PointerIcon_loadSystemIcon(env,
159 contextObj, style, &pointerIcon);
160 if (!status) {
Mike Reed4a9c3892014-07-07 15:44:40 -0400161 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, kN32_SkColorType);
Jeff Brown2352b972011-04-12 22:39:53 -0700162 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
163 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
164 }
165}
166
Jeff Brown905805a2011-10-12 13:57:59 -0700167enum {
168 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700169};
170
Jeff Brown928e0542011-01-10 11:17:36 -0800171
172// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800173
Jeff Brown9c3cda02010-06-15 01:31:58 -0700174class NativeInputManager : public virtual RefBase,
175 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700176 public virtual InputDispatcherPolicyInterface,
177 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700178protected:
179 virtual ~NativeInputManager();
180
181public:
Jeff Brown4532e612012-04-05 14:27:12 -0700182 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700183
184 inline sp<InputManager> getInputManager() const { return mInputManager; }
185
Jeff Brownb88102f2010-09-08 11:49:43 -0700186 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700187
Jeff Brownd728bf52012-09-08 18:05:28 -0700188 void setDisplayViewport(bool external, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700189
Jeff Brown7fbdc842010-06-17 20:52:56 -0700190 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800191 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700192 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
193
Jeff Brown9302c872011-07-13 22:51:29 -0700194 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
195 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700196 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800197 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700198 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700199 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700200 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800201 void reloadCalibration();
Jun Mukai1db53972015-09-11 18:08:31 -0700202 void setPointerIconShape(int32_t iconId);
Jeff Brown349703e2010-06-22 01:27:15 -0700203
Jeff Brown9c3cda02010-06-15 01:31:58 -0700204 /* --- InputReaderPolicyInterface implementation --- */
205
Jeff Brown214eaf42011-05-26 19:17:02 -0700206 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800207 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700208 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800209 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700210 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700211 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
212 jfloatArray matrixArr);
213 virtual TouchAffineTransformation getTouchAffineTransformation(
214 const String8& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700215
216 /* --- InputDispatcherPolicyInterface implementation --- */
217
Jeff Brownbcc046a2012-09-27 20:46:43 -0700218 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700219 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700220 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700221 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700222 const sp<InputWindowHandle>& inputWindowHandle,
223 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800224 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700225 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700226 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800227 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800228 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700229 virtual nsecs_t interceptKeyBeforeDispatching(
230 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700231 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800232 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800233 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700234 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700235 virtual bool checkInjectEventsPermissionNonReentrant(
236 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237
Jeff Brown2352b972011-04-12 22:39:53 -0700238 /* --- PointerControllerPolicyInterface implementation --- */
239
240 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai5ec74202015-10-07 16:58:09 +0900241 virtual void loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources);
242 virtual int32_t getDefaultPointerIconId();
Jeff Brown2352b972011-04-12 22:39:53 -0700243
Jeff Brown9c3cda02010-06-15 01:31:58 -0700244private:
245 sp<InputManager> mInputManager;
246
Jeff Brown2352b972011-04-12 22:39:53 -0700247 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700248 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800249 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700250
Jeff Brown83c09682010-12-23 17:50:18 -0800251 Mutex mLock;
252 struct Locked {
253 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700254 DisplayViewport internalViewport;
255 DisplayViewport externalViewport;
Jeff Brown83c09682010-12-23 17:50:18 -0800256
Jeff Brown05dc66a2011-03-02 14:41:58 -0800257 // System UI visibility.
258 int32_t systemUiVisibility;
259
Jeff Brown1a84fd12011-06-02 01:26:32 -0700260 // Pointer speed.
261 int32_t pointerSpeed;
262
Jeff Brown474dcb52011-06-14 20:22:50 -0700263 // True if pointer gestures are enabled.
264 bool pointerGesturesEnabled;
265
Jeff Browndaf4a122011-08-26 17:14:14 -0700266 // Show touches feature enable/disable.
267 bool showTouches;
268
Jeff Brown5541de92011-04-11 11:54:25 -0700269 // Sprite controller singleton, created on first use.
270 sp<SpriteController> spriteController;
271
Jeff Brown83c09682010-12-23 17:50:18 -0800272 // Pointer controller singleton, created and destroyed as needed.
273 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800274 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700275
Michael Wrighta4051212015-07-23 17:04:40 +0100276 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700277
Jeff Brown2352b972011-04-12 22:39:53 -0700278 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800279 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700280 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800281
Jeff Brownb88102f2010-09-08 11:49:43 -0700282 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700283
Jeff Brown9c3cda02010-06-15 01:31:58 -0700284 static inline JNIEnv* jniEnv() {
285 return AndroidRuntime::getJNIEnv();
286 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700287};
288
Jeff Brown928e0542011-01-10 11:17:36 -0800289
Jeff Brown9c3cda02010-06-15 01:31:58 -0700290
Jeff Brown2352b972011-04-12 22:39:53 -0700291NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700292 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700293 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700294 JNIEnv* env = jniEnv();
295
Jeff Brown2352b972011-04-12 22:39:53 -0700296 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700297 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700298
Jeff Brown83c09682010-12-23 17:50:18 -0800299 {
300 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800301 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700302 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700303 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700304 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800305 }
Michael Wrighta4051212015-07-23 17:04:40 +0100306 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800307
Jeff Brown9c3cda02010-06-15 01:31:58 -0700308 sp<EventHub> eventHub = new EventHub();
309 mInputManager = new InputManager(eventHub, this, this);
310}
311
312NativeInputManager::~NativeInputManager() {
313 JNIEnv* env = jniEnv();
314
Jeff Brown2352b972011-04-12 22:39:53 -0700315 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700316 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700317}
318
Jeff Brownb88102f2010-09-08 11:49:43 -0700319void NativeInputManager::dump(String8& dump) {
Michael Wrighta4051212015-07-23 17:04:40 +0100320 dump.append("Input Manager State:\n");
321 {
322 dump.appendFormat(INDENT "Interactive: %s\n", toString(mInteractive.load()));
323 }
324 {
325 AutoMutex _l(mLock);
326 dump.appendFormat(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
327 mLocked.systemUiVisibility);
328 dump.appendFormat(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
329 dump.appendFormat(INDENT "Pointer Gestures Enabled: %s\n",
330 toString(mLocked.pointerGesturesEnabled));
331 dump.appendFormat(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
332 }
333 dump.append("\n");
334
Jeff Brownb88102f2010-09-08 11:49:43 -0700335 mInputManager->getReader()->dump(dump);
336 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700337
Jeff Brownb88102f2010-09-08 11:49:43 -0700338 mInputManager->getDispatcher()->dump(dump);
339 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700340}
341
Jeff Brown7fbdc842010-06-17 20:52:56 -0700342bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700343 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000344 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700345 LOGE_EX(env);
346 env->ExceptionClear();
347 return true;
348 }
349 return false;
350}
351
Jeff Brownd728bf52012-09-08 18:05:28 -0700352void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700353 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700354 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700355 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700356
Jeff Brownd728bf52012-09-08 18:05:28 -0700357 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
358 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700359 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700360 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700361
Jeff Brownd728bf52012-09-08 18:05:28 -0700362 if (!external) {
363 sp<PointerController> controller = mLocked.pointerController.promote();
364 if (controller != NULL) {
365 controller->setDisplayViewport(
366 viewport.logicalRight - viewport.logicalLeft,
367 viewport.logicalBottom - viewport.logicalTop,
368 viewport.orientation);
369 }
Jeff Brown2352b972011-04-12 22:39:53 -0700370 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700371 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700372 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700373
374 if (changed) {
375 mInputManager->getReader()->requestRefreshConfiguration(
376 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
377 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700378}
379
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700380status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Jeff Brown928e0542011-01-10 11:17:36 -0800381 const sp<InputChannel>& inputChannel,
382 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
383 return mInputManager->getDispatcher()->registerInputChannel(
384 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700385}
386
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700387status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700388 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700389 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700390}
391
Jeff Brown214eaf42011-05-26 19:17:02 -0700392void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700393 JNIEnv* env = jniEnv();
394
Jeff Brown4532e612012-04-05 14:27:12 -0700395 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
396 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700397 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
398 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
399 }
400
401 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700402 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
403 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700404 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
405 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700406 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700407 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700408 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700409 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700410 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700411 env->DeleteLocalRef(item);
412 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700413 env->DeleteLocalRef(excludedDeviceNames);
414 }
415
Jeff Brown4532e612012-04-05 14:27:12 -0700416 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
417 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700418 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700419 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
420 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700421 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700422 jint longPressTimeout = env->CallIntMethod(mServiceObj,
423 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700424 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700425 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700426
427 // We must ensure that the tap-drag interval is significantly shorter than
428 // the long-press timeout because the tap is held down for the entire duration
429 // of the double-tap timeout.
430 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700431 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700432 outConfig->pointerGestureTapDragInterval =
433 milliseconds_to_nanoseconds(tapDragInterval);
434 }
435 }
436 }
437
Jeff Brown4532e612012-04-05 14:27:12 -0700438 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
439 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700440 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
441 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700442 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700443
444 { // acquire lock
445 AutoMutex _l(mLock);
446
447 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
448 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700449 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700450
Jeff Browndaf4a122011-08-26 17:14:14 -0700451 outConfig->showTouches = mLocked.showTouches;
452
Jeff Brownd728bf52012-09-08 18:05:28 -0700453 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
454 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700455 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700456}
457
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700458sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Jeff Brown83c09682010-12-23 17:50:18 -0800459 AutoMutex _l(mLock);
460
461 sp<PointerController> controller = mLocked.pointerController.promote();
462 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700463 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800464
Jeff Brown2352b972011-04-12 22:39:53 -0700465 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800466 mLocked.pointerController = controller;
467
Jeff Brownd728bf52012-09-08 18:05:28 -0700468 DisplayViewport& v = mLocked.internalViewport;
469 controller->setDisplayViewport(
470 v.logicalRight - v.logicalLeft,
471 v.logicalBottom - v.logicalTop,
472 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800473
Jeff Brown5541de92011-04-11 11:54:25 -0700474 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700475 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
476 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700477 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
478 PointerIcon pointerIcon;
479 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
480 mContextObj, &pointerIcon);
481 if (!status && !pointerIcon.isNullIcon()) {
482 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
483 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
484 } else {
485 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800486 }
Jeff Brown2352b972011-04-12 22:39:53 -0700487 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800488 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800489
Jeff Brown2352b972011-04-12 22:39:53 -0700490 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800491 }
492 return controller;
493}
494
Jeff Brown5541de92011-04-11 11:54:25 -0700495void NativeInputManager::ensureSpriteControllerLocked() {
496 if (mLocked.spriteController == NULL) {
497 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700498 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700499 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
500 layer = -1;
501 }
502 mLocked.spriteController = new SpriteController(mLooper, layer);
503 }
504}
505
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700506void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
507 JNIEnv* env = jniEnv();
508
509 size_t count = inputDevices.size();
510 jobjectArray inputDevicesObjArray = env->NewObjectArray(
511 count, gInputDeviceClassInfo.clazz, NULL);
512 if (inputDevicesObjArray) {
513 bool error = false;
514 for (size_t i = 0; i < count; i++) {
515 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
516 if (!inputDeviceObj) {
517 error = true;
518 break;
519 }
520
521 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
522 env->DeleteLocalRef(inputDeviceObj);
523 }
524
525 if (!error) {
526 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
527 inputDevicesObjArray);
528 }
529
530 env->DeleteLocalRef(inputDevicesObjArray);
531 }
532
533 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
534}
535
Jeff Brown6ec6f792012-04-17 16:52:41 -0700536sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800537 const InputDeviceIdentifier& identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700538 JNIEnv* env = jniEnv();
539
540 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800541 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
542 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
543 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
544 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700545 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800546 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700547 if (arrayObj.get()) {
548 ScopedLocalRef<jstring> filenameObj(env,
549 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
550 ScopedLocalRef<jstring> contentsObj(env,
551 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
552 ScopedUtfChars filenameChars(env, filenameObj.get());
553 ScopedUtfChars contentsChars(env, contentsObj.get());
554
555 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
556 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
557 }
558 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
559 return result;
560}
561
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700562String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
563 JNIEnv* env = jniEnv();
564
565 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
566 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
567 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
568 String8 result;
569 if (aliasObj.get()) {
570 ScopedUtfChars aliasChars(env, aliasObj.get());
571 result.setTo(aliasChars.c_str());
572 }
573 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
574 return result;
575}
576
Jeff Brownbcc046a2012-09-27 20:46:43 -0700577void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700578 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700579#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700580 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
581 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700582#endif
583
584 JNIEnv* env = jniEnv();
585
Jeff Brown53384282012-08-20 20:16:01 -0700586 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700587 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700588 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700589}
590
Jeff Brown9c3cda02010-06-15 01:31:58 -0700591void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
592#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000593 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700594#endif
595
596 JNIEnv* env = jniEnv();
597
Jeff Brown4532e612012-04-05 14:27:12 -0700598 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700599 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700600}
601
Jeff Brown519e0242010-09-15 15:18:56 -0700602nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700603 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700604#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000605 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700606#endif
607
608 JNIEnv* env = jniEnv();
609
Jeff Brown928e0542011-01-10 11:17:36 -0800610 jobject inputApplicationHandleObj =
611 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
612 jobject inputWindowHandleObj =
613 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700614 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700615
Jeff Brown4532e612012-04-05 14:27:12 -0700616 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700617 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
618 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700619 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
620 newTimeout = 0; // abort dispatch
621 } else {
622 assert(newTimeout >= 0);
623 }
624
Jeff Brownbd181bb2013-09-10 16:44:24 -0700625 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800626 env->DeleteLocalRef(inputWindowHandleObj);
627 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700628 return newTimeout;
629}
630
Jeff Brown928e0542011-01-10 11:17:36 -0800631void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700632#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000633 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700634#endif
635
Jeff Brown7fbdc842010-06-17 20:52:56 -0700636 JNIEnv* env = jniEnv();
637
Jeff Brown928e0542011-01-10 11:17:36 -0800638 jobject inputWindowHandleObj =
639 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
640 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700641 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800642 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700643 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
644
Jeff Brown928e0542011-01-10 11:17:36 -0800645 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700646 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700647}
648
Jeff Brown214eaf42011-05-26 19:17:02 -0700649void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
650 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800651
Jeff Brown4532e612012-04-05 14:27:12 -0700652 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
653 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700654 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
655 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
656 }
Jeff Browna4547672011-03-02 21:38:11 -0800657
Jeff Brown4532e612012-04-05 14:27:12 -0700658 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
659 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700660 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
661 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
662 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700663}
664
Jeff Brown9302c872011-07-13 22:51:29 -0700665void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
666 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700667
Jeff Brown9302c872011-07-13 22:51:29 -0700668 if (windowHandleObjArray) {
669 jsize length = env->GetArrayLength(windowHandleObjArray);
670 for (jsize i = 0; i < length; i++) {
671 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
672 if (! windowHandleObj) {
673 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700674 }
Jeff Brown9302c872011-07-13 22:51:29 -0700675
676 sp<InputWindowHandle> windowHandle =
677 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
678 if (windowHandle != NULL) {
679 windowHandles.push(windowHandle);
680 }
681 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700682 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700683 }
Jeff Brown349703e2010-06-22 01:27:15 -0700684
Jeff Brown9302c872011-07-13 22:51:29 -0700685 mInputManager->getDispatcher()->setInputWindows(windowHandles);
686
687 // Do this after the dispatcher has updated the window handle state.
688 bool newPointerGesturesEnabled = true;
689 size_t numWindows = windowHandles.size();
690 for (size_t i = 0; i < numWindows; i++) {
691 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700692 const InputWindowInfo* windowInfo = windowHandle->getInfo();
693 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
694 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700695 newPointerGesturesEnabled = false;
696 }
697 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700698
699 uint32_t changes = 0;
700 { // acquire lock
701 AutoMutex _l(mLock);
702
703 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
704 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
705 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
706 }
707 } // release lock
708
709 if (changes) {
710 mInputManager->getReader()->requestRefreshConfiguration(changes);
711 }
Jeff Brown349703e2010-06-22 01:27:15 -0700712}
713
Jeff Brown9302c872011-07-13 22:51:29 -0700714void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
715 sp<InputApplicationHandle> applicationHandle =
716 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
717 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700718}
719
720void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700721 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700722}
723
Jeff Brown05dc66a2011-03-02 14:41:58 -0800724void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
725 AutoMutex _l(mLock);
726
727 if (mLocked.systemUiVisibility != visibility) {
728 mLocked.systemUiVisibility = visibility;
729
730 sp<PointerController> controller = mLocked.pointerController.promote();
731 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700732 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800733 }
734 }
735}
736
Jeff Brown2352b972011-04-12 22:39:53 -0700737void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800738 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700739 controller->setInactivityTimeout(lightsOut
740 ? PointerController::INACTIVITY_TIMEOUT_SHORT
741 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800742}
743
Jeff Brown1a84fd12011-06-02 01:26:32 -0700744void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700745 { // acquire lock
746 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700747
Jeff Brown474dcb52011-06-14 20:22:50 -0700748 if (mLocked.pointerSpeed == speed) {
749 return;
750 }
751
Steve Block6215d3f2012-01-04 20:05:49 +0000752 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700753 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700754 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700755
Jeff Brown474dcb52011-06-14 20:22:50 -0700756 mInputManager->getReader()->requestRefreshConfiguration(
757 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700758}
759
Jeff Browndaf4a122011-08-26 17:14:14 -0700760void NativeInputManager::setShowTouches(bool enabled) {
761 { // acquire lock
762 AutoMutex _l(mLock);
763
764 if (mLocked.showTouches == enabled) {
765 return;
766 }
767
Steve Block6215d3f2012-01-04 20:05:49 +0000768 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700769 mLocked.showTouches = enabled;
770 } // release lock
771
772 mInputManager->getReader()->requestRefreshConfiguration(
773 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
774}
775
Jeff Brown037c33e2014-04-09 00:31:55 -0700776void NativeInputManager::setInteractive(bool interactive) {
777 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700778}
779
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800780void NativeInputManager::reloadCalibration() {
781 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100782 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800783}
784
Jun Mukai1db53972015-09-11 18:08:31 -0700785void NativeInputManager::setPointerIconShape(int32_t iconId) {
786 AutoMutex _l(mLock);
787 sp<PointerController> controller = mLocked.pointerController.promote();
788 if (controller != NULL) {
789 // Use 0 (the default icon) for ARROW.
Jun Mukai5ec74202015-10-07 16:58:09 +0900790 controller->updatePointerShape(iconId);
Jun Mukai1db53972015-09-11 18:08:31 -0700791 }
792}
793
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800794TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
795 JNIEnv *env, jfloatArray matrixArr) {
796 ScopedFloatArrayRO matrix(env, matrixArr);
797 assert(matrix.size() == 6);
798
799 TouchAffineTransformation transform;
800 transform.x_scale = matrix[0];
801 transform.x_ymix = matrix[1];
802 transform.x_offset = matrix[2];
803 transform.y_xmix = matrix[3];
804 transform.y_scale = matrix[4];
805 transform.y_offset = matrix[5];
806
807 return transform;
808}
809
810TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700811 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800812 JNIEnv* env = jniEnv();
813
814 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
815
816 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700817 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
818 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800819
820 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
821 gTouchCalibrationClassInfo.getAffineTransform));
822
823 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
824
825 env->DeleteLocalRef(matrixArr);
826 env->DeleteLocalRef(cal);
827
828 return transform;
829}
830
Jeff Brown0029c662011-03-30 02:25:18 -0700831bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
832 jobject inputEventObj;
833
834 JNIEnv* env = jniEnv();
835 switch (inputEvent->getType()) {
836 case AINPUT_EVENT_TYPE_KEY:
837 inputEventObj = android_view_KeyEvent_fromNative(env,
838 static_cast<const KeyEvent*>(inputEvent));
839 break;
840 case AINPUT_EVENT_TYPE_MOTION:
841 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
842 static_cast<const MotionEvent*>(inputEvent));
843 break;
844 default:
845 return true; // dispatch the event normally
846 }
847
848 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000849 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700850 return true; // dispatch the event normally
851 }
852
853 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700854 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700855 inputEventObj, policyFlags);
856 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
857 pass = true;
858 }
859 env->DeleteLocalRef(inputEventObj);
860 return pass;
861}
862
Jeff Brown1f245102010-11-18 20:53:46 -0800863void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
864 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700865 // Policy:
866 // - Ignore untrusted events and pass them along.
867 // - Ask the window manager what to do with normal events and trusted injected events.
868 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100869 bool interactive = mInteractive.load();
870 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700871 policyFlags |= POLICY_FLAG_INTERACTIVE;
872 }
Jeff Brown3122e442010-10-11 23:32:49 -0700873 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800874 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700875 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800876 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
877 jint wmActions;
878 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700879 wmActions = env->CallIntMethod(mServiceObj,
880 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700881 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800882 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
883 wmActions = 0;
884 }
885 android_view_KeyEvent_recycle(env, keyEventObj);
886 env->DeleteLocalRef(keyEventObj);
887 } else {
Steve Block3762c312012-01-06 19:20:56 +0000888 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700889 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700890 }
891
Jeff Brown56194eb2011-03-02 19:23:13 -0800892 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700893 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100894 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -0700895 policyFlags |= POLICY_FLAG_PASS_TO_USER;
896 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700897 }
898}
899
Jeff Brown56194eb2011-03-02 19:23:13 -0800900void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700901 // Policy:
902 // - Ignore untrusted events and pass them along.
903 // - No special filtering for injected events required at this time.
904 // - Filter normal events based on screen state.
905 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100906 bool interactive = mInteractive.load();
907 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700908 policyFlags |= POLICY_FLAG_INTERACTIVE;
909 }
Jeff Brown3122e442010-10-11 23:32:49 -0700910 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700911 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -0700912 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -0700913 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -0800914 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700915 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -0700916 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -0800917 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800918 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -0700919 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800920 wmActions = 0;
921 }
922
Jeff Brown56194eb2011-03-02 19:23:13 -0800923 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700924 }
Jeff Brown3122e442010-10-11 23:32:49 -0700925 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100926 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -0700927 policyFlags |= POLICY_FLAG_PASS_TO_USER;
928 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700929 }
930}
931
Jeff Brown56194eb2011-03-02 19:23:13 -0800932void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
933 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800934 if (wmActions & WM_ACTION_PASS_TO_USER) {
935 policyFlags |= POLICY_FLAG_PASS_TO_USER;
936 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800937#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000938 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800939#endif
940 }
941}
942
Jeff Brown905805a2011-10-12 13:57:59 -0700943nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800944 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700945 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700946 // Policy:
947 // - Ignore untrusted events and pass them along.
948 // - Filter normal events and trusted injected events through the window manager policy to
949 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700950 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700951 if (policyFlags & POLICY_FLAG_TRUSTED) {
952 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700953
Jeff Brown928e0542011-01-10 11:17:36 -0800954 // Note: inputWindowHandle may be null.
955 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800956 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
957 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700958 jlong delayMillis = env->CallLongMethod(mServiceObj,
959 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800960 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800961 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
962 android_view_KeyEvent_recycle(env, keyEventObj);
963 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700964 if (!error) {
965 if (delayMillis < 0) {
966 result = -1;
967 } else if (delayMillis > 0) {
968 result = milliseconds_to_nanoseconds(delayMillis);
969 }
970 }
Jeff Brown1f245102010-11-18 20:53:46 -0800971 } else {
Steve Block3762c312012-01-06 19:20:56 +0000972 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800973 }
Jeff Brown928e0542011-01-10 11:17:36 -0800974 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700975 }
Jeff Brown1f245102010-11-18 20:53:46 -0800976 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700977}
978
Jeff Brown928e0542011-01-10 11:17:36 -0800979bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800980 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700981 // Policy:
982 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800983 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700984 if (policyFlags & POLICY_FLAG_TRUSTED) {
985 JNIEnv* env = jniEnv();
986
Jeff Brown928e0542011-01-10 11:17:36 -0800987 // Note: inputWindowHandle may be null.
988 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800989 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
990 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700991 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
992 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800993 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700994 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
995 fallbackKeyEventObj = NULL;
996 }
Jeff Brown1f245102010-11-18 20:53:46 -0800997 android_view_KeyEvent_recycle(env, keyEventObj);
998 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800999
1000 if (fallbackKeyEventObj) {
1001 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1002 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1003 outFallbackKeyEvent)) {
1004 result = true;
1005 }
1006 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1007 env->DeleteLocalRef(fallbackKeyEventObj);
1008 }
Jeff Brown1f245102010-11-18 20:53:46 -08001009 } else {
Steve Block3762c312012-01-06 19:20:56 +00001010 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001011 }
Jeff Brown928e0542011-01-10 11:17:36 -08001012 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001013 }
Jeff Brown1f245102010-11-18 20:53:46 -08001014 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001015}
1016
Jeff Brown01ce2e92010-09-26 22:20:12 -07001017void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
1018 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001019}
1020
Jeff Brown349703e2010-06-22 01:27:15 -07001021
Jeff Brownb88102f2010-09-08 11:49:43 -07001022bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1023 int32_t injectorPid, int32_t injectorUid) {
1024 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001025 jboolean result = env->CallBooleanMethod(mServiceObj,
1026 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001027 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1028 result = false;
1029 }
Jeff Brown349703e2010-06-22 01:27:15 -07001030 return result;
1031}
1032
Jeff Brown2352b972011-04-12 22:39:53 -07001033void NativeInputManager::loadPointerResources(PointerResources* outResources) {
1034 JNIEnv* env = jniEnv();
1035
1036 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1037 &outResources->spotHover);
1038 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1039 &outResources->spotTouch);
1040 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1041 &outResources->spotAnchor);
1042}
1043
Jun Mukai5ec74202015-10-07 16:58:09 +09001044void NativeInputManager::loadAdditionalMouseResources(std::map<int32_t, SpriteIcon>* outResources) {
Jun Mukai1db53972015-09-11 18:08:31 -07001045 JNIEnv* env = jniEnv();
1046
1047 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1048 ++iconId) {
1049 loadSystemIconAsSprite(env, mContextObj, iconId, &((*outResources)[iconId]));
1050 }
Jun Mukai5ec74202015-10-07 16:58:09 +09001051 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_NULL, &((*outResources)[POINTER_ICON_STYLE_NULL]));
Jun Mukai1db53972015-09-11 18:08:31 -07001052}
1053
Jun Mukai5ec74202015-10-07 16:58:09 +09001054int32_t NativeInputManager::getDefaultPointerIconId() {
1055 return POINTER_ICON_STYLE_ARROW;
1056}
Jeff Brown83c09682010-12-23 17:50:18 -08001057
Jeff Brown9c3cda02010-06-15 01:31:58 -07001058// ----------------------------------------------------------------------------
1059
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001060static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001061 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001062 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001063 if (messageQueue == NULL) {
1064 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1065 return 0;
1066 }
1067
Jeff Brown603b4452012-04-06 17:39:41 -07001068 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1069 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001070 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001071 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001072}
1073
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001074static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001075 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001076
Jeff Brown4532e612012-04-05 14:27:12 -07001077 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001078 if (result) {
1079 jniThrowRuntimeException(env, "Input manager could not be started.");
1080 }
1081}
1082
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001083static void nativeSetDisplayViewport(JNIEnv* /* env */, jclass /* clazz */, jlong ptr,
1084 jboolean external, jint displayId, jint orientation,
Jeff Brownd728bf52012-09-08 18:05:28 -07001085 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001086 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
1087 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001088 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001089
Jeff Brownd728bf52012-09-08 18:05:28 -07001090 DisplayViewport v;
1091 v.displayId = displayId;
1092 v.orientation = orientation;
1093 v.logicalLeft = logicalLeft;
1094 v.logicalTop = logicalTop;
1095 v.logicalRight = logicalRight;
1096 v.logicalBottom = logicalBottom;
1097 v.physicalLeft = physicalLeft;
1098 v.physicalTop = physicalTop;
1099 v.physicalRight = physicalRight;
1100 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001101 v.deviceWidth = deviceWidth;
1102 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -07001103 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001104}
1105
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001106static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001107 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001108 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001109
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001110 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001111 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001112}
1113
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001114static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001115 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001116 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001117
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001118 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001119 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001120}
1121
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001122static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001123 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001124 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001125
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001126 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001127 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001128}
1129
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001130static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001131 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001132 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001133
1134 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1135 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1136 jsize numCodes = env->GetArrayLength(keyCodes);
1137 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001138 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001139 if (im->getInputManager()->getReader()->hasKeys(
1140 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1141 result = JNI_TRUE;
1142 } else {
1143 result = JNI_FALSE;
1144 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001145 } else {
1146 result = JNI_FALSE;
1147 }
1148
1149 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1150 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1151 return result;
1152}
1153
1154static void throwInputChannelNotInitialized(JNIEnv* env) {
1155 jniThrowException(env, "java/lang/IllegalStateException",
1156 "inputChannel is not initialized");
1157}
1158
Jeff Brown4532e612012-04-05 14:27:12 -07001159static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001160 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001161 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1162
Steve Block8564c8d2012-01-05 23:22:43 +00001163 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001164 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001165 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001166}
1167
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001168static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001169 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001170 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001171
1172 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1173 inputChannelObj);
1174 if (inputChannel == NULL) {
1175 throwInputChannelNotInitialized(env);
1176 return;
1177 }
1178
Jeff Brown928e0542011-01-10 11:17:36 -08001179 sp<InputWindowHandle> inputWindowHandle =
1180 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001181
Jeff Brown4532e612012-04-05 14:27:12 -07001182 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001183 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001184 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001185 String8 message;
1186 message.appendFormat("Failed to register input channel. status=%d", status);
1187 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001188 return;
1189 }
1190
Jeff Browna41ca772010-08-11 14:46:32 -07001191 if (! monitor) {
1192 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001193 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001194 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001195}
1196
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001197static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001198 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001199 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001200
1201 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1202 inputChannelObj);
1203 if (inputChannel == NULL) {
1204 throwInputChannelNotInitialized(env);
1205 return;
1206 }
1207
1208 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1209
Jeff Brown4532e612012-04-05 14:27:12 -07001210 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001211 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1212 String8 message;
1213 message.appendFormat("Failed to unregister input channel. status=%d", status);
1214 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001215 }
1216}
1217
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001218static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001219 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001220 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001221
Jeff Brown4532e612012-04-05 14:27:12 -07001222 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001223}
1224
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001225static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Jeff Brownca9bc702014-02-11 14:32:56 -08001226 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001227 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001228 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001229
Jeff Brown6ec402b2010-07-28 15:48:59 -07001230 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1231 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001232 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1233 if (status) {
1234 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1235 return INPUT_EVENT_INJECTION_FAILED;
1236 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001237
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001238 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001239 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001240 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001241 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001242 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1243 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001244 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1245 return INPUT_EVENT_INJECTION_FAILED;
1246 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001247
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001248 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001249 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001250 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001251 } else {
1252 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001253 return INPUT_EVENT_INJECTION_FAILED;
1254 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001255}
1256
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001257static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001258 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001259 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001260
Jeff Brown4532e612012-04-05 14:27:12 -07001261 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001262}
1263
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001264static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001265 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001266 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001267
Jeff Brown4532e612012-04-05 14:27:12 -07001268 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001269}
1270
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001271static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1272 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001273 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001274
Jeff Brown4532e612012-04-05 14:27:12 -07001275 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001276}
1277
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001278static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1279 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001280 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001281
Jeff Brown4532e612012-04-05 14:27:12 -07001282 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001283}
1284
Jeff Brown4532e612012-04-05 14:27:12 -07001285static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001286 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001287 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001288
1289 sp<InputChannel> fromChannel =
1290 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1291 sp<InputChannel> toChannel =
1292 android_view_InputChannel_getInputChannel(env, toChannelObj);
1293
1294 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001295 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001296 }
1297
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001298 if (im->getInputManager()->getDispatcher()->
1299 transferTouchFocus(fromChannel, toChannel)) {
1300 return JNI_TRUE;
1301 } else {
1302 return JNI_FALSE;
1303 }
Jeff Browne6504122010-09-27 14:52:15 -07001304}
1305
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001306static void nativeSetPointerSpeed(JNIEnv* /* env */,
1307 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001308 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001309
Jeff Brown4532e612012-04-05 14:27:12 -07001310 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001311}
1312
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001313static void nativeSetShowTouches(JNIEnv* /* env */,
1314 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001315 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001316
Jeff Brown4532e612012-04-05 14:27:12 -07001317 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001318}
1319
Jeff Brown037c33e2014-04-09 00:31:55 -07001320static void nativeSetInteractive(JNIEnv* env,
1321 jclass clazz, jlong ptr, jboolean interactive) {
1322 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1323
1324 im->setInteractive(interactive);
1325}
1326
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001327static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1328 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1329 im->reloadCalibration();
1330}
1331
Jeff Browna47425a2012-04-13 04:09:27 -07001332static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001333 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001334 jint repeat, jint token) {
1335 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1336
1337 size_t patternSize = env->GetArrayLength(patternObj);
1338 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001339 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001340 "which is more than the maximum supported size of %d.",
1341 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1342 return; // limit to reasonable size
1343 }
1344
1345 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1346 patternObj, NULL));
1347 nsecs_t pattern[patternSize];
1348 for (size_t i = 0; i < patternSize; i++) {
1349 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001350 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001351 }
1352 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1353
1354 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1355}
1356
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001357static void nativeCancelVibrate(JNIEnv* /* env */,
1358 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001359 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1360
1361 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1362}
1363
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001364static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1365 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001366 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1367
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001368 im->getInputManager()->getReader()->requestRefreshConfiguration(
1369 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1370}
1371
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001372static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1373 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001374 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1375
1376 im->getInputManager()->getReader()->requestRefreshConfiguration(
1377 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001378}
1379
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001380static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001381 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001382
Jeff Brownb88102f2010-09-08 11:49:43 -07001383 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001384 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001385 return env->NewStringUTF(dump.string());
1386}
1387
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001388static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001389 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001390
Jeff Brown4532e612012-04-05 14:27:12 -07001391 im->getInputManager()->getReader()->monitor();
1392 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001393}
1394
Jun Mukai1db53972015-09-11 18:08:31 -07001395static void nativeSetPointerIconShape(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
1396 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1397 im->setPointerIconShape(iconId);
1398}
1399
Jeff Brown9c3cda02010-06-15 01:31:58 -07001400// ----------------------------------------------------------------------------
1401
Daniel Micay76f6a862015-09-19 17:31:01 -04001402static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001403 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001404 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001405 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001406 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001407 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001408 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001409 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001410 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001411 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001412 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001413 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001414 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001415 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001416 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001417 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001418 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001419 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001420 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001421 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001422 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001423 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001424 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001425 (void*) nativeSetInputFilterEnabled },
Jeff Brownca9bc702014-02-11 14:32:56 -08001426 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001427 (void*) nativeInjectInputEvent },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001428 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001429 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001430 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001431 (void*) nativeSetFocusedApplication },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001432 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001433 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001434 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001435 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001436 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001437 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001438 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001439 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001440 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001441 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001442 { "nativeSetInteractive", "(JZ)V",
1443 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001444 { "nativeReloadCalibration", "(J)V",
1445 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001446 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001447 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001448 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001449 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001450 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001451 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001452 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001453 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001454 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001455 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001456 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001457 (void*) nativeMonitor },
Jun Mukai1db53972015-09-11 18:08:31 -07001458 { "nativeSetPointerIconShape", "(JI)V",
1459 (void*) nativeSetPointerIconShape },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001460};
1461
1462#define FIND_CLASS(var, className) \
1463 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001464 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001465
1466#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1467 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1468 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1469
1470#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1471 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1472 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1473
1474int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001475 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001476 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001477 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001478 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1479
Jeff Brown9c3cda02010-06-15 01:31:58 -07001480 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001481
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001482 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001483 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001484
Jeff Brown4532e612012-04-05 14:27:12 -07001485 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001486 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001487
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001488 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1489 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1490
Jeff Brown53384282012-08-20 20:16:01 -07001491 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1492 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001493
Jeff Brown4532e612012-04-05 14:27:12 -07001494 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1495 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001496
Jeff Brown4532e612012-04-05 14:27:12 -07001497 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001498 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001499 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001500
Jeff Brown4532e612012-04-05 14:27:12 -07001501 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001502 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1503
Jeff Brown4532e612012-04-05 14:27:12 -07001504 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001505 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001506
Michael Wright70af00a2014-09-03 19:30:20 -07001507 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1508 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001509
Jeff Brown4532e612012-04-05 14:27:12 -07001510 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001511 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001512 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001513
Jeff Brown4532e612012-04-05 14:27:12 -07001514 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001515 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001516 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001517
Jeff Brown4532e612012-04-05 14:27:12 -07001518 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001519 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001520
Jeff Brown4532e612012-04-05 14:27:12 -07001521 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001522 "getVirtualKeyQuietTimeMillis", "()I");
1523
Jeff Brown4532e612012-04-05 14:27:12 -07001524 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001525 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1526
Jeff Brown4532e612012-04-05 14:27:12 -07001527 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001528 "getKeyRepeatTimeout", "()I");
1529
Jeff Brown4532e612012-04-05 14:27:12 -07001530 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001531 "getKeyRepeatDelay", "()I");
1532
Jeff Brown4532e612012-04-05 14:27:12 -07001533 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001534 "getHoverTapTimeout", "()I");
1535
Jeff Brown4532e612012-04-05 14:27:12 -07001536 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001537 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001538
Jeff Brown4532e612012-04-05 14:27:12 -07001539 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001540 "getDoubleTapTimeout", "()I");
1541
Jeff Brown4532e612012-04-05 14:27:12 -07001542 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001543 "getLongPressTimeout", "()I");
1544
Jeff Brown4532e612012-04-05 14:27:12 -07001545 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001546 "getPointerLayer", "()I");
1547
Jeff Brown4532e612012-04-05 14:27:12 -07001548 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001549 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001550
Jeff Brown6ec6f792012-04-17 16:52:41 -07001551 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001552 "getKeyboardLayoutOverlay",
1553 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001554
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001555 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1556 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1557
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001558 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1559 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001560 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001561
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001562 // InputDevice
1563
1564 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1565 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1566
Jeff Brown6ec402b2010-07-28 15:48:59 -07001567 // KeyEvent
1568
1569 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001570 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1571
Jeff Brown8d608662010-08-30 03:02:23 -07001572 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001573
1574 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001575 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001576
RoboErikfb290df2013-12-16 11:27:55 -08001577 // InputDeviceIdentifier
1578
1579 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1580 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1581 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1582 "<init>", "(Ljava/lang/String;II)V");
1583
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001584 // TouchCalibration
1585
1586 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1587 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1588
1589 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1590 "getAffineTransform", "()[F");
1591
Jeff Brown46b9ac02010-04-22 18:58:52 -07001592 return 0;
1593}
1594
Jeff Brown46b9ac02010-04-22 18:58:52 -07001595} /* namespace android */