blob: 8cb0a1310ec52c49697663d978271f00907c2cf0 [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();
Jun Mukai1db53972015-09-11 18:08:31 -0700202 void setPointerIconShape(int32_t iconId);
Jeff Brown349703e2010-06-22 01:27:15 -0700203
Jeff Brown9c3cda02010-06-15 01:31:58 -0700204 /* --- InputReaderPolicyInterface implementation --- */
205
Jeff Brown214eaf42011-05-26 19:17:02 -0700206 virtual void getReaderConfiguration(InputReaderConfiguration* outConfig);
Jeff Brown83c09682010-12-23 17:50:18 -0800207 virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId);
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700208 virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices);
RoboErikfb290df2013-12-16 11:27:55 -0800209 virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const InputDeviceIdentifier& identifier);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700210 virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier);
Jason Gerecked5220742014-03-10 09:47:59 -0700211 virtual TouchAffineTransformation getTouchAffineTransformation(JNIEnv *env,
212 jfloatArray matrixArr);
213 virtual TouchAffineTransformation getTouchAffineTransformation(
214 const String8& inputDeviceDescriptor, int32_t surfaceRotation);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700215
216 /* --- InputDispatcherPolicyInterface implementation --- */
217
Jeff Brownbcc046a2012-09-27 20:46:43 -0700218 virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,
Jeff Browne20c9e02010-10-11 14:20:19 -0700219 uint32_t policyFlags);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700220 virtual void notifyConfigurationChanged(nsecs_t when);
Jeff Brown519e0242010-09-15 15:18:56 -0700221 virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700222 const sp<InputWindowHandle>& inputWindowHandle,
223 const String8& reason);
Jeff Brown928e0542011-01-10 11:17:36 -0800224 virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle);
Jeff Brown0029c662011-03-30 02:25:18 -0700225 virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags);
Jeff Brown214eaf42011-05-26 19:17:02 -0700226 virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig);
Jeff Brown1f245102010-11-18 20:53:46 -0800227 virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800228 virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags);
Jeff Brown905805a2011-10-12 13:57:59 -0700229 virtual nsecs_t interceptKeyBeforeDispatching(
230 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brownb88102f2010-09-08 11:49:43 -0700231 const KeyEvent* keyEvent, uint32_t policyFlags);
Jeff Brown928e0542011-01-10 11:17:36 -0800232 virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800233 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent);
Jeff Brown01ce2e92010-09-26 22:20:12 -0700234 virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType);
Jeff Brownb88102f2010-09-08 11:49:43 -0700235 virtual bool checkInjectEventsPermissionNonReentrant(
236 int32_t injectorPid, int32_t injectorUid);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700237
Jeff Brown2352b972011-04-12 22:39:53 -0700238 /* --- PointerControllerPolicyInterface implementation --- */
239
240 virtual void loadPointerResources(PointerResources* outResources);
Jun Mukai1db53972015-09-11 18:08:31 -0700241 virtual void loadAdditionalMouseResources(std::map<int, SpriteIcon>* outResources);
Jeff Brown2352b972011-04-12 22:39:53 -0700242
Jeff Brown9c3cda02010-06-15 01:31:58 -0700243private:
244 sp<InputManager> mInputManager;
245
Jeff Brown2352b972011-04-12 22:39:53 -0700246 jobject mContextObj;
Jeff Brown4532e612012-04-05 14:27:12 -0700247 jobject mServiceObj;
Jeff Brown05dc66a2011-03-02 14:41:58 -0800248 sp<Looper> mLooper;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700249
Jeff Brown83c09682010-12-23 17:50:18 -0800250 Mutex mLock;
251 struct Locked {
252 // Display size information.
Jeff Brownd728bf52012-09-08 18:05:28 -0700253 DisplayViewport internalViewport;
254 DisplayViewport externalViewport;
Jeff Brown83c09682010-12-23 17:50:18 -0800255
Jeff Brown05dc66a2011-03-02 14:41:58 -0800256 // System UI visibility.
257 int32_t systemUiVisibility;
258
Jeff Brown1a84fd12011-06-02 01:26:32 -0700259 // Pointer speed.
260 int32_t pointerSpeed;
261
Jeff Brown474dcb52011-06-14 20:22:50 -0700262 // True if pointer gestures are enabled.
263 bool pointerGesturesEnabled;
264
Jeff Browndaf4a122011-08-26 17:14:14 -0700265 // Show touches feature enable/disable.
266 bool showTouches;
267
Jeff Brown5541de92011-04-11 11:54:25 -0700268 // Sprite controller singleton, created on first use.
269 sp<SpriteController> spriteController;
270
Jeff Brown83c09682010-12-23 17:50:18 -0800271 // Pointer controller singleton, created and destroyed as needed.
272 wp<PointerController> pointerController;
Jeff Brown83c09682010-12-23 17:50:18 -0800273 } mLocked;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700274
Michael Wrighta4051212015-07-23 17:04:40 +0100275 std::atomic<bool> mInteractive;
Jeff Brown037c33e2014-04-09 00:31:55 -0700276
Jeff Brown2352b972011-04-12 22:39:53 -0700277 void updateInactivityTimeoutLocked(const sp<PointerController>& controller);
Jeff Brown56194eb2011-03-02 19:23:13 -0800278 void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags);
Jeff Brown5541de92011-04-11 11:54:25 -0700279 void ensureSpriteControllerLocked();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800280
Jeff Brownb88102f2010-09-08 11:49:43 -0700281 static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName);
Jeff Browna41ca772010-08-11 14:46:32 -0700282
Jeff Brown9c3cda02010-06-15 01:31:58 -0700283 static inline JNIEnv* jniEnv() {
284 return AndroidRuntime::getJNIEnv();
285 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700286};
287
Jeff Brown928e0542011-01-10 11:17:36 -0800288
Jeff Brown9c3cda02010-06-15 01:31:58 -0700289
Jeff Brown2352b972011-04-12 22:39:53 -0700290NativeInputManager::NativeInputManager(jobject contextObj,
Jeff Brown4532e612012-04-05 14:27:12 -0700291 jobject serviceObj, const sp<Looper>& looper) :
Jeff Brown037c33e2014-04-09 00:31:55 -0700292 mLooper(looper), mInteractive(true) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700293 JNIEnv* env = jniEnv();
294
Jeff Brown2352b972011-04-12 22:39:53 -0700295 mContextObj = env->NewGlobalRef(contextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700296 mServiceObj = env->NewGlobalRef(serviceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700297
Jeff Brown83c09682010-12-23 17:50:18 -0800298 {
299 AutoMutex _l(mLock);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800300 mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE;
Jeff Brown1a84fd12011-06-02 01:26:32 -0700301 mLocked.pointerSpeed = 0;
Jeff Brown474dcb52011-06-14 20:22:50 -0700302 mLocked.pointerGesturesEnabled = true;
Jeff Browndaf4a122011-08-26 17:14:14 -0700303 mLocked.showTouches = false;
Jeff Brown83c09682010-12-23 17:50:18 -0800304 }
Michael Wrighta4051212015-07-23 17:04:40 +0100305 mInteractive = true;
Jeff Brown83c09682010-12-23 17:50:18 -0800306
Jeff Brown9c3cda02010-06-15 01:31:58 -0700307 sp<EventHub> eventHub = new EventHub();
308 mInputManager = new InputManager(eventHub, this, this);
309}
310
311NativeInputManager::~NativeInputManager() {
312 JNIEnv* env = jniEnv();
313
Jeff Brown2352b972011-04-12 22:39:53 -0700314 env->DeleteGlobalRef(mContextObj);
Jeff Brown4532e612012-04-05 14:27:12 -0700315 env->DeleteGlobalRef(mServiceObj);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700316}
317
Jeff Brownb88102f2010-09-08 11:49:43 -0700318void NativeInputManager::dump(String8& dump) {
Michael Wrighta4051212015-07-23 17:04:40 +0100319 dump.append("Input Manager State:\n");
320 {
321 dump.appendFormat(INDENT "Interactive: %s\n", toString(mInteractive.load()));
322 }
323 {
324 AutoMutex _l(mLock);
325 dump.appendFormat(INDENT "System UI Visibility: 0x%0" PRIx32 "\n",
326 mLocked.systemUiVisibility);
327 dump.appendFormat(INDENT "Pointer Speed: %" PRId32 "\n", mLocked.pointerSpeed);
328 dump.appendFormat(INDENT "Pointer Gestures Enabled: %s\n",
329 toString(mLocked.pointerGesturesEnabled));
330 dump.appendFormat(INDENT "Show Touches: %s\n", toString(mLocked.showTouches));
331 }
332 dump.append("\n");
333
Jeff Brownb88102f2010-09-08 11:49:43 -0700334 mInputManager->getReader()->dump(dump);
335 dump.append("\n");
Jeff Brown6d0fec22010-07-23 21:28:06 -0700336
Jeff Brownb88102f2010-09-08 11:49:43 -0700337 mInputManager->getDispatcher()->dump(dump);
338 dump.append("\n");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700339}
340
Jeff Brown7fbdc842010-06-17 20:52:56 -0700341bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700342 if (env->ExceptionCheck()) {
Steve Block3762c312012-01-06 19:20:56 +0000343 ALOGE("An exception was thrown by callback '%s'.", methodName);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700344 LOGE_EX(env);
345 env->ExceptionClear();
346 return true;
347 }
348 return false;
349}
350
Jeff Brownd728bf52012-09-08 18:05:28 -0700351void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700352 bool changed = false;
Jeff Brownd728bf52012-09-08 18:05:28 -0700353 {
Jeff Brown65fd2512011-08-18 11:20:58 -0700354 AutoMutex _l(mLock);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700355
Jeff Brownd728bf52012-09-08 18:05:28 -0700356 DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport;
357 if (v != viewport) {
Jeff Brown65fd2512011-08-18 11:20:58 -0700358 changed = true;
Jeff Brownd728bf52012-09-08 18:05:28 -0700359 v = viewport;
Jeff Brownbc68a592011-07-25 12:58:12 -0700360
Jeff Brownd728bf52012-09-08 18:05:28 -0700361 if (!external) {
362 sp<PointerController> controller = mLocked.pointerController.promote();
363 if (controller != NULL) {
364 controller->setDisplayViewport(
365 viewport.logicalRight - viewport.logicalLeft,
366 viewport.logicalBottom - viewport.logicalTop,
367 viewport.orientation);
368 }
Jeff Brown2352b972011-04-12 22:39:53 -0700369 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700370 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700371 }
Jeff Brown65fd2512011-08-18 11:20:58 -0700372
373 if (changed) {
374 mInputManager->getReader()->requestRefreshConfiguration(
375 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
376 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700377}
378
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700379status_t NativeInputManager::registerInputChannel(JNIEnv* /* env */,
Jeff Brown928e0542011-01-10 11:17:36 -0800380 const sp<InputChannel>& inputChannel,
381 const sp<InputWindowHandle>& inputWindowHandle, bool monitor) {
382 return mInputManager->getDispatcher()->registerInputChannel(
383 inputChannel, inputWindowHandle, monitor);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700384}
385
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700386status_t NativeInputManager::unregisterInputChannel(JNIEnv* /* env */,
Jeff Brown7fbdc842010-06-17 20:52:56 -0700387 const sp<InputChannel>& inputChannel) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700388 return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700389}
390
Jeff Brown214eaf42011-05-26 19:17:02 -0700391void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700392 JNIEnv* env = jniEnv();
393
Jeff Brown4532e612012-04-05 14:27:12 -0700394 jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj,
395 gServiceClassInfo.getVirtualKeyQuietTimeMillis);
Jeff Brown214eaf42011-05-26 19:17:02 -0700396 if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) {
397 outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime);
398 }
399
400 outConfig->excludedDeviceNames.clear();
Jeff Brown4532e612012-04-05 14:27:12 -0700401 jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj,
402 gServiceClassInfo.getExcludedDeviceNames));
Jeff Brown214eaf42011-05-26 19:17:02 -0700403 if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) {
404 jsize length = env->GetArrayLength(excludedDeviceNames);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700405 for (jsize i = 0; i < length; i++) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700406 jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700407 const char* deviceNameChars = env->GetStringUTFChars(item, NULL);
Jeff Brown214eaf42011-05-26 19:17:02 -0700408 outConfig->excludedDeviceNames.add(String8(deviceNameChars));
Jeff Brown9c3cda02010-06-15 01:31:58 -0700409 env->ReleaseStringUTFChars(item, deviceNameChars);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700410 env->DeleteLocalRef(item);
411 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700412 env->DeleteLocalRef(excludedDeviceNames);
413 }
414
Jeff Brown4532e612012-04-05 14:27:12 -0700415 jint hoverTapTimeout = env->CallIntMethod(mServiceObj,
416 gServiceClassInfo.getHoverTapTimeout);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700417 if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700418 jint doubleTapTimeout = env->CallIntMethod(mServiceObj,
419 gServiceClassInfo.getDoubleTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700420 if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) {
Jeff Brown4532e612012-04-05 14:27:12 -0700421 jint longPressTimeout = env->CallIntMethod(mServiceObj,
422 gServiceClassInfo.getLongPressTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700423 if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700424 outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700425
426 // We must ensure that the tap-drag interval is significantly shorter than
427 // the long-press timeout because the tap is held down for the entire duration
428 // of the double-tap timeout.
429 jint tapDragInterval = max(min(longPressTimeout - 100,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700430 doubleTapTimeout), hoverTapTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700431 outConfig->pointerGestureTapDragInterval =
432 milliseconds_to_nanoseconds(tapDragInterval);
433 }
434 }
435 }
436
Jeff Brown4532e612012-04-05 14:27:12 -0700437 jint hoverTapSlop = env->CallIntMethod(mServiceObj,
438 gServiceClassInfo.getHoverTapSlop);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700439 if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) {
440 outConfig->pointerGestureTapSlop = hoverTapSlop;
Jeff Brown9c3cda02010-06-15 01:31:58 -0700441 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700442
443 { // acquire lock
444 AutoMutex _l(mLock);
445
446 outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed
447 * POINTER_SPEED_EXPONENT);
Jeff Brown474dcb52011-06-14 20:22:50 -0700448 outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled;
Jeff Brown65fd2512011-08-18 11:20:58 -0700449
Jeff Browndaf4a122011-08-26 17:14:14 -0700450 outConfig->showTouches = mLocked.showTouches;
451
Jeff Brownd728bf52012-09-08 18:05:28 -0700452 outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport);
453 outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700454 } // release lock
Jeff Brown9c3cda02010-06-15 01:31:58 -0700455}
456
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700457sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t /* deviceId */) {
Jeff Brown83c09682010-12-23 17:50:18 -0800458 AutoMutex _l(mLock);
459
460 sp<PointerController> controller = mLocked.pointerController.promote();
461 if (controller == NULL) {
Jeff Brown5541de92011-04-11 11:54:25 -0700462 ensureSpriteControllerLocked();
Jeff Brown83c09682010-12-23 17:50:18 -0800463
Jeff Brown2352b972011-04-12 22:39:53 -0700464 controller = new PointerController(this, mLooper, mLocked.spriteController);
Jeff Brown83c09682010-12-23 17:50:18 -0800465 mLocked.pointerController = controller;
466
Jeff Brownd728bf52012-09-08 18:05:28 -0700467 DisplayViewport& v = mLocked.internalViewport;
468 controller->setDisplayViewport(
469 v.logicalRight - v.logicalLeft,
470 v.logicalBottom - v.logicalTop,
471 v.orientation);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800472
Jeff Brown5541de92011-04-11 11:54:25 -0700473 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700474 jobject pointerIconObj = env->CallObjectMethod(mServiceObj,
475 gServiceClassInfo.getPointerIcon);
Jeff Brown2352b972011-04-12 22:39:53 -0700476 if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) {
477 PointerIcon pointerIcon;
478 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
479 mContextObj, &pointerIcon);
480 if (!status && !pointerIcon.isNullIcon()) {
481 controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap,
482 pointerIcon.hotSpotX, pointerIcon.hotSpotY));
483 } else {
484 controller->setPointerIcon(SpriteIcon());
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800485 }
Jeff Brown2352b972011-04-12 22:39:53 -0700486 env->DeleteLocalRef(pointerIconObj);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800487 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800488
Jeff Brown2352b972011-04-12 22:39:53 -0700489 updateInactivityTimeoutLocked(controller);
Jeff Brown83c09682010-12-23 17:50:18 -0800490 }
491 return controller;
492}
493
Jeff Brown5541de92011-04-11 11:54:25 -0700494void NativeInputManager::ensureSpriteControllerLocked() {
495 if (mLocked.spriteController == NULL) {
496 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700497 jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer);
Jeff Brown5541de92011-04-11 11:54:25 -0700498 if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) {
499 layer = -1;
500 }
501 mLocked.spriteController = new SpriteController(mLooper, layer);
502 }
503}
504
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700505void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) {
506 JNIEnv* env = jniEnv();
507
508 size_t count = inputDevices.size();
509 jobjectArray inputDevicesObjArray = env->NewObjectArray(
510 count, gInputDeviceClassInfo.clazz, NULL);
511 if (inputDevicesObjArray) {
512 bool error = false;
513 for (size_t i = 0; i < count; i++) {
514 jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i));
515 if (!inputDeviceObj) {
516 error = true;
517 break;
518 }
519
520 env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj);
521 env->DeleteLocalRef(inputDeviceObj);
522 }
523
524 if (!error) {
525 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged,
526 inputDevicesObjArray);
527 }
528
529 env->DeleteLocalRef(inputDevicesObjArray);
530 }
531
532 checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged");
533}
534
Jeff Brown6ec6f792012-04-17 16:52:41 -0700535sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay(
RoboErikfb290df2013-12-16 11:27:55 -0800536 const InputDeviceIdentifier& identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700537 JNIEnv* env = jniEnv();
538
539 sp<KeyCharacterMap> result;
RoboErikfb290df2013-12-16 11:27:55 -0800540 ScopedLocalRef<jstring> descriptor(env, env->NewStringUTF(identifier.descriptor.string()));
541 ScopedLocalRef<jobject> identifierObj(env, env->NewObject(gInputDeviceIdentifierInfo.clazz,
542 gInputDeviceIdentifierInfo.constructor, descriptor.get(),
543 identifier.vendor, identifier.product));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700544 ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj,
RoboErikfb290df2013-12-16 11:27:55 -0800545 gServiceClassInfo.getKeyboardLayoutOverlay, identifierObj.get())));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700546 if (arrayObj.get()) {
547 ScopedLocalRef<jstring> filenameObj(env,
548 jstring(env->GetObjectArrayElement(arrayObj.get(), 0)));
549 ScopedLocalRef<jstring> contentsObj(env,
550 jstring(env->GetObjectArrayElement(arrayObj.get(), 1)));
551 ScopedUtfChars filenameChars(env, filenameObj.get());
552 ScopedUtfChars contentsChars(env, contentsObj.get());
553
554 KeyCharacterMap::loadContents(String8(filenameChars.c_str()),
555 String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result);
556 }
557 checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay");
558 return result;
559}
560
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700561String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) {
562 JNIEnv* env = jniEnv();
563
564 ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string()));
565 ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj,
566 gServiceClassInfo.getDeviceAlias, uniqueIdObj.get())));
567 String8 result;
568 if (aliasObj.get()) {
569 ScopedUtfChars aliasChars(env, aliasObj.get());
570 result.setTo(aliasChars.c_str());
571 }
572 checkAndClearExceptionFromCallback(env, "getDeviceAlias");
573 return result;
574}
575
Jeff Brownbcc046a2012-09-27 20:46:43 -0700576void NativeInputManager::notifySwitch(nsecs_t when,
Andreas Gampe8dcf5932014-09-30 16:41:19 -0700577 uint32_t switchValues, uint32_t switchMask, uint32_t /* policyFlags */) {
Jeff Browne20c9e02010-10-11 14:20:19 -0700578#if DEBUG_INPUT_DISPATCHER_POLICY
Jeff Brownbcc046a2012-09-27 20:46:43 -0700579 ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x",
580 when, switchValues, switchMask, policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700581#endif
582
583 JNIEnv* env = jniEnv();
584
Jeff Brown53384282012-08-20 20:16:01 -0700585 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch,
Jeff Brownbcc046a2012-09-27 20:46:43 -0700586 when, switchValues, switchMask);
Jeff Brown53384282012-08-20 20:16:01 -0700587 checkAndClearExceptionFromCallback(env, "notifySwitch");
Jeff Browne20c9e02010-10-11 14:20:19 -0700588}
589
Jeff Brown9c3cda02010-06-15 01:31:58 -0700590void NativeInputManager::notifyConfigurationChanged(nsecs_t when) {
591#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000592 ALOGD("notifyConfigurationChanged - when=%lld", when);
Jeff Brown9c3cda02010-06-15 01:31:58 -0700593#endif
594
595 JNIEnv* env = jniEnv();
596
Jeff Brown4532e612012-04-05 14:27:12 -0700597 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700598 checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700599}
600
Jeff Brown519e0242010-09-15 15:18:56 -0700601nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700602 const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700603#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000604 ALOGD("notifyANR");
Jeff Brownb88102f2010-09-08 11:49:43 -0700605#endif
606
607 JNIEnv* env = jniEnv();
608
Jeff Brown928e0542011-01-10 11:17:36 -0800609 jobject inputApplicationHandleObj =
610 getInputApplicationHandleObjLocalRef(env, inputApplicationHandle);
611 jobject inputWindowHandleObj =
612 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brownbd181bb2013-09-10 16:44:24 -0700613 jstring reasonObj = env->NewStringUTF(reason.string());
Jeff Brownb88102f2010-09-08 11:49:43 -0700614
Jeff Brown4532e612012-04-05 14:27:12 -0700615 jlong newTimeout = env->CallLongMethod(mServiceObj,
Jeff Brownbd181bb2013-09-10 16:44:24 -0700616 gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj,
617 reasonObj);
Jeff Brown519e0242010-09-15 15:18:56 -0700618 if (checkAndClearExceptionFromCallback(env, "notifyANR")) {
619 newTimeout = 0; // abort dispatch
620 } else {
621 assert(newTimeout >= 0);
622 }
623
Jeff Brownbd181bb2013-09-10 16:44:24 -0700624 env->DeleteLocalRef(reasonObj);
Jeff Brown928e0542011-01-10 11:17:36 -0800625 env->DeleteLocalRef(inputWindowHandleObj);
626 env->DeleteLocalRef(inputApplicationHandleObj);
Jeff Brownb88102f2010-09-08 11:49:43 -0700627 return newTimeout;
628}
629
Jeff Brown928e0542011-01-10 11:17:36 -0800630void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700631#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000632 ALOGD("notifyInputChannelBroken");
Jeff Brown9c3cda02010-06-15 01:31:58 -0700633#endif
634
Jeff Brown7fbdc842010-06-17 20:52:56 -0700635 JNIEnv* env = jniEnv();
636
Jeff Brown928e0542011-01-10 11:17:36 -0800637 jobject inputWindowHandleObj =
638 getInputWindowHandleObjLocalRef(env, inputWindowHandle);
639 if (inputWindowHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700640 env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken,
Jeff Brown928e0542011-01-10 11:17:36 -0800641 inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700642 checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken");
643
Jeff Brown928e0542011-01-10 11:17:36 -0800644 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700645 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700646}
647
Jeff Brown214eaf42011-05-26 19:17:02 -0700648void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) {
649 JNIEnv* env = jniEnv();
Jeff Browna4547672011-03-02 21:38:11 -0800650
Jeff Brown4532e612012-04-05 14:27:12 -0700651 jint keyRepeatTimeout = env->CallIntMethod(mServiceObj,
652 gServiceClassInfo.getKeyRepeatTimeout);
Jeff Brown214eaf42011-05-26 19:17:02 -0700653 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) {
654 outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout);
655 }
Jeff Browna4547672011-03-02 21:38:11 -0800656
Jeff Brown4532e612012-04-05 14:27:12 -0700657 jint keyRepeatDelay = env->CallIntMethod(mServiceObj,
658 gServiceClassInfo.getKeyRepeatDelay);
Jeff Brown214eaf42011-05-26 19:17:02 -0700659 if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) {
660 outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay);
661 }
Jeff Brown9c3cda02010-06-15 01:31:58 -0700662}
663
Jeff Brown9302c872011-07-13 22:51:29 -0700664void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) {
665 Vector<sp<InputWindowHandle> > windowHandles;
Jeff Brown349703e2010-06-22 01:27:15 -0700666
Jeff Brown9302c872011-07-13 22:51:29 -0700667 if (windowHandleObjArray) {
668 jsize length = env->GetArrayLength(windowHandleObjArray);
669 for (jsize i = 0; i < length; i++) {
670 jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i);
671 if (! windowHandleObj) {
672 break; // found null element indicating end of used portion of the array
Jeff Brown474dcb52011-06-14 20:22:50 -0700673 }
Jeff Brown9302c872011-07-13 22:51:29 -0700674
675 sp<InputWindowHandle> windowHandle =
676 android_server_InputWindowHandle_getHandle(env, windowHandleObj);
677 if (windowHandle != NULL) {
678 windowHandles.push(windowHandle);
679 }
680 env->DeleteLocalRef(windowHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -0700681 }
Jeff Brownb88102f2010-09-08 11:49:43 -0700682 }
Jeff Brown349703e2010-06-22 01:27:15 -0700683
Jeff Brown9302c872011-07-13 22:51:29 -0700684 mInputManager->getDispatcher()->setInputWindows(windowHandles);
685
686 // Do this after the dispatcher has updated the window handle state.
687 bool newPointerGesturesEnabled = true;
688 size_t numWindows = windowHandles.size();
689 for (size_t i = 0; i < numWindows; i++) {
690 const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i);
Jeff Browncc4f7db2011-08-30 20:34:48 -0700691 const InputWindowInfo* windowInfo = windowHandle->getInfo();
692 if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures
693 & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) {
Jeff Brown9302c872011-07-13 22:51:29 -0700694 newPointerGesturesEnabled = false;
695 }
696 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700697
698 uint32_t changes = 0;
699 { // acquire lock
700 AutoMutex _l(mLock);
701
702 if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) {
703 mLocked.pointerGesturesEnabled = newPointerGesturesEnabled;
704 changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT;
705 }
706 } // release lock
707
708 if (changes) {
709 mInputManager->getReader()->requestRefreshConfiguration(changes);
710 }
Jeff Brown349703e2010-06-22 01:27:15 -0700711}
712
Jeff Brown9302c872011-07-13 22:51:29 -0700713void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) {
714 sp<InputApplicationHandle> applicationHandle =
715 android_server_InputApplicationHandle_getHandle(env, applicationHandleObj);
716 mInputManager->getDispatcher()->setFocusedApplication(applicationHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700717}
718
719void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) {
Jeff Brownb88102f2010-09-08 11:49:43 -0700720 mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -0700721}
722
Jeff Brown05dc66a2011-03-02 14:41:58 -0800723void NativeInputManager::setSystemUiVisibility(int32_t visibility) {
724 AutoMutex _l(mLock);
725
726 if (mLocked.systemUiVisibility != visibility) {
727 mLocked.systemUiVisibility = visibility;
728
729 sp<PointerController> controller = mLocked.pointerController.promote();
730 if (controller != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -0700731 updateInactivityTimeoutLocked(controller);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800732 }
733 }
734}
735
Jeff Brown2352b972011-04-12 22:39:53 -0700736void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) {
Jeff Brown05dc66a2011-03-02 14:41:58 -0800737 bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN;
Jeff Brown2352b972011-04-12 22:39:53 -0700738 controller->setInactivityTimeout(lightsOut
739 ? PointerController::INACTIVITY_TIMEOUT_SHORT
740 : PointerController::INACTIVITY_TIMEOUT_NORMAL);
Jeff Brown05dc66a2011-03-02 14:41:58 -0800741}
742
Jeff Brown1a84fd12011-06-02 01:26:32 -0700743void NativeInputManager::setPointerSpeed(int32_t speed) {
Jeff Brown474dcb52011-06-14 20:22:50 -0700744 { // acquire lock
745 AutoMutex _l(mLock);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700746
Jeff Brown474dcb52011-06-14 20:22:50 -0700747 if (mLocked.pointerSpeed == speed) {
748 return;
749 }
750
Steve Block6215d3f2012-01-04 20:05:49 +0000751 ALOGI("Setting pointer speed to %d.", speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700752 mLocked.pointerSpeed = speed;
Jeff Brown474dcb52011-06-14 20:22:50 -0700753 } // release lock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700754
Jeff Brown474dcb52011-06-14 20:22:50 -0700755 mInputManager->getReader()->requestRefreshConfiguration(
756 InputReaderConfiguration::CHANGE_POINTER_SPEED);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700757}
758
Jeff Browndaf4a122011-08-26 17:14:14 -0700759void NativeInputManager::setShowTouches(bool enabled) {
760 { // acquire lock
761 AutoMutex _l(mLock);
762
763 if (mLocked.showTouches == enabled) {
764 return;
765 }
766
Steve Block6215d3f2012-01-04 20:05:49 +0000767 ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled");
Jeff Browndaf4a122011-08-26 17:14:14 -0700768 mLocked.showTouches = enabled;
769 } // release lock
770
771 mInputManager->getReader()->requestRefreshConfiguration(
772 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
773}
774
Jeff Brown037c33e2014-04-09 00:31:55 -0700775void NativeInputManager::setInteractive(bool interactive) {
776 mInteractive = interactive;
Jeff Browne20c9e02010-10-11 14:20:19 -0700777}
778
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800779void NativeInputManager::reloadCalibration() {
780 mInputManager->getReader()->requestRefreshConfiguration(
Michael Wright357285c2015-04-17 00:50:31 +0100781 InputReaderConfiguration::CHANGE_TOUCH_AFFINE_TRANSFORMATION);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800782}
783
Jun Mukai1db53972015-09-11 18:08:31 -0700784void NativeInputManager::setPointerIconShape(int32_t iconId) {
785 AutoMutex _l(mLock);
786 sp<PointerController> controller = mLocked.pointerController.promote();
787 if (controller != NULL) {
788 // Use 0 (the default icon) for ARROW.
789 controller->updatePointerShape((iconId == POINTER_ICON_STYLE_ARROW) ? 0 : iconId);
790 }
791}
792
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800793TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
794 JNIEnv *env, jfloatArray matrixArr) {
795 ScopedFloatArrayRO matrix(env, matrixArr);
796 assert(matrix.size() == 6);
797
798 TouchAffineTransformation transform;
799 transform.x_scale = matrix[0];
800 transform.x_ymix = matrix[1];
801 transform.x_offset = matrix[2];
802 transform.y_xmix = matrix[3];
803 transform.y_scale = matrix[4];
804 transform.y_offset = matrix[5];
805
806 return transform;
807}
808
809TouchAffineTransformation NativeInputManager::getTouchAffineTransformation(
Jason Gerecked5220742014-03-10 09:47:59 -0700810 const String8& inputDeviceDescriptor, int32_t surfaceRotation) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800811 JNIEnv* env = jniEnv();
812
813 ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string()));
814
815 jobject cal = env->CallObjectMethod(mServiceObj,
Jason Gerecked5220742014-03-10 09:47:59 -0700816 gServiceClassInfo.getTouchCalibrationForInputDevice, descriptorObj.get(),
817 surfaceRotation);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800818
819 jfloatArray matrixArr = jfloatArray(env->CallObjectMethod(cal,
820 gTouchCalibrationClassInfo.getAffineTransform));
821
822 TouchAffineTransformation transform = getTouchAffineTransformation(env, matrixArr);
823
824 env->DeleteLocalRef(matrixArr);
825 env->DeleteLocalRef(cal);
826
827 return transform;
828}
829
Jeff Brown0029c662011-03-30 02:25:18 -0700830bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) {
831 jobject inputEventObj;
832
833 JNIEnv* env = jniEnv();
834 switch (inputEvent->getType()) {
835 case AINPUT_EVENT_TYPE_KEY:
836 inputEventObj = android_view_KeyEvent_fromNative(env,
837 static_cast<const KeyEvent*>(inputEvent));
838 break;
839 case AINPUT_EVENT_TYPE_MOTION:
840 inputEventObj = android_view_MotionEvent_obtainAsCopy(env,
841 static_cast<const MotionEvent*>(inputEvent));
842 break;
843 default:
844 return true; // dispatch the event normally
845 }
846
847 if (!inputEventObj) {
Steve Block3762c312012-01-06 19:20:56 +0000848 ALOGE("Failed to obtain input event object for filterInputEvent.");
Jeff Brown0029c662011-03-30 02:25:18 -0700849 return true; // dispatch the event normally
850 }
851
852 // The callee is responsible for recycling the event.
Jeff Brown4532e612012-04-05 14:27:12 -0700853 jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent,
Jeff Brown0029c662011-03-30 02:25:18 -0700854 inputEventObj, policyFlags);
855 if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) {
856 pass = true;
857 }
858 env->DeleteLocalRef(inputEventObj);
859 return pass;
860}
861
Jeff Brown1f245102010-11-18 20:53:46 -0800862void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent,
863 uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700864 // Policy:
865 // - Ignore untrusted events and pass them along.
866 // - Ask the window manager what to do with normal events and trusted injected events.
867 // - For normal events wake and brighten the screen if currently off or dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100868 bool interactive = mInteractive.load();
869 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700870 policyFlags |= POLICY_FLAG_INTERACTIVE;
871 }
Jeff Brown3122e442010-10-11 23:32:49 -0700872 if ((policyFlags & POLICY_FLAG_TRUSTED)) {
Jeff Brown1f245102010-11-18 20:53:46 -0800873 nsecs_t when = keyEvent->getEventTime();
Jeff Brown3122e442010-10-11 23:32:49 -0700874 JNIEnv* env = jniEnv();
Jeff Brown1f245102010-11-18 20:53:46 -0800875 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
876 jint wmActions;
877 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700878 wmActions = env->CallIntMethod(mServiceObj,
879 gServiceClassInfo.interceptKeyBeforeQueueing,
Jeff Brown037c33e2014-04-09 00:31:55 -0700880 keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800881 if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) {
882 wmActions = 0;
883 }
884 android_view_KeyEvent_recycle(env, keyEventObj);
885 env->DeleteLocalRef(keyEventObj);
886 } else {
Steve Block3762c312012-01-06 19:20:56 +0000887 ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing.");
Jeff Brown3122e442010-10-11 23:32:49 -0700888 wmActions = 0;
Jeff Browne20c9e02010-10-11 14:20:19 -0700889 }
890
Jeff Brown56194eb2011-03-02 19:23:13 -0800891 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Brown3122e442010-10-11 23:32:49 -0700892 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100893 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -0700894 policyFlags |= POLICY_FLAG_PASS_TO_USER;
895 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700896 }
897}
898
Jeff Brown56194eb2011-03-02 19:23:13 -0800899void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700900 // Policy:
901 // - Ignore untrusted events and pass them along.
902 // - No special filtering for injected events required at this time.
903 // - Filter normal events based on screen state.
904 // - For normal events brighten (but do not wake) the screen if currently dim.
Michael Wrighta4051212015-07-23 17:04:40 +0100905 bool interactive = mInteractive.load();
906 if (interactive) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700907 policyFlags |= POLICY_FLAG_INTERACTIVE;
908 }
Jeff Brown3122e442010-10-11 23:32:49 -0700909 if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) {
Jeff Brown037c33e2014-04-09 00:31:55 -0700910 if (policyFlags & POLICY_FLAG_INTERACTIVE) {
Jeff Brown3122e442010-10-11 23:32:49 -0700911 policyFlags |= POLICY_FLAG_PASS_TO_USER;
Michael Wright70af00a2014-09-03 19:30:20 -0700912 } else {
Jeff Brown56194eb2011-03-02 19:23:13 -0800913 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -0700914 jint wmActions = env->CallIntMethod(mServiceObj,
Michael Wright70af00a2014-09-03 19:30:20 -0700915 gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive,
Jeff Brown26875502014-01-30 21:47:47 -0800916 when, policyFlags);
Jeff Brown56194eb2011-03-02 19:23:13 -0800917 if (checkAndClearExceptionFromCallback(env,
Michael Wright70af00a2014-09-03 19:30:20 -0700918 "interceptMotionBeforeQueueingNonInteractive")) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800919 wmActions = 0;
920 }
921
Jeff Brown56194eb2011-03-02 19:23:13 -0800922 handleInterceptActions(wmActions, when, /*byref*/ policyFlags);
Jeff Browne20c9e02010-10-11 14:20:19 -0700923 }
Jeff Brown3122e442010-10-11 23:32:49 -0700924 } else {
Michael Wrighta4051212015-07-23 17:04:40 +0100925 if (interactive) {
Michael Wright70af00a2014-09-03 19:30:20 -0700926 policyFlags |= POLICY_FLAG_PASS_TO_USER;
927 }
Jeff Browne20c9e02010-10-11 14:20:19 -0700928 }
929}
930
Jeff Brown56194eb2011-03-02 19:23:13 -0800931void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when,
932 uint32_t& policyFlags) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800933 if (wmActions & WM_ACTION_PASS_TO_USER) {
934 policyFlags |= POLICY_FLAG_PASS_TO_USER;
935 } else {
Jeff Brown9267beb2011-03-07 20:11:22 -0800936#if DEBUG_INPUT_DISPATCHER_POLICY
Steve Block5baa3a62011-12-20 16:23:08 +0000937 ALOGD("handleInterceptActions: Not passing key to user.");
Jeff Brown56194eb2011-03-02 19:23:13 -0800938#endif
939 }
940}
941
Jeff Brown905805a2011-10-12 13:57:59 -0700942nsecs_t NativeInputManager::interceptKeyBeforeDispatching(
Jeff Brown928e0542011-01-10 11:17:36 -0800943 const sp<InputWindowHandle>& inputWindowHandle,
Jeff Browne20c9e02010-10-11 14:20:19 -0700944 const KeyEvent* keyEvent, uint32_t policyFlags) {
Jeff Brown3122e442010-10-11 23:32:49 -0700945 // Policy:
946 // - Ignore untrusted events and pass them along.
947 // - Filter normal events and trusted injected events through the window manager policy to
948 // handle the HOME key and the like.
Jeff Brown905805a2011-10-12 13:57:59 -0700949 nsecs_t result = 0;
Jeff Brown3122e442010-10-11 23:32:49 -0700950 if (policyFlags & POLICY_FLAG_TRUSTED) {
951 JNIEnv* env = jniEnv();
Jeff Brownd0097872010-06-30 14:41:59 -0700952
Jeff Brown928e0542011-01-10 11:17:36 -0800953 // Note: inputWindowHandle may be null.
954 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800955 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
956 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700957 jlong delayMillis = env->CallLongMethod(mServiceObj,
958 gServiceClassInfo.interceptKeyBeforeDispatching,
Jeff Brown928e0542011-01-10 11:17:36 -0800959 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brown1f245102010-11-18 20:53:46 -0800960 bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching");
961 android_view_KeyEvent_recycle(env, keyEventObj);
962 env->DeleteLocalRef(keyEventObj);
Jeff Brown905805a2011-10-12 13:57:59 -0700963 if (!error) {
964 if (delayMillis < 0) {
965 result = -1;
966 } else if (delayMillis > 0) {
967 result = milliseconds_to_nanoseconds(delayMillis);
968 }
969 }
Jeff Brown1f245102010-11-18 20:53:46 -0800970 } else {
Steve Block3762c312012-01-06 19:20:56 +0000971 ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching.");
Jeff Brown1f245102010-11-18 20:53:46 -0800972 }
Jeff Brown928e0542011-01-10 11:17:36 -0800973 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3122e442010-10-11 23:32:49 -0700974 }
Jeff Brown1f245102010-11-18 20:53:46 -0800975 return result;
Jeff Brownd0097872010-06-30 14:41:59 -0700976}
977
Jeff Brown928e0542011-01-10 11:17:36 -0800978bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle,
Jeff Brown49ed71d2010-12-06 17:13:33 -0800979 const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) {
Jeff Brown3915bb82010-11-05 15:02:16 -0700980 // Policy:
981 // - Ignore untrusted events and do not perform default handling.
Jeff Brown49ed71d2010-12-06 17:13:33 -0800982 bool result = false;
Jeff Brown3915bb82010-11-05 15:02:16 -0700983 if (policyFlags & POLICY_FLAG_TRUSTED) {
984 JNIEnv* env = jniEnv();
985
Jeff Brown928e0542011-01-10 11:17:36 -0800986 // Note: inputWindowHandle may be null.
987 jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle);
Jeff Brown1f245102010-11-18 20:53:46 -0800988 jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent);
989 if (keyEventObj) {
Jeff Brown4532e612012-04-05 14:27:12 -0700990 jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj,
991 gServiceClassInfo.dispatchUnhandledKey,
Jeff Brown928e0542011-01-10 11:17:36 -0800992 inputWindowHandleObj, keyEventObj, policyFlags);
Jeff Brownda3d5a92011-03-29 15:11:34 -0700993 if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) {
994 fallbackKeyEventObj = NULL;
995 }
Jeff Brown1f245102010-11-18 20:53:46 -0800996 android_view_KeyEvent_recycle(env, keyEventObj);
997 env->DeleteLocalRef(keyEventObj);
Jeff Brown49ed71d2010-12-06 17:13:33 -0800998
999 if (fallbackKeyEventObj) {
1000 // Note: outFallbackKeyEvent may be the same object as keyEvent.
1001 if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj,
1002 outFallbackKeyEvent)) {
1003 result = true;
1004 }
1005 android_view_KeyEvent_recycle(env, fallbackKeyEventObj);
1006 env->DeleteLocalRef(fallbackKeyEventObj);
1007 }
Jeff Brown1f245102010-11-18 20:53:46 -08001008 } else {
Steve Block3762c312012-01-06 19:20:56 +00001009 ALOGE("Failed to obtain key event object for dispatchUnhandledKey.");
Jeff Brown1f245102010-11-18 20:53:46 -08001010 }
Jeff Brown928e0542011-01-10 11:17:36 -08001011 env->DeleteLocalRef(inputWindowHandleObj);
Jeff Brown3915bb82010-11-05 15:02:16 -07001012 }
Jeff Brown1f245102010-11-18 20:53:46 -08001013 return result;
Jeff Brown3915bb82010-11-05 15:02:16 -07001014}
1015
Jeff Brown01ce2e92010-09-26 22:20:12 -07001016void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) {
1017 android_server_PowerManagerService_userActivity(eventTime, eventType);
Jeff Brown349703e2010-06-22 01:27:15 -07001018}
1019
Jeff Brown349703e2010-06-22 01:27:15 -07001020
Jeff Brownb88102f2010-09-08 11:49:43 -07001021bool NativeInputManager::checkInjectEventsPermissionNonReentrant(
1022 int32_t injectorPid, int32_t injectorUid) {
1023 JNIEnv* env = jniEnv();
Jeff Brown4532e612012-04-05 14:27:12 -07001024 jboolean result = env->CallBooleanMethod(mServiceObj,
1025 gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid);
Jeff Brownda3d5a92011-03-29 15:11:34 -07001026 if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) {
1027 result = false;
1028 }
Jeff Brown349703e2010-06-22 01:27:15 -07001029 return result;
1030}
1031
Jeff Brown2352b972011-04-12 22:39:53 -07001032void NativeInputManager::loadPointerResources(PointerResources* outResources) {
1033 JNIEnv* env = jniEnv();
1034
1035 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER,
1036 &outResources->spotHover);
1037 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH,
1038 &outResources->spotTouch);
1039 loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR,
1040 &outResources->spotAnchor);
1041}
1042
Jun Mukai1db53972015-09-11 18:08:31 -07001043void NativeInputManager::loadAdditionalMouseResources(std::map<int, SpriteIcon>* outResources) {
1044 JNIEnv* env = jniEnv();
1045
1046 for (int iconId = POINTER_ICON_STYLE_CONTEXT_MENU; iconId <= POINTER_ICON_STYLE_GRABBING;
1047 ++iconId) {
1048 loadSystemIconAsSprite(env, mContextObj, iconId, &((*outResources)[iconId]));
1049 }
1050}
1051
Jeff Brown83c09682010-12-23 17:50:18 -08001052
Jeff Brown9c3cda02010-06-15 01:31:58 -07001053// ----------------------------------------------------------------------------
1054
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001055static jlong nativeInit(JNIEnv* env, jclass /* clazz */,
Jeff Brown4532e612012-04-05 14:27:12 -07001056 jobject serviceObj, jobject contextObj, jobject messageQueueObj) {
Jeff Brown603b4452012-04-06 17:39:41 -07001057 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
Jeff Brown864693462013-01-28 14:25:53 -08001058 if (messageQueue == NULL) {
1059 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
1060 return 0;
1061 }
1062
Jeff Brown603b4452012-04-06 17:39:41 -07001063 NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,
1064 messageQueue->getLooper());
Mathias Agopianb1d90c82013-03-06 17:45:42 -08001065 im->incStrong(0);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001066 return reinterpret_cast<jlong>(im);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001067}
1068
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001069static void nativeStart(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001070 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001071
Jeff Brown4532e612012-04-05 14:27:12 -07001072 status_t result = im->getInputManager()->start();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001073 if (result) {
1074 jniThrowRuntimeException(env, "Input manager could not be started.");
1075 }
1076}
1077
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001078static void nativeSetDisplayViewport(JNIEnv* /* env */, jclass /* clazz */, jlong ptr,
1079 jboolean external, jint displayId, jint orientation,
Jeff Brownd728bf52012-09-08 18:05:28 -07001080 jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -07001081 jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom,
1082 jint deviceWidth, jint deviceHeight) {
Jeff Brown4532e612012-04-05 14:27:12 -07001083 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001084
Jeff Brownd728bf52012-09-08 18:05:28 -07001085 DisplayViewport v;
1086 v.displayId = displayId;
1087 v.orientation = orientation;
1088 v.logicalLeft = logicalLeft;
1089 v.logicalTop = logicalTop;
1090 v.logicalRight = logicalRight;
1091 v.logicalBottom = logicalBottom;
1092 v.physicalLeft = physicalLeft;
1093 v.physicalTop = physicalTop;
1094 v.physicalRight = physicalRight;
1095 v.physicalBottom = physicalBottom;
Jeff Brown83d616a2012-09-09 20:33:43 -07001096 v.deviceWidth = deviceWidth;
1097 v.deviceHeight = deviceHeight;
Jeff Brownd728bf52012-09-08 18:05:28 -07001098 im->setDisplayViewport(external, v);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001099}
1100
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001101static jint nativeGetScanCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001102 jlong ptr, jint deviceId, jint sourceMask, jint scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001103 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001104
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001105 return (jint) im->getInputManager()->getReader()->getScanCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001106 deviceId, uint32_t(sourceMask), scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001107}
1108
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001109static jint nativeGetKeyCodeState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001110 jlong ptr, jint deviceId, jint sourceMask, jint keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -07001111 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001112
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001113 return (jint) im->getInputManager()->getReader()->getKeyCodeState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001114 deviceId, uint32_t(sourceMask), keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001115}
1116
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001117static jint nativeGetSwitchState(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001118 jlong ptr, jint deviceId, jint sourceMask, jint sw) {
Jeff Brown4532e612012-04-05 14:27:12 -07001119 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001120
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001121 return (jint) im->getInputManager()->getReader()->getSwitchState(
Jeff Brown6d0fec22010-07-23 21:28:06 -07001122 deviceId, uint32_t(sourceMask), sw);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001123}
1124
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001125static jboolean nativeHasKeys(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001126 jlong ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001127 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001128
1129 int32_t* codes = env->GetIntArrayElements(keyCodes, NULL);
1130 uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL);
1131 jsize numCodes = env->GetArrayLength(keyCodes);
1132 jboolean result;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001133 if (numCodes == env->GetArrayLength(keyCodes)) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001134 if (im->getInputManager()->getReader()->hasKeys(
1135 deviceId, uint32_t(sourceMask), numCodes, codes, flags)) {
1136 result = JNI_TRUE;
1137 } else {
1138 result = JNI_FALSE;
1139 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001140 } else {
1141 result = JNI_FALSE;
1142 }
1143
1144 env->ReleaseBooleanArrayElements(outFlags, flags, 0);
1145 env->ReleaseIntArrayElements(keyCodes, codes, 0);
1146 return result;
1147}
1148
1149static void throwInputChannelNotInitialized(JNIEnv* env) {
1150 jniThrowException(env, "java/lang/IllegalStateException",
1151 "inputChannel is not initialized");
1152}
1153
Jeff Brown4532e612012-04-05 14:27:12 -07001154static void handleInputChannelDisposed(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001155 jobject /* inputChannelObj */, const sp<InputChannel>& inputChannel, void* data) {
Jeff Brown4532e612012-04-05 14:27:12 -07001156 NativeInputManager* im = static_cast<NativeInputManager*>(data);
1157
Steve Block8564c8d2012-01-05 23:22:43 +00001158 ALOGW("Input channel object '%s' was disposed without first being unregistered with "
Jeff Brown46b9ac02010-04-22 18:58:52 -07001159 "the input manager!", inputChannel->getName().string());
Jeff Brown4532e612012-04-05 14:27:12 -07001160 im->unregisterInputChannel(env, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001161}
1162
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001163static void nativeRegisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001164 jlong ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) {
Jeff Brown4532e612012-04-05 14:27:12 -07001165 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001166
1167 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1168 inputChannelObj);
1169 if (inputChannel == NULL) {
1170 throwInputChannelNotInitialized(env);
1171 return;
1172 }
1173
Jeff Brown928e0542011-01-10 11:17:36 -08001174 sp<InputWindowHandle> inputWindowHandle =
1175 android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001176
Jeff Brown4532e612012-04-05 14:27:12 -07001177 status_t status = im->registerInputChannel(
Jeff Brown928e0542011-01-10 11:17:36 -08001178 env, inputChannel, inputWindowHandle, monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001179 if (status) {
Jeff Browncc4f7db2011-08-30 20:34:48 -07001180 String8 message;
1181 message.appendFormat("Failed to register input channel. status=%d", status);
1182 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001183 return;
1184 }
1185
Jeff Browna41ca772010-08-11 14:46:32 -07001186 if (! monitor) {
1187 android_view_InputChannel_setDisposeCallback(env, inputChannelObj,
Jeff Brown4532e612012-04-05 14:27:12 -07001188 handleInputChannelDisposed, im);
Jeff Browna41ca772010-08-11 14:46:32 -07001189 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001190}
1191
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001192static void nativeUnregisterInputChannel(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001193 jlong ptr, jobject inputChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001194 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001195
1196 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
1197 inputChannelObj);
1198 if (inputChannel == NULL) {
1199 throwInputChannelNotInitialized(env);
1200 return;
1201 }
1202
1203 android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL);
1204
Jeff Brown4532e612012-04-05 14:27:12 -07001205 status_t status = im->unregisterInputChannel(env, inputChannel);
Jeff Browncc4f7db2011-08-30 20:34:48 -07001206 if (status && status != BAD_VALUE) { // ignore already unregistered channel
1207 String8 message;
1208 message.appendFormat("Failed to unregister input channel. status=%d", status);
1209 jniThrowRuntimeException(env, message.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -07001210 }
1211}
1212
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001213static void nativeSetInputFilterEnabled(JNIEnv* /* env */, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001214 jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001215 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown0029c662011-03-30 02:25:18 -07001216
Jeff Brown4532e612012-04-05 14:27:12 -07001217 im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled);
Jeff Brown0029c662011-03-30 02:25:18 -07001218}
1219
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001220static jint nativeInjectInputEvent(JNIEnv* env, jclass /* clazz */,
Jeff Brownca9bc702014-02-11 14:32:56 -08001221 jlong ptr, jobject inputEventObj, jint displayId, jint injectorPid, jint injectorUid,
Jeff Brown0029c662011-03-30 02:25:18 -07001222 jint syncMode, jint timeoutMillis, jint policyFlags) {
Jeff Brown4532e612012-04-05 14:27:12 -07001223 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown7fbdc842010-06-17 20:52:56 -07001224
Jeff Brown6ec402b2010-07-28 15:48:59 -07001225 if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) {
1226 KeyEvent keyEvent;
Jeff Brown1f245102010-11-18 20:53:46 -08001227 status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent);
1228 if (status) {
1229 jniThrowRuntimeException(env, "Could not read contents of KeyEvent object.");
1230 return INPUT_EVENT_INJECTION_FAILED;
1231 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001232
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001233 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001234 & keyEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001235 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001236 } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) {
Jeff Brown2ed24622011-03-14 19:39:54 -07001237 const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj);
1238 if (!motionEvent) {
Jeff Brown1f245102010-11-18 20:53:46 -08001239 jniThrowRuntimeException(env, "Could not read contents of MotionEvent object.");
1240 return INPUT_EVENT_INJECTION_FAILED;
1241 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001242
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001243 return (jint) im->getInputManager()->getDispatcher()->injectInputEvent(
Jeff Brownca9bc702014-02-11 14:32:56 -08001244 motionEvent, displayId, injectorPid, injectorUid, syncMode, timeoutMillis,
Jeff Brown0029c662011-03-30 02:25:18 -07001245 uint32_t(policyFlags));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001246 } else {
1247 jniThrowRuntimeException(env, "Invalid input event type.");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001248 return INPUT_EVENT_INJECTION_FAILED;
1249 }
Jeff Brown7fbdc842010-06-17 20:52:56 -07001250}
1251
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001252static void nativeSetInputWindows(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001253 jlong ptr, jobjectArray windowHandleObjArray) {
Jeff Brown4532e612012-04-05 14:27:12 -07001254 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001255
Jeff Brown4532e612012-04-05 14:27:12 -07001256 im->setInputWindows(env, windowHandleObjArray);
Jeff Brown349703e2010-06-22 01:27:15 -07001257}
1258
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001259static void nativeSetFocusedApplication(JNIEnv* env, jclass /* clazz */,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001260 jlong ptr, jobject applicationHandleObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001261 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001262
Jeff Brown4532e612012-04-05 14:27:12 -07001263 im->setFocusedApplication(env, applicationHandleObj);
Jeff Brown349703e2010-06-22 01:27:15 -07001264}
1265
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001266static void nativeSetInputDispatchMode(JNIEnv* /* env */,
1267 jclass /* clazz */, jlong ptr, jboolean enabled, jboolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001268 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown349703e2010-06-22 01:27:15 -07001269
Jeff Brown4532e612012-04-05 14:27:12 -07001270 im->setInputDispatchMode(enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001271}
1272
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001273static void nativeSetSystemUiVisibility(JNIEnv* /* env */,
1274 jclass /* clazz */, jlong ptr, jint visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001275 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001276
Jeff Brown4532e612012-04-05 14:27:12 -07001277 im->setSystemUiVisibility(visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001278}
1279
Jeff Brown4532e612012-04-05 14:27:12 -07001280static jboolean nativeTransferTouchFocus(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001281 jclass /* clazz */, jlong ptr, jobject fromChannelObj, jobject toChannelObj) {
Jeff Brown4532e612012-04-05 14:27:12 -07001282 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne6504122010-09-27 14:52:15 -07001283
1284 sp<InputChannel> fromChannel =
1285 android_view_InputChannel_getInputChannel(env, fromChannelObj);
1286 sp<InputChannel> toChannel =
1287 android_view_InputChannel_getInputChannel(env, toChannelObj);
1288
1289 if (fromChannel == NULL || toChannel == NULL) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001290 return JNI_FALSE;
Jeff Browne6504122010-09-27 14:52:15 -07001291 }
1292
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001293 if (im->getInputManager()->getDispatcher()->
1294 transferTouchFocus(fromChannel, toChannel)) {
1295 return JNI_TRUE;
1296 } else {
1297 return JNI_FALSE;
1298 }
Jeff Browne6504122010-09-27 14:52:15 -07001299}
1300
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001301static void nativeSetPointerSpeed(JNIEnv* /* env */,
1302 jclass /* clazz */, jlong ptr, jint speed) {
Jeff Brown4532e612012-04-05 14:27:12 -07001303 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001304
Jeff Brown4532e612012-04-05 14:27:12 -07001305 im->setPointerSpeed(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001306}
1307
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001308static void nativeSetShowTouches(JNIEnv* /* env */,
1309 jclass /* clazz */, jlong ptr, jboolean enabled) {
Jeff Brown4532e612012-04-05 14:27:12 -07001310 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browndaf4a122011-08-26 17:14:14 -07001311
Jeff Brown4532e612012-04-05 14:27:12 -07001312 im->setShowTouches(enabled);
Jeff Browndaf4a122011-08-26 17:14:14 -07001313}
1314
Jeff Brown037c33e2014-04-09 00:31:55 -07001315static void nativeSetInteractive(JNIEnv* env,
1316 jclass clazz, jlong ptr, jboolean interactive) {
1317 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1318
1319 im->setInteractive(interactive);
1320}
1321
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001322static void nativeReloadCalibration(JNIEnv* env, jclass clazz, jlong ptr) {
1323 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1324 im->reloadCalibration();
1325}
1326
Jeff Browna47425a2012-04-13 04:09:27 -07001327static void nativeVibrate(JNIEnv* env,
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001328 jclass /* clazz */, jlong ptr, jint deviceId, jlongArray patternObj,
Jeff Browna47425a2012-04-13 04:09:27 -07001329 jint repeat, jint token) {
1330 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1331
1332 size_t patternSize = env->GetArrayLength(patternObj);
1333 if (patternSize > MAX_VIBRATE_PATTERN_SIZE) {
Michael Wright9ecba522014-04-04 15:29:53 -07001334 ALOGI("Skipped requested vibration because the pattern size is %zu "
Jeff Browna47425a2012-04-13 04:09:27 -07001335 "which is more than the maximum supported size of %d.",
1336 patternSize, MAX_VIBRATE_PATTERN_SIZE);
1337 return; // limit to reasonable size
1338 }
1339
1340 jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical(
1341 patternObj, NULL));
1342 nsecs_t pattern[patternSize];
1343 for (size_t i = 0; i < patternSize; i++) {
1344 pattern[i] = max(jlong(0), min(patternMillis[i],
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001345 (jlong)(MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL))) * 1000000LL;
Jeff Browna47425a2012-04-13 04:09:27 -07001346 }
1347 env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT);
1348
1349 im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token);
1350}
1351
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001352static void nativeCancelVibrate(JNIEnv* /* env */,
1353 jclass /* clazz */, jlong ptr, jint deviceId, jint token) {
Jeff Browna47425a2012-04-13 04:09:27 -07001354 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1355
1356 im->getInputManager()->getReader()->cancelVibrate(deviceId, token);
1357}
1358
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001359static void nativeReloadKeyboardLayouts(JNIEnv* /* env */,
1360 jclass /* clazz */, jlong ptr) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001361 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1362
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001363 im->getInputManager()->getReader()->requestRefreshConfiguration(
1364 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS);
1365}
1366
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001367static void nativeReloadDeviceAliases(JNIEnv* /* env */,
1368 jclass /* clazz */, jlong ptr) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001369 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1370
1371 im->getInputManager()->getReader()->requestRefreshConfiguration(
1372 InputReaderConfiguration::CHANGE_DEVICE_ALIAS);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001373}
1374
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001375static jstring nativeDump(JNIEnv* env, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001376 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Browne33348b2010-07-15 23:54:05 -07001377
Jeff Brownb88102f2010-09-08 11:49:43 -07001378 String8 dump;
Jeff Brown4532e612012-04-05 14:27:12 -07001379 im->dump(dump);
Jeff Browne33348b2010-07-15 23:54:05 -07001380 return env->NewStringUTF(dump.string());
1381}
1382
Andreas Gampe8dcf5932014-09-30 16:41:19 -07001383static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
Jeff Brown4532e612012-04-05 14:27:12 -07001384 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001385
Jeff Brown4532e612012-04-05 14:27:12 -07001386 im->getInputManager()->getReader()->monitor();
1387 im->getInputManager()->getDispatcher()->monitor();
Jeff Brown89ef0722011-08-10 16:25:21 -07001388}
1389
Jun Mukai1db53972015-09-11 18:08:31 -07001390static void nativeSetPointerIconShape(JNIEnv* /* env */, jclass /* clazz */, jlong ptr, jint iconId) {
1391 NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
1392 im->setPointerIconShape(iconId);
1393}
1394
Jeff Brown9c3cda02010-06-15 01:31:58 -07001395// ----------------------------------------------------------------------------
1396
Daniel Micay76f6a862015-09-19 17:31:01 -04001397static const JNINativeMethod gInputManagerMethods[] = {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001398 /* name, signature, funcPtr */
Jeff Brown4532e612012-04-05 14:27:12 -07001399 { "nativeInit",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001400 "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)J",
Jeff Brown4532e612012-04-05 14:27:12 -07001401 (void*) nativeInit },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001402 { "nativeStart", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001403 (void*) nativeStart },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001404 { "nativeSetDisplayViewport", "(JZIIIIIIIIIIII)V",
Jeff Brownd728bf52012-09-08 18:05:28 -07001405 (void*) nativeSetDisplayViewport },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001406 { "nativeGetScanCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001407 (void*) nativeGetScanCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001408 { "nativeGetKeyCodeState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001409 (void*) nativeGetKeyCodeState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001410 { "nativeGetSwitchState", "(JIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001411 (void*) nativeGetSwitchState },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001412 { "nativeHasKeys", "(JII[I[Z)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001413 (void*) nativeHasKeys },
Jeff Brown928e0542011-01-10 11:17:36 -08001414 { "nativeRegisterInputChannel",
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001415 "(JLandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001416 (void*) nativeRegisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001417 { "nativeUnregisterInputChannel", "(JLandroid/view/InputChannel;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001418 (void*) nativeUnregisterInputChannel },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001419 { "nativeSetInputFilterEnabled", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001420 (void*) nativeSetInputFilterEnabled },
Jeff Brownca9bc702014-02-11 14:32:56 -08001421 { "nativeInjectInputEvent", "(JLandroid/view/InputEvent;IIIIII)I",
Jeff Brown4532e612012-04-05 14:27:12 -07001422 (void*) nativeInjectInputEvent },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001423 { "nativeSetInputWindows", "(J[Lcom/android/server/input/InputWindowHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001424 (void*) nativeSetInputWindows },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001425 { "nativeSetFocusedApplication", "(JLcom/android/server/input/InputApplicationHandle;)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001426 (void*) nativeSetFocusedApplication },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001427 { "nativeSetInputDispatchMode", "(JZZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001428 (void*) nativeSetInputDispatchMode },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001429 { "nativeSetSystemUiVisibility", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001430 (void*) nativeSetSystemUiVisibility },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001431 { "nativeTransferTouchFocus", "(JLandroid/view/InputChannel;Landroid/view/InputChannel;)Z",
Jeff Brown4532e612012-04-05 14:27:12 -07001432 (void*) nativeTransferTouchFocus },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001433 { "nativeSetPointerSpeed", "(JI)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001434 (void*) nativeSetPointerSpeed },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001435 { "nativeSetShowTouches", "(JZ)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001436 (void*) nativeSetShowTouches },
Jeff Brown037c33e2014-04-09 00:31:55 -07001437 { "nativeSetInteractive", "(JZ)V",
1438 (void*) nativeSetInteractive },
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001439 { "nativeReloadCalibration", "(J)V",
1440 (void*) nativeReloadCalibration },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001441 { "nativeVibrate", "(JI[JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001442 (void*) nativeVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001443 { "nativeCancelVibrate", "(JII)V",
Jeff Browna47425a2012-04-13 04:09:27 -07001444 (void*) nativeCancelVibrate },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001445 { "nativeReloadKeyboardLayouts", "(J)V",
Jeff Brown6ec6f792012-04-17 16:52:41 -07001446 (void*) nativeReloadKeyboardLayouts },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001447 { "nativeReloadDeviceAliases", "(J)V",
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001448 (void*) nativeReloadDeviceAliases },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001449 { "nativeDump", "(J)Ljava/lang/String;",
Jeff Brown4532e612012-04-05 14:27:12 -07001450 (void*) nativeDump },
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +00001451 { "nativeMonitor", "(J)V",
Jeff Brown4532e612012-04-05 14:27:12 -07001452 (void*) nativeMonitor },
Jun Mukai1db53972015-09-11 18:08:31 -07001453 { "nativeSetPointerIconShape", "(JI)V",
1454 (void*) nativeSetPointerIconShape },
Jeff Brown46b9ac02010-04-22 18:58:52 -07001455};
1456
1457#define FIND_CLASS(var, className) \
1458 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001459 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001460
1461#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \
1462 var = env->GetMethodID(clazz, methodName, methodDescriptor); \
1463 LOG_FATAL_IF(! var, "Unable to find method " methodName);
1464
1465#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
1466 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
1467 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
1468
1469int register_android_server_InputManager(JNIEnv* env) {
Jeff Brown4532e612012-04-05 14:27:12 -07001470 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService",
Jeff Brown46b9ac02010-04-22 18:58:52 -07001471 gInputManagerMethods, NELEM(gInputManagerMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +01001472 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown46b9ac02010-04-22 18:58:52 -07001473 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1474
Jeff Brown9c3cda02010-06-15 01:31:58 -07001475 // Callbacks
Jeff Brown46b9ac02010-04-22 18:58:52 -07001476
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001477 jclass clazz;
Jeff Brown4532e612012-04-05 14:27:12 -07001478 FIND_CLASS(clazz, "com/android/server/input/InputManagerService");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001479
Jeff Brown4532e612012-04-05 14:27:12 -07001480 GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz,
Jeff Brown57c59372010-09-21 18:22:55 -07001481 "notifyConfigurationChanged", "(J)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001482
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001483 GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz,
1484 "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V");
1485
Jeff Brown53384282012-08-20 20:16:01 -07001486 GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz,
1487 "notifySwitch", "(JII)V");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001488
Jeff Brown4532e612012-04-05 14:27:12 -07001489 GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz,
1490 "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V");
Jeff Brown7fbdc842010-06-17 20:52:56 -07001491
Jeff Brown4532e612012-04-05 14:27:12 -07001492 GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -08001493 "notifyANR",
Jeff Brownbd181bb2013-09-10 16:44:24 -07001494 "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001495
Jeff Brown4532e612012-04-05 14:27:12 -07001496 GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz,
Jeff Brown0029c662011-03-30 02:25:18 -07001497 "filterInputEvent", "(Landroid/view/InputEvent;I)Z");
1498
Jeff Brown4532e612012-04-05 14:27:12 -07001499 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz,
Jeff Brown037c33e2014-04-09 00:31:55 -07001500 "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;I)I");
Jeff Brown349703e2010-06-22 01:27:15 -07001501
Michael Wright70af00a2014-09-03 19:30:20 -07001502 GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingNonInteractive, clazz,
1503 "interceptMotionBeforeQueueingNonInteractive", "(JI)I");
Jeff Brown56194eb2011-03-02 19:23:13 -08001504
Jeff Brown4532e612012-04-05 14:27:12 -07001505 GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz,
Jeff Brown1f245102010-11-18 20:53:46 -08001506 "interceptKeyBeforeDispatching",
Jeff Brown4532e612012-04-05 14:27:12 -07001507 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J");
Jeff Brown349703e2010-06-22 01:27:15 -07001508
Jeff Brown4532e612012-04-05 14:27:12 -07001509 GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz,
Jeff Brown49ed71d2010-12-06 17:13:33 -08001510 "dispatchUnhandledKey",
Jeff Brown4532e612012-04-05 14:27:12 -07001511 "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;");
Jeff Brown3915bb82010-11-05 15:02:16 -07001512
Jeff Brown4532e612012-04-05 14:27:12 -07001513 GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz,
Jeff Brown349703e2010-06-22 01:27:15 -07001514 "checkInjectEventsPermission", "(II)Z");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001515
Jeff Brown4532e612012-04-05 14:27:12 -07001516 GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz,
Jeff Brownfe508922011-01-18 15:10:10 -08001517 "getVirtualKeyQuietTimeMillis", "()I");
1518
Jeff Brown4532e612012-04-05 14:27:12 -07001519 GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -07001520 "getExcludedDeviceNames", "()[Ljava/lang/String;");
1521
Jeff Brown4532e612012-04-05 14:27:12 -07001522 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001523 "getKeyRepeatTimeout", "()I");
1524
Jeff Brown4532e612012-04-05 14:27:12 -07001525 GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz,
Jeff Browna4547672011-03-02 21:38:11 -08001526 "getKeyRepeatDelay", "()I");
1527
Jeff Brown4532e612012-04-05 14:27:12 -07001528 GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001529 "getHoverTapTimeout", "()I");
1530
Jeff Brown4532e612012-04-05 14:27:12 -07001531 GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz,
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07001532 "getHoverTapSlop", "()I");
Jeff Brown214eaf42011-05-26 19:17:02 -07001533
Jeff Brown4532e612012-04-05 14:27:12 -07001534 GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001535 "getDoubleTapTimeout", "()I");
1536
Jeff Brown4532e612012-04-05 14:27:12 -07001537 GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz,
Jeff Brown214eaf42011-05-26 19:17:02 -07001538 "getLongPressTimeout", "()I");
1539
Jeff Brown4532e612012-04-05 14:27:12 -07001540 GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz,
Jeff Brown83c09682010-12-23 17:50:18 -08001541 "getPointerLayer", "()I");
1542
Jeff Brown4532e612012-04-05 14:27:12 -07001543 GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz,
Jeff Brown2352b972011-04-12 22:39:53 -07001544 "getPointerIcon", "()Landroid/view/PointerIcon;");
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001545
Jeff Brown6ec6f792012-04-17 16:52:41 -07001546 GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz,
RoboErikfb290df2013-12-16 11:27:55 -08001547 "getKeyboardLayoutOverlay",
1548 "(Landroid/hardware/input/InputDeviceIdentifier;)[Ljava/lang/String;");
Jeff Brown6ec6f792012-04-17 16:52:41 -07001549
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001550 GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz,
1551 "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;");
1552
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001553 GET_METHOD_ID(gServiceClassInfo.getTouchCalibrationForInputDevice, clazz,
1554 "getTouchCalibrationForInputDevice",
Jason Gerecked5220742014-03-10 09:47:59 -07001555 "(Ljava/lang/String;I)Landroid/hardware/input/TouchCalibration;");
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001556
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001557 // InputDevice
1558
1559 FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice");
1560 gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz));
1561
Jeff Brown6ec402b2010-07-28 15:48:59 -07001562 // KeyEvent
1563
1564 FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001565 gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz));
1566
Jeff Brown8d608662010-08-30 03:02:23 -07001567 // MotionEvent
Jeff Brown6ec402b2010-07-28 15:48:59 -07001568
1569 FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
Carl Shapiro17cc33a2011-03-05 20:53:16 -08001570 gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz));
Jeff Brown6ec402b2010-07-28 15:48:59 -07001571
RoboErikfb290df2013-12-16 11:27:55 -08001572 // InputDeviceIdentifier
1573
1574 FIND_CLASS(gInputDeviceIdentifierInfo.clazz, "android/hardware/input/InputDeviceIdentifier");
1575 gInputDeviceIdentifierInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceIdentifierInfo.clazz));
1576 GET_METHOD_ID(gInputDeviceIdentifierInfo.constructor, gInputDeviceIdentifierInfo.clazz,
1577 "<init>", "(Ljava/lang/String;II)V");
1578
Jason Gerecke857aa7b2014-01-27 18:34:20 -08001579 // TouchCalibration
1580
1581 FIND_CLASS(gTouchCalibrationClassInfo.clazz, "android/hardware/input/TouchCalibration");
1582 gTouchCalibrationClassInfo.clazz = jclass(env->NewGlobalRef(gTouchCalibrationClassInfo.clazz));
1583
1584 GET_METHOD_ID(gTouchCalibrationClassInfo.getAffineTransform, gTouchCalibrationClassInfo.clazz,
1585 "getAffineTransform", "()[F");
1586
Jeff Brown46b9ac02010-04-22 18:58:52 -07001587 return 0;
1588}
1589
Jeff Brown46b9ac02010-04-22 18:58:52 -07001590} /* namespace android */