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 | |
| 17 | #ifndef _UI_INPUT_TRANSPORT_H |
| 18 | #define _UI_INPUT_TRANSPORT_H |
| 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 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 30 | #include <ui/Input.h> |
| 31 | #include <utils/Errors.h> |
| 32 | #include <utils/Timers.h> |
| 33 | #include <utils/RefBase.h> |
| 34 | #include <utils/String8.h> |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | /* |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 39 | * Intermediate representation used to send input events and related signals. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 40 | */ |
| 41 | struct InputMessage { |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 42 | enum { |
| 43 | TYPE_KEY = 1, |
| 44 | TYPE_MOTION = 2, |
| 45 | TYPE_FINISHED = 3, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 48 | struct Header { |
| 49 | uint32_t type; |
| 50 | uint32_t padding; // 8 byte alignment for the body that follows |
| 51 | } header; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 52 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 53 | union Body { |
| 54 | struct Key { |
| 55 | nsecs_t eventTime; |
| 56 | int32_t deviceId; |
| 57 | int32_t source; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 58 | int32_t action; |
| 59 | int32_t flags; |
| 60 | int32_t keyCode; |
| 61 | int32_t scanCode; |
| 62 | int32_t metaState; |
| 63 | int32_t repeatCount; |
| 64 | nsecs_t downTime; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 65 | |
| 66 | inline size_t size() const { |
| 67 | return sizeof(Key); |
| 68 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 69 | } key; |
| 70 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 71 | struct Motion { |
| 72 | nsecs_t eventTime; |
| 73 | int32_t deviceId; |
| 74 | int32_t source; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 75 | int32_t action; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 76 | int32_t flags; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 77 | int32_t metaState; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 78 | int32_t buttonState; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 79 | int32_t edgeFlags; |
| 80 | nsecs_t downTime; |
| 81 | float xOffset; |
| 82 | float yOffset; |
| 83 | float xPrecision; |
| 84 | float yPrecision; |
| 85 | size_t pointerCount; |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 86 | struct Pointer { |
| 87 | PointerProperties properties; |
| 88 | PointerCoords coords; |
| 89 | } pointers[MAX_POINTERS]; |
| 90 | |
| 91 | inline size_t size() const { |
| 92 | return sizeof(Motion) - sizeof(Pointer) * MAX_POINTERS |
| 93 | + sizeof(Pointer) * pointerCount; |
| 94 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 95 | } motion; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 96 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 97 | struct Finished { |
| 98 | bool handled; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 99 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 100 | inline size_t size() const { |
| 101 | return sizeof(Finished); |
| 102 | } |
| 103 | } finished; |
| 104 | } body; |
| 105 | |
| 106 | bool isValid(size_t actualSize) const; |
| 107 | size_t size() const; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /* |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 111 | * An input channel consists of a local unix domain socket used to send and receive |
| 112 | * input messages across processes. Each channel has a descriptive name for debugging purposes. |
| 113 | * |
| 114 | * Each endpoint has its own InputChannel object that specifies its file descriptor. |
| 115 | * |
| 116 | * The input channel is closed when all references to it are released. |
| 117 | */ |
| 118 | class InputChannel : public RefBase { |
| 119 | protected: |
| 120 | virtual ~InputChannel(); |
| 121 | |
| 122 | public: |
| 123 | InputChannel(const String8& name, int32_t fd); |
| 124 | |
| 125 | /* Creates a pair of input channels. |
| 126 | * |
| 127 | * Returns OK on success. |
| 128 | */ |
| 129 | static status_t openInputChannelPair(const String8& name, |
| 130 | sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel); |
| 131 | |
| 132 | inline String8 getName() const { return mName; } |
| 133 | inline int32_t getFd() const { return mFd; } |
| 134 | |
| 135 | /* Sends a message to the other endpoint. |
| 136 | * |
| 137 | * If the channel is full then the message is guaranteed not to have been sent at all. |
| 138 | * Try again after the consumer has sent a finished signal indicating that it has |
| 139 | * consumed some of the pending messages from the channel. |
| 140 | * |
| 141 | * Returns OK on success. |
| 142 | * Returns WOULD_BLOCK if the channel is full. |
| 143 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 144 | * Other errors probably indicate that the channel is broken. |
| 145 | */ |
| 146 | status_t sendMessage(const InputMessage* msg); |
| 147 | |
| 148 | /* Receives a message sent by the other endpoint. |
| 149 | * |
| 150 | * If there is no message present, try again after poll() indicates that the fd |
| 151 | * is readable. |
| 152 | * |
| 153 | * Returns OK on success. |
| 154 | * Returns WOULD_BLOCK if there is no message present. |
| 155 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 156 | * Other errors probably indicate that the channel is broken. |
| 157 | */ |
| 158 | status_t receiveMessage(InputMessage* msg); |
| 159 | |
| 160 | private: |
| 161 | String8 mName; |
| 162 | int32_t mFd; |
| 163 | }; |
| 164 | |
| 165 | /* |
| 166 | * Publishes input events to an input channel. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 167 | */ |
| 168 | class InputPublisher { |
| 169 | public: |
| 170 | /* Creates a publisher associated with an input channel. */ |
| 171 | explicit InputPublisher(const sp<InputChannel>& channel); |
| 172 | |
| 173 | /* Destroys the publisher and releases its input channel. */ |
| 174 | ~InputPublisher(); |
| 175 | |
| 176 | /* Gets the underlying input channel. */ |
| 177 | inline sp<InputChannel> getChannel() { return mChannel; } |
| 178 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 179 | /* Publishes a key event to the input channel. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 180 | * |
| 181 | * Returns OK on success. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 182 | * Returns WOULD_BLOCK if the channel is full. |
| 183 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
| 184 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 185 | */ |
| 186 | status_t publishKeyEvent( |
| 187 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 188 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 189 | int32_t action, |
| 190 | int32_t flags, |
| 191 | int32_t keyCode, |
| 192 | int32_t scanCode, |
| 193 | int32_t metaState, |
| 194 | int32_t repeatCount, |
| 195 | nsecs_t downTime, |
| 196 | nsecs_t eventTime); |
| 197 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 198 | /* Publishes a motion event to the input channel. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 199 | * |
| 200 | * Returns OK on success. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 201 | * Returns WOULD_BLOCK if the channel is full. |
| 202 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 203 | * Returns BAD_VALUE if pointerCount is less than 1 or greater than MAX_POINTERS. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 204 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 205 | */ |
| 206 | status_t publishMotionEvent( |
| 207 | int32_t deviceId, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 208 | int32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 209 | int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 210 | int32_t flags, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 211 | int32_t edgeFlags, |
| 212 | int32_t metaState, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 213 | int32_t buttonState, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 214 | float xOffset, |
| 215 | float yOffset, |
| 216 | float xPrecision, |
| 217 | float yPrecision, |
| 218 | nsecs_t downTime, |
| 219 | nsecs_t eventTime, |
| 220 | size_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 221 | const PointerProperties* pointerProperties, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 222 | const PointerCoords* pointerCoords); |
| 223 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 224 | /* Receives the finished signal from the consumer in reply to the original dispatch signal. |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 225 | * Returns whether the consumer handled the message. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 226 | * |
| 227 | * Returns OK on success. |
| 228 | * Returns WOULD_BLOCK if there is no signal present. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 229 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 230 | * Other errors probably indicate that the channel is broken. |
| 231 | */ |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 232 | status_t receiveFinishedSignal(bool* outHandled); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 233 | |
| 234 | private: |
| 235 | sp<InputChannel> mChannel; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | /* |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 239 | * Consumes input events from an input channel. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 240 | */ |
| 241 | class InputConsumer { |
| 242 | public: |
| 243 | /* Creates a consumer associated with an input channel. */ |
| 244 | explicit InputConsumer(const sp<InputChannel>& channel); |
| 245 | |
| 246 | /* Destroys the consumer and releases its input channel. */ |
| 247 | ~InputConsumer(); |
| 248 | |
| 249 | /* Gets the underlying input channel. */ |
| 250 | inline sp<InputChannel> getChannel() { return mChannel; } |
| 251 | |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 252 | /* 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] | 253 | * an InputEvent object created using the specified factory. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 254 | * |
| 255 | * Returns OK on success. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 256 | * Returns WOULD_BLOCK if there is no event present. |
| 257 | * Returns DEAD_OBJECT if the channel's peer has been closed. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 258 | * Returns NO_MEMORY if the event could not be created. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 259 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 260 | */ |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 261 | status_t consume(InputEventFactoryInterface* factory, InputEvent** outEvent); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 262 | |
| 263 | /* Sends a finished signal to the publisher to inform it that the current message is |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 264 | * finished processing and specifies whether the message was handled by the consumer. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 265 | * |
| 266 | * Returns OK on success. |
Jeff Brown | cbee6d6 | 2012-02-03 20:11:27 -0800 | [diff] [blame] | 267 | * Other errors probably indicate that the channel is broken. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 268 | */ |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 269 | status_t sendFinishedSignal(bool handled); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 270 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 271 | private: |
| 272 | sp<InputChannel> mChannel; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | } // namespace android |
| 276 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 277 | #endif // _UI_INPUT_TRANSPORT_H |