xref: /aosp_15_r20/frameworks/native/include/input/InputTransport.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #pragma GCC system_header
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker /**
22*38e8c45fSAndroid Build Coastguard Worker  * Native input transport.
23*38e8c45fSAndroid Build Coastguard Worker  *
24*38e8c45fSAndroid Build Coastguard Worker  * The InputChannel provides a mechanism for exchanging InputMessage structures across processes.
25*38e8c45fSAndroid Build Coastguard Worker  *
26*38e8c45fSAndroid Build Coastguard Worker  * The InputPublisher and InputConsumer each handle one end-point of an input channel.
27*38e8c45fSAndroid Build Coastguard Worker  * The InputPublisher is used by the input dispatcher to send events to the application.
28*38e8c45fSAndroid Build Coastguard Worker  * The InputConsumer is used by the application to receive events from the input dispatcher.
29*38e8c45fSAndroid Build Coastguard Worker  */
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker #include <string>
32*38e8c45fSAndroid Build Coastguard Worker #include <unordered_map>
33*38e8c45fSAndroid Build Coastguard Worker 
34*38e8c45fSAndroid Build Coastguard Worker #include <android-base/chrono_utils.h>
35*38e8c45fSAndroid Build Coastguard Worker #include <android-base/result.h>
36*38e8c45fSAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
37*38e8c45fSAndroid Build Coastguard Worker 
38*38e8c45fSAndroid Build Coastguard Worker #include <android/os/InputChannelCore.h>
39*38e8c45fSAndroid Build Coastguard Worker #include <binder/IBinder.h>
40*38e8c45fSAndroid Build Coastguard Worker #include <input/Input.h>
41*38e8c45fSAndroid Build Coastguard Worker #include <input/InputVerifier.h>
42*38e8c45fSAndroid Build Coastguard Worker #include <sys/stat.h>
43*38e8c45fSAndroid Build Coastguard Worker #include <ui/Transform.h>
44*38e8c45fSAndroid Build Coastguard Worker #include <utils/BitSet.h>
45*38e8c45fSAndroid Build Coastguard Worker #include <utils/Errors.h>
46*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h>
47*38e8c45fSAndroid Build Coastguard Worker 
48*38e8c45fSAndroid Build Coastguard Worker namespace android {
49*38e8c45fSAndroid Build Coastguard Worker class Parcel;
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker /*
52*38e8c45fSAndroid Build Coastguard Worker  * Intermediate representation used to send input events and related signals.
53*38e8c45fSAndroid Build Coastguard Worker  *
54*38e8c45fSAndroid Build Coastguard Worker  * Note that this structure is used for IPCs so its layout must be identical
55*38e8c45fSAndroid Build Coastguard Worker  * on 64 and 32 bit processes. This is tested in StructLayout_test.cpp.
56*38e8c45fSAndroid Build Coastguard Worker  *
57*38e8c45fSAndroid Build Coastguard Worker  * Since the struct must be aligned to an 8-byte boundary, there could be uninitialized bytes
58*38e8c45fSAndroid Build Coastguard Worker  * in-between the defined fields. This padding data should be explicitly accounted for by adding
59*38e8c45fSAndroid Build Coastguard Worker  * "empty" fields into the struct. This data is memset to zero before sending the struct across
60*38e8c45fSAndroid Build Coastguard Worker  * the socket. Adding the explicit fields ensures that the memset is not optimized away by the
61*38e8c45fSAndroid Build Coastguard Worker  * compiler. When a new field is added to the struct, the corresponding change
62*38e8c45fSAndroid Build Coastguard Worker  * in StructLayout_test should be made.
63*38e8c45fSAndroid Build Coastguard Worker  */
64*38e8c45fSAndroid Build Coastguard Worker struct InputMessage {
65*38e8c45fSAndroid Build Coastguard Worker     enum class Type : uint32_t {
66*38e8c45fSAndroid Build Coastguard Worker         KEY,
67*38e8c45fSAndroid Build Coastguard Worker         MOTION,
68*38e8c45fSAndroid Build Coastguard Worker         FINISHED,
69*38e8c45fSAndroid Build Coastguard Worker         FOCUS,
70*38e8c45fSAndroid Build Coastguard Worker         CAPTURE,
71*38e8c45fSAndroid Build Coastguard Worker         DRAG,
72*38e8c45fSAndroid Build Coastguard Worker         TIMELINE,
73*38e8c45fSAndroid Build Coastguard Worker         TOUCH_MODE,
74*38e8c45fSAndroid Build Coastguard Worker 
75*38e8c45fSAndroid Build Coastguard Worker         ftl_last = TOUCH_MODE
76*38e8c45fSAndroid Build Coastguard Worker     };
77*38e8c45fSAndroid Build Coastguard Worker 
78*38e8c45fSAndroid Build Coastguard Worker     struct Header {
79*38e8c45fSAndroid Build Coastguard Worker         Type type; // 4 bytes
80*38e8c45fSAndroid Build Coastguard Worker         uint32_t seq;
81*38e8c45fSAndroid Build Coastguard Worker     } header;
82*38e8c45fSAndroid Build Coastguard Worker 
83*38e8c45fSAndroid Build Coastguard Worker     // For keys and motions, rely on the fact that std::array takes up exactly as much space
84*38e8c45fSAndroid Build Coastguard Worker     // as the underlying data. This is not guaranteed by C++, but it simplifies the conversions.
85*38e8c45fSAndroid Build Coastguard Worker     static_assert(sizeof(std::array<uint8_t, 32>) == 32);
86*38e8c45fSAndroid Build Coastguard Worker 
87*38e8c45fSAndroid Build Coastguard Worker     // For bool values, rely on the fact that they take up exactly one byte. This is not guaranteed
88*38e8c45fSAndroid Build Coastguard Worker     // by C++ and is implementation-dependent, but it simplifies the conversions.
89*38e8c45fSAndroid Build Coastguard Worker     static_assert(sizeof(bool) == 1);
90*38e8c45fSAndroid Build Coastguard Worker 
91*38e8c45fSAndroid Build Coastguard Worker     // Body *must* be 8 byte aligned.
92*38e8c45fSAndroid Build Coastguard Worker     union Body {
93*38e8c45fSAndroid Build Coastguard Worker         struct Key {
94*38e8c45fSAndroid Build Coastguard Worker             int32_t eventId;
95*38e8c45fSAndroid Build Coastguard Worker             uint32_t empty1;
96*38e8c45fSAndroid Build Coastguard Worker             nsecs_t eventTime __attribute__((aligned(8)));
97*38e8c45fSAndroid Build Coastguard Worker             int32_t deviceId;
98*38e8c45fSAndroid Build Coastguard Worker             int32_t source;
99*38e8c45fSAndroid Build Coastguard Worker             int32_t displayId;
100*38e8c45fSAndroid Build Coastguard Worker             std::array<uint8_t, 32> hmac;
101*38e8c45fSAndroid Build Coastguard Worker             int32_t action;
102*38e8c45fSAndroid Build Coastguard Worker             int32_t flags;
103*38e8c45fSAndroid Build Coastguard Worker             int32_t keyCode;
104*38e8c45fSAndroid Build Coastguard Worker             int32_t scanCode;
105*38e8c45fSAndroid Build Coastguard Worker             int32_t metaState;
106*38e8c45fSAndroid Build Coastguard Worker             int32_t repeatCount;
107*38e8c45fSAndroid Build Coastguard Worker             uint32_t empty2;
108*38e8c45fSAndroid Build Coastguard Worker             nsecs_t downTime __attribute__((aligned(8)));
109*38e8c45fSAndroid Build Coastguard Worker 
sizeInputMessage::Body::Key110*38e8c45fSAndroid Build Coastguard Worker             inline size_t size() const { return sizeof(Key); }
111*38e8c45fSAndroid Build Coastguard Worker         } key;
112*38e8c45fSAndroid Build Coastguard Worker 
113*38e8c45fSAndroid Build Coastguard Worker         struct Motion {
114*38e8c45fSAndroid Build Coastguard Worker             int32_t eventId;
115*38e8c45fSAndroid Build Coastguard Worker             uint32_t pointerCount;
116*38e8c45fSAndroid Build Coastguard Worker             nsecs_t eventTime __attribute__((aligned(8)));
117*38e8c45fSAndroid Build Coastguard Worker             int32_t deviceId;
118*38e8c45fSAndroid Build Coastguard Worker             int32_t source;
119*38e8c45fSAndroid Build Coastguard Worker             int32_t displayId;
120*38e8c45fSAndroid Build Coastguard Worker             std::array<uint8_t, 32> hmac;
121*38e8c45fSAndroid Build Coastguard Worker             int32_t action;
122*38e8c45fSAndroid Build Coastguard Worker             int32_t actionButton;
123*38e8c45fSAndroid Build Coastguard Worker             int32_t flags;
124*38e8c45fSAndroid Build Coastguard Worker             int32_t metaState;
125*38e8c45fSAndroid Build Coastguard Worker             int32_t buttonState;
126*38e8c45fSAndroid Build Coastguard Worker             MotionClassification classification; // base type: uint8_t
127*38e8c45fSAndroid Build Coastguard Worker             uint8_t empty2[3];                   // 3 bytes to fill gap created by classification
128*38e8c45fSAndroid Build Coastguard Worker             int32_t edgeFlags;
129*38e8c45fSAndroid Build Coastguard Worker             nsecs_t downTime __attribute__((aligned(8)));
130*38e8c45fSAndroid Build Coastguard Worker             float dsdx; // Begin window transform
131*38e8c45fSAndroid Build Coastguard Worker             float dtdx; //
132*38e8c45fSAndroid Build Coastguard Worker             float dtdy; //
133*38e8c45fSAndroid Build Coastguard Worker             float dsdy; //
134*38e8c45fSAndroid Build Coastguard Worker             float tx;   //
135*38e8c45fSAndroid Build Coastguard Worker             float ty;   // End window transform
136*38e8c45fSAndroid Build Coastguard Worker             float xPrecision;
137*38e8c45fSAndroid Build Coastguard Worker             float yPrecision;
138*38e8c45fSAndroid Build Coastguard Worker             float xCursorPosition;
139*38e8c45fSAndroid Build Coastguard Worker             float yCursorPosition;
140*38e8c45fSAndroid Build Coastguard Worker             float dsdxRaw; // Begin raw transform
141*38e8c45fSAndroid Build Coastguard Worker             float dtdxRaw; //
142*38e8c45fSAndroid Build Coastguard Worker             float dtdyRaw; //
143*38e8c45fSAndroid Build Coastguard Worker             float dsdyRaw; //
144*38e8c45fSAndroid Build Coastguard Worker             float txRaw;   //
145*38e8c45fSAndroid Build Coastguard Worker             float tyRaw;   // End raw transform
146*38e8c45fSAndroid Build Coastguard Worker             /**
147*38e8c45fSAndroid Build Coastguard Worker              * The "pointers" field must be the last field of the struct InputMessage.
148*38e8c45fSAndroid Build Coastguard Worker              * When we send the struct InputMessage across the socket, we are not
149*38e8c45fSAndroid Build Coastguard Worker              * writing the entire "pointers" array, but only the pointerCount portion
150*38e8c45fSAndroid Build Coastguard Worker              * of it as an optimization. Adding a field after "pointers" would break this.
151*38e8c45fSAndroid Build Coastguard Worker              */
152*38e8c45fSAndroid Build Coastguard Worker             struct Pointer {
153*38e8c45fSAndroid Build Coastguard Worker                 PointerProperties properties;
154*38e8c45fSAndroid Build Coastguard Worker                 PointerCoords coords;
155*38e8c45fSAndroid Build Coastguard Worker             } pointers[MAX_POINTERS] __attribute__((aligned(8)));
156*38e8c45fSAndroid Build Coastguard Worker 
getActionIdInputMessage::Body::Motion157*38e8c45fSAndroid Build Coastguard Worker             int32_t getActionId() const {
158*38e8c45fSAndroid Build Coastguard Worker                 uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
159*38e8c45fSAndroid Build Coastguard Worker                         >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
160*38e8c45fSAndroid Build Coastguard Worker                 return pointers[index].properties.id;
161*38e8c45fSAndroid Build Coastguard Worker             }
162*38e8c45fSAndroid Build Coastguard Worker 
sizeInputMessage::Body::Motion163*38e8c45fSAndroid Build Coastguard Worker             inline size_t size() const {
164*38e8c45fSAndroid Build Coastguard Worker                 return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
165*38e8c45fSAndroid Build Coastguard Worker                         + sizeof(Pointer) * pointerCount;
166*38e8c45fSAndroid Build Coastguard Worker             }
167*38e8c45fSAndroid Build Coastguard Worker         } motion;
168*38e8c45fSAndroid Build Coastguard Worker 
169*38e8c45fSAndroid Build Coastguard Worker         struct Finished {
170*38e8c45fSAndroid Build Coastguard Worker             bool handled;
171*38e8c45fSAndroid Build Coastguard Worker             uint8_t empty[7];
172*38e8c45fSAndroid Build Coastguard Worker             nsecs_t consumeTime; // The time when the event was consumed by the receiving end
173*38e8c45fSAndroid Build Coastguard Worker 
sizeInputMessage::Body::Finished174*38e8c45fSAndroid Build Coastguard Worker             inline size_t size() const { return sizeof(Finished); }
175*38e8c45fSAndroid Build Coastguard Worker         } finished;
176*38e8c45fSAndroid Build Coastguard Worker 
177*38e8c45fSAndroid Build Coastguard Worker         struct Focus {
178*38e8c45fSAndroid Build Coastguard Worker             int32_t eventId;
179*38e8c45fSAndroid Build Coastguard Worker             // The following 2 fields take up 4 bytes total
180*38e8c45fSAndroid Build Coastguard Worker             bool hasFocus;
181*38e8c45fSAndroid Build Coastguard Worker             uint8_t empty[3];
182*38e8c45fSAndroid Build Coastguard Worker 
sizeInputMessage::Body::Focus183*38e8c45fSAndroid Build Coastguard Worker             inline size_t size() const { return sizeof(Focus); }
184*38e8c45fSAndroid Build Coastguard Worker         } focus;
185*38e8c45fSAndroid Build Coastguard Worker 
186*38e8c45fSAndroid Build Coastguard Worker         struct Capture {
187*38e8c45fSAndroid Build Coastguard Worker             int32_t eventId;
188*38e8c45fSAndroid Build Coastguard Worker             bool pointerCaptureEnabled;
189*38e8c45fSAndroid Build Coastguard Worker             uint8_t empty[3];
190*38e8c45fSAndroid Build Coastguard Worker 
sizeInputMessage::Body::Capture191*38e8c45fSAndroid Build Coastguard Worker             inline size_t size() const { return sizeof(Capture); }
192*38e8c45fSAndroid Build Coastguard Worker         } capture;
193*38e8c45fSAndroid Build Coastguard Worker 
194*38e8c45fSAndroid Build Coastguard Worker         struct Drag {
195*38e8c45fSAndroid Build Coastguard Worker             int32_t eventId;
196*38e8c45fSAndroid Build Coastguard Worker             float x;
197*38e8c45fSAndroid Build Coastguard Worker             float y;
198*38e8c45fSAndroid Build Coastguard Worker             bool isExiting;
199*38e8c45fSAndroid Build Coastguard Worker             uint8_t empty[3];
200*38e8c45fSAndroid Build Coastguard Worker 
sizeInputMessage::Body::Drag201*38e8c45fSAndroid Build Coastguard Worker             inline size_t size() const { return sizeof(Drag); }
202*38e8c45fSAndroid Build Coastguard Worker         } drag;
203*38e8c45fSAndroid Build Coastguard Worker 
204*38e8c45fSAndroid Build Coastguard Worker         struct Timeline {
205*38e8c45fSAndroid Build Coastguard Worker             int32_t eventId;
206*38e8c45fSAndroid Build Coastguard Worker             uint32_t empty;
207*38e8c45fSAndroid Build Coastguard Worker             std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
208*38e8c45fSAndroid Build Coastguard Worker 
sizeInputMessage::Body::Timeline209*38e8c45fSAndroid Build Coastguard Worker             inline size_t size() const { return sizeof(Timeline); }
210*38e8c45fSAndroid Build Coastguard Worker         } timeline;
211*38e8c45fSAndroid Build Coastguard Worker 
212*38e8c45fSAndroid Build Coastguard Worker         struct TouchMode {
213*38e8c45fSAndroid Build Coastguard Worker             int32_t eventId;
214*38e8c45fSAndroid Build Coastguard Worker             // The following 2 fields take up 4 bytes total
215*38e8c45fSAndroid Build Coastguard Worker             bool isInTouchMode;
216*38e8c45fSAndroid Build Coastguard Worker             uint8_t empty[3];
217*38e8c45fSAndroid Build Coastguard Worker 
sizeInputMessage::Body::TouchMode218*38e8c45fSAndroid Build Coastguard Worker             inline size_t size() const { return sizeof(TouchMode); }
219*38e8c45fSAndroid Build Coastguard Worker         } touchMode;
220*38e8c45fSAndroid Build Coastguard Worker     } __attribute__((aligned(8))) body;
221*38e8c45fSAndroid Build Coastguard Worker 
222*38e8c45fSAndroid Build Coastguard Worker     bool isValid(size_t actualSize) const;
223*38e8c45fSAndroid Build Coastguard Worker     size_t size() const;
224*38e8c45fSAndroid Build Coastguard Worker     void getSanitizedCopy(InputMessage* msg) const;
225*38e8c45fSAndroid Build Coastguard Worker };
226*38e8c45fSAndroid Build Coastguard Worker 
227*38e8c45fSAndroid Build Coastguard Worker /*
228*38e8c45fSAndroid Build Coastguard Worker  * An input channel consists of a local unix domain socket used to send and receive
229*38e8c45fSAndroid Build Coastguard Worker  * input messages across processes.  Each channel has a descriptive name for debugging purposes.
230*38e8c45fSAndroid Build Coastguard Worker  *
231*38e8c45fSAndroid Build Coastguard Worker  * Each endpoint has its own InputChannel object that specifies its file descriptor.
232*38e8c45fSAndroid Build Coastguard Worker  * For parceling, this relies on android::os::InputChannelCore, defined in aidl.
233*38e8c45fSAndroid Build Coastguard Worker  *
234*38e8c45fSAndroid Build Coastguard Worker  * The input channel is closed when all references to it are released.
235*38e8c45fSAndroid Build Coastguard Worker  */
236*38e8c45fSAndroid Build Coastguard Worker class InputChannel : private android::os::InputChannelCore {
237*38e8c45fSAndroid Build Coastguard Worker public:
238*38e8c45fSAndroid Build Coastguard Worker     static std::unique_ptr<InputChannel> create(android::os::InputChannelCore&& parceledChannel);
239*38e8c45fSAndroid Build Coastguard Worker     ~InputChannel();
240*38e8c45fSAndroid Build Coastguard Worker 
241*38e8c45fSAndroid Build Coastguard Worker     /**
242*38e8c45fSAndroid Build Coastguard Worker      * Create a pair of input channels.
243*38e8c45fSAndroid Build Coastguard Worker      * The two returned input channels are equivalent, and are labeled as "server" and "client"
244*38e8c45fSAndroid Build Coastguard Worker      * for convenience. The two input channels share the same token.
245*38e8c45fSAndroid Build Coastguard Worker      *
246*38e8c45fSAndroid Build Coastguard Worker      * Return OK on success.
247*38e8c45fSAndroid Build Coastguard Worker      */
248*38e8c45fSAndroid Build Coastguard Worker     static status_t openInputChannelPair(const std::string& name,
249*38e8c45fSAndroid Build Coastguard Worker                                          std::unique_ptr<InputChannel>& outServerChannel,
250*38e8c45fSAndroid Build Coastguard Worker                                          std::unique_ptr<InputChannel>& outClientChannel);
251*38e8c45fSAndroid Build Coastguard Worker 
getName()252*38e8c45fSAndroid Build Coastguard Worker     inline std::string getName() const { return name; }
getFd()253*38e8c45fSAndroid Build Coastguard Worker     inline int getFd() const { return fd.get(); }
254*38e8c45fSAndroid Build Coastguard Worker 
255*38e8c45fSAndroid Build Coastguard Worker     /* Send a message to the other endpoint.
256*38e8c45fSAndroid Build Coastguard Worker      *
257*38e8c45fSAndroid Build Coastguard Worker      * If the channel is full then the message is guaranteed not to have been sent at all.
258*38e8c45fSAndroid Build Coastguard Worker      * Try again after the consumer has sent a finished signal indicating that it has
259*38e8c45fSAndroid Build Coastguard Worker      * consumed some of the pending messages from the channel.
260*38e8c45fSAndroid Build Coastguard Worker      *
261*38e8c45fSAndroid Build Coastguard Worker      * Return OK on success.
262*38e8c45fSAndroid Build Coastguard Worker      * Return WOULD_BLOCK if the channel is full.
263*38e8c45fSAndroid Build Coastguard Worker      * Return DEAD_OBJECT if the channel's peer has been closed.
264*38e8c45fSAndroid Build Coastguard Worker      * Other errors probably indicate that the channel is broken.
265*38e8c45fSAndroid Build Coastguard Worker      */
266*38e8c45fSAndroid Build Coastguard Worker     virtual status_t sendMessage(const InputMessage* msg);
267*38e8c45fSAndroid Build Coastguard Worker 
268*38e8c45fSAndroid Build Coastguard Worker     /* Receive a message sent by the other endpoint.
269*38e8c45fSAndroid Build Coastguard Worker      *
270*38e8c45fSAndroid Build Coastguard Worker      * If there is no message present, try again after poll() indicates that the fd
271*38e8c45fSAndroid Build Coastguard Worker      * is readable.
272*38e8c45fSAndroid Build Coastguard Worker      *
273*38e8c45fSAndroid Build Coastguard Worker      * Return OK on success.
274*38e8c45fSAndroid Build Coastguard Worker      * Return WOULD_BLOCK if there is no message present.
275*38e8c45fSAndroid Build Coastguard Worker      * Return DEAD_OBJECT if the channel's peer has been closed.
276*38e8c45fSAndroid Build Coastguard Worker      * Other errors probably indicate that the channel is broken.
277*38e8c45fSAndroid Build Coastguard Worker      */
278*38e8c45fSAndroid Build Coastguard Worker     virtual android::base::Result<InputMessage> receiveMessage();
279*38e8c45fSAndroid Build Coastguard Worker 
280*38e8c45fSAndroid Build Coastguard Worker     /* Tells whether there is a message in the channel available to be received.
281*38e8c45fSAndroid Build Coastguard Worker      *
282*38e8c45fSAndroid Build Coastguard Worker      * This is only a performance hint and may return false negative results. Clients should not
283*38e8c45fSAndroid Build Coastguard Worker      * rely on availability of the message based on the return value.
284*38e8c45fSAndroid Build Coastguard Worker      */
285*38e8c45fSAndroid Build Coastguard Worker     virtual bool probablyHasInput() const;
286*38e8c45fSAndroid Build Coastguard Worker 
287*38e8c45fSAndroid Build Coastguard Worker     /* Wait until there is a message in the channel.
288*38e8c45fSAndroid Build Coastguard Worker      *
289*38e8c45fSAndroid Build Coastguard Worker      * The |timeout| specifies how long to block waiting for an input event to appear. Negative
290*38e8c45fSAndroid Build Coastguard Worker      * values are not allowed.
291*38e8c45fSAndroid Build Coastguard Worker      *
292*38e8c45fSAndroid Build Coastguard Worker      * In some cases returning before timeout expiration can happen without a message available.
293*38e8c45fSAndroid Build Coastguard Worker      * This could happen after the channel was closed on the other side. Another possible reason
294*38e8c45fSAndroid Build Coastguard Worker      * is incorrect setup of the channel.
295*38e8c45fSAndroid Build Coastguard Worker      */
296*38e8c45fSAndroid Build Coastguard Worker     void waitForMessage(std::chrono::milliseconds timeout) const;
297*38e8c45fSAndroid Build Coastguard Worker 
298*38e8c45fSAndroid Build Coastguard Worker     /* Return a new object that has a duplicate of this channel's fd. */
299*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<InputChannel> dup() const;
300*38e8c45fSAndroid Build Coastguard Worker 
301*38e8c45fSAndroid Build Coastguard Worker     void copyTo(android::os::InputChannelCore& outChannel) const;
302*38e8c45fSAndroid Build Coastguard Worker 
303*38e8c45fSAndroid Build Coastguard Worker     /**
304*38e8c45fSAndroid Build Coastguard Worker      * Similar to "copyTo", but it takes ownership of the provided InputChannel (and after this is
305*38e8c45fSAndroid Build Coastguard Worker      * called, it destroys it).
306*38e8c45fSAndroid Build Coastguard Worker      * @param from the InputChannel that should be converted to InputChannelCore
307*38e8c45fSAndroid Build Coastguard Worker      * @param outChannel the pre-allocated InputChannelCore to which to transfer the 'from' channel
308*38e8c45fSAndroid Build Coastguard Worker      */
309*38e8c45fSAndroid Build Coastguard Worker     static void moveChannel(std::unique_ptr<InputChannel> from,
310*38e8c45fSAndroid Build Coastguard Worker                             android::os::InputChannelCore& outChannel);
311*38e8c45fSAndroid Build Coastguard Worker 
312*38e8c45fSAndroid Build Coastguard Worker     /**
313*38e8c45fSAndroid Build Coastguard Worker      * The connection token is used to identify the input connection, i.e.
314*38e8c45fSAndroid Build Coastguard Worker      * the pair of input channels that were created simultaneously. Input channels
315*38e8c45fSAndroid Build Coastguard Worker      * are always created in pairs, and the token can be used to find the server-side
316*38e8c45fSAndroid Build Coastguard Worker      * input channel from the client-side input channel, and vice versa.
317*38e8c45fSAndroid Build Coastguard Worker      *
318*38e8c45fSAndroid Build Coastguard Worker      * Do not use connection token to check equality of a specific input channel object
319*38e8c45fSAndroid Build Coastguard Worker      * to another, because two different (client and server) input channels will share the
320*38e8c45fSAndroid Build Coastguard Worker      * same connection token.
321*38e8c45fSAndroid Build Coastguard Worker      *
322*38e8c45fSAndroid Build Coastguard Worker      * Return the token that identifies this connection.
323*38e8c45fSAndroid Build Coastguard Worker      */
324*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> getConnectionToken() const;
325*38e8c45fSAndroid Build Coastguard Worker 
326*38e8c45fSAndroid Build Coastguard Worker protected:
327*38e8c45fSAndroid Build Coastguard Worker     InputChannel(const std::string name, android::base::unique_fd fd, sp<IBinder> token);
328*38e8c45fSAndroid Build Coastguard Worker 
329*38e8c45fSAndroid Build Coastguard Worker private:
330*38e8c45fSAndroid Build Coastguard Worker     static std::unique_ptr<InputChannel> create(const std::string& name,
331*38e8c45fSAndroid Build Coastguard Worker                                                 android::base::unique_fd fd, sp<IBinder> token);
332*38e8c45fSAndroid Build Coastguard Worker };
333*38e8c45fSAndroid Build Coastguard Worker 
334*38e8c45fSAndroid Build Coastguard Worker /*
335*38e8c45fSAndroid Build Coastguard Worker  * Publishes input events to an input channel.
336*38e8c45fSAndroid Build Coastguard Worker  */
337*38e8c45fSAndroid Build Coastguard Worker class InputPublisher {
338*38e8c45fSAndroid Build Coastguard Worker public:
339*38e8c45fSAndroid Build Coastguard Worker     /* Creates a publisher associated with an input channel. */
340*38e8c45fSAndroid Build Coastguard Worker     explicit InputPublisher(const std::shared_ptr<InputChannel>& channel);
341*38e8c45fSAndroid Build Coastguard Worker 
342*38e8c45fSAndroid Build Coastguard Worker     /* Destroys the publisher and releases its input channel. */
343*38e8c45fSAndroid Build Coastguard Worker     ~InputPublisher();
344*38e8c45fSAndroid Build Coastguard Worker 
345*38e8c45fSAndroid Build Coastguard Worker     /* Gets the underlying input channel. */
getChannel()346*38e8c45fSAndroid Build Coastguard Worker     inline InputChannel& getChannel() const { return *mChannel; }
347*38e8c45fSAndroid Build Coastguard Worker 
348*38e8c45fSAndroid Build Coastguard Worker     /* Publishes a key event to the input channel.
349*38e8c45fSAndroid Build Coastguard Worker      *
350*38e8c45fSAndroid Build Coastguard Worker      * Returns OK on success.
351*38e8c45fSAndroid Build Coastguard Worker      * Returns WOULD_BLOCK if the channel is full.
352*38e8c45fSAndroid Build Coastguard Worker      * Returns DEAD_OBJECT if the channel's peer has been closed.
353*38e8c45fSAndroid Build Coastguard Worker      * Returns BAD_VALUE if seq is 0.
354*38e8c45fSAndroid Build Coastguard Worker      * Other errors probably indicate that the channel is broken.
355*38e8c45fSAndroid Build Coastguard Worker      */
356*38e8c45fSAndroid Build Coastguard Worker     status_t publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source,
357*38e8c45fSAndroid Build Coastguard Worker                              ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac,
358*38e8c45fSAndroid Build Coastguard Worker                              int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode,
359*38e8c45fSAndroid Build Coastguard Worker                              int32_t metaState, int32_t repeatCount, nsecs_t downTime,
360*38e8c45fSAndroid Build Coastguard Worker                              nsecs_t eventTime);
361*38e8c45fSAndroid Build Coastguard Worker 
362*38e8c45fSAndroid Build Coastguard Worker     /* Publishes a motion event to the input channel.
363*38e8c45fSAndroid Build Coastguard Worker      *
364*38e8c45fSAndroid Build Coastguard Worker      * Returns OK on success.
365*38e8c45fSAndroid Build Coastguard Worker      * Returns WOULD_BLOCK if the channel is full.
366*38e8c45fSAndroid Build Coastguard Worker      * Returns DEAD_OBJECT if the channel's peer has been closed.
367*38e8c45fSAndroid Build Coastguard Worker      * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS,
368*38e8c45fSAndroid Build Coastguard Worker      * or if the verifier is enabled and the event failed verification upon publishing.
369*38e8c45fSAndroid Build Coastguard Worker      * Other errors probably indicate that the channel is broken.
370*38e8c45fSAndroid Build Coastguard Worker      */
371*38e8c45fSAndroid Build Coastguard Worker     status_t publishMotionEvent(uint32_t seq, int32_t eventId, int32_t deviceId, int32_t source,
372*38e8c45fSAndroid Build Coastguard Worker                                 ui::LogicalDisplayId displayId, std::array<uint8_t, 32> hmac,
373*38e8c45fSAndroid Build Coastguard Worker                                 int32_t action, int32_t actionButton, int32_t flags,
374*38e8c45fSAndroid Build Coastguard Worker                                 int32_t edgeFlags, int32_t metaState, int32_t buttonState,
375*38e8c45fSAndroid Build Coastguard Worker                                 MotionClassification classification, const ui::Transform& transform,
376*38e8c45fSAndroid Build Coastguard Worker                                 float xPrecision, float yPrecision, float xCursorPosition,
377*38e8c45fSAndroid Build Coastguard Worker                                 float yCursorPosition, const ui::Transform& rawTransform,
378*38e8c45fSAndroid Build Coastguard Worker                                 nsecs_t downTime, nsecs_t eventTime, uint32_t pointerCount,
379*38e8c45fSAndroid Build Coastguard Worker                                 const PointerProperties* pointerProperties,
380*38e8c45fSAndroid Build Coastguard Worker                                 const PointerCoords* pointerCoords);
381*38e8c45fSAndroid Build Coastguard Worker 
382*38e8c45fSAndroid Build Coastguard Worker     /* Publishes a focus event to the input channel.
383*38e8c45fSAndroid Build Coastguard Worker      *
384*38e8c45fSAndroid Build Coastguard Worker      * Returns OK on success.
385*38e8c45fSAndroid Build Coastguard Worker      * Returns WOULD_BLOCK if the channel is full.
386*38e8c45fSAndroid Build Coastguard Worker      * Returns DEAD_OBJECT if the channel's peer has been closed.
387*38e8c45fSAndroid Build Coastguard Worker      * Other errors probably indicate that the channel is broken.
388*38e8c45fSAndroid Build Coastguard Worker      */
389*38e8c45fSAndroid Build Coastguard Worker     status_t publishFocusEvent(uint32_t seq, int32_t eventId, bool hasFocus);
390*38e8c45fSAndroid Build Coastguard Worker 
391*38e8c45fSAndroid Build Coastguard Worker     /* Publishes a capture event to the input channel.
392*38e8c45fSAndroid Build Coastguard Worker      *
393*38e8c45fSAndroid Build Coastguard Worker      * Returns OK on success.
394*38e8c45fSAndroid Build Coastguard Worker      * Returns WOULD_BLOCK if the channel is full.
395*38e8c45fSAndroid Build Coastguard Worker      * Returns DEAD_OBJECT if the channel's peer has been closed.
396*38e8c45fSAndroid Build Coastguard Worker      * Other errors probably indicate that the channel is broken.
397*38e8c45fSAndroid Build Coastguard Worker      */
398*38e8c45fSAndroid Build Coastguard Worker     status_t publishCaptureEvent(uint32_t seq, int32_t eventId, bool pointerCaptureEnabled);
399*38e8c45fSAndroid Build Coastguard Worker 
400*38e8c45fSAndroid Build Coastguard Worker     /* Publishes a drag event to the input channel.
401*38e8c45fSAndroid Build Coastguard Worker      *
402*38e8c45fSAndroid Build Coastguard Worker      * Returns OK on success.
403*38e8c45fSAndroid Build Coastguard Worker      * Returns WOULD_BLOCK if the channel is full.
404*38e8c45fSAndroid Build Coastguard Worker      * Returns DEAD_OBJECT if the channel's peer has been closed.
405*38e8c45fSAndroid Build Coastguard Worker      * Other errors probably indicate that the channel is broken.
406*38e8c45fSAndroid Build Coastguard Worker      */
407*38e8c45fSAndroid Build Coastguard Worker     status_t publishDragEvent(uint32_t seq, int32_t eventId, float x, float y, bool isExiting);
408*38e8c45fSAndroid Build Coastguard Worker 
409*38e8c45fSAndroid Build Coastguard Worker     /* Publishes a touch mode event to the input channel.
410*38e8c45fSAndroid Build Coastguard Worker      *
411*38e8c45fSAndroid Build Coastguard Worker      * Returns OK on success.
412*38e8c45fSAndroid Build Coastguard Worker      * Returns WOULD_BLOCK if the channel is full.
413*38e8c45fSAndroid Build Coastguard Worker      * Returns DEAD_OBJECT if the channel's peer has been closed.
414*38e8c45fSAndroid Build Coastguard Worker      * Other errors probably indicate that the channel is broken.
415*38e8c45fSAndroid Build Coastguard Worker      */
416*38e8c45fSAndroid Build Coastguard Worker     status_t publishTouchModeEvent(uint32_t seq, int32_t eventId, bool isInTouchMode);
417*38e8c45fSAndroid Build Coastguard Worker 
418*38e8c45fSAndroid Build Coastguard Worker     struct Finished {
419*38e8c45fSAndroid Build Coastguard Worker         uint32_t seq;
420*38e8c45fSAndroid Build Coastguard Worker         bool handled;
421*38e8c45fSAndroid Build Coastguard Worker         nsecs_t consumeTime;
422*38e8c45fSAndroid Build Coastguard Worker     };
423*38e8c45fSAndroid Build Coastguard Worker 
424*38e8c45fSAndroid Build Coastguard Worker     struct Timeline {
425*38e8c45fSAndroid Build Coastguard Worker         int32_t inputEventId;
426*38e8c45fSAndroid Build Coastguard Worker         std::array<nsecs_t, GraphicsTimeline::SIZE> graphicsTimeline;
427*38e8c45fSAndroid Build Coastguard Worker     };
428*38e8c45fSAndroid Build Coastguard Worker 
429*38e8c45fSAndroid Build Coastguard Worker     typedef std::variant<Finished, Timeline> ConsumerResponse;
430*38e8c45fSAndroid Build Coastguard Worker     /* Receive a signal from the consumer in reply to the original dispatch signal.
431*38e8c45fSAndroid Build Coastguard Worker      * If a signal was received, returns a Finished or a Timeline object.
432*38e8c45fSAndroid Build Coastguard Worker      * The InputConsumer should return a Finished object for every InputMessage that it is sent
433*38e8c45fSAndroid Build Coastguard Worker      * to confirm that it has been processed and that the InputConsumer is responsive.
434*38e8c45fSAndroid Build Coastguard Worker      * If several InputMessages are sent to InputConsumer, it's possible to receive Finished
435*38e8c45fSAndroid Build Coastguard Worker      * events out of order for those messages.
436*38e8c45fSAndroid Build Coastguard Worker      *
437*38e8c45fSAndroid Build Coastguard Worker      * The Timeline object is returned whenever the receiving end has processed a graphical frame
438*38e8c45fSAndroid Build Coastguard Worker      * and is returning the timeline of the frame. Not all input events will cause a Timeline
439*38e8c45fSAndroid Build Coastguard Worker      * object to be returned, and there is not guarantee about when it will arrive.
440*38e8c45fSAndroid Build Coastguard Worker      *
441*38e8c45fSAndroid Build Coastguard Worker      * If an object of Finished is returned, the returned sequence number is never 0 unless the
442*38e8c45fSAndroid Build Coastguard Worker      * operation failed.
443*38e8c45fSAndroid Build Coastguard Worker      *
444*38e8c45fSAndroid Build Coastguard Worker      * Returned error codes:
445*38e8c45fSAndroid Build Coastguard Worker      *         OK on success.
446*38e8c45fSAndroid Build Coastguard Worker      *         WOULD_BLOCK if there is no signal present.
447*38e8c45fSAndroid Build Coastguard Worker      *         DEAD_OBJECT if the channel's peer has been closed.
448*38e8c45fSAndroid Build Coastguard Worker      *         Other errors probably indicate that the channel is broken.
449*38e8c45fSAndroid Build Coastguard Worker      */
450*38e8c45fSAndroid Build Coastguard Worker     android::base::Result<ConsumerResponse> receiveConsumerResponse();
451*38e8c45fSAndroid Build Coastguard Worker 
452*38e8c45fSAndroid Build Coastguard Worker private:
453*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<InputChannel> mChannel;
454*38e8c45fSAndroid Build Coastguard Worker     InputVerifier mInputVerifier;
455*38e8c45fSAndroid Build Coastguard Worker };
456*38e8c45fSAndroid Build Coastguard Worker 
457*38e8c45fSAndroid Build Coastguard Worker } // namespace android
458