Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Prabir Pradhan | c08b0db | 2022-09-10 00:57:15 +0000 | [diff] [blame] | 17 | #pragma once |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 18 | |
Robert Carr | 2c358bf | 2018-08-08 15:58:15 -0700 | [diff] [blame] | 19 | #pragma GCC system_header |
| 20 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 21 | /** |
| 22 | * Native input transport. |
| 23 | * |
| 24 | * The InputChannel provides a mechanism for exchanging InputMessage structures across processes. |
| 25 | * |
| 26 | * The InputPublisher and InputConsumer each handle one end-point of an input channel. |
| 27 | * The InputPublisher is used by the input dispatcher to send events to the application. |
| 28 | * The InputConsumer is used by the application to receive events from the input dispatcher. |
| 29 | */ |
| 30 | |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 31 | #include <string> |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 32 | #include <unordered_map> |
Siarhei Vishniakou | ec8f725 | 2018-07-06 11:19:32 +0100 | [diff] [blame] | 33 | |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 34 | #include <android-base/chrono_utils.h> |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 35 | #include <android-base/result.h> |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 36 | #include <android-base/unique_fd.h> |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 37 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 38 | #include <binder/IBinder.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 39 | #include <binder/Parcelable.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 40 | #include <input/Input.h> |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 41 | #include <sys/stat.h> |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 42 | #include <ui/Transform.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 43 | #include <utils/BitSet.h> |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 44 | #include <utils/Errors.h> |
| 45 | #include <utils/RefBase.h> |
| 46 | #include <utils/Timers.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 47 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 48 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 49 | namespace android { |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 50 | class Parcel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * Intermediate representation used to send input events and related signals. |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 54 | * |
| 55 | * Note that this structure is used for IPCs so its layout must be identical |
| 56 | * on 64 and 32 bit processes. This is tested in StructLayout_test.cpp. |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 57 | * |
| 58 | * Since the struct must be aligned to an 8-byte boundary, there could be uninitialized bytes |
| 59 | * in-between the defined fields. This padding data should be explicitly accounted for by adding |
| 60 | * "empty" fields into the struct. This data is memset to zero before sending the struct across |
| 61 | * the socket. Adding the explicit fields ensures that the memset is not optimized away by the |
| 62 | * compiler. When a new field is added to the struct, the corresponding change |
| 63 | * in StructLayout_test should be made. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 64 | */ |
| 65 | struct InputMessage { |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 66 | enum class Type : uint32_t { |
| 67 | KEY, |
| 68 | MOTION, |
| 69 | FINISHED, |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 70 | FOCUS, |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 71 | CAPTURE, |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 72 | DRAG, |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 73 | TIMELINE, |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 74 | TOUCH_MODE, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 75 | |
| 76 | ftl_last = TOUCH_MODE |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | struct Header { |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 80 | Type type; // 4 bytes |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 81 | uint32_t seq; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 82 | } header; |
| 83 | |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 84 | // For keys and motions, rely on the fact that std::array takes up exactly as much space |
| 85 | // as the underlying data. This is not guaranteed by C++, but it simplifies the conversions. |
| 86 | static_assert(sizeof(std::array<uint8_t, 32>) == 32); |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 87 | |
| 88 | // For bool values, rely on the fact that they take up exactly one byte. This is not guaranteed |
| 89 | // by C++ and is implementation-dependent, but it simplifies the conversions. |
| 90 | static_assert(sizeof(bool) == 1); |
| 91 | |
| 92 | // Body *must* be 8 byte aligned. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 93 | union Body { |
| 94 | struct Key { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 95 | int32_t eventId; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 96 | uint32_t empty1; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 97 | nsecs_t eventTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 98 | int32_t deviceId; |
| 99 | int32_t source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 100 | int32_t displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 101 | std::array<uint8_t, 32> hmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 102 | int32_t action; |
| 103 | int32_t flags; |
| 104 | int32_t keyCode; |
| 105 | int32_t scanCode; |
| 106 | int32_t metaState; |
| 107 | int32_t repeatCount; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 108 | uint32_t empty2; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 109 | nsecs_t downTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 110 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 111 | inline size_t size() const { return sizeof(Key); } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 112 | } key; |
| 113 | |
| 114 | struct Motion { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 115 | int32_t eventId; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 116 | uint32_t pointerCount; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 117 | nsecs_t eventTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 118 | int32_t deviceId; |
| 119 | int32_t source; |
Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 120 | int32_t displayId; |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 121 | std::array<uint8_t, 32> hmac; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 122 | int32_t action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 123 | int32_t actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 124 | int32_t flags; |
| 125 | int32_t metaState; |
| 126 | int32_t buttonState; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 127 | MotionClassification classification; // base type: uint8_t |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 128 | uint8_t empty2[3]; // 3 bytes to fill gap created by classification |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 129 | int32_t edgeFlags; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 130 | nsecs_t downTime __attribute__((aligned(8))); |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 131 | float dsdx; // Begin window transform |
| 132 | float dtdx; // |
| 133 | float dtdy; // |
| 134 | float dsdy; // |
| 135 | float tx; // |
| 136 | float ty; // End window transform |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 137 | float xPrecision; |
| 138 | float yPrecision; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 139 | float xCursorPosition; |
| 140 | float yCursorPosition; |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 141 | float dsdxRaw; // Begin raw transform |
| 142 | float dtdxRaw; // |
| 143 | float dtdyRaw; // |
| 144 | float dsdyRaw; // |
| 145 | float txRaw; // |
| 146 | float tyRaw; // End raw transform |
Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 147 | /** |
| 148 | * The "pointers" field must be the last field of the struct InputMessage. |
| 149 | * When we send the struct InputMessage across the socket, we are not |
| 150 | * writing the entire "pointers" array, but only the pointerCount portion |
| 151 | * of it as an optimization. Adding a field after "pointers" would break this. |
| 152 | */ |
Michael Wright | b03f103 | 2015-05-14 16:29:13 +0100 | [diff] [blame] | 153 | struct Pointer { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 154 | PointerProperties properties; |
| 155 | PointerCoords coords; |
Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame] | 156 | } pointers[MAX_POINTERS] __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 157 | |
| 158 | int32_t getActionId() const { |
| 159 | uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) |
| 160 | >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 161 | return pointers[index].properties.id; |
| 162 | } |
| 163 | |
| 164 | inline size_t size() const { |
| 165 | return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS |
| 166 | + sizeof(Pointer) * pointerCount; |
| 167 | } |
| 168 | } motion; |
| 169 | |
| 170 | struct Finished { |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 171 | bool handled; |
| 172 | uint8_t empty[7]; |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 173 | nsecs_t consumeTime; // The time when the event was consumed by the receiving end |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 174 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 175 | inline size_t size() const { return sizeof(Finished); } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 176 | } finished; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 177 | |
| 178 | struct Focus { |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 179 | int32_t eventId; |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 180 | // The following 2 fields take up 4 bytes total |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 181 | bool hasFocus; |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 182 | uint8_t empty[3]; |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 183 | |
| 184 | inline size_t size() const { return sizeof(Focus); } |
| 185 | } focus; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 186 | |
| 187 | struct Capture { |
| 188 | int32_t eventId; |
Siarhei Vishniakou | 38b7f7f | 2021-03-05 01:57:08 +0000 | [diff] [blame] | 189 | bool pointerCaptureEnabled; |
| 190 | uint8_t empty[3]; |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 191 | |
| 192 | inline size_t size() const { return sizeof(Capture); } |
| 193 | } capture; |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 194 | |
| 195 | struct Drag { |
| 196 | int32_t eventId; |
| 197 | float x; |
| 198 | float y; |
| 199 | bool isExiting; |
| 200 | uint8_t empty[3]; |
| 201 | |
| 202 | inline size_t size() const { return sizeof(Drag); } |
| 203 | } drag; |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 204 | |
| 205 | struct Timeline { |
| 206 | int32_t eventId; |
| 207 | uint32_t empty; |
| 208 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 209 | |
| 210 | inline size_t size() const { return sizeof(Timeline); } |
| 211 | } timeline; |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 212 | |
| 213 | struct TouchMode { |
| 214 | int32_t eventId; |
| 215 | // The following 2 fields take up 4 bytes total |
| 216 | bool isInTouchMode; |
| 217 | uint8_t empty[3]; |
| 218 | |
| 219 | inline size_t size() const { return sizeof(TouchMode); } |
| 220 | } touchMode; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 221 | } __attribute__((aligned(8))) body; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 222 | |
| 223 | bool isValid(size_t actualSize) const; |
| 224 | size_t size() const; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 225 | void getSanitizedCopy(InputMessage* msg) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | /* |
| 229 | * An input channel consists of a local unix domain socket used to send and receive |
| 230 | * input messages across processes. Each channel has a descriptive name for debugging purposes. |
| 231 | * |
| 232 | * Each endpoint has its own InputChannel object that specifies its file descriptor. |
| 233 | * |
| 234 | * The input channel is closed when all references to it are released. |
| 235 | */ |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 236 | class InputChannel : public Parcelable { |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 237 | public: |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 238 | static std::unique_ptr<InputChannel> create(const std::string& name, |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 239 | android::base::unique_fd fd, sp<IBinder> token); |
| 240 | InputChannel() = default; |
| 241 | InputChannel(const InputChannel& other) |
| 242 | : mName(other.mName), mFd(::dup(other.mFd)), mToken(other.mToken){}; |
| 243 | InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token); |
Siarhei Vishniakou | ae02a1f | 2021-05-01 23:14:04 +0000 | [diff] [blame] | 244 | ~InputChannel() override; |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 245 | /** |
| 246 | * Create a pair of input channels. |
| 247 | * The two returned input channels are equivalent, and are labeled as "server" and "client" |
| 248 | * for convenience. The two input channels share the same token. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 249 | * |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 250 | * Return OK on success. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 251 | */ |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 252 | static status_t openInputChannelPair(const std::string& name, |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 253 | std::unique_ptr<InputChannel>& outServerChannel, |
| 254 | std::unique_ptr<InputChannel>& outClientChannel); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 255 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 256 | inline std::string getName() const { return mName; } |
| 257 | inline const android::base::unique_fd& getFd() const { return mFd; } |
| 258 | inline sp<IBinder> getToken() const { return mToken; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 259 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 260 | /* Send a message to the other endpoint. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 261 | * |
| 262 | * If the channel is full then the message is guaranteed not to have been sent at all. |
| 263 | * Try again after the consumer has sent a finished signal indicating that it has |
| 264 | * consumed some of the pending messages from the channel. |
| 265 | * |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 266 | * Return OK on success. |
| 267 | * Return WOULD_BLOCK if the channel is full. |
| 268 | * Return DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 269 | * Other errors probably indicate that the channel is broken. |
| 270 | */ |
| 271 | status_t sendMessage(const InputMessage* msg); |
| 272 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 273 | /* Receive a message sent by the other endpoint. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 274 | * |
| 275 | * If there is no message present, try again after poll() indicates that the fd |
| 276 | * is readable. |
| 277 | * |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 278 | * Return OK on success. |
| 279 | * Return WOULD_BLOCK if there is no message present. |
| 280 | * Return DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 281 | * Other errors probably indicate that the channel is broken. |
| 282 | */ |
| 283 | status_t receiveMessage(InputMessage* msg); |
| 284 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 285 | /* Return a new object that has a duplicate of this channel's fd. */ |
Siarhei Vishniakou | d258827 | 2020-07-10 11:15:40 -0500 | [diff] [blame] | 286 | std::unique_ptr<InputChannel> dup() const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 287 | |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 288 | void copyTo(InputChannel& outChannel) const; |
| 289 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 290 | status_t readFromParcel(const android::Parcel* parcel) override; |
| 291 | status_t writeToParcel(android::Parcel* parcel) const override; |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 292 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 293 | /** |
| 294 | * The connection token is used to identify the input connection, i.e. |
| 295 | * the pair of input channels that were created simultaneously. Input channels |
| 296 | * are always created in pairs, and the token can be used to find the server-side |
| 297 | * input channel from the client-side input channel, and vice versa. |
| 298 | * |
| 299 | * Do not use connection token to check equality of a specific input channel object |
| 300 | * to another, because two different (client and server) input channels will share the |
| 301 | * same connection token. |
| 302 | * |
| 303 | * Return the token that identifies this connection. |
| 304 | */ |
| 305 | sp<IBinder> getConnectionToken() const; |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 306 | |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 307 | bool operator==(const InputChannel& inputChannel) const { |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 308 | struct stat lhs, rhs; |
| 309 | if (fstat(mFd.get(), &lhs) != 0) { |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 310 | return false; |
| 311 | } |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 312 | if (fstat(inputChannel.getFd(), &rhs) != 0) { |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 313 | return false; |
| 314 | } |
| 315 | // If file descriptors are pointing to same inode they are duplicated fds. |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 316 | return inputChannel.getName() == getName() && inputChannel.getConnectionToken() == mToken && |
| 317 | lhs.st_ino == rhs.st_ino; |
Chris Ye | 0783e99 | 2020-06-02 21:34:49 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 320 | private: |
Garfield Tan | 1560166 | 2020-09-22 15:32:38 -0700 | [diff] [blame] | 321 | base::unique_fd dupFd() const; |
| 322 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 323 | std::string mName; |
| 324 | android::base::unique_fd mFd; |
| 325 | |
| 326 | sp<IBinder> mToken; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | /* |
| 330 | * Publishes input events to an input channel. |
| 331 | */ |
| 332 | class InputPublisher { |
| 333 | public: |
| 334 | /* Creates a publisher associated with an input channel. */ |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 335 | explicit InputPublisher(const std::shared_ptr<InputChannel>& channel); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 336 | |
| 337 | /* Destroys the publisher and releases its input channel. */ |
| 338 | ~InputPublisher(); |
| 339 | |
| 340 | /* Gets the underlying input channel. */ |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 341 | inline std::shared_ptr<InputChannel> getChannel() { return mChannel; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 342 | |
| 343 | /* Publishes a key event to the input channel. |
| 344 | * |
| 345 | * Returns OK on success. |
| 346 | * Returns WOULD_BLOCK if the channel is full. |
| 347 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 348 | * Returns BAD_VALUE if seq is 0. |
| 349 | * Other errors probably indicate that the channel is broken. |
| 350 | */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 351 | status_t publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, |
| 352 | int32_t displayId, std::array<uint8_t, 32> hmac, int32_t action, |
| 353 | int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, |
Siarhei Vishniakou | 9c858ac | 2020-01-23 14:20:11 -0600 | [diff] [blame] | 354 | int32_t repeatCount, nsecs_t downTime, nsecs_t eventTime); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 355 | |
| 356 | /* Publishes a motion event to the input channel. |
| 357 | * |
| 358 | * Returns OK on success. |
| 359 | * Returns WOULD_BLOCK if the channel is full. |
| 360 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 361 | * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS. |
| 362 | * Other errors probably indicate that the channel is broken. |
| 363 | */ |
Garfield Tan | 1c7bc86 | 2020-01-28 13:24:04 -0800 | [diff] [blame] | 364 | status_t publishMotionEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source, |
| 365 | int32_t displayId, std::array<uint8_t, 32> hmac, int32_t action, |
| 366 | int32_t actionButton, int32_t flags, int32_t edgeFlags, |
| 367 | int32_t metaState, int32_t buttonState, |
chaviw | 9eaa22c | 2020-07-01 16:21:27 -0700 | [diff] [blame] | 368 | MotionClassification classification, const ui::Transform& transform, |
| 369 | float xPrecision, float yPrecision, float xCursorPosition, |
Prabir Pradhan | b9b1850 | 2021-08-26 12:30:32 -0700 | [diff] [blame] | 370 | float yCursorPosition, const ui::Transform& rawTransform, |
| 371 | nsecs_t downTime, nsecs_t eventTime, uint32_t pointerCount, |
Evan Rosky | 84f07f0 | 2021-04-16 10:42:42 -0700 | [diff] [blame] | 372 | const PointerProperties* pointerProperties, |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 373 | const PointerCoords* pointerCoords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 374 | |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 375 | /* Publishes a focus event to the input channel. |
| 376 | * |
| 377 | * Returns OK on success. |
| 378 | * Returns WOULD_BLOCK if the channel is full. |
| 379 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 380 | * Other errors probably indicate that the channel is broken. |
| 381 | */ |
Antonio Kantek | 3cfec7b | 2021-11-05 18:26:17 -0700 | [diff] [blame] | 382 | status_t publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 383 | |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 384 | /* Publishes a capture event to the input channel. |
| 385 | * |
| 386 | * Returns OK on success. |
| 387 | * Returns WOULD_BLOCK if the channel is full. |
| 388 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 389 | * Other errors probably indicate that the channel is broken. |
| 390 | */ |
| 391 | status_t publishCaptureEvent(uint32_t seq, int32_t eventId, bool pointerCaptureEnabled); |
| 392 | |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 393 | /* Publishes a drag event to the input channel. |
| 394 | * |
| 395 | * Returns OK on success. |
| 396 | * Returns WOULD_BLOCK if the channel is full. |
| 397 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 398 | * Other errors probably indicate that the channel is broken. |
| 399 | */ |
| 400 | status_t publishDragEvent(uint32_t seq, int32_t eventId, float x, float y, bool isExiting); |
| 401 | |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 402 | /* Publishes a touch mode event to the input channel. |
| 403 | * |
| 404 | * Returns OK on success. |
| 405 | * Returns WOULD_BLOCK if the channel is full. |
| 406 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 407 | * Other errors probably indicate that the channel is broken. |
| 408 | */ |
| 409 | status_t publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode); |
| 410 | |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 411 | struct Finished { |
| 412 | uint32_t seq; |
| 413 | bool handled; |
| 414 | nsecs_t consumeTime; |
| 415 | }; |
| 416 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 417 | struct Timeline { |
| 418 | int32_t inputEventId; |
| 419 | std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline; |
| 420 | }; |
| 421 | |
| 422 | typedef std::variant<Finished, Timeline> ConsumerResponse; |
| 423 | /* Receive a signal from the consumer in reply to the original dispatch signal. |
| 424 | * If a signal was received, returns a Finished or a Timeline object. |
| 425 | * The InputConsumer should return a Finished object for every InputMessage that it is sent |
| 426 | * to confirm that it has been processed and that the InputConsumer is responsive. |
| 427 | * If several InputMessages are sent to InputConsumer, it's possible to receive Finished |
| 428 | * events out of order for those messages. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 429 | * |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 430 | * The Timeline object is returned whenever the receiving end has processed a graphical frame |
| 431 | * and is returning the timeline of the frame. Not all input events will cause a Timeline |
| 432 | * object to be returned, and there is not guarantee about when it will arrive. |
| 433 | * |
| 434 | * If an object of Finished is returned, the returned sequence number is never 0 unless the |
| 435 | * operation failed. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 436 | * |
Siarhei Vishniakou | eedd0fc | 2021-03-12 09:50:36 +0000 | [diff] [blame] | 437 | * Returned error codes: |
| 438 | * OK on success. |
| 439 | * WOULD_BLOCK if there is no signal present. |
| 440 | * DEAD_OBJECT if the channel's peer has been closed. |
| 441 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 442 | */ |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 443 | android::base::Result<ConsumerResponse> receiveConsumerResponse(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 444 | |
| 445 | private: |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 446 | std::shared_ptr<InputChannel> mChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | /* |
| 450 | * Consumes input events from an input channel. |
| 451 | */ |
| 452 | class InputConsumer { |
| 453 | public: |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 454 | /* Create a consumer associated with an input channel. */ |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 455 | explicit InputConsumer(const std::shared_ptr<InputChannel>& channel); |
Siarhei Vishniakou | 0ced3cc | 2017-11-21 15:33:17 -0800 | [diff] [blame] | 456 | /* Create a consumer associated with an input channel, override resampling system property */ |
| 457 | explicit InputConsumer(const std::shared_ptr<InputChannel>& channel, |
| 458 | bool enableTouchResampling); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 459 | |
| 460 | /* Destroys the consumer and releases its input channel. */ |
| 461 | ~InputConsumer(); |
| 462 | |
| 463 | /* Gets the underlying input channel. */ |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 464 | inline std::shared_ptr<InputChannel> getChannel() { return mChannel; } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 465 | |
| 466 | /* Consumes an input event from the input channel and copies its contents into |
| 467 | * an InputEvent object created using the specified factory. |
| 468 | * |
| 469 | * Tries to combine a series of move events into larger batches whenever possible. |
| 470 | * |
| 471 | * If consumeBatches is false, then defers consuming pending batched events if it |
| 472 | * is possible for additional samples to be added to them later. Call hasPendingBatch() |
| 473 | * to determine whether a pending batch is available to be consumed. |
| 474 | * |
| 475 | * If consumeBatches is true, then events are still batched but they are consumed |
| 476 | * immediately as soon as the input channel is exhausted. |
| 477 | * |
| 478 | * The frameTime parameter specifies the time when the current display frame started |
| 479 | * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown. |
| 480 | * |
| 481 | * The returned sequence number is never 0 unless the operation failed. |
| 482 | * |
| 483 | * Returns OK on success. |
| 484 | * Returns WOULD_BLOCK if there is no event present. |
| 485 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 486 | * Returns NO_MEMORY if the event could not be created. |
| 487 | * Other errors probably indicate that the channel is broken. |
| 488 | */ |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 489 | status_t consume(InputEventFactoryInterface* factory, bool consumeBatches, nsecs_t frameTime, |
| 490 | uint32_t* outSeq, InputEvent** outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 491 | |
| 492 | /* Sends a finished signal to the publisher to inform it that the message |
| 493 | * with the specified sequence number has finished being process and whether |
| 494 | * the message was handled by the consumer. |
| 495 | * |
| 496 | * Returns OK on success. |
| 497 | * Returns BAD_VALUE if seq is 0. |
| 498 | * Other errors probably indicate that the channel is broken. |
| 499 | */ |
| 500 | status_t sendFinishedSignal(uint32_t seq, bool handled); |
| 501 | |
Siarhei Vishniakou | f94ae02 | 2021-02-04 01:23:17 +0000 | [diff] [blame] | 502 | status_t sendTimeline(int32_t inputEventId, |
| 503 | std::array<nsecs_t, GraphicsTimeline::SIZE> timeline); |
| 504 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 505 | /* Returns true if there is a pending batch. |
| 506 | * |
| 507 | * Should be called after calling consume() with consumeBatches == false to determine |
| 508 | * whether consume() should be called again later on with consumeBatches == true. |
| 509 | */ |
| 510 | bool hasPendingBatch() const; |
| 511 | |
Arthur Hung | c7812be | 2020-02-27 22:40:27 +0800 | [diff] [blame] | 512 | /* Returns the source of first pending batch if exist. |
| 513 | * |
| 514 | * Should be called after calling consume() with consumeBatches == false to determine |
| 515 | * whether consume() should be called again later on with consumeBatches == true. |
| 516 | */ |
| 517 | int32_t getPendingBatchSource() const; |
| 518 | |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 519 | std::string dump() const; |
| 520 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 521 | private: |
| 522 | // True if touch resampling is enabled. |
| 523 | const bool mResampleTouch; |
| 524 | |
Siarhei Vishniakou | ce5ab08 | 2020-07-09 17:03:21 -0500 | [diff] [blame] | 525 | std::shared_ptr<InputChannel> mChannel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 526 | |
| 527 | // The current input message. |
| 528 | InputMessage mMsg; |
| 529 | |
| 530 | // True if mMsg contains a valid input message that was deferred from the previous |
| 531 | // call to consume and that still needs to be handled. |
| 532 | bool mMsgDeferred; |
| 533 | |
| 534 | // Batched motion events per device and source. |
| 535 | struct Batch { |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 536 | std::vector<InputMessage> samples; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 537 | }; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 538 | std::vector<Batch> mBatches; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 539 | |
| 540 | // Touch state per device and source, only for sources of class pointer. |
| 541 | struct History { |
| 542 | nsecs_t eventTime; |
| 543 | BitSet32 idBits; |
| 544 | int32_t idToIndex[MAX_POINTER_ID + 1]; |
| 545 | PointerCoords pointers[MAX_POINTERS]; |
| 546 | |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 547 | void initializeFrom(const InputMessage& msg) { |
| 548 | eventTime = msg.body.motion.eventTime; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 549 | idBits.clear(); |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 550 | for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) { |
| 551 | uint32_t id = msg.body.motion.pointers[i].properties.id; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 552 | idBits.markBit(id); |
| 553 | idToIndex[id] = i; |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 554 | pointers[i].copyFrom(msg.body.motion.pointers[i].coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 558 | void initializeFrom(const History& other) { |
| 559 | eventTime = other.eventTime; |
| 560 | idBits = other.idBits; // temporary copy |
| 561 | for (size_t i = 0; i < other.idBits.count(); i++) { |
| 562 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 563 | int32_t index = other.idToIndex[id]; |
| 564 | idToIndex[id] = index; |
| 565 | pointers[index].copyFrom(other.pointers[index]); |
| 566 | } |
| 567 | idBits = other.idBits; // final copy |
| 568 | } |
| 569 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 570 | const PointerCoords& getPointerById(uint32_t id) const { |
| 571 | return pointers[idToIndex[id]]; |
| 572 | } |
Siarhei Vishniakou | c7dc378 | 2017-08-24 20:36:28 -0700 | [diff] [blame] | 573 | |
| 574 | bool hasPointerId(uint32_t id) const { |
| 575 | return idBits.hasBit(id); |
| 576 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 577 | }; |
| 578 | struct TouchState { |
| 579 | int32_t deviceId; |
| 580 | int32_t source; |
| 581 | size_t historyCurrent; |
| 582 | size_t historySize; |
| 583 | History history[2]; |
| 584 | History lastResample; |
| 585 | |
| 586 | void initialize(int32_t deviceId, int32_t source) { |
| 587 | this->deviceId = deviceId; |
| 588 | this->source = source; |
| 589 | historyCurrent = 0; |
| 590 | historySize = 0; |
| 591 | lastResample.eventTime = 0; |
| 592 | lastResample.idBits.clear(); |
| 593 | } |
| 594 | |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 595 | void addHistory(const InputMessage& msg) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 596 | historyCurrent ^= 1; |
| 597 | if (historySize < 2) { |
| 598 | historySize += 1; |
| 599 | } |
| 600 | history[historyCurrent].initializeFrom(msg); |
| 601 | } |
| 602 | |
| 603 | const History* getHistory(size_t index) const { |
| 604 | return &history[(historyCurrent + index) & 1]; |
| 605 | } |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 606 | |
| 607 | bool recentCoordinatesAreIdentical(uint32_t id) const { |
| 608 | // Return true if the two most recently received "raw" coordinates are identical |
| 609 | if (historySize < 2) { |
| 610 | return false; |
| 611 | } |
Siarhei Vishniakou | c7dc378 | 2017-08-24 20:36:28 -0700 | [diff] [blame] | 612 | if (!getHistory(0)->hasPointerId(id) || !getHistory(1)->hasPointerId(id)) { |
| 613 | return false; |
| 614 | } |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 615 | float currentX = getHistory(0)->getPointerById(id).getX(); |
| 616 | float currentY = getHistory(0)->getPointerById(id).getY(); |
| 617 | float previousX = getHistory(1)->getPointerById(id).getX(); |
| 618 | float previousY = getHistory(1)->getPointerById(id).getY(); |
| 619 | if (currentX == previousX && currentY == previousY) { |
| 620 | return true; |
| 621 | } |
| 622 | return false; |
| 623 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 624 | }; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 625 | std::vector<TouchState> mTouchStates; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 626 | |
| 627 | // Chain of batched sequence numbers. When multiple input messages are combined into |
| 628 | // a batch, we append a record here that associates the last sequence number in the |
| 629 | // batch with the previous one. When the finished signal is sent, we traverse the |
| 630 | // chain to individually finish all input messages that were part of the batch. |
| 631 | struct SeqChain { |
| 632 | uint32_t seq; // sequence number of batched input message |
| 633 | uint32_t chain; // sequence number of previous batched input message |
| 634 | }; |
Siarhei Vishniakou | a64c159 | 2020-06-22 12:02:29 -0500 | [diff] [blame] | 635 | std::vector<SeqChain> mSeqChains; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 636 | |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 637 | // The time at which each event with the sequence number 'seq' was consumed. |
| 638 | // This data is provided in 'finishInputEvent' so that the receiving end can measure the latency |
| 639 | // This collection is populated when the event is received, and the entries are erased when the |
| 640 | // events are finished. It should not grow infinitely because if an event is not ack'd, ANR |
| 641 | // will be raised for that connection, and no further events will be posted to that channel. |
| 642 | std::unordered_map<uint32_t /*seq*/, nsecs_t /*consumeTime*/> mConsumeTimes; |
| 643 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 644 | status_t consumeBatch(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 645 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 646 | status_t consumeSamples(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 647 | Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 648 | |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 649 | void updateTouchState(InputMessage& msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 650 | void resampleTouchState(nsecs_t frameTime, MotionEvent* event, |
| 651 | const InputMessage *next); |
| 652 | |
| 653 | ssize_t findBatch(int32_t deviceId, int32_t source) const; |
| 654 | ssize_t findTouchState(int32_t deviceId, int32_t source) const; |
| 655 | |
Siarhei Vishniakou | 3531ae7 | 2021-02-02 12:12:27 -1000 | [diff] [blame] | 656 | nsecs_t getConsumeTime(uint32_t seq) const; |
| 657 | void popConsumeTime(uint32_t seq); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 658 | status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled); |
| 659 | |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 660 | static void rewriteMessage(TouchState& state, InputMessage& msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 661 | static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg); |
| 662 | static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg); |
Siarhei Vishniakou | 7feb2ea | 2019-11-25 15:11:23 -0800 | [diff] [blame] | 663 | static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg); |
Prabir Pradhan | 3f37b7b | 2020-11-10 16:50:18 -0800 | [diff] [blame] | 664 | static void initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg); |
arthurhung | 7632c33 | 2020-12-30 16:58:01 +0800 | [diff] [blame] | 665 | static void initializeDragEvent(DragEvent* event, const InputMessage* msg); |
Antonio Kantek | 7cdf8ef | 2021-07-13 18:04:53 -0700 | [diff] [blame] | 666 | static void initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 667 | static void addSample(MotionEvent* event, const InputMessage* msg); |
| 668 | static bool canAddSample(const Batch& batch, const InputMessage* msg); |
| 669 | static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time); |
| 670 | static bool shouldResampleTool(int32_t toolType); |
| 671 | |
| 672 | static bool isTouchResamplingEnabled(); |
| 673 | }; |
| 674 | |
| 675 | } // namespace android |