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.

4.1 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// An iPhone through a USB cable, in Multi-device, the same on the Mac, Windows and Linux (each
// mesh/main.js calls iosMesh once):
//   - the iPhones and iPads on our cable (iosusb.js's watch, ios-control's helper), each linked to its
//     TwinDesk app once (ios-links.js: asked through the cable by iosnet.js) and known to the mesh;
//   - Show Phone Screen of the app's iPhone (phoneview.js cableFor): through our cable, its window's own
//     session, or through another computer's (phone-relay.js);
//   - our cable's picture and input for the computers that show it (phone-relay.js's ScreenHost, ios-cable.js);
//   - Apple's developer disk image the helper mounts when the device has none (ios-ddi.js).
'use strict';

const { IosLinks, norm } = require('./ios-links.js');
const { IosCableSessions } = require('./ios-cable.js');
const { ScreenHost, RelayScreen } = require('./phone-relay.js');
const { IosDdi } = require('./ios-ddi.js');
const iosusb = require('./iosusb.js');

// hub: the mesh hub; iosNet: iosnet.js's (its app check and askLink; null: none); store: {get, set} of
// settings.iosLinks; dir: where Apple's image is kept (userData/ios-ddi); borrow(udid): the open
// window's session of that device here, or null; onChange(): the state to show changed.
function iosMesh({ hub, iosNet, store, dir, borrow, onChange }) {
  const selfId = hub.device.id;
  const cable = new IosCableSessions({ borrow });
  const links = new IosLinks({ hub, store, ask: (udid) => (iosNet ? iosNet.askLink(udid) : Promise.resolve(null)) });
  const ddi = new IosDdi({ hub, dir });
  links.on('change', onChange);
  let cabled = []; // the helper's list: [{id (UDID, as usbmuxd has it), name, control}]

  const local = () => {
    const app = new Set(iosNet ? iosNet.view.devices.filter((d) => d.app).map((d) => norm(d.serial)) : []);
    links.local(cabled.map((d) => ({ udid: d.id, name: d.name, control: !!d.control, app: app.has(norm(d.id)) })));
  };
  if (iosNet) iosNet.on('change', local); // the app answers on it now (or not)
  iosusb.watch((list) => {
    cabled = list;
    cable.set(list.map((d) => d.id));
    local();
    // One that takes input: Apple's image ready for the helper (fetched once, here or from a Mac).
    if (list.some((d) => d.control) && !iosusb.IosUsbScreen.ddiDir) {
      ddi.ensure().then((d) => { if (d) iosusb.IosUsbScreen.ddiDir = d; }, () => {});
    }
  });

  // The app's iPhone `phone` on our cable: its UDID as the helper takes it, and whether it takes input.
  const ours = (phone) => {
    const w = links.where(phone);
    const d = w && w.at === selfId ? cabled.find((x) => norm(x.id) === w.udid) : null;
    return d ? { udid: d.id, control: !!d.control } : null;
  };

  const host = ScreenHost.of(hub).add({
    find: (phone) => { const c = ours(phone); return c && c.control ? c.udid : ''; },
    use: (udid) => cable.use(udid),
    current: (udid) => cable.current(udid),
    done: (udid) => cable.unuse(udid),
  });

  return {
    links, cable, host, ddi,
    // Show Phone Screen of the app's iPhone `phone`: {udid, control} on our cable, {relay: {host, id}}
    // through another computer's, or null (its Share Screen).
    cableFor(phone) {
      const c = ours(phone);
      if (c) return c;
      const w = links.where(phone);
      const s = w && w.control ? hub.sessions.get(w.at) : null;
      return s && s.connected ? { relay: { host: w.at, id: phone } } : null;
    },
    // The app's iPhones whose screen comes through a cable now (on ours, or a connected computer's):
    // shown from any computer, wherever its app is connected (the sidebar opens it, openCable).
    screens() {
      const out = new Set();
      for (const [at, list] of links.cables) {
        const s = at === selfId ? null : hub.sessions.get(at);
        if (at !== selfId && !(s && s.connected)) continue;
        for (const c of list) if (c.control) out.add(c.phone);
      }
      return [...out];
    },
    makeRelay: (r) => new RelayScreen({ hub, host: r.host, id: r.id, os: 'ios' }),
    onUsbScreen: (udid) => cable.release(udid), // its window opens here: one stream per device
  };
}

module.exports = { iosMesh };