+struct vnc_rectangle {
+ unsigned short x, y, width, height;
+ unsigned int encoding;
+};
+
+#define SBX 32
+#define SBY 32
+
+void handle_vnc_fbupdate(int vnc_sock)
+{
+ struct vnc_rectangle rect;
+ unsigned char *data, *ptr, *src, *dst;
+ unsigned short num_rect;
+ int ret, data_left, i, xt, yt;
+ unsigned char smallblock[SBX * SBY * 3];
+
+ if (read(vnc_sock, &num_rect, 2) != 2)
+ error("short read on num_rect\n");
+
+ for (i = 0; i < ntohs(num_rect); ++i) {
+ int width, height;
+
+ if (read(vnc_sock, &rect, sizeof(struct vnc_rectangle)) != sizeof(struct vnc_rectangle))
+ error("short read on vnc_rectangle\n");
+
+ 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 + (SBY-1)) / SBY; ++yt) {
+ for (xt = 0; xt < (width + (SBX-1)) / SBX; ++xt) {
+ int x, y;
+ int bw = width - xt * SBX;
+ int bh = height - yt * SBY;
+ if (bw > SBX)
+ bw = SBX;
+ if (bh > SBY)
+ bh = SBY;
+
+ dst = smallblock;
+ for (y = 0; y < bh; ++y) {
+ src = data + ((yt * SBY + (bh - 1 - y)) * width + (xt * SBX)) * 4;
+ for (x = 0; x < bw; ++x) {
+ *dst++ = *src++;
+ *dst++ = *src++;
+ *dst++ = *src++;
+ ++src;
+ }
+ }
+
+ rdp_send_bitmap_update(ntohs(rect.x) + xt * SBX, ntohs(rect.y) + yt * SBY, bw, bh, smallblock);
+ }
+ }
+
+ xfree(data);
+ }
+}
+
+int listen_on_vnc = 0;
+