2 * Copyright (c) 2010, 2015 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 * Declaration of a VNC server
45 #ifndef __BASE_VNC_VNC_SERVER_HH__
46 #define __BASE_VNC_VNC_SERVER_HH__
50 #include "base/vnc/vncinput.hh"
51 #include "base/bitmap.hh"
52 #include "base/circlebuf.hh"
53 #include "base/pollevent.hh"
54 #include "base/socket.hh"
55 #include "params/VncServer.hh"
56 #include "sim/sim_object.hh"
59 * Declaration of a VNC server
62 class VncServer : public VncInput
67 * \defgroup VncConstants A set of constants and structs from the VNC spec
70 /** Authentication modes */
71 const static uint32_t AuthInvalid = 0;
72 const static uint32_t AuthNone = 1;
74 /** Error conditions */
75 const static uint32_t VncOK = 0;
77 /** Server -> Client message IDs */
79 ServerFrameBufferUpdate = 0,
80 ServerSetColorMapEntries = 1,
90 EncodingDesktopSize = -223
93 /** keyboard/mouse support */
95 MouseLeftButton = 0x1,
96 MouseRightButton = 0x2,
97 MouseMiddleButton = 0x4
100 const char* vncVersion() const
102 return "RFB 003.008\n";
105 enum ConnectionState {
106 WaitForProtocolVersion,
107 WaitForSecurityResponse,
113 struct ServerInitMsg {
118 char name[2]; // just to put M5 in here
121 struct FrameBufferUpdate {
127 struct FrameBufferRect {
135 struct ServerCutText {
144 /** ListenEvent to accept a vnc client connection */
145 class ListenEvent: public PollEvent
148 VncServer *vncserver;
151 ListenEvent(VncServer *vs, int fd, int e);
152 void process(int revent);
155 friend class ListenEvent;
156 ListenEvent *listenEvent;
158 /** DataEvent to read data from vnc */
159 class DataEvent: public PollEvent
162 VncServer *vncserver;
165 DataEvent(VncServer *vs, int fd, int e);
166 void process(int revent);
169 friend class DataEvent;
170 DataEvent *dataEvent;
173 int dataFd; // data stream file describer
175 ListenSocket listener;
177 void listen(int port);
183 typedef VncServerParams Params;
184 VncServer(const Params *p);
190 /** The rfb prototol state the connection is in */
191 ConnectionState curState;
193 /** An update needs to be sent to the client. Without doing this the
194 * client will constantly request data that is pointless */
197 /** The one and only pixel format we support */
198 PixelFormat pixelFormat;
200 /** If the vnc client supports receiving raw data. It always should */
203 /** If the vnc client supports the desktop resize command */
204 bool supportsResizeEnc;
208 * vnc client Interface
211 /** Send an error message to the client
212 * @param error_msg text to send describing the error
214 void sendError(const char* error_msg);
216 /** Read some data from the client
217 * @param buf the data to read
218 * @param len the amount of data to read
219 * @return length read
221 size_t read(uint8_t *buf, size_t len);
223 /** Read len -1 bytes from the client into the buffer provided + 1
224 * assert that we read enough bytes. This function exists to handle
225 * reading all of the protocol structs above when we've already read
226 * the first byte which describes which one we're reading
227 * @param buf the address of the buffer to add one to and read data into
228 * @param len the amount of data + 1 to read
229 * @return length read
231 size_t read1(uint8_t *buf, size_t len);
234 /** Templated version of the read function above to
235 * read simple data to the client
236 * @param val data to recv from the client
238 template <typename T> size_t read(T* val);
241 /** Write a buffer to the client.
242 * @param buf buffer to send
243 * @param len length of the buffer
244 * @return number of bytes sent
246 size_t write(const uint8_t *buf, size_t len);
248 /** Templated version of the write function above to
249 * write simple data to the client
250 * @param val data to send to the client
252 template <typename T> size_t write(T* val);
254 /** Send a string to the client
255 * @param str string to transmit
257 size_t write(const char* str);
259 /** Check the client's protocol verion for compatibility and send
260 * the security types we support
262 void checkProtocolVersion();
264 /** Check that the security exchange was successful
266 void checkSecurity();
268 /** Send client our idea about what the frame buffer looks like */
269 void sendServerInit();
271 /** Send an error message to the client when something goes wrong
272 * @param error_msg error to send
274 void sendError(std::string error_msg);
276 /** Send a updated frame buffer to the client.
277 * @todo this doesn't do anything smart and just sends the entire image
279 void sendFrameBufferUpdate();
281 /** Receive pixel foramt message from client and process it. */
282 void setPixelFormat();
284 /** Receive encodings message from client and process it. */
287 /** Receive message from client asking for updated frame buffer */
288 void requestFbUpdate();
290 /** Receive message from client providing new keyboard input */
291 void recvKeyboardInput();
293 /** Recv message from client providing new mouse movement or button click */
294 void recvPointerInput();
296 /** Receive message from client that there is text in it's paste buffer.
297 * This is a no-op at the moment, but perhaps we would want to be able to
298 * paste it at some point.
302 /** Tell the client that the frame buffer resized. This happens when the
303 * simulated system changes video modes (E.g. X11 starts).
305 void sendFrameBufferResized();
307 static const PixelConverter pixelConverter;
310 void setDirty() M5_ATTR_OVERRIDE;
311 void frameBufferResized() M5_ATTR_OVERRIDE;