Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // Log debug messages about InputReaderPolicy |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 22 | #define DEBUG_INPUT_READER_POLICY 0 |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 23 | |
| 24 | // Log debug messages about InputDispatcherPolicy |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 25 | #define DEBUG_INPUT_DISPATCHER_POLICY 0 |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 26 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 27 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 28 | #include "JNIHelp.h" |
| 29 | #include "jni.h" |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 30 | #include <limits.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 31 | #include <android_runtime/AndroidRuntime.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 32 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 33 | #include <utils/Log.h> |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 34 | #include <utils/Looper.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 35 | #include <utils/threads.h> |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 36 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 37 | #include <input/InputManager.h> |
| 38 | #include <input/PointerController.h> |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 39 | #include <input/SpriteController.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 40 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 41 | #include <android_os_MessageQueue.h> |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 42 | #include <android_view_InputDevice.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 43 | #include <android_view_KeyEvent.h> |
| 44 | #include <android_view_MotionEvent.h> |
| 45 | #include <android_view_InputChannel.h> |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 46 | #include <android_view_PointerIcon.h> |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 47 | #include <android/graphics/GraphicsJNI.h> |
| 48 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 49 | #include <ScopedLocalRef.h> |
| 50 | #include <ScopedUtfChars.h> |
| 51 | |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 52 | #include "com_android_server_power_PowerManagerService.h" |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 53 | #include "com_android_server_input_InputApplicationHandle.h" |
| 54 | #include "com_android_server_input_InputWindowHandle.h" |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 55 | |
| 56 | namespace android { |
| 57 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 58 | // The exponent used to calculate the pointer speed scaling factor. |
| 59 | // The scaling factor is calculated as 2 ^ (speed * exponent), |
| 60 | // where the speed ranges from -7 to + 7 and is supplied by the user. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 61 | static const float POINTER_SPEED_EXPONENT = 1.0f / 4; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 62 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 63 | static struct { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 64 | jmethodID notifyConfigurationChanged; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 65 | jmethodID notifyInputDevicesChanged; |
Jeff Brown | 5338428 | 2012-08-20 20:16:01 -0700 | [diff] [blame] | 66 | jmethodID notifySwitch; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 67 | jmethodID notifyInputChannelBroken; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 68 | jmethodID notifyANR; |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 69 | jmethodID filterInputEvent; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 70 | jmethodID interceptKeyBeforeQueueing; |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 71 | jmethodID interceptMotionBeforeQueueingWhenScreenOff; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 72 | jmethodID interceptKeyBeforeDispatching; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 73 | jmethodID dispatchUnhandledKey; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 74 | jmethodID checkInjectEventsPermission; |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 75 | jmethodID getVirtualKeyQuietTimeMillis; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 76 | jmethodID getExcludedDeviceNames; |
Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 77 | jmethodID getKeyRepeatTimeout; |
| 78 | jmethodID getKeyRepeatDelay; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 79 | jmethodID getHoverTapTimeout; |
| 80 | jmethodID getHoverTapSlop; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 81 | jmethodID getDoubleTapTimeout; |
| 82 | jmethodID getLongPressTimeout; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 83 | jmethodID getPointerLayer; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 84 | jmethodID getPointerIcon; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 85 | jmethodID getKeyboardLayoutOverlay; |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 86 | jmethodID getDeviceAlias; |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 87 | } gServiceClassInfo; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 88 | |
| 89 | static struct { |
| 90 | jclass clazz; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 91 | } gInputDeviceClassInfo; |
| 92 | |
| 93 | static struct { |
| 94 | jclass clazz; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 95 | } gKeyEventClassInfo; |
| 96 | |
| 97 | static struct { |
| 98 | jclass clazz; |
| 99 | } gMotionEventClassInfo; |
| 100 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 101 | |
| 102 | // --- Global functions --- |
| 103 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 104 | template<typename T> |
| 105 | inline static T min(const T& a, const T& b) { |
| 106 | return a < b ? a : b; |
| 107 | } |
| 108 | |
| 109 | template<typename T> |
| 110 | inline static T max(const T& a, const T& b) { |
| 111 | return a > b ? a : b; |
| 112 | } |
| 113 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 114 | static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env, |
| 115 | const sp<InputApplicationHandle>& inputApplicationHandle) { |
| 116 | if (inputApplicationHandle == NULL) { |
| 117 | return NULL; |
| 118 | } |
| 119 | return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())-> |
| 120 | getInputApplicationHandleObjLocalRef(env); |
| 121 | } |
| 122 | |
| 123 | static jobject getInputWindowHandleObjLocalRef(JNIEnv* env, |
| 124 | const sp<InputWindowHandle>& inputWindowHandle) { |
| 125 | if (inputWindowHandle == NULL) { |
| 126 | return NULL; |
| 127 | } |
| 128 | return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())-> |
| 129 | getInputWindowHandleObjLocalRef(env); |
| 130 | } |
| 131 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 132 | static void loadSystemIconAsSprite(JNIEnv* env, jobject contextObj, int32_t style, |
| 133 | SpriteIcon* outSpriteIcon) { |
| 134 | PointerIcon pointerIcon; |
| 135 | status_t status = android_view_PointerIcon_loadSystemIcon(env, |
| 136 | contextObj, style, &pointerIcon); |
| 137 | if (!status) { |
| 138 | pointerIcon.bitmap.copyTo(&outSpriteIcon->bitmap, SkBitmap::kARGB_8888_Config); |
| 139 | outSpriteIcon->hotSpotX = pointerIcon.hotSpotX; |
| 140 | outSpriteIcon->hotSpotY = pointerIcon.hotSpotY; |
| 141 | } |
| 142 | } |
| 143 | |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 144 | enum { |
| 145 | WM_ACTION_PASS_TO_USER = 1, |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 146 | WM_ACTION_WAKE_UP = 2, |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 147 | WM_ACTION_GO_TO_SLEEP = 4, |
| 148 | }; |
| 149 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 150 | |
| 151 | // --- NativeInputManager --- |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 152 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 153 | class NativeInputManager : public virtual RefBase, |
| 154 | public virtual InputReaderPolicyInterface, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 155 | public virtual InputDispatcherPolicyInterface, |
| 156 | public virtual PointerControllerPolicyInterface { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 157 | protected: |
| 158 | virtual ~NativeInputManager(); |
| 159 | |
| 160 | public: |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 161 | NativeInputManager(jobject contextObj, jobject serviceObj, const sp<Looper>& looper); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 162 | |
| 163 | inline sp<InputManager> getInputManager() const { return mInputManager; } |
| 164 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 165 | void dump(String8& dump); |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 166 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 167 | void setDisplayViewport(bool external, const DisplayViewport& viewport); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 168 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 169 | status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 170 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 171 | status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel); |
| 172 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 173 | void setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray); |
| 174 | void setFocusedApplication(JNIEnv* env, jobject applicationHandleObj); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 175 | void setInputDispatchMode(bool enabled, bool frozen); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 176 | void setSystemUiVisibility(int32_t visibility); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 177 | void setPointerSpeed(int32_t speed); |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 178 | void setShowTouches(bool enabled); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 179 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 180 | /* --- InputReaderPolicyInterface implementation --- */ |
| 181 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 182 | virtual void getReaderConfiguration(InputReaderConfiguration* outConfig); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 183 | virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 184 | virtual void notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 185 | virtual sp<KeyCharacterMap> getKeyboardLayoutOverlay(const String8& inputDeviceDescriptor); |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 186 | virtual String8 getDeviceAlias(const InputDeviceIdentifier& identifier); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 187 | |
| 188 | /* --- InputDispatcherPolicyInterface implementation --- */ |
| 189 | |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 190 | virtual void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask, |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 191 | uint32_t policyFlags); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 192 | virtual void notifyConfigurationChanged(nsecs_t when); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 193 | virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle, |
Jeff Brown | bd181bb | 2013-09-10 16:44:24 -0700 | [diff] [blame^] | 194 | const sp<InputWindowHandle>& inputWindowHandle, |
| 195 | const String8& reason); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 196 | virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 197 | virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 198 | virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig); |
| 199 | virtual bool isKeyRepeatEnabled(); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 200 | virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 201 | virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags); |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 202 | virtual nsecs_t interceptKeyBeforeDispatching( |
| 203 | const sp<InputWindowHandle>& inputWindowHandle, |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 204 | const KeyEvent* keyEvent, uint32_t policyFlags); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 205 | virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 206 | const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 207 | virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 208 | virtual bool checkInjectEventsPermissionNonReentrant( |
| 209 | int32_t injectorPid, int32_t injectorUid); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 210 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 211 | /* --- PointerControllerPolicyInterface implementation --- */ |
| 212 | |
| 213 | virtual void loadPointerResources(PointerResources* outResources); |
| 214 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 215 | private: |
| 216 | sp<InputManager> mInputManager; |
| 217 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 218 | jobject mContextObj; |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 219 | jobject mServiceObj; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 220 | sp<Looper> mLooper; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 221 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 222 | Mutex mLock; |
| 223 | struct Locked { |
| 224 | // Display size information. |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 225 | DisplayViewport internalViewport; |
| 226 | DisplayViewport externalViewport; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 227 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 228 | // System UI visibility. |
| 229 | int32_t systemUiVisibility; |
| 230 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 231 | // Pointer speed. |
| 232 | int32_t pointerSpeed; |
| 233 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 234 | // True if pointer gestures are enabled. |
| 235 | bool pointerGesturesEnabled; |
| 236 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 237 | // Show touches feature enable/disable. |
| 238 | bool showTouches; |
| 239 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 240 | // Sprite controller singleton, created on first use. |
| 241 | sp<SpriteController> spriteController; |
| 242 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 243 | // Pointer controller singleton, created and destroyed as needed. |
| 244 | wp<PointerController> pointerController; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 245 | } mLocked; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 246 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 247 | void updateInactivityTimeoutLocked(const sp<PointerController>& controller); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 248 | void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 249 | void ensureSpriteControllerLocked(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 250 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 251 | // Power manager interactions. |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 252 | bool isScreenOn(); |
| 253 | bool isScreenBright(); |
| 254 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 255 | static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName); |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 256 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 257 | static inline JNIEnv* jniEnv() { |
| 258 | return AndroidRuntime::getJNIEnv(); |
| 259 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 260 | }; |
| 261 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 262 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 263 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 264 | NativeInputManager::NativeInputManager(jobject contextObj, |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 265 | jobject serviceObj, const sp<Looper>& looper) : |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 266 | mLooper(looper) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 267 | JNIEnv* env = jniEnv(); |
| 268 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 269 | mContextObj = env->NewGlobalRef(contextObj); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 270 | mServiceObj = env->NewGlobalRef(serviceObj); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 271 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 272 | { |
| 273 | AutoMutex _l(mLock); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 274 | mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 275 | mLocked.pointerSpeed = 0; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 276 | mLocked.pointerGesturesEnabled = true; |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 277 | mLocked.showTouches = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 280 | sp<EventHub> eventHub = new EventHub(); |
| 281 | mInputManager = new InputManager(eventHub, this, this); |
| 282 | } |
| 283 | |
| 284 | NativeInputManager::~NativeInputManager() { |
| 285 | JNIEnv* env = jniEnv(); |
| 286 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 287 | env->DeleteGlobalRef(mContextObj); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 288 | env->DeleteGlobalRef(mServiceObj); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 291 | void NativeInputManager::dump(String8& dump) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 292 | mInputManager->getReader()->dump(dump); |
| 293 | dump.append("\n"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 294 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 295 | mInputManager->getDispatcher()->dump(dump); |
| 296 | dump.append("\n"); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 299 | bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 300 | if (env->ExceptionCheck()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 301 | ALOGE("An exception was thrown by callback '%s'.", methodName); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 302 | LOGE_EX(env); |
| 303 | env->ExceptionClear(); |
| 304 | return true; |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 309 | void NativeInputManager::setDisplayViewport(bool external, const DisplayViewport& viewport) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 310 | bool changed = false; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 311 | { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 312 | AutoMutex _l(mLock); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 313 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 314 | DisplayViewport& v = external ? mLocked.externalViewport : mLocked.internalViewport; |
| 315 | if (v != viewport) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 316 | changed = true; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 317 | v = viewport; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 318 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 319 | if (!external) { |
| 320 | sp<PointerController> controller = mLocked.pointerController.promote(); |
| 321 | if (controller != NULL) { |
| 322 | controller->setDisplayViewport( |
| 323 | viewport.logicalRight - viewport.logicalLeft, |
| 324 | viewport.logicalBottom - viewport.logicalTop, |
| 325 | viewport.orientation); |
| 326 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 327 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 328 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 329 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 330 | |
| 331 | if (changed) { |
| 332 | mInputManager->getReader()->requestRefreshConfiguration( |
| 333 | InputReaderConfiguration::CHANGE_DISPLAY_INFO); |
| 334 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 337 | status_t NativeInputManager::registerInputChannel(JNIEnv* env, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 338 | const sp<InputChannel>& inputChannel, |
| 339 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor) { |
| 340 | return mInputManager->getDispatcher()->registerInputChannel( |
| 341 | inputChannel, inputWindowHandle, monitor); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | status_t NativeInputManager::unregisterInputChannel(JNIEnv* env, |
| 345 | const sp<InputChannel>& inputChannel) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 346 | return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 349 | void NativeInputManager::getReaderConfiguration(InputReaderConfiguration* outConfig) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 350 | JNIEnv* env = jniEnv(); |
| 351 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 352 | jint virtualKeyQuietTime = env->CallIntMethod(mServiceObj, |
| 353 | gServiceClassInfo.getVirtualKeyQuietTimeMillis); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 354 | if (!checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) { |
| 355 | outConfig->virtualKeyQuietTime = milliseconds_to_nanoseconds(virtualKeyQuietTime); |
| 356 | } |
| 357 | |
| 358 | outConfig->excludedDeviceNames.clear(); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 359 | jobjectArray excludedDeviceNames = jobjectArray(env->CallObjectMethod(mServiceObj, |
| 360 | gServiceClassInfo.getExcludedDeviceNames)); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 361 | if (!checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && excludedDeviceNames) { |
| 362 | jsize length = env->GetArrayLength(excludedDeviceNames); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 363 | for (jsize i = 0; i < length; i++) { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 364 | jstring item = jstring(env->GetObjectArrayElement(excludedDeviceNames, i)); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 365 | const char* deviceNameChars = env->GetStringUTFChars(item, NULL); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 366 | outConfig->excludedDeviceNames.add(String8(deviceNameChars)); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 367 | env->ReleaseStringUTFChars(item, deviceNameChars); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 368 | env->DeleteLocalRef(item); |
| 369 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 370 | env->DeleteLocalRef(excludedDeviceNames); |
| 371 | } |
| 372 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 373 | jint hoverTapTimeout = env->CallIntMethod(mServiceObj, |
| 374 | gServiceClassInfo.getHoverTapTimeout); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 375 | if (!checkAndClearExceptionFromCallback(env, "getHoverTapTimeout")) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 376 | jint doubleTapTimeout = env->CallIntMethod(mServiceObj, |
| 377 | gServiceClassInfo.getDoubleTapTimeout); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 378 | if (!checkAndClearExceptionFromCallback(env, "getDoubleTapTimeout")) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 379 | jint longPressTimeout = env->CallIntMethod(mServiceObj, |
| 380 | gServiceClassInfo.getLongPressTimeout); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 381 | if (!checkAndClearExceptionFromCallback(env, "getLongPressTimeout")) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 382 | outConfig->pointerGestureTapInterval = milliseconds_to_nanoseconds(hoverTapTimeout); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 383 | |
| 384 | // We must ensure that the tap-drag interval is significantly shorter than |
| 385 | // the long-press timeout because the tap is held down for the entire duration |
| 386 | // of the double-tap timeout. |
| 387 | jint tapDragInterval = max(min(longPressTimeout - 100, |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 388 | doubleTapTimeout), hoverTapTimeout); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 389 | outConfig->pointerGestureTapDragInterval = |
| 390 | milliseconds_to_nanoseconds(tapDragInterval); |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 395 | jint hoverTapSlop = env->CallIntMethod(mServiceObj, |
| 396 | gServiceClassInfo.getHoverTapSlop); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 397 | if (!checkAndClearExceptionFromCallback(env, "getHoverTapSlop")) { |
| 398 | outConfig->pointerGestureTapSlop = hoverTapSlop; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 399 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 400 | |
| 401 | { // acquire lock |
| 402 | AutoMutex _l(mLock); |
| 403 | |
| 404 | outConfig->pointerVelocityControlParameters.scale = exp2f(mLocked.pointerSpeed |
| 405 | * POINTER_SPEED_EXPONENT); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 406 | outConfig->pointerGesturesEnabled = mLocked.pointerGesturesEnabled; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 407 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 408 | outConfig->showTouches = mLocked.showTouches; |
| 409 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 410 | outConfig->setDisplayInfo(false /*external*/, mLocked.internalViewport); |
| 411 | outConfig->setDisplayInfo(true /*external*/, mLocked.externalViewport); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 412 | } // release lock |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 415 | sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) { |
| 416 | AutoMutex _l(mLock); |
| 417 | |
| 418 | sp<PointerController> controller = mLocked.pointerController.promote(); |
| 419 | if (controller == NULL) { |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 420 | ensureSpriteControllerLocked(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 421 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 422 | controller = new PointerController(this, mLooper, mLocked.spriteController); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 423 | mLocked.pointerController = controller; |
| 424 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 425 | DisplayViewport& v = mLocked.internalViewport; |
| 426 | controller->setDisplayViewport( |
| 427 | v.logicalRight - v.logicalLeft, |
| 428 | v.logicalBottom - v.logicalTop, |
| 429 | v.orientation); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 430 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 431 | JNIEnv* env = jniEnv(); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 432 | jobject pointerIconObj = env->CallObjectMethod(mServiceObj, |
| 433 | gServiceClassInfo.getPointerIcon); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 434 | if (!checkAndClearExceptionFromCallback(env, "getPointerIcon")) { |
| 435 | PointerIcon pointerIcon; |
| 436 | status_t status = android_view_PointerIcon_load(env, pointerIconObj, |
| 437 | mContextObj, &pointerIcon); |
| 438 | if (!status && !pointerIcon.isNullIcon()) { |
| 439 | controller->setPointerIcon(SpriteIcon(pointerIcon.bitmap, |
| 440 | pointerIcon.hotSpotX, pointerIcon.hotSpotY)); |
| 441 | } else { |
| 442 | controller->setPointerIcon(SpriteIcon()); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 443 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 444 | env->DeleteLocalRef(pointerIconObj); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 445 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 446 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 447 | updateInactivityTimeoutLocked(controller); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 448 | } |
| 449 | return controller; |
| 450 | } |
| 451 | |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 452 | void NativeInputManager::ensureSpriteControllerLocked() { |
| 453 | if (mLocked.spriteController == NULL) { |
| 454 | JNIEnv* env = jniEnv(); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 455 | jint layer = env->CallIntMethod(mServiceObj, gServiceClassInfo.getPointerLayer); |
Jeff Brown | 5541de9 | 2011-04-11 11:54:25 -0700 | [diff] [blame] | 456 | if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) { |
| 457 | layer = -1; |
| 458 | } |
| 459 | mLocked.spriteController = new SpriteController(mLooper, layer); |
| 460 | } |
| 461 | } |
| 462 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 463 | void NativeInputManager::notifyInputDevicesChanged(const Vector<InputDeviceInfo>& inputDevices) { |
| 464 | JNIEnv* env = jniEnv(); |
| 465 | |
| 466 | size_t count = inputDevices.size(); |
| 467 | jobjectArray inputDevicesObjArray = env->NewObjectArray( |
| 468 | count, gInputDeviceClassInfo.clazz, NULL); |
| 469 | if (inputDevicesObjArray) { |
| 470 | bool error = false; |
| 471 | for (size_t i = 0; i < count; i++) { |
| 472 | jobject inputDeviceObj = android_view_InputDevice_create(env, inputDevices.itemAt(i)); |
| 473 | if (!inputDeviceObj) { |
| 474 | error = true; |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | env->SetObjectArrayElement(inputDevicesObjArray, i, inputDeviceObj); |
| 479 | env->DeleteLocalRef(inputDeviceObj); |
| 480 | } |
| 481 | |
| 482 | if (!error) { |
| 483 | env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputDevicesChanged, |
| 484 | inputDevicesObjArray); |
| 485 | } |
| 486 | |
| 487 | env->DeleteLocalRef(inputDevicesObjArray); |
| 488 | } |
| 489 | |
| 490 | checkAndClearExceptionFromCallback(env, "notifyInputDevicesChanged"); |
| 491 | } |
| 492 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 493 | sp<KeyCharacterMap> NativeInputManager::getKeyboardLayoutOverlay( |
| 494 | const String8& inputDeviceDescriptor) { |
| 495 | JNIEnv* env = jniEnv(); |
| 496 | |
| 497 | sp<KeyCharacterMap> result; |
| 498 | ScopedLocalRef<jstring> descriptorObj(env, env->NewStringUTF(inputDeviceDescriptor.string())); |
| 499 | ScopedLocalRef<jobjectArray> arrayObj(env, jobjectArray(env->CallObjectMethod(mServiceObj, |
| 500 | gServiceClassInfo.getKeyboardLayoutOverlay, descriptorObj.get()))); |
| 501 | if (arrayObj.get()) { |
| 502 | ScopedLocalRef<jstring> filenameObj(env, |
| 503 | jstring(env->GetObjectArrayElement(arrayObj.get(), 0))); |
| 504 | ScopedLocalRef<jstring> contentsObj(env, |
| 505 | jstring(env->GetObjectArrayElement(arrayObj.get(), 1))); |
| 506 | ScopedUtfChars filenameChars(env, filenameObj.get()); |
| 507 | ScopedUtfChars contentsChars(env, contentsObj.get()); |
| 508 | |
| 509 | KeyCharacterMap::loadContents(String8(filenameChars.c_str()), |
| 510 | String8(contentsChars.c_str()), KeyCharacterMap::FORMAT_OVERLAY, &result); |
| 511 | } |
| 512 | checkAndClearExceptionFromCallback(env, "getKeyboardLayoutOverlay"); |
| 513 | return result; |
| 514 | } |
| 515 | |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 516 | String8 NativeInputManager::getDeviceAlias(const InputDeviceIdentifier& identifier) { |
| 517 | JNIEnv* env = jniEnv(); |
| 518 | |
| 519 | ScopedLocalRef<jstring> uniqueIdObj(env, env->NewStringUTF(identifier.uniqueId.string())); |
| 520 | ScopedLocalRef<jstring> aliasObj(env, jstring(env->CallObjectMethod(mServiceObj, |
| 521 | gServiceClassInfo.getDeviceAlias, uniqueIdObj.get()))); |
| 522 | String8 result; |
| 523 | if (aliasObj.get()) { |
| 524 | ScopedUtfChars aliasChars(env, aliasObj.get()); |
| 525 | result.setTo(aliasChars.c_str()); |
| 526 | } |
| 527 | checkAndClearExceptionFromCallback(env, "getDeviceAlias"); |
| 528 | return result; |
| 529 | } |
| 530 | |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 531 | void NativeInputManager::notifySwitch(nsecs_t when, |
| 532 | uint32_t switchValues, uint32_t switchMask, uint32_t policyFlags) { |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 533 | #if DEBUG_INPUT_DISPATCHER_POLICY |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 534 | ALOGD("notifySwitch - when=%lld, switchValues=0x%08x, switchMask=0x%08x, policyFlags=0x%x", |
| 535 | when, switchValues, switchMask, policyFlags); |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 536 | #endif |
| 537 | |
| 538 | JNIEnv* env = jniEnv(); |
| 539 | |
Jeff Brown | 5338428 | 2012-08-20 20:16:01 -0700 | [diff] [blame] | 540 | env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifySwitch, |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 541 | when, switchValues, switchMask); |
Jeff Brown | 5338428 | 2012-08-20 20:16:01 -0700 | [diff] [blame] | 542 | checkAndClearExceptionFromCallback(env, "notifySwitch"); |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 545 | void NativeInputManager::notifyConfigurationChanged(nsecs_t when) { |
| 546 | #if DEBUG_INPUT_DISPATCHER_POLICY |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 547 | ALOGD("notifyConfigurationChanged - when=%lld", when); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 548 | #endif |
| 549 | |
| 550 | JNIEnv* env = jniEnv(); |
| 551 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 552 | env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyConfigurationChanged, when); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 553 | checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged"); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 556 | nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle, |
Jeff Brown | bd181bb | 2013-09-10 16:44:24 -0700 | [diff] [blame^] | 557 | const sp<InputWindowHandle>& inputWindowHandle, const String8& reason) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 558 | #if DEBUG_INPUT_DISPATCHER_POLICY |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 559 | ALOGD("notifyANR"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 560 | #endif |
| 561 | |
| 562 | JNIEnv* env = jniEnv(); |
| 563 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 564 | jobject inputApplicationHandleObj = |
| 565 | getInputApplicationHandleObjLocalRef(env, inputApplicationHandle); |
| 566 | jobject inputWindowHandleObj = |
| 567 | getInputWindowHandleObjLocalRef(env, inputWindowHandle); |
Jeff Brown | bd181bb | 2013-09-10 16:44:24 -0700 | [diff] [blame^] | 568 | jstring reasonObj = env->NewStringUTF(reason.string()); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 569 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 570 | jlong newTimeout = env->CallLongMethod(mServiceObj, |
Jeff Brown | bd181bb | 2013-09-10 16:44:24 -0700 | [diff] [blame^] | 571 | gServiceClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj, |
| 572 | reasonObj); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 573 | if (checkAndClearExceptionFromCallback(env, "notifyANR")) { |
| 574 | newTimeout = 0; // abort dispatch |
| 575 | } else { |
| 576 | assert(newTimeout >= 0); |
| 577 | } |
| 578 | |
Jeff Brown | bd181bb | 2013-09-10 16:44:24 -0700 | [diff] [blame^] | 579 | env->DeleteLocalRef(reasonObj); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 580 | env->DeleteLocalRef(inputWindowHandleObj); |
| 581 | env->DeleteLocalRef(inputApplicationHandleObj); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 582 | return newTimeout; |
| 583 | } |
| 584 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 585 | void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 586 | #if DEBUG_INPUT_DISPATCHER_POLICY |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 587 | ALOGD("notifyInputChannelBroken"); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 588 | #endif |
| 589 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 590 | JNIEnv* env = jniEnv(); |
| 591 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 592 | jobject inputWindowHandleObj = |
| 593 | getInputWindowHandleObjLocalRef(env, inputWindowHandle); |
| 594 | if (inputWindowHandleObj) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 595 | env->CallVoidMethod(mServiceObj, gServiceClassInfo.notifyInputChannelBroken, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 596 | inputWindowHandleObj); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 597 | checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken"); |
| 598 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 599 | env->DeleteLocalRef(inputWindowHandleObj); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 600 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 603 | void NativeInputManager::getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) { |
| 604 | JNIEnv* env = jniEnv(); |
Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 605 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 606 | jint keyRepeatTimeout = env->CallIntMethod(mServiceObj, |
| 607 | gServiceClassInfo.getKeyRepeatTimeout); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 608 | if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) { |
| 609 | outConfig->keyRepeatTimeout = milliseconds_to_nanoseconds(keyRepeatTimeout); |
| 610 | } |
Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 611 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 612 | jint keyRepeatDelay = env->CallIntMethod(mServiceObj, |
| 613 | gServiceClassInfo.getKeyRepeatDelay); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 614 | if (!checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) { |
| 615 | outConfig->keyRepeatDelay = milliseconds_to_nanoseconds(keyRepeatDelay); |
| 616 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 619 | bool NativeInputManager::isKeyRepeatEnabled() { |
| 620 | // Only enable automatic key repeating when the screen is on. |
| 621 | return isScreenOn(); |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 624 | void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowHandleObjArray) { |
| 625 | Vector<sp<InputWindowHandle> > windowHandles; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 626 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 627 | if (windowHandleObjArray) { |
| 628 | jsize length = env->GetArrayLength(windowHandleObjArray); |
| 629 | for (jsize i = 0; i < length; i++) { |
| 630 | jobject windowHandleObj = env->GetObjectArrayElement(windowHandleObjArray, i); |
| 631 | if (! windowHandleObj) { |
| 632 | break; // found null element indicating end of used portion of the array |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 633 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 634 | |
| 635 | sp<InputWindowHandle> windowHandle = |
| 636 | android_server_InputWindowHandle_getHandle(env, windowHandleObj); |
| 637 | if (windowHandle != NULL) { |
| 638 | windowHandles.push(windowHandle); |
| 639 | } |
| 640 | env->DeleteLocalRef(windowHandleObj); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 641 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 642 | } |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 643 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 644 | mInputManager->getDispatcher()->setInputWindows(windowHandles); |
| 645 | |
| 646 | // Do this after the dispatcher has updated the window handle state. |
| 647 | bool newPointerGesturesEnabled = true; |
| 648 | size_t numWindows = windowHandles.size(); |
| 649 | for (size_t i = 0; i < numWindows; i++) { |
| 650 | const sp<InputWindowHandle>& windowHandle = windowHandles.itemAt(i); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 651 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 652 | if (windowInfo && windowInfo->hasFocus && (windowInfo->inputFeatures |
| 653 | & InputWindowInfo::INPUT_FEATURE_DISABLE_TOUCH_PAD_GESTURES)) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 654 | newPointerGesturesEnabled = false; |
| 655 | } |
| 656 | } |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 657 | |
| 658 | uint32_t changes = 0; |
| 659 | { // acquire lock |
| 660 | AutoMutex _l(mLock); |
| 661 | |
| 662 | if (mLocked.pointerGesturesEnabled != newPointerGesturesEnabled) { |
| 663 | mLocked.pointerGesturesEnabled = newPointerGesturesEnabled; |
| 664 | changes |= InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT; |
| 665 | } |
| 666 | } // release lock |
| 667 | |
| 668 | if (changes) { |
| 669 | mInputManager->getReader()->requestRefreshConfiguration(changes); |
| 670 | } |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 673 | void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationHandleObj) { |
| 674 | sp<InputApplicationHandle> applicationHandle = |
| 675 | android_server_InputApplicationHandle_getHandle(env, applicationHandleObj); |
| 676 | mInputManager->getDispatcher()->setFocusedApplication(applicationHandle); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 680 | mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 683 | void NativeInputManager::setSystemUiVisibility(int32_t visibility) { |
| 684 | AutoMutex _l(mLock); |
| 685 | |
| 686 | if (mLocked.systemUiVisibility != visibility) { |
| 687 | mLocked.systemUiVisibility = visibility; |
| 688 | |
| 689 | sp<PointerController> controller = mLocked.pointerController.promote(); |
| 690 | if (controller != NULL) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 691 | updateInactivityTimeoutLocked(controller); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 696 | void NativeInputManager::updateInactivityTimeoutLocked(const sp<PointerController>& controller) { |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 697 | bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 698 | controller->setInactivityTimeout(lightsOut |
| 699 | ? PointerController::INACTIVITY_TIMEOUT_SHORT |
| 700 | : PointerController::INACTIVITY_TIMEOUT_NORMAL); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 701 | } |
| 702 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 703 | void NativeInputManager::setPointerSpeed(int32_t speed) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 704 | { // acquire lock |
| 705 | AutoMutex _l(mLock); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 706 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 707 | if (mLocked.pointerSpeed == speed) { |
| 708 | return; |
| 709 | } |
| 710 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 711 | ALOGI("Setting pointer speed to %d.", speed); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 712 | mLocked.pointerSpeed = speed; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 713 | } // release lock |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 714 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 715 | mInputManager->getReader()->requestRefreshConfiguration( |
| 716 | InputReaderConfiguration::CHANGE_POINTER_SPEED); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 719 | void NativeInputManager::setShowTouches(bool enabled) { |
| 720 | { // acquire lock |
| 721 | AutoMutex _l(mLock); |
| 722 | |
| 723 | if (mLocked.showTouches == enabled) { |
| 724 | return; |
| 725 | } |
| 726 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 727 | ALOGI("Setting show touches feature to %s.", enabled ? "enabled" : "disabled"); |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 728 | mLocked.showTouches = enabled; |
| 729 | } // release lock |
| 730 | |
| 731 | mInputManager->getReader()->requestRefreshConfiguration( |
| 732 | InputReaderConfiguration::CHANGE_SHOW_TOUCHES); |
| 733 | } |
| 734 | |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 735 | bool NativeInputManager::isScreenOn() { |
| 736 | return android_server_PowerManagerService_isScreenOn(); |
| 737 | } |
| 738 | |
| 739 | bool NativeInputManager::isScreenBright() { |
| 740 | return android_server_PowerManagerService_isScreenBright(); |
| 741 | } |
| 742 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 743 | bool NativeInputManager::filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) { |
| 744 | jobject inputEventObj; |
| 745 | |
| 746 | JNIEnv* env = jniEnv(); |
| 747 | switch (inputEvent->getType()) { |
| 748 | case AINPUT_EVENT_TYPE_KEY: |
| 749 | inputEventObj = android_view_KeyEvent_fromNative(env, |
| 750 | static_cast<const KeyEvent*>(inputEvent)); |
| 751 | break; |
| 752 | case AINPUT_EVENT_TYPE_MOTION: |
| 753 | inputEventObj = android_view_MotionEvent_obtainAsCopy(env, |
| 754 | static_cast<const MotionEvent*>(inputEvent)); |
| 755 | break; |
| 756 | default: |
| 757 | return true; // dispatch the event normally |
| 758 | } |
| 759 | |
| 760 | if (!inputEventObj) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 761 | ALOGE("Failed to obtain input event object for filterInputEvent."); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 762 | return true; // dispatch the event normally |
| 763 | } |
| 764 | |
| 765 | // The callee is responsible for recycling the event. |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 766 | jboolean pass = env->CallBooleanMethod(mServiceObj, gServiceClassInfo.filterInputEvent, |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 767 | inputEventObj, policyFlags); |
| 768 | if (checkAndClearExceptionFromCallback(env, "filterInputEvent")) { |
| 769 | pass = true; |
| 770 | } |
| 771 | env->DeleteLocalRef(inputEventObj); |
| 772 | return pass; |
| 773 | } |
| 774 | |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 775 | void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent, |
| 776 | uint32_t& policyFlags) { |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 777 | // Policy: |
| 778 | // - Ignore untrusted events and pass them along. |
| 779 | // - Ask the window manager what to do with normal events and trusted injected events. |
| 780 | // - For normal events wake and brighten the screen if currently off or dim. |
| 781 | if ((policyFlags & POLICY_FLAG_TRUSTED)) { |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 782 | nsecs_t when = keyEvent->getEventTime(); |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 783 | bool isScreenOn = this->isScreenOn(); |
| 784 | bool isScreenBright = this->isScreenBright(); |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 785 | |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 786 | JNIEnv* env = jniEnv(); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 787 | jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent); |
| 788 | jint wmActions; |
| 789 | if (keyEventObj) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 790 | wmActions = env->CallIntMethod(mServiceObj, |
| 791 | gServiceClassInfo.interceptKeyBeforeQueueing, |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 792 | keyEventObj, policyFlags, isScreenOn); |
| 793 | if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) { |
| 794 | wmActions = 0; |
| 795 | } |
| 796 | android_view_KeyEvent_recycle(env, keyEventObj); |
| 797 | env->DeleteLocalRef(keyEventObj); |
| 798 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 799 | ALOGE("Failed to obtain key event object for interceptKeyBeforeQueueing."); |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 800 | wmActions = 0; |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 803 | if (!(policyFlags & POLICY_FLAG_INJECTED)) { |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 804 | if (!isScreenOn) { |
| 805 | policyFlags |= POLICY_FLAG_WOKE_HERE; |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | if (!isScreenBright) { |
| 809 | policyFlags |= POLICY_FLAG_BRIGHT_HERE; |
| 810 | } |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 813 | handleInterceptActions(wmActions, when, /*byref*/ policyFlags); |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 814 | } else { |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 815 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 816 | } |
| 817 | } |
| 818 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 819 | void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) { |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 820 | // Policy: |
| 821 | // - Ignore untrusted events and pass them along. |
| 822 | // - No special filtering for injected events required at this time. |
| 823 | // - Filter normal events based on screen state. |
| 824 | // - For normal events brighten (but do not wake) the screen if currently dim. |
| 825 | if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) { |
| 826 | if (isScreenOn()) { |
| 827 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 828 | |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 829 | if (!isScreenBright()) { |
| 830 | policyFlags |= POLICY_FLAG_BRIGHT_HERE; |
| 831 | } |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 832 | } else { |
| 833 | JNIEnv* env = jniEnv(); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 834 | jint wmActions = env->CallIntMethod(mServiceObj, |
| 835 | gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff, |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 836 | policyFlags); |
| 837 | if (checkAndClearExceptionFromCallback(env, |
| 838 | "interceptMotionBeforeQueueingWhenScreenOff")) { |
| 839 | wmActions = 0; |
| 840 | } |
| 841 | |
| 842 | policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE; |
| 843 | handleInterceptActions(wmActions, when, /*byref*/ policyFlags); |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 844 | } |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 845 | } else { |
| 846 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 850 | void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when, |
| 851 | uint32_t& policyFlags) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 852 | if (wmActions & WM_ACTION_GO_TO_SLEEP) { |
Jeff Brown | 9267beb | 2011-03-07 20:11:22 -0800 | [diff] [blame] | 853 | #if DEBUG_INPUT_DISPATCHER_POLICY |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 854 | ALOGD("handleInterceptActions: Going to sleep."); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 855 | #endif |
| 856 | android_server_PowerManagerService_goToSleep(when); |
| 857 | } |
| 858 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 859 | if (wmActions & WM_ACTION_WAKE_UP) { |
Jeff Brown | 9267beb | 2011-03-07 20:11:22 -0800 | [diff] [blame] | 860 | #if DEBUG_INPUT_DISPATCHER_POLICY |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 861 | ALOGD("handleInterceptActions: Waking up."); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 862 | #endif |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 863 | android_server_PowerManagerService_wakeUp(when); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | if (wmActions & WM_ACTION_PASS_TO_USER) { |
| 867 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 868 | } else { |
Jeff Brown | 9267beb | 2011-03-07 20:11:22 -0800 | [diff] [blame] | 869 | #if DEBUG_INPUT_DISPATCHER_POLICY |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 870 | ALOGD("handleInterceptActions: Not passing key to user."); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 871 | #endif |
| 872 | } |
| 873 | } |
| 874 | |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 875 | nsecs_t NativeInputManager::interceptKeyBeforeDispatching( |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 876 | const sp<InputWindowHandle>& inputWindowHandle, |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 877 | const KeyEvent* keyEvent, uint32_t policyFlags) { |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 878 | // Policy: |
| 879 | // - Ignore untrusted events and pass them along. |
| 880 | // - Filter normal events and trusted injected events through the window manager policy to |
| 881 | // handle the HOME key and the like. |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 882 | nsecs_t result = 0; |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 883 | if (policyFlags & POLICY_FLAG_TRUSTED) { |
| 884 | JNIEnv* env = jniEnv(); |
Jeff Brown | d009787 | 2010-06-30 14:41:59 -0700 | [diff] [blame] | 885 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 886 | // Note: inputWindowHandle may be null. |
| 887 | jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 888 | jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent); |
| 889 | if (keyEventObj) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 890 | jlong delayMillis = env->CallLongMethod(mServiceObj, |
| 891 | gServiceClassInfo.interceptKeyBeforeDispatching, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 892 | inputWindowHandleObj, keyEventObj, policyFlags); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 893 | bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching"); |
| 894 | android_view_KeyEvent_recycle(env, keyEventObj); |
| 895 | env->DeleteLocalRef(keyEventObj); |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 896 | if (!error) { |
| 897 | if (delayMillis < 0) { |
| 898 | result = -1; |
| 899 | } else if (delayMillis > 0) { |
| 900 | result = milliseconds_to_nanoseconds(delayMillis); |
| 901 | } |
| 902 | } |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 903 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 904 | ALOGE("Failed to obtain key event object for interceptKeyBeforeDispatching."); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 905 | } |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 906 | env->DeleteLocalRef(inputWindowHandleObj); |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 907 | } |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 908 | return result; |
Jeff Brown | d009787 | 2010-06-30 14:41:59 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 911 | bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 912 | const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 913 | // Policy: |
| 914 | // - Ignore untrusted events and do not perform default handling. |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 915 | bool result = false; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 916 | if (policyFlags & POLICY_FLAG_TRUSTED) { |
| 917 | JNIEnv* env = jniEnv(); |
| 918 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 919 | // Note: inputWindowHandle may be null. |
| 920 | jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 921 | jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent); |
| 922 | if (keyEventObj) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 923 | jobject fallbackKeyEventObj = env->CallObjectMethod(mServiceObj, |
| 924 | gServiceClassInfo.dispatchUnhandledKey, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 925 | inputWindowHandleObj, keyEventObj, policyFlags); |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 926 | if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) { |
| 927 | fallbackKeyEventObj = NULL; |
| 928 | } |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 929 | android_view_KeyEvent_recycle(env, keyEventObj); |
| 930 | env->DeleteLocalRef(keyEventObj); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 931 | |
| 932 | if (fallbackKeyEventObj) { |
| 933 | // Note: outFallbackKeyEvent may be the same object as keyEvent. |
| 934 | if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj, |
| 935 | outFallbackKeyEvent)) { |
| 936 | result = true; |
| 937 | } |
| 938 | android_view_KeyEvent_recycle(env, fallbackKeyEventObj); |
| 939 | env->DeleteLocalRef(fallbackKeyEventObj); |
| 940 | } |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 941 | } else { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 942 | ALOGE("Failed to obtain key event object for dispatchUnhandledKey."); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 943 | } |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 944 | env->DeleteLocalRef(inputWindowHandleObj); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 945 | } |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 946 | return result; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 949 | void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) { |
| 950 | android_server_PowerManagerService_userActivity(eventTime, eventType); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 953 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 954 | bool NativeInputManager::checkInjectEventsPermissionNonReentrant( |
| 955 | int32_t injectorPid, int32_t injectorUid) { |
| 956 | JNIEnv* env = jniEnv(); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 957 | jboolean result = env->CallBooleanMethod(mServiceObj, |
| 958 | gServiceClassInfo.checkInjectEventsPermission, injectorPid, injectorUid); |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 959 | if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) { |
| 960 | result = false; |
| 961 | } |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 962 | return result; |
| 963 | } |
| 964 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 965 | void NativeInputManager::loadPointerResources(PointerResources* outResources) { |
| 966 | JNIEnv* env = jniEnv(); |
| 967 | |
| 968 | loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_HOVER, |
| 969 | &outResources->spotHover); |
| 970 | loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_TOUCH, |
| 971 | &outResources->spotTouch); |
| 972 | loadSystemIconAsSprite(env, mContextObj, POINTER_ICON_STYLE_SPOT_ANCHOR, |
| 973 | &outResources->spotAnchor); |
| 974 | } |
| 975 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 976 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 977 | // ---------------------------------------------------------------------------- |
| 978 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 979 | static jint nativeInit(JNIEnv* env, jclass clazz, |
| 980 | jobject serviceObj, jobject contextObj, jobject messageQueueObj) { |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 981 | sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj); |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 982 | if (messageQueue == NULL) { |
| 983 | jniThrowRuntimeException(env, "MessageQueue is not initialized."); |
| 984 | return 0; |
| 985 | } |
| 986 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 987 | NativeInputManager* im = new NativeInputManager(contextObj, serviceObj, |
| 988 | messageQueue->getLooper()); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 989 | im->incStrong(0); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 990 | return reinterpret_cast<jint>(im); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 993 | static void nativeStart(JNIEnv* env, jclass clazz, jint ptr) { |
| 994 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 995 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 996 | status_t result = im->getInputManager()->start(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 997 | if (result) { |
| 998 | jniThrowRuntimeException(env, "Input manager could not be started."); |
| 999 | } |
| 1000 | } |
| 1001 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1002 | static void nativeSetDisplayViewport(JNIEnv* env, jclass clazz, jint ptr, jboolean external, |
| 1003 | jint displayId, jint orientation, |
| 1004 | jint logicalLeft, jint logicalTop, jint logicalRight, jint logicalBottom, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 1005 | jint physicalLeft, jint physicalTop, jint physicalRight, jint physicalBottom, |
| 1006 | jint deviceWidth, jint deviceHeight) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1007 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1008 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1009 | DisplayViewport v; |
| 1010 | v.displayId = displayId; |
| 1011 | v.orientation = orientation; |
| 1012 | v.logicalLeft = logicalLeft; |
| 1013 | v.logicalTop = logicalTop; |
| 1014 | v.logicalRight = logicalRight; |
| 1015 | v.logicalBottom = logicalBottom; |
| 1016 | v.physicalLeft = physicalLeft; |
| 1017 | v.physicalTop = physicalTop; |
| 1018 | v.physicalRight = physicalRight; |
| 1019 | v.physicalBottom = physicalBottom; |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 1020 | v.deviceWidth = deviceWidth; |
| 1021 | v.deviceHeight = deviceHeight; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1022 | im->setDisplayViewport(external, v); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1025 | static jint nativeGetScanCodeState(JNIEnv* env, jclass clazz, |
| 1026 | jint ptr, jint deviceId, jint sourceMask, jint scanCode) { |
| 1027 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1028 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1029 | return im->getInputManager()->getReader()->getScanCodeState( |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1030 | deviceId, uint32_t(sourceMask), scanCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1033 | static jint nativeGetKeyCodeState(JNIEnv* env, jclass clazz, |
| 1034 | jint ptr, jint deviceId, jint sourceMask, jint keyCode) { |
| 1035 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1036 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1037 | return im->getInputManager()->getReader()->getKeyCodeState( |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1038 | deviceId, uint32_t(sourceMask), keyCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1041 | static jint nativeGetSwitchState(JNIEnv* env, jclass clazz, |
| 1042 | jint ptr, jint deviceId, jint sourceMask, jint sw) { |
| 1043 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1044 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1045 | return im->getInputManager()->getReader()->getSwitchState( |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1046 | deviceId, uint32_t(sourceMask), sw); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1049 | static jboolean nativeHasKeys(JNIEnv* env, jclass clazz, |
| 1050 | jint ptr, jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) { |
| 1051 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1052 | |
| 1053 | int32_t* codes = env->GetIntArrayElements(keyCodes, NULL); |
| 1054 | uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL); |
| 1055 | jsize numCodes = env->GetArrayLength(keyCodes); |
| 1056 | jboolean result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1057 | if (numCodes == env->GetArrayLength(keyCodes)) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1058 | result = im->getInputManager()->getReader()->hasKeys( |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1059 | deviceId, uint32_t(sourceMask), numCodes, codes, flags); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1060 | } else { |
| 1061 | result = JNI_FALSE; |
| 1062 | } |
| 1063 | |
| 1064 | env->ReleaseBooleanArrayElements(outFlags, flags, 0); |
| 1065 | env->ReleaseIntArrayElements(keyCodes, codes, 0); |
| 1066 | return result; |
| 1067 | } |
| 1068 | |
| 1069 | static void throwInputChannelNotInitialized(JNIEnv* env) { |
| 1070 | jniThrowException(env, "java/lang/IllegalStateException", |
| 1071 | "inputChannel is not initialized"); |
| 1072 | } |
| 1073 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1074 | static void handleInputChannelDisposed(JNIEnv* env, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1075 | jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1076 | NativeInputManager* im = static_cast<NativeInputManager*>(data); |
| 1077 | |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1078 | ALOGW("Input channel object '%s' was disposed without first being unregistered with " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1079 | "the input manager!", inputChannel->getName().string()); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1080 | im->unregisterInputChannel(env, inputChannel); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1083 | static void nativeRegisterInputChannel(JNIEnv* env, jclass clazz, |
| 1084 | jint ptr, jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) { |
| 1085 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1086 | |
| 1087 | sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env, |
| 1088 | inputChannelObj); |
| 1089 | if (inputChannel == NULL) { |
| 1090 | throwInputChannelNotInitialized(env); |
| 1091 | return; |
| 1092 | } |
| 1093 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1094 | sp<InputWindowHandle> inputWindowHandle = |
| 1095 | android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1096 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1097 | status_t status = im->registerInputChannel( |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1098 | env, inputChannel, inputWindowHandle, monitor); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1099 | if (status) { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1100 | String8 message; |
| 1101 | message.appendFormat("Failed to register input channel. status=%d", status); |
| 1102 | jniThrowRuntimeException(env, message.string()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1103 | return; |
| 1104 | } |
| 1105 | |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 1106 | if (! monitor) { |
| 1107 | android_view_InputChannel_setDisposeCallback(env, inputChannelObj, |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1108 | handleInputChannelDisposed, im); |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 1109 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1112 | static void nativeUnregisterInputChannel(JNIEnv* env, jclass clazz, |
| 1113 | jint ptr, jobject inputChannelObj) { |
| 1114 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1115 | |
| 1116 | sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env, |
| 1117 | inputChannelObj); |
| 1118 | if (inputChannel == NULL) { |
| 1119 | throwInputChannelNotInitialized(env); |
| 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL); |
| 1124 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1125 | status_t status = im->unregisterInputChannel(env, inputChannel); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1126 | if (status && status != BAD_VALUE) { // ignore already unregistered channel |
| 1127 | String8 message; |
| 1128 | message.appendFormat("Failed to unregister input channel. status=%d", status); |
| 1129 | jniThrowRuntimeException(env, message.string()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1133 | static void nativeSetInputFilterEnabled(JNIEnv* env, jclass clazz, |
| 1134 | jint ptr, jboolean enabled) { |
| 1135 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 1136 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1137 | im->getInputManager()->getDispatcher()->setInputFilterEnabled(enabled); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1140 | static jint nativeInjectInputEvent(JNIEnv* env, jclass clazz, |
| 1141 | jint ptr, jobject inputEventObj, jint injectorPid, jint injectorUid, |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 1142 | jint syncMode, jint timeoutMillis, jint policyFlags) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1143 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1144 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1145 | if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) { |
| 1146 | KeyEvent keyEvent; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 1147 | status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent); |
| 1148 | if (status) { |
| 1149 | jniThrowRuntimeException(env, "Could not read contents of KeyEvent object."); |
| 1150 | return INPUT_EVENT_INJECTION_FAILED; |
| 1151 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1152 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1153 | return im->getInputManager()->getDispatcher()->injectInputEvent( |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 1154 | & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis, |
| 1155 | uint32_t(policyFlags)); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1156 | } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 1157 | const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj); |
| 1158 | if (!motionEvent) { |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 1159 | jniThrowRuntimeException(env, "Could not read contents of MotionEvent object."); |
| 1160 | return INPUT_EVENT_INJECTION_FAILED; |
| 1161 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1162 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1163 | return im->getInputManager()->getDispatcher()->injectInputEvent( |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 1164 | motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis, |
| 1165 | uint32_t(policyFlags)); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1166 | } else { |
| 1167 | jniThrowRuntimeException(env, "Invalid input event type."); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1168 | return INPUT_EVENT_INJECTION_FAILED; |
| 1169 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1172 | static void nativeSetInputWindows(JNIEnv* env, jclass clazz, |
| 1173 | jint ptr, jobjectArray windowHandleObjArray) { |
| 1174 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1175 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1176 | im->setInputWindows(env, windowHandleObjArray); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1179 | static void nativeSetFocusedApplication(JNIEnv* env, jclass clazz, |
| 1180 | jint ptr, jobject applicationHandleObj) { |
| 1181 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1182 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1183 | im->setFocusedApplication(env, applicationHandleObj); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1186 | static void nativeSetInputDispatchMode(JNIEnv* env, |
| 1187 | jclass clazz, jint ptr, jboolean enabled, jboolean frozen) { |
| 1188 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1189 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1190 | im->setInputDispatchMode(enabled, frozen); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1193 | static void nativeSetSystemUiVisibility(JNIEnv* env, |
| 1194 | jclass clazz, jint ptr, jint visibility) { |
| 1195 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1196 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1197 | im->setSystemUiVisibility(visibility); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1200 | static jboolean nativeTransferTouchFocus(JNIEnv* env, |
| 1201 | jclass clazz, jint ptr, jobject fromChannelObj, jobject toChannelObj) { |
| 1202 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 1203 | |
| 1204 | sp<InputChannel> fromChannel = |
| 1205 | android_view_InputChannel_getInputChannel(env, fromChannelObj); |
| 1206 | sp<InputChannel> toChannel = |
| 1207 | android_view_InputChannel_getInputChannel(env, toChannelObj); |
| 1208 | |
| 1209 | if (fromChannel == NULL || toChannel == NULL) { |
| 1210 | return false; |
| 1211 | } |
| 1212 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1213 | return im->getInputManager()->getDispatcher()-> |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 1214 | transferTouchFocus(fromChannel, toChannel); |
| 1215 | } |
| 1216 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1217 | static void nativeSetPointerSpeed(JNIEnv* env, |
| 1218 | jclass clazz, jint ptr, jint speed) { |
| 1219 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1220 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1221 | im->setPointerSpeed(speed); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1224 | static void nativeSetShowTouches(JNIEnv* env, |
| 1225 | jclass clazz, jint ptr, jboolean enabled) { |
| 1226 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 1227 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1228 | im->setShowTouches(enabled); |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1231 | static void nativeVibrate(JNIEnv* env, |
| 1232 | jclass clazz, jint ptr, jint deviceId, jlongArray patternObj, |
| 1233 | jint repeat, jint token) { |
| 1234 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
| 1235 | |
| 1236 | size_t patternSize = env->GetArrayLength(patternObj); |
| 1237 | if (patternSize > MAX_VIBRATE_PATTERN_SIZE) { |
| 1238 | ALOGI("Skipped requested vibration because the pattern size is %d " |
| 1239 | "which is more than the maximum supported size of %d.", |
| 1240 | patternSize, MAX_VIBRATE_PATTERN_SIZE); |
| 1241 | return; // limit to reasonable size |
| 1242 | } |
| 1243 | |
| 1244 | jlong* patternMillis = static_cast<jlong*>(env->GetPrimitiveArrayCritical( |
| 1245 | patternObj, NULL)); |
| 1246 | nsecs_t pattern[patternSize]; |
| 1247 | for (size_t i = 0; i < patternSize; i++) { |
| 1248 | pattern[i] = max(jlong(0), min(patternMillis[i], |
| 1249 | MAX_VIBRATE_PATTERN_DELAY_NSECS / 1000000LL)) * 1000000LL; |
| 1250 | } |
| 1251 | env->ReleasePrimitiveArrayCritical(patternObj, patternMillis, JNI_ABORT); |
| 1252 | |
| 1253 | im->getInputManager()->getReader()->vibrate(deviceId, pattern, patternSize, repeat, token); |
| 1254 | } |
| 1255 | |
| 1256 | static void nativeCancelVibrate(JNIEnv* env, |
| 1257 | jclass clazz, jint ptr, jint deviceId, jint token) { |
| 1258 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
| 1259 | |
| 1260 | im->getInputManager()->getReader()->cancelVibrate(deviceId, token); |
| 1261 | } |
| 1262 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1263 | static void nativeReloadKeyboardLayouts(JNIEnv* env, |
| 1264 | jclass clazz, jint ptr) { |
| 1265 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
| 1266 | |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 1267 | im->getInputManager()->getReader()->requestRefreshConfiguration( |
| 1268 | InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS); |
| 1269 | } |
| 1270 | |
| 1271 | static void nativeReloadDeviceAliases(JNIEnv* env, |
| 1272 | jclass clazz, jint ptr) { |
| 1273 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
| 1274 | |
| 1275 | im->getInputManager()->getReader()->requestRefreshConfiguration( |
| 1276 | InputReaderConfiguration::CHANGE_DEVICE_ALIAS); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1279 | static jstring nativeDump(JNIEnv* env, jclass clazz, jint ptr) { |
| 1280 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 1281 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1282 | String8 dump; |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1283 | im->dump(dump); |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 1284 | return env->NewStringUTF(dump.string()); |
| 1285 | } |
| 1286 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1287 | static void nativeMonitor(JNIEnv* env, jclass clazz, jint ptr) { |
| 1288 | NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 1289 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1290 | im->getInputManager()->getReader()->monitor(); |
| 1291 | im->getInputManager()->getDispatcher()->monitor(); |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1294 | // ---------------------------------------------------------------------------- |
| 1295 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1296 | static JNINativeMethod gInputManagerMethods[] = { |
| 1297 | /* name, signature, funcPtr */ |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1298 | { "nativeInit", |
| 1299 | "(Lcom/android/server/input/InputManagerService;Landroid/content/Context;Landroid/os/MessageQueue;)I", |
| 1300 | (void*) nativeInit }, |
| 1301 | { "nativeStart", "(I)V", |
| 1302 | (void*) nativeStart }, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 1303 | { "nativeSetDisplayViewport", "(IZIIIIIIIIIIII)V", |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 1304 | (void*) nativeSetDisplayViewport }, |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1305 | { "nativeGetScanCodeState", "(IIII)I", |
| 1306 | (void*) nativeGetScanCodeState }, |
| 1307 | { "nativeGetKeyCodeState", "(IIII)I", |
| 1308 | (void*) nativeGetKeyCodeState }, |
| 1309 | { "nativeGetSwitchState", "(IIII)I", |
| 1310 | (void*) nativeGetSwitchState }, |
| 1311 | { "nativeHasKeys", "(III[I[Z)Z", |
| 1312 | (void*) nativeHasKeys }, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1313 | { "nativeRegisterInputChannel", |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1314 | "(ILandroid/view/InputChannel;Lcom/android/server/input/InputWindowHandle;Z)V", |
| 1315 | (void*) nativeRegisterInputChannel }, |
| 1316 | { "nativeUnregisterInputChannel", "(ILandroid/view/InputChannel;)V", |
| 1317 | (void*) nativeUnregisterInputChannel }, |
| 1318 | { "nativeSetInputFilterEnabled", "(IZ)V", |
| 1319 | (void*) nativeSetInputFilterEnabled }, |
| 1320 | { "nativeInjectInputEvent", "(ILandroid/view/InputEvent;IIIII)I", |
| 1321 | (void*) nativeInjectInputEvent }, |
| 1322 | { "nativeSetInputWindows", "(I[Lcom/android/server/input/InputWindowHandle;)V", |
| 1323 | (void*) nativeSetInputWindows }, |
| 1324 | { "nativeSetFocusedApplication", "(ILcom/android/server/input/InputApplicationHandle;)V", |
| 1325 | (void*) nativeSetFocusedApplication }, |
| 1326 | { "nativeSetInputDispatchMode", "(IZZ)V", |
| 1327 | (void*) nativeSetInputDispatchMode }, |
| 1328 | { "nativeSetSystemUiVisibility", "(II)V", |
| 1329 | (void*) nativeSetSystemUiVisibility }, |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1330 | { "nativeTransferTouchFocus", "(ILandroid/view/InputChannel;Landroid/view/InputChannel;)Z", |
| 1331 | (void*) nativeTransferTouchFocus }, |
| 1332 | { "nativeSetPointerSpeed", "(II)V", |
| 1333 | (void*) nativeSetPointerSpeed }, |
| 1334 | { "nativeSetShowTouches", "(IZ)V", |
| 1335 | (void*) nativeSetShowTouches }, |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1336 | { "nativeVibrate", "(II[JII)V", |
| 1337 | (void*) nativeVibrate }, |
| 1338 | { "nativeCancelVibrate", "(III)V", |
| 1339 | (void*) nativeCancelVibrate }, |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1340 | { "nativeReloadKeyboardLayouts", "(I)V", |
| 1341 | (void*) nativeReloadKeyboardLayouts }, |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 1342 | { "nativeReloadDeviceAliases", "(I)V", |
| 1343 | (void*) nativeReloadDeviceAliases }, |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1344 | { "nativeDump", "(I)Ljava/lang/String;", |
| 1345 | (void*) nativeDump }, |
| 1346 | { "nativeMonitor", "(I)V", |
| 1347 | (void*) nativeMonitor }, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1348 | }; |
| 1349 | |
| 1350 | #define FIND_CLASS(var, className) \ |
| 1351 | var = env->FindClass(className); \ |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1352 | LOG_FATAL_IF(! var, "Unable to find class " className); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1353 | |
| 1354 | #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 1355 | var = env->GetMethodID(clazz, methodName, methodDescriptor); \ |
| 1356 | LOG_FATAL_IF(! var, "Unable to find method " methodName); |
| 1357 | |
| 1358 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 1359 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 1360 | LOG_FATAL_IF(! var, "Unable to find field " fieldName); |
| 1361 | |
| 1362 | int register_android_server_InputManager(JNIEnv* env) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1363 | int res = jniRegisterNativeMethods(env, "com/android/server/input/InputManagerService", |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1364 | gInputManagerMethods, NELEM(gInputManagerMethods)); |
| 1365 | LOG_FATAL_IF(res < 0, "Unable to register native methods."); |
| 1366 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1367 | // Callbacks |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1368 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1369 | jclass clazz; |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1370 | FIND_CLASS(clazz, "com/android/server/input/InputManagerService"); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1371 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1372 | GET_METHOD_ID(gServiceClassInfo.notifyConfigurationChanged, clazz, |
Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 1373 | "notifyConfigurationChanged", "(J)V"); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1374 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1375 | GET_METHOD_ID(gServiceClassInfo.notifyInputDevicesChanged, clazz, |
| 1376 | "notifyInputDevicesChanged", "([Landroid/view/InputDevice;)V"); |
| 1377 | |
Jeff Brown | 5338428 | 2012-08-20 20:16:01 -0700 | [diff] [blame] | 1378 | GET_METHOD_ID(gServiceClassInfo.notifySwitch, clazz, |
| 1379 | "notifySwitch", "(JII)V"); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1380 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1381 | GET_METHOD_ID(gServiceClassInfo.notifyInputChannelBroken, clazz, |
| 1382 | "notifyInputChannelBroken", "(Lcom/android/server/input/InputWindowHandle;)V"); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1383 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1384 | GET_METHOD_ID(gServiceClassInfo.notifyANR, clazz, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1385 | "notifyANR", |
Jeff Brown | bd181bb | 2013-09-10 16:44:24 -0700 | [diff] [blame^] | 1386 | "(Lcom/android/server/input/InputApplicationHandle;Lcom/android/server/input/InputWindowHandle;Ljava/lang/String;)J"); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1387 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1388 | GET_METHOD_ID(gServiceClassInfo.filterInputEvent, clazz, |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 1389 | "filterInputEvent", "(Landroid/view/InputEvent;I)Z"); |
| 1390 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1391 | GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeQueueing, clazz, |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 1392 | "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I"); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1393 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1394 | GET_METHOD_ID(gServiceClassInfo.interceptMotionBeforeQueueingWhenScreenOff, |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1395 | clazz, |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1396 | "interceptMotionBeforeQueueingWhenScreenOff", "(I)I"); |
| 1397 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1398 | GET_METHOD_ID(gServiceClassInfo.interceptKeyBeforeDispatching, clazz, |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 1399 | "interceptKeyBeforeDispatching", |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1400 | "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)J"); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1401 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1402 | GET_METHOD_ID(gServiceClassInfo.dispatchUnhandledKey, clazz, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1403 | "dispatchUnhandledKey", |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1404 | "(Lcom/android/server/input/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;"); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 1405 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1406 | GET_METHOD_ID(gServiceClassInfo.checkInjectEventsPermission, clazz, |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1407 | "checkInjectEventsPermission", "(II)Z"); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1408 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1409 | GET_METHOD_ID(gServiceClassInfo.getVirtualKeyQuietTimeMillis, clazz, |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1410 | "getVirtualKeyQuietTimeMillis", "()I"); |
| 1411 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1412 | GET_METHOD_ID(gServiceClassInfo.getExcludedDeviceNames, clazz, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1413 | "getExcludedDeviceNames", "()[Ljava/lang/String;"); |
| 1414 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1415 | GET_METHOD_ID(gServiceClassInfo.getKeyRepeatTimeout, clazz, |
Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 1416 | "getKeyRepeatTimeout", "()I"); |
| 1417 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1418 | GET_METHOD_ID(gServiceClassInfo.getKeyRepeatDelay, clazz, |
Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 1419 | "getKeyRepeatDelay", "()I"); |
| 1420 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1421 | GET_METHOD_ID(gServiceClassInfo.getHoverTapTimeout, clazz, |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 1422 | "getHoverTapTimeout", "()I"); |
| 1423 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1424 | GET_METHOD_ID(gServiceClassInfo.getHoverTapSlop, clazz, |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 1425 | "getHoverTapSlop", "()I"); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 1426 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1427 | GET_METHOD_ID(gServiceClassInfo.getDoubleTapTimeout, clazz, |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 1428 | "getDoubleTapTimeout", "()I"); |
| 1429 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1430 | GET_METHOD_ID(gServiceClassInfo.getLongPressTimeout, clazz, |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 1431 | "getLongPressTimeout", "()I"); |
| 1432 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1433 | GET_METHOD_ID(gServiceClassInfo.getPointerLayer, clazz, |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1434 | "getPointerLayer", "()I"); |
| 1435 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1436 | GET_METHOD_ID(gServiceClassInfo.getPointerIcon, clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1437 | "getPointerIcon", "()Landroid/view/PointerIcon;"); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1438 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1439 | GET_METHOD_ID(gServiceClassInfo.getKeyboardLayoutOverlay, clazz, |
| 1440 | "getKeyboardLayoutOverlay", "(Ljava/lang/String;)[Ljava/lang/String;"); |
| 1441 | |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 1442 | GET_METHOD_ID(gServiceClassInfo.getDeviceAlias, clazz, |
| 1443 | "getDeviceAlias", "(Ljava/lang/String;)Ljava/lang/String;"); |
| 1444 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1445 | // InputDevice |
| 1446 | |
| 1447 | FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice"); |
| 1448 | gInputDeviceClassInfo.clazz = jclass(env->NewGlobalRef(gInputDeviceClassInfo.clazz)); |
| 1449 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1450 | // KeyEvent |
| 1451 | |
| 1452 | FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent"); |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1453 | gKeyEventClassInfo.clazz = jclass(env->NewGlobalRef(gKeyEventClassInfo.clazz)); |
| 1454 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1455 | // MotionEvent |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1456 | |
| 1457 | FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent"); |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 1458 | gMotionEventClassInfo.clazz = jclass(env->NewGlobalRef(gMotionEventClassInfo.clazz)); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1459 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1460 | return 0; |
| 1461 | } |
| 1462 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1463 | } /* namespace android */ |