blob: e29d0a94610c355760b25eb3835f54854c6feaa3 [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"
Michael Wrighta4051212015-07-23 17:04:40 +010030#include <atomic>
31#include <cinttypes>
Jeff Brown349703e2010-06-22 01:27:15 -070032#include <limits.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070033#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070034#include <android_runtime/Log.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080035
Jeff Brown46b9ac02010-04-22 18:58:52 -070036#include <utils/Log.h>
Jeff Brown05dc66a2011-03-02 14:41:58 -080037#include <utils/Looper.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070038#include <utils/threads.h>
Jeff Brown83c09682010-12-23 17:50:18 -080039
Jeff Brownb4ff35d2011-01-02 16:37:43 -080040#include <input/PointerController.h>
Jeff Brown5541de92011-04-11 11:54:25 -070041#include <input/SpriteController.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080042
Michael Wrightd6b473712014-02-10 15:56:36 -080043#include <inputflinger/InputManager.h>
44
Jeff Brown05dc66a2011-03-02 14:41:58 -080045#include <android_os_MessageQueue.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070046#include <android_view_InputDevice.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080047#include <android_view_KeyEvent.h>
48#include <android_view_MotionEvent.h>
49#include <android_view_InputChannel.h>
Jeff Brown2352b972011-04-12 22:39:53 -070050#include <android_view_PointerIcon.h>
Jeff Brownb4ff35d2011-01-02 16:37:43 -080051#include <android/graphics/GraphicsJNI.h>
52
Jeff Brown6ec6f792012-04-17 16:52:41 -070053#include <ScopedLocalRef.h>
Jason Gerecke857aa7b2014-01-27 18:34:20 -080054#include <ScopedPrimitiveArray.h>
Jeff Brown6ec6f792012-04-17 16:52:41 -070055#include <ScopedUtfChars.h>
56
Jeff Brown4f8ecd82012-06-18 18:29:13 -070057#include "com_android_server_power_PowerManagerService.h"
Jeff Brown4532e612012-04-05 14:27:12 -070058#include "com_android_server_input_InputApplicationHandle.h"
59#include "com_android_server_input_InputWindowHandle.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070060
Michael Wrighta4051212015-07-23 17:04:40 +010061#define INDENT " "
62
Jeff Brown46b9ac02010-04-22 18:58:52 -070063namespace android {
64
Jeff Brown1a84fd12011-06-02 01:26:32 -070065// The exponent used to calculate the pointer speed scaling factor.
66// The scaling factor is calculated as 2 ^ (speed * exponent),
67// where the speed ranges from -7 to + 7 and is supplied by the user.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070068static const float POINTER_SPEED_EXPONENT = 1.0f / 4;
Jeff Brown1a84fd12011-06-02 01:26:32 -070069
Jeff Brown46b9ac02010-04-22 18:58:52 -070070static struct {
Jeff Brown46b9ac02010-04-22 18:58:52 -070071 jmethodID notifyConfigurationChanged;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070072 jmethodID notifyInputDevicesChanged;
Jeff Brown53384282012-08-20 20:16:01 -070073 jmethodID notifySwitch;
Jeff Brown7fbdc842010-06-17 20:52:56 -070074 jmethodID notifyInputChannelBroken;
Jeff Brown349703e2010-06-22 01:27:15 -070075 jmethodID notifyANR;
Jeff Brown0029c662011-03-30 02:25:18 -070076 jmethodID filterInputEvent;
Jeff Brown349703e2010-06-22 01:27:15 -070077 jmethodID interceptKeyBeforeQueueing;
Michael Wright70af00a2014-09-03 19:30:20 -070078 jmethodID interceptMotionBeforeQueueingNonInteractive;
Jeff Brown349703e2010-06-22 01:27:15 -070079 jmethodID interceptKeyBeforeDispatching;
Jeff Brown3915bb82010-11-05 15:02:16 -070080 jmethodID dispatchUnhandledKey;
Jeff Brown349703e2010-06-22 01:27:15 -070081 jmethodID checkInjectEventsPermission;
Jeff Brownfe508922011-01-18 15:10:10 -080082 jmethodID getVirtualKeyQuietTimeMillis;
Jeff Brown46b9ac02010-04-22 18:58:52 -070083 jmethodID getExcludedDeviceNames;
Jeff Browna4547672011-03-02 21:38:11 -080084 jmethodID getKeyRepeatTimeout;
85 jmethodID getKeyRepeatDelay;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -070086 jmethodID getHoverTapTimeout;
87 jmethodID getHoverTapSlop;
Jeff Brown214eaf42011-05-26 19:17:02 -070088 jmethodID getDoubleTapTimeout;
89 jmethodID getLongPressTimeout;
Jeff Brown83c09682010-12-23 17:50:18 -080090 jmethodID getPointerLayer;
Jeff Brownb4ff35d2011-01-02 16:37:43 -080091 jmethodID getPointerIcon;
Jeff Brown6ec6f792012-04-17 16:52:41 -070092 jmethodID getKeyboardLayoutOverlay;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070093 jmethodID getDeviceAlias;
Jason Gerecke857aa7b2014-01-27 18:34:20 -080094 jmethodID getTouchCalibrationForInputDevice;
Jeff Brown4532e612012-04-05 14:27:12 -070095} gServiceClassInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -070096
97static struct {
98 jclass clazz;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070099} gInputDeviceClassInfo;
100
101static struct {
102 jclass clazz;
Jeff Brown6ec402b2010-07-28 15:48:59 -0700103} gKeyEventClassInfo;
104
105static struct {
106 jclass clazz;
107} gMotionEventClassInfo;
108
RoboErikfb290df2013-12-16 11:27:55 -0800109static struct {
110 jclass clazz;
111 jmethodID constructor;
112} gInputDeviceIdentifierInfo;
113
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800114static struct {
115 jclass clazz;
116 jmethodID getAffineTransform;
117} gTouchCalibrationClassInfo;
118
RoboErikfb290df2013-12-16 11:27:55 -0800119
Jeff Brown928e0542011-01-10 11:17:36 -0800120
121// --- Global functions ---
122
Jeff Brown214eaf42011-05-26 19:17:02 -0700123template<typename T>
124inline static T min(const T& a, const T& b) {
125 return a < b ? a : b;
126}
127
128template<typename T>
129inline static T max(const T& a, const T& b) {
130 return a > b ? a : b;
131}
132
Michael Wrighta4051212015-07-23 17:04:40 +0100133static inline const char* toString(bool value) {
134 return value ? "true" : "false";
135}
136
Jeff Brown928e0542011-01-10 11:17:36 -0800137static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env,
138 const sp<InputApplicationHandle>& inputApplicationHandle) {
139 if (inputApplicationHandle == NULL) {
140 return NULL;
141 }
142 return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())->
143 getInputApplicationHandleObjLocalRef(env);
144}
145
146static jobject getInputWindowHandleObjLocalRef(JNIEnv* env,
147 const sp<InputWindowHandle>& inputWindowHandle) {
148 if (inputWindowHandle == NULL) {
149 return NULL;
150 }
151 return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())->
152 getInputWindowHandleObjLocalRef(env);
153}
154
Jeff Brown2352b972011-04-12 22:39:53 -0700155static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style,
156 SpriteIcon* outSpriteIcon) {
157 PointerIcon pointerIcon;
158 status_t status = android_view_PointerIcon_loadSystemIcon(env,
159 contextObj, style, &pointerIcon);
160 if (!status) {
Mike Reed4a9c3892014-07-07 15:44:40 -0400161 pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, kN32_SkColorType);
Jeff Brown2352b972011-04-12 22:39:53 -0700162 outSpriteIcon->hotSpotX = pointerIcon.hotSpotX;
163 outSpriteIcon->hotSpotY = pointerIcon.hotSpotY;
164 }
165}
166
Jeff Brown905805a2011-10-12 13:57:59 -0700167enum {
168 WM_ACTION_PASS_TO_USER = 1,
Jeff Brown905805a2011-10-12 13:57:59 -0700169};
170
Jeff Brown928e0542011-01-10 11:17:36 -0800171
172// --- NativeInputManager ---
Jeff Brown83c09682010-12-23 17:50:18 -0800173
Jeff Brown9c3cda02010-06-15 01:31:58 -0700174class NativeInputManager : public virtual RefBase,
175 public virtual InputReaderPolicyInterface,
Jeff Brown2352b972011-04-12 22:39:53 -0700176 public virtual InputDispatcherPolicyInterface,
177 public virtual PointerControllerPolicyInterface {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700178protected:
179 virtual ~NativeInputManager();
180
181public:
Jeff Brown4532e612012-04-05 14:27:12 -0700182 NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700183
184 inline sp<InputManager> getInputManager() const { return mInputManager; }
185
Jeff Brownb88102f2010-09-08 11:49:43 -0700186 void dump(String8& dump);
Jeff Browne33348b2010-07-15 23:54:05 -0700187
Jeff Brownd728bf52012-09-08 18:05:28 -0700188 void setDisplayViewport(bool external, const DisplayViewport& viewport);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700189
Jeff Brown7fbdc842010-06-17 20:52:56 -0700190 status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800191 const sp<InputWindowHandle>& inputWindowHandle, bool monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700192 status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel);
193
Jeff Brown9302c872011-07-13 22:51:29 -0700194 void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray);
195 void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700196 void setInputDispatchMode(bool enabled, bool frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800197 void setSystemUiVisibility(int32_t visibility);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700198 void setPointerSpeed(int32_t speed);
Jeff Browndaf4a122011-08-26 17:14:14 -0700199 void setShowTouches(bool enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700200 void setInteractive(bool interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800201 void reloadCalibration();
Jeff Brown349703e2010-06-22 01:27:15 -0700202
Jeff Brown9c3cda02010-06-15 01:31:58 -0700203 /* --- InputReaderPolicyInterface implementation --- */
204
Jeff Brown214eaf42011-05-26 19:17:02 -0700205 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800206 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700207 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800208 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700209 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700210 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
211 jfloatArray matrixArr);
212 virtual TouchAffineTransformation getTouchAffineTransformation(
213 const String8& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700214
215 /* --- InputDispatcherPolicyInterface implementation --- */
216
Jeff Brownbcc046a2012-09-27 20:46:43 -0700217 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700218 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700219 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700220 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700221 const sp<InputWindowHandle>& inputWindowHandle,
222 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800223 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700224 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700225 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800226 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800227 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700228 virtual nsecs_t interceptKeyBeforeDispatching(
229 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700230 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800231 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800232 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700233 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700234 virtual bool checkInjectEventsPermissionNonReentrant(
235 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700236
Jeff Brown2352b972011-04-12 22:39:53 -0700237 /* --- PointerControllerPolicyInterface implementation --- */
238
239 virtual void loadPointerResources(PointerResources* outResources);
240
Jeff Brown9c3cda02010-06-15 01:31:58 -0700241private:
242 sp<InputManager> mInputManager;
243
Jeff Brown2352b972011-04-12 22:39:53 -0700244 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700245 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800246 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700247
Jeff Brown83c09682010-12-23 17:50:18 -0800248 Mutex mLock;
249 struct Locked {
250 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700251 DisplayViewport internalViewport;
252 DisplayViewport externalViewport;
Jeff Brown83c09682010-12-23 17:50:18 -0800253
Jeff Brown05dc66a2011-03-02 14:41:58 -0800254 // System UI visibility.
255 int32_t systemUiVisibility;
256
Jeff Brown1a84fd12011-06-02 01:26:32 -0700257 // Pointer speed.
258 int32_t pointerSpeed;
259
Jeff Brown474dcb52011-06-14 20:22:50 -0700260 // True if pointer gestures are enabled.
261 bool pointerGesturesEnabled;
262
Jeff Browndaf4a122011-08-26 17:14:14 -0700263 // Show touches feature enable/disable.
264 bool showTouches;
265
Jeff Brown5541de92011-04-11 11:54:25 -0700266 // Sprite controller singleton, created on first use.
267 sp<SpriteController> spriteController;
268
Jeff Brown83c09682010-12-23 17:50:18 -0800269 // Pointer controller singleton, created and destroyed as needed.
270 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800271 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700272
Michael Wrighta4051212015-07-23 17:04:40 +0100273 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700274
Jeff Brown2352b972011-04-12 22:39:53 -0700275 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800276 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700277 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800278
Jeff Brownb88102f2010-09-08 11:49:43 -0700279 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700280
Jeff Brown9c3cda02010-06-15 01:31:58 -0700281 static inline JNIEnv* jniEnv() {
282 return AndroidRuntime::getJNIEnv();
283 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700284};
285
Jeff Brown928e0542011-01-10 11:17:36 -0800286
Jeff Brown9c3cda02010-06-15 01:31:58 -0700287
Jeff Brown2352b972011-04-12 22:39:53 -0700288NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700289 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700290 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700291 JNIEnv* env = jniEnv();
292
Jeff Brown2352b972011-04-12 22:39:53 -0700293 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700294 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700295
Jeff Brown83c09682010-12-23 17:50:18 -0800296 {
297 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800298 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700299 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700300 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700301 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800302 }
Michael Wrighta4051212015-07-23 17:04:40 +0100303 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800304
Jeff Brown9c3cda02010-06-15 01:31:58 -0700305 sp<EventHub> eventHub = new EventHub();
306 mInputManager = new InputManager(eventHub, this, this);
307}
308
309NativeInputManager::~NativeInputManager() {
310 JNIEnv* env = jniEnv();
311
Jeff Brown2352b972011-04-12 22:39:53 -0700312 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700313 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700314}
315
Jeff Brownb88102f2010-09-08 11:49:43 -0700316void NativeInputManager::dump(String8& dump) {
Michael Wrighta4051212015-07-23 17:04:40 +0100317 dump.append("Input Manager State:\n");
318 {
319 dump.appendFormat(INDENT "Interactive: %s\n", toString(mInteractive.load()));
320 }
321 {
322 AutoMutex _l(mLock);
323 dump.appendFormat(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
324 mLocked.systemUiVisibility);
325 dump.appendFormat(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
326 dump.appendFormat(INDENT "Pointer Gestures Enabled: %s\n",
327 toString(mLocked.pointerGesturesEnabled));
328 dump.appendFormat(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
329 }
330 dump.append("\n");
331
Jeff Brownb88102f2010-09-08 11:49:43 -0700332 mInputManager->getReader()->dump(dump);
333 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700334
Jeff Brownb88102f2010-09-08 11:49:43 -0700335 mInputManager->getDispatcher()->dump(dump);
336 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700337}
338
Jeff Brown7fbdc842010-06-17 20:52:56 -0700339bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700340 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000341 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700342 LOGE_EX(env);
343 env->ExceptionClear();
344 return true;
345 }
346 return false;
347}
348
Jeff Brownd728bf52012-09-08 18:05:28 -0700349void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700350 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700351 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700352 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700353
Jeff Brownd728bf52012-09-08 18:05:28 -0700354 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
355 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700356 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700357 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700358
Jeff Brownd728bf52012-09-08 18:05:28 -0700359 if (!external) {
360 sp<PointerController> controller = mLocked.pointerController.promote();
361 if (controller != NULL) {
362 controller->setDisplayViewport(
363 viewport.logicalRight - viewport.logicalLeft,
364 viewport.logicalBottom - viewport.logicalTop,
365 viewport.orientation);
366 }
Jeff Brown2352b972011-04-12 22:39:53 -0700367 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700368 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700369 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700370
371 if (changed) {
372 mInputManager->getReader()->requestRefreshConfiguration(
373 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
374 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700375}
376
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700377status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Jeff Brown928e0542011-01-10 11:17:36 -0800378 const sp<InputChannel>& inputChannel,
379 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
380 return mInputManager->getDispatcher()->registerInputChannel(
381 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700382}
383
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700384status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700385 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700386 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700387}
388
Jeff Brown214eaf42011-05-26 19:17:02 -0700389void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700390 JNIEnv* env = jniEnv();
391
Jeff Brown4532e612012-04-05 14:27:12 -0700392 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
393 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700394 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
395 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
396 }
397
398 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700399 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
400 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700401 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
402 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700403 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700404 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700405 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700406 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700407 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700408 env->DeleteLocalRef(item);
409 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700410 env->DeleteLocalRef(excludedDeviceNames);
411 }
412
Jeff Brown4532e612012-04-05 14:27:12 -0700413 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
414 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700415 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700416 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
417 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700418 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700419 jint longPressTimeout = env->CallIntMethod(mServiceObj,
420 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700421 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700422 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700423
424 // We must ensure that the tap-drag interval is significantly shorter than
425 // the long-press timeout because the tap is held down for the entire duration
426 // of the double-tap timeout.
427 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700428 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700429 outConfig->pointerGestureTapDragInterval =
430 milliseconds_to_nanoseconds(tapDragInterval);
431 }
432 }
433 }
434
Jeff Brown4532e612012-04-05 14:27:12 -0700435 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
436 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700437 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
438 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700439 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700440
441 { // acquire lock
442 AutoMutex _l(mLock);
443
444 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
445 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700446 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700447
Jeff Browndaf4a122011-08-26 17:14:14 -0700448 outConfig->showTouches = mLocked.showTouches;
449
Jeff Brownd728bf52012-09-08 18:05:28 -0700450 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
451 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700452 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700453}
454
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700455sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Jeff Brown83c09682010-12-23 17:50:18 -0800456 AutoMutex _l(mLock);
457
458 sp<PointerController> controller = mLocked.pointerController.promote();
459 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700460 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800461
Jeff Brown2352b972011-04-12 22:39:53 -0700462 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800463 mLocked.pointerController = controller;
464
Jeff Brownd728bf52012-09-08 18:05:28 -0700465 DisplayViewport& v = mLocked.internalViewport;
466 controller->setDisplayViewport(
467 v.logicalRight - v.logicalLeft,
468 v.logicalBottom - v.logicalTop,
469 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800470
Jeff Brown5541de92011-04-11 11:54:25 -0700471 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700472 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
473 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700474 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
475 PointerIcon pointerIcon;
476 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
477 mContextObj, &pointerIcon);
478 if (!status && !pointerIcon.isNullIcon()) {
479 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
480 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
481 } else {
482 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800483 }
Jeff Brown2352b972011-04-12 22:39:53 -0700484 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800485 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800486
Jeff Brown2352b972011-04-12 22:39:53 -0700487 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800488 }
489 return controller;
490}
491
Jeff Brown5541de92011-04-11 11:54:25 -0700492void NativeInputManager::ensureSpriteControllerLocked() {
493 if (mLocked.spriteController == NULL) {
494 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700495 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700496 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
497 layer = -1;
498 }
499 mLocked.spriteController = new SpriteController(mLooper, layer);
500 }
501}
502
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700503void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
504 JNIEnv* env = jniEnv();
505
506 size_t count = inputDevices.size();
507 jobjectArray inputDevicesObjArray = env->NewObjectArray(
508 count, gInputDeviceClassInfo.clazz, NULL);
509 if (inputDevicesObjArray) {
510 bool error = false;
511 for (size_t i = 0; i < count; i++) {
512 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
513 if (!inputDeviceObj) {
514 error = true;
515 break;
516 }
517
518 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
519 env->DeleteLocalRef(inputDeviceObj);
520 }
521
522 if (!error) {
523 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
524 inputDevicesObjArray);
525 }
526
527 env->DeleteLocalRef(inputDevicesObjArray);
528 }
529
530 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
531}
532
Jeff Brown6ec6f792012-04-17 16:52:41 -0700533sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800534 const InputDeviceIdentifier& identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700535 JNIEnv* env = jniEnv();
536
537 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800538 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
539 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
540 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
541 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700542 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800543 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700544 if (arrayObj.get()) {
545 ScopedLocalRef<jstring> filenameObj(env,
546 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
547 ScopedLocalRef<jstring> contentsObj(env,
548 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
549 ScopedUtfChars filenameChars(env, filenameObj.get());
550 ScopedUtfChars contentsChars(env, contentsObj.get());
551
552 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
553 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
554 }
555 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
556 return result;
557}
558
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700559String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
560 JNIEnv* env = jniEnv();
561
562 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
563 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
564 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
565 String8 result;
566 if (aliasObj.get()) {
567 ScopedUtfChars aliasChars(env, aliasObj.get());
568 result.setTo(aliasChars.c_str());
569 }
570 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
571 return result;
572}
573
Jeff Brownbcc046a2012-09-27 20:46:43 -0700574void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700575 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700576#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700577 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
578 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700579#endif
580
581 JNIEnv* env = jniEnv();
582
Jeff Brown53384282012-08-20 20:16:01 -0700583 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700584 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700585 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700586}
587
Jeff Brown9c3cda02010-06-15 01:31:58 -0700588void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
589#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000590 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700591#endif
592
593 JNIEnv* env = jniEnv();
594
Jeff Brown4532e612012-04-05 14:27:12 -0700595 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700596 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700597}
598
Jeff Brown519e0242010-09-15 15:18:56 -0700599nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700600 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700601#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000602 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700603#endif
604
605 JNIEnv* env = jniEnv();
606
Jeff Brown928e0542011-01-10 11:17:36 -0800607 jobject inputApplicationHandleObj =
608 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
609 jobject inputWindowHandleObj =
610 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700611 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700612
Jeff Brown4532e612012-04-05 14:27:12 -0700613 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700614 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
615 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700616 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
617 newTimeout = 0; // abort dispatch
618 } else {
619 assert(newTimeout >= 0);
620 }
621
Jeff Brownbd181bb2013-09-10 16:44:24 -0700622 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800623 env->DeleteLocalRef(inputWindowHandleObj);
624 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700625 return newTimeout;
626}
627
Jeff Brown928e0542011-01-10 11:17:36 -0800628void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700629#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000630 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700631#endif
632
Jeff Brown7fbdc842010-06-17 20:52:56 -0700633 JNIEnv* env = jniEnv();
634
Jeff Brown928e0542011-01-10 11:17:36 -0800635 jobject inputWindowHandleObj =
636 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
637 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700638 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800639 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700640 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
641
Jeff Brown928e0542011-01-10 11:17:36 -0800642 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700643 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700644}
645
Jeff Brown214eaf42011-05-26 19:17:02 -0700646void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
647 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800648
Jeff Brown4532e612012-04-05 14:27:12 -0700649 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
650 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700651 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
652 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
653 }
Jeff Browna4547672011-03-02 21:38:11 -0800654
Jeff Brown4532e612012-04-05 14:27:12 -0700655 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
656 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700657 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
658 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
659 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700660}
661
Jeff Brown9302c872011-07-13 22:51:29 -0700662void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
663 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700664
Jeff Brown9302c872011-07-13 22:51:29 -0700665 if (windowHandleObjArray) {
666 jsize length = env->GetArrayLength(windowHandleObjArray);
667 for (jsize i = 0; i < length; i++) {
668 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
669 if (! windowHandleObj) {
670 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700671 }
Jeff Brown9302c872011-07-13 22:51:29 -0700672
673 sp<InputWindowHandle> windowHandle =
674 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
675 if (windowHandle != NULL) {
676 windowHandles.push(windowHandle);
677 }
678 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700679 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700680 }
Jeff Brown349703e2010-06-22 01:27:15 -0700681
Jeff Brown9302c872011-07-13 22:51:29 -0700682 mInputManager->getDispatcher()->setInputWindows(windowHandles);
683
684 // Do this after the dispatcher has updated the window handle state.
685 bool newPointerGesturesEnabled = true;
686 size_t numWindows = windowHandles.size();
687 for (size_t i = 0; i < numWindows; i++) {
688 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700689 const InputWindowInfo* windowInfo = windowHandle->getInfo();
690 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
691 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700692 newPointerGesturesEnabled = false;
693 }
694 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700695
696 uint32_t changes = 0;
697 { // acquire lock
698 AutoMutex _l(mLock);
699
700 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
701 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
702 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
703 }
704 } // release lock
705
706 if (changes) {
707 mInputManager->getReader()->requestRefreshConfiguration(changes);
708 }
Jeff Brown349703e2010-06-22 01:27:15 -0700709}
710
Jeff Brown9302c872011-07-13 22:51:29 -0700711void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
712 sp<InputApplicationHandle> applicationHandle =
713 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
714 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700715}
716
717void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700718 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700719}
720
Jeff Brown05dc66a2011-03-02 14:41:58 -0800721void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
722 AutoMutex _l(mLock);
723
724 if (mLocked.systemUiVisibility != visibility) {
725 mLocked.systemUiVisibility = visibility;
726
727 sp<PointerController> controller = mLocked.pointerController.promote();
728 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700729 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800730 }
731 }
732}
733
Jeff Brown2352b972011-04-12 22:39:53 -0700734void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800735 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700736 controller->setInactivityTimeout(lightsOut
737 ? PointerController::INACTIVITY_TIMEOUT_SHORT
738 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800739}
740
Jeff Brown1a84fd12011-06-02 01:26:32 -0700741void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700742 { // acquire lock
743 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700744
Jeff Brown474dcb52011-06-14 20:22:50 -0700745 if (mLocked.pointerSpeed == speed) {
746 return;
747 }
748
Steve Block6215d3f2012-01-04 20:05:49 +0000749 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700750 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700751 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700752
Jeff Brown474dcb52011-06-14 20:22:50 -0700753 mInputManager->getReader()->requestRefreshConfiguration(
754 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700755}
756
Jeff Browndaf4a122011-08-26 17:14:14 -0700757void NativeInputManager::setShowTouches(bool enabled) {
758 { // acquire lock
759 AutoMutex _l(mLock);
760
761 if (mLocked.showTouches == enabled) {
762 return;
763 }
764
Steve Block6215d3f2012-01-04 20:05:49 +0000765 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700766 mLocked.showTouches = enabled;
767 } // release lock
768
769 mInputManager->getReader()->requestRefreshConfiguration(
770 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
771}
772
Jeff Brown037c33e2014-04-09 00:31:55 -0700773void NativeInputManager::setInteractive(bool interactive) {
774 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700775}
776
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800777void NativeInputManager::reloadCalibration() {
778 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100779 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800780}
781
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800782TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
783 JNIEnv *env, jfloatArray matrixArr) {
784 ScopedFloatArrayRO matrix(env, matrixArr);
785 assert(matrix.size() == 6);
786
787 TouchAffineTransformation transform;
788 transform.x_scale = matrix[0];
789 transform.x_ymix = matrix[1];
790 transform.x_offset = matrix[2];
791 transform.y_xmix = matrix[3];
792 transform.y_scale = matrix[4];
793 transform.y_offset = matrix[5];
794
795 return transform;
796}
797
798TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700799 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800800 JNIEnv* env = jniEnv();
801
802 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
803
804 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700805 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
806 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800807
808 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
809 gTouchCalibrationClassInfo.getAffineTransform));
810
811 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
812
813 env->DeleteLocalRef(matrixArr);
814 env->DeleteLocalRef(cal);
815
816 return transform;
817}
818
Jeff Brown0029c662011-03-30 02:25:18 -0700819bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
820 jobject inputEventObj;
821
822 JNIEnv* env = jniEnv();
823 switch (inputEvent->getType()) {
824 case AINPUT_EVENT_TYPE_KEY:
825 inputEventObj = android_view_KeyEvent_fromNative(env,
826 static_cast<const KeyEvent*>(inputEvent));
827 break;
828 case AINPUT_EVENT_TYPE_MOTION:
829 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
830 static_cast<const MotionEvent*>(inputEvent));
831 break;
832 default:
833 return true; // dispatch the event normally
834 }
835
836 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000837 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700838 return true; // dispatch the event normally
839 }
840
841 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700842 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700843 inputEventObj, policyFlags);
844 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
845 pass = true;
846 }
847 env->DeleteLocalRef(inputEventObj);
848 return pass;
849}
850
Jeff Brown1f245102010-11-18 20:53:46 -0800851void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
852 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700853 // Policy:
854 // - Ignore untrusted events and pass them along.
855 // - Ask the window manager what to do with normal events and trusted injected events.
856 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100857 bool interactive = mInteractive.load();
858 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700859 policyFlags |= POLICY_FLAG_INTERACTIVE;
860 }
Jeff Brown3122e442010-10-11 23:32:49 -0700861 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800862 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700863 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800864 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
865 jint wmActions;
866 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700867 wmActions = env->CallIntMethod(mServiceObj,
868 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700869 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800870 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
871 wmActions = 0;
872 }
873 android_view_KeyEvent_recycle(env, keyEventObj);
874 env->DeleteLocalRef(keyEventObj);
875 } else {
Steve Block3762c312012-01-06 19:20:56 +0000876 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700877 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700878 }
879
Jeff Brown56194eb2011-03-02 19:23:13 -0800880 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700881 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100882 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -0700883 policyFlags |= POLICY_FLAG_PASS_TO_USER;
884 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700885 }
886}
887
Jeff Brown56194eb2011-03-02 19:23:13 -0800888void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700889 // Policy:
890 // - Ignore untrusted events and pass them along.
891 // - No special filtering for injected events required at this time.
892 // - Filter normal events based on screen state.
893 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100894 bool interactive = mInteractive.load();
895 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700896 policyFlags |= POLICY_FLAG_INTERACTIVE;
897 }
Jeff Brown3122e442010-10-11 23:32:49 -0700898 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700899 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -0700900 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -0700901 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -0800902 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700903 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -0700904 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -0800905 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800906 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -0700907 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800908 wmActions = 0;
909 }
910
Jeff Brown56194eb2011-03-02 19:23:13 -0800911 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700912 }
Jeff Brown3122e442010-10-11 23:32:49 -0700913 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100914 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -0700915 policyFlags |= POLICY_FLAG_PASS_TO_USER;
916 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700917 }
918}
919
Jeff Brown56194eb2011-03-02 19:23:13 -0800920void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
921 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800922 if (wmActions & WM_ACTION_PASS_TO_USER) {
923 policyFlags |= POLICY_FLAG_PASS_TO_USER;
924 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800925#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000926 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800927#endif
928 }
929}
930
Jeff Brown905805a2011-10-12 13:57:59 -0700931nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800932 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700933 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700934 // Policy:
935 // - Ignore untrusted events and pass them along.
936 // - Filter normal events and trusted injected events through the window manager policy to
937 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700938 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700939 if (policyFlags & POLICY_FLAG_TRUSTED) {
940 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700941
Jeff Brown928e0542011-01-10 11:17:36 -0800942 // Note: inputWindowHandle may be null.
943 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800944 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
945 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700946 jlong delayMillis = env->CallLongMethod(mServiceObj,
947 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800948 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800949 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
950 android_view_KeyEvent_recycle(env, keyEventObj);
951 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700952 if (!error) {
953 if (delayMillis < 0) {
954 result = -1;
955 } else if (delayMillis > 0) {
956 result = milliseconds_to_nanoseconds(delayMillis);
957 }
958 }
Jeff Brown1f245102010-11-18 20:53:46 -0800959 } else {
Steve Block3762c312012-01-06 19:20:56 +0000960 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800961 }
Jeff Brown928e0542011-01-10 11:17:36 -0800962 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700963 }
Jeff Brown1f245102010-11-18 20:53:46 -0800964 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700965}
966
Jeff Brown928e0542011-01-10 11:17:36 -0800967bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800968 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700969 // Policy:
970 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800971 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700972 if (policyFlags & POLICY_FLAG_TRUSTED) {
973 JNIEnv* env = jniEnv();
974
Jeff Brown928e0542011-01-10 11:17:36 -0800975 // Note: inputWindowHandle may be null.
976 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800977 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
978 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700979 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
980 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800981 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700982 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
983 fallbackKeyEventObj = NULL;
984 }
Jeff Brown1f245102010-11-18 20:53:46 -0800985 android_view_KeyEvent_recycle(env, keyEventObj);
986 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800987
988 if (fallbackKeyEventObj) {
989 // Note: outFallbackKeyEvent may be the same object as keyEvent.
990 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
991 outFallbackKeyEvent)) {
992 result = true;
993 }
994 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
995 env->DeleteLocalRef(fallbackKeyEventObj);
996 }
Jeff Brown1f245102010-11-18 20:53:46 -0800997 } else {
Steve Block3762c312012-01-06 19:20:56 +0000998 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -0800999 }
Jeff Brown928e0542011-01-10 11:17:36 -08001000 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001001 }
Jeff Brown1f245102010-11-18 20:53:46 -08001002 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001003}
1004
Jeff Brown01ce2e92010-09-26 22:20:12 -07001005void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
1006 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001007}
1008
Jeff Brown349703e2010-06-22 01:27:15 -07001009
Jeff Brownb88102f2010-09-08 11:49:43 -07001010bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1011 int32_t injectorPid, int32_t injectorUid) {
1012 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001013 jboolean result = env->CallBooleanMethod(mServiceObj,
1014 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001015 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1016 result = false;
1017 }
Jeff Brown349703e2010-06-22 01:27:15 -07001018 return result;
1019}
1020
Jeff Brown2352b972011-04-12 22:39:53 -07001021void NativeInputManager::loadPointerResources(PointerResources* outResources) {
1022 JNIEnv* env = jniEnv();
1023
1024 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1025 &outResources->spotHover);
1026 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1027 &outResources->spotTouch);
1028 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1029 &outResources->spotAnchor);
1030}
1031
Jeff Brown83c09682010-12-23 17:50:18 -08001032
Jeff Brown9c3cda02010-06-15 01:31:58 -07001033// ----------------------------------------------------------------------------
1034
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001035static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001036 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001037 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001038 if (messageQueue == NULL) {
1039 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1040 return 0;
1041 }
1042
Jeff Brown603b4452012-04-06 17:39:41 -07001043 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1044 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001045 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001046 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001047}
1048
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001049static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001050 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001051
Jeff Brown4532e612012-04-05 14:27:12 -07001052 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001053 if (result) {
1054 jniThrowRuntimeException(env, "Input manager could not be started.");
1055 }
1056}
1057
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001058static void nativeSetDisplayViewport(JNIEnv* /* env */, jclass /* clazz */, jlong ptr,
1059 jboolean external, jint displayId, jint orientation,
Jeff Brownd728bf52012-09-08 18:05:28 -07001060 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001061 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
1062 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001063 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001064
Jeff Brownd728bf52012-09-08 18:05:28 -07001065 DisplayViewport v;
1066 v.displayId = displayId;
1067 v.orientation = orientation;
1068 v.logicalLeft = logicalLeft;
1069 v.logicalTop = logicalTop;
1070 v.logicalRight = logicalRight;
1071 v.logicalBottom = logicalBottom;
1072 v.physicalLeft = physicalLeft;
1073 v.physicalTop = physicalTop;
1074 v.physicalRight = physicalRight;
1075 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001076 v.deviceWidth = deviceWidth;
1077 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -07001078 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001079}
1080
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001081static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001082 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001083 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001084
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001085 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001086 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001087}
1088
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001089static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001090 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001091 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001092
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001093 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001094 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001095}
1096
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001097static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001098 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001099 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001100
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001101 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001102 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001103}
1104
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001105static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001106 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001107 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001108
1109 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1110 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1111 jsize numCodes = env->GetArrayLength(keyCodes);
1112 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001113 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001114 if (im->getInputManager()->getReader()->hasKeys(
1115 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1116 result = JNI_TRUE;
1117 } else {
1118 result = JNI_FALSE;
1119 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001120 } else {
1121 result = JNI_FALSE;
1122 }
1123
1124 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1125 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1126 return result;
1127}
1128
1129static void throwInputChannelNotInitialized(JNIEnv* env) {
1130 jniThrowException(env, "java/lang/IllegalStateException",
1131 "inputChannel is not initialized");
1132}
1133
Jeff Brown4532e612012-04-05 14:27:12 -07001134static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001135 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001136 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1137
Steve Block8564c8d2012-01-05 23:22:43 +00001138 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001139 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001140 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001141}
1142
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001143static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001144 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001145 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001146
1147 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1148 inputChannelObj);
1149 if (inputChannel == NULL) {
1150 throwInputChannelNotInitialized(env);
1151 return;
1152 }
1153
Jeff Brown928e0542011-01-10 11:17:36 -08001154 sp<InputWindowHandle> inputWindowHandle =
1155 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001156
Jeff Brown4532e612012-04-05 14:27:12 -07001157 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001158 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001159 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001160 String8 message;
1161 message.appendFormat("Failed to register input channel. status=%d", status);
1162 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001163 return;
1164 }
1165
Jeff Browna41ca772010-08-11 14:46:32 -07001166 if (! monitor) {
1167 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001168 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001169 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001170}
1171
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001172static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001173 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001174 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001175
1176 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1177 inputChannelObj);
1178 if (inputChannel == NULL) {
1179 throwInputChannelNotInitialized(env);
1180 return;
1181 }
1182
1183 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1184
Jeff Brown4532e612012-04-05 14:27:12 -07001185 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001186 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1187 String8 message;
1188 message.appendFormat("Failed to unregister input channel. status=%d", status);
1189 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001190 }
1191}
1192
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001193static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001194 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001195 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001196
Jeff Brown4532e612012-04-05 14:27:12 -07001197 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001198}
1199
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001200static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Jeff Brownca9bc702014-02-11 14:32:56 -08001201 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001202 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001203 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001204
Jeff Brown6ec402b2010-07-28 15:48:59 -07001205 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1206 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001207 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1208 if (status) {
1209 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1210 return INPUT_EVENT_INJECTION_FAILED;
1211 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001212
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001213 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001214 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001215 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001216 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001217 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1218 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001219 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1220 return INPUT_EVENT_INJECTION_FAILED;
1221 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001222
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001223 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001224 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001225 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001226 } else {
1227 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001228 return INPUT_EVENT_INJECTION_FAILED;
1229 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001230}
1231
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001232static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001233 jlong ptr, jobjectArray windowHandleObjArray) {
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->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001237}
1238
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001239static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001240 jlong ptr, jobject applicationHandleObj) {
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->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001244}
1245
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001246static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1247 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001248 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001249
Jeff Brown4532e612012-04-05 14:27:12 -07001250 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001251}
1252
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001253static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1254 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001255 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001256
Jeff Brown4532e612012-04-05 14:27:12 -07001257 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001258}
1259
Jeff Brown4532e612012-04-05 14:27:12 -07001260static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001261 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001262 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001263
1264 sp<InputChannel> fromChannel =
1265 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1266 sp<InputChannel> toChannel =
1267 android_view_InputChannel_getInputChannel(env, toChannelObj);
1268
1269 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001270 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001271 }
1272
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001273 if (im->getInputManager()->getDispatcher()->
1274 transferTouchFocus(fromChannel, toChannel)) {
1275 return JNI_TRUE;
1276 } else {
1277 return JNI_FALSE;
1278 }
Jeff Browne6504122010-09-27 14:52:15 -07001279}
1280
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001281static void nativeSetPointerSpeed(JNIEnv* /* env */,
1282 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001283 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001284
Jeff Brown4532e612012-04-05 14:27:12 -07001285 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001286}
1287
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001288static void nativeSetShowTouches(JNIEnv* /* env */,
1289 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001290 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001291
Jeff Brown4532e612012-04-05 14:27:12 -07001292 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001293}
1294
Jeff Brown037c33e2014-04-09 00:31:55 -07001295static void nativeSetInteractive(JNIEnv* env,
1296 jclass clazz, jlong ptr, jboolean interactive) {
1297 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1298
1299 im->setInteractive(interactive);
1300}
1301
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001302static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1303 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1304 im->reloadCalibration();
1305}
1306
Jeff Browna47425a2012-04-13 04:09:27 -07001307static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001308 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001309 jint repeat, jint token) {
1310 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1311
1312 size_t patternSize = env->GetArrayLength(patternObj);
1313 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001314 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001315 "which is more than the maximum supported size of %d.",
1316 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1317 return; // limit to reasonable size
1318 }
1319
1320 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1321 patternObj, NULL));
1322 nsecs_t pattern[patternSize];
1323 for (size_t i = 0; i < patternSize; i++) {
1324 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001325 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001326 }
1327 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1328
1329 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1330}
1331
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001332static void nativeCancelVibrate(JNIEnv* /* env */,
1333 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001334 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1335
1336 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1337}
1338
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001339static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1340 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001341 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1342
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001343 im->getInputManager()->getReader()->requestRefreshConfiguration(
1344 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1345}
1346
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001347static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1348 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001349 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1350
1351 im->getInputManager()->getReader()->requestRefreshConfiguration(
1352 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001353}
1354
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001355static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001356 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001357
Jeff Brownb88102f2010-09-08 11:49:43 -07001358 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001359 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001360 return env->NewStringUTF(dump.string());
1361}
1362
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001363static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001364 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001365
Jeff Brown4532e612012-04-05 14:27:12 -07001366 im->getInputManager()->getReader()->monitor();
1367 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001368}
1369
Jeff Brown9c3cda02010-06-15 01:31:58 -07001370// ----------------------------------------------------------------------------
1371
Jeff Brown46b9ac02010-04-22 18:58:52 -07001372static JNINativeMethod gInputManagerMethods[] = {
1373 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001374 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001375 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001376 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001377 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001378 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001379 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001380 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001381 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001382 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001383 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001384 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001385 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001386 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001387 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001388 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001389 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001390 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001391 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001392 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001393 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001394 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001395 (void*) nativeSetInputFilterEnabled },
Jeff Brownca9bc702014-02-11 14:32:56 -08001396 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001397 (void*) nativeInjectInputEvent },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001398 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001399 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001400 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001401 (void*) nativeSetFocusedApplication },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001402 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001403 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001404 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001405 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001406 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001407 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001408 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001409 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001410 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001411 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001412 { "nativeSetInteractive", "(JZ)V",
1413 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001414 { "nativeReloadCalibration", "(J)V",
1415 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001416 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001417 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001418 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001419 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001420 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001421 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001422 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001423 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001424 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001425 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001426 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001427 (void*) nativeMonitor },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001428};
1429
1430#define FIND_CLASS(var, className) \
1431 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001432 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001433
1434#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1435 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1436 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1437
1438#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1439 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1440 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1441
1442int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001443 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001444 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001445 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001446 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1447
Jeff Brown9c3cda02010-06-15 01:31:58 -07001448 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001449
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001450 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001451 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001452
Jeff Brown4532e612012-04-05 14:27:12 -07001453 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001454 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001455
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001456 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1457 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1458
Jeff Brown53384282012-08-20 20:16:01 -07001459 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1460 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001461
Jeff Brown4532e612012-04-05 14:27:12 -07001462 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1463 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001464
Jeff Brown4532e612012-04-05 14:27:12 -07001465 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001466 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001467 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001468
Jeff Brown4532e612012-04-05 14:27:12 -07001469 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001470 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1471
Jeff Brown4532e612012-04-05 14:27:12 -07001472 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001473 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001474
Michael Wright70af00a2014-09-03 19:30:20 -07001475 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1476 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001477
Jeff Brown4532e612012-04-05 14:27:12 -07001478 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001479 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001480 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001481
Jeff Brown4532e612012-04-05 14:27:12 -07001482 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001483 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001484 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001485
Jeff Brown4532e612012-04-05 14:27:12 -07001486 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001487 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001488
Jeff Brown4532e612012-04-05 14:27:12 -07001489 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001490 "getVirtualKeyQuietTimeMillis", "()I");
1491
Jeff Brown4532e612012-04-05 14:27:12 -07001492 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001493 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1494
Jeff Brown4532e612012-04-05 14:27:12 -07001495 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001496 "getKeyRepeatTimeout", "()I");
1497
Jeff Brown4532e612012-04-05 14:27:12 -07001498 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001499 "getKeyRepeatDelay", "()I");
1500
Jeff Brown4532e612012-04-05 14:27:12 -07001501 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001502 "getHoverTapTimeout", "()I");
1503
Jeff Brown4532e612012-04-05 14:27:12 -07001504 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001505 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001506
Jeff Brown4532e612012-04-05 14:27:12 -07001507 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001508 "getDoubleTapTimeout", "()I");
1509
Jeff Brown4532e612012-04-05 14:27:12 -07001510 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001511 "getLongPressTimeout", "()I");
1512
Jeff Brown4532e612012-04-05 14:27:12 -07001513 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001514 "getPointerLayer", "()I");
1515
Jeff Brown4532e612012-04-05 14:27:12 -07001516 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001517 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001518
Jeff Brown6ec6f792012-04-17 16:52:41 -07001519 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001520 "getKeyboardLayoutOverlay",
1521 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001522
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001523 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1524 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1525
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001526 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1527 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001528 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001529
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001530 // InputDevice
1531
1532 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1533 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1534
Jeff Brown6ec402b2010-07-28 15:48:59 -07001535 // KeyEvent
1536
1537 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001538 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1539
Jeff Brown8d608662010-08-30 03:02:23 -07001540 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001541
1542 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001543 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001544
RoboErikfb290df2013-12-16 11:27:55 -08001545 // InputDeviceIdentifier
1546
1547 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1548 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1549 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1550 "<init>", "(Ljava/lang/String;II)V");
1551
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001552 // TouchCalibration
1553
1554 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1555 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1556
1557 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1558 "getAffineTransform", "()[F");
1559
Jeff Brown46b9ac02010-04-22 18:58:52 -07001560 return 0;
1561}
1562
Jeff Brown46b9ac02010-04-22 18:58:52 -07001563} /* namespace android */