blob: eda01c5032184834823700d76a08715b3286fa30 [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
Garfield Tanfbe732e2020-01-24 11:26:14 -080066// A placeholder sequence number used to initialize native input events before InputFlinger is
67// migrated to new sequence number system.
68static constexpr int32_t INPUT_FLINGER_SEQUENCE_NUM = 0;
69
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -060070/**
71 * System property for enabling / disabling touch resampling.
72 * Resampling extrapolates / interpolates the reported touch event coordinates to better
73 * align them to the VSYNC signal, thus resulting in smoother scrolling performance.
74 * Resampling is not needed (and should be disabled) on hardware that already
75 * has touch events triggered by VSYNC.
76 * Set to "1" to enable resampling (default).
77 * Set to "0" to disable resampling.
78 * Resampling is enabled by default.
79 */
80static const char* PROPERTY_RESAMPLING_ENABLED = "ro.input.resampling";
81
Jeff Brown5912f952013-07-01 19:10:31 -070082template<typename T>
83inline static T min(const T& a, const T& b) {
84 return a < b ? a : b;
85}
86
87inline static float lerp(float a, float b, float alpha) {
88 return a + alpha * (b - a);
89}
90
Siarhei Vishniakou128eab12019-05-23 10:25:59 +080091inline static bool isPointerEvent(int32_t source) {
92 return (source & AINPUT_SOURCE_CLASS_POINTER) == AINPUT_SOURCE_CLASS_POINTER;
93}
94
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -080095inline static const char* toString(bool value) {
96 return value ? "true" : "false";
97}
98
Jeff Brown5912f952013-07-01 19:10:31 -070099// --- InputMessage ---
100
101bool InputMessage::isValid(size_t actualSize) const {
102 if (size() == actualSize) {
103 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700104 case Type::KEY:
105 return true;
106 case Type::MOTION:
107 return body.motion.pointerCount > 0 && body.motion.pointerCount <= MAX_POINTERS;
108 case Type::FINISHED:
109 return true;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800110 case Type::FOCUS:
111 return true;
Jeff Brown5912f952013-07-01 19:10:31 -0700112 }
113 }
114 return false;
115}
116
117size_t InputMessage::size() const {
118 switch (header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700119 case Type::KEY:
120 return sizeof(Header) + body.key.size();
121 case Type::MOTION:
122 return sizeof(Header) + body.motion.size();
123 case Type::FINISHED:
124 return sizeof(Header) + body.finished.size();
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800125 case Type::FOCUS:
126 return sizeof(Header) + body.focus.size();
Jeff Brown5912f952013-07-01 19:10:31 -0700127 }
128 return sizeof(Header);
129}
130
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800131/**
132 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
133 * memory to zero, then only copy the valid bytes on a per-field basis.
134 */
135void InputMessage::getSanitizedCopy(InputMessage* msg) const {
136 memset(msg, 0, sizeof(*msg));
137
138 // Write the header
139 msg->header.type = header.type;
140
141 // Write the body
142 switch(header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700143 case InputMessage::Type::KEY: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800144 // uint32_t seq
145 msg->body.key.seq = body.key.seq;
146 // nsecs_t eventTime
147 msg->body.key.eventTime = body.key.eventTime;
148 // int32_t deviceId
149 msg->body.key.deviceId = body.key.deviceId;
150 // int32_t source
151 msg->body.key.source = body.key.source;
152 // int32_t displayId
153 msg->body.key.displayId = body.key.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600154 // std::array<uint8_t, 32> hmac
155 msg->body.key.hmac = body.key.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800156 // int32_t action
157 msg->body.key.action = body.key.action;
158 // int32_t flags
159 msg->body.key.flags = body.key.flags;
160 // int32_t keyCode
161 msg->body.key.keyCode = body.key.keyCode;
162 // int32_t scanCode
163 msg->body.key.scanCode = body.key.scanCode;
164 // int32_t metaState
165 msg->body.key.metaState = body.key.metaState;
166 // int32_t repeatCount
167 msg->body.key.repeatCount = body.key.repeatCount;
168 // nsecs_t downTime
169 msg->body.key.downTime = body.key.downTime;
170 break;
171 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700172 case InputMessage::Type::MOTION: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800173 // uint32_t seq
174 msg->body.motion.seq = body.motion.seq;
175 // nsecs_t eventTime
176 msg->body.motion.eventTime = body.motion.eventTime;
177 // int32_t deviceId
178 msg->body.motion.deviceId = body.motion.deviceId;
179 // int32_t source
180 msg->body.motion.source = body.motion.source;
181 // int32_t displayId
182 msg->body.motion.displayId = body.motion.displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600183 // std::array<uint8_t, 32> hmac
184 msg->body.motion.hmac = body.motion.hmac;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800185 // int32_t action
186 msg->body.motion.action = body.motion.action;
187 // int32_t actionButton
188 msg->body.motion.actionButton = body.motion.actionButton;
189 // int32_t flags
190 msg->body.motion.flags = body.motion.flags;
191 // int32_t metaState
192 msg->body.motion.metaState = body.motion.metaState;
193 // int32_t buttonState
194 msg->body.motion.buttonState = body.motion.buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800195 // MotionClassification classification
196 msg->body.motion.classification = body.motion.classification;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800197 // int32_t edgeFlags
198 msg->body.motion.edgeFlags = body.motion.edgeFlags;
199 // nsecs_t downTime
200 msg->body.motion.downTime = body.motion.downTime;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600201 // float xScale
202 msg->body.motion.xScale = body.motion.xScale;
203 // float yScale
204 msg->body.motion.yScale = body.motion.yScale;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800205 // float xOffset
206 msg->body.motion.xOffset = body.motion.xOffset;
207 // float yOffset
208 msg->body.motion.yOffset = body.motion.yOffset;
209 // float xPrecision
210 msg->body.motion.xPrecision = body.motion.xPrecision;
211 // float yPrecision
212 msg->body.motion.yPrecision = body.motion.yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700213 // float xCursorPosition
214 msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
215 // float yCursorPosition
216 msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800217 // uint32_t pointerCount
218 msg->body.motion.pointerCount = body.motion.pointerCount;
219 //struct Pointer pointers[MAX_POINTERS]
220 for (size_t i = 0; i < body.motion.pointerCount; i++) {
221 // PointerProperties properties
222 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
223 msg->body.motion.pointers[i].properties.toolType =
224 body.motion.pointers[i].properties.toolType,
225 // PointerCoords coords
226 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
227 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
228 memcpy(&msg->body.motion.pointers[i].coords.values[0],
229 &body.motion.pointers[i].coords.values[0],
230 count * (sizeof(body.motion.pointers[i].coords.values[0])));
231 }
232 break;
233 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700234 case InputMessage::Type::FINISHED: {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800235 msg->body.finished.seq = body.finished.seq;
236 msg->body.finished.handled = body.finished.handled;
237 break;
238 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800239 case InputMessage::Type::FOCUS: {
240 msg->body.focus.seq = body.focus.seq;
241 msg->body.focus.hasFocus = body.focus.hasFocus;
242 msg->body.focus.inTouchMode = body.focus.inTouchMode;
243 break;
244 }
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800245 }
246}
Jeff Brown5912f952013-07-01 19:10:31 -0700247
248// --- InputChannel ---
249
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700250sp<InputChannel> InputChannel::create(const std::string& name, android::base::unique_fd fd,
251 sp<IBinder> token) {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700252 const int result = fcntl(fd, F_SETFL, O_NONBLOCK);
253 if (result != 0) {
254 LOG_ALWAYS_FATAL("channel '%s' ~ Could not make socket non-blocking: %s", name.c_str(),
255 strerror(errno));
256 return nullptr;
257 }
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700258 return new InputChannel(name, std::move(fd), token);
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700259}
260
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700261InputChannel::InputChannel(const std::string& name, android::base::unique_fd fd, sp<IBinder> token)
262 : mName(name), mFd(std::move(fd)), mToken(token) {
263 if (DEBUG_CHANNEL_LIFECYCLE) {
264 ALOGD("Input channel constructed: name='%s', fd=%d", mName.c_str(), mFd.get());
265 }
Jeff Brown5912f952013-07-01 19:10:31 -0700266}
267
268InputChannel::~InputChannel() {
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700269 if (DEBUG_CHANNEL_LIFECYCLE) {
270 ALOGD("Input channel destroyed: name='%s', fd=%d", mName.c_str(), mFd.get());
271 }
Robert Carr3720ed02018-08-08 16:08:27 -0700272}
273
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800274status_t InputChannel::openInputChannelPair(const std::string& name,
Jeff Brown5912f952013-07-01 19:10:31 -0700275 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) {
276 int sockets[2];
277 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
278 status_t result = -errno;
279 ALOGE("channel '%s' ~ Could not create socket pair. errno=%d",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800280 name.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700281 outServerChannel.clear();
282 outClientChannel.clear();
283 return result;
284 }
285
286 int bufferSize = SOCKET_BUFFER_SIZE;
287 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
288 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
289 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
290 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
291
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700292 sp<IBinder> token = new BBinder();
293
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700294 std::string serverChannelName = name + " (server)";
295 android::base::unique_fd serverFd(sockets[0]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700296 outServerChannel = InputChannel::create(serverChannelName, std::move(serverFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700297
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700298 std::string clientChannelName = name + " (client)";
299 android::base::unique_fd clientFd(sockets[1]);
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700300 outClientChannel = InputChannel::create(clientChannelName, std::move(clientFd), token);
Jeff Brown5912f952013-07-01 19:10:31 -0700301 return OK;
302}
303
304status_t InputChannel::sendMessage(const InputMessage* msg) {
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800305 const size_t msgLength = msg->size();
306 InputMessage cleanMsg;
307 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700308 ssize_t nWrite;
309 do {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700310 nWrite = ::send(mFd.get(), &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700311 } while (nWrite == -1 && errno == EINTR);
312
313 if (nWrite < 0) {
314 int error = errno;
315#if DEBUG_CHANNEL_MESSAGES
chaviw81e2bb92019-12-18 15:03:51 -0800316 ALOGD("channel '%s' ~ error sending message of type %d, %s", mName.c_str(),
317 msg->header.type, strerror(error));
Jeff Brown5912f952013-07-01 19:10:31 -0700318#endif
319 if (error == EAGAIN || error == EWOULDBLOCK) {
320 return WOULD_BLOCK;
321 }
322 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
323 return DEAD_OBJECT;
324 }
325 return -error;
326 }
327
328 if (size_t(nWrite) != msgLength) {
329#if DEBUG_CHANNEL_MESSAGES
330 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800331 mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700332#endif
333 return DEAD_OBJECT;
334 }
335
336#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800337 ALOGD("channel '%s' ~ sent message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700338#endif
339 return OK;
340}
341
342status_t InputChannel::receiveMessage(InputMessage* msg) {
343 ssize_t nRead;
344 do {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700345 nRead = ::recv(mFd.get(), msg, sizeof(InputMessage), MSG_DONTWAIT);
Jeff Brown5912f952013-07-01 19:10:31 -0700346 } while (nRead == -1 && errno == EINTR);
347
348 if (nRead < 0) {
349 int error = errno;
350#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800351 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.c_str(), errno);
Jeff Brown5912f952013-07-01 19:10:31 -0700352#endif
353 if (error == EAGAIN || error == EWOULDBLOCK) {
354 return WOULD_BLOCK;
355 }
356 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
357 return DEAD_OBJECT;
358 }
359 return -error;
360 }
361
362 if (nRead == 0) { // check for EOF
363#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800364 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700365#endif
366 return DEAD_OBJECT;
367 }
368
369 if (!msg->isValid(nRead)) {
370#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800371 ALOGD("channel '%s' ~ received invalid message", mName.c_str());
Jeff Brown5912f952013-07-01 19:10:31 -0700372#endif
373 return BAD_VALUE;
374 }
375
376#if DEBUG_CHANNEL_MESSAGES
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800377 ALOGD("channel '%s' ~ received message of type %d", mName.c_str(), msg->header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700378#endif
379 return OK;
380}
381
382sp<InputChannel> InputChannel::dup() const {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700383 android::base::unique_fd newFd(::dup(getFd()));
384 if (!newFd.ok()) {
385 ALOGE("Could not duplicate fd %i for channel %s: %s", getFd(), mName.c_str(),
386 strerror(errno));
Siarhei Vishniakou3d8df0e2019-09-17 14:53:07 +0100387 const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
388 // If this process is out of file descriptors, then throwing that might end up exploding
389 // on the other side of a binder call, which isn't really helpful.
390 // Better to just crash here and hope that the FD leak is slow.
391 // Other failures could be client errors, so we still propagate those back to the caller.
392 LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s",
393 getName().c_str());
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700394 return nullptr;
395 }
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700396 return InputChannel::create(mName, std::move(newFd), mToken);
Jeff Brown5912f952013-07-01 19:10:31 -0700397}
398
Robert Carr3720ed02018-08-08 16:08:27 -0700399status_t InputChannel::write(Parcel& out) const {
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700400 status_t s = out.writeCString(getName().c_str());
Robert Carr3720ed02018-08-08 16:08:27 -0700401 if (s != OK) {
402 return s;
403 }
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700404
Robert Carr803535b2018-08-02 16:38:15 -0700405 s = out.writeStrongBinder(mToken);
406 if (s != OK) {
407 return s;
408 }
Robert Carr3720ed02018-08-08 16:08:27 -0700409
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700410 s = out.writeUniqueFileDescriptor(mFd);
Robert Carr3720ed02018-08-08 16:08:27 -0700411 return s;
412}
413
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700414sp<InputChannel> InputChannel::read(const Parcel& from) {
415 std::string name = from.readCString();
416 sp<IBinder> token = from.readStrongBinder();
417 android::base::unique_fd rawFd;
418 status_t fdResult = from.readUniqueFileDescriptor(&rawFd);
419 if (fdResult != OK) {
420 return nullptr;
Robert Carr3720ed02018-08-08 16:08:27 -0700421 }
422
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700423 return InputChannel::create(name, std::move(rawFd), token);
Robert Carr3720ed02018-08-08 16:08:27 -0700424}
425
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700426sp<IBinder> InputChannel::getConnectionToken() const {
Robert Carr803535b2018-08-02 16:38:15 -0700427 return mToken;
428}
429
Jeff Brown5912f952013-07-01 19:10:31 -0700430// --- InputPublisher ---
431
432InputPublisher::InputPublisher(const sp<InputChannel>& channel) :
433 mChannel(channel) {
434}
435
436InputPublisher::~InputPublisher() {
437}
438
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600439status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t deviceId, int32_t source,
440 int32_t displayId, std::array<uint8_t, 32> hmac,
441 int32_t action, int32_t flags, int32_t keyCode,
442 int32_t scanCode, int32_t metaState, int32_t repeatCount,
443 nsecs_t downTime, nsecs_t eventTime) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000444 if (ATRACE_ENABLED()) {
445 std::string message = StringPrintf("publishKeyEvent(inputChannel=%s, keyCode=%" PRId32 ")",
446 mChannel->getName().c_str(), keyCode);
447 ATRACE_NAME(message.c_str());
448 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800449 if (DEBUG_TRANSPORT_ACTIONS) {
450 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
451 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
452 "downTime=%" PRId64 ", eventTime=%" PRId64,
453 mChannel->getName().c_str(), seq, deviceId, source, action, flags, keyCode, scanCode,
454 metaState, repeatCount, downTime, eventTime);
455 }
Jeff Brown5912f952013-07-01 19:10:31 -0700456
457 if (!seq) {
458 ALOGE("Attempted to publish a key event with sequence number 0.");
459 return BAD_VALUE;
460 }
461
462 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700463 msg.header.type = InputMessage::Type::KEY;
Jeff Brown5912f952013-07-01 19:10:31 -0700464 msg.body.key.seq = seq;
465 msg.body.key.deviceId = deviceId;
466 msg.body.key.source = source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100467 msg.body.key.displayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600468 msg.body.key.hmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700469 msg.body.key.action = action;
470 msg.body.key.flags = flags;
471 msg.body.key.keyCode = keyCode;
472 msg.body.key.scanCode = scanCode;
473 msg.body.key.metaState = metaState;
474 msg.body.key.repeatCount = repeatCount;
475 msg.body.key.downTime = downTime;
476 msg.body.key.eventTime = eventTime;
477 return mChannel->sendMessage(&msg);
478}
479
480status_t InputPublisher::publishMotionEvent(
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600481 uint32_t seq, int32_t deviceId, int32_t source, int32_t displayId,
482 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
483 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
484 MotionClassification classification, float xScale, float yScale, float xOffset,
485 float yOffset, float xPrecision, float yPrecision, float xCursorPosition,
486 float yCursorPosition, nsecs_t downTime, nsecs_t eventTime, uint32_t pointerCount,
Garfield Tan00f511d2019-06-12 16:55:40 -0700487 const PointerProperties* pointerProperties, const PointerCoords* pointerCoords) {
Michael Wright3dd60e22019-03-27 22:06:44 +0000488 if (ATRACE_ENABLED()) {
489 std::string message = StringPrintf(
490 "publishMotionEvent(inputChannel=%s, action=%" PRId32 ")",
491 mChannel->getName().c_str(), action);
492 ATRACE_NAME(message.c_str());
493 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800494 if (DEBUG_TRANSPORT_ACTIONS) {
495 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
496 "displayId=%" PRId32 ", "
497 "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600498 "metaState=0x%x, buttonState=0x%x, classification=%s, xScale=%.1f, yScale=%.1f, "
499 "xOffset=%.1f, yOffset=%.1f, "
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800500 "xPrecision=%f, yPrecision=%f, downTime=%" PRId64 ", eventTime=%" PRId64 ", "
501 "pointerCount=%" PRIu32,
502 mChannel->getName().c_str(), seq, deviceId, source, displayId, action, actionButton,
503 flags, edgeFlags, metaState, buttonState,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600504 motionClassificationToString(classification), xScale, yScale, xOffset, yOffset,
505 xPrecision, yPrecision, downTime, eventTime, pointerCount);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800506 }
Jeff Brown5912f952013-07-01 19:10:31 -0700507
508 if (!seq) {
509 ALOGE("Attempted to publish a motion event with sequence number 0.");
510 return BAD_VALUE;
511 }
512
513 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700514 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800515 mChannel->getName().c_str(), pointerCount);
Jeff Brown5912f952013-07-01 19:10:31 -0700516 return BAD_VALUE;
517 }
518
519 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700520 msg.header.type = InputMessage::Type::MOTION;
Jeff Brown5912f952013-07-01 19:10:31 -0700521 msg.body.motion.seq = seq;
522 msg.body.motion.deviceId = deviceId;
523 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700524 msg.body.motion.displayId = displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600525 msg.body.motion.hmac = hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700526 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100527 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700528 msg.body.motion.flags = flags;
529 msg.body.motion.edgeFlags = edgeFlags;
530 msg.body.motion.metaState = metaState;
531 msg.body.motion.buttonState = buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800532 msg.body.motion.classification = classification;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600533 msg.body.motion.xScale = xScale;
534 msg.body.motion.yScale = yScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700535 msg.body.motion.xOffset = xOffset;
536 msg.body.motion.yOffset = yOffset;
537 msg.body.motion.xPrecision = xPrecision;
538 msg.body.motion.yPrecision = yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700539 msg.body.motion.xCursorPosition = xCursorPosition;
540 msg.body.motion.yCursorPosition = yCursorPosition;
Jeff Brown5912f952013-07-01 19:10:31 -0700541 msg.body.motion.downTime = downTime;
542 msg.body.motion.eventTime = eventTime;
543 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100544 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700545 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
546 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
547 }
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700548
Jeff Brown5912f952013-07-01 19:10:31 -0700549 return mChannel->sendMessage(&msg);
550}
551
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800552status_t InputPublisher::publishFocusEvent(uint32_t seq, bool hasFocus, bool inTouchMode) {
553 if (ATRACE_ENABLED()) {
554 std::string message =
555 StringPrintf("publishFocusEvent(inputChannel=%s, hasFocus=%s, inTouchMode=%s)",
556 mChannel->getName().c_str(), toString(hasFocus),
557 toString(inTouchMode));
558 ATRACE_NAME(message.c_str());
559 }
560
561 InputMessage msg;
562 msg.header.type = InputMessage::Type::FOCUS;
563 msg.body.focus.seq = seq;
564 msg.body.focus.hasFocus = hasFocus ? 1 : 0;
565 msg.body.focus.inTouchMode = inTouchMode ? 1 : 0;
566 return mChannel->sendMessage(&msg);
567}
568
Jeff Brown5912f952013-07-01 19:10:31 -0700569status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandled) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800570 if (DEBUG_TRANSPORT_ACTIONS) {
571 ALOGD("channel '%s' publisher ~ receiveFinishedSignal", mChannel->getName().c_str());
572 }
Jeff Brown5912f952013-07-01 19:10:31 -0700573
574 InputMessage msg;
575 status_t result = mChannel->receiveMessage(&msg);
576 if (result) {
577 *outSeq = 0;
578 *outHandled = false;
579 return result;
580 }
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700581 if (msg.header.type != InputMessage::Type::FINISHED) {
Jeff Brown5912f952013-07-01 19:10:31 -0700582 ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer",
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800583 mChannel->getName().c_str(), msg.header.type);
Jeff Brown5912f952013-07-01 19:10:31 -0700584 return UNKNOWN_ERROR;
585 }
586 *outSeq = msg.body.finished.seq;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800587 *outHandled = msg.body.finished.handled == 1;
Jeff Brown5912f952013-07-01 19:10:31 -0700588 return OK;
589}
590
591// --- InputConsumer ---
592
593InputConsumer::InputConsumer(const sp<InputChannel>& channel) :
594 mResampleTouch(isTouchResamplingEnabled()),
595 mChannel(channel), mMsgDeferred(false) {
596}
597
598InputConsumer::~InputConsumer() {
599}
600
601bool InputConsumer::isTouchResamplingEnabled() {
Siarhei Vishniakoub5433e92019-02-21 09:27:39 -0600602 return property_get_bool(PROPERTY_RESAMPLING_ENABLED, true);
Jeff Brown5912f952013-07-01 19:10:31 -0700603}
604
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800605status_t InputConsumer::consume(InputEventFactoryInterface* factory, bool consumeBatches,
606 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800607 if (DEBUG_TRANSPORT_ACTIONS) {
608 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%" PRId64,
609 mChannel->getName().c_str(), toString(consumeBatches), frameTime);
610 }
Jeff Brown5912f952013-07-01 19:10:31 -0700611
612 *outSeq = 0;
Yi Kong5bed83b2018-07-17 12:53:47 -0700613 *outEvent = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700614
615 // Fetch the next input message.
616 // Loop until an event can be returned or no additional events are received.
617 while (!*outEvent) {
618 if (mMsgDeferred) {
619 // mMsg contains a valid input message from the previous call to consume
620 // that has not yet been processed.
621 mMsgDeferred = false;
622 } else {
623 // Receive a fresh message.
624 status_t result = mChannel->receiveMessage(&mMsg);
625 if (result) {
626 // Consume the next batched event unless batches are being held for later.
627 if (consumeBatches || result != WOULD_BLOCK) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800628 result = consumeBatch(factory, frameTime, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700629 if (*outEvent) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800630 if (DEBUG_TRANSPORT_ACTIONS) {
631 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
632 mChannel->getName().c_str(), *outSeq);
633 }
Jeff Brown5912f952013-07-01 19:10:31 -0700634 break;
635 }
636 }
637 return result;
638 }
639 }
640
641 switch (mMsg.header.type) {
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700642 case InputMessage::Type::KEY: {
643 KeyEvent* keyEvent = factory->createKeyEvent();
644 if (!keyEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700645
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700646 initializeKeyEvent(keyEvent, &mMsg);
647 *outSeq = mMsg.body.key.seq;
648 *outEvent = keyEvent;
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800649 if (DEBUG_TRANSPORT_ACTIONS) {
650 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
651 mChannel->getName().c_str(), *outSeq);
652 }
Jeff Brown5912f952013-07-01 19:10:31 -0700653 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700654 }
Jeff Brown5912f952013-07-01 19:10:31 -0700655
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700656 case InputMessage::Type::MOTION: {
657 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
658 if (batchIndex >= 0) {
659 Batch& batch = mBatches.editItemAt(batchIndex);
660 if (canAddSample(batch, &mMsg)) {
661 batch.samples.push(mMsg);
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800662 if (DEBUG_TRANSPORT_ACTIONS) {
663 ALOGD("channel '%s' consumer ~ appended to batch event",
664 mChannel->getName().c_str());
665 }
Jeff Brown5912f952013-07-01 19:10:31 -0700666 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700667 } else if (isPointerEvent(mMsg.body.motion.source) &&
668 mMsg.body.motion.action == AMOTION_EVENT_ACTION_CANCEL) {
669 // No need to process events that we are going to cancel anyways
670 const size_t count = batch.samples.size();
671 for (size_t i = 0; i < count; i++) {
672 const InputMessage& msg = batch.samples.itemAt(i);
673 sendFinishedSignal(msg.body.motion.seq, false);
674 }
675 batch.samples.removeItemsAt(0, count);
676 mBatches.removeAt(batchIndex);
677 } else {
678 // We cannot append to the batch in progress, so we need to consume
679 // the previous batch right now and defer the new message until later.
680 mMsgDeferred = true;
681 status_t result = consumeSamples(factory, batch, batch.samples.size(),
682 outSeq, outEvent);
683 mBatches.removeAt(batchIndex);
684 if (result) {
685 return result;
686 }
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800687 if (DEBUG_TRANSPORT_ACTIONS) {
688 ALOGD("channel '%s' consumer ~ consumed batch event and "
689 "deferred current event, seq=%u",
690 mChannel->getName().c_str(), *outSeq);
691 }
Jeff Brown5912f952013-07-01 19:10:31 -0700692 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700693 }
Jeff Brown5912f952013-07-01 19:10:31 -0700694 }
Jeff Brown5912f952013-07-01 19:10:31 -0700695
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800696 // Start a new batch if needed.
697 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE ||
698 mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
699 mBatches.push();
700 Batch& batch = mBatches.editTop();
701 batch.samples.push(mMsg);
702 if (DEBUG_TRANSPORT_ACTIONS) {
703 ALOGD("channel '%s' consumer ~ started batch event",
704 mChannel->getName().c_str());
705 }
706 break;
707 }
Jeff Brown5912f952013-07-01 19:10:31 -0700708
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800709 MotionEvent* motionEvent = factory->createMotionEvent();
710 if (!motionEvent) return NO_MEMORY;
Jeff Brown5912f952013-07-01 19:10:31 -0700711
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800712 updateTouchState(mMsg);
713 initializeMotionEvent(motionEvent, &mMsg);
714 *outSeq = mMsg.body.motion.seq;
715 *outEvent = motionEvent;
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800716
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800717 if (DEBUG_TRANSPORT_ACTIONS) {
718 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
719 mChannel->getName().c_str(), *outSeq);
720 }
721 break;
Siarhei Vishniakou52402772019-10-22 09:32:30 -0700722 }
Jeff Brown5912f952013-07-01 19:10:31 -0700723
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -0800724 case InputMessage::Type::FINISHED: {
725 LOG_ALWAYS_FATAL("Consumed a FINISHED message, which should never be seen by "
726 "InputConsumer!");
727 break;
728 }
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800729
730 case InputMessage::Type::FOCUS: {
731 FocusEvent* focusEvent = factory->createFocusEvent();
732 if (!focusEvent) return NO_MEMORY;
733
734 initializeFocusEvent(focusEvent, &mMsg);
735 *outSeq = mMsg.body.focus.seq;
736 *outEvent = focusEvent;
737 break;
738 }
Jeff Brown5912f952013-07-01 19:10:31 -0700739 }
740 }
741 return OK;
742}
743
744status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800745 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700746 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700747 for (size_t i = mBatches.size(); i > 0; ) {
748 i--;
Jeff Brown5912f952013-07-01 19:10:31 -0700749 Batch& batch = mBatches.editItemAt(i);
Michael Wright32232172013-10-21 12:05:22 -0700750 if (frameTime < 0) {
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800751 result = consumeSamples(factory, batch, batch.samples.size(), outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700752 mBatches.removeAt(i);
753 return result;
754 }
755
Michael Wright32232172013-10-21 12:05:22 -0700756 nsecs_t sampleTime = frameTime;
757 if (mResampleTouch) {
758 sampleTime -= RESAMPLE_LATENCY;
759 }
Jeff Brown5912f952013-07-01 19:10:31 -0700760 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
761 if (split < 0) {
762 continue;
763 }
764
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800765 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700766 const InputMessage* next;
767 if (batch.samples.isEmpty()) {
768 mBatches.removeAt(i);
Yi Kong5bed83b2018-07-17 12:53:47 -0700769 next = nullptr;
Jeff Brown5912f952013-07-01 19:10:31 -0700770 } else {
771 next = &batch.samples.itemAt(0);
772 }
Michael Wright32232172013-10-21 12:05:22 -0700773 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700774 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
775 }
776 return result;
777 }
778
779 return WOULD_BLOCK;
780}
781
782status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800783 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown5912f952013-07-01 19:10:31 -0700784 MotionEvent* motionEvent = factory->createMotionEvent();
785 if (! motionEvent) return NO_MEMORY;
786
787 uint32_t chain = 0;
788 for (size_t i = 0; i < count; i++) {
789 InputMessage& msg = batch.samples.editItemAt(i);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100790 updateTouchState(msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700791 if (i) {
792 SeqChain seqChain;
793 seqChain.seq = msg.body.motion.seq;
794 seqChain.chain = chain;
795 mSeqChains.push(seqChain);
796 addSample(motionEvent, &msg);
797 } else {
798 initializeMotionEvent(motionEvent, &msg);
799 }
800 chain = msg.body.motion.seq;
801 }
802 batch.samples.removeItemsAt(0, count);
803
804 *outSeq = chain;
805 *outEvent = motionEvent;
806 return OK;
807}
808
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100809void InputConsumer::updateTouchState(InputMessage& msg) {
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800810 if (!mResampleTouch || !isPointerEvent(msg.body.motion.source)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700811 return;
812 }
813
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100814 int32_t deviceId = msg.body.motion.deviceId;
815 int32_t source = msg.body.motion.source;
Jeff Brown5912f952013-07-01 19:10:31 -0700816
817 // Update the touch state history to incorporate the new input message.
818 // If the message is in the past relative to the most recently produced resampled
819 // touch, then use the resampled time and coordinates instead.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100820 switch (msg.body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown5912f952013-07-01 19:10:31 -0700821 case AMOTION_EVENT_ACTION_DOWN: {
822 ssize_t index = findTouchState(deviceId, source);
823 if (index < 0) {
824 mTouchStates.push();
825 index = mTouchStates.size() - 1;
826 }
827 TouchState& touchState = mTouchStates.editItemAt(index);
828 touchState.initialize(deviceId, source);
829 touchState.addHistory(msg);
830 break;
831 }
832
833 case AMOTION_EVENT_ACTION_MOVE: {
834 ssize_t index = findTouchState(deviceId, source);
835 if (index >= 0) {
836 TouchState& touchState = mTouchStates.editItemAt(index);
837 touchState.addHistory(msg);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800838 rewriteMessage(touchState, msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700839 }
840 break;
841 }
842
843 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
844 ssize_t index = findTouchState(deviceId, source);
845 if (index >= 0) {
846 TouchState& touchState = mTouchStates.editItemAt(index);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100847 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -0700848 rewriteMessage(touchState, msg);
849 }
850 break;
851 }
852
853 case AMOTION_EVENT_ACTION_POINTER_UP: {
854 ssize_t index = findTouchState(deviceId, source);
855 if (index >= 0) {
856 TouchState& touchState = mTouchStates.editItemAt(index);
857 rewriteMessage(touchState, msg);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100858 touchState.lastResample.idBits.clearBit(msg.body.motion.getActionId());
Jeff Brown5912f952013-07-01 19:10:31 -0700859 }
860 break;
861 }
862
863 case AMOTION_EVENT_ACTION_SCROLL: {
864 ssize_t index = findTouchState(deviceId, source);
865 if (index >= 0) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800866 TouchState& touchState = mTouchStates.editItemAt(index);
Jeff Brown5912f952013-07-01 19:10:31 -0700867 rewriteMessage(touchState, msg);
868 }
869 break;
870 }
871
872 case AMOTION_EVENT_ACTION_UP:
873 case AMOTION_EVENT_ACTION_CANCEL: {
874 ssize_t index = findTouchState(deviceId, source);
875 if (index >= 0) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800876 TouchState& touchState = mTouchStates.editItemAt(index);
Jeff Brown5912f952013-07-01 19:10:31 -0700877 rewriteMessage(touchState, msg);
878 mTouchStates.removeAt(index);
879 }
880 break;
881 }
882 }
883}
884
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800885/**
886 * Replace the coordinates in msg with the coordinates in lastResample, if necessary.
887 *
888 * If lastResample is no longer valid for a specific pointer (i.e. the lastResample time
889 * is in the past relative to msg and the past two events do not contain identical coordinates),
890 * then invalidate the lastResample data for that pointer.
891 * If the two past events have identical coordinates, then lastResample data for that pointer will
892 * remain valid, and will be used to replace these coordinates. Thus, if a certain coordinate x0 is
893 * resampled to the new value x1, then x1 will always be used to replace x0 until some new value
894 * not equal to x0 is received.
895 */
896void InputConsumer::rewriteMessage(TouchState& state, InputMessage& msg) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100897 nsecs_t eventTime = msg.body.motion.eventTime;
898 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
899 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -0700900 if (state.lastResample.idBits.hasBit(id)) {
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100901 if (eventTime < state.lastResample.eventTime ||
902 state.recentCoordinatesAreIdentical(id)) {
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800903 PointerCoords& msgCoords = msg.body.motion.pointers[i].coords;
904 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -0700905#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100906 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
907 resampleCoords.getX(), resampleCoords.getY(),
908 msgCoords.getX(), msgCoords.getY());
Jeff Brown5912f952013-07-01 19:10:31 -0700909#endif
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800910 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
911 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
912 } else {
913 state.lastResample.idBits.clearBit(id);
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100914 }
Jeff Brown5912f952013-07-01 19:10:31 -0700915 }
916 }
917}
918
919void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
920 const InputMessage* next) {
921 if (!mResampleTouch
Siarhei Vishniakou128eab12019-05-23 10:25:59 +0800922 || !(isPointerEvent(event->getSource()))
Jeff Brown5912f952013-07-01 19:10:31 -0700923 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
924 return;
925 }
926
927 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
928 if (index < 0) {
929#if DEBUG_RESAMPLING
930 ALOGD("Not resampled, no touch state for device.");
931#endif
932 return;
933 }
934
935 TouchState& touchState = mTouchStates.editItemAt(index);
936 if (touchState.historySize < 1) {
937#if DEBUG_RESAMPLING
938 ALOGD("Not resampled, no history for device.");
939#endif
940 return;
941 }
942
943 // Ensure that the current sample has all of the pointers that need to be reported.
944 const History* current = touchState.getHistory(0);
945 size_t pointerCount = event->getPointerCount();
946 for (size_t i = 0; i < pointerCount; i++) {
947 uint32_t id = event->getPointerId(i);
948 if (!current->idBits.hasBit(id)) {
949#if DEBUG_RESAMPLING
950 ALOGD("Not resampled, missing id %d", id);
951#endif
952 return;
953 }
954 }
955
956 // Find the data to use for resampling.
957 const History* other;
958 History future;
959 float alpha;
960 if (next) {
961 // Interpolate between current sample and future sample.
962 // So current->eventTime <= sampleTime <= future.eventTime.
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100963 future.initializeFrom(*next);
Jeff Brown5912f952013-07-01 19:10:31 -0700964 other = &future;
965 nsecs_t delta = future.eventTime - current->eventTime;
966 if (delta < RESAMPLE_MIN_DELTA) {
967#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100968 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -0700969#endif
970 return;
971 }
972 alpha = float(sampleTime - current->eventTime) / delta;
973 } else if (touchState.historySize >= 2) {
974 // Extrapolate future sample using current sample and past sample.
975 // So other->eventTime <= current->eventTime <= sampleTime.
976 other = touchState.getHistory(1);
977 nsecs_t delta = current->eventTime - other->eventTime;
978 if (delta < RESAMPLE_MIN_DELTA) {
979#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100980 ALOGD("Not resampled, delta time is too small: %" PRId64 " ns.", delta);
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -0700981#endif
982 return;
983 } else if (delta > RESAMPLE_MAX_DELTA) {
984#if DEBUG_RESAMPLING
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100985 ALOGD("Not resampled, delta time is too large: %" PRId64 " ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -0700986#endif
987 return;
988 }
989 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
990 if (sampleTime > maxPredict) {
991#if DEBUG_RESAMPLING
992 ALOGD("Sample time is too far in the future, adjusting prediction "
Siarhei Vishniakou0aeec072017-06-12 15:01:41 +0100993 "from %" PRId64 " to %" PRId64 " ns.",
Jeff Brown5912f952013-07-01 19:10:31 -0700994 sampleTime - current->eventTime, maxPredict - current->eventTime);
995#endif
996 sampleTime = maxPredict;
997 }
998 alpha = float(current->eventTime - sampleTime) / delta;
999 } else {
1000#if DEBUG_RESAMPLING
1001 ALOGD("Not resampled, insufficient data.");
1002#endif
1003 return;
1004 }
1005
1006 // Resample touch coordinates.
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001007 History oldLastResample;
1008 oldLastResample.initializeFrom(touchState.lastResample);
Jeff Brown5912f952013-07-01 19:10:31 -07001009 touchState.lastResample.eventTime = sampleTime;
1010 touchState.lastResample.idBits.clear();
1011 for (size_t i = 0; i < pointerCount; i++) {
1012 uint32_t id = event->getPointerId(i);
1013 touchState.lastResample.idToIndex[id] = i;
1014 touchState.lastResample.idBits.markBit(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001015 if (oldLastResample.hasPointerId(id) && touchState.recentCoordinatesAreIdentical(id)) {
1016 // We maintain the previously resampled value for this pointer (stored in
1017 // oldLastResample) when the coordinates for this pointer haven't changed since then.
1018 // This way we don't introduce artificial jitter when pointers haven't actually moved.
1019
1020 // We know here that the coordinates for the pointer haven't changed because we
1021 // would've cleared the resampled bit in rewriteMessage if they had. We can't modify
1022 // lastResample in place becasue the mapping from pointer ID to index may have changed.
1023 touchState.lastResample.pointers[i].copyFrom(oldLastResample.getPointerById(id));
1024 continue;
1025 }
1026
Jeff Brown5912f952013-07-01 19:10:31 -07001027 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
1028 const PointerCoords& currentCoords = current->getPointerById(id);
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -08001029 resampledCoords.copyFrom(currentCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001030 if (other->idBits.hasBit(id)
1031 && shouldResampleTool(event->getToolType(i))) {
1032 const PointerCoords& otherCoords = other->getPointerById(id);
Jeff Brown5912f952013-07-01 19:10:31 -07001033 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
1034 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
1035 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
1036 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
1037#if DEBUG_RESAMPLING
1038 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
1039 "other (%0.3f, %0.3f), alpha %0.3f",
1040 id, resampledCoords.getX(), resampledCoords.getY(),
1041 currentCoords.getX(), currentCoords.getY(),
1042 otherCoords.getX(), otherCoords.getY(),
1043 alpha);
1044#endif
1045 } else {
Jeff Brown5912f952013-07-01 19:10:31 -07001046#if DEBUG_RESAMPLING
1047 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
1048 id, resampledCoords.getX(), resampledCoords.getY(),
1049 currentCoords.getX(), currentCoords.getY());
1050#endif
1051 }
1052 }
1053
1054 event->addSample(sampleTime, touchState.lastResample.pointers);
1055}
1056
1057bool InputConsumer::shouldResampleTool(int32_t toolType) {
1058 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
1059 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1060}
1061
1062status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Siarhei Vishniakou3b37f9a2019-11-23 13:42:41 -08001063 if (DEBUG_TRANSPORT_ACTIONS) {
1064 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
1065 mChannel->getName().c_str(), seq, toString(handled));
1066 }
Jeff Brown5912f952013-07-01 19:10:31 -07001067
1068 if (!seq) {
1069 ALOGE("Attempted to send a finished signal with sequence number 0.");
1070 return BAD_VALUE;
1071 }
1072
1073 // Send finished signals for the batch sequence chain first.
1074 size_t seqChainCount = mSeqChains.size();
1075 if (seqChainCount) {
1076 uint32_t currentSeq = seq;
1077 uint32_t chainSeqs[seqChainCount];
1078 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -07001079 for (size_t i = seqChainCount; i > 0; ) {
1080 i--;
Jeff Brown5912f952013-07-01 19:10:31 -07001081 const SeqChain& seqChain = mSeqChains.itemAt(i);
1082 if (seqChain.seq == currentSeq) {
1083 currentSeq = seqChain.chain;
1084 chainSeqs[chainIndex++] = currentSeq;
1085 mSeqChains.removeAt(i);
1086 }
1087 }
1088 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -07001089 while (!status && chainIndex > 0) {
1090 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -07001091 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
1092 }
1093 if (status) {
1094 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +08001095 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -07001096 SeqChain seqChain;
1097 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
1098 seqChain.chain = chainSeqs[chainIndex];
1099 mSeqChains.push(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +08001100 if (!chainIndex) break;
1101 chainIndex--;
1102 }
Jeff Brown5912f952013-07-01 19:10:31 -07001103 return status;
1104 }
1105 }
1106
1107 // Send finished signal for the last message in the batch.
1108 return sendUnchainedFinishedSignal(seq, handled);
1109}
1110
1111status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
1112 InputMessage msg;
Siarhei Vishniakou52402772019-10-22 09:32:30 -07001113 msg.header.type = InputMessage::Type::FINISHED;
Jeff Brown5912f952013-07-01 19:10:31 -07001114 msg.body.finished.seq = seq;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -08001115 msg.body.finished.handled = handled ? 1 : 0;
Jeff Brown5912f952013-07-01 19:10:31 -07001116 return mChannel->sendMessage(&msg);
1117}
1118
1119bool InputConsumer::hasDeferredEvent() const {
1120 return mMsgDeferred;
1121}
1122
1123bool InputConsumer::hasPendingBatch() const {
1124 return !mBatches.isEmpty();
1125}
1126
1127ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
1128 for (size_t i = 0; i < mBatches.size(); i++) {
1129 const Batch& batch = mBatches.itemAt(i);
1130 const InputMessage& head = batch.samples.itemAt(0);
1131 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
1132 return i;
1133 }
1134 }
1135 return -1;
1136}
1137
1138ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
1139 for (size_t i = 0; i < mTouchStates.size(); i++) {
1140 const TouchState& touchState = mTouchStates.itemAt(i);
1141 if (touchState.deviceId == deviceId && touchState.source == source) {
1142 return i;
1143 }
1144 }
1145 return -1;
1146}
1147
1148void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
Garfield Tanfbe732e2020-01-24 11:26:14 -08001149 event->initialize(INPUT_FLINGER_SEQUENCE_NUM, msg->body.key.deviceId, msg->body.key.source,
1150 msg->body.key.displayId, msg->body.key.hmac, msg->body.key.action,
1151 msg->body.key.flags, msg->body.key.keyCode, msg->body.key.scanCode,
1152 msg->body.key.metaState, msg->body.key.repeatCount, msg->body.key.downTime,
1153 msg->body.key.eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -07001154}
1155
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001156void InputConsumer::initializeFocusEvent(FocusEvent* event, const InputMessage* msg) {
Garfield Tanfbe732e2020-01-24 11:26:14 -08001157 event->initialize(INPUT_FLINGER_SEQUENCE_NUM, msg->body.focus.hasFocus == 1,
1158 msg->body.focus.inTouchMode == 1);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -08001159}
1160
Jeff Brown5912f952013-07-01 19:10:31 -07001161void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001162 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001163 PointerProperties pointerProperties[pointerCount];
1164 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001165 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001166 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
1167 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1168 }
1169
Garfield Tanfbe732e2020-01-24 11:26:14 -08001170 event->initialize(INPUT_FLINGER_SEQUENCE_NUM, msg->body.motion.deviceId,
1171 msg->body.motion.source, msg->body.motion.displayId, msg->body.motion.hmac,
1172 msg->body.motion.action, msg->body.motion.actionButton,
1173 msg->body.motion.flags, msg->body.motion.edgeFlags,
1174 msg->body.motion.metaState, msg->body.motion.buttonState,
1175 msg->body.motion.classification, msg->body.motion.xScale,
1176 msg->body.motion.yScale, msg->body.motion.xOffset, msg->body.motion.yOffset,
1177 msg->body.motion.xPrecision, msg->body.motion.yPrecision,
1178 msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
1179 msg->body.motion.downTime, msg->body.motion.eventTime, pointerCount,
1180 pointerProperties, pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -07001181}
1182
1183void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001184 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001185 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001186 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001187 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1188 }
1189
1190 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1191 event->addSample(msg->body.motion.eventTime, pointerCoords);
1192}
1193
1194bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
1195 const InputMessage& head = batch.samples.itemAt(0);
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001196 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001197 if (head.body.motion.pointerCount != pointerCount
1198 || head.body.motion.action != msg->body.motion.action) {
1199 return false;
1200 }
1201 for (size_t i = 0; i < pointerCount; i++) {
1202 if (head.body.motion.pointers[i].properties
1203 != msg->body.motion.pointers[i].properties) {
1204 return false;
1205 }
1206 }
1207 return true;
1208}
1209
1210ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1211 size_t numSamples = batch.samples.size();
1212 size_t index = 0;
1213 while (index < numSamples
1214 && batch.samples.itemAt(index).body.motion.eventTime <= time) {
1215 index += 1;
1216 }
1217 return ssize_t(index) - 1;
1218}
1219
1220} // namespace android