|
| 1 | +#include <sys/types.h> |
| 2 | +#include <sys/socket.h> |
| 3 | +#include <netdb.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <unistd.h> |
| 7 | +#include <string.h> |
| 8 | +#include <iostream> |
| 9 | +#include "../build/rmkit.h" |
| 10 | +#include "../vendor/json/json.hpp" |
| 11 | +#include "../shared/string.h" |
| 12 | + |
| 13 | +#define BUF_SIZE 1024 |
| 14 | + |
| 15 | +// TODO: |
| 16 | +// * implement TINIT |
| 17 | +// * implement latency calculation (while drawing?) |
| 18 | +// * implement room counter |
| 19 | +// * implement shared clear |
| 20 | +// * implement undrawing when server doesn't respond within time limit |
| 21 | + |
| 22 | +// message types |
| 23 | +#define TINIT "init" |
| 24 | +#define TJOIN "join" |
| 25 | +#define TDRAW "draw" |
| 26 | +#define TCLEAR "clear" |
| 27 | + |
| 28 | +using json = nlohmann::json |
| 29 | + |
| 30 | +HOST := getenv("HOST") ? getenv("HOST") : "rmkit.dev" |
| 31 | +PORT := getenv("PORT") ? getenv("PORT") : "65432" |
| 32 | + |
| 33 | +using PLS::Observable |
| 34 | +class AppState: |
| 35 | + public: |
| 36 | + Observable<bool> erase |
| 37 | + Observable<string> room |
| 38 | +AppState STATE |
| 39 | + |
| 40 | +class JSONSocket: |
| 41 | + public: |
| 42 | + int sockfd |
| 43 | + struct addrinfo hints; |
| 44 | + struct addrinfo *result, *rp; |
| 45 | + char buf[BUF_SIZE] |
| 46 | + string leftover |
| 47 | + deque<json> out_queue |
| 48 | + std::mutex lock |
| 49 | + deque<json> in_queue |
| 50 | + const char* host |
| 51 | + const char* port |
| 52 | + bool _connected = false |
| 53 | + |
| 54 | + JSONSocket(const char* host, port): |
| 55 | + sockfd = socket(AF_INET, SOCK_STREAM, 0) |
| 56 | + memset(&hints, 0, sizeof(struct addrinfo)); |
| 57 | + hints.ai_family = AF_UNSPEC; |
| 58 | + hints.ai_socktype = SOCK_DGRAM; |
| 59 | + hints.ai_flags = 0; |
| 60 | + hints.ai_protocol = 0; |
| 61 | + self.host = host |
| 62 | + self.port = port |
| 63 | + self.leftover = "" |
| 64 | + |
| 65 | + new thread([=]() { |
| 66 | + s := getaddrinfo(host, port, &hints, &result) |
| 67 | + if s != 0: |
| 68 | + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); |
| 69 | + exit(EXIT_FAILURE); |
| 70 | + |
| 71 | + self.listen() |
| 72 | + }) |
| 73 | + |
| 74 | + new thread([=]() { |
| 75 | + self.write_loop(); |
| 76 | + }) |
| 77 | + |
| 78 | + void write_loop(): |
| 79 | + while true: |
| 80 | + if self.sockfd < 3: |
| 81 | + debug "CANT WRITE TO SOCKET" |
| 82 | + sleep(1) |
| 83 | + continue |
| 84 | + |
| 85 | + self.lock.lock() |
| 86 | + if !self._connected: |
| 87 | + // wait for listen() to reconnect |
| 88 | + self.lock.unlock() |
| 89 | + sleep(1) |
| 90 | + continue |
| 91 | + |
| 92 | + for (i:=0;i<self.in_queue.size();i++): |
| 93 | + json_dump := self.in_queue[i].dump() |
| 94 | + msg_c_str := json_dump.c_str() |
| 95 | + ::send(self.sockfd, msg_c_str, strlen(msg_c_str), MSG_DONTWAIT) |
| 96 | + ::send(self.sockfd, "\n", 1, MSG_DONTWAIT) |
| 97 | + self.in_queue.clear() |
| 98 | + self.lock.unlock() |
| 99 | + |
| 100 | + void write(json &j): |
| 101 | + self.lock.lock() |
| 102 | + self.in_queue.push_back(j); |
| 103 | + self.lock.unlock() |
| 104 | + |
| 105 | + void listen(): |
| 106 | + bytes_read := -1 |
| 107 | + while true: |
| 108 | + while bytes_read <= 0: |
| 109 | + err := connect(self.sockfd, self.result->ai_addr, self.result->ai_addrlen) |
| 110 | + if err == 0 || errno == EISCONN: |
| 111 | + debug "(re)connected" |
| 112 | + self.lock.lock() |
| 113 | + self._connected = true |
| 114 | + self.lock.unlock() |
| 115 | + break |
| 116 | + debug "(re)connecting...", err, errno |
| 117 | + self.lock.lock() |
| 118 | + close(self.sockfd) |
| 119 | + self._connected = false |
| 120 | + self.lock.unlock() |
| 121 | + sleep(1) |
| 122 | + |
| 123 | + bytes_read = read(sockfd, buf, BUF_SIZE-1) |
| 124 | + if bytes_read <= 0: |
| 125 | + if bytes_read == -1 and errno == EAGAIN: |
| 126 | + continue |
| 127 | + |
| 128 | + close(self.sockfd) |
| 129 | + self.sockfd = socket(AF_INET, SOCK_STREAM, 0) |
| 130 | + sleep(1) |
| 131 | + continue |
| 132 | + buf[bytes_read] = 0 |
| 133 | + sbuf := string(buf) |
| 134 | + memset(buf, 0, BUF_SIZE) |
| 135 | + |
| 136 | + msgs := str_utils::split(sbuf, '\n') |
| 137 | + if leftover != "" && msgs.size() > 0: |
| 138 | + msgs[0] = leftover + msgs[0] |
| 139 | + leftover = "" |
| 140 | + if sbuf[sbuf.length()-1] != '\n': |
| 141 | + leftover = msgs.back() |
| 142 | + msgs.pop_back() |
| 143 | + for (i:=0; i!=msgs.size(); ++i): |
| 144 | + try: |
| 145 | + msg_json := json::parse(msgs[i].begin(), msgs[i].end()) |
| 146 | + lock.lock() |
| 147 | + out_queue.push_back(msg_json) |
| 148 | + lock.unlock() |
| 149 | + catch(...): |
| 150 | + debug "COULDNT PARSE", msgs[i] |
| 151 | + |
| 152 | + ui::TaskQueue::wakeup() |
| 153 | + |
| 154 | + |
| 155 | +class Note: public ui::Widget: |
| 156 | + public: |
| 157 | + int prevx = -1, prevy = -1 |
| 158 | + framebuffer::VirtualFB *vfb |
| 159 | + bool full_redraw |
| 160 | + JSONSocket *socket |
| 161 | + |
| 162 | + Note(int x, y, w, h, JSONSocket* s): Widget(x, y, w, h): |
| 163 | + vfb = new framebuffer::VirtualFB(self.fb->width, self.fb->height) |
| 164 | + vfb->clear_screen() |
| 165 | + self.full_redraw = true |
| 166 | + self.socket = s |
| 167 | + self.mouse_down = false |
| 168 | + |
| 169 | + void on_mouse_up(input::SynMotionEvent &ev): |
| 170 | + prevx = prevy = -1 |
| 171 | + |
| 172 | + bool ignore_event(input::SynMotionEvent &ev): |
| 173 | + return input::is_touch_event(ev) != NULL |
| 174 | + |
| 175 | + void on_mouse_move(input::SynMotionEvent &ev): |
| 176 | + width := STATE.erase ? 20 : 5 |
| 177 | + if prevx != -1: |
| 178 | + vfb->draw_line(prevx, prevy, ev.x, ev.y, width, GRAY) |
| 179 | + self.dirty = 1 |
| 180 | + |
| 181 | + json j |
| 182 | + j["type"] = TDRAW |
| 183 | + j["prevx"] = prevx |
| 184 | + j["prevy"] = prevy |
| 185 | + j["x"] = ev.x |
| 186 | + j["y"] = ev.y |
| 187 | + j["width"] = width |
| 188 | + j["color"] = STATE.erase ? WHITE : BLACK |
| 189 | + |
| 190 | + self.socket->write(j) |
| 191 | + |
| 192 | + prevx = ev.x |
| 193 | + prevy = ev.y |
| 194 | + |
| 195 | + void render(): |
| 196 | + if self.full_redraw: |
| 197 | + self.full_redraw = false |
| 198 | + memcpy(self.fb->fbmem, vfb->fbmem, vfb->byte_size) |
| 199 | + return |
| 200 | + |
| 201 | + dirty_rect := self.vfb->dirty_area |
| 202 | + for int i = dirty_rect.y0; i < dirty_rect.y1; i++: |
| 203 | + memcpy(&fb->fbmem[i*fb->width + dirty_rect.x0], &vfb->fbmem[i*fb->width + dirty_rect.x0], |
| 204 | + (dirty_rect.x1 - dirty_rect.x0) * sizeof(remarkable_color)) |
| 205 | + self.fb->dirty_area = vfb->dirty_area |
| 206 | + self.fb->dirty = 1 |
| 207 | + framebuffer::reset_dirty(vfb->dirty_area) |
| 208 | + |
| 209 | + |
| 210 | +class EraseButton: public ui::Button: |
| 211 | + public: |
| 212 | + EraseButton(int x, y, w, h): Button(x, y, w, h, "erase"): |
| 213 | + pass |
| 214 | + |
| 215 | + void on_mouse_down(input::SynMotionEvent &ev): |
| 216 | + STATE.erase = !STATE.erase |
| 217 | + debug "SETTING ERASER TO", STATE.erase |
| 218 | + self.dirty = 1 |
| 219 | + if STATE.erase: |
| 220 | + self.text = "pen" |
| 221 | + else: |
| 222 | + self.text = "eraser" |
| 223 | + |
| 224 | +class RoomInput: public ui::TextInput: |
| 225 | + public: |
| 226 | + |
| 227 | + RoomInput(int x, y, w, h, JSONSocket *sock): TextInput(x, y, w, h, "default"): |
| 228 | + self->events.done += PLS_LAMBDA(string &s): |
| 229 | + self.join(s) |
| 230 | + ; |
| 231 | + |
| 232 | + void join(string room): |
| 233 | + debug "SETTING ROOM TO", room |
| 234 | + STATE.room = room |
| 235 | + |
| 236 | + |
| 237 | +class App: |
| 238 | + public: |
| 239 | + Note *note |
| 240 | + JSONSocket *socket |
| 241 | + ui::HorizontalLayout *button_bar |
| 242 | + |
| 243 | + App(): |
| 244 | + demo_scene := ui::make_scene() |
| 245 | + ui::MainLoop::set_scene(demo_scene) |
| 246 | + |
| 247 | + fb := framebuffer::get() |
| 248 | + fb->clear_screen() |
| 249 | + fb->redraw_screen() |
| 250 | + w, h = fb->get_display_size() |
| 251 | + |
| 252 | + socket = new JSONSocket(HOST, PORT) |
| 253 | + note = new Note(0, 0, w, h-50, socket) |
| 254 | + demo_scene->add(note) |
| 255 | + |
| 256 | + button_bar = new ui::HorizontalLayout(0, 0, w, 50, demo_scene) |
| 257 | + hbar := new ui::VerticalLayout(0, 0, w, h, demo_scene) |
| 258 | + hbar->pack_end(button_bar) |
| 259 | + |
| 260 | + erase_button := new EraseButton(0, 0, 200, 50) |
| 261 | + room_label := new ui::Text(0, 0, 200, 50, "room: ") |
| 262 | + room_label->justify = ui::Text::JUSTIFY::RIGHT |
| 263 | + room_button := new RoomInput(0, 0, 200, 50, socket) |
| 264 | + |
| 265 | + button_bar->pack_start(erase_button) |
| 266 | + button_bar->pack_end(room_button) |
| 267 | + button_bar->pack_end(room_label) |
| 268 | + |
| 269 | + STATE.room(PLS_DELEGATE(self.join_room)) |
| 270 | + room_button->join("default") |
| 271 | + |
| 272 | + |
| 273 | + void join_room(string room): |
| 274 | + // we are not connected to socket just yet |
| 275 | + json j |
| 276 | + j["type"] = TJOIN |
| 277 | + j["room"] = room |
| 278 | + socket->write(j) |
| 279 | + // hit URL for downloading room image |
| 280 | + url := "http://rmkit.dev:65431/room/" + room |
| 281 | + room_file := "/tmp/room_" + room |
| 282 | + curl_cmd := "curl " + url + " > " + room_file |
| 283 | + ret := system(curl_cmd.c_str()) |
| 284 | + |
| 285 | + if ret != 0: |
| 286 | + debug "ERROR WITH CURL?" |
| 287 | + else: |
| 288 | + debug "DISPLAYING IMAGE" |
| 289 | + self.note->vfb->load_from_png(room_file) |
| 290 | + ui::MainLoop::full_refresh() |
| 291 | + |
| 292 | + def handle_key_event(input::SynKeyEvent ev): |
| 293 | + // pressing any button will clear the screen |
| 294 | + if ev.key == KEY_LEFT: |
| 295 | + debug "CLEARING SCREEN" |
| 296 | + note->vfb->clear_screen() |
| 297 | + ui::MainLoop::fb->clear_screen() |
| 298 | + button_bar->refresh() |
| 299 | + |
| 300 | + def handle_server_response(): |
| 301 | + socket->lock.lock() |
| 302 | + for (i:=0; i < socket->out_queue.size(); i++): |
| 303 | + j := socket->out_queue[i] |
| 304 | + try: |
| 305 | + if j["type"] == TDRAW: |
| 306 | + note->vfb->draw_line(j["prevx"], j["prevy"], j["x"], j["y"], j["width"], j["color"]) |
| 307 | + note->dirty = 1 |
| 308 | + button_bar->refresh() |
| 309 | + else if j["type"] == TCLEAR: |
| 310 | + // TODO |
| 311 | + pass |
| 312 | + else: |
| 313 | + debug "unknown message type" |
| 314 | + catch(...): |
| 315 | + debug "COULDN'T PARSE RESPONSE FROM SERVER", j |
| 316 | + socket->out_queue.clear() |
| 317 | + socket->lock.unlock() |
| 318 | + |
| 319 | + def run(): |
| 320 | + ui::MainLoop::key_event += PLS_DELEGATE(self.handle_key_event) |
| 321 | + |
| 322 | + while true: |
| 323 | + self.handle_server_response() |
| 324 | + ui::MainLoop::main() |
| 325 | + ui::MainLoop::redraw() |
| 326 | + ui::MainLoop::read_input() |
| 327 | + |
| 328 | +app := App() |
| 329 | +int main(): |
| 330 | + app.run() |
0 commit comments