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