2 * Copyright (c) 2010 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 __DEV_VNC_SERVER_HH__
46 #define __DEV_VNC_SERVER_HH__
50 #include "base/vnc/convert.hh"
51 #include "base/circlebuf.hh"
52 #include "base/pollevent.hh"
53 #include "base/socket.hh"
54 #include "cpu/intr_control.hh"
55 #include "params/VncServer.hh"
56 #include "sim/sim_object.hh"
59 * A device that expects to receive input from the vnc server should derrive
60 * (through mulitple inheritence if necessary from VncKeyboard or VncMouse
61 * and call setKeyboard() or setMouse() respectively on the vnc server.
67 * Called when the vnc server receives a key press event from the
69 * @param key the key passed is an x11 keysym
70 * @param down is the key now down or up?
72 virtual void keyPress(uint32_t key, bool down) = 0;
79 * called whenever the mouse moves or it's button state changes
80 * buttons is a simple mask with each button (0-8) corresponding to
81 * a bit position in the byte with 1 being down and 0 being up
82 * @param x the x position of the mouse
83 * @param y the y position of the mouse
84 * @param buttos the button state as described above
86 virtual void mouseAt(uint16_t x, uint16_t y, uint8_t buttons) = 0;
89 class VncServer : public SimObject
94 * \defgroup VncConstants A set of constants and structs from the VNC spec
97 /** Authentication modes */
98 const static uint32_t AuthInvalid = 0;
99 const static uint32_t AuthNone = 1;
101 /** Error conditions */
102 const static uint32_t VncOK = 0;
104 /** Client -> Server message IDs */
105 enum ClientMessages {
106 ClientSetPixelFormat = 0,
107 ClientSetEncodings = 2,
108 ClientFrameBufferUpdate = 3,
110 ClientPointerEvent = 5,
114 /** Server -> Client message IDs */
115 enum ServerMessages {
116 ServerFrameBufferUpdate = 0,
117 ServerSetColorMapEntries = 1,
122 /** Encoding types */
125 EncodingCopyRect = 1,
127 EncodingDesktopSize = -223
130 /** keyboard/mouse support */
132 MouseLeftButton = 0x1,
133 MouseRightButton = 0x2,
134 MouseMiddleButton = 0x4
137 const char* vncVersion() const
139 return "RFB 003.008\n";
142 enum ConnectionState {
143 WaitForProtocolVersion,
144 WaitForSecurityResponse,
164 struct ServerInitMsg {
169 char name[2]; // just to put M5 in here
172 struct PixelFormatMessage {
178 struct PixelEncodingsMessage {
181 uint16_t num_encodings;
184 struct FrameBufferUpdateReq {
193 struct KeyEventMessage {
200 struct PointerEventMessage {
207 struct ClientCutTextMessage {
213 struct FrameBufferUpdate {
219 struct FrameBufferRect {
227 struct ServerCutText {
236 /** ListenEvent to accept a vnc client connection */
237 class ListenEvent: public PollEvent
240 VncServer *vncserver;
243 ListenEvent(VncServer *vs, int fd, int e);
244 void process(int revent);
247 friend class ListenEvent;
248 ListenEvent *listenEvent;
250 /** DataEvent to read data from vnc */
251 class DataEvent: public PollEvent
254 VncServer *vncserver;
257 DataEvent(VncServer *vs, int fd, int e);
258 void process(int revent);
261 friend class DataEvent;
262 DataEvent *dataEvent;
265 int dataFd; // data stream file describer
267 ListenSocket listener;
269 void listen(int port);
275 typedef VncServerParams Params;
276 VncServer(const Params *p);
282 /** The rfb prototol state the connection is in */
283 ConnectionState curState;
285 /** the width of the frame buffer we are sending to the client */
286 uint16_t _videoWidth;
288 /** the height of the frame buffer we are sending to the client */
289 uint16_t _videoHeight;
291 /** pointer to the actual data that is stored in the frame buffer device */
294 /** The device to notify when we get key events */
295 VncKeyboard *keyboard;
297 /** The device to notify when we get mouse events */
300 /** An update needs to be sent to the client. Without doing this the
301 * client will constantly request data that is pointless */
304 /** The one and only pixel format we support */
305 PixelFormat pixelFormat;
307 /** If the vnc client supports receiving raw data. It always should */
310 /** If the vnc client supports the desktop resize command */
311 bool supportsResizeEnc;
313 /** The mode of data we're getting frame buffer in */
314 VideoConvert::Mode videoMode;
316 /** The video converter that transforms data for us */
321 * vnc client Interface
324 /** Send an error message to the client
325 * @param error_msg text to send describing the error
327 void sendError(const char* error_msg);
329 /** Read some data from the client
330 * @param buf the data to read
331 * @param len the amount of data to read
332 * @return length read
334 size_t read(uint8_t *buf, size_t len);
336 /** Read len -1 bytes from the client into the buffer provided + 1
337 * assert that we read enough bytes. This function exists to handle
338 * reading all of the protocol structs above when we've already read
339 * the first byte which describes which one we're reading
340 * @param buf the address of the buffer to add one to and read data into
341 * @param len the amount of data + 1 to read
342 * @return length read
344 size_t read1(uint8_t *buf, size_t len);
347 /** Templated version of the read function above to
348 * read simple data to the client
349 * @param val data to recv from the client
351 template <typename T> size_t read(T* val);
354 /** Write a buffer to the client.
355 * @param buf buffer to send
356 * @param len length of the buffer
357 * @return number of bytes sent
359 size_t write(const uint8_t *buf, size_t len);
361 /** Templated version of the write function above to
362 * write simple data to the client
363 * @param val data to send to the client
365 template <typename T> size_t write(T* val);
367 /** Send a string to the client
368 * @param str string to transmit
370 size_t write(const char* str);
372 /** Check the client's protocol verion for compatibility and send
373 * the security types we support
375 void checkProtocolVersion();
377 /** Check that the security exchange was successful
379 void checkSecurity();
381 /** Send client our idea about what the frame buffer looks like */
382 void sendServerInit();
384 /** Send an error message to the client when something goes wrong
385 * @param error_msg error to send
387 void sendError(std::string error_msg);
389 /** Send a updated frame buffer to the client.
390 * @todo this doesn't do anything smart and just sends the entire image
392 void sendFrameBufferUpdate();
394 /** Receive pixel foramt message from client and process it. */
395 void setPixelFormat();
397 /** Receive encodings message from client and process it. */
400 /** Receive message from client asking for updated frame buffer */
401 void requestFbUpdate();
403 /** Receive message from client providing new keyboard input */
404 void recvKeyboardInput();
406 /** Recv message from client providing new mouse movement or button click */
407 void recvPointerInput();
409 /** Receive message from client that there is text in it's paste buffer.
410 * This is a no-op at the moment, but perhaps we would want to be able to
411 * paste it at some point.
415 /** Tell the client that the frame buffer resized. This happens when the
416 * simulated system changes video modes (E.g. X11 starts).
418 void sendFrameBufferResized();
421 /** Set the address of the frame buffer we are going to show.
422 * To avoid copying, just have the display controller
423 * tell us where the data is instead of constanly copying it around
424 * @param rfb frame buffer that we're going to use
427 setFramebufferAddr(uint8_t* rfb)
432 /** Set up the device that would like to receive notifications when keys are
433 * pressed in the vnc client keyboard
434 * @param _keyboard an object that derrives from VncKeyboard
436 void setKeyboard(VncKeyboard *_keyboard) { keyboard = _keyboard; }
438 /** Setup the device that would like to receive notifications when mouse
439 * movements or button presses are received from the vnc client.
440 * @param _mouse an object that derrives from VncMouse
442 void setMouse(VncMouse *_mouse) { mouse = _mouse; }
444 /** The frame buffer uses this call to notify the vnc server that
445 * the frame buffer has been updated and a new image needs to be sent to the
452 sendFrameBufferUpdate();
455 /** What is the width of the screen we're displaying.
456 * This is used for pointer/tablet devices that need to know to calculate
457 * the correct value to send to the device driver.
458 * @return the width of the simulated screen
460 uint16_t videoWidth() { return _videoWidth; }
462 /** What is the height of the screen we're displaying.
463 * This is used for pointer/tablet devices that need to know to calculate
464 * the correct value to send to the device driver.
465 * @return the height of the simulated screen
467 uint16_t videoHeight() { return _videoHeight; }
469 /** Set the mode of the data the frame buffer will be sending us
470 * @param mode the mode
472 void setFrameBufferParams(VideoConvert::Mode mode, int width, int height);