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