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