blob: e1069aecaeb46199e13d8c33af1d4c30c9b8c718 [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>
Jason Gerecke857aa7b2014-01-27 18:34:20 -080052#include <ScopedPrimitiveArray.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070053#include <ScopedUtfChars.h>
54
Jeff Brown4f8ecd82012-06-18 18:29:13 -070055#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070056#include "com_android_server_input_InputApplicationHandle.h"
57#include "com_android_server_input_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070058
59namespace android {
60
Jeff Brown1a84fd12011-06-02 01:26:32 -070061// The exponent used to calculate the pointer speed scaling factor.
62// The scaling factor is calculated as 2 ^ (speed * exponent),
63// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070064static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070065
Jeff Brown46b9ac02010-04-22 18:58:52 -070066static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070067 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070068 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070069 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070070 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070071 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070072 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070073 jmethodID interceptKeyBeforeQueueing;
Jeff Brown56194eb2011-03-02 19:23:13 -080074 jmethodID interceptMotionBeforeQueueingWhenScreenOff;
Jeff Brown349703e2010-06-22 01:27:15 -070075 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070076 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070077 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080078 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070079 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080080 jmethodID getKeyRepeatTimeout;
81 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070082 jmethodID getHoverTapTimeout;
83 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070084 jmethodID getDoubleTapTimeout;
85 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080086 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080087 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070088 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070089 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -080090 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -070091} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070092
93static struct {
94 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070095} gInputDeviceClassInfo;
96
97static struct {
98 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -070099} gKeyEventClassInfo;
100
101static struct {
102 jclass clazz;
103} gMotionEventClassInfo;
104
RoboErikfb290df2013-12-16 11:27:55 -0800105static struct {
106 jclass clazz;
107 jmethodID constructor;
108} gInputDeviceIdentifierInfo;
109
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800110static struct {
111 jclass clazz;
112 jmethodID getAffineTransform;
113} gTouchCalibrationClassInfo;
114
RoboErikfb290df2013-12-16 11:27:55 -0800115
Jeff Brown928e0542011-01-10 11:17:36 -0800116
117// --- Global functions ---
118
Jeff Brown214eaf42011-05-26 19:17:02 -0700119template<typename T>
120inline static T min(const T& a, const T& b) {
121 return a < b ? a : b;
122}
123
124template<typename T>
125inline static T max(const T& a, const T& b) {
126 return a > b ? a : b;
127}
128
Jeff Brown928e0542011-01-10 11:17:36 -0800129static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
130 const sp<InputApplicationHandle>& inputApplicationHandle) {
131 if (inputApplicationHandle == NULL) {
132 return NULL;
133 }
134 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
135 getInputApplicationHandleObjLocalRef(env);
136}
137
138static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
139 const sp<InputWindowHandle>& inputWindowHandle) {
140 if (inputWindowHandle == NULL) {
141 return NULL;
142 }
143 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
144 getInputWindowHandleObjLocalRef(env);
145}
146
Jeff Brown2352b972011-04-12 22:39:53 -0700147static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
148 SpriteIcon* outSpriteIcon) {
149 PointerIcon pointerIcon;
150 status_t status = android_view_PointerIcon_loadSystemIcon(env,
151 contextObj, style, &pointerIcon);
152 if (!status) {
153 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config);
154 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
155 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
156 }
157}
158
Jeff Brown905805a2011-10-12 13:57:59 -0700159enum {
160 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700161};
162
Jeff Brown928e0542011-01-10 11:17:36 -0800163
164// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800165
Jeff Brown9c3cda02010-06-15 01:31:58 -0700166class NativeInputManager : public virtual RefBase,
167 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700168 public virtual InputDispatcherPolicyInterface,
169 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700170protected:
171 virtual ~NativeInputManager();
172
173public:
Jeff Brown4532e612012-04-05 14:27:12 -0700174 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700175
176 inline sp<InputManager> getInputManager() const { return mInputManager; }
177
Jeff Brownb88102f2010-09-08 11:49:43 -0700178 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700179
Jeff Brownd728bf52012-09-08 18:05:28 -0700180 void setDisplayViewport(bool external, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700181
Jeff Brown7fbdc842010-06-17 20:52:56 -0700182 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800183 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700184 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
185
Jeff Brown9302c872011-07-13 22:51:29 -0700186 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
187 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700188 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800189 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700190 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700191 void setShowTouches(bool enabled);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800192 void reloadCalibration();
Jeff Brown349703e2010-06-22 01:27:15 -0700193
Jeff Brown9c3cda02010-06-15 01:31:58 -0700194 /* --- InputReaderPolicyInterface implementation --- */
195
Jeff Brown214eaf42011-05-26 19:17:02 -0700196 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800197 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700198 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800199 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700200 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700201 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
202 jfloatArray matrixArr);
203 virtual TouchAffineTransformation getTouchAffineTransformation(
204 const String8& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700205
206 /* --- InputDispatcherPolicyInterface implementation --- */
207
Jeff Brownbcc046a2012-09-27 20:46:43 -0700208 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700209 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700210 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700211 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700212 const sp<InputWindowHandle>& inputWindowHandle,
213 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800214 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700215 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700216 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
217 virtual bool isKeyRepeatEnabled();
Jeff Brown1f245102010-11-18 20:53:46 -0800218 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800219 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700220 virtual nsecs_t interceptKeyBeforeDispatching(
221 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700222 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800223 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800224 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700225 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700226 virtual bool checkInjectEventsPermissionNonReentrant(
227 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700228
Jeff Brown2352b972011-04-12 22:39:53 -0700229 /* --- PointerControllerPolicyInterface implementation --- */
230
231 virtual void loadPointerResources(PointerResources* outResources);
232
Jeff Brown9c3cda02010-06-15 01:31:58 -0700233private:
234 sp<InputManager> mInputManager;
235
Jeff Brown2352b972011-04-12 22:39:53 -0700236 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700237 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800238 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700239
Jeff Brown83c09682010-12-23 17:50:18 -0800240 Mutex mLock;
241 struct Locked {
242 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700243 DisplayViewport internalViewport;
244 DisplayViewport externalViewport;
Jeff Brown83c09682010-12-23 17:50:18 -0800245
Jeff Brown05dc66a2011-03-02 14:41:58 -0800246 // System UI visibility.
247 int32_t systemUiVisibility;
248
Jeff Brown1a84fd12011-06-02 01:26:32 -0700249 // Pointer speed.
250 int32_t pointerSpeed;
251
Jeff Brown474dcb52011-06-14 20:22:50 -0700252 // True if pointer gestures are enabled.
253 bool pointerGesturesEnabled;
254
Jeff Browndaf4a122011-08-26 17:14:14 -0700255 // Show touches feature enable/disable.
256 bool showTouches;
257
Jeff Brown5541de92011-04-11 11:54:25 -0700258 // Sprite controller singleton, created on first use.
259 sp<SpriteController> spriteController;
260
Jeff Brown83c09682010-12-23 17:50:18 -0800261 // Pointer controller singleton, created and destroyed as needed.
262 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800263 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700264
Jeff Brown2352b972011-04-12 22:39:53 -0700265 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800266 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700267 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800268
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700269 // Power manager interactions.
Jeff Brown9c3cda02010-06-15 01:31:58 -0700270 bool isScreenOn();
271 bool isScreenBright();
272
Jeff Brownb88102f2010-09-08 11:49:43 -0700273 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700274
Jeff Brown9c3cda02010-06-15 01:31:58 -0700275 static inline JNIEnv* jniEnv() {
276 return AndroidRuntime::getJNIEnv();
277 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700278};
279
Jeff Brown928e0542011-01-10 11:17:36 -0800280
Jeff Brown9c3cda02010-06-15 01:31:58 -0700281
Jeff Brown2352b972011-04-12 22:39:53 -0700282NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700283 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown214eaf42011-05-26 19:17:02 -0700284 mLooper(looper) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700285 JNIEnv* env = jniEnv();
286
Jeff Brown2352b972011-04-12 22:39:53 -0700287 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700288 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700289
Jeff Brown83c09682010-12-23 17:50:18 -0800290 {
291 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800292 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700293 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700294 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700295 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800296 }
297
Jeff Brown9c3cda02010-06-15 01:31:58 -0700298 sp<EventHub> eventHub = new EventHub();
299 mInputManager = new InputManager(eventHub, this, this);
300}
301
302NativeInputManager::~NativeInputManager() {
303 JNIEnv* env = jniEnv();
304
Jeff Brown2352b972011-04-12 22:39:53 -0700305 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700306 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700307}
308
Jeff Brownb88102f2010-09-08 11:49:43 -0700309void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700310 mInputManager->getReader()->dump(dump);
311 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700312
Jeff Brownb88102f2010-09-08 11:49:43 -0700313 mInputManager->getDispatcher()->dump(dump);
314 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700315}
316
Jeff Brown7fbdc842010-06-17 20:52:56 -0700317bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700318 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000319 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320 LOGE_EX(env);
321 env->ExceptionClear();
322 return true;
323 }
324 return false;
325}
326
Jeff Brownd728bf52012-09-08 18:05:28 -0700327void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700328 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700329 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700330 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700331
Jeff Brownd728bf52012-09-08 18:05:28 -0700332 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
333 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700334 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700335 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700336
Jeff Brownd728bf52012-09-08 18:05:28 -0700337 if (!external) {
338 sp<PointerController> controller = mLocked.pointerController.promote();
339 if (controller != NULL) {
340 controller->setDisplayViewport(
341 viewport.logicalRight - viewport.logicalLeft,
342 viewport.logicalBottom - viewport.logicalTop,
343 viewport.orientation);
344 }
Jeff Brown2352b972011-04-12 22:39:53 -0700345 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700346 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700347 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700348
349 if (changed) {
350 mInputManager->getReader()->requestRefreshConfiguration(
351 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
352 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700353}
354
Jeff Brown7fbdc842010-06-17 20:52:56 -0700355status_t NativeInputManager::registerInputChannel(JNIEnv* env,
Jeff Brown928e0542011-01-10 11:17:36 -0800356 const sp<InputChannel>& inputChannel,
357 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
358 return mInputManager->getDispatcher()->registerInputChannel(
359 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700360}
361
362status_t NativeInputManager::unregisterInputChannel(JNIEnv* env,
363 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700364 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700365}
366
Jeff Brown214eaf42011-05-26 19:17:02 -0700367void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700368 JNIEnv* env = jniEnv();
369
Jeff Brown4532e612012-04-05 14:27:12 -0700370 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
371 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700372 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
373 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
374 }
375
376 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700377 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
378 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700379 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
380 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700381 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700382 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700383 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700384 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700385 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700386 env->DeleteLocalRef(item);
387 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700388 env->DeleteLocalRef(excludedDeviceNames);
389 }
390
Jeff Brown4532e612012-04-05 14:27:12 -0700391 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
392 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700393 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700394 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
395 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700396 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700397 jint longPressTimeout = env->CallIntMethod(mServiceObj,
398 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700399 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700400 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700401
402 // We must ensure that the tap-drag interval is significantly shorter than
403 // the long-press timeout because the tap is held down for the entire duration
404 // of the double-tap timeout.
405 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700406 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700407 outConfig->pointerGestureTapDragInterval =
408 milliseconds_to_nanoseconds(tapDragInterval);
409 }
410 }
411 }
412
Jeff Brown4532e612012-04-05 14:27:12 -0700413 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
414 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700415 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
416 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700417 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700418
419 { // acquire lock
420 AutoMutex _l(mLock);
421
422 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
423 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700424 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700425
Jeff Browndaf4a122011-08-26 17:14:14 -0700426 outConfig->showTouches = mLocked.showTouches;
427
Jeff Brownd728bf52012-09-08 18:05:28 -0700428 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
429 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700430 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700431}
432
Jeff Brown83c09682010-12-23 17:50:18 -0800433sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) {
434 AutoMutex _l(mLock);
435
436 sp<PointerController> controller = mLocked.pointerController.promote();
437 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700438 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800439
Jeff Brown2352b972011-04-12 22:39:53 -0700440 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800441 mLocked.pointerController = controller;
442
Jeff Brownd728bf52012-09-08 18:05:28 -0700443 DisplayViewport& v = mLocked.internalViewport;
444 controller->setDisplayViewport(
445 v.logicalRight - v.logicalLeft,
446 v.logicalBottom - v.logicalTop,
447 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800448
Jeff Brown5541de92011-04-11 11:54:25 -0700449 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700450 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
451 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700452 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
453 PointerIcon pointerIcon;
454 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
455 mContextObj, &pointerIcon);
456 if (!status && !pointerIcon.isNullIcon()) {
457 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
458 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
459 } else {
460 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800461 }
Jeff Brown2352b972011-04-12 22:39:53 -0700462 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800463 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800464
Jeff Brown2352b972011-04-12 22:39:53 -0700465 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800466 }
467 return controller;
468}
469
Jeff Brown5541de92011-04-11 11:54:25 -0700470void NativeInputManager::ensureSpriteControllerLocked() {
471 if (mLocked.spriteController == NULL) {
472 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700473 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700474 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
475 layer = -1;
476 }
477 mLocked.spriteController = new SpriteController(mLooper, layer);
478 }
479}
480
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700481void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
482 JNIEnv* env = jniEnv();
483
484 size_t count = inputDevices.size();
485 jobjectArray inputDevicesObjArray = env->NewObjectArray(
486 count, gInputDeviceClassInfo.clazz, NULL);
487 if (inputDevicesObjArray) {
488 bool error = false;
489 for (size_t i = 0; i < count; i++) {
490 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
491 if (!inputDeviceObj) {
492 error = true;
493 break;
494 }
495
496 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
497 env->DeleteLocalRef(inputDeviceObj);
498 }
499
500 if (!error) {
501 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
502 inputDevicesObjArray);
503 }
504
505 env->DeleteLocalRef(inputDevicesObjArray);
506 }
507
508 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
509}
510
Jeff Brown6ec6f792012-04-17 16:52:41 -0700511sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800512 const InputDeviceIdentifier& identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700513 JNIEnv* env = jniEnv();
514
515 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800516 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
517 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
518 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
519 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700520 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800521 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700522 if (arrayObj.get()) {
523 ScopedLocalRef<jstring> filenameObj(env,
524 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
525 ScopedLocalRef<jstring> contentsObj(env,
526 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
527 ScopedUtfChars filenameChars(env, filenameObj.get());
528 ScopedUtfChars contentsChars(env, contentsObj.get());
529
530 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
531 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
532 }
533 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
534 return result;
535}
536
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700537String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
538 JNIEnv* env = jniEnv();
539
540 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
541 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
542 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
543 String8 result;
544 if (aliasObj.get()) {
545 ScopedUtfChars aliasChars(env, aliasObj.get());
546 result.setTo(aliasChars.c_str());
547 }
548 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
549 return result;
550}
551
Jeff Brownbcc046a2012-09-27 20:46:43 -0700552void NativeInputManager::notifySwitch(nsecs_t when,
553 uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700554#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700555 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
556 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700557#endif
558
559 JNIEnv* env = jniEnv();
560
Jeff Brown53384282012-08-20 20:16:01 -0700561 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700562 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700563 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700564}
565
Jeff Brown9c3cda02010-06-15 01:31:58 -0700566void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
567#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000568 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700569#endif
570
571 JNIEnv* env = jniEnv();
572
Jeff Brown4532e612012-04-05 14:27:12 -0700573 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700574 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700575}
576
Jeff Brown519e0242010-09-15 15:18:56 -0700577nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700578 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700579#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000580 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700581#endif
582
583 JNIEnv* env = jniEnv();
584
Jeff Brown928e0542011-01-10 11:17:36 -0800585 jobject inputApplicationHandleObj =
586 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
587 jobject inputWindowHandleObj =
588 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700589 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700590
Jeff Brown4532e612012-04-05 14:27:12 -0700591 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700592 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
593 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700594 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
595 newTimeout = 0; // abort dispatch
596 } else {
597 assert(newTimeout >= 0);
598 }
599
Jeff Brownbd181bb2013-09-10 16:44:24 -0700600 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800601 env->DeleteLocalRef(inputWindowHandleObj);
602 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700603 return newTimeout;
604}
605
Jeff Brown928e0542011-01-10 11:17:36 -0800606void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700607#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000608 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700609#endif
610
Jeff Brown7fbdc842010-06-17 20:52:56 -0700611 JNIEnv* env = jniEnv();
612
Jeff Brown928e0542011-01-10 11:17:36 -0800613 jobject inputWindowHandleObj =
614 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
615 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700616 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800617 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700618 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
619
Jeff Brown928e0542011-01-10 11:17:36 -0800620 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700621 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700622}
623
Jeff Brown214eaf42011-05-26 19:17:02 -0700624void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
625 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800626
Jeff Brown4532e612012-04-05 14:27:12 -0700627 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
628 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700629 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
630 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
631 }
Jeff Browna4547672011-03-02 21:38:11 -0800632
Jeff Brown4532e612012-04-05 14:27:12 -0700633 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
634 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700635 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
636 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
637 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700638}
639
Jeff Brown214eaf42011-05-26 19:17:02 -0700640bool NativeInputManager::isKeyRepeatEnabled() {
641 // Only enable automatic key repeating when the screen is on.
642 return isScreenOn();
Jeff Brownae9fc032010-08-18 15:51:08 -0700643}
644
Jeff Brown9302c872011-07-13 22:51:29 -0700645void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
646 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700647
Jeff Brown9302c872011-07-13 22:51:29 -0700648 if (windowHandleObjArray) {
649 jsize length = env->GetArrayLength(windowHandleObjArray);
650 for (jsize i = 0; i < length; i++) {
651 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
652 if (! windowHandleObj) {
653 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700654 }
Jeff Brown9302c872011-07-13 22:51:29 -0700655
656 sp<InputWindowHandle> windowHandle =
657 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
658 if (windowHandle != NULL) {
659 windowHandles.push(windowHandle);
660 }
661 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700662 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700663 }
Jeff Brown349703e2010-06-22 01:27:15 -0700664
Jeff Brown9302c872011-07-13 22:51:29 -0700665 mInputManager->getDispatcher()->setInputWindows(windowHandles);
666
667 // Do this after the dispatcher has updated the window handle state.
668 bool newPointerGesturesEnabled = true;
669 size_t numWindows = windowHandles.size();
670 for (size_t i = 0; i < numWindows; i++) {
671 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700672 const InputWindowInfo* windowInfo = windowHandle->getInfo();
673 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
674 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700675 newPointerGesturesEnabled = false;
676 }
677 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700678
679 uint32_t changes = 0;
680 { // acquire lock
681 AutoMutex _l(mLock);
682
683 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
684 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
685 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
686 }
687 } // release lock
688
689 if (changes) {
690 mInputManager->getReader()->requestRefreshConfiguration(changes);
691 }
Jeff Brown349703e2010-06-22 01:27:15 -0700692}
693
Jeff Brown9302c872011-07-13 22:51:29 -0700694void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
695 sp<InputApplicationHandle> applicationHandle =
696 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
697 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700698}
699
700void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700701 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700702}
703
Jeff Brown05dc66a2011-03-02 14:41:58 -0800704void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
705 AutoMutex _l(mLock);
706
707 if (mLocked.systemUiVisibility != visibility) {
708 mLocked.systemUiVisibility = visibility;
709
710 sp<PointerController> controller = mLocked.pointerController.promote();
711 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700712 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800713 }
714 }
715}
716
Jeff Brown2352b972011-04-12 22:39:53 -0700717void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800718 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700719 controller->setInactivityTimeout(lightsOut
720 ? PointerController::INACTIVITY_TIMEOUT_SHORT
721 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800722}
723
Jeff Brown1a84fd12011-06-02 01:26:32 -0700724void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700725 { // acquire lock
726 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700727
Jeff Brown474dcb52011-06-14 20:22:50 -0700728 if (mLocked.pointerSpeed == speed) {
729 return;
730 }
731
Steve Block6215d3f2012-01-04 20:05:49 +0000732 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700733 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700734 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700735
Jeff Brown474dcb52011-06-14 20:22:50 -0700736 mInputManager->getReader()->requestRefreshConfiguration(
737 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700738}
739
Jeff Browndaf4a122011-08-26 17:14:14 -0700740void NativeInputManager::setShowTouches(bool enabled) {
741 { // acquire lock
742 AutoMutex _l(mLock);
743
744 if (mLocked.showTouches == enabled) {
745 return;
746 }
747
Steve Block6215d3f2012-01-04 20:05:49 +0000748 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700749 mLocked.showTouches = enabled;
750 } // release lock
751
752 mInputManager->getReader()->requestRefreshConfiguration(
753 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
754}
755
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800756void NativeInputManager::reloadCalibration() {
757 mInputManager->getReader()->requestRefreshConfiguration(
758 InputReaderConfiguration::TOUCH_AFFINE_TRANSFORMATION);
759}
760
Jeff Browne20c9e02010-10-11 14:20:19 -0700761bool NativeInputManager::isScreenOn() {
762 return android_server_PowerManagerService_isScreenOn();
763}
764
765bool NativeInputManager::isScreenBright() {
766 return android_server_PowerManagerService_isScreenBright();
767}
768
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800769TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
770 JNIEnv *env, jfloatArray matrixArr) {
771 ScopedFloatArrayRO matrix(env, matrixArr);
772 assert(matrix.size() == 6);
773
774 TouchAffineTransformation transform;
775 transform.x_scale = matrix[0];
776 transform.x_ymix = matrix[1];
777 transform.x_offset = matrix[2];
778 transform.y_xmix = matrix[3];
779 transform.y_scale = matrix[4];
780 transform.y_offset = matrix[5];
781
782 return transform;
783}
784
785TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700786 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800787 JNIEnv* env = jniEnv();
788
789 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
790
791 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700792 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
793 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800794
795 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
796 gTouchCalibrationClassInfo.getAffineTransform));
797
798 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
799
800 env->DeleteLocalRef(matrixArr);
801 env->DeleteLocalRef(cal);
802
803 return transform;
804}
805
Jeff Brown0029c662011-03-30 02:25:18 -0700806bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
807 jobject inputEventObj;
808
809 JNIEnv* env = jniEnv();
810 switch (inputEvent->getType()) {
811 case AINPUT_EVENT_TYPE_KEY:
812 inputEventObj = android_view_KeyEvent_fromNative(env,
813 static_cast<const KeyEvent*>(inputEvent));
814 break;
815 case AINPUT_EVENT_TYPE_MOTION:
816 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
817 static_cast<const MotionEvent*>(inputEvent));
818 break;
819 default:
820 return true; // dispatch the event normally
821 }
822
823 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000824 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700825 return true; // dispatch the event normally
826 }
827
828 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700829 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700830 inputEventObj, policyFlags);
831 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
832 pass = true;
833 }
834 env->DeleteLocalRef(inputEventObj);
835 return pass;
836}
837
Jeff Brown1f245102010-11-18 20:53:46 -0800838void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
839 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700840 // Policy:
841 // - Ignore untrusted events and pass them along.
842 // - Ask the window manager what to do with normal events and trusted injected events.
843 // - For normal events wake and brighten the screen if currently off or dim.
844 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800845 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700846 bool isScreenOn = this->isScreenOn();
847 bool isScreenBright = this->isScreenBright();
Jeff Browne20c9e02010-10-11 14:20:19 -0700848
Jeff Brown3122e442010-10-11 23:32:49 -0700849 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800850 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
851 jint wmActions;
852 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700853 wmActions = env->CallIntMethod(mServiceObj,
854 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown1f245102010-11-18 20:53:46 -0800855 keyEventObj, policyFlags, isScreenOn);
856 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
857 wmActions = 0;
858 }
859 android_view_KeyEvent_recycle(env, keyEventObj);
860 env->DeleteLocalRef(keyEventObj);
861 } else {
Steve Block3762c312012-01-06 19:20:56 +0000862 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700863 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700864 }
865
Jeff Brown1f245102010-11-18 20:53:46 -0800866 if (!(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown3122e442010-10-11 23:32:49 -0700867 if (!isScreenOn) {
868 policyFlags |= POLICY_FLAG_WOKE_HERE;
Jeff Brown3122e442010-10-11 23:32:49 -0700869 }
870
871 if (!isScreenBright) {
872 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
873 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700874 }
875
Jeff Brown56194eb2011-03-02 19:23:13 -0800876 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700877 } else {
Jeff Browne20c9e02010-10-11 14:20:19 -0700878 policyFlags |= POLICY_FLAG_PASS_TO_USER;
879 }
880}
881
Jeff Brown56194eb2011-03-02 19:23:13 -0800882void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700883 // Policy:
884 // - Ignore untrusted events and pass them along.
885 // - No special filtering for injected events required at this time.
886 // - Filter normal events based on screen state.
887 // - For normal events brighten (but do not wake) the screen if currently dim.
888 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
889 if (isScreenOn()) {
890 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700891
Jeff Brown3122e442010-10-11 23:32:49 -0700892 if (!isScreenBright()) {
893 policyFlags |= POLICY_FLAG_BRIGHT_HERE;
894 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800895 } else {
896 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700897 jint wmActions = env->CallIntMethod(mServiceObj,
898 gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Jeff Brown26875502014-01-30 21:47:47 -0800899 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800900 if (checkAndClearExceptionFromCallback(env,
901 "interceptMotionBeforeQueueingWhenScreenOff")) {
902 wmActions = 0;
903 }
904
905 policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE;
906 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700907 }
Jeff Brown3122e442010-10-11 23:32:49 -0700908 } else {
909 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Jeff Browne20c9e02010-10-11 14:20:19 -0700910 }
911}
912
Jeff Brown56194eb2011-03-02 19:23:13 -0800913void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
914 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800915 if (wmActions & WM_ACTION_PASS_TO_USER) {
916 policyFlags |= POLICY_FLAG_PASS_TO_USER;
917 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800918#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000919 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800920#endif
921 }
922}
923
Jeff Brown905805a2011-10-12 13:57:59 -0700924nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800925 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700926 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700927 // Policy:
928 // - Ignore untrusted events and pass them along.
929 // - Filter normal events and trusted injected events through the window manager policy to
930 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700931 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700932 if (policyFlags & POLICY_FLAG_TRUSTED) {
933 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700934
Jeff Brown928e0542011-01-10 11:17:36 -0800935 // Note: inputWindowHandle may be null.
936 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800937 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
938 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700939 jlong delayMillis = env->CallLongMethod(mServiceObj,
940 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800941 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800942 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
943 android_view_KeyEvent_recycle(env, keyEventObj);
944 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700945 if (!error) {
946 if (delayMillis < 0) {
947 result = -1;
948 } else if (delayMillis > 0) {
949 result = milliseconds_to_nanoseconds(delayMillis);
950 }
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 interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800954 }
Jeff Brown928e0542011-01-10 11:17:36 -0800955 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700956 }
Jeff Brown1f245102010-11-18 20:53:46 -0800957 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700958}
959
Jeff Brown928e0542011-01-10 11:17:36 -0800960bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800961 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700962 // Policy:
963 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800964 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700965 if (policyFlags & POLICY_FLAG_TRUSTED) {
966 JNIEnv* env = jniEnv();
967
Jeff Brown928e0542011-01-10 11:17:36 -0800968 // Note: inputWindowHandle may be null.
969 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800970 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
971 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700972 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
973 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800974 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700975 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
976 fallbackKeyEventObj = NULL;
977 }
Jeff Brown1f245102010-11-18 20:53:46 -0800978 android_view_KeyEvent_recycle(env, keyEventObj);
979 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800980
981 if (fallbackKeyEventObj) {
982 // Note: outFallbackKeyEvent may be the same object as keyEvent.
983 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
984 outFallbackKeyEvent)) {
985 result = true;
986 }
987 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
988 env->DeleteLocalRef(fallbackKeyEventObj);
989 }
Jeff Brown1f245102010-11-18 20:53:46 -0800990 } else {
Steve Block3762c312012-01-06 19:20:56 +0000991 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800992 }
Jeff Brown928e0542011-01-10 11:17:36 -0800993 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700994 }
Jeff Brown1f245102010-11-18 20:53:46 -0800995 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700996}
997
Jeff Brown01ce2e92010-09-26 22:20:12 -0700998void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
999 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001000}
1001
Jeff Brown349703e2010-06-22 01:27:15 -07001002
Jeff Brownb88102f2010-09-08 11:49:43 -07001003bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1004 int32_t injectorPid, int32_t injectorUid) {
1005 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001006 jboolean result = env->CallBooleanMethod(mServiceObj,
1007 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001008 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1009 result = false;
1010 }
Jeff Brown349703e2010-06-22 01:27:15 -07001011 return result;
1012}
1013
Jeff Brown2352b972011-04-12 22:39:53 -07001014void NativeInputManager::loadPointerResources(PointerResources* outResources) {
1015 JNIEnv* env = jniEnv();
1016
1017 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1018 &outResources->spotHover);
1019 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1020 &outResources->spotTouch);
1021 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1022 &outResources->spotAnchor);
1023}
1024
Jeff Brown83c09682010-12-23 17:50:18 -08001025
Jeff Brown9c3cda02010-06-15 01:31:58 -07001026// ----------------------------------------------------------------------------
1027
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001028static jlong nativeInit(JNIEnv* env, jclass clazz,
Jeff Brown4532e612012-04-05 14:27:12 -07001029 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001030 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001031 if (messageQueue == NULL) {
1032 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1033 return 0;
1034 }
1035
Jeff Brown603b4452012-04-06 17:39:41 -07001036 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1037 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001038 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001039 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001040}
1041
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001042static void nativeStart(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001043 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001044
Jeff Brown4532e612012-04-05 14:27:12 -07001045 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001046 if (result) {
1047 jniThrowRuntimeException(env, "Input manager could not be started.");
1048 }
1049}
1050
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001051static void nativeSetDisplayViewport(JNIEnv* env, jclass clazz, jlong ptr, jboolean external,
Jeff Brownd728bf52012-09-08 18:05:28 -07001052 jint displayId, jint orientation,
1053 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001054 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
1055 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001056 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001057
Jeff Brownd728bf52012-09-08 18:05:28 -07001058 DisplayViewport v;
1059 v.displayId = displayId;
1060 v.orientation = orientation;
1061 v.logicalLeft = logicalLeft;
1062 v.logicalTop = logicalTop;
1063 v.logicalRight = logicalRight;
1064 v.logicalBottom = logicalBottom;
1065 v.physicalLeft = physicalLeft;
1066 v.physicalTop = physicalTop;
1067 v.physicalRight = physicalRight;
1068 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001069 v.deviceWidth = deviceWidth;
1070 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -07001071 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001072}
1073
Jeff Brown4532e612012-04-05 14:27:12 -07001074static jint nativeGetScanCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001075 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001076 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001077
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001078 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001079 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001080}
1081
Jeff Brown4532e612012-04-05 14:27:12 -07001082static jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001083 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001084 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001085
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001086 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001087 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001088}
1089
Jeff Brown4532e612012-04-05 14:27:12 -07001090static jint nativeGetSwitchState(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001091 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001092 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001093
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001094 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001095 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001096}
1097
Jeff Brown4532e612012-04-05 14:27:12 -07001098static jboolean nativeHasKeys(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001099 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001100 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001101
1102 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1103 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1104 jsize numCodes = env->GetArrayLength(keyCodes);
1105 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001106 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001107 if (im->getInputManager()->getReader()->hasKeys(
1108 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1109 result = JNI_TRUE;
1110 } else {
1111 result = JNI_FALSE;
1112 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001113 } else {
1114 result = JNI_FALSE;
1115 }
1116
1117 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1118 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1119 return result;
1120}
1121
1122static void throwInputChannelNotInitialized(JNIEnv* env) {
1123 jniThrowException(env, "java/lang/IllegalStateException",
1124 "inputChannel is not initialized");
1125}
1126
Jeff Brown4532e612012-04-05 14:27:12 -07001127static void handleInputChannelDisposed(JNIEnv* env,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001128 jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001129 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1130
Steve Block8564c8d2012-01-05 23:22:43 +00001131 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001132 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001133 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001134}
1135
Jeff Brown4532e612012-04-05 14:27:12 -07001136static void nativeRegisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001137 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001138 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001139
1140 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1141 inputChannelObj);
1142 if (inputChannel == NULL) {
1143 throwInputChannelNotInitialized(env);
1144 return;
1145 }
1146
Jeff Brown928e0542011-01-10 11:17:36 -08001147 sp<InputWindowHandle> inputWindowHandle =
1148 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001149
Jeff Brown4532e612012-04-05 14:27:12 -07001150 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001151 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001152 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001153 String8 message;
1154 message.appendFormat("Failed to register input channel. status=%d", status);
1155 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001156 return;
1157 }
1158
Jeff Browna41ca772010-08-11 14:46:32 -07001159 if (! monitor) {
1160 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001161 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001162 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001163}
1164
Jeff Brown4532e612012-04-05 14:27:12 -07001165static void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001166 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001167 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001168
1169 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1170 inputChannelObj);
1171 if (inputChannel == NULL) {
1172 throwInputChannelNotInitialized(env);
1173 return;
1174 }
1175
1176 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1177
Jeff Brown4532e612012-04-05 14:27:12 -07001178 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001179 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1180 String8 message;
1181 message.appendFormat("Failed to unregister input channel. status=%d", status);
1182 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001183 }
1184}
1185
Jeff Brown4532e612012-04-05 14:27:12 -07001186static void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001187 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001188 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001189
Jeff Brown4532e612012-04-05 14:27:12 -07001190 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001191}
1192
Jeff Brown4532e612012-04-05 14:27:12 -07001193static jint nativeInjectInputEvent(JNIEnv* env, jclass clazz,
Jeff Brownca9bc702014-02-11 14:32:56 -08001194 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001195 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001196 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001197
Jeff Brown6ec402b2010-07-28 15:48:59 -07001198 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1199 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001200 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1201 if (status) {
1202 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1203 return INPUT_EVENT_INJECTION_FAILED;
1204 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001205
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001206 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001207 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001208 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001209 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001210 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1211 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001212 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1213 return INPUT_EVENT_INJECTION_FAILED;
1214 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001215
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001216 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001217 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001218 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001219 } else {
1220 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001221 return INPUT_EVENT_INJECTION_FAILED;
1222 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001223}
1224
Jeff Brown4532e612012-04-05 14:27:12 -07001225static void nativeSetInputWindows(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001226 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001227 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001228
Jeff Brown4532e612012-04-05 14:27:12 -07001229 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001230}
1231
Jeff Brown4532e612012-04-05 14:27:12 -07001232static void nativeSetFocusedApplication(JNIEnv* env, jclass clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001233 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001234 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001235
Jeff Brown4532e612012-04-05 14:27:12 -07001236 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001237}
1238
Jeff Brown4532e612012-04-05 14:27:12 -07001239static void nativeSetInputDispatchMode(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001240 jclass clazz, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001241 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001242
Jeff Brown4532e612012-04-05 14:27:12 -07001243 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001244}
1245
Jeff Brown4532e612012-04-05 14:27:12 -07001246static void nativeSetSystemUiVisibility(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001247 jclass clazz, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001248 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001249
Jeff Brown4532e612012-04-05 14:27:12 -07001250 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001251}
1252
Jeff Brown4532e612012-04-05 14:27:12 -07001253static jboolean nativeTransferTouchFocus(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001254 jclass clazz, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001255 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001256
1257 sp<InputChannel> fromChannel =
1258 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1259 sp<InputChannel> toChannel =
1260 android_view_InputChannel_getInputChannel(env, toChannelObj);
1261
1262 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001263 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001264 }
1265
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001266 if (im->getInputManager()->getDispatcher()->
1267 transferTouchFocus(fromChannel, toChannel)) {
1268 return JNI_TRUE;
1269 } else {
1270 return JNI_FALSE;
1271 }
Jeff Browne6504122010-09-27 14:52:15 -07001272}
1273
Jeff Brown4532e612012-04-05 14:27:12 -07001274static void nativeSetPointerSpeed(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001275 jclass clazz, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001276 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001277
Jeff Brown4532e612012-04-05 14:27:12 -07001278 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001279}
1280
Jeff Brown4532e612012-04-05 14:27:12 -07001281static void nativeSetShowTouches(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001282 jclass clazz, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001283 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001284
Jeff Brown4532e612012-04-05 14:27:12 -07001285 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001286}
1287
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001288static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1289 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1290 im->reloadCalibration();
1291}
1292
Jeff Browna47425a2012-04-13 04:09:27 -07001293static void nativeVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001294 jclass clazz, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001295 jint repeat, jint token) {
1296 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1297
1298 size_t patternSize = env->GetArrayLength(patternObj);
1299 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
1300 ALOGI("Skipped requested vibration because the pattern size is %d "
1301 "which is more than the maximum supported size of %d.",
1302 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1303 return; // limit to reasonable size
1304 }
1305
1306 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1307 patternObj, NULL));
1308 nsecs_t pattern[patternSize];
1309 for (size_t i = 0; i < patternSize; i++) {
1310 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001311 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001312 }
1313 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1314
1315 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1316}
1317
1318static void nativeCancelVibrate(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001319 jclass clazz, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001320 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1321
1322 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1323}
1324
Jeff Brown6ec6f792012-04-17 16:52:41 -07001325static void nativeReloadKeyboardLayouts(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001326 jclass clazz, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001327 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1328
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001329 im->getInputManager()->getReader()->requestRefreshConfiguration(
1330 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1331}
1332
1333static void nativeReloadDeviceAliases(JNIEnv* env,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001334 jclass clazz, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001335 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1336
1337 im->getInputManager()->getReader()->requestRefreshConfiguration(
1338 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001339}
1340
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001341static jstring nativeDump(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001342 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001343
Jeff Brownb88102f2010-09-08 11:49:43 -07001344 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001345 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001346 return env->NewStringUTF(dump.string());
1347}
1348
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001349static void nativeMonitor(JNIEnv* env, jclass clazz, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001350 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001351
Jeff Brown4532e612012-04-05 14:27:12 -07001352 im->getInputManager()->getReader()->monitor();
1353 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001354}
1355
Jeff Brown9c3cda02010-06-15 01:31:58 -07001356// ----------------------------------------------------------------------------
1357
Jeff Brown46b9ac02010-04-22 18:58:52 -07001358static JNINativeMethod gInputManagerMethods[] = {
1359 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001360 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001361 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001362 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001363 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001364 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001365 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001366 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001367 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001368 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001369 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001370 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001371 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001372 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001373 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001374 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001375 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001376 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001377 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001378 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001379 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001380 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001381 (void*) nativeSetInputFilterEnabled },
Jeff Brownca9bc702014-02-11 14:32:56 -08001382 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001383 (void*) nativeInjectInputEvent },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001384 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001385 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001386 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001387 (void*) nativeSetFocusedApplication },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001388 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001389 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001390 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001391 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001392 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001393 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001394 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001395 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001396 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001397 (void*) nativeSetShowTouches },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001398 { "nativeReloadCalibration", "(J)V",
1399 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001400 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001401 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001402 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001403 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001404 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001405 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001406 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001407 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001408 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001409 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001410 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001411 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001412};
1413
1414#define FIND_CLASS(var, className) \
1415 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001416 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001417
1418#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1419 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1420 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1421
1422#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1423 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1424 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1425
1426int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001427 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001428 gInputManagerMethods, NELEM(gInputManagerMethods));
1429 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1430
Jeff Brown9c3cda02010-06-15 01:31:58 -07001431 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001432
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001433 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001434 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001435
Jeff Brown4532e612012-04-05 14:27:12 -07001436 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001437 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001438
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001439 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1440 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1441
Jeff Brown53384282012-08-20 20:16:01 -07001442 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1443 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001444
Jeff Brown4532e612012-04-05 14:27:12 -07001445 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1446 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001447
Jeff Brown4532e612012-04-05 14:27:12 -07001448 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001449 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001450 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001451
Jeff Brown4532e612012-04-05 14:27:12 -07001452 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001453 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1454
Jeff Brown4532e612012-04-05 14:27:12 -07001455 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001456 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001457
Jeff Brown4532e612012-04-05 14:27:12 -07001458 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff,
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001459 clazz,
Jeff Brown26875502014-01-30 21:47:47 -08001460 "interceptMotionBeforeQueueingWhenScreenOff", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001461
Jeff Brown4532e612012-04-05 14:27:12 -07001462 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001463 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001464 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001465
Jeff Brown4532e612012-04-05 14:27:12 -07001466 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001467 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001468 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001469
Jeff Brown4532e612012-04-05 14:27:12 -07001470 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001471 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001472
Jeff Brown4532e612012-04-05 14:27:12 -07001473 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001474 "getVirtualKeyQuietTimeMillis", "()I");
1475
Jeff Brown4532e612012-04-05 14:27:12 -07001476 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001477 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1478
Jeff Brown4532e612012-04-05 14:27:12 -07001479 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001480 "getKeyRepeatTimeout", "()I");
1481
Jeff Brown4532e612012-04-05 14:27:12 -07001482 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001483 "getKeyRepeatDelay", "()I");
1484
Jeff Brown4532e612012-04-05 14:27:12 -07001485 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001486 "getHoverTapTimeout", "()I");
1487
Jeff Brown4532e612012-04-05 14:27:12 -07001488 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001489 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001490
Jeff Brown4532e612012-04-05 14:27:12 -07001491 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001492 "getDoubleTapTimeout", "()I");
1493
Jeff Brown4532e612012-04-05 14:27:12 -07001494 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001495 "getLongPressTimeout", "()I");
1496
Jeff Brown4532e612012-04-05 14:27:12 -07001497 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001498 "getPointerLayer", "()I");
1499
Jeff Brown4532e612012-04-05 14:27:12 -07001500 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001501 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001502
Jeff Brown6ec6f792012-04-17 16:52:41 -07001503 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001504 "getKeyboardLayoutOverlay",
1505 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001506
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001507 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1508 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1509
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001510 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1511 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001512 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001513
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001514 // InputDevice
1515
1516 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1517 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1518
Jeff Brown6ec402b2010-07-28 15:48:59 -07001519 // KeyEvent
1520
1521 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001522 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1523
Jeff Brown8d608662010-08-30 03:02:23 -07001524 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001525
1526 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001527 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001528
RoboErikfb290df2013-12-16 11:27:55 -08001529 // InputDeviceIdentifier
1530
1531 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1532 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1533 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1534 "<init>", "(Ljava/lang/String;II)V");
1535
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001536 // TouchCalibration
1537
1538 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1539 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1540
1541 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1542 "getAffineTransform", "()[F");
1543
Jeff Brown46b9ac02010-04-22 18:58:52 -07001544 return 0;
1545}
1546
Jeff Brown46b9ac02010-04-22 18:58:52 -07001547} /* namespace android */