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.

11.8 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Two-way switches per pair in Multi-device (step 5 of docs/mesh-graph.md), on the bus instead of the facade.
// The wire and its meaning are pairsync.js's (an older app speaks the same): `pair.sync {feature, dir, on}`,
// `{key, value}` (a shared value) and `{all}` (the host's, when the session starts), directions from the
// sender's side.
//
// Every device's pair is kept (settings.pairSync[id]) and told by its own session; the settings hold the
// pair of the device the pages show (the selected one), which is where they're read and changed. The facade
// did that on its welcome and closed (a crossing): PairSync adopted the new device's pair into the settings
// and a pair.sync from a device that wasn't selected was only stashed. Here the hub says so ('selected'):
// the pair of the device the pages showed is kept, the next one's goes into the settings; a pair.sync from
// any device is kept for it, and goes into the settings when it's the one shown.
//
//   pairSyncMesh({ bus, hub, features, values, settings(), save(), setFeature(f, dir, on), setValue(key, v),
//                  peerRole(id), peerOs(id), onPair(id) })
//     features / values: the platform's pairsync.js FEATURES / VALUES (only those this app syncs)
//     setFeature / setValue: apply one to the settings with its side effects (ipc.set, the Mac's actions)
//     check()   with every state push: what changed in the settings goes to the device shown, and is kept
//
// Off at first (FRESH_OFF): Terminal and Sound start off for a device met for the first time, both ways.
//
// Asked, never taken (GUARD): a switch that lets another device in here (Terminal's "Let {name} open a
// terminal on {me}", our out) is turned on only on this computer. With several computers any of them may
// ask for a shell on any other, so a pair.sync from another device that would turn it on is kept as a
// request (settings.pairAsk[id][feature] = when), for this computer's Terminal page to answer; turning it
// off is always taken. A device met for the first time starts with it off, whatever the device shown has.
//     asks()               [{id, feature, at}] the requests waiting here
//     answer(id, f, yes)   Allow (the switch on for that device, told to it) or Decline (told it's off)
'use strict';

const P = require('./protocol.js');

const T = P.T;
const GUARD = { terminal: 'out' }; // feature → our direction that lets the other device in
// Off for a device met for the first time, both ways, whatever the device shown has: each one is turned on
// for that device only (Terminal; Sound, so turning sound on doesn't send it to every device of the mesh).
const FRESH_OFF = { terminal: ['out', 'in'], audio: ['out', 'in'] };
const ASK_MAX = 10;

class PairSyncMesh {
  constructor(o) {
    Object.assign(this, { guard: GUARD }, o);
    if (!this.settings().pairSync || typeof this.settings().pairSync !== 'object') this.settings().pairSync = {};
    if (!this.settings().pairAsk || typeof this.settings().pairAsk !== 'object') this.settings().pairAsk = {};
    this.shownId = this.hub.selectedId || null; // whose pair the settings hold
    this.mirroring = false;
    this.bus.on(T.PAIR_SYNC, (m, from) => this._heard(m, from));
    this.bus.onPeer('welcome', (id) => this._welcome(id));
    this.hub.on('selected', (id) => this._show(id || null));
  }

  get all() {
    return this.settings().pairSync;
  }

  _flat(f, dir) {
    const p = this.features[f] && this.features[f][dir];
    const sec = p && this.settings()[p[0]];
    return sec && typeof sec === 'object' ? !!sec[p[1]] : undefined;
  }

  _valuesFor(id) {
    const phone = !!(this.peerOs && P.isPhoneOs(this.peerOs(id)));
    return Object.fromEntries(Object.entries(this.values).filter(([, d]) => (phone || !d.phone) && (!d.feature || this.features[d.feature])));
  }

  // The settings as a pair (directions from our side), for the device shown.
  _snapshot(id) {
    const out = { values: {} };
    for (const f of Object.keys(this.features)) out[f] = { out: this._flat(f, 'out'), in: this._flat(f, 'in') };
    for (const [k, d] of Object.entries(this._valuesFor(id))) {
      let v = this.settings();
      for (const p of d.path) v = v == null ? undefined : v[p];
      out.values[k] = v;
    }
    return out;
  }

  // A device's pair: its saved one, else the settings (a device met for the first time takes them).
  pairOf(id) {
    return id === this.shownId ? this._snapshot(id) : this.all[id] || this._closed(this._snapshot(id));
  }

  // A pair for a device met for the first time: the guarded switches off, whatever the settings have.
  _closed(pair) {
    for (const [f, dirs] of Object.entries(FRESH_OFF)) {
      for (const dir of dirs) if (this.features[f] && this.features[f][dir]) pair[f] = { ...(pair[f] || {}), [dir]: false };
    }
    return pair;
  }

  // Our side of a guarded switch for that device now.
  _has(id, f, dir) {
    if (id === this.shownId) return !!this._flat(f, dir);
    const v = this.all[id] && this.all[id][f];
    return !!(v && v[dir] === true);
  }

  get _asks() {
    return this.settings().pairAsk;
  }

  _ask(id, f) {
    const a = this._asks;
    if (!a[id] && Object.keys(a).length >= ASK_MAX) return;
    a[id] = { ...(a[id] || {}), [f]: Date.now() };
    this.save();
    if (this.onAsk) this.onAsk(id, f);
  }

  _unask(id, f) {
    const a = this._asks;
    if (!a[id] || !a[id][f]) return false;
    delete a[id][f];
    if (!Object.keys(a[id]).length) delete a[id];
    this.save();
    return true;
  }

  asks() {
    const out = [];
    for (const [id, fs] of Object.entries(this._asks)) for (const [feature, at] of Object.entries(fs || {})) if (this.guard[feature] && !this._has(id, feature, this.guard[feature])) out.push({ id, feature, at: Number(at) || 0 });
    return out;
  }

  // This computer's answer to a request: Allow turns the switch on for that device (and tells it), Decline
  // tells it the switch is off (its own "open a terminal there" goes off with it).
  answer(id, f, yes) {
    const dir = this.guard[f];
    if (!dir || !this.features[f] || !this.features[f][dir]) return false;
    this._unask(id, f);
    if (id === this.shownId) {
      if (yes && !this._flat(f, dir)) { this.setFeature(f, dir, true); this.check(); }
      else if (!yes) this.bus.send(id, { t: T.PAIR_SYNC, feature: f, dir, on: false });
    } else {
      this._keep(id, { [f]: { [dir]: !!yes } });
      this.bus.send(id, { t: T.PAIR_SYNC, feature: f, dir, on: !!yes });
    }
    if (this.onAsk) this.onAsk(id, f);
    return true;
  }

  // Anything in a pair from another device that would let it in here: taken out, kept as a request.
  _screen(add, id) {
    for (const [f, dir] of Object.entries(this.guard)) {
      const v = add[f];
      if (!v || typeof v[dir] !== 'boolean') continue;
      if (v[dir] === false) { this._unask(id, f); continue; }
      if (this._has(id, f, dir)) continue; // already allowed: nothing new
      delete v[dir];
      if (!Object.keys(v).length) delete add[f];
      this._ask(id, f);
    }
    return add;
  }

  // A message as a pair to keep (our side's directions), or null.
  _fromWire(m, id) {
    const vals = this._valuesFor(id);
    if (vals[m.key] && vals[m.key].allowed.includes(m.value)) return { values: { [m.key]: m.value } };
    if (m.all && typeof m.all === 'object') {
      const mine = { values: {} };
      for (const [k, d] of Object.entries(vals)) if (m.all.values && d.allowed.includes(m.all.values[k])) mine.values[k] = m.all.values[k];
      for (const f of Object.keys(this.features)) {
        const v = m.all[f];
        if (v && typeof v === 'object') mine[f] = { out: v.in, in: v.out }; // their in = our out
      }
      return mine;
    }
    if (this.features[m.feature] && (m.dir === 'out' || m.dir === 'in') && typeof m.on === 'boolean') {
      return { [m.feature]: { [m.dir === 'out' ? 'in' : 'out']: m.on } };
    }
    return null;
  }

  _keep(id, add) {
    const cur = this.all[id] || { values: {} };
    if (!cur.values) cur.values = {};
    for (const [k, v] of Object.entries(add.values || {})) if (v !== undefined) cur.values[k] = v;
    for (const [f, v] of Object.entries(add)) {
      if (f === 'values' || !v || !this.features[f]) continue;
      cur[f] = { ...(cur[f] || {}) };
      for (const dir of ['out', 'in']) if (typeof v[dir] === 'boolean') cur[f][dir] = v[dir];
    }
    this.all[id] = cur;
    this.save();
    if (this.onPair) this.onPair(id);
  }

  // Into the settings, each with its side effects, without telling it back (check() sees no change).
  _mirror(pair, id) {
    this.mirroring = true;
    try {
      for (const f of Object.keys(this.features)) {
        const v = pair[f];
        if (!v) continue;
        for (const dir of ['out', 'in']) {
          if (typeof v[dir] === 'boolean' && this.features[f][dir] && this._flat(f, dir) !== v[dir]) this.setFeature(f, dir, v[dir]);
        }
      }
      const now = this._snapshot(id).values;
      for (const [k, d] of Object.entries(this._valuesFor(id))) {
        const v = pair.values && pair.values[k];
        if (v !== undefined && d.allowed.includes(v) && now[k] !== v) this.setValue(k, v);
      }
    } finally {
      this.mirroring = false;
    }
  }

  _heard(m, id) {
    const raw = this._fromWire(m, id);
    if (!raw) return;
    const add = this._screen(raw, id);
    this._keep(id, add);
    if (id === this.shownId) this._mirror(add, id);
  }

  // A device's session started: the host tells its pair (an older app adopts it as before).
  _welcome(id) {
    if (id === this.shownId) this.check();
    if (this.peerRole && this.peerRole(id) !== 'guest') return;
    this.bus.send(id, { t: T.PAIR_SYNC, all: this.pairOf(id) });
  }

  // The pages show another device: the last one's pair kept as it is now, the next one's into the settings.
  _show(id) {
    if (id === this.shownId) return;
    this.check();
    this.shownId = id;
    if (!id) return;
    const saved = this.all[id];
    if (saved) { // a guarded switch its pair never had: off, not the device shown before
      const p = { ...saved };
      for (const [f, dirs] of Object.entries(FRESH_OFF)) {
        for (const dir of dirs) if (this.features[f] && this.features[f][dir] && !(p[f] && typeof p[f][dir] === 'boolean')) p[f] = { ...(p[f] || {}), [dir]: false };
      }
      this._mirror(p, id);
    } else { // met for the first time: the settings are its pair, but nothing that lets it in here
      const first = this._closed(this._snapshot(id));
      this._keep(id, first);
      this._mirror(first, id);
    }
  }

  // With every state push: a switch or value changed on the pages goes to the device shown, and is kept.
  check() {
    const id = this.shownId;
    if (!id || this.mirroring) return;
    const now = this._snapshot(id);
    const was = this.all[id];
    if (!was) return this._keep(id, now); // its first pair: the settings, nothing changed
    // One the pair never had (an older entry, a value added since) is the settings' now: kept, not told.
    let changed = false;
    let fresh = false;
    for (const f of Object.keys(this.features)) {
      for (const dir of ['out', 'in']) {
        const v = now[f][dir];
        const old = (was[f] || {})[dir];
        if (typeof v !== 'boolean' || v === old) continue;
        if (typeof old !== 'boolean') { fresh = true; continue; }
        changed = true;
        if (this.guard[f] === dir) this._unask(id, f); // answered on the pages themselves
        this.bus.send(id, { t: T.PAIR_SYNC, feature: f, dir, on: v });
      }
    }
    for (const [k, v] of Object.entries(now.values)) {
      const old = (was.values || {})[k];
      if (v === undefined || v === old) continue;
      if (old === undefined) { fresh = true; continue; }
      changed = true;
      this.bus.send(id, { t: T.PAIR_SYNC, key: k, value: v });
    }
    if (changed || fresh) this._keep(id, now);
  }
}

module.exports = { PairSyncMesh, pairSyncMesh: (o) => new PairSyncMesh(o), GUARD, FRESH_OFF };