blob: d25a5cc0ef3f2ef7da7ae1501ce5e6e23fc6ab3c [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
8//#define LOG_NDEBUG 0
9
10// Log debug messages about channel messages (send message, receive message)
11#define DEBUG_CHANNEL_MESSAGES 0
12
13// Log debug messages whenever InputChannel objects are created/destroyed
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -070014static constexpr bool DEBUG_CHANNEL_LIFECYCLE = false;
Jeff Brown5912f952013-07-01 19:10:31 -070015
16// Log debug messages about transport actions
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -080017static constexpr bool DEBUG_TRANSPORT_ACTIONS = false;
Jeff Brown5912f952013-07-01 19:10:31 -070018
19// Log debug messages about touch event resampling
20#define DEBUG_RESAMPLING 0
21
Jeff Brown5912f952013-07-01 19:10:31 -070022#include <errno.h>
23#include <fcntl.h>
Michael Wrightd0a4a622014-06-09 19:03:32 -070024#include <inttypes.h>
Jeff Brown5912f952013-07-01 19:10:31 -070025#include <math.h>
Jeff Brown5912f952013-07-01 19:10:31 -070026#include <sys/socket.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070027#include <sys/types.h>
Jeff Brown5912f952013-07-01 19:10:31 -070028#include <unistd.h>
29
Michael Wright3dd60e22019-03-27 22:06:44 +000030#include <android-base/stringprintf.h>
31#include <binder/Parcel.h>
Jeff Brown5912f952013-07-01 19:10:31 -070032#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070033#include <log/log.h>
Michael Wright3dd60e22019-03-27 22:06:44 +000034#include <utils/Trace.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070035
Jeff Brown5912f952013-07-01 19:10:31 -070036#include <input/InputTransport.h>
37
Michael Wright3dd60e22019-03-27 22:06:44 +000038using android::base::StringPrintf;
39
Jeff Brown5912f952013-07-01 19:10:31 -070040namespace android {
41
42// Socket buffer size. The default is typically about 128KB, which is much larger than
43// we really need. So we make it smaller. It just needs to be big enough to hold
44// a few dozen large multi-finger motion events in the case where an application gets
45// behind processing touches.
46static const size_t SOCKET_BUFFER_SIZE = 32 * 1024;
47
48// Nanoseconds per milliseconds.
49static const nsecs_t NANOS_PER_MS = 1000000;
50
51// Latency added during resampling. A few milliseconds doesn't hurt much but
52// reduces the impact of mispredicted touch positions.
53static const nsecs_t RESAMPLE_LATENCY = 5 * NANOS_PER_MS;
54
55// Minimum time difference between consecutive samples before attempting to resample.
56static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS;
57
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -070058// Maximum time difference between consecutive samples before attempting to resample
59// by extrapolation.
60static const nsecs_t RESAMPLE_MAX_DELTA = 20 * NANOS_PER_MS;
61
Jeff Brown5912f952013-07-01 19:10:31 -070062// Maximum time to predict forward from the last known state, to avoid predicting too
63// far into the future. This time is further bounded by 50% of the last time delta.
64static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS;
65
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -060066/**
67 * System property for enabling / disabling touch resampling.
68 * Resampling extrapolates / interpolates the reported touch event coordinates to better
69 * align them to the VSYNC signal, thus resulting in smoother scrolling performance.
70 * Resampling is not needed (and should be disabled) on hardware that already
71 * has touch events triggered by VSYNC.
72 * Set to "1" to enable resampling (default).
73 * Set to "0" to disable resampling.
74 * Resampling is enabled by default.
75 */
76static const char* PROPERTY_RESAMPLING_ENABLED = "ro.input.resampling";
77
Jeff Brown5912f952013-07-01 19:10:31 -070078template<typename T>
79inline static T min(const T& a, const T& b) {
80 return a < b ? a : b;
81}
82
83inline static float lerp(float a, float b, float alpha) {
84 return a + alpha * (b - a);
85}
86
Siarhei Vishniakou128eab12019-05-23 10:25:59 +080087inline static bool isPointerEvent(int32_t source) {
88 return (source & AINPUT_SOURCE_CLASS_POINTER) == AINPUT_SOURCE_CLASS_POINTER;
89}
90
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -080091inline static const char* toString(bool value) {
92 return value ? "true" : "false";
93}
94
Jeff Brown5912f952013-07-01 19:10:31 -070095// --- InputMessage ---
96
97bool InputMessage::isValid(size_t actualSize) const {
98 if (size() == actualSize) {
99 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700100 case Type::KEY:
101 return true;
102 case Type::MOTION:
103 return body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
104 case Type::FINISHED:
105 return true;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800106 case Type::FOCUS:
107 return true;
Jeff Brown5912f952013-07-01 19:10:31 -0700108 }
109 }
110 return false;
111}
112
113size_t InputMessage::size() const {
114 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700115 case Type::KEY:
116 return sizeof(Header) + body.key.size();
117 case Type::MOTION:
118 return sizeof(Header) + body.motion.size();
119 case Type::FINISHED:
120 return sizeof(Header) + body.finished.size();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800121 case Type::FOCUS:
122 return sizeof(Header) + body.focus.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700123 }
124 return sizeof(Header);
125}
126
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800127/**
128 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
129 * memory to zero, then only copy the valid bytes on a per-field basis.
130 */
131void InputMessage::getSanitizedCopy(InputMessage* msg) const {
132 memset(msg, 0, sizeof(*msg));
133
134 // Write the header
135 msg->header.type = header.type;
136
137 // Write the body
138 switch(header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700139 case InputMessage::Type::KEY: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800140 // uint32_t seq
141 msg->body.key.seq = body.key.seq;
142 // nsecs_t eventTime
143 msg->body.key.eventTime = body.key.eventTime;
144 // int32_t deviceId
145 msg->body.key.deviceId = body.key.deviceId;
146 // int32_t source
147 msg->body.key.source = body.key.source;
148 // int32_t displayId
149 msg->body.key.displayId = body.key.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600150 // std::array<uint8_t, 32> hmac
151 msg->body.key.hmac = body.key.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800152 // int32_t action
153 msg->body.key.action = body.key.action;
154 // int32_t flags
155 msg->body.key.flags = body.key.flags;
156 // int32_t keyCode
157 msg->body.key.keyCode = body.key.keyCode;
158 // int32_t scanCode
159 msg->body.key.scanCode = body.key.scanCode;
160 // int32_t metaState
161 msg->body.key.metaState = body.key.metaState;
162 // int32_t repeatCount
163 msg->body.key.repeatCount = body.key.repeatCount;
164 // nsecs_t downTime
165 msg->body.key.downTime = body.key.downTime;
166 break;
167 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700168 case InputMessage::Type::MOTION: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800169 // uint32_t seq
170 msg->body.motion.seq = body.motion.seq;
171 // nsecs_t eventTime
172 msg->body.motion.eventTime = body.motion.eventTime;
173 // int32_t deviceId
174 msg->body.motion.deviceId = body.motion.deviceId;
175 // int32_t source
176 msg->body.motion.source = body.motion.source;
177 // int32_t displayId
178 msg->body.motion.displayId = body.motion.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600179 // std::array<uint8_t, 32> hmac
180 msg->body.motion.hmac = body.motion.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800181 // int32_t action
182 msg->body.motion.action = body.motion.action;
183 // int32_t actionButton
184 msg->body.motion.actionButton = body.motion.actionButton;
185 // int32_t flags
186 msg->body.motion.flags = body.motion.flags;
187 // int32_t metaState
188 msg->body.motion.metaState = body.motion.metaState;
189 // int32_t buttonState
190 msg->body.motion.buttonState = body.motion.buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800191 // MotionClassification classification
192 msg->body.motion.classification = body.motion.classification;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800193 // int32_t edgeFlags
194 msg->body.motion.edgeFlags = body.motion.edgeFlags;
195 // nsecs_t downTime
196 msg->body.motion.downTime = body.motion.downTime;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600197 // float xScale
198 msg->body.motion.xScale = body.motion.xScale;
199 // float yScale
200 msg->body.motion.yScale = body.motion.yScale;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800201 // float xOffset
202 msg->body.motion.xOffset = body.motion.xOffset;
203 // float yOffset
204 msg->body.motion.yOffset = body.motion.yOffset;
205 // float xPrecision
206 msg->body.motion.xPrecision = body.motion.xPrecision;
207 // float yPrecision
208 msg->body.motion.yPrecision = body.motion.yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700209 // float xCursorPosition
210 msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
211 // float yCursorPosition
212 msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800213 // uint32_t pointerCount
214 msg->body.motion.pointerCount = body.motion.pointerCount;
215 //struct Pointer pointers[MAX_POINTERS]
216 for (size_t i = 0; i < body.motion.pointerCount; i++) {
217 // PointerProperties properties
218 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
219 msg->body.motion.pointers[i].properties.toolType =
220 body.motion.pointers[i].properties.toolType,
221 // PointerCoords coords
222 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
223 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
224 memcpy(&msg->body.motion.pointers[i].coords.values[0],
225 &body.motion.pointers[i].coords.values[0],
226 count * (sizeof(body.motion.pointers[i].coords.values[0])));
227 }
228 break;
229 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700230 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800231 msg->body.finished.seq = body.finished.seq;
232 msg->body.finished.handled = body.finished.handled;
233 break;
234 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800235 case InputMessage::Type::FOCUS: {
236 msg->body.focus.seq = body.focus.seq;
237 msg->body.focus.hasFocus = body.focus.hasFocus;
238 msg->body.focus.inTouchMode = body.focus.inTouchMode;
239 break;
240 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800241 }
242}
Jeff Brown5912f952013-07-01 19:10:31 -0700243
244// --- InputChannel ---
245
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700246sp<InputChannel> InputChannel::create(const std::string& name, android::base::unique_fd fd,
247 sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700248 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
249 if (result != 0) {
250 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
251 strerror(errno));
252 return nullptr;
253 }
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700254 return new InputChannel(name, std::move(fd), token);
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700255}
256
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700257InputChannel::InputChannel(const std::string& name, android::base::unique_fd fd, sp<IBinder> token)
258 : mName(name), mFd(std::move(fd)), mToken(token) {
259 if (DEBUG_CHANNEL_LIFECYCLE) {
260 ALOGD("Input channel constructed: name='%s', fd=%d", mName.c_str(), mFd.get());
261 }
Jeff Brown5912f952013-07-01 19:10:31 -0700262}
263
264InputChannel::~InputChannel() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700265 if (DEBUG_CHANNEL_LIFECYCLE) {
266 ALOGD("Input channel destroyed: name='%s', fd=%d", mName.c_str(), mFd.get());
267 }
Robert Carr3720ed02018-08-08 16:08:27 -0700268}
269
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800270status_t InputChannel::openInputChannelPair(const std::string& name,
Jeff Brown5912f952013-07-01 19:10:31 -0700271 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) {
272 int sockets[2];
273 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
274 status_t result = -errno;
275 ALOGE("channel '%s' ~ Could not create socket pair. errno=%d",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800276 name.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700277 outServerChannel.clear();
278 outClientChannel.clear();
279 return result;
280 }
281
282 int bufferSize = SOCKET_BUFFER_SIZE;
283 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
284 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
285 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
286 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
287
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700288 sp<IBinder> token = new BBinder();
289
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700290 std::string serverChannelName = name + " (server)";
291 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700292 outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700293
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700294 std::string clientChannelName = name + " (client)";
295 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700296 outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700297 return OK;
298}
299
300status_t InputChannel::sendMessage(const InputMessage* msg) {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800301 const size_t msgLength = msg->size();
302 InputMessage cleanMsg;
303 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700304 ssize_t nWrite;
305 do {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700306 nWrite = ::send(mFd.get(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700307 } while (nWrite == -1 && errno == EINTR);
308
309 if (nWrite < 0) {
310 int error = errno;
311#if DEBUG_CHANNEL_MESSAGES
chaviw81e2bb92019-12-18 15:03:51 -0800312 ALOGD("channel '%s' ~ error sending message of type %d, %s", mName.c_str(),
313 msg->header.type, strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700314#endif
315 if (error == EAGAIN || error == EWOULDBLOCK) {
316 return WOULD_BLOCK;
317 }
318 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
319 return DEAD_OBJECT;
320 }
321 return -error;
322 }
323
324 if (size_t(nWrite) != msgLength) {
325#if DEBUG_CHANNEL_MESSAGES
326 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800327 mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700328#endif
329 return DEAD_OBJECT;
330 }
331
332#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800333 ALOGD("channel '%s' ~ sent message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700334#endif
335 return OK;
336}
337
338status_t InputChannel::receiveMessage(InputMessage* msg) {
339 ssize_t nRead;
340 do {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700341 nRead = ::recv(mFd.get(), msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700342 } while (nRead == -1 && errno == EINTR);
343
344 if (nRead < 0) {
345 int error = errno;
346#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800347 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700348#endif
349 if (error == EAGAIN || error == EWOULDBLOCK) {
350 return WOULD_BLOCK;
351 }
352 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
353 return DEAD_OBJECT;
354 }
355 return -error;
356 }
357
358 if (nRead == 0) { // check for EOF
359#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800360 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700361#endif
362 return DEAD_OBJECT;
363 }
364
365 if (!msg->isValid(nRead)) {
366#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800367 ALOGD("channel '%s' ~ received invalid message", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700368#endif
369 return BAD_VALUE;
370 }
371
372#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800373 ALOGD("channel '%s' ~ received message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700374#endif
375 return OK;
376}
377
378sp<InputChannel> InputChannel::dup() const {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700379 android::base::unique_fd newFd(::dup(getFd()));
380 if (!newFd.ok()) {
381 ALOGE("Could not duplicate fd %i for channel %s: %s", getFd(), mName.c_str(),
382 strerror(errno));
Siarhei Vishniakou3d8df0e2019-09-17 14:53:07 +0100383 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
384 // If this process is out of file descriptors, then throwing that might end up exploding
385 // on the other side of a binder call, which isn't really helpful.
386 // Better to just crash here and hope that the FD leak is slow.
387 // Other failures could be client errors, so we still propagate those back to the caller.
388 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s",
389 getName().c_str());
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700390 return nullptr;
391 }
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700392 return InputChannel::create(mName, std::move(newFd), mToken);
Jeff Brown5912f952013-07-01 19:10:31 -0700393}
394
Robert Carr3720ed02018-08-08 16:08:27 -0700395status_t InputChannel::write(Parcel& out) const {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700396 status_t s = out.writeCString(getName().c_str());
Robert Carr3720ed02018-08-08 16:08:27 -0700397 if (s != OK) {
398 return s;
399 }
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700400
Robert Carr803535b2018-08-02 16:38:15 -0700401 s = out.writeStrongBinder(mToken);
402 if (s != OK) {
403 return s;
404 }
Robert Carr3720ed02018-08-08 16:08:27 -0700405
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700406 s = out.writeUniqueFileDescriptor(mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700407 return s;
408}
409
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700410sp<InputChannel> InputChannel::read(const Parcel& from) {
411 std::string name = from.readCString();
412 sp<IBinder> token = from.readStrongBinder();
413 android::base::unique_fd rawFd;
414 status_t fdResult = from.readUniqueFileDescriptor(&rawFd);
415 if (fdResult != OK) {
416 return nullptr;
Robert Carr3720ed02018-08-08 16:08:27 -0700417 }
418
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700419 return InputChannel::create(name, std::move(rawFd), token);
Robert Carr3720ed02018-08-08 16:08:27 -0700420}
421
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700422sp<IBinder> InputChannel::getConnectionToken() const {
Robert Carr803535b2018-08-02 16:38:15 -0700423 return mToken;
424}
425
Jeff Brown5912f952013-07-01 19:10:31 -0700426// --- InputPublisher ---
427
428InputPublisher::InputPublisher(const sp<InputChannel>& channel) :
429 mChannel(channel) {
430}
431
432InputPublisher::~InputPublisher() {
433}
434
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600435status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t deviceId, int32_t source,
436 int32_t displayId, std::array<uint8_t, 32> hmac,
437 int32_t action, int32_t flags, int32_t keyCode,
438 int32_t scanCode, int32_t metaState, int32_t repeatCount,
439 nsecs_t downTime, nsecs_t eventTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000440 if (ATRACE_ENABLED()) {
441 std::string message = StringPrintf("publishKeyEvent(inputChannel=%s, keyCode=%" PRId32 ")",
442 mChannel->getName().c_str(), keyCode);
443 ATRACE_NAME(message.c_str());
444 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800445 if (DEBUG_TRANSPORT_ACTIONS) {
446 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
447 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
448 "downTime=%" PRId64 ", eventTime=%" PRId64,
449 mChannel->getName().c_str(), seq, deviceId, source, action, flags, keyCode, scanCode,
450 metaState, repeatCount, downTime, eventTime);
451 }
Jeff Brown5912f952013-07-01 19:10:31 -0700452
453 if (!seq) {
454 ALOGE("Attempted to publish a key event with sequence number 0.");
455 return BAD_VALUE;
456 }
457
458 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700459 msg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -0700460 msg.body.key.seq = seq;
461 msg.body.key.deviceId = deviceId;
462 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100463 msg.body.key.displayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600464 msg.body.key.hmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700465 msg.body.key.action = action;
466 msg.body.key.flags = flags;
467 msg.body.key.keyCode = keyCode;
468 msg.body.key.scanCode = scanCode;
469 msg.body.key.metaState = metaState;
470 msg.body.key.repeatCount = repeatCount;
471 msg.body.key.downTime = downTime;
472 msg.body.key.eventTime = eventTime;
473 return mChannel->sendMessage(&msg);
474}
475
476status_t InputPublisher::publishMotionEvent(
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600477 uint32_t seq, int32_t deviceId, int32_t source, int32_t displayId,
478 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
479 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
480 MotionClassification classification, float xScale, float yScale, float xOffset,
481 float yOffset, float xPrecision, float yPrecision, float xCursorPosition,
482 float yCursorPosition, nsecs_t downTime, nsecs_t eventTime, uint32_t pointerCount,
Garfield Tan00f511d2019-06-12 16:55:40 -0700483 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000484 if (ATRACE_ENABLED()) {
485 std::string message = StringPrintf(
486 "publishMotionEvent(inputChannel=%s, action=%" PRId32 ")",
487 mChannel->getName().c_str(), action);
488 ATRACE_NAME(message.c_str());
489 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800490 if (DEBUG_TRANSPORT_ACTIONS) {
491 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
492 "displayId=%" PRId32 ", "
493 "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600494 "metaState=0x%x, buttonState=0x%x, classification=%s, xScale=%.1f, yScale=%.1f, "
495 "xOffset=%.1f, yOffset=%.1f, "
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800496 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
497 "pointerCount=%" PRIu32,
498 mChannel->getName().c_str(), seq, deviceId, source, displayId, action, actionButton,
499 flags, edgeFlags, metaState, buttonState,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600500 motionClassificationToString(classification), xScale, yScale, xOffset, yOffset,
501 xPrecision, yPrecision, downTime, eventTime, pointerCount);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800502 }
Jeff Brown5912f952013-07-01 19:10:31 -0700503
504 if (!seq) {
505 ALOGE("Attempted to publish a motion event with sequence number 0.");
506 return BAD_VALUE;
507 }
508
509 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700510 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800511 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700512 return BAD_VALUE;
513 }
514
515 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700516 msg.header.type = InputMessage::Type::MOTION;
Jeff Brown5912f952013-07-01 19:10:31 -0700517 msg.body.motion.seq = seq;
518 msg.body.motion.deviceId = deviceId;
519 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700520 msg.body.motion.displayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600521 msg.body.motion.hmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700522 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100523 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700524 msg.body.motion.flags = flags;
525 msg.body.motion.edgeFlags = edgeFlags;
526 msg.body.motion.metaState = metaState;
527 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800528 msg.body.motion.classification = classification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600529 msg.body.motion.xScale = xScale;
530 msg.body.motion.yScale = yScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700531 msg.body.motion.xOffset = xOffset;
532 msg.body.motion.yOffset = yOffset;
533 msg.body.motion.xPrecision = xPrecision;
534 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700535 msg.body.motion.xCursorPosition = xCursorPosition;
536 msg.body.motion.yCursorPosition = yCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700537 msg.body.motion.downTime = downTime;
538 msg.body.motion.eventTime = eventTime;
539 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100540 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700541 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
542 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
543 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700544
Jeff Brown5912f952013-07-01 19:10:31 -0700545 return mChannel->sendMessage(&msg);
546}
547
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800548status_t InputPublisher::publishFocusEvent(uint32_t seq, bool hasFocus, bool inTouchMode) {
549 if (ATRACE_ENABLED()) {
550 std::string message =
551 StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s, inTouchMode=%s)",
552 mChannel->getName().c_str(), toString(hasFocus),
553 toString(inTouchMode));
554 ATRACE_NAME(message.c_str());
555 }
556
557 InputMessage msg;
558 msg.header.type = InputMessage::Type::FOCUS;
559 msg.body.focus.seq = seq;
560 msg.body.focus.hasFocus = hasFocus ? 1 : 0;
561 msg.body.focus.inTouchMode = inTouchMode ? 1 : 0;
562 return mChannel->sendMessage(&msg);
563}
564
Jeff Brown5912f952013-07-01 19:10:31 -0700565status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandled) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800566 if (DEBUG_TRANSPORT_ACTIONS) {
567 ALOGD("channel '%s' publisher ~ receiveFinishedSignal", mChannel->getName().c_str());
568 }
Jeff Brown5912f952013-07-01 19:10:31 -0700569
570 InputMessage msg;
571 status_t result = mChannel->receiveMessage(&msg);
572 if (result) {
573 *outSeq = 0;
574 *outHandled = false;
575 return result;
576 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700577 if (msg.header.type != InputMessage::Type::FINISHED) {
Jeff Brown5912f952013-07-01 19:10:31 -0700578 ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800579 mChannel->getName().c_str(), msg.header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700580 return UNKNOWN_ERROR;
581 }
582 *outSeq = msg.body.finished.seq;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800583 *outHandled = msg.body.finished.handled == 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700584 return OK;
585}
586
587// --- InputConsumer ---
588
589InputConsumer::InputConsumer(const sp<InputChannel>& channel) :
590 mResampleTouch(isTouchResamplingEnabled()),
591 mChannel(channel), mMsgDeferred(false) {
592}
593
594InputConsumer::~InputConsumer() {
595}
596
597bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600598 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700599}
600
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800601status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
602 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800603 if (DEBUG_TRANSPORT_ACTIONS) {
604 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
605 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
606 }
Jeff Brown5912f952013-07-01 19:10:31 -0700607
608 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700609 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700610
611 // Fetch the next input message.
612 // Loop until an event can be returned or no additional events are received.
613 while (!*outEvent) {
614 if (mMsgDeferred) {
615 // mMsg contains a valid input message from the previous call to consume
616 // that has not yet been processed.
617 mMsgDeferred = false;
618 } else {
619 // Receive a fresh message.
620 status_t result = mChannel->receiveMessage(&mMsg);
621 if (result) {
622 // Consume the next batched event unless batches are being held for later.
623 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800624 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700625 if (*outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800626 if (DEBUG_TRANSPORT_ACTIONS) {
627 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
628 mChannel->getName().c_str(), *outSeq);
629 }
Jeff Brown5912f952013-07-01 19:10:31 -0700630 break;
631 }
632 }
633 return result;
634 }
635 }
636
637 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700638 case InputMessage::Type::KEY: {
639 KeyEvent* keyEvent = factory->createKeyEvent();
640 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700641
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700642 initializeKeyEvent(keyEvent, &mMsg);
643 *outSeq = mMsg.body.key.seq;
644 *outEvent = keyEvent;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800645 if (DEBUG_TRANSPORT_ACTIONS) {
646 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
647 mChannel->getName().c_str(), *outSeq);
648 }
Jeff Brown5912f952013-07-01 19:10:31 -0700649 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700650 }
Jeff Brown5912f952013-07-01 19:10:31 -0700651
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700652 case InputMessage::Type::MOTION: {
653 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
654 if (batchIndex >= 0) {
655 Batch& batch = mBatches.editItemAt(batchIndex);
656 if (canAddSample(batch, &mMsg)) {
657 batch.samples.push(mMsg);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800658 if (DEBUG_TRANSPORT_ACTIONS) {
659 ALOGD("channel '%s' consumer ~ appended to batch event",
660 mChannel->getName().c_str());
661 }
Jeff Brown5912f952013-07-01 19:10:31 -0700662 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700663 } else if (isPointerEvent(mMsg.body.motion.source) &&
664 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
665 // No need to process events that we are going to cancel anyways
666 const size_t count = batch.samples.size();
667 for (size_t i = 0; i < count; i++) {
668 const InputMessage& msg = batch.samples.itemAt(i);
669 sendFinishedSignal(msg.body.motion.seq, false);
670 }
671 batch.samples.removeItemsAt(0, count);
672 mBatches.removeAt(batchIndex);
673 } else {
674 // We cannot append to the batch in progress, so we need to consume
675 // the previous batch right now and defer the new message until later.
676 mMsgDeferred = true;
677 status_t result = consumeSamples(factory, batch, batch.samples.size(),
678 outSeq, outEvent);
679 mBatches.removeAt(batchIndex);
680 if (result) {
681 return result;
682 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800683 if (DEBUG_TRANSPORT_ACTIONS) {
684 ALOGD("channel '%s' consumer ~ consumed batch event and "
685 "deferred current event, seq=%u",
686 mChannel->getName().c_str(), *outSeq);
687 }
Jeff Brown5912f952013-07-01 19:10:31 -0700688 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700689 }
Jeff Brown5912f952013-07-01 19:10:31 -0700690 }
Jeff Brown5912f952013-07-01 19:10:31 -0700691
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800692 // Start a new batch if needed.
693 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
694 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
695 mBatches.push();
696 Batch& batch = mBatches.editTop();
697 batch.samples.push(mMsg);
698 if (DEBUG_TRANSPORT_ACTIONS) {
699 ALOGD("channel '%s' consumer ~ started batch event",
700 mChannel->getName().c_str());
701 }
702 break;
703 }
Jeff Brown5912f952013-07-01 19:10:31 -0700704
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800705 MotionEvent* motionEvent = factory->createMotionEvent();
706 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700707
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800708 updateTouchState(mMsg);
709 initializeMotionEvent(motionEvent, &mMsg);
710 *outSeq = mMsg.body.motion.seq;
711 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800712
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800713 if (DEBUG_TRANSPORT_ACTIONS) {
714 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
715 mChannel->getName().c_str(), *outSeq);
716 }
717 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700718 }
Jeff Brown5912f952013-07-01 19:10:31 -0700719
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800720 case InputMessage::Type::FINISHED: {
721 LOG_ALWAYS_FATAL("Consumed a FINISHED message, which should never be seen by "
722 "InputConsumer!");
723 break;
724 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800725
726 case InputMessage::Type::FOCUS: {
727 FocusEvent* focusEvent = factory->createFocusEvent();
728 if (!focusEvent) return NO_MEMORY;
729
730 initializeFocusEvent(focusEvent, &mMsg);
731 *outSeq = mMsg.body.focus.seq;
732 *outEvent = focusEvent;
733 break;
734 }
Jeff Brown5912f952013-07-01 19:10:31 -0700735 }
736 }
737 return OK;
738}
739
740status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800741 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700742 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700743 for (size_t i = mBatches.size(); i > 0; ) {
744 i--;
Jeff Brown5912f952013-07-01 19:10:31 -0700745 Batch& batch = mBatches.editItemAt(i);
Michael Wright32232172013-10-21 12:05:22 -0700746 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800747 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700748 mBatches.removeAt(i);
749 return result;
750 }
751
Michael Wright32232172013-10-21 12:05:22 -0700752 nsecs_t sampleTime = frameTime;
753 if (mResampleTouch) {
754 sampleTime -= RESAMPLE_LATENCY;
755 }
Jeff Brown5912f952013-07-01 19:10:31 -0700756 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
757 if (split < 0) {
758 continue;
759 }
760
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800761 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700762 const InputMessage* next;
763 if (batch.samples.isEmpty()) {
764 mBatches.removeAt(i);
Yi Kong5bed83b2018-07-17 12:53:47 -0700765 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700766 } else {
767 next = &batch.samples.itemAt(0);
768 }
Michael Wright32232172013-10-21 12:05:22 -0700769 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700770 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
771 }
772 return result;
773 }
774
775 return WOULD_BLOCK;
776}
777
778status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800779 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700780 MotionEvent* motionEvent = factory->createMotionEvent();
781 if (! motionEvent) return NO_MEMORY;
782
783 uint32_t chain = 0;
784 for (size_t i = 0; i < count; i++) {
785 InputMessage& msg = batch.samples.editItemAt(i);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100786 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700787 if (i) {
788 SeqChain seqChain;
789 seqChain.seq = msg.body.motion.seq;
790 seqChain.chain = chain;
791 mSeqChains.push(seqChain);
792 addSample(motionEvent, &msg);
793 } else {
794 initializeMotionEvent(motionEvent, &msg);
795 }
796 chain = msg.body.motion.seq;
797 }
798 batch.samples.removeItemsAt(0, count);
799
800 *outSeq = chain;
801 *outEvent = motionEvent;
802 return OK;
803}
804
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100805void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800806 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700807 return;
808 }
809
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100810 int32_t deviceId = msg.body.motion.deviceId;
811 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -0700812
813 // Update the touch state history to incorporate the new input message.
814 // If the message is in the past relative to the most recently produced resampled
815 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100816 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -0700817 case AMOTION_EVENT_ACTION_DOWN: {
818 ssize_t index = findTouchState(deviceId, source);
819 if (index < 0) {
820 mTouchStates.push();
821 index = mTouchStates.size() - 1;
822 }
823 TouchState& touchState = mTouchStates.editItemAt(index);
824 touchState.initialize(deviceId, source);
825 touchState.addHistory(msg);
826 break;
827 }
828
829 case AMOTION_EVENT_ACTION_MOVE: {
830 ssize_t index = findTouchState(deviceId, source);
831 if (index >= 0) {
832 TouchState& touchState = mTouchStates.editItemAt(index);
833 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800834 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700835 }
836 break;
837 }
838
839 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
840 ssize_t index = findTouchState(deviceId, source);
841 if (index >= 0) {
842 TouchState& touchState = mTouchStates.editItemAt(index);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100843 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -0700844 rewriteMessage(touchState, msg);
845 }
846 break;
847 }
848
849 case AMOTION_EVENT_ACTION_POINTER_UP: {
850 ssize_t index = findTouchState(deviceId, source);
851 if (index >= 0) {
852 TouchState& touchState = mTouchStates.editItemAt(index);
853 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100854 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -0700855 }
856 break;
857 }
858
859 case AMOTION_EVENT_ACTION_SCROLL: {
860 ssize_t index = findTouchState(deviceId, source);
861 if (index >= 0) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800862 TouchState& touchState = mTouchStates.editItemAt(index);
Jeff Brown5912f952013-07-01 19:10:31 -0700863 rewriteMessage(touchState, msg);
864 }
865 break;
866 }
867
868 case AMOTION_EVENT_ACTION_UP:
869 case AMOTION_EVENT_ACTION_CANCEL: {
870 ssize_t index = findTouchState(deviceId, source);
871 if (index >= 0) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800872 TouchState& touchState = mTouchStates.editItemAt(index);
Jeff Brown5912f952013-07-01 19:10:31 -0700873 rewriteMessage(touchState, msg);
874 mTouchStates.removeAt(index);
875 }
876 break;
877 }
878 }
879}
880
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800881/**
882 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
883 *
884 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
885 * is in the past relative to msg and the past two events do not contain identical coordinates),
886 * then invalidate the lastResample data for that pointer.
887 * If the two past events have identical coordinates, then lastResample data for that pointer will
888 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
889 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
890 * not equal to x0 is received.
891 */
892void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100893 nsecs_t eventTime = msg.body.motion.eventTime;
894 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
895 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -0700896 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100897 if (eventTime < state.lastResample.eventTime ||
898 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800899 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
900 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -0700901#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100902 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
903 resampleCoords.getX(), resampleCoords.getY(),
904 msgCoords.getX(), msgCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -0700905#endif
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800906 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
907 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
908 } else {
909 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100910 }
Jeff Brown5912f952013-07-01 19:10:31 -0700911 }
912 }
913}
914
915void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
916 const InputMessage* next) {
917 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800918 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -0700919 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
920 return;
921 }
922
923 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
924 if (index < 0) {
925#if DEBUG_RESAMPLING
926 ALOGD("Not resampled, no touch state for device.");
927#endif
928 return;
929 }
930
931 TouchState& touchState = mTouchStates.editItemAt(index);
932 if (touchState.historySize < 1) {
933#if DEBUG_RESAMPLING
934 ALOGD("Not resampled, no history for device.");
935#endif
936 return;
937 }
938
939 // Ensure that the current sample has all of the pointers that need to be reported.
940 const History* current = touchState.getHistory(0);
941 size_t pointerCount = event->getPointerCount();
942 for (size_t i = 0; i < pointerCount; i++) {
943 uint32_t id = event->getPointerId(i);
944 if (!current->idBits.hasBit(id)) {
945#if DEBUG_RESAMPLING
946 ALOGD("Not resampled, missing id %d", id);
947#endif
948 return;
949 }
950 }
951
952 // Find the data to use for resampling.
953 const History* other;
954 History future;
955 float alpha;
956 if (next) {
957 // Interpolate between current sample and future sample.
958 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100959 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -0700960 other = &future;
961 nsecs_t delta = future.eventTime - current->eventTime;
962 if (delta < RESAMPLE_MIN_DELTA) {
963#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100964 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -0700965#endif
966 return;
967 }
968 alpha = float(sampleTime - current->eventTime) / delta;
969 } else if (touchState.historySize >= 2) {
970 // Extrapolate future sample using current sample and past sample.
971 // So other->eventTime <= current->eventTime <= sampleTime.
972 other = touchState.getHistory(1);
973 nsecs_t delta = current->eventTime - other->eventTime;
974 if (delta < RESAMPLE_MIN_DELTA) {
975#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100976 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -0700977#endif
978 return;
979 } else if (delta > RESAMPLE_MAX_DELTA) {
980#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100981 ALOGD("Not resampled, delta time is too large: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -0700982#endif
983 return;
984 }
985 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
986 if (sampleTime > maxPredict) {
987#if DEBUG_RESAMPLING
988 ALOGD("Sample time is too far in the future, adjusting prediction "
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100989 "from %" PRId64 " to %" PRId64 " ns.",
Jeff Brown5912f952013-07-01 19:10:31 -0700990 sampleTime - current->eventTime, maxPredict - current->eventTime);
991#endif
992 sampleTime = maxPredict;
993 }
994 alpha = float(current->eventTime - sampleTime) / delta;
995 } else {
996#if DEBUG_RESAMPLING
997 ALOGD("Not resampled, insufficient data.");
998#endif
999 return;
1000 }
1001
1002 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001003 History oldLastResample;
1004 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001005 touchState.lastResample.eventTime = sampleTime;
1006 touchState.lastResample.idBits.clear();
1007 for (size_t i = 0; i < pointerCount; i++) {
1008 uint32_t id = event->getPointerId(i);
1009 touchState.lastResample.idToIndex[id] = i;
1010 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001011 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1012 // We maintain the previously resampled value for this pointer (stored in
1013 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1014 // This way we don't introduce artificial jitter when pointers haven't actually moved.
1015
1016 // We know here that the coordinates for the pointer haven't changed because we
1017 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1018 // lastResample in place becasue the mapping from pointer ID to index may have changed.
1019 touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id));
1020 continue;
1021 }
1022
Jeff Brown5912f952013-07-01 19:10:31 -07001023 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1024 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001025 resampledCoords.copyFrom(currentCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001026 if (other->idBits.hasBit(id)
1027 && shouldResampleTool(event->getToolType(i))) {
1028 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001029 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
1030 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
1031 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
1032 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
1033#if DEBUG_RESAMPLING
1034 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1035 "other (%0.3f, %0.3f), alpha %0.3f",
1036 id, resampledCoords.getX(), resampledCoords.getY(),
1037 currentCoords.getX(), currentCoords.getY(),
1038 otherCoords.getX(), otherCoords.getY(),
1039 alpha);
1040#endif
1041 } else {
Jeff Brown5912f952013-07-01 19:10:31 -07001042#if DEBUG_RESAMPLING
1043 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
1044 id, resampledCoords.getX(), resampledCoords.getY(),
1045 currentCoords.getX(), currentCoords.getY());
1046#endif
1047 }
1048 }
1049
1050 event->addSample(sampleTime, touchState.lastResample.pointers);
1051}
1052
1053bool InputConsumer::shouldResampleTool(int32_t toolType) {
1054 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
1055 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1056}
1057
1058status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08001059 if (DEBUG_TRANSPORT_ACTIONS) {
1060 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1061 mChannel->getName().c_str(), seq, toString(handled));
1062 }
Jeff Brown5912f952013-07-01 19:10:31 -07001063
1064 if (!seq) {
1065 ALOGE("Attempted to send a finished signal with sequence number 0.");
1066 return BAD_VALUE;
1067 }
1068
1069 // Send finished signals for the batch sequence chain first.
1070 size_t seqChainCount = mSeqChains.size();
1071 if (seqChainCount) {
1072 uint32_t currentSeq = seq;
1073 uint32_t chainSeqs[seqChainCount];
1074 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001075 for (size_t i = seqChainCount; i > 0; ) {
1076 i--;
Jeff Brown5912f952013-07-01 19:10:31 -07001077 const SeqChain& seqChain = mSeqChains.itemAt(i);
1078 if (seqChain.seq == currentSeq) {
1079 currentSeq = seqChain.chain;
1080 chainSeqs[chainIndex++] = currentSeq;
1081 mSeqChains.removeAt(i);
1082 }
1083 }
1084 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001085 while (!status && chainIndex > 0) {
1086 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001087 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1088 }
1089 if (status) {
1090 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001091 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001092 SeqChain seqChain;
1093 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1094 seqChain.chain = chainSeqs[chainIndex];
1095 mSeqChains.push(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001096 if (!chainIndex) break;
1097 chainIndex--;
1098 }
Jeff Brown5912f952013-07-01 19:10:31 -07001099 return status;
1100 }
1101 }
1102
1103 // Send finished signal for the last message in the batch.
1104 return sendUnchainedFinishedSignal(seq, handled);
1105}
1106
1107status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1108 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001109 msg.header.type = InputMessage::Type::FINISHED;
Jeff Brown5912f952013-07-01 19:10:31 -07001110 msg.body.finished.seq = seq;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -08001111 msg.body.finished.handled = handled ? 1 : 0;
Jeff Brown5912f952013-07-01 19:10:31 -07001112 return mChannel->sendMessage(&msg);
1113}
1114
1115bool InputConsumer::hasDeferredEvent() const {
1116 return mMsgDeferred;
1117}
1118
1119bool InputConsumer::hasPendingBatch() const {
1120 return !mBatches.isEmpty();
1121}
1122
1123ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1124 for (size_t i = 0; i < mBatches.size(); i++) {
1125 const Batch& batch = mBatches.itemAt(i);
1126 const InputMessage& head = batch.samples.itemAt(0);
1127 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1128 return i;
1129 }
1130 }
1131 return -1;
1132}
1133
1134ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1135 for (size_t i = 0; i < mTouchStates.size(); i++) {
1136 const TouchState& touchState = mTouchStates.itemAt(i);
1137 if (touchState.deviceId == deviceId && touchState.source == source) {
1138 return i;
1139 }
1140 }
1141 return -1;
1142}
1143
1144void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001145 event->initialize(msg->body.key.deviceId, msg->body.key.source, msg->body.key.displayId,
1146 msg->body.key.hmac, msg->body.key.action, msg->body.key.flags,
1147 msg->body.key.keyCode, msg->body.key.scanCode, msg->body.key.metaState,
1148 msg->body.key.repeatCount, msg->body.key.downTime, msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001149}
1150
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001151void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
1152 event->initialize(msg->body.focus.hasFocus == 1, msg->body.focus.inTouchMode == 1);
1153}
1154
Jeff Brown5912f952013-07-01 19:10:31 -07001155void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001156 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001157 PointerProperties pointerProperties[pointerCount];
1158 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001159 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001160 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
1161 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1162 }
1163
Garfield Tan00f511d2019-06-12 16:55:40 -07001164 event->initialize(msg->body.motion.deviceId, msg->body.motion.source,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001165 msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
Garfield Tan00f511d2019-06-12 16:55:40 -07001166 msg->body.motion.actionButton, msg->body.motion.flags,
1167 msg->body.motion.edgeFlags, msg->body.motion.metaState,
1168 msg->body.motion.buttonState, msg->body.motion.classification,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -06001169 msg->body.motion.xScale, msg->body.motion.yScale, msg->body.motion.xOffset,
1170 msg->body.motion.yOffset, msg->body.motion.xPrecision,
1171 msg->body.motion.yPrecision, msg->body.motion.xCursorPosition,
1172 msg->body.motion.yCursorPosition, msg->body.motion.downTime,
1173 msg->body.motion.eventTime, pointerCount, pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001174}
1175
1176void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001177 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001178 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001179 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001180 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1181 }
1182
1183 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1184 event->addSample(msg->body.motion.eventTime, pointerCoords);
1185}
1186
1187bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
1188 const InputMessage& head = batch.samples.itemAt(0);
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001189 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001190 if (head.body.motion.pointerCount != pointerCount
1191 || head.body.motion.action != msg->body.motion.action) {
1192 return false;
1193 }
1194 for (size_t i = 0; i < pointerCount; i++) {
1195 if (head.body.motion.pointers[i].properties
1196 != msg->body.motion.pointers[i].properties) {
1197 return false;
1198 }
1199 }
1200 return true;
1201}
1202
1203ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1204 size_t numSamples = batch.samples.size();
1205 size_t index = 0;
1206 while (index < numSamples
1207 && batch.samples.itemAt(index).body.motion.eventTime <= time) {
1208 index += 1;
1209 }
1210 return ssize_t(index) - 1;
1211}
1212
1213} // namespace android