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