Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

45.3 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Wayland backend: on Wayland no client may read or inject global input,
// so the helper goes under the compositor instead: it reads the keyboards, mice and touchpads
// from /dev/input (the `input` group, set up once from Setup), grabs them (EVIOCGRAB) while this
// computer drives the other one, and injects through two uinput devices (/dev/uinput): an absolute
// pointer (moves land exactly where we say, no second acceleration) and a keyboard + relative mouse
// for buttons, wheel and keys. Same commands and events as the X11 backend (main.c, capture.c).
//
// The compositor never tells anyone where the pointer is, so we follow it: our own moves are exact,
// the user's are dead-reckoned from the mice's deltas with libinput's acceleration (the desktop's
// profile and speed, kvmConfig accelProfile/accelSpeed), clamped to the monitors (which main sends
// with `screens`). Pinned against an edge both agree again, which is where the edge push is
// measured. Ahead of the real pointer, a quick flick crossed before it reached the edge.
#include "helper.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/input.h>
#include <linux/uinput.h>
// Kernel headers before 4.16 (the old-glibc release build, #149) name these time.tv_sec / tv_usec.
#ifndef input_event_sec
#define input_event_sec time.tv_sec
#define input_event_usec time.tv_usec
#endif

#define NAME_PREFIX "TwinDesk virtual"
#define ABS_MAXV 65535
#define MAX_IN 32

enum { K_MOUSE = 1, K_PAD = 2, K_KBD = 4 };
typedef struct {
  int fd, kind, grabbed;
  char path[300];
  int absX, absY, touching, hasLast; // touchpad: single-finger motion
  double lastX, lastY, scaleX, scaleY;
  // Touchpad gestures while grabbed (#222): libinput doesn't see the touchpad then, so its taps and
  // two-finger scrolling are made here (pad_frame).
  int tools, fingers, slot, mtX[2], mtY[2], mtOn[2];
  int tapAlive, tapMax, tapPressed;
  double tapStart, tapMove;
  int twoMode, twoHas; // 0 not decided yet, 1 scroll, 2 pinch
  double twoCx, twoCy, twoDist, twoAccC, twoAccD;
} In;

static In ins[MAX_IN];
static int nin;
static int uabs = -1, urel = -1;
static double lastScan;

// config (kvmConfig), as capture.c
static int side = SIDE_NONE;
static double resistancePx = 120;
static int hotkeyEvs[2] = { 100, -1 };
static int driveOther, edgeKeyboard = 1;
static double deadmanMs = 1500, doubleTapMs = 400;
static double speed = 1.0;
static int accelFlat;          // the desktop's pointer profile is flat (no acceleration)
static double accelSpeed;       // the desktop's pointer speed, libinput's -1..1
static int padTap = 1, padNatural; // the desktop's touchpad: tap to click, natural scrolling (kvmConfig)
// Drags of files across the edge (wlstrip.c): kvmConfig dragFiles (our switches), dragSideList (the
// sides whose device takes drags; absent: every watched side).
static int dragFiles = 1, dragSides[4], nDragSides = -1;
static void strip_tick(void);

// state
static double px, py;                 // where we think the pointer is (logical px)
static int capturing, capMouse, capKeys, wantCapture, wantMouse, wantKeys;
static double wantSince, lastCmd;
static unsigned char localKeys[256], localButtons[8];
static unsigned char sentKeys[256], sentButtons[8];
static double push, lastPush, pushSince;
static int retArmed, retSide = SIDE_NONE;
static double retResistance, retPush, retLast, retSince;
// Several watched sides (Multi-device, #225): kvmConfig / armReturn `sideList` ("left,top": our JSON
// reader skips arrays), else the one `side`. Each side has its own push; events name the side.
static int edgeSides[4], nEdgeSides, retSides[4], nRetSides;
static double pushBy[4], lastPushBy[4], pushSinceBy[4], retPushBy[4], retLastBy[4], retSinceBy[4];

static int parse_side_list(const char *list, int fallback, int *out) {
  int n = 0;
  if (!list) {
    if (fallback != SIDE_NONE) out[n++] = fallback;
    return n;
  }
  char buf[64];
  snprintf(buf, sizeof buf, "%s", list);
  for (char *tok = strtok(buf, ","); tok && n < 4; tok = strtok(NULL, ",")) {
    int s = side_of(tok), dup = 0;
    for (int i = 0; i < n; i++) if (out[i] == s) dup = 1;
    if (s != SIDE_NONE && !dup) out[n++] = s;
  }
  return n;
}
// A push crosses once it went the resistance outward and lasted dwellMs (kvmConfig dwellMs,
// KVM_EDGE_DWELL_MS; none with no resistance): the end of a quick flick that reaches the edge is
// over by then, a push meant to cross goes on.
static double dwellMs = 150;
static int dwelt(double resistance, double since, double now) { return resistance <= 0 || now - since >= dwellMs; }
// Cooldown and arrival guard, as in capture.c.
#define GUARD_MS 500
static double cooldownMs = 250, lastCross;
static int arrivalSide = SIDE_NONE;
static double arrivalAt, arrivalInward;
static double edgeZ[MAX_ZONES][3], retZ[MAX_ZONES][3]; // zones, as in capture.c
static int nEdgeZ = -1, nRetZ = -1;
static int hkDown;
static double lastTapUp = -1;
static int hkPending = -1;
static unsigned char hkRemote[256];
static unsigned char injKeys[256], injButtons[8];
static double vrem, hrem, lastMoveAt;

static void forward(const char *k) {
#ifdef TDFAST
  char fbuf[4200];
  k = lane_fence(k, fbuf, sizeof fbuf); // a click, key or wheel: its event number and fence (lane.c)
#endif
  JOut o;
  jo_begin(&o);
  jo_str(&o, "event", "ev");
  jo_raw(&o, "ev", k);
  emit(jo_end(&o));
}

static int test_bit(const unsigned long *bits, int n) {
  return (bits[n / (8 * sizeof(long))] >> (n % (8 * sizeof(long)))) & 1;
}

// ---- uinput ----

static void uemit(int fd, int type, int code, int value) {
  if (fd < 0) return;
  struct input_event ie;
  memset(&ie, 0, sizeof ie);
  ie.type = (unsigned short)type;
  ie.code = (unsigned short)code;
  ie.value = value;
  if (write(fd, &ie, sizeof ie) < 0) { /* full: dropped */ }
}

static void usyn(int fd) { uemit(fd, EV_SYN, SYN_REPORT, 0); }

static int make_device(int absolute) {
  int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK | O_CLOEXEC);
  if (fd < 0) return -1;
  ioctl(fd, UI_SET_EVBIT, EV_KEY);
  ioctl(fd, UI_SET_EVBIT, EV_SYN);
  ioctl(fd, UI_SET_KEYBIT, BTN_LEFT);
  ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT);
  ioctl(fd, UI_SET_KEYBIT, BTN_MIDDLE);
  if (absolute) {
    ioctl(fd, UI_SET_EVBIT, EV_ABS);
    ioctl(fd, UI_SET_ABSBIT, ABS_X);
    ioctl(fd, UI_SET_ABSBIT, ABS_Y);
  } else {
    ioctl(fd, UI_SET_KEYBIT, BTN_SIDE);
    ioctl(fd, UI_SET_KEYBIT, BTN_EXTRA);
    for (int k = 1; k < 249; k++) ioctl(fd, UI_SET_KEYBIT, k);
    ioctl(fd, UI_SET_EVBIT, EV_REL);
    ioctl(fd, UI_SET_RELBIT, REL_X);
    ioctl(fd, UI_SET_RELBIT, REL_Y);
    ioctl(fd, UI_SET_RELBIT, REL_WHEEL);
    ioctl(fd, UI_SET_RELBIT, REL_HWHEEL);
  }
  struct uinput_setup us;
  memset(&us, 0, sizeof us);
  us.id.bustype = BUS_VIRTUAL;
  us.id.vendor = 0x1209;
  us.id.product = absolute ? 0x7d01 : 0x7d02;
  snprintf(us.name, sizeof us.name, NAME_PREFIX " %s", absolute ? "pointer" : "keyboard");
  if (ioctl(fd, UI_DEV_SETUP, &us) < 0) { close(fd); return -1; }
  if (absolute) {
    for (int a = 0; a < 2; a++) {
      struct uinput_abs_setup ab;
      memset(&ab, 0, sizeof ab);
      ab.code = a ? ABS_Y : ABS_X;
      ab.absinfo.minimum = 0;
      ab.absinfo.maximum = ABS_MAXV;
      ioctl(fd, UI_ABS_SETUP, &ab);
    }
  }
  if (ioctl(fd, UI_DEV_CREATE) < 0) { close(fd); return -1; }
  return fd;
}

// ---- reading /dev/input ----

static void close_in(int i) {
  if (ins[i].grabbed) ioctl(ins[i].fd, EVIOCGRAB, 0);
  close(ins[i].fd);
  ins[i] = ins[--nin];
}

static int known(const char *path) {
  for (int i = 0; i < nin; i++) if (!strcmp(ins[i].path, path)) return 1;
  return 0;
}

// Nodes scan() looked at and left (power button, lid, jacks, our own devices…), by path and device
// number: not opened again. Closing an evdev node waits for an RCU grace period, so reopening some
// twenty of them every scan stalled input for up to half a second every 3 s (#84). A node whose
// device number changed (another device on that path after a hotplug) is looked at again.
#define MAX_SKIP 128
static struct { char path[64]; dev_t rdev; } skip[MAX_SKIP];
static int nskip;

static int skipped(const char *path, dev_t rdev) {
  for (int i = 0; i < nskip; i++) if (skip[i].rdev == rdev && !strcmp(skip[i].path, path)) return 1;
  return 0;
}

static void reject(const char *path, dev_t rdev) {
  for (int i = 0; i < nskip; i++) if (!strcmp(skip[i].path, path)) { skip[i].rdev = rdev; return; }
  if (nskip == MAX_SKIP) return; // full: such a node is only looked at again (as before)
  snprintf(skip[nskip].path, sizeof skip[nskip].path, "%s", path);
  skip[nskip++].rdev = rdev;
}

static void scan(void) {
  lastScan = now_ms();
  DIR *d = opendir("/dev/input");
  if (!d) return;
  struct dirent *e;
  while ((e = readdir(d)) && nin < MAX_IN) {
    if (strncmp(e->d_name, "event", 5)) continue;
    char path[300];
    snprintf(path, sizeof path, "/dev/input/%s", e->d_name);
    if (known(path)) continue;
    struct stat st;
    const int seen = stat(path, &st) == 0; // its device number, to know it again
    if (seen && skipped(path, st.st_rdev)) continue;
    int fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
    if (fd < 0) continue; // no permission yet (it can come after the node): tried again next scan
    char name[256] = "";
    ioctl(fd, EVIOCGNAME(sizeof name), name);
    if (!strncmp(name, NAME_PREFIX, strlen(NAME_PREFIX))) { if (seen) reject(path, st.st_rdev); close(fd); continue; } // ours (rule 4)
    unsigned long ev[(EV_CNT + 63) / 64] = { 0 }, key[(KEY_CNT + 63) / 64] = { 0 }, rel[(REL_CNT + 63) / 64] = { 0 }, abs[(ABS_CNT + 63) / 64] = { 0 };
    ioctl(fd, EVIOCGBIT(0, sizeof ev), ev);
    ioctl(fd, EVIOCGBIT(EV_KEY, sizeof key), key);
    ioctl(fd, EVIOCGBIT(EV_REL, sizeof rel), rel);
    ioctl(fd, EVIOCGBIT(EV_ABS, sizeof abs), abs);
    int kind = 0;
    if (test_bit(ev, EV_REL) && test_bit(rel, REL_X) && test_bit(rel, REL_Y)) kind |= K_MOUSE;
    if (test_bit(ev, EV_ABS) && test_bit(abs, ABS_X) && test_bit(key, BTN_TOOL_FINGER) && !test_bit(key, BTN_TOOL_PEN)) kind |= K_PAD;
    if (test_bit(ev, EV_KEY) && test_bit(key, KEY_A) && test_bit(key, KEY_Z) && test_bit(key, KEY_SPACE)) kind |= K_KBD;
    if (!kind) { if (seen) reject(path, st.st_rdev); close(fd); continue; }
    In *in = &ins[nin++];
    memset(in, 0, sizeof *in);
    in->fd = fd;
    in->kind = kind;
    snprintf(in->path, sizeof in->path, "%s", path);
    if (kind & K_PAD) { // units → px: about 4 px per mm of finger travel
      struct input_absinfo ax, ay;
      in->scaleX = in->scaleY = 0.5;
      if (!ioctl(fd, EVIOCGABS(ABS_X), &ax) && ax.resolution > 0) in->scaleX = 4.0 / ax.resolution;
      if (!ioctl(fd, EVIOCGABS(ABS_Y), &ay) && ay.resolution > 0) in->scaleY = 4.0 / ay.resolution;
      struct input_absinfo sl;
      if (!ioctl(fd, EVIOCGABS(ABS_MT_SLOT), &sl)) in->slot = sl.value; // a pad says the slot only when it changes
    }
    if (capturing) { ioctl(fd, EVIOCGRAB, 1); in->grabbed = 1; }
    fprintf(stderr, "input: %s (%s%s%s)\n", name, kind & K_MOUSE ? "mouse " : "", kind & K_PAD ? "touchpad " : "", kind & K_KBD ? "keyboard" : "");
  }
  closedir(d);
}

static void grab_all(int on) {
  for (int i = 0; i < nin; i++) {
    if (ins[i].grabbed == on) continue;
    if (ioctl(ins[i].fd, EVIOCGRAB, on ? 1 : 0) == 0 || !on) ins[i].grabbed = on;
  }
}

// ---- injection ----

static void abs_in(double x, double y, int bx, int by, int bw, int bh) {
  int ax = (int)lround((x - bx) * ABS_MAXV / (bw > 1 ? bw - 1 : 1));
  int ay = (int)lround((y - by) * ABS_MAXV / (bh > 1 ? bh - 1 : 1));
  uemit(uabs, EV_ABS, ABS_X, ax < 0 ? 0 : ax > ABS_MAXV ? ABS_MAXV : ax);
  uemit(uabs, EV_ABS, ABS_Y, ay < 0 ? 0 : ay > ABS_MAXV ? ABS_MAXV : ay);
  usyn(uabs);
}

// Where an absolute pointer's 0..65535 lands. Most compositors spread it over all the monitors (the
// box around them). COSMIC (#244) puts it on the monitor the cursor is on now, so the box's far side
// meant that monitor's edge and the next monitor was never reached. There the position is told
// within the cursor's monitor, and going to another one is first a step across their border with
// the relative device (a relative move lands on whichever monitor it reaches, as a mouse's does),
// then the place on the new one. TWINDESK_POINTER=output|box sets it on any desktop.
static int per_output = -1;
static double lastX = -1, lastY = -1; // where the last move put the cursor

static int abs_per_output(void) {
  if (per_output < 0) {
    const char *e = getenv("TWINDESK_POINTER");
    const char *d = getenv("XDG_CURRENT_DESKTOP");
    per_output = e && *e ? !strcmp(e, "output") : (d && (strstr(d, "COSMIC") || strstr(d, "cosmic")));
    if (per_output) fprintf(stderr, "[wl] pointer: placed within the cursor's monitor (%s)\n", e && *e ? "TWINDESK_POINTER" : "COSMIC");
  }
  return per_output;
}

static void abs_to(double x, double y) {
  int mx, my, mw, mh, ox, oy, ow, oh;
  if (abs_per_output() && screens_monitor((int)x, (int)y, &mx, &my, &mw, &mh)) {
    if (lastX >= 0 && screens_monitor((int)lastX, (int)lastY, &ox, &oy, &ow, &oh) && (ox != mx || oy != my || ow != mw || oh != mh)) {
      // To this monitor's side that faces the target, then across it.
      double ex = x < ox ? ox : x > ox + ow - 1 ? ox + ow - 1 : x;
      double ey = y < oy ? oy : y > oy + oh - 1 ? oy + oh - 1 : y;
      abs_in(ex, ey, ox, oy, ow, oh);
      int sx = x < ox ? -8 : x > ox + ow - 1 ? 8 : 0;
      int sy = y < oy ? -8 : y > oy + oh - 1 ? 8 : 0;
      if (sx) uemit(urel, EV_REL, REL_X, sx);
      if (sy) uemit(urel, EV_REL, REL_Y, sy);
      usyn(urel);
    }
    abs_in(x, y, mx, my, mw, mh);
  } else {
    int bx, by, bw, bh;
    screens_box(&bx, &by, &bw, &bh);
    abs_in(x, y, bx, by, bw, bh);
  }
  lastX = x;
  lastY = y;
}

void wl_move_to(int x, int y) {
  int cx, cy;
  screens_clamp(x, y, (int)px, (int)py, &cx, &cy);
  px = cx; py = cy;
  abs_to(px, py);
}

static void feed_return(double dx, double dy);
void wl_forward(const char *k) { forward(k); }
int wl_mouse(void) { return capturing && capMouse; }

// The pointer came here (back home, or placed by the driver) through `s`, or by the hotkey
// (SIDE_NONE): starts the cooldown and the arrival guard, and clears every push.
void wl_arrived(int s) {
  lastCross = now_ms();
  arrivalSide = s; arrivalAt = lastCross; arrivalInward = 0;
  push = retPush = 0;
  memset(pushBy, 0, sizeof pushBy);
  memset(retPushBy, 0, sizeof retPushBy);
}

// 0 while a crossing through `s` has to wait (cooldown, arrival guard).
static int may_cross(int s, double outward) {
  double now = now_ms();
  if (now - lastCross < cooldownMs) return 0;
  if (arrivalSide != SIDE_NONE && arrivalSide == s) {
    if (outward < 0) arrivalInward -= outward;
    if (now - arrivalAt < GUARD_MS && arrivalInward < 8) return 0;
    arrivalSide = SIDE_NONE;
  }
  return 1;
}

static void inj_move(double dx, double dy) {
  int ix = (int)lround(px + dx), iy = (int)lround(py + dy), cx, cy;
  if (!portals_walk_stop((int)px, (int)py, ix, iy, &cx, &cy)) screens_walk(ix, iy, (int)px, (int)py, &retSide, retArmed && !portals_returning() ? 1 : 0, &cx, &cy);
  px = (cx == ix) ? px + dx : cx;
  py = (cy == iy) ? py + dy : cy;
  abs_to(px, py);
  lastMoveAt = now_ms();
  feed_return(dx, dy);
}

static const int BTN[] = { BTN_LEFT, BTN_RIGHT, BTN_MIDDLE, BTN_SIDE, BTN_EXTRA };

static void inj_button(int b, int down) {
  if (b < 0 || b > 4) return;
  injButtons[b] = (unsigned char)down;
  uemit(urel, EV_KEY, BTN[b], down);
  usyn(urel);
}

static void inj_wheel(double dx, double dy, int pixel) {
  double k = pixel ? 1.0 / 40 : 1.0;
  vrem += dy * k;
  hrem += dx * k;
  int v = 0, h = 0;
  while (vrem >= 1) { v--; vrem -= 1; } // DOM +dy = content down = REL_WHEEL -1
  while (vrem <= -1) { v++; vrem += 1; }
  while (hrem >= 1) { h++; hrem -= 1; }
  while (hrem <= -1) { h--; hrem += 1; }
  if (v) uemit(urel, EV_REL, REL_WHEEL, v);
  if (h) uemit(urel, EV_REL, REL_HWHEEL, h);
  if (v || h) usyn(urel);
}

static void inj_key(const char *code, int down) {
  int ev = key_evdev(code);
  if (ev < 1 || ev > 248) return;
  injKeys[ev] = (unsigned char)down;
  uemit(urel, EV_KEY, ev, down);
  usyn(urel);
}

void wl_button(int b, int down) { inj_button(b, down); }

void wl_pen_moved(int x, int y) {
  int cx, cy;
  screens_clamp(x, y, (int)px, (int)py, &cx, &cy);
  px = cx; py = cy;
}

void wl_release_all(void) {
  for (int i = 0; i < 256; i++) if (injKeys[i]) { injKeys[i] = 0; uemit(urel, EV_KEY, i, 0); }
  for (int b = 0; b < 5; b++) if (injButtons[b]) { injButtons[b] = 0; uemit(urel, EV_KEY, BTN[b], 0); }
  usyn(urel);
  vrem = hrem = 0;
}

// A relative move from the input lane (lane.c): no JSON per move.
void wl_move_rel(double dx, double dy) {
  if (crossing_blocked()) return;
  inj_move(dx, dy);
}

void wl_inject(const JObj *ev) {
  if (crossing_blocked()) return; // the emergency shortcut, until main ended the session
  const char *t = jstr(ev, "t", "");
  if (!strcmp(t, "k.mm")) inj_move(jnum(ev, "dx", 0), jnum(ev, "dy", 0));
  else if (!strcmp(t, "k.md")) inj_button((int)jnum(ev, "b", -1), 1);
  else if (!strcmp(t, "k.mu")) inj_button((int)jnum(ev, "b", -1), 0);
  else if (!strcmp(t, "k.wh")) inj_wheel(jnum(ev, "dx", 0), jnum(ev, "dy", 0), !strcmp(jstr(ev, "mode", "line"), "pixel"));
  else if (!strcmp(t, "k.kd")) inj_key(jstr(ev, "code", NULL), 1);
  else if (!strcmp(t, "k.ku")) inj_key(jstr(ev, "code", NULL), 0);
}

// ---- capture ----

static int any(const unsigned char *a, int n) {
  for (int i = 0; i < n; i++) if (a[i]) return 1;
  return 0;
}

static int really_begin(int mouse, int keys) {
  grab_all(1);
  capturing = 1; capMouse = mouse; capKeys = keys;
  wantCapture = 0;
  memset(sentKeys, 0, sizeof sentKeys);
  memset(sentButtons, 0, sizeof sentButtons);
  lastCmd = now_ms();
  push = 0;
  lastCross = now_ms();
  arrivalSide = SIDE_NONE;
  return 1;
}

// Grabbing while a key or button is down would leave it pressed for the compositor (it never sees
// the release): wait until everything is up (at most a second, then go anyway).
int wl_capture_begin(int mouse, int keys) {
  if (capturing) { capMouse = mouse; capKeys = keys; return 1; }
  if (!any(localKeys, 256) && !any(localButtons, 8)) return really_begin(mouse, keys);
  wantCapture = 1; wantMouse = mouse; wantKeys = keys; wantSince = now_ms();
  return 1;
}

void wl_capture_stop(int toSide, double offset, int hasSide) {
  wantCapture = 0;
  if (!capturing) return;
  capturing = 0;
  hkPending = -1;
  memset(hkRemote, 0, sizeof hkRemote);
  // What is still held was pressed over there: let it go there (the other side's releaseAll does
  // too); the compositor never saw those presses, so their releases are harmless here.
  grab_all(0);
  crossing_reset();
  if (capMouse && hasSide && toSide != SIDE_NONE) {
    int x, y;
    screens_edge_point(toSide, offset, &x, &y);
    wl_move_to(x, y);
  }
  wl_arrived(capMouse && hasSide ? toSide : SIDE_NONE);
}

void wl_return_zones(const char *text) { nRetZ = zones_parse(text, retZ, MAX_ZONES); }

void wl_arm_return(int on, const char *list, int s, double resistance) {
  local_watch(on);
  nRetSides = on ? parse_side_list(list, s, retSides) : 0;
  retArmed = nRetSides > 0;
  retSide = nRetSides ? retSides[0] : SIDE_NONE; // injected moves stop at this edge (inj_move)
  retResistance = resistance;
  retPush = 0;
  memset(retPushBy, 0, sizeof retPushBy);
}

static void feed_return(double dx, double dy) {
  if (!retArmed) return;
  if (crossing_return_ok()) { // come in through a cursor portal: its stretch only (portals.c)
    int ps;
    double poff;
    int pr = portals_return_step((int)px, (int)py, dx, dy, retResistance, dwellMs, may_cross, &ps, &poff);
    if (pr == 1) return;
    if (pr == 2) {
      retArmed = 0;
      lastCross = now_ms();
      JOut o;
      jo_begin(&o);
      jo_str(&o, "event", "return");
      jo_str(&o, "side", side_name(ps));
      jo_num(&o, "offset", round(poff * 10000) / 10000);
      jo_bool(&o, "portal", 1);
      emit(jo_end(&o));
      return;
    }
  }
  double now = now_ms();
  for (int i = 0; i < nRetSides; i++) {
    int s = retSides[i];
    double outward = screens_outward(s, dx, dy);
    if (!may_cross(s, outward) || !crossing_return_ok()) { retPushBy[s] = 0; continue; }
    if (outward <= 0 || !screens_on_outer_edge((int)px, (int)py, s) || !zones_contain(retZ, nRetZ, (int)px, (int)py, s)) { retPushBy[s] = 0; continue; }
    if (now - retLastBy[s] > 300) retPushBy[s] = 0;
    retLastBy[s] = now;
    if (retPushBy[s] == 0) retSinceBy[s] = now;
    retPushBy[s] += outward;
    if (retPushBy[s] < retResistance || !dwelt(retResistance, retSinceBy[s], now)) continue;
    retArmed = 0;
    lastCross = now;
    memset(retPushBy, 0, sizeof retPushBy);
    JOut o;
    jo_begin(&o);
    jo_str(&o, "event", "return");
    jo_str(&o, "side", side_name(s));
    jo_num(&o, "offset", round(screens_offset((int)px, (int)py, s) * 10000) / 10000);
    emit(jo_end(&o));
    return;
  }
}

void wl_config(const JObj *c) {
  side = side_of(jstr(c, "side", NULL));
  nEdgeSides = parse_side_list(jstr(c, "sideList", NULL), side, edgeSides);
  resistancePx = jnum(c, "resistancePx", 120);
  cooldownMs = jnum(c, "cooldownMs", 250);
  dwellMs = jnum(c, "dwellMs", 150);
  nEdgeZ = zones_parse(jstr(c, "zones", NULL), edgeZ, MAX_ZONES);
  const char *hk = jstr(c, "hotkey", "AltRight");
  char l[24], r[24];
  snprintf(l, sizeof l, "%sLeft", hk);
  snprintf(r, sizeof r, "%sRight", hk);
  int one = key_evdev(hk), left = key_evdev(l), right = key_evdev(r);
  if (one > 0) { hotkeyEvs[0] = one; hotkeyEvs[1] = -1; }
  else if (left > 0 && right > 0) { hotkeyEvs[0] = left; hotkeyEvs[1] = right; }
  else { hotkeyEvs[0] = 100; hotkeyEvs[1] = -1; }
  driveOther = jbool(c, "driveOther", 0);
  edgeKeyboard = jbool(c, "keyboard", 1);
  dragFiles = jbool(c, "dragFiles", 1);
  nDragSides = jhas(c, "dragSideList") ? parse_side_list(jstr(c, "dragSideList", ""), SIDE_NONE, dragSides) : -1;
  accelFlat = !strcmp(jstr(c, "accelProfile", "adaptive"), "flat");
  // The user session's word on the pointer mode (Full KVM: this helper is the service's, without
  // that session's XDG_CURRENT_DESKTOP, #244). Our own TWINDESK_POINTER still wins.
  const char *pm = jstr(c, "pointerMode", "");
  const char *own = getenv("TWINDESK_POINTER");
  if ((!strcmp(pm, "output") || !strcmp(pm, "box")) && !(own && *own)) {
    int want = !strcmp(pm, "output");
    if (want != per_output) fprintf(stderr, "[wl] pointer: %s (kvmConfig)\n", want ? "placed within the cursor's monitor" : "over all the monitors");
    per_output = want;
  }
  accelSpeed = fmax(-1, fmin(1, jnum(c, "accelSpeed", 0)));
  padTap = jbool(c, "padTap", 1);
  padNatural = jbool(c, "padNatural", 0);
  deadmanMs = jnum(c, "deadmanMs", 1500);
  doubleTapMs = jnum(c, "doubleTapMs", 400);
  shortcut_config(c);
  crossing_config(c);
  portals_config(jstr(c, "portalList", NULL)); // cursor portals (portals.c)
}

void wl_touch(void) { lastCmd = now_ms(); }
int wl_active(void) { return capturing; }
void wl_position(int *x, int *y) { *x = (int)px; *y = (int)py; }

// ---- local input ----

static int is_hotkey(int ev) { return ev == hotkeyEvs[0] || ev == hotkeyEvs[1]; }

static int detect_hotkey(int ev, int down) {
  double now = now_ms();
  if (!is_hotkey(ev)) {
    if (down) lastTapUp = -1;
    return 0;
  }
  if (down) { hkDown = 1; return 0; }
  if (!hkDown) return 0;
  hkDown = 0;
  if (lastTapUp >= 0 && now - lastTapUp <= doubleTapMs) { lastTapUp = -1; return 1; }
  lastTapUp = now;
  return 0;
}

static void fire_hotkey(void) {
  int was = capturing;
  if (capturing) wl_capture_stop(SIDE_NONE, 0, 0);
  JOut o;
  jo_begin(&o);
  jo_str(&o, "event", "hotkey");
  jo_bool(&o, "wasCapturing", was);
  emit(jo_end(&o));
}

static void send_key(int ev, int down) {
  const char *code = key_code(ev);
  if (!code) return;
  JOut k;
  jo_begin(&k);
  jo_str(&k, "t", down ? "k.kd" : "k.ku");
  jo_str(&k, "code", code);
  forward(jo_end(&k));
}

static void flush_hotkey(void) {
  int ev = hkPending;
  hkPending = -1;
  if (ev < 0 || !capturing || !capKeys) return;
  hkRemote[ev] = 1;
  sentKeys[ev] = 1;
  send_key(ev, 1);
}

// value: 1 down, 0 up, 2 autorepeat (sent as another k.kd, like the PC)
// The emergency shortcut: capture stops here first (or what we injected is let go), then main is told.
void wl_emergency(void) {
  if (capturing) wl_capture_stop(SIDE_NONE, 0, 0);
  else { pen_leave(1); touch_leave(1); wl_release_all(); }
  retArmed = 0;
  local_watch(0);
  driveOther = 0;
  memset(pushBy, 0, sizeof pushBy);
  crossing_emergency();
}

static void on_key(int ev, int value) {
  portals_key(ev, value != 0); // a portal's key held (portals.c)
  if (ev < 0 || ev > 255) return;
  {
    int cr = crossing_key(ev, value, capturing, capturing && capMouse, forward);
    if (cr == 1) { if (value != 2) localKeys[ev] = (unsigned char)value; return; }
    if (cr == 2) { localKeys[ev] = 1; wl_emergency(); return; }
  }
  if (value != 2) { localKeys[ev] = (unsigned char)value; shortcut_key(ev, value, !capturing, !(capturing && capKeys)); }
  int hot = value == 2 ? 0 : detect_hotkey(ev, value);
  if (capturing && capKeys) {
    if (is_hotkey(ev)) {
      if (value == 1) { if (!hkRemote[ev]) hkPending = ev; }
      else if (value == 0) {
        if (hkRemote[ev]) { hkRemote[ev] = 0; sentKeys[ev] = 0; send_key(ev, 0); }
        else if (hkPending == ev) hkPending = -1;
      }
      if (hot) fire_hotkey();
      return;
    }
    if (value) flush_hotkey();
    if (value == 0 && !sentKeys[ev]) { if (hot) fire_hotkey(); return; } // pressed before capture
    sentKeys[ev] = value != 0;
    send_key(ev, value != 0);
  }
  if (hot) fire_hotkey();
}

static int watchRel; // wl_watch_release: say `released` at our next left button-up at home
void wl_watch_release(int on) { watchRel = on; }

static void on_button(int code, int down) {
  int b = code == BTN_LEFT ? 0 : code == BTN_RIGHT ? 1 : code == BTN_MIDDLE ? 2 : code == BTN_SIDE ? 3 : code == BTN_EXTRA ? 4 : -1;
  if (b < 0) return;
  localButtons[b] = (unsigned char)down;
  if (b == 0 && !down && watchRel && !capturing) { watchRel = 0; emit("{\"event\":\"released\"}"); }
  if (down && !capturing && b <= 2 && local_note(0, 0, 1)) retArmed = 0; // a click here while driven
  if (!capturing || !capMouse) return;
  if (!down && !sentButtons[b]) return;
  if (down) flush_hotkey();
  sentButtons[b] = (unsigned char)down;
  JOut k;
  jo_begin(&k);
  jo_str(&k, "t", down ? "k.md" : "k.mu");
  jo_num(&k, "b", b);
  forward(jo_end(&k));
}

static void on_wheel(int horiz, int v) {
  if (!capturing || !capMouse || !v) return;
  JOut k;
  jo_begin(&k);
  jo_str(&k, "t", "k.wh");
  jo_num(&k, "dx", horiz ? v : 0);
  jo_num(&k, "dy", horiz ? 0 : -v);
  jo_str(&k, "mode", "line");
  forward(jo_end(&k));
}

// Why a push against the screen's edge didn't cross (#237: Hyprland, niri), in the log: each reason
// once, again only after 20 s.
static void edge_why(const char *why) {
  static char last[160];
  static double at;
  double now = now_ms();
  if (!strncmp(last, why, sizeof last - 1) && now - at < 20000) return;
  snprintf(last, sizeof last, "%s", why);
  at = now;
  fprintf(stderr, "[edge] not crossing: %s\n", why);
}

// libinput's pointer acceleration for a mouse (filter-flat.c, filter-linear.c) at the desktop's
// speed s: flat, 1 + s; adaptive (the default), slow hands slowed a little, 1:1 up to a
// threshold, then steeper up to 2 + 1.5 s. v: units per ms.
static double accel(double v) {
  if (accelFlat) return fmax(0.005, 1 + accelSpeed) * speed;
  double threshold = fmax(0.2, 0.4 - 0.25 * accelSpeed);
  double f = v < 0.07 ? 10 * v + 0.3 : v < threshold ? 1 : 1 + (1.1 + 0.75 * accelSpeed) * (v - threshold);
  return fmin(f, 2.0 + 1.5 * accelSpeed) * speed;
}

// The hand's speed as libinput sees it: over the last few events (up to 8, within 50 ms) by the
// times the kernel stamped them, not when we read them (several read at once aren't instant).
#define TRACK 8
static double trackT[TRACK], trackD[TRACK];
static int trackN, trackI;

static double velocity(double dist, double tMs) {
  trackT[trackI] = tMs; trackD[trackI] = dist;
  trackI = (trackI + 1) % TRACK;
  if (trackN < TRACK) trackN++;
  // The way covered since an earlier event, over the time since then.
  double sum = 0, span = 0;
  for (int k = 1; k < trackN; k++) {
    int cur = (trackI - k + TRACK) % TRACK, prev = (cur - 1 + TRACK) % TRACK;
    if (tMs - trackT[prev] > 50) break;
    sum += trackD[cur];
    span = tMs - trackT[prev];
  }
  return span > 0 ? sum / span : dist / 8; // the first after a pause: as slow as a frame (8 ms) of it
}

static void on_motion(double rdx, double rdy, double tMs) {
  double now = now_ms();
  double f = accel(velocity(sqrt(rdx * rdx + rdy * rdy), tMs));
  double dx = rdx * f, dy = rdy * f;
  if (capturing) {
    if (!capMouse || (dx == 0 && dy == 0)) return;
    JOut k;
    jo_begin(&k);
    jo_str(&k, "t", "k.mm");
    jo_num(&k, "dx", round(dx * 100) / 100);
    jo_num(&k, "dy", round(dy * 100) / 100);
#ifdef TDFAST
    if (!lane_move(dx, dy, &k)) // on the input lane (lane.c), nothing is written
#endif
      forward(jo_end(&k));
    shake_feed(dx, dy); // after it went: shaken, the pointer comes home (shake.c)
    return;
  }
  // Follow the pointer.
  int ix = (int)lround(px + dx), iy = (int)lround(py + dy), cx, cy;
  screens_clamp(ix, iy, (int)px, (int)py, &cx, &cy);
  // Keep the fraction, as the compositor does: a mouse polling at 4-8 kHz moves well under a pixel
  // per event, and rounding each one ran ahead of the real pointer, the more so the slower the hand
  // (it crossed before the edge, #349). Only a clamped axis lands on the whole pixel.
  px = (cx == ix) ? px + dx : cx;
  py = (cy == iy) ? py + dy : cy;
  int pinned = cx != ix || cy != iy; // pushed against the screens' outline: a crossing may be meant
  if (local_note(rdx, rdy, 0)) { retArmed = 0; return; }
  if (retArmed) { feed_return(rdx, rdy); return; }
  if (!driveOther || (!nEdgeSides && !portals_any()) || any(localButtons, 8)) { // rule 6
    if (pinned) edge_why(!driveOther ? "crossing to the other device is off now (not connected, paused, Mouse off, or a phone window has focus)" : !nEdgeSides ? "no edge leads to the other device" : "a mouse button is held");
    memset(pushBy, 0, sizeof pushBy);
    return;
  }
  if (portals_any() && crossing_edge_ok()) { // through a cursor portal (portals.c)
    int pidx, pinX, pinY;
    double poff;
    if (portals_step((int)px, (int)py, rdx, rdy, resistancePx, dwellMs, may_cross, &pidx, &poff, &pinX, &pinY)) {
      memset(pushBy, 0, sizeof pushBy);
      really_begin(1, edgeKeyboard);
      wl_move_to(pinX, pinY); // the compositor's pointer went on: back where it left
      JOut o;
      jo_begin(&o);
      jo_str(&o, "event", "portal");
      jo_str(&o, "id", portals_id(pidx));
      jo_num(&o, "offset", round(poff * 1e6) / 1e6);
      emit(jo_end(&o));
      return;
    }
  }
  for (int i = 0; i < nEdgeSides; i++) {
    int s = edgeSides[i];
    double outward = screens_outward(s, rdx, rdy);
    if (!may_cross(s, outward) || !crossing_edge_ok()) {
      if (pinned && outward > 0) edge_why(crossing_edge_ok() ? "just crossed back: waiting a moment" : "hold the crossing key to cross");
      pushBy[s] = 0;
      continue;
    }
    if (outward <= 0 || !screens_on_outer_edge((int)px, (int)py, s) || !zones_contain(edgeZ, nEdgeZ, (int)px, (int)py, s) || portals_covers((int)px, (int)py, s)) {
      if (pinned && i == nEdgeSides - 1) {
        char m[160];
        snprintf(m, sizeof m, "the pointer (%d,%d, as followed here) is at a screen edge, the other device is on the %s side%s", (int)px, (int)py, side_name(s), outward > 0 ? ", outside its zone" : "");
        edge_why(m);
      }
      pushBy[s] = 0;
      continue;
    }
    if (now - lastPushBy[s] > 300) pushBy[s] = 0;
    lastPushBy[s] = now;
    if (pushBy[s] == 0) pushSinceBy[s] = now;
    pushBy[s] += outward;
    if (pushBy[s] < resistancePx || !dwelt(resistancePx, pushSinceBy[s], now)) continue;
    double offset = screens_offset((int)px, (int)py, s);
    memset(pushBy, 0, sizeof pushBy);
    really_begin(1, edgeKeyboard);
    JOut o;
    jo_begin(&o);
    jo_str(&o, "event", "edge");
    jo_num(&o, "crossingFlags", crossing_bits());
    jo_str(&o, "side", side_name(s));
    jo_num(&o, "offset", round(offset * 10000) / 10000);
    emit(jo_end(&o));
    return;
  }
}

// ---- touchpad gestures (#222) ----
// While this computer drives another one the touchpads are grabbed, so the desktop's libinput never
// sees them: tap to click (one finger left, two right, three middle), two-finger scrolling and the
// two-finger pinch are made here, as libinput would, and go as k.md/k.mu, k.wh (pixels) and k.ge
// zoom (shared/pinch.js on a phone). At home (not grabbed) libinput does it; two fingers there only
// keep from moving the pointer we follow.
#define TAP_MS 180       // a touch shorter than this, that hardly moved, is a tap
#define TAP_MOVE_PX 10   // … about 2.5 mm (pad px are about 4 per mm)
#define SCROLL_GAIN 2.0  // pad px → wheel px

static void pad_button(int b) {
  if (!capturing || !capMouse) return;
  flush_hotkey();
  for (int down = 1; down >= 0; down--) {
    JOut k;
    jo_begin(&k);
    jo_str(&k, "t", down ? "k.md" : "k.mu");
    jo_num(&k, "b", b);
    forward(jo_end(&k));
  }
}

static void pad_scroll(double dx, double dy) {
  if (!capturing || !capMouse || (dx == 0 && dy == 0)) return;
  double s = padNatural ? -SCROLL_GAIN : SCROLL_GAIN; // DOM sign: +dy = content further down
  JOut k;
  jo_begin(&k);
  jo_str(&k, "t", "k.wh");
  jo_num(&k, "dx", round(dx * s * 100) / 100);
  jo_num(&k, "dy", round(dy * s * 100) / 100);
  jo_str(&k, "mode", "pixel");
  forward(jo_end(&k));
}

static void pad_zoom(const char *phase, double v) {
  if (!capturing || !capMouse) return;
  JOut k;
  jo_begin(&k);
  jo_str(&k, "t", "k.ge");
  jo_str(&k, "g", "zoom");
  jo_num(&k, "v", round(v * 10000) / 10000);
  jo_str(&k, "phase", phase);
  forward(jo_end(&k));
}

static int pad_fingers(const In *in) {
  if (!in->touching) return 0;
  for (int n = 5; n >= 2; n--) if (in->tools & (1 << n)) return n;
  return 1;
}

static void pad_two_end(In *in) {
  if (in->twoMode == 2) pad_zoom("end", 0);
  in->twoMode = in->twoHas = 0;
  in->twoAccC = in->twoAccD = 0;
}

// One frame (SYN_REPORT) of a touchpad. *mdx, *mdy: the pointer's move (one finger only).
static void pad_frame(In *in, double tMs, double *mdx, double *mdy) {
  int was = in->fingers, f = pad_fingers(in);
  in->fingers = f;
  if (f && !was) { in->tapAlive = 1; in->tapStart = tMs; in->tapMax = f; in->tapMove = 0; in->tapPressed = 0; }
  if (f > in->tapMax) in->tapMax = f;
  if (f != was) { in->hasLast = 0; pad_two_end(in); } // a finger more or less: start over, no jump
  if (!f) {
    if (in->tapAlive && padTap && !in->tapPressed && tMs - in->tapStart < TAP_MS && in->tapMove < TAP_MOVE_PX && in->tapMax <= 3)
      pad_button(in->tapMax == 1 ? 0 : in->tapMax == 2 ? 1 : 2);
    in->tapAlive = 0;
    return;
  }
  if (f == 1) {
    if (in->hasLast) {
      double dx = (in->absX - in->lastX) * in->scaleX, dy = (in->absY - in->lastY) * in->scaleY;
      *mdx += dx; *mdy += dy;
      in->tapMove += sqrt(dx * dx + dy * dy);
    }
    in->lastX = in->absX; in->lastY = in->absY; in->hasLast = 1;
    return;
  }
  if (f != 2) return; // three or more: no pointer, no scroll
  // Two fingers: their middle scrolls, their distance pinches. Without slots (an old single-touch
  // pad that only counts fingers) the one position there is scrolls.
  double ax, ay, dist = 0;
  int both = in->mtOn[0] && in->mtOn[1];
  if (both) {
    double x0 = in->mtX[0] * in->scaleX, y0 = in->mtY[0] * in->scaleY, x1 = in->mtX[1] * in->scaleX, y1 = in->mtY[1] * in->scaleY;
    ax = (x0 + x1) / 2; ay = (y0 + y1) / 2;
    dist = sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
  } else {
    ax = in->absX * in->scaleX; ay = in->absY * in->scaleY;
  }
  if (in->twoHas) {
    double dcx = ax - in->twoCx, dcy = ay - in->twoCy, dd = both ? dist - in->twoDist : 0;
    in->tapMove += sqrt(dcx * dcx + dcy * dcy) + fabs(dd);
    if (in->twoMode == 0) {
      in->twoAccC += sqrt(dcx * dcx + dcy * dcy);
      in->twoAccD += fabs(dd);
      if (in->twoAccD > 16 && in->twoAccD > 1.5 * in->twoAccC) { in->twoMode = 2; pad_zoom("begin", 0); }
      else if (in->twoAccC > 6) in->twoMode = 1;
    }
    if (in->twoMode == 1) pad_scroll(dcx, dcy);
    else if (in->twoMode == 2 && in->twoDist > 1) pad_zoom("change", dist / in->twoDist - 1);
  }
  in->twoCx = ax; in->twoCy = ay; in->twoDist = dist; in->twoHas = 1;
}

static void read_in(int i) {
  In *in = &ins[i];
  struct input_event evs[64];
  double mdx = 0, mdy = 0;
  for (;;) {
    ssize_t n = read(in->fd, evs, sizeof evs);
    if (n < 0) {
      if (errno == EAGAIN || errno == EINTR) break;
      fprintf(stderr, "input: %s gone\n", in->path);
      close_in(i);
      return;
    }
    if (n == 0) break;
    for (int k = 0; k < (int)(n / (ssize_t)sizeof evs[0]); k++) {
      struct input_event *e = &evs[k];
      if (e->type == EV_KEY) {
        if (e->code >= BTN_MOUSE && e->code < BTN_JOYSTICK) { if (e->value) in->tapPressed = 1; on_button(e->code, e->value != 0); }
        else if (e->code == BTN_TOUCH) { in->touching = e->value != 0; in->hasLast = 0; }
        else if ((in->kind & K_PAD) && (e->code == BTN_TOOL_FINGER || e->code == BTN_TOOL_DOUBLETAP || e->code == BTN_TOOL_TRIPLETAP || e->code == BTN_TOOL_QUADTAP || e->code == BTN_TOOL_QUINTTAP)) {
          int n = e->code == BTN_TOOL_FINGER ? 1 : e->code == BTN_TOOL_DOUBLETAP ? 2 : e->code == BTN_TOOL_TRIPLETAP ? 3 : e->code == BTN_TOOL_QUADTAP ? 4 : 5;
          if (e->value) in->tools |= 1 << n; else in->tools &= ~(1 << n);
        }
        else if (e->code < 256) on_key(e->code, e->value);
      } else if (e->type == EV_REL) {
        if (e->code == REL_X) mdx += e->value;
        else if (e->code == REL_Y) mdy += e->value;
        else if (e->code == REL_WHEEL) on_wheel(0, e->value);
        else if (e->code == REL_HWHEEL) on_wheel(1, e->value);
      } else if (e->type == EV_ABS && (in->kind & K_PAD)) {
        if (e->code == ABS_X) in->absX = e->value;
        else if (e->code == ABS_Y) in->absY = e->value;
        else if (e->code == ABS_MT_SLOT) in->slot = e->value;
        else if (in->slot >= 0 && in->slot < 2) {
          if (e->code == ABS_MT_POSITION_X) in->mtX[in->slot] = e->value;
          else if (e->code == ABS_MT_POSITION_Y) in->mtY[in->slot] = e->value;
          else if (e->code == ABS_MT_TRACKING_ID) in->mtOn[in->slot] = e->value >= 0;
        }
      } else if (e->type == EV_SYN && e->code == SYN_REPORT) {
        if (in->kind & K_PAD) pad_frame(in, e->input_event_sec * 1000.0 + e->input_event_usec / 1000.0, &mdx, &mdy);
        if (mdx != 0 || mdy != 0) { on_motion(mdx, mdy, e->input_event_sec * 1000.0 + e->input_event_usec / 1000.0); mdx = mdy = 0; }
      }
    }
  }
}

// ---- loop ----

int wl_init(void) {
  uabs = make_device(1);
  urel = make_device(0);
  if (uabs < 0 || urel < 0) {
    fprintf(stderr, "no access to /dev/uinput (%s)\n", strerror(errno));
    return 0;
  }
  scan();
  if (!nin) fprintf(stderr, "no readable devices in /dev/input: crossing and the hotkey need the input group\n");
  // Somewhere sane until the first `screens` and moves.
  int cx, cy;
  screens_center(&cx, &cy);
  px = cx; py = cy;
  lastCmd = now_ms();
  const char *s = getenv("MACSHARE_HELPER_SPEED");
  if (s && atof(s) > 0) speed = atof(s);
  return 1;
}

int wl_fds(struct pollfd *p, int max) {
  int n = 0;
  for (int i = 0; i < nin && n < max; i++) p[n++] = (struct pollfd){ ins[i].fd, POLLIN, 0 };
  return n;
}

void wl_read(struct pollfd *p, int n) {
  // Back to front: close_in moves the last device into the hole.
  for (int k = n - 1; k >= 0; k--) {
    if (!(p[k].revents & (POLLIN | POLLHUP | POLLERR))) continue;
    for (int i = 0; i < nin; i++) if (ins[i].fd == p[k].fd) { read_in(i); break; }
  }
}

void wl_tick(void) {
  double now = now_ms();
  shortcut_tick();
  strip_tick();
  wlstrip_tick();
  if (now - lastScan > 3000) scan(); // hot-plugged devices
  if (wantCapture && ((!any(localKeys, 256) && !any(localButtons, 8)) || now - wantSince > 1000)) really_begin(wantMouse, wantKeys);
  if (capturing && now - lastCmd > deadmanMs) {
    wl_capture_stop(SIDE_NONE, 0, 0);
    JOut o;
    jo_begin(&o);
    jo_str(&o, "event", "captureEnded");
    jo_str(&o, "reason", "deadman");
    emit(jo_end(&o));
  }
}

// ---- file drags across the edge (wlstrip.c) ----

static int stripOn, stripX, stripY, stripW, stripH, stripSideNow, stripBackNow;

// The nearest of these sides to the pointer whose edge, on the pointer's monitor, is an outer one.
static int nearest_side(const int *list, int n, int mx, int my, int mw, int mh) {
  int best = SIDE_NONE;
  double bestD = 1e9;
  for (int i = 0; i < n; i++) {
    int s = list[i];
    int ex = s == SIDE_LEFT ? mx : s == SIDE_RIGHT ? mx + mw - 1 : (int)px;
    int ey = s == SIDE_TOP ? my : s == SIDE_BOTTOM ? my + mh - 1 : (int)py;
    if (!screens_on_outer_edge(ex, ey, s)) continue;
    double d = s == SIDE_LEFT ? px - mx : s == SIDE_RIGHT ? mx + mw - 1 - px : s == SIDE_TOP ? py - my : my + mh - 1 - py;
    if (d < bestD) { bestD = d; best = s; }
  }
  return best;
}

// While the left button is held at home near an edge whose device takes drags (or the driver's, on the
// way back while driven): the strip there. Shown here when this helper has XWayland, else said to main
// (`dragStrip`: the service's helper, which main relays to the user's own).
static void strip_tick(void) {
  int s = SIDE_NONE, back = 0, mx, my, mw, mh;
  if (!capturing && dragFiles && screens_monitor((int)px, (int)py, &mx, &my, &mw, &mh)) {
    if (retArmed && injButtons[0]) { back = 1; s = nearest_side(retSides, nRetSides, mx, my, mw, mh); }
    else if (driveOther && localButtons[0]) s = nearest_side(nDragSides >= 0 ? dragSides : edgeSides, nDragSides >= 0 ? nDragSides : nEdgeSides, mx, my, mw, mh);
  }
  int on = s != SIDE_NONE, x = 0, y = 0, w = 0, h = 0;
  if (on) {
    x = s == SIDE_RIGHT ? mx + mw - 2 : mx; y = s == SIDE_BOTTOM ? my + mh - 2 : my;
    w = s == SIDE_LEFT || s == SIDE_RIGHT ? 2 : mw; h = s == SIDE_TOP || s == SIDE_BOTTOM ? 2 : mh;
  }
  if (on == stripOn && (!on || (x == stripX && y == stripY && w == stripW && h == stripH && s == stripSideNow && back == stripBackNow))) return;
  stripOn = on; stripX = x; stripY = y; stripW = w; stripH = h; stripSideNow = s; stripBackNow = back;
  if (wlstrip_available()) {
    if (on) wlstrip_show(x, y, w, h, s, back, 0);
    else wlstrip_hide();
  }
  JOut o;
  jo_begin(&o);
  jo_str(&o, "event", "dragStrip");
  jo_bool(&o, "on", on);
  if (on) {
    jo_num(&o, "x", x); jo_num(&o, "y", y); jo_num(&o, "w", w); jo_num(&o, "h", h);
    jo_str(&o, "side", side_name(s));
    jo_bool(&o, "back", back);
  }
  emit(jo_end(&o));
}

// Paths one per line → a JSON array (malloc'd); n: how many.
static char *paths_json(const char *text, int *n) {
  size_t cap = strlen(text) * 2 + 16, len = 0;
  char *out = malloc(cap);
  *n = 0;
  out[len++] = '[';
  const char *p = text;
  while (*p) {
    const char *e = strchr(p, '\n');
    if (!e) e = p + strlen(p);
    if (e > p) {
      if (*n) out[len++] = ',';
      out[len++] = '"';
      for (const char *q = p; q < e && len + 4 < cap; q++) {
        if (*q == '"' || *q == '\\') out[len++] = '\\';
        out[len++] = *q;
      }
      out[len++] = '"';
      (*n)++;
    }
    p = *e ? e + 1 : e;
  }
  out[len++] = ']';
  out[len] = 0;
  return out;
}

// A file drag caught on the strip (wlstrip.c here, or the user's helper's: the `dragCross` command):
// home, it crosses with the files (`edge {side, offset, drag:{paths}}`): the compositor's drag ends on
// the strip (our virtual mouse lets the button go there, where the drop does nothing), the real button,
// grabbed now, goes over and drops them there. The way back (driven): the input goes home with them
// (`return {…, drag}`), the driver's button that holds the drag here let go on the strip.
const char *wl_drag_cross(const char *text, int s, int back) {
  int n = 0;
  char *json = paths_json(text, &n);
  const char *why = !n ? "no paths" : s == SIDE_NONE ? "no side"
    : back ? (retArmed ? NULL : "not driven any more")
    : capturing ? "already capturing" : !driveOther ? "crossing to the other device is off now" : !localButtons[0] ? "the button was let go already" : NULL;
  fprintf(stderr, "[drag] %s: %s\n", back ? "back home" : "crossing", why ? why : "with the files");
  if (why) { free(json); return why; } // why not: the command's error (route.js logs it)
  size_t cap = strlen(json) + 200;
  char *line = malloc(cap);
  double offset = round(screens_offset((int)px, (int)py, s) * 10000) / 10000;
  if (back) {
    retArmed = 0;
    memset(retPushBy, 0, sizeof retPushBy);
    lastCross = now_ms();
    snprintf(line, cap, "{\"event\":\"return\",\"side\":\"%s\",\"offset\":%.4f,\"drag\":{\"paths\":%s}}", side_name(s), offset, json);
    emit(line);
    inj_button(0, 0);
  } else {
    inj_button(0, 1); // the compositor's button, from our virtual mouse: down and up, so its drag drops on the strip
    inj_button(0, 0);
    really_begin(1, edgeKeyboard);
    sentButtons[0] = 1; // the real button is held: its release goes over and drops the files there
    snprintf(line, cap, "{\"event\":\"edge\",\"side\":\"%s\",\"offset\":%.4f,\"drag\":{\"paths\":%s}}", side_name(s), offset, json);
    emit(line);
  }
  free(line);
  free(json);
  return NULL;
}

void wl_strip_caught(const char *text, int s, int back) { wl_drag_cross(text, s, back); }

void wl_shutdown(void) {
  wlstrip_shutdown();
  if (capturing) wl_capture_stop(SIDE_NONE, 0, 0);
  wl_release_all();
  grab_all(0);
  if (uabs >= 0) { ioctl(uabs, UI_DEV_DESTROY); close(uabs); uabs = -1; }
  if (urel >= 0) { ioctl(urel, UI_DEV_DESTROY); close(urel); urel = -1; }
}

int wl_devices(void) { return nin; }