blob: de06292761c260408ebffcba3c0e3b62f527d587 [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
14#define DEBUG_CHANNEL_LIFECYCLE 0
15
16// Log debug messages about transport actions
17#define DEBUG_TRANSPORT_ACTIONS 0
18
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
Jeff Brown5912f952013-07-01 19:10:31 -070030#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070031#include <log/log.h>
32
Jeff Brown5912f952013-07-01 19:10:31 -070033#include <input/InputTransport.h>
34
Jeff Brown5912f952013-07-01 19:10:31 -070035namespace android {
36
37// Socket buffer size. The default is typically about 128KB, which is much larger than
38// we really need. So we make it smaller. It just needs to be big enough to hold
39// a few dozen large multi-finger motion events in the case where an application gets
40// behind processing touches.
41static const size_t SOCKET_BUFFER_SIZE = 32 * 1024;
42
43// Nanoseconds per milliseconds.
44static const nsecs_t NANOS_PER_MS = 1000000;
45
46// Latency added during resampling. A few milliseconds doesn't hurt much but
47// reduces the impact of mispredicted touch positions.
48static const nsecs_t RESAMPLE_LATENCY = 5 * NANOS_PER_MS;
49
50// Minimum time difference between consecutive samples before attempting to resample.
51static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS;
52
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -070053// Maximum time difference between consecutive samples before attempting to resample
54// by extrapolation.
55static const nsecs_t RESAMPLE_MAX_DELTA = 20 * NANOS_PER_MS;
56
Jeff Brown5912f952013-07-01 19:10:31 -070057// Maximum time to predict forward from the last known state, to avoid predicting too
58// far into the future. This time is further bounded by 50% of the last time delta.
59static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS;
60
61template<typename T>
62inline static T min(const T& a, const T& b) {
63 return a < b ? a : b;
64}
65
66inline static float lerp(float a, float b, float alpha) {
67 return a + alpha * (b - a);
68}
69
70// --- InputMessage ---
71
72bool InputMessage::isValid(size_t actualSize) const {
73 if (size() == actualSize) {
74 switch (header.type) {
75 case TYPE_KEY:
76 return true;
77 case TYPE_MOTION:
78 return body.motion.pointerCount > 0
79 && body.motion.pointerCount <= MAX_POINTERS;
80 case TYPE_FINISHED:
81 return true;
82 }
83 }
84 return false;
85}
86
87size_t InputMessage::size() const {
88 switch (header.type) {
89 case TYPE_KEY:
90 return sizeof(Header) + body.key.size();
91 case TYPE_MOTION:
92 return sizeof(Header) + body.motion.size();
93 case TYPE_FINISHED:
94 return sizeof(Header) + body.finished.size();
95 }
96 return sizeof(Header);
97}
98
Siarhei Vishniakoucb2f0ce2018-11-16 22:18:53 -080099/**
100 * There could be non-zero bytes in-between InputMessage fields. Force-initialize the entire
101 * memory to zero, then only copy the valid bytes on a per-field basis.
102 */
103void InputMessage::getSanitizedCopy(InputMessage* msg) const {
104 memset(msg, 0, sizeof(*msg));
105
106 // Write the header
107 msg->header.type = header.type;
108
109 // Write the body
110 switch(header.type) {
111 case InputMessage::TYPE_KEY: {
112 // uint32_t seq
113 msg->body.key.seq = body.key.seq;
114 // nsecs_t eventTime
115 msg->body.key.eventTime = body.key.eventTime;
116 // int32_t deviceId
117 msg->body.key.deviceId = body.key.deviceId;
118 // int32_t source
119 msg->body.key.source = body.key.source;
120 // int32_t action
121 msg->body.key.action = body.key.action;
122 // int32_t flags
123 msg->body.key.flags = body.key.flags;
124 // int32_t keyCode
125 msg->body.key.keyCode = body.key.keyCode;
126 // int32_t scanCode
127 msg->body.key.scanCode = body.key.scanCode;
128 // int32_t metaState
129 msg->body.key.metaState = body.key.metaState;
130 // int32_t repeatCount
131 msg->body.key.repeatCount = body.key.repeatCount;
132 // nsecs_t downTime
133 msg->body.key.downTime = body.key.downTime;
134 break;
135 }
136 case InputMessage::TYPE_MOTION: {
137 // uint32_t seq
138 msg->body.motion.seq = body.motion.seq;
139 // nsecs_t eventTime
140 msg->body.motion.eventTime = body.motion.eventTime;
141 // int32_t deviceId
142 msg->body.motion.deviceId = body.motion.deviceId;
143 // int32_t source
144 msg->body.motion.source = body.motion.source;
145 // int32_t action
146 msg->body.motion.action = body.motion.action;
147 // int32_t actionButton
148 msg->body.motion.actionButton = body.motion.actionButton;
149 // int32_t flags
150 msg->body.motion.flags = body.motion.flags;
151 // int32_t metaState
152 msg->body.motion.metaState = body.motion.metaState;
153 // int32_t buttonState
154 msg->body.motion.buttonState = body.motion.buttonState;
155 // int32_t edgeFlags
156 msg->body.motion.edgeFlags = body.motion.edgeFlags;
157 // nsecs_t downTime
158 msg->body.motion.downTime = body.motion.downTime;
159 // float xOffset
160 msg->body.motion.xOffset = body.motion.xOffset;
161 // float yOffset
162 msg->body.motion.yOffset = body.motion.yOffset;
163 // float xPrecision
164 msg->body.motion.xPrecision = body.motion.xPrecision;
165 // float yPrecision
166 msg->body.motion.yPrecision = body.motion.yPrecision;
167 // uint32_t pointerCount
168 msg->body.motion.pointerCount = body.motion.pointerCount;
169 //struct Pointer pointers[MAX_POINTERS]
170 for (size_t i = 0; i < body.motion.pointerCount; i++) {
171 // PointerProperties properties
172 msg->body.motion.pointers[i].properties.id = body.motion.pointers[i].properties.id;
173 msg->body.motion.pointers[i].properties.toolType =
174 body.motion.pointers[i].properties.toolType,
175 // PointerCoords coords
176 msg->body.motion.pointers[i].coords.bits = body.motion.pointers[i].coords.bits;
177 const uint32_t count = BitSet64::count(body.motion.pointers[i].coords.bits);
178 memcpy(&msg->body.motion.pointers[i].coords.values[0],
179 &body.motion.pointers[i].coords.values[0],
180 count * (sizeof(body.motion.pointers[i].coords.values[0])));
181 }
182 break;
183 }
184 case InputMessage::TYPE_FINISHED: {
185 msg->body.finished.seq = body.finished.seq;
186 msg->body.finished.handled = body.finished.handled;
187 break;
188 }
189 default: {
190 LOG_FATAL("Unexpected message type %i", header.type);
191 break;
192 }
193 }
194}
Jeff Brown5912f952013-07-01 19:10:31 -0700195
196// --- InputChannel ---
197
198InputChannel::InputChannel(const String8& name, int fd) :
199 mName(name), mFd(fd) {
200#if DEBUG_CHANNEL_LIFECYCLE
201 ALOGD("Input channel constructed: name='%s', fd=%d",
202 mName.string(), fd);
203#endif
204
205 int result = fcntl(mFd, F_SETFL, O_NONBLOCK);
206 LOG_ALWAYS_FATAL_IF(result != 0, "channel '%s' ~ Could not make socket "
207 "non-blocking. errno=%d", mName.string(), errno);
208}
209
210InputChannel::~InputChannel() {
211#if DEBUG_CHANNEL_LIFECYCLE
212 ALOGD("Input channel destroyed: name='%s', fd=%d",
213 mName.string(), mFd);
214#endif
215
216 ::close(mFd);
217}
218
219status_t InputChannel::openInputChannelPair(const String8& name,
220 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) {
221 int sockets[2];
222 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
223 status_t result = -errno;
224 ALOGE("channel '%s' ~ Could not create socket pair. errno=%d",
225 name.string(), errno);
226 outServerChannel.clear();
227 outClientChannel.clear();
228 return result;
229 }
230
231 int bufferSize = SOCKET_BUFFER_SIZE;
232 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
233 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
234 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
235 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
236
237 String8 serverChannelName = name;
238 serverChannelName.append(" (server)");
239 outServerChannel = new InputChannel(serverChannelName, sockets[0]);
240
241 String8 clientChannelName = name;
242 clientChannelName.append(" (client)");
243 outClientChannel = new InputChannel(clientChannelName, sockets[1]);
244 return OK;
245}
246
247status_t InputChannel::sendMessage(const InputMessage* msg) {
Siarhei Vishniakoucb2f0ce2018-11-16 22:18:53 -0800248 const size_t msgLength = msg->size();
249 InputMessage cleanMsg;
250 msg->getSanitizedCopy(&cleanMsg);
Jeff Brown5912f952013-07-01 19:10:31 -0700251 ssize_t nWrite;
252 do {
Siarhei Vishniakoucb2f0ce2018-11-16 22:18:53 -0800253 nWrite = ::send(mFd, &cleanMsg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown5912f952013-07-01 19:10:31 -0700254 } while (nWrite == -1 && errno == EINTR);
255
256 if (nWrite < 0) {
257 int error = errno;
258#if DEBUG_CHANNEL_MESSAGES
259 ALOGD("channel '%s' ~ error sending message of type %d, errno=%d", mName.string(),
260 msg->header.type, error);
261#endif
262 if (error == EAGAIN || error == EWOULDBLOCK) {
263 return WOULD_BLOCK;
264 }
265 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
266 return DEAD_OBJECT;
267 }
268 return -error;
269 }
270
271 if (size_t(nWrite) != msgLength) {
272#if DEBUG_CHANNEL_MESSAGES
273 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
274 mName.string(), msg->header.type);
275#endif
276 return DEAD_OBJECT;
277 }
278
279#if DEBUG_CHANNEL_MESSAGES
280 ALOGD("channel '%s' ~ sent message of type %d", mName.string(), msg->header.type);
281#endif
282 return OK;
283}
284
285status_t InputChannel::receiveMessage(InputMessage* msg) {
286 ssize_t nRead;
287 do {
288 nRead = ::recv(mFd, msg, sizeof(InputMessage), MSG_DONTWAIT);
289 } while (nRead == -1 && errno == EINTR);
290
291 if (nRead < 0) {
292 int error = errno;
293#if DEBUG_CHANNEL_MESSAGES
294 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.string(), errno);
295#endif
296 if (error == EAGAIN || error == EWOULDBLOCK) {
297 return WOULD_BLOCK;
298 }
299 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
300 return DEAD_OBJECT;
301 }
302 return -error;
303 }
304
305 if (nRead == 0) { // check for EOF
306#if DEBUG_CHANNEL_MESSAGES
307 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.string());
308#endif
309 return DEAD_OBJECT;
310 }
311
312 if (!msg->isValid(nRead)) {
313#if DEBUG_CHANNEL_MESSAGES
314 ALOGD("channel '%s' ~ received invalid message", mName.string());
315#endif
316 return BAD_VALUE;
317 }
318
319#if DEBUG_CHANNEL_MESSAGES
320 ALOGD("channel '%s' ~ received message of type %d", mName.string(), msg->header.type);
321#endif
322 return OK;
323}
324
325sp<InputChannel> InputChannel::dup() const {
326 int fd = ::dup(getFd());
327 return fd >= 0 ? new InputChannel(getName(), fd) : NULL;
328}
329
330
331// --- InputPublisher ---
332
333InputPublisher::InputPublisher(const sp<InputChannel>& channel) :
334 mChannel(channel) {
335}
336
337InputPublisher::~InputPublisher() {
338}
339
340status_t InputPublisher::publishKeyEvent(
341 uint32_t seq,
342 int32_t deviceId,
343 int32_t source,
344 int32_t action,
345 int32_t flags,
346 int32_t keyCode,
347 int32_t scanCode,
348 int32_t metaState,
349 int32_t repeatCount,
350 nsecs_t downTime,
351 nsecs_t eventTime) {
352#if DEBUG_TRANSPORT_ACTIONS
353 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
354 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
355 "downTime=%lld, eventTime=%lld",
356 mChannel->getName().string(), seq,
357 deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount,
358 downTime, eventTime);
359#endif
360
361 if (!seq) {
362 ALOGE("Attempted to publish a key event with sequence number 0.");
363 return BAD_VALUE;
364 }
365
366 InputMessage msg;
367 msg.header.type = InputMessage::TYPE_KEY;
368 msg.body.key.seq = seq;
369 msg.body.key.deviceId = deviceId;
370 msg.body.key.source = source;
371 msg.body.key.action = action;
372 msg.body.key.flags = flags;
373 msg.body.key.keyCode = keyCode;
374 msg.body.key.scanCode = scanCode;
375 msg.body.key.metaState = metaState;
376 msg.body.key.repeatCount = repeatCount;
377 msg.body.key.downTime = downTime;
378 msg.body.key.eventTime = eventTime;
379 return mChannel->sendMessage(&msg);
380}
381
382status_t InputPublisher::publishMotionEvent(
383 uint32_t seq,
384 int32_t deviceId,
385 int32_t source,
Tarandeep Singh58641502017-07-31 10:51:54 -0700386 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700387 int32_t action,
Michael Wright7b159c92015-05-14 14:48:03 +0100388 int32_t actionButton,
Jeff Brown5912f952013-07-01 19:10:31 -0700389 int32_t flags,
390 int32_t edgeFlags,
391 int32_t metaState,
392 int32_t buttonState,
393 float xOffset,
394 float yOffset,
395 float xPrecision,
396 float yPrecision,
397 nsecs_t downTime,
398 nsecs_t eventTime,
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100399 uint32_t pointerCount,
Jeff Brown5912f952013-07-01 19:10:31 -0700400 const PointerProperties* pointerProperties,
401 const PointerCoords* pointerCoords) {
402#if DEBUG_TRANSPORT_ACTIONS
403 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
Michael Wright7b159c92015-05-14 14:48:03 +0100404 "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
405 "metaState=0x%x, buttonState=0x%x, xOffset=%f, yOffset=%f, "
Jeff Brown5912f952013-07-01 19:10:31 -0700406 "xPrecision=%f, yPrecision=%f, downTime=%lld, eventTime=%lld, "
Michael Wright63ff3a82014-06-10 13:03:17 -0700407 "pointerCount=%" PRIu32,
Jeff Brown5912f952013-07-01 19:10:31 -0700408 mChannel->getName().string(), seq,
Michael Wright7b159c92015-05-14 14:48:03 +0100409 deviceId, source, action, actionButton, flags, edgeFlags, metaState, buttonState,
Jeff Brown5912f952013-07-01 19:10:31 -0700410 xOffset, yOffset, xPrecision, yPrecision, downTime, eventTime, pointerCount);
411#endif
412
413 if (!seq) {
414 ALOGE("Attempted to publish a motion event with sequence number 0.");
415 return BAD_VALUE;
416 }
417
418 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700419 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Jeff Brown5912f952013-07-01 19:10:31 -0700420 mChannel->getName().string(), pointerCount);
421 return BAD_VALUE;
422 }
423
424 InputMessage msg;
425 msg.header.type = InputMessage::TYPE_MOTION;
426 msg.body.motion.seq = seq;
427 msg.body.motion.deviceId = deviceId;
428 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700429 msg.body.motion.displayId = displayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700430 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100431 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700432 msg.body.motion.flags = flags;
433 msg.body.motion.edgeFlags = edgeFlags;
434 msg.body.motion.metaState = metaState;
435 msg.body.motion.buttonState = buttonState;
436 msg.body.motion.xOffset = xOffset;
437 msg.body.motion.yOffset = yOffset;
438 msg.body.motion.xPrecision = xPrecision;
439 msg.body.motion.yPrecision = yPrecision;
440 msg.body.motion.downTime = downTime;
441 msg.body.motion.eventTime = eventTime;
442 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100443 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700444 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
445 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
446 }
447 return mChannel->sendMessage(&msg);
448}
449
450status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandled) {
451#if DEBUG_TRANSPORT_ACTIONS
452 ALOGD("channel '%s' publisher ~ receiveFinishedSignal",
453 mChannel->getName().string());
454#endif
455
456 InputMessage msg;
457 status_t result = mChannel->receiveMessage(&msg);
458 if (result) {
459 *outSeq = 0;
460 *outHandled = false;
461 return result;
462 }
463 if (msg.header.type != InputMessage::TYPE_FINISHED) {
464 ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer",
465 mChannel->getName().string(), msg.header.type);
466 return UNKNOWN_ERROR;
467 }
468 *outSeq = msg.body.finished.seq;
469 *outHandled = msg.body.finished.handled;
470 return OK;
471}
472
473// --- InputConsumer ---
474
475InputConsumer::InputConsumer(const sp<InputChannel>& channel) :
476 mResampleTouch(isTouchResamplingEnabled()),
477 mChannel(channel), mMsgDeferred(false) {
478}
479
480InputConsumer::~InputConsumer() {
481}
482
483bool InputConsumer::isTouchResamplingEnabled() {
484 char value[PROPERTY_VALUE_MAX];
Michael Wrightad526f82013-10-10 18:54:12 -0700485 int length = property_get("ro.input.noresample", value, NULL);
Jeff Brown5912f952013-07-01 19:10:31 -0700486 if (length > 0) {
Michael Wrightad526f82013-10-10 18:54:12 -0700487 if (!strcmp("1", value)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700488 return false;
489 }
Michael Wrightad526f82013-10-10 18:54:12 -0700490 if (strcmp("0", value)) {
491 ALOGD("Unrecognized property value for 'ro.input.noresample'. "
Jeff Brown5912f952013-07-01 19:10:31 -0700492 "Use '1' or '0'.");
493 }
494 }
495 return true;
496}
497
498status_t InputConsumer::consume(InputEventFactoryInterface* factory,
Tarandeep Singh58641502017-07-31 10:51:54 -0700499 bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent,
500 int32_t* displayId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700501#if DEBUG_TRANSPORT_ACTIONS
502 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%lld",
503 mChannel->getName().string(), consumeBatches ? "true" : "false", frameTime);
504#endif
505
506 *outSeq = 0;
507 *outEvent = NULL;
Tarandeep Singh58641502017-07-31 10:51:54 -0700508 *displayId = -1; // Invalid display.
Jeff Brown5912f952013-07-01 19:10:31 -0700509
510 // Fetch the next input message.
511 // Loop until an event can be returned or no additional events are received.
512 while (!*outEvent) {
513 if (mMsgDeferred) {
514 // mMsg contains a valid input message from the previous call to consume
515 // that has not yet been processed.
516 mMsgDeferred = false;
517 } else {
518 // Receive a fresh message.
519 status_t result = mChannel->receiveMessage(&mMsg);
520 if (result) {
521 // Consume the next batched event unless batches are being held for later.
522 if (consumeBatches || result != WOULD_BLOCK) {
Tarandeep Singh58641502017-07-31 10:51:54 -0700523 result = consumeBatch(factory, frameTime, outSeq, outEvent, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700524 if (*outEvent) {
525#if DEBUG_TRANSPORT_ACTIONS
526 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
527 mChannel->getName().string(), *outSeq);
528#endif
529 break;
530 }
531 }
532 return result;
533 }
534 }
535
536 switch (mMsg.header.type) {
537 case InputMessage::TYPE_KEY: {
538 KeyEvent* keyEvent = factory->createKeyEvent();
539 if (!keyEvent) return NO_MEMORY;
540
541 initializeKeyEvent(keyEvent, &mMsg);
542 *outSeq = mMsg.body.key.seq;
543 *outEvent = keyEvent;
544#if DEBUG_TRANSPORT_ACTIONS
545 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
546 mChannel->getName().string(), *outSeq);
547#endif
548 break;
549 }
550
551 case AINPUT_EVENT_TYPE_MOTION: {
552 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
553 if (batchIndex >= 0) {
554 Batch& batch = mBatches.editItemAt(batchIndex);
555 if (canAddSample(batch, &mMsg)) {
556 batch.samples.push(mMsg);
557#if DEBUG_TRANSPORT_ACTIONS
558 ALOGD("channel '%s' consumer ~ appended to batch event",
559 mChannel->getName().string());
560#endif
561 break;
562 } else {
563 // We cannot append to the batch in progress, so we need to consume
564 // the previous batch right now and defer the new message until later.
565 mMsgDeferred = true;
566 status_t result = consumeSamples(factory,
Tarandeep Singh58641502017-07-31 10:51:54 -0700567 batch, batch.samples.size(), outSeq, outEvent, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700568 mBatches.removeAt(batchIndex);
569 if (result) {
570 return result;
571 }
572#if DEBUG_TRANSPORT_ACTIONS
573 ALOGD("channel '%s' consumer ~ consumed batch event and "
574 "deferred current event, seq=%u",
575 mChannel->getName().string(), *outSeq);
576#endif
577 break;
578 }
579 }
580
581 // Start a new batch if needed.
582 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE
583 || mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
584 mBatches.push();
585 Batch& batch = mBatches.editTop();
586 batch.samples.push(mMsg);
587#if DEBUG_TRANSPORT_ACTIONS
588 ALOGD("channel '%s' consumer ~ started batch event",
589 mChannel->getName().string());
590#endif
591 break;
592 }
593
594 MotionEvent* motionEvent = factory->createMotionEvent();
595 if (! motionEvent) return NO_MEMORY;
596
597 updateTouchState(&mMsg);
598 initializeMotionEvent(motionEvent, &mMsg);
599 *outSeq = mMsg.body.motion.seq;
600 *outEvent = motionEvent;
Tarandeep Singh58641502017-07-31 10:51:54 -0700601 *displayId = mMsg.body.motion.displayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700602#if DEBUG_TRANSPORT_ACTIONS
603 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
604 mChannel->getName().string(), *outSeq);
605#endif
606 break;
607 }
608
609 default:
610 ALOGE("channel '%s' consumer ~ Received unexpected message of type %d",
611 mChannel->getName().string(), mMsg.header.type);
612 return UNKNOWN_ERROR;
613 }
614 }
615 return OK;
616}
617
618status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Tarandeep Singh58641502017-07-31 10:51:54 -0700619 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent, int32_t* displayId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700620 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700621 for (size_t i = mBatches.size(); i > 0; ) {
622 i--;
Jeff Brown5912f952013-07-01 19:10:31 -0700623 Batch& batch = mBatches.editItemAt(i);
Michael Wright32232172013-10-21 12:05:22 -0700624 if (frameTime < 0) {
Jeff Brown5912f952013-07-01 19:10:31 -0700625 result = consumeSamples(factory, batch, batch.samples.size(),
Tarandeep Singh58641502017-07-31 10:51:54 -0700626 outSeq, outEvent, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700627 mBatches.removeAt(i);
628 return result;
629 }
630
Michael Wright32232172013-10-21 12:05:22 -0700631 nsecs_t sampleTime = frameTime;
632 if (mResampleTouch) {
633 sampleTime -= RESAMPLE_LATENCY;
634 }
Jeff Brown5912f952013-07-01 19:10:31 -0700635 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
636 if (split < 0) {
637 continue;
638 }
639
Tarandeep Singh58641502017-07-31 10:51:54 -0700640 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700641 const InputMessage* next;
642 if (batch.samples.isEmpty()) {
643 mBatches.removeAt(i);
644 next = NULL;
645 } else {
646 next = &batch.samples.itemAt(0);
647 }
Michael Wright32232172013-10-21 12:05:22 -0700648 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700649 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
650 }
651 return result;
652 }
653
654 return WOULD_BLOCK;
655}
656
657status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Tarandeep Singh58641502017-07-31 10:51:54 -0700658 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent, int32_t* displayId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700659 MotionEvent* motionEvent = factory->createMotionEvent();
660 if (! motionEvent) return NO_MEMORY;
661
662 uint32_t chain = 0;
663 for (size_t i = 0; i < count; i++) {
664 InputMessage& msg = batch.samples.editItemAt(i);
665 updateTouchState(&msg);
666 if (i) {
667 SeqChain seqChain;
668 seqChain.seq = msg.body.motion.seq;
669 seqChain.chain = chain;
670 mSeqChains.push(seqChain);
671 addSample(motionEvent, &msg);
672 } else {
Tarandeep Singh58641502017-07-31 10:51:54 -0700673 *displayId = msg.body.motion.displayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700674 initializeMotionEvent(motionEvent, &msg);
675 }
676 chain = msg.body.motion.seq;
677 }
678 batch.samples.removeItemsAt(0, count);
679
680 *outSeq = chain;
681 *outEvent = motionEvent;
682 return OK;
683}
684
685void InputConsumer::updateTouchState(InputMessage* msg) {
686 if (!mResampleTouch ||
687 !(msg->body.motion.source & AINPUT_SOURCE_CLASS_POINTER)) {
688 return;
689 }
690
691 int32_t deviceId = msg->body.motion.deviceId;
692 int32_t source = msg->body.motion.source;
693 nsecs_t eventTime = msg->body.motion.eventTime;
694
695 // Update the touch state history to incorporate the new input message.
696 // If the message is in the past relative to the most recently produced resampled
697 // touch, then use the resampled time and coordinates instead.
698 switch (msg->body.motion.action & AMOTION_EVENT_ACTION_MASK) {
699 case AMOTION_EVENT_ACTION_DOWN: {
700 ssize_t index = findTouchState(deviceId, source);
701 if (index < 0) {
702 mTouchStates.push();
703 index = mTouchStates.size() - 1;
704 }
705 TouchState& touchState = mTouchStates.editItemAt(index);
706 touchState.initialize(deviceId, source);
707 touchState.addHistory(msg);
708 break;
709 }
710
711 case AMOTION_EVENT_ACTION_MOVE: {
712 ssize_t index = findTouchState(deviceId, source);
713 if (index >= 0) {
714 TouchState& touchState = mTouchStates.editItemAt(index);
715 touchState.addHistory(msg);
716 if (eventTime < touchState.lastResample.eventTime) {
717 rewriteMessage(touchState, msg);
718 } else {
719 touchState.lastResample.idBits.clear();
720 }
721 }
722 break;
723 }
724
725 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
726 ssize_t index = findTouchState(deviceId, source);
727 if (index >= 0) {
728 TouchState& touchState = mTouchStates.editItemAt(index);
729 touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId());
730 rewriteMessage(touchState, msg);
731 }
732 break;
733 }
734
735 case AMOTION_EVENT_ACTION_POINTER_UP: {
736 ssize_t index = findTouchState(deviceId, source);
737 if (index >= 0) {
738 TouchState& touchState = mTouchStates.editItemAt(index);
739 rewriteMessage(touchState, msg);
740 touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId());
741 }
742 break;
743 }
744
745 case AMOTION_EVENT_ACTION_SCROLL: {
746 ssize_t index = findTouchState(deviceId, source);
747 if (index >= 0) {
748 const TouchState& touchState = mTouchStates.itemAt(index);
749 rewriteMessage(touchState, msg);
750 }
751 break;
752 }
753
754 case AMOTION_EVENT_ACTION_UP:
755 case AMOTION_EVENT_ACTION_CANCEL: {
756 ssize_t index = findTouchState(deviceId, source);
757 if (index >= 0) {
758 const TouchState& touchState = mTouchStates.itemAt(index);
759 rewriteMessage(touchState, msg);
760 mTouchStates.removeAt(index);
761 }
762 break;
763 }
764 }
765}
766
767void InputConsumer::rewriteMessage(const TouchState& state, InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100768 for (uint32_t i = 0; i < msg->body.motion.pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700769 uint32_t id = msg->body.motion.pointers[i].properties.id;
770 if (state.lastResample.idBits.hasBit(id)) {
771 PointerCoords& msgCoords = msg->body.motion.pointers[i].coords;
772 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
773#if DEBUG_RESAMPLING
774 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
775 resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
776 resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_Y),
777 msgCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
778 msgCoords.getAxisValue(AMOTION_EVENT_AXIS_Y));
779#endif
780 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
781 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
782 }
783 }
784}
785
786void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
787 const InputMessage* next) {
788 if (!mResampleTouch
789 || !(event->getSource() & AINPUT_SOURCE_CLASS_POINTER)
790 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
791 return;
792 }
793
794 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
795 if (index < 0) {
796#if DEBUG_RESAMPLING
797 ALOGD("Not resampled, no touch state for device.");
798#endif
799 return;
800 }
801
802 TouchState& touchState = mTouchStates.editItemAt(index);
803 if (touchState.historySize < 1) {
804#if DEBUG_RESAMPLING
805 ALOGD("Not resampled, no history for device.");
806#endif
807 return;
808 }
809
810 // Ensure that the current sample has all of the pointers that need to be reported.
811 const History* current = touchState.getHistory(0);
812 size_t pointerCount = event->getPointerCount();
813 for (size_t i = 0; i < pointerCount; i++) {
814 uint32_t id = event->getPointerId(i);
815 if (!current->idBits.hasBit(id)) {
816#if DEBUG_RESAMPLING
817 ALOGD("Not resampled, missing id %d", id);
818#endif
819 return;
820 }
821 }
822
823 // Find the data to use for resampling.
824 const History* other;
825 History future;
826 float alpha;
827 if (next) {
828 // Interpolate between current sample and future sample.
829 // So current->eventTime <= sampleTime <= future.eventTime.
830 future.initializeFrom(next);
831 other = &future;
832 nsecs_t delta = future.eventTime - current->eventTime;
833 if (delta < RESAMPLE_MIN_DELTA) {
834#if DEBUG_RESAMPLING
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -0700835 ALOGD("Not resampled, delta time is too small: %lld ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -0700836#endif
837 return;
838 }
839 alpha = float(sampleTime - current->eventTime) / delta;
840 } else if (touchState.historySize >= 2) {
841 // Extrapolate future sample using current sample and past sample.
842 // So other->eventTime <= current->eventTime <= sampleTime.
843 other = touchState.getHistory(1);
844 nsecs_t delta = current->eventTime - other->eventTime;
845 if (delta < RESAMPLE_MIN_DELTA) {
846#if DEBUG_RESAMPLING
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -0700847 ALOGD("Not resampled, delta time is too small: %lld ns.", delta);
848#endif
849 return;
850 } else if (delta > RESAMPLE_MAX_DELTA) {
851#if DEBUG_RESAMPLING
852 ALOGD("Not resampled, delta time is too large: %lld ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -0700853#endif
854 return;
855 }
856 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
857 if (sampleTime > maxPredict) {
858#if DEBUG_RESAMPLING
859 ALOGD("Sample time is too far in the future, adjusting prediction "
860 "from %lld to %lld ns.",
861 sampleTime - current->eventTime, maxPredict - current->eventTime);
862#endif
863 sampleTime = maxPredict;
864 }
865 alpha = float(current->eventTime - sampleTime) / delta;
866 } else {
867#if DEBUG_RESAMPLING
868 ALOGD("Not resampled, insufficient data.");
869#endif
870 return;
871 }
872
873 // Resample touch coordinates.
874 touchState.lastResample.eventTime = sampleTime;
875 touchState.lastResample.idBits.clear();
876 for (size_t i = 0; i < pointerCount; i++) {
877 uint32_t id = event->getPointerId(i);
878 touchState.lastResample.idToIndex[id] = i;
879 touchState.lastResample.idBits.markBit(id);
880 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
881 const PointerCoords& currentCoords = current->getPointerById(id);
882 if (other->idBits.hasBit(id)
883 && shouldResampleTool(event->getToolType(i))) {
884 const PointerCoords& otherCoords = other->getPointerById(id);
885 resampledCoords.copyFrom(currentCoords);
886 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
887 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
888 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
889 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
890#if DEBUG_RESAMPLING
891 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
892 "other (%0.3f, %0.3f), alpha %0.3f",
893 id, resampledCoords.getX(), resampledCoords.getY(),
894 currentCoords.getX(), currentCoords.getY(),
895 otherCoords.getX(), otherCoords.getY(),
896 alpha);
897#endif
898 } else {
899 resampledCoords.copyFrom(currentCoords);
900#if DEBUG_RESAMPLING
901 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
902 id, resampledCoords.getX(), resampledCoords.getY(),
903 currentCoords.getX(), currentCoords.getY());
904#endif
905 }
906 }
907
908 event->addSample(sampleTime, touchState.lastResample.pointers);
909}
910
911bool InputConsumer::shouldResampleTool(int32_t toolType) {
912 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
913 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
914}
915
916status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
917#if DEBUG_TRANSPORT_ACTIONS
918 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
919 mChannel->getName().string(), seq, handled ? "true" : "false");
920#endif
921
922 if (!seq) {
923 ALOGE("Attempted to send a finished signal with sequence number 0.");
924 return BAD_VALUE;
925 }
926
927 // Send finished signals for the batch sequence chain first.
928 size_t seqChainCount = mSeqChains.size();
929 if (seqChainCount) {
930 uint32_t currentSeq = seq;
931 uint32_t chainSeqs[seqChainCount];
932 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -0700933 for (size_t i = seqChainCount; i > 0; ) {
934 i--;
Jeff Brown5912f952013-07-01 19:10:31 -0700935 const SeqChain& seqChain = mSeqChains.itemAt(i);
936 if (seqChain.seq == currentSeq) {
937 currentSeq = seqChain.chain;
938 chainSeqs[chainIndex++] = currentSeq;
939 mSeqChains.removeAt(i);
940 }
941 }
942 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -0700943 while (!status && chainIndex > 0) {
944 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -0700945 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
946 }
947 if (status) {
948 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +0800949 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -0700950 SeqChain seqChain;
951 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
952 seqChain.chain = chainSeqs[chainIndex];
953 mSeqChains.push(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +0800954 if (!chainIndex) break;
955 chainIndex--;
956 }
Jeff Brown5912f952013-07-01 19:10:31 -0700957 return status;
958 }
959 }
960
961 // Send finished signal for the last message in the batch.
962 return sendUnchainedFinishedSignal(seq, handled);
963}
964
965status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
966 InputMessage msg;
967 msg.header.type = InputMessage::TYPE_FINISHED;
968 msg.body.finished.seq = seq;
969 msg.body.finished.handled = handled;
970 return mChannel->sendMessage(&msg);
971}
972
973bool InputConsumer::hasDeferredEvent() const {
974 return mMsgDeferred;
975}
976
977bool InputConsumer::hasPendingBatch() const {
978 return !mBatches.isEmpty();
979}
980
981ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
982 for (size_t i = 0; i < mBatches.size(); i++) {
983 const Batch& batch = mBatches.itemAt(i);
984 const InputMessage& head = batch.samples.itemAt(0);
985 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
986 return i;
987 }
988 }
989 return -1;
990}
991
992ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
993 for (size_t i = 0; i < mTouchStates.size(); i++) {
994 const TouchState& touchState = mTouchStates.itemAt(i);
995 if (touchState.deviceId == deviceId && touchState.source == source) {
996 return i;
997 }
998 }
999 return -1;
1000}
1001
1002void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
1003 event->initialize(
1004 msg->body.key.deviceId,
1005 msg->body.key.source,
1006 msg->body.key.action,
1007 msg->body.key.flags,
1008 msg->body.key.keyCode,
1009 msg->body.key.scanCode,
1010 msg->body.key.metaState,
1011 msg->body.key.repeatCount,
1012 msg->body.key.downTime,
1013 msg->body.key.eventTime);
1014}
1015
1016void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001017 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001018 PointerProperties pointerProperties[pointerCount];
1019 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001020 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001021 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
1022 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1023 }
1024
1025 event->initialize(
1026 msg->body.motion.deviceId,
1027 msg->body.motion.source,
1028 msg->body.motion.action,
Michael Wright7b159c92015-05-14 14:48:03 +01001029 msg->body.motion.actionButton,
Jeff Brown5912f952013-07-01 19:10:31 -07001030 msg->body.motion.flags,
1031 msg->body.motion.edgeFlags,
1032 msg->body.motion.metaState,
1033 msg->body.motion.buttonState,
1034 msg->body.motion.xOffset,
1035 msg->body.motion.yOffset,
1036 msg->body.motion.xPrecision,
1037 msg->body.motion.yPrecision,
1038 msg->body.motion.downTime,
1039 msg->body.motion.eventTime,
1040 pointerCount,
1041 pointerProperties,
1042 pointerCoords);
1043}
1044
1045void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001046 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001047 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001048 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -07001049 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
1050 }
1051
1052 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
1053 event->addSample(msg->body.motion.eventTime, pointerCoords);
1054}
1055
1056bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
1057 const InputMessage& head = batch.samples.itemAt(0);
Narayan Kamathbc6001b2014-05-02 17:53:33 +01001058 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -07001059 if (head.body.motion.pointerCount != pointerCount
1060 || head.body.motion.action != msg->body.motion.action) {
1061 return false;
1062 }
1063 for (size_t i = 0; i < pointerCount; i++) {
1064 if (head.body.motion.pointers[i].properties
1065 != msg->body.motion.pointers[i].properties) {
1066 return false;
1067 }
1068 }
1069 return true;
1070}
1071
1072ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
1073 size_t numSamples = batch.samples.size();
1074 size_t index = 0;
1075 while (index < numSamples
1076 && batch.samples.itemAt(index).body.motion.eventTime <= time) {
1077 index += 1;
1078 }
1079 return ssize_t(index) - 1;
1080}
1081
1082} // namespace android