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