blob: fef41c9b04d193930e4fe0bc110dcf498aefac35 [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>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080032
Jeff Brown46b9ac02010-04-22 18:58:52 -070033#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080034#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070035#include <utils/threads.h>
Jeff Brown83c09682010-12-23 17:50:18 -080036
Jeff Brownb4ff35d2011-01-02 16:37:43 -080037#include <input/InputManager.h>
38#include <input/PointerController.h>
Jeff Browna6dbfdd2011-04-11 11:54:25 -070039#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080040
Jeff Brown05dc66a2011-03-02 14:41:58 -080041#include <android_os_MessageQueue.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042#include <android_view_KeyEvent.h>
43#include <android_view_MotionEvent.h>
44#include <android_view_InputChannel.h>
Jeff Brown86ea1f52011-04-12 22:39:53 -070045#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080046#include <android/graphics/GraphicsJNI.h>
47
Jeff Brown00fa7bd2010-07-02 15:37:36 -070048#include "com_android_server_PowerManagerService.h"
Jeff Brown928e0542011-01-10 11:17:36 -080049#include "com_android_server_InputApplication.h"
50#include "com_android_server_InputApplicationHandle.h"
51#include "com_android_server_InputWindow.h"
52#include "com_android_server_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070053
54namespace android {
55
Jeff Brown46b9ac02010-04-22 18:58:52 -070056static struct {
57 jclass clazz;
58
Jeff Brown46b9ac02010-04-22 18:58:52 -070059 jmethodID notifyConfigurationChanged;
60 jmethodID notifyLidSwitchChanged;
Jeff Brown7fbdc842010-06-17 20:52:56 -070061 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070062 jmethodID notifyANR;
Jeff Brown349703e2010-06-22 01:27:15 -070063 jmethodID interceptKeyBeforeQueueing;
Jeff Brown56194eb2011-03-02 19:23:13 -080064 jmethodID interceptMotionBeforeQueueingWhenScreenOff;
Jeff Brown349703e2010-06-22 01:27:15 -070065 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070066 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070067 jmethodID checkInjectEventsPermission;
Jeff Brown46b9ac02010-04-22 18:58:52 -070068 jmethodID filterTouchEvents;
69 jmethodID filterJumpyTouchEvents;
Jeff Brownfe508922011-01-18 15:10:10 -080070 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070071 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080072 jmethodID getKeyRepeatTimeout;
73 jmethodID getKeyRepeatDelay;
Jeff Brownae9fc032010-08-18 15:51:08 -070074 jmethodID getMaxEventsPerSecond;
Jeff Brown214eaf42011-05-26 19:17:02 -070075 jmethodID getTapTimeout;
76 jmethodID getDoubleTapTimeout;
77 jmethodID getLongPressTimeout;
78 jmethodID getTouchSlop;
Jeff Brown83c09682010-12-23 17:50:18 -080079 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080080 jmethodID getPointerIcon;
Jeff Brown46b9ac02010-04-22 18:58:52 -070081} gCallbacksClassInfo;
82
83static struct {
84 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070085} gKeyEventClassInfo;
86
87static struct {
88 jclass clazz;
89} gMotionEventClassInfo;
90
Jeff Brown8d608662010-08-30 03:02:23 -070091static struct {
92 jclass clazz;
93
94 jmethodID ctor;
95 jmethodID addMotionRange;
96
97 jfieldID mId;
98 jfieldID mName;
99 jfieldID mSources;
100 jfieldID mKeyboardType;
Jeff Brown8d608662010-08-30 03:02:23 -0700101} gInputDeviceClassInfo;
102
Jeff Brown57c59372010-09-21 18:22:55 -0700103static struct {
104 jclass clazz;
105
106 jfieldID touchscreen;
107 jfieldID keyboard;
108 jfieldID navigation;
109} gConfigurationClassInfo;
110
Jeff Brown928e0542011-01-10 11:17:36 -0800111
112// --- Global functions ---
113
Jeff Brown214eaf42011-05-26 19:17:02 -0700114template<typename T>
115inline static T min(const T& a, const T& b) {
116 return a < b ? a : b;
117}
118
119template<typename T>
120inline static T max(const T& a, const T& b) {
121 return a > b ? a : b;
122}
123
Jeff Brown928e0542011-01-10 11:17:36 -0800124static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
125 const sp<InputApplicationHandle>& inputApplicationHandle) {
126 if (inputApplicationHandle == NULL) {
127 return NULL;
128 }
129 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
130 getInputApplicationHandleObjLocalRef(env);
131}
132
133static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
134 const sp<InputWindowHandle>& inputWindowHandle) {
135 if (inputWindowHandle == NULL) {
136 return NULL;
137 }
138 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
139 getInputWindowHandleObjLocalRef(env);
140}
141
Jeff Brown86ea1f52011-04-12 22:39:53 -0700142static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
143 SpriteIcon* outSpriteIcon) {
144 PointerIcon pointerIcon;
145 status_t status = android_view_PointerIcon_loadSystemIcon(env,
146 contextObj, style, &pointerIcon);
147 if (!status) {
148 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
149 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
150 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
151 }
152}
153
Jeff Brown928e0542011-01-10 11:17:36 -0800154
155// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800156
Jeff Brown9c3cda02010-06-15 01:31:58 -0700157class NativeInputManager : public virtual RefBase,
158 public virtual InputReaderPolicyInterface,
Jeff Brown86ea1f52011-04-12 22:39:53 -0700159 public virtual InputDispatcherPolicyInterface,
160 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700161protected:
162 virtual ~NativeInputManager();
163
164public:
Jeff Brown86ea1f52011-04-12 22:39:53 -0700165 NativeInputManager(jobject contextObj, jobject callbacksObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700166
167 inline sp<InputManager> getInputManager() const { return mInputManager; }
168
Jeff Brownb88102f2010-09-08 11:49:43 -0700169 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700170
Jeff Brown9c3cda02010-06-15 01:31:58 -0700171 void setDisplaySize(int32_t displayId, int32_t width, int32_t height);
172 void setDisplayOrientation(int32_t displayId, int32_t orientation);
173
Jeff Brown7fbdc842010-06-17 20:52:56 -0700174 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800175 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700176 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
177
Jeff Brown349703e2010-06-22 01:27:15 -0700178 void setInputWindows(JNIEnv* env, jobjectArray windowObjArray);
179 void setFocusedApplication(JNIEnv* env, jobject applicationObj);
180 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800181 void setSystemUiVisibility(int32_t visibility);
Jeff Brown349703e2010-06-22 01:27:15 -0700182
Jeff Brown9c3cda02010-06-15 01:31:58 -0700183 /* --- InputReaderPolicyInterface implementation --- */
184
185 virtual bool getDisplayInfo(int32_t displayId,
186 int32_t* width, int32_t* height, int32_t* orientation);
Jeff Brown214eaf42011-05-26 19:17:02 -0700187 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800188 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700189
190 /* --- InputDispatcherPolicyInterface implementation --- */
191
Jeff Browne20c9e02010-10-11 14:20:19 -0700192 virtual void notifySwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
193 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700194 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700195 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800196 const sp<InputWindowHandle>& inputWindowHandle);
197 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown214eaf42011-05-26 19:17:02 -0700198 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
199 virtual bool isKeyRepeatEnabled();
Jeff Brown1f245102010-11-18 20:53:46 -0800200 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800201 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800202 virtual bool interceptKeyBeforeDispatching(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 Brown86ea1f52011-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 Brown86ea1f52011-04-12 22:39:53 -0700217 jobject mContextObj;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700218 jobject mCallbacksObj;
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.
224 int32_t displayWidth, displayHeight; // -1 when initialized
225 int32_t displayOrientation;
226
Jeff Brown05dc66a2011-03-02 14:41:58 -0800227 // System UI visibility.
228 int32_t systemUiVisibility;
229
Jeff Browna6dbfdd2011-04-11 11:54:25 -0700230 // Sprite controller singleton, created on first use.
231 sp<SpriteController> spriteController;
232
Jeff Brown83c09682010-12-23 17:50:18 -0800233 // Pointer controller singleton, created and destroyed as needed.
234 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800235 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700236
Jeff Brown86ea1f52011-04-12 22:39:53 -0700237 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800238 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Browna6dbfdd2011-04-11 11:54:25 -0700239 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800240
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700241 // Power manager interactions.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700242 bool isScreenOn();
243 bool isScreenBright();
244
Jeff Brownb88102f2010-09-08 11:49:43 -0700245 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700246
Jeff Brown9c3cda02010-06-15 01:31:58 -0700247 static inline JNIEnv* jniEnv() {
248 return AndroidRuntime::getJNIEnv();
249 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700250};
251
Jeff Brown928e0542011-01-10 11:17:36 -0800252
Jeff Brown9c3cda02010-06-15 01:31:58 -0700253
Jeff Brown86ea1f52011-04-12 22:39:53 -0700254NativeInputManager::NativeInputManager(jobject contextObj,
255 jobject callbacksObj, const sp<Looper>& looper) :
Jeff Brown214eaf42011-05-26 19:17:02 -0700256 mLooper(looper) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700257 JNIEnv* env = jniEnv();
258
Jeff Brown86ea1f52011-04-12 22:39:53 -0700259 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700260 mCallbacksObj = env->NewGlobalRef(callbacksObj);
261
Jeff Brown83c09682010-12-23 17:50:18 -0800262 {
263 AutoMutex _l(mLock);
264 mLocked.displayWidth = -1;
265 mLocked.displayHeight = -1;
266 mLocked.displayOrientation = ROTATION_0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800267
268 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown83c09682010-12-23 17:50:18 -0800269 }
270
Jeff Brown9c3cda02010-06-15 01:31:58 -0700271 sp<EventHub> eventHub = new EventHub();
272 mInputManager = new InputManager(eventHub, this, this);
273}
274
275NativeInputManager::~NativeInputManager() {
276 JNIEnv* env = jniEnv();
277
Jeff Brown86ea1f52011-04-12 22:39:53 -0700278 env->DeleteGlobalRef(mContextObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700279 env->DeleteGlobalRef(mCallbacksObj);
280}
281
Jeff Brownb88102f2010-09-08 11:49:43 -0700282void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700283 mInputManager->getReader()->dump(dump);
284 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700285
Jeff Brownb88102f2010-09-08 11:49:43 -0700286 mInputManager->getDispatcher()->dump(dump);
287 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700288}
289
Jeff Brown7fbdc842010-06-17 20:52:56 -0700290bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700291 if (env->ExceptionCheck()) {
292 LOGE("An exception was thrown by callback '%s'.", methodName);
293 LOGE_EX(env);
294 env->ExceptionClear();
295 return true;
296 }
297 return false;
298}
299
300void NativeInputManager::setDisplaySize(int32_t displayId, int32_t width, int32_t height) {
301 if (displayId == 0) {
Jeff Brown86ea1f52011-04-12 22:39:53 -0700302 { // acquire lock
303 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700304
Jeff Brown86ea1f52011-04-12 22:39:53 -0700305 if (mLocked.displayWidth == width && mLocked.displayHeight == height) {
306 return;
307 }
308
Jeff Brown83c09682010-12-23 17:50:18 -0800309 mLocked.displayWidth = width;
310 mLocked.displayHeight = height;
311
312 sp<PointerController> controller = mLocked.pointerController.promote();
313 if (controller != NULL) {
314 controller->setDisplaySize(width, height);
315 }
Jeff Brown86ea1f52011-04-12 22:39:53 -0700316 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700317 }
318}
319
320void NativeInputManager::setDisplayOrientation(int32_t displayId, int32_t orientation) {
321 if (displayId == 0) {
Jeff Brown83c09682010-12-23 17:50:18 -0800322 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700323
Jeff Brown83c09682010-12-23 17:50:18 -0800324 if (mLocked.displayOrientation != orientation) {
325 mLocked.displayOrientation = orientation;
326
327 sp<PointerController> controller = mLocked.pointerController.promote();
328 if (controller != NULL) {
329 controller->setDisplayOrientation(orientation);
330 }
331 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700332 }
333}
334
Jeff Brown7fbdc842010-06-17 20:52:56 -0700335status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Brown928e0542011-01-10 11:17:36 -0800336 const sp<InputChannel>& inputChannel,
337 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
338 return mInputManager->getDispatcher()->registerInputChannel(
339 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700340}
341
342status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
343 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700344 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700345}
346
Jeff Brown9c3cda02010-06-15 01:31:58 -0700347bool NativeInputManager::getDisplayInfo(int32_t displayId,
348 int32_t* width, int32_t* height, int32_t* orientation) {
349 bool result = false;
350 if (displayId == 0) {
Jeff Brown83c09682010-12-23 17:50:18 -0800351 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700352
Jeff Brown83c09682010-12-23 17:50:18 -0800353 if (mLocked.displayWidth > 0 && mLocked.displayHeight > 0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700354 if (width) {
Jeff Brown83c09682010-12-23 17:50:18 -0800355 *width = mLocked.displayWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700356 }
357 if (height) {
Jeff Brown83c09682010-12-23 17:50:18 -0800358 *height = mLocked.displayHeight;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700359 }
360 if (orientation) {
Jeff Brown83c09682010-12-23 17:50:18 -0800361 *orientation = mLocked.displayOrientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700363 result = true;
364 }
365 }
366 return result;
367}
368
Jeff Brown214eaf42011-05-26 19:17:02 -0700369void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700370 JNIEnv* env = jniEnv();
371
Jeff Brown214eaf42011-05-26 19:17:02 -0700372 jboolean filterTouchEvents = env->CallBooleanMethod(mCallbacksObj,
373 gCallbacksClassInfo.filterTouchEvents);
374 if (!checkAndClearExceptionFromCallback(env, "filterTouchEvents")) {
375 outConfig->filterTouchEvents = filterTouchEvents;
376 }
377
378 jboolean filterJumpyTouchEvents = env->CallBooleanMethod(mCallbacksObj,
379 gCallbacksClassInfo.filterJumpyTouchEvents);
380 if (!checkAndClearExceptionFromCallback(env, "filterJumpyTouchEvents")) {
381 outConfig->filterJumpyTouchEvents = filterJumpyTouchEvents;
382 }
383
384 jint virtualKeyQuietTime = env->CallIntMethod(mCallbacksObj,
385 gCallbacksClassInfo.getVirtualKeyQuietTimeMillis);
386 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
387 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
388 }
389
390 outConfig->excludedDeviceNames.clear();
391 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mCallbacksObj,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700392 gCallbacksClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700393 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
394 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700395 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700396 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700397 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700398 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700399 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700400 env->DeleteLocalRef(item);
401 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700402 env->DeleteLocalRef(excludedDeviceNames);
403 }
404
405 jint tapTimeout = env->CallIntMethod(mCallbacksObj,
406 gCallbacksClassInfo.getTapTimeout);
407 if (!checkAndClearExceptionFromCallback(env, "getTapTimeout")) {
408 jint doubleTapTimeout = env->CallIntMethod(mCallbacksObj,
409 gCallbacksClassInfo.getDoubleTapTimeout);
410 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
411 jint longPressTimeout = env->CallIntMethod(mCallbacksObj,
412 gCallbacksClassInfo.getLongPressTimeout);
413 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
414 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(tapTimeout);
415
416 // We must ensure that the tap-drag interval is significantly shorter than
417 // the long-press timeout because the tap is held down for the entire duration
418 // of the double-tap timeout.
419 jint tapDragInterval = max(min(longPressTimeout - 100,
420 doubleTapTimeout), tapTimeout);
421 outConfig->pointerGestureTapDragInterval =
422 milliseconds_to_nanoseconds(tapDragInterval);
423 }
424 }
425 }
426
427 jint touchSlop = env->CallIntMethod(mCallbacksObj,
428 gCallbacksClassInfo.getTouchSlop);
429 if (!checkAndClearExceptionFromCallback(env, "getTouchSlop")) {
430 outConfig->pointerGestureTapSlop = touchSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700431 }
432}
433
Jeff Brown83c09682010-12-23 17:50:18 -0800434sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
435 AutoMutex _l(mLock);
436
437 sp<PointerController> controller = mLocked.pointerController.promote();
438 if (controller == NULL) {
Jeff Browna6dbfdd2011-04-11 11:54:25 -0700439 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800440
Jeff Brown86ea1f52011-04-12 22:39:53 -0700441 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800442 mLocked.pointerController = controller;
443
444 controller->setDisplaySize(mLocked.displayWidth, mLocked.displayHeight);
445 controller->setDisplayOrientation(mLocked.displayOrientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800446
Jeff Browna6dbfdd2011-04-11 11:54:25 -0700447 JNIEnv* env = jniEnv();
Jeff Brown86ea1f52011-04-12 22:39:53 -0700448 jobject pointerIconObj = env->CallObjectMethod(mCallbacksObj,
449 gCallbacksClassInfo.getPointerIcon);
450 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
451 PointerIcon pointerIcon;
452 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
453 mContextObj, &pointerIcon);
454 if (!status && !pointerIcon.isNullIcon()) {
455 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
456 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
457 } else {
458 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800459 }
Jeff Brown86ea1f52011-04-12 22:39:53 -0700460 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800461 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800462
Jeff Brown86ea1f52011-04-12 22:39:53 -0700463 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800464 }
465 return controller;
466}
467
Jeff Browna6dbfdd2011-04-11 11:54:25 -0700468void NativeInputManager::ensureSpriteControllerLocked() {
469 if (mLocked.spriteController == NULL) {
470 JNIEnv* env = jniEnv();
471 jint layer = env->CallIntMethod(mCallbacksObj, gCallbacksClassInfo.getPointerLayer);
472 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
473 layer = -1;
474 }
475 mLocked.spriteController = new SpriteController(mLooper, layer);
476 }
477}
478
Jeff Browne20c9e02010-10-11 14:20:19 -0700479void NativeInputManager::notifySwitch(nsecs_t when, int32_t switchCode,
480 int32_t switchValue, uint32_t policyFlags) {
481#if DEBUG_INPUT_DISPATCHER_POLICY
482 LOGD("notifySwitch - when=%lld, switchCode=%d, switchValue=%d, policyFlags=0x%x",
483 when, switchCode, switchValue, policyFlags);
484#endif
485
486 JNIEnv* env = jniEnv();
487
488 switch (switchCode) {
489 case SW_LID:
490 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyLidSwitchChanged,
491 when, switchValue == 0);
492 checkAndClearExceptionFromCallback(env, "notifyLidSwitchChanged");
493 break;
494 }
495}
496
Jeff Brown9c3cda02010-06-15 01:31:58 -0700497void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
498#if DEBUG_INPUT_DISPATCHER_POLICY
499 LOGD("notifyConfigurationChanged - when=%lld", when);
500#endif
501
502 JNIEnv* env = jniEnv();
503
Jeff Brown57c59372010-09-21 18:22:55 -0700504 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700505 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700506}
507
Jeff Brown519e0242010-09-15 15:18:56 -0700508nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800509 const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700510#if DEBUG_INPUT_DISPATCHER_POLICY
511 LOGD("notifyANR");
512#endif
513
514 JNIEnv* env = jniEnv();
515
Jeff Brown928e0542011-01-10 11:17:36 -0800516 jobject inputApplicationHandleObj =
517 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
518 jobject inputWindowHandleObj =
519 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -0700520
Jeff Brown519e0242010-09-15 15:18:56 -0700521 jlong newTimeout = env->CallLongMethod(mCallbacksObj,
Jeff Brown928e0542011-01-10 11:17:36 -0800522 gCallbacksClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700523 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
524 newTimeout = 0; // abort dispatch
525 } else {
526 assert(newTimeout >= 0);
527 }
528
Jeff Brown928e0542011-01-10 11:17:36 -0800529 env->DeleteLocalRef(inputWindowHandleObj);
530 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700531 return newTimeout;
532}
533
Jeff Brown928e0542011-01-10 11:17:36 -0800534void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700535#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown928e0542011-01-10 11:17:36 -0800536 LOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700537#endif
538
Jeff Brown7fbdc842010-06-17 20:52:56 -0700539 JNIEnv* env = jniEnv();
540
Jeff Brown928e0542011-01-10 11:17:36 -0800541 jobject inputWindowHandleObj =
542 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
543 if (inputWindowHandleObj) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700544 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800545 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700546 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
547
Jeff Brown928e0542011-01-10 11:17:36 -0800548 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700549 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700550}
551
Jeff Brown214eaf42011-05-26 19:17:02 -0700552void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
553 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800554
Jeff Brown214eaf42011-05-26 19:17:02 -0700555 jint keyRepeatTimeout = env->CallIntMethod(mCallbacksObj,
556 gCallbacksClassInfo.getKeyRepeatTimeout);
557 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
558 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
559 }
Jeff Browna4547672011-03-02 21:38:11 -0800560
Jeff Brown214eaf42011-05-26 19:17:02 -0700561 jint keyRepeatDelay = env->CallIntMethod(mCallbacksObj,
562 gCallbacksClassInfo.getKeyRepeatDelay);
563 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
564 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
565 }
566
567 jint maxEventsPerSecond = env->CallIntMethod(mCallbacksObj,
568 gCallbacksClassInfo.getMaxEventsPerSecond);
569 if (!checkAndClearExceptionFromCallback(env, "getMaxEventsPerSecond")) {
570 outConfig->maxEventsPerSecond = maxEventsPerSecond;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700571 }
572}
573
Jeff Brown214eaf42011-05-26 19:17:02 -0700574bool NativeInputManager::isKeyRepeatEnabled() {
575 // Only enable automatic key repeating when the screen is on.
576 return isScreenOn();
Jeff Brownae9fc032010-08-18 15:51:08 -0700577}
578
Jeff Brown349703e2010-06-22 01:27:15 -0700579void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowObjArray) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700580 Vector<InputWindow> windows;
Jeff Brown349703e2010-06-22 01:27:15 -0700581
Jeff Brownb88102f2010-09-08 11:49:43 -0700582 jsize length = env->GetArrayLength(windowObjArray);
583 for (jsize i = 0; i < length; i++) {
Jeff Brown928e0542011-01-10 11:17:36 -0800584 jobject windowObj = env->GetObjectArrayElement(windowObjArray, i);
585 if (! windowObj) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700586 break; // found null element indicating end of used portion of the array
Jeff Brown349703e2010-06-22 01:27:15 -0700587 }
588
Jeff Brownb88102f2010-09-08 11:49:43 -0700589 windows.push();
590 InputWindow& window = windows.editTop();
Jeff Brown928e0542011-01-10 11:17:36 -0800591 android_server_InputWindow_toNative(env, windowObj, &window);
592 if (window.inputChannel == NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700593 windows.pop();
Jeff Brown349703e2010-06-22 01:27:15 -0700594 }
Jeff Brown928e0542011-01-10 11:17:36 -0800595 env->DeleteLocalRef(windowObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700596 }
Jeff Brown349703e2010-06-22 01:27:15 -0700597
Jeff Brownb88102f2010-09-08 11:49:43 -0700598 mInputManager->getDispatcher()->setInputWindows(windows);
Jeff Brown349703e2010-06-22 01:27:15 -0700599}
600
Jeff Brown349703e2010-06-22 01:27:15 -0700601void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationObj) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700602 if (applicationObj) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700603 InputApplication application;
Jeff Brown928e0542011-01-10 11:17:36 -0800604 android_server_InputApplication_toNative(env, applicationObj, &application);
605 if (application.inputApplicationHandle != NULL) {
606 mInputManager->getDispatcher()->setFocusedApplication(&application);
Jeff Brown22d789d2011-03-25 11:58:46 -0700607 return;
Jeff Brown928e0542011-01-10 11:17:36 -0800608 }
Jeff Brown349703e2010-06-22 01:27:15 -0700609 }
Jeff Brown928e0542011-01-10 11:17:36 -0800610 mInputManager->getDispatcher()->setFocusedApplication(NULL);
Jeff Brown349703e2010-06-22 01:27:15 -0700611}
612
613void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700614 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700615}
616
Jeff Brown05dc66a2011-03-02 14:41:58 -0800617void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
618 AutoMutex _l(mLock);
619
620 if (mLocked.systemUiVisibility != visibility) {
621 mLocked.systemUiVisibility = visibility;
622
623 sp<PointerController> controller = mLocked.pointerController.promote();
624 if (controller != NULL) {
Jeff Brown86ea1f52011-04-12 22:39:53 -0700625 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800626 }
627 }
628}
629
Jeff Brown86ea1f52011-04-12 22:39:53 -0700630void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800631 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown86ea1f52011-04-12 22:39:53 -0700632 controller->setInactivityTimeout(lightsOut
633 ? PointerController::INACTIVITY_TIMEOUT_SHORT
634 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800635}
636
Jeff Browne20c9e02010-10-11 14:20:19 -0700637bool NativeInputManager::isScreenOn() {
638 return android_server_PowerManagerService_isScreenOn();
639}
640
641bool NativeInputManager::isScreenBright() {
642 return android_server_PowerManagerService_isScreenBright();
643}
644
Jeff Brown1f245102010-11-18 20:53:46 -0800645void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
646 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700647 // Policy:
648 // - Ignore untrusted events and pass them along.
649 // - Ask the window manager what to do with normal events and trusted injected events.
650 // - For normal events wake and brighten the screen if currently off or dim.
651 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800652 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700653 bool isScreenOn = this->isScreenOn();
654 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700655
Jeff Brown3122e442010-10-11 23:32:49 -0700656 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800657 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
658 jint wmActions;
659 if (keyEventObj) {
660 wmActions = env->CallIntMethod(mCallbacksObj,
661 gCallbacksClassInfo.interceptKeyBeforeQueueing,
662 keyEventObj, policyFlags, isScreenOn);
663 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
664 wmActions = 0;
665 }
666 android_view_KeyEvent_recycle(env, keyEventObj);
667 env->DeleteLocalRef(keyEventObj);
668 } else {
669 LOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700670 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700671 }
672
Jeff Brown1f245102010-11-18 20:53:46 -0800673 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700674 if (!isScreenOn) {
675 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700676 }
677
678 if (!isScreenBright) {
679 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
680 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700681 }
682
Jeff Brown56194eb2011-03-02 19:23:13 -0800683 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700684 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700685 policyFlags |= POLICY_FLAG_PASS_TO_USER;
686 }
687}
688
Jeff Brown56194eb2011-03-02 19:23:13 -0800689void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700690 // Policy:
691 // - Ignore untrusted events and pass them along.
692 // - No special filtering for injected events required at this time.
693 // - Filter normal events based on screen state.
694 // - For normal events brighten (but do not wake) the screen if currently dim.
695 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
696 if (isScreenOn()) {
697 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700698
Jeff Brown3122e442010-10-11 23:32:49 -0700699 if (!isScreenBright()) {
700 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
701 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800702 } else {
703 JNIEnv* env = jniEnv();
704 jint wmActions = env->CallIntMethod(mCallbacksObj,
705 gCallbacksClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
706 policyFlags);
707 if (checkAndClearExceptionFromCallback(env,
708 "interceptMotionBeforeQueueingWhenScreenOff")) {
709 wmActions = 0;
710 }
711
712 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
713 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700714 }
Jeff Brown3122e442010-10-11 23:32:49 -0700715 } else {
716 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700717 }
718}
719
Jeff Brown56194eb2011-03-02 19:23:13 -0800720void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
721 uint32_t& policyFlags) {
722 enum {
723 WM_ACTION_PASS_TO_USER = 1,
724 WM_ACTION_POKE_USER_ACTIVITY = 2,
725 WM_ACTION_GO_TO_SLEEP = 4,
726 };
727
728 if (wmActions & WM_ACTION_GO_TO_SLEEP) {
Jeff Brownb23bdf52011-03-07 20:11:22 -0800729#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown56194eb2011-03-02 19:23:13 -0800730 LOGD("handleInterceptActions: Going to sleep.");
731#endif
732 android_server_PowerManagerService_goToSleep(when);
733 }
734
735 if (wmActions & WM_ACTION_POKE_USER_ACTIVITY) {
Jeff Brownb23bdf52011-03-07 20:11:22 -0800736#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown56194eb2011-03-02 19:23:13 -0800737 LOGD("handleInterceptActions: Poking user activity.");
738#endif
739 android_server_PowerManagerService_userActivity(when, POWER_MANAGER_BUTTON_EVENT);
740 }
741
742 if (wmActions & WM_ACTION_PASS_TO_USER) {
743 policyFlags |= POLICY_FLAG_PASS_TO_USER;
744 } else {
Jeff Brownb23bdf52011-03-07 20:11:22 -0800745#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown56194eb2011-03-02 19:23:13 -0800746 LOGD("handleInterceptActions: Not passing key to user.");
747#endif
748 }
749}
750
Jeff Brown928e0542011-01-10 11:17:36 -0800751bool NativeInputManager::interceptKeyBeforeDispatching(
752 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700753 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700754 // Policy:
755 // - Ignore untrusted events and pass them along.
756 // - Filter normal events and trusted injected events through the window manager policy to
757 // handle the HOME key and the like.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800758 bool result = false;
Jeff Brown3122e442010-10-11 23:32:49 -0700759 if (policyFlags & POLICY_FLAG_TRUSTED) {
760 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700761
Jeff Brown928e0542011-01-10 11:17:36 -0800762 // Note: inputWindowHandle may be null.
763 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800764 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
765 if (keyEventObj) {
766 jboolean consumed = env->CallBooleanMethod(mCallbacksObj,
767 gCallbacksClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800768 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800769 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
770 android_view_KeyEvent_recycle(env, keyEventObj);
771 env->DeleteLocalRef(keyEventObj);
772 result = consumed && !error;
773 } else {
774 LOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800775 }
Jeff Brown928e0542011-01-10 11:17:36 -0800776 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700777 }
Jeff Brown1f245102010-11-18 20:53:46 -0800778 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700779}
780
Jeff Brown928e0542011-01-10 11:17:36 -0800781bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800782 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700783 // Policy:
784 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800785 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700786 if (policyFlags & POLICY_FLAG_TRUSTED) {
787 JNIEnv* env = jniEnv();
788
Jeff Brown928e0542011-01-10 11:17:36 -0800789 // Note: inputWindowHandle may be null.
790 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800791 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
792 if (keyEventObj) {
Jeff Brown49ed71d2010-12-06 17:13:33 -0800793 jobject fallbackKeyEventObj = env->CallObjectMethod(mCallbacksObj,
Jeff Brown1f245102010-11-18 20:53:46 -0800794 gCallbacksClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800795 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown524ee642011-03-29 15:11:34 -0700796 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
797 fallbackKeyEventObj = NULL;
798 }
Jeff Brown1f245102010-11-18 20:53:46 -0800799 android_view_KeyEvent_recycle(env, keyEventObj);
800 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800801
802 if (fallbackKeyEventObj) {
803 // Note: outFallbackKeyEvent may be the same object as keyEvent.
804 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
805 outFallbackKeyEvent)) {
806 result = true;
807 }
808 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
809 env->DeleteLocalRef(fallbackKeyEventObj);
810 }
Jeff Brown1f245102010-11-18 20:53:46 -0800811 } else {
812 LOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800813 }
Jeff Brown928e0542011-01-10 11:17:36 -0800814 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700815 }
Jeff Brown1f245102010-11-18 20:53:46 -0800816 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700817}
818
Jeff Brown01ce2e92010-09-26 22:20:12 -0700819void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
820 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700821}
822
Jeff Brown349703e2010-06-22 01:27:15 -0700823
Jeff Brownb88102f2010-09-08 11:49:43 -0700824bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
825 int32_t injectorPid, int32_t injectorUid) {
826 JNIEnv* env = jniEnv();
827 jboolean result = env->CallBooleanMethod(mCallbacksObj,
828 gCallbacksClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brown524ee642011-03-29 15:11:34 -0700829 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
830 result = false;
831 }
Jeff Brown349703e2010-06-22 01:27:15 -0700832 return result;
833}
834
Jeff Brown86ea1f52011-04-12 22:39:53 -0700835void NativeInputManager::loadPointerResources(PointerResources* outResources) {
836 JNIEnv* env = jniEnv();
837
838 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
839 &outResources->spotHover);
840 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
841 &outResources->spotTouch);
842 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
843 &outResources->spotAnchor);
844}
845
Jeff Brown83c09682010-12-23 17:50:18 -0800846
Jeff Brown9c3cda02010-06-15 01:31:58 -0700847// ----------------------------------------------------------------------------
848
849static sp<NativeInputManager> gNativeInputManager;
850
Jeff Brown46b9ac02010-04-22 18:58:52 -0700851static bool checkInputManagerUnitialized(JNIEnv* env) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700852 if (gNativeInputManager == NULL) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700853 LOGE("Input manager not initialized.");
854 jniThrowRuntimeException(env, "Input manager not initialized.");
855 return true;
856 }
857 return false;
858}
859
860static void android_server_InputManager_nativeInit(JNIEnv* env, jclass clazz,
Jeff Brown86ea1f52011-04-12 22:39:53 -0700861 jobject contextObj, jobject callbacksObj, jobject messageQueueObj) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700862 if (gNativeInputManager == NULL) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800863 sp<Looper> looper = android_os_MessageQueue_getLooper(env, messageQueueObj);
Jeff Brown86ea1f52011-04-12 22:39:53 -0700864 gNativeInputManager = new NativeInputManager(contextObj, callbacksObj, looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700865 } else {
866 LOGE("Input manager already initialized.");
867 jniThrowRuntimeException(env, "Input manager already initialized.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700868 }
869}
870
871static void android_server_InputManager_nativeStart(JNIEnv* env, jclass clazz) {
872 if (checkInputManagerUnitialized(env)) {
873 return;
874 }
875
Jeff Brown9c3cda02010-06-15 01:31:58 -0700876 status_t result = gNativeInputManager->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700877 if (result) {
878 jniThrowRuntimeException(env, "Input manager could not be started.");
879 }
880}
881
882static void android_server_InputManager_nativeSetDisplaySize(JNIEnv* env, jclass clazz,
883 jint displayId, jint width, jint height) {
884 if (checkInputManagerUnitialized(env)) {
885 return;
886 }
887
888 // XXX we could get this from the SurfaceFlinger directly instead of requiring it
889 // to be passed in like this, not sure which is better but leaving it like this
890 // keeps the window manager in direct control of when display transitions propagate down
891 // to the input dispatcher
Jeff Brown9c3cda02010-06-15 01:31:58 -0700892 gNativeInputManager->setDisplaySize(displayId, width, height);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700893}
894
895static void android_server_InputManager_nativeSetDisplayOrientation(JNIEnv* env, jclass clazz,
896 jint displayId, jint orientation) {
897 if (checkInputManagerUnitialized(env)) {
898 return;
899 }
900
Jeff Brown9c3cda02010-06-15 01:31:58 -0700901 gNativeInputManager->setDisplayOrientation(displayId, orientation);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700902}
903
904static jint android_server_InputManager_nativeGetScanCodeState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700905 jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700906 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700907 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700908 }
909
Jeff Brownb88102f2010-09-08 11:49:43 -0700910 return gNativeInputManager->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700911 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700912}
913
914static jint android_server_InputManager_nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700915 jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700916 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700917 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700918 }
919
Jeff Brownb88102f2010-09-08 11:49:43 -0700920 return gNativeInputManager->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700921 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700922}
923
924static jint android_server_InputManager_nativeGetSwitchState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700925 jint deviceId, jint sourceMask, jint sw) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700926 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700927 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700928 }
929
Jeff Brownb88102f2010-09-08 11:49:43 -0700930 return gNativeInputManager->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700931 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700932}
933
934static jboolean android_server_InputManager_nativeHasKeys(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700935 jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700936 if (checkInputManagerUnitialized(env)) {
937 return JNI_FALSE;
938 }
939
940 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
941 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
942 jsize numCodes = env->GetArrayLength(keyCodes);
943 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700944 if (numCodes == env->GetArrayLength(keyCodes)) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700945 result = gNativeInputManager->getInputManager()->getReader()->hasKeys(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700946 deviceId, uint32_t(sourceMask), numCodes, codes, flags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700947 } else {
948 result = JNI_FALSE;
949 }
950
951 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
952 env->ReleaseIntArrayElements(keyCodes, codes, 0);
953 return result;
954}
955
956static void throwInputChannelNotInitialized(JNIEnv* env) {
957 jniThrowException(env, "java/lang/IllegalStateException",
958 "inputChannel is not initialized");
959}
960
961static void android_server_InputManager_handleInputChannelDisposed(JNIEnv* env,
962 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
963 LOGW("Input channel object '%s' was disposed without first being unregistered with "
964 "the input manager!", inputChannel->getName().string());
965
Jeff Brown9c3cda02010-06-15 01:31:58 -0700966 if (gNativeInputManager != NULL) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700967 gNativeInputManager->unregisterInputChannel(env, inputChannel);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700968 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700969}
970
971static void android_server_InputManager_nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
Jeff Brown928e0542011-01-10 11:17:36 -0800972 jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700973 if (checkInputManagerUnitialized(env)) {
974 return;
975 }
976
977 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
978 inputChannelObj);
979 if (inputChannel == NULL) {
980 throwInputChannelNotInitialized(env);
981 return;
982 }
983
Jeff Brown928e0542011-01-10 11:17:36 -0800984 sp<InputWindowHandle> inputWindowHandle =
985 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700986
987 status_t status = gNativeInputManager->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -0800988 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700989 if (status) {
990 jniThrowRuntimeException(env, "Failed to register input channel. "
991 "Check logs for details.");
992 return;
993 }
994
Jeff Browna41ca772010-08-11 14:46:32 -0700995 if (! monitor) {
996 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
997 android_server_InputManager_handleInputChannelDisposed, NULL);
998 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700999}
1000
1001static void android_server_InputManager_nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
1002 jobject inputChannelObj) {
1003 if (checkInputManagerUnitialized(env)) {
1004 return;
1005 }
1006
1007 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1008 inputChannelObj);
1009 if (inputChannel == NULL) {
1010 throwInputChannelNotInitialized(env);
1011 return;
1012 }
1013
1014 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1015
Jeff Brown7fbdc842010-06-17 20:52:56 -07001016 status_t status = gNativeInputManager->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001017 if (status) {
1018 jniThrowRuntimeException(env, "Failed to unregister input channel. "
1019 "Check logs for details.");
1020 }
1021}
1022
Jeff Brown6ec402b2010-07-28 15:48:59 -07001023static jint android_server_InputManager_nativeInjectInputEvent(JNIEnv* env, jclass clazz,
1024 jobject inputEventObj, jint injectorPid, jint injectorUid,
1025 jint syncMode, jint timeoutMillis) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07001026 if (checkInputManagerUnitialized(env)) {
1027 return INPUT_EVENT_INJECTION_FAILED;
1028 }
1029
Jeff Brown6ec402b2010-07-28 15:48:59 -07001030 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1031 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001032 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1033 if (status) {
1034 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1035 return INPUT_EVENT_INJECTION_FAILED;
1036 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001037
Jeff Brownb88102f2010-09-08 11:49:43 -07001038 return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent(
1039 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001040 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown5b2b4d92011-03-14 19:39:54 -07001041 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1042 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001043 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1044 return INPUT_EVENT_INJECTION_FAILED;
1045 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001046
Jeff Brownb88102f2010-09-08 11:49:43 -07001047 return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown5b2b4d92011-03-14 19:39:54 -07001048 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001049 } else {
1050 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001051 return INPUT_EVENT_INJECTION_FAILED;
1052 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001053}
1054
Jeff Brown349703e2010-06-22 01:27:15 -07001055static void android_server_InputManager_nativeSetInputWindows(JNIEnv* env, jclass clazz,
1056 jobjectArray windowObjArray) {
1057 if (checkInputManagerUnitialized(env)) {
1058 return;
1059 }
1060
1061 gNativeInputManager->setInputWindows(env, windowObjArray);
1062}
1063
1064static void android_server_InputManager_nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
1065 jobject applicationObj) {
1066 if (checkInputManagerUnitialized(env)) {
1067 return;
1068 }
1069
1070 gNativeInputManager->setFocusedApplication(env, applicationObj);
1071}
1072
1073static void android_server_InputManager_nativeSetInputDispatchMode(JNIEnv* env,
1074 jclass clazz, jboolean enabled, jboolean frozen) {
1075 if (checkInputManagerUnitialized(env)) {
1076 return;
1077 }
1078
1079 gNativeInputManager->setInputDispatchMode(enabled, frozen);
1080}
1081
Jeff Brown05dc66a2011-03-02 14:41:58 -08001082static void android_server_InputManager_nativeSetSystemUiVisibility(JNIEnv* env,
1083 jclass clazz, jint visibility) {
1084 if (checkInputManagerUnitialized(env)) {
1085 return;
1086 }
1087
1088 gNativeInputManager->setSystemUiVisibility(visibility);
1089}
1090
Jeff Brown8d608662010-08-30 03:02:23 -07001091static jobject android_server_InputManager_nativeGetInputDevice(JNIEnv* env,
1092 jclass clazz, jint deviceId) {
1093 if (checkInputManagerUnitialized(env)) {
1094 return NULL;
1095 }
1096
1097 InputDeviceInfo deviceInfo;
Jeff Brownb88102f2010-09-08 11:49:43 -07001098 status_t status = gNativeInputManager->getInputManager()->getReader()->getInputDeviceInfo(
Jeff Brown8d608662010-08-30 03:02:23 -07001099 deviceId, & deviceInfo);
1100 if (status) {
1101 return NULL;
1102 }
1103
1104 jobject deviceObj = env->NewObject(gInputDeviceClassInfo.clazz, gInputDeviceClassInfo.ctor);
1105 if (! deviceObj) {
1106 return NULL;
1107 }
1108
1109 jstring deviceNameObj = env->NewStringUTF(deviceInfo.getName().string());
1110 if (! deviceNameObj) {
1111 return NULL;
1112 }
1113
1114 env->SetIntField(deviceObj, gInputDeviceClassInfo.mId, deviceInfo.getId());
1115 env->SetObjectField(deviceObj, gInputDeviceClassInfo.mName, deviceNameObj);
1116 env->SetIntField(deviceObj, gInputDeviceClassInfo.mSources, deviceInfo.getSources());
1117 env->SetIntField(deviceObj, gInputDeviceClassInfo.mKeyboardType, deviceInfo.getKeyboardType());
1118
Jeff Brownefd32662011-03-08 15:13:06 -08001119 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Brown8d608662010-08-30 03:02:23 -07001120 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -08001121 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
Jeff Brown8d608662010-08-30 03:02:23 -07001122 env->CallVoidMethod(deviceObj, gInputDeviceClassInfo.addMotionRange,
Jeff Brownefd32662011-03-08 15:13:06 -08001123 range.axis, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Brown8d608662010-08-30 03:02:23 -07001124 if (env->ExceptionCheck()) {
1125 return NULL;
1126 }
1127 }
1128
1129 return deviceObj;
1130}
1131
1132static jintArray android_server_InputManager_nativeGetInputDeviceIds(JNIEnv* env,
1133 jclass clazz) {
1134 if (checkInputManagerUnitialized(env)) {
1135 return NULL;
1136 }
1137
1138 Vector<int> deviceIds;
Jeff Brownb88102f2010-09-08 11:49:43 -07001139 gNativeInputManager->getInputManager()->getReader()->getInputDeviceIds(deviceIds);
Jeff Brown8d608662010-08-30 03:02:23 -07001140
1141 jintArray deviceIdsObj = env->NewIntArray(deviceIds.size());
1142 if (! deviceIdsObj) {
1143 return NULL;
1144 }
1145
1146 env->SetIntArrayRegion(deviceIdsObj, 0, deviceIds.size(), deviceIds.array());
1147 return deviceIdsObj;
1148}
1149
Jeff Brown57c59372010-09-21 18:22:55 -07001150static void android_server_InputManager_nativeGetInputConfiguration(JNIEnv* env,
1151 jclass clazz, jobject configObj) {
1152 if (checkInputManagerUnitialized(env)) {
1153 return;
1154 }
1155
1156 InputConfiguration config;
1157 gNativeInputManager->getInputManager()->getReader()->getInputConfiguration(& config);
1158
1159 env->SetIntField(configObj, gConfigurationClassInfo.touchscreen, config.touchScreen);
1160 env->SetIntField(configObj, gConfigurationClassInfo.keyboard, config.keyboard);
1161 env->SetIntField(configObj, gConfigurationClassInfo.navigation, config.navigation);
1162}
1163
Jeff Browne6504122010-09-27 14:52:15 -07001164static jboolean android_server_InputManager_nativeTransferTouchFocus(JNIEnv* env,
1165 jclass clazz, jobject fromChannelObj, jobject toChannelObj) {
1166 if (checkInputManagerUnitialized(env)) {
1167 return false;
1168 }
1169
1170 sp<InputChannel> fromChannel =
1171 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1172 sp<InputChannel> toChannel =
1173 android_view_InputChannel_getInputChannel(env, toChannelObj);
1174
1175 if (fromChannel == NULL || toChannel == NULL) {
1176 return false;
1177 }
1178
1179 return gNativeInputManager->getInputManager()->getDispatcher()->
1180 transferTouchFocus(fromChannel, toChannel);
1181}
1182
Jeff Browne33348b2010-07-15 23:54:05 -07001183static jstring android_server_InputManager_nativeDump(JNIEnv* env, jclass clazz) {
1184 if (checkInputManagerUnitialized(env)) {
1185 return NULL;
1186 }
1187
Jeff Brownb88102f2010-09-08 11:49:43 -07001188 String8 dump;
1189 gNativeInputManager->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001190 return env->NewStringUTF(dump.string());
1191}
1192
Jeff Brown9c3cda02010-06-15 01:31:58 -07001193// ----------------------------------------------------------------------------
1194
Jeff Brown46b9ac02010-04-22 18:58:52 -07001195static JNINativeMethod gInputManagerMethods[] = {
1196 /* name, signature, funcPtr */
Jeff Brown86ea1f52011-04-12 22:39:53 -07001197 { "nativeInit", "(Landroid/content/Context;"
1198 "Lcom/android/server/wm/InputManager$Callbacks;Landroid/os/MessageQueue;)V",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001199 (void*) android_server_InputManager_nativeInit },
1200 { "nativeStart", "()V",
1201 (void*) android_server_InputManager_nativeStart },
1202 { "nativeSetDisplaySize", "(III)V",
1203 (void*) android_server_InputManager_nativeSetDisplaySize },
1204 { "nativeSetDisplayOrientation", "(II)V",
1205 (void*) android_server_InputManager_nativeSetDisplayOrientation },
1206 { "nativeGetScanCodeState", "(III)I",
1207 (void*) android_server_InputManager_nativeGetScanCodeState },
1208 { "nativeGetKeyCodeState", "(III)I",
1209 (void*) android_server_InputManager_nativeGetKeyCodeState },
1210 { "nativeGetSwitchState", "(III)I",
1211 (void*) android_server_InputManager_nativeGetSwitchState },
Jeff Brown6d0fec22010-07-23 21:28:06 -07001212 { "nativeHasKeys", "(II[I[Z)Z",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001213 (void*) android_server_InputManager_nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001214 { "nativeRegisterInputChannel",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001215 "(Landroid/view/InputChannel;Lcom/android/server/wm/InputWindowHandle;Z)V",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001216 (void*) android_server_InputManager_nativeRegisterInputChannel },
1217 { "nativeUnregisterInputChannel", "(Landroid/view/InputChannel;)V",
Jeff Brown7fbdc842010-06-17 20:52:56 -07001218 (void*) android_server_InputManager_nativeUnregisterInputChannel },
Jeff Brown6ec402b2010-07-28 15:48:59 -07001219 { "nativeInjectInputEvent", "(Landroid/view/InputEvent;IIII)I",
1220 (void*) android_server_InputManager_nativeInjectInputEvent },
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001221 { "nativeSetInputWindows", "([Lcom/android/server/wm/InputWindow;)V",
Jeff Brown349703e2010-06-22 01:27:15 -07001222 (void*) android_server_InputManager_nativeSetInputWindows },
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001223 { "nativeSetFocusedApplication", "(Lcom/android/server/wm/InputApplication;)V",
Jeff Brown349703e2010-06-22 01:27:15 -07001224 (void*) android_server_InputManager_nativeSetFocusedApplication },
1225 { "nativeSetInputDispatchMode", "(ZZ)V",
1226 (void*) android_server_InputManager_nativeSetInputDispatchMode },
Jeff Brown05dc66a2011-03-02 14:41:58 -08001227 { "nativeSetSystemUiVisibility", "(I)V",
1228 (void*) android_server_InputManager_nativeSetSystemUiVisibility },
Jeff Brown8d608662010-08-30 03:02:23 -07001229 { "nativeGetInputDevice", "(I)Landroid/view/InputDevice;",
1230 (void*) android_server_InputManager_nativeGetInputDevice },
1231 { "nativeGetInputDeviceIds", "()[I",
1232 (void*) android_server_InputManager_nativeGetInputDeviceIds },
Jeff Brown57c59372010-09-21 18:22:55 -07001233 { "nativeGetInputConfiguration", "(Landroid/content/res/Configuration;)V",
1234 (void*) android_server_InputManager_nativeGetInputConfiguration },
Jeff Browne6504122010-09-27 14:52:15 -07001235 { "nativeTransferTouchFocus", "(Landroid/view/InputChannel;Landroid/view/InputChannel;)Z",
1236 (void*) android_server_InputManager_nativeTransferTouchFocus },
Jeff Browne33348b2010-07-15 23:54:05 -07001237 { "nativeDump", "()Ljava/lang/String;",
1238 (void*) android_server_InputManager_nativeDump },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001239};
1240
1241#define FIND_CLASS(var, className) \
1242 var = env->FindClass(className); \
1243 LOG_FATAL_IF(! var, "Unable to find class " className); \
1244 var = jclass(env->NewGlobalRef(var));
1245
1246#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1247 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1248 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1249
1250#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1251 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1252 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1253
1254int register_android_server_InputManager(JNIEnv* env) {
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001255 int res = jniRegisterNativeMethods(env, "com/android/server/wm/InputManager",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001256 gInputManagerMethods, NELEM(gInputManagerMethods));
1257 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1258
Jeff Brown9c3cda02010-06-15 01:31:58 -07001259 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001260
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001261 FIND_CLASS(gCallbacksClassInfo.clazz, "com/android/server/wm/InputManager$Callbacks");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001262
Jeff Brown46b9ac02010-04-22 18:58:52 -07001263 GET_METHOD_ID(gCallbacksClassInfo.notifyConfigurationChanged, gCallbacksClassInfo.clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001264 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001265
1266 GET_METHOD_ID(gCallbacksClassInfo.notifyLidSwitchChanged, gCallbacksClassInfo.clazz,
1267 "notifyLidSwitchChanged", "(JZ)V");
1268
Jeff Brown7fbdc842010-06-17 20:52:56 -07001269 GET_METHOD_ID(gCallbacksClassInfo.notifyInputChannelBroken, gCallbacksClassInfo.clazz,
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001270 "notifyInputChannelBroken", "(Lcom/android/server/wm/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001271
Jeff Brown349703e2010-06-22 01:27:15 -07001272 GET_METHOD_ID(gCallbacksClassInfo.notifyANR, gCallbacksClassInfo.clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001273 "notifyANR",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001274 "(Lcom/android/server/wm/InputApplicationHandle;Lcom/android/server/wm/InputWindowHandle;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001275
Jeff Brown349703e2010-06-22 01:27:15 -07001276 GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeQueueing, gCallbacksClassInfo.clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001277 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001278
Jeff Brown56194eb2011-03-02 19:23:13 -08001279 GET_METHOD_ID(gCallbacksClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
1280 gCallbacksClassInfo.clazz,
1281 "interceptMotionBeforeQueueingWhenScreenOff", "(I)I");
1282
Jeff Brown349703e2010-06-22 01:27:15 -07001283 GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeDispatching, gCallbacksClassInfo.clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001284 "interceptKeyBeforeDispatching",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001285 "(Lcom/android/server/wm/InputWindowHandle;Landroid/view/KeyEvent;I)Z");
Jeff Brown349703e2010-06-22 01:27:15 -07001286
Jeff Brown3915bb82010-11-05 15:02:16 -07001287 GET_METHOD_ID(gCallbacksClassInfo.dispatchUnhandledKey, gCallbacksClassInfo.clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001288 "dispatchUnhandledKey",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001289 "(Lcom/android/server/wm/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001290
Jeff Brown349703e2010-06-22 01:27:15 -07001291 GET_METHOD_ID(gCallbacksClassInfo.checkInjectEventsPermission, gCallbacksClassInfo.clazz,
1292 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001293
Jeff Brown46b9ac02010-04-22 18:58:52 -07001294 GET_METHOD_ID(gCallbacksClassInfo.filterTouchEvents, gCallbacksClassInfo.clazz,
1295 "filterTouchEvents", "()Z");
1296
1297 GET_METHOD_ID(gCallbacksClassInfo.filterJumpyTouchEvents, gCallbacksClassInfo.clazz,
1298 "filterJumpyTouchEvents", "()Z");
1299
Jeff Brownfe508922011-01-18 15:10:10 -08001300 GET_METHOD_ID(gCallbacksClassInfo.getVirtualKeyQuietTimeMillis, gCallbacksClassInfo.clazz,
1301 "getVirtualKeyQuietTimeMillis", "()I");
1302
Jeff Brown46b9ac02010-04-22 18:58:52 -07001303 GET_METHOD_ID(gCallbacksClassInfo.getExcludedDeviceNames, gCallbacksClassInfo.clazz,
1304 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1305
Jeff Browna4547672011-03-02 21:38:11 -08001306 GET_METHOD_ID(gCallbacksClassInfo.getKeyRepeatTimeout, gCallbacksClassInfo.clazz,
1307 "getKeyRepeatTimeout", "()I");
1308
1309 GET_METHOD_ID(gCallbacksClassInfo.getKeyRepeatDelay, gCallbacksClassInfo.clazz,
1310 "getKeyRepeatDelay", "()I");
1311
Jeff Brown214eaf42011-05-26 19:17:02 -07001312 GET_METHOD_ID(gCallbacksClassInfo.getTapTimeout, gCallbacksClassInfo.clazz,
1313 "getTapTimeout", "()I");
1314
1315 GET_METHOD_ID(gCallbacksClassInfo.getDoubleTapTimeout, gCallbacksClassInfo.clazz,
1316 "getDoubleTapTimeout", "()I");
1317
1318 GET_METHOD_ID(gCallbacksClassInfo.getLongPressTimeout, gCallbacksClassInfo.clazz,
1319 "getLongPressTimeout", "()I");
1320
1321 GET_METHOD_ID(gCallbacksClassInfo.getTouchSlop, gCallbacksClassInfo.clazz,
1322 "getTouchSlop", "()I");
1323
Jeff Brownae9fc032010-08-18 15:51:08 -07001324 GET_METHOD_ID(gCallbacksClassInfo.getMaxEventsPerSecond, gCallbacksClassInfo.clazz,
1325 "getMaxEventsPerSecond", "()I");
1326
Jeff Brown83c09682010-12-23 17:50:18 -08001327 GET_METHOD_ID(gCallbacksClassInfo.getPointerLayer, gCallbacksClassInfo.clazz,
1328 "getPointerLayer", "()I");
1329
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001330 GET_METHOD_ID(gCallbacksClassInfo.getPointerIcon, gCallbacksClassInfo.clazz,
Jeff Brown86ea1f52011-04-12 22:39:53 -07001331 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001332
Jeff Brown6ec402b2010-07-28 15:48:59 -07001333 // KeyEvent
1334
1335 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
1336
Jeff Brown8d608662010-08-30 03:02:23 -07001337 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001338
1339 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
1340
Jeff Brown8d608662010-08-30 03:02:23 -07001341 // InputDevice
1342
1343 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1344
1345 GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz,
1346 "<init>", "()V");
1347
1348 GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz,
Jeff Brownefd32662011-03-08 15:13:06 -08001349 "addMotionRange", "(IIFFFF)V");
Jeff Brown8d608662010-08-30 03:02:23 -07001350
1351 GET_FIELD_ID(gInputDeviceClassInfo.mId, gInputDeviceClassInfo.clazz,
1352 "mId", "I");
1353
1354 GET_FIELD_ID(gInputDeviceClassInfo.mName, gInputDeviceClassInfo.clazz,
1355 "mName", "Ljava/lang/String;");
1356
1357 GET_FIELD_ID(gInputDeviceClassInfo.mSources, gInputDeviceClassInfo.clazz,
1358 "mSources", "I");
1359
1360 GET_FIELD_ID(gInputDeviceClassInfo.mKeyboardType, gInputDeviceClassInfo.clazz,
1361 "mKeyboardType", "I");
1362
Jeff Brown57c59372010-09-21 18:22:55 -07001363 // Configuration
1364
1365 FIND_CLASS(gConfigurationClassInfo.clazz, "android/content/res/Configuration");
1366
1367 GET_FIELD_ID(gConfigurationClassInfo.touchscreen, gConfigurationClassInfo.clazz,
1368 "touchscreen", "I");
1369
1370 GET_FIELD_ID(gConfigurationClassInfo.keyboard, gConfigurationClassInfo.clazz,
1371 "keyboard", "I");
1372
1373 GET_FIELD_ID(gConfigurationClassInfo.navigation, gConfigurationClassInfo.clazz,
1374 "navigation", "I");
1375
Jeff Brown46b9ac02010-04-22 18:58:52 -07001376 return 0;
1377}
1378
Jeff Brown46b9ac02010-04-22 18:58:52 -07001379} /* namespace android */