blob: 0bb84035327e0bbefe7fe2ce7d6832b26aacd2e2 [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;
Michael Wright70af00a2014-09-03 19:30:20 -070074 jmethodID interceptMotionBeforeQueueingNonInteractive;
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) {
Mike Reed4a9c3892014-07-07 15:44:40 -0400153 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, kN32_SkColorType);
Jeff Brown2352b972011-04-12 22:39:53 -0700154 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);
Jeff Brown037c33e2014-04-09 00:31:55 -0700192 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800193 void reloadCalibration();
Jeff Brown349703e2010-06-22 01:27:15 -0700194
Jeff Brown9c3cda02010-06-15 01:31:58 -0700195 /* --- InputReaderPolicyInterface implementation --- */
196
Jeff Brown214eaf42011-05-26 19:17:02 -0700197 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800198 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700199 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800200 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700201 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700202 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
203 jfloatArray matrixArr);
204 virtual TouchAffineTransformation getTouchAffineTransformation(
205 const String8& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700206
207 /* --- InputDispatcherPolicyInterface implementation --- */
208
Jeff Brownbcc046a2012-09-27 20:46:43 -0700209 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700210 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700211 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700212 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700213 const sp<InputWindowHandle>& inputWindowHandle,
214 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800215 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700216 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700217 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
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 Brown037c33e2014-04-09 00:31:55 -0700265 volatile bool mInteractive;
266
Jeff Brown2352b972011-04-12 22:39:53 -0700267 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800268 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700269 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800270
Jeff Brownb88102f2010-09-08 11:49:43 -0700271 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700272
Jeff Brown9c3cda02010-06-15 01:31:58 -0700273 static inline JNIEnv* jniEnv() {
274 return AndroidRuntime::getJNIEnv();
275 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700276};
277
Jeff Brown928e0542011-01-10 11:17:36 -0800278
Jeff Brown9c3cda02010-06-15 01:31:58 -0700279
Jeff Brown2352b972011-04-12 22:39:53 -0700280NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700281 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700282 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700283 JNIEnv* env = jniEnv();
284
Jeff Brown2352b972011-04-12 22:39:53 -0700285 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700286 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700287
Jeff Brown83c09682010-12-23 17:50:18 -0800288 {
289 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800290 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700291 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700292 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700293 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800294 }
295
Jeff Brown9c3cda02010-06-15 01:31:58 -0700296 sp<EventHub> eventHub = new EventHub();
297 mInputManager = new InputManager(eventHub, this, this);
298}
299
300NativeInputManager::~NativeInputManager() {
301 JNIEnv* env = jniEnv();
302
Jeff Brown2352b972011-04-12 22:39:53 -0700303 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700304 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700305}
306
Jeff Brownb88102f2010-09-08 11:49:43 -0700307void NativeInputManager::dump(String8& dump) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700308 mInputManager->getReader()->dump(dump);
309 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700310
Jeff Brownb88102f2010-09-08 11:49:43 -0700311 mInputManager->getDispatcher()->dump(dump);
312 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700313}
314
Jeff Brown7fbdc842010-06-17 20:52:56 -0700315bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700316 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000317 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700318 LOGE_EX(env);
319 env->ExceptionClear();
320 return true;
321 }
322 return false;
323}
324
Jeff Brownd728bf52012-09-08 18:05:28 -0700325void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700326 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700327 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700328 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700329
Jeff Brownd728bf52012-09-08 18:05:28 -0700330 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
331 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700332 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700333 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700334
Jeff Brownd728bf52012-09-08 18:05:28 -0700335 if (!external) {
336 sp<PointerController> controller = mLocked.pointerController.promote();
337 if (controller != NULL) {
338 controller->setDisplayViewport(
339 viewport.logicalRight - viewport.logicalLeft,
340 viewport.logicalBottom - viewport.logicalTop,
341 viewport.orientation);
342 }
Jeff Brown2352b972011-04-12 22:39:53 -0700343 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700344 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700345 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700346
347 if (changed) {
348 mInputManager->getReader()->requestRefreshConfiguration(
349 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
350 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700351}
352
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700353status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Jeff Brown928e0542011-01-10 11:17:36 -0800354 const sp<InputChannel>& inputChannel,
355 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
356 return mInputManager->getDispatcher()->registerInputChannel(
357 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700358}
359
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700360status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700361 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700362 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700363}
364
Jeff Brown214eaf42011-05-26 19:17:02 -0700365void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700366 JNIEnv* env = jniEnv();
367
Jeff Brown4532e612012-04-05 14:27:12 -0700368 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
369 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700370 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
371 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
372 }
373
374 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700375 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
376 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700377 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
378 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700379 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700380 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700381 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700382 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700383 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700384 env->DeleteLocalRef(item);
385 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700386 env->DeleteLocalRef(excludedDeviceNames);
387 }
388
Jeff Brown4532e612012-04-05 14:27:12 -0700389 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
390 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700391 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700392 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
393 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700394 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700395 jint longPressTimeout = env->CallIntMethod(mServiceObj,
396 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700397 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700398 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700399
400 // We must ensure that the tap-drag interval is significantly shorter than
401 // the long-press timeout because the tap is held down for the entire duration
402 // of the double-tap timeout.
403 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700404 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700405 outConfig->pointerGestureTapDragInterval =
406 milliseconds_to_nanoseconds(tapDragInterval);
407 }
408 }
409 }
410
Jeff Brown4532e612012-04-05 14:27:12 -0700411 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
412 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700413 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
414 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700415 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700416
417 { // acquire lock
418 AutoMutex _l(mLock);
419
420 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
421 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700422 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700423
Jeff Browndaf4a122011-08-26 17:14:14 -0700424 outConfig->showTouches = mLocked.showTouches;
425
Jeff Brownd728bf52012-09-08 18:05:28 -0700426 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
427 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700428 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700429}
430
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700431sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Jeff Brown83c09682010-12-23 17:50:18 -0800432 AutoMutex _l(mLock);
433
434 sp<PointerController> controller = mLocked.pointerController.promote();
435 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700436 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800437
Jeff Brown2352b972011-04-12 22:39:53 -0700438 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800439 mLocked.pointerController = controller;
440
Jeff Brownd728bf52012-09-08 18:05:28 -0700441 DisplayViewport& v = mLocked.internalViewport;
442 controller->setDisplayViewport(
443 v.logicalRight - v.logicalLeft,
444 v.logicalBottom - v.logicalTop,
445 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800446
Jeff Brown5541de92011-04-11 11:54:25 -0700447 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700448 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
449 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700450 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
451 PointerIcon pointerIcon;
452 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
453 mContextObj, &pointerIcon);
454 if (!status && !pointerIcon.isNullIcon()) {
455 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
456 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
457 } else {
458 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800459 }
Jeff Brown2352b972011-04-12 22:39:53 -0700460 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800461 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800462
Jeff Brown2352b972011-04-12 22:39:53 -0700463 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800464 }
465 return controller;
466}
467
Jeff Brown5541de92011-04-11 11:54:25 -0700468void NativeInputManager::ensureSpriteControllerLocked() {
469 if (mLocked.spriteController == NULL) {
470 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700471 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700472 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
473 layer = -1;
474 }
475 mLocked.spriteController = new SpriteController(mLooper, layer);
476 }
477}
478
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700479void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
480 JNIEnv* env = jniEnv();
481
482 size_t count = inputDevices.size();
483 jobjectArray inputDevicesObjArray = env->NewObjectArray(
484 count, gInputDeviceClassInfo.clazz, NULL);
485 if (inputDevicesObjArray) {
486 bool error = false;
487 for (size_t i = 0; i < count; i++) {
488 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
489 if (!inputDeviceObj) {
490 error = true;
491 break;
492 }
493
494 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
495 env->DeleteLocalRef(inputDeviceObj);
496 }
497
498 if (!error) {
499 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
500 inputDevicesObjArray);
501 }
502
503 env->DeleteLocalRef(inputDevicesObjArray);
504 }
505
506 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
507}
508
Jeff Brown6ec6f792012-04-17 16:52:41 -0700509sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800510 const InputDeviceIdentifier& identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700511 JNIEnv* env = jniEnv();
512
513 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800514 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
515 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
516 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
517 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700518 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800519 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700520 if (arrayObj.get()) {
521 ScopedLocalRef<jstring> filenameObj(env,
522 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
523 ScopedLocalRef<jstring> contentsObj(env,
524 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
525 ScopedUtfChars filenameChars(env, filenameObj.get());
526 ScopedUtfChars contentsChars(env, contentsObj.get());
527
528 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
529 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
530 }
531 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
532 return result;
533}
534
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700535String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
536 JNIEnv* env = jniEnv();
537
538 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
539 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
540 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
541 String8 result;
542 if (aliasObj.get()) {
543 ScopedUtfChars aliasChars(env, aliasObj.get());
544 result.setTo(aliasChars.c_str());
545 }
546 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
547 return result;
548}
549
Jeff Brownbcc046a2012-09-27 20:46:43 -0700550void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700551 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700552#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700553 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
554 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700555#endif
556
557 JNIEnv* env = jniEnv();
558
Jeff Brown53384282012-08-20 20:16:01 -0700559 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700560 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700561 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700562}
563
Jeff Brown9c3cda02010-06-15 01:31:58 -0700564void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
565#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000566 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700567#endif
568
569 JNIEnv* env = jniEnv();
570
Jeff Brown4532e612012-04-05 14:27:12 -0700571 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700572 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700573}
574
Jeff Brown519e0242010-09-15 15:18:56 -0700575nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700576 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700577#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000578 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700579#endif
580
581 JNIEnv* env = jniEnv();
582
Jeff Brown928e0542011-01-10 11:17:36 -0800583 jobject inputApplicationHandleObj =
584 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
585 jobject inputWindowHandleObj =
586 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700587 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700588
Jeff Brown4532e612012-04-05 14:27:12 -0700589 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700590 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
591 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700592 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
593 newTimeout = 0; // abort dispatch
594 } else {
595 assert(newTimeout >= 0);
596 }
597
Jeff Brownbd181bb2013-09-10 16:44:24 -0700598 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800599 env->DeleteLocalRef(inputWindowHandleObj);
600 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700601 return newTimeout;
602}
603
Jeff Brown928e0542011-01-10 11:17:36 -0800604void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700605#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000606 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700607#endif
608
Jeff Brown7fbdc842010-06-17 20:52:56 -0700609 JNIEnv* env = jniEnv();
610
Jeff Brown928e0542011-01-10 11:17:36 -0800611 jobject inputWindowHandleObj =
612 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
613 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700614 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800615 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700616 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
617
Jeff Brown928e0542011-01-10 11:17:36 -0800618 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700619 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700620}
621
Jeff Brown214eaf42011-05-26 19:17:02 -0700622void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
623 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800624
Jeff Brown4532e612012-04-05 14:27:12 -0700625 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
626 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700627 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
628 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
629 }
Jeff Browna4547672011-03-02 21:38:11 -0800630
Jeff Brown4532e612012-04-05 14:27:12 -0700631 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
632 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700633 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
634 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
635 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700636}
637
Jeff Brown9302c872011-07-13 22:51:29 -0700638void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
639 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700640
Jeff Brown9302c872011-07-13 22:51:29 -0700641 if (windowHandleObjArray) {
642 jsize length = env->GetArrayLength(windowHandleObjArray);
643 for (jsize i = 0; i < length; i++) {
644 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
645 if (! windowHandleObj) {
646 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700647 }
Jeff Brown9302c872011-07-13 22:51:29 -0700648
649 sp<InputWindowHandle> windowHandle =
650 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
651 if (windowHandle != NULL) {
652 windowHandles.push(windowHandle);
653 }
654 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700655 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700656 }
Jeff Brown349703e2010-06-22 01:27:15 -0700657
Jeff Brown9302c872011-07-13 22:51:29 -0700658 mInputManager->getDispatcher()->setInputWindows(windowHandles);
659
660 // Do this after the dispatcher has updated the window handle state.
661 bool newPointerGesturesEnabled = true;
662 size_t numWindows = windowHandles.size();
663 for (size_t i = 0; i < numWindows; i++) {
664 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700665 const InputWindowInfo* windowInfo = windowHandle->getInfo();
666 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
667 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700668 newPointerGesturesEnabled = false;
669 }
670 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700671
672 uint32_t changes = 0;
673 { // acquire lock
674 AutoMutex _l(mLock);
675
676 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
677 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
678 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
679 }
680 } // release lock
681
682 if (changes) {
683 mInputManager->getReader()->requestRefreshConfiguration(changes);
684 }
Jeff Brown349703e2010-06-22 01:27:15 -0700685}
686
Jeff Brown9302c872011-07-13 22:51:29 -0700687void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
688 sp<InputApplicationHandle> applicationHandle =
689 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
690 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700691}
692
693void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700694 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700695}
696
Jeff Brown05dc66a2011-03-02 14:41:58 -0800697void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
698 AutoMutex _l(mLock);
699
700 if (mLocked.systemUiVisibility != visibility) {
701 mLocked.systemUiVisibility = visibility;
702
703 sp<PointerController> controller = mLocked.pointerController.promote();
704 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700705 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800706 }
707 }
708}
709
Jeff Brown2352b972011-04-12 22:39:53 -0700710void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800711 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700712 controller->setInactivityTimeout(lightsOut
713 ? PointerController::INACTIVITY_TIMEOUT_SHORT
714 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800715}
716
Jeff Brown1a84fd12011-06-02 01:26:32 -0700717void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700718 { // acquire lock
719 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700720
Jeff Brown474dcb52011-06-14 20:22:50 -0700721 if (mLocked.pointerSpeed == speed) {
722 return;
723 }
724
Steve Block6215d3f2012-01-04 20:05:49 +0000725 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700726 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700727 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700728
Jeff Brown474dcb52011-06-14 20:22:50 -0700729 mInputManager->getReader()->requestRefreshConfiguration(
730 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700731}
732
Jeff Browndaf4a122011-08-26 17:14:14 -0700733void NativeInputManager::setShowTouches(bool enabled) {
734 { // acquire lock
735 AutoMutex _l(mLock);
736
737 if (mLocked.showTouches == enabled) {
738 return;
739 }
740
Steve Block6215d3f2012-01-04 20:05:49 +0000741 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700742 mLocked.showTouches = enabled;
743 } // release lock
744
745 mInputManager->getReader()->requestRefreshConfiguration(
746 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
747}
748
Jeff Brown037c33e2014-04-09 00:31:55 -0700749void NativeInputManager::setInteractive(bool interactive) {
750 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700751}
752
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800753void NativeInputManager::reloadCalibration() {
754 mInputManager->getReader()->requestRefreshConfiguration(
755 InputReaderConfiguration::TOUCH_AFFINE_TRANSFORMATION);
756}
757
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800758TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
759 JNIEnv *env, jfloatArray matrixArr) {
760 ScopedFloatArrayRO matrix(env, matrixArr);
761 assert(matrix.size() == 6);
762
763 TouchAffineTransformation transform;
764 transform.x_scale = matrix[0];
765 transform.x_ymix = matrix[1];
766 transform.x_offset = matrix[2];
767 transform.y_xmix = matrix[3];
768 transform.y_scale = matrix[4];
769 transform.y_offset = matrix[5];
770
771 return transform;
772}
773
774TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700775 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800776 JNIEnv* env = jniEnv();
777
778 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
779
780 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700781 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
782 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800783
784 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
785 gTouchCalibrationClassInfo.getAffineTransform));
786
787 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
788
789 env->DeleteLocalRef(matrixArr);
790 env->DeleteLocalRef(cal);
791
792 return transform;
793}
794
Jeff Brown0029c662011-03-30 02:25:18 -0700795bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
796 jobject inputEventObj;
797
798 JNIEnv* env = jniEnv();
799 switch (inputEvent->getType()) {
800 case AINPUT_EVENT_TYPE_KEY:
801 inputEventObj = android_view_KeyEvent_fromNative(env,
802 static_cast<const KeyEvent*>(inputEvent));
803 break;
804 case AINPUT_EVENT_TYPE_MOTION:
805 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
806 static_cast<const MotionEvent*>(inputEvent));
807 break;
808 default:
809 return true; // dispatch the event normally
810 }
811
812 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000813 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700814 return true; // dispatch the event normally
815 }
816
817 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700818 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700819 inputEventObj, policyFlags);
820 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
821 pass = true;
822 }
823 env->DeleteLocalRef(inputEventObj);
824 return pass;
825}
826
Jeff Brown1f245102010-11-18 20:53:46 -0800827void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
828 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700829 // Policy:
830 // - Ignore untrusted events and pass them along.
831 // - Ask the window manager what to do with normal events and trusted injected events.
832 // - For normal events wake and brighten the screen if currently off or dim.
Jeff Brown037c33e2014-04-09 00:31:55 -0700833 if (mInteractive) {
834 policyFlags |= POLICY_FLAG_INTERACTIVE;
835 }
Jeff Brown3122e442010-10-11 23:32:49 -0700836 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800837 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700838 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800839 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
840 jint wmActions;
841 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700842 wmActions = env->CallIntMethod(mServiceObj,
843 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700844 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800845 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
846 wmActions = 0;
847 }
848 android_view_KeyEvent_recycle(env, keyEventObj);
849 env->DeleteLocalRef(keyEventObj);
850 } else {
Steve Block3762c312012-01-06 19:20:56 +0000851 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700852 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700853 }
854
Jeff Brown56194eb2011-03-02 19:23:13 -0800855 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700856 } else {
Michael Wright70af00a2014-09-03 19:30:20 -0700857 if (mInteractive) {
858 policyFlags |= POLICY_FLAG_PASS_TO_USER;
859 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700860 }
861}
862
Jeff Brown56194eb2011-03-02 19:23:13 -0800863void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700864 // Policy:
865 // - Ignore untrusted events and pass them along.
866 // - No special filtering for injected events required at this time.
867 // - Filter normal events based on screen state.
868 // - For normal events brighten (but do not wake) the screen if currently dim.
Jeff Brown037c33e2014-04-09 00:31:55 -0700869 if (mInteractive) {
870 policyFlags |= POLICY_FLAG_INTERACTIVE;
871 }
Jeff Brown3122e442010-10-11 23:32:49 -0700872 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700873 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -0700874 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -0700875 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -0800876 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700877 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -0700878 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -0800879 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800880 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -0700881 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800882 wmActions = 0;
883 }
884
Jeff Brown56194eb2011-03-02 19:23:13 -0800885 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700886 }
Jeff Brown3122e442010-10-11 23:32:49 -0700887 } else {
Michael Wright70af00a2014-09-03 19:30:20 -0700888 if (mInteractive) {
889 policyFlags |= POLICY_FLAG_PASS_TO_USER;
890 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700891 }
892}
893
Jeff Brown56194eb2011-03-02 19:23:13 -0800894void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
895 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800896 if (wmActions & WM_ACTION_PASS_TO_USER) {
897 policyFlags |= POLICY_FLAG_PASS_TO_USER;
898 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800899#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000900 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800901#endif
902 }
903}
904
Jeff Brown905805a2011-10-12 13:57:59 -0700905nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800906 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700907 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700908 // Policy:
909 // - Ignore untrusted events and pass them along.
910 // - Filter normal events and trusted injected events through the window manager policy to
911 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700912 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700913 if (policyFlags & POLICY_FLAG_TRUSTED) {
914 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700915
Jeff Brown928e0542011-01-10 11:17:36 -0800916 // Note: inputWindowHandle may be null.
917 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800918 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
919 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700920 jlong delayMillis = env->CallLongMethod(mServiceObj,
921 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800922 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800923 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
924 android_view_KeyEvent_recycle(env, keyEventObj);
925 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700926 if (!error) {
927 if (delayMillis < 0) {
928 result = -1;
929 } else if (delayMillis > 0) {
930 result = milliseconds_to_nanoseconds(delayMillis);
931 }
932 }
Jeff Brown1f245102010-11-18 20:53:46 -0800933 } else {
Steve Block3762c312012-01-06 19:20:56 +0000934 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800935 }
Jeff Brown928e0542011-01-10 11:17:36 -0800936 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700937 }
Jeff Brown1f245102010-11-18 20:53:46 -0800938 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700939}
940
Jeff Brown928e0542011-01-10 11:17:36 -0800941bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800942 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700943 // Policy:
944 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800945 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700946 if (policyFlags & POLICY_FLAG_TRUSTED) {
947 JNIEnv* env = jniEnv();
948
Jeff Brown928e0542011-01-10 11:17:36 -0800949 // Note: inputWindowHandle may be null.
950 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800951 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
952 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700953 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
954 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800955 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700956 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
957 fallbackKeyEventObj = NULL;
958 }
Jeff Brown1f245102010-11-18 20:53:46 -0800959 android_view_KeyEvent_recycle(env, keyEventObj);
960 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800961
962 if (fallbackKeyEventObj) {
963 // Note: outFallbackKeyEvent may be the same object as keyEvent.
964 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
965 outFallbackKeyEvent)) {
966 result = true;
967 }
968 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
969 env->DeleteLocalRef(fallbackKeyEventObj);
970 }
Jeff Brown1f245102010-11-18 20:53:46 -0800971 } else {
Steve Block3762c312012-01-06 19:20:56 +0000972 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800973 }
Jeff Brown928e0542011-01-10 11:17:36 -0800974 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -0700975 }
Jeff Brown1f245102010-11-18 20:53:46 -0800976 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -0700977}
978
Jeff Brown01ce2e92010-09-26 22:20:12 -0700979void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
980 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -0700981}
982
Jeff Brown349703e2010-06-22 01:27:15 -0700983
Jeff Brownb88102f2010-09-08 11:49:43 -0700984bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
985 int32_t injectorPid, int32_t injectorUid) {
986 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700987 jboolean result = env->CallBooleanMethod(mServiceObj,
988 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700989 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
990 result = false;
991 }
Jeff Brown349703e2010-06-22 01:27:15 -0700992 return result;
993}
994
Jeff Brown2352b972011-04-12 22:39:53 -0700995void NativeInputManager::loadPointerResources(PointerResources* outResources) {
996 JNIEnv* env = jniEnv();
997
998 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
999 &outResources->spotHover);
1000 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1001 &outResources->spotTouch);
1002 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1003 &outResources->spotAnchor);
1004}
1005
Jeff Brown83c09682010-12-23 17:50:18 -08001006
Jeff Brown9c3cda02010-06-15 01:31:58 -07001007// ----------------------------------------------------------------------------
1008
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001009static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001010 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001011 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001012 if (messageQueue == NULL) {
1013 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1014 return 0;
1015 }
1016
Jeff Brown603b4452012-04-06 17:39:41 -07001017 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1018 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001019 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001020 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001021}
1022
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001023static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001024 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001025
Jeff Brown4532e612012-04-05 14:27:12 -07001026 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001027 if (result) {
1028 jniThrowRuntimeException(env, "Input manager could not be started.");
1029 }
1030}
1031
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001032static void nativeSetDisplayViewport(JNIEnv* /* env */, jclass /* clazz */, jlong ptr,
1033 jboolean external, jint displayId, jint orientation,
Jeff Brownd728bf52012-09-08 18:05:28 -07001034 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001035 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
1036 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001037 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001038
Jeff Brownd728bf52012-09-08 18:05:28 -07001039 DisplayViewport v;
1040 v.displayId = displayId;
1041 v.orientation = orientation;
1042 v.logicalLeft = logicalLeft;
1043 v.logicalTop = logicalTop;
1044 v.logicalRight = logicalRight;
1045 v.logicalBottom = logicalBottom;
1046 v.physicalLeft = physicalLeft;
1047 v.physicalTop = physicalTop;
1048 v.physicalRight = physicalRight;
1049 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001050 v.deviceWidth = deviceWidth;
1051 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -07001052 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001053}
1054
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001055static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001056 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001057 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001058
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001059 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001060 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001061}
1062
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001063static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001064 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001065 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001066
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001067 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001068 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001069}
1070
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001071static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001072 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001073 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001074
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001075 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001076 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001077}
1078
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001079static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001080 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001081 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001082
1083 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1084 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1085 jsize numCodes = env->GetArrayLength(keyCodes);
1086 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001087 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001088 if (im->getInputManager()->getReader()->hasKeys(
1089 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1090 result = JNI_TRUE;
1091 } else {
1092 result = JNI_FALSE;
1093 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001094 } else {
1095 result = JNI_FALSE;
1096 }
1097
1098 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1099 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1100 return result;
1101}
1102
1103static void throwInputChannelNotInitialized(JNIEnv* env) {
1104 jniThrowException(env, "java/lang/IllegalStateException",
1105 "inputChannel is not initialized");
1106}
1107
Jeff Brown4532e612012-04-05 14:27:12 -07001108static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001109 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001110 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1111
Steve Block8564c8d2012-01-05 23:22:43 +00001112 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001113 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001114 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001115}
1116
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001117static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001118 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001119 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001120
1121 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1122 inputChannelObj);
1123 if (inputChannel == NULL) {
1124 throwInputChannelNotInitialized(env);
1125 return;
1126 }
1127
Jeff Brown928e0542011-01-10 11:17:36 -08001128 sp<InputWindowHandle> inputWindowHandle =
1129 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001130
Jeff Brown4532e612012-04-05 14:27:12 -07001131 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001132 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001133 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001134 String8 message;
1135 message.appendFormat("Failed to register input channel. status=%d", status);
1136 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001137 return;
1138 }
1139
Jeff Browna41ca772010-08-11 14:46:32 -07001140 if (! monitor) {
1141 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001142 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001143 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001144}
1145
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001146static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001147 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001148 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001149
1150 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1151 inputChannelObj);
1152 if (inputChannel == NULL) {
1153 throwInputChannelNotInitialized(env);
1154 return;
1155 }
1156
1157 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1158
Jeff Brown4532e612012-04-05 14:27:12 -07001159 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001160 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1161 String8 message;
1162 message.appendFormat("Failed to unregister input channel. status=%d", status);
1163 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001164 }
1165}
1166
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001167static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001168 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001169 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001170
Jeff Brown4532e612012-04-05 14:27:12 -07001171 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001172}
1173
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001174static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Jeff Brownca9bc702014-02-11 14:32:56 -08001175 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001176 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001177 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001178
Jeff Brown6ec402b2010-07-28 15:48:59 -07001179 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1180 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001181 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1182 if (status) {
1183 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1184 return INPUT_EVENT_INJECTION_FAILED;
1185 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001186
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001187 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001188 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001189 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001190 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001191 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1192 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001193 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1194 return INPUT_EVENT_INJECTION_FAILED;
1195 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001196
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001197 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001198 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001199 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001200 } else {
1201 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001202 return INPUT_EVENT_INJECTION_FAILED;
1203 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001204}
1205
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001206static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001207 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001208 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001209
Jeff Brown4532e612012-04-05 14:27:12 -07001210 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001211}
1212
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001213static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001214 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001215 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001216
Jeff Brown4532e612012-04-05 14:27:12 -07001217 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001218}
1219
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001220static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1221 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001222 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001223
Jeff Brown4532e612012-04-05 14:27:12 -07001224 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001225}
1226
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001227static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1228 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001229 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001230
Jeff Brown4532e612012-04-05 14:27:12 -07001231 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001232}
1233
Jeff Brown4532e612012-04-05 14:27:12 -07001234static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001235 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001236 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001237
1238 sp<InputChannel> fromChannel =
1239 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1240 sp<InputChannel> toChannel =
1241 android_view_InputChannel_getInputChannel(env, toChannelObj);
1242
1243 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001244 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001245 }
1246
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001247 if (im->getInputManager()->getDispatcher()->
1248 transferTouchFocus(fromChannel, toChannel)) {
1249 return JNI_TRUE;
1250 } else {
1251 return JNI_FALSE;
1252 }
Jeff Browne6504122010-09-27 14:52:15 -07001253}
1254
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001255static void nativeSetPointerSpeed(JNIEnv* /* env */,
1256 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001257 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001258
Jeff Brown4532e612012-04-05 14:27:12 -07001259 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001260}
1261
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001262static void nativeSetShowTouches(JNIEnv* /* env */,
1263 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001264 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001265
Jeff Brown4532e612012-04-05 14:27:12 -07001266 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001267}
1268
Jeff Brown037c33e2014-04-09 00:31:55 -07001269static void nativeSetInteractive(JNIEnv* env,
1270 jclass clazz, jlong ptr, jboolean interactive) {
1271 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1272
1273 im->setInteractive(interactive);
1274}
1275
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001276static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1277 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1278 im->reloadCalibration();
1279}
1280
Jeff Browna47425a2012-04-13 04:09:27 -07001281static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001282 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001283 jint repeat, jint token) {
1284 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1285
1286 size_t patternSize = env->GetArrayLength(patternObj);
1287 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001288 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001289 "which is more than the maximum supported size of %d.",
1290 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1291 return; // limit to reasonable size
1292 }
1293
1294 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1295 patternObj, NULL));
1296 nsecs_t pattern[patternSize];
1297 for (size_t i = 0; i < patternSize; i++) {
1298 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001299 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001300 }
1301 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1302
1303 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1304}
1305
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001306static void nativeCancelVibrate(JNIEnv* /* env */,
1307 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001308 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1309
1310 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1311}
1312
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001313static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1314 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001315 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1316
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001317 im->getInputManager()->getReader()->requestRefreshConfiguration(
1318 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1319}
1320
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001321static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1322 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001323 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1324
1325 im->getInputManager()->getReader()->requestRefreshConfiguration(
1326 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001327}
1328
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001329static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001330 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001331
Jeff Brownb88102f2010-09-08 11:49:43 -07001332 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001333 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001334 return env->NewStringUTF(dump.string());
1335}
1336
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001337static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001338 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001339
Jeff Brown4532e612012-04-05 14:27:12 -07001340 im->getInputManager()->getReader()->monitor();
1341 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001342}
1343
Jeff Brown9c3cda02010-06-15 01:31:58 -07001344// ----------------------------------------------------------------------------
1345
Daniel Micay76f6a862015-09-19 17:31:01 -04001346static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001347 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001348 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001349 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001350 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001351 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001352 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001353 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001354 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001355 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001356 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001357 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001358 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001359 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001360 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001361 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001362 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001363 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001364 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001365 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001366 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001367 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001368 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001369 (void*) nativeSetInputFilterEnabled },
Jeff Brownca9bc702014-02-11 14:32:56 -08001370 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001371 (void*) nativeInjectInputEvent },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001372 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001373 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001374 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001375 (void*) nativeSetFocusedApplication },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001376 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001377 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001378 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001379 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001380 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001381 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001382 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001383 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001384 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001385 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001386 { "nativeSetInteractive", "(JZ)V",
1387 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001388 { "nativeReloadCalibration", "(J)V",
1389 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001390 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001391 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001392 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001393 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001394 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001395 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001396 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001397 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001398 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001399 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001400 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001401 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001402};
1403
1404#define FIND_CLASS(var, className) \
1405 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001406 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001407
1408#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1409 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1410 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1411
1412#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1413 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1414 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1415
1416int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001417 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001418 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001419 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001420 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1421
Jeff Brown9c3cda02010-06-15 01:31:58 -07001422 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001423
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001424 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001425 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001426
Jeff Brown4532e612012-04-05 14:27:12 -07001427 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001428 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001429
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001430 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1431 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1432
Jeff Brown53384282012-08-20 20:16:01 -07001433 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1434 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001435
Jeff Brown4532e612012-04-05 14:27:12 -07001436 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1437 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001438
Jeff Brown4532e612012-04-05 14:27:12 -07001439 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001440 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001441 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001442
Jeff Brown4532e612012-04-05 14:27:12 -07001443 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001444 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1445
Jeff Brown4532e612012-04-05 14:27:12 -07001446 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001447 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001448
Michael Wright70af00a2014-09-03 19:30:20 -07001449 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1450 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001451
Jeff Brown4532e612012-04-05 14:27:12 -07001452 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001453 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001454 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001455
Jeff Brown4532e612012-04-05 14:27:12 -07001456 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001457 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001458 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001459
Jeff Brown4532e612012-04-05 14:27:12 -07001460 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001461 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001462
Jeff Brown4532e612012-04-05 14:27:12 -07001463 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001464 "getVirtualKeyQuietTimeMillis", "()I");
1465
Jeff Brown4532e612012-04-05 14:27:12 -07001466 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001467 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1468
Jeff Brown4532e612012-04-05 14:27:12 -07001469 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001470 "getKeyRepeatTimeout", "()I");
1471
Jeff Brown4532e612012-04-05 14:27:12 -07001472 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001473 "getKeyRepeatDelay", "()I");
1474
Jeff Brown4532e612012-04-05 14:27:12 -07001475 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001476 "getHoverTapTimeout", "()I");
1477
Jeff Brown4532e612012-04-05 14:27:12 -07001478 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001479 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001480
Jeff Brown4532e612012-04-05 14:27:12 -07001481 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001482 "getDoubleTapTimeout", "()I");
1483
Jeff Brown4532e612012-04-05 14:27:12 -07001484 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001485 "getLongPressTimeout", "()I");
1486
Jeff Brown4532e612012-04-05 14:27:12 -07001487 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001488 "getPointerLayer", "()I");
1489
Jeff Brown4532e612012-04-05 14:27:12 -07001490 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001491 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001492
Jeff Brown6ec6f792012-04-17 16:52:41 -07001493 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001494 "getKeyboardLayoutOverlay",
1495 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001496
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001497 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1498 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1499
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001500 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1501 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001502 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001503
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001504 // InputDevice
1505
1506 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1507 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1508
Jeff Brown6ec402b2010-07-28 15:48:59 -07001509 // KeyEvent
1510
1511 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001512 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1513
Jeff Brown8d608662010-08-30 03:02:23 -07001514 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001515
1516 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001517 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001518
RoboErikfb290df2013-12-16 11:27:55 -08001519 // InputDeviceIdentifier
1520
1521 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1522 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1523 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1524 "<init>", "(Ljava/lang/String;II)V");
1525
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001526 // TouchCalibration
1527
1528 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1529 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1530
1531 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1532 "getAffineTransform", "()[F");
1533
Jeff Brown46b9ac02010-04-22 18:58:52 -07001534 return 0;
1535}
1536
Jeff Brown46b9ac02010-04-22 18:58:52 -07001537} /* namespace android */