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