Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "InputEventReceiver" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 21 | #include <inttypes.h> |
| 22 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 23 | #include <nativehelper/JNIHelp.h> |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 24 | |
| 25 | #include <android_runtime/AndroidRuntime.h> |
| 26 | #include <utils/Log.h> |
| 27 | #include <utils/Looper.h> |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 28 | #include <utils/Vector.h> |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 29 | #include <utils/threads.h> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 30 | #include <input/InputTransport.h> |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 31 | #include "android_os_MessageQueue.h" |
| 32 | #include "android_view_InputChannel.h" |
| 33 | #include "android_view_KeyEvent.h" |
| 34 | #include "android_view_MotionEvent.h" |
| 35 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 36 | #include <nativehelper/ScopedLocalRef.h> |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 37 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 38 | #include "core_jni_helpers.h" |
| 39 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 42 | static const bool kDebugDispatchCycle = false; |
| 43 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 44 | static struct { |
| 45 | jclass clazz; |
| 46 | |
| 47 | jmethodID dispatchInputEvent; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 48 | jmethodID dispatchBatchedInputEventPending; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 49 | } gInputEventReceiverClassInfo; |
| 50 | |
| 51 | |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 52 | class NativeInputEventReceiver : public LooperCallback { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 53 | public: |
| 54 | NativeInputEventReceiver(JNIEnv* env, |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 55 | jobject receiverWeak, const sp<InputChannel>& inputChannel, |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 56 | const sp<MessageQueue>& messageQueue); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 57 | |
| 58 | status_t initialize(); |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 59 | void dispose(); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 60 | status_t finishInputEvent(uint32_t seq, bool handled); |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 61 | status_t consumeEvents(JNIEnv* env, bool consumeBatches, nsecs_t frameTime, |
| 62 | bool* outConsumedBatch); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 63 | |
| 64 | protected: |
| 65 | virtual ~NativeInputEventReceiver(); |
| 66 | |
| 67 | private: |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 68 | struct Finish { |
| 69 | uint32_t seq; |
| 70 | bool handled; |
| 71 | }; |
| 72 | |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 73 | jobject mReceiverWeakGlobal; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 74 | InputConsumer mInputConsumer; |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 75 | sp<MessageQueue> mMessageQueue; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 76 | PreallocatedInputEventFactory mInputEventFactory; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 77 | bool mBatchedInputEventPending; |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 78 | int mFdEvents; |
| 79 | Vector<Finish> mFinishQueue; |
| 80 | |
| 81 | void setFdEvents(int events); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 82 | |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 83 | const std::string getInputChannelName() { |
| 84 | return mInputConsumer.getChannel()->getName(); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 85 | } |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 86 | |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 87 | virtual int handleEvent(int receiveFd, int events, void* data); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | |
| 91 | NativeInputEventReceiver::NativeInputEventReceiver(JNIEnv* env, |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 92 | jobject receiverWeak, const sp<InputChannel>& inputChannel, |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 93 | const sp<MessageQueue>& messageQueue) : |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 94 | mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)), |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 95 | mInputConsumer(inputChannel), mMessageQueue(messageQueue), |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 96 | mBatchedInputEventPending(false), mFdEvents(0) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 97 | if (kDebugDispatchCycle) { |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 98 | ALOGD("channel '%s' ~ Initializing input event receiver.", getInputChannelName().c_str()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 99 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | NativeInputEventReceiver::~NativeInputEventReceiver() { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 103 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 104 | env->DeleteGlobalRef(mReceiverWeakGlobal); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | status_t NativeInputEventReceiver::initialize() { |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 108 | setFdEvents(ALOOPER_EVENT_INPUT); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 109 | return OK; |
| 110 | } |
| 111 | |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 112 | void NativeInputEventReceiver::dispose() { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 113 | if (kDebugDispatchCycle) { |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 114 | ALOGD("channel '%s' ~ Disposing input event receiver.", getInputChannelName().c_str()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 115 | } |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 116 | |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 117 | setFdEvents(0); |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 120 | status_t NativeInputEventReceiver::finishInputEvent(uint32_t seq, bool handled) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 121 | if (kDebugDispatchCycle) { |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 122 | ALOGD("channel '%s' ~ Finished input event.", getInputChannelName().c_str()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 123 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 124 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 125 | status_t status = mInputConsumer.sendFinishedSignal(seq, handled); |
| 126 | if (status) { |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 127 | if (status == WOULD_BLOCK) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 128 | if (kDebugDispatchCycle) { |
| 129 | ALOGD("channel '%s' ~ Could not send finished signal immediately. " |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 130 | "Enqueued for later.", getInputChannelName().c_str()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 131 | } |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 132 | Finish finish; |
| 133 | finish.seq = seq; |
| 134 | finish.handled = handled; |
| 135 | mFinishQueue.add(finish); |
| 136 | if (mFinishQueue.size() == 1) { |
| 137 | setFdEvents(ALOOPER_EVENT_INPUT | ALOOPER_EVENT_OUTPUT); |
| 138 | } |
| 139 | return OK; |
| 140 | } |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 141 | ALOGW("Failed to send finished signal on channel '%s'. status=%d", |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 142 | getInputChannelName().c_str(), status); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 143 | } |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 144 | return status; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 147 | void NativeInputEventReceiver::setFdEvents(int events) { |
| 148 | if (mFdEvents != events) { |
| 149 | mFdEvents = events; |
| 150 | int fd = mInputConsumer.getChannel()->getFd(); |
| 151 | if (events) { |
| 152 | mMessageQueue->getLooper()->addFd(fd, 0, events, this, NULL); |
| 153 | } else { |
| 154 | mMessageQueue->getLooper()->removeFd(fd); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 159 | int NativeInputEventReceiver::handleEvent(int receiveFd, int events, void* data) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 160 | if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) { |
Jeff Brown | 44e13ef | 2013-03-27 12:34:30 -0700 | [diff] [blame] | 161 | // This error typically occurs when the publisher has closed the input channel |
| 162 | // as part of removing a window or finishing an IME session, in which case |
| 163 | // the consumer will soon be disposed as well. |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 164 | if (kDebugDispatchCycle) { |
| 165 | ALOGD("channel '%s' ~ Publisher closed input channel or an error occurred. " |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 166 | "events=0x%x", getInputChannelName().c_str(), events); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 167 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 168 | return 0; // remove the callback |
| 169 | } |
| 170 | |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 171 | if (events & ALOOPER_EVENT_INPUT) { |
| 172 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 173 | status_t status = consumeEvents(env, false /*consumeBatches*/, -1, NULL); |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 174 | mMessageQueue->raiseAndClearException(env, "handleReceiveCallback"); |
| 175 | return status == OK || status == NO_MEMORY ? 1 : 0; |
| 176 | } |
| 177 | |
| 178 | if (events & ALOOPER_EVENT_OUTPUT) { |
| 179 | for (size_t i = 0; i < mFinishQueue.size(); i++) { |
| 180 | const Finish& finish = mFinishQueue.itemAt(i); |
| 181 | status_t status = mInputConsumer.sendFinishedSignal(finish.seq, finish.handled); |
| 182 | if (status) { |
| 183 | mFinishQueue.removeItemsAt(0, i); |
| 184 | |
| 185 | if (status == WOULD_BLOCK) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 186 | if (kDebugDispatchCycle) { |
Bernhard Rosenkränzer | 46c82b4 | 2014-11-30 11:04:10 +0100 | [diff] [blame] | 187 | ALOGD("channel '%s' ~ Sent %zu queued finish events; %zu left.", |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 188 | getInputChannelName().c_str(), i, mFinishQueue.size()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 189 | } |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 190 | return 1; // keep the callback, try again later |
| 191 | } |
| 192 | |
| 193 | ALOGW("Failed to send finished signal on channel '%s'. status=%d", |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 194 | getInputChannelName().c_str(), status); |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 195 | if (status != DEAD_OBJECT) { |
| 196 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 197 | String8 message; |
| 198 | message.appendFormat("Failed to finish input event. status=%d", status); |
| 199 | jniThrowRuntimeException(env, message.string()); |
| 200 | mMessageQueue->raiseAndClearException(env, "finishInputEvent"); |
| 201 | } |
| 202 | return 0; // remove the callback |
| 203 | } |
| 204 | } |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 205 | if (kDebugDispatchCycle) { |
Bernhard Rosenkränzer | 46c82b4 | 2014-11-30 11:04:10 +0100 | [diff] [blame] | 206 | ALOGD("channel '%s' ~ Sent %zu queued finish events; none left.", |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 207 | getInputChannelName().c_str(), mFinishQueue.size()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 208 | } |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 209 | mFinishQueue.clear(); |
| 210 | setFdEvents(ALOOPER_EVENT_INPUT); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 211 | return 1; |
| 212 | } |
| 213 | |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 214 | ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 215 | "events=0x%x", getInputChannelName().c_str(), events); |
Jeff Brown | 5651349 | 2013-05-21 20:37:51 -0700 | [diff] [blame] | 216 | return 1; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 217 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 218 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 219 | status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env, |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 220 | bool consumeBatches, nsecs_t frameTime, bool* outConsumedBatch) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 221 | if (kDebugDispatchCycle) { |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 222 | ALOGD("channel '%s' ~ Consuming input events, consumeBatches=%s, frameTime=%" PRId64, |
| 223 | getInputChannelName().c_str(), |
| 224 | consumeBatches ? "true" : "false", frameTime); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 225 | } |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 226 | |
| 227 | if (consumeBatches) { |
| 228 | mBatchedInputEventPending = false; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 229 | } |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 230 | if (outConsumedBatch) { |
| 231 | *outConsumedBatch = false; |
| 232 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 233 | |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 234 | ScopedLocalRef<jobject> receiverObj(env, NULL); |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 235 | bool skipCallbacks = false; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 236 | for (;;) { |
| 237 | uint32_t seq; |
| 238 | InputEvent* inputEvent; |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 239 | int32_t displayId; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 240 | status_t status = mInputConsumer.consume(&mInputEventFactory, |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 241 | consumeBatches, frameTime, &seq, &inputEvent, &displayId); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 242 | if (status) { |
| 243 | if (status == WOULD_BLOCK) { |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 244 | if (!skipCallbacks && !mBatchedInputEventPending |
| 245 | && mInputConsumer.hasPendingBatch()) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 246 | // There is a pending batch. Come back later. |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 247 | if (!receiverObj.get()) { |
| 248 | receiverObj.reset(jniGetReferent(env, mReceiverWeakGlobal)); |
| 249 | if (!receiverObj.get()) { |
| 250 | ALOGW("channel '%s' ~ Receiver object was finalized " |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 251 | "without being disposed.", getInputChannelName().c_str()); |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 252 | return DEAD_OBJECT; |
| 253 | } |
| 254 | } |
| 255 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 256 | mBatchedInputEventPending = true; |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 257 | if (kDebugDispatchCycle) { |
| 258 | ALOGD("channel '%s' ~ Dispatching batched input event pending notification.", |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 259 | getInputChannelName().c_str()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 260 | } |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 261 | env->CallVoidMethod(receiverObj.get(), |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 262 | gInputEventReceiverClassInfo.dispatchBatchedInputEventPending); |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 263 | if (env->ExceptionCheck()) { |
| 264 | ALOGE("Exception dispatching batched input events."); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 265 | mBatchedInputEventPending = false; // try again later |
| 266 | } |
| 267 | } |
| 268 | return OK; |
| 269 | } |
| 270 | ALOGE("channel '%s' ~ Failed to consume input event. status=%d", |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 271 | getInputChannelName().c_str(), status); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 272 | return status; |
| 273 | } |
| 274 | assert(inputEvent); |
| 275 | |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 276 | if (!skipCallbacks) { |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 277 | if (!receiverObj.get()) { |
| 278 | receiverObj.reset(jniGetReferent(env, mReceiverWeakGlobal)); |
| 279 | if (!receiverObj.get()) { |
| 280 | ALOGW("channel '%s' ~ Receiver object was finalized " |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 281 | "without being disposed.", getInputChannelName().c_str()); |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 282 | return DEAD_OBJECT; |
| 283 | } |
| 284 | } |
| 285 | |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 286 | jobject inputEventObj; |
| 287 | switch (inputEvent->getType()) { |
| 288 | case AINPUT_EVENT_TYPE_KEY: |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 289 | if (kDebugDispatchCycle) { |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 290 | ALOGD("channel '%s' ~ Received key event.", getInputChannelName().c_str()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 291 | } |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 292 | inputEventObj = android_view_KeyEvent_fromNative(env, |
| 293 | static_cast<KeyEvent*>(inputEvent)); |
| 294 | break; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 295 | |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 296 | case AINPUT_EVENT_TYPE_MOTION: { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 297 | if (kDebugDispatchCycle) { |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 298 | ALOGD("channel '%s' ~ Received motion event.", getInputChannelName().c_str()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 299 | } |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 300 | MotionEvent* motionEvent = static_cast<MotionEvent*>(inputEvent); |
| 301 | if ((motionEvent->getAction() & AMOTION_EVENT_ACTION_MOVE) && outConsumedBatch) { |
| 302 | *outConsumedBatch = true; |
| 303 | } |
| 304 | inputEventObj = android_view_MotionEvent_obtainAsCopy(env, motionEvent); |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 305 | break; |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 306 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 307 | |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 308 | default: |
| 309 | assert(false); // InputConsumer should prevent this from ever happening |
| 310 | inputEventObj = NULL; |
| 311 | } |
| 312 | |
| 313 | if (inputEventObj) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 314 | if (kDebugDispatchCycle) { |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 315 | ALOGD("channel '%s' ~ Dispatching input event.", getInputChannelName().c_str()); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 316 | } |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 317 | env->CallVoidMethod(receiverObj.get(), |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 318 | gInputEventReceiverClassInfo.dispatchInputEvent, seq, inputEventObj, |
| 319 | displayId); |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 320 | if (env->ExceptionCheck()) { |
| 321 | ALOGE("Exception dispatching input event."); |
| 322 | skipCallbacks = true; |
| 323 | } |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 324 | env->DeleteLocalRef(inputEventObj); |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 325 | } else { |
Siarhei Vishniakou | d6058f4 | 2018-01-04 13:00:48 -0800 | [diff] [blame] | 326 | ALOGW("channel '%s' ~ Failed to obtain event object.", |
| 327 | getInputChannelName().c_str()); |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 328 | skipCallbacks = true; |
| 329 | } |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 330 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 331 | |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 332 | if (skipCallbacks) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 333 | mInputConsumer.sendFinishedSignal(seq, false); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 334 | } |
| 335 | } |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 339 | static jlong nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak, |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 340 | jobject inputChannelObj, jobject messageQueueObj) { |
| 341 | sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env, |
| 342 | inputChannelObj); |
| 343 | if (inputChannel == NULL) { |
| 344 | jniThrowRuntimeException(env, "InputChannel is not initialized."); |
| 345 | return 0; |
| 346 | } |
| 347 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 348 | sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj); |
| 349 | if (messageQueue == NULL) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 350 | jniThrowRuntimeException(env, "MessageQueue is not initialized."); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | sp<NativeInputEventReceiver> receiver = new NativeInputEventReceiver(env, |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 355 | receiverWeak, inputChannel, messageQueue); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 356 | status_t status = receiver->initialize(); |
| 357 | if (status) { |
| 358 | String8 message; |
| 359 | message.appendFormat("Failed to initialize input event receiver. status=%d", status); |
| 360 | jniThrowRuntimeException(env, message.string()); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | receiver->incStrong(gInputEventReceiverClassInfo.clazz); // retain a reference for the object |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 365 | return reinterpret_cast<jlong>(receiver.get()); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 368 | static void nativeDispose(JNIEnv* env, jclass clazz, jlong receiverPtr) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 369 | sp<NativeInputEventReceiver> receiver = |
| 370 | reinterpret_cast<NativeInputEventReceiver*>(receiverPtr); |
Jeff Brown | 80a1de1 | 2012-05-31 16:23:11 -0700 | [diff] [blame] | 371 | receiver->dispose(); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 372 | receiver->decStrong(gInputEventReceiverClassInfo.clazz); // drop reference held by the object |
| 373 | } |
| 374 | |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 375 | static void nativeFinishInputEvent(JNIEnv* env, jclass clazz, jlong receiverPtr, |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 376 | jint seq, jboolean handled) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 377 | sp<NativeInputEventReceiver> receiver = |
| 378 | reinterpret_cast<NativeInputEventReceiver*>(receiverPtr); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 379 | status_t status = receiver->finishInputEvent(seq, handled); |
Jeff Brown | 9806a23 | 2012-02-17 10:28:09 -0800 | [diff] [blame] | 380 | if (status && status != DEAD_OBJECT) { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 381 | String8 message; |
| 382 | message.appendFormat("Failed to finish input event. status=%d", status); |
| 383 | jniThrowRuntimeException(env, message.string()); |
| 384 | } |
| 385 | } |
| 386 | |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 387 | static jboolean nativeConsumeBatchedInputEvents(JNIEnv* env, jclass clazz, jlong receiverPtr, |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 388 | jlong frameTimeNanos) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 389 | sp<NativeInputEventReceiver> receiver = |
| 390 | reinterpret_cast<NativeInputEventReceiver*>(receiverPtr); |
Michael Wright | 62ce65d | 2013-10-25 14:50:36 -0700 | [diff] [blame] | 391 | bool consumedBatch; |
| 392 | status_t status = receiver->consumeEvents(env, true /*consumeBatches*/, frameTimeNanos, |
| 393 | &consumedBatch); |
Jeff Brown | 3bdcdd8 | 2012-04-10 20:36:07 -0700 | [diff] [blame] | 394 | if (status && status != DEAD_OBJECT && !env->ExceptionCheck()) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 395 | String8 message; |
| 396 | message.appendFormat("Failed to consume batched input event. status=%d", status); |
| 397 | jniThrowRuntimeException(env, message.string()); |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 398 | return JNI_FALSE; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 399 | } |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 400 | return consumedBatch ? JNI_TRUE : JNI_FALSE; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 403 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 404 | static const JNINativeMethod gMethods[] = { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 405 | /* name, signature, funcPtr */ |
| 406 | { "nativeInit", |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 407 | "(Ljava/lang/ref/WeakReference;Landroid/view/InputChannel;Landroid/os/MessageQueue;)J", |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 408 | (void*)nativeInit }, |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 409 | { "nativeDispose", "(J)V", |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 410 | (void*)nativeDispose }, |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 411 | { "nativeFinishInputEvent", "(JIZ)V", |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 412 | (void*)nativeFinishInputEvent }, |
Ashok Bhat | a931d521 | 2014-01-08 14:04:51 +0000 | [diff] [blame] | 413 | { "nativeConsumeBatchedInputEvents", "(JJ)Z", |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 414 | (void*)nativeConsumeBatchedInputEvents }, |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 415 | }; |
| 416 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 417 | int register_android_view_InputEventReceiver(JNIEnv* env) { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 418 | int res = RegisterMethodsOrDie(env, "android/view/InputEventReceiver", |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 419 | gMethods, NELEM(gMethods)); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 420 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 421 | jclass clazz = FindClassOrDie(env, "android/view/InputEventReceiver"); |
| 422 | gInputEventReceiverClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 423 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 424 | gInputEventReceiverClassInfo.dispatchInputEvent = GetMethodIDOrDie(env, |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 425 | gInputEventReceiverClassInfo.clazz, |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 426 | "dispatchInputEvent", "(ILandroid/view/InputEvent;I)V"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 427 | gInputEventReceiverClassInfo.dispatchBatchedInputEventPending = GetMethodIDOrDie(env, |
| 428 | gInputEventReceiverClassInfo.clazz, "dispatchBatchedInputEventPending", "()V"); |
| 429 | |
| 430 | return res; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | } // namespace android |