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