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