Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "ikvm_args.hpp" |
| 4 | #include "ikvm_input.hpp" |
| 5 | #include "ikvm_video.hpp" |
| 6 | |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 7 | #include <rfb/rfb.h> |
| 8 | |
| 9 | #include <vector> |
| 10 | |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 11 | namespace ikvm |
| 12 | { |
| 13 | |
| 14 | /* |
| 15 | * @class Server |
| 16 | * @brief Manages the RFB server connection and updates |
| 17 | */ |
| 18 | class 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 | { |
| 35 | } |
| 36 | ~ClientData() = default; |
| 37 | ClientData(const ClientData&) = default; |
| 38 | ClientData& operator=(const ClientData&) = default; |
| 39 | ClientData(ClientData&&) = default; |
| 40 | ClientData& operator=(ClientData&&) = default; |
| 41 | |
| 42 | int skipFrame; |
| 43 | Input* input; |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * @brief Constructs Server object |
| 48 | * |
| 49 | * @param[in] args - Reference to Args object |
| 50 | * @param[in] i - Reference to Input object |
| 51 | * @param[in] v - Reference to Video object |
| 52 | */ |
| 53 | Server(const Args& args, Input& i, Video& v); |
| 54 | ~Server(); |
| 55 | Server(const Server&) = default; |
| 56 | Server& operator=(const Server&) = default; |
| 57 | Server(Server&&) = default; |
| 58 | Server& operator=(Server&&) = default; |
| 59 | |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 60 | /* @brief Resizes the RFB framebuffer */ |
| 61 | void resize(); |
| 62 | /* @brief Executes any pending RFB updates and client input */ |
| 63 | void run(); |
| 64 | /* @brief Sends pending video frame to clients */ |
| 65 | void sendFrame(); |
| 66 | |
| 67 | /* |
| 68 | * @brief Indicates whether or not video data is desired |
| 69 | * |
| 70 | * @return Boolean to indicate whether any clients need a video frame |
| 71 | */ |
| 72 | inline bool wantsFrame() const |
| 73 | { |
| 74 | return server->clientHead; |
| 75 | } |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 76 | /* |
| 77 | * @brief Get the Video object |
| 78 | * |
| 79 | * @return Reference to the Video object |
| 80 | */ |
| 81 | inline const Video& getVideo() const |
| 82 | { |
| 83 | return video; |
| 84 | } |
| 85 | |
| 86 | private: |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 87 | /* |
| 88 | * @brief Handler for a client disconnecting |
| 89 | * |
| 90 | * @param[in] cl - Handle to the client object |
| 91 | */ |
| 92 | static void clientGone(rfbClientPtr cl); |
| 93 | /* |
| 94 | * @brief Handler for client connecting |
| 95 | * |
| 96 | * @param[in] cl - Handle to the client object |
| 97 | */ |
| 98 | static enum rfbNewClientAction newClient(rfbClientPtr cl); |
| 99 | |
| 100 | /* @brief Performs the resize operation on the framebuffer */ |
| 101 | void doResize(); |
| 102 | |
| 103 | /* @brief Boolean to indicate if a resize operation is on-going */ |
| 104 | bool pendingResize; |
| 105 | /* @brief Number of frames handled since a client connected */ |
| 106 | int frameCounter; |
| 107 | /* @brief Number of connected clients */ |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame^] | 108 | unsigned int numClients; |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 109 | /* @brief Microseconds to process RFB events every frame */ |
| 110 | long int processTime; |
| 111 | /* @brief Handle to the RFB server object */ |
| 112 | rfbScreenInfoPtr server; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 113 | /* @brief Reference to the Input object */ |
| 114 | Input& input; |
| 115 | /* @brief Reference to the Video object */ |
| 116 | Video& video; |
Eddie James | 29f775a | 2018-12-11 13:22:54 -0600 | [diff] [blame] | 117 | /* @brief Default framebuffer storage */ |
| 118 | std::vector<char> framebuffer; |
Jae Hyun Yoo | 7dfac9f | 2019-01-15 10:14:59 -0800 | [diff] [blame^] | 119 | /* @brief Cursor bitmap width */ |
| 120 | static constexpr int cursorWidth = 20; |
| 121 | /* @brief Cursor bitmap height */ |
| 122 | static constexpr int cursorHeight = 20; |
| 123 | /* @brief Cursor bitmap */ |
| 124 | static constexpr char cursor[] = " " |
| 125 | " x " |
| 126 | " xx " |
| 127 | " xxx " |
| 128 | " xxxx " |
| 129 | " xxxxx " |
| 130 | " xxxxxx " |
| 131 | " xxxxxxx " |
| 132 | " xxxxxxxx " |
| 133 | " xxxxxxxxx " |
| 134 | " xxxxxxxxxx " |
| 135 | " xxxxxxxxxxx " |
| 136 | " xxxxxxx " |
| 137 | " xxxxxxx " |
| 138 | " xxx xxx " |
| 139 | " xx xxx " |
| 140 | " x xxx " |
| 141 | " xxx " |
| 142 | " x " |
| 143 | " "; |
| 144 | /* @brief Cursor bitmap mask */ |
| 145 | static constexpr char cursorMask[] = " o " |
| 146 | "oxo " |
| 147 | "oxxo " |
| 148 | "oxxxo " |
| 149 | "oxxxxo " |
| 150 | "oxxxxxo " |
| 151 | "oxxxxxxo " |
| 152 | "oxxxxxxxo " |
| 153 | "oxxxxxxxxo " |
| 154 | "oxxxxxxxxxo " |
| 155 | "oxxxxxxxxxxo " |
| 156 | "oxxxxxxxxxxxo " |
| 157 | "oxxxxxxxoooo " |
| 158 | "oxxxxxxxo " |
| 159 | "oxxxooxxxo " |
| 160 | "oxxo oxxxo " |
| 161 | "oxo oxxxo " |
| 162 | " o oxxxo " |
| 163 | " oxo " |
| 164 | " o "; |
Eddie James | 21b177e | 2018-12-11 13:14:46 -0600 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | } // namespace ikvm |