blob: 06fd3bb364725157c28ea5186c97abbfb49beffe [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>
Jeff Brown5912f952013-07-01 19:10:31 -070037#include <input/Input.h>
Jeff Brown5912f952013-07-01 19:10:31 -070038#include <utils/BitSet.h>
Atif Niyaz3d3fa522019-07-25 11:12:39 -070039#include <utils/Errors.h>
40#include <utils/RefBase.h>
41#include <utils/Timers.h>
42#include <utils/Vector.h>
Jeff Brown5912f952013-07-01 19:10:31 -070043
Josh Gao2ccbe3a2019-08-09 14:35:36 -070044#include <android-base/unique_fd.h>
45
Jeff Brown5912f952013-07-01 19:10:31 -070046namespace android {
Robert Carr3720ed02018-08-08 16:08:27 -070047class Parcel;
Jeff Brown5912f952013-07-01 19:10:31 -070048
49/*
50 * Intermediate representation used to send input events and related signals.
Fengwei Yin83e0e422014-05-24 05:32:09 +080051 *
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 Vishniakou1f7c0e42018-11-16 22:18:53 -080054 *
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 Brown5912f952013-07-01 19:10:31 -070061 */
62struct InputMessage {
Siarhei Vishniakou52402772019-10-22 09:32:30 -070063 enum class Type : uint32_t {
64 KEY,
65 MOTION,
66 FINISHED,
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080067 FOCUS,
Jeff Brown5912f952013-07-01 19:10:31 -070068 };
69
70 struct Header {
Siarhei Vishniakou52402772019-10-22 09:32:30 -070071 Type type; // 4 bytes
Fengwei Yin83e0e422014-05-24 05:32:09 +080072 // 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 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 {
84 uint32_t seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -080085 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 {
104 uint32_t seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800105 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)));
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600120 float xScale;
121 float yScale;
Jeff Brown5912f952013-07-01 19:10:31 -0700122 float xOffset;
123 float yOffset;
124 float xPrecision;
125 float yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700126 float xCursorPosition;
127 float yCursorPosition;
Narayan Kamathed5fd382014-05-02 17:53:33 +0100128 uint32_t pointerCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800129 uint32_t empty3;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800130 /**
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 Wrightb03f1032015-05-14 16:29:13 +0100136 struct Pointer {
Jeff Brown5912f952013-07-01 19:10:31 -0700137 PointerProperties properties;
138 PointerCoords coords;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800139 } pointers[MAX_POINTERS] __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700140
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 Vishniakou10fe6762019-11-25 11:44:11 -0800155 uint32_t handled; // actually a bool, but we must maintain 8-byte alignment
Jeff Brown5912f952013-07-01 19:10:31 -0700156
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800157 inline size_t size() const { return sizeof(Finished); }
Jeff Brown5912f952013-07-01 19:10:31 -0700158 } finished;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800159
160 struct Focus {
161 uint32_t seq;
162 // The following two fields take up 4 bytes total
163 uint16_t hasFocus; // actually a bool
164 uint16_t inTouchMode; // actually a bool, but we must maintain 8-byte alignment
165
166 inline size_t size() const { return sizeof(Focus); }
167 } focus;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800168 } __attribute__((aligned(8))) body;
Jeff Brown5912f952013-07-01 19:10:31 -0700169
170 bool isValid(size_t actualSize) const;
171 size_t size() const;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800172 void getSanitizedCopy(InputMessage* msg) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700173};
174
175/*
176 * An input channel consists of a local unix domain socket used to send and receive
177 * input messages across processes. Each channel has a descriptive name for debugging purposes.
178 *
179 * Each endpoint has its own InputChannel object that specifies its file descriptor.
180 *
181 * The input channel is closed when all references to it are released.
182 */
183class InputChannel : public RefBase {
184protected:
185 virtual ~InputChannel();
186
187public:
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700188 static sp<InputChannel> create(const std::string& name, android::base::unique_fd fd,
189 sp<IBinder> token);
Jeff Brown5912f952013-07-01 19:10:31 -0700190
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700191 /**
192 * Create a pair of input channels.
193 * The two returned input channels are equivalent, and are labeled as "server" and "client"
194 * for convenience. The two input channels share the same token.
Jeff Brown5912f952013-07-01 19:10:31 -0700195 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700196 * Return OK on success.
Jeff Brown5912f952013-07-01 19:10:31 -0700197 */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800198 static status_t openInputChannelPair(const std::string& name,
Jeff Brown5912f952013-07-01 19:10:31 -0700199 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel);
200
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800201 inline std::string getName() const { return mName; }
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700202 inline int getFd() const { return mFd.get(); }
Jeff Brown5912f952013-07-01 19:10:31 -0700203
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700204 /* Send a message to the other endpoint.
Jeff Brown5912f952013-07-01 19:10:31 -0700205 *
206 * If the channel is full then the message is guaranteed not to have been sent at all.
207 * Try again after the consumer has sent a finished signal indicating that it has
208 * consumed some of the pending messages from the channel.
209 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700210 * Return OK on success.
211 * Return WOULD_BLOCK if the channel is full.
212 * Return DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown5912f952013-07-01 19:10:31 -0700213 * Other errors probably indicate that the channel is broken.
214 */
215 status_t sendMessage(const InputMessage* msg);
216
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700217 /* Receive a message sent by the other endpoint.
Jeff Brown5912f952013-07-01 19:10:31 -0700218 *
219 * If there is no message present, try again after poll() indicates that the fd
220 * is readable.
221 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700222 * Return OK on success.
223 * Return WOULD_BLOCK if there is no message present.
224 * Return DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown5912f952013-07-01 19:10:31 -0700225 * Other errors probably indicate that the channel is broken.
226 */
227 status_t receiveMessage(InputMessage* msg);
228
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700229 /* Return a new object that has a duplicate of this channel's fd. */
Jeff Brown5912f952013-07-01 19:10:31 -0700230 sp<InputChannel> dup() const;
231
Robert Carr3720ed02018-08-08 16:08:27 -0700232 status_t write(Parcel& out) const;
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700233 static sp<InputChannel> read(const Parcel& from);
Robert Carr3720ed02018-08-08 16:08:27 -0700234
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700235 /**
236 * The connection token is used to identify the input connection, i.e.
237 * the pair of input channels that were created simultaneously. Input channels
238 * are always created in pairs, and the token can be used to find the server-side
239 * input channel from the client-side input channel, and vice versa.
240 *
241 * Do not use connection token to check equality of a specific input channel object
242 * to another, because two different (client and server) input channels will share the
243 * same connection token.
244 *
245 * Return the token that identifies this connection.
246 */
247 sp<IBinder> getConnectionToken() const;
Robert Carr803535b2018-08-02 16:38:15 -0700248
Jeff Brown5912f952013-07-01 19:10:31 -0700249private:
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700250 InputChannel(const std::string& name, android::base::unique_fd fd, sp<IBinder> token);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800251 std::string mName;
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700252 android::base::unique_fd mFd;
Robert Carr803535b2018-08-02 16:38:15 -0700253
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700254 sp<IBinder> mToken;
Jeff Brown5912f952013-07-01 19:10:31 -0700255};
256
257/*
258 * Publishes input events to an input channel.
259 */
260class InputPublisher {
261public:
262 /* Creates a publisher associated with an input channel. */
263 explicit InputPublisher(const sp<InputChannel>& channel);
264
265 /* Destroys the publisher and releases its input channel. */
266 ~InputPublisher();
267
268 /* Gets the underlying input channel. */
269 inline sp<InputChannel> getChannel() { return mChannel; }
270
271 /* Publishes a key event to the input channel.
272 *
273 * Returns OK on success.
274 * Returns WOULD_BLOCK if the channel is full.
275 * Returns DEAD_OBJECT if the channel's peer has been closed.
276 * Returns BAD_VALUE if seq is 0.
277 * Other errors probably indicate that the channel is broken.
278 */
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600279 status_t publishKeyEvent(uint32_t seq, int32_t deviceId, int32_t source, int32_t displayId,
280 std::array<uint8_t, 32> hmac, int32_t action, int32_t flags,
281 int32_t keyCode, int32_t scanCode, int32_t metaState,
282 int32_t repeatCount, nsecs_t downTime, nsecs_t eventTime);
Jeff Brown5912f952013-07-01 19:10:31 -0700283
284 /* Publishes a motion event to the input channel.
285 *
286 * Returns OK on success.
287 * Returns WOULD_BLOCK if the channel is full.
288 * Returns DEAD_OBJECT if the channel's peer has been closed.
289 * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS.
290 * Other errors probably indicate that the channel is broken.
291 */
Garfield Tan00f511d2019-06-12 16:55:40 -0700292 status_t publishMotionEvent(uint32_t seq, int32_t deviceId, int32_t source, int32_t displayId,
Siarhei Vishniakou9c858ac2020-01-23 14:20:11 -0600293 std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton,
294 int32_t flags, int32_t edgeFlags, int32_t metaState,
295 int32_t buttonState, MotionClassification classification,
296 float xScale, float yScale, float xOffset, float yOffset,
Garfield Tan00f511d2019-06-12 16:55:40 -0700297 float xPrecision, float yPrecision, float xCursorPosition,
298 float yCursorPosition, nsecs_t downTime, nsecs_t eventTime,
299 uint32_t pointerCount, const PointerProperties* pointerProperties,
300 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700301
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800302 /* Publishes a focus event to the input channel.
303 *
304 * Returns OK on success.
305 * Returns WOULD_BLOCK if the channel is full.
306 * Returns DEAD_OBJECT if the channel's peer has been closed.
307 * Other errors probably indicate that the channel is broken.
308 */
309 status_t publishFocusEvent(uint32_t seq, bool hasFocus, bool inTouchMode);
310
Jeff Brown5912f952013-07-01 19:10:31 -0700311 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
312 * If a signal was received, returns the message sequence number,
313 * and whether the consumer handled the message.
314 *
315 * The returned sequence number is never 0 unless the operation failed.
316 *
317 * Returns OK on success.
318 * Returns WOULD_BLOCK if there is no signal present.
319 * Returns DEAD_OBJECT if the channel's peer has been closed.
320 * Other errors probably indicate that the channel is broken.
321 */
322 status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled);
323
324private:
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700325
Jeff Brown5912f952013-07-01 19:10:31 -0700326 sp<InputChannel> mChannel;
327};
328
329/*
330 * Consumes input events from an input channel.
331 */
332class InputConsumer {
333public:
334 /* Creates a consumer associated with an input channel. */
335 explicit InputConsumer(const sp<InputChannel>& channel);
336
337 /* Destroys the consumer and releases its input channel. */
338 ~InputConsumer();
339
340 /* Gets the underlying input channel. */
341 inline sp<InputChannel> getChannel() { return mChannel; }
342
343 /* Consumes an input event from the input channel and copies its contents into
344 * an InputEvent object created using the specified factory.
345 *
346 * Tries to combine a series of move events into larger batches whenever possible.
347 *
348 * If consumeBatches is false, then defers consuming pending batched events if it
349 * is possible for additional samples to be added to them later. Call hasPendingBatch()
350 * to determine whether a pending batch is available to be consumed.
351 *
352 * If consumeBatches is true, then events are still batched but they are consumed
353 * immediately as soon as the input channel is exhausted.
354 *
355 * The frameTime parameter specifies the time when the current display frame started
356 * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown.
357 *
358 * The returned sequence number is never 0 unless the operation failed.
359 *
360 * Returns OK on success.
361 * Returns WOULD_BLOCK if there is no event present.
362 * Returns DEAD_OBJECT if the channel's peer has been closed.
363 * Returns NO_MEMORY if the event could not be created.
364 * Other errors probably indicate that the channel is broken.
365 */
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800366 status_t consume(InputEventFactoryInterface* factory, bool consumeBatches, nsecs_t frameTime,
367 uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700368
369 /* Sends a finished signal to the publisher to inform it that the message
370 * with the specified sequence number has finished being process and whether
371 * the message was handled by the consumer.
372 *
373 * Returns OK on success.
374 * Returns BAD_VALUE if seq is 0.
375 * Other errors probably indicate that the channel is broken.
376 */
377 status_t sendFinishedSignal(uint32_t seq, bool handled);
378
379 /* Returns true if there is a deferred event waiting.
380 *
381 * Should be called after calling consume() to determine whether the consumer
382 * has a deferred event to be processed. Deferred events are somewhat special in
383 * that they have already been removed from the input channel. If the input channel
384 * becomes empty, the client may need to do extra work to ensure that it processes
385 * the deferred event despite the fact that the input channel's file descriptor
386 * is not readable.
387 *
388 * One option is simply to call consume() in a loop until it returns WOULD_BLOCK.
389 * This guarantees that all deferred events will be processed.
390 *
391 * Alternately, the caller can call hasDeferredEvent() to determine whether there is
392 * a deferred event waiting and then ensure that its event loop wakes up at least
393 * one more time to consume the deferred event.
394 */
395 bool hasDeferredEvent() const;
396
397 /* Returns true if there is a pending batch.
398 *
399 * Should be called after calling consume() with consumeBatches == false to determine
400 * whether consume() should be called again later on with consumeBatches == true.
401 */
402 bool hasPendingBatch() const;
403
404private:
405 // True if touch resampling is enabled.
406 const bool mResampleTouch;
407
408 // The input channel.
409 sp<InputChannel> mChannel;
410
411 // The current input message.
412 InputMessage mMsg;
413
414 // True if mMsg contains a valid input message that was deferred from the previous
415 // call to consume and that still needs to be handled.
416 bool mMsgDeferred;
417
418 // Batched motion events per device and source.
419 struct Batch {
420 Vector<InputMessage> samples;
421 };
422 Vector<Batch> mBatches;
423
424 // Touch state per device and source, only for sources of class pointer.
425 struct History {
426 nsecs_t eventTime;
427 BitSet32 idBits;
428 int32_t idToIndex[MAX_POINTER_ID + 1];
429 PointerCoords pointers[MAX_POINTERS];
430
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100431 void initializeFrom(const InputMessage& msg) {
432 eventTime = msg.body.motion.eventTime;
Jeff Brown5912f952013-07-01 19:10:31 -0700433 idBits.clear();
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100434 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
435 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -0700436 idBits.markBit(id);
437 idToIndex[id] = i;
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100438 pointers[i].copyFrom(msg.body.motion.pointers[i].coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700439 }
440 }
441
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800442 void initializeFrom(const History& other) {
443 eventTime = other.eventTime;
444 idBits = other.idBits; // temporary copy
445 for (size_t i = 0; i < other.idBits.count(); i++) {
446 uint32_t id = idBits.clearFirstMarkedBit();
447 int32_t index = other.idToIndex[id];
448 idToIndex[id] = index;
449 pointers[index].copyFrom(other.pointers[index]);
450 }
451 idBits = other.idBits; // final copy
452 }
453
Jeff Brown5912f952013-07-01 19:10:31 -0700454 const PointerCoords& getPointerById(uint32_t id) const {
455 return pointers[idToIndex[id]];
456 }
Siarhei Vishniakouc7dc3782017-08-24 20:36:28 -0700457
458 bool hasPointerId(uint32_t id) const {
459 return idBits.hasBit(id);
460 }
Jeff Brown5912f952013-07-01 19:10:31 -0700461 };
462 struct TouchState {
463 int32_t deviceId;
464 int32_t source;
465 size_t historyCurrent;
466 size_t historySize;
467 History history[2];
468 History lastResample;
469
470 void initialize(int32_t deviceId, int32_t source) {
471 this->deviceId = deviceId;
472 this->source = source;
473 historyCurrent = 0;
474 historySize = 0;
475 lastResample.eventTime = 0;
476 lastResample.idBits.clear();
477 }
478
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100479 void addHistory(const InputMessage& msg) {
Jeff Brown5912f952013-07-01 19:10:31 -0700480 historyCurrent ^= 1;
481 if (historySize < 2) {
482 historySize += 1;
483 }
484 history[historyCurrent].initializeFrom(msg);
485 }
486
487 const History* getHistory(size_t index) const {
488 return &history[(historyCurrent + index) & 1];
489 }
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100490
491 bool recentCoordinatesAreIdentical(uint32_t id) const {
492 // Return true if the two most recently received "raw" coordinates are identical
493 if (historySize < 2) {
494 return false;
495 }
Siarhei Vishniakouc7dc3782017-08-24 20:36:28 -0700496 if (!getHistory(0)->hasPointerId(id) || !getHistory(1)->hasPointerId(id)) {
497 return false;
498 }
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100499 float currentX = getHistory(0)->getPointerById(id).getX();
500 float currentY = getHistory(0)->getPointerById(id).getY();
501 float previousX = getHistory(1)->getPointerById(id).getX();
502 float previousY = getHistory(1)->getPointerById(id).getY();
503 if (currentX == previousX && currentY == previousY) {
504 return true;
505 }
506 return false;
507 }
Jeff Brown5912f952013-07-01 19:10:31 -0700508 };
509 Vector<TouchState> mTouchStates;
510
511 // Chain of batched sequence numbers. When multiple input messages are combined into
512 // a batch, we append a record here that associates the last sequence number in the
513 // batch with the previous one. When the finished signal is sent, we traverse the
514 // chain to individually finish all input messages that were part of the batch.
515 struct SeqChain {
516 uint32_t seq; // sequence number of batched input message
517 uint32_t chain; // sequence number of previous batched input message
518 };
519 Vector<SeqChain> mSeqChains;
520
521 status_t consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800522 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700523 status_t consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800524 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700525
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100526 void updateTouchState(InputMessage& msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700527 void resampleTouchState(nsecs_t frameTime, MotionEvent* event,
528 const InputMessage *next);
529
530 ssize_t findBatch(int32_t deviceId, int32_t source) const;
531 ssize_t findTouchState(int32_t deviceId, int32_t source) const;
532
533 status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
534
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800535 static void rewriteMessage(TouchState& state, InputMessage& msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700536 static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
537 static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800538 static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700539 static void addSample(MotionEvent* event, const InputMessage* msg);
540 static bool canAddSample(const Batch& batch, const InputMessage* msg);
541 static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
542 static bool shouldResampleTool(int32_t toolType);
543
544 static bool isTouchResamplingEnabled();
545};
546
547} // namespace android
548
549#endif // _LIBINPUT_INPUT_TRANSPORT_H