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