blob: d5c592752e9ca694f242d6af97e1afdb119c65c6 [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
99
100// --- InputChannel ---
101
102InputChannel::InputChannel(const String8& name, int fd) :
103 mName(name), mFd(fd) {
104#if DEBUG_CHANNEL_LIFECYCLE
105 ALOGD("Input channel constructed: name='%s', fd=%d",
106 mName.string(), fd);
107#endif
108
109 int result = fcntl(mFd, F_SETFL, O_NONBLOCK);
110 LOG_ALWAYS_FATAL_IF(result != 0, "channel '%s' ~ Could not make socket "
111 "non-blocking. errno=%d", mName.string(), errno);
112}
113
114InputChannel::~InputChannel() {
115#if DEBUG_CHANNEL_LIFECYCLE
116 ALOGD("Input channel destroyed: name='%s', fd=%d",
117 mName.string(), mFd);
118#endif
119
120 ::close(mFd);
121}
122
123status_t InputChannel::openInputChannelPair(const String8& name,
124 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) {
125 int sockets[2];
126 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
127 status_t result = -errno;
128 ALOGE("channel '%s' ~ Could not create socket pair. errno=%d",
129 name.string(), errno);
130 outServerChannel.clear();
131 outClientChannel.clear();
132 return result;
133 }
134
135 int bufferSize = SOCKET_BUFFER_SIZE;
136 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
137 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
138 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
139 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
140
141 String8 serverChannelName = name;
142 serverChannelName.append(" (server)");
143 outServerChannel = new InputChannel(serverChannelName, sockets[0]);
144
145 String8 clientChannelName = name;
146 clientChannelName.append(" (client)");
147 outClientChannel = new InputChannel(clientChannelName, sockets[1]);
148 return OK;
149}
150
151status_t InputChannel::sendMessage(const InputMessage* msg) {
152 size_t msgLength = msg->size();
153 ssize_t nWrite;
154 do {
155 nWrite = ::send(mFd, msg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
156 } while (nWrite == -1 && errno == EINTR);
157
158 if (nWrite < 0) {
159 int error = errno;
160#if DEBUG_CHANNEL_MESSAGES
161 ALOGD("channel '%s' ~ error sending message of type %d, errno=%d", mName.string(),
162 msg->header.type, error);
163#endif
164 if (error == EAGAIN || error == EWOULDBLOCK) {
165 return WOULD_BLOCK;
166 }
167 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED || error == ECONNRESET) {
168 return DEAD_OBJECT;
169 }
170 return -error;
171 }
172
173 if (size_t(nWrite) != msgLength) {
174#if DEBUG_CHANNEL_MESSAGES
175 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
176 mName.string(), msg->header.type);
177#endif
178 return DEAD_OBJECT;
179 }
180
181#if DEBUG_CHANNEL_MESSAGES
182 ALOGD("channel '%s' ~ sent message of type %d", mName.string(), msg->header.type);
183#endif
184 return OK;
185}
186
187status_t InputChannel::receiveMessage(InputMessage* msg) {
188 ssize_t nRead;
189 do {
190 nRead = ::recv(mFd, msg, sizeof(InputMessage), MSG_DONTWAIT);
191 } while (nRead == -1 && errno == EINTR);
192
193 if (nRead < 0) {
194 int error = errno;
195#if DEBUG_CHANNEL_MESSAGES
196 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.string(), errno);
197#endif
198 if (error == EAGAIN || error == EWOULDBLOCK) {
199 return WOULD_BLOCK;
200 }
201 if (error == EPIPE || error == ENOTCONN || error == ECONNREFUSED) {
202 return DEAD_OBJECT;
203 }
204 return -error;
205 }
206
207 if (nRead == 0) { // check for EOF
208#if DEBUG_CHANNEL_MESSAGES
209 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.string());
210#endif
211 return DEAD_OBJECT;
212 }
213
214 if (!msg->isValid(nRead)) {
215#if DEBUG_CHANNEL_MESSAGES
216 ALOGD("channel '%s' ~ received invalid message", mName.string());
217#endif
218 return BAD_VALUE;
219 }
220
221#if DEBUG_CHANNEL_MESSAGES
222 ALOGD("channel '%s' ~ received message of type %d", mName.string(), msg->header.type);
223#endif
224 return OK;
225}
226
227sp<InputChannel> InputChannel::dup() const {
228 int fd = ::dup(getFd());
229 return fd >= 0 ? new InputChannel(getName(), fd) : NULL;
230}
231
232
233// --- InputPublisher ---
234
235InputPublisher::InputPublisher(const sp<InputChannel>& channel) :
236 mChannel(channel) {
237}
238
239InputPublisher::~InputPublisher() {
240}
241
242status_t InputPublisher::publishKeyEvent(
243 uint32_t seq,
244 int32_t deviceId,
245 int32_t source,
246 int32_t action,
247 int32_t flags,
248 int32_t keyCode,
249 int32_t scanCode,
250 int32_t metaState,
251 int32_t repeatCount,
252 nsecs_t downTime,
253 nsecs_t eventTime) {
254#if DEBUG_TRANSPORT_ACTIONS
255 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
256 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
257 "downTime=%lld, eventTime=%lld",
258 mChannel->getName().string(), seq,
259 deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount,
260 downTime, eventTime);
261#endif
262
263 if (!seq) {
264 ALOGE("Attempted to publish a key event with sequence number 0.");
265 return BAD_VALUE;
266 }
267
268 InputMessage msg;
269 msg.header.type = InputMessage::TYPE_KEY;
270 msg.body.key.seq = seq;
271 msg.body.key.deviceId = deviceId;
272 msg.body.key.source = source;
273 msg.body.key.action = action;
274 msg.body.key.flags = flags;
275 msg.body.key.keyCode = keyCode;
276 msg.body.key.scanCode = scanCode;
277 msg.body.key.metaState = metaState;
278 msg.body.key.repeatCount = repeatCount;
279 msg.body.key.downTime = downTime;
280 msg.body.key.eventTime = eventTime;
281 return mChannel->sendMessage(&msg);
282}
283
284status_t InputPublisher::publishMotionEvent(
285 uint32_t seq,
286 int32_t deviceId,
287 int32_t source,
Tarandeep Singh58641502017-07-31 10:51:54 -0700288 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700289 int32_t action,
Michael Wright7b159c92015-05-14 14:48:03 +0100290 int32_t actionButton,
Jeff Brown5912f952013-07-01 19:10:31 -0700291 int32_t flags,
292 int32_t edgeFlags,
293 int32_t metaState,
294 int32_t buttonState,
295 float xOffset,
296 float yOffset,
297 float xPrecision,
298 float yPrecision,
299 nsecs_t downTime,
300 nsecs_t eventTime,
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100301 uint32_t pointerCount,
Jeff Brown5912f952013-07-01 19:10:31 -0700302 const PointerProperties* pointerProperties,
303 const PointerCoords* pointerCoords) {
304#if DEBUG_TRANSPORT_ACTIONS
305 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
Michael Wright7b159c92015-05-14 14:48:03 +0100306 "action=0x%x, actionButton=0x%08x, flags=0x%x, edgeFlags=0x%x, "
307 "metaState=0x%x, buttonState=0x%x, xOffset=%f, yOffset=%f, "
Jeff Brown5912f952013-07-01 19:10:31 -0700308 "xPrecision=%f, yPrecision=%f, downTime=%lld, eventTime=%lld, "
Michael Wright63ff3a82014-06-10 13:03:17 -0700309 "pointerCount=%" PRIu32,
Jeff Brown5912f952013-07-01 19:10:31 -0700310 mChannel->getName().string(), seq,
Michael Wright7b159c92015-05-14 14:48:03 +0100311 deviceId, source, action, actionButton, flags, edgeFlags, metaState, buttonState,
Jeff Brown5912f952013-07-01 19:10:31 -0700312 xOffset, yOffset, xPrecision, yPrecision, downTime, eventTime, pointerCount);
313#endif
314
315 if (!seq) {
316 ALOGE("Attempted to publish a motion event with sequence number 0.");
317 return BAD_VALUE;
318 }
319
320 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Michael Wright63ff3a82014-06-10 13:03:17 -0700321 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %" PRIu32 ".",
Jeff Brown5912f952013-07-01 19:10:31 -0700322 mChannel->getName().string(), pointerCount);
323 return BAD_VALUE;
324 }
325
326 InputMessage msg;
327 msg.header.type = InputMessage::TYPE_MOTION;
328 msg.body.motion.seq = seq;
329 msg.body.motion.deviceId = deviceId;
330 msg.body.motion.source = source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700331 msg.body.motion.displayId = displayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700332 msg.body.motion.action = action;
Michael Wright7b159c92015-05-14 14:48:03 +0100333 msg.body.motion.actionButton = actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700334 msg.body.motion.flags = flags;
335 msg.body.motion.edgeFlags = edgeFlags;
336 msg.body.motion.metaState = metaState;
337 msg.body.motion.buttonState = buttonState;
338 msg.body.motion.xOffset = xOffset;
339 msg.body.motion.yOffset = yOffset;
340 msg.body.motion.xPrecision = xPrecision;
341 msg.body.motion.yPrecision = yPrecision;
342 msg.body.motion.downTime = downTime;
343 msg.body.motion.eventTime = eventTime;
344 msg.body.motion.pointerCount = pointerCount;
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100345 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700346 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
347 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
348 }
349 return mChannel->sendMessage(&msg);
350}
351
352status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandled) {
353#if DEBUG_TRANSPORT_ACTIONS
354 ALOGD("channel '%s' publisher ~ receiveFinishedSignal",
355 mChannel->getName().string());
356#endif
357
358 InputMessage msg;
359 status_t result = mChannel->receiveMessage(&msg);
360 if (result) {
361 *outSeq = 0;
362 *outHandled = false;
363 return result;
364 }
365 if (msg.header.type != InputMessage::TYPE_FINISHED) {
366 ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer",
367 mChannel->getName().string(), msg.header.type);
368 return UNKNOWN_ERROR;
369 }
370 *outSeq = msg.body.finished.seq;
371 *outHandled = msg.body.finished.handled;
372 return OK;
373}
374
375// --- InputConsumer ---
376
377InputConsumer::InputConsumer(const sp<InputChannel>& channel) :
378 mResampleTouch(isTouchResamplingEnabled()),
379 mChannel(channel), mMsgDeferred(false) {
380}
381
382InputConsumer::~InputConsumer() {
383}
384
385bool InputConsumer::isTouchResamplingEnabled() {
386 char value[PROPERTY_VALUE_MAX];
Michael Wrightad526f82013-10-10 18:54:12 -0700387 int length = property_get("ro.input.noresample", value, NULL);
Jeff Brown5912f952013-07-01 19:10:31 -0700388 if (length > 0) {
Michael Wrightad526f82013-10-10 18:54:12 -0700389 if (!strcmp("1", value)) {
Jeff Brown5912f952013-07-01 19:10:31 -0700390 return false;
391 }
Michael Wrightad526f82013-10-10 18:54:12 -0700392 if (strcmp("0", value)) {
393 ALOGD("Unrecognized property value for 'ro.input.noresample'. "
Jeff Brown5912f952013-07-01 19:10:31 -0700394 "Use '1' or '0'.");
395 }
396 }
397 return true;
398}
399
400status_t InputConsumer::consume(InputEventFactoryInterface* factory,
Tarandeep Singh58641502017-07-31 10:51:54 -0700401 bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent,
402 int32_t* displayId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700403#if DEBUG_TRANSPORT_ACTIONS
404 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%lld",
405 mChannel->getName().string(), consumeBatches ? "true" : "false", frameTime);
406#endif
407
408 *outSeq = 0;
409 *outEvent = NULL;
Tarandeep Singh58641502017-07-31 10:51:54 -0700410 *displayId = -1; // Invalid display.
Jeff Brown5912f952013-07-01 19:10:31 -0700411
412 // Fetch the next input message.
413 // Loop until an event can be returned or no additional events are received.
414 while (!*outEvent) {
415 if (mMsgDeferred) {
416 // mMsg contains a valid input message from the previous call to consume
417 // that has not yet been processed.
418 mMsgDeferred = false;
419 } else {
420 // Receive a fresh message.
421 status_t result = mChannel->receiveMessage(&mMsg);
422 if (result) {
423 // Consume the next batched event unless batches are being held for later.
424 if (consumeBatches || result != WOULD_BLOCK) {
Tarandeep Singh58641502017-07-31 10:51:54 -0700425 result = consumeBatch(factory, frameTime, outSeq, outEvent, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700426 if (*outEvent) {
427#if DEBUG_TRANSPORT_ACTIONS
428 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
429 mChannel->getName().string(), *outSeq);
430#endif
431 break;
432 }
433 }
434 return result;
435 }
436 }
437
438 switch (mMsg.header.type) {
439 case InputMessage::TYPE_KEY: {
440 KeyEvent* keyEvent = factory->createKeyEvent();
441 if (!keyEvent) return NO_MEMORY;
442
443 initializeKeyEvent(keyEvent, &mMsg);
444 *outSeq = mMsg.body.key.seq;
445 *outEvent = keyEvent;
446#if DEBUG_TRANSPORT_ACTIONS
447 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
448 mChannel->getName().string(), *outSeq);
449#endif
450 break;
451 }
452
453 case AINPUT_EVENT_TYPE_MOTION: {
454 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
455 if (batchIndex >= 0) {
456 Batch& batch = mBatches.editItemAt(batchIndex);
457 if (canAddSample(batch, &mMsg)) {
458 batch.samples.push(mMsg);
459#if DEBUG_TRANSPORT_ACTIONS
460 ALOGD("channel '%s' consumer ~ appended to batch event",
461 mChannel->getName().string());
462#endif
463 break;
464 } else {
465 // We cannot append to the batch in progress, so we need to consume
466 // the previous batch right now and defer the new message until later.
467 mMsgDeferred = true;
468 status_t result = consumeSamples(factory,
Tarandeep Singh58641502017-07-31 10:51:54 -0700469 batch, batch.samples.size(), outSeq, outEvent, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700470 mBatches.removeAt(batchIndex);
471 if (result) {
472 return result;
473 }
474#if DEBUG_TRANSPORT_ACTIONS
475 ALOGD("channel '%s' consumer ~ consumed batch event and "
476 "deferred current event, seq=%u",
477 mChannel->getName().string(), *outSeq);
478#endif
479 break;
480 }
481 }
482
483 // Start a new batch if needed.
484 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE
485 || mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
486 mBatches.push();
487 Batch& batch = mBatches.editTop();
488 batch.samples.push(mMsg);
489#if DEBUG_TRANSPORT_ACTIONS
490 ALOGD("channel '%s' consumer ~ started batch event",
491 mChannel->getName().string());
492#endif
493 break;
494 }
495
496 MotionEvent* motionEvent = factory->createMotionEvent();
497 if (! motionEvent) return NO_MEMORY;
498
499 updateTouchState(&mMsg);
500 initializeMotionEvent(motionEvent, &mMsg);
501 *outSeq = mMsg.body.motion.seq;
502 *outEvent = motionEvent;
Tarandeep Singh58641502017-07-31 10:51:54 -0700503 *displayId = mMsg.body.motion.displayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700504#if DEBUG_TRANSPORT_ACTIONS
505 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
506 mChannel->getName().string(), *outSeq);
507#endif
508 break;
509 }
510
511 default:
512 ALOGE("channel '%s' consumer ~ Received unexpected message of type %d",
513 mChannel->getName().string(), mMsg.header.type);
514 return UNKNOWN_ERROR;
515 }
516 }
517 return OK;
518}
519
520status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
Tarandeep Singh58641502017-07-31 10:51:54 -0700521 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent, int32_t* displayId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700522 status_t result;
Dan Austin1faef802015-09-22 14:28:07 -0700523 for (size_t i = mBatches.size(); i > 0; ) {
524 i--;
Jeff Brown5912f952013-07-01 19:10:31 -0700525 Batch& batch = mBatches.editItemAt(i);
Michael Wright32232172013-10-21 12:05:22 -0700526 if (frameTime < 0) {
Jeff Brown5912f952013-07-01 19:10:31 -0700527 result = consumeSamples(factory, batch, batch.samples.size(),
Tarandeep Singh58641502017-07-31 10:51:54 -0700528 outSeq, outEvent, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700529 mBatches.removeAt(i);
530 return result;
531 }
532
Michael Wright32232172013-10-21 12:05:22 -0700533 nsecs_t sampleTime = frameTime;
534 if (mResampleTouch) {
535 sampleTime -= RESAMPLE_LATENCY;
536 }
Jeff Brown5912f952013-07-01 19:10:31 -0700537 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
538 if (split < 0) {
539 continue;
540 }
541
Tarandeep Singh58641502017-07-31 10:51:54 -0700542 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent, displayId);
Jeff Brown5912f952013-07-01 19:10:31 -0700543 const InputMessage* next;
544 if (batch.samples.isEmpty()) {
545 mBatches.removeAt(i);
546 next = NULL;
547 } else {
548 next = &batch.samples.itemAt(0);
549 }
Michael Wright32232172013-10-21 12:05:22 -0700550 if (!result && mResampleTouch) {
Jeff Brown5912f952013-07-01 19:10:31 -0700551 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
552 }
553 return result;
554 }
555
556 return WOULD_BLOCK;
557}
558
559status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
Tarandeep Singh58641502017-07-31 10:51:54 -0700560 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent, int32_t* displayId) {
Jeff Brown5912f952013-07-01 19:10:31 -0700561 MotionEvent* motionEvent = factory->createMotionEvent();
562 if (! motionEvent) return NO_MEMORY;
563
564 uint32_t chain = 0;
565 for (size_t i = 0; i < count; i++) {
566 InputMessage& msg = batch.samples.editItemAt(i);
567 updateTouchState(&msg);
568 if (i) {
569 SeqChain seqChain;
570 seqChain.seq = msg.body.motion.seq;
571 seqChain.chain = chain;
572 mSeqChains.push(seqChain);
573 addSample(motionEvent, &msg);
574 } else {
Tarandeep Singh58641502017-07-31 10:51:54 -0700575 *displayId = msg.body.motion.displayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700576 initializeMotionEvent(motionEvent, &msg);
577 }
578 chain = msg.body.motion.seq;
579 }
580 batch.samples.removeItemsAt(0, count);
581
582 *outSeq = chain;
583 *outEvent = motionEvent;
584 return OK;
585}
586
587void InputConsumer::updateTouchState(InputMessage* msg) {
588 if (!mResampleTouch ||
589 !(msg->body.motion.source & AINPUT_SOURCE_CLASS_POINTER)) {
590 return;
591 }
592
593 int32_t deviceId = msg->body.motion.deviceId;
594 int32_t source = msg->body.motion.source;
595 nsecs_t eventTime = msg->body.motion.eventTime;
596
597 // Update the touch state history to incorporate the new input message.
598 // If the message is in the past relative to the most recently produced resampled
599 // touch, then use the resampled time and coordinates instead.
600 switch (msg->body.motion.action & AMOTION_EVENT_ACTION_MASK) {
601 case AMOTION_EVENT_ACTION_DOWN: {
602 ssize_t index = findTouchState(deviceId, source);
603 if (index < 0) {
604 mTouchStates.push();
605 index = mTouchStates.size() - 1;
606 }
607 TouchState& touchState = mTouchStates.editItemAt(index);
608 touchState.initialize(deviceId, source);
609 touchState.addHistory(msg);
610 break;
611 }
612
613 case AMOTION_EVENT_ACTION_MOVE: {
614 ssize_t index = findTouchState(deviceId, source);
615 if (index >= 0) {
616 TouchState& touchState = mTouchStates.editItemAt(index);
617 touchState.addHistory(msg);
618 if (eventTime < touchState.lastResample.eventTime) {
619 rewriteMessage(touchState, msg);
620 } else {
621 touchState.lastResample.idBits.clear();
622 }
623 }
624 break;
625 }
626
627 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
628 ssize_t index = findTouchState(deviceId, source);
629 if (index >= 0) {
630 TouchState& touchState = mTouchStates.editItemAt(index);
631 touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId());
632 rewriteMessage(touchState, msg);
633 }
634 break;
635 }
636
637 case AMOTION_EVENT_ACTION_POINTER_UP: {
638 ssize_t index = findTouchState(deviceId, source);
639 if (index >= 0) {
640 TouchState& touchState = mTouchStates.editItemAt(index);
641 rewriteMessage(touchState, msg);
642 touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId());
643 }
644 break;
645 }
646
647 case AMOTION_EVENT_ACTION_SCROLL: {
648 ssize_t index = findTouchState(deviceId, source);
649 if (index >= 0) {
650 const TouchState& touchState = mTouchStates.itemAt(index);
651 rewriteMessage(touchState, msg);
652 }
653 break;
654 }
655
656 case AMOTION_EVENT_ACTION_UP:
657 case AMOTION_EVENT_ACTION_CANCEL: {
658 ssize_t index = findTouchState(deviceId, source);
659 if (index >= 0) {
660 const TouchState& touchState = mTouchStates.itemAt(index);
661 rewriteMessage(touchState, msg);
662 mTouchStates.removeAt(index);
663 }
664 break;
665 }
666 }
667}
668
669void InputConsumer::rewriteMessage(const TouchState& state, InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100670 for (uint32_t i = 0; i < msg->body.motion.pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700671 uint32_t id = msg->body.motion.pointers[i].properties.id;
672 if (state.lastResample.idBits.hasBit(id)) {
673 PointerCoords& msgCoords = msg->body.motion.pointers[i].coords;
674 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
675#if DEBUG_RESAMPLING
676 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
677 resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
678 resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_Y),
679 msgCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
680 msgCoords.getAxisValue(AMOTION_EVENT_AXIS_Y));
681#endif
682 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
683 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
684 }
685 }
686}
687
688void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
689 const InputMessage* next) {
690 if (!mResampleTouch
691 || !(event->getSource() & AINPUT_SOURCE_CLASS_POINTER)
692 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
693 return;
694 }
695
696 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
697 if (index < 0) {
698#if DEBUG_RESAMPLING
699 ALOGD("Not resampled, no touch state for device.");
700#endif
701 return;
702 }
703
704 TouchState& touchState = mTouchStates.editItemAt(index);
705 if (touchState.historySize < 1) {
706#if DEBUG_RESAMPLING
707 ALOGD("Not resampled, no history for device.");
708#endif
709 return;
710 }
711
712 // Ensure that the current sample has all of the pointers that need to be reported.
713 const History* current = touchState.getHistory(0);
714 size_t pointerCount = event->getPointerCount();
715 for (size_t i = 0; i < pointerCount; i++) {
716 uint32_t id = event->getPointerId(i);
717 if (!current->idBits.hasBit(id)) {
718#if DEBUG_RESAMPLING
719 ALOGD("Not resampled, missing id %d", id);
720#endif
721 return;
722 }
723 }
724
725 // Find the data to use for resampling.
726 const History* other;
727 History future;
728 float alpha;
729 if (next) {
730 // Interpolate between current sample and future sample.
731 // So current->eventTime <= sampleTime <= future.eventTime.
732 future.initializeFrom(next);
733 other = &future;
734 nsecs_t delta = future.eventTime - current->eventTime;
735 if (delta < RESAMPLE_MIN_DELTA) {
736#if DEBUG_RESAMPLING
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -0700737 ALOGD("Not resampled, delta time is too small: %lld ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -0700738#endif
739 return;
740 }
741 alpha = float(sampleTime - current->eventTime) / delta;
742 } else if (touchState.historySize >= 2) {
743 // Extrapolate future sample using current sample and past sample.
744 // So other->eventTime <= current->eventTime <= sampleTime.
745 other = touchState.getHistory(1);
746 nsecs_t delta = current->eventTime - other->eventTime;
747 if (delta < RESAMPLE_MIN_DELTA) {
748#if DEBUG_RESAMPLING
Andrew de los Reyesde18f6c2015-10-01 15:57:25 -0700749 ALOGD("Not resampled, delta time is too small: %lld ns.", delta);
750#endif
751 return;
752 } else if (delta > RESAMPLE_MAX_DELTA) {
753#if DEBUG_RESAMPLING
754 ALOGD("Not resampled, delta time is too large: %lld ns.", delta);
Jeff Brown5912f952013-07-01 19:10:31 -0700755#endif
756 return;
757 }
758 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
759 if (sampleTime > maxPredict) {
760#if DEBUG_RESAMPLING
761 ALOGD("Sample time is too far in the future, adjusting prediction "
762 "from %lld to %lld ns.",
763 sampleTime - current->eventTime, maxPredict - current->eventTime);
764#endif
765 sampleTime = maxPredict;
766 }
767 alpha = float(current->eventTime - sampleTime) / delta;
768 } else {
769#if DEBUG_RESAMPLING
770 ALOGD("Not resampled, insufficient data.");
771#endif
772 return;
773 }
774
775 // Resample touch coordinates.
776 touchState.lastResample.eventTime = sampleTime;
777 touchState.lastResample.idBits.clear();
778 for (size_t i = 0; i < pointerCount; i++) {
779 uint32_t id = event->getPointerId(i);
780 touchState.lastResample.idToIndex[id] = i;
781 touchState.lastResample.idBits.markBit(id);
782 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
783 const PointerCoords& currentCoords = current->getPointerById(id);
784 if (other->idBits.hasBit(id)
785 && shouldResampleTool(event->getToolType(i))) {
786 const PointerCoords& otherCoords = other->getPointerById(id);
787 resampledCoords.copyFrom(currentCoords);
788 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
789 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
790 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
791 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
792#if DEBUG_RESAMPLING
793 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
794 "other (%0.3f, %0.3f), alpha %0.3f",
795 id, resampledCoords.getX(), resampledCoords.getY(),
796 currentCoords.getX(), currentCoords.getY(),
797 otherCoords.getX(), otherCoords.getY(),
798 alpha);
799#endif
800 } else {
801 resampledCoords.copyFrom(currentCoords);
802#if DEBUG_RESAMPLING
803 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
804 id, resampledCoords.getX(), resampledCoords.getY(),
805 currentCoords.getX(), currentCoords.getY());
806#endif
807 }
808 }
809
810 event->addSample(sampleTime, touchState.lastResample.pointers);
811}
812
813bool InputConsumer::shouldResampleTool(int32_t toolType) {
814 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
815 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
816}
817
818status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
819#if DEBUG_TRANSPORT_ACTIONS
820 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
821 mChannel->getName().string(), seq, handled ? "true" : "false");
822#endif
823
824 if (!seq) {
825 ALOGE("Attempted to send a finished signal with sequence number 0.");
826 return BAD_VALUE;
827 }
828
829 // Send finished signals for the batch sequence chain first.
830 size_t seqChainCount = mSeqChains.size();
831 if (seqChainCount) {
832 uint32_t currentSeq = seq;
833 uint32_t chainSeqs[seqChainCount];
834 size_t chainIndex = 0;
Dan Austin1faef802015-09-22 14:28:07 -0700835 for (size_t i = seqChainCount; i > 0; ) {
836 i--;
Jeff Brown5912f952013-07-01 19:10:31 -0700837 const SeqChain& seqChain = mSeqChains.itemAt(i);
838 if (seqChain.seq == currentSeq) {
839 currentSeq = seqChain.chain;
840 chainSeqs[chainIndex++] = currentSeq;
841 mSeqChains.removeAt(i);
842 }
843 }
844 status_t status = OK;
Dan Austin1faef802015-09-22 14:28:07 -0700845 while (!status && chainIndex > 0) {
846 chainIndex--;
Jeff Brown5912f952013-07-01 19:10:31 -0700847 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
848 }
849 if (status) {
850 // An error occurred so at least one signal was not sent, reconstruct the chain.
gaoshang9090d4f2017-05-17 14:36:46 +0800851 for (;;) {
Jeff Brown5912f952013-07-01 19:10:31 -0700852 SeqChain seqChain;
853 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
854 seqChain.chain = chainSeqs[chainIndex];
855 mSeqChains.push(seqChain);
gaoshang9090d4f2017-05-17 14:36:46 +0800856 if (!chainIndex) break;
857 chainIndex--;
858 }
Jeff Brown5912f952013-07-01 19:10:31 -0700859 return status;
860 }
861 }
862
863 // Send finished signal for the last message in the batch.
864 return sendUnchainedFinishedSignal(seq, handled);
865}
866
867status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
868 InputMessage msg;
869 msg.header.type = InputMessage::TYPE_FINISHED;
870 msg.body.finished.seq = seq;
871 msg.body.finished.handled = handled;
872 return mChannel->sendMessage(&msg);
873}
874
875bool InputConsumer::hasDeferredEvent() const {
876 return mMsgDeferred;
877}
878
879bool InputConsumer::hasPendingBatch() const {
880 return !mBatches.isEmpty();
881}
882
883ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
884 for (size_t i = 0; i < mBatches.size(); i++) {
885 const Batch& batch = mBatches.itemAt(i);
886 const InputMessage& head = batch.samples.itemAt(0);
887 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
888 return i;
889 }
890 }
891 return -1;
892}
893
894ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
895 for (size_t i = 0; i < mTouchStates.size(); i++) {
896 const TouchState& touchState = mTouchStates.itemAt(i);
897 if (touchState.deviceId == deviceId && touchState.source == source) {
898 return i;
899 }
900 }
901 return -1;
902}
903
904void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
905 event->initialize(
906 msg->body.key.deviceId,
907 msg->body.key.source,
908 msg->body.key.action,
909 msg->body.key.flags,
910 msg->body.key.keyCode,
911 msg->body.key.scanCode,
912 msg->body.key.metaState,
913 msg->body.key.repeatCount,
914 msg->body.key.downTime,
915 msg->body.key.eventTime);
916}
917
918void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100919 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -0700920 PointerProperties pointerProperties[pointerCount];
921 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100922 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700923 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
924 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
925 }
926
927 event->initialize(
928 msg->body.motion.deviceId,
929 msg->body.motion.source,
930 msg->body.motion.action,
Michael Wright7b159c92015-05-14 14:48:03 +0100931 msg->body.motion.actionButton,
Jeff Brown5912f952013-07-01 19:10:31 -0700932 msg->body.motion.flags,
933 msg->body.motion.edgeFlags,
934 msg->body.motion.metaState,
935 msg->body.motion.buttonState,
936 msg->body.motion.xOffset,
937 msg->body.motion.yOffset,
938 msg->body.motion.xPrecision,
939 msg->body.motion.yPrecision,
940 msg->body.motion.downTime,
941 msg->body.motion.eventTime,
942 pointerCount,
943 pointerProperties,
944 pointerCoords);
945}
946
947void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100948 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -0700949 PointerCoords pointerCoords[pointerCount];
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100950 for (uint32_t i = 0; i < pointerCount; i++) {
Jeff Brown5912f952013-07-01 19:10:31 -0700951 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
952 }
953
954 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
955 event->addSample(msg->body.motion.eventTime, pointerCoords);
956}
957
958bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
959 const InputMessage& head = batch.samples.itemAt(0);
Narayan Kamathbc6001b2014-05-02 17:53:33 +0100960 uint32_t pointerCount = msg->body.motion.pointerCount;
Jeff Brown5912f952013-07-01 19:10:31 -0700961 if (head.body.motion.pointerCount != pointerCount
962 || head.body.motion.action != msg->body.motion.action) {
963 return false;
964 }
965 for (size_t i = 0; i < pointerCount; i++) {
966 if (head.body.motion.pointers[i].properties
967 != msg->body.motion.pointers[i].properties) {
968 return false;
969 }
970 }
971 return true;
972}
973
974ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
975 size_t numSamples = batch.samples.size();
976 size_t index = 0;
977 while (index < numSamples
978 && batch.samples.itemAt(index).body.motion.eventTime <= time) {
979 index += 1;
980 }
981 return ssize_t(index) - 1;
982}
983
984} // namespace android