blob: 87129958bf1a315ea329f993cf36a926f0de24bf [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 Brown771526c2012-04-27 15:13:25 -070036#include <utils/BitSet.h>
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -070079 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -070080 int32_t flags;
Jeff Brown46b9ac02010-04-22 18:58:52 -070081 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070082 int32_t buttonState;
Jeff Brown46b9ac02010-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
Jeff Brown7174a492012-05-14 17:00:27 -070095 int32_t getActionId() const {
96 uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
97 >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
98 return pointers[index].properties.id;
99 }
100
Jeff Browncbee6d62012-02-03 20:11:27 -0800101 inline size_t size() const {
102 return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS
103 + sizeof(Pointer) * pointerCount;
104 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700105 } motion;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700106
Jeff Browncbee6d62012-02-03 20:11:27 -0800107 struct Finished {
Jeff Brown072ec962012-02-07 14:46:57 -0800108 uint32_t seq;
Jeff Browncbee6d62012-02-03 20:11:27 -0800109 bool handled;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700110
Jeff Browncbee6d62012-02-03 20:11:27 -0800111 inline size_t size() const {
112 return sizeof(Finished);
113 }
114 } finished;
115 } body;
116
117 bool isValid(size_t actualSize) const;
118 size_t size() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700119};
120
121/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800122 * An input channel consists of a local unix domain socket used to send and receive
123 * input messages across processes. Each channel has a descriptive name for debugging purposes.
124 *
125 * Each endpoint has its own InputChannel object that specifies its file descriptor.
126 *
127 * The input channel is closed when all references to it are released.
128 */
129class InputChannel : public RefBase {
130protected:
131 virtual ~InputChannel();
132
133public:
Jeff Brown91e32892012-02-14 15:56:29 -0800134 InputChannel(const String8& name, int fd);
Jeff Browncbee6d62012-02-03 20:11:27 -0800135
136 /* Creates a pair of input channels.
137 *
138 * Returns OK on success.
139 */
140 static status_t openInputChannelPair(const String8& name,
141 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel);
142
143 inline String8 getName() const { return mName; }
Jeff Brown91e32892012-02-14 15:56:29 -0800144 inline int getFd() const { return mFd; }
Jeff Browncbee6d62012-02-03 20:11:27 -0800145
146 /* Sends a message to the other endpoint.
147 *
148 * If the channel is full then the message is guaranteed not to have been sent at all.
149 * Try again after the consumer has sent a finished signal indicating that it has
150 * consumed some of the pending messages from the channel.
151 *
152 * Returns OK on success.
153 * Returns WOULD_BLOCK if the channel is full.
154 * Returns DEAD_OBJECT if the channel's peer has been closed.
155 * Other errors probably indicate that the channel is broken.
156 */
157 status_t sendMessage(const InputMessage* msg);
158
159 /* Receives a message sent by the other endpoint.
160 *
161 * If there is no message present, try again after poll() indicates that the fd
162 * is readable.
163 *
164 * Returns OK on success.
165 * Returns WOULD_BLOCK if there is no message present.
166 * Returns DEAD_OBJECT if the channel's peer has been closed.
167 * Other errors probably indicate that the channel is broken.
168 */
169 status_t receiveMessage(InputMessage* msg);
170
Jeff Brown1951ce82013-04-04 22:45:12 -0700171 /* Returns a new object that has a duplicate of this channel's fd. */
172 sp<InputChannel> dup() const;
173
Jeff Browncbee6d62012-02-03 20:11:27 -0800174private:
175 String8 mName;
Jeff Brown91e32892012-02-14 15:56:29 -0800176 int mFd;
Jeff Browncbee6d62012-02-03 20:11:27 -0800177};
178
179/*
180 * Publishes input events to an input channel.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700181 */
182class InputPublisher {
183public:
184 /* Creates a publisher associated with an input channel. */
185 explicit InputPublisher(const sp<InputChannel>& channel);
186
187 /* Destroys the publisher and releases its input channel. */
188 ~InputPublisher();
189
190 /* Gets the underlying input channel. */
191 inline sp<InputChannel> getChannel() { return mChannel; }
192
Jeff Browncbee6d62012-02-03 20:11:27 -0800193 /* Publishes a key event to the input channel.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700194 *
195 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800196 * Returns WOULD_BLOCK if the channel is full.
197 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown072ec962012-02-07 14:46:57 -0800198 * Returns BAD_VALUE if seq is 0.
Jeff Browncbee6d62012-02-03 20:11:27 -0800199 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700200 */
201 status_t publishKeyEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800202 uint32_t seq,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700203 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700204 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700205 int32_t action,
206 int32_t flags,
207 int32_t keyCode,
208 int32_t scanCode,
209 int32_t metaState,
210 int32_t repeatCount,
211 nsecs_t downTime,
212 nsecs_t eventTime);
213
Jeff Browncbee6d62012-02-03 20:11:27 -0800214 /* Publishes a motion event to the input channel.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700215 *
216 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800217 * Returns WOULD_BLOCK if the channel is full.
218 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown072ec962012-02-07 14:46:57 -0800219 * 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 -0800220 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700221 */
222 status_t publishMotionEvent(
Jeff Brown072ec962012-02-07 14:46:57 -0800223 uint32_t seq,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700224 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700225 int32_t source,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700226 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700227 int32_t flags,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700228 int32_t edgeFlags,
229 int32_t metaState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700230 int32_t buttonState,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700231 float xOffset,
232 float yOffset,
233 float xPrecision,
234 float yPrecision,
235 nsecs_t downTime,
236 nsecs_t eventTime,
237 size_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700238 const PointerProperties* pointerProperties,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700239 const PointerCoords* pointerCoords);
240
Jeff Brown46b9ac02010-04-22 18:58:52 -0700241 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
Jeff Brown072ec962012-02-07 14:46:57 -0800242 * If a signal was received, returns the message sequence number,
243 * and whether the consumer handled the message.
244 *
245 * The returned sequence number is never 0 unless the operation failed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700246 *
247 * Returns OK on success.
248 * Returns WOULD_BLOCK if there is no signal present.
Jeff Browncbee6d62012-02-03 20:11:27 -0800249 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700250 * Other errors probably indicate that the channel is broken.
251 */
Jeff Brown072ec962012-02-07 14:46:57 -0800252 status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700253
254private:
255 sp<InputChannel> mChannel;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700256};
257
258/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800259 * Consumes input events from an input channel.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700260 */
261class InputConsumer {
262public:
263 /* Creates a consumer associated with an input channel. */
264 explicit InputConsumer(const sp<InputChannel>& channel);
265
266 /* Destroys the consumer and releases its input channel. */
267 ~InputConsumer();
268
269 /* Gets the underlying input channel. */
270 inline sp<InputChannel> getChannel() { return mChannel; }
271
Jeff Browncbee6d62012-02-03 20:11:27 -0800272 /* Consumes an input event from the input channel and copies its contents into
Jeff Brown46b9ac02010-04-22 18:58:52 -0700273 * an InputEvent object created using the specified factory.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700274 *
Jeff Brown072ec962012-02-07 14:46:57 -0800275 * Tries to combine a series of move events into larger batches whenever possible.
276 *
277 * If consumeBatches is false, then defers consuming pending batched events if it
278 * is possible for additional samples to be added to them later. Call hasPendingBatch()
279 * to determine whether a pending batch is available to be consumed.
280 *
281 * If consumeBatches is true, then events are still batched but they are consumed
282 * immediately as soon as the input channel is exhausted.
283 *
Jeff Brown771526c2012-04-27 15:13:25 -0700284 * The frameTime parameter specifies the time when the current display frame started
285 * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown.
286 *
Jeff Brown072ec962012-02-07 14:46:57 -0800287 * The returned sequence number is never 0 unless the operation failed.
288 *
Jeff Brown46b9ac02010-04-22 18:58:52 -0700289 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800290 * Returns WOULD_BLOCK if there is no event present.
291 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292 * Returns NO_MEMORY if the event could not be created.
Jeff Browncbee6d62012-02-03 20:11:27 -0800293 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700294 */
Jeff Brown072ec962012-02-07 14:46:57 -0800295 status_t consume(InputEventFactoryInterface* factory, bool consumeBatches,
Jeff Brown771526c2012-04-27 15:13:25 -0700296 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700297
Jeff Brown072ec962012-02-07 14:46:57 -0800298 /* Sends a finished signal to the publisher to inform it that the message
299 * with the specified sequence number has finished being process and whether
300 * the message was handled by the consumer.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700301 *
302 * Returns OK on success.
Jeff Brown072ec962012-02-07 14:46:57 -0800303 * Returns BAD_VALUE if seq is 0.
Jeff Browncbee6d62012-02-03 20:11:27 -0800304 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700305 */
Jeff Brown072ec962012-02-07 14:46:57 -0800306 status_t sendFinishedSignal(uint32_t seq, bool handled);
307
Jeff Brown2b6c32c2012-03-13 15:00:09 -0700308 /* Returns true if there is a deferred event waiting.
309 *
310 * Should be called after calling consume() to determine whether the consumer
311 * has a deferred event to be processed. Deferred events are somewhat special in
312 * that they have already been removed from the input channel. If the input channel
313 * becomes empty, the client may need to do extra work to ensure that it processes
Jeff Brown771526c2012-04-27 15:13:25 -0700314 * the deferred event despite the fact that the input channel's file descriptor
Jeff Brown2b6c32c2012-03-13 15:00:09 -0700315 * is not readable.
316 *
317 * One option is simply to call consume() in a loop until it returns WOULD_BLOCK.
318 * This guarantees that all deferred events will be processed.
319 *
320 * Alternately, the caller can call hasDeferredEvent() to determine whether there is
321 * a deferred event waiting and then ensure that its event loop wakes up at least
322 * one more time to consume the deferred event.
323 */
324 bool hasDeferredEvent() const;
325
326 /* Returns true if there is a pending batch.
327 *
328 * Should be called after calling consume() with consumeBatches == false to determine
329 * whether consume() should be called again later on with consumeBatches == true.
330 */
Jeff Brown072ec962012-02-07 14:46:57 -0800331 bool hasPendingBatch() const;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700332
Jeff Brown46b9ac02010-04-22 18:58:52 -0700333private:
Jeff Brown7174a492012-05-14 17:00:27 -0700334 // True if touch resampling is enabled.
335 const bool mResampleTouch;
336
337 // The input channel.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700338 sp<InputChannel> mChannel;
Jeff Brown072ec962012-02-07 14:46:57 -0800339
Jeff Brown90fde932012-02-13 12:44:01 -0800340 // The current input message.
341 InputMessage mMsg;
342
343 // True if mMsg contains a valid input message that was deferred from the previous
344 // call to consume and that still needs to be handled.
345 bool mMsgDeferred;
Jeff Brown072ec962012-02-07 14:46:57 -0800346
347 // Batched motion events per device and source.
348 struct Batch {
Jeff Brown771526c2012-04-27 15:13:25 -0700349 Vector<InputMessage> samples;
Jeff Brown072ec962012-02-07 14:46:57 -0800350 };
351 Vector<Batch> mBatches;
352
Jeff Brown771526c2012-04-27 15:13:25 -0700353 // Touch state per device and source, only for sources of class pointer.
354 struct History {
355 nsecs_t eventTime;
356 BitSet32 idBits;
Jeff Brown7174a492012-05-14 17:00:27 -0700357 int32_t idToIndex[MAX_POINTER_ID + 1];
Jeff Brown771526c2012-04-27 15:13:25 -0700358 PointerCoords pointers[MAX_POINTERS];
359
360 void initializeFrom(const InputMessage* msg) {
361 eventTime = msg->body.motion.eventTime;
362 idBits.clear();
363 for (size_t i = 0; i < msg->body.motion.pointerCount; i++) {
364 uint32_t id = msg->body.motion.pointers[i].properties.id;
365 idBits.markBit(id);
Jeff Brown7174a492012-05-14 17:00:27 -0700366 idToIndex[id] = i;
367 pointers[i].copyFrom(msg->body.motion.pointers[i].coords);
Jeff Brown771526c2012-04-27 15:13:25 -0700368 }
369 }
Jeff Brown7174a492012-05-14 17:00:27 -0700370
371 const PointerCoords& getPointerById(uint32_t id) const {
372 return pointers[idToIndex[id]];
373 }
Jeff Brown771526c2012-04-27 15:13:25 -0700374 };
375 struct TouchState {
376 int32_t deviceId;
377 int32_t source;
378 size_t historyCurrent;
379 size_t historySize;
380 History history[2];
Jeff Brown7174a492012-05-14 17:00:27 -0700381 History lastResample;
Jeff Brown771526c2012-04-27 15:13:25 -0700382
383 void initialize(int32_t deviceId, int32_t source) {
384 this->deviceId = deviceId;
385 this->source = source;
386 historyCurrent = 0;
387 historySize = 0;
Jeff Brown7174a492012-05-14 17:00:27 -0700388 lastResample.eventTime = 0;
389 lastResample.idBits.clear();
Jeff Brown771526c2012-04-27 15:13:25 -0700390 }
391
392 void addHistory(const InputMessage* msg) {
393 historyCurrent ^= 1;
394 if (historySize < 2) {
395 historySize += 1;
396 }
397 history[historyCurrent].initializeFrom(msg);
398 }
399
400 const History* getHistory(size_t index) const {
401 return &history[(historyCurrent + index) & 1];
402 }
403 };
404 Vector<TouchState> mTouchStates;
405
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800406 // Chain of batched sequence numbers. When multiple input messages are combined into
407 // a batch, we append a record here that associates the last sequence number in the
408 // batch with the previous one. When the finished signal is sent, we traverse the
409 // chain to individually finish all input messages that were part of the batch.
410 struct SeqChain {
411 uint32_t seq; // sequence number of batched input message
412 uint32_t chain; // sequence number of previous batched input message
413 };
414 Vector<SeqChain> mSeqChains;
415
Jeff Brown771526c2012-04-27 15:13:25 -0700416 status_t consumeBatch(InputEventFactoryInterface* factory,
417 nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent);
418 status_t consumeSamples(InputEventFactoryInterface* factory,
419 Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent);
420
421 void updateTouchState(InputMessage* msg);
Jeff Brown7174a492012-05-14 17:00:27 -0700422 void rewriteMessage(const TouchState& state, InputMessage* msg);
Jeff Brown771526c2012-04-27 15:13:25 -0700423 void resampleTouchState(nsecs_t frameTime, MotionEvent* event,
424 const InputMessage *next);
425
Jeff Brown072ec962012-02-07 14:46:57 -0800426 ssize_t findBatch(int32_t deviceId, int32_t source) const;
Jeff Brown771526c2012-04-27 15:13:25 -0700427 ssize_t findTouchState(int32_t deviceId, int32_t source) const;
428
Jeff Brown2d34e0c2012-02-13 13:18:09 -0800429 status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled);
Jeff Brown072ec962012-02-07 14:46:57 -0800430
431 static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg);
432 static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg);
Jeff Brown771526c2012-04-27 15:13:25 -0700433 static void addSample(MotionEvent* event, const InputMessage* msg);
434 static bool canAddSample(const Batch& batch, const InputMessage* msg);
435 static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time);
436 static bool shouldResampleTool(int32_t toolType);
Jeff Brown7174a492012-05-14 17:00:27 -0700437
438 static bool isTouchResamplingEnabled();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700439};
440
441} // namespace android
442
Mathias Agopianb93a03f82012-02-17 15:34:57 -0800443#endif // _ANDROIDFW_INPUT_TRANSPORT_H