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