blob: 2924505c2aa4e50c7a17ba00425d8030f5acc761 [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
Mathias Agopianb93a03f82012-02-17 15:34:57 -080017#ifndef _ANDROIDFW_INPUT_TRANSPORT_H
18#define _ANDROIDFW_INPUT_TRANSPORT_H
Jeff Brown46b9ac0a2010-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 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
Mathias Agopianb93a03f82012-02-17 15:34:57 -080030#include <androidfw/Input.h>
Jeff Brown46b9ac0a2010-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 Brown771526c2012-04-27 15:13:25 -070036#include <utils/BitSet.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070037
38namespace android {
39
40/*
Jeff Browncbee6d62012-02-03 20:11:27 -080041 * Intermediate representation used to send input events and related signals.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070042 */
43struct InputMessage {
Jeff Browncbee6d62012-02-03 20:11:27 -080044 enum {
45 TYPE_KEY = 1,
46 TYPE_MOTION = 2,
47 TYPE_FINISHED = 3,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048 };
49
Jeff Browncbee6d62012-02-03 20:11:27 -080050 struct Header {
51 uint32_t type;
52 uint32_t padding; // 8 byte alignment for the body that follows
53 } header;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070054
Jeff Browncbee6d62012-02-03 20:11:27 -080055 union Body {
56 struct Key {
Jeff Brown072ec962012-02-07 14:46:57 -080057 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -080058 nsecs_t eventTime;
59 int32_t deviceId;
60 int32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070061 int32_t action;
62 int32_t flags;
63 int32_t keyCode;
64 int32_t scanCode;
65 int32_t metaState;
66 int32_t repeatCount;
67 nsecs_t downTime;
Jeff Browncbee6d62012-02-03 20:11:27 -080068
69 inline size_t size() const {
70 return sizeof(Key);
71 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070072 } key;
73
Jeff Browncbee6d62012-02-03 20:11:27 -080074 struct Motion {
Jeff Brown072ec962012-02-07 14:46:57 -080075 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -080076 nsecs_t eventTime;
77 int32_t deviceId;
78 int32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070079 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -070080 int32_t flags;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070081 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070082 int32_t buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070083 int32_t edgeFlags;
84 nsecs_t downTime;
85 float xOffset;
86 float yOffset;
87 float xPrecision;
88 float yPrecision;
89 size_t pointerCount;
Jeff Browncbee6d62012-02-03 20:11:27 -080090 struct Pointer {
91 PointerProperties properties;
92 PointerCoords coords;
93 } pointers[MAX_POINTERS];
94
95 inline size_t size() const {
96 return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
97 + sizeof(Pointer) * pointerCount;
98 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070099 } motion;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700100
Jeff Browncbee6d62012-02-03 20:11:27 -0800101 struct Finished {
Jeff Brown072ec962012-02-07 14:46:57 -0800102 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -0800103 bool handled;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700104
Jeff Browncbee6d62012-02-03 20:11:27 -0800105 inline size_t size() const {
106 return sizeof(Finished);
107 }
108 } finished;
109 } body;
110
111 bool isValid(size_t actualSize) const;
112 size_t size() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700113};
114
115/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800116 * An input channel consists of a local unix domain socket used to send and receive
117 * input messages across processes. Each channel has a descriptive name for debugging purposes.
118 *
119 * Each endpoint has its own InputChannel object that specifies its file descriptor.
120 *
121 * The input channel is closed when all references to it are released.
122 */
123class InputChannel : public RefBase {
124protected:
125 virtual ~InputChannel();
126
127public:
Jeff Brown91e32892012-02-14 15:56:29 -0800128 InputChannel(const String8& name, int fd);
Jeff Browncbee6d62012-02-03 20:11:27 -0800129
130 /* Creates a pair of input channels.
131 *
132 * Returns OK on success.
133 */
134 static status_t openInputChannelPair(const String8& name,
135 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel);
136
137 inline String8 getName() const { return mName; }
Jeff Brown91e32892012-02-14 15:56:29 -0800138 inline int getFd() const { return mFd; }
Jeff Browncbee6d62012-02-03 20:11:27 -0800139
140 /* Sends a message to the other endpoint.
141 *
142 * If the channel is full then the message is guaranteed not to have been sent at all.
143 * Try again after the consumer has sent a finished signal indicating that it has
144 * consumed some of the pending messages from the channel.
145 *
146 * Returns OK on success.
147 * Returns WOULD_BLOCK if the channel is full.
148 * Returns DEAD_OBJECT if the channel's peer has been closed.
149 * Other errors probably indicate that the channel is broken.
150 */
151 status_t sendMessage(const InputMessage* msg);
152
153 /* Receives a message sent by the other endpoint.
154 *
155 * If there is no message present, try again after poll() indicates that the fd
156 * is readable.
157 *
158 * Returns OK on success.
159 * Returns WOULD_BLOCK if there is no message present.
160 * Returns DEAD_OBJECT if the channel's peer has been closed.
161 * Other errors probably indicate that the channel is broken.
162 */
163 status_t receiveMessage(InputMessage* msg);
164
165private:
166 String8 mName;
Jeff Brown91e32892012-02-14 15:56:29 -0800167 int mFd;
Jeff Browncbee6d62012-02-03 20:11:27 -0800168};
169
170/*
171 * Publishes input events to an input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700172 */
173class InputPublisher {
174public:
175 /* Creates a publisher associated with an input channel. */
176 explicit InputPublisher(const sp<InputChannel>& channel);
177
178 /* Destroys the publisher and releases its input channel. */
179 ~InputPublisher();
180
181 /* Gets the underlying input channel. */
182 inline sp<InputChannel> getChannel() { return mChannel; }
183
Jeff Browncbee6d62012-02-03 20:11:27 -0800184 /* Publishes a key event to the input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700185 *
186 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800187 * Returns WOULD_BLOCK if the channel is full.
188 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown072ec962012-02-07 14:46:57 -0800189 * Returns BAD_VALUE if seq is 0.
Jeff Browncbee6d62012-02-03 20:11:27 -0800190 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700191 */
192 status_t publishKeyEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800193 uint32_t seq,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700194 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700195 int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700196 int32_t action,
197 int32_t flags,
198 int32_t keyCode,
199 int32_t scanCode,
200 int32_t metaState,
201 int32_t repeatCount,
202 nsecs_t downTime,
203 nsecs_t eventTime);
204
Jeff Browncbee6d62012-02-03 20:11:27 -0800205 /* Publishes a motion event to the input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700206 *
207 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800208 * Returns WOULD_BLOCK if the channel is full.
209 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown072ec962012-02-07 14:46:57 -0800210 * 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 -0800211 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700212 */
213 status_t publishMotionEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800214 uint32_t seq,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700215 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700216 int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700217 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700218 int32_t flags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700219 int32_t edgeFlags,
220 int32_t metaState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700221 int32_t buttonState,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700222 float xOffset,
223 float yOffset,
224 float xPrecision,
225 float yPrecision,
226 nsecs_t downTime,
227 nsecs_t eventTime,
228 size_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700229 const PointerProperties* pointerProperties,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700230 const PointerCoords* pointerCoords);
231
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700232 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
Jeff Brown072ec962012-02-07 14:46:57 -0800233 * If a signal was received, returns the message sequence number,
234 * and whether the consumer handled the message.
235 *
236 * The returned sequence number is never 0 unless the operation failed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700237 *
238 * Returns OK on success.
239 * Returns WOULD_BLOCK if there is no signal present.
Jeff Browncbee6d62012-02-03 20:11:27 -0800240 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700241 * Other errors probably indicate that the channel is broken.
242 */
Jeff Brown072ec962012-02-07 14:46:57 -0800243 status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700244
245private:
246 sp<InputChannel> mChannel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700247};
248
249/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800250 * Consumes input events from an input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700251 */
252class InputConsumer {
253public:
254 /* Creates a consumer associated with an input channel. */
255 explicit InputConsumer(const sp<InputChannel>& channel);
256
257 /* Destroys the consumer and releases its input channel. */
258 ~InputConsumer();
259
260 /* Gets the underlying input channel. */
261 inline sp<InputChannel> getChannel() { return mChannel; }
262
Jeff Browncbee6d62012-02-03 20:11:27 -0800263 /* Consumes an input event from the input channel and copies its contents into
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700264 * an InputEvent object created using the specified factory.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700265 *
Jeff Brown072ec962012-02-07 14:46:57 -0800266 * Tries to combine a series of move events into larger batches whenever possible.
267 *
268 * If consumeBatches is false, then defers consuming pending batched events if it
269 * is possible for additional samples to be added to them later. Call hasPendingBatch()
270 * to determine whether a pending batch is available to be consumed.
271 *
272 * If consumeBatches is true, then events are still batched but they are consumed
273 * immediately as soon as the input channel is exhausted.
274 *
Jeff Brown771526c2012-04-27 15:13:25 -0700275 * The frameTime parameter specifies the time when the current display frame started
276 * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown.
277 *
Jeff Brown072ec962012-02-07 14:46:57 -0800278 * The returned sequence number is never 0 unless the operation failed.
279 *
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700280 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800281 * Returns WOULD_BLOCK if there is no event present.
282 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700283 * Returns NO_MEMORY if the event could not be created.
Jeff Browncbee6d62012-02-03 20:11:27 -0800284 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700285 */
Jeff Brown072ec962012-02-07 14:46:57 -0800286 status_t consume(InputEventFactoryInterface* factory, bool consumeBatches,
Jeff Brown771526c2012-04-27 15:13:25 -0700287 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700288
Jeff Brown072ec962012-02-07 14:46:57 -0800289 /* Sends a finished signal to the publisher to inform it that the message
290 * with the specified sequence number has finished being process and whether
291 * the message was handled by the consumer.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700292 *
293 * Returns OK on success.
Jeff Brown072ec962012-02-07 14:46:57 -0800294 * Returns BAD_VALUE if seq is 0.
Jeff Browncbee6d62012-02-03 20:11:27 -0800295 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700296 */
Jeff Brown072ec962012-02-07 14:46:57 -0800297 status_t sendFinishedSignal(uint32_t seq, bool handled);
298
Jeff Brown2b6c32c2012-03-13 15:00:09 -0700299 /* Returns true if there is a deferred event waiting.
300 *
301 * Should be called after calling consume() to determine whether the consumer
302 * has a deferred event to be processed. Deferred events are somewhat special in
303 * that they have already been removed from the input channel. If the input channel
304 * becomes empty, the client may need to do extra work to ensure that it processes
Jeff Brown771526c2012-04-27 15:13:25 -0700305 * the deferred event despite the fact that the input channel's file descriptor
Jeff Brown2b6c32c2012-03-13 15:00:09 -0700306 * is not readable.
307 *
308 * One option is simply to call consume() in a loop until it returns WOULD_BLOCK.
309 * This guarantees that all deferred events will be processed.
310 *
311 * Alternately, the caller can call hasDeferredEvent() to determine whether there is
312 * a deferred event waiting and then ensure that its event loop wakes up at least
313 * one more time to consume the deferred event.
314 */
315 bool hasDeferredEvent() const;
316
317 /* Returns true if there is a pending batch.
318 *
319 * Should be called after calling consume() with consumeBatches == false to determine
320 * whether consume() should be called again later on with consumeBatches == true.
321 */
Jeff Brown072ec962012-02-07 14:46:57 -0800322 bool hasPendingBatch() const;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700323
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700324private:
325 sp<InputChannel> mChannel;
Jeff Brown072ec962012-02-07 14:46:57 -0800326
Jeff Brown90fde932012-02-13 12:44:01 -0800327 // The current input message.
328 InputMessage mMsg;
329
330 // True if mMsg contains a valid input message that was deferred from the previous
331 // call to consume and that still needs to be handled.
332 bool mMsgDeferred;
Jeff Brown072ec962012-02-07 14:46:57 -0800333
334 // Batched motion events per device and source.
335 struct Batch {
Jeff Brown771526c2012-04-27 15:13:25 -0700336 Vector<InputMessage> samples;
Jeff Brown072ec962012-02-07 14:46:57 -0800337 };
338 Vector<Batch> mBatches;
339
Jeff Brown771526c2012-04-27 15:13:25 -0700340 // Touch state per device and source, only for sources of class pointer.
341 struct History {
342 nsecs_t eventTime;
343 BitSet32 idBits;
344 PointerCoords pointers[MAX_POINTERS];
345
346 void initializeFrom(const InputMessage* msg) {
347 eventTime = msg->body.motion.eventTime;
348 idBits.clear();
349 for (size_t i = 0; i < msg->body.motion.pointerCount; i++) {
350 uint32_t id = msg->body.motion.pointers[i].properties.id;
351 idBits.markBit(id);
352 size_t index = idBits.getIndexOfBit(id);
353 pointers[index].copyFrom(msg->body.motion.pointers[i].coords);
354 }
355 }
356 };
357 struct TouchState {
358 int32_t deviceId;
359 int32_t source;
360 size_t historyCurrent;
361 size_t historySize;
362 History history[2];
363
364 void initialize(int32_t deviceId, int32_t source) {
365 this->deviceId = deviceId;
366 this->source = source;
367 historyCurrent = 0;
368 historySize = 0;
369 }
370
371 void addHistory(const InputMessage* msg) {
372 historyCurrent ^= 1;
373 if (historySize < 2) {
374 historySize += 1;
375 }
376 history[historyCurrent].initializeFrom(msg);
377 }
378
379 const History* getHistory(size_t index) const {
380 return &history[(historyCurrent + index) & 1];
381 }
382 };
383 Vector<TouchState> mTouchStates;
384
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800385 // Chain of batched sequence numbers. When multiple input messages are combined into
386 // a batch, we append a record here that associates the last sequence number in the
387 // batch with the previous one. When the finished signal is sent, we traverse the
388 // chain to individually finish all input messages that were part of the batch.
389 struct SeqChain {
390 uint32_t seq; // sequence number of batched input message
391 uint32_t chain; // sequence number of previous batched input message
392 };
393 Vector<SeqChain> mSeqChains;
394
Jeff Brown771526c2012-04-27 15:13:25 -0700395 status_t consumeBatch(InputEventFactoryInterface* factory,
396 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
397 status_t consumeSamples(InputEventFactoryInterface* factory,
398 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent);
399
400 void updateTouchState(InputMessage* msg);
401 void resampleTouchState(nsecs_t frameTime, MotionEvent* event,
402 const InputMessage *next);
403
Jeff Brown072ec962012-02-07 14:46:57 -0800404 ssize_t findBatch(int32_t deviceId, int32_t source) const;
Jeff Brown771526c2012-04-27 15:13:25 -0700405 ssize_t findTouchState(int32_t deviceId, int32_t source) const;
406
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800407 status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
Jeff Brown072ec962012-02-07 14:46:57 -0800408
409 static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
410 static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
Jeff Brown771526c2012-04-27 15:13:25 -0700411 static void addSample(MotionEvent* event, const InputMessage* msg);
412 static bool canAddSample(const Batch& batch, const InputMessage* msg);
413 static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
414 static bool shouldResampleTool(int32_t toolType);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700415};
416
417} // namespace android
418
Mathias Agopianb93a03f82012-02-17 15:34:57 -0800419#endif // _ANDROIDFW_INPUT_TRANSPORT_H