blob: ebe4ad2b287e3b7d1e789c70541ed0a611e8a7e3 [file] [log] [blame]
Eddie James21b177e2018-12-11 13:14:46 -06001#pragma once
2
3#include "ikvm_args.hpp"
4#include "ikvm_input.hpp"
5#include "ikvm_video.hpp"
6
Eddie James29f775a2018-12-11 13:22:54 -06007#include <rfb/rfb.h>
8
9#include <vector>
10
Eddie James21b177e2018-12-11 13:14:46 -060011namespace ikvm
12{
13
14/*
15 * @class Server
16 * @brief Manages the RFB server connection and updates
17 */
18class Server
19{
20 public:
21 /*
22 * @struct ClientData
23 * @brief Store necessary data for each connected RFB client
24 */
25 struct ClientData
26 {
27 /*
28 * @brief Constructs ClientData object
29 *
30 * @param[in] s - Number of frames to skip when client connects
31 * @param[in] i - Pointer to Input object
32 */
33 ClientData(int s, Input* i) : skipFrame(s), input(i)
34 {
Jae Hyun Yoo85d04552019-05-09 16:26:53 -070035 needUpdate = false;
Eddie James21b177e2018-12-11 13:14:46 -060036 }
37 ~ClientData() = default;
38 ClientData(const ClientData&) = default;
39 ClientData& operator=(const ClientData&) = default;
40 ClientData(ClientData&&) = default;
41 ClientData& operator=(ClientData&&) = default;
42
43 int skipFrame;
44 Input* input;
Jae Hyun Yoo85d04552019-05-09 16:26:53 -070045 bool needUpdate;
Eddie James21b177e2018-12-11 13:14:46 -060046 };
47
48 /*
49 * @brief Constructs Server object
50 *
51 * @param[in] args - Reference to Args object
52 * @param[in] i - Reference to Input object
53 * @param[in] v - Reference to Video object
54 */
55 Server(const Args& args, Input& i, Video& v);
56 ~Server();
57 Server(const Server&) = default;
58 Server& operator=(const Server&) = default;
59 Server(Server&&) = default;
60 Server& operator=(Server&&) = default;
61
Eddie James29f775a2018-12-11 13:22:54 -060062 /* @brief Resizes the RFB framebuffer */
63 void resize();
64 /* @brief Executes any pending RFB updates and client input */
65 void run();
66 /* @brief Sends pending video frame to clients */
67 void sendFrame();
68
69 /*
70 * @brief Indicates whether or not video data is desired
71 *
72 * @return Boolean to indicate whether any clients need a video frame
73 */
74 inline bool wantsFrame() const
75 {
76 return server->clientHead;
77 }
Eddie James21b177e2018-12-11 13:14:46 -060078 /*
79 * @brief Get the Video object
80 *
81 * @return Reference to the Video object
82 */
83 inline const Video& getVideo() const
84 {
85 return video;
86 }
87
88 private:
Eddie James29f775a2018-12-11 13:22:54 -060089 /*
Jae Hyun Yoo85d04552019-05-09 16:26:53 -070090 * @brief Handler for a client frame update message
91 *
92 * @param[in] cl - Handle to the client object
93 * @param[in] furMsg - Pointer of the FUR message
94 */
95 static void
96 clientFramebufferUpdateRequest(rfbClientPtr cl,
97 rfbFramebufferUpdateRequestMsg *furMsg);
98 /*
Eddie James29f775a2018-12-11 13:22:54 -060099 * @brief Handler for a client disconnecting
100 *
101 * @param[in] cl - Handle to the client object
102 */
103 static void clientGone(rfbClientPtr cl);
104 /*
105 * @brief Handler for client connecting
106 *
107 * @param[in] cl - Handle to the client object
108 */
109 static enum rfbNewClientAction newClient(rfbClientPtr cl);
110
111 /* @brief Performs the resize operation on the framebuffer */
112 void doResize();
113
114 /* @brief Boolean to indicate if a resize operation is on-going */
115 bool pendingResize;
116 /* @brief Number of frames handled since a client connected */
117 int frameCounter;
118 /* @brief Number of connected clients */
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800119 unsigned int numClients;
Eddie James29f775a2018-12-11 13:22:54 -0600120 /* @brief Microseconds to process RFB events every frame */
121 long int processTime;
122 /* @brief Handle to the RFB server object */
123 rfbScreenInfoPtr server;
Eddie James21b177e2018-12-11 13:14:46 -0600124 /* @brief Reference to the Input object */
125 Input& input;
126 /* @brief Reference to the Video object */
127 Video& video;
Eddie James29f775a2018-12-11 13:22:54 -0600128 /* @brief Default framebuffer storage */
129 std::vector<char> framebuffer;
Jae Hyun Yoo7dfac9f2019-01-15 10:14:59 -0800130 /* @brief Cursor bitmap width */
131 static constexpr int cursorWidth = 20;
132 /* @brief Cursor bitmap height */
133 static constexpr int cursorHeight = 20;
134 /* @brief Cursor bitmap */
135 static constexpr char cursor[] = " "
136 " x "
137 " xx "
138 " xxx "
139 " xxxx "
140 " xxxxx "
141 " xxxxxx "
142 " xxxxxxx "
143 " xxxxxxxx "
144 " xxxxxxxxx "
145 " xxxxxxxxxx "
146 " xxxxxxxxxxx "
147 " xxxxxxx "
148 " xxxxxxx "
149 " xxx xxx "
150 " xx xxx "
151 " x xxx "
152 " xxx "
153 " x "
154 " ";
155 /* @brief Cursor bitmap mask */
156 static constexpr char cursorMask[] = " o "
157 "oxo "
158 "oxxo "
159 "oxxxo "
160 "oxxxxo "
161 "oxxxxxo "
162 "oxxxxxxo "
163 "oxxxxxxxo "
164 "oxxxxxxxxo "
165 "oxxxxxxxxxo "
166 "oxxxxxxxxxxo "
167 "oxxxxxxxxxxxo "
168 "oxxxxxxxoooo "
169 "oxxxxxxxo "
170 "oxxxooxxxo "
171 "oxxo oxxxo "
172 "oxo oxxxo "
173 " o oxxxo "
174 " oxo "
175 " o ";
Eddie James21b177e2018-12-11 13:14:46 -0600176};
177
178} // namespace ikvm