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.

2.8 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// The sessions of the iPhones and iPads on this computer's cable (iOS 27: iosusb.js and ios-control's
// helper: the device's own picture, and input), for the computers of the mesh that show one
// (shared/phone-relay.js's ScreenHost). One session per device: the tunnel and the screen stream.
// The device's window here has one while it's open (borrow), else this opens its own, in the
// background, closed a while after nobody watches any more (the device shows it's being viewed and
// doesn't lock while one is open).
'use strict';

const usb = () => require('./iosusb.js'); // loaded when a cable session opens

const IDLE_MS = 30000; // our own session, unwatched this long: closed

class IosCableSessions {
  // borrow(udid): an open IosUsbScreen of that device (its window's), or null
  constructor({ borrow = () => null } = {}) {
    this.borrow = borrow;
    this.own = new Map(); // udid -> IosUsbScreen of ours
    this.idle = new Map(); // udid -> timer
    this.watched = new Map(); // udid -> how many watch it
  }

  // Watched from another computer: its session, started, kept while watched.
  use(udid) {
    this.watched.set(udid, (this.watched.get(udid) || 0) + 1);
    return this._session(udid);
  }

  // The session there is now for a device already watched (its own one ended): not counted again.
  current(udid) {
    return this._session(udid);
  }

  unuse(udid) {
    const n = (this.watched.get(udid) || 1) - 1;
    if (n > 0) return void this.watched.set(udid, n);
    this.watched.delete(udid);
    this._idle(udid);
  }

  // The device's window opens here (one screen stream per device): our own session gives way.
  release(udid) {
    const s = this.own.get(udid);
    if (!s) return;
    this.own.delete(udid);
    clearTimeout(this.idle.get(udid));
    s.stop();
  }

  // The devices on the cable now (UDIDs): a session of one that left closes.
  set(udids) {
    for (const udid of [...this.own.keys()]) if (!udids.includes(udid)) this.release(udid);
  }

  _session(udid) {
    const b = this.borrow(udid);
    if (b && !b.closed) return b; // the window's, even while it starts: never a second stream
    clearTimeout(this.idle.get(udid));
    let s = this.own.get(udid);
    if (s && !s.closed) return s;
    s = new (usb().IosUsbScreen)({ id: udid, control: true });
    this.own.set(udid, s);
    s.on('closed', () => { if (this.own.get(udid) === s) this.own.delete(udid); });
    s.start().then(() => { if (!s.controlled) s.stop(); }, (e) => console.log(`[ios] ${udid}: ${e.message}`));
    return s;
  }

  _idle(udid) {
    clearTimeout(this.idle.get(udid));
    if (!this.own.has(udid) || this.watched.get(udid)) return;
    const t = setTimeout(() => this.release(udid), IDLE_MS);
    if (t.unref) t.unref();
    this.idle.set(udid, t);
  }
}

module.exports = { IosCableSessions };