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