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.

3.0 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// #375: a phone or tablet on the monitor map at its real size and way round. The computer it is
// connected to sends its screen (from phone.info display) with its own; the others draw it so.
// The same on the Mac's, Windows' and Linux's layout sync.
'use strict';
const assert = require('assert');
const { phoneDisplays } = require('../screen-mesh.js');

// Android: pixels and density → dp; turned → landscape. An iPhone: points (dpi 160). Nothing: [].
assert.deepStrictEqual(phoneDisplays({ display: { w: 1080, h: 2400, rot: 0, dpi: 420 } }).map((d) => [d.w, d.h]), [[411, 914]]);
assert.deepStrictEqual(phoneDisplays({ display: { w: 2560, h: 1600, rot: 1, dpi: 320 } }).map((d) => [d.w, d.h]), [[1280, 800]]);
assert.deepStrictEqual(phoneDisplays({ display: { w: 393, h: 852, dpi: 160 } }).map((d) => [d.w, d.h]), [[393, 852]]);
assert.deepStrictEqual(phoneDisplays({}), []);
assert.deepStrictEqual(phoneDisplays({ display: { w: -1, h: 5 } }), []);

for (const os of ['windows', 'linux', 'mac']) {
  const LayoutSync = require(`../../${os}/mesh/layout-sync.js`);
  const nodes = {};
  const make = (id, opts = {}) => {
    const handlers = {};
    const n = { id, changes: 0, handlers };
    n.ls = new LayoutSync({
      bus: { on: (t, fn) => { handlers[t] = fn; }, onPeer: () => {} },
      mesh: { peers: () => Object.keys(nodes).filter((x) => x !== id).map((x) => ({ id: x, mesh: true, send: (m) => { const h = nodes[x].handlers[m.t]; if (h) h(m, id); } })) },
      selfId: id,
      storage: { get: () => null, set: () => {} },
      knownIds: () => Object.keys(nodes).concat(['tab']),
      onChange: () => { n.changes++; },
      myDisplays: () => [{ id: '1', x: 0, y: 0, w: 1920, h: 1080 }],
      endsChain: (x) => x === 'tab', // the phone
      ...opts,
    });
    nodes[id] = n;
    return n;
  };
  let tablet = { tab: [{ id: 'screen', x: 0, y: 0, w: 800, h: 1280 }] };
  const a = make('pc-a', { phoneDisplays: () => tablet });
  const b = make('pc-b');
  assert.deepStrictEqual(a.ls.allDisplays().tab.map((d) => [d.w, d.h]), [[800, 1280]], os + ': the host draws its phone');
  a.ls.phonesChanged();
  assert.deepStrictEqual(b.ls.allDisplays().tab.map((d) => [d.w, d.h]), [[800, 1280]], os + ': the others get it');
  tablet = { tab: [{ id: 'screen', x: 0, y: 0, w: 1280, h: 800 }] }; // turned
  const before = b.changes;
  a.ls.phonesChanged();
  assert.deepStrictEqual(b.ls.allDisplays().tab.map((d) => [d.w, d.h]), [[1280, 800]], os + ': turned');
  assert.ok(b.changes > before);
  const quiet = b.changes;
  a.ls.phonesChanged(); // nothing changed: nothing sent
  assert.strictEqual(b.changes, quiet, os + ': unchanged is not sent again');
  // A computer can't describe another computer's screens as a phone
  nodes['pc-b'].handlers['layout.displays']({ t: 'layout.displays', o: 'pc-a', s: 99, displays: [{ id: '1', x: 0, y: 0, w: 1920, h: 1080 }], phones: { 'pc-b': [{ id: 'x', x: 0, y: 0, w: 10, h: 10 }] } }, 'pc-a');
  assert.ok(!b.ls.displays['pc-b'], os + ': only phones');
}
console.log('layout-phones: ok');