blob: 29815f6636023d7b98aff9c5bb581f3231ef31ad [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 Brown46b9ac02010-04-22 18:58:52 -070027#include "JNIHelp.h"
28#include "jni.h"
Jeff Brown349703e2010-06-22 01:27:15 -070029#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070030#include <android_runtime/AndroidRuntime.h>
Jeff Brown9c3cda02010-06-15 01:31:58 -070031#include <ui/InputReader.h>
32#include <ui/InputDispatcher.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070033#include <ui/InputManager.h>
34#include <ui/InputTransport.h>
35#include <utils/Log.h>
36#include <utils/threads.h>
37#include "../../core/jni/android_view_KeyEvent.h"
38#include "../../core/jni/android_view_MotionEvent.h"
39#include "../../core/jni/android_view_InputChannel.h"
Jeff Brown00fa7bd2010-07-02 15:37:36 -070040#include "com_android_server_PowerManagerService.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070041
42namespace android {
43
Jeff Brown9c3cda02010-06-15 01:31:58 -070044// ----------------------------------------------------------------------------
Jeff Brown46b9ac02010-04-22 18:58:52 -070045
46static struct {
47 jclass clazz;
48
Jeff Brown46b9ac02010-04-22 18:58:52 -070049 jmethodID notifyConfigurationChanged;
50 jmethodID notifyLidSwitchChanged;
Jeff Brown7fbdc842010-06-17 20:52:56 -070051 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070052 jmethodID notifyANR;
Jeff Brown349703e2010-06-22 01:27:15 -070053 jmethodID interceptKeyBeforeQueueing;
54 jmethodID interceptKeyBeforeDispatching;
55 jmethodID checkInjectEventsPermission;
Jeff Brown46b9ac02010-04-22 18:58:52 -070056 jmethodID filterTouchEvents;
57 jmethodID filterJumpyTouchEvents;
58 jmethodID getVirtualKeyDefinitions;
Jeff Brown8d608662010-08-30 03:02:23 -070059 jmethodID getInputDeviceCalibration;
Jeff Brown46b9ac02010-04-22 18:58:52 -070060 jmethodID getExcludedDeviceNames;
Jeff Brownae9fc032010-08-18 15:51:08 -070061 jmethodID getMaxEventsPerSecond;
Jeff Brown46b9ac02010-04-22 18:58:52 -070062} gCallbacksClassInfo;
63
64static struct {
65 jclass clazz;
66
67 jfieldID scanCode;
68 jfieldID centerX;
69 jfieldID centerY;
70 jfieldID width;
71 jfieldID height;
72} gVirtualKeyDefinitionClassInfo;
73
Jeff Brown7fbdc842010-06-17 20:52:56 -070074static struct {
75 jclass clazz;
76
Jeff Brown8d608662010-08-30 03:02:23 -070077 jfieldID keys;
78 jfieldID values;
79} gInputDeviceCalibrationClassInfo;
80
81static struct {
82 jclass clazz;
83
Jeff Brown349703e2010-06-22 01:27:15 -070084 jfieldID inputChannel;
Jeff Brown519e0242010-09-15 15:18:56 -070085 jfieldID name;
Jeff Brown349703e2010-06-22 01:27:15 -070086 jfieldID layoutParamsFlags;
87 jfieldID layoutParamsType;
88 jfieldID dispatchingTimeoutNanos;
89 jfieldID frameLeft;
90 jfieldID frameTop;
Jeff Brown85a31762010-09-01 17:01:00 -070091 jfieldID frameRight;
92 jfieldID frameBottom;
93 jfieldID visibleFrameLeft;
94 jfieldID visibleFrameTop;
95 jfieldID visibleFrameRight;
96 jfieldID visibleFrameBottom;
Jeff Brown349703e2010-06-22 01:27:15 -070097 jfieldID touchableAreaLeft;
98 jfieldID touchableAreaTop;
99 jfieldID touchableAreaRight;
100 jfieldID touchableAreaBottom;
101 jfieldID visible;
Jeff Brown519e0242010-09-15 15:18:56 -0700102 jfieldID canReceiveKeys;
Jeff Brown349703e2010-06-22 01:27:15 -0700103 jfieldID hasFocus;
104 jfieldID hasWallpaper;
105 jfieldID paused;
Jeff Brown519e0242010-09-15 15:18:56 -0700106 jfieldID layer;
Jeff Brown349703e2010-06-22 01:27:15 -0700107 jfieldID ownerPid;
108 jfieldID ownerUid;
109} gInputWindowClassInfo;
110
111static struct {
112 jclass clazz;
113
114 jfieldID name;
115 jfieldID dispatchingTimeoutNanos;
116 jfieldID token;
117} gInputApplicationClassInfo;
118
Jeff Brown6ec402b2010-07-28 15:48:59 -0700119static struct {
120 jclass clazz;
121} gKeyEventClassInfo;
122
123static struct {
124 jclass clazz;
125} gMotionEventClassInfo;
126
Jeff Brown8d608662010-08-30 03:02:23 -0700127static struct {
128 jclass clazz;
129
130 jmethodID ctor;
131 jmethodID addMotionRange;
132
133 jfieldID mId;
134 jfieldID mName;
135 jfieldID mSources;
136 jfieldID mKeyboardType;
137 jfieldID mMotionRanges;
138} gInputDeviceClassInfo;
139
Jeff Brown57c59372010-09-21 18:22:55 -0700140static struct {
141 jclass clazz;
142
143 jfieldID touchscreen;
144 jfieldID keyboard;
145 jfieldID navigation;
146} gConfigurationClassInfo;
147
Jeff Brown349703e2010-06-22 01:27:15 -0700148// ----------------------------------------------------------------------------
149
150static inline nsecs_t now() {
151 return systemTime(SYSTEM_TIME_MONOTONIC);
152}
Jeff Brown7fbdc842010-06-17 20:52:56 -0700153
Jeff Brown9c3cda02010-06-15 01:31:58 -0700154// ----------------------------------------------------------------------------
155
156class NativeInputManager : public virtual RefBase,
157 public virtual InputReaderPolicyInterface,
158 public virtual InputDispatcherPolicyInterface {
159protected:
160 virtual ~NativeInputManager();
161
162public:
163 NativeInputManager(jobject callbacksObj);
164
165 inline sp<InputManager> getInputManager() const { return mInputManager; }
166
Jeff Brownb88102f2010-09-08 11:49:43 -0700167 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700168
Jeff Brown9c3cda02010-06-15 01:31:58 -0700169 void setDisplaySize(int32_t displayId, int32_t width, int32_t height);
170 void setDisplayOrientation(int32_t displayId, int32_t orientation);
171
Jeff Brown7fbdc842010-06-17 20:52:56 -0700172 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Browna41ca772010-08-11 14:46:32 -0700173 jweak inputChannelObjWeak, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700174 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
175
Jeff Brown349703e2010-06-22 01:27:15 -0700176 void setInputWindows(JNIEnv* env, jobjectArray windowObjArray);
177 void setFocusedApplication(JNIEnv* env, jobject applicationObj);
178 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700179
Jeff Brown9c3cda02010-06-15 01:31:58 -0700180 /* --- InputReaderPolicyInterface implementation --- */
181
182 virtual bool getDisplayInfo(int32_t displayId,
183 int32_t* width, int32_t* height, int32_t* orientation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700184 virtual bool filterTouchEvents();
185 virtual bool filterJumpyTouchEvents();
186 virtual void getVirtualKeyDefinitions(const String8& deviceName,
Jeff Brown8d608662010-08-30 03:02:23 -0700187 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions);
188 virtual void getInputDeviceCalibration(const String8& deviceName,
189 InputDeviceCalibration& outCalibration);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700190 virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames);
191
192 /* --- InputDispatcherPolicyInterface implementation --- */
193
Jeff Browne20c9e02010-10-11 14:20:19 -0700194 virtual void notifySwitch(nsecs_t when, int32_t switchCode, int32_t switchValue,
195 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700196 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700197 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
198 const sp<InputChannel>& inputChannel);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700199 virtual void notifyInputChannelBroken(const sp<InputChannel>& inputChannel);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700200 virtual nsecs_t getKeyRepeatTimeout();
Jeff Brownb21fb102010-09-07 10:44:57 -0700201 virtual nsecs_t getKeyRepeatDelay();
Jeff Brownae9fc032010-08-18 15:51:08 -0700202 virtual int32_t getMaxEventsPerSecond();
Jeff Brownb6997262010-10-08 22:31:17 -0700203 virtual void interceptKeyBeforeQueueing(nsecs_t when, int32_t deviceId,
204 int32_t action, int32_t& flags, int32_t keyCode, int32_t scanCode,
205 uint32_t& policyFlags);
206 virtual void interceptGenericBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brownb88102f2010-09-08 11:49:43 -0700207 virtual bool interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
208 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700209 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700210 virtual bool checkInjectEventsPermissionNonReentrant(
211 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700212
213private:
Jeff Brownb88102f2010-09-08 11:49:43 -0700214 class ApplicationToken : public InputApplicationHandle {
215 jweak mTokenObjWeak;
Jeff Brown349703e2010-06-22 01:27:15 -0700216
217 public:
Jeff Brownb88102f2010-09-08 11:49:43 -0700218 ApplicationToken(jweak tokenObjWeak) :
219 mTokenObjWeak(tokenObjWeak) { }
Jeff Brown349703e2010-06-22 01:27:15 -0700220
Jeff Brownb88102f2010-09-08 11:49:43 -0700221 virtual ~ApplicationToken() {
222 JNIEnv* env = NativeInputManager::jniEnv();
223 env->DeleteWeakGlobalRef(mTokenObjWeak);
224 }
Jeff Brown349703e2010-06-22 01:27:15 -0700225
Jeff Brownb88102f2010-09-08 11:49:43 -0700226 inline jweak getTokenObj() { return mTokenObjWeak; }
Jeff Brown349703e2010-06-22 01:27:15 -0700227 };
228
Jeff Brown9c3cda02010-06-15 01:31:58 -0700229 sp<InputManager> mInputManager;
230
231 jobject mCallbacksObj;
232
233 // Cached filtering policies.
234 int32_t mFilterTouchEvents;
235 int32_t mFilterJumpyTouchEvents;
236
Jeff Brownae9fc032010-08-18 15:51:08 -0700237 // Cached throttling policy.
238 int32_t mMaxEventsPerSecond;
239
Jeff Brown9c3cda02010-06-15 01:31:58 -0700240 // Cached display state. (lock mDisplayLock)
241 Mutex mDisplayLock;
242 int32_t mDisplayWidth, mDisplayHeight;
243 int32_t mDisplayOrientation;
244
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700245 // Power manager interactions.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700246 bool isScreenOn();
247 bool isScreenBright();
248
Jeff Brown2cbecea2010-08-17 15:59:26 -0700249 // Weak references to all currently registered input channels by connection pointer.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700250 Mutex mInputChannelRegistryLock;
Jeff Brown2cbecea2010-08-17 15:59:26 -0700251 KeyedVector<InputChannel*, jweak> mInputChannelObjWeakTable;
Jeff Brown7fbdc842010-06-17 20:52:56 -0700252
253 jobject getInputChannelObjLocal(JNIEnv* env, const sp<InputChannel>& inputChannel);
254
Jeff Brown349703e2010-06-22 01:27:15 -0700255 static bool populateWindow(JNIEnv* env, jobject windowObj, InputWindow& outWindow);
Jeff Brown349703e2010-06-22 01:27:15 -0700256
Jeff Brownb88102f2010-09-08 11:49:43 -0700257 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700258
Jeff Brown9c3cda02010-06-15 01:31:58 -0700259 static inline JNIEnv* jniEnv() {
260 return AndroidRuntime::getJNIEnv();
261 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700262};
263
264// ----------------------------------------------------------------------------
265
266NativeInputManager::NativeInputManager(jobject callbacksObj) :
267 mFilterTouchEvents(-1), mFilterJumpyTouchEvents(-1),
Jeff Brownae9fc032010-08-18 15:51:08 -0700268 mMaxEventsPerSecond(-1),
Jeff Brownb88102f2010-09-08 11:49:43 -0700269 mDisplayWidth(-1), mDisplayHeight(-1), mDisplayOrientation(ROTATION_0) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270 JNIEnv* env = jniEnv();
271
272 mCallbacksObj = env->NewGlobalRef(callbacksObj);
273
274 sp<EventHub> eventHub = new EventHub();
275 mInputManager = new InputManager(eventHub, this, this);
276}
277
278NativeInputManager::~NativeInputManager() {
279 JNIEnv* env = jniEnv();
280
281 env->DeleteGlobalRef(mCallbacksObj);
282}
283
Jeff Brownb88102f2010-09-08 11:49:43 -0700284void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700285 mInputManager->getReader()->dump(dump);
286 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700287
Jeff Brownb88102f2010-09-08 11:49:43 -0700288 mInputManager->getDispatcher()->dump(dump);
289 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700290}
291
Jeff Brown7fbdc842010-06-17 20:52:56 -0700292bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700293 if (env->ExceptionCheck()) {
294 LOGE("An exception was thrown by callback '%s'.", methodName);
295 LOGE_EX(env);
296 env->ExceptionClear();
297 return true;
298 }
299 return false;
300}
301
302void NativeInputManager::setDisplaySize(int32_t displayId, int32_t width, int32_t height) {
303 if (displayId == 0) {
304 AutoMutex _l(mDisplayLock);
305
306 mDisplayWidth = width;
307 mDisplayHeight = height;
308 }
309}
310
311void NativeInputManager::setDisplayOrientation(int32_t displayId, int32_t orientation) {
312 if (displayId == 0) {
313 AutoMutex _l(mDisplayLock);
314
315 mDisplayOrientation = orientation;
316 }
317}
318
Jeff Brown7fbdc842010-06-17 20:52:56 -0700319status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Browna41ca772010-08-11 14:46:32 -0700320 const sp<InputChannel>& inputChannel, jobject inputChannelObj, bool monitor) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700321 jweak inputChannelObjWeak = env->NewWeakGlobalRef(inputChannelObj);
322 if (! inputChannelObjWeak) {
323 LOGE("Could not create weak reference for input channel.");
324 LOGE_EX(env);
325 return NO_MEMORY;
326 }
327
328 status_t status;
329 {
330 AutoMutex _l(mInputChannelRegistryLock);
331
Jeff Brown2cbecea2010-08-17 15:59:26 -0700332 ssize_t index = mInputChannelObjWeakTable.indexOfKey(inputChannel.get());
Jeff Brown7fbdc842010-06-17 20:52:56 -0700333 if (index >= 0) {
334 LOGE("Input channel object '%s' has already been registered",
335 inputChannel->getName().string());
336 status = INVALID_OPERATION;
337 goto DeleteWeakRef;
338 }
339
Jeff Brown2cbecea2010-08-17 15:59:26 -0700340 mInputChannelObjWeakTable.add(inputChannel.get(), inputChannelObjWeak);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700341 }
342
Jeff Brownb88102f2010-09-08 11:49:43 -0700343 status = mInputManager->getDispatcher()->registerInputChannel(inputChannel, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700344 if (! status) {
Jeff Browna41ca772010-08-11 14:46:32 -0700345 // Success.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700346 return OK;
347 }
348
Jeff Browna41ca772010-08-11 14:46:32 -0700349 // Failed!
Jeff Brown7fbdc842010-06-17 20:52:56 -0700350 {
351 AutoMutex _l(mInputChannelRegistryLock);
Jeff Brown2cbecea2010-08-17 15:59:26 -0700352 mInputChannelObjWeakTable.removeItem(inputChannel.get());
Jeff Brown7fbdc842010-06-17 20:52:56 -0700353 }
354
355DeleteWeakRef:
356 env->DeleteWeakGlobalRef(inputChannelObjWeak);
357 return status;
358}
359
360status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
361 const sp<InputChannel>& inputChannel) {
362 jweak inputChannelObjWeak;
363 {
364 AutoMutex _l(mInputChannelRegistryLock);
365
Jeff Brown2cbecea2010-08-17 15:59:26 -0700366 ssize_t index = mInputChannelObjWeakTable.indexOfKey(inputChannel.get());
Jeff Brown7fbdc842010-06-17 20:52:56 -0700367 if (index < 0) {
368 LOGE("Input channel object '%s' is not currently registered",
369 inputChannel->getName().string());
370 return INVALID_OPERATION;
371 }
372
Jeff Brown2cbecea2010-08-17 15:59:26 -0700373 inputChannelObjWeak = mInputChannelObjWeakTable.valueAt(index);
374 mInputChannelObjWeakTable.removeItemsAt(index);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700375 }
376
377 env->DeleteWeakGlobalRef(inputChannelObjWeak);
378
Jeff Brownb88102f2010-09-08 11:49:43 -0700379 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700380}
381
382jobject NativeInputManager::getInputChannelObjLocal(JNIEnv* env,
383 const sp<InputChannel>& inputChannel) {
Jeff Brown54a18252010-09-16 14:07:33 -0700384 InputChannel* inputChannelPtr = inputChannel.get();
385 if (! inputChannelPtr) {
386 return NULL;
387 }
388
Jeff Brown7fbdc842010-06-17 20:52:56 -0700389 {
390 AutoMutex _l(mInputChannelRegistryLock);
391
Jeff Brown54a18252010-09-16 14:07:33 -0700392 ssize_t index = mInputChannelObjWeakTable.indexOfKey(inputChannelPtr);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700393 if (index < 0) {
394 return NULL;
395 }
396
Jeff Brown2cbecea2010-08-17 15:59:26 -0700397 jweak inputChannelObjWeak = mInputChannelObjWeakTable.valueAt(index);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700398 return env->NewLocalRef(inputChannelObjWeak);
399 }
400}
401
Jeff Brown9c3cda02010-06-15 01:31:58 -0700402bool NativeInputManager::getDisplayInfo(int32_t displayId,
403 int32_t* width, int32_t* height, int32_t* orientation) {
404 bool result = false;
405 if (displayId == 0) {
406 AutoMutex _l(mDisplayLock);
407
408 if (mDisplayWidth > 0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 if (width) {
410 *width = mDisplayWidth;
411 }
412 if (height) {
413 *height = mDisplayHeight;
414 }
415 if (orientation) {
416 *orientation = mDisplayOrientation;
417 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700418 result = true;
419 }
420 }
421 return result;
422}
423
Jeff Brown9c3cda02010-06-15 01:31:58 -0700424bool NativeInputManager::filterTouchEvents() {
425 if (mFilterTouchEvents < 0) {
426 JNIEnv* env = jniEnv();
427
428 jboolean result = env->CallBooleanMethod(mCallbacksObj,
429 gCallbacksClassInfo.filterTouchEvents);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700430 if (checkAndClearExceptionFromCallback(env, "filterTouchEvents")) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700431 result = false;
432 }
433
434 mFilterTouchEvents = result ? 1 : 0;
435 }
436 return mFilterTouchEvents;
437}
438
439bool NativeInputManager::filterJumpyTouchEvents() {
440 if (mFilterJumpyTouchEvents < 0) {
441 JNIEnv* env = jniEnv();
442
443 jboolean result = env->CallBooleanMethod(mCallbacksObj,
444 gCallbacksClassInfo.filterJumpyTouchEvents);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700445 if (checkAndClearExceptionFromCallback(env, "filterJumpyTouchEvents")) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700446 result = false;
447 }
448
449 mFilterJumpyTouchEvents = result ? 1 : 0;
450 }
451 return mFilterJumpyTouchEvents;
452}
453
454void NativeInputManager::getVirtualKeyDefinitions(const String8& deviceName,
Jeff Brown8d608662010-08-30 03:02:23 -0700455 Vector<VirtualKeyDefinition>& outVirtualKeyDefinitions) {
456 outVirtualKeyDefinitions.clear();
457
Jeff Brown9c3cda02010-06-15 01:31:58 -0700458 JNIEnv* env = jniEnv();
459
460 jstring deviceNameStr = env->NewStringUTF(deviceName.string());
Jeff Brown7fbdc842010-06-17 20:52:56 -0700461 if (! checkAndClearExceptionFromCallback(env, "getVirtualKeyDefinitions")) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700462 jobjectArray result = jobjectArray(env->CallObjectMethod(mCallbacksObj,
463 gCallbacksClassInfo.getVirtualKeyDefinitions, deviceNameStr));
Jeff Brown7fbdc842010-06-17 20:52:56 -0700464 if (! checkAndClearExceptionFromCallback(env, "getVirtualKeyDefinitions") && result) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700465 jsize length = env->GetArrayLength(result);
466 for (jsize i = 0; i < length; i++) {
467 jobject item = env->GetObjectArrayElement(result, i);
468
469 outVirtualKeyDefinitions.add();
470 outVirtualKeyDefinitions.editTop().scanCode =
471 int32_t(env->GetIntField(item, gVirtualKeyDefinitionClassInfo.scanCode));
472 outVirtualKeyDefinitions.editTop().centerX =
473 int32_t(env->GetIntField(item, gVirtualKeyDefinitionClassInfo.centerX));
474 outVirtualKeyDefinitions.editTop().centerY =
475 int32_t(env->GetIntField(item, gVirtualKeyDefinitionClassInfo.centerY));
476 outVirtualKeyDefinitions.editTop().width =
477 int32_t(env->GetIntField(item, gVirtualKeyDefinitionClassInfo.width));
478 outVirtualKeyDefinitions.editTop().height =
479 int32_t(env->GetIntField(item, gVirtualKeyDefinitionClassInfo.height));
480
481 env->DeleteLocalRef(item);
482 }
483 env->DeleteLocalRef(result);
484 }
485 env->DeleteLocalRef(deviceNameStr);
486 }
487}
488
Jeff Brown8d608662010-08-30 03:02:23 -0700489void NativeInputManager::getInputDeviceCalibration(const String8& deviceName,
490 InputDeviceCalibration& outCalibration) {
491 outCalibration.clear();
492
493 JNIEnv* env = jniEnv();
494
495 jstring deviceNameStr = env->NewStringUTF(deviceName.string());
496 if (! checkAndClearExceptionFromCallback(env, "getInputDeviceCalibration")) {
497 jobject result = env->CallObjectMethod(mCallbacksObj,
498 gCallbacksClassInfo.getInputDeviceCalibration, deviceNameStr);
499 if (! checkAndClearExceptionFromCallback(env, "getInputDeviceCalibration") && result) {
500 jobjectArray keys = jobjectArray(env->GetObjectField(result,
501 gInputDeviceCalibrationClassInfo.keys));
502 jobjectArray values = jobjectArray(env->GetObjectField(result,
503 gInputDeviceCalibrationClassInfo.values));
504
505 jsize length = env->GetArrayLength(keys);
506 for (jsize i = 0; i < length; i++) {
507 jstring keyStr = jstring(env->GetObjectArrayElement(keys, i));
508 jstring valueStr = jstring(env->GetObjectArrayElement(values, i));
509
510 const char* keyChars = env->GetStringUTFChars(keyStr, NULL);
511 String8 key(keyChars);
512 env->ReleaseStringUTFChars(keyStr, keyChars);
513
514 const char* valueChars = env->GetStringUTFChars(valueStr, NULL);
515 String8 value(valueChars);
516 env->ReleaseStringUTFChars(valueStr, valueChars);
517
518 outCalibration.addProperty(key, value);
519
520 env->DeleteLocalRef(keyStr);
521 env->DeleteLocalRef(valueStr);
522 }
523 env->DeleteLocalRef(keys);
524 env->DeleteLocalRef(values);
525 env->DeleteLocalRef(result);
526 }
527 env->DeleteLocalRef(deviceNameStr);
528 }
529}
530
Jeff Brown9c3cda02010-06-15 01:31:58 -0700531void NativeInputManager::getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) {
Jeff Brown8d608662010-08-30 03:02:23 -0700532 outExcludedDeviceNames.clear();
533
Jeff Brown9c3cda02010-06-15 01:31:58 -0700534 JNIEnv* env = jniEnv();
535
536 jobjectArray result = jobjectArray(env->CallObjectMethod(mCallbacksObj,
537 gCallbacksClassInfo.getExcludedDeviceNames));
Jeff Brown7fbdc842010-06-17 20:52:56 -0700538 if (! checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && result) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700539 jsize length = env->GetArrayLength(result);
540 for (jsize i = 0; i < length; i++) {
541 jstring item = jstring(env->GetObjectArrayElement(result, i));
542
543 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
544 outExcludedDeviceNames.add(String8(deviceNameChars));
545 env->ReleaseStringUTFChars(item, deviceNameChars);
546
547 env->DeleteLocalRef(item);
548 }
549 env->DeleteLocalRef(result);
550 }
551}
552
Jeff Browne20c9e02010-10-11 14:20:19 -0700553void NativeInputManager::notifySwitch(nsecs_t when, int32_t switchCode,
554 int32_t switchValue, uint32_t policyFlags) {
555#if DEBUG_INPUT_DISPATCHER_POLICY
556 LOGD("notifySwitch - when=%lld, switchCode=%d, switchValue=%d, policyFlags=0x%x",
557 when, switchCode, switchValue, policyFlags);
558#endif
559
560 JNIEnv* env = jniEnv();
561
562 switch (switchCode) {
563 case SW_LID:
564 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyLidSwitchChanged,
565 when, switchValue == 0);
566 checkAndClearExceptionFromCallback(env, "notifyLidSwitchChanged");
567 break;
568 }
569}
570
Jeff Brown9c3cda02010-06-15 01:31:58 -0700571void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
572#if DEBUG_INPUT_DISPATCHER_POLICY
573 LOGD("notifyConfigurationChanged - when=%lld", when);
574#endif
575
576 JNIEnv* env = jniEnv();
577
Jeff Brown57c59372010-09-21 18:22:55 -0700578 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700579 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700580}
581
Jeff Brown519e0242010-09-15 15:18:56 -0700582nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
583 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700584#if DEBUG_INPUT_DISPATCHER_POLICY
585 LOGD("notifyANR");
586#endif
587
588 JNIEnv* env = jniEnv();
589
Jeff Brown519e0242010-09-15 15:18:56 -0700590 jobject tokenObjLocal;
591 if (inputApplicationHandle.get()) {
592 ApplicationToken* token = static_cast<ApplicationToken*>(inputApplicationHandle.get());
593 jweak tokenObjWeak = token->getTokenObj();
594 tokenObjLocal = env->NewLocalRef(tokenObjWeak);
Jeff Brownb88102f2010-09-08 11:49:43 -0700595 } else {
Jeff Brown519e0242010-09-15 15:18:56 -0700596 tokenObjLocal = NULL;
Jeff Brownb88102f2010-09-08 11:49:43 -0700597 }
598
Jeff Brown54a18252010-09-16 14:07:33 -0700599 jobject inputChannelObjLocal = getInputChannelObjLocal(env, inputChannel);
Jeff Brown519e0242010-09-15 15:18:56 -0700600 jlong newTimeout = env->CallLongMethod(mCallbacksObj,
601 gCallbacksClassInfo.notifyANR, tokenObjLocal, inputChannelObjLocal);
602 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
603 newTimeout = 0; // abort dispatch
604 } else {
605 assert(newTimeout >= 0);
606 }
607
608 env->DeleteLocalRef(tokenObjLocal);
609 env->DeleteLocalRef(inputChannelObjLocal);
Jeff Brownb88102f2010-09-08 11:49:43 -0700610 return newTimeout;
611}
612
Jeff Brown9c3cda02010-06-15 01:31:58 -0700613void NativeInputManager::notifyInputChannelBroken(const sp<InputChannel>& inputChannel) {
614#if DEBUG_INPUT_DISPATCHER_POLICY
615 LOGD("notifyInputChannelBroken - inputChannel='%s'", inputChannel->getName().string());
616#endif
617
Jeff Brown7fbdc842010-06-17 20:52:56 -0700618 JNIEnv* env = jniEnv();
619
620 jobject inputChannelObjLocal = getInputChannelObjLocal(env, inputChannel);
621 if (inputChannelObjLocal) {
622 env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyInputChannelBroken,
623 inputChannelObjLocal);
624 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
625
626 env->DeleteLocalRef(inputChannelObjLocal);
627 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700628}
629
Jeff Brown9c3cda02010-06-15 01:31:58 -0700630nsecs_t NativeInputManager::getKeyRepeatTimeout() {
631 if (! isScreenOn()) {
632 // Disable key repeat when the screen is off.
633 return -1;
634 } else {
635 // TODO use ViewConfiguration.getLongPressTimeout()
636 return milliseconds_to_nanoseconds(500);
637 }
638}
639
Jeff Brownb21fb102010-09-07 10:44:57 -0700640nsecs_t NativeInputManager::getKeyRepeatDelay() {
641 return milliseconds_to_nanoseconds(50);
642}
643
Jeff Brownae9fc032010-08-18 15:51:08 -0700644int32_t NativeInputManager::getMaxEventsPerSecond() {
645 if (mMaxEventsPerSecond < 0) {
646 JNIEnv* env = jniEnv();
647
648 jint result = env->CallIntMethod(mCallbacksObj,
649 gCallbacksClassInfo.getMaxEventsPerSecond);
650 if (checkAndClearExceptionFromCallback(env, "getMaxEventsPerSecond")) {
Jeff Brown3d8c9bd2010-08-18 17:48:53 -0700651 result = 60;
Jeff Brownae9fc032010-08-18 15:51:08 -0700652 }
653
654 mMaxEventsPerSecond = result;
655 }
656 return mMaxEventsPerSecond;
657}
658
Jeff Brown349703e2010-06-22 01:27:15 -0700659void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowObjArray) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700660 Vector<InputWindow> windows;
Jeff Brown349703e2010-06-22 01:27:15 -0700661
Jeff Brownb88102f2010-09-08 11:49:43 -0700662 jsize length = env->GetArrayLength(windowObjArray);
663 for (jsize i = 0; i < length; i++) {
664 jobject inputTargetObj = env->GetObjectArrayElement(windowObjArray, i);
665 if (! inputTargetObj) {
666 break; // found null element indicating end of used portion of the array
Jeff Brown349703e2010-06-22 01:27:15 -0700667 }
668
Jeff Brownb88102f2010-09-08 11:49:43 -0700669 windows.push();
670 InputWindow& window = windows.editTop();
671 bool valid = populateWindow(env, inputTargetObj, window);
672 if (! valid) {
673 windows.pop();
Jeff Brown349703e2010-06-22 01:27:15 -0700674 }
675
Jeff Brownb88102f2010-09-08 11:49:43 -0700676 env->DeleteLocalRef(inputTargetObj);
677 }
Jeff Brown349703e2010-06-22 01:27:15 -0700678
Jeff Brownb88102f2010-09-08 11:49:43 -0700679 mInputManager->getDispatcher()->setInputWindows(windows);
Jeff Brown349703e2010-06-22 01:27:15 -0700680}
681
682bool NativeInputManager::populateWindow(JNIEnv* env, jobject windowObj,
683 InputWindow& outWindow) {
684 bool valid = false;
685
686 jobject inputChannelObj = env->GetObjectField(windowObj,
687 gInputWindowClassInfo.inputChannel);
688 if (inputChannelObj) {
689 sp<InputChannel> inputChannel =
690 android_view_InputChannel_getInputChannel(env, inputChannelObj);
691 if (inputChannel != NULL) {
Jeff Brown519e0242010-09-15 15:18:56 -0700692 jstring name = jstring(env->GetObjectField(windowObj,
693 gInputWindowClassInfo.name));
Jeff Brown349703e2010-06-22 01:27:15 -0700694 jint layoutParamsFlags = env->GetIntField(windowObj,
695 gInputWindowClassInfo.layoutParamsFlags);
696 jint layoutParamsType = env->GetIntField(windowObj,
697 gInputWindowClassInfo.layoutParamsType);
698 jlong dispatchingTimeoutNanos = env->GetLongField(windowObj,
699 gInputWindowClassInfo.dispatchingTimeoutNanos);
700 jint frameLeft = env->GetIntField(windowObj,
701 gInputWindowClassInfo.frameLeft);
702 jint frameTop = env->GetIntField(windowObj,
703 gInputWindowClassInfo.frameTop);
Jeff Brown85a31762010-09-01 17:01:00 -0700704 jint frameRight = env->GetIntField(windowObj,
705 gInputWindowClassInfo.frameRight);
706 jint frameBottom = env->GetIntField(windowObj,
707 gInputWindowClassInfo.frameBottom);
708 jint visibleFrameLeft = env->GetIntField(windowObj,
709 gInputWindowClassInfo.visibleFrameLeft);
710 jint visibleFrameTop = env->GetIntField(windowObj,
711 gInputWindowClassInfo.visibleFrameTop);
712 jint visibleFrameRight = env->GetIntField(windowObj,
713 gInputWindowClassInfo.visibleFrameRight);
714 jint visibleFrameBottom = env->GetIntField(windowObj,
715 gInputWindowClassInfo.visibleFrameBottom);
Jeff Brown349703e2010-06-22 01:27:15 -0700716 jint touchableAreaLeft = env->GetIntField(windowObj,
717 gInputWindowClassInfo.touchableAreaLeft);
718 jint touchableAreaTop = env->GetIntField(windowObj,
719 gInputWindowClassInfo.touchableAreaTop);
720 jint touchableAreaRight = env->GetIntField(windowObj,
721 gInputWindowClassInfo.touchableAreaRight);
722 jint touchableAreaBottom = env->GetIntField(windowObj,
723 gInputWindowClassInfo.touchableAreaBottom);
724 jboolean visible = env->GetBooleanField(windowObj,
725 gInputWindowClassInfo.visible);
Jeff Brown519e0242010-09-15 15:18:56 -0700726 jboolean canReceiveKeys = env->GetBooleanField(windowObj,
727 gInputWindowClassInfo.canReceiveKeys);
Jeff Brown349703e2010-06-22 01:27:15 -0700728 jboolean hasFocus = env->GetBooleanField(windowObj,
729 gInputWindowClassInfo.hasFocus);
730 jboolean hasWallpaper = env->GetBooleanField(windowObj,
731 gInputWindowClassInfo.hasWallpaper);
732 jboolean paused = env->GetBooleanField(windowObj,
733 gInputWindowClassInfo.paused);
Jeff Brown519e0242010-09-15 15:18:56 -0700734 jint layer = env->GetIntField(windowObj,
735 gInputWindowClassInfo.layer);
Jeff Brown349703e2010-06-22 01:27:15 -0700736 jint ownerPid = env->GetIntField(windowObj,
737 gInputWindowClassInfo.ownerPid);
738 jint ownerUid = env->GetIntField(windowObj,
739 gInputWindowClassInfo.ownerUid);
740
Jeff Brown519e0242010-09-15 15:18:56 -0700741 const char* nameStr = env->GetStringUTFChars(name, NULL);
742
Jeff Brown349703e2010-06-22 01:27:15 -0700743 outWindow.inputChannel = inputChannel;
Jeff Brown519e0242010-09-15 15:18:56 -0700744 outWindow.name.setTo(nameStr);
Jeff Brown349703e2010-06-22 01:27:15 -0700745 outWindow.layoutParamsFlags = layoutParamsFlags;
746 outWindow.layoutParamsType = layoutParamsType;
747 outWindow.dispatchingTimeout = dispatchingTimeoutNanos;
748 outWindow.frameLeft = frameLeft;
749 outWindow.frameTop = frameTop;
Jeff Brown85a31762010-09-01 17:01:00 -0700750 outWindow.frameRight = frameRight;
751 outWindow.frameBottom = frameBottom;
752 outWindow.visibleFrameLeft = visibleFrameLeft;
753 outWindow.visibleFrameTop = visibleFrameTop;
754 outWindow.visibleFrameRight = visibleFrameRight;
755 outWindow.visibleFrameBottom = visibleFrameBottom;
Jeff Brown349703e2010-06-22 01:27:15 -0700756 outWindow.touchableAreaLeft = touchableAreaLeft;
757 outWindow.touchableAreaTop = touchableAreaTop;
758 outWindow.touchableAreaRight = touchableAreaRight;
759 outWindow.touchableAreaBottom = touchableAreaBottom;
760 outWindow.visible = visible;
Jeff Brown519e0242010-09-15 15:18:56 -0700761 outWindow.canReceiveKeys = canReceiveKeys;
Jeff Brown349703e2010-06-22 01:27:15 -0700762 outWindow.hasFocus = hasFocus;
763 outWindow.hasWallpaper = hasWallpaper;
764 outWindow.paused = paused;
Jeff Brown519e0242010-09-15 15:18:56 -0700765 outWindow.layer = layer;
Jeff Brown349703e2010-06-22 01:27:15 -0700766 outWindow.ownerPid = ownerPid;
767 outWindow.ownerUid = ownerUid;
Jeff Brown519e0242010-09-15 15:18:56 -0700768
769 env->ReleaseStringUTFChars(name, nameStr);
Jeff Brown349703e2010-06-22 01:27:15 -0700770 valid = true;
771 } else {
772 LOGW("Dropping input target because its input channel is not initialized.");
773 }
774
775 env->DeleteLocalRef(inputChannelObj);
776 } else {
777 LOGW("Dropping input target because the input channel object was null.");
778 }
779 return valid;
780}
781
782void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationObj) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700783 if (applicationObj) {
784 jstring nameObj = jstring(env->GetObjectField(applicationObj,
785 gInputApplicationClassInfo.name));
786 jlong dispatchingTimeoutNanos = env->GetLongField(applicationObj,
787 gInputApplicationClassInfo.dispatchingTimeoutNanos);
788 jobject tokenObj = env->GetObjectField(applicationObj,
789 gInputApplicationClassInfo.token);
790 jweak tokenObjWeak = env->NewWeakGlobalRef(tokenObj);
791 if (! tokenObjWeak) {
792 LOGE("Could not create weak reference for application token.");
793 LOGE_EX(env);
794 env->ExceptionClear();
795 }
796 env->DeleteLocalRef(tokenObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700797
Jeff Brownb88102f2010-09-08 11:49:43 -0700798 String8 name;
799 if (nameObj) {
800 const char* nameStr = env->GetStringUTFChars(nameObj, NULL);
801 name.setTo(nameStr);
802 env->ReleaseStringUTFChars(nameObj, nameStr);
803 env->DeleteLocalRef(nameObj);
804 } else {
805 LOGE("InputApplication.name should not be null.");
806 name.setTo("unknown");
Jeff Brown349703e2010-06-22 01:27:15 -0700807 }
808
Jeff Brownb88102f2010-09-08 11:49:43 -0700809 InputApplication application;
810 application.name = name;
811 application.dispatchingTimeout = dispatchingTimeoutNanos;
812 application.handle = new ApplicationToken(tokenObjWeak);
813 mInputManager->getDispatcher()->setFocusedApplication(& application);
814 } else {
815 mInputManager->getDispatcher()->setFocusedApplication(NULL);
Jeff Brown349703e2010-06-22 01:27:15 -0700816 }
817}
818
819void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700820 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700821}
822
Jeff Browne20c9e02010-10-11 14:20:19 -0700823bool NativeInputManager::isScreenOn() {
824 return android_server_PowerManagerService_isScreenOn();
825}
826
827bool NativeInputManager::isScreenBright() {
828 return android_server_PowerManagerService_isScreenBright();
829}
830
831void NativeInputManager::interceptKeyBeforeQueueing(nsecs_t when,
832 int32_t deviceId, int32_t action, int32_t &flags,
833 int32_t keyCode, int32_t scanCode, uint32_t& policyFlags) {
834#if DEBUG_INPUT_DISPATCHER_POLICY
835 LOGD("interceptKeyBeforeQueueing - when=%lld, deviceId=%d, action=%d, flags=%d, "
836 "keyCode=%d, scanCode=%d, policyFlags=0x%x",
837 when, deviceId, action, flags, keyCode, scanCode, policyFlags);
838#endif
839
840 if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) {
841 policyFlags |= POLICY_FLAG_VIRTUAL;
842 flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
Jeff Brownb88102f2010-09-08 11:49:43 -0700843 }
844
Jeff Browne20c9e02010-10-11 14:20:19 -0700845 const int32_t WM_ACTION_PASS_TO_USER = 1;
846 const int32_t WM_ACTION_POKE_USER_ACTIVITY = 2;
847 const int32_t WM_ACTION_GO_TO_SLEEP = 4;
848
849 bool isScreenOn = this->isScreenOn();
850 bool isScreenBright = this->isScreenBright();
851
852 JNIEnv* env = jniEnv();
853 jint wmActions = env->CallIntMethod(mCallbacksObj,
854 gCallbacksClassInfo.interceptKeyBeforeQueueing,
855 when, keyCode, action == AKEY_EVENT_ACTION_DOWN, policyFlags, isScreenOn);
856 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
857 wmActions = 0;
858 }
859
860 if (policyFlags & POLICY_FLAG_TRUSTED) {
861 if (! isScreenOn) {
862 // Key presses and releases wake the device.
863 policyFlags |= POLICY_FLAG_WOKE_HERE;
864 flags |= AKEY_EVENT_FLAG_WOKE_HERE;
865 }
866
867 if (! isScreenBright) {
868 // Key presses and releases brighten the screen if dimmed.
869 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
870 }
871
872 if (wmActions & WM_ACTION_GO_TO_SLEEP) {
873 android_server_PowerManagerService_goToSleep(when);
874 }
875
876 if (wmActions & WM_ACTION_POKE_USER_ACTIVITY) {
877 android_server_PowerManagerService_userActivity(when, POWER_MANAGER_BUTTON_EVENT);
878 }
879 }
880
881 if (wmActions & WM_ACTION_PASS_TO_USER) {
882 policyFlags |= POLICY_FLAG_PASS_TO_USER;
883 }
884}
885
886void NativeInputManager::interceptGenericBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
887#if DEBUG_INPUT_DISPATCHER_POLICY
888 LOGD("interceptGenericBeforeQueueing - when=%lld, policyFlags=0x%x", when, policyFlags);
889#endif
890
891 if (isScreenOn()) {
892 // Only dispatch events when the device is awake.
893 // Do not wake the device.
894 policyFlags |= POLICY_FLAG_PASS_TO_USER;
895
896 if ((policyFlags & POLICY_FLAG_TRUSTED) && !isScreenBright()) {
897 // Brighten the screen if dimmed.
898 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
899 }
900 }
901}
902
903bool NativeInputManager::interceptKeyBeforeDispatching(const sp<InputChannel>& inputChannel,
904 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brownd0097872010-06-30 14:41:59 -0700905 JNIEnv* env = jniEnv();
906
Jeff Brown54a18252010-09-16 14:07:33 -0700907 // Note: inputChannel may be null.
Jeff Brownb88102f2010-09-08 11:49:43 -0700908 jobject inputChannelObj = getInputChannelObjLocal(env, inputChannel);
Jeff Brown54a18252010-09-16 14:07:33 -0700909 jboolean consumed = env->CallBooleanMethod(mCallbacksObj,
910 gCallbacksClassInfo.interceptKeyBeforeDispatching,
911 inputChannelObj, keyEvent->getAction(), keyEvent->getFlags(),
912 keyEvent->getKeyCode(), keyEvent->getMetaState(),
913 keyEvent->getRepeatCount(), policyFlags);
914 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
Jeff Brownd0097872010-06-30 14:41:59 -0700915
Jeff Brown54a18252010-09-16 14:07:33 -0700916 env->DeleteLocalRef(inputChannelObj);
917 return consumed && ! error;
Jeff Brownd0097872010-06-30 14:41:59 -0700918}
919
Jeff Brown01ce2e92010-09-26 22:20:12 -0700920void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
921 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700922}
923
Jeff Brown349703e2010-06-22 01:27:15 -0700924
Jeff Brownb88102f2010-09-08 11:49:43 -0700925bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
926 int32_t injectorPid, int32_t injectorUid) {
927 JNIEnv* env = jniEnv();
928 jboolean result = env->CallBooleanMethod(mCallbacksObj,
929 gCallbacksClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
930 checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission");
Jeff Brown349703e2010-06-22 01:27:15 -0700931 return result;
932}
933
Jeff Brown9c3cda02010-06-15 01:31:58 -0700934// ----------------------------------------------------------------------------
935
936static sp<NativeInputManager> gNativeInputManager;
937
Jeff Brown46b9ac02010-04-22 18:58:52 -0700938static bool checkInputManagerUnitialized(JNIEnv* env) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700939 if (gNativeInputManager == NULL) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700940 LOGE("Input manager not initialized.");
941 jniThrowRuntimeException(env, "Input manager not initialized.");
942 return true;
943 }
944 return false;
945}
946
947static void android_server_InputManager_nativeInit(JNIEnv* env, jclass clazz,
948 jobject callbacks) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700949 if (gNativeInputManager == NULL) {
950 gNativeInputManager = new NativeInputManager(callbacks);
951 } else {
952 LOGE("Input manager already initialized.");
953 jniThrowRuntimeException(env, "Input manager already initialized.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700954 }
955}
956
957static void android_server_InputManager_nativeStart(JNIEnv* env, jclass clazz) {
958 if (checkInputManagerUnitialized(env)) {
959 return;
960 }
961
Jeff Brown9c3cda02010-06-15 01:31:58 -0700962 status_t result = gNativeInputManager->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700963 if (result) {
964 jniThrowRuntimeException(env, "Input manager could not be started.");
965 }
966}
967
968static void android_server_InputManager_nativeSetDisplaySize(JNIEnv* env, jclass clazz,
969 jint displayId, jint width, jint height) {
970 if (checkInputManagerUnitialized(env)) {
971 return;
972 }
973
974 // XXX we could get this from the SurfaceFlinger directly instead of requiring it
975 // to be passed in like this, not sure which is better but leaving it like this
976 // keeps the window manager in direct control of when display transitions propagate down
977 // to the input dispatcher
Jeff Brown9c3cda02010-06-15 01:31:58 -0700978 gNativeInputManager->setDisplaySize(displayId, width, height);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700979}
980
981static void android_server_InputManager_nativeSetDisplayOrientation(JNIEnv* env, jclass clazz,
982 jint displayId, jint orientation) {
983 if (checkInputManagerUnitialized(env)) {
984 return;
985 }
986
Jeff Brown9c3cda02010-06-15 01:31:58 -0700987 gNativeInputManager->setDisplayOrientation(displayId, orientation);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700988}
989
990static jint android_server_InputManager_nativeGetScanCodeState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700991 jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700992 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700993 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700994 }
995
Jeff Brownb88102f2010-09-08 11:49:43 -0700996 return gNativeInputManager->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -0700997 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700998}
999
1000static jint android_server_InputManager_nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001001 jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001002 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001003 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001004 }
1005
Jeff Brownb88102f2010-09-08 11:49:43 -07001006 return gNativeInputManager->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001007 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001008}
1009
1010static jint android_server_InputManager_nativeGetSwitchState(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001011 jint deviceId, jint sourceMask, jint sw) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001012 if (checkInputManagerUnitialized(env)) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001013 return AKEY_STATE_UNKNOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001014 }
1015
Jeff Brownb88102f2010-09-08 11:49:43 -07001016 return gNativeInputManager->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001017 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001018}
1019
1020static jboolean android_server_InputManager_nativeHasKeys(JNIEnv* env, jclass clazz,
Jeff Brown6d0fec22010-07-23 21:28:06 -07001021 jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001022 if (checkInputManagerUnitialized(env)) {
1023 return JNI_FALSE;
1024 }
1025
1026 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1027 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1028 jsize numCodes = env->GetArrayLength(keyCodes);
1029 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001030 if (numCodes == env->GetArrayLength(keyCodes)) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001031 result = gNativeInputManager->getInputManager()->getReader()->hasKeys(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001032 deviceId, uint32_t(sourceMask), numCodes, codes, flags);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001033 } else {
1034 result = JNI_FALSE;
1035 }
1036
1037 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1038 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1039 return result;
1040}
1041
1042static void throwInputChannelNotInitialized(JNIEnv* env) {
1043 jniThrowException(env, "java/lang/IllegalStateException",
1044 "inputChannel is not initialized");
1045}
1046
1047static void android_server_InputManager_handleInputChannelDisposed(JNIEnv* env,
1048 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
1049 LOGW("Input channel object '%s' was disposed without first being unregistered with "
1050 "the input manager!", inputChannel->getName().string());
1051
Jeff Brown9c3cda02010-06-15 01:31:58 -07001052 if (gNativeInputManager != NULL) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07001053 gNativeInputManager->unregisterInputChannel(env, inputChannel);
Jeff Brown9c3cda02010-06-15 01:31:58 -07001054 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001055}
1056
1057static void android_server_InputManager_nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
Jeff Browna41ca772010-08-11 14:46:32 -07001058 jobject inputChannelObj, jboolean monitor) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001059 if (checkInputManagerUnitialized(env)) {
1060 return;
1061 }
1062
1063 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1064 inputChannelObj);
1065 if (inputChannel == NULL) {
1066 throwInputChannelNotInitialized(env);
1067 return;
1068 }
1069
Jeff Brown7fbdc842010-06-17 20:52:56 -07001070
1071 status_t status = gNativeInputManager->registerInputChannel(
Jeff Browna41ca772010-08-11 14:46:32 -07001072 env, inputChannel, inputChannelObj, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001073 if (status) {
1074 jniThrowRuntimeException(env, "Failed to register input channel. "
1075 "Check logs for details.");
1076 return;
1077 }
1078
Jeff Browna41ca772010-08-11 14:46:32 -07001079 if (! monitor) {
1080 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
1081 android_server_InputManager_handleInputChannelDisposed, NULL);
1082 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001083}
1084
1085static void android_server_InputManager_nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
1086 jobject inputChannelObj) {
1087 if (checkInputManagerUnitialized(env)) {
1088 return;
1089 }
1090
1091 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1092 inputChannelObj);
1093 if (inputChannel == NULL) {
1094 throwInputChannelNotInitialized(env);
1095 return;
1096 }
1097
1098 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1099
Jeff Brown7fbdc842010-06-17 20:52:56 -07001100 status_t status = gNativeInputManager->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001101 if (status) {
1102 jniThrowRuntimeException(env, "Failed to unregister input channel. "
1103 "Check logs for details.");
1104 }
1105}
1106
Jeff Brown6ec402b2010-07-28 15:48:59 -07001107static jint android_server_InputManager_nativeInjectInputEvent(JNIEnv* env, jclass clazz,
1108 jobject inputEventObj, jint injectorPid, jint injectorUid,
1109 jint syncMode, jint timeoutMillis) {
Jeff Brown7fbdc842010-06-17 20:52:56 -07001110 if (checkInputManagerUnitialized(env)) {
1111 return INPUT_EVENT_INJECTION_FAILED;
1112 }
1113
Jeff Brown6ec402b2010-07-28 15:48:59 -07001114 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1115 KeyEvent keyEvent;
1116 android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001117
Jeff Brownb88102f2010-09-08 11:49:43 -07001118 return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent(
1119 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001120 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
1121 MotionEvent motionEvent;
1122 android_view_MotionEvent_toNative(env, inputEventObj, & motionEvent);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001123
Jeff Brownb88102f2010-09-08 11:49:43 -07001124 return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent(
1125 & motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis);
Jeff Brown6ec402b2010-07-28 15:48:59 -07001126 } else {
1127 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001128 return INPUT_EVENT_INJECTION_FAILED;
1129 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001130}
1131
Jeff Brown349703e2010-06-22 01:27:15 -07001132static void android_server_InputManager_nativeSetInputWindows(JNIEnv* env, jclass clazz,
1133 jobjectArray windowObjArray) {
1134 if (checkInputManagerUnitialized(env)) {
1135 return;
1136 }
1137
1138 gNativeInputManager->setInputWindows(env, windowObjArray);
1139}
1140
1141static void android_server_InputManager_nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
1142 jobject applicationObj) {
1143 if (checkInputManagerUnitialized(env)) {
1144 return;
1145 }
1146
1147 gNativeInputManager->setFocusedApplication(env, applicationObj);
1148}
1149
1150static void android_server_InputManager_nativeSetInputDispatchMode(JNIEnv* env,
1151 jclass clazz, jboolean enabled, jboolean frozen) {
1152 if (checkInputManagerUnitialized(env)) {
1153 return;
1154 }
1155
1156 gNativeInputManager->setInputDispatchMode(enabled, frozen);
1157}
1158
Jeff Brown8d608662010-08-30 03:02:23 -07001159static jobject android_server_InputManager_nativeGetInputDevice(JNIEnv* env,
1160 jclass clazz, jint deviceId) {
1161 if (checkInputManagerUnitialized(env)) {
1162 return NULL;
1163 }
1164
1165 InputDeviceInfo deviceInfo;
Jeff Brownb88102f2010-09-08 11:49:43 -07001166 status_t status = gNativeInputManager->getInputManager()->getReader()->getInputDeviceInfo(
Jeff Brown8d608662010-08-30 03:02:23 -07001167 deviceId, & deviceInfo);
1168 if (status) {
1169 return NULL;
1170 }
1171
1172 jobject deviceObj = env->NewObject(gInputDeviceClassInfo.clazz, gInputDeviceClassInfo.ctor);
1173 if (! deviceObj) {
1174 return NULL;
1175 }
1176
1177 jstring deviceNameObj = env->NewStringUTF(deviceInfo.getName().string());
1178 if (! deviceNameObj) {
1179 return NULL;
1180 }
1181
1182 env->SetIntField(deviceObj, gInputDeviceClassInfo.mId, deviceInfo.getId());
1183 env->SetObjectField(deviceObj, gInputDeviceClassInfo.mName, deviceNameObj);
1184 env->SetIntField(deviceObj, gInputDeviceClassInfo.mSources, deviceInfo.getSources());
1185 env->SetIntField(deviceObj, gInputDeviceClassInfo.mKeyboardType, deviceInfo.getKeyboardType());
1186
1187 const KeyedVector<int, InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
1188 for (size_t i = 0; i < ranges.size(); i++) {
1189 int rangeType = ranges.keyAt(i);
1190 const InputDeviceInfo::MotionRange& range = ranges.valueAt(i);
1191 env->CallVoidMethod(deviceObj, gInputDeviceClassInfo.addMotionRange,
1192 rangeType, range.min, range.max, range.flat, range.fuzz);
1193 if (env->ExceptionCheck()) {
1194 return NULL;
1195 }
1196 }
1197
1198 return deviceObj;
1199}
1200
1201static jintArray android_server_InputManager_nativeGetInputDeviceIds(JNIEnv* env,
1202 jclass clazz) {
1203 if (checkInputManagerUnitialized(env)) {
1204 return NULL;
1205 }
1206
1207 Vector<int> deviceIds;
Jeff Brownb88102f2010-09-08 11:49:43 -07001208 gNativeInputManager->getInputManager()->getReader()->getInputDeviceIds(deviceIds);
Jeff Brown8d608662010-08-30 03:02:23 -07001209
1210 jintArray deviceIdsObj = env->NewIntArray(deviceIds.size());
1211 if (! deviceIdsObj) {
1212 return NULL;
1213 }
1214
1215 env->SetIntArrayRegion(deviceIdsObj, 0, deviceIds.size(), deviceIds.array());
1216 return deviceIdsObj;
1217}
1218
Jeff Brown57c59372010-09-21 18:22:55 -07001219static void android_server_InputManager_nativeGetInputConfiguration(JNIEnv* env,
1220 jclass clazz, jobject configObj) {
1221 if (checkInputManagerUnitialized(env)) {
1222 return;
1223 }
1224
1225 InputConfiguration config;
1226 gNativeInputManager->getInputManager()->getReader()->getInputConfiguration(& config);
1227
1228 env->SetIntField(configObj, gConfigurationClassInfo.touchscreen, config.touchScreen);
1229 env->SetIntField(configObj, gConfigurationClassInfo.keyboard, config.keyboard);
1230 env->SetIntField(configObj, gConfigurationClassInfo.navigation, config.navigation);
1231}
1232
Jeff Browne6504122010-09-27 14:52:15 -07001233static jboolean android_server_InputManager_nativeTransferTouchFocus(JNIEnv* env,
1234 jclass clazz, jobject fromChannelObj, jobject toChannelObj) {
1235 if (checkInputManagerUnitialized(env)) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001236 LOGD("input manager uninitialized; bailing");
Jeff Browne6504122010-09-27 14:52:15 -07001237 return false;
1238 }
1239
1240 sp<InputChannel> fromChannel =
1241 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1242 sp<InputChannel> toChannel =
1243 android_view_InputChannel_getInputChannel(env, toChannelObj);
1244
1245 if (fromChannel == NULL || toChannel == NULL) {
Christopher Tatea53146c2010-09-07 11:57:52 -07001246 LOGD("bailing because from=%p to=%p", fromChannel, toChannel);
Jeff Browne6504122010-09-27 14:52:15 -07001247 return false;
1248 }
1249
1250 return gNativeInputManager->getInputManager()->getDispatcher()->
1251 transferTouchFocus(fromChannel, toChannel);
1252}
1253
Jeff Browne33348b2010-07-15 23:54:05 -07001254static jstring android_server_InputManager_nativeDump(JNIEnv* env, jclass clazz) {
1255 if (checkInputManagerUnitialized(env)) {
1256 return NULL;
1257 }
1258
Jeff Brownb88102f2010-09-08 11:49:43 -07001259 String8 dump;
1260 gNativeInputManager->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001261 return env->NewStringUTF(dump.string());
1262}
1263
Jeff Brown9c3cda02010-06-15 01:31:58 -07001264// ----------------------------------------------------------------------------
1265
Jeff Brown46b9ac02010-04-22 18:58:52 -07001266static JNINativeMethod gInputManagerMethods[] = {
1267 /* name, signature, funcPtr */
1268 { "nativeInit", "(Lcom/android/server/InputManager$Callbacks;)V",
1269 (void*) android_server_InputManager_nativeInit },
1270 { "nativeStart", "()V",
1271 (void*) android_server_InputManager_nativeStart },
1272 { "nativeSetDisplaySize", "(III)V",
1273 (void*) android_server_InputManager_nativeSetDisplaySize },
1274 { "nativeSetDisplayOrientation", "(II)V",
1275 (void*) android_server_InputManager_nativeSetDisplayOrientation },
1276 { "nativeGetScanCodeState", "(III)I",
1277 (void*) android_server_InputManager_nativeGetScanCodeState },
1278 { "nativeGetKeyCodeState", "(III)I",
1279 (void*) android_server_InputManager_nativeGetKeyCodeState },
1280 { "nativeGetSwitchState", "(III)I",
1281 (void*) android_server_InputManager_nativeGetSwitchState },
Jeff Brown6d0fec22010-07-23 21:28:06 -07001282 { "nativeHasKeys", "(II[I[Z)Z",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001283 (void*) android_server_InputManager_nativeHasKeys },
Jeff Browna41ca772010-08-11 14:46:32 -07001284 { "nativeRegisterInputChannel", "(Landroid/view/InputChannel;Z)V",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001285 (void*) android_server_InputManager_nativeRegisterInputChannel },
1286 { "nativeUnregisterInputChannel", "(Landroid/view/InputChannel;)V",
Jeff Brown7fbdc842010-06-17 20:52:56 -07001287 (void*) android_server_InputManager_nativeUnregisterInputChannel },
Jeff Brown6ec402b2010-07-28 15:48:59 -07001288 { "nativeInjectInputEvent", "(Landroid/view/InputEvent;IIII)I",
1289 (void*) android_server_InputManager_nativeInjectInputEvent },
Jeff Brown349703e2010-06-22 01:27:15 -07001290 { "nativeSetInputWindows", "([Lcom/android/server/InputWindow;)V",
1291 (void*) android_server_InputManager_nativeSetInputWindows },
1292 { "nativeSetFocusedApplication", "(Lcom/android/server/InputApplication;)V",
1293 (void*) android_server_InputManager_nativeSetFocusedApplication },
1294 { "nativeSetInputDispatchMode", "(ZZ)V",
1295 (void*) android_server_InputManager_nativeSetInputDispatchMode },
Jeff Brown8d608662010-08-30 03:02:23 -07001296 { "nativeGetInputDevice", "(I)Landroid/view/InputDevice;",
1297 (void*) android_server_InputManager_nativeGetInputDevice },
1298 { "nativeGetInputDeviceIds", "()[I",
1299 (void*) android_server_InputManager_nativeGetInputDeviceIds },
Jeff Brown57c59372010-09-21 18:22:55 -07001300 { "nativeGetInputConfiguration", "(Landroid/content/res/Configuration;)V",
1301 (void*) android_server_InputManager_nativeGetInputConfiguration },
Jeff Browne6504122010-09-27 14:52:15 -07001302 { "nativeTransferTouchFocus", "(Landroid/view/InputChannel;Landroid/view/InputChannel;)Z",
1303 (void*) android_server_InputManager_nativeTransferTouchFocus },
Jeff Browne33348b2010-07-15 23:54:05 -07001304 { "nativeDump", "()Ljava/lang/String;",
1305 (void*) android_server_InputManager_nativeDump },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001306};
1307
1308#define FIND_CLASS(var, className) \
1309 var = env->FindClass(className); \
1310 LOG_FATAL_IF(! var, "Unable to find class " className); \
1311 var = jclass(env->NewGlobalRef(var));
1312
1313#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1314 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1315 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1316
1317#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1318 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1319 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1320
1321int register_android_server_InputManager(JNIEnv* env) {
1322 int res = jniRegisterNativeMethods(env, "com/android/server/InputManager",
1323 gInputManagerMethods, NELEM(gInputManagerMethods));
1324 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1325
Jeff Brown9c3cda02010-06-15 01:31:58 -07001326 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001327
1328 FIND_CLASS(gCallbacksClassInfo.clazz, "com/android/server/InputManager$Callbacks");
1329
Jeff Brown46b9ac02010-04-22 18:58:52 -07001330 GET_METHOD_ID(gCallbacksClassInfo.notifyConfigurationChanged, gCallbacksClassInfo.clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001331 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001332
1333 GET_METHOD_ID(gCallbacksClassInfo.notifyLidSwitchChanged, gCallbacksClassInfo.clazz,
1334 "notifyLidSwitchChanged", "(JZ)V");
1335
Jeff Brown7fbdc842010-06-17 20:52:56 -07001336 GET_METHOD_ID(gCallbacksClassInfo.notifyInputChannelBroken, gCallbacksClassInfo.clazz,
1337 "notifyInputChannelBroken", "(Landroid/view/InputChannel;)V");
1338
Jeff Brown349703e2010-06-22 01:27:15 -07001339 GET_METHOD_ID(gCallbacksClassInfo.notifyANR, gCallbacksClassInfo.clazz,
Jeff Brown519e0242010-09-15 15:18:56 -07001340 "notifyANR", "(Ljava/lang/Object;Landroid/view/InputChannel;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001341
Jeff Brown349703e2010-06-22 01:27:15 -07001342 GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeQueueing, gCallbacksClassInfo.clazz,
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001343 "interceptKeyBeforeQueueing", "(JIZIZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001344
1345 GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeDispatching, gCallbacksClassInfo.clazz,
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001346 "interceptKeyBeforeDispatching", "(Landroid/view/InputChannel;IIIIII)Z");
Jeff Brown349703e2010-06-22 01:27:15 -07001347
1348 GET_METHOD_ID(gCallbacksClassInfo.checkInjectEventsPermission, gCallbacksClassInfo.clazz,
1349 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001350
Jeff Brown46b9ac02010-04-22 18:58:52 -07001351 GET_METHOD_ID(gCallbacksClassInfo.filterTouchEvents, gCallbacksClassInfo.clazz,
1352 "filterTouchEvents", "()Z");
1353
1354 GET_METHOD_ID(gCallbacksClassInfo.filterJumpyTouchEvents, gCallbacksClassInfo.clazz,
1355 "filterJumpyTouchEvents", "()Z");
1356
1357 GET_METHOD_ID(gCallbacksClassInfo.getVirtualKeyDefinitions, gCallbacksClassInfo.clazz,
1358 "getVirtualKeyDefinitions",
1359 "(Ljava/lang/String;)[Lcom/android/server/InputManager$VirtualKeyDefinition;");
1360
Jeff Brown8d608662010-08-30 03:02:23 -07001361 GET_METHOD_ID(gCallbacksClassInfo.getInputDeviceCalibration, gCallbacksClassInfo.clazz,
1362 "getInputDeviceCalibration",
1363 "(Ljava/lang/String;)Lcom/android/server/InputManager$InputDeviceCalibration;");
1364
Jeff Brown46b9ac02010-04-22 18:58:52 -07001365 GET_METHOD_ID(gCallbacksClassInfo.getExcludedDeviceNames, gCallbacksClassInfo.clazz,
1366 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1367
Jeff Brownae9fc032010-08-18 15:51:08 -07001368 GET_METHOD_ID(gCallbacksClassInfo.getMaxEventsPerSecond, gCallbacksClassInfo.clazz,
1369 "getMaxEventsPerSecond", "()I");
1370
Jeff Brown46b9ac02010-04-22 18:58:52 -07001371 // VirtualKeyDefinition
1372
1373 FIND_CLASS(gVirtualKeyDefinitionClassInfo.clazz,
1374 "com/android/server/InputManager$VirtualKeyDefinition");
1375
1376 GET_FIELD_ID(gVirtualKeyDefinitionClassInfo.scanCode, gVirtualKeyDefinitionClassInfo.clazz,
1377 "scanCode", "I");
1378
1379 GET_FIELD_ID(gVirtualKeyDefinitionClassInfo.centerX, gVirtualKeyDefinitionClassInfo.clazz,
1380 "centerX", "I");
1381
1382 GET_FIELD_ID(gVirtualKeyDefinitionClassInfo.centerY, gVirtualKeyDefinitionClassInfo.clazz,
1383 "centerY", "I");
1384
1385 GET_FIELD_ID(gVirtualKeyDefinitionClassInfo.width, gVirtualKeyDefinitionClassInfo.clazz,
1386 "width", "I");
1387
1388 GET_FIELD_ID(gVirtualKeyDefinitionClassInfo.height, gVirtualKeyDefinitionClassInfo.clazz,
1389 "height", "I");
1390
Jeff Brown8d608662010-08-30 03:02:23 -07001391 // InputDeviceCalibration
1392
1393 FIND_CLASS(gInputDeviceCalibrationClassInfo.clazz,
1394 "com/android/server/InputManager$InputDeviceCalibration");
1395
1396 GET_FIELD_ID(gInputDeviceCalibrationClassInfo.keys, gInputDeviceCalibrationClassInfo.clazz,
1397 "keys", "[Ljava/lang/String;");
1398
1399 GET_FIELD_ID(gInputDeviceCalibrationClassInfo.values, gInputDeviceCalibrationClassInfo.clazz,
1400 "values", "[Ljava/lang/String;");
1401
Jeff Brown349703e2010-06-22 01:27:15 -07001402 // InputWindow
Jeff Brown7fbdc842010-06-17 20:52:56 -07001403
Jeff Brown349703e2010-06-22 01:27:15 -07001404 FIND_CLASS(gInputWindowClassInfo.clazz, "com/android/server/InputWindow");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001405
Jeff Brown349703e2010-06-22 01:27:15 -07001406 GET_FIELD_ID(gInputWindowClassInfo.inputChannel, gInputWindowClassInfo.clazz,
1407 "inputChannel", "Landroid/view/InputChannel;");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001408
Jeff Brown519e0242010-09-15 15:18:56 -07001409 GET_FIELD_ID(gInputWindowClassInfo.name, gInputWindowClassInfo.clazz,
1410 "name", "Ljava/lang/String;");
1411
Jeff Brown349703e2010-06-22 01:27:15 -07001412 GET_FIELD_ID(gInputWindowClassInfo.layoutParamsFlags, gInputWindowClassInfo.clazz,
1413 "layoutParamsFlags", "I");
1414
1415 GET_FIELD_ID(gInputWindowClassInfo.layoutParamsType, gInputWindowClassInfo.clazz,
1416 "layoutParamsType", "I");
1417
1418 GET_FIELD_ID(gInputWindowClassInfo.dispatchingTimeoutNanos, gInputWindowClassInfo.clazz,
1419 "dispatchingTimeoutNanos", "J");
1420
1421 GET_FIELD_ID(gInputWindowClassInfo.frameLeft, gInputWindowClassInfo.clazz,
1422 "frameLeft", "I");
1423
1424 GET_FIELD_ID(gInputWindowClassInfo.frameTop, gInputWindowClassInfo.clazz,
1425 "frameTop", "I");
1426
Jeff Brown85a31762010-09-01 17:01:00 -07001427 GET_FIELD_ID(gInputWindowClassInfo.frameRight, gInputWindowClassInfo.clazz,
1428 "frameRight", "I");
1429
1430 GET_FIELD_ID(gInputWindowClassInfo.frameBottom, gInputWindowClassInfo.clazz,
1431 "frameBottom", "I");
1432
1433 GET_FIELD_ID(gInputWindowClassInfo.visibleFrameLeft, gInputWindowClassInfo.clazz,
1434 "visibleFrameLeft", "I");
1435
1436 GET_FIELD_ID(gInputWindowClassInfo.visibleFrameTop, gInputWindowClassInfo.clazz,
1437 "visibleFrameTop", "I");
1438
1439 GET_FIELD_ID(gInputWindowClassInfo.visibleFrameRight, gInputWindowClassInfo.clazz,
1440 "visibleFrameRight", "I");
1441
1442 GET_FIELD_ID(gInputWindowClassInfo.visibleFrameBottom, gInputWindowClassInfo.clazz,
1443 "visibleFrameBottom", "I");
1444
Jeff Brown349703e2010-06-22 01:27:15 -07001445 GET_FIELD_ID(gInputWindowClassInfo.touchableAreaLeft, gInputWindowClassInfo.clazz,
1446 "touchableAreaLeft", "I");
1447
1448 GET_FIELD_ID(gInputWindowClassInfo.touchableAreaTop, gInputWindowClassInfo.clazz,
1449 "touchableAreaTop", "I");
1450
1451 GET_FIELD_ID(gInputWindowClassInfo.touchableAreaRight, gInputWindowClassInfo.clazz,
1452 "touchableAreaRight", "I");
1453
1454 GET_FIELD_ID(gInputWindowClassInfo.touchableAreaBottom, gInputWindowClassInfo.clazz,
1455 "touchableAreaBottom", "I");
1456
1457 GET_FIELD_ID(gInputWindowClassInfo.visible, gInputWindowClassInfo.clazz,
1458 "visible", "Z");
1459
Jeff Brown519e0242010-09-15 15:18:56 -07001460 GET_FIELD_ID(gInputWindowClassInfo.canReceiveKeys, gInputWindowClassInfo.clazz,
1461 "canReceiveKeys", "Z");
1462
Jeff Brown349703e2010-06-22 01:27:15 -07001463 GET_FIELD_ID(gInputWindowClassInfo.hasFocus, gInputWindowClassInfo.clazz,
1464 "hasFocus", "Z");
1465
1466 GET_FIELD_ID(gInputWindowClassInfo.hasWallpaper, gInputWindowClassInfo.clazz,
1467 "hasWallpaper", "Z");
1468
1469 GET_FIELD_ID(gInputWindowClassInfo.paused, gInputWindowClassInfo.clazz,
1470 "paused", "Z");
1471
Jeff Brown519e0242010-09-15 15:18:56 -07001472 GET_FIELD_ID(gInputWindowClassInfo.layer, gInputWindowClassInfo.clazz,
1473 "layer", "I");
1474
Jeff Brown349703e2010-06-22 01:27:15 -07001475 GET_FIELD_ID(gInputWindowClassInfo.ownerPid, gInputWindowClassInfo.clazz,
1476 "ownerPid", "I");
1477
1478 GET_FIELD_ID(gInputWindowClassInfo.ownerUid, gInputWindowClassInfo.clazz,
1479 "ownerUid", "I");
1480
1481 // InputApplication
1482
1483 FIND_CLASS(gInputApplicationClassInfo.clazz, "com/android/server/InputApplication");
1484
1485 GET_FIELD_ID(gInputApplicationClassInfo.name, gInputApplicationClassInfo.clazz,
1486 "name", "Ljava/lang/String;");
1487
1488 GET_FIELD_ID(gInputApplicationClassInfo.dispatchingTimeoutNanos,
1489 gInputApplicationClassInfo.clazz,
1490 "dispatchingTimeoutNanos", "J");
1491
1492 GET_FIELD_ID(gInputApplicationClassInfo.token, gInputApplicationClassInfo.clazz,
1493 "token", "Ljava/lang/Object;");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001494
Jeff Brown6ec402b2010-07-28 15:48:59 -07001495 // KeyEvent
1496
1497 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
1498
Jeff Brown8d608662010-08-30 03:02:23 -07001499 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001500
1501 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
1502
Jeff Brown8d608662010-08-30 03:02:23 -07001503 // InputDevice
1504
1505 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1506
1507 GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz,
1508 "<init>", "()V");
1509
1510 GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz,
1511 "addMotionRange", "(IFFFF)V");
1512
1513 GET_FIELD_ID(gInputDeviceClassInfo.mId, gInputDeviceClassInfo.clazz,
1514 "mId", "I");
1515
1516 GET_FIELD_ID(gInputDeviceClassInfo.mName, gInputDeviceClassInfo.clazz,
1517 "mName", "Ljava/lang/String;");
1518
1519 GET_FIELD_ID(gInputDeviceClassInfo.mSources, gInputDeviceClassInfo.clazz,
1520 "mSources", "I");
1521
1522 GET_FIELD_ID(gInputDeviceClassInfo.mKeyboardType, gInputDeviceClassInfo.clazz,
1523 "mKeyboardType", "I");
1524
1525 GET_FIELD_ID(gInputDeviceClassInfo.mMotionRanges, gInputDeviceClassInfo.clazz,
1526 "mMotionRanges", "[Landroid/view/InputDevice$MotionRange;");
1527
Jeff Brown57c59372010-09-21 18:22:55 -07001528 // Configuration
1529
1530 FIND_CLASS(gConfigurationClassInfo.clazz, "android/content/res/Configuration");
1531
1532 GET_FIELD_ID(gConfigurationClassInfo.touchscreen, gConfigurationClassInfo.clazz,
1533 "touchscreen", "I");
1534
1535 GET_FIELD_ID(gConfigurationClassInfo.keyboard, gConfigurationClassInfo.clazz,
1536 "keyboard", "I");
1537
1538 GET_FIELD_ID(gConfigurationClassInfo.navigation, gConfigurationClassInfo.clazz,
1539 "navigation", "I");
1540
Jeff Brown46b9ac02010-04-22 18:58:52 -07001541 return 0;
1542}
1543
Jeff Brown46b9ac02010-04-22 18:58:52 -07001544} /* namespace android */