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