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