blob: 420912ed724da32afbac83be183c72108485e3fa [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 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 ANDROID_RFB_SERVER_H
18#define ANDROID_RFB_SERVER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22#include <sys/uio.h>
23#include <unistd.h>
24#include <arpa/inet.h>
25
26#include <utils/Errors.h>
27#include <utils/threads.h>
28#include <ui/Region.h>
29#include <ui/PixelFormat.h>
30
31#include <pixelflinger/pixelflinger.h>
32
33#include "Barrier.h"
34
35namespace android {
36
37class SurfaceFlinger;
38
39class RFBServer : public Thread
40{
41public:
42 RFBServer(uint32_t w, uint32_t h, android::PixelFormat format);
43 virtual ~RFBServer();
44
45 void frameBufferUpdated(const GGLSurface& front, const Region& reg);
46 bool isConnected() const;
47
48private:
49 typedef uint8_t card8;
50 typedef uint16_t card16;
51 typedef uint32_t card32;
52
53 struct Message {
54 Message(size_t size);
55 virtual ~Message();
56 void* payload(int offset=0) {
57 return static_cast<char*>(mPayload)+offset;
58 }
59 void const * payload(int offset=0) const {
60 return static_cast<char const *>(mPayload)+offset;
61 }
62 size_t size() const { return mSize; }
63 protected:
64 Message(void* payload, size_t size);
65 status_t resize(size_t size);
66 private:
67 void* mPayload;
68 size_t mSize;
69 size_t mAllocatedSize;
70 };
71
72 struct ProtocolVersion : public Message {
73 ProtocolVersion(uint8_t major, uint8_t minor)
74 : Message(&messageData, 12) {
75 char* p = static_cast<char*>(payload());
76 snprintf(p, 13, "RFB %03u.%03u%c", major, minor, 0xA);
77 }
78 status_t decode(int& maj, int& min) {
79 char* p = static_cast<char*>(payload());
80 int n = sscanf(p, "RFB %03u.%03u", &maj, &min);
81 return (n == 2) ? NO_ERROR : NOT_ENOUGH_DATA;
82 }
83 private:
84 char messageData[12+1];
85 };
86
87 struct Authentication : public Message {
88 enum { Failed=0, None=1, Vnc=2 };
89 Authentication(int auth) : Message(&messageData, 4) {
90 *static_cast<card32*>(payload()) = htonl(auth);
91 }
92 private:
93 card32 messageData;
94 };
95
96 struct ClientInitialization : public Message {
97 ClientInitialization() : Message(&messageData, 1) { }
98 int sharedFlags() {
99 return messageData;
100 }
101 private:
102 card8 messageData;
103 };
104
105 struct PixelFormat {
106 card8 bitsPerPixel;
107 card8 depth;
108 card8 bigEndianFlag;
109 card8 trueColorFlag;
110 card16 redMax;
111 card16 greenMax;
112 card16 blueMax;
113 card8 redShift;
114 card8 greenShift;
115 card8 blueShift;
116 uint8_t padding[3];
117 } __attribute__((packed));
118
119 struct ServerInitialization : public Message {
120 ServerInitialization(char const * name)
121 : Message(sizeof(Payload) + strlen(name))
122 {
123 const size_t nameLength = size() - sizeof(Payload);
124 message().nameLength = htonl(nameLength);
125 memcpy((char*)message().nameString, name,nameLength);
126 }
127 struct Payload {
128 card16 framebufferWidth;
129 card16 framebufferHeight;
130 PixelFormat serverPixelFormat;
131 card32 nameLength;
132 card8 nameString[0];
133 } __attribute__((packed));
134 Payload& message() {
135 return *static_cast<Payload*>(payload());
136 }
137 };
138
139 // client messages...
140
141 struct SetPixelFormat {
142 card8 type;
143 uint8_t padding[3];
144 PixelFormat pixelFormat;
145 } __attribute__((packed));
146
147 struct SetEncodings {
148 enum { Raw=0, CoR=1, RRE=2, CoRRE=4, Hextile=5 };
149 card8 type;
150 uint8_t padding;
151 card16 numberOfEncodings;
152 card32 encodings[0];
153 } __attribute__((packed));
154
155 struct FrameBufferUpdateRequest {
156 card8 type;
157 card8 incremental;
158 card16 x;
159 card16 y;
160 card16 width;
161 card16 height;
162 } __attribute__((packed));
163
164 struct KeyEvent {
165 card8 type;
166 card8 downFlag;
167 uint8_t padding[2];
168 card32 key;
169 } __attribute__((packed));
170
171 struct PointerEvent {
172 card8 type;
173 card8 buttonMask;
174 card16 x;
175 card16 y;
176 } __attribute__((packed));
177
178 struct ClientCutText {
179 card8 type;
180 uint8_t padding[3];
181 card32 length;
182 card8 text[0];
183 } __attribute__((packed));
184
185 union ClientMessages {
186 card8 type;
187 SetPixelFormat setPixelFormat;
188 SetEncodings setEncodings;
189 FrameBufferUpdateRequest frameBufferUpdateRequest;
190 KeyEvent keyEvent;
191 PointerEvent pointerEvent;
192 ClientCutText clientCutText;
193 };
194
195 struct Rectangle {
196 card16 x;
197 card16 y;
198 card16 w;
199 card16 h;
200 card32 encoding;
201 } __attribute__((packed));
202
203 struct FrameBufferUpdate {
204 card8 type;
205 uint8_t padding;
206 card16 numberOfRectangles;
207 Rectangle rectangles[0];
208 } __attribute__((packed));
209
210 enum {
211 SET_PIXEL_FORMAT = 0,
212 FIX_COLOUR_MAP_ENTRIES = 1,
213 SET_ENCODINGS = 2,
214 FRAME_BUFFER_UPDATE_REQ = 3,
215 KEY_EVENT = 4,
216 POINTER_EVENT = 5,
217 CLIENT_CUT_TEXT = 6,
218 };
219
220 struct ClientMessage : public Message {
221 ClientMessage()
222 : Message(&messageData, sizeof(messageData)) {
223 }
224 const ClientMessages& messages() const {
225 return *static_cast<ClientMessages const *>(payload());
226 }
227 const int type() const {
228 return messages().type;
229 }
230 status_t resize(size_t size) {
231 return Message::resize(size);
232 }
233
234 ClientMessages messageData;
235 };
236
237
238 class ServerThread : public Thread
239 {
240 friend class RFBServer;
241 public:
242 ServerThread(const sp<RFBServer>& receiver);
243 virtual ~ServerThread();
244 void wake();
245 void exitAndWait();
246 private:
247 virtual bool threadLoop();
248 virtual status_t readyToRun();
249 virtual void onFirstRef();
250 wp<RFBServer> mReceiver;
251 bool (RFBServer::*mAction)();
252 Barrier mUpdateBarrier;
253 };
254
255 class EventInjector {
256 public:
257 enum { UP=0, DOWN=1 };
258 EventInjector();
259 ~EventInjector();
260 void injectKey(uint16_t code, uint16_t value);
261 private:
262 struct input_event {
263 struct timeval time;
264 uint16_t type;
265 uint16_t code;
266 uint32_t value;
267 };
268 int mFD;
269 };
270
271 void handshake(uint8_t major, uint8_t minor, uint32_t auth);
272 void waitForClientMessage(ClientMessage& msg);
273 void handleClientMessage(const ClientMessage& msg);
274 void handleSetPixelFormat(const SetPixelFormat& msg);
275 void handleSetEncodings(const SetEncodings& msg);
276 void handleFrameBufferUpdateReq(const FrameBufferUpdateRequest& msg);
277 void handleKeyEvent(const KeyEvent& msg);
278 void sendFrameBufferUpdates();
279
280 bool validatePixelFormat(const PixelFormat& pf);
281 bool alive() const;
282 bool write(const Message& msg);
283 bool read(Message& msg);
284
285 bool write(const void* buffer, int size);
286 bool read(void* buffer, int size);
287
288 virtual bool threadLoop();
289 virtual status_t readyToRun();
290 virtual void onFirstRef();
291
292 sp<ServerThread> mRobinThread;
293
294 int mFD;
295 int mStatus;
296 iovec* mIoVec;
297
298 EventInjector mEventInjector;
299
300 Mutex mRegionLock;
301 // This is the region requested by the client since the last
302 // time we updated it
303 Region mClientRegionRequest;
304 // This is the region of the screen that needs to be sent to the
305 // client since the last time we updated it.
306 // Typically this is the dirty region, but not necessarily, for
307 // instance if the client asked for a non incremental update.
308 Region mDirtyRegion;
309
310 GGLSurface mFrameBuffer;
311 GGLSurface mFrontBuffer;
312};
313
314}; // namespace android
315
316#endif // ANDROID_RFB_SERVER_H