blob: 8fee7baa398f940e0718ca93815339b8520e1f4e [file] [log] [blame]
Jeff Brown32cbc38552011-12-01 14:01:49 -08001/*
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 Vishniakoud6058f42018-01-04 13:00:48 -080021#include <inttypes.h>
22
Steven Moreland2279b252017-07-19 09:50:45 -070023#include <nativehelper/JNIHelp.h>
Jeff Brown32cbc38552011-12-01 14:01:49 -080024
25#include <android_runtime/AndroidRuntime.h>
26#include <utils/Log.h>
27#include <utils/Looper.h>
Jeff Brown56513492013-05-21 20:37:51 -070028#include <utils/Vector.h>
Jeff Brown32cbc38552011-12-01 14:01:49 -080029#include <utils/threads.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070030#include <input/InputTransport.h>
Jeff Brown32cbc38552011-12-01 14:01:49 -080031#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 Moreland2279b252017-07-19 09:50:45 -070036#include <nativehelper/ScopedLocalRef.h>
Jeff Browna4ca8ea2013-04-02 18:01:38 -070037
Andreas Gampe987f79f2014-11-18 17:29:46 -080038#include "core_jni_helpers.h"
39
Jeff Brown32cbc38552011-12-01 14:01:49 -080040namespace android {
41
Andreas Gampeed6b9df2014-11-20 22:02:20 -080042static const bool kDebugDispatchCycle = false;
43
Jeff Brown32cbc38552011-12-01 14:01:49 -080044static struct {
45 jclass clazz;
46
47 jmethodID dispatchInputEvent;
Jeff Brown072ec962012-02-07 14:46:57 -080048 jmethodID dispatchBatchedInputEventPending;
Jeff Brown32cbc38552011-12-01 14:01:49 -080049} gInputEventReceiverClassInfo;
50
51
Jeff Brown80a1de12012-05-31 16:23:11 -070052class NativeInputEventReceiver : public LooperCallback {
Jeff Brown32cbc38552011-12-01 14:01:49 -080053public:
54 NativeInputEventReceiver(JNIEnv* env,
Jeff Browna4ca8ea2013-04-02 18:01:38 -070055 jobject receiverWeak, const sp<InputChannel>& inputChannel,
Jeff Brown603b4452012-04-06 17:39:41 -070056 const sp<MessageQueue>& messageQueue);
Jeff Brown32cbc38552011-12-01 14:01:49 -080057
58 status_t initialize();
Jeff Brown80a1de12012-05-31 16:23:11 -070059 void dispose();
Jeff Brown072ec962012-02-07 14:46:57 -080060 status_t finishInputEvent(uint32_t seq, bool handled);
Michael Wright62ce65d2013-10-25 14:50:36 -070061 status_t consumeEvents(JNIEnv* env, bool consumeBatches, nsecs_t frameTime,
62 bool* outConsumedBatch);
Jeff Brown32cbc38552011-12-01 14:01:49 -080063
64protected:
65 virtual ~NativeInputEventReceiver();
66
67private:
Jeff Brown56513492013-05-21 20:37:51 -070068 struct Finish {
69 uint32_t seq;
70 bool handled;
71 };
72
Jeff Browna4ca8ea2013-04-02 18:01:38 -070073 jobject mReceiverWeakGlobal;
Jeff Brown32cbc38552011-12-01 14:01:49 -080074 InputConsumer mInputConsumer;
Jeff Brown603b4452012-04-06 17:39:41 -070075 sp<MessageQueue> mMessageQueue;
Jeff Brown32cbc38552011-12-01 14:01:49 -080076 PreallocatedInputEventFactory mInputEventFactory;
Jeff Brown072ec962012-02-07 14:46:57 -080077 bool mBatchedInputEventPending;
Jeff Brown56513492013-05-21 20:37:51 -070078 int mFdEvents;
79 Vector<Finish> mFinishQueue;
80
81 void setFdEvents(int events);
Jeff Brown32cbc38552011-12-01 14:01:49 -080082
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -080083 const std::string getInputChannelName() {
84 return mInputConsumer.getChannel()->getName();
Jeff Brown32cbc38552011-12-01 14:01:49 -080085 }
Jeff Brown3bdcdd82012-04-10 20:36:07 -070086
Jeff Brown80a1de12012-05-31 16:23:11 -070087 virtual int handleEvent(int receiveFd, int events, void* data);
Jeff Brown32cbc38552011-12-01 14:01:49 -080088};
89
90
91NativeInputEventReceiver::NativeInputEventReceiver(JNIEnv* env,
Jeff Browna4ca8ea2013-04-02 18:01:38 -070092 jobject receiverWeak, const sp<InputChannel>& inputChannel,
Jeff Brown603b4452012-04-06 17:39:41 -070093 const sp<MessageQueue>& messageQueue) :
Jeff Browna4ca8ea2013-04-02 18:01:38 -070094 mReceiverWeakGlobal(env->NewGlobalRef(receiverWeak)),
Jeff Brown603b4452012-04-06 17:39:41 -070095 mInputConsumer(inputChannel), mMessageQueue(messageQueue),
Jeff Brown56513492013-05-21 20:37:51 -070096 mBatchedInputEventPending(false), mFdEvents(0) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -080097 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -080098 ALOGD("channel '%s' ~ Initializing input event receiver.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -080099 }
Jeff Brown32cbc38552011-12-01 14:01:49 -0800100}
101
102NativeInputEventReceiver::~NativeInputEventReceiver() {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800103 JNIEnv* env = AndroidRuntime::getJNIEnv();
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700104 env->DeleteGlobalRef(mReceiverWeakGlobal);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800105}
106
107status_t NativeInputEventReceiver::initialize() {
Jeff Brown56513492013-05-21 20:37:51 -0700108 setFdEvents(ALOOPER_EVENT_INPUT);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800109 return OK;
110}
111
Jeff Brown80a1de12012-05-31 16:23:11 -0700112void NativeInputEventReceiver::dispose() {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800113 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800114 ALOGD("channel '%s' ~ Disposing input event receiver.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800115 }
Jeff Brown80a1de12012-05-31 16:23:11 -0700116
Jeff Brown56513492013-05-21 20:37:51 -0700117 setFdEvents(0);
Jeff Brown80a1de12012-05-31 16:23:11 -0700118}
119
Jeff Brown072ec962012-02-07 14:46:57 -0800120status_t NativeInputEventReceiver::finishInputEvent(uint32_t seq, bool handled) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800121 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800122 ALOGD("channel '%s' ~ Finished input event.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800123 }
Jeff Brown32cbc38552011-12-01 14:01:49 -0800124
Jeff Brown072ec962012-02-07 14:46:57 -0800125 status_t status = mInputConsumer.sendFinishedSignal(seq, handled);
126 if (status) {
Jeff Brown56513492013-05-21 20:37:51 -0700127 if (status == WOULD_BLOCK) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800128 if (kDebugDispatchCycle) {
129 ALOGD("channel '%s' ~ Could not send finished signal immediately. "
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800130 "Enqueued for later.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800131 }
Jeff Brown56513492013-05-21 20:37:51 -0700132 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 Brown072ec962012-02-07 14:46:57 -0800141 ALOGW("Failed to send finished signal on channel '%s'. status=%d",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800142 getInputChannelName().c_str(), status);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800143 }
Jeff Brown072ec962012-02-07 14:46:57 -0800144 return status;
Jeff Brown32cbc38552011-12-01 14:01:49 -0800145}
146
Jeff Brown56513492013-05-21 20:37:51 -0700147void 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 Brown80a1de12012-05-31 16:23:11 -0700159int NativeInputEventReceiver::handleEvent(int receiveFd, int events, void* data) {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800160 if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) {
Jeff Brown44e13ef2013-03-27 12:34:30 -0700161 // 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 Gampeed6b9df2014-11-20 22:02:20 -0800164 if (kDebugDispatchCycle) {
165 ALOGD("channel '%s' ~ Publisher closed input channel or an error occurred. "
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800166 "events=0x%x", getInputChannelName().c_str(), events);
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800167 }
Jeff Brown32cbc38552011-12-01 14:01:49 -0800168 return 0; // remove the callback
169 }
170
Jeff Brown56513492013-05-21 20:37:51 -0700171 if (events & ALOOPER_EVENT_INPUT) {
172 JNIEnv* env = AndroidRuntime::getJNIEnv();
Michael Wright62ce65d2013-10-25 14:50:36 -0700173 status_t status = consumeEvents(env, false /*consumeBatches*/, -1, NULL);
Jeff Brown56513492013-05-21 20:37:51 -0700174 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 Gampeed6b9df2014-11-20 22:02:20 -0800186 if (kDebugDispatchCycle) {
Bernhard Rosenkränzer46c82b42014-11-30 11:04:10 +0100187 ALOGD("channel '%s' ~ Sent %zu queued finish events; %zu left.",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800188 getInputChannelName().c_str(), i, mFinishQueue.size());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800189 }
Jeff Brown56513492013-05-21 20:37:51 -0700190 return 1; // keep the callback, try again later
191 }
192
193 ALOGW("Failed to send finished signal on channel '%s'. status=%d",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800194 getInputChannelName().c_str(), status);
Jeff Brown56513492013-05-21 20:37:51 -0700195 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 Gampeed6b9df2014-11-20 22:02:20 -0800205 if (kDebugDispatchCycle) {
Bernhard Rosenkränzer46c82b42014-11-30 11:04:10 +0100206 ALOGD("channel '%s' ~ Sent %zu queued finish events; none left.",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800207 getInputChannelName().c_str(), mFinishQueue.size());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800208 }
Jeff Brown56513492013-05-21 20:37:51 -0700209 mFinishQueue.clear();
210 setFdEvents(ALOOPER_EVENT_INPUT);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800211 return 1;
212 }
213
Jeff Brown56513492013-05-21 20:37:51 -0700214 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. "
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800215 "events=0x%x", getInputChannelName().c_str(), events);
Jeff Brown56513492013-05-21 20:37:51 -0700216 return 1;
Jeff Brown072ec962012-02-07 14:46:57 -0800217}
Jeff Brown32cbc38552011-12-01 14:01:49 -0800218
Jeff Brown771526c2012-04-27 15:13:25 -0700219status_t NativeInputEventReceiver::consumeEvents(JNIEnv* env,
Michael Wright62ce65d2013-10-25 14:50:36 -0700220 bool consumeBatches, nsecs_t frameTime, bool* outConsumedBatch) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800221 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800222 ALOGD("channel '%s' ~ Consuming input events, consumeBatches=%s, frameTime=%" PRId64,
223 getInputChannelName().c_str(),
224 consumeBatches ? "true" : "false", frameTime);
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800225 }
Jeff Brown072ec962012-02-07 14:46:57 -0800226
227 if (consumeBatches) {
228 mBatchedInputEventPending = false;
Jeff Brown32cbc38552011-12-01 14:01:49 -0800229 }
Michael Wright62ce65d2013-10-25 14:50:36 -0700230 if (outConsumedBatch) {
231 *outConsumedBatch = false;
232 }
Jeff Brown32cbc38552011-12-01 14:01:49 -0800233
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700234 ScopedLocalRef<jobject> receiverObj(env, NULL);
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700235 bool skipCallbacks = false;
Jeff Brown072ec962012-02-07 14:46:57 -0800236 for (;;) {
237 uint32_t seq;
238 InputEvent* inputEvent;
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700239 int32_t displayId;
Jeff Brown072ec962012-02-07 14:46:57 -0800240 status_t status = mInputConsumer.consume(&mInputEventFactory,
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700241 consumeBatches, frameTime, &seq, &inputEvent, &displayId);
Jeff Brown072ec962012-02-07 14:46:57 -0800242 if (status) {
243 if (status == WOULD_BLOCK) {
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700244 if (!skipCallbacks && !mBatchedInputEventPending
245 && mInputConsumer.hasPendingBatch()) {
Jeff Brown072ec962012-02-07 14:46:57 -0800246 // There is a pending batch. Come back later.
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700247 if (!receiverObj.get()) {
248 receiverObj.reset(jniGetReferent(env, mReceiverWeakGlobal));
249 if (!receiverObj.get()) {
250 ALOGW("channel '%s' ~ Receiver object was finalized "
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800251 "without being disposed.", getInputChannelName().c_str());
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700252 return DEAD_OBJECT;
253 }
254 }
255
Jeff Brown072ec962012-02-07 14:46:57 -0800256 mBatchedInputEventPending = true;
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800257 if (kDebugDispatchCycle) {
258 ALOGD("channel '%s' ~ Dispatching batched input event pending notification.",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800259 getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800260 }
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700261 env->CallVoidMethod(receiverObj.get(),
Jeff Brown072ec962012-02-07 14:46:57 -0800262 gInputEventReceiverClassInfo.dispatchBatchedInputEventPending);
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700263 if (env->ExceptionCheck()) {
264 ALOGE("Exception dispatching batched input events.");
Jeff Brown072ec962012-02-07 14:46:57 -0800265 mBatchedInputEventPending = false; // try again later
266 }
267 }
268 return OK;
269 }
270 ALOGE("channel '%s' ~ Failed to consume input event. status=%d",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800271 getInputChannelName().c_str(), status);
Jeff Brown072ec962012-02-07 14:46:57 -0800272 return status;
273 }
274 assert(inputEvent);
275
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700276 if (!skipCallbacks) {
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700277 if (!receiverObj.get()) {
278 receiverObj.reset(jniGetReferent(env, mReceiverWeakGlobal));
279 if (!receiverObj.get()) {
280 ALOGW("channel '%s' ~ Receiver object was finalized "
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800281 "without being disposed.", getInputChannelName().c_str());
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700282 return DEAD_OBJECT;
283 }
284 }
285
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700286 jobject inputEventObj;
287 switch (inputEvent->getType()) {
288 case AINPUT_EVENT_TYPE_KEY:
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800289 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800290 ALOGD("channel '%s' ~ Received key event.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800291 }
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700292 inputEventObj = android_view_KeyEvent_fromNative(env,
293 static_cast<KeyEvent*>(inputEvent));
294 break;
Jeff Brown32cbc38552011-12-01 14:01:49 -0800295
Michael Wright62ce65d2013-10-25 14:50:36 -0700296 case AINPUT_EVENT_TYPE_MOTION: {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800297 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800298 ALOGD("channel '%s' ~ Received motion event.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800299 }
Michael Wright62ce65d2013-10-25 14:50:36 -0700300 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 Brown3bdcdd82012-04-10 20:36:07 -0700305 break;
Michael Wright62ce65d2013-10-25 14:50:36 -0700306 }
Jeff Brown32cbc38552011-12-01 14:01:49 -0800307
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700308 default:
309 assert(false); // InputConsumer should prevent this from ever happening
310 inputEventObj = NULL;
311 }
312
313 if (inputEventObj) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800314 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800315 ALOGD("channel '%s' ~ Dispatching input event.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800316 }
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700317 env->CallVoidMethod(receiverObj.get(),
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700318 gInputEventReceiverClassInfo.dispatchInputEvent, seq, inputEventObj,
319 displayId);
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700320 if (env->ExceptionCheck()) {
321 ALOGE("Exception dispatching input event.");
322 skipCallbacks = true;
323 }
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700324 env->DeleteLocalRef(inputEventObj);
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700325 } else {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800326 ALOGW("channel '%s' ~ Failed to obtain event object.",
327 getInputChannelName().c_str());
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700328 skipCallbacks = true;
329 }
Jeff Brown072ec962012-02-07 14:46:57 -0800330 }
Jeff Brown32cbc38552011-12-01 14:01:49 -0800331
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700332 if (skipCallbacks) {
Jeff Brown072ec962012-02-07 14:46:57 -0800333 mInputConsumer.sendFinishedSignal(seq, false);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800334 }
335 }
Jeff Brown32cbc38552011-12-01 14:01:49 -0800336}
337
338
Ashok Bhata931d5212014-01-08 14:04:51 +0000339static jlong nativeInit(JNIEnv* env, jclass clazz, jobject receiverWeak,
Jeff Brown32cbc38552011-12-01 14:01:49 -0800340 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 Brown603b4452012-04-06 17:39:41 -0700348 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
349 if (messageQueue == NULL) {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800350 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
351 return 0;
352 }
353
354 sp<NativeInputEventReceiver> receiver = new NativeInputEventReceiver(env,
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700355 receiverWeak, inputChannel, messageQueue);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800356 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 Bhata931d5212014-01-08 14:04:51 +0000365 return reinterpret_cast<jlong>(receiver.get());
Jeff Brown32cbc38552011-12-01 14:01:49 -0800366}
367
Ashok Bhata931d5212014-01-08 14:04:51 +0000368static void nativeDispose(JNIEnv* env, jclass clazz, jlong receiverPtr) {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800369 sp<NativeInputEventReceiver> receiver =
370 reinterpret_cast<NativeInputEventReceiver*>(receiverPtr);
Jeff Brown80a1de12012-05-31 16:23:11 -0700371 receiver->dispose();
Jeff Brown32cbc38552011-12-01 14:01:49 -0800372 receiver->decStrong(gInputEventReceiverClassInfo.clazz); // drop reference held by the object
373}
374
Ashok Bhata931d5212014-01-08 14:04:51 +0000375static void nativeFinishInputEvent(JNIEnv* env, jclass clazz, jlong receiverPtr,
Jeff Brown072ec962012-02-07 14:46:57 -0800376 jint seq, jboolean handled) {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800377 sp<NativeInputEventReceiver> receiver =
378 reinterpret_cast<NativeInputEventReceiver*>(receiverPtr);
Jeff Brown072ec962012-02-07 14:46:57 -0800379 status_t status = receiver->finishInputEvent(seq, handled);
Jeff Brown9806a232012-02-17 10:28:09 -0800380 if (status && status != DEAD_OBJECT) {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800381 String8 message;
382 message.appendFormat("Failed to finish input event. status=%d", status);
383 jniThrowRuntimeException(env, message.string());
384 }
385}
386
Ashok Bhata931d5212014-01-08 14:04:51 +0000387static jboolean nativeConsumeBatchedInputEvents(JNIEnv* env, jclass clazz, jlong receiverPtr,
Jeff Brown771526c2012-04-27 15:13:25 -0700388 jlong frameTimeNanos) {
Jeff Brown072ec962012-02-07 14:46:57 -0800389 sp<NativeInputEventReceiver> receiver =
390 reinterpret_cast<NativeInputEventReceiver*>(receiverPtr);
Michael Wright62ce65d2013-10-25 14:50:36 -0700391 bool consumedBatch;
392 status_t status = receiver->consumeEvents(env, true /*consumeBatches*/, frameTimeNanos,
393 &consumedBatch);
Jeff Brown3bdcdd82012-04-10 20:36:07 -0700394 if (status && status != DEAD_OBJECT && !env->ExceptionCheck()) {
Jeff Brown072ec962012-02-07 14:46:57 -0800395 String8 message;
396 message.appendFormat("Failed to consume batched input event. status=%d", status);
397 jniThrowRuntimeException(env, message.string());
Ashok Bhata931d5212014-01-08 14:04:51 +0000398 return JNI_FALSE;
Jeff Brown072ec962012-02-07 14:46:57 -0800399 }
Ashok Bhata931d5212014-01-08 14:04:51 +0000400 return consumedBatch ? JNI_TRUE : JNI_FALSE;
Jeff Brown072ec962012-02-07 14:46:57 -0800401}
402
Jeff Brown32cbc38552011-12-01 14:01:49 -0800403
Daniel Micay76f6a862015-09-19 17:31:01 -0400404static const JNINativeMethod gMethods[] = {
Jeff Brown32cbc38552011-12-01 14:01:49 -0800405 /* name, signature, funcPtr */
406 { "nativeInit",
Ashok Bhata931d5212014-01-08 14:04:51 +0000407 "(Ljava/lang/ref/WeakReference;Landroid/view/InputChannel;Landroid/os/MessageQueue;)J",
Jeff Brown32cbc38552011-12-01 14:01:49 -0800408 (void*)nativeInit },
Ashok Bhata931d5212014-01-08 14:04:51 +0000409 { "nativeDispose", "(J)V",
Jeff Brown32cbc38552011-12-01 14:01:49 -0800410 (void*)nativeDispose },
Ashok Bhata931d5212014-01-08 14:04:51 +0000411 { "nativeFinishInputEvent", "(JIZ)V",
Jeff Brown072ec962012-02-07 14:46:57 -0800412 (void*)nativeFinishInputEvent },
Ashok Bhata931d5212014-01-08 14:04:51 +0000413 { "nativeConsumeBatchedInputEvents", "(JJ)Z",
Jeff Brown072ec962012-02-07 14:46:57 -0800414 (void*)nativeConsumeBatchedInputEvents },
Jeff Brown32cbc38552011-12-01 14:01:49 -0800415};
416
Jeff Brown32cbc38552011-12-01 14:01:49 -0800417int register_android_view_InputEventReceiver(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -0800418 int res = RegisterMethodsOrDie(env, "android/view/InputEventReceiver",
Jeff Brown32cbc38552011-12-01 14:01:49 -0800419 gMethods, NELEM(gMethods));
Jeff Brown32cbc38552011-12-01 14:01:49 -0800420
Andreas Gampe987f79f2014-11-18 17:29:46 -0800421 jclass clazz = FindClassOrDie(env, "android/view/InputEventReceiver");
422 gInputEventReceiverClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
Jeff Brown32cbc38552011-12-01 14:01:49 -0800423
Andreas Gampe987f79f2014-11-18 17:29:46 -0800424 gInputEventReceiverClassInfo.dispatchInputEvent = GetMethodIDOrDie(env,
Jeff Brown32cbc38552011-12-01 14:01:49 -0800425 gInputEventReceiverClassInfo.clazz,
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700426 "dispatchInputEvent", "(ILandroid/view/InputEvent;I)V");
Andreas Gampe987f79f2014-11-18 17:29:46 -0800427 gInputEventReceiverClassInfo.dispatchBatchedInputEventPending = GetMethodIDOrDie(env,
428 gInputEventReceiverClassInfo.clazz, "dispatchBatchedInputEventPending", "()V");
429
430 return res;
Jeff Brown32cbc38552011-12-01 14:01:49 -0800431}
432
433} // namespace android