blob: 0e0e80dc79d3cd497165d9bf6cd8e17a666ef451 [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001//
2// Copyright 2010 The Android Open Source Project
3//
4// Provides a shared memory transport for input events.
5//
6#define LOG_TAG "InputTransport"
Zimd8402b62023-06-02 11:56:26 +01007#define ATRACE_TAG ATRACE_TAG_INPUT
Jeff Brown5912f952013-07-01 19:10:31 -07008
Jeff Brown5912f952013-07-01 19:10:31 -07009#include <errno.h>
10#include <fcntl.h>
Michael Wrightd0a4a622014-06-09 19:03:32 -070011#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070012#include <math.h>
Egor Paskoa0d32af2023-12-14 17:45:41 +010013#include <poll.h>
Jeff Brown5912f952013-07-01 19:10:31 -070014#include <sys/socket.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070015#include <sys/types.h>
Jeff Brown5912f952013-07-01 19:10:31 -070016#include <unistd.h>
17
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -070018#include <android-base/logging.h>
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +000019#include <android-base/properties.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000020#include <android-base/stringprintf.h>
21#include <binder/Parcel.h>
Jeff Brown5912f952013-07-01 19:10:31 -070022#include <cutils/properties.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080023#include <ftl/enum.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070024#include <log/log.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000025#include <utils/Trace.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070026
Siarhei Vishniakou96818962023-08-23 10:19:02 -070027#include <com_android_input_flags.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <input/InputTransport.h>
Prabir Pradhana37bad12023-08-18 15:55:32 +000029#include <input/TraceTools.h>
Jeff Brown5912f952013-07-01 19:10:31 -070030
Siarhei Vishniakou96818962023-08-23 10:19:02 -070031namespace input_flags = com::android::input::flags;
32
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000033namespace {
34
35/**
36 * Log debug messages about channel messages (send message, receive message).
37 * Enable this via "adb shell setprop log.tag.InputTransportMessages DEBUG"
38 * (requires restart)
39 */
40const bool DEBUG_CHANNEL_MESSAGES =
41 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Messages", ANDROID_LOG_INFO);
42
43/**
44 * Log debug messages whenever InputChannel objects are created/destroyed.
45 * Enable this via "adb shell setprop log.tag.InputTransportLifecycle DEBUG"
46 * (requires restart)
47 */
48const bool DEBUG_CHANNEL_LIFECYCLE =
49 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Lifecycle", ANDROID_LOG_INFO);
50
51/**
52 * Log debug messages relating to the consumer end of the transport channel.
53 * Enable this via "adb shell setprop log.tag.InputTransportConsumer DEBUG" (requires restart)
54 */
55
56const bool DEBUG_TRANSPORT_CONSUMER =
57 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Consumer", ANDROID_LOG_INFO);
58
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +000059const bool IS_DEBUGGABLE_BUILD =
60#if defined(__ANDROID__)
61 android::base::GetBoolProperty("ro.debuggable", false);
62#else
63 true;
64#endif
65
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000066/**
67 * Log debug messages relating to the producer end of the transport channel.
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +000068 * Enable this via "adb shell setprop log.tag.InputTransportPublisher DEBUG".
69 * This requires a restart on non-debuggable (e.g. user) builds, but should take effect immediately
70 * on debuggable builds (e.g. userdebug).
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000071 */
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +000072bool debugTransportPublisher() {
73 if (!IS_DEBUGGABLE_BUILD) {
74 static const bool DEBUG_TRANSPORT_PUBLISHER =
75 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Publisher", ANDROID_LOG_INFO);
76 return DEBUG_TRANSPORT_PUBLISHER;
77 }
78 return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Publisher", ANDROID_LOG_INFO);
79}
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000080
81/**
82 * Log debug messages about touch event resampling.
Harry Cutts6c658cc2023-08-02 14:40:40 +000083 *
84 * Enable this via "adb shell setprop log.tag.InputTransportResampling DEBUG".
85 * This requires a restart on non-debuggable (e.g. user) builds, but should take effect immediately
86 * on debuggable builds (e.g. userdebug).
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000087 */
Harry Cutts6c658cc2023-08-02 14:40:40 +000088bool debugResampling() {
89 if (!IS_DEBUGGABLE_BUILD) {
90 static const bool DEBUG_TRANSPORT_RESAMPLING =
91 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling",
92 ANDROID_LOG_INFO);
93 return DEBUG_TRANSPORT_RESAMPLING;
94 }
95 return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", ANDROID_LOG_INFO);
96}
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000097
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080098android::base::unique_fd dupChannelFd(int fd) {
99 android::base::unique_fd newFd(::dup(fd));
100 if (!newFd.ok()) {
101 ALOGE("Could not duplicate fd %i : %s", fd, strerror(errno));
102 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
103 // If this process is out of file descriptors, then throwing that might end up exploding
104 // on the other side of a binder call, which isn't really helpful.
105 // Better to just crash here and hope that the FD leak is slow.
106 // Other failures could be client errors, so we still propagate those back to the caller.
107 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel");
108 return {};
109 }
110 return newFd;
111}
112
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000113} // namespace
114
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700115using android::base::Result;
Michael Wright3dd60e22019-03-27 22:06:44 +0000116using android::base::StringPrintf;
117
Jeff Brown5912f952013-07-01 19:10:31 -0700118namespace android {
119
120// Socket buffer size. The default is typically about 128KB, which is much larger than
121// we really need. So we make it smaller. It just needs to be big enough to hold
122// a few dozen large multi-finger motion events in the case where an application gets
123// behind processing touches.
124static const size_t SOCKET_BUFFER_SIZE = 32 * 1024;
125
126// Nanoseconds per milliseconds.
127static const nsecs_t NANOS_PER_MS = 1000000;
128
129// Latency added during resampling. A few milliseconds doesn't hurt much but
130// reduces the impact of mispredicted touch positions.
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800131const std::chrono::duration RESAMPLE_LATENCY = 5ms;
Jeff Brown5912f952013-07-01 19:10:31 -0700132
133// Minimum time difference between consecutive samples before attempting to resample.
134static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS;
135
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -0700136// Maximum time difference between consecutive samples before attempting to resample
137// by extrapolation.
138static const nsecs_t RESAMPLE_MAX_DELTA = 20 * NANOS_PER_MS;
139
Jeff Brown5912f952013-07-01 19:10:31 -0700140// Maximum time to predict forward from the last known state, to avoid predicting too
141// far into the future. This time is further bounded by 50% of the last time delta.
142static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS;
143
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600144/**
145 * System property for enabling / disabling touch resampling.
146 * Resampling extrapolates / interpolates the reported touch event coordinates to better
147 * align them to the VSYNC signal, thus resulting in smoother scrolling performance.
148 * Resampling is not needed (and should be disabled) on hardware that already
149 * has touch events triggered by VSYNC.
150 * Set to "1" to enable resampling (default).
151 * Set to "0" to disable resampling.
152 * Resampling is enabled by default.
153 */
154static const char* PROPERTY_RESAMPLING_ENABLED = "ro.input.resampling";
155
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800156/**
157 * Crash if the events that are getting sent to the InputPublisher are inconsistent.
158 * Enable this via "adb shell setprop log.tag.InputTransportVerifyEvents DEBUG"
159 */
160static bool verifyEvents() {
Siarhei Vishniakou96818962023-08-23 10:19:02 -0700161 return input_flags::enable_outbound_event_verification() ||
162 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "VerifyEvents", ANDROID_LOG_INFO);
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800163}
164
Jeff Brown5912f952013-07-01 19:10:31 -0700165template<typename T>
166inline static T min(const T& a, const T& b) {
167 return a < b ? a : b;
168}
169
170inline static float lerp(float a, float b, float alpha) {
171 return a + alpha * (b - a);
172}
173
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800174inline static bool isPointerEvent(int32_t source) {
175 return (source & AINPUT_SOURCE_CLASS_POINTER) == AINPUT_SOURCE_CLASS_POINTER;
176}
177
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800178inline static const char* toString(bool value) {
179 return value ? "true" : "false";
180}
181
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -0700182static bool shouldResampleTool(ToolType toolType) {
183 return toolType == ToolType::FINGER || toolType == ToolType::UNKNOWN;
184}
185
Jeff Brown5912f952013-07-01 19:10:31 -0700186// --- InputMessage ---
187
188bool InputMessage::isValid(size_t actualSize) const {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000189 if (size() != actualSize) {
190 ALOGE("Received message of incorrect size %zu (expected %zu)", actualSize, size());
191 return false;
192 }
193
194 switch (header.type) {
195 case Type::KEY:
196 return true;
197 case Type::MOTION: {
198 const bool valid =
199 body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
200 if (!valid) {
201 ALOGE("Received invalid MOTION: pointerCount = %" PRIu32, body.motion.pointerCount);
202 }
203 return valid;
204 }
205 case Type::FINISHED:
206 case Type::FOCUS:
207 case Type::CAPTURE:
208 case Type::DRAG:
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700209 case Type::TOUCH_MODE:
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000210 return true;
211 case Type::TIMELINE: {
212 const nsecs_t gpuCompletedTime =
213 body.timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
214 const nsecs_t presentTime =
215 body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
216 const bool valid = presentTime > gpuCompletedTime;
217 if (!valid) {
218 ALOGE("Received invalid TIMELINE: gpuCompletedTime = %" PRId64
219 " presentTime = %" PRId64,
220 gpuCompletedTime, presentTime);
221 }
222 return valid;
Jeff Brown5912f952013-07-01 19:10:31 -0700223 }
224 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000225 ALOGE("Invalid message type: %s", ftl::enum_string(header.type).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700226 return false;
227}
228
229size_t InputMessage::size() const {
230 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700231 case Type::KEY:
232 return sizeof(Header) + body.key.size();
233 case Type::MOTION:
234 return sizeof(Header) + body.motion.size();
235 case Type::FINISHED:
236 return sizeof(Header) + body.finished.size();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800237 case Type::FOCUS:
238 return sizeof(Header) + body.focus.size();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800239 case Type::CAPTURE:
240 return sizeof(Header) + body.capture.size();
arthurhung7632c332020-12-30 16:58:01 +0800241 case Type::DRAG:
242 return sizeof(Header) + body.drag.size();
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000243 case Type::TIMELINE:
244 return sizeof(Header) + body.timeline.size();
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700245 case Type::TOUCH_MODE:
246 return sizeof(Header) + body.touchMode.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700247 }
248 return sizeof(Header);
249}
250
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800251/**
252 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
253 * memory to zero, then only copy the valid bytes on a per-field basis.
254 */
255void InputMessage::getSanitizedCopy(InputMessage* msg) const {
256 memset(msg, 0, sizeof(*msg));
257
258 // Write the header
259 msg->header.type = header.type;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500260 msg->header.seq = header.seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800261
262 // Write the body
263 switch(header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700264 case InputMessage::Type::KEY: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800265 // int32_t eventId
266 msg->body.key.eventId = body.key.eventId;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800267 // nsecs_t eventTime
268 msg->body.key.eventTime = body.key.eventTime;
269 // int32_t deviceId
270 msg->body.key.deviceId = body.key.deviceId;
271 // int32_t source
272 msg->body.key.source = body.key.source;
273 // int32_t displayId
274 msg->body.key.displayId = body.key.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600275 // std::array<uint8_t, 32> hmac
276 msg->body.key.hmac = body.key.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800277 // int32_t action
278 msg->body.key.action = body.key.action;
279 // int32_t flags
280 msg->body.key.flags = body.key.flags;
281 // int32_t keyCode
282 msg->body.key.keyCode = body.key.keyCode;
283 // int32_t scanCode
284 msg->body.key.scanCode = body.key.scanCode;
285 // int32_t metaState
286 msg->body.key.metaState = body.key.metaState;
287 // int32_t repeatCount
288 msg->body.key.repeatCount = body.key.repeatCount;
289 // nsecs_t downTime
290 msg->body.key.downTime = body.key.downTime;
291 break;
292 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700293 case InputMessage::Type::MOTION: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800294 // int32_t eventId
295 msg->body.motion.eventId = body.motion.eventId;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700296 // uint32_t pointerCount
297 msg->body.motion.pointerCount = body.motion.pointerCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800298 // nsecs_t eventTime
299 msg->body.motion.eventTime = body.motion.eventTime;
300 // int32_t deviceId
301 msg->body.motion.deviceId = body.motion.deviceId;
302 // int32_t source
303 msg->body.motion.source = body.motion.source;
304 // int32_t displayId
305 msg->body.motion.displayId = body.motion.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600306 // std::array<uint8_t, 32> hmac
307 msg->body.motion.hmac = body.motion.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800308 // int32_t action
309 msg->body.motion.action = body.motion.action;
310 // int32_t actionButton
311 msg->body.motion.actionButton = body.motion.actionButton;
312 // int32_t flags
313 msg->body.motion.flags = body.motion.flags;
314 // int32_t metaState
315 msg->body.motion.metaState = body.motion.metaState;
316 // int32_t buttonState
317 msg->body.motion.buttonState = body.motion.buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800318 // MotionClassification classification
319 msg->body.motion.classification = body.motion.classification;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800320 // int32_t edgeFlags
321 msg->body.motion.edgeFlags = body.motion.edgeFlags;
322 // nsecs_t downTime
323 msg->body.motion.downTime = body.motion.downTime;
chaviw9eaa22c2020-07-01 16:21:27 -0700324
325 msg->body.motion.dsdx = body.motion.dsdx;
326 msg->body.motion.dtdx = body.motion.dtdx;
327 msg->body.motion.dtdy = body.motion.dtdy;
328 msg->body.motion.dsdy = body.motion.dsdy;
329 msg->body.motion.tx = body.motion.tx;
330 msg->body.motion.ty = body.motion.ty;
331
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800332 // float xPrecision
333 msg->body.motion.xPrecision = body.motion.xPrecision;
334 // float yPrecision
335 msg->body.motion.yPrecision = body.motion.yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700336 // float xCursorPosition
337 msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
338 // float yCursorPosition
339 msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700340
341 msg->body.motion.dsdxRaw = body.motion.dsdxRaw;
342 msg->body.motion.dtdxRaw = body.motion.dtdxRaw;
343 msg->body.motion.dtdyRaw = body.motion.dtdyRaw;
344 msg->body.motion.dsdyRaw = body.motion.dsdyRaw;
345 msg->body.motion.txRaw = body.motion.txRaw;
346 msg->body.motion.tyRaw = body.motion.tyRaw;
347
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800348 //struct Pointer pointers[MAX_POINTERS]
349 for (size_t i = 0; i < body.motion.pointerCount; i++) {
350 // PointerProperties properties
351 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
352 msg->body.motion.pointers[i].properties.toolType =
353 body.motion.pointers[i].properties.toolType,
354 // PointerCoords coords
355 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
356 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
357 memcpy(&msg->body.motion.pointers[i].coords.values[0],
358 &body.motion.pointers[i].coords.values[0],
359 count * (sizeof(body.motion.pointers[i].coords.values[0])));
Philip Quinnafb31282022-12-20 18:17:55 -0800360 msg->body.motion.pointers[i].coords.isResampled =
361 body.motion.pointers[i].coords.isResampled;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800362 }
363 break;
364 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700365 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800366 msg->body.finished.handled = body.finished.handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000367 msg->body.finished.consumeTime = body.finished.consumeTime;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800368 break;
369 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800370 case InputMessage::Type::FOCUS: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800371 msg->body.focus.eventId = body.focus.eventId;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800372 msg->body.focus.hasFocus = body.focus.hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800373 break;
374 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800375 case InputMessage::Type::CAPTURE: {
376 msg->body.capture.eventId = body.capture.eventId;
377 msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
378 break;
379 }
arthurhung7632c332020-12-30 16:58:01 +0800380 case InputMessage::Type::DRAG: {
381 msg->body.drag.eventId = body.drag.eventId;
382 msg->body.drag.x = body.drag.x;
383 msg->body.drag.y = body.drag.y;
384 msg->body.drag.isExiting = body.drag.isExiting;
385 break;
386 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000387 case InputMessage::Type::TIMELINE: {
388 msg->body.timeline.eventId = body.timeline.eventId;
389 msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
390 break;
391 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700392 case InputMessage::Type::TOUCH_MODE: {
393 msg->body.touchMode.eventId = body.touchMode.eventId;
394 msg->body.touchMode.isInTouchMode = body.touchMode.isInTouchMode;
395 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800396 }
397}
Jeff Brown5912f952013-07-01 19:10:31 -0700398
399// --- InputChannel ---
400
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500401std::unique_ptr<InputChannel> InputChannel::create(const std::string& name,
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500402 android::base::unique_fd fd, sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700403 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
404 if (result != 0) {
405 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
406 strerror(errno));
407 return nullptr;
408 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500409 // using 'new' to access a non-public constructor
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500410 return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700411}
412
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800413std::unique_ptr<InputChannel> InputChannel::create(
414 android::os::InputChannelCore&& parceledChannel) {
415 return InputChannel::create(parceledChannel.name, parceledChannel.fd.release(),
416 parceledChannel.token);
417}
418
419InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token) {
420 this->name = std::move(name);
421 this->fd.reset(std::move(fd));
422 this->token = std::move(token);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000423 ALOGD_IF(DEBUG_CHANNEL_LIFECYCLE, "Input channel constructed: name='%s', fd=%d",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800424 getName().c_str(), getFd());
Jeff Brown5912f952013-07-01 19:10:31 -0700425}
426
427InputChannel::~InputChannel() {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000428 ALOGD_IF(DEBUG_CHANNEL_LIFECYCLE, "Input channel destroyed: name='%s', fd=%d",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800429 getName().c_str(), getFd());
Robert Carr3720ed02018-08-08 16:08:27 -0700430}
431
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800432status_t InputChannel::openInputChannelPair(const std::string& name,
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500433 std::unique_ptr<InputChannel>& outServerChannel,
434 std::unique_ptr<InputChannel>& outClientChannel) {
Jeff Brown5912f952013-07-01 19:10:31 -0700435 int sockets[2];
436 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
437 status_t result = -errno;
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +0000438 ALOGE("channel '%s' ~ Could not create socket pair. errno=%s(%d)", name.c_str(),
439 strerror(errno), errno);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500440 outServerChannel.reset();
441 outClientChannel.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700442 return result;
443 }
444
445 int bufferSize = SOCKET_BUFFER_SIZE;
446 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
447 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
448 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
449 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
450
Siarhei Vishniakou4c155eb2023-06-30 11:47:12 -0700451 sp<IBinder> token = sp<BBinder>::make();
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700452
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700453 std::string serverChannelName = name + " (server)";
454 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700455 outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700456
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700457 std::string clientChannelName = name + " (client)";
458 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700459 outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700460 return OK;
461}
462
463status_t InputChannel::sendMessage(const InputMessage* msg) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000464 ATRACE_NAME_IF(ATRACE_ENABLED(),
465 StringPrintf("sendMessage(inputChannel=%s, seq=0x%" PRIx32 ", type=0x%" PRIx32
466 ")",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800467 name.c_str(), msg->header.seq, msg->header.type));
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800468 const size_t msgLength = msg->size();
469 InputMessage cleanMsg;
470 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700471 ssize_t nWrite;
472 do {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800473 nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700474 } while (nWrite == -1 && errno == EINTR);
475
476 if (nWrite < 0) {
477 int error = errno;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000478 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ error sending message of type %s, %s",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800479 name.c_str(), ftl::enum_string(msg->header.type).c_str(), strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700480 if (error == EAGAIN || error == EWOULDBLOCK) {
481 return WOULD_BLOCK;
482 }
483 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
484 return DEAD_OBJECT;
485 }
486 return -error;
487 }
488
489 if (size_t(nWrite) != msgLength) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000490 ALOGD_IF(DEBUG_CHANNEL_MESSAGES,
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800491 "channel '%s' ~ error sending message type %s, send was incomplete", name.c_str(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000492 ftl::enum_string(msg->header.type).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700493 return DEAD_OBJECT;
494 }
495
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800496 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ sent message of type %s", name.c_str(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000497 ftl::enum_string(msg->header.type).c_str());
Zimd8402b62023-06-02 11:56:26 +0100498
Jeff Brown5912f952013-07-01 19:10:31 -0700499 return OK;
500}
501
502status_t InputChannel::receiveMessage(InputMessage* msg) {
503 ssize_t nRead;
504 do {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800505 nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700506 } while (nRead == -1 && errno == EINTR);
507
508 if (nRead < 0) {
509 int error = errno;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000510 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ receive message failed, errno=%d",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800511 name.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700512 if (error == EAGAIN || error == EWOULDBLOCK) {
513 return WOULD_BLOCK;
514 }
515 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
516 return DEAD_OBJECT;
517 }
518 return -error;
519 }
520
521 if (nRead == 0) { // check for EOF
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000522 ALOGD_IF(DEBUG_CHANNEL_MESSAGES,
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800523 "channel '%s' ~ receive message failed because peer was closed", name.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700524 return DEAD_OBJECT;
525 }
526
527 if (!msg->isValid(nRead)) {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800528 ALOGE("channel '%s' ~ received invalid message of size %zd", name.c_str(), nRead);
Jeff Brown5912f952013-07-01 19:10:31 -0700529 return BAD_VALUE;
530 }
531
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800532 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ received message of type %s", name.c_str(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000533 ftl::enum_string(msg->header.type).c_str());
Zimd8402b62023-06-02 11:56:26 +0100534 if (ATRACE_ENABLED()) {
Prabir Pradhana37bad12023-08-18 15:55:32 +0000535 // Add an additional trace point to include data about the received message.
Zimd8402b62023-06-02 11:56:26 +0100536 std::string message = StringPrintf("receiveMessage(inputChannel=%s, seq=0x%" PRIx32
537 ", type=0x%" PRIx32 ")",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800538 name.c_str(), msg->header.seq, msg->header.type);
Zimd8402b62023-06-02 11:56:26 +0100539 ATRACE_NAME(message.c_str());
540 }
Jeff Brown5912f952013-07-01 19:10:31 -0700541 return OK;
542}
543
Egor Paskoa0d32af2023-12-14 17:45:41 +0100544bool InputChannel::probablyHasInput() const {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800545 struct pollfd pfds = {.fd = fd.get(), .events = POLLIN};
Egor Paskoa0d32af2023-12-14 17:45:41 +0100546 if (::poll(&pfds, /*nfds=*/1, /*timeout=*/0) <= 0) {
Egor Pasko5a67a562024-01-16 16:46:45 +0100547 // This can be a false negative because EINTR and ENOMEM are not handled. The latter should
548 // be extremely rare. The EINTR is also unlikely because it happens only when the signal
549 // arrives while the syscall is executed, and the syscall is quick. Hitting EINTR too often
Egor Paskoa0d32af2023-12-14 17:45:41 +0100550 // would be a sign of having too many signals, which is a bigger performance problem. A
Egor Pasko5a67a562024-01-16 16:46:45 +0100551 // common tradition is to repeat the syscall on each EINTR, but it is not necessary here.
Egor Paskoa0d32af2023-12-14 17:45:41 +0100552 // In other words, the missing one liner is replaced by a multiline explanation.
553 return false;
554 }
555 // From poll(2): The bits returned in |revents| can include any of those specified in |events|,
556 // or one of the values POLLERR, POLLHUP, or POLLNVAL.
557 return (pfds.revents & POLLIN) != 0;
558}
559
Egor Pasko5a67a562024-01-16 16:46:45 +0100560void InputChannel::waitForMessage(std::chrono::milliseconds timeout) const {
561 if (timeout < 0ms) {
562 LOG(FATAL) << "Timeout cannot be negative, received " << timeout.count();
563 }
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800564 struct pollfd pfds = {.fd = fd.get(), .events = POLLIN};
Egor Pasko5a67a562024-01-16 16:46:45 +0100565 int ret;
566 std::chrono::time_point<std::chrono::steady_clock> stopTime =
567 std::chrono::steady_clock::now() + timeout;
568 std::chrono::milliseconds remaining = timeout;
569 do {
570 ret = ::poll(&pfds, /*nfds=*/1, /*timeout=*/remaining.count());
571 remaining = std::chrono::duration_cast<std::chrono::milliseconds>(
572 stopTime - std::chrono::steady_clock::now());
573 } while (ret == -1 && errno == EINTR && remaining > 0ms);
574}
575
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500576std::unique_ptr<InputChannel> InputChannel::dup() const {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800577 base::unique_fd newFd(dupChannelFd(fd.get()));
Chris Ye0783e992020-06-02 21:34:49 -0700578 return InputChannel::create(getName(), std::move(newFd), getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -0700579}
580
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800581void InputChannel::copyTo(android::os::InputChannelCore& outChannel) const {
582 outChannel.name = getName();
583 outChannel.fd.reset(dupChannelFd(fd.get()));
584 outChannel.token = getConnectionToken();
Robert Carr3720ed02018-08-08 16:08:27 -0700585}
586
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700587sp<IBinder> InputChannel::getConnectionToken() const {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800588 return token;
Garfield Tan15601662020-09-22 15:32:38 -0700589}
590
Jeff Brown5912f952013-07-01 19:10:31 -0700591// --- InputPublisher ---
592
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800593InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel)
594 : mChannel(channel), mInputVerifier(channel->getName()) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700595
596InputPublisher::~InputPublisher() {
597}
598
Garfield Tan1c7bc862020-01-28 13:24:04 -0800599status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
600 int32_t source, int32_t displayId,
601 std::array<uint8_t, 32> hmac, int32_t action,
602 int32_t flags, int32_t keyCode, int32_t scanCode,
603 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
604 nsecs_t eventTime) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000605 ATRACE_NAME_IF(ATRACE_ENABLED(),
606 StringPrintf("publishKeyEvent(inputChannel=%s, action=%s, keyCode=%s)",
607 mChannel->getName().c_str(), KeyEvent::actionToString(action),
608 KeyEvent::getLabel(keyCode)));
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000609 ALOGD_IF(debugTransportPublisher(),
Prabir Pradhan96282b02023-02-24 22:36:17 +0000610 "channel '%s' publisher ~ %s: seq=%u, id=%d, deviceId=%d, source=%s, "
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000611 "action=%s, flags=0x%x, keyCode=%s, scanCode=%d, metaState=0x%x, repeatCount=%d,"
612 "downTime=%" PRId64 ", eventTime=%" PRId64,
Prabir Pradhan96282b02023-02-24 22:36:17 +0000613 mChannel->getName().c_str(), __func__, seq, eventId, deviceId,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000614 inputEventSourceToString(source).c_str(), KeyEvent::actionToString(action), flags,
615 KeyEvent::getLabel(keyCode), scanCode, metaState, repeatCount, downTime, eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700616
617 if (!seq) {
618 ALOGE("Attempted to publish a key event with sequence number 0.");
619 return BAD_VALUE;
620 }
621
622 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700623 msg.header.type = InputMessage::Type::KEY;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500624 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800625 msg.body.key.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700626 msg.body.key.deviceId = deviceId;
627 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100628 msg.body.key.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700629 msg.body.key.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700630 msg.body.key.action = action;
631 msg.body.key.flags = flags;
632 msg.body.key.keyCode = keyCode;
633 msg.body.key.scanCode = scanCode;
634 msg.body.key.metaState = metaState;
635 msg.body.key.repeatCount = repeatCount;
636 msg.body.key.downTime = downTime;
637 msg.body.key.eventTime = eventTime;
638 return mChannel->sendMessage(&msg);
639}
640
641status_t InputPublisher::publishMotionEvent(
Garfield Tan1c7bc862020-01-28 13:24:04 -0800642 uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600643 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
644 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700645 MotionClassification classification, const ui::Transform& transform, float xPrecision,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700646 float yPrecision, float xCursorPosition, float yCursorPosition,
647 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700648 uint32_t pointerCount, const PointerProperties* pointerProperties,
649 const PointerCoords* pointerCoords) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000650 ATRACE_NAME_IF(ATRACE_ENABLED(),
651 StringPrintf("publishMotionEvent(inputChannel=%s, action=%s)",
652 mChannel->getName().c_str(),
653 MotionEvent::actionToString(action).c_str()));
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800654 if (verifyEvents()) {
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700655 Result<void> result =
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -0700656 mInputVerifier.processMovement(deviceId, source, action, pointerCount,
657 pointerProperties, pointerCoords, flags);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700658 if (!result.ok()) {
659 LOG(FATAL) << "Bad stream: " << result.error();
660 }
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800661 }
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000662 if (debugTransportPublisher()) {
chaviw9eaa22c2020-07-01 16:21:27 -0700663 std::string transformString;
chaviw85b44202020-07-24 11:46:21 -0700664 transform.dump(transformString, "transform", " ");
Prabir Pradhan96282b02023-02-24 22:36:17 +0000665 ALOGD("channel '%s' publisher ~ %s: seq=%u, id=%d, deviceId=%d, source=%s, "
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800666 "displayId=%" PRId32 ", "
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000667 "action=%s, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
chaviw9eaa22c2020-07-01 16:21:27 -0700668 "metaState=0x%x, buttonState=0x%x, classification=%s,"
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800669 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700670 "pointerCount=%" PRIu32 "\n%s",
Prabir Pradhan96282b02023-02-24 22:36:17 +0000671 mChannel->getName().c_str(), __func__, seq, eventId, deviceId,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000672 inputEventSourceToString(source).c_str(), displayId,
673 MotionEvent::actionToString(action).c_str(), actionButton, flags, edgeFlags,
674 metaState, buttonState, motionClassificationToString(classification), xPrecision,
675 yPrecision, downTime, eventTime, pointerCount, transformString.c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800676 }
Jeff Brown5912f952013-07-01 19:10:31 -0700677
678 if (!seq) {
679 ALOGE("Attempted to publish a motion event with sequence number 0.");
680 return BAD_VALUE;
681 }
682
683 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700684 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800685 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700686 return BAD_VALUE;
687 }
688
689 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700690 msg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500691 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800692 msg.body.motion.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700693 msg.body.motion.deviceId = deviceId;
694 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700695 msg.body.motion.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700696 msg.body.motion.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700697 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100698 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700699 msg.body.motion.flags = flags;
700 msg.body.motion.edgeFlags = edgeFlags;
701 msg.body.motion.metaState = metaState;
702 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800703 msg.body.motion.classification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700704 msg.body.motion.dsdx = transform.dsdx();
705 msg.body.motion.dtdx = transform.dtdx();
706 msg.body.motion.dtdy = transform.dtdy();
707 msg.body.motion.dsdy = transform.dsdy();
708 msg.body.motion.tx = transform.tx();
709 msg.body.motion.ty = transform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700710 msg.body.motion.xPrecision = xPrecision;
711 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700712 msg.body.motion.xCursorPosition = xCursorPosition;
713 msg.body.motion.yCursorPosition = yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700714 msg.body.motion.dsdxRaw = rawTransform.dsdx();
715 msg.body.motion.dtdxRaw = rawTransform.dtdx();
716 msg.body.motion.dtdyRaw = rawTransform.dtdy();
717 msg.body.motion.dsdyRaw = rawTransform.dsdy();
718 msg.body.motion.txRaw = rawTransform.tx();
719 msg.body.motion.tyRaw = rawTransform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700720 msg.body.motion.downTime = downTime;
721 msg.body.motion.eventTime = eventTime;
722 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100723 for (uint32_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -0700724 msg.body.motion.pointers[i].properties = pointerProperties[i];
725 msg.body.motion.pointers[i].coords = pointerCoords[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700726 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700727
Jeff Brown5912f952013-07-01 19:10:31 -0700728 return mChannel->sendMessage(&msg);
729}
730
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700731status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000732 ATRACE_NAME_IF(ATRACE_ENABLED(),
733 StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)",
734 mChannel->getName().c_str(), toString(hasFocus)));
Prabir Pradhan96282b02023-02-24 22:36:17 +0000735 ALOGD_IF(debugTransportPublisher(), "channel '%s' publisher ~ %s: seq=%u, id=%d, hasFocus=%s",
736 mChannel->getName().c_str(), __func__, seq, eventId, toString(hasFocus));
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800737
738 InputMessage msg;
739 msg.header.type = InputMessage::Type::FOCUS;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500740 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800741 msg.body.focus.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000742 msg.body.focus.hasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800743 return mChannel->sendMessage(&msg);
744}
745
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800746status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
747 bool pointerCaptureEnabled) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000748 ATRACE_NAME_IF(ATRACE_ENABLED(),
749 StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
750 mChannel->getName().c_str(), toString(pointerCaptureEnabled)));
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000751 ALOGD_IF(debugTransportPublisher(),
Prabir Pradhan96282b02023-02-24 22:36:17 +0000752 "channel '%s' publisher ~ %s: seq=%u, id=%d, pointerCaptureEnabled=%s",
753 mChannel->getName().c_str(), __func__, seq, eventId, toString(pointerCaptureEnabled));
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800754
755 InputMessage msg;
756 msg.header.type = InputMessage::Type::CAPTURE;
757 msg.header.seq = seq;
758 msg.body.capture.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000759 msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800760 return mChannel->sendMessage(&msg);
761}
762
arthurhung7632c332020-12-30 16:58:01 +0800763status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
764 bool isExiting) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000765 ATRACE_NAME_IF(ATRACE_ENABLED(),
766 StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
767 mChannel->getName().c_str(), x, y, toString(isExiting)));
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000768 ALOGD_IF(debugTransportPublisher(),
Prabir Pradhan96282b02023-02-24 22:36:17 +0000769 "channel '%s' publisher ~ %s: seq=%u, id=%d, x=%f, y=%f, isExiting=%s",
770 mChannel->getName().c_str(), __func__, seq, eventId, x, y, toString(isExiting));
arthurhung7632c332020-12-30 16:58:01 +0800771
772 InputMessage msg;
773 msg.header.type = InputMessage::Type::DRAG;
774 msg.header.seq = seq;
775 msg.body.drag.eventId = eventId;
776 msg.body.drag.isExiting = isExiting;
777 msg.body.drag.x = x;
778 msg.body.drag.y = y;
779 return mChannel->sendMessage(&msg);
780}
781
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700782status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000783 ATRACE_NAME_IF(ATRACE_ENABLED(),
784 StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
785 mChannel->getName().c_str(), toString(isInTouchMode)));
Prabir Pradhan96282b02023-02-24 22:36:17 +0000786 ALOGD_IF(debugTransportPublisher(),
787 "channel '%s' publisher ~ %s: seq=%u, id=%d, isInTouchMode=%s",
788 mChannel->getName().c_str(), __func__, seq, eventId, toString(isInTouchMode));
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700789
790 InputMessage msg;
791 msg.header.type = InputMessage::Type::TOUCH_MODE;
792 msg.header.seq = seq;
793 msg.body.touchMode.eventId = eventId;
794 msg.body.touchMode.isInTouchMode = isInTouchMode;
795 return mChannel->sendMessage(&msg);
796}
797
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000798android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
Jeff Brown5912f952013-07-01 19:10:31 -0700799 InputMessage msg;
800 status_t result = mChannel->receiveMessage(&msg);
801 if (result) {
Siarhei Vishniakou69112652023-08-24 08:34:18 -0700802 if (debugTransportPublisher() && result != WOULD_BLOCK) {
803 LOG(INFO) << "channel '" << mChannel->getName() << "' publisher ~ " << __func__ << ": "
804 << strerror(result);
805 }
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000806 return android::base::Error(result);
Jeff Brown5912f952013-07-01 19:10:31 -0700807 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000808 if (msg.header.type == InputMessage::Type::FINISHED) {
Prabir Pradhan96282b02023-02-24 22:36:17 +0000809 ALOGD_IF(debugTransportPublisher(),
810 "channel '%s' publisher ~ %s: finished: seq=%u, handled=%s",
811 mChannel->getName().c_str(), __func__, msg.header.seq,
812 toString(msg.body.finished.handled));
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000813 return Finished{
814 .seq = msg.header.seq,
815 .handled = msg.body.finished.handled,
816 .consumeTime = msg.body.finished.consumeTime,
817 };
Jeff Brown5912f952013-07-01 19:10:31 -0700818 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000819
820 if (msg.header.type == InputMessage::Type::TIMELINE) {
Prabir Pradhan96282b02023-02-24 22:36:17 +0000821 ALOGD_IF(debugTransportPublisher(), "channel '%s' publisher ~ %s: timeline: id=%d",
822 mChannel->getName().c_str(), __func__, msg.body.timeline.eventId);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000823 return Timeline{
824 .inputEventId = msg.body.timeline.eventId,
825 .graphicsTimeline = msg.body.timeline.graphicsTimeline,
826 };
827 }
828
829 ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer",
Dominik Laskowski75788452021-02-09 18:51:25 -0800830 mChannel->getName().c_str(), ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000831 return android::base::Error(UNKNOWN_ERROR);
Jeff Brown5912f952013-07-01 19:10:31 -0700832}
833
834// --- InputConsumer ---
835
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500836InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel)
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800837 : InputConsumer(channel, isTouchResamplingEnabled()) {}
838
839InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel,
840 bool enableTouchResampling)
841 : mResampleTouch(enableTouchResampling), mChannel(channel), mMsgDeferred(false) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700842
843InputConsumer::~InputConsumer() {
844}
845
846bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600847 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700848}
849
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800850status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
851 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000852 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
853 "channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
854 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700855
856 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700857 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700858
859 // Fetch the next input message.
860 // Loop until an event can be returned or no additional events are received.
861 while (!*outEvent) {
862 if (mMsgDeferred) {
863 // mMsg contains a valid input message from the previous call to consume
864 // that has not yet been processed.
865 mMsgDeferred = false;
866 } else {
867 // Receive a fresh message.
868 status_t result = mChannel->receiveMessage(&mMsg);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000869 if (result == OK) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800870 const auto [_, inserted] =
871 mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
872 LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
873 mMsg.header.seq);
Siarhei Vishniakouca42e0d2024-01-12 12:10:32 -0800874
875 // Trace the event processing timeline - event was just read from the socket
876 ATRACE_ASYNC_BEGIN("InputConsumer processing", /*cookie=*/mMsg.header.seq);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000877 }
Jeff Brown5912f952013-07-01 19:10:31 -0700878 if (result) {
879 // Consume the next batched event unless batches are being held for later.
880 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800881 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700882 if (*outEvent) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000883 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
884 "channel '%s' consumer ~ consumed batch event, seq=%u",
885 mChannel->getName().c_str(), *outSeq);
Jeff Brown5912f952013-07-01 19:10:31 -0700886 break;
887 }
888 }
889 return result;
890 }
891 }
892
893 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700894 case InputMessage::Type::KEY: {
895 KeyEvent* keyEvent = factory->createKeyEvent();
896 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700897
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700898 initializeKeyEvent(keyEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500899 *outSeq = mMsg.header.seq;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700900 *outEvent = keyEvent;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000901 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
902 "channel '%s' consumer ~ consumed key event, seq=%u",
903 mChannel->getName().c_str(), *outSeq);
904 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700905 }
Jeff Brown5912f952013-07-01 19:10:31 -0700906
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700907 case InputMessage::Type::MOTION: {
908 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
909 if (batchIndex >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500910 Batch& batch = mBatches[batchIndex];
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700911 if (canAddSample(batch, &mMsg)) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500912 batch.samples.push_back(mMsg);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000913 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
914 "channel '%s' consumer ~ appended to batch event",
915 mChannel->getName().c_str());
916 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700917 } else if (isPointerEvent(mMsg.body.motion.source) &&
918 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
919 // No need to process events that we are going to cancel anyways
920 const size_t count = batch.samples.size();
921 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500922 const InputMessage& msg = batch.samples[i];
923 sendFinishedSignal(msg.header.seq, false);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700924 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500925 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
926 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700927 } else {
928 // We cannot append to the batch in progress, so we need to consume
929 // the previous batch right now and defer the new message until later.
930 mMsgDeferred = true;
931 status_t result = consumeSamples(factory, batch, batch.samples.size(),
932 outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500933 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700934 if (result) {
935 return result;
936 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000937 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
938 "channel '%s' consumer ~ consumed batch event and "
939 "deferred current event, seq=%u",
940 mChannel->getName().c_str(), *outSeq);
941 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700942 }
Jeff Brown5912f952013-07-01 19:10:31 -0700943 }
Jeff Brown5912f952013-07-01 19:10:31 -0700944
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800945 // Start a new batch if needed.
946 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
947 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500948 Batch batch;
949 batch.samples.push_back(mMsg);
950 mBatches.push_back(batch);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000951 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
952 "channel '%s' consumer ~ started batch event",
953 mChannel->getName().c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800954 break;
955 }
Jeff Brown5912f952013-07-01 19:10:31 -0700956
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800957 MotionEvent* motionEvent = factory->createMotionEvent();
958 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700959
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800960 updateTouchState(mMsg);
961 initializeMotionEvent(motionEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500962 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800963 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800964
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000965 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
966 "channel '%s' consumer ~ consumed motion event, seq=%u",
967 mChannel->getName().c_str(), *outSeq);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800968 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700969 }
Jeff Brown5912f952013-07-01 19:10:31 -0700970
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000971 case InputMessage::Type::FINISHED:
972 case InputMessage::Type::TIMELINE: {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +0000973 LOG_ALWAYS_FATAL("Consumed a %s message, which should never be seen by "
974 "InputConsumer!",
Dominik Laskowski75788452021-02-09 18:51:25 -0800975 ftl::enum_string(mMsg.header.type).c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800976 break;
977 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800978
979 case InputMessage::Type::FOCUS: {
980 FocusEvent* focusEvent = factory->createFocusEvent();
981 if (!focusEvent) return NO_MEMORY;
982
983 initializeFocusEvent(focusEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500984 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800985 *outEvent = focusEvent;
986 break;
987 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800988
989 case InputMessage::Type::CAPTURE: {
990 CaptureEvent* captureEvent = factory->createCaptureEvent();
991 if (!captureEvent) return NO_MEMORY;
992
993 initializeCaptureEvent(captureEvent, &mMsg);
994 *outSeq = mMsg.header.seq;
995 *outEvent = captureEvent;
996 break;
997 }
arthurhung7632c332020-12-30 16:58:01 +0800998
999 case InputMessage::Type::DRAG: {
1000 DragEvent* dragEvent = factory->createDragEvent();
1001 if (!dragEvent) return NO_MEMORY;
1002
1003 initializeDragEvent(dragEvent, &mMsg);
1004 *outSeq = mMsg.header.seq;
1005 *outEvent = dragEvent;
1006 break;
1007 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001008
1009 case InputMessage::Type::TOUCH_MODE: {
1010 TouchModeEvent* touchModeEvent = factory->createTouchModeEvent();
1011 if (!touchModeEvent) return NO_MEMORY;
1012
1013 initializeTouchModeEvent(touchModeEvent, &mMsg);
1014 *outSeq = mMsg.header.seq;
1015 *outEvent = touchModeEvent;
1016 break;
1017 }
Jeff Brown5912f952013-07-01 19:10:31 -07001018 }
1019 }
1020 return OK;
1021}
1022
1023status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -08001024 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -07001025 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -07001026 for (size_t i = mBatches.size(); i > 0; ) {
1027 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001028 Batch& batch = mBatches[i];
Michael Wright32232172013-10-21 12:05:22 -07001029 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -08001030 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001031 mBatches.erase(mBatches.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001032 return result;
1033 }
1034
Michael Wright32232172013-10-21 12:05:22 -07001035 nsecs_t sampleTime = frameTime;
1036 if (mResampleTouch) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -08001037 sampleTime -= std::chrono::nanoseconds(RESAMPLE_LATENCY).count();
Michael Wright32232172013-10-21 12:05:22 -07001038 }
Jeff Brown5912f952013-07-01 19:10:31 -07001039 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
1040 if (split < 0) {
1041 continue;
1042 }
1043
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -08001044 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -07001045 const InputMessage* next;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001046 if (batch.samples.empty()) {
1047 mBatches.erase(mBatches.begin() + i);
Yi Kong5bed83b2018-07-17 12:53:47 -07001048 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -07001049 } else {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001050 next = &batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001051 }
Michael Wright32232172013-10-21 12:05:22 -07001052 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -07001053 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
1054 }
1055 return result;
1056 }
1057
1058 return WOULD_BLOCK;
1059}
1060
1061status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -08001062 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -07001063 MotionEvent* motionEvent = factory->createMotionEvent();
1064 if (! motionEvent) return NO_MEMORY;
1065
1066 uint32_t chain = 0;
1067 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001068 InputMessage& msg = batch.samples[i];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001069 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -07001070 if (i) {
1071 SeqChain seqChain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001072 seqChain.seq = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -07001073 seqChain.chain = chain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001074 mSeqChains.push_back(seqChain);
Jeff Brown5912f952013-07-01 19:10:31 -07001075 addSample(motionEvent, &msg);
1076 } else {
1077 initializeMotionEvent(motionEvent, &msg);
1078 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001079 chain = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -07001080 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001081 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
Jeff Brown5912f952013-07-01 19:10:31 -07001082
1083 *outSeq = chain;
1084 *outEvent = motionEvent;
1085 return OK;
1086}
1087
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001088void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001089 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -07001090 return;
1091 }
1092
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001093 int32_t deviceId = msg.body.motion.deviceId;
1094 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -07001095
1096 // Update the touch state history to incorporate the new input message.
1097 // If the message is in the past relative to the most recently produced resampled
1098 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001099 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -07001100 case AMOTION_EVENT_ACTION_DOWN: {
1101 ssize_t index = findTouchState(deviceId, source);
1102 if (index < 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001103 mTouchStates.push_back({});
Jeff Brown5912f952013-07-01 19:10:31 -07001104 index = mTouchStates.size() - 1;
1105 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001106 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001107 touchState.initialize(deviceId, source);
1108 touchState.addHistory(msg);
1109 break;
1110 }
1111
1112 case AMOTION_EVENT_ACTION_MOVE: {
1113 ssize_t index = findTouchState(deviceId, source);
1114 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001115 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001116 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001117 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -07001118 }
1119 break;
1120 }
1121
1122 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
1123 ssize_t index = findTouchState(deviceId, source);
1124 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001125 TouchState& touchState = mTouchStates[index];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001126 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001127 rewriteMessage(touchState, msg);
1128 }
1129 break;
1130 }
1131
1132 case AMOTION_EVENT_ACTION_POINTER_UP: {
1133 ssize_t index = findTouchState(deviceId, source);
1134 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001135 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001136 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001137 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001138 }
1139 break;
1140 }
1141
1142 case AMOTION_EVENT_ACTION_SCROLL: {
1143 ssize_t index = findTouchState(deviceId, source);
1144 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001145 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001146 rewriteMessage(touchState, msg);
1147 }
1148 break;
1149 }
1150
1151 case AMOTION_EVENT_ACTION_UP:
1152 case AMOTION_EVENT_ACTION_CANCEL: {
1153 ssize_t index = findTouchState(deviceId, source);
1154 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001155 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001156 rewriteMessage(touchState, msg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001157 mTouchStates.erase(mTouchStates.begin() + index);
Jeff Brown5912f952013-07-01 19:10:31 -07001158 }
1159 break;
1160 }
1161 }
1162}
1163
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001164/**
1165 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
1166 *
1167 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
1168 * is in the past relative to msg and the past two events do not contain identical coordinates),
1169 * then invalidate the lastResample data for that pointer.
1170 * If the two past events have identical coordinates, then lastResample data for that pointer will
1171 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
1172 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
1173 * not equal to x0 is received.
1174 */
1175void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001176 nsecs_t eventTime = msg.body.motion.eventTime;
1177 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1178 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -07001179 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001180 if (eventTime < state.lastResample.eventTime ||
1181 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001182 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
1183 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Harry Cutts6c658cc2023-08-02 14:40:40 +00001184 ALOGD_IF(debugResampling(), "[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001185 resampleCoords.getX(), resampleCoords.getY(), msgCoords.getX(),
1186 msgCoords.getY());
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001187 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
1188 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
Philip Quinnafb31282022-12-20 18:17:55 -08001189 msgCoords.isResampled = true;
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001190 } else {
1191 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001192 }
Jeff Brown5912f952013-07-01 19:10:31 -07001193 }
1194 }
1195}
1196
1197void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
1198 const InputMessage* next) {
1199 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001200 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -07001201 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
1202 return;
1203 }
1204
1205 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
1206 if (index < 0) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001207 ALOGD_IF(debugResampling(), "Not resampled, no touch state for device.");
Jeff Brown5912f952013-07-01 19:10:31 -07001208 return;
1209 }
1210
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001211 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001212 if (touchState.historySize < 1) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001213 ALOGD_IF(debugResampling(), "Not resampled, no history for device.");
Jeff Brown5912f952013-07-01 19:10:31 -07001214 return;
1215 }
1216
1217 // Ensure that the current sample has all of the pointers that need to be reported.
1218 const History* current = touchState.getHistory(0);
1219 size_t pointerCount = event->getPointerCount();
1220 for (size_t i = 0; i < pointerCount; i++) {
1221 uint32_t id = event->getPointerId(i);
1222 if (!current->idBits.hasBit(id)) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001223 ALOGD_IF(debugResampling(), "Not resampled, missing id %d", id);
Jeff Brown5912f952013-07-01 19:10:31 -07001224 return;
1225 }
1226 }
1227
1228 // Find the data to use for resampling.
1229 const History* other;
1230 History future;
1231 float alpha;
1232 if (next) {
1233 // Interpolate between current sample and future sample.
1234 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001235 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -07001236 other = &future;
1237 nsecs_t delta = future.eventTime - current->eventTime;
1238 if (delta < RESAMPLE_MIN_DELTA) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001239 ALOGD_IF(debugResampling(), "Not resampled, delta time is too small: %" PRId64 " ns.",
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001240 delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001241 return;
1242 }
1243 alpha = float(sampleTime - current->eventTime) / delta;
1244 } else if (touchState.historySize >= 2) {
1245 // Extrapolate future sample using current sample and past sample.
1246 // So other->eventTime <= current->eventTime <= sampleTime.
1247 other = touchState.getHistory(1);
1248 nsecs_t delta = current->eventTime - other->eventTime;
1249 if (delta < RESAMPLE_MIN_DELTA) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001250 ALOGD_IF(debugResampling(), "Not resampled, delta time is too small: %" PRId64 " ns.",
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001251 delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -07001252 return;
1253 } else if (delta > RESAMPLE_MAX_DELTA) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001254 ALOGD_IF(debugResampling(), "Not resampled, delta time is too large: %" PRId64 " ns.",
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001255 delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001256 return;
1257 }
1258 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
1259 if (sampleTime > maxPredict) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001260 ALOGD_IF(debugResampling(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001261 "Sample time is too far in the future, adjusting prediction "
1262 "from %" PRId64 " to %" PRId64 " ns.",
1263 sampleTime - current->eventTime, maxPredict - current->eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001264 sampleTime = maxPredict;
1265 }
1266 alpha = float(current->eventTime - sampleTime) / delta;
1267 } else {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001268 ALOGD_IF(debugResampling(), "Not resampled, insufficient data.");
Jeff Brown5912f952013-07-01 19:10:31 -07001269 return;
1270 }
1271
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -08001272 if (current->eventTime == sampleTime) {
1273 // Prevents having 2 events with identical times and coordinates.
1274 return;
1275 }
1276
Jeff Brown5912f952013-07-01 19:10:31 -07001277 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001278 History oldLastResample;
1279 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001280 touchState.lastResample.eventTime = sampleTime;
1281 touchState.lastResample.idBits.clear();
1282 for (size_t i = 0; i < pointerCount; i++) {
1283 uint32_t id = event->getPointerId(i);
1284 touchState.lastResample.idToIndex[id] = i;
1285 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001286 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1287 // We maintain the previously resampled value for this pointer (stored in
1288 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1289 // This way we don't introduce artificial jitter when pointers haven't actually moved.
Philip Quinnafb31282022-12-20 18:17:55 -08001290 // The isResampled flag isn't cleared as the values don't reflect what the device is
1291 // actually reporting.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001292
1293 // We know here that the coordinates for the pointer haven't changed because we
1294 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1295 // lastResample in place becasue the mapping from pointer ID to index may have changed.
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -07001296 touchState.lastResample.pointers[i] = oldLastResample.getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001297 continue;
1298 }
1299
Jeff Brown5912f952013-07-01 19:10:31 -07001300 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1301 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -07001302 resampledCoords = currentCoords;
Philip Quinn4e955a22023-09-26 12:09:40 -07001303 resampledCoords.isResampled = true;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001304 if (other->idBits.hasBit(id) && shouldResampleTool(event->getToolType(i))) {
Jeff Brown5912f952013-07-01 19:10:31 -07001305 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001306 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001307 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
Jeff Brown5912f952013-07-01 19:10:31 -07001308 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001309 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
Harry Cutts6c658cc2023-08-02 14:40:40 +00001310 ALOGD_IF(debugResampling(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001311 "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1312 "other (%0.3f, %0.3f), alpha %0.3f",
1313 id, resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(),
1314 currentCoords.getY(), otherCoords.getX(), otherCoords.getY(), alpha);
Jeff Brown5912f952013-07-01 19:10:31 -07001315 } else {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001316 ALOGD_IF(debugResampling(), "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", id,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001317 resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(),
1318 currentCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -07001319 }
1320 }
1321
1322 event->addSample(sampleTime, touchState.lastResample.pointers);
1323}
1324
Jeff Brown5912f952013-07-01 19:10:31 -07001325status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001326 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
1327 "channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1328 mChannel->getName().c_str(), seq, toString(handled));
Jeff Brown5912f952013-07-01 19:10:31 -07001329
1330 if (!seq) {
1331 ALOGE("Attempted to send a finished signal with sequence number 0.");
1332 return BAD_VALUE;
1333 }
1334
1335 // Send finished signals for the batch sequence chain first.
1336 size_t seqChainCount = mSeqChains.size();
1337 if (seqChainCount) {
1338 uint32_t currentSeq = seq;
1339 uint32_t chainSeqs[seqChainCount];
1340 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001341 for (size_t i = seqChainCount; i > 0; ) {
1342 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001343 const SeqChain& seqChain = mSeqChains[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001344 if (seqChain.seq == currentSeq) {
1345 currentSeq = seqChain.chain;
1346 chainSeqs[chainIndex++] = currentSeq;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001347 mSeqChains.erase(mSeqChains.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001348 }
1349 }
1350 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001351 while (!status && chainIndex > 0) {
1352 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001353 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1354 }
1355 if (status) {
1356 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001357 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001358 SeqChain seqChain;
1359 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1360 seqChain.chain = chainSeqs[chainIndex];
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001361 mSeqChains.push_back(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001362 if (!chainIndex) break;
1363 chainIndex--;
1364 }
Jeff Brown5912f952013-07-01 19:10:31 -07001365 return status;
1366 }
1367 }
1368
1369 // Send finished signal for the last message in the batch.
1370 return sendUnchainedFinishedSignal(seq, handled);
1371}
1372
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001373status_t InputConsumer::sendTimeline(int32_t inputEventId,
1374 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001375 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
1376 "channel '%s' consumer ~ sendTimeline: inputEventId=%" PRId32
1377 ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64,
1378 mChannel->getName().c_str(), inputEventId,
1379 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME],
1380 graphicsTimeline[GraphicsTimeline::PRESENT_TIME]);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001381
1382 InputMessage msg;
1383 msg.header.type = InputMessage::Type::TIMELINE;
1384 msg.header.seq = 0;
1385 msg.body.timeline.eventId = inputEventId;
1386 msg.body.timeline.graphicsTimeline = std::move(graphicsTimeline);
1387 return mChannel->sendMessage(&msg);
1388}
1389
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001390nsecs_t InputConsumer::getConsumeTime(uint32_t seq) const {
1391 auto it = mConsumeTimes.find(seq);
1392 // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was
1393 // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed.
1394 LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32,
1395 seq);
1396 return it->second;
1397}
1398
1399void InputConsumer::popConsumeTime(uint32_t seq) {
1400 mConsumeTimes.erase(seq);
1401}
1402
Jeff Brown5912f952013-07-01 19:10:31 -07001403status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1404 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001405 msg.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001406 msg.header.seq = seq;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001407 msg.body.finished.handled = handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001408 msg.body.finished.consumeTime = getConsumeTime(seq);
1409 status_t result = mChannel->sendMessage(&msg);
1410 if (result == OK) {
1411 // Remove the consume time if the socket write succeeded. We will not need to ack this
1412 // message anymore. If the socket write did not succeed, we will try again and will still
1413 // need consume time.
1414 popConsumeTime(seq);
Siarhei Vishniakouca42e0d2024-01-12 12:10:32 -08001415
1416 // Trace the event processing timeline - event was just finished
1417 ATRACE_ASYNC_END("InputConsumer processing", /*cookie=*/seq);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001418 }
1419 return result;
Jeff Brown5912f952013-07-01 19:10:31 -07001420}
1421
Jeff Brown5912f952013-07-01 19:10:31 -07001422bool InputConsumer::hasPendingBatch() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001423 return !mBatches.empty();
Jeff Brown5912f952013-07-01 19:10:31 -07001424}
1425
Arthur Hungc7812be2020-02-27 22:40:27 +08001426int32_t InputConsumer::getPendingBatchSource() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001427 if (mBatches.empty()) {
Arthur Hungc7812be2020-02-27 22:40:27 +08001428 return AINPUT_SOURCE_CLASS_NONE;
1429 }
1430
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001431 const Batch& batch = mBatches[0];
1432 const InputMessage& head = batch.samples[0];
Arthur Hungc7812be2020-02-27 22:40:27 +08001433 return head.body.motion.source;
1434}
1435
Egor Paskoa0d32af2023-12-14 17:45:41 +01001436bool InputConsumer::probablyHasInput() const {
1437 return hasPendingBatch() || mChannel->probablyHasInput();
1438}
1439
Jeff Brown5912f952013-07-01 19:10:31 -07001440ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1441 for (size_t i = 0; i < mBatches.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001442 const Batch& batch = mBatches[i];
1443 const InputMessage& head = batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001444 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1445 return i;
1446 }
1447 }
1448 return -1;
1449}
1450
1451ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1452 for (size_t i = 0; i < mTouchStates.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001453 const TouchState& touchState = mTouchStates[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001454 if (touchState.deviceId == deviceId && touchState.source == source) {
1455 return i;
1456 }
1457 }
1458 return -1;
1459}
1460
1461void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08001462 event->initialize(msg->body.key.eventId, msg->body.key.deviceId, msg->body.key.source,
Garfield Tanfbe732e2020-01-24 11:26:14 -08001463 msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
1464 msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
1465 msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
1466 msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001467}
1468
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001469void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001470 event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001471}
1472
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001473void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001474 event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001475}
1476
arthurhung7632c332020-12-30 16:58:01 +08001477void InputConsumer::initializeDragEvent(DragEvent* event, const InputMessage* msg) {
1478 event->initialize(msg->body.drag.eventId, msg->body.drag.x, msg->body.drag.y,
1479 msg->body.drag.isExiting);
1480}
1481
Jeff Brown5912f952013-07-01 19:10:31 -07001482void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001483 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001484 PointerProperties pointerProperties[pointerCount];
1485 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001486 for (uint32_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -07001487 pointerProperties[i] = msg->body.motion.pointers[i].properties;
1488 pointerCoords[i] = msg->body.motion.pointers[i].coords;
Jeff Brown5912f952013-07-01 19:10:31 -07001489 }
1490
chaviw9eaa22c2020-07-01 16:21:27 -07001491 ui::Transform transform;
1492 transform.set({msg->body.motion.dsdx, msg->body.motion.dtdx, msg->body.motion.tx,
1493 msg->body.motion.dtdy, msg->body.motion.dsdy, msg->body.motion.ty, 0, 0, 1});
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001494 ui::Transform displayTransform;
1495 displayTransform.set({msg->body.motion.dsdxRaw, msg->body.motion.dtdxRaw,
1496 msg->body.motion.txRaw, msg->body.motion.dtdyRaw,
1497 msg->body.motion.dsdyRaw, msg->body.motion.tyRaw, 0, 0, 1});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001498 event->initialize(msg->body.motion.eventId, msg->body.motion.deviceId, msg->body.motion.source,
1499 msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
1500 msg->body.motion.actionButton, msg->body.motion.flags,
1501 msg->body.motion.edgeFlags, msg->body.motion.metaState,
chaviw9eaa22c2020-07-01 16:21:27 -07001502 msg->body.motion.buttonState, msg->body.motion.classification, transform,
1503 msg->body.motion.xPrecision, msg->body.motion.yPrecision,
1504 msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001505 displayTransform, msg->body.motion.downTime, msg->body.motion.eventTime,
1506 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001507}
1508
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001509void InputConsumer::initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg) {
1510 event->initialize(msg->body.touchMode.eventId, msg->body.touchMode.isInTouchMode);
1511}
1512
Jeff Brown5912f952013-07-01 19:10:31 -07001513void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001514 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001515 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001516 for (uint32_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -07001517 pointerCoords[i] = msg->body.motion.pointers[i].coords;
Jeff Brown5912f952013-07-01 19:10:31 -07001518 }
1519
1520 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1521 event->addSample(msg->body.motion.eventTime, pointerCoords);
1522}
1523
1524bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001525 const InputMessage& head = batch.samples[0];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001526 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001527 if (head.body.motion.pointerCount != pointerCount
1528 || head.body.motion.action != msg->body.motion.action) {
1529 return false;
1530 }
1531 for (size_t i = 0; i < pointerCount; i++) {
1532 if (head.body.motion.pointers[i].properties
1533 != msg->body.motion.pointers[i].properties) {
1534 return false;
1535 }
1536 }
1537 return true;
1538}
1539
1540ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1541 size_t numSamples = batch.samples.size();
1542 size_t index = 0;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001543 while (index < numSamples && batch.samples[index].body.motion.eventTime <= time) {
Jeff Brown5912f952013-07-01 19:10:31 -07001544 index += 1;
1545 }
1546 return ssize_t(index) - 1;
1547}
1548
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001549std::string InputConsumer::dump() const {
1550 std::string out;
1551 out = out + "mResampleTouch = " + toString(mResampleTouch) + "\n";
1552 out = out + "mChannel = " + mChannel->getName() + "\n";
1553 out = out + "mMsgDeferred: " + toString(mMsgDeferred) + "\n";
1554 if (mMsgDeferred) {
Dominik Laskowski75788452021-02-09 18:51:25 -08001555 out = out + "mMsg : " + ftl::enum_string(mMsg.header.type) + "\n";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001556 }
1557 out += "Batches:\n";
1558 for (const Batch& batch : mBatches) {
1559 out += " Batch:\n";
1560 for (const InputMessage& msg : batch.samples) {
1561 out += android::base::StringPrintf(" Message %" PRIu32 ": %s ", msg.header.seq,
Dominik Laskowski75788452021-02-09 18:51:25 -08001562 ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001563 switch (msg.header.type) {
1564 case InputMessage::Type::KEY: {
1565 out += android::base::StringPrintf("action=%s keycode=%" PRId32,
1566 KeyEvent::actionToString(
1567 msg.body.key.action),
1568 msg.body.key.keyCode);
1569 break;
1570 }
1571 case InputMessage::Type::MOTION: {
1572 out = out + "action=" + MotionEvent::actionToString(msg.body.motion.action);
1573 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1574 const float x = msg.body.motion.pointers[i].coords.getX();
1575 const float y = msg.body.motion.pointers[i].coords.getY();
1576 out += android::base::StringPrintf("\n Pointer %" PRIu32
1577 " : x=%.1f y=%.1f",
1578 i, x, y);
1579 }
1580 break;
1581 }
1582 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001583 out += android::base::StringPrintf("handled=%s, consumeTime=%" PRId64,
1584 toString(msg.body.finished.handled),
1585 msg.body.finished.consumeTime);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001586 break;
1587 }
1588 case InputMessage::Type::FOCUS: {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001589 out += android::base::StringPrintf("hasFocus=%s",
1590 toString(msg.body.focus.hasFocus));
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001591 break;
1592 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001593 case InputMessage::Type::CAPTURE: {
1594 out += android::base::StringPrintf("hasCapture=%s",
1595 toString(msg.body.capture
1596 .pointerCaptureEnabled));
1597 break;
1598 }
arthurhung7632c332020-12-30 16:58:01 +08001599 case InputMessage::Type::DRAG: {
1600 out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s",
1601 msg.body.drag.x, msg.body.drag.y,
1602 toString(msg.body.drag.isExiting));
1603 break;
1604 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001605 case InputMessage::Type::TIMELINE: {
1606 const nsecs_t gpuCompletedTime =
1607 msg.body.timeline
1608 .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
1609 const nsecs_t presentTime =
1610 msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
1611 out += android::base::StringPrintf("inputEventId=%" PRId32
1612 ", gpuCompletedTime=%" PRId64
1613 ", presentTime=%" PRId64,
1614 msg.body.timeline.eventId, gpuCompletedTime,
1615 presentTime);
1616 break;
1617 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001618 case InputMessage::Type::TOUCH_MODE: {
1619 out += android::base::StringPrintf("isInTouchMode=%s",
1620 toString(msg.body.touchMode.isInTouchMode));
1621 break;
1622 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001623 }
1624 out += "\n";
1625 }
1626 }
1627 if (mBatches.empty()) {
1628 out += " <empty>\n";
1629 }
1630 out += "mSeqChains:\n";
1631 for (const SeqChain& chain : mSeqChains) {
1632 out += android::base::StringPrintf(" chain: seq = %" PRIu32 " chain=%" PRIu32, chain.seq,
1633 chain.chain);
1634 }
1635 if (mSeqChains.empty()) {
1636 out += " <empty>\n";
1637 }
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001638 out += "mConsumeTimes:\n";
1639 for (const auto& [seq, consumeTime] : mConsumeTimes) {
1640 out += android::base::StringPrintf(" seq = %" PRIu32 " consumeTime = %" PRId64, seq,
1641 consumeTime);
1642 }
1643 if (mConsumeTimes.empty()) {
1644 out += " <empty>\n";
1645 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001646 return out;
1647}
1648
Jeff Brown5912f952013-07-01 19:10:31 -07001649} // namespace android