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