blob: 29c296e2388d4030294a2720f16853a80f49bc2f [file] [log] [blame]
Jeff Brown46b9ac02010-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
Mathias Agopianb93a03f82012-02-17 15:34:57 -080017#ifndef _ANDROIDFW_INPUT_TRANSPORT_H
18#define _ANDROIDFW_INPUT_TRANSPORT_H
Jeff Brown46b9ac02010-04-22 18:58:52 -070019
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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070028 */
29
Mathias Agopianb93a03f82012-02-17 15:34:57 -080030#include <androidfw/Input.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070031#include <utils/Errors.h>
32#include <utils/Timers.h>
33#include <utils/RefBase.h>
34#include <utils/String8.h>
Mathias Agopianb93a03f82012-02-17 15:34:57 -080035#include <utils/Vector.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070036
37namespace android {
38
39/*
Jeff Browncbee6d62012-02-03 20:11:27 -080040 * Intermediate representation used to send input events and related signals.
Jeff Brown46b9ac02010-04-22 18:58:52 -070041 */
42struct InputMessage {
Jeff Browncbee6d62012-02-03 20:11:27 -080043 enum {
44 TYPE_KEY = 1,
45 TYPE_MOTION = 2,
46 TYPE_FINISHED = 3,
Jeff Brown46b9ac02010-04-22 18:58:52 -070047 };
48
Jeff Browncbee6d62012-02-03 20:11:27 -080049 struct Header {
50 uint32_t type;
51 uint32_t padding; // 8 byte alignment for the body that follows
52 } header;
Jeff Brown46b9ac02010-04-22 18:58:52 -070053
Jeff Browncbee6d62012-02-03 20:11:27 -080054 union Body {
55 struct Key {
Jeff Brown072ec962012-02-07 14:46:57 -080056 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -080057 nsecs_t eventTime;
58 int32_t deviceId;
59 int32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -070060 int32_t action;
61 int32_t flags;
62 int32_t keyCode;
63 int32_t scanCode;
64 int32_t metaState;
65 int32_t repeatCount;
66 nsecs_t downTime;
Jeff Browncbee6d62012-02-03 20:11:27 -080067
68 inline size_t size() const {
69 return sizeof(Key);
70 }
Jeff Brown46b9ac02010-04-22 18:58:52 -070071 } key;
72
Jeff Browncbee6d62012-02-03 20:11:27 -080073 struct Motion {
Jeff Brown072ec962012-02-07 14:46:57 -080074 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -080075 nsecs_t eventTime;
76 int32_t deviceId;
77 int32_t source;
Jeff Brown46b9ac02010-04-22 18:58:52 -070078 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -070079 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -070080 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070081 int32_t buttonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -070082 int32_t edgeFlags;
83 nsecs_t downTime;
84 float xOffset;
85 float yOffset;
86 float xPrecision;
87 float yPrecision;
88 size_t pointerCount;
Jeff Browncbee6d62012-02-03 20:11:27 -080089 struct Pointer {
90 PointerProperties properties;
91 PointerCoords coords;
92 } pointers[MAX_POINTERS];
93
94 inline size_t size() const {
95 return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
96 + sizeof(Pointer) * pointerCount;
97 }
Jeff Brown46b9ac02010-04-22 18:58:52 -070098 } motion;
Jeff Brown46b9ac02010-04-22 18:58:52 -070099
Jeff Browncbee6d62012-02-03 20:11:27 -0800100 struct Finished {
Jeff Brown072ec962012-02-07 14:46:57 -0800101 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -0800102 bool handled;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700103
Jeff Browncbee6d62012-02-03 20:11:27 -0800104 inline size_t size() const {
105 return sizeof(Finished);
106 }
107 } finished;
108 } body;
109
110 bool isValid(size_t actualSize) const;
111 size_t size() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700112};
113
114/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800115 * An input channel consists of a local unix domain socket used to send and receive
116 * input messages across processes. Each channel has a descriptive name for debugging purposes.
117 *
118 * Each endpoint has its own InputChannel object that specifies its file descriptor.
119 *
120 * The input channel is closed when all references to it are released.
121 */
122class InputChannel : public RefBase {
123protected:
124 virtual ~InputChannel();
125
126public:
Jeff Brown91e32892012-02-14 15:56:29 -0800127 InputChannel(const String8& name, int fd);
Jeff Browncbee6d62012-02-03 20:11:27 -0800128
129 /* Creates a pair of input channels.
130 *
131 * Returns OK on success.
132 */
133 static status_t openInputChannelPair(const String8& name,
134 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel);
135
136 inline String8 getName() const { return mName; }
Jeff Brown91e32892012-02-14 15:56:29 -0800137 inline int getFd() const { return mFd; }
Jeff Browncbee6d62012-02-03 20:11:27 -0800138
139 /* Sends a message to the other endpoint.
140 *
141 * If the channel is full then the message is guaranteed not to have been sent at all.
142 * Try again after the consumer has sent a finished signal indicating that it has
143 * consumed some of the pending messages from the channel.
144 *
145 * Returns OK on success.
146 * Returns WOULD_BLOCK if the channel is full.
147 * Returns DEAD_OBJECT if the channel's peer has been closed.
148 * Other errors probably indicate that the channel is broken.
149 */
150 status_t sendMessage(const InputMessage* msg);
151
152 /* Receives a message sent by the other endpoint.
153 *
154 * If there is no message present, try again after poll() indicates that the fd
155 * is readable.
156 *
157 * Returns OK on success.
158 * Returns WOULD_BLOCK if there is no message present.
159 * Returns DEAD_OBJECT if the channel's peer has been closed.
160 * Other errors probably indicate that the channel is broken.
161 */
162 status_t receiveMessage(InputMessage* msg);
163
164private:
165 String8 mName;
Jeff Brown91e32892012-02-14 15:56:29 -0800166 int mFd;
Jeff Browncbee6d62012-02-03 20:11:27 -0800167};
168
169/*
170 * Publishes input events to an input channel.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700171 */
172class InputPublisher {
173public:
174 /* Creates a publisher associated with an input channel. */
175 explicit InputPublisher(const sp<InputChannel>& channel);
176
177 /* Destroys the publisher and releases its input channel. */
178 ~InputPublisher();
179
180 /* Gets the underlying input channel. */
181 inline sp<InputChannel> getChannel() { return mChannel; }
182
Jeff Browncbee6d62012-02-03 20:11:27 -0800183 /* Publishes a key event to the input channel.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700184 *
185 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800186 * Returns WOULD_BLOCK if the channel is full.
187 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown072ec962012-02-07 14:46:57 -0800188 * Returns BAD_VALUE if seq is 0.
Jeff Browncbee6d62012-02-03 20:11:27 -0800189 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700190 */
191 status_t publishKeyEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800192 uint32_t seq,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700193 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700194 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700195 int32_t action,
196 int32_t flags,
197 int32_t keyCode,
198 int32_t scanCode,
199 int32_t metaState,
200 int32_t repeatCount,
201 nsecs_t downTime,
202 nsecs_t eventTime);
203
Jeff Browncbee6d62012-02-03 20:11:27 -0800204 /* Publishes a motion event to the input channel.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700205 *
206 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800207 * Returns WOULD_BLOCK if the channel is full.
208 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown072ec962012-02-07 14:46:57 -0800209 * 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 -0800210 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700211 */
212 status_t publishMotionEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800213 uint32_t seq,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700214 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700215 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700216 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700217 int32_t flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700218 int32_t edgeFlags,
219 int32_t metaState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700220 int32_t buttonState,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700221 float xOffset,
222 float yOffset,
223 float xPrecision,
224 float yPrecision,
225 nsecs_t downTime,
226 nsecs_t eventTime,
227 size_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700228 const PointerProperties* pointerProperties,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700229 const PointerCoords* pointerCoords);
230
Jeff Brown46b9ac02010-04-22 18:58:52 -0700231 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
Jeff Brown072ec962012-02-07 14:46:57 -0800232 * If a signal was received, returns the message sequence number,
233 * and whether the consumer handled the message.
234 *
235 * The returned sequence number is never 0 unless the operation failed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700236 *
237 * Returns OK on success.
238 * Returns WOULD_BLOCK if there is no signal present.
Jeff Browncbee6d62012-02-03 20:11:27 -0800239 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700240 * Other errors probably indicate that the channel is broken.
241 */
Jeff Brown072ec962012-02-07 14:46:57 -0800242 status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700243
244private:
245 sp<InputChannel> mChannel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700246};
247
248/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800249 * Consumes input events from an input channel.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700250 */
251class InputConsumer {
252public:
253 /* Creates a consumer associated with an input channel. */
254 explicit InputConsumer(const sp<InputChannel>& channel);
255
256 /* Destroys the consumer and releases its input channel. */
257 ~InputConsumer();
258
259 /* Gets the underlying input channel. */
260 inline sp<InputChannel> getChannel() { return mChannel; }
261
Jeff Browncbee6d62012-02-03 20:11:27 -0800262 /* Consumes an input event from the input channel and copies its contents into
Jeff Brown46b9ac02010-04-22 18:58:52 -0700263 * an InputEvent object created using the specified factory.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700264 *
Jeff Brown072ec962012-02-07 14:46:57 -0800265 * Tries to combine a series of move events into larger batches whenever possible.
266 *
267 * If consumeBatches is false, then defers consuming pending batched events if it
268 * is possible for additional samples to be added to them later. Call hasPendingBatch()
269 * to determine whether a pending batch is available to be consumed.
270 *
271 * If consumeBatches is true, then events are still batched but they are consumed
272 * immediately as soon as the input channel is exhausted.
273 *
274 * The returned sequence number is never 0 unless the operation failed.
275 *
Jeff Brown46b9ac02010-04-22 18:58:52 -0700276 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800277 * Returns WOULD_BLOCK if there is no event present.
278 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700279 * Returns NO_MEMORY if the event could not be created.
Jeff Browncbee6d62012-02-03 20:11:27 -0800280 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700281 */
Jeff Brown072ec962012-02-07 14:46:57 -0800282 status_t consume(InputEventFactoryInterface* factory, bool consumeBatches,
283 uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700284
Jeff Brown072ec962012-02-07 14:46:57 -0800285 /* Sends a finished signal to the publisher to inform it that the message
286 * with the specified sequence number has finished being process and whether
287 * the message was handled by the consumer.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700288 *
289 * Returns OK on success.
Jeff Brown072ec962012-02-07 14:46:57 -0800290 * Returns BAD_VALUE if seq is 0.
Jeff Browncbee6d62012-02-03 20:11:27 -0800291 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292 */
Jeff Brown072ec962012-02-07 14:46:57 -0800293 status_t sendFinishedSignal(uint32_t seq, bool handled);
294
Jeff Brown2b6c32c2012-03-13 15:00:09 -0700295 /* Returns true if there is a deferred event waiting.
296 *
297 * Should be called after calling consume() to determine whether the consumer
298 * has a deferred event to be processed. Deferred events are somewhat special in
299 * that they have already been removed from the input channel. If the input channel
300 * becomes empty, the client may need to do extra work to ensure that it processes
301 * the deferred event despite the fact that the inptu channel's file descriptor
302 * is not readable.
303 *
304 * One option is simply to call consume() in a loop until it returns WOULD_BLOCK.
305 * This guarantees that all deferred events will be processed.
306 *
307 * Alternately, the caller can call hasDeferredEvent() to determine whether there is
308 * a deferred event waiting and then ensure that its event loop wakes up at least
309 * one more time to consume the deferred event.
310 */
311 bool hasDeferredEvent() const;
312
313 /* Returns true if there is a pending batch.
314 *
315 * Should be called after calling consume() with consumeBatches == false to determine
316 * whether consume() should be called again later on with consumeBatches == true.
317 */
Jeff Brown072ec962012-02-07 14:46:57 -0800318 bool hasPendingBatch() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700319
Jeff Brown46b9ac02010-04-22 18:58:52 -0700320private:
321 sp<InputChannel> mChannel;
Jeff Brown072ec962012-02-07 14:46:57 -0800322
Jeff Brown90fde932012-02-13 12:44:01 -0800323 // The current input message.
324 InputMessage mMsg;
325
326 // True if mMsg contains a valid input message that was deferred from the previous
327 // call to consume and that still needs to be handled.
328 bool mMsgDeferred;
Jeff Brown072ec962012-02-07 14:46:57 -0800329
330 // Batched motion events per device and source.
331 struct Batch {
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800332 uint32_t seq; // sequence number of last input message batched in the event
Jeff Brown072ec962012-02-07 14:46:57 -0800333 MotionEvent event;
334 };
335 Vector<Batch> mBatches;
336
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800337 // Chain of batched sequence numbers. When multiple input messages are combined into
338 // a batch, we append a record here that associates the last sequence number in the
339 // batch with the previous one. When the finished signal is sent, we traverse the
340 // chain to individually finish all input messages that were part of the batch.
341 struct SeqChain {
342 uint32_t seq; // sequence number of batched input message
343 uint32_t chain; // sequence number of previous batched input message
344 };
345 Vector<SeqChain> mSeqChains;
346
Jeff Brown072ec962012-02-07 14:46:57 -0800347 ssize_t findBatch(int32_t deviceId, int32_t source) const;
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800348 status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
Jeff Brown072ec962012-02-07 14:46:57 -0800349
350 static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
351 static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
352 static bool canAppendSamples(const MotionEvent* event, const InputMessage* msg);
353 static void appendSamples(MotionEvent* event, const InputMessage* msg);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700354};
355
356} // namespace android
357
Mathias Agopianb93a03f82012-02-17 15:34:57 -0800358#endif // _ANDROIDFW_INPUT_TRANSPORT_H