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