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