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