blob: 498389eac3f61ee48137f3ca50f657aa22088f29 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -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
Jeff Browncbee6d62012-02-03 20:11:27 -080010// Log debug messages about channel messages (send message, receive message)
11#define DEBUG_CHANNEL_MESSAGES 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070012
13// Log debug messages whenever InputChannel objects are created/destroyed
Jeff Brown5c225b12010-06-16 01:53:36 -070014#define DEBUG_CHANNEL_LIFECYCLE 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070015
Jeff Browncbee6d62012-02-03 20:11:27 -080016// Log debug messages about transport actions
Jeff Brown072ec962012-02-07 14:46:57 -080017#define DEBUG_TRANSPORT_ACTIONS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070018
Jeff Brown771526c2012-04-27 15:13:25 -070019// Log debug messages about touch event resampling
20#define DEBUG_RESAMPLING 0
21
Jeff Brown46b9ac02010-04-22 18:58:52 -070022
Jeff Brown46b9ac02010-04-22 18:58:52 -070023#include <cutils/log.h>
Jeff Brown7174a492012-05-14 17:00:27 -070024#include <cutils/properties.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070025#include <errno.h>
26#include <fcntl.h>
Mathias Agopianb93a03f82012-02-17 15:34:57 -080027#include <androidfw/InputTransport.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070028#include <unistd.h>
Jeff Browncbee6d62012-02-03 20:11:27 -080029#include <sys/types.h>
30#include <sys/socket.h>
Jeff Brown771526c2012-04-27 15:13:25 -070031#include <math.h>
Jeff Browncbee6d62012-02-03 20:11:27 -080032
Jeff Brown46b9ac02010-04-22 18:58:52 -070033
34namespace android {
35
Jeff Brownd1c48a02012-02-06 19:12:47 -080036// Socket buffer size. The default is typically about 128KB, which is much larger than
37// we really need. So we make it smaller. It just needs to be big enough to hold
38// a few dozen large multi-finger motion events in the case where an application gets
39// behind processing touches.
40static const size_t SOCKET_BUFFER_SIZE = 32 * 1024;
41
Jeff Brown771526c2012-04-27 15:13:25 -070042// Nanoseconds per milliseconds.
43static const nsecs_t NANOS_PER_MS = 1000000;
44
45// Latency added during resampling. A few milliseconds doesn't hurt much but
46// reduces the impact of mispredicted touch positions.
Jeff Brown7174a492012-05-14 17:00:27 -070047static const nsecs_t RESAMPLE_LATENCY = 5 * NANOS_PER_MS;
Jeff Brown771526c2012-04-27 15:13:25 -070048
49// Minimum time difference between consecutive samples before attempting to resample.
Jeff Brown7174a492012-05-14 17:00:27 -070050static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS;
Jeff Brown771526c2012-04-27 15:13:25 -070051
Jeff Brown7174a492012-05-14 17:00:27 -070052// Maximum time to predict forward from the last known state, to avoid predicting too
53// far into the future. This time is further bounded by 50% of the last time delta.
54static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS;
Jeff Brown771526c2012-04-27 15:13:25 -070055
Jeff Brown7174a492012-05-14 17:00:27 -070056template<typename T>
57inline static T min(const T& a, const T& b) {
58 return a < b ? a : b;
59}
60
61inline static float lerp(float a, float b, float alpha) {
62 return a + alpha * (b - a);
63}
Jeff Brownd1c48a02012-02-06 19:12:47 -080064
Jeff Browncbee6d62012-02-03 20:11:27 -080065// --- InputMessage ---
Jeff Brown4e91a182011-04-07 11:38:09 -070066
Jeff Browncbee6d62012-02-03 20:11:27 -080067bool InputMessage::isValid(size_t actualSize) const {
68 if (size() == actualSize) {
69 switch (header.type) {
70 case TYPE_KEY:
71 return true;
72 case TYPE_MOTION:
73 return body.motion.pointerCount > 0
74 && body.motion.pointerCount <= MAX_POINTERS;
75 case TYPE_FINISHED:
76 return true;
77 }
78 }
79 return false;
80}
Jeff Brown46b9ac02010-04-22 18:58:52 -070081
Jeff Browncbee6d62012-02-03 20:11:27 -080082size_t InputMessage::size() const {
83 switch (header.type) {
84 case TYPE_KEY:
85 return sizeof(Header) + body.key.size();
86 case TYPE_MOTION:
87 return sizeof(Header) + body.motion.size();
88 case TYPE_FINISHED:
89 return sizeof(Header) + body.finished.size();
90 }
91 return sizeof(Header);
92}
Jeff Brown46b9ac02010-04-22 18:58:52 -070093
94
95// --- InputChannel ---
96
Jeff Browncbee6d62012-02-03 20:11:27 -080097InputChannel::InputChannel(const String8& name, int fd) :
98 mName(name), mFd(fd) {
Jeff Brown46b9ac02010-04-22 18:58:52 -070099#if DEBUG_CHANNEL_LIFECYCLE
Jeff Browncbee6d62012-02-03 20:11:27 -0800100 ALOGD("Input channel constructed: name='%s', fd=%d",
101 mName.string(), fd);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700102#endif
103
Jeff Browncbee6d62012-02-03 20:11:27 -0800104 int result = fcntl(mFd, F_SETFL, O_NONBLOCK);
105 LOG_ALWAYS_FATAL_IF(result != 0, "channel '%s' ~ Could not make socket "
Jeff Brown46b9ac02010-04-22 18:58:52 -0700106 "non-blocking. errno=%d", mName.string(), errno);
107}
108
109InputChannel::~InputChannel() {
110#if DEBUG_CHANNEL_LIFECYCLE
Jeff Browncbee6d62012-02-03 20:11:27 -0800111 ALOGD("Input channel destroyed: name='%s', fd=%d",
112 mName.string(), mFd);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700113#endif
114
Jeff Browncbee6d62012-02-03 20:11:27 -0800115 ::close(mFd);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700116}
117
118status_t InputChannel::openInputChannelPair(const String8& name,
Jeff Brown5c225b12010-06-16 01:53:36 -0700119 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800120 int sockets[2];
121 if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
122 status_t result = -errno;
123 ALOGE("channel '%s' ~ Could not create socket pair. errno=%d",
Jeff Brown46b9ac02010-04-22 18:58:52 -0700124 name.string(), errno);
Jeff Browncbee6d62012-02-03 20:11:27 -0800125 outServerChannel.clear();
126 outClientChannel.clear();
127 return result;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700128 }
129
Jeff Brownd1c48a02012-02-06 19:12:47 -0800130 int bufferSize = SOCKET_BUFFER_SIZE;
131 setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
132 setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
133 setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
134 setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
135
Jeff Browncbee6d62012-02-03 20:11:27 -0800136 String8 serverChannelName = name;
137 serverChannelName.append(" (server)");
138 outServerChannel = new InputChannel(serverChannelName, sockets[0]);
139
140 String8 clientChannelName = name;
141 clientChannelName.append(" (client)");
142 outClientChannel = new InputChannel(clientChannelName, sockets[1]);
143 return OK;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700144}
145
Jeff Browncbee6d62012-02-03 20:11:27 -0800146status_t InputChannel::sendMessage(const InputMessage* msg) {
147 size_t msgLength = msg->size();
Jeff Brown7dae0e42010-09-16 17:04:52 -0700148 ssize_t nWrite;
149 do {
Jeff Browncbee6d62012-02-03 20:11:27 -0800150 nWrite = ::send(mFd, msg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL);
Jeff Brown7dae0e42010-09-16 17:04:52 -0700151 } while (nWrite == -1 && errno == EINTR);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700152
Jeff Browncbee6d62012-02-03 20:11:27 -0800153 if (nWrite < 0) {
154 int error = errno;
155#if DEBUG_CHANNEL_MESSAGES
156 ALOGD("channel '%s' ~ error sending message of type %d, errno=%d", mName.string(),
157 msg->header.type, error);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700158#endif
Jeff Browncbee6d62012-02-03 20:11:27 -0800159 if (error == EAGAIN || error == EWOULDBLOCK) {
160 return WOULD_BLOCK;
161 }
162 if (error == EPIPE || error == ENOTCONN) {
163 return DEAD_OBJECT;
164 }
165 return -error;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700166 }
167
Jeff Browncbee6d62012-02-03 20:11:27 -0800168 if (size_t(nWrite) != msgLength) {
169#if DEBUG_CHANNEL_MESSAGES
170 ALOGD("channel '%s' ~ error sending message type %d, send was incomplete",
171 mName.string(), msg->header.type);
Jeff Brown5c225b12010-06-16 01:53:36 -0700172#endif
173 return DEAD_OBJECT;
174 }
175
Jeff Browncbee6d62012-02-03 20:11:27 -0800176#if DEBUG_CHANNEL_MESSAGES
177 ALOGD("channel '%s' ~ sent message of type %d", mName.string(), msg->header.type);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700178#endif
Jeff Browncbee6d62012-02-03 20:11:27 -0800179 return OK;
180}
181
182status_t InputChannel::receiveMessage(InputMessage* msg) {
183 ssize_t nRead;
184 do {
185 nRead = ::recv(mFd, msg, sizeof(InputMessage), MSG_DONTWAIT);
186 } while (nRead == -1 && errno == EINTR);
187
188 if (nRead < 0) {
189 int error = errno;
190#if DEBUG_CHANNEL_MESSAGES
191 ALOGD("channel '%s' ~ receive message failed, errno=%d", mName.string(), errno);
192#endif
193 if (error == EAGAIN || error == EWOULDBLOCK) {
194 return WOULD_BLOCK;
195 }
196 if (error == EPIPE || error == ENOTCONN) {
197 return DEAD_OBJECT;
198 }
199 return -error;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700200 }
201
Jeff Browncbee6d62012-02-03 20:11:27 -0800202 if (nRead == 0) { // check for EOF
203#if DEBUG_CHANNEL_MESSAGES
204 ALOGD("channel '%s' ~ receive message failed because peer was closed", mName.string());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700205#endif
Jeff Browncbee6d62012-02-03 20:11:27 -0800206 return DEAD_OBJECT;
207 }
208
209 if (!msg->isValid(nRead)) {
210#if DEBUG_CHANNEL_MESSAGES
211 ALOGD("channel '%s' ~ received invalid message", mName.string());
212#endif
213 return BAD_VALUE;
214 }
215
216#if DEBUG_CHANNEL_MESSAGES
217 ALOGD("channel '%s' ~ received message of type %d", mName.string(), msg->header.type);
218#endif
219 return OK;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700220}
221
Jeff Brown1951ce82013-04-04 22:45:12 -0700222sp<InputChannel> InputChannel::dup() const {
223 int fd = ::dup(getFd());
224 return fd >= 0 ? new InputChannel(getName(), fd) : NULL;
225}
226
Jeff Brown46b9ac02010-04-22 18:58:52 -0700227
228// --- InputPublisher ---
229
230InputPublisher::InputPublisher(const sp<InputChannel>& channel) :
Jeff Browncbee6d62012-02-03 20:11:27 -0800231 mChannel(channel) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700232}
233
234InputPublisher::~InputPublisher() {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700235}
236
237status_t InputPublisher::publishKeyEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800238 uint32_t seq,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700239 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700240 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700241 int32_t action,
242 int32_t flags,
243 int32_t keyCode,
244 int32_t scanCode,
245 int32_t metaState,
246 int32_t repeatCount,
247 nsecs_t downTime,
248 nsecs_t eventTime) {
249#if DEBUG_TRANSPORT_ACTIONS
Jeff Brown072ec962012-02-07 14:46:57 -0800250 ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, "
Jeff Brown85a31762010-09-01 17:01:00 -0700251 "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d,"
Jeff Brown46b9ac02010-04-22 18:58:52 -0700252 "downTime=%lld, eventTime=%lld",
Jeff Brown072ec962012-02-07 14:46:57 -0800253 mChannel->getName().string(), seq,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700254 deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700255 downTime, eventTime);
256#endif
257
Jeff Brown072ec962012-02-07 14:46:57 -0800258 if (!seq) {
259 ALOGE("Attempted to publish a key event with sequence number 0.");
260 return BAD_VALUE;
261 }
262
Jeff Browncbee6d62012-02-03 20:11:27 -0800263 InputMessage msg;
264 msg.header.type = InputMessage::TYPE_KEY;
Jeff Brown072ec962012-02-07 14:46:57 -0800265 msg.body.key.seq = seq;
Jeff Browncbee6d62012-02-03 20:11:27 -0800266 msg.body.key.deviceId = deviceId;
267 msg.body.key.source = source;
268 msg.body.key.action = action;
269 msg.body.key.flags = flags;
270 msg.body.key.keyCode = keyCode;
271 msg.body.key.scanCode = scanCode;
272 msg.body.key.metaState = metaState;
273 msg.body.key.repeatCount = repeatCount;
274 msg.body.key.downTime = downTime;
275 msg.body.key.eventTime = eventTime;
276 return mChannel->sendMessage(&msg);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700277}
278
279status_t InputPublisher::publishMotionEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800280 uint32_t seq,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700281 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700282 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700283 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700284 int32_t flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700285 int32_t edgeFlags,
286 int32_t metaState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700287 int32_t buttonState,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700288 float xOffset,
289 float yOffset,
290 float xPrecision,
291 float yPrecision,
292 nsecs_t downTime,
293 nsecs_t eventTime,
294 size_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700295 const PointerProperties* pointerProperties,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700296 const PointerCoords* pointerCoords) {
297#if DEBUG_TRANSPORT_ACTIONS
Jeff Brown072ec962012-02-07 14:46:57 -0800298 ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, "
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700299 "action=0x%x, flags=0x%x, edgeFlags=0x%x, metaState=0x%x, buttonState=0x%x, "
300 "xOffset=%f, yOffset=%f, "
Jeff Brown46b9ac02010-04-22 18:58:52 -0700301 "xPrecision=%f, yPrecision=%f, downTime=%lld, eventTime=%lld, "
302 "pointerCount=%d",
Jeff Brown072ec962012-02-07 14:46:57 -0800303 mChannel->getName().string(), seq,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700304 deviceId, source, action, flags, edgeFlags, metaState, buttonState,
305 xOffset, yOffset, xPrecision, yPrecision, downTime, eventTime, pointerCount);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700306#endif
307
Jeff Brown072ec962012-02-07 14:46:57 -0800308 if (!seq) {
309 ALOGE("Attempted to publish a motion event with sequence number 0.");
310 return BAD_VALUE;
311 }
312
Jeff Brown46b9ac02010-04-22 18:58:52 -0700313 if (pointerCount > MAX_POINTERS || pointerCount < 1) {
Steve Block3762c312012-01-06 19:20:56 +0000314 ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %d.",
Jeff Brown46b9ac02010-04-22 18:58:52 -0700315 mChannel->getName().string(), pointerCount);
316 return BAD_VALUE;
317 }
318
Jeff Browncbee6d62012-02-03 20:11:27 -0800319 InputMessage msg;
320 msg.header.type = InputMessage::TYPE_MOTION;
Jeff Brown072ec962012-02-07 14:46:57 -0800321 msg.body.motion.seq = seq;
Jeff Browncbee6d62012-02-03 20:11:27 -0800322 msg.body.motion.deviceId = deviceId;
323 msg.body.motion.source = source;
324 msg.body.motion.action = action;
325 msg.body.motion.flags = flags;
326 msg.body.motion.edgeFlags = edgeFlags;
327 msg.body.motion.metaState = metaState;
328 msg.body.motion.buttonState = buttonState;
329 msg.body.motion.xOffset = xOffset;
330 msg.body.motion.yOffset = yOffset;
331 msg.body.motion.xPrecision = xPrecision;
332 msg.body.motion.yPrecision = yPrecision;
333 msg.body.motion.downTime = downTime;
334 msg.body.motion.eventTime = eventTime;
335 msg.body.motion.pointerCount = pointerCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700336 for (size_t i = 0; i < pointerCount; i++) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800337 msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]);
338 msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700339 }
Jeff Browncbee6d62012-02-03 20:11:27 -0800340 return mChannel->sendMessage(&msg);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700341}
342
Jeff Brown072ec962012-02-07 14:46:57 -0800343status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandled) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700344#if DEBUG_TRANSPORT_ACTIONS
Steve Block5baa3a62011-12-20 16:23:08 +0000345 ALOGD("channel '%s' publisher ~ receiveFinishedSignal",
Jeff Brown46b9ac02010-04-22 18:58:52 -0700346 mChannel->getName().string());
347#endif
348
Jeff Browncbee6d62012-02-03 20:11:27 -0800349 InputMessage msg;
350 status_t result = mChannel->receiveMessage(&msg);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700351 if (result) {
Jeff Brown072ec962012-02-07 14:46:57 -0800352 *outSeq = 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800353 *outHandled = false;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700354 return result;
355 }
Jeff Browncbee6d62012-02-03 20:11:27 -0800356 if (msg.header.type != InputMessage::TYPE_FINISHED) {
357 ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer",
358 mChannel->getName().string(), msg.header.type);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700359 return UNKNOWN_ERROR;
360 }
Jeff Brown072ec962012-02-07 14:46:57 -0800361 *outSeq = msg.body.finished.seq;
Jeff Browncbee6d62012-02-03 20:11:27 -0800362 *outHandled = msg.body.finished.handled;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700363 return OK;
364}
365
366// --- InputConsumer ---
367
368InputConsumer::InputConsumer(const sp<InputChannel>& channel) :
Jeff Brown7174a492012-05-14 17:00:27 -0700369 mResampleTouch(isTouchResamplingEnabled()),
Jeff Brown90fde932012-02-13 12:44:01 -0800370 mChannel(channel), mMsgDeferred(false) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700371}
372
373InputConsumer::~InputConsumer() {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700374}
375
Jeff Brown7174a492012-05-14 17:00:27 -0700376bool InputConsumer::isTouchResamplingEnabled() {
377 char value[PROPERTY_VALUE_MAX];
378 int length = property_get("debug.inputconsumer.resample", value, NULL);
379 if (length > 0) {
380 if (!strcmp("0", value)) {
381 return false;
382 }
383 if (strcmp("1", value)) {
384 ALOGD("Unrecognized property value for 'debug.inputconsumer.resample'. "
385 "Use '1' or '0'.");
386 }
387 }
388 return true;
389}
390
Jeff Brown072ec962012-02-07 14:46:57 -0800391status_t InputConsumer::consume(InputEventFactoryInterface* factory,
Jeff Brown771526c2012-04-27 15:13:25 -0700392 bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700393#if DEBUG_TRANSPORT_ACTIONS
Jeff Brown771526c2012-04-27 15:13:25 -0700394 ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%lld",
395 mChannel->getName().string(), consumeBatches ? "true" : "false", frameTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700396#endif
397
Jeff Brown072ec962012-02-07 14:46:57 -0800398 *outSeq = 0;
Jeff Brown5c225b12010-06-16 01:53:36 -0700399 *outEvent = NULL;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700400
Jeff Brown072ec962012-02-07 14:46:57 -0800401 // Fetch the next input message.
402 // Loop until an event can be returned or no additional events are received.
403 while (!*outEvent) {
Jeff Brown90fde932012-02-13 12:44:01 -0800404 if (mMsgDeferred) {
405 // mMsg contains a valid input message from the previous call to consume
406 // that has not yet been processed.
407 mMsgDeferred = false;
408 } else {
409 // Receive a fresh message.
410 status_t result = mChannel->receiveMessage(&mMsg);
411 if (result) {
412 // Consume the next batched event unless batches are being held for later.
Jeff Brown771526c2012-04-27 15:13:25 -0700413 if (consumeBatches || result != WOULD_BLOCK) {
414 result = consumeBatch(factory, frameTime, outSeq, outEvent);
415 if (*outEvent) {
Jeff Brown072ec962012-02-07 14:46:57 -0800416#if DEBUG_TRANSPORT_ACTIONS
Jeff Brown771526c2012-04-27 15:13:25 -0700417 ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u",
418 mChannel->getName().string(), *outSeq);
Jeff Brown072ec962012-02-07 14:46:57 -0800419#endif
Jeff Brown771526c2012-04-27 15:13:25 -0700420 break;
421 }
Jeff Brown90fde932012-02-13 12:44:01 -0800422 }
423 return result;
Jeff Brown072ec962012-02-07 14:46:57 -0800424 }
Jeff Browncbee6d62012-02-03 20:11:27 -0800425 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700426
Jeff Brown90fde932012-02-13 12:44:01 -0800427 switch (mMsg.header.type) {
Jeff Brown072ec962012-02-07 14:46:57 -0800428 case InputMessage::TYPE_KEY: {
429 KeyEvent* keyEvent = factory->createKeyEvent();
430 if (!keyEvent) return NO_MEMORY;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700431
Jeff Brown90fde932012-02-13 12:44:01 -0800432 initializeKeyEvent(keyEvent, &mMsg);
433 *outSeq = mMsg.body.key.seq;
Jeff Brown072ec962012-02-07 14:46:57 -0800434 *outEvent = keyEvent;
435#if DEBUG_TRANSPORT_ACTIONS
436 ALOGD("channel '%s' consumer ~ consumed key event, seq=%u",
437 mChannel->getName().string(), *outSeq);
438#endif
439 break;
440 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700441
Jeff Brown072ec962012-02-07 14:46:57 -0800442 case AINPUT_EVENT_TYPE_MOTION: {
Jeff Brown90fde932012-02-13 12:44:01 -0800443 ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source);
Jeff Brown072ec962012-02-07 14:46:57 -0800444 if (batchIndex >= 0) {
445 Batch& batch = mBatches.editItemAt(batchIndex);
Jeff Brown771526c2012-04-27 15:13:25 -0700446 if (canAddSample(batch, &mMsg)) {
447 batch.samples.push(mMsg);
Jeff Brown072ec962012-02-07 14:46:57 -0800448#if DEBUG_TRANSPORT_ACTIONS
449 ALOGD("channel '%s' consumer ~ appended to batch event",
450 mChannel->getName().string());
451#endif
452 break;
453 } else {
Jeff Brown072ec962012-02-07 14:46:57 -0800454 // We cannot append to the batch in progress, so we need to consume
Jeff Brown90fde932012-02-13 12:44:01 -0800455 // the previous batch right now and defer the new message until later.
456 mMsgDeferred = true;
Jeff Brown771526c2012-04-27 15:13:25 -0700457 status_t result = consumeSamples(factory,
458 batch, batch.samples.size(), outSeq, outEvent);
Jeff Brown072ec962012-02-07 14:46:57 -0800459 mBatches.removeAt(batchIndex);
Jeff Brown771526c2012-04-27 15:13:25 -0700460 if (result) {
461 return result;
462 }
Jeff Brown072ec962012-02-07 14:46:57 -0800463#if DEBUG_TRANSPORT_ACTIONS
464 ALOGD("channel '%s' consumer ~ consumed batch event and "
465 "deferred current event, seq=%u",
466 mChannel->getName().string(), *outSeq);
467#endif
468 break;
469 }
470 }
471
472 // Start a new batch if needed.
Jeff Brown90fde932012-02-13 12:44:01 -0800473 if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE
474 || mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) {
Jeff Brown072ec962012-02-07 14:46:57 -0800475 mBatches.push();
476 Batch& batch = mBatches.editTop();
Jeff Brown771526c2012-04-27 15:13:25 -0700477 batch.samples.push(mMsg);
Jeff Brown072ec962012-02-07 14:46:57 -0800478#if DEBUG_TRANSPORT_ACTIONS
479 ALOGD("channel '%s' consumer ~ started batch event",
480 mChannel->getName().string());
481#endif
482 break;
483 }
484
485 MotionEvent* motionEvent = factory->createMotionEvent();
486 if (! motionEvent) return NO_MEMORY;
487
Jeff Brown771526c2012-04-27 15:13:25 -0700488 updateTouchState(&mMsg);
Jeff Brown90fde932012-02-13 12:44:01 -0800489 initializeMotionEvent(motionEvent, &mMsg);
490 *outSeq = mMsg.body.motion.seq;
Jeff Brown072ec962012-02-07 14:46:57 -0800491 *outEvent = motionEvent;
492#if DEBUG_TRANSPORT_ACTIONS
493 ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u",
494 mChannel->getName().string(), *outSeq);
495#endif
496 break;
497 }
498
499 default:
500 ALOGE("channel '%s' consumer ~ Received unexpected message of type %d",
Jeff Brown90fde932012-02-13 12:44:01 -0800501 mChannel->getName().string(), mMsg.header.type);
Jeff Brown072ec962012-02-07 14:46:57 -0800502 return UNKNOWN_ERROR;
503 }
504 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700505 return OK;
506}
507
Jeff Brown771526c2012-04-27 15:13:25 -0700508status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory,
509 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) {
510 status_t result;
511 for (size_t i = mBatches.size(); i-- > 0; ) {
512 Batch& batch = mBatches.editItemAt(i);
513 if (frameTime < 0) {
514 result = consumeSamples(factory, batch, batch.samples.size(),
515 outSeq, outEvent);
516 mBatches.removeAt(i);
517 return result;
518 }
519
520 nsecs_t sampleTime = frameTime - RESAMPLE_LATENCY;
521 ssize_t split = findSampleNoLaterThan(batch, sampleTime);
522 if (split < 0) {
523 continue;
524 }
525
526 result = consumeSamples(factory, batch, split + 1, outSeq, outEvent);
527 const InputMessage* next;
528 if (batch.samples.isEmpty()) {
529 mBatches.removeAt(i);
530 next = NULL;
531 } else {
532 next = &batch.samples.itemAt(0);
533 }
534 if (!result) {
535 resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next);
536 }
537 return result;
538 }
539
540 return WOULD_BLOCK;
541}
542
543status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory,
544 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) {
545 MotionEvent* motionEvent = factory->createMotionEvent();
546 if (! motionEvent) return NO_MEMORY;
547
548 uint32_t chain = 0;
549 for (size_t i = 0; i < count; i++) {
550 InputMessage& msg = batch.samples.editItemAt(i);
551 updateTouchState(&msg);
552 if (i) {
553 SeqChain seqChain;
554 seqChain.seq = msg.body.motion.seq;
555 seqChain.chain = chain;
556 mSeqChains.push(seqChain);
557 addSample(motionEvent, &msg);
558 } else {
559 initializeMotionEvent(motionEvent, &msg);
560 }
561 chain = msg.body.motion.seq;
562 }
563 batch.samples.removeItemsAt(0, count);
564
565 *outSeq = chain;
566 *outEvent = motionEvent;
567 return OK;
568}
569
570void InputConsumer::updateTouchState(InputMessage* msg) {
Jeff Brown7174a492012-05-14 17:00:27 -0700571 if (!mResampleTouch ||
572 !(msg->body.motion.source & AINPUT_SOURCE_CLASS_POINTER)) {
Jeff Brown771526c2012-04-27 15:13:25 -0700573 return;
574 }
575
576 int32_t deviceId = msg->body.motion.deviceId;
577 int32_t source = msg->body.motion.source;
Jeff Brown7174a492012-05-14 17:00:27 -0700578 nsecs_t eventTime = msg->body.motion.eventTime;
Jeff Brown771526c2012-04-27 15:13:25 -0700579
Jeff Brown7174a492012-05-14 17:00:27 -0700580 // Update the touch state history to incorporate the new input message.
581 // If the message is in the past relative to the most recently produced resampled
582 // touch, then use the resampled time and coordinates instead.
583 switch (msg->body.motion.action & AMOTION_EVENT_ACTION_MASK) {
Jeff Brown771526c2012-04-27 15:13:25 -0700584 case AMOTION_EVENT_ACTION_DOWN: {
585 ssize_t index = findTouchState(deviceId, source);
586 if (index < 0) {
587 mTouchStates.push();
588 index = mTouchStates.size() - 1;
589 }
590 TouchState& touchState = mTouchStates.editItemAt(index);
591 touchState.initialize(deviceId, source);
592 touchState.addHistory(msg);
593 break;
594 }
595
596 case AMOTION_EVENT_ACTION_MOVE: {
597 ssize_t index = findTouchState(deviceId, source);
598 if (index >= 0) {
599 TouchState& touchState = mTouchStates.editItemAt(index);
600 touchState.addHistory(msg);
Jeff Brown7174a492012-05-14 17:00:27 -0700601 if (eventTime < touchState.lastResample.eventTime) {
602 rewriteMessage(touchState, msg);
603 } else {
604 touchState.lastResample.idBits.clear();
605 }
606 }
607 break;
608 }
609
610 case AMOTION_EVENT_ACTION_POINTER_DOWN: {
611 ssize_t index = findTouchState(deviceId, source);
612 if (index >= 0) {
613 TouchState& touchState = mTouchStates.editItemAt(index);
614 touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId());
615 rewriteMessage(touchState, msg);
616 }
617 break;
618 }
619
620 case AMOTION_EVENT_ACTION_POINTER_UP: {
621 ssize_t index = findTouchState(deviceId, source);
622 if (index >= 0) {
623 TouchState& touchState = mTouchStates.editItemAt(index);
624 rewriteMessage(touchState, msg);
625 touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId());
626 }
627 break;
628 }
629
630 case AMOTION_EVENT_ACTION_SCROLL: {
631 ssize_t index = findTouchState(deviceId, source);
632 if (index >= 0) {
633 const TouchState& touchState = mTouchStates.itemAt(index);
634 rewriteMessage(touchState, msg);
Jeff Brown771526c2012-04-27 15:13:25 -0700635 }
636 break;
637 }
638
639 case AMOTION_EVENT_ACTION_UP:
640 case AMOTION_EVENT_ACTION_CANCEL: {
641 ssize_t index = findTouchState(deviceId, source);
642 if (index >= 0) {
Jeff Brown7174a492012-05-14 17:00:27 -0700643 const TouchState& touchState = mTouchStates.itemAt(index);
644 rewriteMessage(touchState, msg);
Jeff Brown771526c2012-04-27 15:13:25 -0700645 mTouchStates.removeAt(index);
646 }
647 break;
648 }
649 }
650}
651
Jeff Brown7174a492012-05-14 17:00:27 -0700652void InputConsumer::rewriteMessage(const TouchState& state, InputMessage* msg) {
653 for (size_t i = 0; i < msg->body.motion.pointerCount; i++) {
654 uint32_t id = msg->body.motion.pointers[i].properties.id;
655 if (state.lastResample.idBits.hasBit(id)) {
656 PointerCoords& msgCoords = msg->body.motion.pointers[i].coords;
657 const PointerCoords& resampleCoords = state.lastResample.getPointerById(id);
658#if DEBUG_RESAMPLING
659 ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id,
660 resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
661 resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_Y),
662 msgCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
663 msgCoords.getAxisValue(AMOTION_EVENT_AXIS_Y));
664#endif
665 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX());
666 msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY());
667 }
668 }
669}
670
Jeff Brown771526c2012-04-27 15:13:25 -0700671void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event,
672 const InputMessage* next) {
Jeff Brown7174a492012-05-14 17:00:27 -0700673 if (!mResampleTouch
674 || !(event->getSource() & AINPUT_SOURCE_CLASS_POINTER)
675 || event->getAction() != AMOTION_EVENT_ACTION_MOVE) {
Jeff Brown771526c2012-04-27 15:13:25 -0700676 return;
677 }
678
679 ssize_t index = findTouchState(event->getDeviceId(), event->getSource());
680 if (index < 0) {
681#if DEBUG_RESAMPLING
682 ALOGD("Not resampled, no touch state for device.");
683#endif
684 return;
685 }
686
687 TouchState& touchState = mTouchStates.editItemAt(index);
688 if (touchState.historySize < 1) {
689#if DEBUG_RESAMPLING
690 ALOGD("Not resampled, no history for device.");
691#endif
692 return;
693 }
694
Jeff Brown7174a492012-05-14 17:00:27 -0700695 // Ensure that the current sample has all of the pointers that need to be reported.
Jeff Brown771526c2012-04-27 15:13:25 -0700696 const History* current = touchState.getHistory(0);
Jeff Brown771526c2012-04-27 15:13:25 -0700697 size_t pointerCount = event->getPointerCount();
Jeff Brown771526c2012-04-27 15:13:25 -0700698 for (size_t i = 0; i < pointerCount; i++) {
699 uint32_t id = event->getPointerId(i);
700 if (!current->idBits.hasBit(id)) {
701#if DEBUG_RESAMPLING
702 ALOGD("Not resampled, missing id %d", id);
703#endif
704 return;
705 }
Jeff Brown7174a492012-05-14 17:00:27 -0700706 }
707
708 // Find the data to use for resampling.
709 const History* other;
710 History future;
711 float alpha;
712 if (next) {
713 // Interpolate between current sample and future sample.
714 // So current->eventTime <= sampleTime <= future.eventTime.
715 future.initializeFrom(next);
716 other = &future;
717 nsecs_t delta = future.eventTime - current->eventTime;
718 if (delta < RESAMPLE_MIN_DELTA) {
719#if DEBUG_RESAMPLING
720 ALOGD("Not resampled, delta time is %lld ns.", delta);
721#endif
722 return;
723 }
724 alpha = float(sampleTime - current->eventTime) / delta;
725 } else if (touchState.historySize >= 2) {
726 // Extrapolate future sample using current sample and past sample.
727 // So other->eventTime <= current->eventTime <= sampleTime.
728 other = touchState.getHistory(1);
729 nsecs_t delta = current->eventTime - other->eventTime;
730 if (delta < RESAMPLE_MIN_DELTA) {
731#if DEBUG_RESAMPLING
732 ALOGD("Not resampled, delta time is %lld ns.", delta);
733#endif
734 return;
735 }
736 nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION);
737 if (sampleTime > maxPredict) {
738#if DEBUG_RESAMPLING
739 ALOGD("Sample time is too far in the future, adjusting prediction "
740 "from %lld to %lld ns.",
741 sampleTime - current->eventTime, maxPredict - current->eventTime);
742#endif
743 sampleTime = maxPredict;
744 }
745 alpha = float(current->eventTime - sampleTime) / delta;
746 } else {
747#if DEBUG_RESAMPLING
748 ALOGD("Not resampled, insufficient data.");
749#endif
750 return;
751 }
752
753 // Resample touch coordinates.
754 touchState.lastResample.eventTime = sampleTime;
755 touchState.lastResample.idBits.clear();
756 for (size_t i = 0; i < pointerCount; i++) {
757 uint32_t id = event->getPointerId(i);
758 touchState.lastResample.idToIndex[id] = i;
759 touchState.lastResample.idBits.markBit(id);
760 PointerCoords& resampledCoords = touchState.lastResample.pointers[i];
761 const PointerCoords& currentCoords = current->getPointerById(id);
Jeff Brown771526c2012-04-27 15:13:25 -0700762 if (other->idBits.hasBit(id)
763 && shouldResampleTool(event->getToolType(i))) {
Jeff Brown7174a492012-05-14 17:00:27 -0700764 const PointerCoords& otherCoords = other->getPointerById(id);
765 resampledCoords.copyFrom(currentCoords);
766 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X,
767 lerp(currentCoords.getX(), otherCoords.getX(), alpha));
768 resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y,
769 lerp(currentCoords.getY(), otherCoords.getY(), alpha));
Jeff Brown771526c2012-04-27 15:13:25 -0700770#if DEBUG_RESAMPLING
771 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), "
772 "other (%0.3f, %0.3f), alpha %0.3f",
Jeff Brown7174a492012-05-14 17:00:27 -0700773 id, resampledCoords.getX(), resampledCoords.getY(),
Jeff Brown771526c2012-04-27 15:13:25 -0700774 currentCoords.getX(), currentCoords.getY(),
775 otherCoords.getX(), otherCoords.getY(),
776 alpha);
777#endif
778 } else {
Jeff Brown7174a492012-05-14 17:00:27 -0700779 resampledCoords.copyFrom(currentCoords);
Jeff Brown771526c2012-04-27 15:13:25 -0700780#if DEBUG_RESAMPLING
781 ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)",
Jeff Brown7174a492012-05-14 17:00:27 -0700782 id, resampledCoords.getX(), resampledCoords.getY(),
Jeff Brown771526c2012-04-27 15:13:25 -0700783 currentCoords.getX(), currentCoords.getY());
784#endif
785 }
786 }
787
Jeff Brown7174a492012-05-14 17:00:27 -0700788 event->addSample(sampleTime, touchState.lastResample.pointers);
Jeff Brown771526c2012-04-27 15:13:25 -0700789}
790
791bool InputConsumer::shouldResampleTool(int32_t toolType) {
792 return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER
793 || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
794}
795
Jeff Brown072ec962012-02-07 14:46:57 -0800796status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700797#if DEBUG_TRANSPORT_ACTIONS
Jeff Brown072ec962012-02-07 14:46:57 -0800798 ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s",
799 mChannel->getName().string(), seq, handled ? "true" : "false");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700800#endif
801
Jeff Brown072ec962012-02-07 14:46:57 -0800802 if (!seq) {
803 ALOGE("Attempted to send a finished signal with sequence number 0.");
804 return BAD_VALUE;
805 }
806
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800807 // Send finished signals for the batch sequence chain first.
808 size_t seqChainCount = mSeqChains.size();
809 if (seqChainCount) {
810 uint32_t currentSeq = seq;
811 uint32_t chainSeqs[seqChainCount];
812 size_t chainIndex = 0;
813 for (size_t i = seqChainCount; i-- > 0; ) {
814 const SeqChain& seqChain = mSeqChains.itemAt(i);
815 if (seqChain.seq == currentSeq) {
816 currentSeq = seqChain.chain;
817 chainSeqs[chainIndex++] = currentSeq;
818 mSeqChains.removeAt(i);
819 }
820 }
821 status_t status = OK;
822 while (!status && chainIndex-- > 0) {
823 status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled);
824 }
825 if (status) {
826 // An error occurred so at least one signal was not sent, reconstruct the chain.
827 do {
828 SeqChain seqChain;
829 seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq;
830 seqChain.chain = chainSeqs[chainIndex];
831 mSeqChains.push(seqChain);
832 } while (chainIndex-- > 0);
833 return status;
834 }
835 }
836
837 // Send finished signal for the last message in the batch.
838 return sendUnchainedFinishedSignal(seq, handled);
839}
840
841status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) {
Jeff Browncbee6d62012-02-03 20:11:27 -0800842 InputMessage msg;
843 msg.header.type = InputMessage::TYPE_FINISHED;
Jeff Brown072ec962012-02-07 14:46:57 -0800844 msg.body.finished.seq = seq;
Jeff Browncbee6d62012-02-03 20:11:27 -0800845 msg.body.finished.handled = handled;
846 return mChannel->sendMessage(&msg);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700847}
848
Jeff Brown2b6c32c2012-03-13 15:00:09 -0700849bool InputConsumer::hasDeferredEvent() const {
850 return mMsgDeferred;
851}
852
Jeff Brown072ec962012-02-07 14:46:57 -0800853bool InputConsumer::hasPendingBatch() const {
854 return !mBatches.isEmpty();
855}
856
857ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const {
858 for (size_t i = 0; i < mBatches.size(); i++) {
859 const Batch& batch = mBatches.itemAt(i);
Jeff Brown771526c2012-04-27 15:13:25 -0700860 const InputMessage& head = batch.samples.itemAt(0);
861 if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) {
862 return i;
863 }
864 }
865 return -1;
866}
867
868ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const {
869 for (size_t i = 0; i < mTouchStates.size(); i++) {
870 const TouchState& touchState = mTouchStates.itemAt(i);
871 if (touchState.deviceId == deviceId && touchState.source == source) {
Jeff Brown072ec962012-02-07 14:46:57 -0800872 return i;
873 }
874 }
875 return -1;
876}
877
878void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) {
879 event->initialize(
880 msg->body.key.deviceId,
881 msg->body.key.source,
882 msg->body.key.action,
883 msg->body.key.flags,
884 msg->body.key.keyCode,
885 msg->body.key.scanCode,
886 msg->body.key.metaState,
887 msg->body.key.repeatCount,
888 msg->body.key.downTime,
889 msg->body.key.eventTime);
890}
891
892void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) {
893 size_t pointerCount = msg->body.motion.pointerCount;
894 PointerProperties pointerProperties[pointerCount];
895 PointerCoords pointerCoords[pointerCount];
896 for (size_t i = 0; i < pointerCount; i++) {
897 pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties);
898 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
899 }
900
901 event->initialize(
902 msg->body.motion.deviceId,
903 msg->body.motion.source,
904 msg->body.motion.action,
905 msg->body.motion.flags,
906 msg->body.motion.edgeFlags,
907 msg->body.motion.metaState,
908 msg->body.motion.buttonState,
909 msg->body.motion.xOffset,
910 msg->body.motion.yOffset,
911 msg->body.motion.xPrecision,
912 msg->body.motion.yPrecision,
913 msg->body.motion.downTime,
914 msg->body.motion.eventTime,
915 pointerCount,
916 pointerProperties,
917 pointerCoords);
918}
919
Jeff Brown771526c2012-04-27 15:13:25 -0700920void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) {
Jeff Brown072ec962012-02-07 14:46:57 -0800921 size_t pointerCount = msg->body.motion.pointerCount;
922 PointerCoords pointerCoords[pointerCount];
923 for (size_t i = 0; i < pointerCount; i++) {
924 pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords);
925 }
926
927 event->setMetaState(event->getMetaState() | msg->body.motion.metaState);
928 event->addSample(msg->body.motion.eventTime, pointerCoords);
929}
930
Jeff Brown771526c2012-04-27 15:13:25 -0700931bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) {
932 const InputMessage& head = batch.samples.itemAt(0);
933 size_t pointerCount = msg->body.motion.pointerCount;
934 if (head.body.motion.pointerCount != pointerCount
935 || head.body.motion.action != msg->body.motion.action) {
936 return false;
937 }
938 for (size_t i = 0; i < pointerCount; i++) {
939 if (head.body.motion.pointers[i].properties
940 != msg->body.motion.pointers[i].properties) {
941 return false;
942 }
943 }
944 return true;
945}
946
947ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) {
948 size_t numSamples = batch.samples.size();
949 size_t index = 0;
950 while (index < numSamples
951 && batch.samples.itemAt(index).body.motion.eventTime <= time) {
952 index += 1;
953 }
954 return ssize_t(index) - 1;
955}
956
Jeff Brown46b9ac02010-04-22 18:58:52 -0700957} // namespace android