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. GPL-3.0, see LICENSE.
// The mesh's computers for a phone (every build): their names, whether each is connected, and a laptop's
// battery, on the phone's Overview › Computers and its share sheet (a phone's files sent on through the
// computer it's connected to, shared/files-relay.js). Read only: a phone manages nothing in the mesh
// (no power, pairings, layout or unlock of other computers); it unlocks only the computer it's connected
// to, directly (Full KVM, shared/fullkvm/unlock-host.js). Mesh managers are computers
// (shared/mesh-mgr.js).
//
// Every computer tells the others its battery (a pool, `mesh.dev {batt}`), so the phone's computer can show
// the whole mesh.
// mesh.ask {} ← phone: send me the view (now, and on every change for ASK_MS)
// mesh.view {me, devices} → phone (each device: {id, name, os, self, online, battery?, charging?})
// mesh.do {rid, …} ← an older phone: mesh.done {rid, ok: false, why: 'off'}
//
// const v = meshView({ bus, mesh, selfId, name(), os, isPhone(id), devices() → [{id, name, os, online}], battery?() → {level, charging}, onChange() })
// v.tick() with every state push: publish ours when it changed (and every minute)
// v.view() what the phone gets (also for tests)
'use strict';
const { Pool } = require('./mesh.js');
const T = { ASK: 'mesh.ask', VIEW: 'mesh.view', DO: 'mesh.do', DONE: 'mesh.done', DEV: 'mesh.dev' };
const REPUBLISH_MS = 60000;
const ASK_MS = 90000; // a phone that asked hears changes this long (it asks again while its page is open)
const MAX = 32;
const str = (v, n) => (typeof v === 'string' ? v.slice(0, n) : '');
// A laptop's battery: {level 0–100, charging}, or null (none, or not said).
function cleanBatt(b) {
if (!b || typeof b !== 'object') return null;
const level = Number.isInteger(b.level) ? b.level : b.battery;
return Number.isInteger(level) && level >= 0 && level <= 100 ? { level, charging: b.charging === true } : null;
}
class MeshView {
constructor(o) {
Object.assign(this, { onChange: () => {}, battery: () => null }, o);
this.pool = new Pool({ name: 'manage', selfId: this.selfId, peers: () => this.mesh.peers().filter((e) => e.mesh && !this.isPhone(e.id)).map((e) => ({ id: e.id, mesh: true, pool: true, send: (msg) => e.send(msg) })) });
this.batts = new Map(); // computer id → {batt, at}
this.watchers = new Map(); // phone id → when it last asked
this.viewKey = new Map(); // phone id → the last view sent
this.sentKey = '';
this.sentAt = 0;
this.bus.on(T.DEV, (m, from) => this._heardDev(m, from));
this.bus.on(T.ASK, (m, from) => this._asked(from));
this.bus.on(T.DO, (m, from) => { if (this.isPhone(from)) this.bus.send(from, { t: T.DONE, rid: str(m && m.rid, 40), ok: false, why: 'off' }); });
this.bus.onPeer('welcome', () => { this.sentKey = ''; this.tick(); });
this.bus.onPeer('closed', (id) => { this.watchers.delete(id); this.viewKey.delete(id); this.push(); });
}
_mine() {
const b = cleanBatt(this.battery());
return b ? { batt: b } : {};
}
tick() {
const ours = this._mine();
const key = JSON.stringify(ours);
if (key !== this.sentKey || Date.now() - this.sentAt >= REPUBLISH_MS) {
const changed = key !== this.sentKey;
this.sentKey = key;
this.sentAt = Date.now();
this.pool.publish({ t: T.DEV, ...ours });
if (changed) this.push();
}
}
_heardDev(m, from) {
if (this.isPhone(from)) return;
const r = this.pool.receive(from, m);
if (!r.fresh || !r.msg || typeof r.msg.o !== 'string' || r.msg.o === this.selfId) return;
this.batts.set(r.msg.o, { batt: cleanBatt(m.batt), at: Date.now() });
this.push();
}
_asked(from) {
if (!this.isPhone(from)) return;
this.watchers.set(from, Date.now());
this.viewKey.delete(from);
this._send(from);
}
// Every phone still looking gets the view again when it changed.
push() {
const now = Date.now();
for (const [id, at] of this.watchers) {
if (now - at > ASK_MS) { this.watchers.delete(id); this.viewKey.delete(id); continue; }
this._send(id);
}
}
_send(phone) {
const v = this.view();
const k = JSON.stringify(v);
if (this.viewKey.get(phone) === k) return;
this.viewKey.set(phone, k);
this.bus.send(phone, { t: T.VIEW, ...v });
}
view() {
const b = (x) => (x ? { battery: x.level, charging: x.charging } : {});
const devices = [{ id: this.selfId, name: str(this.name(), 64), os: this.os, self: true, online: true, ...b(cleanBatt(this.battery())) }];
for (const d of (this.devices() || []).filter((x) => x && x.id && x.id !== this.selfId && !this.isPhone(x.id)).slice(0, MAX)) {
const s = this.batts.get(d.id);
devices.push({ id: d.id, name: str(d.name, 64) || d.id.slice(0, 8), os: str(d.os, 10), self: false, online: !!d.online, ...(d.online && s ? b(s.batt) : {}) });
}
return { me: this.selfId, devices };
}
}
// Windows, Linux and the Mac: from a mesh hub (*/mesh/hub.js) and this computer's DevLocal (its battery).
function onHub({ hub, mesh, selfId, name, os, devLocal, onChange }) {
const P = require('./protocol.js');
const peer = (id) => (hub.sessions && hub.sessions.get(id)) || null;
return new MeshView({
bus: hub.bus, mesh, selfId, name, os, onChange,
isPhone: (id) => { const s = peer(id); return !!s && P.isPhoneOs(s.peerOs); },
devices: () => hub.peers({ indirect: true }).filter((d) => !P.isPhoneOs(d.os)).map((d) => ({ id: d.id, name: d.name, os: d.os, online: !!d.connected || d.state === 'via' })),
battery: () => (devLocal && devLocal.batteryNow ? devLocal.batteryNow() : null),
});
}
module.exports = { MeshView, meshView: (o) => new MeshView(o), onHub, T };