+ if (ntohl(rect.encoding) != 0)
+ error("unsupported encoding\n");
+
+ width = ntohs(rect.width);
+ height = ntohs(rect.height);
+
+ printf("UPDATE: %ux%u at (%u,%u)\n", width, height,
+ ntohs(rect.x), ntohs(rect.y));
+
+ ptr = data = xmalloc(width * height * 4);
+ data_left = width * height * 4;
+
+ while (data_left > 0) {
+ ret = read(vnc_sock, ptr, data_left);
+ if (ret <= 0)
+ error("error on data read\n");
+
+ ptr += ret;
+ data_left -= ret;
+ }
+
+ // push our 32-bit RGB data into small enough chunks
+ // (64x64) so we are sure we can send them without
+ // everything crashing and stuff :-)
+ for (yt = 0; yt < (height + 63) / 64; ++yt) {
+ for (xt = 0; xt < (width + 63) / 64; ++xt) {
+ int x, y;
+ int bw = width - xt * 64;
+ int bh = height - yt * 64;
+ if (bw > 64)
+ bw = 64;
+ if (bh > 64)
+ bh = 64;
+
+ dst = smallblock;
+ for (y = 0; y < bh; ++y) {
+ src = data + ((yt * 64 + (bh - 1 - y)) * width + (xt * 64)) * 4;
+ for (x = 0; x < bw; ++x) {
+ *dst++ = *src++;
+ *dst++ = *src++;
+ *dst++ = *src++;
+ ++src;
+ }
+ }
+
+ rdp_send_bitmap_update(ntohs(rect.x) + xt * 64, ntohs(rect.y) + yt * 64, bw, bh, smallblock);
+ }
+ }
+
+ xfree(data);
+ }
+}
+
+int listen_on_vnc = 0;
+
+int serve_client()
+{
+ int vnc_sock = vnc_init();
+
+ if (!mcs_recv_connect_initial())
+ error("MCS_CONNECT_INITIAL recv failed");
+ mcs_send_connect_response();
+
+ for ( ;; ) {
+ uint8 type, data_pdu_type;
+ STREAM s;
+
+ fd_set readfs;
+ FD_ZERO(&readfs);
+ FD_SET(tcp_get_socket(), &readfs);
+ FD_SET(vnc_sock, &readfs);
+
+ select(FD_SETSIZE, &readfs, NULL, NULL, NULL);
+
+ // activity on RDP socket?
+ if (FD_ISSET(tcp_get_socket(), &readfs)) {
+ if ((s = rdp_recv(&type)) != NULL) {
+ if (type != RDP_PDU_DATA) {
+ printf("Unknown RDP packet of type %u\n", type);
+ continue;
+ }
+
+ in_uint8s(s, 8); /* shareid, pad, streamid, length */
+ in_uint8(s, data_pdu_type);
+ in_uint8s(s, 3); /* compress_type, compress_len */
+
+ switch (data_pdu_type) {
+ case RDP_DATA_PDU_INPUT:
+ handle_input_pdu(s, vnc_sock);
+ listen_on_vnc = 1;
+ break;
+ default:
+ printf("Unknown data PDU type %u\n", data_pdu_type);
+ };
+ }
+ }
+
+ // activity on VNC socket?
+ if (FD_ISSET(vnc_sock, &readfs) && listen_on_vnc) {
+ unsigned char buf[256];
+
+ printf("Activity on VNC socket!\n");
+
+ if (read(vnc_sock, buf, 1) != 1)
+ error("short read on vnc_sock\n");
+
+ switch (buf[0]) {
+ case 0:
+ // frame buffer update!
+ printf("Framebuffer update\n");
+ if (read(vnc_sock, buf, 1) != 1)
+ error("short read on vnc_sock\n");
+ handle_vnc_fbupdate(vnc_sock);
+ break;
+ case 2: // bell
+ printf("\a\n");
+ break;
+ case 3:
+ printf("New text in clipboard, ignoring\n");
+ if (read(vnc_sock, buf, 3) != 3)
+ error("short read on vnc_sock\n");
+ if (read(vnc_sock, buf, 4) != 4)
+ error("short read on vnc_sock\n");
+ if (read(vnc_sock, buf, (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]) != 4)
+ error("short read on vnc_sock\n");
+
+ default:
+ printf("Unknown VNC server message %x\n", buf[0]);
+ exit(1);
+ }
+ }