blob: 0219cf7c2a45b2202e153d76ce74d2804cc1639b [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>
Jeff Brown5912f952013-07-01 19:10:31 -070040#include <utils/BitSet.h>
Atif Niyaz3d3fa522019-07-25 11:12:39 -070041#include <utils/Errors.h>
42#include <utils/RefBase.h>
43#include <utils/Timers.h>
44#include <utils/Vector.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,
Jeff Brown5912f952013-07-01 19:10:31 -070070 };
71
72 struct Header {
Siarhei Vishniakou52402772019-10-22 09:32:30 -070073 Type type; // 4 bytes
Fengwei Yin83e0e422014-05-24 05:32:09 +080074 // We don't need this field in order to align the body below but we
75 // leave it here because InputMessage::size() and other functions
76 // compute the size of this structure as sizeof(Header) + sizeof(Body).
77 uint32_t padding;
Jeff Brown5912f952013-07-01 19:10:31 -070078 } header;
79
Fengwei Yin83e0e422014-05-24 05:32:09 +080080 // Body *must* be 8 byte aligned.
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060081 // For keys and motions, rely on the fact that std::array takes up exactly as much space
82 // as the underlying data. This is not guaranteed by C++, but it simplifies the conversions.
83 static_assert(sizeof(std::array<uint8_t, 32>) == 32);
Jeff Brown5912f952013-07-01 19:10:31 -070084 union Body {
85 struct Key {
86 uint32_t seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -080087 int32_t eventId;
Fengwei Yin83e0e422014-05-24 05:32:09 +080088 nsecs_t eventTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -070089 int32_t deviceId;
90 int32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010091 int32_t displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -060092 std::array<uint8_t, 32> hmac;
Jeff Brown5912f952013-07-01 19:10:31 -070093 int32_t action;
94 int32_t flags;
95 int32_t keyCode;
96 int32_t scanCode;
97 int32_t metaState;
98 int32_t repeatCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -080099 uint32_t empty2;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800100 nsecs_t downTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700101
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800102 inline size_t size() const { return sizeof(Key); }
Jeff Brown5912f952013-07-01 19:10:31 -0700103 } key;
104
105 struct Motion {
106 uint32_t seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800107 int32_t eventId;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800108 nsecs_t eventTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700109 int32_t deviceId;
110 int32_t source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700111 int32_t displayId;
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600112 std::array<uint8_t, 32> hmac;
Jeff Brown5912f952013-07-01 19:10:31 -0700113 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100114 int32_t actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700115 int32_t flags;
116 int32_t metaState;
117 int32_t buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800118 MotionClassification classification; // base type: uint8_t
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800119 uint8_t empty2[3]; // 3 bytes to fill gap created by classification
Jeff Brown5912f952013-07-01 19:10:31 -0700120 int32_t edgeFlags;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800121 nsecs_t downTime __attribute__((aligned(8)));
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600122 float xScale;
123 float yScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700124 float xOffset;
125 float yOffset;
126 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 {
156 uint32_t seq;
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 {
163 uint32_t seq;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800164 int32_t eventId;
165 uint32_t empty1;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800166 // The following two fields take up 4 bytes total
167 uint16_t hasFocus; // actually a bool
168 uint16_t inTouchMode; // actually a bool, but we must maintain 8-byte alignment
169
170 inline size_t size() const { return sizeof(Focus); }
171 } focus;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800172 } __attribute__((aligned(8))) body;
Jeff Brown5912f952013-07-01 19:10:31 -0700173
174 bool isValid(size_t actualSize) const;
175 size_t size() const;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800176 void getSanitizedCopy(InputMessage* msg) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700177};
178
Chris Ye0783e992020-06-02 21:34:49 -0700179struct InputChannelInfo : public Parcelable {
180 std::string mName;
181 android::base::unique_fd mFd;
182 sp<IBinder> mToken;
183
184 InputChannelInfo() = default;
185 InputChannelInfo(const std::string& name, android::base::unique_fd fd, sp<IBinder> token)
186 : mName(name), mFd(std::move(fd)), mToken(token){};
187 status_t readFromParcel(const android::Parcel* parcel) override;
188 status_t writeToParcel(android::Parcel* parcel) const override;
189};
190
Jeff Brown5912f952013-07-01 19:10:31 -0700191/*
192 * An input channel consists of a local unix domain socket used to send and receive
193 * input messages across processes. Each channel has a descriptive name for debugging purposes.
194 *
195 * Each endpoint has its own InputChannel object that specifies its file descriptor.
196 *
197 * The input channel is closed when all references to it are released.
198 */
199class InputChannel : public RefBase {
Chris Ye0783e992020-06-02 21:34:49 -0700200public:
201 InputChannel();
Jeff Brown5912f952013-07-01 19:10:31 -0700202 virtual ~InputChannel();
203
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700204 static sp<InputChannel> create(const std::string& name, android::base::unique_fd fd,
205 sp<IBinder> token);
Jeff Brown5912f952013-07-01 19:10:31 -0700206
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700207 /**
208 * Create a pair of input channels.
209 * The two returned input channels are equivalent, and are labeled as "server" and "client"
210 * for convenience. The two input channels share the same token.
Jeff Brown5912f952013-07-01 19:10:31 -0700211 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700212 * Return OK on success.
Jeff Brown5912f952013-07-01 19:10:31 -0700213 */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800214 static status_t openInputChannelPair(const std::string& name,
Jeff Brown5912f952013-07-01 19:10:31 -0700215 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel);
216
Chris Ye0783e992020-06-02 21:34:49 -0700217 inline std::string getName() const { return mInfo.mName; }
218 inline int getFd() const { return mInfo.mFd.get(); }
219 inline sp<IBinder> getToken() const { return mInfo.mToken; }
220 inline InputChannelInfo& getInfo() { return mInfo; }
Jeff Brown5912f952013-07-01 19:10:31 -0700221
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700222 /* Send a message to the other endpoint.
Jeff Brown5912f952013-07-01 19:10:31 -0700223 *
224 * If the channel is full then the message is guaranteed not to have been sent at all.
225 * Try again after the consumer has sent a finished signal indicating that it has
226 * consumed some of the pending messages from the channel.
227 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700228 * Return OK on success.
229 * Return WOULD_BLOCK if the channel is full.
230 * Return DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown5912f952013-07-01 19:10:31 -0700231 * Other errors probably indicate that the channel is broken.
232 */
233 status_t sendMessage(const InputMessage* msg);
234
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700235 /* Receive a message sent by the other endpoint.
Jeff Brown5912f952013-07-01 19:10:31 -0700236 *
237 * If there is no message present, try again after poll() indicates that the fd
238 * is readable.
239 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700240 * Return OK on success.
241 * Return WOULD_BLOCK if there is no message present.
242 * Return DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown5912f952013-07-01 19:10:31 -0700243 * Other errors probably indicate that the channel is broken.
244 */
245 status_t receiveMessage(InputMessage* msg);
246
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700247 /* Return a new object that has a duplicate of this channel's fd. */
Jeff Brown5912f952013-07-01 19:10:31 -0700248 sp<InputChannel> dup() const;
249
Chris Ye0783e992020-06-02 21:34:49 -0700250 status_t readFromParcel(const android::Parcel* parcel);
251
252 status_t writeToParcel(android::Parcel* parcel) const;
Robert Carr3720ed02018-08-08 16:08:27 -0700253
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700254 /**
255 * The connection token is used to identify the input connection, i.e.
256 * the pair of input channels that were created simultaneously. Input channels
257 * are always created in pairs, and the token can be used to find the server-side
258 * input channel from the client-side input channel, and vice versa.
259 *
260 * Do not use connection token to check equality of a specific input channel object
261 * to another, because two different (client and server) input channels will share the
262 * same connection token.
263 *
264 * Return the token that identifies this connection.
265 */
266 sp<IBinder> getConnectionToken() const;
Robert Carr803535b2018-08-02 16:38:15 -0700267
Chris Ye0783e992020-06-02 21:34:49 -0700268 bool operator==(const InputChannel& inputChannel) const {
269 struct stat lhsInfo, rhsInfo;
270 if (fstat(mInfo.mFd.get(), &lhsInfo) != 0) {
271 return false;
272 }
273 if (fstat(inputChannel.getFd(), &rhsInfo) != 0) {
274 return false;
275 }
276 // If file descriptors are pointing to same inode they are duplicated fds.
277 return inputChannel.getName() == getName() &&
278 inputChannel.getConnectionToken() == mInfo.mToken &&
279 lhsInfo.st_ino == rhsInfo.st_ino;
280 }
281
Jeff Brown5912f952013-07-01 19:10:31 -0700282private:
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700283 InputChannel(const std::string& name, android::base::unique_fd fd, sp<IBinder> token);
Chris Ye0783e992020-06-02 21:34:49 -0700284 InputChannelInfo mInfo;
Jeff Brown5912f952013-07-01 19:10:31 -0700285};
286
287/*
288 * Publishes input events to an input channel.
289 */
290class InputPublisher {
291public:
292 /* Creates a publisher associated with an input channel. */
293 explicit InputPublisher(const sp<InputChannel>& channel);
294
295 /* Destroys the publisher and releases its input channel. */
296 ~InputPublisher();
297
298 /* Gets the underlying input channel. */
299 inline sp<InputChannel> getChannel() { return mChannel; }
300
301 /* Publishes a key event to the input channel.
302 *
303 * Returns OK on success.
304 * Returns WOULD_BLOCK if the channel is full.
305 * Returns DEAD_OBJECT if the channel's peer has been closed.
306 * Returns BAD_VALUE if seq is 0.
307 * Other errors probably indicate that the channel is broken.
308 */
Garfield Tan1c7bc862020-01-28 13:24:04 -0800309 status_t publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source,
310 int32_t displayId, std::array<uint8_t, 32> hmac, int32_t action,
311 int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600312 int32_t repeatCount, nsecs_t downTime, nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700313
314 /* Publishes a motion 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 or if pointerCount is less than 1 or greater than MAX_POINTERS.
320 * Other errors probably indicate that the channel is broken.
321 */
Garfield Tan1c7bc862020-01-28 13:24:04 -0800322 status_t publishMotionEvent(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 actionButton, int32_t flags, int32_t edgeFlags,
325 int32_t metaState, int32_t buttonState,
326 MotionClassification classification, float xScale, float yScale,
327 float xOffset, float yOffset, float xPrecision, float yPrecision,
328 float xCursorPosition, float yCursorPosition, nsecs_t downTime,
329 nsecs_t eventTime, uint32_t pointerCount,
330 const PointerProperties* pointerProperties,
Garfield Tan00f511d2019-06-12 16:55:40 -0700331 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700332
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800333 /* Publishes a focus event to the input channel.
334 *
335 * Returns OK on success.
336 * Returns WOULD_BLOCK if the channel is full.
337 * Returns DEAD_OBJECT if the channel's peer has been closed.
338 * Other errors probably indicate that the channel is broken.
339 */
Garfield Tan1c7bc862020-01-28 13:24:04 -0800340 status_t publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus, bool inTouchMode);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800341
Jeff Brown5912f952013-07-01 19:10:31 -0700342 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
343 * If a signal was received, returns the message sequence number,
344 * and whether the consumer handled the message.
345 *
346 * The returned sequence number is never 0 unless the operation failed.
347 *
348 * Returns OK on success.
349 * Returns WOULD_BLOCK if there is no signal present.
350 * Returns DEAD_OBJECT if the channel's peer has been closed.
351 * Other errors probably indicate that the channel is broken.
352 */
353 status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled);
354
355private:
356 sp<InputChannel> mChannel;
357};
358
359/*
360 * Consumes input events from an input channel.
361 */
362class InputConsumer {
363public:
364 /* Creates a consumer associated with an input channel. */
365 explicit InputConsumer(const sp<InputChannel>& channel);
366
367 /* Destroys the consumer and releases its input channel. */
368 ~InputConsumer();
369
370 /* Gets the underlying input channel. */
371 inline sp<InputChannel> getChannel() { return mChannel; }
372
373 /* Consumes an input event from the input channel and copies its contents into
374 * an InputEvent object created using the specified factory.
375 *
376 * Tries to combine a series of move events into larger batches whenever possible.
377 *
378 * If consumeBatches is false, then defers consuming pending batched events if it
379 * is possible for additional samples to be added to them later. Call hasPendingBatch()
380 * to determine whether a pending batch is available to be consumed.
381 *
382 * If consumeBatches is true, then events are still batched but they are consumed
383 * immediately as soon as the input channel is exhausted.
384 *
385 * The frameTime parameter specifies the time when the current display frame started
386 * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown.
387 *
388 * The returned sequence number is never 0 unless the operation failed.
389 *
390 * Returns OK on success.
391 * Returns WOULD_BLOCK if there is no event present.
392 * Returns DEAD_OBJECT if the channel's peer has been closed.
393 * Returns NO_MEMORY if the event could not be created.
394 * Other errors probably indicate that the channel is broken.
395 */
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800396 status_t consume(InputEventFactoryInterface* factory, bool consumeBatches, nsecs_t frameTime,
397 uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700398
399 /* Sends a finished signal to the publisher to inform it that the message
400 * with the specified sequence number has finished being process and whether
401 * the message was handled by the consumer.
402 *
403 * Returns OK on success.
404 * Returns BAD_VALUE if seq is 0.
405 * Other errors probably indicate that the channel is broken.
406 */
407 status_t sendFinishedSignal(uint32_t seq, bool handled);
408
409 /* Returns true if there is a deferred event waiting.
410 *
411 * Should be called after calling consume() to determine whether the consumer
412 * has a deferred event to be processed. Deferred events are somewhat special in
413 * that they have already been removed from the input channel. If the input channel
414 * becomes empty, the client may need to do extra work to ensure that it processes
415 * the deferred event despite the fact that the input channel's file descriptor
416 * is not readable.
417 *
418 * One option is simply to call consume() in a loop until it returns WOULD_BLOCK.
419 * This guarantees that all deferred events will be processed.
420 *
421 * Alternately, the caller can call hasDeferredEvent() to determine whether there is
422 * a deferred event waiting and then ensure that its event loop wakes up at least
423 * one more time to consume the deferred event.
424 */
425 bool hasDeferredEvent() const;
426
427 /* Returns true if there is a pending batch.
428 *
429 * Should be called after calling consume() with consumeBatches == false to determine
430 * whether consume() should be called again later on with consumeBatches == true.
431 */
432 bool hasPendingBatch() const;
433
Arthur Hungc7812be2020-02-27 22:40:27 +0800434 /* Returns the source of first pending batch if exist.
435 *
436 * Should be called after calling consume() with consumeBatches == false to determine
437 * whether consume() should be called again later on with consumeBatches == true.
438 */
439 int32_t getPendingBatchSource() const;
440
Jeff Brown5912f952013-07-01 19:10:31 -0700441private:
442 // True if touch resampling is enabled.
443 const bool mResampleTouch;
444
445 // The input channel.
446 sp<InputChannel> mChannel;
447
448 // The current input message.
449 InputMessage mMsg;
450
451 // True if mMsg contains a valid input message that was deferred from the previous
452 // call to consume and that still needs to be handled.
453 bool mMsgDeferred;
454
455 // Batched motion events per device and source.
456 struct Batch {
457 Vector<InputMessage> samples;
458 };
459 Vector<Batch> mBatches;
460
461 // Touch state per device and source, only for sources of class pointer.
462 struct History {
463 nsecs_t eventTime;
464 BitSet32 idBits;
465 int32_t idToIndex[MAX_POINTER_ID + 1];
466 PointerCoords pointers[MAX_POINTERS];
467
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100468 void initializeFrom(const InputMessage& msg) {
469 eventTime = msg.body.motion.eventTime;
Jeff Brown5912f952013-07-01 19:10:31 -0700470 idBits.clear();
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100471 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
472 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -0700473 idBits.markBit(id);
474 idToIndex[id] = i;
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100475 pointers[i].copyFrom(msg.body.motion.pointers[i].coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700476 }
477 }
478
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800479 void initializeFrom(const History& other) {
480 eventTime = other.eventTime;
481 idBits = other.idBits; // temporary copy
482 for (size_t i = 0; i < other.idBits.count(); i++) {
483 uint32_t id = idBits.clearFirstMarkedBit();
484 int32_t index = other.idToIndex[id];
485 idToIndex[id] = index;
486 pointers[index].copyFrom(other.pointers[index]);
487 }
488 idBits = other.idBits; // final copy
489 }
490
Jeff Brown5912f952013-07-01 19:10:31 -0700491 const PointerCoords& getPointerById(uint32_t id) const {
492 return pointers[idToIndex[id]];
493 }
Siarhei Vishniakouc7dc3782017-08-24 20:36:28 -0700494
495 bool hasPointerId(uint32_t id) const {
496 return idBits.hasBit(id);
497 }
Jeff Brown5912f952013-07-01 19:10:31 -0700498 };
499 struct TouchState {
500 int32_t deviceId;
501 int32_t source;
502 size_t historyCurrent;
503 size_t historySize;
504 History history[2];
505 History lastResample;
506
507 void initialize(int32_t deviceId, int32_t source) {
508 this->deviceId = deviceId;
509 this->source = source;
510 historyCurrent = 0;
511 historySize = 0;
512 lastResample.eventTime = 0;
513 lastResample.idBits.clear();
514 }
515
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100516 void addHistory(const InputMessage& msg) {
Jeff Brown5912f952013-07-01 19:10:31 -0700517 historyCurrent ^= 1;
518 if (historySize < 2) {
519 historySize += 1;
520 }
521 history[historyCurrent].initializeFrom(msg);
522 }
523
524 const History* getHistory(size_t index) const {
525 return &history[(historyCurrent + index) & 1];
526 }
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100527
528 bool recentCoordinatesAreIdentical(uint32_t id) const {
529 // Return true if the two most recently received "raw" coordinates are identical
530 if (historySize < 2) {
531 return false;
532 }
Siarhei Vishniakouc7dc3782017-08-24 20:36:28 -0700533 if (!getHistory(0)->hasPointerId(id) || !getHistory(1)->hasPointerId(id)) {
534 return false;
535 }
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100536 float currentX = getHistory(0)->getPointerById(id).getX();
537 float currentY = getHistory(0)->getPointerById(id).getY();
538 float previousX = getHistory(1)->getPointerById(id).getX();
539 float previousY = getHistory(1)->getPointerById(id).getY();
540 if (currentX == previousX && currentY == previousY) {
541 return true;
542 }
543 return false;
544 }
Jeff Brown5912f952013-07-01 19:10:31 -0700545 };
546 Vector<TouchState> mTouchStates;
547
548 // Chain of batched sequence numbers. When multiple input messages are combined into
549 // a batch, we append a record here that associates the last sequence number in the
550 // batch with the previous one. When the finished signal is sent, we traverse the
551 // chain to individually finish all input messages that were part of the batch.
552 struct SeqChain {
553 uint32_t seq; // sequence number of batched input message
554 uint32_t chain; // sequence number of previous batched input message
555 };
556 Vector<SeqChain> mSeqChains;
557
558 status_t consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800559 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700560 status_t consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800561 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700562
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100563 void updateTouchState(InputMessage& msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700564 void resampleTouchState(nsecs_t frameTime, MotionEvent* event,
565 const InputMessage *next);
566
567 ssize_t findBatch(int32_t deviceId, int32_t source) const;
568 ssize_t findTouchState(int32_t deviceId, int32_t source) const;
569
570 status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
571
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800572 static void rewriteMessage(TouchState& state, InputMessage& msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700573 static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
574 static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800575 static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700576 static void addSample(MotionEvent* event, const InputMessage* msg);
577 static bool canAddSample(const Batch& batch, const InputMessage* msg);
578 static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
579 static bool shouldResampleTool(int32_t toolType);
580
581 static bool isTouchResamplingEnabled();
582};
583
584} // namespace android
585
586#endif // _LIBINPUT_INPUT_TRANSPORT_H