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