Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -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 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 10 | // Log debug messages about channel messages (send message, receive message) |
| 11 | #define DEBUG_CHANNEL_MESSAGES 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 12 | |
| 13 | // Log debug messages whenever InputChannel objects are created/destroyed |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 14 | #define DEBUG_CHANNEL_LIFECYCLE 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 15 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 16 | // Log debug messages about transport actions |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 17 | #define DEBUG_TRANSPORT_ACTIONS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 18 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 19 | // Log debug messages about touch event resampling |
| 20 | #define DEBUG_RESAMPLING 0 |
| 21 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 22 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 23 | #include <cutils/log.h> |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 24 | #include <cutils/properties.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <fcntl.h> |
Mathias Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 27 | #include <androidfw/InputTransport.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/socket.h> |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 31 | #include <math.h> |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 32 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
Jeff Brown | d1c48a0 | 2012-02-06 19:12:47 -0800 | [diff] [blame] | 36 | // 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. |
| 40 | static const size_t SOCKET_BUFFER_SIZE = 32 * 1024; |
| 41 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 42 | // Nanoseconds per milliseconds. |
| 43 | static 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 Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 47 | static const nsecs_t RESAMPLE_LATENCY = 5 * NANOS_PER_MS; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 48 | |
| 49 | // Minimum time difference between consecutive samples before attempting to resample. |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 50 | static const nsecs_t RESAMPLE_MIN_DELTA = 2 * NANOS_PER_MS; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 51 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 52 | // 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. |
| 54 | static const nsecs_t RESAMPLE_MAX_PREDICTION = 8 * NANOS_PER_MS; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 55 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 56 | template<typename T> |
| 57 | inline static T min(const T& a, const T& b) { |
| 58 | return a < b ? a : b; |
| 59 | } |
| 60 | |
| 61 | inline static float lerp(float a, float b, float alpha) { |
| 62 | return a + alpha * (b - a); |
| 63 | } |
Jeff Brown | d1c48a0 | 2012-02-06 19:12:47 -0800 | [diff] [blame] | 64 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 65 | // --- InputMessage --- |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 66 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 67 | bool 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 81 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 82 | size_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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 93 | |
| 94 | |
| 95 | // --- InputChannel --- |
| 96 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 97 | InputChannel::InputChannel(const String8& name, int fd) : |
| 98 | mName(name), mFd(fd) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 99 | #if DEBUG_CHANNEL_LIFECYCLE |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 100 | ALOGD("Input channel constructed: name='%s', fd=%d", |
| 101 | mName.string(), fd); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 102 | #endif |
| 103 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 104 | int result = fcntl(mFd, F_SETFL, O_NONBLOCK); |
| 105 | LOG_ALWAYS_FATAL_IF(result != 0, "channel '%s' ~ Could not make socket " |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 106 | "non-blocking. errno=%d", mName.string(), errno); |
| 107 | } |
| 108 | |
| 109 | InputChannel::~InputChannel() { |
| 110 | #if DEBUG_CHANNEL_LIFECYCLE |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 111 | ALOGD("Input channel destroyed: name='%s', fd=%d", |
| 112 | mName.string(), mFd); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 113 | #endif |
| 114 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 115 | ::close(mFd); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | status_t InputChannel::openInputChannelPair(const String8& name, |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 119 | sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 120 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 124 | name.string(), errno); |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 125 | outServerChannel.clear(); |
| 126 | outClientChannel.clear(); |
| 127 | return result; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jeff Brown | d1c48a0 | 2012-02-06 19:12:47 -0800 | [diff] [blame] | 130 | 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 Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 136 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 146 | status_t InputChannel::sendMessage(const InputMessage* msg) { |
| 147 | size_t msgLength = msg->size(); |
Jeff Brown | 7dae0e4 | 2010-09-16 17:04:52 -0700 | [diff] [blame] | 148 | ssize_t nWrite; |
| 149 | do { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 150 | nWrite = ::send(mFd, msg, msgLength, MSG_DONTWAIT | MSG_NOSIGNAL); |
Jeff Brown | 7dae0e4 | 2010-09-16 17:04:52 -0700 | [diff] [blame] | 151 | } while (nWrite == -1 && errno == EINTR); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 152 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 153 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 158 | #endif |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 159 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 168 | 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 Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 172 | #endif |
| 173 | return DEAD_OBJECT; |
| 174 | } |
| 175 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 176 | #if DEBUG_CHANNEL_MESSAGES |
| 177 | ALOGD("channel '%s' ~ sent message of type %d", mName.string(), msg->header.type); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 178 | #endif |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 179 | return OK; |
| 180 | } |
| 181 | |
| 182 | status_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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 202 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 205 | #endif |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 206 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Jeff Brown | 1951ce8 | 2013-04-04 22:45:12 -0700 | [diff] [blame^] | 222 | sp<InputChannel> InputChannel::dup() const { |
| 223 | int fd = ::dup(getFd()); |
| 224 | return fd >= 0 ? new InputChannel(getName(), fd) : NULL; |
| 225 | } |
| 226 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 227 | |
| 228 | // --- InputPublisher --- |
| 229 | |
| 230 | InputPublisher::InputPublisher(const sp<InputChannel>& channel) : |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 231 | mChannel(channel) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | InputPublisher::~InputPublisher() { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | status_t InputPublisher::publishKeyEvent( |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 238 | uint32_t seq, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 239 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 240 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 241 | 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 Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 250 | ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, " |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 251 | "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d," |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 252 | "downTime=%lld, eventTime=%lld", |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 253 | mChannel->getName().string(), seq, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 254 | deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 255 | downTime, eventTime); |
| 256 | #endif |
| 257 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 258 | if (!seq) { |
| 259 | ALOGE("Attempted to publish a key event with sequence number 0."); |
| 260 | return BAD_VALUE; |
| 261 | } |
| 262 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 263 | InputMessage msg; |
| 264 | msg.header.type = InputMessage::TYPE_KEY; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 265 | msg.body.key.seq = seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 266 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | status_t InputPublisher::publishMotionEvent( |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 280 | uint32_t seq, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 281 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 282 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 283 | int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 284 | int32_t flags, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 285 | int32_t edgeFlags, |
| 286 | int32_t metaState, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 287 | int32_t buttonState, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 288 | float xOffset, |
| 289 | float yOffset, |
| 290 | float xPrecision, |
| 291 | float yPrecision, |
| 292 | nsecs_t downTime, |
| 293 | nsecs_t eventTime, |
| 294 | size_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 295 | const PointerProperties* pointerProperties, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 296 | const PointerCoords* pointerCoords) { |
| 297 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 298 | ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 299 | "action=0x%x, flags=0x%x, edgeFlags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 300 | "xOffset=%f, yOffset=%f, " |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 301 | "xPrecision=%f, yPrecision=%f, downTime=%lld, eventTime=%lld, " |
| 302 | "pointerCount=%d", |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 303 | mChannel->getName().string(), seq, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 304 | deviceId, source, action, flags, edgeFlags, metaState, buttonState, |
| 305 | xOffset, yOffset, xPrecision, yPrecision, downTime, eventTime, pointerCount); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 306 | #endif |
| 307 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 308 | if (!seq) { |
| 309 | ALOGE("Attempted to publish a motion event with sequence number 0."); |
| 310 | return BAD_VALUE; |
| 311 | } |
| 312 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 313 | if (pointerCount > MAX_POINTERS || pointerCount < 1) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 314 | ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %d.", |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 315 | mChannel->getName().string(), pointerCount); |
| 316 | return BAD_VALUE; |
| 317 | } |
| 318 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 319 | InputMessage msg; |
| 320 | msg.header.type = InputMessage::TYPE_MOTION; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 321 | msg.body.motion.seq = seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 322 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 336 | for (size_t i = 0; i < pointerCount; i++) { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 337 | msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]); |
| 338 | msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 339 | } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 340 | return mChannel->sendMessage(&msg); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 343 | status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandled) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 344 | #if DEBUG_TRANSPORT_ACTIONS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 345 | ALOGD("channel '%s' publisher ~ receiveFinishedSignal", |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 346 | mChannel->getName().string()); |
| 347 | #endif |
| 348 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 349 | InputMessage msg; |
| 350 | status_t result = mChannel->receiveMessage(&msg); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 351 | if (result) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 352 | *outSeq = 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 353 | *outHandled = false; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 354 | return result; |
| 355 | } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 356 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 359 | return UNKNOWN_ERROR; |
| 360 | } |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 361 | *outSeq = msg.body.finished.seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 362 | *outHandled = msg.body.finished.handled; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 363 | return OK; |
| 364 | } |
| 365 | |
| 366 | // --- InputConsumer --- |
| 367 | |
| 368 | InputConsumer::InputConsumer(const sp<InputChannel>& channel) : |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 369 | mResampleTouch(isTouchResamplingEnabled()), |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 370 | mChannel(channel), mMsgDeferred(false) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | InputConsumer::~InputConsumer() { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 376 | bool 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 Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 391 | status_t InputConsumer::consume(InputEventFactoryInterface* factory, |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 392 | bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 393 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 394 | ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%lld", |
| 395 | mChannel->getName().string(), consumeBatches ? "true" : "false", frameTime); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 396 | #endif |
| 397 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 398 | *outSeq = 0; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 399 | *outEvent = NULL; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 400 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 401 | // Fetch the next input message. |
| 402 | // Loop until an event can be returned or no additional events are received. |
| 403 | while (!*outEvent) { |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 404 | 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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 413 | if (consumeBatches || result != WOULD_BLOCK) { |
| 414 | result = consumeBatch(factory, frameTime, outSeq, outEvent); |
| 415 | if (*outEvent) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 416 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 417 | ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u", |
| 418 | mChannel->getName().string(), *outSeq); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 419 | #endif |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 420 | break; |
| 421 | } |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 422 | } |
| 423 | return result; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 424 | } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 425 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 426 | |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 427 | switch (mMsg.header.type) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 428 | case InputMessage::TYPE_KEY: { |
| 429 | KeyEvent* keyEvent = factory->createKeyEvent(); |
| 430 | if (!keyEvent) return NO_MEMORY; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 431 | |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 432 | initializeKeyEvent(keyEvent, &mMsg); |
| 433 | *outSeq = mMsg.body.key.seq; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 434 | *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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 441 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 442 | case AINPUT_EVENT_TYPE_MOTION: { |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 443 | ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 444 | if (batchIndex >= 0) { |
| 445 | Batch& batch = mBatches.editItemAt(batchIndex); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 446 | if (canAddSample(batch, &mMsg)) { |
| 447 | batch.samples.push(mMsg); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 448 | #if DEBUG_TRANSPORT_ACTIONS |
| 449 | ALOGD("channel '%s' consumer ~ appended to batch event", |
| 450 | mChannel->getName().string()); |
| 451 | #endif |
| 452 | break; |
| 453 | } else { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 454 | // We cannot append to the batch in progress, so we need to consume |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 455 | // the previous batch right now and defer the new message until later. |
| 456 | mMsgDeferred = true; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 457 | status_t result = consumeSamples(factory, |
| 458 | batch, batch.samples.size(), outSeq, outEvent); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 459 | mBatches.removeAt(batchIndex); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 460 | if (result) { |
| 461 | return result; |
| 462 | } |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 463 | #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 Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 473 | if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE |
| 474 | || mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 475 | mBatches.push(); |
| 476 | Batch& batch = mBatches.editTop(); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 477 | batch.samples.push(mMsg); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 478 | #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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 488 | updateTouchState(&mMsg); |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 489 | initializeMotionEvent(motionEvent, &mMsg); |
| 490 | *outSeq = mMsg.body.motion.seq; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 491 | *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 Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 501 | mChannel->getName().string(), mMsg.header.type); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 502 | return UNKNOWN_ERROR; |
| 503 | } |
| 504 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 505 | return OK; |
| 506 | } |
| 507 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 508 | status_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 | |
| 543 | status_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 | |
| 570 | void InputConsumer::updateTouchState(InputMessage* msg) { |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 571 | if (!mResampleTouch || |
| 572 | !(msg->body.motion.source & AINPUT_SOURCE_CLASS_POINTER)) { |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 573 | return; |
| 574 | } |
| 575 | |
| 576 | int32_t deviceId = msg->body.motion.deviceId; |
| 577 | int32_t source = msg->body.motion.source; |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 578 | nsecs_t eventTime = msg->body.motion.eventTime; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 579 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 580 | // 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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 584 | 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 Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 601 | 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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 635 | } |
| 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 Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 643 | const TouchState& touchState = mTouchStates.itemAt(index); |
| 644 | rewriteMessage(touchState, msg); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 645 | mTouchStates.removeAt(index); |
| 646 | } |
| 647 | break; |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 652 | void 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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 671 | void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event, |
| 672 | const InputMessage* next) { |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 673 | if (!mResampleTouch |
| 674 | || !(event->getSource() & AINPUT_SOURCE_CLASS_POINTER) |
| 675 | || event->getAction() != AMOTION_EVENT_ACTION_MOVE) { |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 676 | 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 Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 695 | // Ensure that the current sample has all of the pointers that need to be reported. |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 696 | const History* current = touchState.getHistory(0); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 697 | size_t pointerCount = event->getPointerCount(); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 698 | 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 Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 706 | } |
| 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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 762 | if (other->idBits.hasBit(id) |
| 763 | && shouldResampleTool(event->getToolType(i))) { |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 764 | 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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 770 | #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 Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 773 | id, resampledCoords.getX(), resampledCoords.getY(), |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 774 | currentCoords.getX(), currentCoords.getY(), |
| 775 | otherCoords.getX(), otherCoords.getY(), |
| 776 | alpha); |
| 777 | #endif |
| 778 | } else { |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 779 | resampledCoords.copyFrom(currentCoords); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 780 | #if DEBUG_RESAMPLING |
| 781 | ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 782 | id, resampledCoords.getX(), resampledCoords.getY(), |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 783 | currentCoords.getX(), currentCoords.getY()); |
| 784 | #endif |
| 785 | } |
| 786 | } |
| 787 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 788 | event->addSample(sampleTime, touchState.lastResample.pointers); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | bool InputConsumer::shouldResampleTool(int32_t toolType) { |
| 792 | return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 793 | || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 794 | } |
| 795 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 796 | status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 797 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 798 | ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s", |
| 799 | mChannel->getName().string(), seq, handled ? "true" : "false"); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 800 | #endif |
| 801 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 802 | if (!seq) { |
| 803 | ALOGE("Attempted to send a finished signal with sequence number 0."); |
| 804 | return BAD_VALUE; |
| 805 | } |
| 806 | |
Jeff Brown | 2d34e0c | 2012-02-13 13:18:09 -0800 | [diff] [blame] | 807 | // 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 | |
| 841 | status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 842 | InputMessage msg; |
| 843 | msg.header.type = InputMessage::TYPE_FINISHED; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 844 | msg.body.finished.seq = seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 845 | msg.body.finished.handled = handled; |
| 846 | return mChannel->sendMessage(&msg); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Jeff Brown | 2b6c32c | 2012-03-13 15:00:09 -0700 | [diff] [blame] | 849 | bool InputConsumer::hasDeferredEvent() const { |
| 850 | return mMsgDeferred; |
| 851 | } |
| 852 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 853 | bool InputConsumer::hasPendingBatch() const { |
| 854 | return !mBatches.isEmpty(); |
| 855 | } |
| 856 | |
| 857 | ssize_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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 860 | 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 | |
| 868 | ssize_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 Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 872 | return i; |
| 873 | } |
| 874 | } |
| 875 | return -1; |
| 876 | } |
| 877 | |
| 878 | void 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 | |
| 892 | void 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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 920 | void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 921 | 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 Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 931 | bool 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 | |
| 947 | ssize_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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 957 | } // namespace android |