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