Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 17 | #ifndef _ANDROIDFW_INPUT_TRANSPORT_H |
| 18 | #define _ANDROIDFW_INPUT_TRANSPORT_H |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Native input transport. |
| 22 | * |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 23 | * The InputChannel provides a mechanism for exchanging InputMessage structures across processes. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 24 | * |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 25 | * 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
Mathias Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 30 | #include <androidfw/Input.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 31 | #include <utils/Errors.h> |
| 32 | #include <utils/Timers.h> |
| 33 | #include <utils/RefBase.h> |
| 34 | #include <utils/String8.h> |
Mathias Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 35 | #include <utils/Vector.h> |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 36 | #include <utils/BitSet.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | /* |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 41 | * Intermediate representation used to send input events and related signals. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 42 | */ |
| 43 | struct InputMessage { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 44 | enum { |
| 45 | TYPE_KEY = 1, |
| 46 | TYPE_MOTION = 2, |
| 47 | TYPE_FINISHED = 3, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 50 | struct Header { |
| 51 | uint32_t type; |
| 52 | uint32_t padding; // 8 byte alignment for the body that follows |
| 53 | } header; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 54 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 55 | union Body { |
| 56 | struct Key { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 57 | uint32_t seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 58 | nsecs_t eventTime; |
| 59 | int32_t deviceId; |
| 60 | int32_t source; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 61 | 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 Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 68 | |
| 69 | inline size_t size() const { |
| 70 | return sizeof(Key); |
| 71 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 72 | } key; |
| 73 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 74 | struct Motion { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 75 | uint32_t seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 76 | nsecs_t eventTime; |
| 77 | int32_t deviceId; |
| 78 | int32_t source; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 79 | int32_t action; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 80 | int32_t flags; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 81 | int32_t metaState; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 82 | int32_t buttonState; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 83 | int32_t edgeFlags; |
| 84 | nsecs_t downTime; |
| 85 | float xOffset; |
| 86 | float yOffset; |
| 87 | float xPrecision; |
| 88 | float yPrecision; |
| 89 | size_t pointerCount; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 90 | struct Pointer { |
| 91 | PointerProperties properties; |
| 92 | PointerCoords coords; |
| 93 | } pointers[MAX_POINTERS]; |
| 94 | |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 95 | 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 Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 101 | inline size_t size() const { |
| 102 | return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS |
| 103 | + sizeof(Pointer) * pointerCount; |
| 104 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 105 | } motion; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 106 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 107 | struct Finished { |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 108 | uint32_t seq; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 109 | bool handled; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 110 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 111 | 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 Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | /* |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 122 | * 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 | */ |
| 129 | class InputChannel : public RefBase { |
| 130 | protected: |
| 131 | virtual ~InputChannel(); |
| 132 | |
| 133 | public: |
Jeff Brown | 91e3289 | 2012-02-14 15:56:29 -0800 | [diff] [blame] | 134 | InputChannel(const String8& name, int fd); |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 135 | |
| 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 Brown | 91e3289 | 2012-02-14 15:56:29 -0800 | [diff] [blame] | 144 | inline int getFd() const { return mFd; } |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 145 | |
| 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 | |
| 171 | private: |
| 172 | String8 mName; |
Jeff Brown | 91e3289 | 2012-02-14 15:56:29 -0800 | [diff] [blame] | 173 | int mFd; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | /* |
| 177 | * Publishes input events to an input channel. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 178 | */ |
| 179 | class InputPublisher { |
| 180 | public: |
| 181 | /* Creates a publisher associated with an input channel. */ |
| 182 | explicit InputPublisher(const sp<InputChannel>& channel); |
| 183 | |
| 184 | /* Destroys the publisher and releases its input channel. */ |
| 185 | ~InputPublisher(); |
| 186 | |
| 187 | /* Gets the underlying input channel. */ |
| 188 | inline sp<InputChannel> getChannel() { return mChannel; } |
| 189 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 190 | /* Publishes a key event to the input channel. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 191 | * |
| 192 | * Returns OK on success. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 193 | * Returns WOULD_BLOCK if the channel is full. |
| 194 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 195 | * Returns BAD_VALUE if seq is 0. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 196 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 197 | */ |
| 198 | status_t publishKeyEvent( |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 199 | uint32_t seq, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 200 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 201 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 202 | int32_t action, |
| 203 | int32_t flags, |
| 204 | int32_t keyCode, |
| 205 | int32_t scanCode, |
| 206 | int32_t metaState, |
| 207 | int32_t repeatCount, |
| 208 | nsecs_t downTime, |
| 209 | nsecs_t eventTime); |
| 210 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 211 | /* Publishes a motion event to the input channel. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 212 | * |
| 213 | * Returns OK on success. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 214 | * Returns WOULD_BLOCK if the channel is full. |
| 215 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 216 | * Returns BAD_VALUE if seq is 0 or if pointerCount is less than 1 or greater than MAX_POINTERS. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 217 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 218 | */ |
| 219 | status_t publishMotionEvent( |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 220 | uint32_t seq, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 221 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 222 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 223 | int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 224 | int32_t flags, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 225 | int32_t edgeFlags, |
| 226 | int32_t metaState, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 227 | int32_t buttonState, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 228 | float xOffset, |
| 229 | float yOffset, |
| 230 | float xPrecision, |
| 231 | float yPrecision, |
| 232 | nsecs_t downTime, |
| 233 | nsecs_t eventTime, |
| 234 | size_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 235 | const PointerProperties* pointerProperties, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 236 | const PointerCoords* pointerCoords); |
| 237 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 238 | /* Receives the finished signal from the consumer in reply to the original dispatch signal. |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 239 | * If a signal was received, returns the message sequence number, |
| 240 | * and whether the consumer handled the message. |
| 241 | * |
| 242 | * The returned sequence number is never 0 unless the operation failed. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 243 | * |
| 244 | * Returns OK on success. |
| 245 | * Returns WOULD_BLOCK if there is no signal present. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 246 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 247 | * Other errors probably indicate that the channel is broken. |
| 248 | */ |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 249 | status_t receiveFinishedSignal(uint32_t* outSeq, bool* outHandled); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 250 | |
| 251 | private: |
| 252 | sp<InputChannel> mChannel; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | /* |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 256 | * Consumes input events from an input channel. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 257 | */ |
| 258 | class InputConsumer { |
| 259 | public: |
| 260 | /* Creates a consumer associated with an input channel. */ |
| 261 | explicit InputConsumer(const sp<InputChannel>& channel); |
| 262 | |
| 263 | /* Destroys the consumer and releases its input channel. */ |
| 264 | ~InputConsumer(); |
| 265 | |
| 266 | /* Gets the underlying input channel. */ |
| 267 | inline sp<InputChannel> getChannel() { return mChannel; } |
| 268 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 269 | /* Consumes an input event from the input channel and copies its contents into |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 270 | * an InputEvent object created using the specified factory. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 271 | * |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 272 | * Tries to combine a series of move events into larger batches whenever possible. |
| 273 | * |
| 274 | * If consumeBatches is false, then defers consuming pending batched events if it |
| 275 | * is possible for additional samples to be added to them later. Call hasPendingBatch() |
| 276 | * to determine whether a pending batch is available to be consumed. |
| 277 | * |
| 278 | * If consumeBatches is true, then events are still batched but they are consumed |
| 279 | * immediately as soon as the input channel is exhausted. |
| 280 | * |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 281 | * The frameTime parameter specifies the time when the current display frame started |
| 282 | * rendering in the CLOCK_MONOTONIC time base, or -1 if unknown. |
| 283 | * |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 284 | * The returned sequence number is never 0 unless the operation failed. |
| 285 | * |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 286 | * Returns OK on success. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 287 | * Returns WOULD_BLOCK if there is no event present. |
| 288 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 289 | * Returns NO_MEMORY if the event could not be created. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 290 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 291 | */ |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 292 | status_t consume(InputEventFactoryInterface* factory, bool consumeBatches, |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 293 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 294 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 295 | /* Sends a finished signal to the publisher to inform it that the message |
| 296 | * with the specified sequence number has finished being process and whether |
| 297 | * the message was handled by the consumer. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 298 | * |
| 299 | * Returns OK on success. |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 300 | * Returns BAD_VALUE if seq is 0. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 301 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 302 | */ |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 303 | status_t sendFinishedSignal(uint32_t seq, bool handled); |
| 304 | |
Jeff Brown | 2b6c32c | 2012-03-13 15:00:09 -0700 | [diff] [blame] | 305 | /* Returns true if there is a deferred event waiting. |
| 306 | * |
| 307 | * Should be called after calling consume() to determine whether the consumer |
| 308 | * has a deferred event to be processed. Deferred events are somewhat special in |
| 309 | * that they have already been removed from the input channel. If the input channel |
| 310 | * becomes empty, the client may need to do extra work to ensure that it processes |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 311 | * the deferred event despite the fact that the input channel's file descriptor |
Jeff Brown | 2b6c32c | 2012-03-13 15:00:09 -0700 | [diff] [blame] | 312 | * is not readable. |
| 313 | * |
| 314 | * One option is simply to call consume() in a loop until it returns WOULD_BLOCK. |
| 315 | * This guarantees that all deferred events will be processed. |
| 316 | * |
| 317 | * Alternately, the caller can call hasDeferredEvent() to determine whether there is |
| 318 | * a deferred event waiting and then ensure that its event loop wakes up at least |
| 319 | * one more time to consume the deferred event. |
| 320 | */ |
| 321 | bool hasDeferredEvent() const; |
| 322 | |
| 323 | /* Returns true if there is a pending batch. |
| 324 | * |
| 325 | * Should be called after calling consume() with consumeBatches == false to determine |
| 326 | * whether consume() should be called again later on with consumeBatches == true. |
| 327 | */ |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 328 | bool hasPendingBatch() const; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 329 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 330 | private: |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 331 | // True if touch resampling is enabled. |
| 332 | const bool mResampleTouch; |
| 333 | |
| 334 | // The input channel. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 335 | sp<InputChannel> mChannel; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 336 | |
Jeff Brown | 90fde93 | 2012-02-13 12:44:01 -0800 | [diff] [blame] | 337 | // The current input message. |
| 338 | InputMessage mMsg; |
| 339 | |
| 340 | // True if mMsg contains a valid input message that was deferred from the previous |
| 341 | // call to consume and that still needs to be handled. |
| 342 | bool mMsgDeferred; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 343 | |
| 344 | // Batched motion events per device and source. |
| 345 | struct Batch { |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 346 | Vector<InputMessage> samples; |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 347 | }; |
| 348 | Vector<Batch> mBatches; |
| 349 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 350 | // Touch state per device and source, only for sources of class pointer. |
| 351 | struct History { |
| 352 | nsecs_t eventTime; |
| 353 | BitSet32 idBits; |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 354 | int32_t idToIndex[MAX_POINTER_ID + 1]; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 355 | PointerCoords pointers[MAX_POINTERS]; |
| 356 | |
| 357 | void initializeFrom(const InputMessage* msg) { |
| 358 | eventTime = msg->body.motion.eventTime; |
| 359 | idBits.clear(); |
| 360 | for (size_t i = 0; i < msg->body.motion.pointerCount; i++) { |
| 361 | uint32_t id = msg->body.motion.pointers[i].properties.id; |
| 362 | idBits.markBit(id); |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 363 | idToIndex[id] = i; |
| 364 | pointers[i].copyFrom(msg->body.motion.pointers[i].coords); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 367 | |
| 368 | const PointerCoords& getPointerById(uint32_t id) const { |
| 369 | return pointers[idToIndex[id]]; |
| 370 | } |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 371 | }; |
| 372 | struct TouchState { |
| 373 | int32_t deviceId; |
| 374 | int32_t source; |
| 375 | size_t historyCurrent; |
| 376 | size_t historySize; |
| 377 | History history[2]; |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 378 | History lastResample; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 379 | |
| 380 | void initialize(int32_t deviceId, int32_t source) { |
| 381 | this->deviceId = deviceId; |
| 382 | this->source = source; |
| 383 | historyCurrent = 0; |
| 384 | historySize = 0; |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 385 | lastResample.eventTime = 0; |
| 386 | lastResample.idBits.clear(); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void addHistory(const InputMessage* msg) { |
| 390 | historyCurrent ^= 1; |
| 391 | if (historySize < 2) { |
| 392 | historySize += 1; |
| 393 | } |
| 394 | history[historyCurrent].initializeFrom(msg); |
| 395 | } |
| 396 | |
| 397 | const History* getHistory(size_t index) const { |
| 398 | return &history[(historyCurrent + index) & 1]; |
| 399 | } |
| 400 | }; |
| 401 | Vector<TouchState> mTouchStates; |
| 402 | |
Jeff Brown | 2d34e0c | 2012-02-13 13:18:09 -0800 | [diff] [blame] | 403 | // Chain of batched sequence numbers. When multiple input messages are combined into |
| 404 | // a batch, we append a record here that associates the last sequence number in the |
| 405 | // batch with the previous one. When the finished signal is sent, we traverse the |
| 406 | // chain to individually finish all input messages that were part of the batch. |
| 407 | struct SeqChain { |
| 408 | uint32_t seq; // sequence number of batched input message |
| 409 | uint32_t chain; // sequence number of previous batched input message |
| 410 | }; |
| 411 | Vector<SeqChain> mSeqChains; |
| 412 | |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 413 | status_t consumeBatch(InputEventFactoryInterface* factory, |
| 414 | nsecs_t frameTime, uint32_t* outSeq, InputEvent** outEvent); |
| 415 | status_t consumeSamples(InputEventFactoryInterface* factory, |
| 416 | Batch& batch, size_t count, uint32_t* outSeq, InputEvent** outEvent); |
| 417 | |
| 418 | void updateTouchState(InputMessage* msg); |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 419 | void rewriteMessage(const TouchState& state, InputMessage* msg); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 420 | void resampleTouchState(nsecs_t frameTime, MotionEvent* event, |
| 421 | const InputMessage *next); |
| 422 | |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 423 | ssize_t findBatch(int32_t deviceId, int32_t source) const; |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 424 | ssize_t findTouchState(int32_t deviceId, int32_t source) const; |
| 425 | |
Jeff Brown | 2d34e0c | 2012-02-13 13:18:09 -0800 | [diff] [blame] | 426 | status_t sendUnchainedFinishedSignal(uint32_t seq, bool handled); |
Jeff Brown | 072ec96 | 2012-02-07 14:46:57 -0800 | [diff] [blame] | 427 | |
| 428 | static void initializeKeyEvent(KeyEvent* event, const InputMessage* msg); |
| 429 | static void initializeMotionEvent(MotionEvent* event, const InputMessage* msg); |
Jeff Brown | 771526c | 2012-04-27 15:13:25 -0700 | [diff] [blame] | 430 | static void addSample(MotionEvent* event, const InputMessage* msg); |
| 431 | static bool canAddSample(const Batch& batch, const InputMessage* msg); |
| 432 | static ssize_t findSampleNoLaterThan(const Batch& batch, nsecs_t time); |
| 433 | static bool shouldResampleTool(int32_t toolType); |
Jeff Brown | 7174a49 | 2012-05-14 17:00:27 -0700 | [diff] [blame] | 434 | |
| 435 | static bool isTouchResamplingEnabled(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 436 | }; |
| 437 | |
| 438 | } // namespace android |
| 439 | |
Mathias Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 440 | #endif // _ANDROIDFW_INPUT_TRANSPORT_H |