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