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