1 /*
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
21 #include <android_runtime/AndroidRuntime.h>
22 #include <input/InputTransport.h>
23 #include <log/log.h>
24 #include <nativehelper/JNIHelp.h>
25 #include <nativehelper/ScopedLocalRef.h>
26 #include <utils/Looper.h>
27 #include "android_os_MessageQueue.h"
28 #include "android_view_InputChannel.h"
29 #include "android_view_KeyEvent.h"
30 #include "android_view_MotionEvent.h"
31 #include "core_jni_helpers.h"
32
33 #include <inttypes.h>
34 #include <unordered_map>
35
36
37 using android::base::Result;
38
39 namespace android {
40
41 // Log debug messages about the dispatch cycle.
42 static constexpr bool kDebugDispatchCycle = false;
43
44 static struct {
45 jclass clazz;
46
47 jmethodID dispatchInputEventFinished;
48 jmethodID dispatchTimelineReported;
49 } gInputEventSenderClassInfo;
50
51
52 class NativeInputEventSender : public LooperCallback {
53 public:
54 NativeInputEventSender(JNIEnv* env, jobject senderWeak,
55 const std::shared_ptr<InputChannel>& inputChannel,
56 const sp<MessageQueue>& messageQueue);
57
58 status_t initialize();
59 void dispose();
60 status_t sendKeyEvent(uint32_t seq, const KeyEvent* event);
61 status_t sendMotionEvent(uint32_t seq, const MotionEvent* event);
62
63 protected:
64 virtual ~NativeInputEventSender();
65
66 private:
67 jobject mSenderWeakGlobal;
68 InputPublisher mInputPublisher;
69 sp<MessageQueue> mMessageQueue;
70 std::unordered_map<uint32_t, uint32_t> mPublishedSeqMap;
71
72 uint32_t mNextPublishedSeq;
73
getInputChannelName()74 const std::string getInputChannelName() {
75 return mInputPublisher.getChannel()->getName();
76 }
77
78 int handleEvent(int receiveFd, int events, void* data) override;
79 status_t processConsumerResponse(JNIEnv* env);
80 bool notifyConsumerResponse(JNIEnv* env, jobject sender,
81 const InputPublisher::ConsumerResponse& response,
82 bool skipCallbacks);
83 };
84
NativeInputEventSender(JNIEnv * env,jobject senderWeak,const std::shared_ptr<InputChannel> & inputChannel,const sp<MessageQueue> & messageQueue)85 NativeInputEventSender::NativeInputEventSender(JNIEnv* env, jobject senderWeak,
86 const std::shared_ptr<InputChannel>& inputChannel,
87 const sp<MessageQueue>& messageQueue)
88 : mSenderWeakGlobal(env->NewGlobalRef(senderWeak)),
89 mInputPublisher(inputChannel),
90 mMessageQueue(messageQueue),
91 mNextPublishedSeq(1) {
92 if (kDebugDispatchCycle) {
93 ALOGD("channel '%s' ~ Initializing input event sender.", getInputChannelName().c_str());
94 }
95 }
96
~NativeInputEventSender()97 NativeInputEventSender::~NativeInputEventSender() {
98 JNIEnv* env = AndroidRuntime::getJNIEnv();
99 env->DeleteGlobalRef(mSenderWeakGlobal);
100 }
101
initialize()102 status_t NativeInputEventSender::initialize() {
103 int receiveFd = mInputPublisher.getChannel()->getFd();
104 mMessageQueue->getLooper()->addFd(receiveFd, 0, ALOOPER_EVENT_INPUT, this, NULL);
105 return OK;
106 }
107
dispose()108 void NativeInputEventSender::dispose() {
109 if (kDebugDispatchCycle) {
110 ALOGD("channel '%s' ~ Disposing input event sender.", getInputChannelName().c_str());
111 }
112
113 mMessageQueue->getLooper()->removeFd(mInputPublisher.getChannel()->getFd());
114 }
115
sendKeyEvent(uint32_t seq,const KeyEvent * event)116 status_t NativeInputEventSender::sendKeyEvent(uint32_t seq, const KeyEvent* event) {
117 if (kDebugDispatchCycle) {
118 ALOGD("channel '%s' ~ Sending key event, seq=%u.", getInputChannelName().c_str(), seq);
119 }
120
121 uint32_t publishedSeq = mNextPublishedSeq++;
122 status_t status =
123 mInputPublisher.publishKeyEvent(publishedSeq, event->getId(), event->getDeviceId(),
124 event->getSource(), event->getDisplayId(),
125 event->getHmac(), event->getAction(), event->getFlags(),
126 event->getKeyCode(), event->getScanCode(),
127 event->getMetaState(), event->getRepeatCount(),
128 event->getDownTime(), event->getEventTime());
129 if (status) {
130 ALOGW("Failed to send key event on channel '%s'. status=%d",
131 getInputChannelName().c_str(), status);
132 return status;
133 }
134 mPublishedSeqMap.emplace(publishedSeq, seq);
135 return OK;
136 }
137
sendMotionEvent(uint32_t seq,const MotionEvent * event)138 status_t NativeInputEventSender::sendMotionEvent(uint32_t seq, const MotionEvent* event) {
139 if (kDebugDispatchCycle) {
140 ALOGD("channel '%s' ~ Sending motion event, seq=%u.", getInputChannelName().c_str(), seq);
141 }
142
143 uint32_t publishedSeq;
144 for (size_t i = 0; i <= event->getHistorySize(); i++) {
145 publishedSeq = mNextPublishedSeq++;
146 status_t status =
147 mInputPublisher.publishMotionEvent(publishedSeq, event->getId(),
148 event->getDeviceId(), event->getSource(),
149 event->getDisplayId(), event->getHmac(),
150 event->getAction(), event->getActionButton(),
151 event->getFlags(), event->getEdgeFlags(),
152 event->getMetaState(), event->getButtonState(),
153 event->getClassification(),
154 event->getTransform(), event->getXPrecision(),
155 event->getYPrecision(),
156 event->getRawXCursorPosition(),
157 event->getRawYCursorPosition(),
158 event->getDisplayOrientation(),
159 event->getDisplaySize().x,
160 event->getDisplaySize().y, event->getDownTime(),
161 event->getHistoricalEventTime(i),
162 event->getPointerCount(),
163 event->getPointerProperties(),
164 event->getHistoricalRawPointerCoords(0, i));
165 if (status) {
166 ALOGW("Failed to send motion event sample on channel '%s'. status=%d",
167 getInputChannelName().c_str(), status);
168 return status;
169 }
170 }
171 mPublishedSeqMap.emplace(publishedSeq, seq);
172 return OK;
173 }
174
handleEvent(int receiveFd,int events,void * data)175 int NativeInputEventSender::handleEvent(int receiveFd, int events, void* data) {
176 if (events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP)) {
177 // This error typically occurs when the consumer has closed the input channel
178 // as part of finishing an IME session, in which case the publisher will
179 // soon be disposed as well.
180 if (kDebugDispatchCycle) {
181 ALOGD("channel '%s' ~ Consumer closed input channel or an error occurred. events=0x%x",
182 getInputChannelName().c_str(), events);
183 }
184
185 return 0; // remove the callback
186 }
187
188 if (!(events & ALOOPER_EVENT_INPUT)) {
189 ALOGW("channel '%s' ~ Received spurious callback for unhandled poll event. events=0x%x",
190 getInputChannelName().c_str(), events);
191 return 1;
192 }
193
194 JNIEnv* env = AndroidRuntime::getJNIEnv();
195 status_t status = processConsumerResponse(env);
196 mMessageQueue->raiseAndClearException(env, "handleReceiveCallback");
197 return status == OK || status == NO_MEMORY ? 1 : 0;
198 }
199
processConsumerResponse(JNIEnv * env)200 status_t NativeInputEventSender::processConsumerResponse(JNIEnv* env) {
201 if (kDebugDispatchCycle) {
202 ALOGD("channel '%s' ~ Receiving finished signals.", getInputChannelName().c_str());
203 }
204
205 ScopedLocalRef<jobject> senderObj(env, jniGetReferent(env, mSenderWeakGlobal));
206 if (!senderObj.get()) {
207 ALOGW("channel '%s' ~ Sender object was finalized without being disposed.",
208 getInputChannelName().c_str());
209 return DEAD_OBJECT;
210 }
211 bool skipCallbacks = false; // stop calling Java functions after an exception occurs
212 for (;;) {
213 Result<InputPublisher::ConsumerResponse> result = mInputPublisher.receiveConsumerResponse();
214 if (!result.ok()) {
215 const status_t status = result.error().code();
216 if (status == WOULD_BLOCK) {
217 return OK;
218 }
219 ALOGE("channel '%s' ~ Failed to process consumer response. status=%d",
220 getInputChannelName().c_str(), status);
221 return status;
222 }
223
224 const bool notified = notifyConsumerResponse(env, senderObj.get(), *result, skipCallbacks);
225 if (!notified) {
226 skipCallbacks = true;
227 }
228 }
229 }
230
231 /**
232 * Invoke the corresponding Java function for the different variants of response.
233 * If the response is a Finished object, invoke dispatchInputEventFinished.
234 * If the response is a Timeline object, invoke dispatchTimelineReported.
235 * Set 'skipCallbacks' to 'true' if a Java exception occurred.
236 * Java function will only be called if 'skipCallbacks' is originally 'false'.
237 *
238 * Return "false" if an exception occurred while calling the Java function
239 * "true" otherwise
240 */
notifyConsumerResponse(JNIEnv * env,jobject sender,const InputPublisher::ConsumerResponse & response,bool skipCallbacks)241 bool NativeInputEventSender::notifyConsumerResponse(
242 JNIEnv* env, jobject sender, const InputPublisher::ConsumerResponse& response,
243 bool skipCallbacks) {
244 if (std::holds_alternative<InputPublisher::Timeline>(response)) {
245 const InputPublisher::Timeline& timeline = std::get<InputPublisher::Timeline>(response);
246
247 if (kDebugDispatchCycle) {
248 ALOGD("channel '%s' ~ Received timeline, inputEventId=%" PRId32
249 ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64,
250 getInputChannelName().c_str(), timeline.inputEventId,
251 timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME],
252 timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME]);
253 }
254
255 if (skipCallbacks) {
256 ALOGW("Java exception occurred. Skipping dispatchTimelineReported for "
257 "inputEventId=%" PRId32,
258 timeline.inputEventId);
259 return true;
260 }
261
262 env->CallVoidMethod(sender, gInputEventSenderClassInfo.dispatchTimelineReported,
263 timeline.inputEventId, timeline.graphicsTimeline);
264 if (env->ExceptionCheck()) {
265 ALOGE("Exception dispatching timeline, inputEventId=%" PRId32, timeline.inputEventId);
266 return false;
267 }
268
269 return true;
270 }
271
272 // Must be a Finished event
273 const InputPublisher::Finished& finished = std::get<InputPublisher::Finished>(response);
274
275 auto it = mPublishedSeqMap.find(finished.seq);
276 if (it == mPublishedSeqMap.end()) {
277 ALOGW("Received 'finished' signal for unknown seq number = %" PRIu32, finished.seq);
278 // Since this is coming from the receiver (typically app), it's possible that an app
279 // does something wrong and sends bad data. Just ignore and process other events.
280 return true;
281 }
282 const uint32_t seq = it->second;
283 mPublishedSeqMap.erase(it);
284
285 if (kDebugDispatchCycle) {
286 ALOGD("channel '%s' ~ Received finished signal, seq=%u, handled=%s, pendingEvents=%zu.",
287 getInputChannelName().c_str(), seq, finished.handled ? "true" : "false",
288 mPublishedSeqMap.size());
289 }
290 if (skipCallbacks) {
291 return true;
292 }
293
294 env->CallVoidMethod(sender, gInputEventSenderClassInfo.dispatchInputEventFinished,
295 static_cast<jint>(seq), static_cast<jboolean>(finished.handled));
296 if (env->ExceptionCheck()) {
297 ALOGE("Exception dispatching finished signal for seq=%" PRIu32, seq);
298 return false;
299 }
300 return true;
301 }
302
nativeInit(JNIEnv * env,jclass clazz,jobject senderWeak,jobject inputChannelObj,jobject messageQueueObj)303 static jlong nativeInit(JNIEnv* env, jclass clazz, jobject senderWeak,
304 jobject inputChannelObj, jobject messageQueueObj) {
305 std::shared_ptr<InputChannel> inputChannel =
306 android_view_InputChannel_getInputChannel(env, inputChannelObj);
307 if (inputChannel == NULL) {
308 jniThrowRuntimeException(env, "InputChannel is not initialized.");
309 return 0;
310 }
311
312 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
313 if (messageQueue == NULL) {
314 jniThrowRuntimeException(env, "MessageQueue is not initialized.");
315 return 0;
316 }
317
318 sp<NativeInputEventSender> sender = new NativeInputEventSender(env,
319 senderWeak, inputChannel, messageQueue);
320 status_t status = sender->initialize();
321 if (status) {
322 String8 message;
323 message.appendFormat("Failed to initialize input event sender. status=%d", status);
324 jniThrowRuntimeException(env, message.string());
325 return 0;
326 }
327
328 sender->incStrong(gInputEventSenderClassInfo.clazz); // retain a reference for the object
329 return reinterpret_cast<jlong>(sender.get());
330 }
331
nativeDispose(JNIEnv * env,jclass clazz,jlong senderPtr)332 static void nativeDispose(JNIEnv* env, jclass clazz, jlong senderPtr) {
333 sp<NativeInputEventSender> sender =
334 reinterpret_cast<NativeInputEventSender*>(senderPtr);
335 sender->dispose();
336 sender->decStrong(gInputEventSenderClassInfo.clazz); // drop reference held by the object
337 }
338
nativeSendKeyEvent(JNIEnv * env,jclass clazz,jlong senderPtr,jint seq,jobject eventObj)339 static jboolean nativeSendKeyEvent(JNIEnv* env, jclass clazz, jlong senderPtr,
340 jint seq, jobject eventObj) {
341 sp<NativeInputEventSender> sender =
342 reinterpret_cast<NativeInputEventSender*>(senderPtr);
343 KeyEvent event;
344 android_view_KeyEvent_toNative(env, eventObj, &event);
345 status_t status = sender->sendKeyEvent(seq, &event);
346 return !status;
347 }
348
nativeSendMotionEvent(JNIEnv * env,jclass clazz,jlong senderPtr,jint seq,jobject eventObj)349 static jboolean nativeSendMotionEvent(JNIEnv* env, jclass clazz, jlong senderPtr,
350 jint seq, jobject eventObj) {
351 sp<NativeInputEventSender> sender =
352 reinterpret_cast<NativeInputEventSender*>(senderPtr);
353 MotionEvent* event = android_view_MotionEvent_getNativePtr(env, eventObj);
354 status_t status = sender->sendMotionEvent(seq, event);
355 return !status;
356 }
357
358
359 static const JNINativeMethod gMethods[] = {
360 /* name, signature, funcPtr */
361 { "nativeInit",
362 "(Ljava/lang/ref/WeakReference;Landroid/view/InputChannel;Landroid/os/MessageQueue;)J",
363 (void*)nativeInit },
364 { "nativeDispose", "(J)V",
365 (void*)nativeDispose },
366 { "nativeSendKeyEvent", "(JILandroid/view/KeyEvent;)Z",
367 (void*)nativeSendKeyEvent },
368 { "nativeSendMotionEvent", "(JILandroid/view/MotionEvent;)Z",
369 (void*)nativeSendMotionEvent },
370 };
371
register_android_view_InputEventSender(JNIEnv * env)372 int register_android_view_InputEventSender(JNIEnv* env) {
373 int res = RegisterMethodsOrDie(env, "android/view/InputEventSender", gMethods, NELEM(gMethods));
374
375 jclass clazz = FindClassOrDie(env, "android/view/InputEventSender");
376 gInputEventSenderClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
377
378 gInputEventSenderClassInfo.dispatchInputEventFinished = GetMethodIDOrDie(
379 env, gInputEventSenderClassInfo.clazz, "dispatchInputEventFinished", "(IZ)V");
380 gInputEventSenderClassInfo.dispatchTimelineReported =
381 GetMethodIDOrDie(env, gInputEventSenderClassInfo.clazz, "dispatchTimelineReported",
382 "(IJJ)V");
383
384 return res;
385 }
386
387 } // namespace android
388