#include "remote_internal.h" #include #include #include #include #include #include #include #include #include #include #include typedef struct { uint8_t bpp; uint8_t depth; uint8_t big_endian; uint8_t true_colour; uint16_t red_max; uint16_t green_max; uint16_t blue_max; uint8_t red_shift; uint8_t green_shift; uint8_t blue_shift; } RfbPixelFormat; typedef struct { int fd; RemoteApp *app; RfbPixelFormat pf; int supports_raw; int supports_zlib; int supports_tight; int jpeg_quality_idx; /* -1 if client did not advertise -32..-23 */ int supports_desktop_size; int supports_ext_desktop_size; int supports_continuous; int continuous; int update_requested; int force_full; uint16_t req_x, req_y, req_w, req_h; uint32_t client_w, client_h; uint64_t last_seq; uint64_t last_send_ms; uint8_t prev_btn_mask; uint32_t held_mods; int z_init; z_stream z; tjhandle tj; FbReader fb; uint8_t *scratch; size_t scratch_cap; uint8_t *pixbuf; size_t pixbuf_cap; uint8_t *zbuf; size_t zbuf_cap; } RfbClient; static int write_all(int fd, const void *buf, size_t len) { const uint8_t *p = (const uint8_t *)buf; size_t done = 0; while (done < len) { ssize_t w = send(fd, p + done, len - done, MSG_NOSIGNAL); if (w < 0) { if (errno == EINTR) continue; if (errno == EAGAIN || errno == EWOULDBLOCK) { struct pollfd pfd = { .fd = fd, .events = POLLOUT }; if (poll(&pfd, 1, 5000) <= 0) return -1; continue; } return -1; } if (w == 0) return -1; done += (size_t)w; } return 0; } static int read_exact(int fd, void *buf, size_t len) { uint8_t *p = (uint8_t *)buf; size_t got = 0; while (got < len) { ssize_t r = recv(fd, p + got, len - got, 0); if (r < 0) { if (errno == EINTR) continue; if (errno == EAGAIN || errno == EWOULDBLOCK) { struct pollfd pfd = { .fd = fd, .events = POLLIN }; if (poll(&pfd, 1, 10000) <= 0) return -1; continue; } return -1; } if (r == 0) return -1; got += (size_t)r; } return 0; } static void put_be16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)v; } static void put_be32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)(v >> 24); p[1] = (uint8_t)(v >> 16); p[2] = (uint8_t)(v >> 8); p[3] = (uint8_t)v; } static uint16_t get_be16(const uint8_t *p) { return (uint16_t)(((uint16_t)p[0] << 8) | p[1]); } static uint32_t get_be32(const uint8_t *p) { return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3]; } static void encode_pixel_format(uint8_t out[16], const RfbPixelFormat *pf) { out[0] = pf->bpp; out[1] = pf->depth; out[2] = pf->big_endian; out[3] = pf->true_colour; put_be16(out + 4, pf->red_max); put_be16(out + 6, pf->green_max); put_be16(out + 8, pf->blue_max); out[10] = pf->red_shift; out[11] = pf->green_shift; out[12] = pf->blue_shift; out[13] = out[14] = out[15] = 0; } static void decode_pixel_format(const uint8_t in[16], RfbPixelFormat *pf) { pf->bpp = in[0]; pf->depth = in[1]; pf->big_endian = in[2] ? 1 : 0; pf->true_colour = in[3] ? 1 : 0; pf->red_max = get_be16(in + 4); pf->green_max = get_be16(in + 6); pf->blue_max = get_be16(in + 8); pf->red_shift = in[10]; pf->green_shift = in[11]; pf->blue_shift = in[12]; } static int ensure_buf(uint8_t **buf, size_t *cap, size_t need) { if (need <= *cap) return 0; uint8_t *nb = realloc(*buf, need); if (!nb) return -1; *buf = nb; *cap = need; return 0; } static int rfb_send_reason(int fd, const char *msg) { size_t len = strlen(msg); uint8_t hdr[8]; put_be32(hdr, 1u); put_be32(hdr + 4, (uint32_t)len); if (write_all(fd, hdr, 8) < 0) return -1; return write_all(fd, msg, len); } static int rfb_handshake(RfbClient *c) { const char ver[] = "RFB 003.008\n"; if (write_all(c->fd, ver, 12) < 0) return -1; char cver[13]; if (read_exact(c->fd, cver, 12) < 0) return -1; cver[12] = 0; if (strncmp(cver, "RFB ", 4) != 0) return -1; int minor = atoi(cver + 8); RemoteConfig cfg; pthread_mutex_lock(&c->app->cfg_mu); cfg = c->app->cfg; pthread_mutex_unlock(&c->app->cfg_mu); if (minor <= 3) { if (cfg.auth_mode == REMOTE_AUTH_NONE) { uint8_t ok4[4] = { 0, 0, 0, 1 }; if (write_all(c->fd, ok4, 4) < 0) return -1; } else { uint8_t sec4[4] = { 0, 0, 0, 2 }; uint8_t chal[16], resp[16], exp[16]; if (write_all(c->fd, sec4, 4) < 0) return -1; if (RAND_bytes(chal, 16) != 1) return -1; if (write_all(c->fd, chal, 16) < 0) return -1; if (read_exact(c->fd, resp, 16) < 0) return -1; vnc_des_encrypt_challenge(cfg.password, chal, exp); if (memcmp(resp, exp, 16) != 0) { uint8_t fail[4] = { 0, 0, 0, 1 }; write_all(c->fd, fail, 4); return -1; } uint8_t ok[4] = { 0, 0, 0, 0 }; if (write_all(c->fd, ok, 4) < 0) return -1; } } else { uint8_t sec_type = cfg.auth_mode == REMOTE_AUTH_NONE ? 1 : 2; uint8_t list[2] = { 1, sec_type }; if (write_all(c->fd, list, 2) < 0) return -1; uint8_t chosen = 0; if (read_exact(c->fd, &chosen, 1) < 0) return -1; if (chosen != sec_type) { rfb_send_reason(c->fd, "Unsupported security type"); return -1; } if (sec_type == 2) { uint8_t chal[16], resp[16], exp[16]; if (RAND_bytes(chal, 16) != 1) return -1; if (write_all(c->fd, chal, 16) < 0) return -1; if (read_exact(c->fd, resp, 16) < 0) return -1; vnc_des_encrypt_challenge(cfg.password, chal, exp); int match = 0; volatile uint8_t diff = 0; for (int i = 0; i < 16; i++) diff |= (uint8_t)(resp[i] ^ exp[i]); match = (diff == 0); if (!match) { rfb_send_reason(c->fd, "Authentication failed"); return -1; } } uint8_t ok[4] = { 0, 0, 0, 0 }; if (write_all(c->fd, ok, 4) < 0) return -1; } uint8_t shared_flag = 0; if (read_exact(c->fd, &shared_flag, 1) < 0) return -1; (void)shared_flag; /* Query compositor size if shm isn't populated yet */ uint32_t init_w = 540, init_h = 1170; char reply[256]; if (ctl_cmd(&c->app->ctl, "status", reply, sizeof reply) == 0) { unsigned int rw = 0, rh = 0; if (sscanf(reply, "ok %ux%u", &rw, &rh) == 2 && rw > 0 && rh > 0) { init_w = rw; init_h = rh; } } c->client_w = init_w; c->client_h = init_h; uint8_t sinit[24 + 7]; put_be16(sinit, (uint16_t)init_w); put_be16(sinit + 2, (uint16_t)init_h); encode_pixel_format(sinit + 4, &c->pf); put_be32(sinit + 20, 7u); memcpy(sinit + 24, "Chandra", 7); return write_all(c->fd, sinit, sizeof sinit); } static size_t pack_rect_pixels(RfbClient *c, const uint8_t *rgba, uint32_t fb_w, int rx, int ry, int rw, int rh) { int bpp_bytes = (c->pf.bpp == 8) ? 1 : (c->pf.bpp == 16) ? 2 : 4; size_t need = (size_t)rw * (size_t)rh * (size_t)bpp_bytes; if (ensure_buf(&c->pixbuf, &c->pixbuf_cap, need) < 0) return 0; if (bpp_bytes == 4 && !c->pf.big_endian && c->pf.true_colour && c->pf.red_shift == 0 && c->pf.green_shift == 8 && c->pf.blue_shift == 16 && c->pf.red_max == 255 && c->pf.green_max == 255 && c->pf.blue_max == 255) { for (int y = 0; y < rh; y++) { const uint8_t *src = rgba + ((size_t)(ry + y) * (size_t)fb_w + (size_t)rx) * 4; memcpy(c->pixbuf + (size_t)y * (size_t)rw * 4, src, (size_t)rw * 4); } return need; } uint8_t *dst = c->pixbuf; uint32_t rmax = c->pf.red_max ? c->pf.red_max : 255u; uint32_t gmax = c->pf.green_max ? c->pf.green_max : 255u; uint32_t bmax = c->pf.blue_max ? c->pf.blue_max : 255u; for (int y = 0; y < rh; y++) { const uint8_t *src = rgba + ((size_t)(ry + y) * (size_t)fb_w + (size_t)rx) * 4; for (int x = 0; x < rw; x++) { uint32_t r = ((uint32_t)src[x * 4 + 0] * rmax + 127u) / 255u; uint32_t g = ((uint32_t)src[x * 4 + 1] * gmax + 127u) / 255u; uint32_t b = ((uint32_t)src[x * 4 + 2] * bmax + 127u) / 255u; uint32_t v = (r << c->pf.red_shift) | (g << c->pf.green_shift) | (b << c->pf.blue_shift); if (bpp_bytes == 1) { *dst++ = (uint8_t)v; } else if (bpp_bytes == 2) { if (c->pf.big_endian) { dst[0] = (uint8_t)(v >> 8); dst[1] = (uint8_t)v; } else { dst[0] = (uint8_t)v; dst[1] = (uint8_t)(v >> 8); } dst += 2; } else { if (c->pf.big_endian) { dst[0] = (uint8_t)(v >> 24); dst[1] = (uint8_t)(v >> 16); dst[2] = (uint8_t)(v >> 8); dst[3] = (uint8_t)v; } else { dst[0] = (uint8_t)v; dst[1] = (uint8_t)(v >> 8); dst[2] = (uint8_t)(v >> 16); dst[3] = (uint8_t)(v >> 24); } dst += 4; } } } return need; } static size_t encode_tight_compact_len(uint32_t len, uint8_t out[3]) { out[0] = (uint8_t)(len & 0x7Fu); if (len < 128u) return 1; out[0] |= 0x80u; out[1] = (uint8_t)((len >> 7) & 0x7Fu); if (len < 16384u) return 2; out[1] |= 0x80u; out[2] = (uint8_t)((len >> 14) & 0xFFu); return 3; } static int send_rect_raw(RfbClient *c, const uint8_t *rgba, uint32_t fb_w, int rx, int ry, int rw, int rh) { size_t raw_bytes = pack_rect_pixels(c, rgba, fb_w, rx, ry, rw, rh); if (raw_bytes == 0) return -1; uint8_t hdr[12]; put_be16(hdr + 0, (uint16_t)rx); put_be16(hdr + 2, (uint16_t)ry); put_be16(hdr + 4, (uint16_t)rw); put_be16(hdr + 6, (uint16_t)rh); put_be32(hdr + 8, 0u); /* Raw */ if (write_all(c->fd, hdr, 12) < 0) return -1; return write_all(c->fd, c->pixbuf, raw_bytes); } static int send_rect_zlib(RfbClient *c, const uint8_t *rgba, uint32_t fb_w, int rx, int ry, int rw, int rh) { if (!c->z_init) { memset(&c->z, 0, sizeof c->z); if (deflateInit(&c->z, Z_BEST_SPEED) != Z_OK) { return send_rect_raw(c, rgba, fb_w, rx, ry, rw, rh); } c->z_init = 1; } size_t raw_bytes = pack_rect_pixels(c, rgba, fb_w, rx, ry, rw, rh); if (raw_bytes == 0) return -1; uLong bound = deflateBound(&c->z, (uLong)raw_bytes); if (ensure_buf(&c->zbuf, &c->zbuf_cap, (size_t)bound + 64) < 0) return -1; c->z.next_in = c->pixbuf; c->z.avail_in = (uInt)raw_bytes; c->z.next_out = c->zbuf; c->z.avail_out = (uInt)c->zbuf_cap; int zr = deflate(&c->z, Z_SYNC_FLUSH); if (zr != Z_OK || c->z.avail_in != 0) return -1; size_t zlen = c->zbuf_cap - (size_t)c->z.avail_out; uint8_t hdr[16]; put_be16(hdr + 0, (uint16_t)rx); put_be16(hdr + 2, (uint16_t)ry); put_be16(hdr + 4, (uint16_t)rw); put_be16(hdr + 6, (uint16_t)rh); put_be32(hdr + 8, 6u); /* Zlib */ put_be32(hdr + 12, (uint32_t)zlen); if (write_all(c->fd, hdr, 16) < 0) return -1; return write_all(c->fd, c->zbuf, zlen); } static int send_rect_tight_jpeg(RfbClient *c, const uint8_t *rgba, uint32_t fb_w, int rx, int ry, int rw, int rh) { static const int qtbl[10] = { 25, 35, 45, 55, 65, 72, 78, 84, 89, 94 }; if (!c->tj) { c->tj = tjInitCompress(); if (!c->tj) return send_rect_raw(c, rgba, fb_w, rx, ry, rw, rh); } int q = qtbl[c->jpeg_quality_idx >= 0 && c->jpeg_quality_idx <= 9 ? c->jpeg_quality_idx : 7]; const uint8_t *ptr = rgba + ((size_t)ry * (size_t)fb_w + (size_t)rx) * 4; unsigned char *jbuf = NULL; unsigned long jsz = 0; if (tjCompress2(c->tj, ptr, rw, (int)fb_w * 4, rh, TJPF_RGBX, &jbuf, &jsz, TJSAMP_420, q, TJFLAG_FASTDCT) != 0 || !jbuf || jsz == 0) { if (jbuf) tjFree(jbuf); return send_rect_raw(c, rgba, fb_w, rx, ry, rw, rh); } uint8_t hdr[16]; put_be16(hdr + 0, (uint16_t)rx); put_be16(hdr + 2, (uint16_t)ry); put_be16(hdr + 4, (uint16_t)rw); put_be16(hdr + 6, (uint16_t)rh); put_be32(hdr + 8, 7u); /* Tight */ hdr[12] = 0x90u; /* Tight JPEG subencoding */ size_t clen = encode_tight_compact_len((uint32_t)jsz, hdr + 13); int rc = write_all(c->fd, hdr, 13 + clen); if (rc == 0) rc = write_all(c->fd, jbuf, (size_t)jsz); tjFree(jbuf); return rc; } static int can_use_tight_jpeg(const RfbClient *c) { return c->supports_tight && c->jpeg_quality_idx >= 0 && c->pf.true_colour && (c->pf.bpp == 32 || c->pf.bpp == 16); } static int send_pixel_rect(RfbClient *c, const uint8_t *rgba, uint32_t fb_w, int rx, int ry, int rw, int rh) { if (can_use_tight_jpeg(c)) return send_rect_tight_jpeg(c, rgba, fb_w, rx, ry, rw, rh); if (c->supports_zlib && c->pf.true_colour) return send_rect_zlib(c, rgba, fb_w, rx, ry, rw, rh); return send_rect_raw(c, rgba, fb_w, rx, ry, rw, rh); } static int send_frame_update(RfbClient *c, const uint8_t *rgba, uint32_t fb_w, uint32_t fb_h, int rx, int ry, int rw, int rh, int need_resize) { int use_ext = need_resize && c->supports_ext_desktop_size; int use_ds = need_resize && !use_ext && c->supports_desktop_size; /* Count pixel tiles (Tight JPEG caps tile width/height at 2048). */ int max_tile = can_use_tight_jpeg(c) ? 2048 : 4096; int n_tiles_x = (rw + max_tile - 1) / max_tile; int n_tiles_y = (rh + max_tile - 1) / max_tile; int n_pixel_rects = (rw > 0 && rh > 0) ? (n_tiles_x * n_tiles_y) : 0; int nrects = n_pixel_rects + ((use_ext || use_ds) ? 1 : 0); if (nrects <= 0) return 0; uint8_t u_hdr[4] = { 0, 0, (uint8_t)(nrects >> 8), (uint8_t)nrects }; if (write_all(c->fd, u_hdr, 4) < 0) return -1; if (use_ext) { uint8_t r[32]; memset(r, 0, sizeof r); put_be16(r + 0, 0); put_be16(r + 2, 0); put_be16(r + 4, (uint16_t)fb_w); put_be16(r + 6, (uint16_t)fb_h); put_be32(r + 8, (uint32_t)(-308)); /* ExtendedDesktopSize */ r[12] = 1; /* number-of-screens */ put_be32(r + 16, 1u); /* screen id */ put_be16(r + 24, (uint16_t)fb_w); put_be16(r + 26, (uint16_t)fb_h); if (write_all(c->fd, r, 32) < 0) return -1; } else if (use_ds) { uint8_t r[12]; put_be16(r + 0, 0); put_be16(r + 2, 0); put_be16(r + 4, (uint16_t)fb_w); put_be16(r + 6, (uint16_t)fb_h); put_be32(r + 8, (uint32_t)(-223)); /* DesktopSize */ if (write_all(c->fd, r, 12) < 0) return -1; } for (int ty = 0; ty < rh; ty += max_tile) { int th = (ty + max_tile <= rh) ? max_tile : (rh - ty); for (int tx = 0; tx < rw; tx += max_tile) { int tw = (tx + max_tile <= rw) ? max_tile : (rw - tx); if (send_pixel_rect(c, rgba, fb_w, rx + tx, ry + ty, tw, th) < 0) return -1; } } return 0; } static void rfb_send_mods(RfbClient *c, uint32_t mods) { char cmd[64]; snprintf(cmd, sizeof cmd, "input-mods %u 0 0 0", mods); ctl_fire(&c->app->ctl, cmd); } static int rfb_handle_message(RfbClient *c) { uint8_t msg_type = 0; if (read_exact(c->fd, &msg_type, 1) < 0) return -1; switch (msg_type) { case 0: { /* SetPixelFormat */ uint8_t buf[19]; if (read_exact(c->fd, buf, 19) < 0) return -1; decode_pixel_format(buf + 3, &c->pf); c->force_full = 1; break; } case 2: { /* SetEncodings */ uint8_t buf[3]; if (read_exact(c->fd, buf, 3) < 0) return -1; uint16_t n = get_be16(buf + 1); c->supports_raw = 0; c->supports_zlib = 0; c->supports_tight = 0; c->jpeg_quality_idx = -1; c->supports_desktop_size = 0; c->supports_ext_desktop_size = 0; c->supports_continuous = 0; for (uint16_t i = 0; i < n; i++) { uint8_t eb[4]; if (read_exact(c->fd, eb, 4) < 0) return -1; int32_t enc = (int32_t)get_be32(eb); if (enc == 0) c->supports_raw = 1; else if (enc == 6) c->supports_zlib = 1; else if (enc == 7) c->supports_tight = 1; else if (enc == -223) c->supports_desktop_size = 1; else if (enc == -308) c->supports_ext_desktop_size = 1; else if (enc == -313) c->supports_continuous = 1; else if (enc >= -32 && enc <= -23) { int qi = enc - (-32); if (qi > c->jpeg_quality_idx) c->jpeg_quality_idx = qi; } } break; } case 3: { /* FramebufferUpdateRequest */ uint8_t buf[9]; if (read_exact(c->fd, buf, 9) < 0) return -1; uint8_t inc = buf[0]; c->req_x = get_be16(buf + 1); c->req_y = get_be16(buf + 3); c->req_w = get_be16(buf + 5); c->req_h = get_be16(buf + 7); if (!inc) c->force_full = 1; c->update_requested = 1; break; } case 4: { /* KeyEvent */ uint8_t buf[7]; if (read_exact(c->fd, buf, 7) < 0) return -1; int down = buf[0] != 0; uint32_t sym = get_be32(buf + 3); uint32_t ev = 0, uc = 0; int sh = 0, mod_bit = 0; if (!keysym_to_evdev(sym, &ev, &sh, &mod_bit, &uc)) break; if (uc != 0) { if (down) { uint8_t u8[8]; size_t n = 0; if (uc < 0x80u) { u8[n++] = (uint8_t)uc; } else if (uc < 0x800u) { u8[n++] = (uint8_t)(0xC0u | (uc >> 6)); u8[n++] = (uint8_t)(0x80u | (uc & 0x3Fu)); } else if (uc < 0x10000u) { u8[n++] = (uint8_t)(0xE0u | (uc >> 12)); u8[n++] = (uint8_t)(0x80u | ((uc >> 6) & 0x3Fu)); u8[n++] = (uint8_t)(0x80u | (uc & 0x3Fu)); } else { u8[n++] = (uint8_t)(0xF0u | (uc >> 18)); u8[n++] = (uint8_t)(0x80u | ((uc >> 12) & 0x3Fu)); u8[n++] = (uint8_t)(0x80u | ((uc >> 6) & 0x3Fu)); u8[n++] = (uint8_t)(0x80u | (uc & 0x3Fu)); } char cmd[48]; memcpy(cmd, "input-text ", 11); hex_encode(u8, n, cmd + 11); ctl_fire(&c->app->ctl, cmd); } break; } if (mod_bit) { if (down) c->held_mods |= (uint32_t)mod_bit; else c->held_mods &= ~(uint32_t)mod_bit; rfb_send_mods(c, c->held_mods); char cmd[48]; snprintf(cmd, sizeof cmd, "input-key %u %d", ev, down); ctl_fire(&c->app->ctl, cmd); break; } uint32_t eff_mods = c->held_mods | (sh ? 1u : 0u); if (eff_mods != c->held_mods) rfb_send_mods(c, eff_mods); char cmd[48]; snprintf(cmd, sizeof cmd, "input-key %u %d", ev, down); ctl_fire(&c->app->ctl, cmd); if (eff_mods != c->held_mods) rfb_send_mods(c, c->held_mods); break; } case 5: { /* PointerEvent */ uint8_t buf[5]; if (read_exact(c->fd, buf, 5) < 0) return -1; uint8_t mask = buf[0]; uint16_t x = get_be16(buf + 1); uint16_t y = get_be16(buf + 3); char cmd[64]; snprintf(cmd, sizeof cmd, "input-ptr %u %u", x, y); ctl_fire(&c->app->ctl, cmd); uint8_t diff = (uint8_t)(mask ^ c->prev_btn_mask); if (diff & 0x01u) { snprintf(cmd, sizeof cmd, "input-btn 272 %d", (mask & 0x01u) ? 1 : 0); ctl_fire(&c->app->ctl, cmd); } if (diff & 0x02u) { snprintf(cmd, sizeof cmd, "input-btn 274 %d", (mask & 0x02u) ? 1 : 0); ctl_fire(&c->app->ctl, cmd); } if (diff & 0x04u) { snprintf(cmd, sizeof cmd, "input-btn 273 %d", (mask & 0x04u) ? 1 : 0); ctl_fire(&c->app->ctl, cmd); } if ((diff & 0x08u) && (mask & 0x08u)) ctl_fire(&c->app->ctl, "input-scroll 0 -30 -1"); if ((diff & 0x10u) && (mask & 0x10u)) ctl_fire(&c->app->ctl, "input-scroll 0 30 1"); if ((diff & 0x20u) && (mask & 0x20u)) ctl_fire(&c->app->ctl, "input-scroll 1 -30 -1"); if ((diff & 0x40u) && (mask & 0x40u)) ctl_fire(&c->app->ctl, "input-scroll 1 30 1"); c->prev_btn_mask = mask; break; } case 6: { /* ClientCutText */ uint8_t hdr[7]; if (read_exact(c->fd, hdr, 7) < 0) return -1; uint32_t len = get_be32(hdr + 3); uint32_t take = len > 32768u ? 32768u : len; uint8_t *txt = take ? malloc(take) : NULL; if (take && !txt) return -1; if (take && read_exact(c->fd, txt, take) < 0) { free(txt); return -1; } uint32_t rem = len - take; uint8_t drain[512]; while (rem > 0) { size_t chunk = rem > sizeof drain ? sizeof drain : rem; if (read_exact(c->fd, drain, chunk) < 0) { free(txt); return -1; } rem -= (uint32_t)chunk; } if (take > 0) { size_t clen = take > 4096u ? 4096u : take; char *cmd = malloc(16 + clen * 2 + 1); if (cmd) { memcpy(cmd, "clipboard-set ", 14); hex_encode(txt, clen, cmd + 14); ctl_fire(&c->app->ctl, cmd); free(cmd); } } free(txt); break; } case 150: { /* EnableContinuousUpdates */ uint8_t buf[9]; if (read_exact(c->fd, buf, 9) < 0) return -1; c->continuous = buf[0] != 0; if (!c->continuous && c->supports_continuous) { uint8_t eoc = 150; write_all(c->fd, &eoc, 1); } break; } case 251: { /* SetDesktopSize (consume gracefully) */ uint8_t hdr[7]; if (read_exact(c->fd, hdr, 7) < 0) return -1; uint8_t nscreens = hdr[5]; for (int i = 0; i < (int)nscreens; i++) { uint8_t sc[16]; if (read_exact(c->fd, sc, 16) < 0) return -1; } break; } default: return -1; } return 0; } void rfb_handle_client(RemoteApp *app, int fd) { RfbClient *c = calloc(1, sizeof *c); if (!c) { close(fd); return; } c->fd = fd; c->app = app; c->pf.bpp = 32; c->pf.depth = 24; c->pf.big_endian = 0; c->pf.true_colour = 1; c->pf.red_max = 255; c->pf.green_max = 255; c->pf.blue_max = 255; c->pf.red_shift = 0; c->pf.green_shift = 8; c->pf.blue_shift = 16; c->jpeg_quality_idx = -1; c->supports_raw = 1; fb_reader_init(&c->fb, app->shm_path); fb_reader_ping(&c->fb); ctl_fire(&app->ctl, "refresh"); if (rfb_handshake(c) < 0) goto done; uint64_t last_ping = 0; while (*app->running) { uint64_t now = remote_now_ms(); if (now - last_ping >= 250) { fb_reader_ping(&c->fb); last_ping = now; } struct pollfd pfd = { .fd = c->fd, .events = POLLIN }; int pr = poll(&pfd, 1, 16); if (pr < 0) { if (errno == EINTR) continue; break; } if (pr > 0) { if (pfd.revents & (POLLHUP | POLLERR)) break; if (pfd.revents & POLLIN) { if (rfb_handle_message(c) < 0) break; /* Drain any immediately queued client requests before sending next frame. */ while (*app->running) { struct pollfd p2 = { .fd = c->fd, .events = POLLIN }; if (poll(&p2, 1, 0) <= 0 || !(p2.revents & POLLIN)) break; if (rfb_handle_message(c) < 0) goto done; } } } if (!c->update_requested && !c->continuous) continue; RemoteConfig cfg; pthread_mutex_lock(&app->cfg_mu); cfg = app->cfg; pthread_mutex_unlock(&app->cfg_mu); uint64_t min_gap = (uint64_t)(1000 / (cfg.max_fps > 0 ? cfg.max_fps : 30)); now = remote_now_ms(); if (!c->force_full && now - c->last_send_ms < min_gap) continue; uint64_t seq_before = c->force_full ? 0 : c->last_seq; int full = 0, x0 = 0, y0 = 0, x1 = 0, y1 = 0; uint32_t fb_w = 0, fb_h = 0; uint64_t new_seq = 0; int got = fb_reader_grab(&c->fb, seq_before, c->client_w, c->client_h, &full, &fb_w, &fb_h, &x0, &y0, &x1, &y1, &new_seq, &c->scratch, &c->scratch_cap); if (got <= 0) { if (c->force_full && c->scratch && c->client_w > 0 && c->client_h > 0 && fb_reader_grab(&c->fb, 0, c->client_w, c->client_h, &full, &fb_w, &fb_h, &x0, &y0, &x1, &y1, &new_seq, &c->scratch, &c->scratch_cap) > 0) { got = 1; } else { continue; } } int need_resize = (fb_w != c->client_w || fb_h != c->client_h); if (need_resize || c->force_full || full) { x0 = 0; y0 = 0; x1 = (int)fb_w; y1 = (int)fb_h; } if (need_resize && !c->supports_ext_desktop_size && !c->supports_desktop_size) { /* Client didn't advertise resize support; clip to its buffer size. */ if (x1 > (int)c->client_w) x1 = (int)c->client_w; if (y1 > (int)c->client_h) y1 = (int)c->client_h; } if (!c->force_full && !need_resize && c->req_w > 0 && c->req_h > 0 && !c->continuous) { int rx0 = c->req_x, ry0 = c->req_y; int rx1 = rx0 + c->req_w, ry1 = ry0 + c->req_h; if (x0 < rx0) x0 = rx0; if (y0 < ry0) y0 = ry0; if (x1 > rx1) x1 = rx1; if (y1 > ry1) y1 = ry1; } int rw = x1 - x0; int rh = y1 - y0; if (rw <= 0 || rh <= 0) { c->last_seq = new_seq; c->force_full = 0; continue; } if (send_frame_update(c, c->scratch, fb_w, fb_h, x0, y0, rw, rh, need_resize) < 0) break; c->client_w = fb_w; c->client_h = fb_h; c->last_seq = new_seq; c->last_send_ms = now; c->force_full = 0; c->update_requested = 0; } done: if (c->held_mods) rfb_send_mods(c, 0u); if (c->prev_btn_mask & 0x01u) ctl_fire(&app->ctl, "input-btn 272 0"); if (c->prev_btn_mask & 0x02u) ctl_fire(&app->ctl, "input-btn 274 0"); if (c->prev_btn_mask & 0x04u) ctl_fire(&app->ctl, "input-btn 273 0"); if (c->z_init) deflateEnd(&c->z); if (c->tj) tjDestroy(c->tj); fb_reader_close(&c->fb); free(c->scratch); free(c->pixbuf); free(c->zbuf); close(c->fd); free(c); }