michael@0: /* michael@0: * Copyright (C) 2010 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef _ANDROIDFW_INPUT_TRANSPORT_H michael@0: #define _ANDROIDFW_INPUT_TRANSPORT_H michael@0: michael@0: /** michael@0: * Native input transport. michael@0: * michael@0: * The InputChannel provides a mechanism for exchanging InputMessage structures across processes. michael@0: * michael@0: * The InputPublisher and InputConsumer each handle one end-point of an input channel. michael@0: * The InputPublisher is used by the input dispatcher to send events to the application. michael@0: * The InputConsumer is used by the application to receive events from the input dispatcher. michael@0: */ michael@0: michael@0: #include "Input.h" michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: namespace android { michael@0: michael@0: /* michael@0: * Intermediate representation used to send input events and related signals. michael@0: */ michael@0: struct InputMessage { michael@0: enum { michael@0: TYPE_KEY = 1, michael@0: TYPE_MOTION = 2, michael@0: TYPE_FINISHED = 3, michael@0: }; michael@0: michael@0: struct Header { michael@0: uint32_t type; michael@0: uint32_t padding; // 8 byte alignment for the body that follows michael@0: } header; michael@0: michael@0: union Body { michael@0: struct Key { michael@0: uint32_t seq; michael@0: nsecs_t eventTime; michael@0: int32_t deviceId; michael@0: int32_t source; michael@0: int32_t action; michael@0: int32_t flags; michael@0: int32_t keyCode; michael@0: int32_t scanCode; michael@0: int32_t metaState; michael@0: int32_t repeatCount; michael@0: nsecs_t downTime; michael@0: michael@0: inline size_t size() const { michael@0: return sizeof(Key); michael@0: } michael@0: } key; michael@0: michael@0: struct Motion { michael@0: uint32_t seq; michael@0: nsecs_t eventTime; michael@0: int32_t deviceId; michael@0: int32_t source; michael@0: int32_t action; michael@0: int32_t flags; michael@0: int32_t metaState; michael@0: int32_t buttonState; michael@0: int32_t edgeFlags; michael@0: nsecs_t downTime; michael@0: float xOffset; michael@0: float yOffset; michael@0: float xPrecision; michael@0: float yPrecision; michael@0: size_t pointerCount; michael@0: struct Pointer { michael@0: PointerProperties properties; michael@0: PointerCoords coords; michael@0: } pointers[MAX_POINTERS]; michael@0: michael@0: int32_t getActionId() const { michael@0: uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) michael@0: >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; michael@0: return pointers[index].properties.id; michael@0: } michael@0: michael@0: inline size_t size() const { michael@0: return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS michael@0: + sizeof(Pointer) * pointerCount; michael@0: } michael@0: } motion; michael@0: michael@0: struct Finished { michael@0: uint32_t seq; michael@0: bool handled; michael@0: michael@0: inline size_t size() const { michael@0: return sizeof(Finished); michael@0: } michael@0: } finished; michael@0: } body; michael@0: michael@0: bool isValid(size_t actualSize) const; michael@0: size_t size() const; michael@0: }; michael@0: michael@0: /* michael@0: * An input channel consists of a local unix domain socket used to send and receive michael@0: * input messages across processes. Each channel has a descriptive name for debugging purposes. michael@0: * michael@0: * Each endpoint has its own InputChannel object that specifies its file descriptor. michael@0: * michael@0: * The input channel is closed when all references to it are released. michael@0: */ michael@0: class InputChannel : public RefBase { michael@0: protected: michael@0: virtual ~InputChannel(); michael@0: michael@0: public: michael@0: InputChannel(const String8& name, int fd); michael@0: michael@0: /* Creates a pair of input channels. michael@0: * michael@0: * Returns OK on success. michael@0: */ michael@0: static status_t openInputChannelPair(const String8& name, michael@0: sp& outServerChannel, sp& outClientChannel); michael@0: michael@0: inline String8 getName() const { return mName; } michael@0: inline int getFd() const { return mFd; } michael@0: michael@0: /* Sends a message to the other endpoint. michael@0: * michael@0: * If the channel is full then the message is guaranteed not to have been sent at all. michael@0: * Try again after the consumer has sent a finished signal indicating that it has michael@0: * consumed some of the pending messages from the channel. michael@0: * michael@0: * Returns OK on success. michael@0: * Returns WOULD_BLOCK if the channel is full. michael@0: * Returns DEAD_OBJECT if the channel's peer has been closed. michael@0: * Other errors probably indicate that the channel is broken. michael@0: */ michael@0: status_t sendMessage(const InputMessage* msg); michael@0: michael@0: /* Receives a message sent by the other endpoint. michael@0: * michael@0: * If there is no message present, try again after poll() indicates that the fd michael@0: * is readable. michael@0: * michael@0: * Returns OK on success. michael@0: * Returns WOULD_BLOCK if there is no message present. michael@0: * Returns DEAD_OBJECT if the channel's peer has been closed. michael@0: * Other errors probably indicate that the channel is broken. michael@0: */ michael@0: status_t receiveMessage(InputMessage* msg); michael@0: michael@0: /* Returns a new object that has a duplicate of this channel's fd. */ michael@0: sp dup() const; michael@0: michael@0: private: michael@0: String8 mName; michael@0: int mFd; michael@0: }; michael@0: michael@0: /* michael@0: * Publishes input events to an input channel. michael@0: */ michael@0: class InputPublisher { michael@0: public: michael@0: /* Creates a publisher associated with an input channel. */ michael@0: explicit InputPublisher(const sp& channel); michael@0: michael@0: /* Destroys the publisher and releases its input channel. */ michael@0: ~InputPublisher(); michael@0: michael@0: /* Gets the underlying input channel. */ michael@0: inline sp getChannel() { return mChannel; } michael@0: michael@0: /* Publishes a key event to the input channel. michael@0: * michael@0: * Returns OK on success. michael@0: * Returns WOULD_BLOCK if the channel is full. michael@0: * Returns DEAD_OBJECT if the channel's peer has been closed. michael@0: * Returns BAD_VALUE if seq is 0. michael@0: * Other errors probably indicate that the channel is broken. michael@0: */ michael@0: status_t publishKeyEvent( michael@0: uint32_t seq, michael@0: int32_t deviceId, michael@0: int32_t source, michael@0: int32_t action, michael@0: int32_t flags, michael@0: int32_t keyCode, michael@0: int32_t scanCode, michael@0: int32_t metaState, michael@0: int32_t repeatCount, michael@0: nsecs_t downTime, michael@0: nsecs_t eventTime); michael@0: michael@0: /* Publishes a motion event to the input channel. michael@0: * michael@0: * Returns OK on success. michael@0: * Returns WOULD_BLOCK if the channel is full. michael@0: * Returns DEAD_OBJECT if the channel's peer has been closed. michael@0: * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS. michael@0: * Other errors probably indicate that the channel is broken. michael@0: */ michael@0: status_t publishMotionEvent( michael@0: uint32_t seq, michael@0: int32_t deviceId, michael@0: int32_t source, michael@0: int32_t action, michael@0: int32_t flags, michael@0: int32_t edgeFlags, michael@0: int32_t metaState, michael@0: int32_t buttonState, michael@0: float xOffset, michael@0: float yOffset, michael@0: float xPrecision, michael@0: float yPrecision, michael@0: nsecs_t downTime, michael@0: nsecs_t eventTime, michael@0: size_t pointerCount, michael@0: const PointerProperties* pointerProperties, michael@0: const PointerCoords* pointerCoords); michael@0: michael@0: /* Receives the finished signal from the consumer in reply to the original dispatch signal. michael@0: * If a signal was received, returns the message sequence number, michael@0: * and whether the consumer handled the message. michael@0: * michael@0: * The returned sequence number is never 0 unless the operation failed. michael@0: * michael@0: * Returns OK on success. michael@0: * Returns WOULD_BLOCK if there is no signal present. michael@0: * Returns DEAD_OBJECT if the channel's peer has been closed. michael@0: * Other errors probably indicate that the channel is broken. michael@0: */ michael@0: status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled); michael@0: michael@0: private: michael@0: sp mChannel; michael@0: }; michael@0: michael@0: /* michael@0: * Consumes input events from an input channel. michael@0: */ michael@0: class InputConsumer { michael@0: public: michael@0: /* Creates a consumer associated with an input channel. */ michael@0: explicit InputConsumer(const sp& channel); michael@0: michael@0: /* Destroys the consumer and releases its input channel. */ michael@0: ~InputConsumer(); michael@0: michael@0: /* Gets the underlying input channel. */ michael@0: inline sp getChannel() { return mChannel; } michael@0: michael@0: /* Consumes an input event from the input channel and copies its contents into michael@0: * an InputEvent object created using the specified factory. michael@0: * michael@0: * Tries to combine a series of move events into larger batches whenever possible. michael@0: * michael@0: * If consumeBatches is false, then defers consuming pending batched events if it michael@0: * is possible for additional samples to be added to them later. Call hasPendingBatch() michael@0: * to determine whether a pending batch is available to be consumed. michael@0: * michael@0: * If consumeBatches is true, then events are still batched but they are consumed michael@0: * immediately as soon as the input channel is exhausted. michael@0: * michael@0: * The frameTime parameter specifies the time when the current display frame started michael@0: * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown. michael@0: * michael@0: * The returned sequence number is never 0 unless the operation failed. michael@0: * michael@0: * Returns OK on success. michael@0: * Returns WOULD_BLOCK if there is no event present. michael@0: * Returns DEAD_OBJECT if the channel's peer has been closed. michael@0: * Returns NO_MEMORY if the event could not be created. michael@0: * Other errors probably indicate that the channel is broken. michael@0: */ michael@0: status_t consume(InputEventFactoryInterface* factory, bool consumeBatches, michael@0: nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent); michael@0: michael@0: /* Sends a finished signal to the publisher to inform it that the message michael@0: * with the specified sequence number has finished being process and whether michael@0: * the message was handled by the consumer. michael@0: * michael@0: * Returns OK on success. michael@0: * Returns BAD_VALUE if seq is 0. michael@0: * Other errors probably indicate that the channel is broken. michael@0: */ michael@0: status_t sendFinishedSignal(uint32_t seq, bool handled); michael@0: michael@0: /* Returns true if there is a deferred event waiting. michael@0: * michael@0: * Should be called after calling consume() to determine whether the consumer michael@0: * has a deferred event to be processed. Deferred events are somewhat special in michael@0: * that they have already been removed from the input channel. If the input channel michael@0: * becomes empty, the client may need to do extra work to ensure that it processes michael@0: * the deferred event despite the fact that the input channel's file descriptor michael@0: * is not readable. michael@0: * michael@0: * One option is simply to call consume() in a loop until it returns WOULD_BLOCK. michael@0: * This guarantees that all deferred events will be processed. michael@0: * michael@0: * Alternately, the caller can call hasDeferredEvent() to determine whether there is michael@0: * a deferred event waiting and then ensure that its event loop wakes up at least michael@0: * one more time to consume the deferred event. michael@0: */ michael@0: bool hasDeferredEvent() const; michael@0: michael@0: /* Returns true if there is a pending batch. michael@0: * michael@0: * Should be called after calling consume() with consumeBatches == false to determine michael@0: * whether consume() should be called again later on with consumeBatches == true. michael@0: */ michael@0: bool hasPendingBatch() const; michael@0: michael@0: private: michael@0: // True if touch resampling is enabled. michael@0: const bool mResampleTouch; michael@0: michael@0: // The input channel. michael@0: sp mChannel; michael@0: michael@0: // The current input message. michael@0: InputMessage mMsg; michael@0: michael@0: // True if mMsg contains a valid input message that was deferred from the previous michael@0: // call to consume and that still needs to be handled. michael@0: bool mMsgDeferred; michael@0: michael@0: // Batched motion events per device and source. michael@0: struct Batch { michael@0: Vector samples; michael@0: }; michael@0: Vector mBatches; michael@0: michael@0: // Touch state per device and source, only for sources of class pointer. michael@0: struct History { michael@0: nsecs_t eventTime; michael@0: BitSet32 idBits; michael@0: int32_t idToIndex[MAX_POINTER_ID + 1]; michael@0: PointerCoords pointers[MAX_POINTERS]; michael@0: michael@0: void initializeFrom(const InputMessage* msg) { michael@0: eventTime = msg->body.motion.eventTime; michael@0: idBits.clear(); michael@0: for (size_t i = 0; i < msg->body.motion.pointerCount; i++) { michael@0: uint32_t id = msg->body.motion.pointers[i].properties.id; michael@0: idBits.markBit(id); michael@0: idToIndex[id] = i; michael@0: pointers[i].copyFrom(msg->body.motion.pointers[i].coords); michael@0: } michael@0: } michael@0: michael@0: const PointerCoords& getPointerById(uint32_t id) const { michael@0: return pointers[idToIndex[id]]; michael@0: } michael@0: }; michael@0: struct TouchState { michael@0: int32_t deviceId; michael@0: int32_t source; michael@0: size_t historyCurrent; michael@0: size_t historySize; michael@0: History history[2]; michael@0: History lastResample; michael@0: michael@0: void initialize(int32_t deviceId, int32_t source) { michael@0: this->deviceId = deviceId; michael@0: this->source = source; michael@0: historyCurrent = 0; michael@0: historySize = 0; michael@0: lastResample.eventTime = 0; michael@0: lastResample.idBits.clear(); michael@0: } michael@0: michael@0: void addHistory(const InputMessage* msg) { michael@0: historyCurrent ^= 1; michael@0: if (historySize < 2) { michael@0: historySize += 1; michael@0: } michael@0: history[historyCurrent].initializeFrom(msg); michael@0: } michael@0: michael@0: const History* getHistory(size_t index) const { michael@0: return &history[(historyCurrent + index) & 1]; michael@0: } michael@0: }; michael@0: Vector mTouchStates; michael@0: michael@0: // Chain of batched sequence numbers. When multiple input messages are combined into michael@0: // a batch, we append a record here that associates the last sequence number in the michael@0: // batch with the previous one. When the finished signal is sent, we traverse the michael@0: // chain to individually finish all input messages that were part of the batch. michael@0: struct SeqChain { michael@0: uint32_t seq; // sequence number of batched input message michael@0: uint32_t chain; // sequence number of previous batched input message michael@0: }; michael@0: Vector mSeqChains; michael@0: michael@0: status_t consumeBatch(InputEventFactoryInterface* factory, michael@0: nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent); michael@0: status_t consumeSamples(InputEventFactoryInterface* factory, michael@0: Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent); michael@0: michael@0: void updateTouchState(InputMessage* msg); michael@0: void rewriteMessage(const TouchState& state, InputMessage* msg); michael@0: void resampleTouchState(nsecs_t frameTime, MotionEvent* event, michael@0: const InputMessage *next); michael@0: michael@0: ssize_t findBatch(int32_t deviceId, int32_t source) const; michael@0: ssize_t findTouchState(int32_t deviceId, int32_t source) const; michael@0: michael@0: status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled); michael@0: michael@0: static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg); michael@0: static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg); michael@0: static void addSample(MotionEvent* event, const InputMessage* msg); michael@0: static bool canAddSample(const Batch& batch, const InputMessage* msg); michael@0: static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time); michael@0: static bool shouldResampleTool(int32_t toolType); michael@0: michael@0: static bool isTouchResamplingEnabled(); michael@0: }; michael@0: michael@0: } // namespace android michael@0: michael@0: #endif // _ANDROIDFW_INPUT_TRANSPORT_H