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