| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | #include "uSynergy.h" |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | static int16_t sNetToNative16(const unsigned char *value) |
| 42 | { |
| 43 | #ifdef USYNERGY_LITTLE_ENDIAN |
| 44 | return value[1] | (value[0] << 8); |
| 45 | #else |
| 46 | return value[0] | (value[1] << 8); |
| 47 | #endif |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | static int32_t sNetToNative32(const unsigned char *value) |
| 56 | { |
| 57 | #ifdef USYNERGY_LITTLE_ENDIAN |
| 58 | return value[3] | (value[2] << 8) | (value[1] << 16) | (value[0] << 24); |
| 59 | #else |
| 60 | return value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24); |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | static void sTrace(uSynergyContext *context, const char* text) |
| 70 | { |
| 71 | |
| 72 | if (context->m_traceFunc != 0L) |
| 73 | context->m_traceFunc(context->m_cookie, text); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | static void sAddString(uSynergyContext *context, const char *string) |
| 82 | { |
| 83 | size_t len = strlen(string); |
| 84 | memcpy(context->m_replyCur, string, len); |
| 85 | context->m_replyCur += len; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | static void sAddUInt8(uSynergyContext *context, uint8_t value) |
| 94 | { |
| 95 | *context->m_replyCur++ = value; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | static void sAddUInt16(uSynergyContext *context, uint16_t value) |
| 104 | { |
| 105 | uint8_t *reply = context->m_replyCur; |
| 106 | *reply++ = (uint8_t)(value >> 8); |
| 107 | *reply++ = (uint8_t)value; |
| 108 | context->m_replyCur = reply; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | static void sAddUInt32(uSynergyContext *context, uint32_t value) |
| 117 | { |
| 118 | uint8_t *reply = context->m_replyCur; |
| 119 | *reply++ = (uint8_t)(value >> 24); |
| 120 | *reply++ = (uint8_t)(value >> 16); |
| 121 | *reply++ = (uint8_t)(value >> 8); |
| 122 | *reply++ = (uint8_t)value; |
| 123 | context->m_replyCur = reply; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | static uSynergyBool sSendReply(uSynergyContext *context) |
| 132 | { |
| 133 | |
| 134 | uint8_t *reply_buf = context->m_replyBuffer; |
| 135 | uint32_t reply_len = (uint32_t)(context->m_replyCur - reply_buf); |
| 136 | uint32_t body_len = reply_len - 4; |
| 137 | uSynergyBool ret; |
| 138 | reply_buf[0] = (uint8_t)(body_len >> 24); |
| 139 | reply_buf[1] = (uint8_t)(body_len >> 16); |
| 140 | reply_buf[2] = (uint8_t)(body_len >> 8); |
| 141 | reply_buf[3] = (uint8_t)body_len; |
| 142 | |
| 143 | |
| 144 | ret = context->m_sendFunc(context->m_cookie, context->m_replyBuffer, reply_len); |
| 145 | |
| 146 | |
| 147 | context->m_replyCur = context->m_replyBuffer+4; |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | static void sSendMouseCallback(uSynergyContext *context) |
| 157 | { |
| 158 | |
| 159 | if (context->m_mouseCallback == 0L) |
| 160 | return; |
| 161 | |
| 162 | |
| 163 | context->m_mouseCallback(context->m_cookie, context->m_mouseX, context->m_mouseY, context->m_mouseWheelX, |
| 164 | context->m_mouseWheelY, context->m_mouseButtonLeft, context->m_mouseButtonRight, context->m_mouseButtonMiddle); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | static void sSendKeyboardCallback(uSynergyContext *context, uint16_t key, uint16_t modifiers, uSynergyBool down, uSynergyBool repeat) |
| 173 | { |
| 174 | |
| 175 | if (context->m_keyboardCallback == 0L) |
| 176 | return; |
| 177 | |
| 178 | |
| 179 | context->m_keyboardCallback(context->m_cookie, key, modifiers, down, repeat); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | static void sSendJoystickCallback(uSynergyContext *context, uint8_t joyNum) |
| 188 | { |
| 189 | int8_t *sticks; |
| 190 | |
| 191 | |
| 192 | if (context->m_joystickCallback == 0L) |
| 193 | return; |
| 194 | |
| 195 | |
| 196 | sticks = context->m_joystickSticks[joyNum]; |
| 197 | context->m_joystickCallback(context->m_cookie, joyNum, context->m_joystickButtons[joyNum], sticks[0], sticks[1], sticks[2], sticks[3]); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | #define USYNERGY_IS_PACKET(pkt_id) memcmp(message+4, pkt_id, 4)==0 |
| 206 | static void sProcessMessage(uSynergyContext *context, const uint8_t *message) |
| 207 | { |
| 208 | |
| 209 | if (memcmp(message+4, "Synergy", 7)==0) |
| 210 | { |
| 211 | |
| 212 | |
| 213 | |
| 214 | sAddString(context, "Synergy"); |
| 215 | sAddUInt16(context, USYNERGY_PROTOCOL_MAJOR); |
| 216 | sAddUInt16(context, USYNERGY_PROTOCOL_MINOR); |
| 217 | sAddUInt32(context, (uint32_t)strlen(context->m_clientName)); |
| 218 | sAddString(context, context->m_clientName); |
| 219 | if (!sSendReply(context)) |
| 220 | { |
| 221 | |
| 222 | sTrace(context, "SendReply failed, trying to reconnect in a second"); |
| 223 | context->m_connected = USYNERGY_FALSE; |
| 224 | context->m_sleepFunc(context->m_cookie, 1000); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | |
| 229 | char buffer[256+1]; |
| 230 | sprintf(buffer, "Connected as client \"%s\"", context->m_clientName); |
| 231 | sTrace(context, buffer); |
| 232 | context->m_hasReceivedHello = USYNERGY_TRUE; |
| 233 | } |
| 234 | return; |
| 235 | } |
| 236 | else if (USYNERGY_IS_PACKET("QINF")) |
| 237 | { |
| 238 | |
| 239 | |
| 240 | |
| 241 | uint16_t x = 0, y = 0, warp = 0; |
| 242 | sAddString(context, "DINF"); |
| 243 | sAddUInt16(context, x); |
| 244 | sAddUInt16(context, y); |
| 245 | sAddUInt16(context, context->m_clientWidth); |
| 246 | sAddUInt16(context, context->m_clientHeight); |
| 247 | sAddUInt16(context, warp); |
| 248 | sAddUInt16(context, 0); |
| 249 | sAddUInt16(context, 0); |
| 250 | sSendReply(context); |
| 251 | return; |
| 252 | } |
| 253 | else if (USYNERGY_IS_PACKET("CIAK")) |
| 254 | { |
| 255 | |
| 256 | |
| 257 | return; |
| 258 | } |
| 259 | else if (USYNERGY_IS_PACKET("CROP")) |
| 260 | { |
| 261 | |
| 262 | |
| 263 | return; |
| 264 | } |
| 265 | else if (USYNERGY_IS_PACKET("CINN")) |
| 266 | { |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | context->m_sequenceNumber = sNetToNative32(message + 12); |
| 272 | context->m_isCaptured = USYNERGY_TRUE; |
| 273 | |
| 274 | |
| 275 | if (context->m_screenActiveCallback != 0L) |
| 276 | context->m_screenActiveCallback(context->m_cookie, USYNERGY_TRUE); |
| 277 | } |
| 278 | else if (USYNERGY_IS_PACKET("COUT")) |
| 279 | { |
| 280 | |
| 281 | |
| 282 | context->m_isCaptured = USYNERGY_FALSE; |
| 283 | |
| 284 | |
| 285 | if (context->m_screenActiveCallback != 0L) |
| 286 | context->m_screenActiveCallback(context->m_cookie, USYNERGY_FALSE); |
| 287 | } |
| 288 | else if (USYNERGY_IS_PACKET("DMDN")) |
| 289 | { |
| 290 | |
| 291 | |
| 292 | char btn = message[8]-1; |
| 293 | if (btn==2) |
| 294 | context->m_mouseButtonRight = USYNERGY_TRUE; |
| 295 | else if (btn==1) |
| 296 | context->m_mouseButtonMiddle = USYNERGY_TRUE; |
| 297 | else |
| 298 | context->m_mouseButtonLeft = USYNERGY_TRUE; |
| 299 | sSendMouseCallback(context); |
| 300 | } |
| 301 | else if (USYNERGY_IS_PACKET("DMUP")) |
| 302 | { |
| 303 | |
| 304 | |
| 305 | char btn = message[8]-1; |
| 306 | if (btn==2) |
| 307 | context->m_mouseButtonRight = USYNERGY_FALSE; |
| 308 | else if (btn==1) |
| 309 | context->m_mouseButtonMiddle = USYNERGY_FALSE; |
| 310 | else |
| 311 | context->m_mouseButtonLeft = USYNERGY_FALSE; |
| 312 | sSendMouseCallback(context); |
| 313 | } |
| 314 | else if (USYNERGY_IS_PACKET("DMMV")) |
| 315 | { |
| 316 | |
| 317 | |
| 318 | context->m_mouseX = sNetToNative16(message+8); |
| 319 | context->m_mouseY = sNetToNative16(message+10); |
| 320 | sSendMouseCallback(context); |
| 321 | } |
| 322 | else if (USYNERGY_IS_PACKET("DMWM")) |
| 323 | { |
| 324 | |
| 325 | |
| 326 | |
| 327 | context->m_mouseWheelX += sNetToNative16(message+8); |
| 328 | context->m_mouseWheelY += sNetToNative16(message+10); |
| 329 | sSendMouseCallback(context); |
| 330 | } |
| 331 | else if (USYNERGY_IS_PACKET("DKDN")) |
| 332 | { |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | uint16_t mod = sNetToNative16(message+10); |
| 338 | uint16_t key = sNetToNative16(message+12); |
| 339 | sSendKeyboardCallback(context, key, mod, USYNERGY_TRUE, USYNERGY_FALSE); |
| 340 | } |
| 341 | else if (USYNERGY_IS_PACKET("DKRP")) |
| 342 | { |
| 343 | |
| 344 | |
| 345 | |
| 346 | uint16_t mod = sNetToNative16(message+10); |
| 347 | |
| 348 | uint16_t key = sNetToNative16(message+14); |
| 349 | sSendKeyboardCallback(context, key, mod, USYNERGY_TRUE, USYNERGY_TRUE); |
| 350 | } |
| 351 | else if (USYNERGY_IS_PACKET("DKUP")) |
| 352 | { |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | uint16_t mod = sNetToNative16(message+10); |
| 358 | uint16_t key = sNetToNative16(message+12); |
| 359 | sSendKeyboardCallback(context, key, mod, USYNERGY_FALSE, USYNERGY_FALSE); |
| 360 | } |
| 361 | else if (USYNERGY_IS_PACKET("DGBT")) |
| 362 | { |
| 363 | |
| 364 | |
| 365 | uint8_t joy_num = message[8]; |
| 366 | if (joy_num<USYNERGY_NUM_JOYSTICKS) |
| 367 | { |
| 368 | |
| 369 | context->m_joystickButtons[joy_num] = (message[9] << 8) | message[10]; |
| 370 | sSendJoystickCallback(context, joy_num); |
| 371 | } |
| 372 | } |
| 373 | else if (USYNERGY_IS_PACKET("DGST")) |
| 374 | { |
| 375 | |
| 376 | |
| 377 | uint8_t joy_num = message[8]; |
| 378 | if (joy_num<USYNERGY_NUM_JOYSTICKS) |
| 379 | { |
| 380 | |
| 381 | memcpy(context->m_joystickSticks[joy_num], message+9, 4); |
| 382 | sSendJoystickCallback(context, joy_num); |
| 383 | } |
| 384 | } |
| 385 | else if (USYNERGY_IS_PACKET("DSOP")) |
| 386 | { |
| 387 | |
| 388 | |
| 389 | } |
| 390 | else if (USYNERGY_IS_PACKET("CALV")) |
| 391 | { |
| 392 | |
| 393 | |
| 394 | sAddString(context, "CALV"); |
| 395 | sSendReply(context); |
| 396 | |
| 397 | } |
| 398 | else if (USYNERGY_IS_PACKET("DCLP")) |
| 399 | { |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | const uint8_t * parse_msg = message+17; |
| 415 | uint32_t num_formats = sNetToNative32(parse_msg); |
| 416 | parse_msg += 4; |
| 417 | for (; num_formats; num_formats--) |
| 418 | { |
| 419 | |
| 420 | uint32_t format = sNetToNative32(parse_msg); |
| 421 | uint32_t size = sNetToNative32(parse_msg+4); |
| 422 | parse_msg += 8; |
| 423 | |
| 424 | |
| 425 | if (context->m_clipboardCallback) |
| 426 | context->m_clipboardCallback(context->m_cookie, format, parse_msg, size); |
| 427 | |
| 428 | parse_msg += size; |
| 429 | } |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | char buffer[64]; |
| 446 | sprintf(buffer, "Unknown packet '%c%c%c%c'", message[4], message[5], message[6], message[7]); |
| 447 | sTrace(context, buffer); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | |
| 452 | sAddString(context, "CNOP"); |
| 453 | sSendReply(context); |
| 454 | } |
| 455 | #undef USYNERGY_IS_PACKET |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | static void sSetDisconnected(uSynergyContext *context) |
| 463 | { |
| 464 | context->m_connected = USYNERGY_FALSE; |
| 465 | context->m_hasReceivedHello = USYNERGY_FALSE; |
| 466 | context->m_isCaptured = USYNERGY_FALSE; |
| 467 | context->m_replyCur = context->m_replyBuffer + 4; |
| 468 | context->m_sequenceNumber = 0; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | static void sUpdateContext(uSynergyContext *context) |
| 477 | { |
| 478 | |
| 479 | int receive_size = USYNERGY_RECEIVE_BUFFER_SIZE - context->m_receiveOfs; |
| 480 | int num_received = 0; |
| 481 | int packlen = 0; |
| 482 | if (context->m_receiveFunc(context->m_cookie, context->m_receiveBuffer + context->m_receiveOfs, receive_size, &num_received) == USYNERGY_FALSE) |
| 483 | { |
| 484 | |
| 485 | char buffer[128]; |
| 486 | sprintf(buffer, "Receive failed (%d bytes asked, %d bytes received), trying to reconnect in a second", receive_size, num_received); |
| 487 | sTrace(context, buffer); |
| 488 | sSetDisconnected(context); |
| 489 | context->m_sleepFunc(context->m_cookie, 1000); |
| 490 | return; |
| 491 | } |
| 492 | context->m_receiveOfs += num_received; |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | if (num_received == 0) |
| 498 | context->m_sleepFunc(context->m_cookie, 500); |
| 499 | |
| 500 | |
| 501 | if (context->m_hasReceivedHello) |
| 502 | { |
| 503 | uint32_t cur_time = context->m_getTimeFunc(); |
| 504 | if (num_received == 0) |
| 505 | { |
| 506 | |
| 507 | if ((cur_time - context->m_lastMessageTime) > USYNERGY_IDLE_TIMEOUT) |
| 508 | sSetDisconnected(context); |
| 509 | } |
| 510 | else |
| 511 | context->m_lastMessageTime = cur_time; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | for (;;) |
| 516 | { |
| 517 | |
| 518 | packlen = sNetToNative32(context->m_receiveBuffer); |
| 519 | if (packlen+4 > context->m_receiveOfs) |
| 520 | break; |
| 521 | |
| 522 | |
| 523 | sProcessMessage(context, context->m_receiveBuffer); |
| 524 | |
| 525 | |
| 526 | memmove(context->m_receiveBuffer, context->m_receiveBuffer+packlen+4, context->m_receiveOfs-packlen-4); |
| 527 | context->m_receiveOfs -= packlen+4; |
| 528 | } |
| 529 | |
| 530 | |
| 531 | if (packlen > USYNERGY_RECEIVE_BUFFER_SIZE) |
| 532 | { |
| 533 | |
| 534 | char buffer[128]; |
| 535 | sprintf(buffer, "Oversized packet: '%c%c%c%c' (length %d)", context->m_receiveBuffer[4], context->m_receiveBuffer[5], context->m_receiveBuffer[6], context->m_receiveBuffer[7], packlen); |
| 536 | sTrace(context, buffer); |
| 537 | num_received = context->m_receiveOfs-4; |
| 538 | while (num_received != packlen) |
| 539 | { |
| 540 | int buffer_left = packlen - num_received; |
| 541 | int to_receive = buffer_left < USYNERGY_RECEIVE_BUFFER_SIZE ? buffer_left : USYNERGY_RECEIVE_BUFFER_SIZE; |
| 542 | int ditch_received = 0; |
| 543 | if (context->m_receiveFunc(context->m_cookie, context->m_receiveBuffer, to_receive, &ditch_received) == USYNERGY_FALSE) |
| 544 | { |
| 545 | |
| 546 | sTrace(context, "Receive failed, trying to reconnect in a second"); |
| 547 | sSetDisconnected(context); |
| 548 | context->m_sleepFunc(context->m_cookie, 1000); |
| 549 | break; |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | num_received += ditch_received; |
| 554 | } |
| 555 | } |
| 556 | context->m_receiveOfs = 0; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | void uSynergyInit(uSynergyContext *context) |
| 571 | { |
| 572 | |
| 573 | memset(context, 0, sizeof(uSynergyContext)); |
| 574 | |
| 575 | |
| 576 | sSetDisconnected(context); |
| 577 | } |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | void uSynergyUpdate(uSynergyContext *context) |
| 584 | { |
| 585 | if (context->m_connected) |
| 586 | { |
| 587 | |
| 588 | sUpdateContext(context); |
| 589 | } |
| 590 | else |
| 591 | { |
| 592 | |
| 593 | if (context->m_connectFunc(context->m_cookie)) |
| 594 | context->m_connected = USYNERGY_TRUE; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | void uSynergySendClipboard(uSynergyContext *context, const char *text) |
| 604 | { |
| 605 | |
| 606 | uint32_t overhead_size = 4 + |
| 607 | 4 + |
| 608 | 1 + |
| 609 | 4 + |
| 610 | 4 + |
| 611 | 4 + |
| 612 | 4 + |
| 613 | 4; |
| 614 | uint32_t max_length = USYNERGY_REPLY_BUFFER_SIZE - overhead_size; |
| 615 | |
| 616 | |
| 617 | uint32_t text_length = (uint32_t)strlen(text); |
| 618 | if (text_length > max_length) |
| 619 | { |
| 620 | char buffer[128]; |
| 621 | sprintf(buffer, "Clipboard buffer too small, clipboard truncated at %d characters", max_length); |
| 622 | sTrace(context, buffer); |
| 623 | text_length = max_length; |
| 624 | } |
| 625 | |
| 626 | |
| 627 | sAddString(context, "DCLP"); |
| 628 | sAddUInt8(context, 0); |
| 629 | sAddUInt32(context, context->m_sequenceNumber); |
| 630 | sAddUInt32(context, 4+4+4+text_length); |
| 631 | sAddUInt32(context, 1); |
| 632 | sAddUInt32(context, USYNERGY_CLIPBOARD_FORMAT_TEXT); |
| 633 | sAddUInt32(context, text_length); |
| 634 | sAddString(context, text); |
| 635 | sSendReply(context); |
| 636 | } |