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> |
| 33 | |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 34 | #include <android-base/chrono_utils.h> |
| 35 | |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 36 | #include <binder/IBinder.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 37 | #include <input/Input.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 38 | #include <utils/BitSet.h> |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 39 | #include <utils/Errors.h> |
| 40 | #include <utils/RefBase.h> |
| 41 | #include <utils/Timers.h> |
| 42 | #include <utils/Vector.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 43 | |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 44 | #include <android-base/unique_fd.h> |
| 45 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 46 | namespace android { |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 47 | class Parcel; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Intermediate representation used to send input events and related signals. |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 51 | * |
| 52 | * Note that this structure is used for IPCs so its layout must be identical |
| 53 | * 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] | 54 | * |
| 55 | * Since the struct must be aligned to an 8-byte boundary, there could be uninitialized bytes |
| 56 | * in-between the defined fields. This padding data should be explicitly accounted for by adding |
| 57 | * "empty" fields into the struct. This data is memset to zero before sending the struct across |
| 58 | * the socket. Adding the explicit fields ensures that the memset is not optimized away by the |
| 59 | * compiler. When a new field is added to the struct, the corresponding change |
| 60 | * in StructLayout_test should be made. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 61 | */ |
| 62 | struct InputMessage { |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 63 | enum class Type : uint32_t { |
| 64 | KEY, |
| 65 | MOTION, |
| 66 | FINISHED, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct Header { |
Siarhei Vishniakou | 5240277 | 2019-10-22 09:32:30 -0700 | [diff] [blame] | 70 | Type type; // 4 bytes |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 71 | // We don't need this field in order to align the body below but we |
| 72 | // leave it here because InputMessage::size() and other functions |
| 73 | // compute the size of this structure as sizeof(Header) + sizeof(Body). |
| 74 | uint32_t padding; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 75 | } header; |
| 76 | |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 77 | // Body *must* be 8 byte aligned. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 78 | union Body { |
| 79 | struct Key { |
| 80 | uint32_t seq; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 81 | uint32_t empty1; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 82 | nsecs_t eventTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 83 | int32_t deviceId; |
| 84 | int32_t source; |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 85 | int32_t displayId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 86 | int32_t action; |
| 87 | int32_t flags; |
| 88 | int32_t keyCode; |
| 89 | int32_t scanCode; |
| 90 | int32_t metaState; |
| 91 | int32_t repeatCount; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 92 | uint32_t empty2; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 93 | nsecs_t downTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 94 | |
| 95 | inline size_t size() const { |
| 96 | return sizeof(Key); |
| 97 | } |
| 98 | } key; |
| 99 | |
| 100 | struct Motion { |
| 101 | uint32_t seq; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 102 | uint32_t empty1; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 103 | nsecs_t eventTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 104 | int32_t deviceId; |
| 105 | int32_t source; |
Tarandeep Singh | 5864150 | 2017-07-31 10:51:54 -0700 | [diff] [blame] | 106 | int32_t displayId; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 107 | int32_t action; |
Michael Wright | 7b159c9 | 2015-05-14 14:48:03 +0100 | [diff] [blame] | 108 | int32_t actionButton; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 109 | int32_t flags; |
| 110 | int32_t metaState; |
| 111 | int32_t buttonState; |
Siarhei Vishniakou | 16a2e30 | 2019-01-14 19:21:45 -0800 | [diff] [blame] | 112 | MotionClassification classification; // base type: uint8_t |
| 113 | uint8_t empty2[3]; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 114 | int32_t edgeFlags; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 115 | nsecs_t downTime __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 116 | float xOffset; |
| 117 | float yOffset; |
| 118 | float xPrecision; |
| 119 | float yPrecision; |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 120 | float xCursorPosition; |
| 121 | float yCursorPosition; |
Narayan Kamath | ed5fd38 | 2014-05-02 17:53:33 +0100 | [diff] [blame] | 122 | uint32_t pointerCount; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 123 | uint32_t empty3; |
Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame^] | 124 | /** |
| 125 | * The "pointers" field must be the last field of the struct InputMessage. |
| 126 | * When we send the struct InputMessage across the socket, we are not |
| 127 | * writing the entire "pointers" array, but only the pointerCount portion |
| 128 | * of it as an optimization. Adding a field after "pointers" would break this. |
| 129 | */ |
Michael Wright | b03f103 | 2015-05-14 16:29:13 +0100 | [diff] [blame] | 130 | struct Pointer { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 131 | PointerProperties properties; |
| 132 | PointerCoords coords; |
Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame^] | 133 | } pointers[MAX_POINTERS] __attribute__((aligned(8))); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 134 | |
| 135 | int32_t getActionId() const { |
| 136 | uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) |
| 137 | >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 138 | return pointers[index].properties.id; |
| 139 | } |
| 140 | |
| 141 | inline size_t size() const { |
| 142 | return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS |
| 143 | + sizeof(Pointer) * pointerCount; |
| 144 | } |
| 145 | } motion; |
| 146 | |
| 147 | struct Finished { |
| 148 | uint32_t seq; |
Siarhei Vishniakou | 10fe676 | 2019-11-25 11:44:11 -0800 | [diff] [blame^] | 149 | uint32_t handled; // actually a bool, but we must maintain 8-byte alignment |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 150 | |
| 151 | inline size_t size() const { |
| 152 | return sizeof(Finished); |
| 153 | } |
| 154 | } finished; |
Fengwei Yin | 83e0e42 | 2014-05-24 05:32:09 +0800 | [diff] [blame] | 155 | } __attribute__((aligned(8))) body; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 156 | |
| 157 | bool isValid(size_t actualSize) const; |
| 158 | size_t size() const; |
Siarhei Vishniakou | 1f7c0e4 | 2018-11-16 22:18:53 -0800 | [diff] [blame] | 159 | void getSanitizedCopy(InputMessage* msg) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | /* |
| 163 | * An input channel consists of a local unix domain socket used to send and receive |
| 164 | * input messages across processes. Each channel has a descriptive name for debugging purposes. |
| 165 | * |
| 166 | * Each endpoint has its own InputChannel object that specifies its file descriptor. |
| 167 | * |
| 168 | * The input channel is closed when all references to it are released. |
| 169 | */ |
| 170 | class InputChannel : public RefBase { |
| 171 | protected: |
| 172 | virtual ~InputChannel(); |
| 173 | |
| 174 | public: |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 175 | static sp<InputChannel> create(const std::string& name, android::base::unique_fd fd, |
| 176 | sp<IBinder> token); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 177 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Create a pair of input channels. |
| 180 | * The two returned input channels are equivalent, and are labeled as "server" and "client" |
| 181 | * for convenience. The two input channels share the same token. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 182 | * |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 183 | * Return OK on success. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 184 | */ |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 185 | static status_t openInputChannelPair(const std::string& name, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 186 | sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel); |
| 187 | |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 188 | inline std::string getName() const { return mName; } |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 189 | inline int getFd() const { return mFd.get(); } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 190 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 191 | /* Send a message to the other endpoint. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 192 | * |
| 193 | * If the channel is full then the message is guaranteed not to have been sent at all. |
| 194 | * Try again after the consumer has sent a finished signal indicating that it has |
| 195 | * consumed some of the pending messages from the channel. |
| 196 | * |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 197 | * Return OK on success. |
| 198 | * Return WOULD_BLOCK if the channel is full. |
| 199 | * Return DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 200 | * Other errors probably indicate that the channel is broken. |
| 201 | */ |
| 202 | status_t sendMessage(const InputMessage* msg); |
| 203 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 204 | /* Receive a message sent by the other endpoint. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 205 | * |
| 206 | * If there is no message present, try again after poll() indicates that the fd |
| 207 | * is readable. |
| 208 | * |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 209 | * Return OK on success. |
| 210 | * Return WOULD_BLOCK if there is no message present. |
| 211 | * Return DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 212 | * Other errors probably indicate that the channel is broken. |
| 213 | */ |
| 214 | status_t receiveMessage(InputMessage* msg); |
| 215 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 216 | /* Return a new object that has a duplicate of this channel's fd. */ |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 217 | sp<InputChannel> dup() const; |
| 218 | |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 219 | status_t write(Parcel& out) const; |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 220 | static sp<InputChannel> read(const Parcel& from); |
Robert Carr | 3720ed0 | 2018-08-08 16:08:27 -0700 | [diff] [blame] | 221 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 222 | /** |
| 223 | * The connection token is used to identify the input connection, i.e. |
| 224 | * the pair of input channels that were created simultaneously. Input channels |
| 225 | * are always created in pairs, and the token can be used to find the server-side |
| 226 | * input channel from the client-side input channel, and vice versa. |
| 227 | * |
| 228 | * Do not use connection token to check equality of a specific input channel object |
| 229 | * to another, because two different (client and server) input channels will share the |
| 230 | * same connection token. |
| 231 | * |
| 232 | * Return the token that identifies this connection. |
| 233 | */ |
| 234 | sp<IBinder> getConnectionToken() const; |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 235 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 236 | private: |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 237 | InputChannel(const std::string& name, android::base::unique_fd fd, sp<IBinder> token); |
Siarhei Vishniakou | f93fcf4 | 2017-11-22 16:00:14 -0800 | [diff] [blame] | 238 | std::string mName; |
Josh Gao | 2ccbe3a | 2019-08-09 14:35:36 -0700 | [diff] [blame] | 239 | android::base::unique_fd mFd; |
Robert Carr | 803535b | 2018-08-02 16:38:15 -0700 | [diff] [blame] | 240 | |
Siarhei Vishniakou | 26d3cfb | 2019-10-15 17:02:32 -0700 | [diff] [blame] | 241 | sp<IBinder> mToken; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | /* |
| 245 | * Publishes input events to an input channel. |
| 246 | */ |
| 247 | class InputPublisher { |
| 248 | public: |
| 249 | /* Creates a publisher associated with an input channel. */ |
| 250 | explicit InputPublisher(const sp<InputChannel>& channel); |
| 251 | |
| 252 | /* Destroys the publisher and releases its input channel. */ |
| 253 | ~InputPublisher(); |
| 254 | |
| 255 | /* Gets the underlying input channel. */ |
| 256 | inline sp<InputChannel> getChannel() { return mChannel; } |
| 257 | |
| 258 | /* Publishes a key event to the input channel. |
| 259 | * |
| 260 | * Returns OK on success. |
| 261 | * Returns WOULD_BLOCK if the channel is full. |
| 262 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 263 | * Returns BAD_VALUE if seq is 0. |
| 264 | * Other errors probably indicate that the channel is broken. |
| 265 | */ |
| 266 | status_t publishKeyEvent( |
| 267 | uint32_t seq, |
| 268 | int32_t deviceId, |
| 269 | int32_t source, |
Siarhei Vishniakou | a62a8dd | 2018-06-08 21:17:33 +0100 | [diff] [blame] | 270 | int32_t displayId, |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 271 | int32_t action, |
| 272 | int32_t flags, |
| 273 | int32_t keyCode, |
| 274 | int32_t scanCode, |
| 275 | int32_t metaState, |
| 276 | int32_t repeatCount, |
| 277 | nsecs_t downTime, |
| 278 | nsecs_t eventTime); |
| 279 | |
| 280 | /* Publishes a motion event to the input channel. |
| 281 | * |
| 282 | * Returns OK on success. |
| 283 | * Returns WOULD_BLOCK if the channel is full. |
| 284 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 285 | * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS. |
| 286 | * Other errors probably indicate that the channel is broken. |
| 287 | */ |
Garfield Tan | 00f511d | 2019-06-12 16:55:40 -0700 | [diff] [blame] | 288 | status_t publishMotionEvent(uint32_t seq, int32_t deviceId, int32_t source, int32_t displayId, |
| 289 | int32_t action, int32_t actionButton, int32_t flags, |
| 290 | int32_t edgeFlags, int32_t metaState, int32_t buttonState, |
| 291 | MotionClassification classification, float xOffset, float yOffset, |
| 292 | float xPrecision, float yPrecision, float xCursorPosition, |
| 293 | float yCursorPosition, nsecs_t downTime, nsecs_t eventTime, |
| 294 | uint32_t pointerCount, const PointerProperties* pointerProperties, |
| 295 | const PointerCoords* pointerCoords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 296 | |
| 297 | /* Receives the finished signal from the consumer in reply to the original dispatch signal. |
| 298 | * If a signal was received, returns the message sequence number, |
| 299 | * and whether the consumer handled the message. |
| 300 | * |
| 301 | * The returned sequence number is never 0 unless the operation failed. |
| 302 | * |
| 303 | * Returns OK on success. |
| 304 | * Returns WOULD_BLOCK if there is no signal present. |
| 305 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 306 | * Other errors probably indicate that the channel is broken. |
| 307 | */ |
| 308 | status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled); |
| 309 | |
| 310 | private: |
Atif Niyaz | 3d3fa52 | 2019-07-25 11:12:39 -0700 | [diff] [blame] | 311 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 312 | sp<InputChannel> mChannel; |
| 313 | }; |
| 314 | |
| 315 | /* |
| 316 | * Consumes input events from an input channel. |
| 317 | */ |
| 318 | class InputConsumer { |
| 319 | public: |
| 320 | /* Creates a consumer associated with an input channel. */ |
| 321 | explicit InputConsumer(const sp<InputChannel>& channel); |
| 322 | |
| 323 | /* Destroys the consumer and releases its input channel. */ |
| 324 | ~InputConsumer(); |
| 325 | |
| 326 | /* Gets the underlying input channel. */ |
| 327 | inline sp<InputChannel> getChannel() { return mChannel; } |
| 328 | |
| 329 | /* Consumes an input event from the input channel and copies its contents into |
| 330 | * an InputEvent object created using the specified factory. |
| 331 | * |
| 332 | * Tries to combine a series of move events into larger batches whenever possible. |
| 333 | * |
| 334 | * If consumeBatches is false, then defers consuming pending batched events if it |
| 335 | * is possible for additional samples to be added to them later. Call hasPendingBatch() |
| 336 | * to determine whether a pending batch is available to be consumed. |
| 337 | * |
| 338 | * If consumeBatches is true, then events are still batched but they are consumed |
| 339 | * immediately as soon as the input channel is exhausted. |
| 340 | * |
| 341 | * The frameTime parameter specifies the time when the current display frame started |
| 342 | * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown. |
| 343 | * |
| 344 | * The returned sequence number is never 0 unless the operation failed. |
| 345 | * |
| 346 | * Returns OK on success. |
| 347 | * Returns WOULD_BLOCK if there is no event present. |
| 348 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 349 | * Returns NO_MEMORY if the event could not be created. |
| 350 | * Other errors probably indicate that the channel is broken. |
| 351 | */ |
| 352 | status_t consume(InputEventFactoryInterface* factory, bool consumeBatches, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 353 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 354 | |
| 355 | /* Sends a finished signal to the publisher to inform it that the message |
| 356 | * with the specified sequence number has finished being process and whether |
| 357 | * the message was handled by the consumer. |
| 358 | * |
| 359 | * Returns OK on success. |
| 360 | * Returns BAD_VALUE if seq is 0. |
| 361 | * Other errors probably indicate that the channel is broken. |
| 362 | */ |
| 363 | status_t sendFinishedSignal(uint32_t seq, bool handled); |
| 364 | |
| 365 | /* Returns true if there is a deferred event waiting. |
| 366 | * |
| 367 | * Should be called after calling consume() to determine whether the consumer |
| 368 | * has a deferred event to be processed. Deferred events are somewhat special in |
| 369 | * that they have already been removed from the input channel. If the input channel |
| 370 | * becomes empty, the client may need to do extra work to ensure that it processes |
| 371 | * the deferred event despite the fact that the input channel's file descriptor |
| 372 | * is not readable. |
| 373 | * |
| 374 | * One option is simply to call consume() in a loop until it returns WOULD_BLOCK. |
| 375 | * This guarantees that all deferred events will be processed. |
| 376 | * |
| 377 | * Alternately, the caller can call hasDeferredEvent() to determine whether there is |
| 378 | * a deferred event waiting and then ensure that its event loop wakes up at least |
| 379 | * one more time to consume the deferred event. |
| 380 | */ |
| 381 | bool hasDeferredEvent() const; |
| 382 | |
| 383 | /* Returns true if there is a pending batch. |
| 384 | * |
| 385 | * Should be called after calling consume() with consumeBatches == false to determine |
| 386 | * whether consume() should be called again later on with consumeBatches == true. |
| 387 | */ |
| 388 | bool hasPendingBatch() const; |
| 389 | |
| 390 | private: |
| 391 | // True if touch resampling is enabled. |
| 392 | const bool mResampleTouch; |
| 393 | |
| 394 | // The input channel. |
| 395 | sp<InputChannel> mChannel; |
| 396 | |
| 397 | // The current input message. |
| 398 | InputMessage mMsg; |
| 399 | |
| 400 | // True if mMsg contains a valid input message that was deferred from the previous |
| 401 | // call to consume and that still needs to be handled. |
| 402 | bool mMsgDeferred; |
| 403 | |
| 404 | // Batched motion events per device and source. |
| 405 | struct Batch { |
| 406 | Vector<InputMessage> samples; |
| 407 | }; |
| 408 | Vector<Batch> mBatches; |
| 409 | |
| 410 | // Touch state per device and source, only for sources of class pointer. |
| 411 | struct History { |
| 412 | nsecs_t eventTime; |
| 413 | BitSet32 idBits; |
| 414 | int32_t idToIndex[MAX_POINTER_ID + 1]; |
| 415 | PointerCoords pointers[MAX_POINTERS]; |
| 416 | |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 417 | void initializeFrom(const InputMessage& msg) { |
| 418 | eventTime = msg.body.motion.eventTime; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 419 | idBits.clear(); |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 420 | for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) { |
| 421 | uint32_t id = msg.body.motion.pointers[i].properties.id; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 422 | idBits.markBit(id); |
| 423 | idToIndex[id] = i; |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 424 | pointers[i].copyFrom(msg.body.motion.pointers[i].coords); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 428 | void initializeFrom(const History& other) { |
| 429 | eventTime = other.eventTime; |
| 430 | idBits = other.idBits; // temporary copy |
| 431 | for (size_t i = 0; i < other.idBits.count(); i++) { |
| 432 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 433 | int32_t index = other.idToIndex[id]; |
| 434 | idToIndex[id] = index; |
| 435 | pointers[index].copyFrom(other.pointers[index]); |
| 436 | } |
| 437 | idBits = other.idBits; // final copy |
| 438 | } |
| 439 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 440 | const PointerCoords& getPointerById(uint32_t id) const { |
| 441 | return pointers[idToIndex[id]]; |
| 442 | } |
Siarhei Vishniakou | c7dc378 | 2017-08-24 20:36:28 -0700 | [diff] [blame] | 443 | |
| 444 | bool hasPointerId(uint32_t id) const { |
| 445 | return idBits.hasBit(id); |
| 446 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 447 | }; |
| 448 | struct TouchState { |
| 449 | int32_t deviceId; |
| 450 | int32_t source; |
| 451 | size_t historyCurrent; |
| 452 | size_t historySize; |
| 453 | History history[2]; |
| 454 | History lastResample; |
| 455 | |
| 456 | void initialize(int32_t deviceId, int32_t source) { |
| 457 | this->deviceId = deviceId; |
| 458 | this->source = source; |
| 459 | historyCurrent = 0; |
| 460 | historySize = 0; |
| 461 | lastResample.eventTime = 0; |
| 462 | lastResample.idBits.clear(); |
| 463 | } |
| 464 | |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 465 | void addHistory(const InputMessage& msg) { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 466 | historyCurrent ^= 1; |
| 467 | if (historySize < 2) { |
| 468 | historySize += 1; |
| 469 | } |
| 470 | history[historyCurrent].initializeFrom(msg); |
| 471 | } |
| 472 | |
| 473 | const History* getHistory(size_t index) const { |
| 474 | return &history[(historyCurrent + index) & 1]; |
| 475 | } |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 476 | |
| 477 | bool recentCoordinatesAreIdentical(uint32_t id) const { |
| 478 | // Return true if the two most recently received "raw" coordinates are identical |
| 479 | if (historySize < 2) { |
| 480 | return false; |
| 481 | } |
Siarhei Vishniakou | c7dc378 | 2017-08-24 20:36:28 -0700 | [diff] [blame] | 482 | if (!getHistory(0)->hasPointerId(id) || !getHistory(1)->hasPointerId(id)) { |
| 483 | return false; |
| 484 | } |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 485 | float currentX = getHistory(0)->getPointerById(id).getX(); |
| 486 | float currentY = getHistory(0)->getPointerById(id).getY(); |
| 487 | float previousX = getHistory(1)->getPointerById(id).getX(); |
| 488 | float previousY = getHistory(1)->getPointerById(id).getY(); |
| 489 | if (currentX == previousX && currentY == previousY) { |
| 490 | return true; |
| 491 | } |
| 492 | return false; |
| 493 | } |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 494 | }; |
| 495 | Vector<TouchState> mTouchStates; |
| 496 | |
| 497 | // Chain of batched sequence numbers. When multiple input messages are combined into |
| 498 | // a batch, we append a record here that associates the last sequence number in the |
| 499 | // batch with the previous one. When the finished signal is sent, we traverse the |
| 500 | // chain to individually finish all input messages that were part of the batch. |
| 501 | struct SeqChain { |
| 502 | uint32_t seq; // sequence number of batched input message |
| 503 | uint32_t chain; // sequence number of previous batched input message |
| 504 | }; |
| 505 | Vector<SeqChain> mSeqChains; |
| 506 | |
| 507 | status_t consumeBatch(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 508 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 509 | status_t consumeSamples(InputEventFactoryInterface* factory, |
Siarhei Vishniakou | 777a10b | 2018-01-31 16:45:06 -0800 | [diff] [blame] | 510 | Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 511 | |
Siarhei Vishniakou | 086a02a | 2017-06-12 15:01:41 +0100 | [diff] [blame] | 512 | void updateTouchState(InputMessage& msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 513 | void resampleTouchState(nsecs_t frameTime, MotionEvent* event, |
| 514 | const InputMessage *next); |
| 515 | |
| 516 | ssize_t findBatch(int32_t deviceId, int32_t source) const; |
| 517 | ssize_t findTouchState(int32_t deviceId, int32_t source) const; |
| 518 | |
| 519 | status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled); |
| 520 | |
Siarhei Vishniakou | 56c9ae1 | 2017-11-06 21:16:47 -0800 | [diff] [blame] | 521 | static void rewriteMessage(TouchState& state, InputMessage& msg); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 522 | static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg); |
| 523 | static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg); |
| 524 | static void addSample(MotionEvent* event, const InputMessage* msg); |
| 525 | static bool canAddSample(const Batch& batch, const InputMessage* msg); |
| 526 | static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time); |
| 527 | static bool shouldResampleTool(int32_t toolType); |
| 528 | |
| 529 | static bool isTouchResamplingEnabled(); |
| 530 | }; |
| 531 | |
| 532 | } // namespace android |
| 533 | |
| 534 | #endif // _LIBINPUT_INPUT_TRANSPORT_H |