blob: e49f4eb6f62ebb67de42b8605d0ca02784e1bd30 [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 Vishniakou7b9f4f52024-02-02 13:07:16 -0800587void InputChannel::moveChannel(std::unique_ptr<InputChannel> from,
588 android::os::InputChannelCore& outChannel) {
589 outChannel.name = from->getName();
590 outChannel.fd = android::os::ParcelFileDescriptor(std::move(from->fd));
591 outChannel.token = from->getConnectionToken();
592}
593
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700594sp<IBinder> InputChannel::getConnectionToken() const {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800595 return token;
Garfield Tan15601662020-09-22 15:32:38 -0700596}
597
Jeff Brown5912f952013-07-01 19:10:31 -0700598// --- InputPublisher ---
599
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800600InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel)
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -0800601 : mChannel(channel), mInputVerifier(mChannel->getName()) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700602
603InputPublisher::~InputPublisher() {
604}
605
Garfield Tan1c7bc862020-01-28 13:24:04 -0800606status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
607 int32_t source, int32_t displayId,
608 std::array<uint8_t, 32> hmac, int32_t action,
609 int32_t flags, int32_t keyCode, int32_t scanCode,
610 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
611 nsecs_t eventTime) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000612 ATRACE_NAME_IF(ATRACE_ENABLED(),
613 StringPrintf("publishKeyEvent(inputChannel=%s, action=%s, keyCode=%s)",
614 mChannel->getName().c_str(), KeyEvent::actionToString(action),
615 KeyEvent::getLabel(keyCode)));
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000616 ALOGD_IF(debugTransportPublisher(),
Prabir Pradhan96282b02023-02-24 22:36:17 +0000617 "channel '%s' publisher ~ %s: seq=%u, id=%d, deviceId=%d, source=%s, "
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000618 "action=%s, flags=0x%x, keyCode=%s, scanCode=%d, metaState=0x%x, repeatCount=%d,"
619 "downTime=%" PRId64 ", eventTime=%" PRId64,
Prabir Pradhan96282b02023-02-24 22:36:17 +0000620 mChannel->getName().c_str(), __func__, seq, eventId, deviceId,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000621 inputEventSourceToString(source).c_str(), KeyEvent::actionToString(action), flags,
622 KeyEvent::getLabel(keyCode), scanCode, metaState, repeatCount, downTime, eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700623
624 if (!seq) {
625 ALOGE("Attempted to publish a key event with sequence number 0.");
626 return BAD_VALUE;
627 }
628
629 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700630 msg.header.type = InputMessage::Type::KEY;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500631 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800632 msg.body.key.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700633 msg.body.key.deviceId = deviceId;
634 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100635 msg.body.key.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700636 msg.body.key.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700637 msg.body.key.action = action;
638 msg.body.key.flags = flags;
639 msg.body.key.keyCode = keyCode;
640 msg.body.key.scanCode = scanCode;
641 msg.body.key.metaState = metaState;
642 msg.body.key.repeatCount = repeatCount;
643 msg.body.key.downTime = downTime;
644 msg.body.key.eventTime = eventTime;
645 return mChannel->sendMessage(&msg);
646}
647
648status_t InputPublisher::publishMotionEvent(
Garfield Tan1c7bc862020-01-28 13:24:04 -0800649 uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600650 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
651 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700652 MotionClassification classification, const ui::Transform& transform, float xPrecision,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700653 float yPrecision, float xCursorPosition, float yCursorPosition,
654 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700655 uint32_t pointerCount, const PointerProperties* pointerProperties,
656 const PointerCoords* pointerCoords) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000657 ATRACE_NAME_IF(ATRACE_ENABLED(),
658 StringPrintf("publishMotionEvent(inputChannel=%s, action=%s)",
659 mChannel->getName().c_str(),
660 MotionEvent::actionToString(action).c_str()));
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800661 if (verifyEvents()) {
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700662 Result<void> result =
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -0700663 mInputVerifier.processMovement(deviceId, source, action, pointerCount,
664 pointerProperties, pointerCoords, flags);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700665 if (!result.ok()) {
666 LOG(FATAL) << "Bad stream: " << result.error();
667 }
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800668 }
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000669 if (debugTransportPublisher()) {
chaviw9eaa22c2020-07-01 16:21:27 -0700670 std::string transformString;
chaviw85b44202020-07-24 11:46:21 -0700671 transform.dump(transformString, "transform", " ");
Prabir Pradhan96282b02023-02-24 22:36:17 +0000672 ALOGD("channel '%s' publisher ~ %s: seq=%u, id=%d, deviceId=%d, source=%s, "
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800673 "displayId=%" PRId32 ", "
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000674 "action=%s, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
chaviw9eaa22c2020-07-01 16:21:27 -0700675 "metaState=0x%x, buttonState=0x%x, classification=%s,"
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800676 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700677 "pointerCount=%" PRIu32 "\n%s",
Prabir Pradhan96282b02023-02-24 22:36:17 +0000678 mChannel->getName().c_str(), __func__, seq, eventId, deviceId,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000679 inputEventSourceToString(source).c_str(), displayId,
680 MotionEvent::actionToString(action).c_str(), actionButton, flags, edgeFlags,
681 metaState, buttonState, motionClassificationToString(classification), xPrecision,
682 yPrecision, downTime, eventTime, pointerCount, transformString.c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800683 }
Jeff Brown5912f952013-07-01 19:10:31 -0700684
685 if (!seq) {
686 ALOGE("Attempted to publish a motion event with sequence number 0.");
687 return BAD_VALUE;
688 }
689
690 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700691 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800692 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700693 return BAD_VALUE;
694 }
695
696 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700697 msg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500698 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800699 msg.body.motion.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700700 msg.body.motion.deviceId = deviceId;
701 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700702 msg.body.motion.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700703 msg.body.motion.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700704 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100705 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700706 msg.body.motion.flags = flags;
707 msg.body.motion.edgeFlags = edgeFlags;
708 msg.body.motion.metaState = metaState;
709 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800710 msg.body.motion.classification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700711 msg.body.motion.dsdx = transform.dsdx();
712 msg.body.motion.dtdx = transform.dtdx();
713 msg.body.motion.dtdy = transform.dtdy();
714 msg.body.motion.dsdy = transform.dsdy();
715 msg.body.motion.tx = transform.tx();
716 msg.body.motion.ty = transform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700717 msg.body.motion.xPrecision = xPrecision;
718 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700719 msg.body.motion.xCursorPosition = xCursorPosition;
720 msg.body.motion.yCursorPosition = yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700721 msg.body.motion.dsdxRaw = rawTransform.dsdx();
722 msg.body.motion.dtdxRaw = rawTransform.dtdx();
723 msg.body.motion.dtdyRaw = rawTransform.dtdy();
724 msg.body.motion.dsdyRaw = rawTransform.dsdy();
725 msg.body.motion.txRaw = rawTransform.tx();
726 msg.body.motion.tyRaw = rawTransform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700727 msg.body.motion.downTime = downTime;
728 msg.body.motion.eventTime = eventTime;
729 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100730 for (uint32_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -0700731 msg.body.motion.pointers[i].properties = pointerProperties[i];
732 msg.body.motion.pointers[i].coords = pointerCoords[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700733 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700734
Jeff Brown5912f952013-07-01 19:10:31 -0700735 return mChannel->sendMessage(&msg);
736}
737
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700738status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000739 ATRACE_NAME_IF(ATRACE_ENABLED(),
740 StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)",
741 mChannel->getName().c_str(), toString(hasFocus)));
Prabir Pradhan96282b02023-02-24 22:36:17 +0000742 ALOGD_IF(debugTransportPublisher(), "channel '%s' publisher ~ %s: seq=%u, id=%d, hasFocus=%s",
743 mChannel->getName().c_str(), __func__, seq, eventId, toString(hasFocus));
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800744
745 InputMessage msg;
746 msg.header.type = InputMessage::Type::FOCUS;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500747 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800748 msg.body.focus.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000749 msg.body.focus.hasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800750 return mChannel->sendMessage(&msg);
751}
752
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800753status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
754 bool pointerCaptureEnabled) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000755 ATRACE_NAME_IF(ATRACE_ENABLED(),
756 StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
757 mChannel->getName().c_str(), toString(pointerCaptureEnabled)));
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000758 ALOGD_IF(debugTransportPublisher(),
Prabir Pradhan96282b02023-02-24 22:36:17 +0000759 "channel '%s' publisher ~ %s: seq=%u, id=%d, pointerCaptureEnabled=%s",
760 mChannel->getName().c_str(), __func__, seq, eventId, toString(pointerCaptureEnabled));
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800761
762 InputMessage msg;
763 msg.header.type = InputMessage::Type::CAPTURE;
764 msg.header.seq = seq;
765 msg.body.capture.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000766 msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800767 return mChannel->sendMessage(&msg);
768}
769
arthurhung7632c332020-12-30 16:58:01 +0800770status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
771 bool isExiting) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000772 ATRACE_NAME_IF(ATRACE_ENABLED(),
773 StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
774 mChannel->getName().c_str(), x, y, toString(isExiting)));
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000775 ALOGD_IF(debugTransportPublisher(),
Prabir Pradhan96282b02023-02-24 22:36:17 +0000776 "channel '%s' publisher ~ %s: seq=%u, id=%d, x=%f, y=%f, isExiting=%s",
777 mChannel->getName().c_str(), __func__, seq, eventId, x, y, toString(isExiting));
arthurhung7632c332020-12-30 16:58:01 +0800778
779 InputMessage msg;
780 msg.header.type = InputMessage::Type::DRAG;
781 msg.header.seq = seq;
782 msg.body.drag.eventId = eventId;
783 msg.body.drag.isExiting = isExiting;
784 msg.body.drag.x = x;
785 msg.body.drag.y = y;
786 return mChannel->sendMessage(&msg);
787}
788
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700789status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000790 ATRACE_NAME_IF(ATRACE_ENABLED(),
791 StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
792 mChannel->getName().c_str(), toString(isInTouchMode)));
Prabir Pradhan96282b02023-02-24 22:36:17 +0000793 ALOGD_IF(debugTransportPublisher(),
794 "channel '%s' publisher ~ %s: seq=%u, id=%d, isInTouchMode=%s",
795 mChannel->getName().c_str(), __func__, seq, eventId, toString(isInTouchMode));
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700796
797 InputMessage msg;
798 msg.header.type = InputMessage::Type::TOUCH_MODE;
799 msg.header.seq = seq;
800 msg.body.touchMode.eventId = eventId;
801 msg.body.touchMode.isInTouchMode = isInTouchMode;
802 return mChannel->sendMessage(&msg);
803}
804
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000805android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
Jeff Brown5912f952013-07-01 19:10:31 -0700806 InputMessage msg;
807 status_t result = mChannel->receiveMessage(&msg);
808 if (result) {
Siarhei Vishniakou69112652023-08-24 08:34:18 -0700809 if (debugTransportPublisher() && result != WOULD_BLOCK) {
810 LOG(INFO) << "channel '" << mChannel->getName() << "' publisher ~ " << __func__ << ": "
811 << strerror(result);
812 }
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000813 return android::base::Error(result);
Jeff Brown5912f952013-07-01 19:10:31 -0700814 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000815 if (msg.header.type == InputMessage::Type::FINISHED) {
Prabir Pradhan96282b02023-02-24 22:36:17 +0000816 ALOGD_IF(debugTransportPublisher(),
817 "channel '%s' publisher ~ %s: finished: seq=%u, handled=%s",
818 mChannel->getName().c_str(), __func__, msg.header.seq,
819 toString(msg.body.finished.handled));
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000820 return Finished{
821 .seq = msg.header.seq,
822 .handled = msg.body.finished.handled,
823 .consumeTime = msg.body.finished.consumeTime,
824 };
Jeff Brown5912f952013-07-01 19:10:31 -0700825 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000826
827 if (msg.header.type == InputMessage::Type::TIMELINE) {
Prabir Pradhan96282b02023-02-24 22:36:17 +0000828 ALOGD_IF(debugTransportPublisher(), "channel '%s' publisher ~ %s: timeline: id=%d",
829 mChannel->getName().c_str(), __func__, msg.body.timeline.eventId);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000830 return Timeline{
831 .inputEventId = msg.body.timeline.eventId,
832 .graphicsTimeline = msg.body.timeline.graphicsTimeline,
833 };
834 }
835
836 ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer",
Dominik Laskowski75788452021-02-09 18:51:25 -0800837 mChannel->getName().c_str(), ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000838 return android::base::Error(UNKNOWN_ERROR);
Jeff Brown5912f952013-07-01 19:10:31 -0700839}
840
841// --- InputConsumer ---
842
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500843InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel)
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800844 : InputConsumer(channel, isTouchResamplingEnabled()) {}
845
846InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel,
847 bool enableTouchResampling)
848 : mResampleTouch(enableTouchResampling), mChannel(channel), mMsgDeferred(false) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700849
850InputConsumer::~InputConsumer() {
851}
852
853bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600854 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700855}
856
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800857status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
858 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000859 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
860 "channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
861 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700862
863 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700864 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700865
866 // Fetch the next input message.
867 // Loop until an event can be returned or no additional events are received.
868 while (!*outEvent) {
869 if (mMsgDeferred) {
870 // mMsg contains a valid input message from the previous call to consume
871 // that has not yet been processed.
872 mMsgDeferred = false;
873 } else {
874 // Receive a fresh message.
875 status_t result = mChannel->receiveMessage(&mMsg);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000876 if (result == OK) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800877 const auto [_, inserted] =
878 mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
879 LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
880 mMsg.header.seq);
Siarhei Vishniakouca42e0d2024-01-12 12:10:32 -0800881
882 // Trace the event processing timeline - event was just read from the socket
883 ATRACE_ASYNC_BEGIN("InputConsumer processing", /*cookie=*/mMsg.header.seq);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000884 }
Jeff Brown5912f952013-07-01 19:10:31 -0700885 if (result) {
886 // Consume the next batched event unless batches are being held for later.
887 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800888 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700889 if (*outEvent) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000890 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
891 "channel '%s' consumer ~ consumed batch event, seq=%u",
892 mChannel->getName().c_str(), *outSeq);
Jeff Brown5912f952013-07-01 19:10:31 -0700893 break;
894 }
895 }
896 return result;
897 }
898 }
899
900 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700901 case InputMessage::Type::KEY: {
902 KeyEvent* keyEvent = factory->createKeyEvent();
903 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700904
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700905 initializeKeyEvent(keyEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500906 *outSeq = mMsg.header.seq;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700907 *outEvent = keyEvent;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000908 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
909 "channel '%s' consumer ~ consumed key event, seq=%u",
910 mChannel->getName().c_str(), *outSeq);
911 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700912 }
Jeff Brown5912f952013-07-01 19:10:31 -0700913
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700914 case InputMessage::Type::MOTION: {
915 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
916 if (batchIndex >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500917 Batch& batch = mBatches[batchIndex];
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700918 if (canAddSample(batch, &mMsg)) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500919 batch.samples.push_back(mMsg);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000920 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
921 "channel '%s' consumer ~ appended to batch event",
922 mChannel->getName().c_str());
923 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700924 } else if (isPointerEvent(mMsg.body.motion.source) &&
925 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
926 // No need to process events that we are going to cancel anyways
927 const size_t count = batch.samples.size();
928 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500929 const InputMessage& msg = batch.samples[i];
930 sendFinishedSignal(msg.header.seq, false);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700931 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500932 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
933 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700934 } else {
935 // We cannot append to the batch in progress, so we need to consume
936 // the previous batch right now and defer the new message until later.
937 mMsgDeferred = true;
938 status_t result = consumeSamples(factory, batch, batch.samples.size(),
939 outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500940 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700941 if (result) {
942 return result;
943 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000944 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
945 "channel '%s' consumer ~ consumed batch event and "
946 "deferred current event, seq=%u",
947 mChannel->getName().c_str(), *outSeq);
948 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700949 }
Jeff Brown5912f952013-07-01 19:10:31 -0700950 }
Jeff Brown5912f952013-07-01 19:10:31 -0700951
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800952 // Start a new batch if needed.
953 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
954 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500955 Batch batch;
956 batch.samples.push_back(mMsg);
957 mBatches.push_back(batch);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000958 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
959 "channel '%s' consumer ~ started batch event",
960 mChannel->getName().c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800961 break;
962 }
Jeff Brown5912f952013-07-01 19:10:31 -0700963
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800964 MotionEvent* motionEvent = factory->createMotionEvent();
965 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700966
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800967 updateTouchState(mMsg);
968 initializeMotionEvent(motionEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500969 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800970 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800971
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000972 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
973 "channel '%s' consumer ~ consumed motion event, seq=%u",
974 mChannel->getName().c_str(), *outSeq);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800975 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700976 }
Jeff Brown5912f952013-07-01 19:10:31 -0700977
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000978 case InputMessage::Type::FINISHED:
979 case InputMessage::Type::TIMELINE: {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +0000980 LOG_ALWAYS_FATAL("Consumed a %s message, which should never be seen by "
981 "InputConsumer!",
Dominik Laskowski75788452021-02-09 18:51:25 -0800982 ftl::enum_string(mMsg.header.type).c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800983 break;
984 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800985
986 case InputMessage::Type::FOCUS: {
987 FocusEvent* focusEvent = factory->createFocusEvent();
988 if (!focusEvent) return NO_MEMORY;
989
990 initializeFocusEvent(focusEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500991 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800992 *outEvent = focusEvent;
993 break;
994 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800995
996 case InputMessage::Type::CAPTURE: {
997 CaptureEvent* captureEvent = factory->createCaptureEvent();
998 if (!captureEvent) return NO_MEMORY;
999
1000 initializeCaptureEvent(captureEvent, &mMsg);
1001 *outSeq = mMsg.header.seq;
1002 *outEvent = captureEvent;
1003 break;
1004 }
arthurhung7632c332020-12-30 16:58:01 +08001005
1006 case InputMessage::Type::DRAG: {
1007 DragEvent* dragEvent = factory->createDragEvent();
1008 if (!dragEvent) return NO_MEMORY;
1009
1010 initializeDragEvent(dragEvent, &mMsg);
1011 *outSeq = mMsg.header.seq;
1012 *outEvent = dragEvent;
1013 break;
1014 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001015
1016 case InputMessage::Type::TOUCH_MODE: {
1017 TouchModeEvent* touchModeEvent = factory->createTouchModeEvent();
1018 if (!touchModeEvent) return NO_MEMORY;
1019
1020 initializeTouchModeEvent(touchModeEvent, &mMsg);
1021 *outSeq = mMsg.header.seq;
1022 *outEvent = touchModeEvent;
1023 break;
1024 }
Jeff Brown5912f952013-07-01 19:10:31 -07001025 }
1026 }
1027 return OK;
1028}
1029
1030status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -08001031 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -07001032 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -07001033 for (size_t i = mBatches.size(); i > 0; ) {
1034 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001035 Batch& batch = mBatches[i];
Michael Wright32232172013-10-21 12:05:22 -07001036 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -08001037 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001038 mBatches.erase(mBatches.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001039 return result;
1040 }
1041
Michael Wright32232172013-10-21 12:05:22 -07001042 nsecs_t sampleTime = frameTime;
1043 if (mResampleTouch) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -08001044 sampleTime -= std::chrono::nanoseconds(RESAMPLE_LATENCY).count();
Michael Wright32232172013-10-21 12:05:22 -07001045 }
Jeff Brown5912f952013-07-01 19:10:31 -07001046 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
1047 if (split < 0) {
1048 continue;
1049 }
1050
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -08001051 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -07001052 const InputMessage* next;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001053 if (batch.samples.empty()) {
1054 mBatches.erase(mBatches.begin() + i);
Yi Kong5bed83b2018-07-17 12:53:47 -07001055 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -07001056 } else {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001057 next = &batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001058 }
Michael Wright32232172013-10-21 12:05:22 -07001059 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -07001060 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
1061 }
1062 return result;
1063 }
1064
1065 return WOULD_BLOCK;
1066}
1067
1068status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -08001069 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -07001070 MotionEvent* motionEvent = factory->createMotionEvent();
1071 if (! motionEvent) return NO_MEMORY;
1072
1073 uint32_t chain = 0;
1074 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001075 InputMessage& msg = batch.samples[i];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001076 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -07001077 if (i) {
1078 SeqChain seqChain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001079 seqChain.seq = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -07001080 seqChain.chain = chain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001081 mSeqChains.push_back(seqChain);
Jeff Brown5912f952013-07-01 19:10:31 -07001082 addSample(motionEvent, &msg);
1083 } else {
1084 initializeMotionEvent(motionEvent, &msg);
1085 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001086 chain = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -07001087 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001088 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
Jeff Brown5912f952013-07-01 19:10:31 -07001089
1090 *outSeq = chain;
1091 *outEvent = motionEvent;
1092 return OK;
1093}
1094
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001095void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001096 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -07001097 return;
1098 }
1099
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001100 int32_t deviceId = msg.body.motion.deviceId;
1101 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -07001102
1103 // Update the touch state history to incorporate the new input message.
1104 // If the message is in the past relative to the most recently produced resampled
1105 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001106 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -07001107 case AMOTION_EVENT_ACTION_DOWN: {
1108 ssize_t index = findTouchState(deviceId, source);
1109 if (index < 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001110 mTouchStates.push_back({});
Jeff Brown5912f952013-07-01 19:10:31 -07001111 index = mTouchStates.size() - 1;
1112 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001113 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001114 touchState.initialize(deviceId, source);
1115 touchState.addHistory(msg);
1116 break;
1117 }
1118
1119 case AMOTION_EVENT_ACTION_MOVE: {
1120 ssize_t index = findTouchState(deviceId, source);
1121 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001122 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001123 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001124 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -07001125 }
1126 break;
1127 }
1128
1129 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
1130 ssize_t index = findTouchState(deviceId, source);
1131 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001132 TouchState& touchState = mTouchStates[index];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001133 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001134 rewriteMessage(touchState, msg);
1135 }
1136 break;
1137 }
1138
1139 case AMOTION_EVENT_ACTION_POINTER_UP: {
1140 ssize_t index = findTouchState(deviceId, source);
1141 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001142 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001143 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001144 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001145 }
1146 break;
1147 }
1148
1149 case AMOTION_EVENT_ACTION_SCROLL: {
1150 ssize_t index = findTouchState(deviceId, source);
1151 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001152 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001153 rewriteMessage(touchState, msg);
1154 }
1155 break;
1156 }
1157
1158 case AMOTION_EVENT_ACTION_UP:
1159 case AMOTION_EVENT_ACTION_CANCEL: {
1160 ssize_t index = findTouchState(deviceId, source);
1161 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001162 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001163 rewriteMessage(touchState, msg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001164 mTouchStates.erase(mTouchStates.begin() + index);
Jeff Brown5912f952013-07-01 19:10:31 -07001165 }
1166 break;
1167 }
1168 }
1169}
1170
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001171/**
1172 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
1173 *
1174 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
1175 * is in the past relative to msg and the past two events do not contain identical coordinates),
1176 * then invalidate the lastResample data for that pointer.
1177 * If the two past events have identical coordinates, then lastResample data for that pointer will
1178 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
1179 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
1180 * not equal to x0 is received.
1181 */
1182void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001183 nsecs_t eventTime = msg.body.motion.eventTime;
1184 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1185 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -07001186 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001187 if (eventTime < state.lastResample.eventTime ||
1188 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001189 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
1190 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Harry Cutts6c658cc2023-08-02 14:40:40 +00001191 ALOGD_IF(debugResampling(), "[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001192 resampleCoords.getX(), resampleCoords.getY(), msgCoords.getX(),
1193 msgCoords.getY());
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001194 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
1195 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
Philip Quinnafb31282022-12-20 18:17:55 -08001196 msgCoords.isResampled = true;
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001197 } else {
1198 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001199 }
Jeff Brown5912f952013-07-01 19:10:31 -07001200 }
1201 }
1202}
1203
1204void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
1205 const InputMessage* next) {
1206 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001207 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -07001208 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
1209 return;
1210 }
1211
1212 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
1213 if (index < 0) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001214 ALOGD_IF(debugResampling(), "Not resampled, no touch state for device.");
Jeff Brown5912f952013-07-01 19:10:31 -07001215 return;
1216 }
1217
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001218 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001219 if (touchState.historySize < 1) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001220 ALOGD_IF(debugResampling(), "Not resampled, no history for device.");
Jeff Brown5912f952013-07-01 19:10:31 -07001221 return;
1222 }
1223
1224 // Ensure that the current sample has all of the pointers that need to be reported.
1225 const History* current = touchState.getHistory(0);
1226 size_t pointerCount = event->getPointerCount();
1227 for (size_t i = 0; i < pointerCount; i++) {
1228 uint32_t id = event->getPointerId(i);
1229 if (!current->idBits.hasBit(id)) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001230 ALOGD_IF(debugResampling(), "Not resampled, missing id %d", id);
Jeff Brown5912f952013-07-01 19:10:31 -07001231 return;
1232 }
1233 }
1234
1235 // Find the data to use for resampling.
1236 const History* other;
1237 History future;
1238 float alpha;
1239 if (next) {
1240 // Interpolate between current sample and future sample.
1241 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001242 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -07001243 other = &future;
1244 nsecs_t delta = future.eventTime - current->eventTime;
1245 if (delta < RESAMPLE_MIN_DELTA) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001246 ALOGD_IF(debugResampling(), "Not resampled, delta time is too small: %" PRId64 " ns.",
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001247 delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001248 return;
1249 }
1250 alpha = float(sampleTime - current->eventTime) / delta;
1251 } else if (touchState.historySize >= 2) {
1252 // Extrapolate future sample using current sample and past sample.
1253 // So other->eventTime <= current->eventTime <= sampleTime.
1254 other = touchState.getHistory(1);
1255 nsecs_t delta = current->eventTime - other->eventTime;
1256 if (delta < RESAMPLE_MIN_DELTA) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001257 ALOGD_IF(debugResampling(), "Not resampled, delta time is too small: %" PRId64 " ns.",
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001258 delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -07001259 return;
1260 } else if (delta > RESAMPLE_MAX_DELTA) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001261 ALOGD_IF(debugResampling(), "Not resampled, delta time is too large: %" PRId64 " ns.",
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001262 delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001263 return;
1264 }
1265 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
1266 if (sampleTime > maxPredict) {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001267 ALOGD_IF(debugResampling(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001268 "Sample time is too far in the future, adjusting prediction "
1269 "from %" PRId64 " to %" PRId64 " ns.",
1270 sampleTime - current->eventTime, maxPredict - current->eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001271 sampleTime = maxPredict;
1272 }
1273 alpha = float(current->eventTime - sampleTime) / delta;
1274 } else {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001275 ALOGD_IF(debugResampling(), "Not resampled, insufficient data.");
Jeff Brown5912f952013-07-01 19:10:31 -07001276 return;
1277 }
1278
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -08001279 if (current->eventTime == sampleTime) {
1280 // Prevents having 2 events with identical times and coordinates.
1281 return;
1282 }
1283
Jeff Brown5912f952013-07-01 19:10:31 -07001284 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001285 History oldLastResample;
1286 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001287 touchState.lastResample.eventTime = sampleTime;
1288 touchState.lastResample.idBits.clear();
1289 for (size_t i = 0; i < pointerCount; i++) {
1290 uint32_t id = event->getPointerId(i);
1291 touchState.lastResample.idToIndex[id] = i;
1292 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001293 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1294 // We maintain the previously resampled value for this pointer (stored in
1295 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1296 // This way we don't introduce artificial jitter when pointers haven't actually moved.
Philip Quinnafb31282022-12-20 18:17:55 -08001297 // The isResampled flag isn't cleared as the values don't reflect what the device is
1298 // actually reporting.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001299
1300 // We know here that the coordinates for the pointer haven't changed because we
1301 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1302 // lastResample in place becasue the mapping from pointer ID to index may have changed.
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -07001303 touchState.lastResample.pointers[i] = oldLastResample.getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001304 continue;
1305 }
1306
Jeff Brown5912f952013-07-01 19:10:31 -07001307 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1308 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -07001309 resampledCoords = currentCoords;
Philip Quinn4e955a22023-09-26 12:09:40 -07001310 resampledCoords.isResampled = true;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001311 if (other->idBits.hasBit(id) && shouldResampleTool(event->getToolType(i))) {
Jeff Brown5912f952013-07-01 19:10:31 -07001312 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001313 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001314 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
Jeff Brown5912f952013-07-01 19:10:31 -07001315 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001316 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
Harry Cutts6c658cc2023-08-02 14:40:40 +00001317 ALOGD_IF(debugResampling(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001318 "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1319 "other (%0.3f, %0.3f), alpha %0.3f",
1320 id, resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(),
1321 currentCoords.getY(), otherCoords.getX(), otherCoords.getY(), alpha);
Jeff Brown5912f952013-07-01 19:10:31 -07001322 } else {
Harry Cutts6c658cc2023-08-02 14:40:40 +00001323 ALOGD_IF(debugResampling(), "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", id,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001324 resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(),
1325 currentCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -07001326 }
1327 }
1328
1329 event->addSample(sampleTime, touchState.lastResample.pointers);
1330}
1331
Jeff Brown5912f952013-07-01 19:10:31 -07001332status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001333 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
1334 "channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1335 mChannel->getName().c_str(), seq, toString(handled));
Jeff Brown5912f952013-07-01 19:10:31 -07001336
1337 if (!seq) {
1338 ALOGE("Attempted to send a finished signal with sequence number 0.");
1339 return BAD_VALUE;
1340 }
1341
1342 // Send finished signals for the batch sequence chain first.
1343 size_t seqChainCount = mSeqChains.size();
1344 if (seqChainCount) {
1345 uint32_t currentSeq = seq;
1346 uint32_t chainSeqs[seqChainCount];
1347 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001348 for (size_t i = seqChainCount; i > 0; ) {
1349 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001350 const SeqChain& seqChain = mSeqChains[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001351 if (seqChain.seq == currentSeq) {
1352 currentSeq = seqChain.chain;
1353 chainSeqs[chainIndex++] = currentSeq;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001354 mSeqChains.erase(mSeqChains.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001355 }
1356 }
1357 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001358 while (!status && chainIndex > 0) {
1359 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001360 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1361 }
1362 if (status) {
1363 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001364 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001365 SeqChain seqChain;
1366 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1367 seqChain.chain = chainSeqs[chainIndex];
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001368 mSeqChains.push_back(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001369 if (!chainIndex) break;
1370 chainIndex--;
1371 }
Jeff Brown5912f952013-07-01 19:10:31 -07001372 return status;
1373 }
1374 }
1375
1376 // Send finished signal for the last message in the batch.
1377 return sendUnchainedFinishedSignal(seq, handled);
1378}
1379
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001380status_t InputConsumer::sendTimeline(int32_t inputEventId,
1381 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001382 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
1383 "channel '%s' consumer ~ sendTimeline: inputEventId=%" PRId32
1384 ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64,
1385 mChannel->getName().c_str(), inputEventId,
1386 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME],
1387 graphicsTimeline[GraphicsTimeline::PRESENT_TIME]);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001388
1389 InputMessage msg;
1390 msg.header.type = InputMessage::Type::TIMELINE;
1391 msg.header.seq = 0;
1392 msg.body.timeline.eventId = inputEventId;
1393 msg.body.timeline.graphicsTimeline = std::move(graphicsTimeline);
1394 return mChannel->sendMessage(&msg);
1395}
1396
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001397nsecs_t InputConsumer::getConsumeTime(uint32_t seq) const {
1398 auto it = mConsumeTimes.find(seq);
1399 // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was
1400 // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed.
1401 LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32,
1402 seq);
1403 return it->second;
1404}
1405
1406void InputConsumer::popConsumeTime(uint32_t seq) {
1407 mConsumeTimes.erase(seq);
1408}
1409
Jeff Brown5912f952013-07-01 19:10:31 -07001410status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1411 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001412 msg.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001413 msg.header.seq = seq;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001414 msg.body.finished.handled = handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001415 msg.body.finished.consumeTime = getConsumeTime(seq);
1416 status_t result = mChannel->sendMessage(&msg);
1417 if (result == OK) {
1418 // Remove the consume time if the socket write succeeded. We will not need to ack this
1419 // message anymore. If the socket write did not succeed, we will try again and will still
1420 // need consume time.
1421 popConsumeTime(seq);
Siarhei Vishniakouca42e0d2024-01-12 12:10:32 -08001422
1423 // Trace the event processing timeline - event was just finished
1424 ATRACE_ASYNC_END("InputConsumer processing", /*cookie=*/seq);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001425 }
1426 return result;
Jeff Brown5912f952013-07-01 19:10:31 -07001427}
1428
Jeff Brown5912f952013-07-01 19:10:31 -07001429bool InputConsumer::hasPendingBatch() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001430 return !mBatches.empty();
Jeff Brown5912f952013-07-01 19:10:31 -07001431}
1432
Arthur Hungc7812be2020-02-27 22:40:27 +08001433int32_t InputConsumer::getPendingBatchSource() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001434 if (mBatches.empty()) {
Arthur Hungc7812be2020-02-27 22:40:27 +08001435 return AINPUT_SOURCE_CLASS_NONE;
1436 }
1437
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001438 const Batch& batch = mBatches[0];
1439 const InputMessage& head = batch.samples[0];
Arthur Hungc7812be2020-02-27 22:40:27 +08001440 return head.body.motion.source;
1441}
1442
Egor Paskoa0d32af2023-12-14 17:45:41 +01001443bool InputConsumer::probablyHasInput() const {
1444 return hasPendingBatch() || mChannel->probablyHasInput();
1445}
1446
Jeff Brown5912f952013-07-01 19:10:31 -07001447ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1448 for (size_t i = 0; i < mBatches.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001449 const Batch& batch = mBatches[i];
1450 const InputMessage& head = batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001451 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1452 return i;
1453 }
1454 }
1455 return -1;
1456}
1457
1458ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1459 for (size_t i = 0; i < mTouchStates.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001460 const TouchState& touchState = mTouchStates[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001461 if (touchState.deviceId == deviceId && touchState.source == source) {
1462 return i;
1463 }
1464 }
1465 return -1;
1466}
1467
1468void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08001469 event->initialize(msg->body.key.eventId, msg->body.key.deviceId, msg->body.key.source,
Garfield Tanfbe732e2020-01-24 11:26:14 -08001470 msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
1471 msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
1472 msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
1473 msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001474}
1475
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001476void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001477 event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001478}
1479
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001480void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001481 event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001482}
1483
arthurhung7632c332020-12-30 16:58:01 +08001484void InputConsumer::initializeDragEvent(DragEvent* event, const InputMessage* msg) {
1485 event->initialize(msg->body.drag.eventId, msg->body.drag.x, msg->body.drag.y,
1486 msg->body.drag.isExiting);
1487}
1488
Jeff Brown5912f952013-07-01 19:10:31 -07001489void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001490 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001491 PointerProperties pointerProperties[pointerCount];
1492 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001493 for (uint32_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -07001494 pointerProperties[i] = msg->body.motion.pointers[i].properties;
1495 pointerCoords[i] = msg->body.motion.pointers[i].coords;
Jeff Brown5912f952013-07-01 19:10:31 -07001496 }
1497
chaviw9eaa22c2020-07-01 16:21:27 -07001498 ui::Transform transform;
1499 transform.set({msg->body.motion.dsdx, msg->body.motion.dtdx, msg->body.motion.tx,
1500 msg->body.motion.dtdy, msg->body.motion.dsdy, msg->body.motion.ty, 0, 0, 1});
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001501 ui::Transform displayTransform;
1502 displayTransform.set({msg->body.motion.dsdxRaw, msg->body.motion.dtdxRaw,
1503 msg->body.motion.txRaw, msg->body.motion.dtdyRaw,
1504 msg->body.motion.dsdyRaw, msg->body.motion.tyRaw, 0, 0, 1});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001505 event->initialize(msg->body.motion.eventId, msg->body.motion.deviceId, msg->body.motion.source,
1506 msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
1507 msg->body.motion.actionButton, msg->body.motion.flags,
1508 msg->body.motion.edgeFlags, msg->body.motion.metaState,
chaviw9eaa22c2020-07-01 16:21:27 -07001509 msg->body.motion.buttonState, msg->body.motion.classification, transform,
1510 msg->body.motion.xPrecision, msg->body.motion.yPrecision,
1511 msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001512 displayTransform, msg->body.motion.downTime, msg->body.motion.eventTime,
1513 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001514}
1515
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001516void InputConsumer::initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg) {
1517 event->initialize(msg->body.touchMode.eventId, msg->body.touchMode.isInTouchMode);
1518}
1519
Jeff Brown5912f952013-07-01 19:10:31 -07001520void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001521 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001522 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001523 for (uint32_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -07001524 pointerCoords[i] = msg->body.motion.pointers[i].coords;
Jeff Brown5912f952013-07-01 19:10:31 -07001525 }
1526
1527 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1528 event->addSample(msg->body.motion.eventTime, pointerCoords);
1529}
1530
1531bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001532 const InputMessage& head = batch.samples[0];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001533 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001534 if (head.body.motion.pointerCount != pointerCount
1535 || head.body.motion.action != msg->body.motion.action) {
1536 return false;
1537 }
1538 for (size_t i = 0; i < pointerCount; i++) {
1539 if (head.body.motion.pointers[i].properties
1540 != msg->body.motion.pointers[i].properties) {
1541 return false;
1542 }
1543 }
1544 return true;
1545}
1546
1547ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1548 size_t numSamples = batch.samples.size();
1549 size_t index = 0;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001550 while (index < numSamples && batch.samples[index].body.motion.eventTime <= time) {
Jeff Brown5912f952013-07-01 19:10:31 -07001551 index += 1;
1552 }
1553 return ssize_t(index) - 1;
1554}
1555
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001556std::string InputConsumer::dump() const {
1557 std::string out;
1558 out = out + "mResampleTouch = " + toString(mResampleTouch) + "\n";
1559 out = out + "mChannel = " + mChannel->getName() + "\n";
1560 out = out + "mMsgDeferred: " + toString(mMsgDeferred) + "\n";
1561 if (mMsgDeferred) {
Dominik Laskowski75788452021-02-09 18:51:25 -08001562 out = out + "mMsg : " + ftl::enum_string(mMsg.header.type) + "\n";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001563 }
1564 out += "Batches:\n";
1565 for (const Batch& batch : mBatches) {
1566 out += " Batch:\n";
1567 for (const InputMessage& msg : batch.samples) {
1568 out += android::base::StringPrintf(" Message %" PRIu32 ": %s ", msg.header.seq,
Dominik Laskowski75788452021-02-09 18:51:25 -08001569 ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001570 switch (msg.header.type) {
1571 case InputMessage::Type::KEY: {
1572 out += android::base::StringPrintf("action=%s keycode=%" PRId32,
1573 KeyEvent::actionToString(
1574 msg.body.key.action),
1575 msg.body.key.keyCode);
1576 break;
1577 }
1578 case InputMessage::Type::MOTION: {
1579 out = out + "action=" + MotionEvent::actionToString(msg.body.motion.action);
1580 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1581 const float x = msg.body.motion.pointers[i].coords.getX();
1582 const float y = msg.body.motion.pointers[i].coords.getY();
1583 out += android::base::StringPrintf("\n Pointer %" PRIu32
1584 " : x=%.1f y=%.1f",
1585 i, x, y);
1586 }
1587 break;
1588 }
1589 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001590 out += android::base::StringPrintf("handled=%s, consumeTime=%" PRId64,
1591 toString(msg.body.finished.handled),
1592 msg.body.finished.consumeTime);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001593 break;
1594 }
1595 case InputMessage::Type::FOCUS: {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001596 out += android::base::StringPrintf("hasFocus=%s",
1597 toString(msg.body.focus.hasFocus));
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001598 break;
1599 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001600 case InputMessage::Type::CAPTURE: {
1601 out += android::base::StringPrintf("hasCapture=%s",
1602 toString(msg.body.capture
1603 .pointerCaptureEnabled));
1604 break;
1605 }
arthurhung7632c332020-12-30 16:58:01 +08001606 case InputMessage::Type::DRAG: {
1607 out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s",
1608 msg.body.drag.x, msg.body.drag.y,
1609 toString(msg.body.drag.isExiting));
1610 break;
1611 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001612 case InputMessage::Type::TIMELINE: {
1613 const nsecs_t gpuCompletedTime =
1614 msg.body.timeline
1615 .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
1616 const nsecs_t presentTime =
1617 msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
1618 out += android::base::StringPrintf("inputEventId=%" PRId32
1619 ", gpuCompletedTime=%" PRId64
1620 ", presentTime=%" PRId64,
1621 msg.body.timeline.eventId, gpuCompletedTime,
1622 presentTime);
1623 break;
1624 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001625 case InputMessage::Type::TOUCH_MODE: {
1626 out += android::base::StringPrintf("isInTouchMode=%s",
1627 toString(msg.body.touchMode.isInTouchMode));
1628 break;
1629 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001630 }
1631 out += "\n";
1632 }
1633 }
1634 if (mBatches.empty()) {
1635 out += " <empty>\n";
1636 }
1637 out += "mSeqChains:\n";
1638 for (const SeqChain& chain : mSeqChains) {
1639 out += android::base::StringPrintf(" chain: seq = %" PRIu32 " chain=%" PRIu32, chain.seq,
1640 chain.chain);
1641 }
1642 if (mSeqChains.empty()) {
1643 out += " <empty>\n";
1644 }
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001645 out += "mConsumeTimes:\n";
1646 for (const auto& [seq, consumeTime] : mConsumeTimes) {
1647 out += android::base::StringPrintf(" seq = %" PRIu32 " consumeTime = %" PRId64, seq,
1648 consumeTime);
1649 }
1650 if (mConsumeTimes.empty()) {
1651 out += " <empty>\n";
1652 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001653 return out;
1654}
1655
Jeff Brown5912f952013-07-01 19:10:31 -07001656} // namespace android