blob: 10ad278a8e108e1431545c9442c506fa21d60a0c [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "InputManager-JNI"
18
Jeff Brown9c3cda02010-06-15 01:31:58 -070019//#define LOG_NDEBUG 0
20
21// Log debug messages about InputReaderPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070022#define DEBUG_INPUT_READER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070023
24// Log debug messages about InputDispatcherPolicy
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_INPUT_DISPATCHER_POLICY 0
Jeff Brown9c3cda02010-06-15 01:31:58 -070026
Jeff Brown83c09682010-12-23 17:50:18 -080027
Jeff Brown46b9ac02010-04-22 18:58:52 -070028#include "JNIHelp.h"
29#include "jni.h"
Jeff Brown349703e2010-06-22 01:27:15 -070030#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070032#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080033
Jeff Brown46b9ac02010-04-22 18:58:52 -070034#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080035#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <utils/threads.h>
Jeff Brown83c09682010-12-23 17:50:18 -080037
Jeff Brownb4ff35d2011-01-02 16:37:43 -080038#include <input/InputManager.h>
39#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070040#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080041
Jeff Brown05dc66a2011-03-02 14:41:58 -080042#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070043#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080044#include <android_view_KeyEvent.h>
45#include <android_view_MotionEvent.h>
46#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070047#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080048#include <android/graphics/GraphicsJNI.h>
49
Jeff Brown6ec6f792012-04-17 16:52:41 -070050#include <ScopedLocalRef.h>
51#include <ScopedUtfChars.h>
52
Jeff Brown4f8ecd82012-06-18 18:29:13 -070053#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070054#include "com_android_server_input_InputApplicationHandle.h"
55#include "com_android_server_input_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070056
57namespace android {
58
Jeff Brown1a84fd12011-06-02 01:26:32 -070059// The exponent used to calculate the pointer speed scaling factor.
60// The scaling factor is calculated as 2 ^ (speed * exponent),
61// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070062static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070063
Jeff Brown46b9ac02010-04-22 18:58:52 -070064static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070065 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070066 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070067 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070068 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070069 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070070 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070071 jmethodID interceptKeyBeforeQueueing;
Jeff Brown56194eb2011-03-02 19:23:13 -080072 jmethodID interceptMotionBeforeQueueingWhenScreenOff;
Jeff Brown349703e2010-06-22 01:27:15 -070073 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070074 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070075 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080076 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070077 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080078 jmethodID getKeyRepeatTimeout;
79 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070080 jmethodID getHoverTapTimeout;
81 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070082 jmethodID getDoubleTapTimeout;
83 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080084 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080085 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070086 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070087 jmethodID getDeviceAlias;
Jeff Brown4532e612012-04-05 14:27:12 -070088} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070089
90static struct {
91 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070092} gInputDeviceClassInfo;
93
94static struct {
95 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070096} gKeyEventClassInfo;
97
98static struct {
99 jclass clazz;
100} gMotionEventClassInfo;
101
Jeff Brown928e0542011-01-10 11:17:36 -0800102
103// --- Global functions ---
104
Jeff Brown214eaf42011-05-26 19:17:02 -0700105template<typename T>
106inline static T min(const T& a, const T& b) {
107 return a < b ? a : b;
108}
109
110template<typename T>
111inline static T max(const T& a, const T& b) {
112 return a > b ? a : b;
113}
114
Jeff Brown928e0542011-01-10 11:17:36 -0800115static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
116 const sp<InputApplicationHandle>& inputApplicationHandle) {
117 if (inputApplicationHandle == NULL) {
118 return NULL;
119 }
120 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
121 getInputApplicationHandleObjLocalRef(env);
122}
123
124static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
125 const sp<InputWindowHandle>& inputWindowHandle) {
126 if (inputWindowHandle == NULL) {
127 return NULL;
128 }
129 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
130 getInputWindowHandleObjLocalRef(env);
131}
132
Jeff Brown2352b972011-04-12 22:39:53 -0700133static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
134 SpriteIcon* outSpriteIcon) {
135 PointerIcon pointerIcon;
136 status_t status = android_view_PointerIcon_loadSystemIcon(env,
137 contextObj, style, &pointerIcon);
138 if (!status) {
139 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
140 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
141 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
142 }
143}
144
Jeff Brown905805a2011-10-12 13:57:59 -0700145enum {
146 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown96307042012-07-27 15:51:34 -0700147 WM_ACTION_WAKE_UP = 2,
Jeff Brown905805a2011-10-12 13:57:59 -0700148 WM_ACTION_GO_TO_SLEEP = 4,
149};
150
Jeff Brown928e0542011-01-10 11:17:36 -0800151
152// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800153
Jeff Brown9c3cda02010-06-15 01:31:58 -0700154class NativeInputManager : public virtual RefBase,
155 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700156 public virtual InputDispatcherPolicyInterface,
157 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700158protected:
159 virtual ~NativeInputManager();
160
161public:
Jeff Brown4532e612012-04-05 14:27:12 -0700162 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700163
164 inline sp<InputManager> getInputManager() const { return mInputManager; }
165
Jeff Brownb88102f2010-09-08 11:49:43 -0700166 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700167
Jeff Brownd728bf52012-09-08 18:05:28 -0700168 void setDisplayViewport(bool external, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700169
Jeff Brown7fbdc842010-06-17 20:52:56 -0700170 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800171 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700172 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
173
Jeff Brown9302c872011-07-13 22:51:29 -0700174 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
175 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700176 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800177 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700178 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700179 void setShowTouches(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700180
Jeff Brown9c3cda02010-06-15 01:31:58 -0700181 /* --- InputReaderPolicyInterface implementation --- */
182
Jeff Brown214eaf42011-05-26 19:17:02 -0700183 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800184 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700185 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700186 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const String8& inputDeviceDescriptor);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700187 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700188
189 /* --- InputDispatcherPolicyInterface implementation --- */
190
Jeff Brownbcc046a2012-09-27 20:46:43 -0700191 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700192 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700193 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700194 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700195 const sp<InputWindowHandle>& inputWindowHandle,
196 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800197 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700198 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700199 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
200 virtual bool isKeyRepeatEnabled();
Jeff Brown1f245102010-11-18 20:53:46 -0800201 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800202 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700203 virtual nsecs_t interceptKeyBeforeDispatching(
204 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700205 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800206 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800207 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700208 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700209 virtual bool checkInjectEventsPermissionNonReentrant(
210 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700211
Jeff Brown2352b972011-04-12 22:39:53 -0700212 /* --- PointerControllerPolicyInterface implementation --- */
213
214 virtual void loadPointerResources(PointerResources* outResources);
215
Jeff Brown9c3cda02010-06-15 01:31:58 -0700216private:
217 sp<InputManager> mInputManager;
218
Jeff Brown2352b972011-04-12 22:39:53 -0700219 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700220 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800221 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700222
Jeff Brown83c09682010-12-23 17:50:18 -0800223 Mutex mLock;
224 struct Locked {
225 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700226 DisplayViewport internalViewport;
227 DisplayViewport externalViewport;
Jeff Brown83c09682010-12-23 17:50:18 -0800228
Jeff Brown05dc66a2011-03-02 14:41:58 -0800229 // System UI visibility.
230 int32_t systemUiVisibility;
231
Jeff Brown1a84fd12011-06-02 01:26:32 -0700232 // Pointer speed.
233 int32_t pointerSpeed;
234
Jeff Brown474dcb52011-06-14 20:22:50 -0700235 // True if pointer gestures are enabled.
236 bool pointerGesturesEnabled;
237
Jeff Browndaf4a122011-08-26 17:14:14 -0700238 // Show touches feature enable/disable.
239 bool showTouches;
240
Jeff Brown5541de92011-04-11 11:54:25 -0700241 // Sprite controller singleton, created on first use.
242 sp<SpriteController> spriteController;
243
Jeff Brown83c09682010-12-23 17:50:18 -0800244 // Pointer controller singleton, created and destroyed as needed.
245 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800246 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700247
Jeff Brown2352b972011-04-12 22:39:53 -0700248 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800249 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700250 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800251
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700252 // Power manager interactions.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700253 bool isScreenOn();
254 bool isScreenBright();
255
Jeff Brownb88102f2010-09-08 11:49:43 -0700256 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700257
Jeff Brown9c3cda02010-06-15 01:31:58 -0700258 static inline JNIEnv* jniEnv() {
259 return AndroidRuntime::getJNIEnv();
260 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700261};
262
Jeff Brown928e0542011-01-10 11:17:36 -0800263
Jeff Brown9c3cda02010-06-15 01:31:58 -0700264
Jeff Brown2352b972011-04-12 22:39:53 -0700265NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700266 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown214eaf42011-05-26 19:17:02 -0700267 mLooper(looper) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700268 JNIEnv* env = jniEnv();
269
Jeff Brown2352b972011-04-12 22:39:53 -0700270 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700271 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700272
Jeff Brown83c09682010-12-23 17:50:18 -0800273 {
274 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800275 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700276 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700277 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700278 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800279 }
280
Jeff Brown9c3cda02010-06-15 01:31:58 -0700281 sp<EventHub> eventHub = new EventHub();
282 mInputManager = new InputManager(eventHub, this, this);
283}
284
285NativeInputManager::~NativeInputManager() {
286 JNIEnv* env = jniEnv();
287
Jeff Brown2352b972011-04-12 22:39:53 -0700288 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700289 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700290}
291
Jeff Brownb88102f2010-09-08 11:49:43 -0700292void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700293 mInputManager->getReader()->dump(dump);
294 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700295
Jeff Brownb88102f2010-09-08 11:49:43 -0700296 mInputManager->getDispatcher()->dump(dump);
297 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700298}
299
Jeff Brown7fbdc842010-06-17 20:52:56 -0700300bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700301 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000302 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700303 LOGE_EX(env);
304 env->ExceptionClear();
305 return true;
306 }
307 return false;
308}
309
Jeff Brownd728bf52012-09-08 18:05:28 -0700310void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700311 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700312 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700313 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700314
Jeff Brownd728bf52012-09-08 18:05:28 -0700315 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
316 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700317 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700318 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700319
Jeff Brownd728bf52012-09-08 18:05:28 -0700320 if (!external) {
321 sp<PointerController> controller = mLocked.pointerController.promote();
322 if (controller != NULL) {
323 controller->setDisplayViewport(
324 viewport.logicalRight - viewport.logicalLeft,
325 viewport.logicalBottom - viewport.logicalTop,
326 viewport.orientation);
327 }
Jeff Brown2352b972011-04-12 22:39:53 -0700328 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700329 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700330 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700331
332 if (changed) {
333 mInputManager->getReader()->requestRefreshConfiguration(
334 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
335 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700336}
337
Jeff Brown7fbdc842010-06-17 20:52:56 -0700338status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Brown928e0542011-01-10 11:17:36 -0800339 const sp<InputChannel>& inputChannel,
340 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
341 return mInputManager->getDispatcher()->registerInputChannel(
342 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700343}
344
345status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
346 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700347 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700348}
349
Jeff Brown214eaf42011-05-26 19:17:02 -0700350void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700351 JNIEnv* env = jniEnv();
352
Jeff Brown4532e612012-04-05 14:27:12 -0700353 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
354 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700355 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
356 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
357 }
358
359 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700360 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
361 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700362 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
363 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700364 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700365 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700366 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700367 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700368 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700369 env->DeleteLocalRef(item);
370 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700371 env->DeleteLocalRef(excludedDeviceNames);
372 }
373
Jeff Brown4532e612012-04-05 14:27:12 -0700374 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
375 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700376 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700377 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
378 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700379 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700380 jint longPressTimeout = env->CallIntMethod(mServiceObj,
381 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700382 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700383 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700384
385 // We must ensure that the tap-drag interval is significantly shorter than
386 // the long-press timeout because the tap is held down for the entire duration
387 // of the double-tap timeout.
388 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700389 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700390 outConfig->pointerGestureTapDragInterval =
391 milliseconds_to_nanoseconds(tapDragInterval);
392 }
393 }
394 }
395
Jeff Brown4532e612012-04-05 14:27:12 -0700396 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
397 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700398 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
399 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700400 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700401
402 { // acquire lock
403 AutoMutex _l(mLock);
404
405 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
406 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700407 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700408
Jeff Browndaf4a122011-08-26 17:14:14 -0700409 outConfig->showTouches = mLocked.showTouches;
410
Jeff Brownd728bf52012-09-08 18:05:28 -0700411 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
412 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700413 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700414}
415
Jeff Brown83c09682010-12-23 17:50:18 -0800416sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
417 AutoMutex _l(mLock);
418
419 sp<PointerController> controller = mLocked.pointerController.promote();
420 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700421 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800422
Jeff Brown2352b972011-04-12 22:39:53 -0700423 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800424 mLocked.pointerController = controller;
425
Jeff Brownd728bf52012-09-08 18:05:28 -0700426 DisplayViewport& v = mLocked.internalViewport;
427 controller->setDisplayViewport(
428 v.logicalRight - v.logicalLeft,
429 v.logicalBottom - v.logicalTop,
430 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800431
Jeff Brown5541de92011-04-11 11:54:25 -0700432 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700433 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
434 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700435 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
436 PointerIcon pointerIcon;
437 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
438 mContextObj, &pointerIcon);
439 if (!status && !pointerIcon.isNullIcon()) {
440 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
441 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
442 } else {
443 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800444 }
Jeff Brown2352b972011-04-12 22:39:53 -0700445 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800446 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800447
Jeff Brown2352b972011-04-12 22:39:53 -0700448 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800449 }
450 return controller;
451}
452
Jeff Brown5541de92011-04-11 11:54:25 -0700453void NativeInputManager::ensureSpriteControllerLocked() {
454 if (mLocked.spriteController == NULL) {
455 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700456 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700457 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
458 layer = -1;
459 }
460 mLocked.spriteController = new SpriteController(mLooper, layer);
461 }
462}
463
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700464void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
465 JNIEnv* env = jniEnv();
466
467 size_t count = inputDevices.size();
468 jobjectArray inputDevicesObjArray = env->NewObjectArray(
469 count, gInputDeviceClassInfo.clazz, NULL);
470 if (inputDevicesObjArray) {
471 bool error = false;
472 for (size_t i = 0; i < count; i++) {
473 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
474 if (!inputDeviceObj) {
475 error = true;
476 break;
477 }
478
479 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
480 env->DeleteLocalRef(inputDeviceObj);
481 }
482
483 if (!error) {
484 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
485 inputDevicesObjArray);
486 }
487
488 env->DeleteLocalRef(inputDevicesObjArray);
489 }
490
491 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
492}
493
Jeff Brown6ec6f792012-04-17 16:52:41 -0700494sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
495 const String8& inputDeviceDescriptor) {
496 JNIEnv* env = jniEnv();
497
498 sp<KeyCharacterMap> result;
499 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
500 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
501 gServiceClassInfo.getKeyboardLayoutOverlay, descriptorObj.get())));
502 if (arrayObj.get()) {
503 ScopedLocalRef<jstring> filenameObj(env,
504 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
505 ScopedLocalRef<jstring> contentsObj(env,
506 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
507 ScopedUtfChars filenameChars(env, filenameObj.get());
508 ScopedUtfChars contentsChars(env, contentsObj.get());
509
510 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
511 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
512 }
513 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
514 return result;
515}
516
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700517String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
518 JNIEnv* env = jniEnv();
519
520 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
521 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
522 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
523 String8 result;
524 if (aliasObj.get()) {
525 ScopedUtfChars aliasChars(env, aliasObj.get());
526 result.setTo(aliasChars.c_str());
527 }
528 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
529 return result;
530}
531
Jeff Brownbcc046a2012-09-27 20:46:43 -0700532void NativeInputManager::notifySwitch(nsecs_t when,
533 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700534#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700535 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
536 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700537#endif
538
539 JNIEnv* env = jniEnv();
540
Jeff Brown53384282012-08-20 20:16:01 -0700541 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700542 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700543 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700544}
545
Jeff Brown9c3cda02010-06-15 01:31:58 -0700546void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
547#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000548 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700549#endif
550
551 JNIEnv* env = jniEnv();
552
Jeff Brown4532e612012-04-05 14:27:12 -0700553 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700554 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700555}
556
Jeff Brown519e0242010-09-15 15:18:56 -0700557nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700558 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700559#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000560 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700561#endif
562
563 JNIEnv* env = jniEnv();
564
Jeff Brown928e0542011-01-10 11:17:36 -0800565 jobject inputApplicationHandleObj =
566 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
567 jobject inputWindowHandleObj =
568 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700569 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700570
Jeff Brown4532e612012-04-05 14:27:12 -0700571 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700572 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
573 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700574 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
575 newTimeout = 0; // abort dispatch
576 } else {
577 assert(newTimeout >= 0);
578 }
579
Jeff Brownbd181bb2013-09-10 16:44:24 -0700580 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800581 env->DeleteLocalRef(inputWindowHandleObj);
582 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700583 return newTimeout;
584}
585
Jeff Brown928e0542011-01-10 11:17:36 -0800586void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700587#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000588 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700589#endif
590
Jeff Brown7fbdc842010-06-17 20:52:56 -0700591 JNIEnv* env = jniEnv();
592
Jeff Brown928e0542011-01-10 11:17:36 -0800593 jobject inputWindowHandleObj =
594 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
595 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700596 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800597 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700598 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
599
Jeff Brown928e0542011-01-10 11:17:36 -0800600 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700601 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700602}
603
Jeff Brown214eaf42011-05-26 19:17:02 -0700604void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
605 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800606
Jeff Brown4532e612012-04-05 14:27:12 -0700607 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
608 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700609 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
610 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
611 }
Jeff Browna4547672011-03-02 21:38:11 -0800612
Jeff Brown4532e612012-04-05 14:27:12 -0700613 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
614 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700615 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
616 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
617 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700618}
619
Jeff Brown214eaf42011-05-26 19:17:02 -0700620bool NativeInputManager::isKeyRepeatEnabled() {
621 // Only enable automatic key repeating when the screen is on.
622 return isScreenOn();
Jeff Brownae9fc032010-08-18 15:51:08 -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 Browne20c9e02010-10-11 14:20:19 -0700736bool NativeInputManager::isScreenOn() {
737 return android_server_PowerManagerService_isScreenOn();
738}
739
740bool NativeInputManager::isScreenBright() {
741 return android_server_PowerManagerService_isScreenBright();
742}
743
Jeff Brown0029c662011-03-30 02:25:18 -0700744bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
745 jobject inputEventObj;
746
747 JNIEnv* env = jniEnv();
748 switch (inputEvent->getType()) {
749 case AINPUT_EVENT_TYPE_KEY:
750 inputEventObj = android_view_KeyEvent_fromNative(env,
751 static_cast<const KeyEvent*>(inputEvent));
752 break;
753 case AINPUT_EVENT_TYPE_MOTION:
754 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
755 static_cast<const MotionEvent*>(inputEvent));
756 break;
757 default:
758 return true; // dispatch the event normally
759 }
760
761 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000762 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700763 return true; // dispatch the event normally
764 }
765
766 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700767 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700768 inputEventObj, policyFlags);
769 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
770 pass = true;
771 }
772 env->DeleteLocalRef(inputEventObj);
773 return pass;
774}
775
Jeff Brown1f245102010-11-18 20:53:46 -0800776void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
777 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700778 // Policy:
779 // - Ignore untrusted events and pass them along.
780 // - Ask the window manager what to do with normal events and trusted injected events.
781 // - For normal events wake and brighten the screen if currently off or dim.
782 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800783 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700784 bool isScreenOn = this->isScreenOn();
785 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700786
Jeff Brown3122e442010-10-11 23:32:49 -0700787 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800788 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
789 jint wmActions;
790 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700791 wmActions = env->CallIntMethod(mServiceObj,
792 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown1f245102010-11-18 20:53:46 -0800793 keyEventObj, policyFlags, isScreenOn);
794 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
795 wmActions = 0;
796 }
797 android_view_KeyEvent_recycle(env, keyEventObj);
798 env->DeleteLocalRef(keyEventObj);
799 } else {
Steve Block3762c312012-01-06 19:20:56 +0000800 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700801 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700802 }
803
Jeff Brown1f245102010-11-18 20:53:46 -0800804 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700805 if (!isScreenOn) {
806 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700807 }
808
809 if (!isScreenBright) {
810 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
811 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700812 }
813
Jeff Brown56194eb2011-03-02 19:23:13 -0800814 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700815 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700816 policyFlags |= POLICY_FLAG_PASS_TO_USER;
817 }
818}
819
Jeff Brown56194eb2011-03-02 19:23:13 -0800820void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700821 // Policy:
822 // - Ignore untrusted events and pass them along.
823 // - No special filtering for injected events required at this time.
824 // - Filter normal events based on screen state.
825 // - For normal events brighten (but do not wake) the screen if currently dim.
826 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
827 if (isScreenOn()) {
828 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700829
Jeff Brown3122e442010-10-11 23:32:49 -0700830 if (!isScreenBright()) {
831 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
832 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800833 } else {
834 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700835 jint wmActions = env->CallIntMethod(mServiceObj,
836 gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Jeff Brown56194eb2011-03-02 19:23:13 -0800837 policyFlags);
838 if (checkAndClearExceptionFromCallback(env,
839 "interceptMotionBeforeQueueingWhenScreenOff")) {
840 wmActions = 0;
841 }
842
843 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
844 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700845 }
Jeff Brown3122e442010-10-11 23:32:49 -0700846 } else {
847 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700848 }
849}
850
Jeff Brown56194eb2011-03-02 19:23:13 -0800851void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
852 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800853 if (wmActions & WM_ACTION_GO_TO_SLEEP) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800854#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000855 ALOGD("handleInterceptActions: Going to sleep.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800856#endif
857 android_server_PowerManagerService_goToSleep(when);
858 }
859
Jeff Brown96307042012-07-27 15:51:34 -0700860 if (wmActions & WM_ACTION_WAKE_UP) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800861#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown96307042012-07-27 15:51:34 -0700862 ALOGD("handleInterceptActions: Waking up.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800863#endif
Jeff Brown96307042012-07-27 15:51:34 -0700864 android_server_PowerManagerService_wakeUp(when);
Jeff Brown56194eb2011-03-02 19:23:13 -0800865 }
866
867 if (wmActions & WM_ACTION_PASS_TO_USER) {
868 policyFlags |= POLICY_FLAG_PASS_TO_USER;
869 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800870#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000871 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800872#endif
873 }
874}
875
Jeff Brown905805a2011-10-12 13:57:59 -0700876nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800877 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700878 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700879 // Policy:
880 // - Ignore untrusted events and pass them along.
881 // - Filter normal events and trusted injected events through the window manager policy to
882 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700883 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700884 if (policyFlags & POLICY_FLAG_TRUSTED) {
885 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700886
Jeff Brown928e0542011-01-10 11:17:36 -0800887 // Note: inputWindowHandle may be null.
888 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800889 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
890 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700891 jlong delayMillis = env->CallLongMethod(mServiceObj,
892 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800893 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800894 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
895 android_view_KeyEvent_recycle(env, keyEventObj);
896 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700897 if (!error) {
898 if (delayMillis < 0) {
899 result = -1;
900 } else if (delayMillis > 0) {
901 result = milliseconds_to_nanoseconds(delayMillis);
902 }
903 }
Jeff Brown1f245102010-11-18 20:53:46 -0800904 } else {
Steve Block3762c312012-01-06 19:20:56 +0000905 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800906 }
Jeff Brown928e0542011-01-10 11:17:36 -0800907 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700908 }
Jeff Brown1f245102010-11-18 20:53:46 -0800909 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700910}
911
Jeff Brown928e0542011-01-10 11:17:36 -0800912bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800913 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700914 // Policy:
915 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800916 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700917 if (policyFlags & POLICY_FLAG_TRUSTED) {
918 JNIEnv* env = jniEnv();
919
Jeff Brown928e0542011-01-10 11:17:36 -0800920 // Note: inputWindowHandle may be null.
921 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800922 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
923 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700924 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
925 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800926 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700927 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
928 fallbackKeyEventObj = NULL;
929 }
Jeff Brown1f245102010-11-18 20:53:46 -0800930 android_view_KeyEvent_recycle(env, keyEventObj);
931 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800932
933 if (fallbackKeyEventObj) {
934 // Note: outFallbackKeyEvent may be the same object as keyEvent.
935 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
936 outFallbackKeyEvent)) {
937 result = true;
938 }
939 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
940 env->DeleteLocalRef(fallbackKeyEventObj);
941 }
Jeff Brown1f245102010-11-18 20:53:46 -0800942 } else {
Steve Block3762c312012-01-06 19:20:56 +0000943 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800944 }
Jeff Brown928e0542011-01-10 11:17:36 -0800945 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700946 }
Jeff Brown1f245102010-11-18 20:53:46 -0800947 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700948}
949
Jeff Brown01ce2e92010-09-26 22:20:12 -0700950void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
951 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700952}
953
Jeff Brown349703e2010-06-22 01:27:15 -0700954
Jeff Brownb88102f2010-09-08 11:49:43 -0700955bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
956 int32_t injectorPid, int32_t injectorUid) {
957 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700958 jboolean result = env->CallBooleanMethod(mServiceObj,
959 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700960 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
961 result = false;
962 }
Jeff Brown349703e2010-06-22 01:27:15 -0700963 return result;
964}
965
Jeff Brown2352b972011-04-12 22:39:53 -0700966void NativeInputManager::loadPointerResources(PointerResources* outResources) {
967 JNIEnv* env = jniEnv();
968
969 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
970 &outResources->spotHover);
971 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
972 &outResources->spotTouch);
973 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
974 &outResources->spotAnchor);
975}
976
Jeff Brown83c09682010-12-23 17:50:18 -0800977
Jeff Brown9c3cda02010-06-15 01:31:58 -0700978// ----------------------------------------------------------------------------
979
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000980static jlong nativeInit(JNIEnv* env, jclass clazz,
Jeff Brown4532e612012-04-05 14:27:12 -0700981 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -0700982 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -0800983 if (messageQueue == NULL) {
984 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
985 return 0;
986 }
987
Jeff Brown603b4452012-04-06 17:39:41 -0700988 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
989 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800990 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000991 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700992}
993
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000994static void nativeStart(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -0700995 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700996
Jeff Brown4532e612012-04-05 14:27:12 -0700997 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700998 if (result) {
999 jniThrowRuntimeException(env, "Input manager could not be started.");
1000 }
1001}
1002
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001003static void nativeSetDisplayViewport(JNIEnv* env, jclass clazz, jlong ptr, jboolean external,
Jeff Brownd728bf52012-09-08 18:05:28 -07001004 jint displayId, jint orientation,
1005 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001006 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
1007 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001008 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001009
Jeff Brownd728bf52012-09-08 18:05:28 -07001010 DisplayViewport v;
1011 v.displayId = displayId;
1012 v.orientation = orientation;
1013 v.logicalLeft = logicalLeft;
1014 v.logicalTop = logicalTop;
1015 v.logicalRight = logicalRight;
1016 v.logicalBottom = logicalBottom;
1017 v.physicalLeft = physicalLeft;
1018 v.physicalTop = physicalTop;
1019 v.physicalRight = physicalRight;
1020 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001021 v.deviceWidth = deviceWidth;
1022 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -07001023 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001024}
1025
Jeff Brown4532e612012-04-05 14:27:12 -07001026static jint nativeGetScanCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001027 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001028 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001029
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001030 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001031 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001032}
1033
Jeff Brown4532e612012-04-05 14:27:12 -07001034static jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001035 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001036 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001037
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001038 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001039 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001040}
1041
Jeff Brown4532e612012-04-05 14:27:12 -07001042static jint nativeGetSwitchState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001043 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001044 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001045
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001046 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001047 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001048}
1049
Jeff Brown4532e612012-04-05 14:27:12 -07001050static jboolean nativeHasKeys(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001051 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001052 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001053
1054 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1055 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1056 jsize numCodes = env->GetArrayLength(keyCodes);
1057 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001058 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001059 if (im->getInputManager()->getReader()->hasKeys(
1060 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1061 result = JNI_TRUE;
1062 } else {
1063 result = JNI_FALSE;
1064 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001065 } else {
1066 result = JNI_FALSE;
1067 }
1068
1069 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1070 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1071 return result;
1072}
1073
1074static void throwInputChannelNotInitialized(JNIEnv* env) {
1075 jniThrowException(env, "java/lang/IllegalStateException",
1076 "inputChannel is not initialized");
1077}
1078
Jeff Brown4532e612012-04-05 14:27:12 -07001079static void handleInputChannelDisposed(JNIEnv* env,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001080 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001081 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1082
Steve Block8564c8d2012-01-05 23:22:43 +00001083 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001084 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001085 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001086}
1087
Jeff Brown4532e612012-04-05 14:27:12 -07001088static void nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001089 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001090 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001091
1092 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1093 inputChannelObj);
1094 if (inputChannel == NULL) {
1095 throwInputChannelNotInitialized(env);
1096 return;
1097 }
1098
Jeff Brown928e0542011-01-10 11:17:36 -08001099 sp<InputWindowHandle> inputWindowHandle =
1100 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001101
Jeff Brown4532e612012-04-05 14:27:12 -07001102 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001103 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001104 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001105 String8 message;
1106 message.appendFormat("Failed to register input channel. status=%d", status);
1107 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001108 return;
1109 }
1110
Jeff Browna41ca772010-08-11 14:46:32 -07001111 if (! monitor) {
1112 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001113 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001114 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001115}
1116
Jeff Brown4532e612012-04-05 14:27:12 -07001117static void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001118 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001119 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001120
1121 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1122 inputChannelObj);
1123 if (inputChannel == NULL) {
1124 throwInputChannelNotInitialized(env);
1125 return;
1126 }
1127
1128 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1129
Jeff Brown4532e612012-04-05 14:27:12 -07001130 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001131 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1132 String8 message;
1133 message.appendFormat("Failed to unregister input channel. status=%d", status);
1134 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001135 }
1136}
1137
Jeff Brown4532e612012-04-05 14:27:12 -07001138static void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001139 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001140 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001141
Jeff Brown4532e612012-04-05 14:27:12 -07001142 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001143}
1144
Jeff Brown4532e612012-04-05 14:27:12 -07001145static jint nativeInjectInputEvent(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001146 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001147 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001148 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001149
Jeff Brown6ec402b2010-07-28 15:48:59 -07001150 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1151 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001152 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1153 if (status) {
1154 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1155 return INPUT_EVENT_INJECTION_FAILED;
1156 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001157
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001158 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001159 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1160 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001161 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001162 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1163 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001164 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1165 return INPUT_EVENT_INJECTION_FAILED;
1166 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001167
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001168 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001169 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1170 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001171 } else {
1172 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001173 return INPUT_EVENT_INJECTION_FAILED;
1174 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001175}
1176
Jeff Brown4532e612012-04-05 14:27:12 -07001177static void nativeSetInputWindows(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001178 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001179 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001180
Jeff Brown4532e612012-04-05 14:27:12 -07001181 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001182}
1183
Jeff Brown4532e612012-04-05 14:27:12 -07001184static void nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001185 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001186 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001187
Jeff Brown4532e612012-04-05 14:27:12 -07001188 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001189}
1190
Jeff Brown4532e612012-04-05 14:27:12 -07001191static void nativeSetInputDispatchMode(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001192 jclass clazz, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001193 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001194
Jeff Brown4532e612012-04-05 14:27:12 -07001195 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001196}
1197
Jeff Brown4532e612012-04-05 14:27:12 -07001198static void nativeSetSystemUiVisibility(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001199 jclass clazz, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001200 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001201
Jeff Brown4532e612012-04-05 14:27:12 -07001202 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001203}
1204
Jeff Brown4532e612012-04-05 14:27:12 -07001205static jboolean nativeTransferTouchFocus(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001206 jclass clazz, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001207 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001208
1209 sp<InputChannel> fromChannel =
1210 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1211 sp<InputChannel> toChannel =
1212 android_view_InputChannel_getInputChannel(env, toChannelObj);
1213
1214 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001215 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001216 }
1217
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001218 if (im->getInputManager()->getDispatcher()->
1219 transferTouchFocus(fromChannel, toChannel)) {
1220 return JNI_TRUE;
1221 } else {
1222 return JNI_FALSE;
1223 }
Jeff Browne6504122010-09-27 14:52:15 -07001224}
1225
Jeff Brown4532e612012-04-05 14:27:12 -07001226static void nativeSetPointerSpeed(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001227 jclass clazz, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001228 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001229
Jeff Brown4532e612012-04-05 14:27:12 -07001230 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001231}
1232
Jeff Brown4532e612012-04-05 14:27:12 -07001233static void nativeSetShowTouches(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001234 jclass clazz, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001235 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001236
Jeff Brown4532e612012-04-05 14:27:12 -07001237 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001238}
1239
Jeff Browna47425a2012-04-13 04:09:27 -07001240static void nativeVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001241 jclass clazz, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001242 jint repeat, jint token) {
1243 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1244
1245 size_t patternSize = env->GetArrayLength(patternObj);
1246 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
1247 ALOGI("Skipped requested vibration because the pattern size is %d "
1248 "which is more than the maximum supported size of %d.",
1249 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1250 return; // limit to reasonable size
1251 }
1252
1253 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1254 patternObj, NULL));
1255 nsecs_t pattern[patternSize];
1256 for (size_t i = 0; i < patternSize; i++) {
1257 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001258 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001259 }
1260 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1261
1262 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1263}
1264
1265static void nativeCancelVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001266 jclass clazz, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001267 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1268
1269 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1270}
1271
Jeff Brown6ec6f792012-04-17 16:52:41 -07001272static void nativeReloadKeyboardLayouts(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001273 jclass clazz, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001274 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1275
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001276 im->getInputManager()->getReader()->requestRefreshConfiguration(
1277 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1278}
1279
1280static void nativeReloadDeviceAliases(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001281 jclass clazz, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001282 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1283
1284 im->getInputManager()->getReader()->requestRefreshConfiguration(
1285 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001286}
1287
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001288static jstring nativeDump(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001289 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001290
Jeff Brownb88102f2010-09-08 11:49:43 -07001291 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001292 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001293 return env->NewStringUTF(dump.string());
1294}
1295
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001296static void nativeMonitor(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001297 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001298
Jeff Brown4532e612012-04-05 14:27:12 -07001299 im->getInputManager()->getReader()->monitor();
1300 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001301}
1302
Jeff Brown9c3cda02010-06-15 01:31:58 -07001303// ----------------------------------------------------------------------------
1304
Jeff Brown46b9ac02010-04-22 18:58:52 -07001305static JNINativeMethod gInputManagerMethods[] = {
1306 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001307 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001308 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001309 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001310 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001311 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001312 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001313 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001314 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001315 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001316 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001317 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001318 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001319 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001320 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001321 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001322 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001323 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001324 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001325 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001326 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001327 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001328 (void*) nativeSetInputFilterEnabled },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001329 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001330 (void*) nativeInjectInputEvent },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001331 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001332 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001333 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001334 (void*) nativeSetFocusedApplication },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001335 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001336 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001337 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001338 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001339 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001340 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001341 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001342 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001343 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001344 (void*) nativeSetShowTouches },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001345 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001346 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001347 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001348 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001349 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001350 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001351 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001352 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001353 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001354 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001355 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001356 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001357};
1358
1359#define FIND_CLASS(var, className) \
1360 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001361 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001362
1363#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1364 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1365 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1366
1367#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1368 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1369 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1370
1371int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001372 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001373 gInputManagerMethods, NELEM(gInputManagerMethods));
1374 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1375
Jeff Brown9c3cda02010-06-15 01:31:58 -07001376 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001377
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001378 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001379 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001380
Jeff Brown4532e612012-04-05 14:27:12 -07001381 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001382 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001383
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001384 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1385 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1386
Jeff Brown53384282012-08-20 20:16:01 -07001387 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1388 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001389
Jeff Brown4532e612012-04-05 14:27:12 -07001390 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1391 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001392
Jeff Brown4532e612012-04-05 14:27:12 -07001393 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001394 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001395 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001396
Jeff Brown4532e612012-04-05 14:27:12 -07001397 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001398 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1399
Jeff Brown4532e612012-04-05 14:27:12 -07001400 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001401 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001402
Jeff Brown4532e612012-04-05 14:27:12 -07001403 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001404 clazz,
Jeff Brown56194eb2011-03-02 19:23:13 -08001405 "interceptMotionBeforeQueueingWhenScreenOff", "(I)I");
1406
Jeff Brown4532e612012-04-05 14:27:12 -07001407 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001408 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001409 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001410
Jeff Brown4532e612012-04-05 14:27:12 -07001411 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001412 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001413 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001414
Jeff Brown4532e612012-04-05 14:27:12 -07001415 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001416 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001417
Jeff Brown4532e612012-04-05 14:27:12 -07001418 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001419 "getVirtualKeyQuietTimeMillis", "()I");
1420
Jeff Brown4532e612012-04-05 14:27:12 -07001421 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001422 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1423
Jeff Brown4532e612012-04-05 14:27:12 -07001424 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001425 "getKeyRepeatTimeout", "()I");
1426
Jeff Brown4532e612012-04-05 14:27:12 -07001427 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001428 "getKeyRepeatDelay", "()I");
1429
Jeff Brown4532e612012-04-05 14:27:12 -07001430 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001431 "getHoverTapTimeout", "()I");
1432
Jeff Brown4532e612012-04-05 14:27:12 -07001433 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001434 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001435
Jeff Brown4532e612012-04-05 14:27:12 -07001436 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001437 "getDoubleTapTimeout", "()I");
1438
Jeff Brown4532e612012-04-05 14:27:12 -07001439 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001440 "getLongPressTimeout", "()I");
1441
Jeff Brown4532e612012-04-05 14:27:12 -07001442 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001443 "getPointerLayer", "()I");
1444
Jeff Brown4532e612012-04-05 14:27:12 -07001445 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001446 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001447
Jeff Brown6ec6f792012-04-17 16:52:41 -07001448 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
1449 "getKeyboardLayoutOverlay", "(Ljava/lang/String;)[Ljava/lang/String;");
1450
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001451 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1452 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1453
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001454 // InputDevice
1455
1456 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1457 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1458
Jeff Brown6ec402b2010-07-28 15:48:59 -07001459 // KeyEvent
1460
1461 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001462 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1463
Jeff Brown8d608662010-08-30 03:02:23 -07001464 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001465
1466 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001467 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001468
Jeff Brown46b9ac02010-04-22 18:58:52 -07001469 return 0;
1470}
1471
Jeff Brown46b9ac02010-04-22 18:58:52 -07001472} /* namespace android */