blob: 77dcaa9ef2ec5280f419e82c5d3dd03c1d3052f1 [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>
Siarhei Vishniakou6c1e6222024-03-07 13:39:24 -080029#include <input/PrintTools.h>
Prabir Pradhana37bad12023-08-18 15:55:32 +000030#include <input/TraceTools.h>
Jeff Brown5912f952013-07-01 19:10:31 -070031
Siarhei Vishniakou96818962023-08-23 10:19:02 -070032namespace input_flags = com::android::input::flags;
33
Siarhei Vishniakou9dbf6372024-03-06 16:08:01 -080034namespace android {
35
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000036namespace {
37
38/**
39 * Log debug messages about channel messages (send message, receive message).
40 * Enable this via "adb shell setprop log.tag.InputTransportMessages DEBUG"
41 * (requires restart)
42 */
43const bool DEBUG_CHANNEL_MESSAGES =
44 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Messages", ANDROID_LOG_INFO);
45
46/**
47 * Log debug messages whenever InputChannel objects are created/destroyed.
48 * Enable this via "adb shell setprop log.tag.InputTransportLifecycle DEBUG"
49 * (requires restart)
50 */
51const bool DEBUG_CHANNEL_LIFECYCLE =
52 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Lifecycle", ANDROID_LOG_INFO);
53
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +000054const bool IS_DEBUGGABLE_BUILD =
55#if defined(__ANDROID__)
56 android::base::GetBoolProperty("ro.debuggable", false);
57#else
58 true;
59#endif
60
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000061/**
62 * Log debug messages relating to the producer end of the transport channel.
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +000063 * Enable this via "adb shell setprop log.tag.InputTransportPublisher DEBUG".
64 * This requires a restart on non-debuggable (e.g. user) builds, but should take effect immediately
65 * on debuggable builds (e.g. userdebug).
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000066 */
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +000067bool debugTransportPublisher() {
68 if (!IS_DEBUGGABLE_BUILD) {
69 static const bool DEBUG_TRANSPORT_PUBLISHER =
70 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Publisher", ANDROID_LOG_INFO);
71 return DEBUG_TRANSPORT_PUBLISHER;
72 }
73 return __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Publisher", ANDROID_LOG_INFO);
74}
Prabir Pradhan60dd97a2023-02-23 02:23:02 +000075
Siarhei Vishniakou8d660132024-01-11 16:48:44 -080076android::base::unique_fd dupChannelFd(int fd) {
77 android::base::unique_fd newFd(::dup(fd));
78 if (!newFd.ok()) {
79 ALOGE("Could not duplicate fd %i : %s", fd, strerror(errno));
80 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
81 // If this process is out of file descriptors, then throwing that might end up exploding
82 // on the other side of a binder call, which isn't really helpful.
83 // Better to just crash here and hope that the FD leak is slow.
84 // Other failures could be client errors, so we still propagate those back to the caller.
85 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel");
86 return {};
87 }
88 return newFd;
89}
90
Jeff Brown5912f952013-07-01 19:10:31 -070091// Socket buffer size. The default is typically about 128KB, which is much larger than
92// we really need. So we make it smaller. It just needs to be big enough to hold
93// a few dozen large multi-finger motion events in the case where an application gets
94// behind processing touches.
Siarhei Vishniakou0438ca82024-03-12 14:27:25 -070095constexpr size_t SOCKET_BUFFER_SIZE = 32 * 1024;
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -060096
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -080097/**
98 * Crash if the events that are getting sent to the InputPublisher are inconsistent.
99 * Enable this via "adb shell setprop log.tag.InputTransportVerifyEvents DEBUG"
100 */
Siarhei Vishniakou6c1e6222024-03-07 13:39:24 -0800101bool verifyEvents() {
Siarhei Vishniakou96818962023-08-23 10:19:02 -0700102 return input_flags::enable_outbound_event_verification() ||
103 __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "VerifyEvents", ANDROID_LOG_INFO);
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800104}
105
Siarhei Vishniakou6c1e6222024-03-07 13:39:24 -0800106} // namespace
107
108using android::base::Result;
109using android::base::StringPrintf;
110
Jeff Brown5912f952013-07-01 19:10:31 -0700111// --- InputMessage ---
112
113bool InputMessage::isValid(size_t actualSize) const {
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000114 if (size() != actualSize) {
115 ALOGE("Received message of incorrect size %zu (expected %zu)", actualSize, size());
116 return false;
117 }
118
119 switch (header.type) {
120 case Type::KEY:
121 return true;
122 case Type::MOTION: {
123 const bool valid =
124 body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
125 if (!valid) {
126 ALOGE("Received invalid MOTION: pointerCount = %" PRIu32, body.motion.pointerCount);
127 }
128 return valid;
129 }
130 case Type::FINISHED:
131 case Type::FOCUS:
132 case Type::CAPTURE:
133 case Type::DRAG:
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700134 case Type::TOUCH_MODE:
Siarhei Vishniakoudbdb6732021-04-26 19:40:26 +0000135 return true;
136 case Type::TIMELINE: {
137 const nsecs_t gpuCompletedTime =
138 body.timeline.graphicsTimeline[GraphicsTimeline::GPU_COMPLETED_TIME];
139 const nsecs_t presentTime =
140 body.timeline.graphicsTimeline[GraphicsTimeline::PRESENT_TIME];
141 const bool valid = presentTime > gpuCompletedTime;
142 if (!valid) {
143 ALOGE("Received invalid TIMELINE: gpuCompletedTime = %" PRId64
144 " presentTime = %" PRId64,
145 gpuCompletedTime, presentTime);
146 }
147 return valid;
Jeff Brown5912f952013-07-01 19:10:31 -0700148 }
149 }
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000150 ALOGE("Invalid message type: %s", ftl::enum_string(header.type).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700151 return false;
152}
153
154size_t InputMessage::size() const {
155 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700156 case Type::KEY:
157 return sizeof(Header) + body.key.size();
158 case Type::MOTION:
159 return sizeof(Header) + body.motion.size();
160 case Type::FINISHED:
161 return sizeof(Header) + body.finished.size();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800162 case Type::FOCUS:
163 return sizeof(Header) + body.focus.size();
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800164 case Type::CAPTURE:
165 return sizeof(Header) + body.capture.size();
arthurhung7632c332020-12-30 16:58:01 +0800166 case Type::DRAG:
167 return sizeof(Header) + body.drag.size();
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000168 case Type::TIMELINE:
169 return sizeof(Header) + body.timeline.size();
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700170 case Type::TOUCH_MODE:
171 return sizeof(Header) + body.touchMode.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700172 }
173 return sizeof(Header);
174}
175
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800176/**
177 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
178 * memory to zero, then only copy the valid bytes on a per-field basis.
179 */
180void InputMessage::getSanitizedCopy(InputMessage* msg) const {
181 memset(msg, 0, sizeof(*msg));
182
183 // Write the header
184 msg->header.type = header.type;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500185 msg->header.seq = header.seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800186
187 // Write the body
188 switch(header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700189 case InputMessage::Type::KEY: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800190 // int32_t eventId
191 msg->body.key.eventId = body.key.eventId;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800192 // nsecs_t eventTime
193 msg->body.key.eventTime = body.key.eventTime;
194 // int32_t deviceId
195 msg->body.key.deviceId = body.key.deviceId;
196 // int32_t source
197 msg->body.key.source = body.key.source;
198 // int32_t displayId
199 msg->body.key.displayId = body.key.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600200 // std::array<uint8_t, 32> hmac
201 msg->body.key.hmac = body.key.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800202 // int32_t action
203 msg->body.key.action = body.key.action;
204 // int32_t flags
205 msg->body.key.flags = body.key.flags;
206 // int32_t keyCode
207 msg->body.key.keyCode = body.key.keyCode;
208 // int32_t scanCode
209 msg->body.key.scanCode = body.key.scanCode;
210 // int32_t metaState
211 msg->body.key.metaState = body.key.metaState;
212 // int32_t repeatCount
213 msg->body.key.repeatCount = body.key.repeatCount;
214 // nsecs_t downTime
215 msg->body.key.downTime = body.key.downTime;
216 break;
217 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700218 case InputMessage::Type::MOTION: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800219 // int32_t eventId
220 msg->body.motion.eventId = body.motion.eventId;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700221 // uint32_t pointerCount
222 msg->body.motion.pointerCount = body.motion.pointerCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800223 // nsecs_t eventTime
224 msg->body.motion.eventTime = body.motion.eventTime;
225 // int32_t deviceId
226 msg->body.motion.deviceId = body.motion.deviceId;
227 // int32_t source
228 msg->body.motion.source = body.motion.source;
229 // int32_t displayId
230 msg->body.motion.displayId = body.motion.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600231 // std::array<uint8_t, 32> hmac
232 msg->body.motion.hmac = body.motion.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800233 // int32_t action
234 msg->body.motion.action = body.motion.action;
235 // int32_t actionButton
236 msg->body.motion.actionButton = body.motion.actionButton;
237 // int32_t flags
238 msg->body.motion.flags = body.motion.flags;
239 // int32_t metaState
240 msg->body.motion.metaState = body.motion.metaState;
241 // int32_t buttonState
242 msg->body.motion.buttonState = body.motion.buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800243 // MotionClassification classification
244 msg->body.motion.classification = body.motion.classification;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800245 // int32_t edgeFlags
246 msg->body.motion.edgeFlags = body.motion.edgeFlags;
247 // nsecs_t downTime
248 msg->body.motion.downTime = body.motion.downTime;
chaviw9eaa22c2020-07-01 16:21:27 -0700249
250 msg->body.motion.dsdx = body.motion.dsdx;
251 msg->body.motion.dtdx = body.motion.dtdx;
252 msg->body.motion.dtdy = body.motion.dtdy;
253 msg->body.motion.dsdy = body.motion.dsdy;
254 msg->body.motion.tx = body.motion.tx;
255 msg->body.motion.ty = body.motion.ty;
256
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800257 // float xPrecision
258 msg->body.motion.xPrecision = body.motion.xPrecision;
259 // float yPrecision
260 msg->body.motion.yPrecision = body.motion.yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700261 // float xCursorPosition
262 msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
263 // float yCursorPosition
264 msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700265
266 msg->body.motion.dsdxRaw = body.motion.dsdxRaw;
267 msg->body.motion.dtdxRaw = body.motion.dtdxRaw;
268 msg->body.motion.dtdyRaw = body.motion.dtdyRaw;
269 msg->body.motion.dsdyRaw = body.motion.dsdyRaw;
270 msg->body.motion.txRaw = body.motion.txRaw;
271 msg->body.motion.tyRaw = body.motion.tyRaw;
272
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800273 //struct Pointer pointers[MAX_POINTERS]
274 for (size_t i = 0; i < body.motion.pointerCount; i++) {
275 // PointerProperties properties
276 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
277 msg->body.motion.pointers[i].properties.toolType =
278 body.motion.pointers[i].properties.toolType,
279 // PointerCoords coords
280 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
281 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
282 memcpy(&msg->body.motion.pointers[i].coords.values[0],
283 &body.motion.pointers[i].coords.values[0],
284 count * (sizeof(body.motion.pointers[i].coords.values[0])));
Philip Quinnafb31282022-12-20 18:17:55 -0800285 msg->body.motion.pointers[i].coords.isResampled =
286 body.motion.pointers[i].coords.isResampled;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800287 }
288 break;
289 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700290 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800291 msg->body.finished.handled = body.finished.handled;
Siarhei Vishniakou3531ae72021-02-02 12:12:27 -1000292 msg->body.finished.consumeTime = body.finished.consumeTime;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800293 break;
294 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800295 case InputMessage::Type::FOCUS: {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800296 msg->body.focus.eventId = body.focus.eventId;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800297 msg->body.focus.hasFocus = body.focus.hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800298 break;
299 }
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800300 case InputMessage::Type::CAPTURE: {
301 msg->body.capture.eventId = body.capture.eventId;
302 msg->body.capture.pointerCaptureEnabled = body.capture.pointerCaptureEnabled;
303 break;
304 }
arthurhung7632c332020-12-30 16:58:01 +0800305 case InputMessage::Type::DRAG: {
306 msg->body.drag.eventId = body.drag.eventId;
307 msg->body.drag.x = body.drag.x;
308 msg->body.drag.y = body.drag.y;
309 msg->body.drag.isExiting = body.drag.isExiting;
310 break;
311 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000312 case InputMessage::Type::TIMELINE: {
313 msg->body.timeline.eventId = body.timeline.eventId;
314 msg->body.timeline.graphicsTimeline = body.timeline.graphicsTimeline;
315 break;
316 }
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700317 case InputMessage::Type::TOUCH_MODE: {
318 msg->body.touchMode.eventId = body.touchMode.eventId;
319 msg->body.touchMode.isInTouchMode = body.touchMode.isInTouchMode;
320 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800321 }
322}
Jeff Brown5912f952013-07-01 19:10:31 -0700323
324// --- InputChannel ---
325
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500326std::unique_ptr<InputChannel> InputChannel::create(const std::string& name,
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500327 android::base::unique_fd fd, sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700328 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
329 if (result != 0) {
330 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
331 strerror(errno));
332 return nullptr;
333 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500334 // using 'new' to access a non-public constructor
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500335 return std::unique_ptr<InputChannel>(new InputChannel(name, std::move(fd), token));
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700336}
337
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800338std::unique_ptr<InputChannel> InputChannel::create(
339 android::os::InputChannelCore&& parceledChannel) {
340 return InputChannel::create(parceledChannel.name, parceledChannel.fd.release(),
341 parceledChannel.token);
342}
343
344InputChannel::InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token) {
345 this->name = std::move(name);
346 this->fd.reset(std::move(fd));
347 this->token = std::move(token);
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000348 ALOGD_IF(DEBUG_CHANNEL_LIFECYCLE, "Input channel constructed: name='%s', fd=%d",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800349 getName().c_str(), getFd());
Jeff Brown5912f952013-07-01 19:10:31 -0700350}
351
352InputChannel::~InputChannel() {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000353 ALOGD_IF(DEBUG_CHANNEL_LIFECYCLE, "Input channel destroyed: name='%s', fd=%d",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800354 getName().c_str(), getFd());
Robert Carr3720ed02018-08-08 16:08:27 -0700355}
356
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800357status_t InputChannel::openInputChannelPair(const std::string& name,
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500358 std::unique_ptr<InputChannel>& outServerChannel,
359 std::unique_ptr<InputChannel>& outClientChannel) {
Jeff Brown5912f952013-07-01 19:10:31 -0700360 int sockets[2];
361 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
362 status_t result = -errno;
Siarhei Vishniakou09b02ac2021-04-14 22:24:04 +0000363 ALOGE("channel '%s' ~ Could not create socket pair. errno=%s(%d)", name.c_str(),
364 strerror(errno), errno);
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500365 outServerChannel.reset();
366 outClientChannel.reset();
Jeff Brown5912f952013-07-01 19:10:31 -0700367 return result;
368 }
369
370 int bufferSize = SOCKET_BUFFER_SIZE;
371 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
372 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
373 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
374 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
375
Siarhei Vishniakou4c155eb2023-06-30 11:47:12 -0700376 sp<IBinder> token = sp<BBinder>::make();
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700377
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700378 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -0700379 outServerChannel = InputChannel::create(name, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700380
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700381 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou4125ea42024-07-01 16:38:17 -0700382 outClientChannel = InputChannel::create(name, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700383 return OK;
384}
385
386status_t InputChannel::sendMessage(const InputMessage* msg) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000387 ATRACE_NAME_IF(ATRACE_ENABLED(),
Harry Cuttsc57cd3c2024-04-24 13:52:55 +0000388 StringPrintf("sendMessage(inputChannel=%s, seq=0x%" PRIx32 ", type=%s)",
389 name.c_str(), msg->header.seq,
390 ftl::enum_string(msg->header.type).c_str()));
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800391 const size_t msgLength = msg->size();
392 InputMessage cleanMsg;
393 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700394 ssize_t nWrite;
395 do {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800396 nWrite = ::send(getFd(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700397 } while (nWrite == -1 && errno == EINTR);
398
399 if (nWrite < 0) {
400 int error = errno;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000401 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ error sending message of type %s, %s",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800402 name.c_str(), ftl::enum_string(msg->header.type).c_str(), strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700403 if (error == EAGAIN || error == EWOULDBLOCK) {
404 return WOULD_BLOCK;
405 }
406 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
407 return DEAD_OBJECT;
408 }
409 return -error;
410 }
411
412 if (size_t(nWrite) != msgLength) {
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000413 ALOGD_IF(DEBUG_CHANNEL_MESSAGES,
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800414 "channel '%s' ~ error sending message type %s, send was incomplete", name.c_str(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000415 ftl::enum_string(msg->header.type).c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700416 return DEAD_OBJECT;
417 }
418
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800419 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ sent message of type %s", name.c_str(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000420 ftl::enum_string(msg->header.type).c_str());
Zimd8402b62023-06-02 11:56:26 +0100421
Jeff Brown5912f952013-07-01 19:10:31 -0700422 return OK;
423}
424
Paul Ramirez0c25e862024-06-18 21:33:33 +0000425android::base::Result<InputMessage> InputChannel::receiveMessage() {
Jeff Brown5912f952013-07-01 19:10:31 -0700426 ssize_t nRead;
Paul Ramirez0c25e862024-06-18 21:33:33 +0000427 InputMessage msg;
Jeff Brown5912f952013-07-01 19:10:31 -0700428 do {
Paul Ramirez0c25e862024-06-18 21:33:33 +0000429 nRead = ::recv(getFd(), &msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700430 } while (nRead == -1 && errno == EINTR);
431
432 if (nRead < 0) {
433 int error = errno;
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000434 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ receive message failed, errno=%d",
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800435 name.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700436 if (error == EAGAIN || error == EWOULDBLOCK) {
Paul Ramirez0c25e862024-06-18 21:33:33 +0000437 return android::base::Error(WOULD_BLOCK);
Jeff Brown5912f952013-07-01 19:10:31 -0700438 }
439 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
Paul Ramirez0c25e862024-06-18 21:33:33 +0000440 return android::base::Error(DEAD_OBJECT);
Jeff Brown5912f952013-07-01 19:10:31 -0700441 }
Paul Ramirez0c25e862024-06-18 21:33:33 +0000442 return android::base::Error(-error);
Jeff Brown5912f952013-07-01 19:10:31 -0700443 }
444
445 if (nRead == 0) { // check for EOF
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000446 ALOGD_IF(DEBUG_CHANNEL_MESSAGES,
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800447 "channel '%s' ~ receive message failed because peer was closed", name.c_str());
Paul Ramirez0c25e862024-06-18 21:33:33 +0000448 return android::base::Error(DEAD_OBJECT);
Jeff Brown5912f952013-07-01 19:10:31 -0700449 }
450
Paul Ramirez0c25e862024-06-18 21:33:33 +0000451 if (!msg.isValid(nRead)) {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800452 ALOGE("channel '%s' ~ received invalid message of size %zd", name.c_str(), nRead);
Paul Ramirez0c25e862024-06-18 21:33:33 +0000453 return android::base::Error(BAD_VALUE);
Jeff Brown5912f952013-07-01 19:10:31 -0700454 }
455
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800456 ALOGD_IF(DEBUG_CHANNEL_MESSAGES, "channel '%s' ~ received message of type %s", name.c_str(),
Paul Ramirez0c25e862024-06-18 21:33:33 +0000457 ftl::enum_string(msg.header.type).c_str());
Zimd8402b62023-06-02 11:56:26 +0100458 if (ATRACE_ENABLED()) {
Prabir Pradhana37bad12023-08-18 15:55:32 +0000459 // Add an additional trace point to include data about the received message.
Harry Cuttsc57cd3c2024-04-24 13:52:55 +0000460 std::string message =
461 StringPrintf("receiveMessage(inputChannel=%s, seq=0x%" PRIx32 ", type=%s)",
Paul Ramirez0c25e862024-06-18 21:33:33 +0000462 name.c_str(), msg.header.seq,
463 ftl::enum_string(msg.header.type).c_str());
Zimd8402b62023-06-02 11:56:26 +0100464 ATRACE_NAME(message.c_str());
465 }
Paul Ramirez0c25e862024-06-18 21:33:33 +0000466 return msg;
Jeff Brown5912f952013-07-01 19:10:31 -0700467}
468
Egor Paskoa0d32af2023-12-14 17:45:41 +0100469bool InputChannel::probablyHasInput() const {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800470 struct pollfd pfds = {.fd = fd.get(), .events = POLLIN};
Egor Paskoa0d32af2023-12-14 17:45:41 +0100471 if (::poll(&pfds, /*nfds=*/1, /*timeout=*/0) <= 0) {
Egor Pasko5a67a562024-01-16 16:46:45 +0100472 // This can be a false negative because EINTR and ENOMEM are not handled. The latter should
473 // be extremely rare. The EINTR is also unlikely because it happens only when the signal
474 // arrives while the syscall is executed, and the syscall is quick. Hitting EINTR too often
Egor Paskoa0d32af2023-12-14 17:45:41 +0100475 // would be a sign of having too many signals, which is a bigger performance problem. A
Egor Pasko5a67a562024-01-16 16:46:45 +0100476 // common tradition is to repeat the syscall on each EINTR, but it is not necessary here.
Egor Paskoa0d32af2023-12-14 17:45:41 +0100477 // In other words, the missing one liner is replaced by a multiline explanation.
478 return false;
479 }
480 // From poll(2): The bits returned in |revents| can include any of those specified in |events|,
481 // or one of the values POLLERR, POLLHUP, or POLLNVAL.
482 return (pfds.revents & POLLIN) != 0;
483}
484
Egor Pasko5a67a562024-01-16 16:46:45 +0100485void InputChannel::waitForMessage(std::chrono::milliseconds timeout) const {
486 if (timeout < 0ms) {
487 LOG(FATAL) << "Timeout cannot be negative, received " << timeout.count();
488 }
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800489 struct pollfd pfds = {.fd = fd.get(), .events = POLLIN};
Egor Pasko5a67a562024-01-16 16:46:45 +0100490 int ret;
491 std::chrono::time_point<std::chrono::steady_clock> stopTime =
492 std::chrono::steady_clock::now() + timeout;
493 std::chrono::milliseconds remaining = timeout;
494 do {
495 ret = ::poll(&pfds, /*nfds=*/1, /*timeout=*/remaining.count());
496 remaining = std::chrono::duration_cast<std::chrono::milliseconds>(
497 stopTime - std::chrono::steady_clock::now());
498 } while (ret == -1 && errno == EINTR && remaining > 0ms);
499}
500
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500501std::unique_ptr<InputChannel> InputChannel::dup() const {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800502 base::unique_fd newFd(dupChannelFd(fd.get()));
Chris Ye0783e992020-06-02 21:34:49 -0700503 return InputChannel::create(getName(), std::move(newFd), getConnectionToken());
Jeff Brown5912f952013-07-01 19:10:31 -0700504}
505
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800506void InputChannel::copyTo(android::os::InputChannelCore& outChannel) const {
507 outChannel.name = getName();
508 outChannel.fd.reset(dupChannelFd(fd.get()));
509 outChannel.token = getConnectionToken();
Robert Carr3720ed02018-08-08 16:08:27 -0700510}
511
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -0800512void InputChannel::moveChannel(std::unique_ptr<InputChannel> from,
513 android::os::InputChannelCore& outChannel) {
514 outChannel.name = from->getName();
515 outChannel.fd = android::os::ParcelFileDescriptor(std::move(from->fd));
516 outChannel.token = from->getConnectionToken();
517}
518
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700519sp<IBinder> InputChannel::getConnectionToken() const {
Siarhei Vishniakou8d660132024-01-11 16:48:44 -0800520 return token;
Garfield Tan15601662020-09-22 15:32:38 -0700521}
522
Jeff Brown5912f952013-07-01 19:10:31 -0700523// --- InputPublisher ---
524
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800525InputPublisher::InputPublisher(const std::shared_ptr<InputChannel>& channel)
Siarhei Vishniakou7b9f4f52024-02-02 13:07:16 -0800526 : mChannel(channel), mInputVerifier(mChannel->getName()) {}
Jeff Brown5912f952013-07-01 19:10:31 -0700527
528InputPublisher::~InputPublisher() {
529}
530
Garfield Tan1c7bc862020-01-28 13:24:04 -0800531status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,
Linnan Li13bf76a2024-05-05 19:18:02 +0800532 int32_t source, ui::LogicalDisplayId displayId,
Garfield Tan1c7bc862020-01-28 13:24:04 -0800533 std::array<uint8_t, 32> hmac, int32_t action,
534 int32_t flags, int32_t keyCode, int32_t scanCode,
535 int32_t metaState, int32_t repeatCount, nsecs_t downTime,
536 nsecs_t eventTime) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000537 ATRACE_NAME_IF(ATRACE_ENABLED(),
538 StringPrintf("publishKeyEvent(inputChannel=%s, action=%s, keyCode=%s)",
539 mChannel->getName().c_str(), KeyEvent::actionToString(action),
540 KeyEvent::getLabel(keyCode)));
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000541 ALOGD_IF(debugTransportPublisher(),
Prabir Pradhan96282b02023-02-24 22:36:17 +0000542 "channel '%s' publisher ~ %s: seq=%u, id=%d, deviceId=%d, source=%s, "
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000543 "action=%s, flags=0x%x, keyCode=%s, scanCode=%d, metaState=0x%x, repeatCount=%d,"
544 "downTime=%" PRId64 ", eventTime=%" PRId64,
Prabir Pradhan96282b02023-02-24 22:36:17 +0000545 mChannel->getName().c_str(), __func__, seq, eventId, deviceId,
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000546 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;
Linnan Li13bf76a2024-05-05 19:18:02 +0800560 msg.body.key.displayId = displayId.val();
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(
Linnan Li13bf76a2024-05-05 19:18:02 +0800574 uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source,
575 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac, int32_t action,
576 int32_t actionButton, int32_t flags, int32_t edgeFlags, int32_t metaState,
577 int32_t buttonState, MotionClassification classification, const ui::Transform& transform,
578 float xPrecision, float yPrecision, float xCursorPosition, float yCursorPosition,
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700579 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) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000582 ATRACE_NAME_IF(ATRACE_ENABLED(),
583 StringPrintf("publishMotionEvent(inputChannel=%s, action=%s)",
584 mChannel->getName().c_str(),
585 MotionEvent::actionToString(action).c_str()));
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800586 if (verifyEvents()) {
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700587 Result<void> result =
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -0700588 mInputVerifier.processMovement(deviceId, source, action, pointerCount,
589 pointerProperties, pointerCoords, flags);
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700590 if (!result.ok()) {
Siarhei Vishniakoub0058742024-07-01 16:42:49 -0700591 LOG(ERROR) << "Bad stream: " << result.error();
592 return BAD_VALUE;
Siarhei Vishniakou5c02a712023-05-15 15:45:02 -0700593 }
Siarhei Vishniakou92c8fd52023-01-29 14:57:43 -0800594 }
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000595 if (debugTransportPublisher()) {
chaviw9eaa22c2020-07-01 16:21:27 -0700596 std::string transformString;
chaviw85b44202020-07-24 11:46:21 -0700597 transform.dump(transformString, "transform", " ");
Prabir Pradhan96282b02023-02-24 22:36:17 +0000598 ALOGD("channel '%s' publisher ~ %s: seq=%u, id=%d, deviceId=%d, source=%s, "
Linnan Li13bf76a2024-05-05 19:18:02 +0800599 "displayId=%s, "
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000600 "action=%s, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
chaviw9eaa22c2020-07-01 16:21:27 -0700601 "metaState=0x%x, buttonState=0x%x, classification=%s,"
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800602 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
Siarhei Vishniakouf77f60a2023-10-23 17:26:05 -0700603 "pointerCount=%" PRIu32 "\n%s",
Prabir Pradhan96282b02023-02-24 22:36:17 +0000604 mChannel->getName().c_str(), __func__, seq, eventId, deviceId,
Linnan Li13bf76a2024-05-05 19:18:02 +0800605 inputEventSourceToString(source).c_str(), displayId.toString().c_str(),
Prabir Pradhan60dd97a2023-02-23 02:23:02 +0000606 MotionEvent::actionToString(action).c_str(), actionButton, flags, edgeFlags,
607 metaState, buttonState, motionClassificationToString(classification), xPrecision,
608 yPrecision, downTime, eventTime, pointerCount, transformString.c_str());
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800609 }
Jeff Brown5912f952013-07-01 19:10:31 -0700610
611 if (!seq) {
612 ALOGE("Attempted to publish a motion event with sequence number 0.");
613 return BAD_VALUE;
614 }
615
616 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700617 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800618 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700619 return BAD_VALUE;
620 }
621
622 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700623 msg.header.type = InputMessage::Type::MOTION;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500624 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800625 msg.body.motion.eventId = eventId;
Jeff Brown5912f952013-07-01 19:10:31 -0700626 msg.body.motion.deviceId = deviceId;
627 msg.body.motion.source = source;
Linnan Li13bf76a2024-05-05 19:18:02 +0800628 msg.body.motion.displayId = displayId.val();
Edgar Arriagac6ae4bb2020-04-16 18:46:48 -0700629 msg.body.motion.hmac = std::move(hmac);
Jeff Brown5912f952013-07-01 19:10:31 -0700630 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100631 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700632 msg.body.motion.flags = flags;
633 msg.body.motion.edgeFlags = edgeFlags;
634 msg.body.motion.metaState = metaState;
635 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800636 msg.body.motion.classification = classification;
chaviw9eaa22c2020-07-01 16:21:27 -0700637 msg.body.motion.dsdx = transform.dsdx();
638 msg.body.motion.dtdx = transform.dtdx();
639 msg.body.motion.dtdy = transform.dtdy();
640 msg.body.motion.dsdy = transform.dsdy();
641 msg.body.motion.tx = transform.tx();
642 msg.body.motion.ty = transform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700643 msg.body.motion.xPrecision = xPrecision;
644 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700645 msg.body.motion.xCursorPosition = xCursorPosition;
646 msg.body.motion.yCursorPosition = yCursorPosition;
Prabir Pradhanb9b18502021-08-26 12:30:32 -0700647 msg.body.motion.dsdxRaw = rawTransform.dsdx();
648 msg.body.motion.dtdxRaw = rawTransform.dtdx();
649 msg.body.motion.dtdyRaw = rawTransform.dtdy();
650 msg.body.motion.dsdyRaw = rawTransform.dsdy();
651 msg.body.motion.txRaw = rawTransform.tx();
652 msg.body.motion.tyRaw = rawTransform.ty();
Jeff Brown5912f952013-07-01 19:10:31 -0700653 msg.body.motion.downTime = downTime;
654 msg.body.motion.eventTime = eventTime;
655 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100656 for (uint32_t i = 0; i < pointerCount; i++) {
Siarhei Vishniakou73e6d372023-07-06 18:07:21 -0700657 msg.body.motion.pointers[i].properties = pointerProperties[i];
658 msg.body.motion.pointers[i].coords = pointerCoords[i];
Jeff Brown5912f952013-07-01 19:10:31 -0700659 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700660
Jeff Brown5912f952013-07-01 19:10:31 -0700661 return mChannel->sendMessage(&msg);
662}
663
Antonio Kantek3cfec7b2021-11-05 18:26:17 -0700664status_t InputPublisher::publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000665 ATRACE_NAME_IF(ATRACE_ENABLED(),
666 StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s)",
667 mChannel->getName().c_str(), toString(hasFocus)));
Prabir Pradhan96282b02023-02-24 22:36:17 +0000668 ALOGD_IF(debugTransportPublisher(), "channel '%s' publisher ~ %s: seq=%u, id=%d, hasFocus=%s",
669 mChannel->getName().c_str(), __func__, seq, eventId, toString(hasFocus));
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800670
671 InputMessage msg;
672 msg.header.type = InputMessage::Type::FOCUS;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500673 msg.header.seq = seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800674 msg.body.focus.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000675 msg.body.focus.hasFocus = hasFocus;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800676 return mChannel->sendMessage(&msg);
677}
678
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800679status_t InputPublisher::publishCaptureEvent(uint32_t seq, int32_t eventId,
680 bool pointerCaptureEnabled) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000681 ATRACE_NAME_IF(ATRACE_ENABLED(),
682 StringPrintf("publishCaptureEvent(inputChannel=%s, pointerCaptureEnabled=%s)",
683 mChannel->getName().c_str(), toString(pointerCaptureEnabled)));
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000684 ALOGD_IF(debugTransportPublisher(),
Prabir Pradhan96282b02023-02-24 22:36:17 +0000685 "channel '%s' publisher ~ %s: seq=%u, id=%d, pointerCaptureEnabled=%s",
686 mChannel->getName().c_str(), __func__, seq, eventId, toString(pointerCaptureEnabled));
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800687
688 InputMessage msg;
689 msg.header.type = InputMessage::Type::CAPTURE;
690 msg.header.seq = seq;
691 msg.body.capture.eventId = eventId;
Siarhei Vishniakou38b7f7f2021-03-05 01:57:08 +0000692 msg.body.capture.pointerCaptureEnabled = pointerCaptureEnabled;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800693 return mChannel->sendMessage(&msg);
694}
695
arthurhung7632c332020-12-30 16:58:01 +0800696status_t InputPublisher::publishDragEvent(uint32_t seq, int32_t eventId, float x, float y,
697 bool isExiting) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000698 ATRACE_NAME_IF(ATRACE_ENABLED(),
699 StringPrintf("publishDragEvent(inputChannel=%s, x=%f, y=%f, isExiting=%s)",
700 mChannel->getName().c_str(), x, y, toString(isExiting)));
Prabir Pradhanb2bd83c2023-02-23 02:34:40 +0000701 ALOGD_IF(debugTransportPublisher(),
Prabir Pradhan96282b02023-02-24 22:36:17 +0000702 "channel '%s' publisher ~ %s: seq=%u, id=%d, x=%f, y=%f, isExiting=%s",
703 mChannel->getName().c_str(), __func__, seq, eventId, x, y, toString(isExiting));
arthurhung7632c332020-12-30 16:58:01 +0800704
705 InputMessage msg;
706 msg.header.type = InputMessage::Type::DRAG;
707 msg.header.seq = seq;
708 msg.body.drag.eventId = eventId;
709 msg.body.drag.isExiting = isExiting;
710 msg.body.drag.x = x;
711 msg.body.drag.y = y;
712 return mChannel->sendMessage(&msg);
713}
714
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700715status_t InputPublisher::publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode) {
Prabir Pradhan2dac8b82023-09-06 01:11:51 +0000716 ATRACE_NAME_IF(ATRACE_ENABLED(),
717 StringPrintf("publishTouchModeEvent(inputChannel=%s, isInTouchMode=%s)",
718 mChannel->getName().c_str(), toString(isInTouchMode)));
Prabir Pradhan96282b02023-02-24 22:36:17 +0000719 ALOGD_IF(debugTransportPublisher(),
720 "channel '%s' publisher ~ %s: seq=%u, id=%d, isInTouchMode=%s",
721 mChannel->getName().c_str(), __func__, seq, eventId, toString(isInTouchMode));
Antonio Kantek7cdf8ef2021-07-13 18:04:53 -0700722
723 InputMessage msg;
724 msg.header.type = InputMessage::Type::TOUCH_MODE;
725 msg.header.seq = seq;
726 msg.body.touchMode.eventId = eventId;
727 msg.body.touchMode.isInTouchMode = isInTouchMode;
728 return mChannel->sendMessage(&msg);
729}
730
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000731android::base::Result<InputPublisher::ConsumerResponse> InputPublisher::receiveConsumerResponse() {
Paul Ramirez0c25e862024-06-18 21:33:33 +0000732 android::base::Result<InputMessage> result = mChannel->receiveMessage();
733 if (!result.ok()) {
734 if (debugTransportPublisher() && result.error().code() != WOULD_BLOCK) {
Siarhei Vishniakou69112652023-08-24 08:34:18 -0700735 LOG(INFO) << "channel '" << mChannel->getName() << "' publisher ~ " << __func__ << ": "
Paul Ramirez0c25e862024-06-18 21:33:33 +0000736 << result.error().message();
Siarhei Vishniakou69112652023-08-24 08:34:18 -0700737 }
Paul Ramirez0c25e862024-06-18 21:33:33 +0000738 return result.error();
Jeff Brown5912f952013-07-01 19:10:31 -0700739 }
Paul Ramirez0c25e862024-06-18 21:33:33 +0000740
741 const InputMessage& msg = *result;
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000742 if (msg.header.type == InputMessage::Type::FINISHED) {
Prabir Pradhan96282b02023-02-24 22:36:17 +0000743 ALOGD_IF(debugTransportPublisher(),
744 "channel '%s' publisher ~ %s: finished: seq=%u, handled=%s",
745 mChannel->getName().c_str(), __func__, msg.header.seq,
746 toString(msg.body.finished.handled));
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000747 return Finished{
748 .seq = msg.header.seq,
749 .handled = msg.body.finished.handled,
750 .consumeTime = msg.body.finished.consumeTime,
751 };
Jeff Brown5912f952013-07-01 19:10:31 -0700752 }
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000753
754 if (msg.header.type == InputMessage::Type::TIMELINE) {
Prabir Pradhan96282b02023-02-24 22:36:17 +0000755 ALOGD_IF(debugTransportPublisher(), "channel '%s' publisher ~ %s: timeline: id=%d",
756 mChannel->getName().c_str(), __func__, msg.body.timeline.eventId);
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000757 return Timeline{
758 .inputEventId = msg.body.timeline.eventId,
759 .graphicsTimeline = msg.body.timeline.graphicsTimeline,
760 };
761 }
762
763 ALOGE("channel '%s' publisher ~ Received unexpected %s message from consumer",
Dominik Laskowski75788452021-02-09 18:51:25 -0800764 mChannel->getName().c_str(), ftl::enum_string(msg.header.type).c_str());
Siarhei Vishniakouf94ae022021-02-04 01:23:17 +0000765 return android::base::Error(UNKNOWN_ERROR);
Jeff Brown5912f952013-07-01 19:10:31 -0700766}
767
Jeff Brown5912f952013-07-01 19:10:31 -0700768} // namespace android