blob: 321776c32bc99c7943ec91d86fe0ee686c25c033 [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/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070039#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080040
Michael Wrightd6b473712014-02-10 15:56:36 -080041#include <inputflinger/InputManager.h>
42
Jeff Brown05dc66a2011-03-02 14:41:58 -080043#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070044#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080045#include <android_view_KeyEvent.h>
46#include <android_view_MotionEvent.h>
47#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070048#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080049#include <android/graphics/GraphicsJNI.h>
50
Jeff Brown6ec6f792012-04-17 16:52:41 -070051#include <ScopedLocalRef.h>
52#include <ScopedUtfChars.h>
53
Jeff Brown4f8ecd82012-06-18 18:29:13 -070054#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070055#include "com_android_server_input_InputApplicationHandle.h"
56#include "com_android_server_input_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070057
58namespace android {
59
Jeff Brown1a84fd12011-06-02 01:26:32 -070060// The exponent used to calculate the pointer speed scaling factor.
61// The scaling factor is calculated as 2 ^ (speed * exponent),
62// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070063static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070064
Jeff Brown46b9ac02010-04-22 18:58:52 -070065static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070066 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070067 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070068 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070069 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070070 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070071 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070072 jmethodID interceptKeyBeforeQueueing;
Jeff Brown56194eb2011-03-02 19:23:13 -080073 jmethodID interceptMotionBeforeQueueingWhenScreenOff;
Jeff Brown349703e2010-06-22 01:27:15 -070074 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070075 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070076 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080077 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070078 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080079 jmethodID getKeyRepeatTimeout;
80 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070081 jmethodID getHoverTapTimeout;
82 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070083 jmethodID getDoubleTapTimeout;
84 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080085 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080086 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070087 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070088 jmethodID getDeviceAlias;
Jeff Brown4532e612012-04-05 14:27:12 -070089} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070090
91static struct {
92 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070093} gInputDeviceClassInfo;
94
95static struct {
96 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070097} gKeyEventClassInfo;
98
99static struct {
100 jclass clazz;
101} gMotionEventClassInfo;
102
RoboErikfb290df2013-12-16 11:27:55 -0800103static struct {
104 jclass clazz;
105 jmethodID constructor;
106} gInputDeviceIdentifierInfo;
107
108
Jeff Brown928e0542011-01-10 11:17:36 -0800109
110// --- Global functions ---
111
Jeff Brown214eaf42011-05-26 19:17:02 -0700112template<typename T>
113inline static T min(const T& a, const T& b) {
114 return a < b ? a : b;
115}
116
117template<typename T>
118inline static T max(const T& a, const T& b) {
119 return a > b ? a : b;
120}
121
Jeff Brown928e0542011-01-10 11:17:36 -0800122static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
123 const sp<InputApplicationHandle>& inputApplicationHandle) {
124 if (inputApplicationHandle == NULL) {
125 return NULL;
126 }
127 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
128 getInputApplicationHandleObjLocalRef(env);
129}
130
131static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
132 const sp<InputWindowHandle>& inputWindowHandle) {
133 if (inputWindowHandle == NULL) {
134 return NULL;
135 }
136 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
137 getInputWindowHandleObjLocalRef(env);
138}
139
Jeff Brown2352b972011-04-12 22:39:53 -0700140static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
141 SpriteIcon* outSpriteIcon) {
142 PointerIcon pointerIcon;
143 status_t status = android_view_PointerIcon_loadSystemIcon(env,
144 contextObj, style, &pointerIcon);
145 if (!status) {
146 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
147 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
148 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
149 }
150}
151
Jeff Brown905805a2011-10-12 13:57:59 -0700152enum {
153 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown96307042012-07-27 15:51:34 -0700154 WM_ACTION_WAKE_UP = 2,
Jeff Brown905805a2011-10-12 13:57:59 -0700155 WM_ACTION_GO_TO_SLEEP = 4,
156};
157
Jeff Brown928e0542011-01-10 11:17:36 -0800158
159// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800160
Jeff Brown9c3cda02010-06-15 01:31:58 -0700161class NativeInputManager : public virtual RefBase,
162 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700163 public virtual InputDispatcherPolicyInterface,
164 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700165protected:
166 virtual ~NativeInputManager();
167
168public:
Jeff Brown4532e612012-04-05 14:27:12 -0700169 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700170
171 inline sp<InputManager> getInputManager() const { return mInputManager; }
172
Jeff Brownb88102f2010-09-08 11:49:43 -0700173 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700174
Jeff Brownd728bf52012-09-08 18:05:28 -0700175 void setDisplayViewport(bool external, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700176
Jeff Brown7fbdc842010-06-17 20:52:56 -0700177 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800178 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700179 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
180
Jeff Brown9302c872011-07-13 22:51:29 -0700181 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
182 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700183 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800184 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700185 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700186 void setShowTouches(bool enabled);
Jeff Brown349703e2010-06-22 01:27:15 -0700187
Jeff Brown9c3cda02010-06-15 01:31:58 -0700188 /* --- InputReaderPolicyInterface implementation --- */
189
Jeff Brown214eaf42011-05-26 19:17:02 -0700190 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800191 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700192 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800193 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700194 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700195
196 /* --- InputDispatcherPolicyInterface implementation --- */
197
Jeff Brownbcc046a2012-09-27 20:46:43 -0700198 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700199 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700200 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700201 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700202 const sp<InputWindowHandle>& inputWindowHandle,
203 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800204 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700205 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700206 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
207 virtual bool isKeyRepeatEnabled();
Jeff Brown1f245102010-11-18 20:53:46 -0800208 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800209 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700210 virtual nsecs_t interceptKeyBeforeDispatching(
211 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700212 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800213 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800214 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700215 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700216 virtual bool checkInjectEventsPermissionNonReentrant(
217 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700218
Jeff Brown2352b972011-04-12 22:39:53 -0700219 /* --- PointerControllerPolicyInterface implementation --- */
220
221 virtual void loadPointerResources(PointerResources* outResources);
222
Jeff Brown9c3cda02010-06-15 01:31:58 -0700223private:
224 sp<InputManager> mInputManager;
225
Jeff Brown2352b972011-04-12 22:39:53 -0700226 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700227 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800228 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700229
Jeff Brown83c09682010-12-23 17:50:18 -0800230 Mutex mLock;
231 struct Locked {
232 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700233 DisplayViewport internalViewport;
234 DisplayViewport externalViewport;
Jeff Brown83c09682010-12-23 17:50:18 -0800235
Jeff Brown05dc66a2011-03-02 14:41:58 -0800236 // System UI visibility.
237 int32_t systemUiVisibility;
238
Jeff Brown1a84fd12011-06-02 01:26:32 -0700239 // Pointer speed.
240 int32_t pointerSpeed;
241
Jeff Brown474dcb52011-06-14 20:22:50 -0700242 // True if pointer gestures are enabled.
243 bool pointerGesturesEnabled;
244
Jeff Browndaf4a122011-08-26 17:14:14 -0700245 // Show touches feature enable/disable.
246 bool showTouches;
247
Jeff Brown5541de92011-04-11 11:54:25 -0700248 // Sprite controller singleton, created on first use.
249 sp<SpriteController> spriteController;
250
Jeff Brown83c09682010-12-23 17:50:18 -0800251 // Pointer controller singleton, created and destroyed as needed.
252 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800253 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700254
Jeff Brown2352b972011-04-12 22:39:53 -0700255 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800256 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700257 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800258
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700259 // Power manager interactions.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700260 bool isScreenOn();
261 bool isScreenBright();
262
Jeff Brownb88102f2010-09-08 11:49:43 -0700263 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700264
Jeff Brown9c3cda02010-06-15 01:31:58 -0700265 static inline JNIEnv* jniEnv() {
266 return AndroidRuntime::getJNIEnv();
267 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700268};
269
Jeff Brown928e0542011-01-10 11:17:36 -0800270
Jeff Brown9c3cda02010-06-15 01:31:58 -0700271
Jeff Brown2352b972011-04-12 22:39:53 -0700272NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700273 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown214eaf42011-05-26 19:17:02 -0700274 mLooper(looper) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700275 JNIEnv* env = jniEnv();
276
Jeff Brown2352b972011-04-12 22:39:53 -0700277 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700278 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700279
Jeff Brown83c09682010-12-23 17:50:18 -0800280 {
281 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800282 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700283 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700284 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700285 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800286 }
287
Jeff Brown9c3cda02010-06-15 01:31:58 -0700288 sp<EventHub> eventHub = new EventHub();
289 mInputManager = new InputManager(eventHub, this, this);
290}
291
292NativeInputManager::~NativeInputManager() {
293 JNIEnv* env = jniEnv();
294
Jeff Brown2352b972011-04-12 22:39:53 -0700295 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700296 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700297}
298
Jeff Brownb88102f2010-09-08 11:49:43 -0700299void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700300 mInputManager->getReader()->dump(dump);
301 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700302
Jeff Brownb88102f2010-09-08 11:49:43 -0700303 mInputManager->getDispatcher()->dump(dump);
304 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700305}
306
Jeff Brown7fbdc842010-06-17 20:52:56 -0700307bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700308 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000309 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700310 LOGE_EX(env);
311 env->ExceptionClear();
312 return true;
313 }
314 return false;
315}
316
Jeff Brownd728bf52012-09-08 18:05:28 -0700317void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700318 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700319 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700320 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700321
Jeff Brownd728bf52012-09-08 18:05:28 -0700322 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
323 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700324 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700325 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700326
Jeff Brownd728bf52012-09-08 18:05:28 -0700327 if (!external) {
328 sp<PointerController> controller = mLocked.pointerController.promote();
329 if (controller != NULL) {
330 controller->setDisplayViewport(
331 viewport.logicalRight - viewport.logicalLeft,
332 viewport.logicalBottom - viewport.logicalTop,
333 viewport.orientation);
334 }
Jeff Brown2352b972011-04-12 22:39:53 -0700335 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700336 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700337 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700338
339 if (changed) {
340 mInputManager->getReader()->requestRefreshConfiguration(
341 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
342 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700343}
344
Jeff Brown7fbdc842010-06-17 20:52:56 -0700345status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Brown928e0542011-01-10 11:17:36 -0800346 const sp<InputChannel>& inputChannel,
347 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
348 return mInputManager->getDispatcher()->registerInputChannel(
349 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700350}
351
352status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
353 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700354 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700355}
356
Jeff Brown214eaf42011-05-26 19:17:02 -0700357void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700358 JNIEnv* env = jniEnv();
359
Jeff Brown4532e612012-04-05 14:27:12 -0700360 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
361 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700362 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
363 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
364 }
365
366 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700367 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
368 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700369 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
370 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700371 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700372 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700373 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700374 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700375 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700376 env->DeleteLocalRef(item);
377 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700378 env->DeleteLocalRef(excludedDeviceNames);
379 }
380
Jeff Brown4532e612012-04-05 14:27:12 -0700381 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
382 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700383 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700384 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
385 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700386 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700387 jint longPressTimeout = env->CallIntMethod(mServiceObj,
388 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700389 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700390 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700391
392 // We must ensure that the tap-drag interval is significantly shorter than
393 // the long-press timeout because the tap is held down for the entire duration
394 // of the double-tap timeout.
395 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700396 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700397 outConfig->pointerGestureTapDragInterval =
398 milliseconds_to_nanoseconds(tapDragInterval);
399 }
400 }
401 }
402
Jeff Brown4532e612012-04-05 14:27:12 -0700403 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
404 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700405 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
406 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700407 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700408
409 { // acquire lock
410 AutoMutex _l(mLock);
411
412 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
413 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700414 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700415
Jeff Browndaf4a122011-08-26 17:14:14 -0700416 outConfig->showTouches = mLocked.showTouches;
417
Jeff Brownd728bf52012-09-08 18:05:28 -0700418 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
419 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700420 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700421}
422
Jeff Brown83c09682010-12-23 17:50:18 -0800423sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
424 AutoMutex _l(mLock);
425
426 sp<PointerController> controller = mLocked.pointerController.promote();
427 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700428 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800429
Jeff Brown2352b972011-04-12 22:39:53 -0700430 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800431 mLocked.pointerController = controller;
432
Jeff Brownd728bf52012-09-08 18:05:28 -0700433 DisplayViewport& v = mLocked.internalViewport;
434 controller->setDisplayViewport(
435 v.logicalRight - v.logicalLeft,
436 v.logicalBottom - v.logicalTop,
437 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800438
Jeff Brown5541de92011-04-11 11:54:25 -0700439 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700440 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
441 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700442 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
443 PointerIcon pointerIcon;
444 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
445 mContextObj, &pointerIcon);
446 if (!status && !pointerIcon.isNullIcon()) {
447 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
448 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
449 } else {
450 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800451 }
Jeff Brown2352b972011-04-12 22:39:53 -0700452 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800453 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800454
Jeff Brown2352b972011-04-12 22:39:53 -0700455 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800456 }
457 return controller;
458}
459
Jeff Brown5541de92011-04-11 11:54:25 -0700460void NativeInputManager::ensureSpriteControllerLocked() {
461 if (mLocked.spriteController == NULL) {
462 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700463 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700464 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
465 layer = -1;
466 }
467 mLocked.spriteController = new SpriteController(mLooper, layer);
468 }
469}
470
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700471void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
472 JNIEnv* env = jniEnv();
473
474 size_t count = inputDevices.size();
475 jobjectArray inputDevicesObjArray = env->NewObjectArray(
476 count, gInputDeviceClassInfo.clazz, NULL);
477 if (inputDevicesObjArray) {
478 bool error = false;
479 for (size_t i = 0; i < count; i++) {
480 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
481 if (!inputDeviceObj) {
482 error = true;
483 break;
484 }
485
486 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
487 env->DeleteLocalRef(inputDeviceObj);
488 }
489
490 if (!error) {
491 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
492 inputDevicesObjArray);
493 }
494
495 env->DeleteLocalRef(inputDevicesObjArray);
496 }
497
498 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
499}
500
Jeff Brown6ec6f792012-04-17 16:52:41 -0700501sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800502 const InputDeviceIdentifier& identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700503 JNIEnv* env = jniEnv();
504
505 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800506 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
507 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
508 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
509 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700510 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800511 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700512 if (arrayObj.get()) {
513 ScopedLocalRef<jstring> filenameObj(env,
514 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
515 ScopedLocalRef<jstring> contentsObj(env,
516 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
517 ScopedUtfChars filenameChars(env, filenameObj.get());
518 ScopedUtfChars contentsChars(env, contentsObj.get());
519
520 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
521 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
522 }
523 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
524 return result;
525}
526
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700527String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
528 JNIEnv* env = jniEnv();
529
530 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
531 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
532 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
533 String8 result;
534 if (aliasObj.get()) {
535 ScopedUtfChars aliasChars(env, aliasObj.get());
536 result.setTo(aliasChars.c_str());
537 }
538 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
539 return result;
540}
541
Jeff Brownbcc046a2012-09-27 20:46:43 -0700542void NativeInputManager::notifySwitch(nsecs_t when,
543 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700544#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700545 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
546 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700547#endif
548
549 JNIEnv* env = jniEnv();
550
Jeff Brown53384282012-08-20 20:16:01 -0700551 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700552 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700553 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700554}
555
Jeff Brown9c3cda02010-06-15 01:31:58 -0700556void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
557#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000558 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700559#endif
560
561 JNIEnv* env = jniEnv();
562
Jeff Brown4532e612012-04-05 14:27:12 -0700563 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700564 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700565}
566
Jeff Brown519e0242010-09-15 15:18:56 -0700567nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700568 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700569#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000570 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700571#endif
572
573 JNIEnv* env = jniEnv();
574
Jeff Brown928e0542011-01-10 11:17:36 -0800575 jobject inputApplicationHandleObj =
576 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
577 jobject inputWindowHandleObj =
578 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700579 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700580
Jeff Brown4532e612012-04-05 14:27:12 -0700581 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700582 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
583 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700584 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
585 newTimeout = 0; // abort dispatch
586 } else {
587 assert(newTimeout >= 0);
588 }
589
Jeff Brownbd181bb2013-09-10 16:44:24 -0700590 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800591 env->DeleteLocalRef(inputWindowHandleObj);
592 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700593 return newTimeout;
594}
595
Jeff Brown928e0542011-01-10 11:17:36 -0800596void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700597#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000598 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700599#endif
600
Jeff Brown7fbdc842010-06-17 20:52:56 -0700601 JNIEnv* env = jniEnv();
602
Jeff Brown928e0542011-01-10 11:17:36 -0800603 jobject inputWindowHandleObj =
604 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
605 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700606 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800607 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700608 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
609
Jeff Brown928e0542011-01-10 11:17:36 -0800610 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700611 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700612}
613
Jeff Brown214eaf42011-05-26 19:17:02 -0700614void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
615 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800616
Jeff Brown4532e612012-04-05 14:27:12 -0700617 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
618 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700619 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
620 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
621 }
Jeff Browna4547672011-03-02 21:38:11 -0800622
Jeff Brown4532e612012-04-05 14:27:12 -0700623 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
624 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700625 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
626 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
627 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700628}
629
Jeff Brown214eaf42011-05-26 19:17:02 -0700630bool NativeInputManager::isKeyRepeatEnabled() {
631 // Only enable automatic key repeating when the screen is on.
632 return isScreenOn();
Jeff Brownae9fc032010-08-18 15:51:08 -0700633}
634
Jeff Brown9302c872011-07-13 22:51:29 -0700635void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
636 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700637
Jeff Brown9302c872011-07-13 22:51:29 -0700638 if (windowHandleObjArray) {
639 jsize length = env->GetArrayLength(windowHandleObjArray);
640 for (jsize i = 0; i < length; i++) {
641 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
642 if (! windowHandleObj) {
643 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700644 }
Jeff Brown9302c872011-07-13 22:51:29 -0700645
646 sp<InputWindowHandle> windowHandle =
647 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
648 if (windowHandle != NULL) {
649 windowHandles.push(windowHandle);
650 }
651 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700652 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700653 }
Jeff Brown349703e2010-06-22 01:27:15 -0700654
Jeff Brown9302c872011-07-13 22:51:29 -0700655 mInputManager->getDispatcher()->setInputWindows(windowHandles);
656
657 // Do this after the dispatcher has updated the window handle state.
658 bool newPointerGesturesEnabled = true;
659 size_t numWindows = windowHandles.size();
660 for (size_t i = 0; i < numWindows; i++) {
661 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700662 const InputWindowInfo* windowInfo = windowHandle->getInfo();
663 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
664 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700665 newPointerGesturesEnabled = false;
666 }
667 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700668
669 uint32_t changes = 0;
670 { // acquire lock
671 AutoMutex _l(mLock);
672
673 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
674 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
675 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
676 }
677 } // release lock
678
679 if (changes) {
680 mInputManager->getReader()->requestRefreshConfiguration(changes);
681 }
Jeff Brown349703e2010-06-22 01:27:15 -0700682}
683
Jeff Brown9302c872011-07-13 22:51:29 -0700684void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
685 sp<InputApplicationHandle> applicationHandle =
686 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
687 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700688}
689
690void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700691 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700692}
693
Jeff Brown05dc66a2011-03-02 14:41:58 -0800694void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
695 AutoMutex _l(mLock);
696
697 if (mLocked.systemUiVisibility != visibility) {
698 mLocked.systemUiVisibility = visibility;
699
700 sp<PointerController> controller = mLocked.pointerController.promote();
701 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700702 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800703 }
704 }
705}
706
Jeff Brown2352b972011-04-12 22:39:53 -0700707void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800708 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700709 controller->setInactivityTimeout(lightsOut
710 ? PointerController::INACTIVITY_TIMEOUT_SHORT
711 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800712}
713
Jeff Brown1a84fd12011-06-02 01:26:32 -0700714void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700715 { // acquire lock
716 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700717
Jeff Brown474dcb52011-06-14 20:22:50 -0700718 if (mLocked.pointerSpeed == speed) {
719 return;
720 }
721
Steve Block6215d3f2012-01-04 20:05:49 +0000722 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700723 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700724 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700725
Jeff Brown474dcb52011-06-14 20:22:50 -0700726 mInputManager->getReader()->requestRefreshConfiguration(
727 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700728}
729
Jeff Browndaf4a122011-08-26 17:14:14 -0700730void NativeInputManager::setShowTouches(bool enabled) {
731 { // acquire lock
732 AutoMutex _l(mLock);
733
734 if (mLocked.showTouches == enabled) {
735 return;
736 }
737
Steve Block6215d3f2012-01-04 20:05:49 +0000738 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700739 mLocked.showTouches = enabled;
740 } // release lock
741
742 mInputManager->getReader()->requestRefreshConfiguration(
743 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
744}
745
Jeff Browne20c9e02010-10-11 14:20:19 -0700746bool NativeInputManager::isScreenOn() {
747 return android_server_PowerManagerService_isScreenOn();
748}
749
750bool NativeInputManager::isScreenBright() {
751 return android_server_PowerManagerService_isScreenBright();
752}
753
Jeff Brown0029c662011-03-30 02:25:18 -0700754bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
755 jobject inputEventObj;
756
757 JNIEnv* env = jniEnv();
758 switch (inputEvent->getType()) {
759 case AINPUT_EVENT_TYPE_KEY:
760 inputEventObj = android_view_KeyEvent_fromNative(env,
761 static_cast<const KeyEvent*>(inputEvent));
762 break;
763 case AINPUT_EVENT_TYPE_MOTION:
764 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
765 static_cast<const MotionEvent*>(inputEvent));
766 break;
767 default:
768 return true; // dispatch the event normally
769 }
770
771 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000772 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700773 return true; // dispatch the event normally
774 }
775
776 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700777 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700778 inputEventObj, policyFlags);
779 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
780 pass = true;
781 }
782 env->DeleteLocalRef(inputEventObj);
783 return pass;
784}
785
Jeff Brown1f245102010-11-18 20:53:46 -0800786void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
787 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700788 // Policy:
789 // - Ignore untrusted events and pass them along.
790 // - Ask the window manager what to do with normal events and trusted injected events.
791 // - For normal events wake and brighten the screen if currently off or dim.
792 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800793 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700794 bool isScreenOn = this->isScreenOn();
795 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700796
Jeff Brown3122e442010-10-11 23:32:49 -0700797 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800798 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
799 jint wmActions;
800 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700801 wmActions = env->CallIntMethod(mServiceObj,
802 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown1f245102010-11-18 20:53:46 -0800803 keyEventObj, policyFlags, isScreenOn);
804 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
805 wmActions = 0;
806 }
807 android_view_KeyEvent_recycle(env, keyEventObj);
808 env->DeleteLocalRef(keyEventObj);
809 } else {
Steve Block3762c312012-01-06 19:20:56 +0000810 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700811 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700812 }
813
Jeff Brown1f245102010-11-18 20:53:46 -0800814 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700815 if (!isScreenOn) {
816 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700817 }
818
819 if (!isScreenBright) {
820 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
821 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700822 }
823
Jeff Brown56194eb2011-03-02 19:23:13 -0800824 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700825 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700826 policyFlags |= POLICY_FLAG_PASS_TO_USER;
827 }
828}
829
Jeff Brown56194eb2011-03-02 19:23:13 -0800830void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700831 // Policy:
832 // - Ignore untrusted events and pass them along.
833 // - No special filtering for injected events required at this time.
834 // - Filter normal events based on screen state.
835 // - For normal events brighten (but do not wake) the screen if currently dim.
836 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
837 if (isScreenOn()) {
838 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700839
Jeff Brown3122e442010-10-11 23:32:49 -0700840 if (!isScreenBright()) {
841 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
842 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800843 } else {
844 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700845 jint wmActions = env->CallIntMethod(mServiceObj,
846 gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Jeff Brown56194eb2011-03-02 19:23:13 -0800847 policyFlags);
848 if (checkAndClearExceptionFromCallback(env,
849 "interceptMotionBeforeQueueingWhenScreenOff")) {
850 wmActions = 0;
851 }
852
853 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
854 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700855 }
Jeff Brown3122e442010-10-11 23:32:49 -0700856 } else {
857 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700858 }
859}
860
Jeff Brown56194eb2011-03-02 19:23:13 -0800861void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
862 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800863 if (wmActions & WM_ACTION_GO_TO_SLEEP) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800864#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000865 ALOGD("handleInterceptActions: Going to sleep.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800866#endif
867 android_server_PowerManagerService_goToSleep(when);
868 }
869
Jeff Brown96307042012-07-27 15:51:34 -0700870 if (wmActions & WM_ACTION_WAKE_UP) {
Jeff Brown9267beb2011-03-07 20:11:22 -0800871#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brown96307042012-07-27 15:51:34 -0700872 ALOGD("handleInterceptActions: Waking up.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800873#endif
Jeff Brown96307042012-07-27 15:51:34 -0700874 android_server_PowerManagerService_wakeUp(when);
Jeff Brown56194eb2011-03-02 19:23:13 -0800875 }
876
877 if (wmActions & WM_ACTION_PASS_TO_USER) {
878 policyFlags |= POLICY_FLAG_PASS_TO_USER;
879 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800880#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000881 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800882#endif
883 }
884}
885
Jeff Brown905805a2011-10-12 13:57:59 -0700886nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800887 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700888 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700889 // Policy:
890 // - Ignore untrusted events and pass them along.
891 // - Filter normal events and trusted injected events through the window manager policy to
892 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700893 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700894 if (policyFlags & POLICY_FLAG_TRUSTED) {
895 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700896
Jeff Brown928e0542011-01-10 11:17:36 -0800897 // Note: inputWindowHandle may be null.
898 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800899 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
900 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700901 jlong delayMillis = env->CallLongMethod(mServiceObj,
902 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800903 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800904 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
905 android_view_KeyEvent_recycle(env, keyEventObj);
906 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700907 if (!error) {
908 if (delayMillis < 0) {
909 result = -1;
910 } else if (delayMillis > 0) {
911 result = milliseconds_to_nanoseconds(delayMillis);
912 }
913 }
Jeff Brown1f245102010-11-18 20:53:46 -0800914 } else {
Steve Block3762c312012-01-06 19:20:56 +0000915 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800916 }
Jeff Brown928e0542011-01-10 11:17:36 -0800917 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700918 }
Jeff Brown1f245102010-11-18 20:53:46 -0800919 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700920}
921
Jeff Brown928e0542011-01-10 11:17:36 -0800922bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800923 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700924 // Policy:
925 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800926 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700927 if (policyFlags & POLICY_FLAG_TRUSTED) {
928 JNIEnv* env = jniEnv();
929
Jeff Brown928e0542011-01-10 11:17:36 -0800930 // Note: inputWindowHandle may be null.
931 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800932 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
933 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700934 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
935 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800936 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700937 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
938 fallbackKeyEventObj = NULL;
939 }
Jeff Brown1f245102010-11-18 20:53:46 -0800940 android_view_KeyEvent_recycle(env, keyEventObj);
941 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800942
943 if (fallbackKeyEventObj) {
944 // Note: outFallbackKeyEvent may be the same object as keyEvent.
945 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
946 outFallbackKeyEvent)) {
947 result = true;
948 }
949 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
950 env->DeleteLocalRef(fallbackKeyEventObj);
951 }
Jeff Brown1f245102010-11-18 20:53:46 -0800952 } else {
Steve Block3762c312012-01-06 19:20:56 +0000953 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800954 }
Jeff Brown928e0542011-01-10 11:17:36 -0800955 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700956 }
Jeff Brown1f245102010-11-18 20:53:46 -0800957 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700958}
959
Jeff Brown01ce2e92010-09-26 22:20:12 -0700960void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
961 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700962}
963
Jeff Brown349703e2010-06-22 01:27:15 -0700964
Jeff Brownb88102f2010-09-08 11:49:43 -0700965bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
966 int32_t injectorPid, int32_t injectorUid) {
967 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700968 jboolean result = env->CallBooleanMethod(mServiceObj,
969 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700970 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
971 result = false;
972 }
Jeff Brown349703e2010-06-22 01:27:15 -0700973 return result;
974}
975
Jeff Brown2352b972011-04-12 22:39:53 -0700976void NativeInputManager::loadPointerResources(PointerResources* outResources) {
977 JNIEnv* env = jniEnv();
978
979 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
980 &outResources->spotHover);
981 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
982 &outResources->spotTouch);
983 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
984 &outResources->spotAnchor);
985}
986
Jeff Brown83c09682010-12-23 17:50:18 -0800987
Jeff Brown9c3cda02010-06-15 01:31:58 -0700988// ----------------------------------------------------------------------------
989
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000990static jlong nativeInit(JNIEnv* env, jclass clazz,
Jeff Brown4532e612012-04-05 14:27:12 -0700991 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -0700992 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -0800993 if (messageQueue == NULL) {
994 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
995 return 0;
996 }
997
Jeff Brown603b4452012-04-06 17:39:41 -0700998 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
999 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001000 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001001 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001002}
1003
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001004static void nativeStart(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001005 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001006
Jeff Brown4532e612012-04-05 14:27:12 -07001007 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001008 if (result) {
1009 jniThrowRuntimeException(env, "Input manager could not be started.");
1010 }
1011}
1012
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001013static void nativeSetDisplayViewport(JNIEnv* env, jclass clazz, jlong ptr, jboolean external,
Jeff Brownd728bf52012-09-08 18:05:28 -07001014 jint displayId, jint orientation,
1015 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001016 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
1017 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001018 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001019
Jeff Brownd728bf52012-09-08 18:05:28 -07001020 DisplayViewport v;
1021 v.displayId = displayId;
1022 v.orientation = orientation;
1023 v.logicalLeft = logicalLeft;
1024 v.logicalTop = logicalTop;
1025 v.logicalRight = logicalRight;
1026 v.logicalBottom = logicalBottom;
1027 v.physicalLeft = physicalLeft;
1028 v.physicalTop = physicalTop;
1029 v.physicalRight = physicalRight;
1030 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001031 v.deviceWidth = deviceWidth;
1032 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -07001033 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001034}
1035
Jeff Brown4532e612012-04-05 14:27:12 -07001036static jint nativeGetScanCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001037 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001038 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001039
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001040 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001041 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001042}
1043
Jeff Brown4532e612012-04-05 14:27:12 -07001044static jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001045 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001046 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001047
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001048 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001049 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001050}
1051
Jeff Brown4532e612012-04-05 14:27:12 -07001052static jint nativeGetSwitchState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001053 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001054 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001055
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001056 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001057 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001058}
1059
Jeff Brown4532e612012-04-05 14:27:12 -07001060static jboolean nativeHasKeys(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001061 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001062 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001063
1064 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1065 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1066 jsize numCodes = env->GetArrayLength(keyCodes);
1067 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001068 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001069 if (im->getInputManager()->getReader()->hasKeys(
1070 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1071 result = JNI_TRUE;
1072 } else {
1073 result = JNI_FALSE;
1074 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001075 } else {
1076 result = JNI_FALSE;
1077 }
1078
1079 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1080 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1081 return result;
1082}
1083
1084static void throwInputChannelNotInitialized(JNIEnv* env) {
1085 jniThrowException(env, "java/lang/IllegalStateException",
1086 "inputChannel is not initialized");
1087}
1088
Jeff Brown4532e612012-04-05 14:27:12 -07001089static void handleInputChannelDisposed(JNIEnv* env,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001090 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001091 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1092
Steve Block8564c8d2012-01-05 23:22:43 +00001093 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001094 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001095 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001096}
1097
Jeff Brown4532e612012-04-05 14:27:12 -07001098static void nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001099 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001100 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001101
1102 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1103 inputChannelObj);
1104 if (inputChannel == NULL) {
1105 throwInputChannelNotInitialized(env);
1106 return;
1107 }
1108
Jeff Brown928e0542011-01-10 11:17:36 -08001109 sp<InputWindowHandle> inputWindowHandle =
1110 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001111
Jeff Brown4532e612012-04-05 14:27:12 -07001112 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001113 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001114 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001115 String8 message;
1116 message.appendFormat("Failed to register input channel. status=%d", status);
1117 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001118 return;
1119 }
1120
Jeff Browna41ca772010-08-11 14:46:32 -07001121 if (! monitor) {
1122 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001123 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001124 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001125}
1126
Jeff Brown4532e612012-04-05 14:27:12 -07001127static void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001128 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001129 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001130
1131 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1132 inputChannelObj);
1133 if (inputChannel == NULL) {
1134 throwInputChannelNotInitialized(env);
1135 return;
1136 }
1137
1138 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1139
Jeff Brown4532e612012-04-05 14:27:12 -07001140 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001141 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1142 String8 message;
1143 message.appendFormat("Failed to unregister input channel. status=%d", status);
1144 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001145 }
1146}
1147
Jeff Brown4532e612012-04-05 14:27:12 -07001148static void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001149 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001150 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001151
Jeff Brown4532e612012-04-05 14:27:12 -07001152 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001153}
1154
Jeff Brown4532e612012-04-05 14:27:12 -07001155static jint nativeInjectInputEvent(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001156 jlong ptr, jobject inputEventObj, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001157 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001158 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001159
Jeff Brown6ec402b2010-07-28 15:48:59 -07001160 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1161 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001162 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1163 if (status) {
1164 jniThrowRuntimeException(env, "Could not read contents of KeyEvent 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 & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1170 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001171 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001172 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1173 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001174 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1175 return INPUT_EVENT_INJECTION_FAILED;
1176 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001177
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001178 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brown0029c662011-03-30 02:25:18 -07001179 motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis,
1180 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001181 } else {
1182 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001183 return INPUT_EVENT_INJECTION_FAILED;
1184 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001185}
1186
Jeff Brown4532e612012-04-05 14:27:12 -07001187static void nativeSetInputWindows(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001188 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001189 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001190
Jeff Brown4532e612012-04-05 14:27:12 -07001191 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001192}
1193
Jeff Brown4532e612012-04-05 14:27:12 -07001194static void nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001195 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001196 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001197
Jeff Brown4532e612012-04-05 14:27:12 -07001198 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001199}
1200
Jeff Brown4532e612012-04-05 14:27:12 -07001201static void nativeSetInputDispatchMode(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001202 jclass clazz, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001203 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001204
Jeff Brown4532e612012-04-05 14:27:12 -07001205 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001206}
1207
Jeff Brown4532e612012-04-05 14:27:12 -07001208static void nativeSetSystemUiVisibility(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001209 jclass clazz, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001210 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001211
Jeff Brown4532e612012-04-05 14:27:12 -07001212 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001213}
1214
Jeff Brown4532e612012-04-05 14:27:12 -07001215static jboolean nativeTransferTouchFocus(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001216 jclass clazz, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001217 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001218
1219 sp<InputChannel> fromChannel =
1220 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1221 sp<InputChannel> toChannel =
1222 android_view_InputChannel_getInputChannel(env, toChannelObj);
1223
1224 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001225 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001226 }
1227
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001228 if (im->getInputManager()->getDispatcher()->
1229 transferTouchFocus(fromChannel, toChannel)) {
1230 return JNI_TRUE;
1231 } else {
1232 return JNI_FALSE;
1233 }
Jeff Browne6504122010-09-27 14:52:15 -07001234}
1235
Jeff Brown4532e612012-04-05 14:27:12 -07001236static void nativeSetPointerSpeed(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001237 jclass clazz, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001238 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001239
Jeff Brown4532e612012-04-05 14:27:12 -07001240 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001241}
1242
Jeff Brown4532e612012-04-05 14:27:12 -07001243static void nativeSetShowTouches(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001244 jclass clazz, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001245 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001246
Jeff Brown4532e612012-04-05 14:27:12 -07001247 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001248}
1249
Jeff Browna47425a2012-04-13 04:09:27 -07001250static void nativeVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001251 jclass clazz, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001252 jint repeat, jint token) {
1253 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1254
1255 size_t patternSize = env->GetArrayLength(patternObj);
1256 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
1257 ALOGI("Skipped requested vibration because the pattern size is %d "
1258 "which is more than the maximum supported size of %d.",
1259 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1260 return; // limit to reasonable size
1261 }
1262
1263 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1264 patternObj, NULL));
1265 nsecs_t pattern[patternSize];
1266 for (size_t i = 0; i < patternSize; i++) {
1267 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001268 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001269 }
1270 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1271
1272 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1273}
1274
1275static void nativeCancelVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001276 jclass clazz, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001277 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1278
1279 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1280}
1281
Jeff Brown6ec6f792012-04-17 16:52:41 -07001282static void nativeReloadKeyboardLayouts(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001283 jclass clazz, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001284 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1285
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001286 im->getInputManager()->getReader()->requestRefreshConfiguration(
1287 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1288}
1289
1290static void nativeReloadDeviceAliases(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001291 jclass clazz, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001292 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1293
1294 im->getInputManager()->getReader()->requestRefreshConfiguration(
1295 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001296}
1297
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001298static jstring nativeDump(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001299 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001300
Jeff Brownb88102f2010-09-08 11:49:43 -07001301 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001302 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001303 return env->NewStringUTF(dump.string());
1304}
1305
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001306static void nativeMonitor(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001307 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001308
Jeff Brown4532e612012-04-05 14:27:12 -07001309 im->getInputManager()->getReader()->monitor();
1310 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001311}
1312
Jeff Brown9c3cda02010-06-15 01:31:58 -07001313// ----------------------------------------------------------------------------
1314
Jeff Brown46b9ac02010-04-22 18:58:52 -07001315static JNINativeMethod gInputManagerMethods[] = {
1316 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001317 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001318 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001319 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001320 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001321 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001322 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001323 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001324 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001325 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001326 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001327 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001328 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001329 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001330 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001331 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001332 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001333 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001334 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001335 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001336 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001337 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001338 (void*) nativeSetInputFilterEnabled },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001339 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001340 (void*) nativeInjectInputEvent },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001341 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001342 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001343 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001344 (void*) nativeSetFocusedApplication },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001345 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001346 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001347 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001348 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001349 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001350 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001351 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001352 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001353 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001354 (void*) nativeSetShowTouches },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001355 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001356 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001357 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001358 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001359 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001360 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001361 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001362 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001363 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001364 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001365 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001366 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001367};
1368
1369#define FIND_CLASS(var, className) \
1370 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001371 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001372
1373#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1374 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1375 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1376
1377#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1378 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1379 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1380
1381int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001382 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001383 gInputManagerMethods, NELEM(gInputManagerMethods));
1384 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1385
Jeff Brown9c3cda02010-06-15 01:31:58 -07001386 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001387
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001388 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001389 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001390
Jeff Brown4532e612012-04-05 14:27:12 -07001391 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001392 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001393
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001394 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1395 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1396
Jeff Brown53384282012-08-20 20:16:01 -07001397 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1398 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001399
Jeff Brown4532e612012-04-05 14:27:12 -07001400 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1401 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001402
Jeff Brown4532e612012-04-05 14:27:12 -07001403 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001404 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001405 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001406
Jeff Brown4532e612012-04-05 14:27:12 -07001407 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001408 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1409
Jeff Brown4532e612012-04-05 14:27:12 -07001410 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001411 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001412
Jeff Brown4532e612012-04-05 14:27:12 -07001413 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001414 clazz,
Jeff Brown56194eb2011-03-02 19:23:13 -08001415 "interceptMotionBeforeQueueingWhenScreenOff", "(I)I");
1416
Jeff Brown4532e612012-04-05 14:27:12 -07001417 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001418 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001419 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001420
Jeff Brown4532e612012-04-05 14:27:12 -07001421 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001422 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001423 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001424
Jeff Brown4532e612012-04-05 14:27:12 -07001425 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001426 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001427
Jeff Brown4532e612012-04-05 14:27:12 -07001428 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001429 "getVirtualKeyQuietTimeMillis", "()I");
1430
Jeff Brown4532e612012-04-05 14:27:12 -07001431 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001432 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1433
Jeff Brown4532e612012-04-05 14:27:12 -07001434 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001435 "getKeyRepeatTimeout", "()I");
1436
Jeff Brown4532e612012-04-05 14:27:12 -07001437 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001438 "getKeyRepeatDelay", "()I");
1439
Jeff Brown4532e612012-04-05 14:27:12 -07001440 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001441 "getHoverTapTimeout", "()I");
1442
Jeff Brown4532e612012-04-05 14:27:12 -07001443 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001444 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001445
Jeff Brown4532e612012-04-05 14:27:12 -07001446 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001447 "getDoubleTapTimeout", "()I");
1448
Jeff Brown4532e612012-04-05 14:27:12 -07001449 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001450 "getLongPressTimeout", "()I");
1451
Jeff Brown4532e612012-04-05 14:27:12 -07001452 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001453 "getPointerLayer", "()I");
1454
Jeff Brown4532e612012-04-05 14:27:12 -07001455 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001456 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001457
Jeff Brown6ec6f792012-04-17 16:52:41 -07001458 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001459 "getKeyboardLayoutOverlay",
1460 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001461
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001462 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1463 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1464
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001465 // InputDevice
1466
1467 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1468 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1469
Jeff Brown6ec402b2010-07-28 15:48:59 -07001470 // KeyEvent
1471
1472 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001473 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1474
Jeff Brown8d608662010-08-30 03:02:23 -07001475 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001476
1477 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001478 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001479
RoboErikfb290df2013-12-16 11:27:55 -08001480 // InputDeviceIdentifier
1481
1482 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1483 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1484 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1485 "<init>", "(Ljava/lang/String;II)V");
1486
Jeff Brown46b9ac02010-04-22 18:58:52 -07001487 return 0;
1488}
1489
Jeff Brown46b9ac02010-04-22 18:58:52 -07001490} /* namespace android */