blob: f3a39c31a31eb400626d1f46a8f43d1fd373042b [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 *
Jeff Browncbee6d62012-02-03 20:11:27 -080023 * The InputChannel provides a mechanism for exchanging InputMessage structures across processes.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070024 *
Jeff Browncbee6d62012-02-03 20:11:27 -080025 * 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 Brown46b9ac0a2010-04-22 18:58:52 -070028 */
29
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070030#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
36namespace android {
37
38/*
Jeff Browncbee6d62012-02-03 20:11:27 -080039 * Intermediate representation used to send input events and related signals.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070040 */
41struct InputMessage {
Jeff Browncbee6d62012-02-03 20:11:27 -080042 enum {
43 TYPE_KEY = 1,
44 TYPE_MOTION = 2,
45 TYPE_FINISHED = 3,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070046 };
47
Jeff Browncbee6d62012-02-03 20:11:27 -080048 struct Header {
49 uint32_t type;
50 uint32_t padding; // 8 byte alignment for the body that follows
51 } header;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070052
Jeff Browncbee6d62012-02-03 20:11:27 -080053 union Body {
54 struct Key {
55 nsecs_t eventTime;
56 int32_t deviceId;
57 int32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070058 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 Browncbee6d62012-02-03 20:11:27 -080065
66 inline size_t size() const {
67 return sizeof(Key);
68 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070069 } key;
70
Jeff Browncbee6d62012-02-03 20:11:27 -080071 struct Motion {
72 nsecs_t eventTime;
73 int32_t deviceId;
74 int32_t source;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070075 int32_t action;
Jeff Brown85a31762010-09-01 17:01:00 -070076 int32_t flags;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070077 int32_t metaState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070078 int32_t buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070079 int32_t edgeFlags;
80 nsecs_t downTime;
81 float xOffset;
82 float yOffset;
83 float xPrecision;
84 float yPrecision;
85 size_t pointerCount;
Jeff Browncbee6d62012-02-03 20:11:27 -080086 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 Brown46b9ac0a2010-04-22 18:58:52 -070095 } motion;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070096
Jeff Browncbee6d62012-02-03 20:11:27 -080097 struct Finished {
98 bool handled;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070099
Jeff Browncbee6d62012-02-03 20:11:27 -0800100 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 Brown46b9ac0a2010-04-22 18:58:52 -0700108};
109
110/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800111 * 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 */
118class InputChannel : public RefBase {
119protected:
120 virtual ~InputChannel();
121
122public:
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
160private:
161 String8 mName;
162 int32_t mFd;
163};
164
165/*
166 * Publishes input events to an input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700167 */
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
Jeff Browncbee6d62012-02-03 20:11:27 -0800179 /* Publishes a key event to the input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700180 *
181 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800182 * 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 Brown46b9ac0a2010-04-22 18:58:52 -0700185 */
186 status_t publishKeyEvent(
187 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700188 int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700189 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 Browncbee6d62012-02-03 20:11:27 -0800198 /* Publishes a motion event to the input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700199 *
200 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800201 * Returns WOULD_BLOCK if the channel is full.
202 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700203 * Returns BAD_VALUE if pointerCount is less than 1 or greater than MAX_POINTERS.
Jeff Browncbee6d62012-02-03 20:11:27 -0800204 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700205 */
206 status_t publishMotionEvent(
207 int32_t deviceId,
Jeff Brownc5ed5912010-07-14 18:48:53 -0700208 int32_t source,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700209 int32_t action,
Jeff Brown85a31762010-09-01 17:01:00 -0700210 int32_t flags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700211 int32_t edgeFlags,
212 int32_t metaState,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700213 int32_t buttonState,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700214 float xOffset,
215 float yOffset,
216 float xPrecision,
217 float yPrecision,
218 nsecs_t downTime,
219 nsecs_t eventTime,
220 size_t pointerCount,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700221 const PointerProperties* pointerProperties,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700222 const PointerCoords* pointerCoords);
223
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700224 /* Receives the finished signal from the consumer in reply to the original dispatch signal.
Jeff Brown3915bb82010-11-05 15:02:16 -0700225 * Returns whether the consumer handled the message.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700226 *
227 * Returns OK on success.
228 * Returns WOULD_BLOCK if there is no signal present.
Jeff Browncbee6d62012-02-03 20:11:27 -0800229 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700230 * Other errors probably indicate that the channel is broken.
231 */
Jeff Brown49ed71d2010-12-06 17:13:33 -0800232 status_t receiveFinishedSignal(bool* outHandled);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700233
234private:
235 sp<InputChannel> mChannel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700236};
237
238/*
Jeff Browncbee6d62012-02-03 20:11:27 -0800239 * Consumes input events from an input channel.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700240 */
241class InputConsumer {
242public:
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 Browncbee6d62012-02-03 20:11:27 -0800252 /* Consumes an input event from the input channel and copies its contents into
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700253 * an InputEvent object created using the specified factory.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700254 *
255 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800256 * Returns WOULD_BLOCK if there is no event present.
257 * Returns DEAD_OBJECT if the channel's peer has been closed.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700258 * Returns NO_MEMORY if the event could not be created.
Jeff Browncbee6d62012-02-03 20:11:27 -0800259 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700260 */
Jeff Brown5c225b12010-06-16 01:53:36 -0700261 status_t consume(InputEventFactoryInterface* factory, InputEvent** outEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700262
263 /* Sends a finished signal to the publisher to inform it that the current message is
Jeff Brown3915bb82010-11-05 15:02:16 -0700264 * finished processing and specifies whether the message was handled by the consumer.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700265 *
266 * Returns OK on success.
Jeff Browncbee6d62012-02-03 20:11:27 -0800267 * Other errors probably indicate that the channel is broken.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700268 */
Jeff Brown3915bb82010-11-05 15:02:16 -0700269 status_t sendFinishedSignal(bool handled);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700270
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700271private:
272 sp<InputChannel> mChannel;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700273};
274
275} // namespace android
276
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700277#endif // _UI_INPUT_TRANSPORT_H