blob: bd4e7875630f71e8ef1ce4f0e6d3d2f976521b96 [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>
39
Jeff Brown05dc66a2011-03-02 14:41:58 -080040#include <android_os_MessageQueue.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080041#include <android_view_KeyEvent.h>
42#include <android_view_MotionEvent.h>
43#include <android_view_InputChannel.h>
44#include <android/graphics/GraphicsJNI.h>
45
Jeff Brown00fa7bd2010-07-02 15:37:36 -070046#include "com_android_server_PowerManagerService.h"
Jeff Brown928e0542011-01-10 11:17:36 -080047#include "com_android_server_InputApplication.h"
48#include "com_android_server_InputApplicationHandle.h"
49#include "com_android_server_InputWindow.h"
50#include "com_android_server_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070051
52namespace android {
53
Jeff Brown46b9ac02010-04-22 18:58:52 -070054static struct {
55 jclass clazz;
56
Jeff Brown46b9ac02010-04-22 18:58:52 -070057 jmethodID notifyConfigurationChanged;
58 jmethodID notifyLidSwitchChanged;
Jeff Brown7fbdc842010-06-17 20:52:56 -070059 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070060 jmethodID notifyANR;
Jeff Brown349703e2010-06-22 01:27:15 -070061 jmethodID interceptKeyBeforeQueueing;
Jeff Brown56194eb2011-03-02 19:23:13 -080062 jmethodID interceptMotionBeforeQueueingWhenScreenOff;
Jeff Brown349703e2010-06-22 01:27:15 -070063 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070064 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070065 jmethodID checkInjectEventsPermission;
Jeff Brown46b9ac02010-04-22 18:58:52 -070066 jmethodID filterTouchEvents;
67 jmethodID filterJumpyTouchEvents;
Jeff Brownfe508922011-01-18 15:10:10 -080068 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070069 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080070 jmethodID getKeyRepeatTimeout;
71 jmethodID getKeyRepeatDelay;
Jeff Brownae9fc032010-08-18 15:51:08 -070072 jmethodID getMaxEventsPerSecond;
Jeff Brown83c09682010-12-23 17:50:18 -080073 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080074 jmethodID getPointerIcon;
Jeff Brown46b9ac02010-04-22 18:58:52 -070075} gCallbacksClassInfo;
76
77static struct {
78 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070079} gKeyEventClassInfo;
80
81static struct {
82 jclass clazz;
83} gMotionEventClassInfo;
84
Jeff Brown8d608662010-08-30 03:02:23 -070085static struct {
86 jclass clazz;
87
88 jmethodID ctor;
89 jmethodID addMotionRange;
90
91 jfieldID mId;
92 jfieldID mName;
93 jfieldID mSources;
94 jfieldID mKeyboardType;
Jeff Brown8d608662010-08-30 03:02:23 -070095} gInputDeviceClassInfo;
96
Jeff Brown57c59372010-09-21 18:22:55 -070097static struct {
98 jclass clazz;
99
100 jfieldID touchscreen;
101 jfieldID keyboard;
102 jfieldID navigation;
103} gConfigurationClassInfo;
104
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800105static struct {
106 jclass clazz;
Jeff Brown349703e2010-06-22 01:27:15 -0700107
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800108 jfieldID bitmap;
109 jfieldID hotSpotX;
110 jfieldID hotSpotY;
111} gPointerIconClassInfo;
Jeff Brown83c09682010-12-23 17:50:18 -0800112
Jeff Brown928e0542011-01-10 11:17:36 -0800113
114// --- Global functions ---
115
116static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
117 const sp<InputApplicationHandle>& inputApplicationHandle) {
118 if (inputApplicationHandle == NULL) {
119 return NULL;
120 }
121 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
122 getInputApplicationHandleObjLocalRef(env);
123}
124
125static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
126 const sp<InputWindowHandle>& inputWindowHandle) {
127 if (inputWindowHandle == NULL) {
128 return NULL;
129 }
130 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
131 getInputWindowHandleObjLocalRef(env);
132}
133
134
135// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800136
Jeff Brown9c3cda02010-06-15 01:31:58 -0700137class NativeInputManager : public virtual RefBase,
138 public virtual InputReaderPolicyInterface,
139 public virtual InputDispatcherPolicyInterface {
140protected:
141 virtual ~NativeInputManager();
142
143public:
Jeff Brown05dc66a2011-03-02 14:41:58 -0800144 NativeInputManager(jobject callbacksObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700145
146 inline sp<InputManager> getInputManager() const { return mInputManager; }
147
Jeff Brownb88102f2010-09-08 11:49:43 -0700148 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700149
Jeff Brown9c3cda02010-06-15 01:31:58 -0700150 void setDisplaySize(int32_t displayId, int32_t width, int32_t height);
151 void setDisplayOrientation(int32_t displayId, int32_t orientation);
152
Jeff Brown7fbdc842010-06-17 20:52:56 -0700153 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800154 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700155 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
156
Jeff Brown349703e2010-06-22 01:27:15 -0700157 void setInputWindows(JNIEnv* env, jobjectArray windowObjArray);
158 void setFocusedApplication(JNIEnv* env, jobject applicationObj);
159 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800160 void setSystemUiVisibility(int32_t visibility);
Jeff Brown349703e2010-06-22 01:27:15 -0700161
Jeff Brown9c3cda02010-06-15 01:31:58 -0700162 /* --- InputReaderPolicyInterface implementation --- */
163
164 virtual bool getDisplayInfo(int32_t displayId,
165 int32_t* width, int32_t* height, int32_t* orientation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700166 virtual bool filterTouchEvents();
167 virtual bool filterJumpyTouchEvents();
Jeff Brownfe508922011-01-18 15:10:10 -0800168 virtual nsecs_t getVirtualKeyQuietTime();
Jeff Brown9c3cda02010-06-15 01:31:58 -0700169 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames);
Jeff Brown83c09682010-12-23 17:50:18 -0800170 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700171
172 /* --- InputDispatcherPolicyInterface implementation --- */
173
Jeff Browne20c9e02010-10-11 14:20:19 -0700174 virtual void notifySwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
175 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700176 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700177 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800178 const sp<InputWindowHandle>& inputWindowHandle);
179 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700180 virtual nsecs_t getKeyRepeatTimeout();
Jeff Brownb21fb102010-09-07 10:44:57 -0700181 virtual nsecs_t getKeyRepeatDelay();
Jeff Brownae9fc032010-08-18 15:51:08 -0700182 virtual int32_t getMaxEventsPerSecond();
Jeff Brown1f245102010-11-18 20:53:46 -0800183 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800184 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800185 virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700186 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800187 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800188 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700189 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700190 virtual bool checkInjectEventsPermissionNonReentrant(
191 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700192
193private:
194 sp<InputManager> mInputManager;
195
196 jobject mCallbacksObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800197 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700198
199 // Cached filtering policies.
200 int32_t mFilterTouchEvents;
201 int32_t mFilterJumpyTouchEvents;
Jeff Brownfe508922011-01-18 15:10:10 -0800202 nsecs_t mVirtualKeyQuietTime;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700203
Jeff Browna4547672011-03-02 21:38:11 -0800204 // Cached key repeat policy.
205 nsecs_t mKeyRepeatTimeout;
206 nsecs_t mKeyRepeatDelay;
207
Jeff Brownae9fc032010-08-18 15:51:08 -0700208 // Cached throttling policy.
209 int32_t mMaxEventsPerSecond;
210
Jeff Brown83c09682010-12-23 17:50:18 -0800211 Mutex mLock;
212 struct Locked {
213 // Display size information.
214 int32_t displayWidth, displayHeight; // -1 when initialized
215 int32_t displayOrientation;
216
Jeff Brown05dc66a2011-03-02 14:41:58 -0800217 // System UI visibility.
218 int32_t systemUiVisibility;
219
Jeff Brown83c09682010-12-23 17:50:18 -0800220 // Pointer controller singleton, created and destroyed as needed.
221 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800222 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700223
Jeff Brown05dc66a2011-03-02 14:41:58 -0800224 void updateInactivityFadeDelayLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800225 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800226
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700227 // Power manager interactions.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700228 bool isScreenOn();
229 bool isScreenBright();
230
Jeff Brownb88102f2010-09-08 11:49:43 -0700231 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700232
Jeff Brown9c3cda02010-06-15 01:31:58 -0700233 static inline JNIEnv* jniEnv() {
234 return AndroidRuntime::getJNIEnv();
235 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700236};
237
Jeff Brown928e0542011-01-10 11:17:36 -0800238
Jeff Brown9c3cda02010-06-15 01:31:58 -0700239
Jeff Brown05dc66a2011-03-02 14:41:58 -0800240NativeInputManager::NativeInputManager(jobject callbacksObj, const sp<Looper>& looper) :
241 mLooper(looper),
242 mFilterTouchEvents(-1), mFilterJumpyTouchEvents(-1), mVirtualKeyQuietTime(-1),
Jeff Browna4547672011-03-02 21:38:11 -0800243 mKeyRepeatTimeout(-1), mKeyRepeatDelay(-1),
Jeff Brown05dc66a2011-03-02 14:41:58 -0800244 mMaxEventsPerSecond(-1) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700245 JNIEnv* env = jniEnv();
246
247 mCallbacksObj = env->NewGlobalRef(callbacksObj);
248
Jeff Brown83c09682010-12-23 17:50:18 -0800249 {
250 AutoMutex _l(mLock);
251 mLocked.displayWidth = -1;
252 mLocked.displayHeight = -1;
253 mLocked.displayOrientation = ROTATION_0;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800254
255 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown83c09682010-12-23 17:50:18 -0800256 }
257
Jeff Brown9c3cda02010-06-15 01:31:58 -0700258 sp<EventHub> eventHub = new EventHub();
259 mInputManager = new InputManager(eventHub, this, this);
260}
261
262NativeInputManager::~NativeInputManager() {
263 JNIEnv* env = jniEnv();
264
265 env->DeleteGlobalRef(mCallbacksObj);
266}
267
Jeff Brownb88102f2010-09-08 11:49:43 -0700268void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700269 mInputManager->getReader()->dump(dump);
270 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700271
Jeff Brownb88102f2010-09-08 11:49:43 -0700272 mInputManager->getDispatcher()->dump(dump);
273 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700274}
275
Jeff Brown7fbdc842010-06-17 20:52:56 -0700276bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700277 if (env->ExceptionCheck()) {
278 LOGE("An exception was thrown by callback '%s'.", methodName);
279 LOGE_EX(env);
280 env->ExceptionClear();
281 return true;
282 }
283 return false;
284}
285
286void NativeInputManager::setDisplaySize(int32_t displayId, int32_t width, int32_t height) {
287 if (displayId == 0) {
Jeff Brown83c09682010-12-23 17:50:18 -0800288 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700289
Jeff Brown83c09682010-12-23 17:50:18 -0800290 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
291 mLocked.displayWidth = width;
292 mLocked.displayHeight = height;
293
294 sp<PointerController> controller = mLocked.pointerController.promote();
295 if (controller != NULL) {
296 controller->setDisplaySize(width, height);
297 }
298 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700299 }
300}
301
302void NativeInputManager::setDisplayOrientation(int32_t displayId, int32_t orientation) {
303 if (displayId == 0) {
Jeff Brown83c09682010-12-23 17:50:18 -0800304 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700305
Jeff Brown83c09682010-12-23 17:50:18 -0800306 if (mLocked.displayOrientation != orientation) {
307 mLocked.displayOrientation = orientation;
308
309 sp<PointerController> controller = mLocked.pointerController.promote();
310 if (controller != NULL) {
311 controller->setDisplayOrientation(orientation);
312 }
313 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700314 }
315}
316
Jeff Brown7fbdc842010-06-17 20:52:56 -0700317status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Brown928e0542011-01-10 11:17:36 -0800318 const sp<InputChannel>& inputChannel,
319 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
320 return mInputManager->getDispatcher()->registerInputChannel(
321 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700322}
323
324status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
325 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700326 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700327}
328
Jeff Brown9c3cda02010-06-15 01:31:58 -0700329bool NativeInputManager::getDisplayInfo(int32_t displayId,
330 int32_t* width, int32_t* height, int32_t* orientation) {
331 bool result = false;
332 if (displayId == 0) {
Jeff Brown83c09682010-12-23 17:50:18 -0800333 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700334
Jeff Brown83c09682010-12-23 17:50:18 -0800335 if (mLocked.displayWidth > 0 && mLocked.displayHeight > 0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700336 if (width) {
Jeff Brown83c09682010-12-23 17:50:18 -0800337 *width = mLocked.displayWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700338 }
339 if (height) {
Jeff Brown83c09682010-12-23 17:50:18 -0800340 *height = mLocked.displayHeight;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700341 }
342 if (orientation) {
Jeff Brown83c09682010-12-23 17:50:18 -0800343 *orientation = mLocked.displayOrientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700344 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700345 result = true;
346 }
347 }
348 return result;
349}
350
Jeff Brown9c3cda02010-06-15 01:31:58 -0700351bool NativeInputManager::filterTouchEvents() {
352 if (mFilterTouchEvents < 0) {
353 JNIEnv* env = jniEnv();
354
355 jboolean result = env->CallBooleanMethod(mCallbacksObj,
356 gCallbacksClassInfo.filterTouchEvents);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700357 if (checkAndClearExceptionFromCallback(env, "filterTouchEvents")) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700358 result = false;
359 }
360
361 mFilterTouchEvents = result ? 1 : 0;
362 }
363 return mFilterTouchEvents;
364}
365
366bool NativeInputManager::filterJumpyTouchEvents() {
367 if (mFilterJumpyTouchEvents < 0) {
368 JNIEnv* env = jniEnv();
369
370 jboolean result = env->CallBooleanMethod(mCallbacksObj,
371 gCallbacksClassInfo.filterJumpyTouchEvents);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700372 if (checkAndClearExceptionFromCallback(env, "filterJumpyTouchEvents")) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700373 result = false;
374 }
375
376 mFilterJumpyTouchEvents = result ? 1 : 0;
377 }
378 return mFilterJumpyTouchEvents;
379}
380
Jeff Brownfe508922011-01-18 15:10:10 -0800381nsecs_t NativeInputManager::getVirtualKeyQuietTime() {
382 if (mVirtualKeyQuietTime < 0) {
383 JNIEnv* env = jniEnv();
384
385 jint result = env->CallIntMethod(mCallbacksObj,
386 gCallbacksClassInfo.getVirtualKeyQuietTimeMillis);
387 if (checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
388 result = 0;
389 }
390 if (result < 0) {
391 result = 0;
392 }
393
394 mVirtualKeyQuietTime = milliseconds_to_nanoseconds(result);
395 }
396 return mVirtualKeyQuietTime;
397}
398
Jeff Brown9c3cda02010-06-15 01:31:58 -0700399void NativeInputManager::getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) {
Jeff Brown8d608662010-08-30 03:02:23 -0700400 outExcludedDeviceNames.clear();
401
Jeff Brown9c3cda02010-06-15 01:31:58 -0700402 JNIEnv* env = jniEnv();
403
404 jobjectArray result = jobjectArray(env->CallObjectMethod(mCallbacksObj,
405 gCallbacksClassInfo.getExcludedDeviceNames));
Jeff Brown7fbdc842010-06-17 20:52:56 -0700406 if (! checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && result) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700407 jsize length = env->GetArrayLength(result);
408 for (jsize i = 0; i < length; i++) {
409 jstring item = jstring(env->GetObjectArrayElement(result, i));
410
411 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
412 outExcludedDeviceNames.add(String8(deviceNameChars));
413 env->ReleaseStringUTFChars(item, deviceNameChars);
414
415 env->DeleteLocalRef(item);
416 }
417 env->DeleteLocalRef(result);
418 }
419}
420
Jeff Brown83c09682010-12-23 17:50:18 -0800421sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
422 AutoMutex _l(mLock);
423
424 sp<PointerController> controller = mLocked.pointerController.promote();
425 if (controller == NULL) {
426 JNIEnv* env = jniEnv();
427 jint layer = env->CallIntMethod(mCallbacksObj, gCallbacksClassInfo.getPointerLayer);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800428 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
429 layer = -1;
430 }
Jeff Brown83c09682010-12-23 17:50:18 -0800431
Jeff Brown05dc66a2011-03-02 14:41:58 -0800432 controller = new PointerController(mLooper, layer);
Jeff Brown83c09682010-12-23 17:50:18 -0800433 mLocked.pointerController = controller;
434
435 controller->setDisplaySize(mLocked.displayWidth, mLocked.displayHeight);
436 controller->setDisplayOrientation(mLocked.displayOrientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800437
438 jobject iconObj = env->CallObjectMethod(mCallbacksObj, gCallbacksClassInfo.getPointerIcon);
439 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon") && iconObj) {
440 jfloat iconHotSpotX = env->GetFloatField(iconObj, gPointerIconClassInfo.hotSpotX);
441 jfloat iconHotSpotY = env->GetFloatField(iconObj, gPointerIconClassInfo.hotSpotY);
442 jobject iconBitmapObj = env->GetObjectField(iconObj, gPointerIconClassInfo.bitmap);
443 if (iconBitmapObj) {
444 SkBitmap* iconBitmap = GraphicsJNI::getNativeBitmap(env, iconBitmapObj);
445 if (iconBitmap) {
446 controller->setPointerIcon(iconBitmap, iconHotSpotX, iconHotSpotY);
447 }
448 env->DeleteLocalRef(iconBitmapObj);
449 }
450 env->DeleteLocalRef(iconObj);
451 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800452
453 updateInactivityFadeDelayLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800454 }
455 return controller;
456}
457
Jeff Browne20c9e02010-10-11 14:20:19 -0700458void NativeInputManager::notifySwitch(nsecs_t when, int32_t switchCode,
459 int32_t switchValue, uint32_t policyFlags) {
460#if DEBUG_INPUT_DISPATCHER_POLICY
461 LOGD("notifySwitch - when=%lld, switchCode=%d, switchValue=%d, policyFlags=0x%x",
462 when, switchCode, switchValue, policyFlags);
463#endif
464
465 JNIEnv* env = jniEnv();
466
467 switch (switchCode) {
468 case SW_LID:
469 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyLidSwitchChanged,
470 when, switchValue == 0);
471 checkAndClearExceptionFromCallback(env, "notifyLidSwitchChanged");
472 break;
473 }
474}
475
Jeff Brown9c3cda02010-06-15 01:31:58 -0700476void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
477#if DEBUG_INPUT_DISPATCHER_POLICY
478 LOGD("notifyConfigurationChanged - when=%lld", when);
479#endif
480
481 JNIEnv* env = jniEnv();
482
Jeff Brown57c59372010-09-21 18:22:55 -0700483 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700484 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700485}
486
Jeff Brown519e0242010-09-15 15:18:56 -0700487nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brown928e0542011-01-10 11:17:36 -0800488 const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700489#if DEBUG_INPUT_DISPATCHER_POLICY
490 LOGD("notifyANR");
491#endif
492
493 JNIEnv* env = jniEnv();
494
Jeff Brown928e0542011-01-10 11:17:36 -0800495 jobject inputApplicationHandleObj =
496 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
497 jobject inputWindowHandleObj =
498 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownb88102f2010-09-08 11:49:43 -0700499
Jeff Brown519e0242010-09-15 15:18:56 -0700500 jlong newTimeout = env->CallLongMethod(mCallbacksObj,
Jeff Brown928e0542011-01-10 11:17:36 -0800501 gCallbacksClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700502 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
503 newTimeout = 0; // abort dispatch
504 } else {
505 assert(newTimeout >= 0);
506 }
507
Jeff Brown928e0542011-01-10 11:17:36 -0800508 env->DeleteLocalRef(inputWindowHandleObj);
509 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700510 return newTimeout;
511}
512
Jeff Brown928e0542011-01-10 11:17:36 -0800513void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700514#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown928e0542011-01-10 11:17:36 -0800515 LOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700516#endif
517
Jeff Brown7fbdc842010-06-17 20:52:56 -0700518 JNIEnv* env = jniEnv();
519
Jeff Brown928e0542011-01-10 11:17:36 -0800520 jobject inputWindowHandleObj =
521 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
522 if (inputWindowHandleObj) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700523 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800524 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700525 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
526
Jeff Brown928e0542011-01-10 11:17:36 -0800527 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700528 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700529}
530
Jeff Brown9c3cda02010-06-15 01:31:58 -0700531nsecs_t NativeInputManager::getKeyRepeatTimeout() {
532 if (! isScreenOn()) {
533 // Disable key repeat when the screen is off.
534 return -1;
535 } else {
Jeff Browna4547672011-03-02 21:38:11 -0800536 if (mKeyRepeatTimeout < 0) {
537 JNIEnv* env = jniEnv();
538
539 jint result = env->CallIntMethod(mCallbacksObj,
540 gCallbacksClassInfo.getKeyRepeatTimeout);
541 if (checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
542 result = 500;
543 }
544
545 mKeyRepeatTimeout = milliseconds_to_nanoseconds(result);
546 }
547 return mKeyRepeatTimeout;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700548 }
549}
550
Jeff Brownb21fb102010-09-07 10:44:57 -0700551nsecs_t NativeInputManager::getKeyRepeatDelay() {
Jeff Browna4547672011-03-02 21:38:11 -0800552 if (mKeyRepeatDelay < 0) {
553 JNIEnv* env = jniEnv();
554
555 jint result = env->CallIntMethod(mCallbacksObj,
556 gCallbacksClassInfo.getKeyRepeatDelay);
557 if (checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
558 result = 50;
559 }
560
561 mKeyRepeatDelay = milliseconds_to_nanoseconds(result);
562 }
563 return mKeyRepeatDelay;
Jeff Brownb21fb102010-09-07 10:44:57 -0700564}
565
Jeff Brownae9fc032010-08-18 15:51:08 -0700566int32_t NativeInputManager::getMaxEventsPerSecond() {
567 if (mMaxEventsPerSecond < 0) {
568 JNIEnv* env = jniEnv();
569
570 jint result = env->CallIntMethod(mCallbacksObj,
571 gCallbacksClassInfo.getMaxEventsPerSecond);
572 if (checkAndClearExceptionFromCallback(env, "getMaxEventsPerSecond")) {
Jeff Brown3d8c9bd2010-08-18 17:48:53 -0700573 result = 60;
Jeff Brownae9fc032010-08-18 15:51:08 -0700574 }
575
576 mMaxEventsPerSecond = result;
577 }
578 return mMaxEventsPerSecond;
579}
580
Jeff Brown349703e2010-06-22 01:27:15 -0700581void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowObjArray) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700582 Vector<InputWindow> windows;
Jeff Brown349703e2010-06-22 01:27:15 -0700583
Jeff Brownb88102f2010-09-08 11:49:43 -0700584 jsize length = env->GetArrayLength(windowObjArray);
585 for (jsize i = 0; i < length; i++) {
Jeff Brown928e0542011-01-10 11:17:36 -0800586 jobject windowObj = env->GetObjectArrayElement(windowObjArray, i);
587 if (! windowObj) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700588 break; // found null element indicating end of used portion of the array
Jeff Brown349703e2010-06-22 01:27:15 -0700589 }
590
Jeff Brownb88102f2010-09-08 11:49:43 -0700591 windows.push();
592 InputWindow& window = windows.editTop();
Jeff Brown928e0542011-01-10 11:17:36 -0800593 android_server_InputWindow_toNative(env, windowObj, &window);
594 if (window.inputChannel == NULL) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700595 windows.pop();
Jeff Brown349703e2010-06-22 01:27:15 -0700596 }
Jeff Brown928e0542011-01-10 11:17:36 -0800597 env->DeleteLocalRef(windowObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700598 }
Jeff Brown349703e2010-06-22 01:27:15 -0700599
Jeff Brownb88102f2010-09-08 11:49:43 -0700600 mInputManager->getDispatcher()->setInputWindows(windows);
Jeff Brown349703e2010-06-22 01:27:15 -0700601}
602
Jeff Brown349703e2010-06-22 01:27:15 -0700603void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationObj) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700604 if (applicationObj) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700605 InputApplication application;
Jeff Brown928e0542011-01-10 11:17:36 -0800606 android_server_InputApplication_toNative(env, applicationObj, &application);
607 if (application.inputApplicationHandle != NULL) {
608 mInputManager->getDispatcher()->setFocusedApplication(&application);
609 }
Jeff Brown349703e2010-06-22 01:27:15 -0700610 }
Jeff Brown928e0542011-01-10 11:17:36 -0800611 mInputManager->getDispatcher()->setFocusedApplication(NULL);
Jeff Brown349703e2010-06-22 01:27:15 -0700612}
613
614void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700615 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700616}
617
Jeff Brown05dc66a2011-03-02 14:41:58 -0800618void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
619 AutoMutex _l(mLock);
620
621 if (mLocked.systemUiVisibility != visibility) {
622 mLocked.systemUiVisibility = visibility;
623
624 sp<PointerController> controller = mLocked.pointerController.promote();
625 if (controller != NULL) {
626 updateInactivityFadeDelayLocked(controller);
627 }
628 }
629}
630
631void NativeInputManager::updateInactivityFadeDelayLocked(const sp<PointerController>& controller) {
632 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
633 controller->setInactivityFadeDelay(lightsOut
634 ? PointerController::INACTIVITY_FADE_DELAY_SHORT
635 : PointerController::INACTIVITY_FADE_DELAY_NORMAL);
636}
637
Jeff Browne20c9e02010-10-11 14:20:19 -0700638bool NativeInputManager::isScreenOn() {
639 return android_server_PowerManagerService_isScreenOn();
640}
641
642bool NativeInputManager::isScreenBright() {
643 return android_server_PowerManagerService_isScreenBright();
644}
645
Jeff Brown1f245102010-11-18 20:53:46 -0800646void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
647 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700648 // Policy:
649 // - Ignore untrusted events and pass them along.
650 // - Ask the window manager what to do with normal events and trusted injected events.
651 // - For normal events wake and brighten the screen if currently off or dim.
652 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800653 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700654 bool isScreenOn = this->isScreenOn();
655 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700656
Jeff Brown3122e442010-10-11 23:32:49 -0700657 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800658 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
659 jint wmActions;
660 if (keyEventObj) {
661 wmActions = env->CallIntMethod(mCallbacksObj,
662 gCallbacksClassInfo.interceptKeyBeforeQueueing,
663 keyEventObj, policyFlags, isScreenOn);
664 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
665 wmActions = 0;
666 }
667 android_view_KeyEvent_recycle(env, keyEventObj);
668 env->DeleteLocalRef(keyEventObj);
669 } else {
670 LOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700671 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700672 }
673
Jeff Brown1f245102010-11-18 20:53:46 -0800674 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700675 if (!isScreenOn) {
676 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700677 }
678
679 if (!isScreenBright) {
680 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
681 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700682 }
683
Jeff Brown56194eb2011-03-02 19:23:13 -0800684 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700685 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700686 policyFlags |= POLICY_FLAG_PASS_TO_USER;
687 }
688}
689
Jeff Brown56194eb2011-03-02 19:23:13 -0800690void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700691 // Policy:
692 // - Ignore untrusted events and pass them along.
693 // - No special filtering for injected events required at this time.
694 // - Filter normal events based on screen state.
695 // - For normal events brighten (but do not wake) the screen if currently dim.
696 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
697 if (isScreenOn()) {
698 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700699
Jeff Brown3122e442010-10-11 23:32:49 -0700700 if (!isScreenBright()) {
701 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
702 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800703 } else {
704 JNIEnv* env = jniEnv();
705 jint wmActions = env->CallIntMethod(mCallbacksObj,
706 gCallbacksClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
707 policyFlags);
708 if (checkAndClearExceptionFromCallback(env,
709 "interceptMotionBeforeQueueingWhenScreenOff")) {
710 wmActions = 0;
711 }
712
713 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
714 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700715 }
Jeff Brown3122e442010-10-11 23:32:49 -0700716 } else {
717 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700718 }
719}
720
Jeff Brown56194eb2011-03-02 19:23:13 -0800721void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
722 uint32_t& policyFlags) {
723 enum {
724 WM_ACTION_PASS_TO_USER = 1,
725 WM_ACTION_POKE_USER_ACTIVITY = 2,
726 WM_ACTION_GO_TO_SLEEP = 4,
727 };
728
729 if (wmActions & WM_ACTION_GO_TO_SLEEP) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800730#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown56194eb2011-03-02 19:23:13 -0800731 LOGD("handleInterceptActions: Going to sleep.");
732#endif
733 android_server_PowerManagerService_goToSleep(when);
734 }
735
736 if (wmActions & WM_ACTION_POKE_USER_ACTIVITY) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800737#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown56194eb2011-03-02 19:23:13 -0800738 LOGD("handleInterceptActions: Poking user activity.");
739#endif
740 android_server_PowerManagerService_userActivity(when, POWER_MANAGER_BUTTON_EVENT);
741 }
742
743 if (wmActions & WM_ACTION_PASS_TO_USER) {
744 policyFlags |= POLICY_FLAG_PASS_TO_USER;
745 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800746#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown56194eb2011-03-02 19:23:13 -0800747 LOGD("handleInterceptActions: Not passing key to user.");
748#endif
749 }
750}
751
Jeff Brown928e0542011-01-10 11:17:36 -0800752bool NativeInputManager::interceptKeyBeforeDispatching(
753 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700754 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700755 // Policy:
756 // - Ignore untrusted events and pass them along.
757 // - Filter normal events and trusted injected events through the window manager policy to
758 // handle the HOME key and the like.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800759 bool result = false;
Jeff Brown3122e442010-10-11 23:32:49 -0700760 if (policyFlags & POLICY_FLAG_TRUSTED) {
761 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700762
Jeff Brown928e0542011-01-10 11:17:36 -0800763 // Note: inputWindowHandle may be null.
764 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800765 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
766 if (keyEventObj) {
767 jboolean consumed = env->CallBooleanMethod(mCallbacksObj,
768 gCallbacksClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800769 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800770 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
771 android_view_KeyEvent_recycle(env, keyEventObj);
772 env->DeleteLocalRef(keyEventObj);
773 result = consumed && !error;
774 } else {
775 LOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800776 }
Jeff Brown928e0542011-01-10 11:17:36 -0800777 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700778 }
Jeff Brown1f245102010-11-18 20:53:46 -0800779 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700780}
781
Jeff Brown928e0542011-01-10 11:17:36 -0800782bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800783 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700784 // Policy:
785 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800786 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700787 if (policyFlags & POLICY_FLAG_TRUSTED) {
788 JNIEnv* env = jniEnv();
789
Jeff Brown928e0542011-01-10 11:17:36 -0800790 // Note: inputWindowHandle may be null.
791 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800792 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
793 if (keyEventObj) {
Jeff Brown49ed71d2010-12-06 17:13:33 -0800794 jobject fallbackKeyEventObj = env->CallObjectMethod(mCallbacksObj,
Jeff Brown1f245102010-11-18 20:53:46 -0800795 gCallbacksClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800796 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800797 checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey");
Jeff Brown1f245102010-11-18 20:53:46 -0800798 android_view_KeyEvent_recycle(env, keyEventObj);
799 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800800
801 if (fallbackKeyEventObj) {
802 // Note: outFallbackKeyEvent may be the same object as keyEvent.
803 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
804 outFallbackKeyEvent)) {
805 result = true;
806 }
807 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
808 env->DeleteLocalRef(fallbackKeyEventObj);
809 }
Jeff Brown1f245102010-11-18 20:53:46 -0800810 } else {
811 LOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800812 }
Jeff Brown928e0542011-01-10 11:17:36 -0800813 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700814 }
Jeff Brown1f245102010-11-18 20:53:46 -0800815 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700816}
817
Jeff Brown01ce2e92010-09-26 22:20:12 -0700818void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
819 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700820}
821
Jeff Brown349703e2010-06-22 01:27:15 -0700822
Jeff Brownb88102f2010-09-08 11:49:43 -0700823bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
824 int32_t injectorPid, int32_t injectorUid) {
825 JNIEnv* env = jniEnv();
826 jboolean result = env->CallBooleanMethod(mCallbacksObj,
827 gCallbacksClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
828 checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission");
Jeff Brown349703e2010-06-22 01:27:15 -0700829 return result;
830}
831
Jeff Brown83c09682010-12-23 17:50:18 -0800832
Jeff Brown9c3cda02010-06-15 01:31:58 -0700833// ----------------------------------------------------------------------------
834
835static sp<NativeInputManager> gNativeInputManager;
836
Jeff Brown46b9ac02010-04-22 18:58:52 -0700837static bool checkInputManagerUnitialized(JNIEnv* env) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700838 if (gNativeInputManager == NULL) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700839 LOGE("Input manager not initialized.");
840 jniThrowRuntimeException(env, "Input manager not initialized.");
841 return true;
842 }
843 return false;
844}
845
846static void android_server_InputManager_nativeInit(JNIEnv* env, jclass clazz,
Jeff Brown05dc66a2011-03-02 14:41:58 -0800847 jobject callbacks, jobject messageQueueObj) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700848 if (gNativeInputManager == NULL) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800849 sp<Looper> looper = android_os_MessageQueue_getLooper(env, messageQueueObj);
850 gNativeInputManager = new NativeInputManager(callbacks, looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700851 } else {
852 LOGE("Input manager already initialized.");
853 jniThrowRuntimeException(env, "Input manager already initialized.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700854 }
855}
856
857static void android_server_InputManager_nativeStart(JNIEnv* env, jclass clazz) {
858 if (checkInputManagerUnitialized(env)) {
859 return;
860 }
861
Jeff Brown9c3cda02010-06-15 01:31:58 -0700862 status_t result = gNativeInputManager->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700863 if (result) {
864 jniThrowRuntimeException(env, "Input manager could not be started.");
865 }
866}
867
868static void android_server_InputManager_nativeSetDisplaySize(JNIEnv* env, jclass clazz,
869 jint displayId, jint width, jint height) {
870 if (checkInputManagerUnitialized(env)) {
871 return;
872 }
873
874 // XXX we could get this from the SurfaceFlinger directly instead of requiring it
875 // to be passed in like this, not sure which is better but leaving it like this
876 // keeps the window manager in direct control of when display transitions propagate down
877 // to the input dispatcher
Jeff Brown9c3cda02010-06-15 01:31:58 -0700878 gNativeInputManager->setDisplaySize(displayId, width, height);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700879}
880
881static void android_server_InputManager_nativeSetDisplayOrientation(JNIEnv* env, jclass clazz,
882 jint displayId, jint orientation) {
883 if (checkInputManagerUnitialized(env)) {
884 return;
885 }
886
Jeff Brown9c3cda02010-06-15 01:31:58 -0700887 gNativeInputManager->setDisplayOrientation(displayId, orientation);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700888}
889
890static jint android_server_InputManager_nativeGetScanCodeState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700891 jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700892 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700893 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700894 }
895
Jeff Brownb88102f2010-09-08 11:49:43 -0700896 return gNativeInputManager->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700897 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700898}
899
900static jint android_server_InputManager_nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700901 jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700902 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700903 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700904 }
905
Jeff Brownb88102f2010-09-08 11:49:43 -0700906 return gNativeInputManager->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700907 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700908}
909
910static jint android_server_InputManager_nativeGetSwitchState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700911 jint deviceId, jint sourceMask, jint sw) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700912 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700913 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700914 }
915
Jeff Brownb88102f2010-09-08 11:49:43 -0700916 return gNativeInputManager->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700917 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700918}
919
920static jboolean android_server_InputManager_nativeHasKeys(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700921 jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700922 if (checkInputManagerUnitialized(env)) {
923 return JNI_FALSE;
924 }
925
926 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
927 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
928 jsize numCodes = env->GetArrayLength(keyCodes);
929 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700930 if (numCodes == env->GetArrayLength(keyCodes)) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700931 result = gNativeInputManager->getInputManager()->getReader()->hasKeys(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700932 deviceId, uint32_t(sourceMask), numCodes, codes, flags);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700933 } else {
934 result = JNI_FALSE;
935 }
936
937 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
938 env->ReleaseIntArrayElements(keyCodes, codes, 0);
939 return result;
940}
941
942static void throwInputChannelNotInitialized(JNIEnv* env) {
943 jniThrowException(env, "java/lang/IllegalStateException",
944 "inputChannel is not initialized");
945}
946
947static void android_server_InputManager_handleInputChannelDisposed(JNIEnv* env,
948 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
949 LOGW("Input channel object '%s' was disposed without first being unregistered with "
950 "the input manager!", inputChannel->getName().string());
951
Jeff Brown9c3cda02010-06-15 01:31:58 -0700952 if (gNativeInputManager != NULL) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700953 gNativeInputManager->unregisterInputChannel(env, inputChannel);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700954 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700955}
956
957static void android_server_InputManager_nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
Jeff Brown928e0542011-01-10 11:17:36 -0800958 jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700959 if (checkInputManagerUnitialized(env)) {
960 return;
961 }
962
963 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
964 inputChannelObj);
965 if (inputChannel == NULL) {
966 throwInputChannelNotInitialized(env);
967 return;
968 }
969
Jeff Brown928e0542011-01-10 11:17:36 -0800970 sp<InputWindowHandle> inputWindowHandle =
971 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700972
973 status_t status = gNativeInputManager->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -0800974 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700975 if (status) {
976 jniThrowRuntimeException(env, "Failed to register input channel. "
977 "Check logs for details.");
978 return;
979 }
980
Jeff Browna41ca772010-08-11 14:46:32 -0700981 if (! monitor) {
982 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
983 android_server_InputManager_handleInputChannelDisposed, NULL);
984 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700985}
986
987static void android_server_InputManager_nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
988 jobject inputChannelObj) {
989 if (checkInputManagerUnitialized(env)) {
990 return;
991 }
992
993 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
994 inputChannelObj);
995 if (inputChannel == NULL) {
996 throwInputChannelNotInitialized(env);
997 return;
998 }
999
1000 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1001
Jeff Brown7fbdc842010-06-17 20:52:56 -07001002 status_t status = gNativeInputManager->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001003 if (status) {
1004 jniThrowRuntimeException(env, "Failed to unregister input channel. "
1005 "Check logs for details.");
1006 }
1007}
1008
Jeff Brown6ec402b2010-07-28 15:48:59 -07001009static jint android_server_InputManager_nativeInjectInputEvent(JNIEnv* env, jclass clazz,
1010 jobject inputEventObj, jint injectorPid, jint injectorUid,
1011 jint syncMode, jint timeoutMillis) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07001012 if (checkInputManagerUnitialized(env)) {
1013 return INPUT_EVENT_INJECTION_FAILED;
1014 }
1015
Jeff Brown6ec402b2010-07-28 15:48:59 -07001016 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1017 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001018 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1019 if (status) {
1020 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1021 return INPUT_EVENT_INJECTION_FAILED;
1022 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001023
Jeff Brownb88102f2010-09-08 11:49:43 -07001024 return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent(
1025 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001026 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
1027 MotionEvent motionEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001028 status_t status = android_view_MotionEvent_toNative(env, inputEventObj, & motionEvent);
1029 if (status) {
1030 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1031 return INPUT_EVENT_INJECTION_FAILED;
1032 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001033
Jeff Brownb88102f2010-09-08 11:49:43 -07001034 return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent(
1035 & motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001036 } else {
1037 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001038 return INPUT_EVENT_INJECTION_FAILED;
1039 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001040}
1041
Jeff Brown349703e2010-06-22 01:27:15 -07001042static void android_server_InputManager_nativeSetInputWindows(JNIEnv* env, jclass clazz,
1043 jobjectArray windowObjArray) {
1044 if (checkInputManagerUnitialized(env)) {
1045 return;
1046 }
1047
1048 gNativeInputManager->setInputWindows(env, windowObjArray);
1049}
1050
1051static void android_server_InputManager_nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
1052 jobject applicationObj) {
1053 if (checkInputManagerUnitialized(env)) {
1054 return;
1055 }
1056
1057 gNativeInputManager->setFocusedApplication(env, applicationObj);
1058}
1059
1060static void android_server_InputManager_nativeSetInputDispatchMode(JNIEnv* env,
1061 jclass clazz, jboolean enabled, jboolean frozen) {
1062 if (checkInputManagerUnitialized(env)) {
1063 return;
1064 }
1065
1066 gNativeInputManager->setInputDispatchMode(enabled, frozen);
1067}
1068
Jeff Brown05dc66a2011-03-02 14:41:58 -08001069static void android_server_InputManager_nativeSetSystemUiVisibility(JNIEnv* env,
1070 jclass clazz, jint visibility) {
1071 if (checkInputManagerUnitialized(env)) {
1072 return;
1073 }
1074
1075 gNativeInputManager->setSystemUiVisibility(visibility);
1076}
1077
Jeff Brown8d608662010-08-30 03:02:23 -07001078static jobject android_server_InputManager_nativeGetInputDevice(JNIEnv* env,
1079 jclass clazz, jint deviceId) {
1080 if (checkInputManagerUnitialized(env)) {
1081 return NULL;
1082 }
1083
1084 InputDeviceInfo deviceInfo;
Jeff Brownb88102f2010-09-08 11:49:43 -07001085 status_t status = gNativeInputManager->getInputManager()->getReader()->getInputDeviceInfo(
Jeff Brown8d608662010-08-30 03:02:23 -07001086 deviceId, & deviceInfo);
1087 if (status) {
1088 return NULL;
1089 }
1090
1091 jobject deviceObj = env->NewObject(gInputDeviceClassInfo.clazz, gInputDeviceClassInfo.ctor);
1092 if (! deviceObj) {
1093 return NULL;
1094 }
1095
1096 jstring deviceNameObj = env->NewStringUTF(deviceInfo.getName().string());
1097 if (! deviceNameObj) {
1098 return NULL;
1099 }
1100
1101 env->SetIntField(deviceObj, gInputDeviceClassInfo.mId, deviceInfo.getId());
1102 env->SetObjectField(deviceObj, gInputDeviceClassInfo.mName, deviceNameObj);
1103 env->SetIntField(deviceObj, gInputDeviceClassInfo.mSources, deviceInfo.getSources());
1104 env->SetIntField(deviceObj, gInputDeviceClassInfo.mKeyboardType, deviceInfo.getKeyboardType());
1105
1106 const KeyedVector<int, InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
1107 for (size_t i = 0; i < ranges.size(); i++) {
1108 int rangeType = ranges.keyAt(i);
1109 const InputDeviceInfo::MotionRange& range = ranges.valueAt(i);
1110 env->CallVoidMethod(deviceObj, gInputDeviceClassInfo.addMotionRange,
1111 rangeType, range.min, range.max, range.flat, range.fuzz);
1112 if (env->ExceptionCheck()) {
1113 return NULL;
1114 }
1115 }
1116
1117 return deviceObj;
1118}
1119
1120static jintArray android_server_InputManager_nativeGetInputDeviceIds(JNIEnv* env,
1121 jclass clazz) {
1122 if (checkInputManagerUnitialized(env)) {
1123 return NULL;
1124 }
1125
1126 Vector<int> deviceIds;
Jeff Brownb88102f2010-09-08 11:49:43 -07001127 gNativeInputManager->getInputManager()->getReader()->getInputDeviceIds(deviceIds);
Jeff Brown8d608662010-08-30 03:02:23 -07001128
1129 jintArray deviceIdsObj = env->NewIntArray(deviceIds.size());
1130 if (! deviceIdsObj) {
1131 return NULL;
1132 }
1133
1134 env->SetIntArrayRegion(deviceIdsObj, 0, deviceIds.size(), deviceIds.array());
1135 return deviceIdsObj;
1136}
1137
Jeff Brown57c59372010-09-21 18:22:55 -07001138static void android_server_InputManager_nativeGetInputConfiguration(JNIEnv* env,
1139 jclass clazz, jobject configObj) {
1140 if (checkInputManagerUnitialized(env)) {
1141 return;
1142 }
1143
1144 InputConfiguration config;
1145 gNativeInputManager->getInputManager()->getReader()->getInputConfiguration(& config);
1146
1147 env->SetIntField(configObj, gConfigurationClassInfo.touchscreen, config.touchScreen);
1148 env->SetIntField(configObj, gConfigurationClassInfo.keyboard, config.keyboard);
1149 env->SetIntField(configObj, gConfigurationClassInfo.navigation, config.navigation);
1150}
1151
Jeff Browne6504122010-09-27 14:52:15 -07001152static jboolean android_server_InputManager_nativeTransferTouchFocus(JNIEnv* env,
1153 jclass clazz, jobject fromChannelObj, jobject toChannelObj) {
1154 if (checkInputManagerUnitialized(env)) {
1155 return false;
1156 }
1157
1158 sp<InputChannel> fromChannel =
1159 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1160 sp<InputChannel> toChannel =
1161 android_view_InputChannel_getInputChannel(env, toChannelObj);
1162
1163 if (fromChannel == NULL || toChannel == NULL) {
1164 return false;
1165 }
1166
1167 return gNativeInputManager->getInputManager()->getDispatcher()->
1168 transferTouchFocus(fromChannel, toChannel);
1169}
1170
Jeff Browne33348b2010-07-15 23:54:05 -07001171static jstring android_server_InputManager_nativeDump(JNIEnv* env, jclass clazz) {
1172 if (checkInputManagerUnitialized(env)) {
1173 return NULL;
1174 }
1175
Jeff Brownb88102f2010-09-08 11:49:43 -07001176 String8 dump;
1177 gNativeInputManager->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001178 return env->NewStringUTF(dump.string());
1179}
1180
Jeff Brown9c3cda02010-06-15 01:31:58 -07001181// ----------------------------------------------------------------------------
1182
Jeff Brown46b9ac02010-04-22 18:58:52 -07001183static JNINativeMethod gInputManagerMethods[] = {
1184 /* name, signature, funcPtr */
Jeff Brown05dc66a2011-03-02 14:41:58 -08001185 { "nativeInit", "(Lcom/android/server/wm/InputManager$Callbacks;Landroid/os/MessageQueue;)V",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001186 (void*) android_server_InputManager_nativeInit },
1187 { "nativeStart", "()V",
1188 (void*) android_server_InputManager_nativeStart },
1189 { "nativeSetDisplaySize", "(III)V",
1190 (void*) android_server_InputManager_nativeSetDisplaySize },
1191 { "nativeSetDisplayOrientation", "(II)V",
1192 (void*) android_server_InputManager_nativeSetDisplayOrientation },
1193 { "nativeGetScanCodeState", "(III)I",
1194 (void*) android_server_InputManager_nativeGetScanCodeState },
1195 { "nativeGetKeyCodeState", "(III)I",
1196 (void*) android_server_InputManager_nativeGetKeyCodeState },
1197 { "nativeGetSwitchState", "(III)I",
1198 (void*) android_server_InputManager_nativeGetSwitchState },
Jeff Brown6d0fec22010-07-23 21:28:06 -07001199 { "nativeHasKeys", "(II[I[Z)Z",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001200 (void*) android_server_InputManager_nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001201 { "nativeRegisterInputChannel",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001202 "(Landroid/view/InputChannel;Lcom/android/server/wm/InputWindowHandle;Z)V",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001203 (void*) android_server_InputManager_nativeRegisterInputChannel },
1204 { "nativeUnregisterInputChannel", "(Landroid/view/InputChannel;)V",
Jeff Brown7fbdc842010-06-17 20:52:56 -07001205 (void*) android_server_InputManager_nativeUnregisterInputChannel },
Jeff Brown6ec402b2010-07-28 15:48:59 -07001206 { "nativeInjectInputEvent", "(Landroid/view/InputEvent;IIII)I",
1207 (void*) android_server_InputManager_nativeInjectInputEvent },
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001208 { "nativeSetInputWindows", "([Lcom/android/server/wm/InputWindow;)V",
Jeff Brown349703e2010-06-22 01:27:15 -07001209 (void*) android_server_InputManager_nativeSetInputWindows },
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001210 { "nativeSetFocusedApplication", "(Lcom/android/server/wm/InputApplication;)V",
Jeff Brown349703e2010-06-22 01:27:15 -07001211 (void*) android_server_InputManager_nativeSetFocusedApplication },
1212 { "nativeSetInputDispatchMode", "(ZZ)V",
1213 (void*) android_server_InputManager_nativeSetInputDispatchMode },
Jeff Brown05dc66a2011-03-02 14:41:58 -08001214 { "nativeSetSystemUiVisibility", "(I)V",
1215 (void*) android_server_InputManager_nativeSetSystemUiVisibility },
Jeff Brown8d608662010-08-30 03:02:23 -07001216 { "nativeGetInputDevice", "(I)Landroid/view/InputDevice;",
1217 (void*) android_server_InputManager_nativeGetInputDevice },
1218 { "nativeGetInputDeviceIds", "()[I",
1219 (void*) android_server_InputManager_nativeGetInputDeviceIds },
Jeff Brown57c59372010-09-21 18:22:55 -07001220 { "nativeGetInputConfiguration", "(Landroid/content/res/Configuration;)V",
1221 (void*) android_server_InputManager_nativeGetInputConfiguration },
Jeff Browne6504122010-09-27 14:52:15 -07001222 { "nativeTransferTouchFocus", "(Landroid/view/InputChannel;Landroid/view/InputChannel;)Z",
1223 (void*) android_server_InputManager_nativeTransferTouchFocus },
Jeff Browne33348b2010-07-15 23:54:05 -07001224 { "nativeDump", "()Ljava/lang/String;",
1225 (void*) android_server_InputManager_nativeDump },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001226};
1227
1228#define FIND_CLASS(var, className) \
1229 var = env->FindClass(className); \
1230 LOG_FATAL_IF(! var, "Unable to find class " className); \
1231 var = jclass(env->NewGlobalRef(var));
1232
1233#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1234 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1235 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1236
1237#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1238 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1239 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1240
1241int register_android_server_InputManager(JNIEnv* env) {
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001242 int res = jniRegisterNativeMethods(env, "com/android/server/wm/InputManager",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001243 gInputManagerMethods, NELEM(gInputManagerMethods));
1244 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1245
Jeff Brown9c3cda02010-06-15 01:31:58 -07001246 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001247
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001248 FIND_CLASS(gCallbacksClassInfo.clazz, "com/android/server/wm/InputManager$Callbacks");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001249
Jeff Brown46b9ac02010-04-22 18:58:52 -07001250 GET_METHOD_ID(gCallbacksClassInfo.notifyConfigurationChanged, gCallbacksClassInfo.clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001251 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001252
1253 GET_METHOD_ID(gCallbacksClassInfo.notifyLidSwitchChanged, gCallbacksClassInfo.clazz,
1254 "notifyLidSwitchChanged", "(JZ)V");
1255
Jeff Brown7fbdc842010-06-17 20:52:56 -07001256 GET_METHOD_ID(gCallbacksClassInfo.notifyInputChannelBroken, gCallbacksClassInfo.clazz,
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001257 "notifyInputChannelBroken", "(Lcom/android/server/wm/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001258
Jeff Brown349703e2010-06-22 01:27:15 -07001259 GET_METHOD_ID(gCallbacksClassInfo.notifyANR, gCallbacksClassInfo.clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001260 "notifyANR",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001261 "(Lcom/android/server/wm/InputApplicationHandle;Lcom/android/server/wm/InputWindowHandle;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001262
Jeff Brown349703e2010-06-22 01:27:15 -07001263 GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeQueueing, gCallbacksClassInfo.clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001264 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001265
Jeff Brown56194eb2011-03-02 19:23:13 -08001266 GET_METHOD_ID(gCallbacksClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
1267 gCallbacksClassInfo.clazz,
1268 "interceptMotionBeforeQueueingWhenScreenOff", "(I)I");
1269
Jeff Brown349703e2010-06-22 01:27:15 -07001270 GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeDispatching, gCallbacksClassInfo.clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001271 "interceptKeyBeforeDispatching",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001272 "(Lcom/android/server/wm/InputWindowHandle;Landroid/view/KeyEvent;I)Z");
Jeff Brown349703e2010-06-22 01:27:15 -07001273
Jeff Brown3915bb82010-11-05 15:02:16 -07001274 GET_METHOD_ID(gCallbacksClassInfo.dispatchUnhandledKey, gCallbacksClassInfo.clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001275 "dispatchUnhandledKey",
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001276 "(Lcom/android/server/wm/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001277
Jeff Brown349703e2010-06-22 01:27:15 -07001278 GET_METHOD_ID(gCallbacksClassInfo.checkInjectEventsPermission, gCallbacksClassInfo.clazz,
1279 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001280
Jeff Brown46b9ac02010-04-22 18:58:52 -07001281 GET_METHOD_ID(gCallbacksClassInfo.filterTouchEvents, gCallbacksClassInfo.clazz,
1282 "filterTouchEvents", "()Z");
1283
1284 GET_METHOD_ID(gCallbacksClassInfo.filterJumpyTouchEvents, gCallbacksClassInfo.clazz,
1285 "filterJumpyTouchEvents", "()Z");
1286
Jeff Brownfe508922011-01-18 15:10:10 -08001287 GET_METHOD_ID(gCallbacksClassInfo.getVirtualKeyQuietTimeMillis, gCallbacksClassInfo.clazz,
1288 "getVirtualKeyQuietTimeMillis", "()I");
1289
Jeff Brown46b9ac02010-04-22 18:58:52 -07001290 GET_METHOD_ID(gCallbacksClassInfo.getExcludedDeviceNames, gCallbacksClassInfo.clazz,
1291 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1292
Jeff Browna4547672011-03-02 21:38:11 -08001293 GET_METHOD_ID(gCallbacksClassInfo.getKeyRepeatTimeout, gCallbacksClassInfo.clazz,
1294 "getKeyRepeatTimeout", "()I");
1295
1296 GET_METHOD_ID(gCallbacksClassInfo.getKeyRepeatDelay, gCallbacksClassInfo.clazz,
1297 "getKeyRepeatDelay", "()I");
1298
Jeff Brownae9fc032010-08-18 15:51:08 -07001299 GET_METHOD_ID(gCallbacksClassInfo.getMaxEventsPerSecond, gCallbacksClassInfo.clazz,
1300 "getMaxEventsPerSecond", "()I");
1301
Jeff Brown83c09682010-12-23 17:50:18 -08001302 GET_METHOD_ID(gCallbacksClassInfo.getPointerLayer, gCallbacksClassInfo.clazz,
1303 "getPointerLayer", "()I");
1304
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001305 GET_METHOD_ID(gCallbacksClassInfo.getPointerIcon, gCallbacksClassInfo.clazz,
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001306 "getPointerIcon", "()Lcom/android/server/wm/InputManager$PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001307
Jeff Brown6ec402b2010-07-28 15:48:59 -07001308 // KeyEvent
1309
1310 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
1311
Jeff Brown8d608662010-08-30 03:02:23 -07001312 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001313
1314 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
1315
Jeff Brown8d608662010-08-30 03:02:23 -07001316 // InputDevice
1317
1318 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1319
1320 GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz,
1321 "<init>", "()V");
1322
1323 GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz,
1324 "addMotionRange", "(IFFFF)V");
1325
1326 GET_FIELD_ID(gInputDeviceClassInfo.mId, gInputDeviceClassInfo.clazz,
1327 "mId", "I");
1328
1329 GET_FIELD_ID(gInputDeviceClassInfo.mName, gInputDeviceClassInfo.clazz,
1330 "mName", "Ljava/lang/String;");
1331
1332 GET_FIELD_ID(gInputDeviceClassInfo.mSources, gInputDeviceClassInfo.clazz,
1333 "mSources", "I");
1334
1335 GET_FIELD_ID(gInputDeviceClassInfo.mKeyboardType, gInputDeviceClassInfo.clazz,
1336 "mKeyboardType", "I");
1337
Jeff Brown57c59372010-09-21 18:22:55 -07001338 // Configuration
1339
1340 FIND_CLASS(gConfigurationClassInfo.clazz, "android/content/res/Configuration");
1341
1342 GET_FIELD_ID(gConfigurationClassInfo.touchscreen, gConfigurationClassInfo.clazz,
1343 "touchscreen", "I");
1344
1345 GET_FIELD_ID(gConfigurationClassInfo.keyboard, gConfigurationClassInfo.clazz,
1346 "keyboard", "I");
1347
1348 GET_FIELD_ID(gConfigurationClassInfo.navigation, gConfigurationClassInfo.clazz,
1349 "navigation", "I");
1350
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001351 // PointerIcon
1352
Dianne Hackborna924dc0d2011-02-17 14:22:17 -08001353 FIND_CLASS(gPointerIconClassInfo.clazz, "com/android/server/wm/InputManager$PointerIcon");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001354
1355 GET_FIELD_ID(gPointerIconClassInfo.bitmap, gPointerIconClassInfo.clazz,
1356 "bitmap", "Landroid/graphics/Bitmap;");
1357
1358 GET_FIELD_ID(gPointerIconClassInfo.hotSpotX, gPointerIconClassInfo.clazz,
1359 "hotSpotX", "F");
1360
1361 GET_FIELD_ID(gPointerIconClassInfo.hotSpotY, gPointerIconClassInfo.clazz,
1362 "hotSpotY", "F");
1363
Jeff Brown46b9ac02010-04-22 18:58:52 -07001364 return 0;
1365}
1366
Jeff Brown46b9ac02010-04-22 18:58:52 -07001367} /* namespace android */