1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _LIBINPUT_INPUT_TRANSPORT_H
18 #define _LIBINPUT_INPUT_TRANSPORT_H
19 
20 #pragma GCC system_header
21 
22 /**
23  * Native input transport.
24  *
25  * The InputChannel provides a mechanism for exchanging InputMessage structures across processes.
26  *
27  * The InputPublisher and InputConsumer each handle one end-point of an input channel.
28  * The InputPublisher is used by the input dispatcher to send events to the application.
29  * The InputConsumer is used by the application to receive events from the input dispatcher.
30  */
31 
32 #include <string>
33 
34 #include <binder/IBinder.h>
35 #include <input/Input.h>
36 #include <utils/Errors.h>
37 #include <utils/Timers.h>
38 #include <utils/RefBase.h>
39 #include <utils/Vector.h>
40 #include <utils/BitSet.h>
41 
42 namespace android {
43 class Parcel;
44 
45 /*
46  * Intermediate representation used to send input events and related signals.
47  *
48  * Note that this structure is used for IPCs so its layout must be identical
49  * on 64 and 32 bit processes. This is tested in StructLayout_test.cpp.
50  *
51  * Since the struct must be aligned to an 8-byte boundary, there could be uninitialized bytes
52  * in-between the defined fields. This padding data should be explicitly accounted for by adding
53  * "empty" fields into the struct. This data is memset to zero before sending the struct across
54  * the socket. Adding the explicit fields ensures that the memset is not optimized away by the
55  * compiler. When a new field is added to the struct, the corresponding change
56  * in StructLayout_test should be made.
57  */
58 struct InputMessage {
59     enum {
60         TYPE_KEY = 1,
61         TYPE_MOTION = 2,
62         TYPE_FINISHED = 3,
63     };
64 
65     struct Header {
66         uint32_t type;
67         // We don't need this field in order to align the body below but we
68         // leave it here because InputMessage::size() and other functions
69         // compute the size of this structure as sizeof(Header) + sizeof(Body).
70         uint32_t padding;
71     } header;
72 
73     // Body *must* be 8 byte aligned.
74     union Body {
75         struct Key {
76             uint32_t seq;
77             uint32_t empty1;
78             nsecs_t eventTime __attribute__((aligned(8)));
79             int32_t deviceId;
80             int32_t source;
81             int32_t displayId;
82             int32_t action;
83             int32_t flags;
84             int32_t keyCode;
85             int32_t scanCode;
86             int32_t metaState;
87             int32_t repeatCount;
88             uint32_t empty2;
89             nsecs_t downTime __attribute__((aligned(8)));
90 
sizeInputMessage::Body::Key91             inline size_t size() const {
92                 return sizeof(Key);
93             }
94         } key;
95 
96         struct Motion {
97             uint32_t seq;
98             uint32_t empty1;
99             nsecs_t eventTime __attribute__((aligned(8)));
100             int32_t deviceId;
101             int32_t source;
102             int32_t displayId;
103             int32_t action;
104             int32_t actionButton;
105             int32_t flags;
106             int32_t metaState;
107             int32_t buttonState;
108             MotionClassification classification; // base type: uint8_t
109             uint8_t empty2[3];
110             int32_t edgeFlags;
111             nsecs_t downTime __attribute__((aligned(8)));
112             float xOffset;
113             float yOffset;
114             float xPrecision;
115             float yPrecision;
116             uint32_t pointerCount;
117             uint32_t empty3;
118             // Note that PointerCoords requires 8 byte alignment.
119             struct Pointer {
120                 PointerProperties properties;
121                 PointerCoords coords;
122             } pointers[MAX_POINTERS];
123 
getActionIdInputMessage::Body::Motion124             int32_t getActionId() const {
125                 uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
126                         >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
127                 return pointers[index].properties.id;
128             }
129 
sizeInputMessage::Body::Motion130             inline size_t size() const {
131                 return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
132                         + sizeof(Pointer) * pointerCount;
133             }
134         } motion;
135 
136         struct Finished {
137             uint32_t seq;
138             bool handled;
139 
sizeInputMessage::Body::Finished140             inline size_t size() const {
141                 return sizeof(Finished);
142             }
143         } finished;
144     } __attribute__((aligned(8))) body;
145 
146     bool isValid(size_t actualSize) const;
147     size_t size() const;
148     void getSanitizedCopy(InputMessage* msg) const;
149 };
150 
151 /*
152  * An input channel consists of a local unix domain socket used to send and receive
153  * input messages across processes.  Each channel has a descriptive name for debugging purposes.
154  *
155  * Each endpoint has its own InputChannel object that specifies its file descriptor.
156  *
157  * The input channel is closed when all references to it are released.
158  */
159 class InputChannel : public RefBase {
160 protected:
161     virtual ~InputChannel();
162 
163 public:
164     InputChannel() = default;
165     InputChannel(const std::string& name, int fd);
166 
167     /* Creates a pair of input channels.
168      *
169      * Returns OK on success.
170      */
171     static status_t openInputChannelPair(const std::string& name,
172             sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel);
173 
getName()174     inline std::string getName() const { return mName; }
getFd()175     inline int getFd() const { return mFd; }
176 
177     /* Sends a message to the other endpoint.
178      *
179      * If the channel is full then the message is guaranteed not to have been sent at all.
180      * Try again after the consumer has sent a finished signal indicating that it has
181      * consumed some of the pending messages from the channel.
182      *
183      * Returns OK on success.
184      * Returns WOULD_BLOCK if the channel is full.
185      * Returns DEAD_OBJECT if the channel's peer has been closed.
186      * Other errors probably indicate that the channel is broken.
187      */
188     status_t sendMessage(const InputMessage* msg);
189 
190     /* Receives a message sent by the other endpoint.
191      *
192      * If there is no message present, try again after poll() indicates that the fd
193      * is readable.
194      *
195      * Returns OK on success.
196      * Returns WOULD_BLOCK if there is no message present.
197      * Returns DEAD_OBJECT if the channel's peer has been closed.
198      * Other errors probably indicate that the channel is broken.
199      */
200     status_t receiveMessage(InputMessage* msg);
201 
202     /* Returns a new object that has a duplicate of this channel's fd. */
203     sp<InputChannel> dup() const;
204 
205     status_t write(Parcel& out) const;
206     status_t read(const Parcel& from);
207 
208     sp<IBinder> getToken() const;
209     void setToken(const sp<IBinder>& token);
210 
211 private:
212     void setFd(int fd);
213 
214     std::string mName;
215     int mFd = -1;
216 
217     sp<IBinder> mToken = nullptr;
218 };
219 
220 /*
221  * Publishes input events to an input channel.
222  */
223 class InputPublisher {
224 public:
225     /* Creates a publisher associated with an input channel. */
226     explicit InputPublisher(const sp<InputChannel>& channel);
227 
228     /* Destroys the publisher and releases its input channel. */
229     ~InputPublisher();
230 
231     /* Gets the underlying input channel. */
getChannel()232     inline sp<InputChannel> getChannel() { return mChannel; }
233 
234     /* Publishes a key event to the input channel.
235      *
236      * Returns OK on success.
237      * Returns WOULD_BLOCK if the channel is full.
238      * Returns DEAD_OBJECT if the channel's peer has been closed.
239      * Returns BAD_VALUE if seq is 0.
240      * Other errors probably indicate that the channel is broken.
241      */
242     status_t publishKeyEvent(
243             uint32_t seq,
244             int32_t deviceId,
245             int32_t source,
246             int32_t displayId,
247             int32_t action,
248             int32_t flags,
249             int32_t keyCode,
250             int32_t scanCode,
251             int32_t metaState,
252             int32_t repeatCount,
253             nsecs_t downTime,
254             nsecs_t eventTime);
255 
256     /* Publishes a motion event to the input channel.
257      *
258      * Returns OK on success.
259      * Returns WOULD_BLOCK if the channel is full.
260      * Returns DEAD_OBJECT if the channel's peer has been closed.
261      * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS.
262      * Other errors probably indicate that the channel is broken.
263      */
264     status_t publishMotionEvent(
265             uint32_t seq,
266             int32_t deviceId,
267             int32_t source,
268             int32_t displayId,
269             int32_t action,
270             int32_t actionButton,
271             int32_t flags,
272             int32_t edgeFlags,
273             int32_t metaState,
274             int32_t buttonState,
275             MotionClassification classification,
276             float xOffset,
277             float yOffset,
278             float xPrecision,
279             float yPrecision,
280             nsecs_t downTime,
281             nsecs_t eventTime,
282             uint32_t pointerCount,
283             const PointerProperties* pointerProperties,
284             const PointerCoords* pointerCoords);
285 
286     /* Receives the finished signal from the consumer in reply to the original dispatch signal.
287      * If a signal was received, returns the message sequence number,
288      * and whether the consumer handled the message.
289      *
290      * The returned sequence number is never 0 unless the operation failed.
291      *
292      * Returns OK on success.
293      * Returns WOULD_BLOCK if there is no signal present.
294      * Returns DEAD_OBJECT if the channel's peer has been closed.
295      * Other errors probably indicate that the channel is broken.
296      */
297     status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled);
298 
299 private:
300     sp<InputChannel> mChannel;
301 };
302 
303 /*
304  * Consumes input events from an input channel.
305  */
306 class InputConsumer {
307 public:
308     /* Creates a consumer associated with an input channel. */
309     explicit InputConsumer(const sp<InputChannel>& channel);
310 
311     /* Destroys the consumer and releases its input channel. */
312     ~InputConsumer();
313 
314     /* Gets the underlying input channel. */
getChannel()315     inline sp<InputChannel> getChannel() { return mChannel; }
316 
317     /* Consumes an input event from the input channel and copies its contents into
318      * an InputEvent object created using the specified factory.
319      *
320      * Tries to combine a series of move events into larger batches whenever possible.
321      *
322      * If consumeBatches is false, then defers consuming pending batched events if it
323      * is possible for additional samples to be added to them later.  Call hasPendingBatch()
324      * to determine whether a pending batch is available to be consumed.
325      *
326      * If consumeBatches is true, then events are still batched but they are consumed
327      * immediately as soon as the input channel is exhausted.
328      *
329      * The frameTime parameter specifies the time when the current display frame started
330      * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown.
331      *
332      * The returned sequence number is never 0 unless the operation failed.
333      *
334      * Returns OK on success.
335      * Returns WOULD_BLOCK if there is no event present.
336      * Returns DEAD_OBJECT if the channel's peer has been closed.
337      * Returns NO_MEMORY if the event could not be created.
338      * Other errors probably indicate that the channel is broken.
339      */
340     status_t consume(InputEventFactoryInterface* factory, bool consumeBatches,
341             nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
342 
343     /* Sends a finished signal to the publisher to inform it that the message
344      * with the specified sequence number has finished being process and whether
345      * the message was handled by the consumer.
346      *
347      * Returns OK on success.
348      * Returns BAD_VALUE if seq is 0.
349      * Other errors probably indicate that the channel is broken.
350      */
351     status_t sendFinishedSignal(uint32_t seq, bool handled);
352 
353     /* Returns true if there is a deferred event waiting.
354      *
355      * Should be called after calling consume() to determine whether the consumer
356      * has a deferred event to be processed.  Deferred events are somewhat special in
357      * that they have already been removed from the input channel.  If the input channel
358      * becomes empty, the client may need to do extra work to ensure that it processes
359      * the deferred event despite the fact that the input channel's file descriptor
360      * is not readable.
361      *
362      * One option is simply to call consume() in a loop until it returns WOULD_BLOCK.
363      * This guarantees that all deferred events will be processed.
364      *
365      * Alternately, the caller can call hasDeferredEvent() to determine whether there is
366      * a deferred event waiting and then ensure that its event loop wakes up at least
367      * one more time to consume the deferred event.
368      */
369     bool hasDeferredEvent() const;
370 
371     /* Returns true if there is a pending batch.
372      *
373      * Should be called after calling consume() with consumeBatches == false to determine
374      * whether consume() should be called again later on with consumeBatches == true.
375      */
376     bool hasPendingBatch() const;
377 
378 private:
379     // True if touch resampling is enabled.
380     const bool mResampleTouch;
381 
382     // The input channel.
383     sp<InputChannel> mChannel;
384 
385     // The current input message.
386     InputMessage mMsg;
387 
388     // True if mMsg contains a valid input message that was deferred from the previous
389     // call to consume and that still needs to be handled.
390     bool mMsgDeferred;
391 
392     // Batched motion events per device and source.
393     struct Batch {
394         Vector<InputMessage> samples;
395     };
396     Vector<Batch> mBatches;
397 
398     // Touch state per device and source, only for sources of class pointer.
399     struct History {
400         nsecs_t eventTime;
401         BitSet32 idBits;
402         int32_t idToIndex[MAX_POINTER_ID + 1];
403         PointerCoords pointers[MAX_POINTERS];
404 
initializeFromHistory405         void initializeFrom(const InputMessage& msg) {
406             eventTime = msg.body.motion.eventTime;
407             idBits.clear();
408             for (uint32_t i = 0; i < msg.body.motion.pointerCount; i++) {
409                 uint32_t id = msg.body.motion.pointers[i].properties.id;
410                 idBits.markBit(id);
411                 idToIndex[id] = i;
412                 pointers[i].copyFrom(msg.body.motion.pointers[i].coords);
413             }
414         }
415 
initializeFromHistory416         void initializeFrom(const History& other) {
417             eventTime = other.eventTime;
418             idBits = other.idBits; // temporary copy
419             for (size_t i = 0; i < other.idBits.count(); i++) {
420                 uint32_t id = idBits.clearFirstMarkedBit();
421                 int32_t index = other.idToIndex[id];
422                 idToIndex[id] = index;
423                 pointers[index].copyFrom(other.pointers[index]);
424             }
425             idBits = other.idBits; // final copy
426         }
427 
getPointerByIdHistory428         const PointerCoords& getPointerById(uint32_t id) const {
429             return pointers[idToIndex[id]];
430         }
431 
hasPointerIdHistory432         bool hasPointerId(uint32_t id) const {
433             return idBits.hasBit(id);
434         }
435     };
436     struct TouchState {
437         int32_t deviceId;
438         int32_t source;
439         size_t historyCurrent;
440         size_t historySize;
441         History history[2];
442         History lastResample;
443 
initializeTouchState444         void initialize(int32_t deviceId, int32_t source) {
445             this->deviceId = deviceId;
446             this->source = source;
447             historyCurrent = 0;
448             historySize = 0;
449             lastResample.eventTime = 0;
450             lastResample.idBits.clear();
451         }
452 
addHistoryTouchState453         void addHistory(const InputMessage& msg) {
454             historyCurrent ^= 1;
455             if (historySize < 2) {
456                 historySize += 1;
457             }
458             history[historyCurrent].initializeFrom(msg);
459         }
460 
getHistoryTouchState461         const History* getHistory(size_t index) const {
462             return &history[(historyCurrent + index) & 1];
463         }
464 
recentCoordinatesAreIdenticalTouchState465         bool recentCoordinatesAreIdentical(uint32_t id) const {
466             // Return true if the two most recently received "raw" coordinates are identical
467             if (historySize < 2) {
468                 return false;
469             }
470             if (!getHistory(0)->hasPointerId(id) || !getHistory(1)->hasPointerId(id)) {
471                 return false;
472             }
473             float currentX = getHistory(0)->getPointerById(id).getX();
474             float currentY = getHistory(0)->getPointerById(id).getY();
475             float previousX = getHistory(1)->getPointerById(id).getX();
476             float previousY = getHistory(1)->getPointerById(id).getY();
477             if (currentX == previousX && currentY == previousY) {
478                 return true;
479             }
480             return false;
481         }
482     };
483     Vector<TouchState> mTouchStates;
484 
485     // Chain of batched sequence numbers.  When multiple input messages are combined into
486     // a batch, we append a record here that associates the last sequence number in the
487     // batch with the previous one.  When the finished signal is sent, we traverse the
488     // chain to individually finish all input messages that were part of the batch.
489     struct SeqChain {
490         uint32_t seq;   // sequence number of batched input message
491         uint32_t chain; // sequence number of previous batched input message
492     };
493     Vector<SeqChain> mSeqChains;
494 
495     status_t consumeBatch(InputEventFactoryInterface* factory,
496             nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
497     status_t consumeSamples(InputEventFactoryInterface* factory,
498             Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent);
499 
500     void updateTouchState(InputMessage& msg);
501     void resampleTouchState(nsecs_t frameTime, MotionEvent* event,
502             const InputMessage *next);
503 
504     ssize_t findBatch(int32_t deviceId, int32_t source) const;
505     ssize_t findTouchState(int32_t deviceId, int32_t source) const;
506 
507     status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
508 
509     static void rewriteMessage(TouchState& state, InputMessage& msg);
510     static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
511     static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
512     static void addSample(MotionEvent* event, const InputMessage* msg);
513     static bool canAddSample(const Batch& batch, const InputMessage* msg);
514     static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
515     static bool shouldResampleTool(int32_t toolType);
516 
517     static bool isTouchResamplingEnabled();
518 };
519 
520 } // namespace android
521 
522 #endif // _LIBINPUT_INPUT_TRANSPORT_H
523