blob: a4f4a0b21a4c9849b91b3e1e9b4b217c88a6fb76 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputManager-JNI"
18
Jeff Brown9c3cda02010-06-15 01:31:58 -070019//#define LOG_NDEBUG 0
20
21// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070022#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070023
24// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070026
Jeff Brown83c09682010-12-23 17:50:18 -080027
Jeff Brown46b9ac02010-04-22 18:58:52 -070028#include "JNIHelp.h"
29#include "jni.h"
Jeff Brown349703e2010-06-22 01:27:15 -070030#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070032#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080033
Jeff Brown46b9ac02010-04-22 18:58:52 -070034#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080035#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <utils/threads.h>
Jeff Brown83c09682010-12-23 17:50:18 -080037
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038#include <input/InputManager.h>
39#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070040#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080041
Jeff Brown05dc66a2011-03-02 14:41:58 -080042#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070043#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080044#include <android_view_KeyEvent.h>
45#include <android_view_MotionEvent.h>
46#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070047#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080048#include <android/graphics/GraphicsJNI.h>
49
Jeff Brown6ec6f792012-04-17 16:52:41 -070050#include <ScopedLocalRef.h>
51#include <ScopedUtfChars.h>
52
Jeff Brown4f8ecd82012-06-18 18:29:13 -070053#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070054#include "com_android_server_input_InputApplicationHandle.h"
55#include "com_android_server_input_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070056
57namespace android {
58
Jeff Brown1a84fd12011-06-02 01:26:32 -070059// The exponent used to calculate the pointer speed scaling factor.
60// The scaling factor is calculated as 2 ^ (speed * exponent),
61// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070062static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070063
Jeff Brown46b9ac02010-04-22 18:58:52 -070064static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070065 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070066 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070067 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070068 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070069 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070070 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070071 jmethodID interceptKeyBeforeQueueing;
Jeff Brown037c33e2014-04-09 00:31:55 -070072 jmethodID interceptWakeMotionBeforeQueueing;
Jeff Brown349703e2010-06-22 01:27:15 -070073 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070074 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070075 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080076 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070077 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080078 jmethodID getKeyRepeatTimeout;
79 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070080 jmethodID getHoverTapTimeout;
81 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070082 jmethodID getDoubleTapTimeout;
83 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080084 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080085 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070086 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070087 jmethodID getDeviceAlias;
Jeff Brown4532e612012-04-05 14:27:12 -070088} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070089
90static struct {
91 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070092} gInputDeviceClassInfo;
93
94static struct {
95 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070096} gKeyEventClassInfo;
97
98static struct {
99 jclass clazz;
100} gMotionEventClassInfo;
101
RoboErikca9eef62013-12-16 11:27:55 -0800102static struct {
103 jclass clazz;
104 jmethodID constructor;
105} gInputDeviceIdentifierInfo;
106
107
Jeff Brown928e0542011-01-10 11:17:36 -0800108
109// --- Global functions ---
110
Jeff Brown214eaf42011-05-26 19:17:02 -0700111template<typename T>
112inline static T min(const T& a, const T& b) {
113 return a < b ? a : b;
114}
115
116template<typename T>
117inline static T max(const T& a, const T& b) {
118 return a > b ? a : b;
119}
120
Jeff Brown928e0542011-01-10 11:17:36 -0800121static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
122 const sp<InputApplicationHandle>& inputApplicationHandle) {
123 if (inputApplicationHandle == NULL) {
124 return NULL;
125 }
126 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
127 getInputApplicationHandleObjLocalRef(env);
128}
129
130static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
131 const sp<InputWindowHandle>& inputWindowHandle) {
132 if (inputWindowHandle == NULL) {
133 return NULL;
134 }
135 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
136 getInputWindowHandleObjLocalRef(env);
137}
138
Jeff Brown2352b972011-04-12 22:39:53 -0700139static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
140 SpriteIcon* outSpriteIcon) {
141 PointerIcon pointerIcon;
142 status_t status = android_view_PointerIcon_loadSystemIcon(env,
143 contextObj, style, &pointerIcon);
144 if (!status) {
145 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
146 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
147 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
148 }
149}
150
Jeff Brown905805a2011-10-12 13:57:59 -0700151enum {
152 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700153};
154
Jeff Brown928e0542011-01-10 11:17:36 -0800155
156// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800157
Jeff Brown9c3cda02010-06-15 01:31:58 -0700158class NativeInputManager : public virtual RefBase,
159 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700160 public virtual InputDispatcherPolicyInterface,
161 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700162protected:
163 virtual ~NativeInputManager();
164
165public:
Jeff Brown4532e612012-04-05 14:27:12 -0700166 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700167
168 inline sp<InputManager> getInputManager() const { return mInputManager; }
169
Jeff Brownb88102f2010-09-08 11:49:43 -0700170 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700171
Jeff Brownd728bf52012-09-08 18:05:28 -0700172 void setDisplayViewport(bool external, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700173
Jeff Brown7fbdc842010-06-17 20:52:56 -0700174 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800175 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700176 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
177
Jeff Brown9302c872011-07-13 22:51:29 -0700178 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
179 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700180 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800181 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700182 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700183 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700184 void setInteractive(bool interactive);
Jeff Brown349703e2010-06-22 01:27:15 -0700185
Jeff Brown9c3cda02010-06-15 01:31:58 -0700186 /* --- InputReaderPolicyInterface implementation --- */
187
Jeff Brown214eaf42011-05-26 19:17:02 -0700188 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800189 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700190 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikca9eef62013-12-16 11:27:55 -0800191 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700192 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700193
194 /* --- InputDispatcherPolicyInterface implementation --- */
195
Jeff Brownbcc046a2012-09-27 20:46:43 -0700196 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700197 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700198 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700199 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700200 const sp<InputWindowHandle>& inputWindowHandle,
201 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800202 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700203 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700204 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800205 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800206 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700207 virtual nsecs_t interceptKeyBeforeDispatching(
208 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700209 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800210 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800211 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700212 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700213 virtual bool checkInjectEventsPermissionNonReentrant(
214 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700215
Jeff Brown2352b972011-04-12 22:39:53 -0700216 /* --- PointerControllerPolicyInterface implementation --- */
217
218 virtual void loadPointerResources(PointerResources* outResources);
219
Jeff Brown9c3cda02010-06-15 01:31:58 -0700220private:
221 sp<InputManager> mInputManager;
222
Jeff Brown2352b972011-04-12 22:39:53 -0700223 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700224 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800225 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700226
Jeff Brown83c09682010-12-23 17:50:18 -0800227 Mutex mLock;
228 struct Locked {
229 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700230 DisplayViewport internalViewport;
231 DisplayViewport externalViewport;
Jeff Brown83c09682010-12-23 17:50:18 -0800232
Jeff Brown05dc66a2011-03-02 14:41:58 -0800233 // System UI visibility.
234 int32_t systemUiVisibility;
235
Jeff Brown1a84fd12011-06-02 01:26:32 -0700236 // Pointer speed.
237 int32_t pointerSpeed;
238
Jeff Brown474dcb52011-06-14 20:22:50 -0700239 // True if pointer gestures are enabled.
240 bool pointerGesturesEnabled;
241
Jeff Browndaf4a122011-08-26 17:14:14 -0700242 // Show touches feature enable/disable.
243 bool showTouches;
244
Jeff Brown5541de92011-04-11 11:54:25 -0700245 // Sprite controller singleton, created on first use.
246 sp<SpriteController> spriteController;
247
Jeff Brown83c09682010-12-23 17:50:18 -0800248 // Pointer controller singleton, created and destroyed as needed.
249 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800250 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700251
Jeff Brown037c33e2014-04-09 00:31:55 -0700252 volatile bool mInteractive;
253
Jeff Brown2352b972011-04-12 22:39:53 -0700254 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800255 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700256 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800257
Jeff Brownb88102f2010-09-08 11:49:43 -0700258 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700259
Jeff Brown9c3cda02010-06-15 01:31:58 -0700260 static inline JNIEnv* jniEnv() {
261 return AndroidRuntime::getJNIEnv();
262 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700263};
264
Jeff Brown928e0542011-01-10 11:17:36 -0800265
Jeff Brown9c3cda02010-06-15 01:31:58 -0700266
Jeff Brown2352b972011-04-12 22:39:53 -0700267NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700268 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700269 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270 JNIEnv* env = jniEnv();
271
Jeff Brown2352b972011-04-12 22:39:53 -0700272 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700273 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700274
Jeff Brown83c09682010-12-23 17:50:18 -0800275 {
276 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800277 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700278 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700279 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700280 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800281 }
282
Jeff Brown9c3cda02010-06-15 01:31:58 -0700283 sp<EventHub> eventHub = new EventHub();
284 mInputManager = new InputManager(eventHub, this, this);
285}
286
287NativeInputManager::~NativeInputManager() {
288 JNIEnv* env = jniEnv();
289
Jeff Brown2352b972011-04-12 22:39:53 -0700290 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700291 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700292}
293
Jeff Brownb88102f2010-09-08 11:49:43 -0700294void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700295 mInputManager->getReader()->dump(dump);
296 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700297
Jeff Brownb88102f2010-09-08 11:49:43 -0700298 mInputManager->getDispatcher()->dump(dump);
299 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700300}
301
Jeff Brown7fbdc842010-06-17 20:52:56 -0700302bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700303 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000304 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700305 LOGE_EX(env);
306 env->ExceptionClear();
307 return true;
308 }
309 return false;
310}
311
Jeff Brownd728bf52012-09-08 18:05:28 -0700312void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700313 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700314 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700315 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700316
Jeff Brownd728bf52012-09-08 18:05:28 -0700317 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
318 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700319 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700320 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700321
Jeff Brownd728bf52012-09-08 18:05:28 -0700322 if (!external) {
323 sp<PointerController> controller = mLocked.pointerController.promote();
324 if (controller != NULL) {
325 controller->setDisplayViewport(
326 viewport.logicalRight - viewport.logicalLeft,
327 viewport.logicalBottom - viewport.logicalTop,
328 viewport.orientation);
329 }
Jeff Brown2352b972011-04-12 22:39:53 -0700330 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700331 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700332 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700333
334 if (changed) {
335 mInputManager->getReader()->requestRefreshConfiguration(
336 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
337 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700338}
339
Jeff Brown7fbdc842010-06-17 20:52:56 -0700340status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Brown928e0542011-01-10 11:17:36 -0800341 const sp<InputChannel>& inputChannel,
342 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
343 return mInputManager->getDispatcher()->registerInputChannel(
344 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700345}
346
347status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
348 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700349 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700350}
351
Jeff Brown214eaf42011-05-26 19:17:02 -0700352void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700353 JNIEnv* env = jniEnv();
354
Jeff Brown4532e612012-04-05 14:27:12 -0700355 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
356 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700357 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
358 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
359 }
360
361 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700362 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
363 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700364 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
365 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700366 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700367 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700368 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700369 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700370 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700371 env->DeleteLocalRef(item);
372 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700373 env->DeleteLocalRef(excludedDeviceNames);
374 }
375
Jeff Brown4532e612012-04-05 14:27:12 -0700376 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
377 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700378 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700379 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
380 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700381 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700382 jint longPressTimeout = env->CallIntMethod(mServiceObj,
383 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700384 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700385 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700386
387 // We must ensure that the tap-drag interval is significantly shorter than
388 // the long-press timeout because the tap is held down for the entire duration
389 // of the double-tap timeout.
390 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700391 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700392 outConfig->pointerGestureTapDragInterval =
393 milliseconds_to_nanoseconds(tapDragInterval);
394 }
395 }
396 }
397
Jeff Brown4532e612012-04-05 14:27:12 -0700398 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
399 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700400 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
401 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700402 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700403
404 { // acquire lock
405 AutoMutex _l(mLock);
406
407 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
408 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700409 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700410
Jeff Browndaf4a122011-08-26 17:14:14 -0700411 outConfig->showTouches = mLocked.showTouches;
412
Jeff Brownd728bf52012-09-08 18:05:28 -0700413 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
414 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700415 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700416}
417
Jeff Brown83c09682010-12-23 17:50:18 -0800418sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
419 AutoMutex _l(mLock);
420
421 sp<PointerController> controller = mLocked.pointerController.promote();
422 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700423 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800424
Jeff Brown2352b972011-04-12 22:39:53 -0700425 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800426 mLocked.pointerController = controller;
427
Jeff Brownd728bf52012-09-08 18:05:28 -0700428 DisplayViewport& v = mLocked.internalViewport;
429 controller->setDisplayViewport(
430 v.logicalRight - v.logicalLeft,
431 v.logicalBottom - v.logicalTop,
432 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800433
Jeff Brown5541de92011-04-11 11:54:25 -0700434 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700435 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
436 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700437 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
438 PointerIcon pointerIcon;
439 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
440 mContextObj, &pointerIcon);
441 if (!status && !pointerIcon.isNullIcon()) {
442 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
443 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
444 } else {
445 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800446 }
Jeff Brown2352b972011-04-12 22:39:53 -0700447 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800448 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800449
Jeff Brown2352b972011-04-12 22:39:53 -0700450 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800451 }
452 return controller;
453}
454
Jeff Brown5541de92011-04-11 11:54:25 -0700455void NativeInputManager::ensureSpriteControllerLocked() {
456 if (mLocked.spriteController == NULL) {
457 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700458 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700459 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
460 layer = -1;
461 }
462 mLocked.spriteController = new SpriteController(mLooper, layer);
463 }
464}
465
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700466void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
467 JNIEnv* env = jniEnv();
468
469 size_t count = inputDevices.size();
470 jobjectArray inputDevicesObjArray = env->NewObjectArray(
471 count, gInputDeviceClassInfo.clazz, NULL);
472 if (inputDevicesObjArray) {
473 bool error = false;
474 for (size_t i = 0; i < count; i++) {
475 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
476 if (!inputDeviceObj) {
477 error = true;
478 break;
479 }
480
481 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
482 env->DeleteLocalRef(inputDeviceObj);
483 }
484
485 if (!error) {
486 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
487 inputDevicesObjArray);
488 }
489
490 env->DeleteLocalRef(inputDevicesObjArray);
491 }
492
493 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
494}
495
Jeff Brown6ec6f792012-04-17 16:52:41 -0700496sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikca9eef62013-12-16 11:27:55 -0800497 const InputDeviceIdentifier& identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700498 JNIEnv* env = jniEnv();
499
500 sp<KeyCharacterMap> result;
RoboErikca9eef62013-12-16 11:27:55 -0800501 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
502 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
503 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
504 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700505 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikca9eef62013-12-16 11:27:55 -0800506 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700507 if (arrayObj.get()) {
508 ScopedLocalRef<jstring> filenameObj(env,
509 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
510 ScopedLocalRef<jstring> contentsObj(env,
511 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
512 ScopedUtfChars filenameChars(env, filenameObj.get());
513 ScopedUtfChars contentsChars(env, contentsObj.get());
514
515 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
516 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
517 }
518 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
519 return result;
520}
521
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700522String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
523 JNIEnv* env = jniEnv();
524
525 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
526 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
527 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
528 String8 result;
529 if (aliasObj.get()) {
530 ScopedUtfChars aliasChars(env, aliasObj.get());
531 result.setTo(aliasChars.c_str());
532 }
533 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
534 return result;
535}
536
Jeff Brownbcc046a2012-09-27 20:46:43 -0700537void NativeInputManager::notifySwitch(nsecs_t when,
538 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700539#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700540 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
541 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700542#endif
543
544 JNIEnv* env = jniEnv();
545
Jeff Brown53384282012-08-20 20:16:01 -0700546 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700547 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700548 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700549}
550
Jeff Brown9c3cda02010-06-15 01:31:58 -0700551void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
552#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000553 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700554#endif
555
556 JNIEnv* env = jniEnv();
557
Jeff Brown4532e612012-04-05 14:27:12 -0700558 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700559 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700560}
561
Jeff Brown519e0242010-09-15 15:18:56 -0700562nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700563 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700564#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000565 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700566#endif
567
568 JNIEnv* env = jniEnv();
569
Jeff Brown928e0542011-01-10 11:17:36 -0800570 jobject inputApplicationHandleObj =
571 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
572 jobject inputWindowHandleObj =
573 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700574 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700575
Jeff Brown4532e612012-04-05 14:27:12 -0700576 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700577 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
578 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700579 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
580 newTimeout = 0; // abort dispatch
581 } else {
582 assert(newTimeout >= 0);
583 }
584
Jeff Brownbd181bb2013-09-10 16:44:24 -0700585 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800586 env->DeleteLocalRef(inputWindowHandleObj);
587 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700588 return newTimeout;
589}
590
Jeff Brown928e0542011-01-10 11:17:36 -0800591void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700592#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000593 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700594#endif
595
Jeff Brown7fbdc842010-06-17 20:52:56 -0700596 JNIEnv* env = jniEnv();
597
Jeff Brown928e0542011-01-10 11:17:36 -0800598 jobject inputWindowHandleObj =
599 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
600 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700601 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800602 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700603 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
604
Jeff Brown928e0542011-01-10 11:17:36 -0800605 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700606 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700607}
608
Jeff Brown214eaf42011-05-26 19:17:02 -0700609void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
610 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800611
Jeff Brown4532e612012-04-05 14:27:12 -0700612 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
613 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700614 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
615 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
616 }
Jeff Browna4547672011-03-02 21:38:11 -0800617
Jeff Brown4532e612012-04-05 14:27:12 -0700618 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
619 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700620 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
621 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
622 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700623}
624
Jeff Brown9302c872011-07-13 22:51:29 -0700625void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
626 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700627
Jeff Brown9302c872011-07-13 22:51:29 -0700628 if (windowHandleObjArray) {
629 jsize length = env->GetArrayLength(windowHandleObjArray);
630 for (jsize i = 0; i < length; i++) {
631 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
632 if (! windowHandleObj) {
633 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700634 }
Jeff Brown9302c872011-07-13 22:51:29 -0700635
636 sp<InputWindowHandle> windowHandle =
637 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
638 if (windowHandle != NULL) {
639 windowHandles.push(windowHandle);
640 }
641 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700642 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700643 }
Jeff Brown349703e2010-06-22 01:27:15 -0700644
Jeff Brown9302c872011-07-13 22:51:29 -0700645 mInputManager->getDispatcher()->setInputWindows(windowHandles);
646
647 // Do this after the dispatcher has updated the window handle state.
648 bool newPointerGesturesEnabled = true;
649 size_t numWindows = windowHandles.size();
650 for (size_t i = 0; i < numWindows; i++) {
651 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700652 const InputWindowInfo* windowInfo = windowHandle->getInfo();
653 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
654 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700655 newPointerGesturesEnabled = false;
656 }
657 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700658
659 uint32_t changes = 0;
660 { // acquire lock
661 AutoMutex _l(mLock);
662
663 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
664 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
665 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
666 }
667 } // release lock
668
669 if (changes) {
670 mInputManager->getReader()->requestRefreshConfiguration(changes);
671 }
Jeff Brown349703e2010-06-22 01:27:15 -0700672}
673
Jeff Brown9302c872011-07-13 22:51:29 -0700674void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
675 sp<InputApplicationHandle> applicationHandle =
676 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
677 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700678}
679
680void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700681 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700682}
683
Jeff Brown05dc66a2011-03-02 14:41:58 -0800684void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
685 AutoMutex _l(mLock);
686
687 if (mLocked.systemUiVisibility != visibility) {
688 mLocked.systemUiVisibility = visibility;
689
690 sp<PointerController> controller = mLocked.pointerController.promote();
691 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700692 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800693 }
694 }
695}
696
Jeff Brown2352b972011-04-12 22:39:53 -0700697void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800698 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700699 controller->setInactivityTimeout(lightsOut
700 ? PointerController::INACTIVITY_TIMEOUT_SHORT
701 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800702}
703
Jeff Brown1a84fd12011-06-02 01:26:32 -0700704void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700705 { // acquire lock
706 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700707
Jeff Brown474dcb52011-06-14 20:22:50 -0700708 if (mLocked.pointerSpeed == speed) {
709 return;
710 }
711
Steve Block6215d3f2012-01-04 20:05:49 +0000712 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700713 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700714 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700715
Jeff Brown474dcb52011-06-14 20:22:50 -0700716 mInputManager->getReader()->requestRefreshConfiguration(
717 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700718}
719
Jeff Browndaf4a122011-08-26 17:14:14 -0700720void NativeInputManager::setShowTouches(bool enabled) {
721 { // acquire lock
722 AutoMutex _l(mLock);
723
724 if (mLocked.showTouches == enabled) {
725 return;
726 }
727
Steve Block6215d3f2012-01-04 20:05:49 +0000728 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700729 mLocked.showTouches = enabled;
730 } // release lock
731
732 mInputManager->getReader()->requestRefreshConfiguration(
733 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
734}
735
Jeff Brown037c33e2014-04-09 00:31:55 -0700736void NativeInputManager::setInteractive(bool interactive) {
737 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700738}
739
Jeff Brown0029c662011-03-30 02:25:18 -0700740bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
741 jobject inputEventObj;
742
743 JNIEnv* env = jniEnv();
744 switch (inputEvent->getType()) {
745 case AINPUT_EVENT_TYPE_KEY:
746 inputEventObj = android_view_KeyEvent_fromNative(env,
747 static_cast<const KeyEvent*>(inputEvent));
748 break;
749 case AINPUT_EVENT_TYPE_MOTION:
750 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
751 static_cast<const MotionEvent*>(inputEvent));
752 break;
753 default:
754 return true; // dispatch the event normally
755 }
756
757 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000758 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700759 return true; // dispatch the event normally
760 }
761
762 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700763 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700764 inputEventObj, policyFlags);
765 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
766 pass = true;
767 }
768 env->DeleteLocalRef(inputEventObj);
769 return pass;
770}
771
Jeff Brown1f245102010-11-18 20:53:46 -0800772void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
773 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700774 // Policy:
775 // - Ignore untrusted events and pass them along.
776 // - Ask the window manager what to do with normal events and trusted injected events.
777 // - For normal events wake and brighten the screen if currently off or dim.
Jeff Brown037c33e2014-04-09 00:31:55 -0700778 if (mInteractive) {
779 policyFlags |= POLICY_FLAG_INTERACTIVE;
780 }
Jeff Brown3122e442010-10-11 23:32:49 -0700781 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800782 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700783 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800784 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
785 jint wmActions;
786 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700787 wmActions = env->CallIntMethod(mServiceObj,
788 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700789 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800790 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
791 wmActions = 0;
792 }
793 android_view_KeyEvent_recycle(env, keyEventObj);
794 env->DeleteLocalRef(keyEventObj);
795 } else {
Steve Block3762c312012-01-06 19:20:56 +0000796 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700797 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700798 }
799
Jeff Brown56194eb2011-03-02 19:23:13 -0800800 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700801 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700802 policyFlags |= POLICY_FLAG_PASS_TO_USER;
803 }
804}
805
Jeff Brown56194eb2011-03-02 19:23:13 -0800806void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700807 // Policy:
808 // - Ignore untrusted events and pass them along.
809 // - No special filtering for injected events required at this time.
810 // - Filter normal events based on screen state.
811 // - For normal events brighten (but do not wake) the screen if currently dim.
Jeff Brown037c33e2014-04-09 00:31:55 -0700812 if (mInteractive) {
813 policyFlags |= POLICY_FLAG_INTERACTIVE;
814 }
Jeff Brown3122e442010-10-11 23:32:49 -0700815 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700816 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -0700817 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Brown037c33e2014-04-09 00:31:55 -0700818 } else if (policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED)) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800819 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700820 jint wmActions = env->CallIntMethod(mServiceObj,
Jeff Brown037c33e2014-04-09 00:31:55 -0700821 gServiceClassInfo.interceptWakeMotionBeforeQueueing,
Jeff Brown26875502014-01-30 21:47:47 -0800822 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800823 if (checkAndClearExceptionFromCallback(env,
Jeff Brown037c33e2014-04-09 00:31:55 -0700824 "interceptWakeMotionBeforeQueueing")) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800825 wmActions = 0;
826 }
827
Jeff Brown56194eb2011-03-02 19:23:13 -0800828 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700829 }
Jeff Brown3122e442010-10-11 23:32:49 -0700830 } else {
831 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700832 }
833}
834
Jeff Brown56194eb2011-03-02 19:23:13 -0800835void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
836 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800837 if (wmActions & WM_ACTION_PASS_TO_USER) {
838 policyFlags |= POLICY_FLAG_PASS_TO_USER;
839 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800840#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000841 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800842#endif
843 }
844}
845
Jeff Brown905805a2011-10-12 13:57:59 -0700846nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800847 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700848 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700849 // Policy:
850 // - Ignore untrusted events and pass them along.
851 // - Filter normal events and trusted injected events through the window manager policy to
852 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700853 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700854 if (policyFlags & POLICY_FLAG_TRUSTED) {
855 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700856
Jeff Brown928e0542011-01-10 11:17:36 -0800857 // Note: inputWindowHandle may be null.
858 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800859 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
860 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700861 jlong delayMillis = env->CallLongMethod(mServiceObj,
862 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800863 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800864 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
865 android_view_KeyEvent_recycle(env, keyEventObj);
866 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700867 if (!error) {
868 if (delayMillis < 0) {
869 result = -1;
870 } else if (delayMillis > 0) {
871 result = milliseconds_to_nanoseconds(delayMillis);
872 }
873 }
Jeff Brown1f245102010-11-18 20:53:46 -0800874 } else {
Steve Block3762c312012-01-06 19:20:56 +0000875 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800876 }
Jeff Brown928e0542011-01-10 11:17:36 -0800877 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700878 }
Jeff Brown1f245102010-11-18 20:53:46 -0800879 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700880}
881
Jeff Brown928e0542011-01-10 11:17:36 -0800882bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800883 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700884 // Policy:
885 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800886 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700887 if (policyFlags & POLICY_FLAG_TRUSTED) {
888 JNIEnv* env = jniEnv();
889
Jeff Brown928e0542011-01-10 11:17:36 -0800890 // Note: inputWindowHandle may be null.
891 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800892 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
893 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700894 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
895 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800896 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700897 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
898 fallbackKeyEventObj = NULL;
899 }
Jeff Brown1f245102010-11-18 20:53:46 -0800900 android_view_KeyEvent_recycle(env, keyEventObj);
901 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800902
903 if (fallbackKeyEventObj) {
904 // Note: outFallbackKeyEvent may be the same object as keyEvent.
905 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
906 outFallbackKeyEvent)) {
907 result = true;
908 }
909 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
910 env->DeleteLocalRef(fallbackKeyEventObj);
911 }
Jeff Brown1f245102010-11-18 20:53:46 -0800912 } else {
Steve Block3762c312012-01-06 19:20:56 +0000913 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800914 }
Jeff Brown928e0542011-01-10 11:17:36 -0800915 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700916 }
Jeff Brown1f245102010-11-18 20:53:46 -0800917 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -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();
Jeff Brown4532e612012-04-05 14:27:12 -0700928 jboolean result = env->CallBooleanMethod(mServiceObj,
929 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700930 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
931 result = false;
932 }
Jeff Brown349703e2010-06-22 01:27:15 -0700933 return result;
934}
935
Jeff Brown2352b972011-04-12 22:39:53 -0700936void NativeInputManager::loadPointerResources(PointerResources* outResources) {
937 JNIEnv* env = jniEnv();
938
939 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
940 &outResources->spotHover);
941 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
942 &outResources->spotTouch);
943 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
944 &outResources->spotAnchor);
945}
946
Jeff Brown83c09682010-12-23 17:50:18 -0800947
Jeff Brown9c3cda02010-06-15 01:31:58 -0700948// ----------------------------------------------------------------------------
949
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000950static jlong nativeInit(JNIEnv* env, jclass clazz,
Jeff Brown4532e612012-04-05 14:27:12 -0700951 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -0700952 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -0800953 if (messageQueue == NULL) {
954 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
955 return 0;
956 }
957
Jeff Brown603b4452012-04-06 17:39:41 -0700958 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
959 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800960 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000961 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700962}
963
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000964static void nativeStart(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -0700965 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700966
Jeff Brown4532e612012-04-05 14:27:12 -0700967 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700968 if (result) {
969 jniThrowRuntimeException(env, "Input manager could not be started.");
970 }
971}
972
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000973static void nativeSetDisplayViewport(JNIEnv* env, jclass clazz, jlong ptr, jboolean external,
Jeff Brownd728bf52012-09-08 18:05:28 -0700974 jint displayId, jint orientation,
975 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -0700976 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
977 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -0700978 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700979
Jeff Brownd728bf52012-09-08 18:05:28 -0700980 DisplayViewport v;
981 v.displayId = displayId;
982 v.orientation = orientation;
983 v.logicalLeft = logicalLeft;
984 v.logicalTop = logicalTop;
985 v.logicalRight = logicalRight;
986 v.logicalBottom = logicalBottom;
987 v.physicalLeft = physicalLeft;
988 v.physicalTop = physicalTop;
989 v.physicalRight = physicalRight;
990 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -0700991 v.deviceWidth = deviceWidth;
992 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -0700993 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700994}
995
Jeff Brown4532e612012-04-05 14:27:12 -0700996static jint nativeGetScanCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000997 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700998 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700999
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001000 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001001 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001002}
1003
Jeff Brown4532e612012-04-05 14:27:12 -07001004static jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001005 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001006 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001007
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001008 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001009 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001010}
1011
Jeff Brown4532e612012-04-05 14:27:12 -07001012static jint nativeGetSwitchState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001013 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001014 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001015
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001016 return (jint) im->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
Jeff Brown4532e612012-04-05 14:27:12 -07001020static jboolean nativeHasKeys(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001021 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001022 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001023
1024 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1025 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1026 jsize numCodes = env->GetArrayLength(keyCodes);
1027 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001028 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001029 if (im->getInputManager()->getReader()->hasKeys(
1030 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1031 result = JNI_TRUE;
1032 } else {
1033 result = JNI_FALSE;
1034 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001035 } else {
1036 result = JNI_FALSE;
1037 }
1038
1039 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1040 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1041 return result;
1042}
1043
1044static void throwInputChannelNotInitialized(JNIEnv* env) {
1045 jniThrowException(env, "java/lang/IllegalStateException",
1046 "inputChannel is not initialized");
1047}
1048
Jeff Brown4532e612012-04-05 14:27:12 -07001049static void handleInputChannelDisposed(JNIEnv* env,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001050 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001051 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1052
Steve Block8564c8d2012-01-05 23:22:43 +00001053 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001054 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001055 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001056}
1057
Jeff Brown4532e612012-04-05 14:27:12 -07001058static void nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001059 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001060 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001061
1062 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1063 inputChannelObj);
1064 if (inputChannel == NULL) {
1065 throwInputChannelNotInitialized(env);
1066 return;
1067 }
1068
Jeff Brown928e0542011-01-10 11:17:36 -08001069 sp<InputWindowHandle> inputWindowHandle =
1070 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001071
Jeff Brown4532e612012-04-05 14:27:12 -07001072 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001073 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001074 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001075 String8 message;
1076 message.appendFormat("Failed to register input channel. status=%d", status);
1077 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001078 return;
1079 }
1080
Jeff Browna41ca772010-08-11 14:46:32 -07001081 if (! monitor) {
1082 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001083 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001084 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001085}
1086
Jeff Brown4532e612012-04-05 14:27:12 -07001087static void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001088 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001089 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001090
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 Brown4532e612012-04-05 14:27:12 -07001100 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001101 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1102 String8 message;
1103 message.appendFormat("Failed to unregister input channel. status=%d", status);
1104 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001105 }
1106}
1107
Jeff Brown4532e612012-04-05 14:27:12 -07001108static void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001109 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001110 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001111
Jeff Brown4532e612012-04-05 14:27:12 -07001112 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001113}
1114
Jeff Brown4532e612012-04-05 14:27:12 -07001115static jint nativeInjectInputEvent(JNIEnv* env, jclass clazz,
Jeff Brown38f96e52014-02-11 14:32:56 -08001116 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001117 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001118 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001119
Jeff Brown6ec402b2010-07-28 15:48:59 -07001120 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1121 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001122 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1123 if (status) {
1124 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1125 return INPUT_EVENT_INJECTION_FAILED;
1126 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001127
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001128 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown38f96e52014-02-11 14:32:56 -08001129 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001130 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001131 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001132 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1133 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001134 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1135 return INPUT_EVENT_INJECTION_FAILED;
1136 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001137
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001138 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown38f96e52014-02-11 14:32:56 -08001139 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001140 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001141 } else {
1142 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001143 return INPUT_EVENT_INJECTION_FAILED;
1144 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001145}
1146
Jeff Brown4532e612012-04-05 14:27:12 -07001147static void nativeSetInputWindows(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001148 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001149 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001150
Jeff Brown4532e612012-04-05 14:27:12 -07001151 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001152}
1153
Jeff Brown4532e612012-04-05 14:27:12 -07001154static void nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001155 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001156 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001157
Jeff Brown4532e612012-04-05 14:27:12 -07001158 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001159}
1160
Jeff Brown4532e612012-04-05 14:27:12 -07001161static void nativeSetInputDispatchMode(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001162 jclass clazz, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001163 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001164
Jeff Brown4532e612012-04-05 14:27:12 -07001165 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001166}
1167
Jeff Brown4532e612012-04-05 14:27:12 -07001168static void nativeSetSystemUiVisibility(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001169 jclass clazz, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001170 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001171
Jeff Brown4532e612012-04-05 14:27:12 -07001172 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001173}
1174
Jeff Brown4532e612012-04-05 14:27:12 -07001175static jboolean nativeTransferTouchFocus(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001176 jclass clazz, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001177 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001178
1179 sp<InputChannel> fromChannel =
1180 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1181 sp<InputChannel> toChannel =
1182 android_view_InputChannel_getInputChannel(env, toChannelObj);
1183
1184 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001185 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001186 }
1187
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001188 if (im->getInputManager()->getDispatcher()->
1189 transferTouchFocus(fromChannel, toChannel)) {
1190 return JNI_TRUE;
1191 } else {
1192 return JNI_FALSE;
1193 }
Jeff Browne6504122010-09-27 14:52:15 -07001194}
1195
Jeff Brown4532e612012-04-05 14:27:12 -07001196static void nativeSetPointerSpeed(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001197 jclass clazz, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001198 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001199
Jeff Brown4532e612012-04-05 14:27:12 -07001200 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001201}
1202
Jeff Brown4532e612012-04-05 14:27:12 -07001203static void nativeSetShowTouches(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001204 jclass clazz, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001205 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001206
Jeff Brown4532e612012-04-05 14:27:12 -07001207 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001208}
1209
Jeff Brown037c33e2014-04-09 00:31:55 -07001210static void nativeSetInteractive(JNIEnv* env,
1211 jclass clazz, jlong ptr, jboolean interactive) {
1212 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1213
1214 im->setInteractive(interactive);
1215}
1216
Jeff Browna47425a2012-04-13 04:09:27 -07001217static void nativeVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001218 jclass clazz, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001219 jint repeat, jint token) {
1220 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1221
1222 size_t patternSize = env->GetArrayLength(patternObj);
1223 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
1224 ALOGI("Skipped requested vibration because the pattern size is %d "
1225 "which is more than the maximum supported size of %d.",
1226 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1227 return; // limit to reasonable size
1228 }
1229
1230 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1231 patternObj, NULL));
1232 nsecs_t pattern[patternSize];
1233 for (size_t i = 0; i < patternSize; i++) {
1234 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001235 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001236 }
1237 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1238
1239 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1240}
1241
1242static void nativeCancelVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001243 jclass clazz, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001244 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1245
1246 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1247}
1248
Jeff Brown6ec6f792012-04-17 16:52:41 -07001249static void nativeReloadKeyboardLayouts(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001250 jclass clazz, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001251 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1252
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001253 im->getInputManager()->getReader()->requestRefreshConfiguration(
1254 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1255}
1256
1257static void nativeReloadDeviceAliases(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001258 jclass clazz, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001259 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1260
1261 im->getInputManager()->getReader()->requestRefreshConfiguration(
1262 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001263}
1264
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001265static jstring nativeDump(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001266 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001267
Jeff Brownb88102f2010-09-08 11:49:43 -07001268 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001269 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001270 return env->NewStringUTF(dump.string());
1271}
1272
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001273static void nativeMonitor(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001274 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001275
Jeff Brown4532e612012-04-05 14:27:12 -07001276 im->getInputManager()->getReader()->monitor();
1277 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001278}
1279
Jeff Brown9c3cda02010-06-15 01:31:58 -07001280// ----------------------------------------------------------------------------
1281
Jeff Brown46b9ac02010-04-22 18:58:52 -07001282static JNINativeMethod gInputManagerMethods[] = {
1283 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001284 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001285 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001286 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001287 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001288 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001289 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001290 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001291 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001292 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001293 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001294 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001295 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001296 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001297 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001298 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001299 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001300 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001301 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001302 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001303 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001304 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001305 (void*) nativeSetInputFilterEnabled },
Jeff Brown38f96e52014-02-11 14:32:56 -08001306 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001307 (void*) nativeInjectInputEvent },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001308 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001309 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001310 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001311 (void*) nativeSetFocusedApplication },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001312 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001313 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001314 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001315 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001316 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001317 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001318 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001319 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001320 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001321 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001322 { "nativeSetInteractive", "(JZ)V",
1323 (void*) nativeSetInteractive },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001324 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001325 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001326 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001327 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001328 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001329 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001330 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001331 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001332 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001333 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001334 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001335 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001336};
1337
1338#define FIND_CLASS(var, className) \
1339 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001340 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001341
1342#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1343 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1344 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1345
1346#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1347 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1348 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1349
1350int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001351 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001352 gInputManagerMethods, NELEM(gInputManagerMethods));
1353 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1354
Jeff Brown9c3cda02010-06-15 01:31:58 -07001355 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001356
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001357 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001358 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001359
Jeff Brown4532e612012-04-05 14:27:12 -07001360 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001361 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001362
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001363 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1364 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1365
Jeff Brown53384282012-08-20 20:16:01 -07001366 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1367 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001368
Jeff Brown4532e612012-04-05 14:27:12 -07001369 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1370 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001371
Jeff Brown4532e612012-04-05 14:27:12 -07001372 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001373 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001374 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001375
Jeff Brown4532e612012-04-05 14:27:12 -07001376 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001377 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1378
Jeff Brown4532e612012-04-05 14:27:12 -07001379 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001380 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001381
Jeff Brown037c33e2014-04-09 00:31:55 -07001382 GET_METHOD_ID(gServiceClassInfo.interceptWakeMotionBeforeQueueing, clazz,
1383 "interceptWakeMotionBeforeQueueing", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001384
Jeff Brown4532e612012-04-05 14:27:12 -07001385 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001386 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001387 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001388
Jeff Brown4532e612012-04-05 14:27:12 -07001389 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001390 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001391 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001392
Jeff Brown4532e612012-04-05 14:27:12 -07001393 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001394 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001395
Jeff Brown4532e612012-04-05 14:27:12 -07001396 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001397 "getVirtualKeyQuietTimeMillis", "()I");
1398
Jeff Brown4532e612012-04-05 14:27:12 -07001399 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001400 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1401
Jeff Brown4532e612012-04-05 14:27:12 -07001402 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001403 "getKeyRepeatTimeout", "()I");
1404
Jeff Brown4532e612012-04-05 14:27:12 -07001405 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001406 "getKeyRepeatDelay", "()I");
1407
Jeff Brown4532e612012-04-05 14:27:12 -07001408 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001409 "getHoverTapTimeout", "()I");
1410
Jeff Brown4532e612012-04-05 14:27:12 -07001411 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001412 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001413
Jeff Brown4532e612012-04-05 14:27:12 -07001414 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001415 "getDoubleTapTimeout", "()I");
1416
Jeff Brown4532e612012-04-05 14:27:12 -07001417 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001418 "getLongPressTimeout", "()I");
1419
Jeff Brown4532e612012-04-05 14:27:12 -07001420 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001421 "getPointerLayer", "()I");
1422
Jeff Brown4532e612012-04-05 14:27:12 -07001423 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001424 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001425
Jeff Brown6ec6f792012-04-17 16:52:41 -07001426 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikca9eef62013-12-16 11:27:55 -08001427 "getKeyboardLayoutOverlay",
1428 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001429
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001430 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1431 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1432
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001433 // InputDevice
1434
1435 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1436 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1437
Jeff Brown6ec402b2010-07-28 15:48:59 -07001438 // KeyEvent
1439
1440 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001441 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1442
Jeff Brown8d608662010-08-30 03:02:23 -07001443 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001444
1445 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001446 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001447
RoboErikca9eef62013-12-16 11:27:55 -08001448 // InputDeviceIdentifier
1449
1450 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1451 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1452 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1453 "<init>", "(Ljava/lang/String;II)V");
1454
Jeff Brown46b9ac02010-04-22 18:58:52 -07001455 return 0;
1456}
1457
Jeff Brown46b9ac02010-04-22 18:58:52 -07001458} /* namespace android */