blob: 1d57607067d5d0580d47b9e5fb07d95b2cc420c0 [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"
7
Jeff Brown5912f952013-07-01 19:10:31 -07008#include <errno.h>
9#include <fcntl.h>
Michael Wrightd0a4a622014-06-09 19:03:32 -070010#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070011#include <math.h>
Jeff Brown5912f952013-07-01 19:10:31 -070012#include <sys/socket.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070013#include <sys/types.h>
Jeff Brown5912f952013-07-01 19:10:31 -070014#include <unistd.h>
15
Michael Wright3dd60e22019-03-27 22:06:44 +000016#include <android-base/stringprintf.h>
17#include <binder/Parcel.h>
Jeff Brown5912f952013-07-01 19:10:31 -070018#include <cutils/properties.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080019#include <ftl/enum.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070020#include <log/log.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000021#include <utils/Trace.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022
Jeff Brown5912f952013-07-01 19:10:31 -070023#include <input/InputTransport.h>
24
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000025namespace {
26
27/**
28 * Log debug messages about channel messages (send message, receive message).
29 * Enable this via "adb shell setprop log.tag.InputTransportMessages DEBUG"
30 * (requires restart)
31 */
32const bool DEBUG_CHANNEL_MESSAGES =
33 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Messages", ANDROID_LOG_INFO);
34
35/**
36 * Log debug messages whenever InputChannel objects are created/destroyed.
37 * Enable this via "adb shell setprop log.tag.InputTransportLifecycle DEBUG"
38 * (requires restart)
39 */
40const bool DEBUG_CHANNEL_LIFECYCLE =
41 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Lifecycle", ANDROID_LOG_INFO);
42
43/**
44 * Log debug messages relating to the consumer end of the transport channel.
45 * Enable this via "adb shell setprop log.tag.InputTransportConsumer DEBUG" (requires restart)
46 */
47
48const bool DEBUG_TRANSPORT_CONSUMER =
49 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Consumer", ANDROID_LOG_INFO);
50
51/**
52 * Log debug messages relating to the producer end of the transport channel.
53 * Enable this via "adb shell setprop log.tag.InputTransportPublisher DEBUG" (requires restart)
54 */
55const bool DEBUG_TRANSPORT_PUBLISHER =
56 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Publisher", ANDROID_LOG_INFO);
57
58/**
59 * Log debug messages about touch event resampling.
60 * Enable this via "adb shell setprop log.tag.InputTransportResampling DEBUG" (requires restart)
61 */
62const bool DEBUG_RESAMPLING =
63 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Resampling", ANDROID_LOG_INFO);
64
65} // namespace
66
Michael Wright3dd60e22019-03-27 22:06:44 +000067using android::base::StringPrintf;
68
Jeff Brown5912f952013-07-01 19:10:31 -070069namespace android {
70
71// Socket buffer size. The default is typically about 128KB, which is much larger than
72// we really need. So we make it smaller. It just needs to be big enough to hold
73// a few dozen large multi-finger motion events in the case where an application gets
74// behind processing touches.
75static const size_t SOCKET_BUFFER_SIZE = 32 * 1024;
76
77// Nanoseconds per milliseconds.
78static const nsecs_t NANOS_PER_MS = 1000000;
79
80// Latency added during resampling. A few milliseconds doesn't hurt much but
81// reduces the impact of mispredicted touch positions.
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -080082const std::chrono::duration RESAMPLE_LATENCY = 5ms;
Jeff Brown5912f952013-07-01 19:10:31 -070083
84// Minimum time difference between consecutive samples before attempting to resample.
85static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS;
86
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -070087// Maximum time difference between consecutive samples before attempting to resample
88// by extrapolation.
89static const nsecs_t RESAMPLE_MAX_DELTA = 20 * NANOS_PER_MS;
90
Jeff Brown5912f952013-07-01 19:10:31 -070091// Maximum time to predict forward from the last known state, to avoid predicting too
92// far into the future. This time is further bounded by 50% of the last time delta.
93static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS;
94
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -060095/**
96 * System property for enabling / disabling touch resampling.
97 * Resampling extrapolates / interpolates the reported touch event coordinates to better
98 * align them to the VSYNC signal, thus resulting in smoother scrolling performance.
99 * Resampling is not needed (and should be disabled) on hardware that already
100 * has touch events triggered by VSYNC.
101 * Set to "1" to enable resampling (default).
102 * Set to "0" to disable resampling.
103 * Resampling is enabled by default.
104 */
105static const char* PROPERTY_RESAMPLING_ENABLED = "ro.input.resampling";
106
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800107/**
108 * Crash if the events that are getting sent to the InputPublisher are inconsistent.
109 * Enable this via "adb shell setprop log.tag.InputTransportVerifyEvents DEBUG"
110 */
111static bool verifyEvents() {
112 return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "VerifyEvents", ANDROID_LOG_INFO);
113}
114
Jeff Brown5912f952013-07-01 19:10:31 -0700115template<typename T>
116inline static T min(const T& a, const T& b) {
117 return a < b ? a : b;
118}
119
120inline static float lerp(float a, float b, float alpha) {
121 return a + alpha * (b - a);
122}
123
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800124inline static bool isPointerEvent(int32_t source) {
125 return (source & AINPUT_SOURCE_CLASS_POINTER) == AINPUT_SOURCE_CLASS_POINTER;
126}
127
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800128inline static const char* toString(bool value) {
129 return value ? "true" : "false";
130}
131
Jeff Brown5912f952013-07-01 19:10:31 -0700132// --- InputMessage ---
133
134bool InputMessage::isValid(size_t actualSize) const {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000135 if (size() != actualSize) {
136 ALOGE("Received message of incorrect size %zu (expected %zu)", actualSize, size());
137 return false;
138 }
139
140 switch (header.type) {
141 case Type::KEY:
142 return true;
143 case Type::MOTION: {
144 const bool valid =
145 body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
146 if (!valid) {
147 ALOGE("Received invalid MOTION: pointerCount = %" PRIu32, body.motion.pointerCount);
148 }
149 return valid;
150 }
151 case Type::FINISHED:
152 case Type::FOCUS:
153 case Type::CAPTURE:
154 case Type::DRAG:
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700155 case Type::TOUCH_MODE:
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000156 return true;
157 case Type::TIMELINE: {
158 const nsecs_t gpuCompletedTime =
159 body.timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
160 const nsecs_t presentTime =
161 body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
162 const bool valid = presentTime > gpuCompletedTime;
163 if (!valid) {
164 ALOGE("Received invalid TIMELINE: gpuCompletedTime = %" PRId64
165 " presentTime = %" PRId64,
166 gpuCompletedTime, presentTime);
167 }
168 return valid;
Jeff Brown5912f952013-07-01 19:10:31 -0700169 }
170 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000171 ALOGE("Invalid message type: %s", ftl::enum_string(header.type).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700172 return false;
173}
174
175size_t InputMessage::size() const {
176 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700177 case Type::KEY:
178 return sizeof(Header) + body.key.size();
179 case Type::MOTION:
180 return sizeof(Header) + body.motion.size();
181 case Type::FINISHED:
182 return sizeof(Header) + body.finished.size();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800183 case Type::FOCUS:
184 return sizeof(Header) + body.focus.size();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800185 case Type::CAPTURE:
186 return sizeof(Header) + body.capture.size();
arthurhung7632c332020-12-30 16:58:01 +0800187 case Type::DRAG:
188 return sizeof(Header) + body.drag.size();
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000189 case Type::TIMELINE:
190 return sizeof(Header) + body.timeline.size();
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700191 case Type::TOUCH_MODE:
192 return sizeof(Header) + body.touchMode.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700193 }
194 return sizeof(Header);
195}
196
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800197/**
198 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
199 * memory to zero, then only copy the valid bytes on a per-field basis.
200 */
201void InputMessage::getSanitizedCopy(InputMessage* msg) const {
202 memset(msg, 0, sizeof(*msg));
203
204 // Write the header
205 msg->header.type = header.type;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500206 msg->header.seq = header.seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800207
208 // Write the body
209 switch(header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700210 case InputMessage::Type::KEY: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800211 // int32_t eventId
212 msg->body.key.eventId = body.key.eventId;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800213 // nsecs_t eventTime
214 msg->body.key.eventTime = body.key.eventTime;
215 // int32_t deviceId
216 msg->body.key.deviceId = body.key.deviceId;
217 // int32_t source
218 msg->body.key.source = body.key.source;
219 // int32_t displayId
220 msg->body.key.displayId = body.key.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600221 // std::array<uint8_t, 32> hmac
222 msg->body.key.hmac = body.key.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800223 // int32_t action
224 msg->body.key.action = body.key.action;
225 // int32_t flags
226 msg->body.key.flags = body.key.flags;
227 // int32_t keyCode
228 msg->body.key.keyCode = body.key.keyCode;
229 // int32_t scanCode
230 msg->body.key.scanCode = body.key.scanCode;
231 // int32_t metaState
232 msg->body.key.metaState = body.key.metaState;
233 // int32_t repeatCount
234 msg->body.key.repeatCount = body.key.repeatCount;
235 // nsecs_t downTime
236 msg->body.key.downTime = body.key.downTime;
237 break;
238 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700239 case InputMessage::Type::MOTION: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800240 // int32_t eventId
241 msg->body.motion.eventId = body.motion.eventId;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700242 // uint32_t pointerCount
243 msg->body.motion.pointerCount = body.motion.pointerCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800244 // nsecs_t eventTime
245 msg->body.motion.eventTime = body.motion.eventTime;
246 // int32_t deviceId
247 msg->body.motion.deviceId = body.motion.deviceId;
248 // int32_t source
249 msg->body.motion.source = body.motion.source;
250 // int32_t displayId
251 msg->body.motion.displayId = body.motion.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600252 // std::array<uint8_t, 32> hmac
253 msg->body.motion.hmac = body.motion.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800254 // int32_t action
255 msg->body.motion.action = body.motion.action;
256 // int32_t actionButton
257 msg->body.motion.actionButton = body.motion.actionButton;
258 // int32_t flags
259 msg->body.motion.flags = body.motion.flags;
260 // int32_t metaState
261 msg->body.motion.metaState = body.motion.metaState;
262 // int32_t buttonState
263 msg->body.motion.buttonState = body.motion.buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800264 // MotionClassification classification
265 msg->body.motion.classification = body.motion.classification;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800266 // int32_t edgeFlags
267 msg->body.motion.edgeFlags = body.motion.edgeFlags;
268 // nsecs_t downTime
269 msg->body.motion.downTime = body.motion.downTime;
chaviw9eaa22c2020-07-01 16:21:27 -0700270
271 msg->body.motion.dsdx = body.motion.dsdx;
272 msg->body.motion.dtdx = body.motion.dtdx;
273 msg->body.motion.dtdy = body.motion.dtdy;
274 msg->body.motion.dsdy = body.motion.dsdy;
275 msg->body.motion.tx = body.motion.tx;
276 msg->body.motion.ty = body.motion.ty;
277
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800278 // float xPrecision
279 msg->body.motion.xPrecision = body.motion.xPrecision;
280 // float yPrecision
281 msg->body.motion.yPrecision = body.motion.yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700282 // float xCursorPosition
283 msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
284 // float yCursorPosition
285 msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700286
287 msg->body.motion.dsdxRaw = body.motion.dsdxRaw;
288 msg->body.motion.dtdxRaw = body.motion.dtdxRaw;
289 msg->body.motion.dtdyRaw = body.motion.dtdyRaw;
290 msg->body.motion.dsdyRaw = body.motion.dsdyRaw;
291 msg->body.motion.txRaw = body.motion.txRaw;
292 msg->body.motion.tyRaw = body.motion.tyRaw;
293
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800294 //struct Pointer pointers[MAX_POINTERS]
295 for (size_t i = 0; i < body.motion.pointerCount; i++) {
296 // PointerProperties properties
297 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
298 msg->body.motion.pointers[i].properties.toolType =
299 body.motion.pointers[i].properties.toolType,
300 // PointerCoords coords
301 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
302 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
303 memcpy(&msg->body.motion.pointers[i].coords.values[0],
304 &body.motion.pointers[i].coords.values[0],
305 count * (sizeof(body.motion.pointers[i].coords.values[0])));
Philip Quinnafb31282022-12-20 18:17:55 -0800306 msg->body.motion.pointers[i].coords.isResampled =
307 body.motion.pointers[i].coords.isResampled;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800308 }
309 break;
310 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700311 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800312 msg->body.finished.handled = body.finished.handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000313 msg->body.finished.consumeTime = body.finished.consumeTime;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800314 break;
315 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800316 case InputMessage::Type::FOCUS: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800317 msg->body.focus.eventId = body.focus.eventId;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800318 msg->body.focus.hasFocus = body.focus.hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800319 break;
320 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800321 case InputMessage::Type::CAPTURE: {
322 msg->body.capture.eventId = body.capture.eventId;
323 msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
324 break;
325 }
arthurhung7632c332020-12-30 16:58:01 +0800326 case InputMessage::Type::DRAG: {
327 msg->body.drag.eventId = body.drag.eventId;
328 msg->body.drag.x = body.drag.x;
329 msg->body.drag.y = body.drag.y;
330 msg->body.drag.isExiting = body.drag.isExiting;
331 break;
332 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000333 case InputMessage::Type::TIMELINE: {
334 msg->body.timeline.eventId = body.timeline.eventId;
335 msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
336 break;
337 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700338 case InputMessage::Type::TOUCH_MODE: {
339 msg->body.touchMode.eventId = body.touchMode.eventId;
340 msg->body.touchMode.isInTouchMode = body.touchMode.isInTouchMode;
341 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800342 }
343}
Jeff Brown5912f952013-07-01 19:10:31 -0700344
345// --- InputChannel ---
346
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500347std::unique_ptr<InputChannel> InputChannel::create(const std::string& name,
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500348 android::base::unique_fd fd, sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700349 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
350 if (result != 0) {
351 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
352 strerror(errno));
353 return nullptr;
354 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500355 // using 'new' to access a non-public constructor
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500356 return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700357}
358
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500359InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token)
360 : mName(std::move(name)), mFd(std::move(fd)), mToken(std::move(token)) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000361 ALOGD_IF(DEBUG_CHANNEL_LIFECYCLE, "Input channel constructed: name='%s', fd=%d",
362 getName().c_str(), getFd().get());
Jeff Brown5912f952013-07-01 19:10:31 -0700363}
364
365InputChannel::~InputChannel() {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000366 ALOGD_IF(DEBUG_CHANNEL_LIFECYCLE, "Input channel destroyed: name='%s', fd=%d",
367 getName().c_str(), getFd().get());
Robert Carr3720ed02018-08-08 16:08:27 -0700368}
369
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800370status_t InputChannel::openInputChannelPair(const std::string& name,
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500371 std::unique_ptr<InputChannel>& outServerChannel,
372 std::unique_ptr<InputChannel>& outClientChannel) {
Jeff Brown5912f952013-07-01 19:10:31 -0700373 int sockets[2];
374 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
375 status_t result = -errno;
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +0000376 ALOGE("channel '%s' ~ Could not create socket pair. errno=%s(%d)", name.c_str(),
377 strerror(errno), errno);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500378 outServerChannel.reset();
379 outClientChannel.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700380 return result;
381 }
382
383 int bufferSize = SOCKET_BUFFER_SIZE;
384 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
385 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
386 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
387 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
388
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700389 sp<IBinder> token = new BBinder();
390
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700391 std::string serverChannelName = name + " (server)";
392 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700393 outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700394
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700395 std::string clientChannelName = name + " (client)";
396 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700397 outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700398 return OK;
399}
400
401status_t InputChannel::sendMessage(const InputMessage* msg) {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800402 const size_t msgLength = msg->size();
403 InputMessage cleanMsg;
404 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700405 ssize_t nWrite;
406 do {
Chris Ye0783e992020-06-02 21:34:49 -0700407 nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700408 } while (nWrite == -1 && errno == EINTR);
409
410 if (nWrite < 0) {
411 int error = errno;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000412 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ error sending message of type %s, %s",
413 mName.c_str(), ftl::enum_string(msg->header.type).c_str(), strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700414 if (error == EAGAIN || error == EWOULDBLOCK) {
415 return WOULD_BLOCK;
416 }
417 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
418 return DEAD_OBJECT;
419 }
420 return -error;
421 }
422
423 if (size_t(nWrite) != msgLength) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000424 ALOGD_IF(DEBUG_CHANNEL_MESSAGES,
425 "channel '%s' ~ error sending message type %s, send was incomplete", mName.c_str(),
426 ftl::enum_string(msg->header.type).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700427 return DEAD_OBJECT;
428 }
429
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000430 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ sent message of type %s", mName.c_str(),
431 ftl::enum_string(msg->header.type).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700432 return OK;
433}
434
435status_t InputChannel::receiveMessage(InputMessage* msg) {
436 ssize_t nRead;
437 do {
Chris Ye0783e992020-06-02 21:34:49 -0700438 nRead = ::recv(getFd(), msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700439 } while (nRead == -1 && errno == EINTR);
440
441 if (nRead < 0) {
442 int error = errno;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000443 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ receive message failed, errno=%d",
444 mName.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700445 if (error == EAGAIN || error == EWOULDBLOCK) {
446 return WOULD_BLOCK;
447 }
448 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
449 return DEAD_OBJECT;
450 }
451 return -error;
452 }
453
454 if (nRead == 0) { // check for EOF
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000455 ALOGD_IF(DEBUG_CHANNEL_MESSAGES,
456 "channel '%s' ~ receive message failed because peer was closed", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700457 return DEAD_OBJECT;
458 }
459
460 if (!msg->isValid(nRead)) {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000461 ALOGE("channel '%s' ~ received invalid message of size %zd", mName.c_str(), nRead);
Jeff Brown5912f952013-07-01 19:10:31 -0700462 return BAD_VALUE;
463 }
464
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000465 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ received message of type %s", mName.c_str(),
466 ftl::enum_string(msg->header.type).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700467 return OK;
468}
469
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500470std::unique_ptr<InputChannel> InputChannel::dup() const {
Garfield Tan15601662020-09-22 15:32:38 -0700471 base::unique_fd newFd(dupFd());
Chris Ye0783e992020-06-02 21:34:49 -0700472 return InputChannel::create(getName(), std::move(newFd), getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -0700473}
474
Garfield Tan15601662020-09-22 15:32:38 -0700475void InputChannel::copyTo(InputChannel& outChannel) const {
476 outChannel.mName = getName();
477 outChannel.mFd = dupFd();
478 outChannel.mToken = getConnectionToken();
479}
480
Chris Ye0783e992020-06-02 21:34:49 -0700481status_t InputChannel::writeToParcel(android::Parcel* parcel) const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500482 if (parcel == nullptr) {
483 ALOGE("%s: Null parcel", __func__);
484 return BAD_VALUE;
485 }
486 return parcel->writeStrongBinder(mToken)
487 ?: parcel->writeUtf8AsUtf16(mName) ?: parcel->writeUniqueFileDescriptor(mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700488}
489
Chris Ye0783e992020-06-02 21:34:49 -0700490status_t InputChannel::readFromParcel(const android::Parcel* parcel) {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500491 if (parcel == nullptr) {
492 ALOGE("%s: Null parcel", __func__);
493 return BAD_VALUE;
494 }
495 mToken = parcel->readStrongBinder();
496 return parcel->readUtf8FromUtf16(&mName) ?: parcel->readUniqueFileDescriptor(&mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700497}
498
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700499sp<IBinder> InputChannel::getConnectionToken() const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500500 return mToken;
Robert Carr803535b2018-08-02 16:38:15 -0700501}
502
Garfield Tan15601662020-09-22 15:32:38 -0700503base::unique_fd InputChannel::dupFd() const {
504 android::base::unique_fd newFd(::dup(getFd()));
505 if (!newFd.ok()) {
506 ALOGE("Could not duplicate fd %i for channel %s: %s", getFd().get(), getName().c_str(),
507 strerror(errno));
508 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
509 // If this process is out of file descriptors, then throwing that might end up exploding
510 // on the other side of a binder call, which isn't really helpful.
511 // Better to just crash here and hope that the FD leak is slow.
512 // Other failures could be client errors, so we still propagate those back to the caller.
513 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s",
514 getName().c_str());
515 return {};
516 }
517 return newFd;
518}
519
Jeff Brown5912f952013-07-01 19:10:31 -0700520// --- InputPublisher ---
521
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800522InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel)
523 : mChannel(channel), mInputVerifier(channel->getName()) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700524
525InputPublisher::~InputPublisher() {
526}
527
Garfield Tan1c7bc862020-01-28 13:24:04 -0800528status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
529 int32_t source, int32_t displayId,
530 std::array<uint8_t, 32> hmac, int32_t action,
531 int32_t flags, int32_t keyCode, int32_t scanCode,
532 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
533 nsecs_t eventTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000534 if (ATRACE_ENABLED()) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000535 std::string message =
536 StringPrintf("publishKeyEvent(inputChannel=%s, action=%s, keyCode=%s)",
537 mChannel->getName().c_str(), KeyEvent::actionToString(action),
538 KeyEvent::getLabel(keyCode));
Michael Wright3dd60e22019-03-27 22:06:44 +0000539 ATRACE_NAME(message.c_str());
540 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000541 ALOGD_IF(DEBUG_TRANSPORT_PUBLISHER,
542 "channel '%s' publisher ~ %s: seq=%u, deviceId=%d, source=%s, "
543 "action=%s, flags=0x%x, keyCode=%s, scanCode=%d, metaState=0x%x, repeatCount=%d,"
544 "downTime=%" PRId64 ", eventTime=%" PRId64,
545 mChannel->getName().c_str(), __func__, seq, deviceId,
546 inputEventSourceToString(source).c_str(), KeyEvent::actionToString(action), flags,
547 KeyEvent::getLabel(keyCode), scanCode, metaState, repeatCount, downTime, eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700548
549 if (!seq) {
550 ALOGE("Attempted to publish a key event with sequence number 0.");
551 return BAD_VALUE;
552 }
553
554 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700555 msg.header.type = InputMessage::Type::KEY;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500556 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800557 msg.body.key.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700558 msg.body.key.deviceId = deviceId;
559 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100560 msg.body.key.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700561 msg.body.key.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700562 msg.body.key.action = action;
563 msg.body.key.flags = flags;
564 msg.body.key.keyCode = keyCode;
565 msg.body.key.scanCode = scanCode;
566 msg.body.key.metaState = metaState;
567 msg.body.key.repeatCount = repeatCount;
568 msg.body.key.downTime = downTime;
569 msg.body.key.eventTime = eventTime;
570 return mChannel->sendMessage(&msg);
571}
572
573status_t InputPublisher::publishMotionEvent(
Garfield Tan1c7bc862020-01-28 13:24:04 -0800574 uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600575 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
576 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700577 MotionClassification classification, const ui::Transform& transform, float xPrecision,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700578 float yPrecision, float xCursorPosition, float yCursorPosition,
579 const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
Evan Rosky09576692021-07-01 12:22:09 -0700580 uint32_t pointerCount, const PointerProperties* pointerProperties,
581 const PointerCoords* pointerCoords) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000582 if (ATRACE_ENABLED()) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000583 std::string message = StringPrintf("publishMotionEvent(inputChannel=%s, action=%s)",
584 mChannel->getName().c_str(),
585 MotionEvent::actionToString(action).c_str());
Michael Wright3dd60e22019-03-27 22:06:44 +0000586 ATRACE_NAME(message.c_str());
587 }
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800588 if (verifyEvents()) {
589 mInputVerifier.processMovement(deviceId, action, pointerCount, pointerProperties,
590 pointerCoords, flags);
591 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000592 if (DEBUG_TRANSPORT_PUBLISHER) {
chaviw9eaa22c2020-07-01 16:21:27 -0700593 std::string transformString;
chaviw85b44202020-07-24 11:46:21 -0700594 transform.dump(transformString, "transform", " ");
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000595 ALOGD("channel '%s' publisher ~ %s: seq=%u, deviceId=%d, source=%s, "
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800596 "displayId=%" PRId32 ", "
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000597 "action=%s, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
chaviw9eaa22c2020-07-01 16:21:27 -0700598 "metaState=0x%x, buttonState=0x%x, classification=%s,"
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800599 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
chaviw85b44202020-07-24 11:46:21 -0700600 "pointerCount=%" PRIu32 " \n%s",
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000601 mChannel->getName().c_str(), __func__, seq, deviceId,
602 inputEventSourceToString(source).c_str(), displayId,
603 MotionEvent::actionToString(action).c_str(), actionButton, flags, edgeFlags,
604 metaState, buttonState, motionClassificationToString(classification), xPrecision,
605 yPrecision, downTime, eventTime, pointerCount, transformString.c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800606 }
Jeff Brown5912f952013-07-01 19:10:31 -0700607
608 if (!seq) {
609 ALOGE("Attempted to publish a motion event with sequence number 0.");
610 return BAD_VALUE;
611 }
612
613 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700614 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800615 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700616 return BAD_VALUE;
617 }
618
619 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700620 msg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500621 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800622 msg.body.motion.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700623 msg.body.motion.deviceId = deviceId;
624 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700625 msg.body.motion.displayId = displayId;
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700626 msg.body.motion.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700627 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100628 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700629 msg.body.motion.flags = flags;
630 msg.body.motion.edgeFlags = edgeFlags;
631 msg.body.motion.metaState = metaState;
632 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800633 msg.body.motion.classification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700634 msg.body.motion.dsdx = transform.dsdx();
635 msg.body.motion.dtdx = transform.dtdx();
636 msg.body.motion.dtdy = transform.dtdy();
637 msg.body.motion.dsdy = transform.dsdy();
638 msg.body.motion.tx = transform.tx();
639 msg.body.motion.ty = transform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700640 msg.body.motion.xPrecision = xPrecision;
641 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700642 msg.body.motion.xCursorPosition = xCursorPosition;
643 msg.body.motion.yCursorPosition = yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700644 msg.body.motion.dsdxRaw = rawTransform.dsdx();
645 msg.body.motion.dtdxRaw = rawTransform.dtdx();
646 msg.body.motion.dtdyRaw = rawTransform.dtdy();
647 msg.body.motion.dsdyRaw = rawTransform.dsdy();
648 msg.body.motion.txRaw = rawTransform.tx();
649 msg.body.motion.tyRaw = rawTransform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700650 msg.body.motion.downTime = downTime;
651 msg.body.motion.eventTime = eventTime;
652 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100653 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700654 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
655 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
656 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700657
Jeff Brown5912f952013-07-01 19:10:31 -0700658 return mChannel->sendMessage(&msg);
659}
660
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700661status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus) {
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800662 if (ATRACE_ENABLED()) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700663 std::string message = StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)",
664 mChannel->getName().c_str(), toString(hasFocus));
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800665 ATRACE_NAME(message.c_str());
666 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000667 ALOGD_IF(DEBUG_TRANSPORT_PUBLISHER, "channel '%s' publisher ~ %s: seq=%u, hasFocus=%s",
668 mChannel->getName().c_str(), __func__, seq, toString(hasFocus));
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800669
670 InputMessage msg;
671 msg.header.type = InputMessage::Type::FOCUS;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500672 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800673 msg.body.focus.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000674 msg.body.focus.hasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800675 return mChannel->sendMessage(&msg);
676}
677
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800678status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
679 bool pointerCaptureEnabled) {
680 if (ATRACE_ENABLED()) {
681 std::string message =
682 StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
683 mChannel->getName().c_str(), toString(pointerCaptureEnabled));
684 ATRACE_NAME(message.c_str());
685 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000686 ALOGD_IF(DEBUG_TRANSPORT_PUBLISHER,
687 "channel '%s' publisher ~ %s: seq=%u, pointerCaptureEnabled=%s",
688 mChannel->getName().c_str(), __func__, seq, toString(pointerCaptureEnabled));
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800689
690 InputMessage msg;
691 msg.header.type = InputMessage::Type::CAPTURE;
692 msg.header.seq = seq;
693 msg.body.capture.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000694 msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800695 return mChannel->sendMessage(&msg);
696}
697
arthurhung7632c332020-12-30 16:58:01 +0800698status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
699 bool isExiting) {
700 if (ATRACE_ENABLED()) {
701 std::string message =
702 StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
703 mChannel->getName().c_str(), x, y, toString(isExiting));
704 ATRACE_NAME(message.c_str());
705 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000706 ALOGD_IF(DEBUG_TRANSPORT_PUBLISHER,
707 "channel '%s' publisher ~ %s: seq=%u, x=%f, y=%f, isExiting=%s",
708 mChannel->getName().c_str(), __func__, seq, x, y, toString(isExiting));
arthurhung7632c332020-12-30 16:58:01 +0800709
710 InputMessage msg;
711 msg.header.type = InputMessage::Type::DRAG;
712 msg.header.seq = seq;
713 msg.body.drag.eventId = eventId;
714 msg.body.drag.isExiting = isExiting;
715 msg.body.drag.x = x;
716 msg.body.drag.y = y;
717 return mChannel->sendMessage(&msg);
718}
719
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700720status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) {
721 if (ATRACE_ENABLED()) {
722 std::string message =
723 StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
724 mChannel->getName().c_str(), toString(isInTouchMode));
725 ATRACE_NAME(message.c_str());
726 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000727 ALOGD_IF(DEBUG_TRANSPORT_PUBLISHER, "channel '%s' publisher ~ %s: seq=%u, isInTouchMode=%s",
728 mChannel->getName().c_str(), __func__, seq, toString(isInTouchMode));
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700729
730 InputMessage msg;
731 msg.header.type = InputMessage::Type::TOUCH_MODE;
732 msg.header.seq = seq;
733 msg.body.touchMode.eventId = eventId;
734 msg.body.touchMode.isInTouchMode = isInTouchMode;
735 return mChannel->sendMessage(&msg);
736}
737
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000738android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000739 ALOGD_IF(DEBUG_TRANSPORT_PUBLISHER, "channel '%s' publisher ~ %s", mChannel->getName().c_str(),
740 __func__);
Jeff Brown5912f952013-07-01 19:10:31 -0700741
742 InputMessage msg;
743 status_t result = mChannel->receiveMessage(&msg);
744 if (result) {
Siarhei Vishniakoueedd0fc2021-03-12 09:50:36 +0000745 return android::base::Error(result);
Jeff Brown5912f952013-07-01 19:10:31 -0700746 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000747 if (msg.header.type == InputMessage::Type::FINISHED) {
748 return Finished{
749 .seq = msg.header.seq,
750 .handled = msg.body.finished.handled,
751 .consumeTime = msg.body.finished.consumeTime,
752 };
Jeff Brown5912f952013-07-01 19:10:31 -0700753 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000754
755 if (msg.header.type == InputMessage::Type::TIMELINE) {
756 return Timeline{
757 .inputEventId = msg.body.timeline.eventId,
758 .graphicsTimeline = msg.body.timeline.graphicsTimeline,
759 };
760 }
761
762 ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer",
Dominik Laskowski75788452021-02-09 18:51:25 -0800763 mChannel->getName().c_str(), ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000764 return android::base::Error(UNKNOWN_ERROR);
Jeff Brown5912f952013-07-01 19:10:31 -0700765}
766
767// --- InputConsumer ---
768
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500769InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel)
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800770 : InputConsumer(channel, isTouchResamplingEnabled()) {}
771
772InputConsumer::InputConsumer(const std::shared_ptr<InputChannel>& channel,
773 bool enableTouchResampling)
774 : mResampleTouch(enableTouchResampling), mChannel(channel), mMsgDeferred(false) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700775
776InputConsumer::~InputConsumer() {
777}
778
779bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600780 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700781}
782
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800783status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
784 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000785 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
786 "channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
787 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700788
789 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700790 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700791
792 // Fetch the next input message.
793 // Loop until an event can be returned or no additional events are received.
794 while (!*outEvent) {
795 if (mMsgDeferred) {
796 // mMsg contains a valid input message from the previous call to consume
797 // that has not yet been processed.
798 mMsgDeferred = false;
799 } else {
800 // Receive a fresh message.
801 status_t result = mChannel->receiveMessage(&mMsg);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000802 if (result == OK) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800803 const auto [_, inserted] =
804 mConsumeTimes.emplace(mMsg.header.seq, systemTime(SYSTEM_TIME_MONOTONIC));
805 LOG_ALWAYS_FATAL_IF(!inserted, "Already have a consume time for seq=%" PRIu32,
806 mMsg.header.seq);
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000807 }
Jeff Brown5912f952013-07-01 19:10:31 -0700808 if (result) {
809 // Consume the next batched event unless batches are being held for later.
810 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800811 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700812 if (*outEvent) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000813 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
814 "channel '%s' consumer ~ consumed batch event, seq=%u",
815 mChannel->getName().c_str(), *outSeq);
Jeff Brown5912f952013-07-01 19:10:31 -0700816 break;
817 }
818 }
819 return result;
820 }
821 }
822
823 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700824 case InputMessage::Type::KEY: {
825 KeyEvent* keyEvent = factory->createKeyEvent();
826 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700827
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700828 initializeKeyEvent(keyEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500829 *outSeq = mMsg.header.seq;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700830 *outEvent = keyEvent;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000831 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
832 "channel '%s' consumer ~ consumed key event, seq=%u",
833 mChannel->getName().c_str(), *outSeq);
834 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700835 }
Jeff Brown5912f952013-07-01 19:10:31 -0700836
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700837 case InputMessage::Type::MOTION: {
838 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
839 if (batchIndex >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500840 Batch& batch = mBatches[batchIndex];
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700841 if (canAddSample(batch, &mMsg)) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500842 batch.samples.push_back(mMsg);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000843 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
844 "channel '%s' consumer ~ appended to batch event",
845 mChannel->getName().c_str());
846 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700847 } else if (isPointerEvent(mMsg.body.motion.source) &&
848 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
849 // No need to process events that we are going to cancel anyways
850 const size_t count = batch.samples.size();
851 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500852 const InputMessage& msg = batch.samples[i];
853 sendFinishedSignal(msg.header.seq, false);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700854 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500855 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
856 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700857 } else {
858 // We cannot append to the batch in progress, so we need to consume
859 // the previous batch right now and defer the new message until later.
860 mMsgDeferred = true;
861 status_t result = consumeSamples(factory, batch, batch.samples.size(),
862 outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500863 mBatches.erase(mBatches.begin() + batchIndex);
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700864 if (result) {
865 return result;
866 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000867 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
868 "channel '%s' consumer ~ consumed batch event and "
869 "deferred current event, seq=%u",
870 mChannel->getName().c_str(), *outSeq);
871 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700872 }
Jeff Brown5912f952013-07-01 19:10:31 -0700873 }
Jeff Brown5912f952013-07-01 19:10:31 -0700874
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800875 // Start a new batch if needed.
876 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
877 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500878 Batch batch;
879 batch.samples.push_back(mMsg);
880 mBatches.push_back(batch);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000881 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
882 "channel '%s' consumer ~ started batch event",
883 mChannel->getName().c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800884 break;
885 }
Jeff Brown5912f952013-07-01 19:10:31 -0700886
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800887 MotionEvent* motionEvent = factory->createMotionEvent();
888 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700889
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800890 updateTouchState(mMsg);
891 initializeMotionEvent(motionEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500892 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800893 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800894
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000895 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
896 "channel '%s' consumer ~ consumed motion event, seq=%u",
897 mChannel->getName().c_str(), *outSeq);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800898 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700899 }
Jeff Brown5912f952013-07-01 19:10:31 -0700900
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000901 case InputMessage::Type::FINISHED:
902 case InputMessage::Type::TIMELINE: {
Siarhei Vishniakou7766c032021-03-02 20:32:20 +0000903 LOG_ALWAYS_FATAL("Consumed a %s message, which should never be seen by "
904 "InputConsumer!",
Dominik Laskowski75788452021-02-09 18:51:25 -0800905 ftl::enum_string(mMsg.header.type).c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800906 break;
907 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800908
909 case InputMessage::Type::FOCUS: {
910 FocusEvent* focusEvent = factory->createFocusEvent();
911 if (!focusEvent) return NO_MEMORY;
912
913 initializeFocusEvent(focusEvent, &mMsg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500914 *outSeq = mMsg.header.seq;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800915 *outEvent = focusEvent;
916 break;
917 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800918
919 case InputMessage::Type::CAPTURE: {
920 CaptureEvent* captureEvent = factory->createCaptureEvent();
921 if (!captureEvent) return NO_MEMORY;
922
923 initializeCaptureEvent(captureEvent, &mMsg);
924 *outSeq = mMsg.header.seq;
925 *outEvent = captureEvent;
926 break;
927 }
arthurhung7632c332020-12-30 16:58:01 +0800928
929 case InputMessage::Type::DRAG: {
930 DragEvent* dragEvent = factory->createDragEvent();
931 if (!dragEvent) return NO_MEMORY;
932
933 initializeDragEvent(dragEvent, &mMsg);
934 *outSeq = mMsg.header.seq;
935 *outEvent = dragEvent;
936 break;
937 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700938
939 case InputMessage::Type::TOUCH_MODE: {
940 TouchModeEvent* touchModeEvent = factory->createTouchModeEvent();
941 if (!touchModeEvent) return NO_MEMORY;
942
943 initializeTouchModeEvent(touchModeEvent, &mMsg);
944 *outSeq = mMsg.header.seq;
945 *outEvent = touchModeEvent;
946 break;
947 }
Jeff Brown5912f952013-07-01 19:10:31 -0700948 }
949 }
950 return OK;
951}
952
953status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800954 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700955 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700956 for (size_t i = mBatches.size(); i > 0; ) {
957 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500958 Batch& batch = mBatches[i];
Michael Wright32232172013-10-21 12:05:22 -0700959 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800960 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500961 mBatches.erase(mBatches.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -0700962 return result;
963 }
964
Michael Wright32232172013-10-21 12:05:22 -0700965 nsecs_t sampleTime = frameTime;
966 if (mResampleTouch) {
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -0800967 sampleTime -= std::chrono::nanoseconds(RESAMPLE_LATENCY).count();
Michael Wright32232172013-10-21 12:05:22 -0700968 }
Jeff Brown5912f952013-07-01 19:10:31 -0700969 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
970 if (split < 0) {
971 continue;
972 }
973
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800974 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700975 const InputMessage* next;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500976 if (batch.samples.empty()) {
977 mBatches.erase(mBatches.begin() + i);
Yi Kong5bed83b2018-07-17 12:53:47 -0700978 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700979 } else {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500980 next = &batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -0700981 }
Michael Wright32232172013-10-21 12:05:22 -0700982 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700983 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
984 }
985 return result;
986 }
987
988 return WOULD_BLOCK;
989}
990
991status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800992 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700993 MotionEvent* motionEvent = factory->createMotionEvent();
994 if (! motionEvent) return NO_MEMORY;
995
996 uint32_t chain = 0;
997 for (size_t i = 0; i < count; i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500998 InputMessage& msg = batch.samples[i];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100999 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -07001000 if (i) {
1001 SeqChain seqChain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001002 seqChain.seq = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -07001003 seqChain.chain = chain;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001004 mSeqChains.push_back(seqChain);
Jeff Brown5912f952013-07-01 19:10:31 -07001005 addSample(motionEvent, &msg);
1006 } else {
1007 initializeMotionEvent(motionEvent, &msg);
1008 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001009 chain = msg.header.seq;
Jeff Brown5912f952013-07-01 19:10:31 -07001010 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001011 batch.samples.erase(batch.samples.begin(), batch.samples.begin() + count);
Jeff Brown5912f952013-07-01 19:10:31 -07001012
1013 *outSeq = chain;
1014 *outEvent = motionEvent;
1015 return OK;
1016}
1017
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001018void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001019 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -07001020 return;
1021 }
1022
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001023 int32_t deviceId = msg.body.motion.deviceId;
1024 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -07001025
1026 // Update the touch state history to incorporate the new input message.
1027 // If the message is in the past relative to the most recently produced resampled
1028 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001029 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -07001030 case AMOTION_EVENT_ACTION_DOWN: {
1031 ssize_t index = findTouchState(deviceId, source);
1032 if (index < 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001033 mTouchStates.push_back({});
Jeff Brown5912f952013-07-01 19:10:31 -07001034 index = mTouchStates.size() - 1;
1035 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001036 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001037 touchState.initialize(deviceId, source);
1038 touchState.addHistory(msg);
1039 break;
1040 }
1041
1042 case AMOTION_EVENT_ACTION_MOVE: {
1043 ssize_t index = findTouchState(deviceId, source);
1044 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001045 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001046 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001047 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -07001048 }
1049 break;
1050 }
1051
1052 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
1053 ssize_t index = findTouchState(deviceId, source);
1054 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001055 TouchState& touchState = mTouchStates[index];
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001056 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001057 rewriteMessage(touchState, msg);
1058 }
1059 break;
1060 }
1061
1062 case AMOTION_EVENT_ACTION_POINTER_UP: {
1063 ssize_t index = findTouchState(deviceId, source);
1064 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001065 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001066 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001067 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -07001068 }
1069 break;
1070 }
1071
1072 case AMOTION_EVENT_ACTION_SCROLL: {
1073 ssize_t index = findTouchState(deviceId, source);
1074 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001075 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001076 rewriteMessage(touchState, msg);
1077 }
1078 break;
1079 }
1080
1081 case AMOTION_EVENT_ACTION_UP:
1082 case AMOTION_EVENT_ACTION_CANCEL: {
1083 ssize_t index = findTouchState(deviceId, source);
1084 if (index >= 0) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001085 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001086 rewriteMessage(touchState, msg);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001087 mTouchStates.erase(mTouchStates.begin() + index);
Jeff Brown5912f952013-07-01 19:10:31 -07001088 }
1089 break;
1090 }
1091 }
1092}
1093
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001094/**
1095 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
1096 *
1097 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
1098 * is in the past relative to msg and the past two events do not contain identical coordinates),
1099 * then invalidate the lastResample data for that pointer.
1100 * If the two past events have identical coordinates, then lastResample data for that pointer will
1101 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
1102 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
1103 * not equal to x0 is received.
1104 */
1105void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001106 nsecs_t eventTime = msg.body.motion.eventTime;
1107 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1108 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -07001109 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001110 if (eventTime < state.lastResample.eventTime ||
1111 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001112 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
1113 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001114 ALOGD_IF(DEBUG_RESAMPLING, "[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
1115 resampleCoords.getX(), resampleCoords.getY(), msgCoords.getX(),
1116 msgCoords.getY());
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001117 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
1118 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
Philip Quinnafb31282022-12-20 18:17:55 -08001119 msgCoords.isResampled = true;
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001120 } else {
1121 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001122 }
Jeff Brown5912f952013-07-01 19:10:31 -07001123 }
1124 }
1125}
1126
1127void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
1128 const InputMessage* next) {
1129 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +08001130 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -07001131 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
1132 return;
1133 }
1134
1135 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
1136 if (index < 0) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001137 ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, no touch state for device.");
Jeff Brown5912f952013-07-01 19:10:31 -07001138 return;
1139 }
1140
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001141 TouchState& touchState = mTouchStates[index];
Jeff Brown5912f952013-07-01 19:10:31 -07001142 if (touchState.historySize < 1) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001143 ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, no history for device.");
Jeff Brown5912f952013-07-01 19:10:31 -07001144 return;
1145 }
1146
1147 // Ensure that the current sample has all of the pointers that need to be reported.
1148 const History* current = touchState.getHistory(0);
1149 size_t pointerCount = event->getPointerCount();
1150 for (size_t i = 0; i < pointerCount; i++) {
1151 uint32_t id = event->getPointerId(i);
1152 if (!current->idBits.hasBit(id)) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001153 ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, missing id %d", id);
Jeff Brown5912f952013-07-01 19:10:31 -07001154 return;
1155 }
1156 }
1157
1158 // Find the data to use for resampling.
1159 const History* other;
1160 History future;
1161 float alpha;
1162 if (next) {
1163 // Interpolate between current sample and future sample.
1164 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +01001165 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -07001166 other = &future;
1167 nsecs_t delta = future.eventTime - current->eventTime;
1168 if (delta < RESAMPLE_MIN_DELTA) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001169 ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, delta time is too small: %" PRId64 " ns.",
1170 delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001171 return;
1172 }
1173 alpha = float(sampleTime - current->eventTime) / delta;
1174 } else if (touchState.historySize >= 2) {
1175 // Extrapolate future sample using current sample and past sample.
1176 // So other->eventTime <= current->eventTime <= sampleTime.
1177 other = touchState.getHistory(1);
1178 nsecs_t delta = current->eventTime - other->eventTime;
1179 if (delta < RESAMPLE_MIN_DELTA) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001180 ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, delta time is too small: %" PRId64 " ns.",
1181 delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -07001182 return;
1183 } else if (delta > RESAMPLE_MAX_DELTA) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001184 ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, delta time is too large: %" PRId64 " ns.",
1185 delta);
Jeff Brown5912f952013-07-01 19:10:31 -07001186 return;
1187 }
1188 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
1189 if (sampleTime > maxPredict) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001190 ALOGD_IF(DEBUG_RESAMPLING,
1191 "Sample time is too far in the future, adjusting prediction "
1192 "from %" PRId64 " to %" PRId64 " ns.",
1193 sampleTime - current->eventTime, maxPredict - current->eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001194 sampleTime = maxPredict;
1195 }
1196 alpha = float(current->eventTime - sampleTime) / delta;
1197 } else {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001198 ALOGD_IF(DEBUG_RESAMPLING, "Not resampled, insufficient data.");
Jeff Brown5912f952013-07-01 19:10:31 -07001199 return;
1200 }
1201
Siarhei Vishniakou0ced3cc2017-11-21 15:33:17 -08001202 if (current->eventTime == sampleTime) {
1203 // Prevents having 2 events with identical times and coordinates.
1204 return;
1205 }
1206
Jeff Brown5912f952013-07-01 19:10:31 -07001207 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001208 History oldLastResample;
1209 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001210 touchState.lastResample.eventTime = sampleTime;
1211 touchState.lastResample.idBits.clear();
1212 for (size_t i = 0; i < pointerCount; i++) {
1213 uint32_t id = event->getPointerId(i);
1214 touchState.lastResample.idToIndex[id] = i;
1215 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001216 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1217 // We maintain the previously resampled value for this pointer (stored in
1218 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1219 // This way we don't introduce artificial jitter when pointers haven't actually moved.
Philip Quinnafb31282022-12-20 18:17:55 -08001220 // The isResampled flag isn't cleared as the values don't reflect what the device is
1221 // actually reporting.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001222
1223 // We know here that the coordinates for the pointer haven't changed because we
1224 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1225 // lastResample in place becasue the mapping from pointer ID to index may have changed.
1226 touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id));
1227 continue;
1228 }
1229
Jeff Brown5912f952013-07-01 19:10:31 -07001230 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1231 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001232 resampledCoords.copyFrom(currentCoords);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001233 if (other->idBits.hasBit(id) && shouldResampleTool(event->getToolType(i))) {
Jeff Brown5912f952013-07-01 19:10:31 -07001234 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001235 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001236 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
Jeff Brown5912f952013-07-01 19:10:31 -07001237 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001238 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
Philip Quinnafb31282022-12-20 18:17:55 -08001239 resampledCoords.isResampled = true;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001240 ALOGD_IF(DEBUG_RESAMPLING,
1241 "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1242 "other (%0.3f, %0.3f), alpha %0.3f",
1243 id, resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(),
1244 currentCoords.getY(), otherCoords.getX(), otherCoords.getY(), alpha);
Jeff Brown5912f952013-07-01 19:10:31 -07001245 } else {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001246 ALOGD_IF(DEBUG_RESAMPLING, "[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", id,
1247 resampledCoords.getX(), resampledCoords.getY(), currentCoords.getX(),
1248 currentCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -07001249 }
1250 }
1251
1252 event->addSample(sampleTime, touchState.lastResample.pointers);
1253}
1254
1255bool InputConsumer::shouldResampleTool(int32_t toolType) {
1256 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
1257 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1258}
1259
1260status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001261 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
1262 "channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1263 mChannel->getName().c_str(), seq, toString(handled));
Jeff Brown5912f952013-07-01 19:10:31 -07001264
1265 if (!seq) {
1266 ALOGE("Attempted to send a finished signal with sequence number 0.");
1267 return BAD_VALUE;
1268 }
1269
1270 // Send finished signals for the batch sequence chain first.
1271 size_t seqChainCount = mSeqChains.size();
1272 if (seqChainCount) {
1273 uint32_t currentSeq = seq;
1274 uint32_t chainSeqs[seqChainCount];
1275 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001276 for (size_t i = seqChainCount; i > 0; ) {
1277 i--;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001278 const SeqChain& seqChain = mSeqChains[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001279 if (seqChain.seq == currentSeq) {
1280 currentSeq = seqChain.chain;
1281 chainSeqs[chainIndex++] = currentSeq;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001282 mSeqChains.erase(mSeqChains.begin() + i);
Jeff Brown5912f952013-07-01 19:10:31 -07001283 }
1284 }
1285 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001286 while (!status && chainIndex > 0) {
1287 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001288 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1289 }
1290 if (status) {
1291 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001292 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001293 SeqChain seqChain;
1294 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1295 seqChain.chain = chainSeqs[chainIndex];
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001296 mSeqChains.push_back(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001297 if (!chainIndex) break;
1298 chainIndex--;
1299 }
Jeff Brown5912f952013-07-01 19:10:31 -07001300 return status;
1301 }
1302 }
1303
1304 // Send finished signal for the last message in the batch.
1305 return sendUnchainedFinishedSignal(seq, handled);
1306}
1307
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001308status_t InputConsumer::sendTimeline(int32_t inputEventId,
1309 std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +00001310 ALOGD_IF(DEBUG_TRANSPORT_CONSUMER,
1311 "channel '%s' consumer ~ sendTimeline: inputEventId=%" PRId32
1312 ", gpuCompletedTime=%" PRId64 ", presentTime=%" PRId64,
1313 mChannel->getName().c_str(), inputEventId,
1314 graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME],
1315 graphicsTimeline[GraphicsTimeline::PRESENT_TIME]);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001316
1317 InputMessage msg;
1318 msg.header.type = InputMessage::Type::TIMELINE;
1319 msg.header.seq = 0;
1320 msg.body.timeline.eventId = inputEventId;
1321 msg.body.timeline.graphicsTimeline = std::move(graphicsTimeline);
1322 return mChannel->sendMessage(&msg);
1323}
1324
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001325nsecs_t InputConsumer::getConsumeTime(uint32_t seq) const {
1326 auto it = mConsumeTimes.find(seq);
1327 // Consume time will be missing if either 'finishInputEvent' is called twice, or if it was
1328 // called for the wrong (synthetic?) input event. Either way, it is a bug that should be fixed.
1329 LOG_ALWAYS_FATAL_IF(it == mConsumeTimes.end(), "Could not find consume time for seq=%" PRIu32,
1330 seq);
1331 return it->second;
1332}
1333
1334void InputConsumer::popConsumeTime(uint32_t seq) {
1335 mConsumeTimes.erase(seq);
1336}
1337
Jeff Brown5912f952013-07-01 19:10:31 -07001338status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1339 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001340 msg.header.type = InputMessage::Type::FINISHED;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001341 msg.header.seq = seq;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001342 msg.body.finished.handled = handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001343 msg.body.finished.consumeTime = getConsumeTime(seq);
1344 status_t result = mChannel->sendMessage(&msg);
1345 if (result == OK) {
1346 // Remove the consume time if the socket write succeeded. We will not need to ack this
1347 // message anymore. If the socket write did not succeed, we will try again and will still
1348 // need consume time.
1349 popConsumeTime(seq);
1350 }
1351 return result;
Jeff Brown5912f952013-07-01 19:10:31 -07001352}
1353
Jeff Brown5912f952013-07-01 19:10:31 -07001354bool InputConsumer::hasPendingBatch() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001355 return !mBatches.empty();
Jeff Brown5912f952013-07-01 19:10:31 -07001356}
1357
Arthur Hungc7812be2020-02-27 22:40:27 +08001358int32_t InputConsumer::getPendingBatchSource() const {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001359 if (mBatches.empty()) {
Arthur Hungc7812be2020-02-27 22:40:27 +08001360 return AINPUT_SOURCE_CLASS_NONE;
1361 }
1362
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001363 const Batch& batch = mBatches[0];
1364 const InputMessage& head = batch.samples[0];
Arthur Hungc7812be2020-02-27 22:40:27 +08001365 return head.body.motion.source;
1366}
1367
Jeff Brown5912f952013-07-01 19:10:31 -07001368ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1369 for (size_t i = 0; i < mBatches.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001370 const Batch& batch = mBatches[i];
1371 const InputMessage& head = batch.samples[0];
Jeff Brown5912f952013-07-01 19:10:31 -07001372 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1373 return i;
1374 }
1375 }
1376 return -1;
1377}
1378
1379ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1380 for (size_t i = 0; i < mTouchStates.size(); i++) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001381 const TouchState& touchState = mTouchStates[i];
Jeff Brown5912f952013-07-01 19:10:31 -07001382 if (touchState.deviceId == deviceId && touchState.source == source) {
1383 return i;
1384 }
1385 }
1386 return -1;
1387}
1388
1389void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Garfield Tan1c7bc862020-01-28 13:24:04 -08001390 event->initialize(msg->body.key.eventId, msg->body.key.deviceId, msg->body.key.source,
Garfield Tanfbe732e2020-01-24 11:26:14 -08001391 msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
1392 msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
1393 msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
1394 msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001395}
1396
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001397void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001398 event->initialize(msg->body.focus.eventId, msg->body.focus.hasFocus);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001399}
1400
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001401void InputConsumer::initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg) {
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +00001402 event->initialize(msg->body.capture.eventId, msg->body.capture.pointerCaptureEnabled);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001403}
1404
arthurhung7632c332020-12-30 16:58:01 +08001405void InputConsumer::initializeDragEvent(DragEvent* event, const InputMessage* msg) {
1406 event->initialize(msg->body.drag.eventId, msg->body.drag.x, msg->body.drag.y,
1407 msg->body.drag.isExiting);
1408}
1409
Jeff Brown5912f952013-07-01 19:10:31 -07001410void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001411 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001412 PointerProperties pointerProperties[pointerCount];
1413 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001414 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001415 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
1416 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1417 }
1418
chaviw9eaa22c2020-07-01 16:21:27 -07001419 ui::Transform transform;
1420 transform.set({msg->body.motion.dsdx, msg->body.motion.dtdx, msg->body.motion.tx,
1421 msg->body.motion.dtdy, msg->body.motion.dsdy, msg->body.motion.ty, 0, 0, 1});
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001422 ui::Transform displayTransform;
1423 displayTransform.set({msg->body.motion.dsdxRaw, msg->body.motion.dtdxRaw,
1424 msg->body.motion.txRaw, msg->body.motion.dtdyRaw,
1425 msg->body.motion.dsdyRaw, msg->body.motion.tyRaw, 0, 0, 1});
Garfield Tan1c7bc862020-01-28 13:24:04 -08001426 event->initialize(msg->body.motion.eventId, msg->body.motion.deviceId, msg->body.motion.source,
1427 msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
1428 msg->body.motion.actionButton, msg->body.motion.flags,
1429 msg->body.motion.edgeFlags, msg->body.motion.metaState,
chaviw9eaa22c2020-07-01 16:21:27 -07001430 msg->body.motion.buttonState, msg->body.motion.classification, transform,
1431 msg->body.motion.xPrecision, msg->body.motion.yPrecision,
1432 msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -07001433 displayTransform, msg->body.motion.downTime, msg->body.motion.eventTime,
1434 pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001435}
1436
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001437void InputConsumer::initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg) {
1438 event->initialize(msg->body.touchMode.eventId, msg->body.touchMode.isInTouchMode);
1439}
1440
Jeff Brown5912f952013-07-01 19:10:31 -07001441void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001442 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001443 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001444 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001445 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1446 }
1447
1448 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1449 event->addSample(msg->body.motion.eventTime, pointerCoords);
1450}
1451
1452bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001453 const InputMessage& head = batch.samples[0];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001454 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001455 if (head.body.motion.pointerCount != pointerCount
1456 || head.body.motion.action != msg->body.motion.action) {
1457 return false;
1458 }
1459 for (size_t i = 0; i < pointerCount; i++) {
1460 if (head.body.motion.pointers[i].properties
1461 != msg->body.motion.pointers[i].properties) {
1462 return false;
1463 }
1464 }
1465 return true;
1466}
1467
1468ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1469 size_t numSamples = batch.samples.size();
1470 size_t index = 0;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001471 while (index < numSamples && batch.samples[index].body.motion.eventTime <= time) {
Jeff Brown5912f952013-07-01 19:10:31 -07001472 index += 1;
1473 }
1474 return ssize_t(index) - 1;
1475}
1476
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001477std::string InputConsumer::dump() const {
1478 std::string out;
1479 out = out + "mResampleTouch = " + toString(mResampleTouch) + "\n";
1480 out = out + "mChannel = " + mChannel->getName() + "\n";
1481 out = out + "mMsgDeferred: " + toString(mMsgDeferred) + "\n";
1482 if (mMsgDeferred) {
Dominik Laskowski75788452021-02-09 18:51:25 -08001483 out = out + "mMsg : " + ftl::enum_string(mMsg.header.type) + "\n";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001484 }
1485 out += "Batches:\n";
1486 for (const Batch& batch : mBatches) {
1487 out += " Batch:\n";
1488 for (const InputMessage& msg : batch.samples) {
1489 out += android::base::StringPrintf(" Message %" PRIu32 ": %s ", msg.header.seq,
Dominik Laskowski75788452021-02-09 18:51:25 -08001490 ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001491 switch (msg.header.type) {
1492 case InputMessage::Type::KEY: {
1493 out += android::base::StringPrintf("action=%s keycode=%" PRId32,
1494 KeyEvent::actionToString(
1495 msg.body.key.action),
1496 msg.body.key.keyCode);
1497 break;
1498 }
1499 case InputMessage::Type::MOTION: {
1500 out = out + "action=" + MotionEvent::actionToString(msg.body.motion.action);
1501 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
1502 const float x = msg.body.motion.pointers[i].coords.getX();
1503 const float y = msg.body.motion.pointers[i].coords.getY();
1504 out += android::base::StringPrintf("\n Pointer %" PRIu32
1505 " : x=%.1f y=%.1f",
1506 i, x, y);
1507 }
1508 break;
1509 }
1510 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001511 out += android::base::StringPrintf("handled=%s, consumeTime=%" PRId64,
1512 toString(msg.body.finished.handled),
1513 msg.body.finished.consumeTime);
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001514 break;
1515 }
1516 case InputMessage::Type::FOCUS: {
Antonio Kantek3cfec7b2021-11-05 18:26:17 -07001517 out += android::base::StringPrintf("hasFocus=%s",
1518 toString(msg.body.focus.hasFocus));
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001519 break;
1520 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -08001521 case InputMessage::Type::CAPTURE: {
1522 out += android::base::StringPrintf("hasCapture=%s",
1523 toString(msg.body.capture
1524 .pointerCaptureEnabled));
1525 break;
1526 }
arthurhung7632c332020-12-30 16:58:01 +08001527 case InputMessage::Type::DRAG: {
1528 out += android::base::StringPrintf("x=%.1f y=%.1f, isExiting=%s",
1529 msg.body.drag.x, msg.body.drag.y,
1530 toString(msg.body.drag.isExiting));
1531 break;
1532 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +00001533 case InputMessage::Type::TIMELINE: {
1534 const nsecs_t gpuCompletedTime =
1535 msg.body.timeline
1536 .graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
1537 const nsecs_t presentTime =
1538 msg.body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
1539 out += android::base::StringPrintf("inputEventId=%" PRId32
1540 ", gpuCompletedTime=%" PRId64
1541 ", presentTime=%" PRId64,
1542 msg.body.timeline.eventId, gpuCompletedTime,
1543 presentTime);
1544 break;
1545 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -07001546 case InputMessage::Type::TOUCH_MODE: {
1547 out += android::base::StringPrintf("isInTouchMode=%s",
1548 toString(msg.body.touchMode.isInTouchMode));
1549 break;
1550 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001551 }
1552 out += "\n";
1553 }
1554 }
1555 if (mBatches.empty()) {
1556 out += " <empty>\n";
1557 }
1558 out += "mSeqChains:\n";
1559 for (const SeqChain& chain : mSeqChains) {
1560 out += android::base::StringPrintf(" chain: seq = %" PRIu32 " chain=%" PRIu32, chain.seq,
1561 chain.chain);
1562 }
1563 if (mSeqChains.empty()) {
1564 out += " <empty>\n";
1565 }
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -10001566 out += "mConsumeTimes:\n";
1567 for (const auto& [seq, consumeTime] : mConsumeTimes) {
1568 out += android::base::StringPrintf(" seq = %" PRIu32 " consumeTime = %" PRId64, seq,
1569 consumeTime);
1570 }
1571 if (mConsumeTimes.empty()) {
1572 out += " <empty>\n";
1573 }
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -05001574 return out;
1575}
1576
Jeff Brown5912f952013-07-01 19:10:31 -07001577} // namespace android