blob: 8744ef76c15aad63f4cda68aa5193fe6dbd1189c [file] [log] [blame]
Jeff Brown5912f952013-07-01 19:10:31 -07001/*
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 Carr2c358bf2018-08-08 15:58:15 -070020#pragma GCC system_header
21
Jeff Brown5912f952013-07-01 19:10:31 -070022/**
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 Vishniakouec8f7252018-07-06 11:19:32 +010032#include <string>
33
Atif Niyaz3d3fa522019-07-25 11:12:39 -070034#include <android-base/chrono_utils.h>
35
Robert Carr803535b2018-08-02 16:38:15 -070036#include <binder/IBinder.h>
Chris Ye0783e992020-06-02 21:34:49 -070037#include <binder/Parcelable.h>
Jeff Brown5912f952013-07-01 19:10:31 -070038#include <input/Input.h>
Chris Ye0783e992020-06-02 21:34:49 -070039#include <sys/stat.h>
chaviw9eaa22c2020-07-01 16:21:27 -070040#include <ui/Transform.h>
Jeff Brown5912f952013-07-01 19:10:31 -070041#include <utils/BitSet.h>
Atif Niyaz3d3fa522019-07-25 11:12:39 -070042#include <utils/Errors.h>
43#include <utils/RefBase.h>
44#include <utils/Timers.h>
Jeff Brown5912f952013-07-01 19:10:31 -070045
Josh Gao2ccbe3a2019-08-09 14:35:36 -070046#include <android-base/unique_fd.h>
47
Jeff Brown5912f952013-07-01 19:10:31 -070048namespace android {
Robert Carr3720ed02018-08-08 16:08:27 -070049class Parcel;
Jeff Brown5912f952013-07-01 19:10:31 -070050
51/*
52 * Intermediate representation used to send input events and related signals.
Fengwei Yin83e0e422014-05-24 05:32:09 +080053 *
54 * Note that this structure is used for IPCs so its layout must be identical
55 * on 64 and 32 bit processes. This is tested in StructLayout_test.cpp.
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -080056 *
57 * Since the struct must be aligned to an 8-byte boundary, there could be uninitialized bytes
58 * in-between the defined fields. This padding data should be explicitly accounted for by adding
59 * "empty" fields into the struct. This data is memset to zero before sending the struct across
60 * the socket. Adding the explicit fields ensures that the memset is not optimized away by the
61 * compiler. When a new field is added to the struct, the corresponding change
62 * in StructLayout_test should be made.
Jeff Brown5912f952013-07-01 19:10:31 -070063 */
64struct InputMessage {
Siarhei Vishniakou52402772019-10-22 09:32:30 -070065 enum class Type : uint32_t {
66 KEY,
67 MOTION,
68 FINISHED,
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080069 FOCUS,
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -080070 CAPTURE,
Jeff Brown5912f952013-07-01 19:10:31 -070071 };
72
73 struct Header {
Siarhei Vishniakou52402772019-10-22 09:32:30 -070074 Type type; // 4 bytes
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -050075 uint32_t seq;
Jeff Brown5912f952013-07-01 19:10:31 -070076 } header;
77
Fengwei Yin83e0e422014-05-24 05:32:09 +080078 // Body *must* be 8 byte aligned.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060079 // 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 Brown5912f952013-07-01 19:10:31 -070082 union Body {
83 struct Key {
Garfield Tan1c7bc862020-01-28 13:24:04 -080084 int32_t eventId;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -050085 uint32_t empty1;
Fengwei Yin83e0e422014-05-24 05:32:09 +080086 nsecs_t eventTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -070087 int32_t deviceId;
88 int32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010089 int32_t displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060090 std::array<uint8_t, 32> hmac;
Jeff Brown5912f952013-07-01 19:10:31 -070091 int32_t action;
92 int32_t flags;
93 int32_t keyCode;
94 int32_t scanCode;
95 int32_t metaState;
96 int32_t repeatCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -080097 uint32_t empty2;
Fengwei Yin83e0e422014-05-24 05:32:09 +080098 nsecs_t downTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -070099
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800100 inline size_t size() const { return sizeof(Key); }
Jeff Brown5912f952013-07-01 19:10:31 -0700101 } key;
102
103 struct Motion {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800104 int32_t eventId;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500105 uint32_t empty1;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800106 nsecs_t eventTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700107 int32_t deviceId;
108 int32_t source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700109 int32_t displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600110 std::array<uint8_t, 32> hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700111 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100112 int32_t actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700113 int32_t flags;
114 int32_t metaState;
115 int32_t buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800116 MotionClassification classification; // base type: uint8_t
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800117 uint8_t empty2[3]; // 3 bytes to fill gap created by classification
Jeff Brown5912f952013-07-01 19:10:31 -0700118 int32_t edgeFlags;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800119 nsecs_t downTime __attribute__((aligned(8)));
chaviw9eaa22c2020-07-01 16:21:27 -0700120 float dsdx;
121 float dtdx;
122 float dtdy;
123 float dsdy;
124 float tx;
125 float ty;
Jeff Brown5912f952013-07-01 19:10:31 -0700126 float xPrecision;
127 float yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700128 float xCursorPosition;
129 float yCursorPosition;
Narayan Kamathed5fd382014-05-02 17:53:33 +0100130 uint32_t pointerCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800131 uint32_t empty3;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800132 /**
133 * The "pointers" field must be the last field of the struct InputMessage.
134 * When we send the struct InputMessage across the socket, we are not
135 * writing the entire "pointers" array, but only the pointerCount portion
136 * of it as an optimization. Adding a field after "pointers" would break this.
137 */
Michael Wrightb03f1032015-05-14 16:29:13 +0100138 struct Pointer {
Jeff Brown5912f952013-07-01 19:10:31 -0700139 PointerProperties properties;
140 PointerCoords coords;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800141 } pointers[MAX_POINTERS] __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700142
143 int32_t getActionId() const {
144 uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
145 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
146 return pointers[index].properties.id;
147 }
148
149 inline size_t size() const {
150 return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
151 + sizeof(Pointer) * pointerCount;
152 }
153 } motion;
154
155 struct Finished {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500156 uint32_t empty1;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800157 uint32_t handled; // actually a bool, but we must maintain 8-byte alignment
Jeff Brown5912f952013-07-01 19:10:31 -0700158
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800159 inline size_t size() const { return sizeof(Finished); }
Jeff Brown5912f952013-07-01 19:10:31 -0700160 } finished;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800161
162 struct Focus {
Garfield Tan1c7bc862020-01-28 13:24:04 -0800163 int32_t eventId;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800164 // 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;
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800170
171 struct Capture {
172 int32_t eventId;
173 uint32_t pointerCaptureEnabled; // actually a bool, but we maintain 8-byte alignment
174
175 inline size_t size() const { return sizeof(Capture); }
176 } capture;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800177 } __attribute__((aligned(8))) body;
Jeff Brown5912f952013-07-01 19:10:31 -0700178
179 bool isValid(size_t actualSize) const;
180 size_t size() const;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800181 void getSanitizedCopy(InputMessage* msg) const;
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500182
183 static const char* typeToString(Type type) {
184 switch (type) {
185 case Type::KEY:
186 return "KEY";
187 case Type::MOTION:
188 return "MOTION";
189 case Type::FINISHED:
190 return "FINISHED";
191 case Type::FOCUS:
192 return "FOCUS";
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800193 case Type::CAPTURE:
194 return "CAPTURE";
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500195 }
196 }
Jeff Brown5912f952013-07-01 19:10:31 -0700197};
198
199/*
200 * An input channel consists of a local unix domain socket used to send and receive
201 * input messages across processes. Each channel has a descriptive name for debugging purposes.
202 *
203 * Each endpoint has its own InputChannel object that specifies its file descriptor.
204 *
205 * The input channel is closed when all references to it are released.
206 */
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500207class InputChannel : public Parcelable {
Chris Ye0783e992020-06-02 21:34:49 -0700208public:
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500209 static std::unique_ptr<InputChannel> create(const std::string& name,
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500210 android::base::unique_fd fd, sp<IBinder> token);
211 InputChannel() = default;
212 InputChannel(const InputChannel& other)
213 : mName(other.mName), mFd(::dup(other.mFd)), mToken(other.mToken){};
214 InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token);
Jeff Brown5912f952013-07-01 19:10:31 -0700215 virtual ~InputChannel();
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700216 /**
217 * Create a pair of input channels.
218 * The two returned input channels are equivalent, and are labeled as "server" and "client"
219 * for convenience. The two input channels share the same token.
Jeff Brown5912f952013-07-01 19:10:31 -0700220 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700221 * Return OK on success.
Jeff Brown5912f952013-07-01 19:10:31 -0700222 */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800223 static status_t openInputChannelPair(const std::string& name,
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500224 std::unique_ptr<InputChannel>& outServerChannel,
225 std::unique_ptr<InputChannel>& outClientChannel);
Jeff Brown5912f952013-07-01 19:10:31 -0700226
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500227 inline std::string getName() const { return mName; }
228 inline const android::base::unique_fd& getFd() const { return mFd; }
229 inline sp<IBinder> getToken() const { return mToken; }
Jeff Brown5912f952013-07-01 19:10:31 -0700230
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700231 /* Send a message to the other endpoint.
Jeff Brown5912f952013-07-01 19:10:31 -0700232 *
233 * If the channel is full then the message is guaranteed not to have been sent at all.
234 * Try again after the consumer has sent a finished signal indicating that it has
235 * consumed some of the pending messages from the channel.
236 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700237 * Return OK on success.
238 * Return WOULD_BLOCK if the channel is full.
239 * Return DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown5912f952013-07-01 19:10:31 -0700240 * Other errors probably indicate that the channel is broken.
241 */
242 status_t sendMessage(const InputMessage* msg);
243
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700244 /* Receive a message sent by the other endpoint.
Jeff Brown5912f952013-07-01 19:10:31 -0700245 *
246 * If there is no message present, try again after poll() indicates that the fd
247 * is readable.
248 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700249 * Return OK on success.
250 * Return WOULD_BLOCK if there is no message present.
251 * Return DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown5912f952013-07-01 19:10:31 -0700252 * Other errors probably indicate that the channel is broken.
253 */
254 status_t receiveMessage(InputMessage* msg);
255
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700256 /* Return a new object that has a duplicate of this channel's fd. */
Siarhei Vishniakoud2588272020-07-10 11:15:40 -0500257 std::unique_ptr<InputChannel> dup() const;
Jeff Brown5912f952013-07-01 19:10:31 -0700258
Garfield Tan15601662020-09-22 15:32:38 -0700259 void copyTo(InputChannel& outChannel) const;
260
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500261 status_t readFromParcel(const android::Parcel* parcel) override;
262 status_t writeToParcel(android::Parcel* parcel) const override;
Robert Carr3720ed02018-08-08 16:08:27 -0700263
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700264 /**
265 * The connection token is used to identify the input connection, i.e.
266 * the pair of input channels that were created simultaneously. Input channels
267 * are always created in pairs, and the token can be used to find the server-side
268 * input channel from the client-side input channel, and vice versa.
269 *
270 * Do not use connection token to check equality of a specific input channel object
271 * to another, because two different (client and server) input channels will share the
272 * same connection token.
273 *
274 * Return the token that identifies this connection.
275 */
276 sp<IBinder> getConnectionToken() const;
Robert Carr803535b2018-08-02 16:38:15 -0700277
Chris Ye0783e992020-06-02 21:34:49 -0700278 bool operator==(const InputChannel& inputChannel) const {
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500279 struct stat lhs, rhs;
280 if (fstat(mFd.get(), &lhs) != 0) {
Chris Ye0783e992020-06-02 21:34:49 -0700281 return false;
282 }
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500283 if (fstat(inputChannel.getFd(), &rhs) != 0) {
Chris Ye0783e992020-06-02 21:34:49 -0700284 return false;
285 }
286 // If file descriptors are pointing to same inode they are duplicated fds.
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500287 return inputChannel.getName() == getName() && inputChannel.getConnectionToken() == mToken &&
288 lhs.st_ino == rhs.st_ino;
Chris Ye0783e992020-06-02 21:34:49 -0700289 }
290
Jeff Brown5912f952013-07-01 19:10:31 -0700291private:
Garfield Tan15601662020-09-22 15:32:38 -0700292 base::unique_fd dupFd() const;
293
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500294 std::string mName;
295 android::base::unique_fd mFd;
296
297 sp<IBinder> mToken;
Jeff Brown5912f952013-07-01 19:10:31 -0700298};
299
300/*
301 * Publishes input events to an input channel.
302 */
303class InputPublisher {
304public:
305 /* Creates a publisher associated with an input channel. */
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500306 explicit InputPublisher(const std::shared_ptr<InputChannel>& channel);
Jeff Brown5912f952013-07-01 19:10:31 -0700307
308 /* Destroys the publisher and releases its input channel. */
309 ~InputPublisher();
310
311 /* Gets the underlying input channel. */
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500312 inline std::shared_ptr<InputChannel> getChannel() { return mChannel; }
Jeff Brown5912f952013-07-01 19:10:31 -0700313
314 /* Publishes a key event to the input channel.
315 *
316 * Returns OK on success.
317 * Returns WOULD_BLOCK if the channel is full.
318 * Returns DEAD_OBJECT if the channel's peer has been closed.
319 * Returns BAD_VALUE if seq is 0.
320 * Other errors probably indicate that the channel is broken.
321 */
Garfield Tan1c7bc862020-01-28 13:24:04 -0800322 status_t publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source,
323 int32_t displayId, std::array<uint8_t, 32> hmac, int32_t action,
324 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600325 int32_t repeatCount, nsecs_t downTime, nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700326
327 /* Publishes a motion event to the input channel.
328 *
329 * Returns OK on success.
330 * Returns WOULD_BLOCK if the channel is full.
331 * Returns DEAD_OBJECT if the channel's peer has been closed.
332 * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS.
333 * Other errors probably indicate that the channel is broken.
334 */
Garfield Tan1c7bc862020-01-28 13:24:04 -0800335 status_t publishMotionEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source,
336 int32_t displayId, std::array<uint8_t, 32> hmac, int32_t action,
337 int32_t actionButton, int32_t flags, int32_t edgeFlags,
338 int32_t metaState, int32_t buttonState,
chaviw9eaa22c2020-07-01 16:21:27 -0700339 MotionClassification classification, const ui::Transform& transform,
340 float xPrecision, float yPrecision, float xCursorPosition,
341 float yCursorPosition, nsecs_t downTime, nsecs_t eventTime,
342 uint32_t pointerCount, const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700343 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700344
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800345 /* Publishes a focus event to the input channel.
346 *
347 * Returns OK on success.
348 * Returns WOULD_BLOCK if the channel is full.
349 * Returns DEAD_OBJECT if the channel's peer has been closed.
350 * Other errors probably indicate that the channel is broken.
351 */
Garfield Tan1c7bc862020-01-28 13:24:04 -0800352 status_t publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus, bool inTouchMode);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800353
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800354 /* Publishes a capture event to the input channel.
355 *
356 * Returns OK on success.
357 * Returns WOULD_BLOCK if the channel is full.
358 * Returns DEAD_OBJECT if the channel's peer has been closed.
359 * Other errors probably indicate that the channel is broken.
360 */
361 status_t publishCaptureEvent(uint32_t seq, int32_t eventId, bool pointerCaptureEnabled);
362
Jeff Brown5912f952013-07-01 19:10:31 -0700363 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
364 * If a signal was received, returns the message sequence number,
365 * and whether the consumer handled the message.
366 *
367 * The returned sequence number is never 0 unless the operation failed.
368 *
369 * Returns OK on success.
370 * Returns WOULD_BLOCK if there is no signal present.
371 * Returns DEAD_OBJECT if the channel's peer has been closed.
372 * Other errors probably indicate that the channel is broken.
373 */
374 status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled);
375
376private:
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500377 std::shared_ptr<InputChannel> mChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700378};
379
380/*
381 * Consumes input events from an input channel.
382 */
383class InputConsumer {
384public:
385 /* Creates a consumer associated with an input channel. */
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500386 explicit InputConsumer(const std::shared_ptr<InputChannel>& channel);
Jeff Brown5912f952013-07-01 19:10:31 -0700387
388 /* Destroys the consumer and releases its input channel. */
389 ~InputConsumer();
390
391 /* Gets the underlying input channel. */
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500392 inline std::shared_ptr<InputChannel> getChannel() { return mChannel; }
Jeff Brown5912f952013-07-01 19:10:31 -0700393
394 /* Consumes an input event from the input channel and copies its contents into
395 * an InputEvent object created using the specified factory.
396 *
397 * Tries to combine a series of move events into larger batches whenever possible.
398 *
399 * If consumeBatches is false, then defers consuming pending batched events if it
400 * is possible for additional samples to be added to them later. Call hasPendingBatch()
401 * to determine whether a pending batch is available to be consumed.
402 *
403 * If consumeBatches is true, then events are still batched but they are consumed
404 * immediately as soon as the input channel is exhausted.
405 *
406 * The frameTime parameter specifies the time when the current display frame started
407 * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown.
408 *
409 * The returned sequence number is never 0 unless the operation failed.
410 *
411 * Returns OK on success.
412 * Returns WOULD_BLOCK if there is no event present.
413 * Returns DEAD_OBJECT if the channel's peer has been closed.
414 * Returns NO_MEMORY if the event could not be created.
415 * Other errors probably indicate that the channel is broken.
416 */
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800417 status_t consume(InputEventFactoryInterface* factory, bool consumeBatches, nsecs_t frameTime,
418 uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700419
420 /* Sends a finished signal to the publisher to inform it that the message
421 * with the specified sequence number has finished being process and whether
422 * the message was handled by the consumer.
423 *
424 * Returns OK on success.
425 * Returns BAD_VALUE if seq is 0.
426 * Other errors probably indicate that the channel is broken.
427 */
428 status_t sendFinishedSignal(uint32_t seq, bool handled);
429
430 /* Returns true if there is a deferred event waiting.
431 *
432 * Should be called after calling consume() to determine whether the consumer
433 * has a deferred event to be processed. Deferred events are somewhat special in
434 * that they have already been removed from the input channel. If the input channel
435 * becomes empty, the client may need to do extra work to ensure that it processes
436 * the deferred event despite the fact that the input channel's file descriptor
437 * is not readable.
438 *
439 * One option is simply to call consume() in a loop until it returns WOULD_BLOCK.
440 * This guarantees that all deferred events will be processed.
441 *
442 * Alternately, the caller can call hasDeferredEvent() to determine whether there is
443 * a deferred event waiting and then ensure that its event loop wakes up at least
444 * one more time to consume the deferred event.
445 */
446 bool hasDeferredEvent() const;
447
448 /* Returns true if there is a pending batch.
449 *
450 * Should be called after calling consume() with consumeBatches == false to determine
451 * whether consume() should be called again later on with consumeBatches == true.
452 */
453 bool hasPendingBatch() const;
454
Arthur Hungc7812be2020-02-27 22:40:27 +0800455 /* Returns the source of first pending batch if exist.
456 *
457 * Should be called after calling consume() with consumeBatches == false to determine
458 * whether consume() should be called again later on with consumeBatches == true.
459 */
460 int32_t getPendingBatchSource() const;
461
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500462 std::string dump() const;
463
Jeff Brown5912f952013-07-01 19:10:31 -0700464private:
465 // True if touch resampling is enabled.
466 const bool mResampleTouch;
467
Siarhei Vishniakouce5ab082020-07-09 17:03:21 -0500468 std::shared_ptr<InputChannel> mChannel;
Jeff Brown5912f952013-07-01 19:10:31 -0700469
470 // The current input message.
471 InputMessage mMsg;
472
473 // True if mMsg contains a valid input message that was deferred from the previous
474 // call to consume and that still needs to be handled.
475 bool mMsgDeferred;
476
477 // Batched motion events per device and source.
478 struct Batch {
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500479 std::vector<InputMessage> samples;
Jeff Brown5912f952013-07-01 19:10:31 -0700480 };
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500481 std::vector<Batch> mBatches;
Jeff Brown5912f952013-07-01 19:10:31 -0700482
483 // Touch state per device and source, only for sources of class pointer.
484 struct History {
485 nsecs_t eventTime;
486 BitSet32 idBits;
487 int32_t idToIndex[MAX_POINTER_ID + 1];
488 PointerCoords pointers[MAX_POINTERS];
489
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100490 void initializeFrom(const InputMessage& msg) {
491 eventTime = msg.body.motion.eventTime;
Jeff Brown5912f952013-07-01 19:10:31 -0700492 idBits.clear();
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100493 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
494 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -0700495 idBits.markBit(id);
496 idToIndex[id] = i;
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100497 pointers[i].copyFrom(msg.body.motion.pointers[i].coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700498 }
499 }
500
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800501 void initializeFrom(const History& other) {
502 eventTime = other.eventTime;
503 idBits = other.idBits; // temporary copy
504 for (size_t i = 0; i < other.idBits.count(); i++) {
505 uint32_t id = idBits.clearFirstMarkedBit();
506 int32_t index = other.idToIndex[id];
507 idToIndex[id] = index;
508 pointers[index].copyFrom(other.pointers[index]);
509 }
510 idBits = other.idBits; // final copy
511 }
512
Jeff Brown5912f952013-07-01 19:10:31 -0700513 const PointerCoords& getPointerById(uint32_t id) const {
514 return pointers[idToIndex[id]];
515 }
Siarhei Vishniakouc7dc3782017-08-24 20:36:28 -0700516
517 bool hasPointerId(uint32_t id) const {
518 return idBits.hasBit(id);
519 }
Jeff Brown5912f952013-07-01 19:10:31 -0700520 };
521 struct TouchState {
522 int32_t deviceId;
523 int32_t source;
524 size_t historyCurrent;
525 size_t historySize;
526 History history[2];
527 History lastResample;
528
529 void initialize(int32_t deviceId, int32_t source) {
530 this->deviceId = deviceId;
531 this->source = source;
532 historyCurrent = 0;
533 historySize = 0;
534 lastResample.eventTime = 0;
535 lastResample.idBits.clear();
536 }
537
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100538 void addHistory(const InputMessage& msg) {
Jeff Brown5912f952013-07-01 19:10:31 -0700539 historyCurrent ^= 1;
540 if (historySize < 2) {
541 historySize += 1;
542 }
543 history[historyCurrent].initializeFrom(msg);
544 }
545
546 const History* getHistory(size_t index) const {
547 return &history[(historyCurrent + index) & 1];
548 }
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100549
550 bool recentCoordinatesAreIdentical(uint32_t id) const {
551 // Return true if the two most recently received "raw" coordinates are identical
552 if (historySize < 2) {
553 return false;
554 }
Siarhei Vishniakouc7dc3782017-08-24 20:36:28 -0700555 if (!getHistory(0)->hasPointerId(id) || !getHistory(1)->hasPointerId(id)) {
556 return false;
557 }
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100558 float currentX = getHistory(0)->getPointerById(id).getX();
559 float currentY = getHistory(0)->getPointerById(id).getY();
560 float previousX = getHistory(1)->getPointerById(id).getX();
561 float previousY = getHistory(1)->getPointerById(id).getY();
562 if (currentX == previousX && currentY == previousY) {
563 return true;
564 }
565 return false;
566 }
Jeff Brown5912f952013-07-01 19:10:31 -0700567 };
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500568 std::vector<TouchState> mTouchStates;
Jeff Brown5912f952013-07-01 19:10:31 -0700569
570 // Chain of batched sequence numbers. When multiple input messages are combined into
571 // a batch, we append a record here that associates the last sequence number in the
572 // batch with the previous one. When the finished signal is sent, we traverse the
573 // chain to individually finish all input messages that were part of the batch.
574 struct SeqChain {
575 uint32_t seq; // sequence number of batched input message
576 uint32_t chain; // sequence number of previous batched input message
577 };
Siarhei Vishniakoua64c1592020-06-22 12:02:29 -0500578 std::vector<SeqChain> mSeqChains;
Jeff Brown5912f952013-07-01 19:10:31 -0700579
580 status_t consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800581 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700582 status_t consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800583 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700584
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100585 void updateTouchState(InputMessage& msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700586 void resampleTouchState(nsecs_t frameTime, MotionEvent* event,
587 const InputMessage *next);
588
589 ssize_t findBatch(int32_t deviceId, int32_t source) const;
590 ssize_t findTouchState(int32_t deviceId, int32_t source) const;
591
592 status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
593
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800594 static void rewriteMessage(TouchState& state, InputMessage& msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700595 static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
596 static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800597 static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg);
Prabir Pradhan3f37b7b2020-11-10 16:50:18 -0800598 static void initializeCaptureEvent(CaptureEvent* event, const InputMessage* msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700599 static void addSample(MotionEvent* event, const InputMessage* msg);
600 static bool canAddSample(const Batch& batch, const InputMessage* msg);
601 static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
602 static bool shouldResampleTool(int32_t toolType);
603
604 static bool isTouchResamplingEnabled();
605};
606
607} // namespace android
608
609#endif // _LIBINPUT_INPUT_TRANSPORT_H