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