blob: 697f56de76d2aa6c516acdaae01d0cf45ae8fbd8 [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"
Jeff Brown349703e2010-06-22 01:27:15 -070030#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070032#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080033
Jeff Brown46b9ac02010-04-22 18:58:52 -070034#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080035#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <utils/threads.h>
Jeff Brown83c09682010-12-23 17:50:18 -080037
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038#include <input/InputManager.h>
39#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070040#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080041
Jeff Brown05dc66a2011-03-02 14:41:58 -080042#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070043#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080044#include <android_view_KeyEvent.h>
45#include <android_view_MotionEvent.h>
46#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070047#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080048#include <android/graphics/GraphicsJNI.h>
49
Jeff Brown6ec6f792012-04-17 16:52:41 -070050#include <ScopedLocalRef.h>
51#include <ScopedUtfChars.h>
52
Jeff Brown4f8ecd82012-06-18 18:29:13 -070053#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070054#include "com_android_server_input_InputApplicationHandle.h"
55#include "com_android_server_input_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070056
57namespace android {
58
Jeff Brown1a84fd12011-06-02 01:26:32 -070059// The exponent used to calculate the pointer speed scaling factor.
60// The scaling factor is calculated as 2 ^ (speed * exponent),
61// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070062static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070063
Jeff Brown46b9ac02010-04-22 18:58:52 -070064static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070065 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070066 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070067 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070068 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070069 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070070 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070071 jmethodID interceptKeyBeforeQueueing;
Jeff Brown56194eb2011-03-02 19:23:13 -080072 jmethodID interceptMotionBeforeQueueingWhenScreenOff;
Jeff Brown349703e2010-06-22 01:27:15 -070073 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070074 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070075 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080076 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070077 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080078 jmethodID getKeyRepeatTimeout;
79 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070080 jmethodID getHoverTapTimeout;
81 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070082 jmethodID getDoubleTapTimeout;
83 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080084 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080085 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070086 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070087 jmethodID getDeviceAlias;
Jeff Brown4532e612012-04-05 14:27:12 -070088} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070089
90static struct {
91 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070092} gInputDeviceClassInfo;
93
94static struct {
95 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070096} gKeyEventClassInfo;
97
98static struct {
99 jclass clazz;
100} gMotionEventClassInfo;
101
Jeff Brown928e0542011-01-10 11:17:36 -0800102
103// --- Global functions ---
104
Jeff Brown214eaf42011-05-26 19:17:02 -0700105template<typename T>
106inline static T min(const T& a, const T& b) {
107 return a < b ? a : b;
108}
109
110template<typename T>
111inline static T max(const T& a, const T& b) {
112 return a > b ? a : b;
113}
114
Jeff Brown928e0542011-01-10 11:17:36 -0800115static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
116 const sp<InputApplicationHandle>& inputApplicationHandle) {
117 if (inputApplicationHandle == NULL) {
118 return NULL;
119 }
120 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
121 getInputApplicationHandleObjLocalRef(env);
122}
123
124static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
125 const sp<InputWindowHandle>& inputWindowHandle) {
126 if (inputWindowHandle == NULL) {
127 return NULL;
128 }
129 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
130 getInputWindowHandleObjLocalRef(env);
131}
132
Jeff Brown2352b972011-04-12 22:39:53 -0700133static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
134 SpriteIcon* outSpriteIcon) {
135 PointerIcon pointerIcon;
136 status_t status = android_view_PointerIcon_loadSystemIcon(env,
137 contextObj, style, &pointerIcon);
138 if (!status) {
139 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
140 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
141 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
142 }
143}
144
Jeff Brown905805a2011-10-12 13:57:59 -0700145enum {
146 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700147};
148
Jeff Brown928e0542011-01-10 11:17:36 -0800149
150// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800151
Jeff Brown9c3cda02010-06-15 01:31:58 -0700152class NativeInputManager : public virtual RefBase,
153 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700154 public virtual InputDispatcherPolicyInterface,
155 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700156protected:
157 virtual ~NativeInputManager();
158
159public:
Jeff Brown4532e612012-04-05 14:27:12 -0700160 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700161
162 inline sp<InputManager> getInputManager() const { return mInputManager; }
163
Jeff Brownb88102f2010-09-08 11:49:43 -0700164 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700165
Jeff Brownd728bf52012-09-08 18:05:28 -0700166 void setDisplayViewport(bool external, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700167
Jeff Brown7fbdc842010-06-17 20:52:56 -0700168 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800169 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700170 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
171
Jeff Brown9302c872011-07-13 22:51:29 -0700172 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
173 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700174 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800175 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700176 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700177 void setShowTouches(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700178
Jeff Brown9c3cda02010-06-15 01:31:58 -0700179 /* --- InputReaderPolicyInterface implementation --- */
180
Jeff Brown214eaf42011-05-26 19:17:02 -0700181 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800182 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700183 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700184 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const String8& inputDeviceDescriptor);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700185 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700186
187 /* --- InputDispatcherPolicyInterface implementation --- */
188
Jeff Brownbcc046a2012-09-27 20:46:43 -0700189 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700190 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700191 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700192 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700193 const sp<InputWindowHandle>& inputWindowHandle,
194 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800195 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700196 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700197 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
198 virtual bool isKeyRepeatEnabled();
Jeff Brown1f245102010-11-18 20:53:46 -0800199 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800200 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700201 virtual nsecs_t interceptKeyBeforeDispatching(
202 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700203 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800204 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800205 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700206 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700207 virtual bool checkInjectEventsPermissionNonReentrant(
208 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700209
Jeff Brown2352b972011-04-12 22:39:53 -0700210 /* --- PointerControllerPolicyInterface implementation --- */
211
212 virtual void loadPointerResources(PointerResources* outResources);
213
Jeff Brown9c3cda02010-06-15 01:31:58 -0700214private:
215 sp<InputManager> mInputManager;
216
Jeff Brown2352b972011-04-12 22:39:53 -0700217 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700218 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800219 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700220
Jeff Brown83c09682010-12-23 17:50:18 -0800221 Mutex mLock;
222 struct Locked {
223 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700224 DisplayViewport internalViewport;
225 DisplayViewport externalViewport;
Jeff Brown83c09682010-12-23 17:50:18 -0800226
Jeff Brown05dc66a2011-03-02 14:41:58 -0800227 // System UI visibility.
228 int32_t systemUiVisibility;
229
Jeff Brown1a84fd12011-06-02 01:26:32 -0700230 // Pointer speed.
231 int32_t pointerSpeed;
232
Jeff Brown474dcb52011-06-14 20:22:50 -0700233 // True if pointer gestures are enabled.
234 bool pointerGesturesEnabled;
235
Jeff Browndaf4a122011-08-26 17:14:14 -0700236 // Show touches feature enable/disable.
237 bool showTouches;
238
Jeff Brown5541de92011-04-11 11:54:25 -0700239 // Sprite controller singleton, created on first use.
240 sp<SpriteController> spriteController;
241
Jeff Brown83c09682010-12-23 17:50:18 -0800242 // Pointer controller singleton, created and destroyed as needed.
243 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800244 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700245
Jeff Brown2352b972011-04-12 22:39:53 -0700246 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800247 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700248 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800249
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700250 // Power manager interactions.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700251 bool isScreenOn();
252 bool isScreenBright();
253
Jeff Brownb88102f2010-09-08 11:49:43 -0700254 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700255
Jeff Brown9c3cda02010-06-15 01:31:58 -0700256 static inline JNIEnv* jniEnv() {
257 return AndroidRuntime::getJNIEnv();
258 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700259};
260
Jeff Brown928e0542011-01-10 11:17:36 -0800261
Jeff Brown9c3cda02010-06-15 01:31:58 -0700262
Jeff Brown2352b972011-04-12 22:39:53 -0700263NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700264 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown214eaf42011-05-26 19:17:02 -0700265 mLooper(looper) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700266 JNIEnv* env = jniEnv();
267
Jeff Brown2352b972011-04-12 22:39:53 -0700268 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700269 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270
Jeff Brown83c09682010-12-23 17:50:18 -0800271 {
272 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800273 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700274 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700275 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700276 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800277 }
278
Jeff Brown9c3cda02010-06-15 01:31:58 -0700279 sp<EventHub> eventHub = new EventHub();
280 mInputManager = new InputManager(eventHub, this, this);
281}
282
283NativeInputManager::~NativeInputManager() {
284 JNIEnv* env = jniEnv();
285
Jeff Brown2352b972011-04-12 22:39:53 -0700286 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700287 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700288}
289
Jeff Brownb88102f2010-09-08 11:49:43 -0700290void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700291 mInputManager->getReader()->dump(dump);
292 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700293
Jeff Brownb88102f2010-09-08 11:49:43 -0700294 mInputManager->getDispatcher()->dump(dump);
295 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700296}
297
Jeff Brown7fbdc842010-06-17 20:52:56 -0700298bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700299 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000300 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700301 LOGE_EX(env);
302 env->ExceptionClear();
303 return true;
304 }
305 return false;
306}
307
Jeff Brownd728bf52012-09-08 18:05:28 -0700308void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700309 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700310 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700311 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700312
Jeff Brownd728bf52012-09-08 18:05:28 -0700313 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
314 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700315 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700316 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700317
Jeff Brownd728bf52012-09-08 18:05:28 -0700318 if (!external) {
319 sp<PointerController> controller = mLocked.pointerController.promote();
320 if (controller != NULL) {
321 controller->setDisplayViewport(
322 viewport.logicalRight - viewport.logicalLeft,
323 viewport.logicalBottom - viewport.logicalTop,
324 viewport.orientation);
325 }
Jeff Brown2352b972011-04-12 22:39:53 -0700326 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700327 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700328 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700329
330 if (changed) {
331 mInputManager->getReader()->requestRefreshConfiguration(
332 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
333 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700334}
335
Jeff Brown7fbdc842010-06-17 20:52:56 -0700336status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Brown928e0542011-01-10 11:17:36 -0800337 const sp<InputChannel>& inputChannel,
338 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
339 return mInputManager->getDispatcher()->registerInputChannel(
340 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700341}
342
343status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
344 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700345 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700346}
347
Jeff Brown214eaf42011-05-26 19:17:02 -0700348void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700349 JNIEnv* env = jniEnv();
350
Jeff Brown4532e612012-04-05 14:27:12 -0700351 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
352 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700353 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
354 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
355 }
356
357 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700358 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
359 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700360 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
361 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700362 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700363 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700364 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700365 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700366 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700367 env->DeleteLocalRef(item);
368 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700369 env->DeleteLocalRef(excludedDeviceNames);
370 }
371
Jeff Brown4532e612012-04-05 14:27:12 -0700372 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
373 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700374 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700375 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
376 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700377 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700378 jint longPressTimeout = env->CallIntMethod(mServiceObj,
379 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700380 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700381 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700382
383 // We must ensure that the tap-drag interval is significantly shorter than
384 // the long-press timeout because the tap is held down for the entire duration
385 // of the double-tap timeout.
386 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700387 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700388 outConfig->pointerGestureTapDragInterval =
389 milliseconds_to_nanoseconds(tapDragInterval);
390 }
391 }
392 }
393
Jeff Brown4532e612012-04-05 14:27:12 -0700394 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
395 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700396 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
397 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700398 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700399
400 { // acquire lock
401 AutoMutex _l(mLock);
402
403 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
404 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700405 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700406
Jeff Browndaf4a122011-08-26 17:14:14 -0700407 outConfig->showTouches = mLocked.showTouches;
408
Jeff Brownd728bf52012-09-08 18:05:28 -0700409 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
410 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700411 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700412}
413
Jeff Brown83c09682010-12-23 17:50:18 -0800414sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
415 AutoMutex _l(mLock);
416
417 sp<PointerController> controller = mLocked.pointerController.promote();
418 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700419 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800420
Jeff Brown2352b972011-04-12 22:39:53 -0700421 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800422 mLocked.pointerController = controller;
423
Jeff Brownd728bf52012-09-08 18:05:28 -0700424 DisplayViewport& v = mLocked.internalViewport;
425 controller->setDisplayViewport(
426 v.logicalRight - v.logicalLeft,
427 v.logicalBottom - v.logicalTop,
428 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800429
Jeff Brown5541de92011-04-11 11:54:25 -0700430 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700431 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
432 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700433 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
434 PointerIcon pointerIcon;
435 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
436 mContextObj, &pointerIcon);
437 if (!status && !pointerIcon.isNullIcon()) {
438 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
439 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
440 } else {
441 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800442 }
Jeff Brown2352b972011-04-12 22:39:53 -0700443 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800444 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800445
Jeff Brown2352b972011-04-12 22:39:53 -0700446 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800447 }
448 return controller;
449}
450
Jeff Brown5541de92011-04-11 11:54:25 -0700451void NativeInputManager::ensureSpriteControllerLocked() {
452 if (mLocked.spriteController == NULL) {
453 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700454 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700455 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
456 layer = -1;
457 }
458 mLocked.spriteController = new SpriteController(mLooper, layer);
459 }
460}
461
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700462void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
463 JNIEnv* env = jniEnv();
464
465 size_t count = inputDevices.size();
466 jobjectArray inputDevicesObjArray = env->NewObjectArray(
467 count, gInputDeviceClassInfo.clazz, NULL);
468 if (inputDevicesObjArray) {
469 bool error = false;
470 for (size_t i = 0; i < count; i++) {
471 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
472 if (!inputDeviceObj) {
473 error = true;
474 break;
475 }
476
477 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
478 env->DeleteLocalRef(inputDeviceObj);
479 }
480
481 if (!error) {
482 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
483 inputDevicesObjArray);
484 }
485
486 env->DeleteLocalRef(inputDevicesObjArray);
487 }
488
489 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
490}
491
Jeff Brown6ec6f792012-04-17 16:52:41 -0700492sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
493 const String8& inputDeviceDescriptor) {
494 JNIEnv* env = jniEnv();
495
496 sp<KeyCharacterMap> result;
497 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
498 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
499 gServiceClassInfo.getKeyboardLayoutOverlay, descriptorObj.get())));
500 if (arrayObj.get()) {
501 ScopedLocalRef<jstring> filenameObj(env,
502 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
503 ScopedLocalRef<jstring> contentsObj(env,
504 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
505 ScopedUtfChars filenameChars(env, filenameObj.get());
506 ScopedUtfChars contentsChars(env, contentsObj.get());
507
508 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
509 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
510 }
511 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
512 return result;
513}
514
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700515String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
516 JNIEnv* env = jniEnv();
517
518 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
519 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
520 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
521 String8 result;
522 if (aliasObj.get()) {
523 ScopedUtfChars aliasChars(env, aliasObj.get());
524 result.setTo(aliasChars.c_str());
525 }
526 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
527 return result;
528}
529
Jeff Brownbcc046a2012-09-27 20:46:43 -0700530void NativeInputManager::notifySwitch(nsecs_t when,
531 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700532#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700533 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
534 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700535#endif
536
537 JNIEnv* env = jniEnv();
538
Jeff Brown53384282012-08-20 20:16:01 -0700539 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700540 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700541 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700542}
543
Jeff Brown9c3cda02010-06-15 01:31:58 -0700544void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
545#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000546 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700547#endif
548
549 JNIEnv* env = jniEnv();
550
Jeff Brown4532e612012-04-05 14:27:12 -0700551 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700552 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700553}
554
Jeff Brown519e0242010-09-15 15:18:56 -0700555nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700556 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700557#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000558 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700559#endif
560
561 JNIEnv* env = jniEnv();
562
Jeff Brown928e0542011-01-10 11:17:36 -0800563 jobject inputApplicationHandleObj =
564 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
565 jobject inputWindowHandleObj =
566 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700567 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700568
Jeff Brown4532e612012-04-05 14:27:12 -0700569 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700570 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
571 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700572 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
573 newTimeout = 0; // abort dispatch
574 } else {
575 assert(newTimeout >= 0);
576 }
577
Jeff Brownbd181bb2013-09-10 16:44:24 -0700578 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800579 env->DeleteLocalRef(inputWindowHandleObj);
580 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700581 return newTimeout;
582}
583
Jeff Brown928e0542011-01-10 11:17:36 -0800584void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700585#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000586 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700587#endif
588
Jeff Brown7fbdc842010-06-17 20:52:56 -0700589 JNIEnv* env = jniEnv();
590
Jeff Brown928e0542011-01-10 11:17:36 -0800591 jobject inputWindowHandleObj =
592 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
593 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700594 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800595 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700596 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
597
Jeff Brown928e0542011-01-10 11:17:36 -0800598 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700599 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700600}
601
Jeff Brown214eaf42011-05-26 19:17:02 -0700602void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
603 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800604
Jeff Brown4532e612012-04-05 14:27:12 -0700605 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
606 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700607 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
608 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
609 }
Jeff Browna4547672011-03-02 21:38:11 -0800610
Jeff Brown4532e612012-04-05 14:27:12 -0700611 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
612 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700613 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
614 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
615 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700616}
617
Jeff Brown214eaf42011-05-26 19:17:02 -0700618bool NativeInputManager::isKeyRepeatEnabled() {
619 // Only enable automatic key repeating when the screen is on.
620 return isScreenOn();
Jeff Brownae9fc032010-08-18 15:51:08 -0700621}
622
Jeff Brown9302c872011-07-13 22:51:29 -0700623void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
624 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700625
Jeff Brown9302c872011-07-13 22:51:29 -0700626 if (windowHandleObjArray) {
627 jsize length = env->GetArrayLength(windowHandleObjArray);
628 for (jsize i = 0; i < length; i++) {
629 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
630 if (! windowHandleObj) {
631 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700632 }
Jeff Brown9302c872011-07-13 22:51:29 -0700633
634 sp<InputWindowHandle> windowHandle =
635 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
636 if (windowHandle != NULL) {
637 windowHandles.push(windowHandle);
638 }
639 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700640 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700641 }
Jeff Brown349703e2010-06-22 01:27:15 -0700642
Jeff Brown9302c872011-07-13 22:51:29 -0700643 mInputManager->getDispatcher()->setInputWindows(windowHandles);
644
645 // Do this after the dispatcher has updated the window handle state.
646 bool newPointerGesturesEnabled = true;
647 size_t numWindows = windowHandles.size();
648 for (size_t i = 0; i < numWindows; i++) {
649 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700650 const InputWindowInfo* windowInfo = windowHandle->getInfo();
651 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
652 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700653 newPointerGesturesEnabled = false;
654 }
655 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700656
657 uint32_t changes = 0;
658 { // acquire lock
659 AutoMutex _l(mLock);
660
661 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
662 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
663 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
664 }
665 } // release lock
666
667 if (changes) {
668 mInputManager->getReader()->requestRefreshConfiguration(changes);
669 }
Jeff Brown349703e2010-06-22 01:27:15 -0700670}
671
Jeff Brown9302c872011-07-13 22:51:29 -0700672void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
673 sp<InputApplicationHandle> applicationHandle =
674 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
675 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700676}
677
678void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700679 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700680}
681
Jeff Brown05dc66a2011-03-02 14:41:58 -0800682void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
683 AutoMutex _l(mLock);
684
685 if (mLocked.systemUiVisibility != visibility) {
686 mLocked.systemUiVisibility = visibility;
687
688 sp<PointerController> controller = mLocked.pointerController.promote();
689 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700690 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800691 }
692 }
693}
694
Jeff Brown2352b972011-04-12 22:39:53 -0700695void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800696 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700697 controller->setInactivityTimeout(lightsOut
698 ? PointerController::INACTIVITY_TIMEOUT_SHORT
699 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800700}
701
Jeff Brown1a84fd12011-06-02 01:26:32 -0700702void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700703 { // acquire lock
704 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700705
Jeff Brown474dcb52011-06-14 20:22:50 -0700706 if (mLocked.pointerSpeed == speed) {
707 return;
708 }
709
Steve Block6215d3f2012-01-04 20:05:49 +0000710 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700711 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700712 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700713
Jeff Brown474dcb52011-06-14 20:22:50 -0700714 mInputManager->getReader()->requestRefreshConfiguration(
715 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700716}
717
Jeff Browndaf4a122011-08-26 17:14:14 -0700718void NativeInputManager::setShowTouches(bool enabled) {
719 { // acquire lock
720 AutoMutex _l(mLock);
721
722 if (mLocked.showTouches == enabled) {
723 return;
724 }
725
Steve Block6215d3f2012-01-04 20:05:49 +0000726 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700727 mLocked.showTouches = enabled;
728 } // release lock
729
730 mInputManager->getReader()->requestRefreshConfiguration(
731 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
732}
733
Jeff Browne20c9e02010-10-11 14:20:19 -0700734bool NativeInputManager::isScreenOn() {
735 return android_server_PowerManagerService_isScreenOn();
736}
737
738bool NativeInputManager::isScreenBright() {
739 return android_server_PowerManagerService_isScreenBright();
740}
741
Jeff Brown0029c662011-03-30 02:25:18 -0700742bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
743 jobject inputEventObj;
744
745 JNIEnv* env = jniEnv();
746 switch (inputEvent->getType()) {
747 case AINPUT_EVENT_TYPE_KEY:
748 inputEventObj = android_view_KeyEvent_fromNative(env,
749 static_cast<const KeyEvent*>(inputEvent));
750 break;
751 case AINPUT_EVENT_TYPE_MOTION:
752 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
753 static_cast<const MotionEvent*>(inputEvent));
754 break;
755 default:
756 return true; // dispatch the event normally
757 }
758
759 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000760 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700761 return true; // dispatch the event normally
762 }
763
764 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700765 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700766 inputEventObj, policyFlags);
767 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
768 pass = true;
769 }
770 env->DeleteLocalRef(inputEventObj);
771 return pass;
772}
773
Jeff Brown1f245102010-11-18 20:53:46 -0800774void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
775 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700776 // Policy:
777 // - Ignore untrusted events and pass them along.
778 // - Ask the window manager what to do with normal events and trusted injected events.
779 // - For normal events wake and brighten the screen if currently off or dim.
780 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800781 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700782 bool isScreenOn = this->isScreenOn();
783 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700784
Jeff Brown3122e442010-10-11 23:32:49 -0700785 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800786 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
787 jint wmActions;
788 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700789 wmActions = env->CallIntMethod(mServiceObj,
790 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown1f245102010-11-18 20:53:46 -0800791 keyEventObj, policyFlags, isScreenOn);
792 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
793 wmActions = 0;
794 }
795 android_view_KeyEvent_recycle(env, keyEventObj);
796 env->DeleteLocalRef(keyEventObj);
797 } else {
Steve Block3762c312012-01-06 19:20:56 +0000798 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700799 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700800 }
801
Jeff Brown1f245102010-11-18 20:53:46 -0800802 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700803 if (!isScreenOn) {
804 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700805 }
806
807 if (!isScreenBright) {
808 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
809 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700810 }
811
Jeff Brown56194eb2011-03-02 19:23:13 -0800812 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700813 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700814 policyFlags |= POLICY_FLAG_PASS_TO_USER;
815 }
816}
817
Jeff Brown56194eb2011-03-02 19:23:13 -0800818void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700819 // Policy:
820 // - Ignore untrusted events and pass them along.
821 // - No special filtering for injected events required at this time.
822 // - Filter normal events based on screen state.
823 // - For normal events brighten (but do not wake) the screen if currently dim.
824 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
825 if (isScreenOn()) {
826 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700827
Jeff Brown3122e442010-10-11 23:32:49 -0700828 if (!isScreenBright()) {
829 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
830 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800831 } else {
832 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700833 jint wmActions = env->CallIntMethod(mServiceObj,
834 gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Jeff Brown26875502014-01-30 21:47:47 -0800835 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800836 if (checkAndClearExceptionFromCallback(env,
837 "interceptMotionBeforeQueueingWhenScreenOff")) {
838 wmActions = 0;
839 }
840
841 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
842 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700843 }
Jeff Brown3122e442010-10-11 23:32:49 -0700844 } else {
845 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700846 }
847}
848
Jeff Brown56194eb2011-03-02 19:23:13 -0800849void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
850 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800851 if (wmActions & WM_ACTION_PASS_TO_USER) {
852 policyFlags |= POLICY_FLAG_PASS_TO_USER;
853 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800854#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000855 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800856#endif
857 }
858}
859
Jeff Brown905805a2011-10-12 13:57:59 -0700860nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800861 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700862 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700863 // Policy:
864 // - Ignore untrusted events and pass them along.
865 // - Filter normal events and trusted injected events through the window manager policy to
866 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700867 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700868 if (policyFlags & POLICY_FLAG_TRUSTED) {
869 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700870
Jeff Brown928e0542011-01-10 11:17:36 -0800871 // Note: inputWindowHandle may be null.
872 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800873 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
874 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700875 jlong delayMillis = env->CallLongMethod(mServiceObj,
876 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800877 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800878 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
879 android_view_KeyEvent_recycle(env, keyEventObj);
880 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700881 if (!error) {
882 if (delayMillis < 0) {
883 result = -1;
884 } else if (delayMillis > 0) {
885 result = milliseconds_to_nanoseconds(delayMillis);
886 }
887 }
Jeff Brown1f245102010-11-18 20:53:46 -0800888 } else {
Steve Block3762c312012-01-06 19:20:56 +0000889 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800890 }
Jeff Brown928e0542011-01-10 11:17:36 -0800891 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700892 }
Jeff Brown1f245102010-11-18 20:53:46 -0800893 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700894}
895
Jeff Brown928e0542011-01-10 11:17:36 -0800896bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800897 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700898 // Policy:
899 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800900 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700901 if (policyFlags & POLICY_FLAG_TRUSTED) {
902 JNIEnv* env = jniEnv();
903
Jeff Brown928e0542011-01-10 11:17:36 -0800904 // Note: inputWindowHandle may be null.
905 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800906 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
907 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700908 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
909 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800910 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700911 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
912 fallbackKeyEventObj = NULL;
913 }
Jeff Brown1f245102010-11-18 20:53:46 -0800914 android_view_KeyEvent_recycle(env, keyEventObj);
915 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800916
917 if (fallbackKeyEventObj) {
918 // Note: outFallbackKeyEvent may be the same object as keyEvent.
919 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
920 outFallbackKeyEvent)) {
921 result = true;
922 }
923 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
924 env->DeleteLocalRef(fallbackKeyEventObj);
925 }
Jeff Brown1f245102010-11-18 20:53:46 -0800926 } else {
Steve Block3762c312012-01-06 19:20:56 +0000927 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800928 }
Jeff Brown928e0542011-01-10 11:17:36 -0800929 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700930 }
Jeff Brown1f245102010-11-18 20:53:46 -0800931 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700932}
933
Jeff Brown01ce2e92010-09-26 22:20:12 -0700934void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
935 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700936}
937
Jeff Brown349703e2010-06-22 01:27:15 -0700938
Jeff Brownb88102f2010-09-08 11:49:43 -0700939bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
940 int32_t injectorPid, int32_t injectorUid) {
941 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700942 jboolean result = env->CallBooleanMethod(mServiceObj,
943 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700944 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
945 result = false;
946 }
Jeff Brown349703e2010-06-22 01:27:15 -0700947 return result;
948}
949
Jeff Brown2352b972011-04-12 22:39:53 -0700950void NativeInputManager::loadPointerResources(PointerResources* outResources) {
951 JNIEnv* env = jniEnv();
952
953 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
954 &outResources->spotHover);
955 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
956 &outResources->spotTouch);
957 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
958 &outResources->spotAnchor);
959}
960
Jeff Brown83c09682010-12-23 17:50:18 -0800961
Jeff Brown9c3cda02010-06-15 01:31:58 -0700962// ----------------------------------------------------------------------------
963
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000964static jlong nativeInit(JNIEnv* env, jclass clazz,
Jeff Brown4532e612012-04-05 14:27:12 -0700965 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -0700966 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -0800967 if (messageQueue == NULL) {
968 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
969 return 0;
970 }
971
Jeff Brown603b4452012-04-06 17:39:41 -0700972 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
973 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800974 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000975 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700976}
977
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000978static void nativeStart(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -0700979 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700980
Jeff Brown4532e612012-04-05 14:27:12 -0700981 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700982 if (result) {
983 jniThrowRuntimeException(env, "Input manager could not be started.");
984 }
985}
986
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000987static void nativeSetDisplayViewport(JNIEnv* env, jclass clazz, jlong ptr, jboolean external,
Jeff Brownd728bf52012-09-08 18:05:28 -0700988 jint displayId, jint orientation,
989 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -0700990 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
991 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -0700992 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700993
Jeff Brownd728bf52012-09-08 18:05:28 -0700994 DisplayViewport v;
995 v.displayId = displayId;
996 v.orientation = orientation;
997 v.logicalLeft = logicalLeft;
998 v.logicalTop = logicalTop;
999 v.logicalRight = logicalRight;
1000 v.logicalBottom = logicalBottom;
1001 v.physicalLeft = physicalLeft;
1002 v.physicalTop = physicalTop;
1003 v.physicalRight = physicalRight;
1004 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001005 v.deviceWidth = deviceWidth;
1006 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -07001007 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001008}
1009
Jeff Brown4532e612012-04-05 14:27:12 -07001010static jint nativeGetScanCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001011 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001012 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001013
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001014 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001015 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001016}
1017
Jeff Brown4532e612012-04-05 14:27:12 -07001018static jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001019 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001020 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001021
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001022 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001023 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001024}
1025
Jeff Brown4532e612012-04-05 14:27:12 -07001026static jint nativeGetSwitchState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001027 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001028 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001029
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001030 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001031 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001032}
1033
Jeff Brown4532e612012-04-05 14:27:12 -07001034static jboolean nativeHasKeys(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001035 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001036 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001037
1038 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1039 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1040 jsize numCodes = env->GetArrayLength(keyCodes);
1041 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001042 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001043 if (im->getInputManager()->getReader()->hasKeys(
1044 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1045 result = JNI_TRUE;
1046 } else {
1047 result = JNI_FALSE;
1048 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001049 } else {
1050 result = JNI_FALSE;
1051 }
1052
1053 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1054 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1055 return result;
1056}
1057
1058static void throwInputChannelNotInitialized(JNIEnv* env) {
1059 jniThrowException(env, "java/lang/IllegalStateException",
1060 "inputChannel is not initialized");
1061}
1062
Jeff Brown4532e612012-04-05 14:27:12 -07001063static void handleInputChannelDisposed(JNIEnv* env,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001064 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001065 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1066
Steve Block8564c8d2012-01-05 23:22:43 +00001067 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001068 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001069 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001070}
1071
Jeff Brown4532e612012-04-05 14:27:12 -07001072static void nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001073 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001074 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001075
1076 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1077 inputChannelObj);
1078 if (inputChannel == NULL) {
1079 throwInputChannelNotInitialized(env);
1080 return;
1081 }
1082
Jeff Brown928e0542011-01-10 11:17:36 -08001083 sp<InputWindowHandle> inputWindowHandle =
1084 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001085
Jeff Brown4532e612012-04-05 14:27:12 -07001086 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001087 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001088 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001089 String8 message;
1090 message.appendFormat("Failed to register input channel. status=%d", status);
1091 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001092 return;
1093 }
1094
Jeff Browna41ca772010-08-11 14:46:32 -07001095 if (! monitor) {
1096 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001097 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001098 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001099}
1100
Jeff Brown4532e612012-04-05 14:27:12 -07001101static void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001102 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001103 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001104
1105 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1106 inputChannelObj);
1107 if (inputChannel == NULL) {
1108 throwInputChannelNotInitialized(env);
1109 return;
1110 }
1111
1112 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1113
Jeff Brown4532e612012-04-05 14:27:12 -07001114 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001115 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1116 String8 message;
1117 message.appendFormat("Failed to unregister input channel. status=%d", status);
1118 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001119 }
1120}
1121
Jeff Brown4532e612012-04-05 14:27:12 -07001122static void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001123 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001124 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001125
Jeff Brown4532e612012-04-05 14:27:12 -07001126 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001127}
1128
Jeff Brown4532e612012-04-05 14:27:12 -07001129static jint nativeInjectInputEvent(JNIEnv* env, jclass clazz,
Jeff Brown38f96e52014-02-11 14:32:56 -08001130 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001131 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001132 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001133
Jeff Brown6ec402b2010-07-28 15:48:59 -07001134 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1135 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001136 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1137 if (status) {
1138 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1139 return INPUT_EVENT_INJECTION_FAILED;
1140 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001141
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001142 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown38f96e52014-02-11 14:32:56 -08001143 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001144 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001145 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001146 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1147 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001148 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1149 return INPUT_EVENT_INJECTION_FAILED;
1150 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001151
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001152 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown38f96e52014-02-11 14:32:56 -08001153 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001154 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001155 } else {
1156 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001157 return INPUT_EVENT_INJECTION_FAILED;
1158 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001159}
1160
Jeff Brown4532e612012-04-05 14:27:12 -07001161static void nativeSetInputWindows(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001162 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001163 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001164
Jeff Brown4532e612012-04-05 14:27:12 -07001165 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001166}
1167
Jeff Brown4532e612012-04-05 14:27:12 -07001168static void nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001169 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001170 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001171
Jeff Brown4532e612012-04-05 14:27:12 -07001172 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001173}
1174
Jeff Brown4532e612012-04-05 14:27:12 -07001175static void nativeSetInputDispatchMode(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001176 jclass clazz, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001177 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001178
Jeff Brown4532e612012-04-05 14:27:12 -07001179 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001180}
1181
Jeff Brown4532e612012-04-05 14:27:12 -07001182static void nativeSetSystemUiVisibility(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001183 jclass clazz, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001184 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001185
Jeff Brown4532e612012-04-05 14:27:12 -07001186 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001187}
1188
Jeff Brown4532e612012-04-05 14:27:12 -07001189static jboolean nativeTransferTouchFocus(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001190 jclass clazz, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001191 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001192
1193 sp<InputChannel> fromChannel =
1194 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1195 sp<InputChannel> toChannel =
1196 android_view_InputChannel_getInputChannel(env, toChannelObj);
1197
1198 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001199 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001200 }
1201
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001202 if (im->getInputManager()->getDispatcher()->
1203 transferTouchFocus(fromChannel, toChannel)) {
1204 return JNI_TRUE;
1205 } else {
1206 return JNI_FALSE;
1207 }
Jeff Browne6504122010-09-27 14:52:15 -07001208}
1209
Jeff Brown4532e612012-04-05 14:27:12 -07001210static void nativeSetPointerSpeed(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001211 jclass clazz, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001212 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001213
Jeff Brown4532e612012-04-05 14:27:12 -07001214 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001215}
1216
Jeff Brown4532e612012-04-05 14:27:12 -07001217static void nativeSetShowTouches(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001218 jclass clazz, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001219 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001220
Jeff Brown4532e612012-04-05 14:27:12 -07001221 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001222}
1223
Jeff Browna47425a2012-04-13 04:09:27 -07001224static void nativeVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001225 jclass clazz, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001226 jint repeat, jint token) {
1227 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1228
1229 size_t patternSize = env->GetArrayLength(patternObj);
1230 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
1231 ALOGI("Skipped requested vibration because the pattern size is %d "
1232 "which is more than the maximum supported size of %d.",
1233 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1234 return; // limit to reasonable size
1235 }
1236
1237 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1238 patternObj, NULL));
1239 nsecs_t pattern[patternSize];
1240 for (size_t i = 0; i < patternSize; i++) {
1241 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001242 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001243 }
1244 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1245
1246 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1247}
1248
1249static void nativeCancelVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001250 jclass clazz, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001251 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1252
1253 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1254}
1255
Jeff Brown6ec6f792012-04-17 16:52:41 -07001256static void nativeReloadKeyboardLayouts(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001257 jclass clazz, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001258 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1259
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001260 im->getInputManager()->getReader()->requestRefreshConfiguration(
1261 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1262}
1263
1264static void nativeReloadDeviceAliases(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001265 jclass clazz, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001266 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1267
1268 im->getInputManager()->getReader()->requestRefreshConfiguration(
1269 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001270}
1271
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001272static jstring nativeDump(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001273 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001274
Jeff Brownb88102f2010-09-08 11:49:43 -07001275 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001276 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001277 return env->NewStringUTF(dump.string());
1278}
1279
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001280static void nativeMonitor(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001281 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001282
Jeff Brown4532e612012-04-05 14:27:12 -07001283 im->getInputManager()->getReader()->monitor();
1284 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001285}
1286
Jeff Brown9c3cda02010-06-15 01:31:58 -07001287// ----------------------------------------------------------------------------
1288
Jeff Brown46b9ac02010-04-22 18:58:52 -07001289static JNINativeMethod gInputManagerMethods[] = {
1290 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001291 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001292 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001293 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001294 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001295 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001296 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001297 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001298 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001299 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001300 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001301 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001302 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001303 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001304 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001305 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001306 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001307 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001308 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001309 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001310 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001311 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001312 (void*) nativeSetInputFilterEnabled },
Jeff Brown38f96e52014-02-11 14:32:56 -08001313 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001314 (void*) nativeInjectInputEvent },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001315 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001316 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001317 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001318 (void*) nativeSetFocusedApplication },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001319 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001320 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001321 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001322 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001323 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001324 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001325 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001326 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001327 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001328 (void*) nativeSetShowTouches },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001329 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001330 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001331 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001332 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001333 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001334 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001335 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001336 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001337 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001338 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001339 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001340 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001341};
1342
1343#define FIND_CLASS(var, className) \
1344 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001345 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001346
1347#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1348 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1349 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1350
1351#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1352 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1353 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1354
1355int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001356 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001357 gInputManagerMethods, NELEM(gInputManagerMethods));
1358 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1359
Jeff Brown9c3cda02010-06-15 01:31:58 -07001360 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001361
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001362 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001363 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001364
Jeff Brown4532e612012-04-05 14:27:12 -07001365 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001366 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001367
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001368 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1369 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1370
Jeff Brown53384282012-08-20 20:16:01 -07001371 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1372 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001373
Jeff Brown4532e612012-04-05 14:27:12 -07001374 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1375 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001376
Jeff Brown4532e612012-04-05 14:27:12 -07001377 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001378 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001379 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001380
Jeff Brown4532e612012-04-05 14:27:12 -07001381 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001382 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1383
Jeff Brown4532e612012-04-05 14:27:12 -07001384 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001385 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001386
Jeff Brown4532e612012-04-05 14:27:12 -07001387 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001388 clazz,
Jeff Brown26875502014-01-30 21:47:47 -08001389 "interceptMotionBeforeQueueingWhenScreenOff", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001390
Jeff Brown4532e612012-04-05 14:27:12 -07001391 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001392 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001393 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001394
Jeff Brown4532e612012-04-05 14:27:12 -07001395 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001396 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001397 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001398
Jeff Brown4532e612012-04-05 14:27:12 -07001399 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001400 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001401
Jeff Brown4532e612012-04-05 14:27:12 -07001402 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001403 "getVirtualKeyQuietTimeMillis", "()I");
1404
Jeff Brown4532e612012-04-05 14:27:12 -07001405 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001406 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1407
Jeff Brown4532e612012-04-05 14:27:12 -07001408 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001409 "getKeyRepeatTimeout", "()I");
1410
Jeff Brown4532e612012-04-05 14:27:12 -07001411 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001412 "getKeyRepeatDelay", "()I");
1413
Jeff Brown4532e612012-04-05 14:27:12 -07001414 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001415 "getHoverTapTimeout", "()I");
1416
Jeff Brown4532e612012-04-05 14:27:12 -07001417 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001418 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001419
Jeff Brown4532e612012-04-05 14:27:12 -07001420 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001421 "getDoubleTapTimeout", "()I");
1422
Jeff Brown4532e612012-04-05 14:27:12 -07001423 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001424 "getLongPressTimeout", "()I");
1425
Jeff Brown4532e612012-04-05 14:27:12 -07001426 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001427 "getPointerLayer", "()I");
1428
Jeff Brown4532e612012-04-05 14:27:12 -07001429 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001430 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001431
Jeff Brown6ec6f792012-04-17 16:52:41 -07001432 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
1433 "getKeyboardLayoutOverlay", "(Ljava/lang/String;)[Ljava/lang/String;");
1434
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001435 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1436 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1437
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001438 // InputDevice
1439
1440 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1441 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1442
Jeff Brown6ec402b2010-07-28 15:48:59 -07001443 // KeyEvent
1444
1445 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001446 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1447
Jeff Brown8d608662010-08-30 03:02:23 -07001448 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001449
1450 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001451 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001452
Jeff Brown46b9ac02010-04-22 18:58:52 -07001453 return 0;
1454}
1455
Jeff Brown46b9ac02010-04-22 18:58:52 -07001456} /* namespace android */