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