| Jeff Brown | 46b9ac0a | 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 | 46b9ac0a | 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 | 46b9ac0a | 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 | 46b9ac0a | 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 | 46b9ac0a | 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 | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 39 | #include <input/SpotController.h> |
| 40 | #include <input/SpriteController.h> |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 41 | |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 42 | #include <android_os_MessageQueue.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> |
| 46 | #include <android/graphics/GraphicsJNI.h> |
| 47 | |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 48 | #include "com_android_server_PowerManagerService.h" |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 49 | #include "com_android_server_InputApplication.h" |
| 50 | #include "com_android_server_InputApplicationHandle.h" |
| 51 | #include "com_android_server_InputWindow.h" |
| 52 | #include "com_android_server_InputWindowHandle.h" |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 53 | |
| 54 | namespace android { |
| 55 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 56 | static struct { |
| 57 | jclass clazz; |
| 58 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 59 | jmethodID notifyConfigurationChanged; |
| 60 | jmethodID notifyLidSwitchChanged; |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 61 | jmethodID notifyInputChannelBroken; |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 62 | jmethodID notifyANR; |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 63 | jmethodID interceptKeyBeforeQueueing; |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 64 | jmethodID interceptMotionBeforeQueueingWhenScreenOff; |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 65 | jmethodID interceptKeyBeforeDispatching; |
| Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 66 | jmethodID dispatchUnhandledKey; |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 67 | jmethodID checkInjectEventsPermission; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 68 | jmethodID filterTouchEvents; |
| 69 | jmethodID filterJumpyTouchEvents; |
| Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 70 | jmethodID getVirtualKeyQuietTimeMillis; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 71 | jmethodID getExcludedDeviceNames; |
| Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 72 | jmethodID getKeyRepeatTimeout; |
| 73 | jmethodID getKeyRepeatDelay; |
| Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 74 | jmethodID getMaxEventsPerSecond; |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 75 | jmethodID getPointerLayer; |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 76 | jmethodID getPointerIcon; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 77 | } gCallbacksClassInfo; |
| 78 | |
| 79 | static struct { |
| 80 | jclass clazz; |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 81 | } gKeyEventClassInfo; |
| 82 | |
| 83 | static struct { |
| 84 | jclass clazz; |
| 85 | } gMotionEventClassInfo; |
| 86 | |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 87 | static struct { |
| 88 | jclass clazz; |
| 89 | |
| 90 | jmethodID ctor; |
| 91 | jmethodID addMotionRange; |
| 92 | |
| 93 | jfieldID mId; |
| 94 | jfieldID mName; |
| 95 | jfieldID mSources; |
| 96 | jfieldID mKeyboardType; |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 97 | } gInputDeviceClassInfo; |
| 98 | |
| Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 99 | static struct { |
| 100 | jclass clazz; |
| 101 | |
| 102 | jfieldID touchscreen; |
| 103 | jfieldID keyboard; |
| 104 | jfieldID navigation; |
| 105 | } gConfigurationClassInfo; |
| 106 | |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 107 | static struct { |
| 108 | jclass clazz; |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 109 | |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 110 | jfieldID bitmap; |
| 111 | jfieldID hotSpotX; |
| 112 | jfieldID hotSpotY; |
| 113 | } gPointerIconClassInfo; |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 114 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 115 | |
| 116 | // --- Global functions --- |
| 117 | |
| 118 | static jobject getInputApplicationHandleObjLocalRef(JNIEnv* env, |
| 119 | const sp<InputApplicationHandle>& inputApplicationHandle) { |
| 120 | if (inputApplicationHandle == NULL) { |
| 121 | return NULL; |
| 122 | } |
| 123 | return static_cast<NativeInputApplicationHandle*>(inputApplicationHandle.get())-> |
| 124 | getInputApplicationHandleObjLocalRef(env); |
| 125 | } |
| 126 | |
| 127 | static jobject getInputWindowHandleObjLocalRef(JNIEnv* env, |
| 128 | const sp<InputWindowHandle>& inputWindowHandle) { |
| 129 | if (inputWindowHandle == NULL) { |
| 130 | return NULL; |
| 131 | } |
| 132 | return static_cast<NativeInputWindowHandle*>(inputWindowHandle.get())-> |
| 133 | getInputWindowHandleObjLocalRef(env); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | // --- NativeInputManager --- |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 138 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 139 | class NativeInputManager : public virtual RefBase, |
| 140 | public virtual InputReaderPolicyInterface, |
| 141 | public virtual InputDispatcherPolicyInterface { |
| 142 | protected: |
| 143 | virtual ~NativeInputManager(); |
| 144 | |
| 145 | public: |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 146 | NativeInputManager(jobject callbacksObj, const sp<Looper>& looper); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 147 | |
| 148 | inline sp<InputManager> getInputManager() const { return mInputManager; } |
| 149 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 150 | void dump(String8& dump); |
| Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 151 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 152 | void setDisplaySize(int32_t displayId, int32_t width, int32_t height); |
| 153 | void setDisplayOrientation(int32_t displayId, int32_t orientation); |
| 154 | |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 155 | status_t registerInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 156 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 157 | status_t unregisterInputChannel(JNIEnv* env, const sp<InputChannel>& inputChannel); |
| 158 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 159 | void setInputWindows(JNIEnv* env, jobjectArray windowObjArray); |
| 160 | void setFocusedApplication(JNIEnv* env, jobject applicationObj); |
| 161 | void setInputDispatchMode(bool enabled, bool frozen); |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 162 | void setSystemUiVisibility(int32_t visibility); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 163 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 164 | /* --- InputReaderPolicyInterface implementation --- */ |
| 165 | |
| 166 | virtual bool getDisplayInfo(int32_t displayId, |
| 167 | int32_t* width, int32_t* height, int32_t* orientation); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 168 | virtual bool filterTouchEvents(); |
| 169 | virtual bool filterJumpyTouchEvents(); |
| Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 170 | virtual nsecs_t getVirtualKeyQuietTime(); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 171 | virtual void getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames); |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 172 | virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId); |
| Jeff Brown | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 173 | virtual sp<SpotControllerInterface> obtainSpotController(int32_t deviceId); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 174 | |
| 175 | /* --- InputDispatcherPolicyInterface implementation --- */ |
| 176 | |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 177 | virtual void notifySwitch(nsecs_t when, int32_t switchCode, int32_t switchValue, |
| 178 | uint32_t policyFlags); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 179 | virtual void notifyConfigurationChanged(nsecs_t when); |
| Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 180 | virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 181 | const sp<InputWindowHandle>& inputWindowHandle); |
| 182 | virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 183 | virtual nsecs_t getKeyRepeatTimeout(); |
| Jeff Brown | b21fb10 | 2010-09-07 10:44:57 -0700 | [diff] [blame] | 184 | virtual nsecs_t getKeyRepeatDelay(); |
| Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 185 | virtual int32_t getMaxEventsPerSecond(); |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 186 | virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags); |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 187 | virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags); |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 188 | virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle, |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 189 | const KeyEvent* keyEvent, uint32_t policyFlags); |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 190 | virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle, |
| Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 191 | const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent); |
| Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 192 | virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType); |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 193 | virtual bool checkInjectEventsPermissionNonReentrant( |
| 194 | int32_t injectorPid, int32_t injectorUid); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 195 | |
| 196 | private: |
| 197 | sp<InputManager> mInputManager; |
| 198 | |
| 199 | jobject mCallbacksObj; |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 200 | sp<Looper> mLooper; |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 201 | |
| 202 | // Cached filtering policies. |
| 203 | int32_t mFilterTouchEvents; |
| 204 | int32_t mFilterJumpyTouchEvents; |
| Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 205 | nsecs_t mVirtualKeyQuietTime; |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 206 | |
| Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 207 | // Cached key repeat policy. |
| 208 | nsecs_t mKeyRepeatTimeout; |
| 209 | nsecs_t mKeyRepeatDelay; |
| 210 | |
| Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 211 | // Cached throttling policy. |
| 212 | int32_t mMaxEventsPerSecond; |
| 213 | |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 214 | Mutex mLock; |
| 215 | struct Locked { |
| 216 | // Display size information. |
| 217 | int32_t displayWidth, displayHeight; // -1 when initialized |
| 218 | int32_t displayOrientation; |
| 219 | |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 220 | // System UI visibility. |
| 221 | int32_t systemUiVisibility; |
| 222 | |
| Jeff Brown | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 223 | // Sprite controller singleton, created on first use. |
| 224 | sp<SpriteController> spriteController; |
| 225 | |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 226 | // Pointer controller singleton, created and destroyed as needed. |
| 227 | wp<PointerController> pointerController; |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 228 | } mLocked; |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 229 | |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 230 | void updateInactivityFadeDelayLocked(const sp<PointerController>& controller); |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 231 | void handleInterceptActions(jint wmActions, nsecs_t when, uint32_t& policyFlags); |
| Jeff Brown | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 232 | void ensureSpriteControllerLocked(); |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 233 | |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 234 | // Power manager interactions. |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 235 | bool isScreenOn(); |
| 236 | bool isScreenBright(); |
| 237 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 238 | static bool checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName); |
| Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 239 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 240 | static inline JNIEnv* jniEnv() { |
| 241 | return AndroidRuntime::getJNIEnv(); |
| 242 | } |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 245 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 246 | |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 247 | NativeInputManager::NativeInputManager(jobject callbacksObj, const sp<Looper>& looper) : |
| 248 | mLooper(looper), |
| 249 | mFilterTouchEvents(-1), mFilterJumpyTouchEvents(-1), mVirtualKeyQuietTime(-1), |
| Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 250 | mKeyRepeatTimeout(-1), mKeyRepeatDelay(-1), |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 251 | mMaxEventsPerSecond(-1) { |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 252 | JNIEnv* env = jniEnv(); |
| 253 | |
| 254 | mCallbacksObj = env->NewGlobalRef(callbacksObj); |
| 255 | |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 256 | { |
| 257 | AutoMutex _l(mLock); |
| 258 | mLocked.displayWidth = -1; |
| 259 | mLocked.displayHeight = -1; |
| 260 | mLocked.displayOrientation = ROTATION_0; |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 261 | |
| 262 | mLocked.systemUiVisibility = ASYSTEM_UI_VISIBILITY_STATUS_BAR_VISIBLE; |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 265 | sp<EventHub> eventHub = new EventHub(); |
| 266 | mInputManager = new InputManager(eventHub, this, this); |
| 267 | } |
| 268 | |
| 269 | NativeInputManager::~NativeInputManager() { |
| 270 | JNIEnv* env = jniEnv(); |
| 271 | |
| 272 | env->DeleteGlobalRef(mCallbacksObj); |
| 273 | } |
| 274 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 275 | void NativeInputManager::dump(String8& dump) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 276 | mInputManager->getReader()->dump(dump); |
| 277 | dump.append("\n"); |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 278 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 279 | mInputManager->getDispatcher()->dump(dump); |
| 280 | dump.append("\n"); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 283 | bool NativeInputManager::checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 284 | if (env->ExceptionCheck()) { |
| 285 | LOGE("An exception was thrown by callback '%s'.", methodName); |
| 286 | LOGE_EX(env); |
| 287 | env->ExceptionClear(); |
| 288 | return true; |
| 289 | } |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | void NativeInputManager::setDisplaySize(int32_t displayId, int32_t width, int32_t height) { |
| 294 | if (displayId == 0) { |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 295 | AutoMutex _l(mLock); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 296 | |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 297 | if (mLocked.displayWidth != width || mLocked.displayHeight != height) { |
| 298 | mLocked.displayWidth = width; |
| 299 | mLocked.displayHeight = height; |
| 300 | |
| 301 | sp<PointerController> controller = mLocked.pointerController.promote(); |
| 302 | if (controller != NULL) { |
| 303 | controller->setDisplaySize(width, height); |
| 304 | } |
| 305 | } |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | void NativeInputManager::setDisplayOrientation(int32_t displayId, int32_t orientation) { |
| 310 | if (displayId == 0) { |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 311 | AutoMutex _l(mLock); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 312 | |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 313 | if (mLocked.displayOrientation != orientation) { |
| 314 | mLocked.displayOrientation = orientation; |
| 315 | |
| 316 | sp<PointerController> controller = mLocked.pointerController.promote(); |
| 317 | if (controller != NULL) { |
| 318 | controller->setDisplayOrientation(orientation); |
| 319 | } |
| 320 | } |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 324 | status_t NativeInputManager::registerInputChannel(JNIEnv* env, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 325 | const sp<InputChannel>& inputChannel, |
| 326 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor) { |
| 327 | return mInputManager->getDispatcher()->registerInputChannel( |
| 328 | inputChannel, inputWindowHandle, monitor); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | status_t NativeInputManager::unregisterInputChannel(JNIEnv* env, |
| 332 | const sp<InputChannel>& inputChannel) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 333 | return mInputManager->getDispatcher()->unregisterInputChannel(inputChannel); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 336 | bool NativeInputManager::getDisplayInfo(int32_t displayId, |
| 337 | int32_t* width, int32_t* height, int32_t* orientation) { |
| 338 | bool result = false; |
| 339 | if (displayId == 0) { |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 340 | AutoMutex _l(mLock); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 341 | |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 342 | if (mLocked.displayWidth > 0 && mLocked.displayHeight > 0) { |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 343 | if (width) { |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 344 | *width = mLocked.displayWidth; |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 345 | } |
| 346 | if (height) { |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 347 | *height = mLocked.displayHeight; |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 348 | } |
| 349 | if (orientation) { |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 350 | *orientation = mLocked.displayOrientation; |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 351 | } |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 352 | result = true; |
| 353 | } |
| 354 | } |
| 355 | return result; |
| 356 | } |
| 357 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 358 | bool NativeInputManager::filterTouchEvents() { |
| 359 | if (mFilterTouchEvents < 0) { |
| 360 | JNIEnv* env = jniEnv(); |
| 361 | |
| 362 | jboolean result = env->CallBooleanMethod(mCallbacksObj, |
| 363 | gCallbacksClassInfo.filterTouchEvents); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 364 | if (checkAndClearExceptionFromCallback(env, "filterTouchEvents")) { |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 365 | result = false; |
| 366 | } |
| 367 | |
| 368 | mFilterTouchEvents = result ? 1 : 0; |
| 369 | } |
| 370 | return mFilterTouchEvents; |
| 371 | } |
| 372 | |
| 373 | bool NativeInputManager::filterJumpyTouchEvents() { |
| 374 | if (mFilterJumpyTouchEvents < 0) { |
| 375 | JNIEnv* env = jniEnv(); |
| 376 | |
| 377 | jboolean result = env->CallBooleanMethod(mCallbacksObj, |
| 378 | gCallbacksClassInfo.filterJumpyTouchEvents); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 379 | if (checkAndClearExceptionFromCallback(env, "filterJumpyTouchEvents")) { |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 380 | result = false; |
| 381 | } |
| 382 | |
| 383 | mFilterJumpyTouchEvents = result ? 1 : 0; |
| 384 | } |
| 385 | return mFilterJumpyTouchEvents; |
| 386 | } |
| 387 | |
| Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 388 | nsecs_t NativeInputManager::getVirtualKeyQuietTime() { |
| 389 | if (mVirtualKeyQuietTime < 0) { |
| 390 | JNIEnv* env = jniEnv(); |
| 391 | |
| 392 | jint result = env->CallIntMethod(mCallbacksObj, |
| 393 | gCallbacksClassInfo.getVirtualKeyQuietTimeMillis); |
| 394 | if (checkAndClearExceptionFromCallback(env, "getVirtualKeyQuietTimeMillis")) { |
| 395 | result = 0; |
| 396 | } |
| 397 | if (result < 0) { |
| 398 | result = 0; |
| 399 | } |
| 400 | |
| 401 | mVirtualKeyQuietTime = milliseconds_to_nanoseconds(result); |
| 402 | } |
| 403 | return mVirtualKeyQuietTime; |
| 404 | } |
| 405 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 406 | void NativeInputManager::getExcludedDeviceNames(Vector<String8>& outExcludedDeviceNames) { |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 407 | outExcludedDeviceNames.clear(); |
| 408 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 409 | JNIEnv* env = jniEnv(); |
| 410 | |
| 411 | jobjectArray result = jobjectArray(env->CallObjectMethod(mCallbacksObj, |
| 412 | gCallbacksClassInfo.getExcludedDeviceNames)); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 413 | if (! checkAndClearExceptionFromCallback(env, "getExcludedDeviceNames") && result) { |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 414 | jsize length = env->GetArrayLength(result); |
| 415 | for (jsize i = 0; i < length; i++) { |
| 416 | jstring item = jstring(env->GetObjectArrayElement(result, i)); |
| 417 | |
| 418 | const char* deviceNameChars = env->GetStringUTFChars(item, NULL); |
| 419 | outExcludedDeviceNames.add(String8(deviceNameChars)); |
| 420 | env->ReleaseStringUTFChars(item, deviceNameChars); |
| 421 | |
| 422 | env->DeleteLocalRef(item); |
| 423 | } |
| 424 | env->DeleteLocalRef(result); |
| 425 | } |
| 426 | } |
| 427 | |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 428 | sp<PointerControllerInterface> NativeInputManager::obtainPointerController(int32_t deviceId) { |
| 429 | AutoMutex _l(mLock); |
| 430 | |
| 431 | sp<PointerController> controller = mLocked.pointerController.promote(); |
| 432 | if (controller == NULL) { |
| Jeff Brown | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 433 | ensureSpriteControllerLocked(); |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 434 | |
| Jeff Brown | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 435 | controller = new PointerController(mLooper, mLocked.spriteController); |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 436 | mLocked.pointerController = controller; |
| 437 | |
| 438 | controller->setDisplaySize(mLocked.displayWidth, mLocked.displayHeight); |
| 439 | controller->setDisplayOrientation(mLocked.displayOrientation); |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 440 | |
| Jeff Brown | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 441 | JNIEnv* env = jniEnv(); |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 442 | jobject iconObj = env->CallObjectMethod(mCallbacksObj, gCallbacksClassInfo.getPointerIcon); |
| 443 | if (!checkAndClearExceptionFromCallback(env, "getPointerIcon") && iconObj) { |
| 444 | jfloat iconHotSpotX = env->GetFloatField(iconObj, gPointerIconClassInfo.hotSpotX); |
| 445 | jfloat iconHotSpotY = env->GetFloatField(iconObj, gPointerIconClassInfo.hotSpotY); |
| 446 | jobject iconBitmapObj = env->GetObjectField(iconObj, gPointerIconClassInfo.bitmap); |
| 447 | if (iconBitmapObj) { |
| 448 | SkBitmap* iconBitmap = GraphicsJNI::getNativeBitmap(env, iconBitmapObj); |
| 449 | if (iconBitmap) { |
| 450 | controller->setPointerIcon(iconBitmap, iconHotSpotX, iconHotSpotY); |
| 451 | } |
| 452 | env->DeleteLocalRef(iconBitmapObj); |
| 453 | } |
| 454 | env->DeleteLocalRef(iconObj); |
| 455 | } |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 456 | |
| 457 | updateInactivityFadeDelayLocked(controller); |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 458 | } |
| 459 | return controller; |
| 460 | } |
| 461 | |
| Jeff Brown | a6dbfdd | 2011-04-11 11:54:25 -0700 | [diff] [blame^] | 462 | sp<SpotControllerInterface> NativeInputManager::obtainSpotController(int32_t deviceId) { |
| 463 | AutoMutex _l(mLock); |
| 464 | |
| 465 | ensureSpriteControllerLocked(); |
| 466 | return new SpotController(mLooper, mLocked.spriteController); |
| 467 | } |
| 468 | |
| 469 | void NativeInputManager::ensureSpriteControllerLocked() { |
| 470 | if (mLocked.spriteController == NULL) { |
| 471 | JNIEnv* env = jniEnv(); |
| 472 | jint layer = env->CallIntMethod(mCallbacksObj, gCallbacksClassInfo.getPointerLayer); |
| 473 | if (checkAndClearExceptionFromCallback(env, "getPointerLayer")) { |
| 474 | layer = -1; |
| 475 | } |
| 476 | mLocked.spriteController = new SpriteController(mLooper, layer); |
| 477 | } |
| 478 | } |
| 479 | |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 480 | void NativeInputManager::notifySwitch(nsecs_t when, int32_t switchCode, |
| 481 | int32_t switchValue, uint32_t policyFlags) { |
| 482 | #if DEBUG_INPUT_DISPATCHER_POLICY |
| 483 | LOGD("notifySwitch - when=%lld, switchCode=%d, switchValue=%d, policyFlags=0x%x", |
| 484 | when, switchCode, switchValue, policyFlags); |
| 485 | #endif |
| 486 | |
| 487 | JNIEnv* env = jniEnv(); |
| 488 | |
| 489 | switch (switchCode) { |
| 490 | case SW_LID: |
| 491 | env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyLidSwitchChanged, |
| 492 | when, switchValue == 0); |
| 493 | checkAndClearExceptionFromCallback(env, "notifyLidSwitchChanged"); |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 498 | void NativeInputManager::notifyConfigurationChanged(nsecs_t when) { |
| 499 | #if DEBUG_INPUT_DISPATCHER_POLICY |
| 500 | LOGD("notifyConfigurationChanged - when=%lld", when); |
| 501 | #endif |
| 502 | |
| 503 | JNIEnv* env = jniEnv(); |
| 504 | |
| Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 505 | env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyConfigurationChanged, when); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 506 | checkAndClearExceptionFromCallback(env, "notifyConfigurationChanged"); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 509 | nsecs_t NativeInputManager::notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 510 | const sp<InputWindowHandle>& inputWindowHandle) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 511 | #if DEBUG_INPUT_DISPATCHER_POLICY |
| 512 | LOGD("notifyANR"); |
| 513 | #endif |
| 514 | |
| 515 | JNIEnv* env = jniEnv(); |
| 516 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 517 | jobject inputApplicationHandleObj = |
| 518 | getInputApplicationHandleObjLocalRef(env, inputApplicationHandle); |
| 519 | jobject inputWindowHandleObj = |
| 520 | getInputWindowHandleObjLocalRef(env, inputWindowHandle); |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 521 | |
| Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 522 | jlong newTimeout = env->CallLongMethod(mCallbacksObj, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 523 | gCallbacksClassInfo.notifyANR, inputApplicationHandleObj, inputWindowHandleObj); |
| Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 524 | if (checkAndClearExceptionFromCallback(env, "notifyANR")) { |
| 525 | newTimeout = 0; // abort dispatch |
| 526 | } else { |
| 527 | assert(newTimeout >= 0); |
| 528 | } |
| 529 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 530 | env->DeleteLocalRef(inputWindowHandleObj); |
| 531 | env->DeleteLocalRef(inputApplicationHandleObj); |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 532 | return newTimeout; |
| 533 | } |
| 534 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 535 | void NativeInputManager::notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) { |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 536 | #if DEBUG_INPUT_DISPATCHER_POLICY |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 537 | LOGD("notifyInputChannelBroken"); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 538 | #endif |
| 539 | |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 540 | JNIEnv* env = jniEnv(); |
| 541 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 542 | jobject inputWindowHandleObj = |
| 543 | getInputWindowHandleObjLocalRef(env, inputWindowHandle); |
| 544 | if (inputWindowHandleObj) { |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 545 | env->CallVoidMethod(mCallbacksObj, gCallbacksClassInfo.notifyInputChannelBroken, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 546 | inputWindowHandleObj); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 547 | checkAndClearExceptionFromCallback(env, "notifyInputChannelBroken"); |
| 548 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 549 | env->DeleteLocalRef(inputWindowHandleObj); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 550 | } |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 553 | nsecs_t NativeInputManager::getKeyRepeatTimeout() { |
| 554 | if (! isScreenOn()) { |
| 555 | // Disable key repeat when the screen is off. |
| 556 | return -1; |
| 557 | } else { |
| Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 558 | if (mKeyRepeatTimeout < 0) { |
| 559 | JNIEnv* env = jniEnv(); |
| 560 | |
| 561 | jint result = env->CallIntMethod(mCallbacksObj, |
| 562 | gCallbacksClassInfo.getKeyRepeatTimeout); |
| 563 | if (checkAndClearExceptionFromCallback(env, "getKeyRepeatTimeout")) { |
| 564 | result = 500; |
| 565 | } |
| 566 | |
| 567 | mKeyRepeatTimeout = milliseconds_to_nanoseconds(result); |
| 568 | } |
| 569 | return mKeyRepeatTimeout; |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| Jeff Brown | b21fb10 | 2010-09-07 10:44:57 -0700 | [diff] [blame] | 573 | nsecs_t NativeInputManager::getKeyRepeatDelay() { |
| Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 574 | if (mKeyRepeatDelay < 0) { |
| 575 | JNIEnv* env = jniEnv(); |
| 576 | |
| 577 | jint result = env->CallIntMethod(mCallbacksObj, |
| 578 | gCallbacksClassInfo.getKeyRepeatDelay); |
| 579 | if (checkAndClearExceptionFromCallback(env, "getKeyRepeatDelay")) { |
| 580 | result = 50; |
| 581 | } |
| 582 | |
| 583 | mKeyRepeatDelay = milliseconds_to_nanoseconds(result); |
| 584 | } |
| 585 | return mKeyRepeatDelay; |
| Jeff Brown | b21fb10 | 2010-09-07 10:44:57 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 588 | int32_t NativeInputManager::getMaxEventsPerSecond() { |
| 589 | if (mMaxEventsPerSecond < 0) { |
| 590 | JNIEnv* env = jniEnv(); |
| 591 | |
| 592 | jint result = env->CallIntMethod(mCallbacksObj, |
| 593 | gCallbacksClassInfo.getMaxEventsPerSecond); |
| 594 | if (checkAndClearExceptionFromCallback(env, "getMaxEventsPerSecond")) { |
| Jeff Brown | 3d8c9bd | 2010-08-18 17:48:53 -0700 | [diff] [blame] | 595 | result = 60; |
| Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | mMaxEventsPerSecond = result; |
| 599 | } |
| 600 | return mMaxEventsPerSecond; |
| 601 | } |
| 602 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 603 | void NativeInputManager::setInputWindows(JNIEnv* env, jobjectArray windowObjArray) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 604 | Vector<InputWindow> windows; |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 605 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 606 | jsize length = env->GetArrayLength(windowObjArray); |
| 607 | for (jsize i = 0; i < length; i++) { |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 608 | jobject windowObj = env->GetObjectArrayElement(windowObjArray, i); |
| 609 | if (! windowObj) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 610 | break; // found null element indicating end of used portion of the array |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 613 | windows.push(); |
| 614 | InputWindow& window = windows.editTop(); |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 615 | android_server_InputWindow_toNative(env, windowObj, &window); |
| 616 | if (window.inputChannel == NULL) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 617 | windows.pop(); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 618 | } |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 619 | env->DeleteLocalRef(windowObj); |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 620 | } |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 621 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 622 | mInputManager->getDispatcher()->setInputWindows(windows); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 625 | void NativeInputManager::setFocusedApplication(JNIEnv* env, jobject applicationObj) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 626 | if (applicationObj) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 627 | InputApplication application; |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 628 | android_server_InputApplication_toNative(env, applicationObj, &application); |
| 629 | if (application.inputApplicationHandle != NULL) { |
| 630 | mInputManager->getDispatcher()->setFocusedApplication(&application); |
| Jeff Brown | 22d789d | 2011-03-25 11:58:46 -0700 | [diff] [blame] | 631 | return; |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 632 | } |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 633 | } |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 634 | mInputManager->getDispatcher()->setFocusedApplication(NULL); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | void NativeInputManager::setInputDispatchMode(bool enabled, bool frozen) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 638 | mInputManager->getDispatcher()->setInputDispatchMode(enabled, frozen); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 641 | void NativeInputManager::setSystemUiVisibility(int32_t visibility) { |
| 642 | AutoMutex _l(mLock); |
| 643 | |
| 644 | if (mLocked.systemUiVisibility != visibility) { |
| 645 | mLocked.systemUiVisibility = visibility; |
| 646 | |
| 647 | sp<PointerController> controller = mLocked.pointerController.promote(); |
| 648 | if (controller != NULL) { |
| 649 | updateInactivityFadeDelayLocked(controller); |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | void NativeInputManager::updateInactivityFadeDelayLocked(const sp<PointerController>& controller) { |
| 655 | bool lightsOut = mLocked.systemUiVisibility & ASYSTEM_UI_VISIBILITY_STATUS_BAR_HIDDEN; |
| 656 | controller->setInactivityFadeDelay(lightsOut |
| 657 | ? PointerController::INACTIVITY_FADE_DELAY_SHORT |
| 658 | : PointerController::INACTIVITY_FADE_DELAY_NORMAL); |
| 659 | } |
| 660 | |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 661 | bool NativeInputManager::isScreenOn() { |
| 662 | return android_server_PowerManagerService_isScreenOn(); |
| 663 | } |
| 664 | |
| 665 | bool NativeInputManager::isScreenBright() { |
| 666 | return android_server_PowerManagerService_isScreenBright(); |
| 667 | } |
| 668 | |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 669 | void NativeInputManager::interceptKeyBeforeQueueing(const KeyEvent* keyEvent, |
| 670 | uint32_t& policyFlags) { |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 671 | // Policy: |
| 672 | // - Ignore untrusted events and pass them along. |
| 673 | // - Ask the window manager what to do with normal events and trusted injected events. |
| 674 | // - For normal events wake and brighten the screen if currently off or dim. |
| 675 | if ((policyFlags & POLICY_FLAG_TRUSTED)) { |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 676 | nsecs_t when = keyEvent->getEventTime(); |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 677 | bool isScreenOn = this->isScreenOn(); |
| 678 | bool isScreenBright = this->isScreenBright(); |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 679 | |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 680 | JNIEnv* env = jniEnv(); |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 681 | jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent); |
| 682 | jint wmActions; |
| 683 | if (keyEventObj) { |
| 684 | wmActions = env->CallIntMethod(mCallbacksObj, |
| 685 | gCallbacksClassInfo.interceptKeyBeforeQueueing, |
| 686 | keyEventObj, policyFlags, isScreenOn); |
| 687 | if (checkAndClearExceptionFromCallback(env, "interceptKeyBeforeQueueing")) { |
| 688 | wmActions = 0; |
| 689 | } |
| 690 | android_view_KeyEvent_recycle(env, keyEventObj); |
| 691 | env->DeleteLocalRef(keyEventObj); |
| 692 | } else { |
| 693 | LOGE("Failed to obtain key event object for interceptKeyBeforeQueueing."); |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 694 | wmActions = 0; |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 697 | if (!(policyFlags & POLICY_FLAG_INJECTED)) { |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 698 | if (!isScreenOn) { |
| 699 | policyFlags |= POLICY_FLAG_WOKE_HERE; |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | if (!isScreenBright) { |
| 703 | policyFlags |= POLICY_FLAG_BRIGHT_HERE; |
| 704 | } |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 707 | handleInterceptActions(wmActions, when, /*byref*/ policyFlags); |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 708 | } else { |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 709 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 710 | } |
| 711 | } |
| 712 | |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 713 | void NativeInputManager::interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) { |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 714 | // Policy: |
| 715 | // - Ignore untrusted events and pass them along. |
| 716 | // - No special filtering for injected events required at this time. |
| 717 | // - Filter normal events based on screen state. |
| 718 | // - For normal events brighten (but do not wake) the screen if currently dim. |
| 719 | if ((policyFlags & POLICY_FLAG_TRUSTED) && !(policyFlags & POLICY_FLAG_INJECTED)) { |
| 720 | if (isScreenOn()) { |
| 721 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 722 | |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 723 | if (!isScreenBright()) { |
| 724 | policyFlags |= POLICY_FLAG_BRIGHT_HERE; |
| 725 | } |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 726 | } else { |
| 727 | JNIEnv* env = jniEnv(); |
| 728 | jint wmActions = env->CallIntMethod(mCallbacksObj, |
| 729 | gCallbacksClassInfo.interceptMotionBeforeQueueingWhenScreenOff, |
| 730 | policyFlags); |
| 731 | if (checkAndClearExceptionFromCallback(env, |
| 732 | "interceptMotionBeforeQueueingWhenScreenOff")) { |
| 733 | wmActions = 0; |
| 734 | } |
| 735 | |
| 736 | policyFlags |= POLICY_FLAG_WOKE_HERE | POLICY_FLAG_BRIGHT_HERE; |
| 737 | handleInterceptActions(wmActions, when, /*byref*/ policyFlags); |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 738 | } |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 739 | } else { |
| 740 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 744 | void NativeInputManager::handleInterceptActions(jint wmActions, nsecs_t when, |
| 745 | uint32_t& policyFlags) { |
| 746 | enum { |
| 747 | WM_ACTION_PASS_TO_USER = 1, |
| 748 | WM_ACTION_POKE_USER_ACTIVITY = 2, |
| 749 | WM_ACTION_GO_TO_SLEEP = 4, |
| 750 | }; |
| 751 | |
| 752 | if (wmActions & WM_ACTION_GO_TO_SLEEP) { |
| Jeff Brown | b23bdf5 | 2011-03-07 20:11:22 -0800 | [diff] [blame] | 753 | #if DEBUG_INPUT_DISPATCHER_POLICY |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 754 | LOGD("handleInterceptActions: Going to sleep."); |
| 755 | #endif |
| 756 | android_server_PowerManagerService_goToSleep(when); |
| 757 | } |
| 758 | |
| 759 | if (wmActions & WM_ACTION_POKE_USER_ACTIVITY) { |
| Jeff Brown | b23bdf5 | 2011-03-07 20:11:22 -0800 | [diff] [blame] | 760 | #if DEBUG_INPUT_DISPATCHER_POLICY |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 761 | LOGD("handleInterceptActions: Poking user activity."); |
| 762 | #endif |
| 763 | android_server_PowerManagerService_userActivity(when, POWER_MANAGER_BUTTON_EVENT); |
| 764 | } |
| 765 | |
| 766 | if (wmActions & WM_ACTION_PASS_TO_USER) { |
| 767 | policyFlags |= POLICY_FLAG_PASS_TO_USER; |
| 768 | } else { |
| Jeff Brown | b23bdf5 | 2011-03-07 20:11:22 -0800 | [diff] [blame] | 769 | #if DEBUG_INPUT_DISPATCHER_POLICY |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 770 | LOGD("handleInterceptActions: Not passing key to user."); |
| 771 | #endif |
| 772 | } |
| 773 | } |
| 774 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 775 | bool NativeInputManager::interceptKeyBeforeDispatching( |
| 776 | const sp<InputWindowHandle>& inputWindowHandle, |
| Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 777 | const KeyEvent* keyEvent, uint32_t policyFlags) { |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 778 | // Policy: |
| 779 | // - Ignore untrusted events and pass them along. |
| 780 | // - Filter normal events and trusted injected events through the window manager policy to |
| 781 | // handle the HOME key and the like. |
| Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 782 | bool result = false; |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 783 | if (policyFlags & POLICY_FLAG_TRUSTED) { |
| 784 | JNIEnv* env = jniEnv(); |
| Jeff Brown | d009787 | 2010-06-30 14:41:59 -0700 | [diff] [blame] | 785 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 786 | // Note: inputWindowHandle may be null. |
| 787 | jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle); |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 788 | jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent); |
| 789 | if (keyEventObj) { |
| 790 | jboolean consumed = env->CallBooleanMethod(mCallbacksObj, |
| 791 | gCallbacksClassInfo.interceptKeyBeforeDispatching, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 792 | inputWindowHandleObj, keyEventObj, policyFlags); |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 793 | bool error = checkAndClearExceptionFromCallback(env, "interceptKeyBeforeDispatching"); |
| 794 | android_view_KeyEvent_recycle(env, keyEventObj); |
| 795 | env->DeleteLocalRef(keyEventObj); |
| 796 | result = consumed && !error; |
| 797 | } else { |
| 798 | LOGE("Failed to obtain key event object for interceptKeyBeforeDispatching."); |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 799 | } |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 800 | env->DeleteLocalRef(inputWindowHandleObj); |
| Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 801 | } |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 802 | return result; |
| Jeff Brown | d009787 | 2010-06-30 14:41:59 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 805 | bool NativeInputManager::dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle, |
| Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 806 | const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) { |
| Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 807 | // Policy: |
| 808 | // - Ignore untrusted events and do not perform default handling. |
| Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 809 | bool result = false; |
| Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 810 | if (policyFlags & POLICY_FLAG_TRUSTED) { |
| 811 | JNIEnv* env = jniEnv(); |
| 812 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 813 | // Note: inputWindowHandle may be null. |
| 814 | jobject inputWindowHandleObj = getInputWindowHandleObjLocalRef(env, inputWindowHandle); |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 815 | jobject keyEventObj = android_view_KeyEvent_fromNative(env, keyEvent); |
| 816 | if (keyEventObj) { |
| Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 817 | jobject fallbackKeyEventObj = env->CallObjectMethod(mCallbacksObj, |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 818 | gCallbacksClassInfo.dispatchUnhandledKey, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 819 | inputWindowHandleObj, keyEventObj, policyFlags); |
| Jeff Brown | 524ee64 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 820 | if (checkAndClearExceptionFromCallback(env, "dispatchUnhandledKey")) { |
| 821 | fallbackKeyEventObj = NULL; |
| 822 | } |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 823 | android_view_KeyEvent_recycle(env, keyEventObj); |
| 824 | env->DeleteLocalRef(keyEventObj); |
| Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 825 | |
| 826 | if (fallbackKeyEventObj) { |
| 827 | // Note: outFallbackKeyEvent may be the same object as keyEvent. |
| 828 | if (!android_view_KeyEvent_toNative(env, fallbackKeyEventObj, |
| 829 | outFallbackKeyEvent)) { |
| 830 | result = true; |
| 831 | } |
| 832 | android_view_KeyEvent_recycle(env, fallbackKeyEventObj); |
| 833 | env->DeleteLocalRef(fallbackKeyEventObj); |
| 834 | } |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 835 | } else { |
| 836 | LOGE("Failed to obtain key event object for dispatchUnhandledKey."); |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 837 | } |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 838 | env->DeleteLocalRef(inputWindowHandleObj); |
| Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 839 | } |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 840 | return result; |
| Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 843 | void NativeInputManager::pokeUserActivity(nsecs_t eventTime, int32_t eventType) { |
| 844 | android_server_PowerManagerService_userActivity(eventTime, eventType); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 847 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 848 | bool NativeInputManager::checkInjectEventsPermissionNonReentrant( |
| 849 | int32_t injectorPid, int32_t injectorUid) { |
| 850 | JNIEnv* env = jniEnv(); |
| 851 | jboolean result = env->CallBooleanMethod(mCallbacksObj, |
| 852 | gCallbacksClassInfo.checkInjectEventsPermission, injectorPid, injectorUid); |
| Jeff Brown | 524ee64 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 853 | if (checkAndClearExceptionFromCallback(env, "checkInjectEventsPermission")) { |
| 854 | result = false; |
| 855 | } |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 856 | return result; |
| 857 | } |
| 858 | |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 859 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 860 | // ---------------------------------------------------------------------------- |
| 861 | |
| 862 | static sp<NativeInputManager> gNativeInputManager; |
| 863 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 864 | static bool checkInputManagerUnitialized(JNIEnv* env) { |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 865 | if (gNativeInputManager == NULL) { |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 866 | LOGE("Input manager not initialized."); |
| 867 | jniThrowRuntimeException(env, "Input manager not initialized."); |
| 868 | return true; |
| 869 | } |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | static void android_server_InputManager_nativeInit(JNIEnv* env, jclass clazz, |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 874 | jobject callbacks, jobject messageQueueObj) { |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 875 | if (gNativeInputManager == NULL) { |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 876 | sp<Looper> looper = android_os_MessageQueue_getLooper(env, messageQueueObj); |
| 877 | gNativeInputManager = new NativeInputManager(callbacks, looper); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 878 | } else { |
| 879 | LOGE("Input manager already initialized."); |
| 880 | jniThrowRuntimeException(env, "Input manager already initialized."); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
| 884 | static void android_server_InputManager_nativeStart(JNIEnv* env, jclass clazz) { |
| 885 | if (checkInputManagerUnitialized(env)) { |
| 886 | return; |
| 887 | } |
| 888 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 889 | status_t result = gNativeInputManager->getInputManager()->start(); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 890 | if (result) { |
| 891 | jniThrowRuntimeException(env, "Input manager could not be started."); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | static void android_server_InputManager_nativeSetDisplaySize(JNIEnv* env, jclass clazz, |
| 896 | jint displayId, jint width, jint height) { |
| 897 | if (checkInputManagerUnitialized(env)) { |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | // XXX we could get this from the SurfaceFlinger directly instead of requiring it |
| 902 | // to be passed in like this, not sure which is better but leaving it like this |
| 903 | // keeps the window manager in direct control of when display transitions propagate down |
| 904 | // to the input dispatcher |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 905 | gNativeInputManager->setDisplaySize(displayId, width, height); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | static void android_server_InputManager_nativeSetDisplayOrientation(JNIEnv* env, jclass clazz, |
| 909 | jint displayId, jint orientation) { |
| 910 | if (checkInputManagerUnitialized(env)) { |
| 911 | return; |
| 912 | } |
| 913 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 914 | gNativeInputManager->setDisplayOrientation(displayId, orientation); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | static jint android_server_InputManager_nativeGetScanCodeState(JNIEnv* env, jclass clazz, |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 918 | jint deviceId, jint sourceMask, jint scanCode) { |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 919 | if (checkInputManagerUnitialized(env)) { |
| Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 920 | return AKEY_STATE_UNKNOWN; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 923 | return gNativeInputManager->getInputManager()->getReader()->getScanCodeState( |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 924 | deviceId, uint32_t(sourceMask), scanCode); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | static jint android_server_InputManager_nativeGetKeyCodeState(JNIEnv* env, jclass clazz, |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 928 | jint deviceId, jint sourceMask, jint keyCode) { |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 929 | if (checkInputManagerUnitialized(env)) { |
| Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 930 | return AKEY_STATE_UNKNOWN; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 931 | } |
| 932 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 933 | return gNativeInputManager->getInputManager()->getReader()->getKeyCodeState( |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 934 | deviceId, uint32_t(sourceMask), keyCode); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static jint android_server_InputManager_nativeGetSwitchState(JNIEnv* env, jclass clazz, |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 938 | jint deviceId, jint sourceMask, jint sw) { |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 939 | if (checkInputManagerUnitialized(env)) { |
| Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 940 | return AKEY_STATE_UNKNOWN; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 943 | return gNativeInputManager->getInputManager()->getReader()->getSwitchState( |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 944 | deviceId, uint32_t(sourceMask), sw); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | static jboolean android_server_InputManager_nativeHasKeys(JNIEnv* env, jclass clazz, |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 948 | jint deviceId, jint sourceMask, jintArray keyCodes, jbooleanArray outFlags) { |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 949 | if (checkInputManagerUnitialized(env)) { |
| 950 | return JNI_FALSE; |
| 951 | } |
| 952 | |
| 953 | int32_t* codes = env->GetIntArrayElements(keyCodes, NULL); |
| 954 | uint8_t* flags = env->GetBooleanArrayElements(outFlags, NULL); |
| 955 | jsize numCodes = env->GetArrayLength(keyCodes); |
| 956 | jboolean result; |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 957 | if (numCodes == env->GetArrayLength(keyCodes)) { |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 958 | result = gNativeInputManager->getInputManager()->getReader()->hasKeys( |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 959 | deviceId, uint32_t(sourceMask), numCodes, codes, flags); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 960 | } else { |
| 961 | result = JNI_FALSE; |
| 962 | } |
| 963 | |
| 964 | env->ReleaseBooleanArrayElements(outFlags, flags, 0); |
| 965 | env->ReleaseIntArrayElements(keyCodes, codes, 0); |
| 966 | return result; |
| 967 | } |
| 968 | |
| 969 | static void throwInputChannelNotInitialized(JNIEnv* env) { |
| 970 | jniThrowException(env, "java/lang/IllegalStateException", |
| 971 | "inputChannel is not initialized"); |
| 972 | } |
| 973 | |
| 974 | static void android_server_InputManager_handleInputChannelDisposed(JNIEnv* env, |
| 975 | jobject inputChannelObj, const sp<InputChannel>& inputChannel, void* data) { |
| 976 | LOGW("Input channel object '%s' was disposed without first being unregistered with " |
| 977 | "the input manager!", inputChannel->getName().string()); |
| 978 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 979 | if (gNativeInputManager != NULL) { |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 980 | gNativeInputManager->unregisterInputChannel(env, inputChannel); |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 981 | } |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static void android_server_InputManager_nativeRegisterInputChannel(JNIEnv* env, jclass clazz, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 985 | jobject inputChannelObj, jobject inputWindowHandleObj, jboolean monitor) { |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 986 | if (checkInputManagerUnitialized(env)) { |
| 987 | return; |
| 988 | } |
| 989 | |
| 990 | sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env, |
| 991 | inputChannelObj); |
| 992 | if (inputChannel == NULL) { |
| 993 | throwInputChannelNotInitialized(env); |
| 994 | return; |
| 995 | } |
| 996 | |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 997 | sp<InputWindowHandle> inputWindowHandle = |
| 998 | android_server_InputWindowHandle_getHandle(env, inputWindowHandleObj); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 999 | |
| 1000 | status_t status = gNativeInputManager->registerInputChannel( |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1001 | env, inputChannel, inputWindowHandle, monitor); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1002 | if (status) { |
| 1003 | jniThrowRuntimeException(env, "Failed to register input channel. " |
| 1004 | "Check logs for details."); |
| 1005 | return; |
| 1006 | } |
| 1007 | |
| Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 1008 | if (! monitor) { |
| 1009 | android_view_InputChannel_setDisposeCallback(env, inputChannelObj, |
| 1010 | android_server_InputManager_handleInputChannelDisposed, NULL); |
| 1011 | } |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static void android_server_InputManager_nativeUnregisterInputChannel(JNIEnv* env, jclass clazz, |
| 1015 | jobject inputChannelObj) { |
| 1016 | if (checkInputManagerUnitialized(env)) { |
| 1017 | return; |
| 1018 | } |
| 1019 | |
| 1020 | sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env, |
| 1021 | inputChannelObj); |
| 1022 | if (inputChannel == NULL) { |
| 1023 | throwInputChannelNotInitialized(env); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | android_view_InputChannel_setDisposeCallback(env, inputChannelObj, NULL, NULL); |
| 1028 | |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1029 | status_t status = gNativeInputManager->unregisterInputChannel(env, inputChannel); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1030 | if (status) { |
| 1031 | jniThrowRuntimeException(env, "Failed to unregister input channel. " |
| 1032 | "Check logs for details."); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1036 | static jint android_server_InputManager_nativeInjectInputEvent(JNIEnv* env, jclass clazz, |
| 1037 | jobject inputEventObj, jint injectorPid, jint injectorUid, |
| 1038 | jint syncMode, jint timeoutMillis) { |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1039 | if (checkInputManagerUnitialized(env)) { |
| 1040 | return INPUT_EVENT_INJECTION_FAILED; |
| 1041 | } |
| 1042 | |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1043 | if (env->IsInstanceOf(inputEventObj, gKeyEventClassInfo.clazz)) { |
| 1044 | KeyEvent keyEvent; |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 1045 | status_t status = android_view_KeyEvent_toNative(env, inputEventObj, & keyEvent); |
| 1046 | if (status) { |
| 1047 | jniThrowRuntimeException(env, "Could not read contents of KeyEvent object."); |
| 1048 | return INPUT_EVENT_INJECTION_FAILED; |
| 1049 | } |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1050 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1051 | return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent( |
| 1052 | & keyEvent, injectorPid, injectorUid, syncMode, timeoutMillis); |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1053 | } else if (env->IsInstanceOf(inputEventObj, gMotionEventClassInfo.clazz)) { |
| Jeff Brown | 5b2b4d9 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 1054 | const MotionEvent* motionEvent = android_view_MotionEvent_getNativePtr(env, inputEventObj); |
| 1055 | if (!motionEvent) { |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 1056 | jniThrowRuntimeException(env, "Could not read contents of MotionEvent object."); |
| 1057 | return INPUT_EVENT_INJECTION_FAILED; |
| 1058 | } |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1059 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1060 | return gNativeInputManager->getInputManager()->getDispatcher()->injectInputEvent( |
| Jeff Brown | 5b2b4d9 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 1061 | motionEvent, injectorPid, injectorUid, syncMode, timeoutMillis); |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1062 | } else { |
| 1063 | jniThrowRuntimeException(env, "Invalid input event type."); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1064 | return INPUT_EVENT_INJECTION_FAILED; |
| 1065 | } |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1068 | static void android_server_InputManager_nativeSetInputWindows(JNIEnv* env, jclass clazz, |
| 1069 | jobjectArray windowObjArray) { |
| 1070 | if (checkInputManagerUnitialized(env)) { |
| 1071 | return; |
| 1072 | } |
| 1073 | |
| 1074 | gNativeInputManager->setInputWindows(env, windowObjArray); |
| 1075 | } |
| 1076 | |
| 1077 | static void android_server_InputManager_nativeSetFocusedApplication(JNIEnv* env, jclass clazz, |
| 1078 | jobject applicationObj) { |
| 1079 | if (checkInputManagerUnitialized(env)) { |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | gNativeInputManager->setFocusedApplication(env, applicationObj); |
| 1084 | } |
| 1085 | |
| 1086 | static void android_server_InputManager_nativeSetInputDispatchMode(JNIEnv* env, |
| 1087 | jclass clazz, jboolean enabled, jboolean frozen) { |
| 1088 | if (checkInputManagerUnitialized(env)) { |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | gNativeInputManager->setInputDispatchMode(enabled, frozen); |
| 1093 | } |
| 1094 | |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1095 | static void android_server_InputManager_nativeSetSystemUiVisibility(JNIEnv* env, |
| 1096 | jclass clazz, jint visibility) { |
| 1097 | if (checkInputManagerUnitialized(env)) { |
| 1098 | return; |
| 1099 | } |
| 1100 | |
| 1101 | gNativeInputManager->setSystemUiVisibility(visibility); |
| 1102 | } |
| 1103 | |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1104 | static jobject android_server_InputManager_nativeGetInputDevice(JNIEnv* env, |
| 1105 | jclass clazz, jint deviceId) { |
| 1106 | if (checkInputManagerUnitialized(env)) { |
| 1107 | return NULL; |
| 1108 | } |
| 1109 | |
| 1110 | InputDeviceInfo deviceInfo; |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1111 | status_t status = gNativeInputManager->getInputManager()->getReader()->getInputDeviceInfo( |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1112 | deviceId, & deviceInfo); |
| 1113 | if (status) { |
| 1114 | return NULL; |
| 1115 | } |
| 1116 | |
| 1117 | jobject deviceObj = env->NewObject(gInputDeviceClassInfo.clazz, gInputDeviceClassInfo.ctor); |
| 1118 | if (! deviceObj) { |
| 1119 | return NULL; |
| 1120 | } |
| 1121 | |
| 1122 | jstring deviceNameObj = env->NewStringUTF(deviceInfo.getName().string()); |
| 1123 | if (! deviceNameObj) { |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | |
| 1127 | env->SetIntField(deviceObj, gInputDeviceClassInfo.mId, deviceInfo.getId()); |
| 1128 | env->SetObjectField(deviceObj, gInputDeviceClassInfo.mName, deviceNameObj); |
| 1129 | env->SetIntField(deviceObj, gInputDeviceClassInfo.mSources, deviceInfo.getSources()); |
| 1130 | env->SetIntField(deviceObj, gInputDeviceClassInfo.mKeyboardType, deviceInfo.getKeyboardType()); |
| 1131 | |
| Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1132 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1133 | for (size_t i = 0; i < ranges.size(); i++) { |
| Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1134 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1135 | env->CallVoidMethod(deviceObj, gInputDeviceClassInfo.addMotionRange, |
| Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1136 | range.axis, range.source, range.min, range.max, range.flat, range.fuzz); |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1137 | if (env->ExceptionCheck()) { |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | return deviceObj; |
| 1143 | } |
| 1144 | |
| 1145 | static jintArray android_server_InputManager_nativeGetInputDeviceIds(JNIEnv* env, |
| 1146 | jclass clazz) { |
| 1147 | if (checkInputManagerUnitialized(env)) { |
| 1148 | return NULL; |
| 1149 | } |
| 1150 | |
| 1151 | Vector<int> deviceIds; |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1152 | gNativeInputManager->getInputManager()->getReader()->getInputDeviceIds(deviceIds); |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1153 | |
| 1154 | jintArray deviceIdsObj = env->NewIntArray(deviceIds.size()); |
| 1155 | if (! deviceIdsObj) { |
| 1156 | return NULL; |
| 1157 | } |
| 1158 | |
| 1159 | env->SetIntArrayRegion(deviceIdsObj, 0, deviceIds.size(), deviceIds.array()); |
| 1160 | return deviceIdsObj; |
| 1161 | } |
| 1162 | |
| Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 1163 | static void android_server_InputManager_nativeGetInputConfiguration(JNIEnv* env, |
| 1164 | jclass clazz, jobject configObj) { |
| 1165 | if (checkInputManagerUnitialized(env)) { |
| 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | InputConfiguration config; |
| 1170 | gNativeInputManager->getInputManager()->getReader()->getInputConfiguration(& config); |
| 1171 | |
| 1172 | env->SetIntField(configObj, gConfigurationClassInfo.touchscreen, config.touchScreen); |
| 1173 | env->SetIntField(configObj, gConfigurationClassInfo.keyboard, config.keyboard); |
| 1174 | env->SetIntField(configObj, gConfigurationClassInfo.navigation, config.navigation); |
| 1175 | } |
| 1176 | |
| Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 1177 | static jboolean android_server_InputManager_nativeTransferTouchFocus(JNIEnv* env, |
| 1178 | jclass clazz, jobject fromChannelObj, jobject toChannelObj) { |
| 1179 | if (checkInputManagerUnitialized(env)) { |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
| 1183 | sp<InputChannel> fromChannel = |
| 1184 | android_view_InputChannel_getInputChannel(env, fromChannelObj); |
| 1185 | sp<InputChannel> toChannel = |
| 1186 | android_view_InputChannel_getInputChannel(env, toChannelObj); |
| 1187 | |
| 1188 | if (fromChannel == NULL || toChannel == NULL) { |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
| 1192 | return gNativeInputManager->getInputManager()->getDispatcher()-> |
| 1193 | transferTouchFocus(fromChannel, toChannel); |
| 1194 | } |
| 1195 | |
| Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 1196 | static jstring android_server_InputManager_nativeDump(JNIEnv* env, jclass clazz) { |
| 1197 | if (checkInputManagerUnitialized(env)) { |
| 1198 | return NULL; |
| 1199 | } |
| 1200 | |
| Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1201 | String8 dump; |
| 1202 | gNativeInputManager->dump(dump); |
| Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 1203 | return env->NewStringUTF(dump.string()); |
| 1204 | } |
| 1205 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1206 | // ---------------------------------------------------------------------------- |
| 1207 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1208 | static JNINativeMethod gInputManagerMethods[] = { |
| 1209 | /* name, signature, funcPtr */ |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1210 | { "nativeInit", "(Lcom/android/server/wm/InputManager$Callbacks;Landroid/os/MessageQueue;)V", |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1211 | (void*) android_server_InputManager_nativeInit }, |
| 1212 | { "nativeStart", "()V", |
| 1213 | (void*) android_server_InputManager_nativeStart }, |
| 1214 | { "nativeSetDisplaySize", "(III)V", |
| 1215 | (void*) android_server_InputManager_nativeSetDisplaySize }, |
| 1216 | { "nativeSetDisplayOrientation", "(II)V", |
| 1217 | (void*) android_server_InputManager_nativeSetDisplayOrientation }, |
| 1218 | { "nativeGetScanCodeState", "(III)I", |
| 1219 | (void*) android_server_InputManager_nativeGetScanCodeState }, |
| 1220 | { "nativeGetKeyCodeState", "(III)I", |
| 1221 | (void*) android_server_InputManager_nativeGetKeyCodeState }, |
| 1222 | { "nativeGetSwitchState", "(III)I", |
| 1223 | (void*) android_server_InputManager_nativeGetSwitchState }, |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1224 | { "nativeHasKeys", "(II[I[Z)Z", |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1225 | (void*) android_server_InputManager_nativeHasKeys }, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1226 | { "nativeRegisterInputChannel", |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1227 | "(Landroid/view/InputChannel;Lcom/android/server/wm/InputWindowHandle;Z)V", |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1228 | (void*) android_server_InputManager_nativeRegisterInputChannel }, |
| 1229 | { "nativeUnregisterInputChannel", "(Landroid/view/InputChannel;)V", |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1230 | (void*) android_server_InputManager_nativeUnregisterInputChannel }, |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1231 | { "nativeInjectInputEvent", "(Landroid/view/InputEvent;IIII)I", |
| 1232 | (void*) android_server_InputManager_nativeInjectInputEvent }, |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1233 | { "nativeSetInputWindows", "([Lcom/android/server/wm/InputWindow;)V", |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1234 | (void*) android_server_InputManager_nativeSetInputWindows }, |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1235 | { "nativeSetFocusedApplication", "(Lcom/android/server/wm/InputApplication;)V", |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1236 | (void*) android_server_InputManager_nativeSetFocusedApplication }, |
| 1237 | { "nativeSetInputDispatchMode", "(ZZ)V", |
| 1238 | (void*) android_server_InputManager_nativeSetInputDispatchMode }, |
| Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1239 | { "nativeSetSystemUiVisibility", "(I)V", |
| 1240 | (void*) android_server_InputManager_nativeSetSystemUiVisibility }, |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1241 | { "nativeGetInputDevice", "(I)Landroid/view/InputDevice;", |
| 1242 | (void*) android_server_InputManager_nativeGetInputDevice }, |
| 1243 | { "nativeGetInputDeviceIds", "()[I", |
| 1244 | (void*) android_server_InputManager_nativeGetInputDeviceIds }, |
| Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 1245 | { "nativeGetInputConfiguration", "(Landroid/content/res/Configuration;)V", |
| 1246 | (void*) android_server_InputManager_nativeGetInputConfiguration }, |
| Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 1247 | { "nativeTransferTouchFocus", "(Landroid/view/InputChannel;Landroid/view/InputChannel;)Z", |
| 1248 | (void*) android_server_InputManager_nativeTransferTouchFocus }, |
| Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 1249 | { "nativeDump", "()Ljava/lang/String;", |
| 1250 | (void*) android_server_InputManager_nativeDump }, |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1251 | }; |
| 1252 | |
| 1253 | #define FIND_CLASS(var, className) \ |
| 1254 | var = env->FindClass(className); \ |
| 1255 | LOG_FATAL_IF(! var, "Unable to find class " className); \ |
| 1256 | var = jclass(env->NewGlobalRef(var)); |
| 1257 | |
| 1258 | #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 1259 | var = env->GetMethodID(clazz, methodName, methodDescriptor); \ |
| 1260 | LOG_FATAL_IF(! var, "Unable to find method " methodName); |
| 1261 | |
| 1262 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 1263 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 1264 | LOG_FATAL_IF(! var, "Unable to find field " fieldName); |
| 1265 | |
| 1266 | int register_android_server_InputManager(JNIEnv* env) { |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1267 | int res = jniRegisterNativeMethods(env, "com/android/server/wm/InputManager", |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1268 | gInputManagerMethods, NELEM(gInputManagerMethods)); |
| 1269 | LOG_FATAL_IF(res < 0, "Unable to register native methods."); |
| 1270 | |
| Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1271 | // Callbacks |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1272 | |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1273 | FIND_CLASS(gCallbacksClassInfo.clazz, "com/android/server/wm/InputManager$Callbacks"); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1274 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1275 | GET_METHOD_ID(gCallbacksClassInfo.notifyConfigurationChanged, gCallbacksClassInfo.clazz, |
| Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 1276 | "notifyConfigurationChanged", "(J)V"); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1277 | |
| 1278 | GET_METHOD_ID(gCallbacksClassInfo.notifyLidSwitchChanged, gCallbacksClassInfo.clazz, |
| 1279 | "notifyLidSwitchChanged", "(JZ)V"); |
| 1280 | |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1281 | GET_METHOD_ID(gCallbacksClassInfo.notifyInputChannelBroken, gCallbacksClassInfo.clazz, |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1282 | "notifyInputChannelBroken", "(Lcom/android/server/wm/InputWindowHandle;)V"); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1283 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1284 | GET_METHOD_ID(gCallbacksClassInfo.notifyANR, gCallbacksClassInfo.clazz, |
| Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1285 | "notifyANR", |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1286 | "(Lcom/android/server/wm/InputApplicationHandle;Lcom/android/server/wm/InputWindowHandle;)J"); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1287 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1288 | GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeQueueing, gCallbacksClassInfo.clazz, |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 1289 | "interceptKeyBeforeQueueing", "(Landroid/view/KeyEvent;IZ)I"); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1290 | |
| Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1291 | GET_METHOD_ID(gCallbacksClassInfo.interceptMotionBeforeQueueingWhenScreenOff, |
| 1292 | gCallbacksClassInfo.clazz, |
| 1293 | "interceptMotionBeforeQueueingWhenScreenOff", "(I)I"); |
| 1294 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1295 | GET_METHOD_ID(gCallbacksClassInfo.interceptKeyBeforeDispatching, gCallbacksClassInfo.clazz, |
| Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 1296 | "interceptKeyBeforeDispatching", |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1297 | "(Lcom/android/server/wm/InputWindowHandle;Landroid/view/KeyEvent;I)Z"); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1298 | |
| Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 1299 | GET_METHOD_ID(gCallbacksClassInfo.dispatchUnhandledKey, gCallbacksClassInfo.clazz, |
| Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1300 | "dispatchUnhandledKey", |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1301 | "(Lcom/android/server/wm/InputWindowHandle;Landroid/view/KeyEvent;I)Landroid/view/KeyEvent;"); |
| Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 1302 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1303 | GET_METHOD_ID(gCallbacksClassInfo.checkInjectEventsPermission, gCallbacksClassInfo.clazz, |
| 1304 | "checkInjectEventsPermission", "(II)Z"); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1305 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1306 | GET_METHOD_ID(gCallbacksClassInfo.filterTouchEvents, gCallbacksClassInfo.clazz, |
| 1307 | "filterTouchEvents", "()Z"); |
| 1308 | |
| 1309 | GET_METHOD_ID(gCallbacksClassInfo.filterJumpyTouchEvents, gCallbacksClassInfo.clazz, |
| 1310 | "filterJumpyTouchEvents", "()Z"); |
| 1311 | |
| Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1312 | GET_METHOD_ID(gCallbacksClassInfo.getVirtualKeyQuietTimeMillis, gCallbacksClassInfo.clazz, |
| 1313 | "getVirtualKeyQuietTimeMillis", "()I"); |
| 1314 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1315 | GET_METHOD_ID(gCallbacksClassInfo.getExcludedDeviceNames, gCallbacksClassInfo.clazz, |
| 1316 | "getExcludedDeviceNames", "()[Ljava/lang/String;"); |
| 1317 | |
| Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 1318 | GET_METHOD_ID(gCallbacksClassInfo.getKeyRepeatTimeout, gCallbacksClassInfo.clazz, |
| 1319 | "getKeyRepeatTimeout", "()I"); |
| 1320 | |
| 1321 | GET_METHOD_ID(gCallbacksClassInfo.getKeyRepeatDelay, gCallbacksClassInfo.clazz, |
| 1322 | "getKeyRepeatDelay", "()I"); |
| 1323 | |
| Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 1324 | GET_METHOD_ID(gCallbacksClassInfo.getMaxEventsPerSecond, gCallbacksClassInfo.clazz, |
| 1325 | "getMaxEventsPerSecond", "()I"); |
| 1326 | |
| Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1327 | GET_METHOD_ID(gCallbacksClassInfo.getPointerLayer, gCallbacksClassInfo.clazz, |
| 1328 | "getPointerLayer", "()I"); |
| 1329 | |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1330 | GET_METHOD_ID(gCallbacksClassInfo.getPointerIcon, gCallbacksClassInfo.clazz, |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1331 | "getPointerIcon", "()Lcom/android/server/wm/InputManager$PointerIcon;"); |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1332 | |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1333 | // KeyEvent |
| 1334 | |
| 1335 | FIND_CLASS(gKeyEventClassInfo.clazz, "android/view/KeyEvent"); |
| 1336 | |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1337 | // MotionEvent |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 1338 | |
| 1339 | FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent"); |
| 1340 | |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1341 | // InputDevice |
| 1342 | |
| 1343 | FIND_CLASS(gInputDeviceClassInfo.clazz, "android/view/InputDevice"); |
| 1344 | |
| 1345 | GET_METHOD_ID(gInputDeviceClassInfo.ctor, gInputDeviceClassInfo.clazz, |
| 1346 | "<init>", "()V"); |
| 1347 | |
| 1348 | GET_METHOD_ID(gInputDeviceClassInfo.addMotionRange, gInputDeviceClassInfo.clazz, |
| Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1349 | "addMotionRange", "(IIFFFF)V"); |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1350 | |
| 1351 | GET_FIELD_ID(gInputDeviceClassInfo.mId, gInputDeviceClassInfo.clazz, |
| 1352 | "mId", "I"); |
| 1353 | |
| 1354 | GET_FIELD_ID(gInputDeviceClassInfo.mName, gInputDeviceClassInfo.clazz, |
| 1355 | "mName", "Ljava/lang/String;"); |
| 1356 | |
| 1357 | GET_FIELD_ID(gInputDeviceClassInfo.mSources, gInputDeviceClassInfo.clazz, |
| 1358 | "mSources", "I"); |
| 1359 | |
| 1360 | GET_FIELD_ID(gInputDeviceClassInfo.mKeyboardType, gInputDeviceClassInfo.clazz, |
| 1361 | "mKeyboardType", "I"); |
| 1362 | |
| Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 1363 | // Configuration |
| 1364 | |
| 1365 | FIND_CLASS(gConfigurationClassInfo.clazz, "android/content/res/Configuration"); |
| 1366 | |
| 1367 | GET_FIELD_ID(gConfigurationClassInfo.touchscreen, gConfigurationClassInfo.clazz, |
| 1368 | "touchscreen", "I"); |
| 1369 | |
| 1370 | GET_FIELD_ID(gConfigurationClassInfo.keyboard, gConfigurationClassInfo.clazz, |
| 1371 | "keyboard", "I"); |
| 1372 | |
| 1373 | GET_FIELD_ID(gConfigurationClassInfo.navigation, gConfigurationClassInfo.clazz, |
| 1374 | "navigation", "I"); |
| 1375 | |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1376 | // PointerIcon |
| 1377 | |
| Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 1378 | FIND_CLASS(gPointerIconClassInfo.clazz, "com/android/server/wm/InputManager$PointerIcon"); |
| Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1379 | |
| 1380 | GET_FIELD_ID(gPointerIconClassInfo.bitmap, gPointerIconClassInfo.clazz, |
| 1381 | "bitmap", "Landroid/graphics/Bitmap;"); |
| 1382 | |
| 1383 | GET_FIELD_ID(gPointerIconClassInfo.hotSpotX, gPointerIconClassInfo.clazz, |
| 1384 | "hotSpotX", "F"); |
| 1385 | |
| 1386 | GET_FIELD_ID(gPointerIconClassInfo.hotSpotY, gPointerIconClassInfo.clazz, |
| 1387 | "hotSpotY", "F"); |
| 1388 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1389 | return 0; |
| 1390 | } |
| 1391 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1392 | } /* namespace android */ |