Jeff Brown | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 22 | |
Jeff Brown | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | |
| 223 | // --- InputPublisher --- |
| 224 | |
| 225 | InputPublisher::InputPublisher(const sp<InputChannel>& channel) : |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 226 | mChannel(channel) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | InputPublisher::~InputPublisher() { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | status_t InputPublisher::publishKeyEvent( |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 233 | uint32_t seq, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 234 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 235 | int32_t source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 236 | int32_t action, |
| 237 | int32_t flags, |
| 238 | int32_t keyCode, |
| 239 | int32_t scanCode, |
| 240 | int32_t metaState, |
| 241 | int32_t repeatCount, |
| 242 | nsecs_t downTime, |
| 243 | nsecs_t eventTime) { |
| 244 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 245 | ALOGD("channel '%s' publisher ~ publishKeyEvent: seq=%u, deviceId=%d, source=0x%x, " |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 246 | "action=0x%x, flags=0x%x, keyCode=%d, scanCode=%d, metaState=0x%x, repeatCount=%d," |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 247 | "downTime=%lld, eventTime=%lld", |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 248 | mChannel->getName().string(), seq, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 249 | deviceId, source, action, flags, keyCode, scanCode, metaState, repeatCount, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 250 | downTime, eventTime); |
| 251 | #endif |
| 252 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 253 | if (!seq) { |
| 254 | ALOGE("Attempted to publish a key event with sequence number 0."); |
| 255 | return BAD_VALUE; |
| 256 | } |
| 257 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 258 | InputMessage msg; |
| 259 | msg.header.type = InputMessage::TYPE_KEY; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 260 | msg.body.key.seq = seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 261 | msg.body.key.deviceId = deviceId; |
| 262 | msg.body.key.source = source; |
| 263 | msg.body.key.action = action; |
| 264 | msg.body.key.flags = flags; |
| 265 | msg.body.key.keyCode = keyCode; |
| 266 | msg.body.key.scanCode = scanCode; |
| 267 | msg.body.key.metaState = metaState; |
| 268 | msg.body.key.repeatCount = repeatCount; |
| 269 | msg.body.key.downTime = downTime; |
| 270 | msg.body.key.eventTime = eventTime; |
| 271 | return mChannel->sendMessage(&msg); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | status_t InputPublisher::publishMotionEvent( |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 275 | uint32_t seq, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 276 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 277 | int32_t source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 278 | int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 279 | int32_t flags, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 280 | int32_t edgeFlags, |
| 281 | int32_t metaState, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 282 | int32_t buttonState, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 283 | float xOffset, |
| 284 | float yOffset, |
| 285 | float xPrecision, |
| 286 | float yPrecision, |
| 287 | nsecs_t downTime, |
| 288 | nsecs_t eventTime, |
| 289 | size_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 290 | const PointerProperties* pointerProperties, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 291 | const PointerCoords* pointerCoords) { |
| 292 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 293 | ALOGD("channel '%s' publisher ~ publishMotionEvent: seq=%u, deviceId=%d, source=0x%x, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 294 | "action=0x%x, flags=0x%x, edgeFlags=0x%x, metaState=0x%x, buttonState=0x%x, " |
| 295 | "xOffset=%f, yOffset=%f, " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 296 | "xPrecision=%f, yPrecision=%f, downTime=%lld, eventTime=%lld, " |
| 297 | "pointerCount=%d", |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 298 | mChannel->getName().string(), seq, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 299 | deviceId, source, action, flags, edgeFlags, metaState, buttonState, |
| 300 | xOffset, yOffset, xPrecision, yPrecision, downTime, eventTime, pointerCount); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 301 | #endif |
| 302 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 303 | if (!seq) { |
| 304 | ALOGE("Attempted to publish a motion event with sequence number 0."); |
| 305 | return BAD_VALUE; |
| 306 | } |
| 307 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 308 | if (pointerCount > MAX_POINTERS || pointerCount < 1) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 309 | ALOGE("channel '%s' publisher ~ Invalid number of pointers provided: %d.", |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 310 | mChannel->getName().string(), pointerCount); |
| 311 | return BAD_VALUE; |
| 312 | } |
| 313 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 314 | InputMessage msg; |
| 315 | msg.header.type = InputMessage::TYPE_MOTION; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 316 | msg.body.motion.seq = seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 317 | msg.body.motion.deviceId = deviceId; |
| 318 | msg.body.motion.source = source; |
| 319 | msg.body.motion.action = action; |
| 320 | msg.body.motion.flags = flags; |
| 321 | msg.body.motion.edgeFlags = edgeFlags; |
| 322 | msg.body.motion.metaState = metaState; |
| 323 | msg.body.motion.buttonState = buttonState; |
| 324 | msg.body.motion.xOffset = xOffset; |
| 325 | msg.body.motion.yOffset = yOffset; |
| 326 | msg.body.motion.xPrecision = xPrecision; |
| 327 | msg.body.motion.yPrecision = yPrecision; |
| 328 | msg.body.motion.downTime = downTime; |
| 329 | msg.body.motion.eventTime = eventTime; |
| 330 | msg.body.motion.pointerCount = pointerCount; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 331 | for (size_t i = 0; i < pointerCount; i++) { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 332 | msg.body.motion.pointers[i].properties.copyFrom(pointerProperties[i]); |
| 333 | msg.body.motion.pointers[i].coords.copyFrom(pointerCoords[i]); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 334 | } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 335 | return mChannel->sendMessage(&msg); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 338 | status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandled) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 339 | #if DEBUG_TRANSPORT_ACTIONS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 340 | ALOGD("channel '%s' publisher ~ receiveFinishedSignal", |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 341 | mChannel->getName().string()); |
| 342 | #endif |
| 343 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 344 | InputMessage msg; |
| 345 | status_t result = mChannel->receiveMessage(&msg); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 346 | if (result) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 347 | *outSeq = 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 348 | *outHandled = false; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 349 | return result; |
| 350 | } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 351 | if (msg.header.type != InputMessage::TYPE_FINISHED) { |
| 352 | ALOGE("channel '%s' publisher ~ Received unexpected message of type %d from consumer", |
| 353 | mChannel->getName().string(), msg.header.type); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 354 | return UNKNOWN_ERROR; |
| 355 | } |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 356 | *outSeq = msg.body.finished.seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 357 | *outHandled = msg.body.finished.handled; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 358 | return OK; |
| 359 | } |
| 360 | |
| 361 | // --- InputConsumer --- |
| 362 | |
| 363 | InputConsumer::InputConsumer(const sp<InputChannel>& channel) : |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 364 | mResampleTouch(isTouchResamplingEnabled()), |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 365 | mChannel(channel), mMsgDeferred(false) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | InputConsumer::~InputConsumer() { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 371 | bool InputConsumer::isTouchResamplingEnabled() { |
| 372 | char value[PROPERTY_VALUE_MAX]; |
| 373 | int length = property_get("debug.inputconsumer.resample", value, NULL); |
| 374 | if (length > 0) { |
| 375 | if (!strcmp("0", value)) { |
| 376 | return false; |
| 377 | } |
| 378 | if (strcmp("1", value)) { |
| 379 | ALOGD("Unrecognized property value for 'debug.inputconsumer.resample'. " |
| 380 | "Use '1' or '0'."); |
| 381 | } |
| 382 | } |
| 383 | return true; |
| 384 | } |
| 385 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 386 | status_t InputConsumer::consume(InputEventFactoryInterface* factory, |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 387 | bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 388 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 389 | ALOGD("channel '%s' consumer ~ consume: consumeBatches=%s, frameTime=%lld", |
| 390 | mChannel->getName().string(), consumeBatches ? "true" : "false", frameTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 391 | #endif |
| 392 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 393 | *outSeq = 0; |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 394 | *outEvent = NULL; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 395 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 396 | // Fetch the next input message. |
| 397 | // Loop until an event can be returned or no additional events are received. |
| 398 | while (!*outEvent) { |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 399 | if (mMsgDeferred) { |
| 400 | // mMsg contains a valid input message from the previous call to consume |
| 401 | // that has not yet been processed. |
| 402 | mMsgDeferred = false; |
| 403 | } else { |
| 404 | // Receive a fresh message. |
| 405 | status_t result = mChannel->receiveMessage(&mMsg); |
| 406 | if (result) { |
| 407 | // Consume the next batched event unless batches are being held for later. |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 408 | if (consumeBatches || result != WOULD_BLOCK) { |
| 409 | result = consumeBatch(factory, frameTime, outSeq, outEvent); |
| 410 | if (*outEvent) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 411 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 412 | ALOGD("channel '%s' consumer ~ consumed batch event, seq=%u", |
| 413 | mChannel->getName().string(), *outSeq); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 414 | #endif |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 415 | break; |
| 416 | } |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 417 | } |
| 418 | return result; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 419 | } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 420 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 421 | |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 422 | switch (mMsg.header.type) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 423 | case InputMessage::TYPE_KEY: { |
| 424 | KeyEvent* keyEvent = factory->createKeyEvent(); |
| 425 | if (!keyEvent) return NO_MEMORY; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 426 | |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 427 | initializeKeyEvent(keyEvent, &mMsg); |
| 428 | *outSeq = mMsg.body.key.seq; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 429 | *outEvent = keyEvent; |
| 430 | #if DEBUG_TRANSPORT_ACTIONS |
| 431 | ALOGD("channel '%s' consumer ~ consumed key event, seq=%u", |
| 432 | mChannel->getName().string(), *outSeq); |
| 433 | #endif |
| 434 | break; |
| 435 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 436 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 437 | case AINPUT_EVENT_TYPE_MOTION: { |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 438 | ssize_t batchIndex = findBatch(mMsg.body.motion.deviceId, mMsg.body.motion.source); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 439 | if (batchIndex >= 0) { |
| 440 | Batch& batch = mBatches.editItemAt(batchIndex); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 441 | if (canAddSample(batch, &mMsg)) { |
| 442 | batch.samples.push(mMsg); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 443 | #if DEBUG_TRANSPORT_ACTIONS |
| 444 | ALOGD("channel '%s' consumer ~ appended to batch event", |
| 445 | mChannel->getName().string()); |
| 446 | #endif |
| 447 | break; |
| 448 | } else { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 449 | // 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] | 450 | // the previous batch right now and defer the new message until later. |
| 451 | mMsgDeferred = true; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 452 | status_t result = consumeSamples(factory, |
| 453 | batch, batch.samples.size(), outSeq, outEvent); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 454 | mBatches.removeAt(batchIndex); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 455 | if (result) { |
| 456 | return result; |
| 457 | } |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 458 | #if DEBUG_TRANSPORT_ACTIONS |
| 459 | ALOGD("channel '%s' consumer ~ consumed batch event and " |
| 460 | "deferred current event, seq=%u", |
| 461 | mChannel->getName().string(), *outSeq); |
| 462 | #endif |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // Start a new batch if needed. |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 468 | if (mMsg.body.motion.action == AMOTION_EVENT_ACTION_MOVE |
| 469 | || mMsg.body.motion.action == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 470 | mBatches.push(); |
| 471 | Batch& batch = mBatches.editTop(); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 472 | batch.samples.push(mMsg); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 473 | #if DEBUG_TRANSPORT_ACTIONS |
| 474 | ALOGD("channel '%s' consumer ~ started batch event", |
| 475 | mChannel->getName().string()); |
| 476 | #endif |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 481 | if (! motionEvent) return NO_MEMORY; |
| 482 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 483 | updateTouchState(&mMsg); |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 484 | initializeMotionEvent(motionEvent, &mMsg); |
| 485 | *outSeq = mMsg.body.motion.seq; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 486 | *outEvent = motionEvent; |
| 487 | #if DEBUG_TRANSPORT_ACTIONS |
| 488 | ALOGD("channel '%s' consumer ~ consumed motion event, seq=%u", |
| 489 | mChannel->getName().string(), *outSeq); |
| 490 | #endif |
| 491 | break; |
| 492 | } |
| 493 | |
| 494 | default: |
| 495 | ALOGE("channel '%s' consumer ~ Received unexpected message of type %d", |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 496 | mChannel->getName().string(), mMsg.header.type); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 497 | return UNKNOWN_ERROR; |
| 498 | } |
| 499 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 500 | return OK; |
| 501 | } |
| 502 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 503 | status_t InputConsumer::consumeBatch(InputEventFactoryInterface* factory, |
| 504 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent) { |
| 505 | status_t result; |
| 506 | for (size_t i = mBatches.size(); i-- > 0; ) { |
| 507 | Batch& batch = mBatches.editItemAt(i); |
| 508 | if (frameTime < 0) { |
| 509 | result = consumeSamples(factory, batch, batch.samples.size(), |
| 510 | outSeq, outEvent); |
| 511 | mBatches.removeAt(i); |
| 512 | return result; |
| 513 | } |
| 514 | |
| 515 | nsecs_t sampleTime = frameTime - RESAMPLE_LATENCY; |
| 516 | ssize_t split = findSampleNoLaterThan(batch, sampleTime); |
| 517 | if (split < 0) { |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | result = consumeSamples(factory, batch, split + 1, outSeq, outEvent); |
| 522 | const InputMessage* next; |
| 523 | if (batch.samples.isEmpty()) { |
| 524 | mBatches.removeAt(i); |
| 525 | next = NULL; |
| 526 | } else { |
| 527 | next = &batch.samples.itemAt(0); |
| 528 | } |
| 529 | if (!result) { |
| 530 | resampleTouchState(sampleTime, static_cast<MotionEvent*>(*outEvent), next); |
| 531 | } |
| 532 | return result; |
| 533 | } |
| 534 | |
| 535 | return WOULD_BLOCK; |
| 536 | } |
| 537 | |
| 538 | status_t InputConsumer::consumeSamples(InputEventFactoryInterface* factory, |
| 539 | Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent) { |
| 540 | MotionEvent* motionEvent = factory->createMotionEvent(); |
| 541 | if (! motionEvent) return NO_MEMORY; |
| 542 | |
| 543 | uint32_t chain = 0; |
| 544 | for (size_t i = 0; i < count; i++) { |
| 545 | InputMessage& msg = batch.samples.editItemAt(i); |
| 546 | updateTouchState(&msg); |
| 547 | if (i) { |
| 548 | SeqChain seqChain; |
| 549 | seqChain.seq = msg.body.motion.seq; |
| 550 | seqChain.chain = chain; |
| 551 | mSeqChains.push(seqChain); |
| 552 | addSample(motionEvent, &msg); |
| 553 | } else { |
| 554 | initializeMotionEvent(motionEvent, &msg); |
| 555 | } |
| 556 | chain = msg.body.motion.seq; |
| 557 | } |
| 558 | batch.samples.removeItemsAt(0, count); |
| 559 | |
| 560 | *outSeq = chain; |
| 561 | *outEvent = motionEvent; |
| 562 | return OK; |
| 563 | } |
| 564 | |
| 565 | void InputConsumer::updateTouchState(InputMessage* msg) { |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 566 | if (!mResampleTouch || |
| 567 | !(msg->body.motion.source & AINPUT_SOURCE_CLASS_POINTER)) { |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 568 | return; |
| 569 | } |
| 570 | |
| 571 | int32_t deviceId = msg->body.motion.deviceId; |
| 572 | int32_t source = msg->body.motion.source; |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 573 | nsecs_t eventTime = msg->body.motion.eventTime; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 574 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 575 | // Update the touch state history to incorporate the new input message. |
| 576 | // If the message is in the past relative to the most recently produced resampled |
| 577 | // touch, then use the resampled time and coordinates instead. |
| 578 | switch (msg->body.motion.action & AMOTION_EVENT_ACTION_MASK) { |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 579 | case AMOTION_EVENT_ACTION_DOWN: { |
| 580 | ssize_t index = findTouchState(deviceId, source); |
| 581 | if (index < 0) { |
| 582 | mTouchStates.push(); |
| 583 | index = mTouchStates.size() - 1; |
| 584 | } |
| 585 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 586 | touchState.initialize(deviceId, source); |
| 587 | touchState.addHistory(msg); |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | case AMOTION_EVENT_ACTION_MOVE: { |
| 592 | ssize_t index = findTouchState(deviceId, source); |
| 593 | if (index >= 0) { |
| 594 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 595 | touchState.addHistory(msg); |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 596 | if (eventTime < touchState.lastResample.eventTime) { |
| 597 | rewriteMessage(touchState, msg); |
| 598 | } else { |
| 599 | touchState.lastResample.idBits.clear(); |
| 600 | } |
| 601 | } |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | case AMOTION_EVENT_ACTION_POINTER_DOWN: { |
| 606 | ssize_t index = findTouchState(deviceId, source); |
| 607 | if (index >= 0) { |
| 608 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 609 | touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId()); |
| 610 | rewriteMessage(touchState, msg); |
| 611 | } |
| 612 | break; |
| 613 | } |
| 614 | |
| 615 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 616 | ssize_t index = findTouchState(deviceId, source); |
| 617 | if (index >= 0) { |
| 618 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 619 | rewriteMessage(touchState, msg); |
| 620 | touchState.lastResample.idBits.clearBit(msg->body.motion.getActionId()); |
| 621 | } |
| 622 | break; |
| 623 | } |
| 624 | |
| 625 | case AMOTION_EVENT_ACTION_SCROLL: { |
| 626 | ssize_t index = findTouchState(deviceId, source); |
| 627 | if (index >= 0) { |
| 628 | const TouchState& touchState = mTouchStates.itemAt(index); |
| 629 | rewriteMessage(touchState, msg); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 630 | } |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | case AMOTION_EVENT_ACTION_UP: |
| 635 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 636 | ssize_t index = findTouchState(deviceId, source); |
| 637 | if (index >= 0) { |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 638 | const TouchState& touchState = mTouchStates.itemAt(index); |
| 639 | rewriteMessage(touchState, msg); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 640 | mTouchStates.removeAt(index); |
| 641 | } |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 647 | void InputConsumer::rewriteMessage(const TouchState& state, InputMessage* msg) { |
| 648 | for (size_t i = 0; i < msg->body.motion.pointerCount; i++) { |
| 649 | uint32_t id = msg->body.motion.pointers[i].properties.id; |
| 650 | if (state.lastResample.idBits.hasBit(id)) { |
| 651 | PointerCoords& msgCoords = msg->body.motion.pointers[i].coords; |
| 652 | const PointerCoords& resampleCoords = state.lastResample.getPointerById(id); |
| 653 | #if DEBUG_RESAMPLING |
| 654 | ALOGD("[%d] - rewrite (%0.3f, %0.3f), old (%0.3f, %0.3f)", id, |
| 655 | resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_X), |
| 656 | resampleCoords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 657 | msgCoords.getAxisValue(AMOTION_EVENT_AXIS_X), |
| 658 | msgCoords.getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 659 | #endif |
| 660 | msgCoords.setAxisValue(AMOTION_EVENT_AXIS_X, resampleCoords.getX()); |
| 661 | msgCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, resampleCoords.getY()); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 666 | void InputConsumer::resampleTouchState(nsecs_t sampleTime, MotionEvent* event, |
| 667 | const InputMessage* next) { |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 668 | if (!mResampleTouch |
| 669 | || !(event->getSource() & AINPUT_SOURCE_CLASS_POINTER) |
| 670 | || event->getAction() != AMOTION_EVENT_ACTION_MOVE) { |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 671 | return; |
| 672 | } |
| 673 | |
| 674 | ssize_t index = findTouchState(event->getDeviceId(), event->getSource()); |
| 675 | if (index < 0) { |
| 676 | #if DEBUG_RESAMPLING |
| 677 | ALOGD("Not resampled, no touch state for device."); |
| 678 | #endif |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | TouchState& touchState = mTouchStates.editItemAt(index); |
| 683 | if (touchState.historySize < 1) { |
| 684 | #if DEBUG_RESAMPLING |
| 685 | ALOGD("Not resampled, no history for device."); |
| 686 | #endif |
| 687 | return; |
| 688 | } |
| 689 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 690 | // 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] | 691 | const History* current = touchState.getHistory(0); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 692 | size_t pointerCount = event->getPointerCount(); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 693 | for (size_t i = 0; i < pointerCount; i++) { |
| 694 | uint32_t id = event->getPointerId(i); |
| 695 | if (!current->idBits.hasBit(id)) { |
| 696 | #if DEBUG_RESAMPLING |
| 697 | ALOGD("Not resampled, missing id %d", id); |
| 698 | #endif |
| 699 | return; |
| 700 | } |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | // Find the data to use for resampling. |
| 704 | const History* other; |
| 705 | History future; |
| 706 | float alpha; |
| 707 | if (next) { |
| 708 | // Interpolate between current sample and future sample. |
| 709 | // So current->eventTime <= sampleTime <= future.eventTime. |
| 710 | future.initializeFrom(next); |
| 711 | other = &future; |
| 712 | nsecs_t delta = future.eventTime - current->eventTime; |
| 713 | if (delta < RESAMPLE_MIN_DELTA) { |
| 714 | #if DEBUG_RESAMPLING |
| 715 | ALOGD("Not resampled, delta time is %lld ns.", delta); |
| 716 | #endif |
| 717 | return; |
| 718 | } |
| 719 | alpha = float(sampleTime - current->eventTime) / delta; |
| 720 | } else if (touchState.historySize >= 2) { |
| 721 | // Extrapolate future sample using current sample and past sample. |
| 722 | // So other->eventTime <= current->eventTime <= sampleTime. |
| 723 | other = touchState.getHistory(1); |
| 724 | nsecs_t delta = current->eventTime - other->eventTime; |
| 725 | if (delta < RESAMPLE_MIN_DELTA) { |
| 726 | #if DEBUG_RESAMPLING |
| 727 | ALOGD("Not resampled, delta time is %lld ns.", delta); |
| 728 | #endif |
| 729 | return; |
| 730 | } |
| 731 | nsecs_t maxPredict = current->eventTime + min(delta / 2, RESAMPLE_MAX_PREDICTION); |
| 732 | if (sampleTime > maxPredict) { |
| 733 | #if DEBUG_RESAMPLING |
| 734 | ALOGD("Sample time is too far in the future, adjusting prediction " |
| 735 | "from %lld to %lld ns.", |
| 736 | sampleTime - current->eventTime, maxPredict - current->eventTime); |
| 737 | #endif |
| 738 | sampleTime = maxPredict; |
| 739 | } |
| 740 | alpha = float(current->eventTime - sampleTime) / delta; |
| 741 | } else { |
| 742 | #if DEBUG_RESAMPLING |
| 743 | ALOGD("Not resampled, insufficient data."); |
| 744 | #endif |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | // Resample touch coordinates. |
| 749 | touchState.lastResample.eventTime = sampleTime; |
| 750 | touchState.lastResample.idBits.clear(); |
| 751 | for (size_t i = 0; i < pointerCount; i++) { |
| 752 | uint32_t id = event->getPointerId(i); |
| 753 | touchState.lastResample.idToIndex[id] = i; |
| 754 | touchState.lastResample.idBits.markBit(id); |
| 755 | PointerCoords& resampledCoords = touchState.lastResample.pointers[i]; |
| 756 | const PointerCoords& currentCoords = current->getPointerById(id); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 757 | if (other->idBits.hasBit(id) |
| 758 | && shouldResampleTool(event->getToolType(i))) { |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 759 | const PointerCoords& otherCoords = other->getPointerById(id); |
| 760 | resampledCoords.copyFrom(currentCoords); |
| 761 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_X, |
| 762 | lerp(currentCoords.getX(), otherCoords.getX(), alpha)); |
| 763 | resampledCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 764 | lerp(currentCoords.getY(), otherCoords.getY(), alpha)); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 765 | #if DEBUG_RESAMPLING |
| 766 | ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f), " |
| 767 | "other (%0.3f, %0.3f), alpha %0.3f", |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 768 | id, resampledCoords.getX(), resampledCoords.getY(), |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 769 | currentCoords.getX(), currentCoords.getY(), |
| 770 | otherCoords.getX(), otherCoords.getY(), |
| 771 | alpha); |
| 772 | #endif |
| 773 | } else { |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 774 | resampledCoords.copyFrom(currentCoords); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 775 | #if DEBUG_RESAMPLING |
| 776 | ALOGD("[%d] - out (%0.3f, %0.3f), cur (%0.3f, %0.3f)", |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 777 | id, resampledCoords.getX(), resampledCoords.getY(), |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 778 | currentCoords.getX(), currentCoords.getY()); |
| 779 | #endif |
| 780 | } |
| 781 | } |
| 782 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 783 | event->addSample(sampleTime, touchState.lastResample.pointers); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | bool InputConsumer::shouldResampleTool(int32_t toolType) { |
| 787 | return toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 788 | || toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 789 | } |
| 790 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 791 | status_t InputConsumer::sendFinishedSignal(uint32_t seq, bool handled) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 792 | #if DEBUG_TRANSPORT_ACTIONS |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 793 | ALOGD("channel '%s' consumer ~ sendFinishedSignal: seq=%u, handled=%s", |
| 794 | mChannel->getName().string(), seq, handled ? "true" : "false"); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 795 | #endif |
| 796 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 797 | if (!seq) { |
| 798 | ALOGE("Attempted to send a finished signal with sequence number 0."); |
| 799 | return BAD_VALUE; |
| 800 | } |
| 801 | |
Jeff Brown | 2d34e0c | 2012-02-13 13:18:09 -0800 | [diff] [blame] | 802 | // Send finished signals for the batch sequence chain first. |
| 803 | size_t seqChainCount = mSeqChains.size(); |
| 804 | if (seqChainCount) { |
| 805 | uint32_t currentSeq = seq; |
| 806 | uint32_t chainSeqs[seqChainCount]; |
| 807 | size_t chainIndex = 0; |
| 808 | for (size_t i = seqChainCount; i-- > 0; ) { |
| 809 | const SeqChain& seqChain = mSeqChains.itemAt(i); |
| 810 | if (seqChain.seq == currentSeq) { |
| 811 | currentSeq = seqChain.chain; |
| 812 | chainSeqs[chainIndex++] = currentSeq; |
| 813 | mSeqChains.removeAt(i); |
| 814 | } |
| 815 | } |
| 816 | status_t status = OK; |
| 817 | while (!status && chainIndex-- > 0) { |
| 818 | status = sendUnchainedFinishedSignal(chainSeqs[chainIndex], handled); |
| 819 | } |
| 820 | if (status) { |
| 821 | // An error occurred so at least one signal was not sent, reconstruct the chain. |
| 822 | do { |
| 823 | SeqChain seqChain; |
| 824 | seqChain.seq = chainIndex != 0 ? chainSeqs[chainIndex - 1] : seq; |
| 825 | seqChain.chain = chainSeqs[chainIndex]; |
| 826 | mSeqChains.push(seqChain); |
| 827 | } while (chainIndex-- > 0); |
| 828 | return status; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // Send finished signal for the last message in the batch. |
| 833 | return sendUnchainedFinishedSignal(seq, handled); |
| 834 | } |
| 835 | |
| 836 | status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled) { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 837 | InputMessage msg; |
| 838 | msg.header.type = InputMessage::TYPE_FINISHED; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 839 | msg.body.finished.seq = seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 840 | msg.body.finished.handled = handled; |
| 841 | return mChannel->sendMessage(&msg); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Jeff Brown | 2b6c32c | 2012-03-13 15:00:09 -0700 | [diff] [blame] | 844 | bool InputConsumer::hasDeferredEvent() const { |
| 845 | return mMsgDeferred; |
| 846 | } |
| 847 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 848 | bool InputConsumer::hasPendingBatch() const { |
| 849 | return !mBatches.isEmpty(); |
| 850 | } |
| 851 | |
| 852 | ssize_t InputConsumer::findBatch(int32_t deviceId, int32_t source) const { |
| 853 | for (size_t i = 0; i < mBatches.size(); i++) { |
| 854 | const Batch& batch = mBatches.itemAt(i); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 855 | const InputMessage& head = batch.samples.itemAt(0); |
| 856 | if (head.body.motion.deviceId == deviceId && head.body.motion.source == source) { |
| 857 | return i; |
| 858 | } |
| 859 | } |
| 860 | return -1; |
| 861 | } |
| 862 | |
| 863 | ssize_t InputConsumer::findTouchState(int32_t deviceId, int32_t source) const { |
| 864 | for (size_t i = 0; i < mTouchStates.size(); i++) { |
| 865 | const TouchState& touchState = mTouchStates.itemAt(i); |
| 866 | if (touchState.deviceId == deviceId && touchState.source == source) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 867 | return i; |
| 868 | } |
| 869 | } |
| 870 | return -1; |
| 871 | } |
| 872 | |
| 873 | void InputConsumer::initializeKeyEvent(KeyEvent* event, const InputMessage* msg) { |
| 874 | event->initialize( |
| 875 | msg->body.key.deviceId, |
| 876 | msg->body.key.source, |
| 877 | msg->body.key.action, |
| 878 | msg->body.key.flags, |
| 879 | msg->body.key.keyCode, |
| 880 | msg->body.key.scanCode, |
| 881 | msg->body.key.metaState, |
| 882 | msg->body.key.repeatCount, |
| 883 | msg->body.key.downTime, |
| 884 | msg->body.key.eventTime); |
| 885 | } |
| 886 | |
| 887 | void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage* msg) { |
| 888 | size_t pointerCount = msg->body.motion.pointerCount; |
| 889 | PointerProperties pointerProperties[pointerCount]; |
| 890 | PointerCoords pointerCoords[pointerCount]; |
| 891 | for (size_t i = 0; i < pointerCount; i++) { |
| 892 | pointerProperties[i].copyFrom(msg->body.motion.pointers[i].properties); |
| 893 | pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords); |
| 894 | } |
| 895 | |
| 896 | event->initialize( |
| 897 | msg->body.motion.deviceId, |
| 898 | msg->body.motion.source, |
| 899 | msg->body.motion.action, |
| 900 | msg->body.motion.flags, |
| 901 | msg->body.motion.edgeFlags, |
| 902 | msg->body.motion.metaState, |
| 903 | msg->body.motion.buttonState, |
| 904 | msg->body.motion.xOffset, |
| 905 | msg->body.motion.yOffset, |
| 906 | msg->body.motion.xPrecision, |
| 907 | msg->body.motion.yPrecision, |
| 908 | msg->body.motion.downTime, |
| 909 | msg->body.motion.eventTime, |
| 910 | pointerCount, |
| 911 | pointerProperties, |
| 912 | pointerCoords); |
| 913 | } |
| 914 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 915 | void InputConsumer::addSample(MotionEvent* event, const InputMessage* msg) { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 916 | size_t pointerCount = msg->body.motion.pointerCount; |
| 917 | PointerCoords pointerCoords[pointerCount]; |
| 918 | for (size_t i = 0; i < pointerCount; i++) { |
| 919 | pointerCoords[i].copyFrom(msg->body.motion.pointers[i].coords); |
| 920 | } |
| 921 | |
| 922 | event->setMetaState(event->getMetaState() | msg->body.motion.metaState); |
| 923 | event->addSample(msg->body.motion.eventTime, pointerCoords); |
| 924 | } |
| 925 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 926 | bool InputConsumer::canAddSample(const Batch& batch, const InputMessage *msg) { |
| 927 | const InputMessage& head = batch.samples.itemAt(0); |
| 928 | size_t pointerCount = msg->body.motion.pointerCount; |
| 929 | if (head.body.motion.pointerCount != pointerCount |
| 930 | || head.body.motion.action != msg->body.motion.action) { |
| 931 | return false; |
| 932 | } |
| 933 | for (size_t i = 0; i < pointerCount; i++) { |
| 934 | if (head.body.motion.pointers[i].properties |
| 935 | != msg->body.motion.pointers[i].properties) { |
| 936 | return false; |
| 937 | } |
| 938 | } |
| 939 | return true; |
| 940 | } |
| 941 | |
| 942 | ssize_t InputConsumer::findSampleNoLaterThan(const Batch& batch, nsecs_t time) { |
| 943 | size_t numSamples = batch.samples.size(); |
| 944 | size_t index = 0; |
| 945 | while (index < numSamples |
| 946 | && batch.samples.itemAt(index).body.motion.eventTime <= time) { |
| 947 | index += 1; |
| 948 | } |
| 949 | return ssize_t(index) - 1; |
| 950 | } |
| 951 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 952 | } // namespace android |