blob: ae47438ac85bf9f0cfd1d306dd73190fdfd7c0df [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.
Jeff Brown5912f952013-07-01 19:10:31 -070079 union Body {
80 struct Key {
81 uint32_t seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -080082 uint32_t empty1;
Fengwei Yin83e0e422014-05-24 05:32:09 +080083 nsecs_t eventTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -070084 int32_t deviceId;
85 int32_t source;
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +010086 int32_t displayId;
Jeff Brown5912f952013-07-01 19:10:31 -070087 int32_t action;
88 int32_t flags;
89 int32_t keyCode;
90 int32_t scanCode;
91 int32_t metaState;
92 int32_t repeatCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -080093 uint32_t empty2;
Fengwei Yin83e0e422014-05-24 05:32:09 +080094 nsecs_t downTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -070095
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -080096 inline size_t size() const { return sizeof(Key); }
Jeff Brown5912f952013-07-01 19:10:31 -070097 } key;
98
99 struct Motion {
100 uint32_t seq;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800101 uint32_t empty1;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800102 nsecs_t eventTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700103 int32_t deviceId;
104 int32_t source;
Tarandeep Singh58641502017-07-31 10:51:54 -0700105 int32_t displayId;
Jeff Brown5912f952013-07-01 19:10:31 -0700106 int32_t action;
Michael Wright7b159c92015-05-14 14:48:03 +0100107 int32_t actionButton;
Jeff Brown5912f952013-07-01 19:10:31 -0700108 int32_t flags;
109 int32_t metaState;
110 int32_t buttonState;
Siarhei Vishniakou16a2e302019-01-14 19:21:45 -0800111 MotionClassification classification; // base type: uint8_t
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800112 uint8_t empty2[3]; // 3 bytes to fill gap created by classification
Jeff Brown5912f952013-07-01 19:10:31 -0700113 int32_t edgeFlags;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800114 nsecs_t downTime __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700115 float xOffset;
116 float yOffset;
117 float xPrecision;
118 float yPrecision;
Garfield Tan00f511d2019-06-12 16:55:40 -0700119 float xCursorPosition;
120 float yCursorPosition;
Narayan Kamathed5fd382014-05-02 17:53:33 +0100121 uint32_t pointerCount;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800122 uint32_t empty3;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800123 /**
124 * The "pointers" field must be the last field of the struct InputMessage.
125 * When we send the struct InputMessage across the socket, we are not
126 * writing the entire "pointers" array, but only the pointerCount portion
127 * of it as an optimization. Adding a field after "pointers" would break this.
128 */
Michael Wrightb03f1032015-05-14 16:29:13 +0100129 struct Pointer {
Jeff Brown5912f952013-07-01 19:10:31 -0700130 PointerProperties properties;
131 PointerCoords coords;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800132 } pointers[MAX_POINTERS] __attribute__((aligned(8)));
Jeff Brown5912f952013-07-01 19:10:31 -0700133
134 int32_t getActionId() const {
135 uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
136 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
137 return pointers[index].properties.id;
138 }
139
140 inline size_t size() const {
141 return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
142 + sizeof(Pointer) * pointerCount;
143 }
144 } motion;
145
146 struct Finished {
147 uint32_t seq;
Siarhei Vishniakou10fe6762019-11-25 11:44:11 -0800148 uint32_t handled; // actually a bool, but we must maintain 8-byte alignment
Jeff Brown5912f952013-07-01 19:10:31 -0700149
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800150 inline size_t size() const { return sizeof(Finished); }
Jeff Brown5912f952013-07-01 19:10:31 -0700151 } finished;
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800152
153 struct Focus {
154 uint32_t seq;
155 // The following two fields take up 4 bytes total
156 uint16_t hasFocus; // actually a bool
157 uint16_t inTouchMode; // actually a bool, but we must maintain 8-byte alignment
158
159 inline size_t size() const { return sizeof(Focus); }
160 } focus;
Fengwei Yin83e0e422014-05-24 05:32:09 +0800161 } __attribute__((aligned(8))) body;
Jeff Brown5912f952013-07-01 19:10:31 -0700162
163 bool isValid(size_t actualSize) const;
164 size_t size() const;
Siarhei Vishniakou1f7c0e42018-11-16 22:18:53 -0800165 void getSanitizedCopy(InputMessage* msg) const;
Jeff Brown5912f952013-07-01 19:10:31 -0700166};
167
168/*
169 * An input channel consists of a local unix domain socket used to send and receive
170 * input messages across processes. Each channel has a descriptive name for debugging purposes.
171 *
172 * Each endpoint has its own InputChannel object that specifies its file descriptor.
173 *
174 * The input channel is closed when all references to it are released.
175 */
176class InputChannel : public RefBase {
177protected:
178 virtual ~InputChannel();
179
180public:
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700181 static sp<InputChannel> create(const std::string& name, android::base::unique_fd fd,
182 sp<IBinder> token);
Jeff Brown5912f952013-07-01 19:10:31 -0700183
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700184 /**
185 * Create a pair of input channels.
186 * The two returned input channels are equivalent, and are labeled as "server" and "client"
187 * for convenience. The two input channels share the same token.
Jeff Brown5912f952013-07-01 19:10:31 -0700188 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700189 * Return OK on success.
Jeff Brown5912f952013-07-01 19:10:31 -0700190 */
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800191 static status_t openInputChannelPair(const std::string& name,
Jeff Brown5912f952013-07-01 19:10:31 -0700192 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel);
193
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800194 inline std::string getName() const { return mName; }
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700195 inline int getFd() const { return mFd.get(); }
Jeff Brown5912f952013-07-01 19:10:31 -0700196
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700197 /* Send a message to the other endpoint.
Jeff Brown5912f952013-07-01 19:10:31 -0700198 *
199 * If the channel is full then the message is guaranteed not to have been sent at all.
200 * Try again after the consumer has sent a finished signal indicating that it has
201 * consumed some of the pending messages from the channel.
202 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700203 * Return OK on success.
204 * Return WOULD_BLOCK if the channel is full.
205 * Return DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown5912f952013-07-01 19:10:31 -0700206 * Other errors probably indicate that the channel is broken.
207 */
208 status_t sendMessage(const InputMessage* msg);
209
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700210 /* Receive a message sent by the other endpoint.
Jeff Brown5912f952013-07-01 19:10:31 -0700211 *
212 * If there is no message present, try again after poll() indicates that the fd
213 * is readable.
214 *
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700215 * Return OK on success.
216 * Return WOULD_BLOCK if there is no message present.
217 * Return DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown5912f952013-07-01 19:10:31 -0700218 * Other errors probably indicate that the channel is broken.
219 */
220 status_t receiveMessage(InputMessage* msg);
221
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700222 /* Return a new object that has a duplicate of this channel's fd. */
Jeff Brown5912f952013-07-01 19:10:31 -0700223 sp<InputChannel> dup() const;
224
Robert Carr3720ed02018-08-08 16:08:27 -0700225 status_t write(Parcel& out) const;
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700226 static sp<InputChannel> read(const Parcel& from);
Robert Carr3720ed02018-08-08 16:08:27 -0700227
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700228 /**
229 * The connection token is used to identify the input connection, i.e.
230 * the pair of input channels that were created simultaneously. Input channels
231 * are always created in pairs, and the token can be used to find the server-side
232 * input channel from the client-side input channel, and vice versa.
233 *
234 * Do not use connection token to check equality of a specific input channel object
235 * to another, because two different (client and server) input channels will share the
236 * same connection token.
237 *
238 * Return the token that identifies this connection.
239 */
240 sp<IBinder> getConnectionToken() const;
Robert Carr803535b2018-08-02 16:38:15 -0700241
Jeff Brown5912f952013-07-01 19:10:31 -0700242private:
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700243 InputChannel(const std::string& name, android::base::unique_fd fd, sp<IBinder> token);
Siarhei Vishniakouf93fcf42017-11-22 16:00:14 -0800244 std::string mName;
Josh Gao2ccbe3a2019-08-09 14:35:36 -0700245 android::base::unique_fd mFd;
Robert Carr803535b2018-08-02 16:38:15 -0700246
Siarhei Vishniakou26d3cfb2019-10-15 17:02:32 -0700247 sp<IBinder> mToken;
Jeff Brown5912f952013-07-01 19:10:31 -0700248};
249
250/*
251 * Publishes input events to an input channel.
252 */
253class InputPublisher {
254public:
255 /* Creates a publisher associated with an input channel. */
256 explicit InputPublisher(const sp<InputChannel>& channel);
257
258 /* Destroys the publisher and releases its input channel. */
259 ~InputPublisher();
260
261 /* Gets the underlying input channel. */
262 inline sp<InputChannel> getChannel() { return mChannel; }
263
264 /* Publishes a key event to the input channel.
265 *
266 * Returns OK on success.
267 * Returns WOULD_BLOCK if the channel is full.
268 * Returns DEAD_OBJECT if the channel's peer has been closed.
269 * Returns BAD_VALUE if seq is 0.
270 * Other errors probably indicate that the channel is broken.
271 */
272 status_t publishKeyEvent(
273 uint32_t seq,
274 int32_t deviceId,
275 int32_t source,
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +0100276 int32_t displayId,
Jeff Brown5912f952013-07-01 19:10:31 -0700277 int32_t action,
278 int32_t flags,
279 int32_t keyCode,
280 int32_t scanCode,
281 int32_t metaState,
282 int32_t repeatCount,
283 nsecs_t downTime,
284 nsecs_t eventTime);
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 Tan00f511d2019-06-12 16:55:40 -0700294 status_t publishMotionEvent(uint32_t seq, int32_t deviceId, int32_t source, int32_t displayId,
295 int32_t action, int32_t actionButton, int32_t flags,
296 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
297 MotionClassification classification, float xOffset, float yOffset,
298 float xPrecision, float yPrecision, float xCursorPosition,
299 float yCursorPosition, nsecs_t downTime, nsecs_t eventTime,
300 uint32_t pointerCount, const PointerProperties* pointerProperties,
301 const PointerCoords* pointerCoords);
Jeff Brown5912f952013-07-01 19:10:31 -0700302
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800303 /* Publishes a focus event to the input channel.
304 *
305 * Returns OK on success.
306 * Returns WOULD_BLOCK if the channel is full.
307 * Returns DEAD_OBJECT if the channel's peer has been closed.
308 * Other errors probably indicate that the channel is broken.
309 */
310 status_t publishFocusEvent(uint32_t seq, bool hasFocus, bool inTouchMode);
311
Jeff Brown5912f952013-07-01 19:10:31 -0700312 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
313 * If a signal was received, returns the message sequence number,
314 * and whether the consumer handled the message.
315 *
316 * The returned sequence number is never 0 unless the operation failed.
317 *
318 * Returns OK on success.
319 * Returns WOULD_BLOCK if there is no signal present.
320 * Returns DEAD_OBJECT if the channel's peer has been closed.
321 * Other errors probably indicate that the channel is broken.
322 */
323 status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled);
324
325private:
Atif Niyaz3d3fa522019-07-25 11:12:39 -0700326
Jeff Brown5912f952013-07-01 19:10:31 -0700327 sp<InputChannel> mChannel;
328};
329
330/*
331 * Consumes input events from an input channel.
332 */
333class InputConsumer {
334public:
335 /* Creates a consumer associated with an input channel. */
336 explicit InputConsumer(const sp<InputChannel>& channel);
337
338 /* Destroys the consumer and releases its input channel. */
339 ~InputConsumer();
340
341 /* Gets the underlying input channel. */
342 inline sp<InputChannel> getChannel() { return mChannel; }
343
344 /* Consumes an input event from the input channel and copies its contents into
345 * an InputEvent object created using the specified factory.
346 *
347 * Tries to combine a series of move events into larger batches whenever possible.
348 *
349 * If consumeBatches is false, then defers consuming pending batched events if it
350 * is possible for additional samples to be added to them later. Call hasPendingBatch()
351 * to determine whether a pending batch is available to be consumed.
352 *
353 * If consumeBatches is true, then events are still batched but they are consumed
354 * immediately as soon as the input channel is exhausted.
355 *
356 * The frameTime parameter specifies the time when the current display frame started
357 * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown.
358 *
359 * The returned sequence number is never 0 unless the operation failed.
360 *
361 * Returns OK on success.
362 * Returns WOULD_BLOCK if there is no event present.
363 * Returns DEAD_OBJECT if the channel's peer has been closed.
364 * Returns NO_MEMORY if the event could not be created.
365 * Other errors probably indicate that the channel is broken.
366 */
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800367 status_t consume(InputEventFactoryInterface* factory, bool consumeBatches, nsecs_t frameTime,
368 uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700369
370 /* Sends a finished signal to the publisher to inform it that the message
371 * with the specified sequence number has finished being process and whether
372 * the message was handled by the consumer.
373 *
374 * Returns OK on success.
375 * Returns BAD_VALUE if seq is 0.
376 * Other errors probably indicate that the channel is broken.
377 */
378 status_t sendFinishedSignal(uint32_t seq, bool handled);
379
380 /* Returns true if there is a deferred event waiting.
381 *
382 * Should be called after calling consume() to determine whether the consumer
383 * has a deferred event to be processed. Deferred events are somewhat special in
384 * that they have already been removed from the input channel. If the input channel
385 * becomes empty, the client may need to do extra work to ensure that it processes
386 * the deferred event despite the fact that the input channel's file descriptor
387 * is not readable.
388 *
389 * One option is simply to call consume() in a loop until it returns WOULD_BLOCK.
390 * This guarantees that all deferred events will be processed.
391 *
392 * Alternately, the caller can call hasDeferredEvent() to determine whether there is
393 * a deferred event waiting and then ensure that its event loop wakes up at least
394 * one more time to consume the deferred event.
395 */
396 bool hasDeferredEvent() const;
397
398 /* Returns true if there is a pending batch.
399 *
400 * Should be called after calling consume() with consumeBatches == false to determine
401 * whether consume() should be called again later on with consumeBatches == true.
402 */
403 bool hasPendingBatch() const;
404
405private:
406 // True if touch resampling is enabled.
407 const bool mResampleTouch;
408
409 // The input channel.
410 sp<InputChannel> mChannel;
411
412 // The current input message.
413 InputMessage mMsg;
414
415 // True if mMsg contains a valid input message that was deferred from the previous
416 // call to consume and that still needs to be handled.
417 bool mMsgDeferred;
418
419 // Batched motion events per device and source.
420 struct Batch {
421 Vector<InputMessage> samples;
422 };
423 Vector<Batch> mBatches;
424
425 // Touch state per device and source, only for sources of class pointer.
426 struct History {
427 nsecs_t eventTime;
428 BitSet32 idBits;
429 int32_t idToIndex[MAX_POINTER_ID + 1];
430 PointerCoords pointers[MAX_POINTERS];
431
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100432 void initializeFrom(const InputMessage& msg) {
433 eventTime = msg.body.motion.eventTime;
Jeff Brown5912f952013-07-01 19:10:31 -0700434 idBits.clear();
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100435 for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
436 uint32_t id = msg.body.motion.pointers[i].properties.id;
Jeff Brown5912f952013-07-01 19:10:31 -0700437 idBits.markBit(id);
438 idToIndex[id] = i;
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100439 pointers[i].copyFrom(msg.body.motion.pointers[i].coords);
Jeff Brown5912f952013-07-01 19:10:31 -0700440 }
441 }
442
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800443 void initializeFrom(const History& other) {
444 eventTime = other.eventTime;
445 idBits = other.idBits; // temporary copy
446 for (size_t i = 0; i < other.idBits.count(); i++) {
447 uint32_t id = idBits.clearFirstMarkedBit();
448 int32_t index = other.idToIndex[id];
449 idToIndex[id] = index;
450 pointers[index].copyFrom(other.pointers[index]);
451 }
452 idBits = other.idBits; // final copy
453 }
454
Jeff Brown5912f952013-07-01 19:10:31 -0700455 const PointerCoords& getPointerById(uint32_t id) const {
456 return pointers[idToIndex[id]];
457 }
Siarhei Vishniakouc7dc3782017-08-24 20:36:28 -0700458
459 bool hasPointerId(uint32_t id) const {
460 return idBits.hasBit(id);
461 }
Jeff Brown5912f952013-07-01 19:10:31 -0700462 };
463 struct TouchState {
464 int32_t deviceId;
465 int32_t source;
466 size_t historyCurrent;
467 size_t historySize;
468 History history[2];
469 History lastResample;
470
471 void initialize(int32_t deviceId, int32_t source) {
472 this->deviceId = deviceId;
473 this->source = source;
474 historyCurrent = 0;
475 historySize = 0;
476 lastResample.eventTime = 0;
477 lastResample.idBits.clear();
478 }
479
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100480 void addHistory(const InputMessage& msg) {
Jeff Brown5912f952013-07-01 19:10:31 -0700481 historyCurrent ^= 1;
482 if (historySize < 2) {
483 historySize += 1;
484 }
485 history[historyCurrent].initializeFrom(msg);
486 }
487
488 const History* getHistory(size_t index) const {
489 return &history[(historyCurrent + index) & 1];
490 }
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100491
492 bool recentCoordinatesAreIdentical(uint32_t id) const {
493 // Return true if the two most recently received "raw" coordinates are identical
494 if (historySize < 2) {
495 return false;
496 }
Siarhei Vishniakouc7dc3782017-08-24 20:36:28 -0700497 if (!getHistory(0)->hasPointerId(id) || !getHistory(1)->hasPointerId(id)) {
498 return false;
499 }
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100500 float currentX = getHistory(0)->getPointerById(id).getX();
501 float currentY = getHistory(0)->getPointerById(id).getY();
502 float previousX = getHistory(1)->getPointerById(id).getX();
503 float previousY = getHistory(1)->getPointerById(id).getY();
504 if (currentX == previousX && currentY == previousY) {
505 return true;
506 }
507 return false;
508 }
Jeff Brown5912f952013-07-01 19:10:31 -0700509 };
510 Vector<TouchState> mTouchStates;
511
512 // Chain of batched sequence numbers. When multiple input messages are combined into
513 // a batch, we append a record here that associates the last sequence number in the
514 // batch with the previous one. When the finished signal is sent, we traverse the
515 // chain to individually finish all input messages that were part of the batch.
516 struct SeqChain {
517 uint32_t seq; // sequence number of batched input message
518 uint32_t chain; // sequence number of previous batched input message
519 };
520 Vector<SeqChain> mSeqChains;
521
522 status_t consumeBatch(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800523 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700524 status_t consumeSamples(InputEventFactoryInterface* factory,
Siarhei Vishniakou777a10b2018-01-31 16:45:06 -0800525 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown5912f952013-07-01 19:10:31 -0700526
Siarhei Vishniakou086a02a2017-06-12 15:01:41 +0100527 void updateTouchState(InputMessage& msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700528 void resampleTouchState(nsecs_t frameTime, MotionEvent* event,
529 const InputMessage *next);
530
531 ssize_t findBatch(int32_t deviceId, int32_t source) const;
532 ssize_t findTouchState(int32_t deviceId, int32_t source) const;
533
534 status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
535
Siarhei Vishniakou56c9ae12017-11-06 21:16:47 -0800536 static void rewriteMessage(TouchState& state, InputMessage& msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700537 static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
538 static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
Siarhei Vishniakou7feb2ea2019-11-25 15:11:23 -0800539 static void initializeFocusEvent(FocusEvent* event, const InputMessage* msg);
Jeff Brown5912f952013-07-01 19:10:31 -0700540 static void addSample(MotionEvent* event, const InputMessage* msg);
541 static bool canAddSample(const Batch& batch, const InputMessage* msg);
542 static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
543 static bool shouldResampleTool(int32_t toolType);
544
545 static bool isTouchResamplingEnabled();
546};
547
548} // namespace android
549
550#endif // _LIBINPUT_INPUT_TRANSPORT_H