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