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.
JavaScript androidapple-ecosystemconnectelectronfile-sharingintel-unison-alternativeiosios-windowskde-connect-alternativekvmlinuxmacbookmacosmouse-sharingphone-link-alternativeproductivityscreen-sharingsidecar-alternativesynergy-alternativewindows
// TwinDesk — Copyright (C) 2026 inukaasith, Erdoğan Yeşil. GPL-3.0, see LICENSE.
// Share this Mac's screen to several devices at once (docs/screen-mesh.md). Every connected device gets a
// Stream of its own (stream.js) on a peer bound to that device, so each target has its own WebRTC
// session, policy and display. A target follows the shared settings (`sharing.on`, `sharing.displayId`,
// the virtual display) until it is given a choice of its own: none, mirror of a display, or the
// virtual display. The stream table (shared/screen-mesh.js) checks the choices against this Mac's displays.
//
// This object is also the single-stream API the rest of the app grew up with (`stream`): restart(),
// stop(), `view` and `audio` are the ACTIVE target's (the selected device), so with one device
// everything behaves as before. restart() and stop() reach every target.
//
// Events: 'change'.
'use strict';
const { EventEmitter } = require('events');
const S = require('../../shared/screen-mesh.js');
const { ScopedPhonePeer: ScopedPeer } = require('./phones.js');
const { channel, MAX: MAX_CH } = require('../../shared/screen-ch.js');
class StreamSet extends EventEmitter {
// hub: hub.js; server: the Server; make(peer) -> Stream; displays: Displays (model()).
constructor({ hub, server, make, displays }) {
super();
Object.assign(this, { hub, server, make, displays });
this.byId = new Map();
this.all = [];
this.table = {};
this.extras = new Map(); // device id -> Map(ch -> Stream): more screens to a device that takes them (shared/screen-ch.js)
this.first = this._create();
// An added virtual display made again (sharing off and on) can come back with a new id: the devices
// it was shared to keep it.
if (typeof displays.on === 'function') displays.on('extra-moved', (from, to) => {
for (const st of this.all) if (st.choice && st.choice.mode === 'mirror' && Number(st.choice.displayId) === from) st.choice.displayId = to;
for (const c of Object.values(this.table)) if (c.mode === 'mirror' && c.displayId === String(from)) c.displayId = String(to);
this.emit('change');
});
hub.bus.onPeer('welcome', (id) => this._ensure(id));
hub.bus.onPeer('closed', (id) => this._gone(id));
for (const s of hub.sessions.list()) if (s.connected) this._ensure(s.id);
}
_create() {
const peer = new ScopedPeer(this.hub, this.server, null);
const st = this.make(channel(peer, 0)); // stream 0: what it always was; another stream's messages aren't for it
st.peer = peer;
st.audioOk = () => st === this.active;
st.on('change', () => this.emit('change'));
this.all.push(st);
return st;
}
_ensure(id) {
if (this.byId.has(id)) return;
const st = this.first.peer.id === null ? this.first : this._create();
st.peer.bind(id);
this.byId.set(id, st);
this.emit('change');
}
_gone(id) {
delete this.table[id];
for (const x of (this.extras.get(id) || new Map()).values()) if (x.choice && x.choice.mode !== 'none') { x.choice = { mode: 'none' }; x.restart('device gone'); }
this.emit('change');
}
get active() {
return this.byId.get(this.hub.selectedId) || this.first;
}
// That device watches this Mac's stream now (its Stream has a session): its input is taken (input.js).
watching(id) {
const st = id && this.byId.get(id);
return !!(st && st.active);
}
get view() {
const a = this.active;
return {
...a.view,
deviceId: a.peer.id,
displays: S.normalizeDisplays(this.displays.model()),
targets: [...this.byId].filter(([id]) => (this.hub.sessions.get(id) || {}).connected).map(([id, st]) => {
const c = st.choice;
return { id, name: this.hub.sessions.get(id).displayName(), mode: c ? c.mode : 'default', displayId: c && c.displayId ? String(c.displayId) : '', place: (c && c.place) || '', multi: !!st.multi,
more: this.extrasOf(id).map((e) => ({ ch: e.ch, monitor: String(e.choice.displayId), place: e.choice.place || '', status: e.stream.view.status })), status: st.view.status, active: st === a };
}),
};
}
get audio() {
return this.active.audio;
}
// choice: { mode: 'default'|'none'|'mirror'|'virtual', displayId? }. 'default' follows the shared settings again.
setTarget(id, choice) {
const st = this.byId.get(id);
if (!st) return { ok: false, error: 'device' };
if (choice && (choice.add || choice.ch)) return this._setExtra(id, st, choice);
const ids = [...this.byId.keys()];
const displays = S.normalizeDisplays(this.displays.model());
if (!choice || choice.mode === 'default') {
delete this.table[id];
st.choice = null;
} else {
const want = choice.mode === 'virtual' ? { mode: 'virtual', size: { w: 1920, h: 1080 } }
: choice.mode === 'mirror' ? { mode: 'mirror', displayId: String(choice.displayId) } : { mode: choice.mode };
const r = S.setChoice(this.table, id, want, ids, displays);
if (!r.ok) return r;
this.table = r.table;
st.choice = choice.mode === 'mirror' ? { mode: 'mirror', displayId: Number(choice.displayId) } : { mode: choice.mode };
if (choice.mode !== 'none' && typeof choice.place === 'string' && choice.place) st.choice.place = choice.place.slice(0, 40); // which of its monitors it fills there (the sharing map)
}
if (typeof st.noteViewIdentity === 'function') st.noteViewIdentity();
// TwinDesk Extend (the map's Extend, or a viewer that asks for it) is there while any device is on it,
// whatever Screen › Mode says; the picture starts once it exists.
const extend = [...this.byId.values()].some((x) => x.choice && x.choice.mode === 'virtual');
const d = this.displays;
const turns = !!(d && typeof d.hold === 'function' && d.holds && d.holds.has('extend-target') !== extend);
if (turns) d.hold('extend-target', extend).then(() => st.restart('target choice'), () => st.restart('target choice'));
else st.restart('target choice');
this.emit('change');
return { ok: true };
}
// Another screen of this Mac to the same device (choice.add, or choice.ch): only while that device's
// viewer takes them (screen.policy multi) and it already gets this Mac's screen; mirror only. Made once
// per (device, ch) and kept, at most MAX_CH per device. Its screen.state goes out with the others'.
_setExtra(id, base, choice) {
let map = this.extras.get(id);
const ch = Number(choice.ch) || 0;
if (choice.mode === 'none' || choice.mode === 'off') {
const x = map && map.get(ch);
if (x) { x.choice = { mode: 'none' }; x.restart('target choice'); this.emit('change'); }
return { ok: true };
}
if (!base.multi || (base.choice && base.choice.mode === 'none')) return { ok: false, error: 'multi' };
const displayId = Number(choice.displayId);
if (!S.normalizeDisplays(this.displays.model()).some((d) => Number(d.id) === displayId)) return { ok: false, error: 'display' };
if (!map) this.extras.set(id, (map = new Map()));
let n = ch;
if (!n) for (let i = 1; i <= MAX_CH && !n; i++) if (!map.get(i) || !map.get(i).choice || map.get(i).choice.mode === 'none') n = i;
if (!n || n > MAX_CH) return { ok: false, error: 'full' };
let x = map.get(n);
const fresh = !x;
if (fresh) {
const peer = new ScopedPeer(this.hub, this.server, null);
x = this.make(channel(peer, n));
x.peer = peer;
x.ch = n;
x.audioOk = () => false; // the sound goes with stream 0
x.on('change', () => this.emit('change'));
this.all.push(x);
map.set(n, x);
}
x.choice = { mode: 'mirror', displayId, ...(typeof choice.place === 'string' && choice.place ? { place: choice.place.slice(0, 40) } : {}) };
if (fresh) x.peer.bind(id); // its 'client' now: it waits for its viewer's screen.policy
else x.restart('target choice');
this.emit('change');
return { ok: true, ch: n };
}
// The extra streams of a device that are on: [{ch, choice}] (screen.state, the page).
extrasOf(id) {
return [...(this.extras.get(id) || new Map())].filter(([, x]) => x.choice && x.choice.mode === 'mirror').map(([ch, x]) => ({ ch, choice: x.choice, stream: x }));
}
// Every device's selection epoch, after the shared display changes.
noteViewIdentity() {
for (const st of this.all) if (st && typeof st.noteViewIdentity === 'function') st.noteViewIdentity();
}
restart(reason) {
return Promise.all(this.all.map((st) => st.restart(reason)));
}
stop(status) {
for (const st of this.all) st.stop(status);
}
}
module.exports = { StreamSet };