blob: 0facce313fa53373f426b539f255ea1b68740d80 [file] [log] [blame]
Jeff Brown46b9ac0a2010-04-22 18:58:52 -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 _UI_INPUT_TRANSPORT_H
18#define _UI_INPUT_TRANSPORT_H
19
20/**
21 * Native input transport.
22 *
Jeff Browncbee6d62012-02-03 20:11:27 -080023 * The InputChannel provides a mechanism for exchanging InputMessage structures across processes.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070024 *
Jeff Browncbee6d62012-02-03 20:11:27 -080025 * The InputPublisher and InputConsumer each handle one end-point of an input channel.
26 * The InputPublisher is used by the input dispatcher to send events to the application.
27 * The InputConsumer is used by the application to receive events from the input dispatcher.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070028 */
29
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070030#include <ui/Input.h>
31#include <utils/Errors.h>
32#include <utils/Timers.h>
33#include <utils/RefBase.h>
34#include <utils/String8.h>
35
36namespace android {
37
38/*
Jeff Browncbee6d62012-02-03 20:11:27 -080039 * Intermediate representation used to send input events and related signals.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070040 */
41struct InputMessage {
Jeff Browncbee6d62012-02-03 20:11:27 -080042 enum {
43 TYPE_KEY = 1,
44 TYPE_MOTION = 2,
45 TYPE_FINISHED = 3,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070046 };
47
Jeff Browncbee6d62012-02-03 20:11:27 -080048 struct Header {
49 uint32_t type;
50 uint32_t padding; // 8 byte alignment for the body that follows
51 } header;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070052
Jeff Browncbee6d62012-02-03 20:11:27 -080053 union Body {
54 struct Key {
Jeff Brown072ec962012-02-07 14:46:57 -080055 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -080056 nsecs_t eventTime;
57 int32_t deviceId;
58 int32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070059 int32_t action;
60 int32_t flags;
61 int32_t keyCode;
62 int32_t scanCode;
63 int32_t metaState;
64 int32_t repeatCount;
65 nsecs_t downTime;
Jeff Browncbee6d62012-02-03 20:11:27 -080066
67 inline size_t size() const {
68 return sizeof(Key);
69 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070070 } key;
71
Jeff Browncbee6d62012-02-03 20:11:27 -080072 struct Motion {
Jeff Brown072ec962012-02-07 14:46:57 -080073 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -080074 nsecs_t eventTime;
75 int32_t deviceId;
76 int32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070077 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -070078 int32_t flags;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070079 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070080 int32_t buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070081 int32_t edgeFlags;
82 nsecs_t downTime;
83 float xOffset;
84 float yOffset;
85 float xPrecision;
86 float yPrecision;
87 size_t pointerCount;
Jeff Browncbee6d62012-02-03 20:11:27 -080088 struct Pointer {
89 PointerProperties properties;
90 PointerCoords coords;
91 } pointers[MAX_POINTERS];
92
93 inline size_t size() const {
94 return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
95 + sizeof(Pointer) * pointerCount;
96 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070097 } motion;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070098
Jeff Browncbee6d62012-02-03 20:11:27 -080099 struct Finished {
Jeff Brown072ec962012-02-07 14:46:57 -0800100 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -0800101 bool handled;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700102
Jeff Browncbee6d62012-02-03 20:11:27 -0800103 inline size_t size() const {
104 return sizeof(Finished);
105 }
106 } finished;
107 } body;
108
109 bool isValid(size_t actualSize) const;
110 size_t size() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700111};
112
113/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800114 * An input channel consists of a local unix domain socket used to send and receive
115 * input messages across processes. Each channel has a descriptive name for debugging purposes.
116 *
117 * Each endpoint has its own InputChannel object that specifies its file descriptor.
118 *
119 * The input channel is closed when all references to it are released.
120 */
121class InputChannel : public RefBase {
122protected:
123 virtual ~InputChannel();
124
125public:
Jeff Brown91e32892012-02-14 15:56:29 -0800126 InputChannel(const String8& name, int fd);
Jeff Browncbee6d62012-02-03 20:11:27 -0800127
128 /* Creates a pair of input channels.
129 *
130 * Returns OK on success.
131 */
132 static status_t openInputChannelPair(const String8& name,
133 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel);
134
135 inline String8 getName() const { return mName; }
Jeff Brown91e32892012-02-14 15:56:29 -0800136 inline int getFd() const { return mFd; }
Jeff Browncbee6d62012-02-03 20:11:27 -0800137
138 /* Sends a message to the other endpoint.
139 *
140 * If the channel is full then the message is guaranteed not to have been sent at all.
141 * Try again after the consumer has sent a finished signal indicating that it has
142 * consumed some of the pending messages from the channel.
143 *
144 * Returns OK on success.
145 * Returns WOULD_BLOCK if the channel is full.
146 * Returns DEAD_OBJECT if the channel's peer has been closed.
147 * Other errors probably indicate that the channel is broken.
148 */
149 status_t sendMessage(const InputMessage* msg);
150
151 /* Receives a message sent by the other endpoint.
152 *
153 * If there is no message present, try again after poll() indicates that the fd
154 * is readable.
155 *
156 * Returns OK on success.
157 * Returns WOULD_BLOCK if there is no message present.
158 * Returns DEAD_OBJECT if the channel's peer has been closed.
159 * Other errors probably indicate that the channel is broken.
160 */
161 status_t receiveMessage(InputMessage* msg);
162
163private:
164 String8 mName;
Jeff Brown91e32892012-02-14 15:56:29 -0800165 int mFd;
Jeff Browncbee6d62012-02-03 20:11:27 -0800166};
167
168/*
169 * Publishes input events to an input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700170 */
171class InputPublisher {
172public:
173 /* Creates a publisher associated with an input channel. */
174 explicit InputPublisher(const sp<InputChannel>& channel);
175
176 /* Destroys the publisher and releases its input channel. */
177 ~InputPublisher();
178
179 /* Gets the underlying input channel. */
180 inline sp<InputChannel> getChannel() { return mChannel; }
181
Jeff Browncbee6d62012-02-03 20:11:27 -0800182 /* Publishes a key event to the input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700183 *
184 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800185 * Returns WOULD_BLOCK if the channel is full.
186 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown072ec962012-02-07 14:46:57 -0800187 * Returns BAD_VALUE if seq is 0.
Jeff Browncbee6d62012-02-03 20:11:27 -0800188 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700189 */
190 status_t publishKeyEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800191 uint32_t seq,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700192 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700193 int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700194 int32_t action,
195 int32_t flags,
196 int32_t keyCode,
197 int32_t scanCode,
198 int32_t metaState,
199 int32_t repeatCount,
200 nsecs_t downTime,
201 nsecs_t eventTime);
202
Jeff Browncbee6d62012-02-03 20:11:27 -0800203 /* Publishes a motion event to the input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700204 *
205 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800206 * Returns WOULD_BLOCK if the channel is full.
207 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown072ec962012-02-07 14:46:57 -0800208 * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS.
Jeff Browncbee6d62012-02-03 20:11:27 -0800209 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700210 */
211 status_t publishMotionEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800212 uint32_t seq,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700213 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700214 int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700215 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700216 int32_t flags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700217 int32_t edgeFlags,
218 int32_t metaState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700219 int32_t buttonState,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700220 float xOffset,
221 float yOffset,
222 float xPrecision,
223 float yPrecision,
224 nsecs_t downTime,
225 nsecs_t eventTime,
226 size_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700227 const PointerProperties* pointerProperties,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700228 const PointerCoords* pointerCoords);
229
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700230 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
Jeff Brown072ec962012-02-07 14:46:57 -0800231 * If a signal was received, returns the message sequence number,
232 * and whether the consumer handled the message.
233 *
234 * The returned sequence number is never 0 unless the operation failed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700235 *
236 * Returns OK on success.
237 * Returns WOULD_BLOCK if there is no signal present.
Jeff Browncbee6d62012-02-03 20:11:27 -0800238 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700239 * Other errors probably indicate that the channel is broken.
240 */
Jeff Brown072ec962012-02-07 14:46:57 -0800241 status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700242
243private:
244 sp<InputChannel> mChannel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700245};
246
247/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800248 * Consumes input events from an input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700249 */
250class InputConsumer {
251public:
252 /* Creates a consumer associated with an input channel. */
253 explicit InputConsumer(const sp<InputChannel>& channel);
254
255 /* Destroys the consumer and releases its input channel. */
256 ~InputConsumer();
257
258 /* Gets the underlying input channel. */
259 inline sp<InputChannel> getChannel() { return mChannel; }
260
Jeff Browncbee6d62012-02-03 20:11:27 -0800261 /* Consumes an input event from the input channel and copies its contents into
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700262 * an InputEvent object created using the specified factory.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700263 *
Jeff Brown072ec962012-02-07 14:46:57 -0800264 * Tries to combine a series of move events into larger batches whenever possible.
265 *
266 * If consumeBatches is false, then defers consuming pending batched events if it
267 * is possible for additional samples to be added to them later. Call hasPendingBatch()
268 * to determine whether a pending batch is available to be consumed.
269 *
270 * If consumeBatches is true, then events are still batched but they are consumed
271 * immediately as soon as the input channel is exhausted.
272 *
273 * The returned sequence number is never 0 unless the operation failed.
274 *
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700275 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800276 * Returns WOULD_BLOCK if there is no event present.
277 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700278 * Returns NO_MEMORY if the event could not be created.
Jeff Browncbee6d62012-02-03 20:11:27 -0800279 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700280 */
Jeff Brown072ec962012-02-07 14:46:57 -0800281 status_t consume(InputEventFactoryInterface* factory, bool consumeBatches,
282 uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700283
Jeff Brown072ec962012-02-07 14:46:57 -0800284 /* Sends a finished signal to the publisher to inform it that the message
285 * with the specified sequence number has finished being process and whether
286 * the message was handled by the consumer.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700287 *
288 * Returns OK on success.
Jeff Brown072ec962012-02-07 14:46:57 -0800289 * Returns BAD_VALUE if seq is 0.
Jeff Browncbee6d62012-02-03 20:11:27 -0800290 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700291 */
Jeff Brown072ec962012-02-07 14:46:57 -0800292 status_t sendFinishedSignal(uint32_t seq, bool handled);
293
294 /* Returns true if there is a pending batch. */
295 bool hasPendingBatch() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700296
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700297private:
298 sp<InputChannel> mChannel;
Jeff Brown072ec962012-02-07 14:46:57 -0800299
Jeff Brown90fde932012-02-13 12:44:01 -0800300 // The current input message.
301 InputMessage mMsg;
302
303 // True if mMsg contains a valid input message that was deferred from the previous
304 // call to consume and that still needs to be handled.
305 bool mMsgDeferred;
Jeff Brown072ec962012-02-07 14:46:57 -0800306
307 // Batched motion events per device and source.
308 struct Batch {
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800309 uint32_t seq; // sequence number of last input message batched in the event
Jeff Brown072ec962012-02-07 14:46:57 -0800310 MotionEvent event;
311 };
312 Vector<Batch> mBatches;
313
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800314 // Chain of batched sequence numbers. When multiple input messages are combined into
315 // a batch, we append a record here that associates the last sequence number in the
316 // batch with the previous one. When the finished signal is sent, we traverse the
317 // chain to individually finish all input messages that were part of the batch.
318 struct SeqChain {
319 uint32_t seq; // sequence number of batched input message
320 uint32_t chain; // sequence number of previous batched input message
321 };
322 Vector<SeqChain> mSeqChains;
323
Jeff Brown072ec962012-02-07 14:46:57 -0800324 ssize_t findBatch(int32_t deviceId, int32_t source) const;
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800325 status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
Jeff Brown072ec962012-02-07 14:46:57 -0800326
327 static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
328 static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
329 static bool canAppendSamples(const MotionEvent* event, const InputMessage* msg);
330 static void appendSamples(MotionEvent* event, const InputMessage* msg);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700331};
332
333} // namespace android
334
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700335#endif // _UI_INPUT_TRANSPORT_H