blob: aa10a2f98a7e1ce2f48d17ba08a14af83ee52118 [file] [log] [blame]
Jeff Brownc28867a2013-03-26 15:42:39 -07001/*
2 * Copyright (C) 2013 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 "InputEventSender"
18
19//#define LOG_NDEBUG 0
20
Steven Moreland2279b252017-07-19 09:50:45 -070021#include <nativehelper/JNIHelp.h>
Jeff Brownc28867a2013-03-26 15:42:39 -070022
23#include <android_runtime/AndroidRuntime.h>
24#include <utils/Log.h>
25#include <utils/Looper.h>
26#include <utils/threads.h>
27#include <utils/KeyedVector.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070028#include <input/InputTransport.h>
Jeff Brownc28867a2013-03-26 15:42:39 -070029#include "android_os_MessageQueue.h"
30#include "android_view_InputChannel.h"
31#include "android_view_KeyEvent.h"
32#include "android_view_MotionEvent.h"
33
Steven Moreland2279b252017-07-19 09:50:45 -070034#include <nativehelper/ScopedLocalRef.h>
Jeff Browna4ca8ea2013-04-02 18:01:38 -070035
Andreas Gampe987f79f2014-11-18 17:29:46 -080036#include "core_jni_helpers.h"
37
Jeff Brownc28867a2013-03-26 15:42:39 -070038namespace android {
39
Andreas Gampeed6b9df2014-11-20 22:02:20 -080040// Log debug messages about the dispatch cycle.
41static const bool kDebugDispatchCycle = false;
42
Jeff Brownc28867a2013-03-26 15:42:39 -070043static struct {
44 jclass clazz;
45
46 jmethodID dispatchInputEventFinished;
47} gInputEventSenderClassInfo;
48
49
50class NativeInputEventSender : public LooperCallback {
51public:
52 NativeInputEventSender(JNIEnv* env,
Jeff Browna4ca8ea2013-04-02 18:01:38 -070053 jobject senderWeak, const sp<InputChannel>& inputChannel,
Jeff Brownc28867a2013-03-26 15:42:39 -070054 const sp<MessageQueue>& messageQueue);
55
56 status_t initialize();
57 void dispose();
58 status_t sendKeyEvent(uint32_t seq, const KeyEvent* event);
59 status_t sendMotionEvent(uint32_t seq, const MotionEvent* event);
60
61protected:
62 virtual ~NativeInputEventSender();
63
64private:
Jeff Browna4ca8ea2013-04-02 18:01:38 -070065 jobject mSenderWeakGlobal;
Jeff Brownc28867a2013-03-26 15:42:39 -070066 InputPublisher mInputPublisher;
67 sp<MessageQueue> mMessageQueue;
68 KeyedVector<uint32_t, uint32_t> mPublishedSeqMap;
69 uint32_t mNextPublishedSeq;
70
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -080071 const std::string getInputChannelName() {
72 return mInputPublisher.getChannel()->getName();
Jeff Brownc28867a2013-03-26 15:42:39 -070073 }
74
75 virtual int handleEvent(int receiveFd, int events, void* data);
76 status_t receiveFinishedSignals(JNIEnv* env);
77};
78
79
80NativeInputEventSender::NativeInputEventSender(JNIEnv* env,
Jeff Browna4ca8ea2013-04-02 18:01:38 -070081 jobject senderWeak, const sp<InputChannel>& inputChannel,
Jeff Brownc28867a2013-03-26 15:42:39 -070082 const sp<MessageQueue>& messageQueue) :
Jeff Browna4ca8ea2013-04-02 18:01:38 -070083 mSenderWeakGlobal(env->NewGlobalRef(senderWeak)),
Jeff Brownc28867a2013-03-26 15:42:39 -070084 mInputPublisher(inputChannel), mMessageQueue(messageQueue),
Michael Wright10f9b092013-04-01 13:11:34 -070085 mNextPublishedSeq(1) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -080086 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -080087 ALOGD("channel '%s' ~ Initializing input event sender.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -080088 }
Jeff Brownc28867a2013-03-26 15:42:39 -070089}
90
91NativeInputEventSender::~NativeInputEventSender() {
92 JNIEnv* env = AndroidRuntime::getJNIEnv();
Jeff Browna4ca8ea2013-04-02 18:01:38 -070093 env->DeleteGlobalRef(mSenderWeakGlobal);
Jeff Brownc28867a2013-03-26 15:42:39 -070094}
95
96status_t NativeInputEventSender::initialize() {
97 int receiveFd = mInputPublisher.getChannel()->getFd();
98 mMessageQueue->getLooper()->addFd(receiveFd, 0, ALOOPER_EVENT_INPUT, this, NULL);
99 return OK;
100}
101
102void NativeInputEventSender::dispose() {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800103 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800104 ALOGD("channel '%s' ~ Disposing input event sender.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800105 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700106
107 mMessageQueue->getLooper()->removeFd(mInputPublisher.getChannel()->getFd());
108}
109
110status_t NativeInputEventSender::sendKeyEvent(uint32_t seq, const KeyEvent* event) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800111 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800112 ALOGD("channel '%s' ~ Sending key event, seq=%u.", getInputChannelName().c_str(), seq);
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800113 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700114
115 uint32_t publishedSeq = mNextPublishedSeq++;
116 status_t status = mInputPublisher.publishKeyEvent(publishedSeq,
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +0100117 event->getDeviceId(), event->getSource(), event->getDisplayId(), event->getAction(),
118 event->getFlags(), event->getKeyCode(), event->getScanCode(), event->getMetaState(),
Jeff Brownc28867a2013-03-26 15:42:39 -0700119 event->getRepeatCount(), event->getDownTime(), event->getEventTime());
120 if (status) {
121 ALOGW("Failed to send key event on channel '%s'. status=%d",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800122 getInputChannelName().c_str(), status);
Jeff Brownc28867a2013-03-26 15:42:39 -0700123 return status;
124 }
125 mPublishedSeqMap.add(publishedSeq, seq);
126 return OK;
127}
128
129status_t NativeInputEventSender::sendMotionEvent(uint32_t seq, const MotionEvent* event) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800130 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800131 ALOGD("channel '%s' ~ Sending motion event, seq=%u.", getInputChannelName().c_str(), seq);
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800132 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700133
134 uint32_t publishedSeq;
135 for (size_t i = 0; i <= event->getHistorySize(); i++) {
136 publishedSeq = mNextPublishedSeq++;
137 status_t status = mInputPublisher.publishMotionEvent(publishedSeq,
Siarhei Vishniakou91fa08f2018-06-08 22:49:30 +0100138 event->getDeviceId(), event->getSource(), event->getDisplayId(),
Michael Wright5bd69e62015-05-14 14:48:08 +0100139 event->getAction(), event->getActionButton(), event->getFlags(),
Jeff Brownc28867a2013-03-26 15:42:39 -0700140 event->getEdgeFlags(), event->getMetaState(), event->getButtonState(),
Siarhei Vishniakou0fae8702019-01-14 17:37:31 -0800141 event->getClassification(),
Jeff Brownc28867a2013-03-26 15:42:39 -0700142 event->getXOffset(), event->getYOffset(),
143 event->getXPrecision(), event->getYPrecision(),
144 event->getDownTime(), event->getHistoricalEventTime(i),
145 event->getPointerCount(), event->getPointerProperties(),
146 event->getHistoricalRawPointerCoords(0, i));
147 if (status) {
148 ALOGW("Failed to send motion event sample on channel '%s'. status=%d",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800149 getInputChannelName().c_str(), status);
Jeff Brownc28867a2013-03-26 15:42:39 -0700150 return status;
151 }
152 }
153 mPublishedSeqMap.add(publishedSeq, seq);
154 return OK;
155}
156
157int NativeInputEventSender::handleEvent(int receiveFd, int events, void* data) {
158 if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) {
Jeff Brown44e13ef2013-03-27 12:34:30 -0700159 // This error typically occurs when the consumer has closed the input channel
160 // as part of finishing an IME session, in which case the publisher will
161 // soon be disposed as well.
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800162 if (kDebugDispatchCycle) {
163 ALOGD("channel '%s' ~ Consumer closed input channel or an error occurred. "
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800164 "events=0x%x", getInputChannelName().c_str(), events);
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800165 }
166
Jeff Brownc28867a2013-03-26 15:42:39 -0700167 return 0; // remove the callback
168 }
169
170 if (!(events & ALOOPER_EVENT_INPUT)) {
171 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. "
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800172 "events=0x%x", getInputChannelName().c_str(), events);
Jeff Brownc28867a2013-03-26 15:42:39 -0700173 return 1;
174 }
175
176 JNIEnv* env = AndroidRuntime::getJNIEnv();
177 status_t status = receiveFinishedSignals(env);
178 mMessageQueue->raiseAndClearException(env, "handleReceiveCallback");
179 return status == OK || status == NO_MEMORY ? 1 : 0;
180}
181
182status_t NativeInputEventSender::receiveFinishedSignals(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800183 if (kDebugDispatchCycle) {
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800184 ALOGD("channel '%s' ~ Receiving finished signals.", getInputChannelName().c_str());
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800185 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700186
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700187 ScopedLocalRef<jobject> senderObj(env, NULL);
Jeff Brownc28867a2013-03-26 15:42:39 -0700188 bool skipCallbacks = false;
189 for (;;) {
190 uint32_t publishedSeq;
191 bool handled;
192 status_t status = mInputPublisher.receiveFinishedSignal(&publishedSeq, &handled);
193 if (status) {
194 if (status == WOULD_BLOCK) {
195 return OK;
196 }
197 ALOGE("channel '%s' ~ Failed to consume finished signals. status=%d",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800198 getInputChannelName().c_str(), status);
Jeff Brownc28867a2013-03-26 15:42:39 -0700199 return status;
200 }
201
202 ssize_t index = mPublishedSeqMap.indexOfKey(publishedSeq);
203 if (index >= 0) {
204 uint32_t seq = mPublishedSeqMap.valueAt(index);
205 mPublishedSeqMap.removeItemsAt(index);
206
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800207 if (kDebugDispatchCycle) {
208 ALOGD("channel '%s' ~ Received finished signal, seq=%u, handled=%s, "
Bernhard Rosenkränzer46c82b42014-11-30 11:04:10 +0100209 "pendingEvents=%zu.",
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800210 getInputChannelName().c_str(), seq, handled ? "true" : "false",
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800211 mPublishedSeqMap.size());
212 }
Jeff Brownc28867a2013-03-26 15:42:39 -0700213
214 if (!skipCallbacks) {
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700215 if (!senderObj.get()) {
216 senderObj.reset(jniGetReferent(env, mSenderWeakGlobal));
217 if (!senderObj.get()) {
218 ALOGW("channel '%s' ~ Sender object was finalized "
Siarhei Vishniakoud6058f42018-01-04 13:00:48 -0800219 "without being disposed.", getInputChannelName().c_str());
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700220 return DEAD_OBJECT;
221 }
222 }
223
224 env->CallVoidMethod(senderObj.get(),
Jeff Brownc28867a2013-03-26 15:42:39 -0700225 gInputEventSenderClassInfo.dispatchInputEventFinished,
226 jint(seq), jboolean(handled));
227 if (env->ExceptionCheck()) {
228 ALOGE("Exception dispatching finished signal.");
229 skipCallbacks = true;
230 }
231 }
232 }
233 }
234}
235
236
Ashok Bhata931d5212014-01-08 14:04:51 +0000237static jlong nativeInit(JNIEnv* env, jclass clazz, jobject senderWeak,
Jeff Brownc28867a2013-03-26 15:42:39 -0700238 jobject inputChannelObj, jobject messageQueueObj) {
239 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,
240 inputChannelObj);
241 if (inputChannel == NULL) {
242 jniThrowRuntimeException(env, "InputChannel is not initialized.");
243 return 0;
244 }
245
246 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
247 if (messageQueue == NULL) {
248 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
249 return 0;
250 }
251
252 sp<NativeInputEventSender> sender = new NativeInputEventSender(env,
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700253 senderWeak, inputChannel, messageQueue);
Jeff Brownc28867a2013-03-26 15:42:39 -0700254 status_t status = sender->initialize();
255 if (status) {
256 String8 message;
257 message.appendFormat("Failed to initialize input event sender. status=%d", status);
258 jniThrowRuntimeException(env, message.string());
259 return 0;
260 }
261
262 sender->incStrong(gInputEventSenderClassInfo.clazz); // retain a reference for the object
Ashok Bhata931d5212014-01-08 14:04:51 +0000263 return reinterpret_cast<jlong>(sender.get());
Jeff Brownc28867a2013-03-26 15:42:39 -0700264}
265
Ashok Bhata931d5212014-01-08 14:04:51 +0000266static void nativeDispose(JNIEnv* env, jclass clazz, jlong senderPtr) {
Jeff Brownc28867a2013-03-26 15:42:39 -0700267 sp<NativeInputEventSender> sender =
268 reinterpret_cast<NativeInputEventSender*>(senderPtr);
269 sender->dispose();
270 sender->decStrong(gInputEventSenderClassInfo.clazz); // drop reference held by the object
271}
272
Ashok Bhata931d5212014-01-08 14:04:51 +0000273static jboolean nativeSendKeyEvent(JNIEnv* env, jclass clazz, jlong senderPtr,
Jeff Brownc28867a2013-03-26 15:42:39 -0700274 jint seq, jobject eventObj) {
275 sp<NativeInputEventSender> sender =
276 reinterpret_cast<NativeInputEventSender*>(senderPtr);
277 KeyEvent event;
278 android_view_KeyEvent_toNative(env, eventObj, &event);
279 status_t status = sender->sendKeyEvent(seq, &event);
280 return !status;
281}
282
Ashok Bhata931d5212014-01-08 14:04:51 +0000283static jboolean nativeSendMotionEvent(JNIEnv* env, jclass clazz, jlong senderPtr,
Jeff Brownc28867a2013-03-26 15:42:39 -0700284 jint seq, jobject eventObj) {
285 sp<NativeInputEventSender> sender =
286 reinterpret_cast<NativeInputEventSender*>(senderPtr);
287 MotionEvent* event = android_view_MotionEvent_getNativePtr(env, eventObj);
288 status_t status = sender->sendMotionEvent(seq, event);
289 return !status;
290}
291
292
Daniel Micay76f6a862015-09-19 17:31:01 -0400293static const JNINativeMethod gMethods[] = {
Jeff Brownc28867a2013-03-26 15:42:39 -0700294 /* name, signature, funcPtr */
295 { "nativeInit",
Ashok Bhata931d5212014-01-08 14:04:51 +0000296 "(Ljava/lang/ref/WeakReference;Landroid/view/InputChannel;Landroid/os/MessageQueue;)J",
Jeff Brownc28867a2013-03-26 15:42:39 -0700297 (void*)nativeInit },
Ashok Bhata931d5212014-01-08 14:04:51 +0000298 { "nativeDispose", "(J)V",
Jeff Brownc28867a2013-03-26 15:42:39 -0700299 (void*)nativeDispose },
Ashok Bhata931d5212014-01-08 14:04:51 +0000300 { "nativeSendKeyEvent", "(JILandroid/view/KeyEvent;)Z",
Jeff Brownc28867a2013-03-26 15:42:39 -0700301 (void*)nativeSendKeyEvent },
Ashok Bhata931d5212014-01-08 14:04:51 +0000302 { "nativeSendMotionEvent", "(JILandroid/view/MotionEvent;)Z",
Jeff Brownc28867a2013-03-26 15:42:39 -0700303 (void*)nativeSendMotionEvent },
304};
305
Jeff Brownc28867a2013-03-26 15:42:39 -0700306int register_android_view_InputEventSender(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -0800307 int res = RegisterMethodsOrDie(env, "android/view/InputEventSender", gMethods, NELEM(gMethods));
Jeff Brownc28867a2013-03-26 15:42:39 -0700308
Andreas Gampe987f79f2014-11-18 17:29:46 -0800309 jclass clazz = FindClassOrDie(env, "android/view/InputEventSender");
310 gInputEventSenderClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
Jeff Brownc28867a2013-03-26 15:42:39 -0700311
Andreas Gampe987f79f2014-11-18 17:29:46 -0800312 gInputEventSenderClassInfo.dispatchInputEventFinished = GetMethodIDOrDie(
313 env, gInputEventSenderClassInfo.clazz, "dispatchInputEventFinished", "(IZ)V");
314
315 return res;
Jeff Brownc28867a2013-03-26 15:42:39 -0700316}
317
318} // namespace android