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