blob: 31ec7019a624dee97666259ab80d5c36ec7c03e7 [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
17#ifndef _UI_INPUT_TRANSPORT_H
18#define _UI_INPUT_TRANSPORT_H
19
20/**
21 * Native input transport.
22 *
23 * Uses anonymous shared memory as a whiteboard for sending input events from an
24 * InputPublisher to an InputConsumer and ensuring appropriate synchronization.
25 * One interesting feature is that published events can be updated in place as long as they
26 * have not yet been consumed.
27 *
28 * The InputPublisher and InputConsumer only take care of transferring event data
29 * over an InputChannel and sending synchronization signals. The InputDispatcher and InputQueue
30 * build on these abstractions to add multiplexing and queueing.
31 */
32
33#include <semaphore.h>
34#include <ui/Input.h>
35#include <utils/Errors.h>
Dianne Hackborn68267412010-07-02 18:52:01 -070036#include <utils/PollLoop.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070037#include <utils/Timers.h>
38#include <utils/RefBase.h>
39#include <utils/String8.h>
40
41namespace android {
42
43/*
44 * An input channel consists of a shared memory buffer and a pair of pipes
45 * used to send input messages from an InputPublisher to an InputConsumer
46 * across processes. Each channel has a descriptive name for debugging purposes.
47 *
48 * Each endpoint has its own InputChannel object that specifies its own file descriptors.
49 *
50 * The input channel is closed when all references to it are released.
51 */
52class InputChannel : public RefBase {
53protected:
54 virtual ~InputChannel();
55
56public:
57 InputChannel(const String8& name, int32_t ashmemFd, int32_t receivePipeFd,
58 int32_t sendPipeFd);
59
60 /* Creates a pair of input channels and their underlying shared memory buffers
61 * and pipes.
62 *
63 * Returns OK on success.
64 */
65 static status_t openInputChannelPair(const String8& name,
Jeff Brown5c225b12010-06-16 01:53:36 -070066 sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070067
68 inline String8 getName() const { return mName; }
69 inline int32_t getAshmemFd() const { return mAshmemFd; }
70 inline int32_t getReceivePipeFd() const { return mReceivePipeFd; }
71 inline int32_t getSendPipeFd() const { return mSendPipeFd; }
72
73 /* Sends a signal to the other endpoint.
74 *
75 * Returns OK on success.
Jeff Brown5c225b12010-06-16 01:53:36 -070076 * Returns DEAD_OBJECT if the channel's peer has been closed.
77 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070078 */
79 status_t sendSignal(char signal);
80
81 /* Receives a signal send by the other endpoint.
82 * (Should only call this after poll() indicates that the receivePipeFd has available input.)
83 *
84 * Returns OK on success.
85 * Returns WOULD_BLOCK if there is no signal present.
Jeff Brown5c225b12010-06-16 01:53:36 -070086 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070087 * Other errors probably indicate that the channel is broken.
88 */
89 status_t receiveSignal(char* outSignal);
90
91private:
92 String8 mName;
93 int32_t mAshmemFd;
94 int32_t mReceivePipeFd;
95 int32_t mSendPipeFd;
96};
97
98/*
99 * Private intermediate representation of input events as messages written into an
100 * ashmem buffer.
101 */
102struct InputMessage {
103 /* Semaphore count is set to 1 when the message is published.
104 * It becomes 0 transiently while the publisher updates the message.
105 * It becomes 0 permanently when the consumer consumes the message.
106 */
107 sem_t semaphore;
108
109 /* Initialized to false by the publisher.
110 * Set to true by the consumer when it consumes the message.
111 */
112 bool consumed;
113
114 int32_t type;
115
116 struct SampleData {
117 nsecs_t eventTime;
118 PointerCoords coords[0]; // variable length
119 };
120
121 int32_t deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700122 int32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700123
124 union {
125 struct {
126 int32_t action;
127 int32_t flags;
128 int32_t keyCode;
129 int32_t scanCode;
130 int32_t metaState;
131 int32_t repeatCount;
132 nsecs_t downTime;
133 nsecs_t eventTime;
134 } key;
135
136 struct {
137 int32_t action;
138 int32_t metaState;
139 int32_t edgeFlags;
140 nsecs_t downTime;
141 float xOffset;
142 float yOffset;
143 float xPrecision;
144 float yPrecision;
145 size_t pointerCount;
146 int32_t pointerIds[MAX_POINTERS];
147 size_t sampleCount;
148 SampleData sampleData[0]; // variable length
149 } motion;
150 };
151
152 /* Gets the number of bytes to add to step to the next SampleData object in a motion
153 * event message for a given number of pointers.
154 */
155 static inline size_t sampleDataStride(size_t pointerCount) {
156 return sizeof(InputMessage::SampleData) + pointerCount * sizeof(PointerCoords);
157 }
158
159 /* Adds the SampleData stride to the given pointer. */
160 static inline SampleData* sampleDataPtrIncrement(SampleData* ptr, size_t stride) {
161 return reinterpret_cast<InputMessage::SampleData*>(reinterpret_cast<char*>(ptr) + stride);
162 }
163};
164
165/*
166 * Publishes input events to an anonymous shared memory buffer.
167 * Uses atomic operations to coordinate shared access with a single concurrent consumer.
168 */
169class InputPublisher {
170public:
171 /* Creates a publisher associated with an input channel. */
172 explicit InputPublisher(const sp<InputChannel>& channel);
173
174 /* Destroys the publisher and releases its input channel. */
175 ~InputPublisher();
176
177 /* Gets the underlying input channel. */
178 inline sp<InputChannel> getChannel() { return mChannel; }
179
180 /* Prepares the publisher for use. Must be called before it is used.
181 * Returns OK on success.
182 *
183 * This method implicitly calls reset(). */
184 status_t initialize();
185
186 /* Resets the publisher to its initial state and unpins its ashmem buffer.
187 * Returns OK on success.
188 *
189 * Should be called after an event has been consumed to release resources used by the
190 * publisher until the next event is ready to be published.
191 */
192 status_t reset();
193
194 /* Publishes a key event to the ashmem buffer.
195 *
196 * Returns OK on success.
197 * Returns INVALID_OPERATION if the publisher has not been reset.
198 */
199 status_t publishKeyEvent(
200 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700201 int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700202 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
211 /* Publishes a motion event to the ashmem buffer.
212 *
213 * Returns OK on success.
214 * Returns INVALID_OPERATION if the publisher has not been reset.
215 * Returns BAD_VALUE if pointerCount is less than 1 or greater than MAX_POINTERS.
216 */
217 status_t publishMotionEvent(
218 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700219 int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700220 int32_t action,
221 int32_t edgeFlags,
222 int32_t metaState,
223 float xOffset,
224 float yOffset,
225 float xPrecision,
226 float yPrecision,
227 nsecs_t downTime,
228 nsecs_t eventTime,
229 size_t pointerCount,
230 const int32_t* pointerIds,
231 const PointerCoords* pointerCoords);
232
233 /* Appends a motion sample to a motion event unless already consumed.
234 *
235 * Returns OK on success.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700236 * Returns INVALID_OPERATION if the current event is not a AMOTION_EVENT_ACTION_MOVE event.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700237 * Returns FAILED_TRANSACTION if the current event has already been consumed.
238 * Returns NO_MEMORY if the buffer is full and no additional samples can be added.
239 */
240 status_t appendMotionSample(
241 nsecs_t eventTime,
242 const PointerCoords* pointerCoords);
243
244 /* Sends a dispatch signal to the consumer to inform it that a new message is available.
245 *
246 * Returns OK on success.
247 * Errors probably indicate that the channel is broken.
248 */
249 status_t sendDispatchSignal();
250
251 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
252 *
253 * Returns OK on success.
254 * Returns WOULD_BLOCK if there is no signal present.
255 * Other errors probably indicate that the channel is broken.
256 */
257 status_t receiveFinishedSignal();
258
259private:
260 sp<InputChannel> mChannel;
261
262 size_t mAshmemSize;
263 InputMessage* mSharedMessage;
264 bool mPinned;
265 bool mSemaphoreInitialized;
266 bool mWasDispatched;
267
268 size_t mMotionEventPointerCount;
269 InputMessage::SampleData* mMotionEventSampleDataTail;
270 size_t mMotionEventSampleDataStride;
271
272 status_t publishInputEvent(
273 int32_t type,
274 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700275 int32_t source);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700276};
277
278/*
279 * Consumes input events from an anonymous shared memory buffer.
280 * Uses atomic operations to coordinate shared access with a single concurrent publisher.
281 */
282class InputConsumer {
283public:
284 /* Creates a consumer associated with an input channel. */
285 explicit InputConsumer(const sp<InputChannel>& channel);
286
287 /* Destroys the consumer and releases its input channel. */
288 ~InputConsumer();
289
290 /* Gets the underlying input channel. */
291 inline sp<InputChannel> getChannel() { return mChannel; }
292
293 /* Prepares the consumer for use. Must be called before it is used. */
294 status_t initialize();
295
296 /* Consumes the input event in the buffer and copies its contents into
297 * an InputEvent object created using the specified factory.
298 * This operation will block if the publisher is updating the event.
299 *
300 * Returns OK on success.
301 * Returns INVALID_OPERATION if there is no currently published event.
302 * Returns NO_MEMORY if the event could not be created.
303 */
Jeff Brown5c225b12010-06-16 01:53:36 -0700304 status_t consume(InputEventFactoryInterface* factory, InputEvent** outEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700305
306 /* Sends a finished signal to the publisher to inform it that the current message is
307 * finished processing.
308 *
309 * Returns OK on success.
310 * Errors probably indicate that the channel is broken.
311 */
312 status_t sendFinishedSignal();
313
314 /* Receives the dispatched signal from the publisher.
315 *
316 * Returns OK on success.
317 * Returns WOULD_BLOCK if there is no signal present.
318 * Other errors probably indicate that the channel is broken.
319 */
320 status_t receiveDispatchSignal();
321
322private:
323 sp<InputChannel> mChannel;
324
325 size_t mAshmemSize;
326 InputMessage* mSharedMessage;
327
328 void populateKeyEvent(KeyEvent* keyEvent) const;
329 void populateMotionEvent(MotionEvent* motionEvent) const;
330};
331
332} // namespace android
333
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700334#endif // _UI_INPUT_TRANSPORT_H