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.
// Chains of devices (docs/mesh.md, "Shared layout"): real Kvm instances with fake helpers on real sockets.
// Computers A, B, C sit in a row in the shared layout (A - B - C) and are all linked (A drives through a
// link to each). A drives B; B's pointer leaves to the right, A releases B and takes C; C's pointer
// leaves to the left, A takes B again; B's pointer leaves to the left, back home. A phone at the end of
// a chain is taken through kvm.local and cannot pass the input on.
// Run with: node linux/mesh/test/kvm-chain.test.js (NODE_PATH and ELECTRON_OVERRIDE_DIST_PATH as in CI)
'use strict';
const assert = require('assert');
const crypto = require('crypto');
const { EventEmitter } = require('events');
const P = require('../../../shared/protocol.js');
const { Mesh } = require('../../../shared/mesh.js');
const { Server } = require('../server.js');
const { Pairing } = require('../hostpair.js');
const { LinkManager } = require('../links.js');
const { Hub } = require('../hub.js');
const Kvm = require('../kvm.js');
const LayoutSync = require('../layout-sync.js');
const { normalize } = require('../settings.js');
const { runTests } = require('./lib/run.js');
const { loadTestCert } = require('./lib/testcert.js');
const { close, waitFor, pair, pairPhone, helloPhone, memoryStorage } = require('./lib/mesh.js');
const T = P.T;
const TRIANGLE = [['A', 'B'], ['A', 'C'], ['B', 'C']]; // [dialer, host]
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const tests = [];
const test = (name, fn) => tests.push([name, fn]);
// A computer: Server + Pairing + LinkManager + Mesh + Hub + LayoutSync. Extra ids for the layout (a phone
// this computer does not pair with, but places next to it) go in n.extra.
async function hubNode(name) {
const cert = loadTestCert();
const id = crypto.randomUUID();
const server = new Server({ key: cert.key, cert: cert.cert, port: 0 });
await new Promise((resolve) => server.once('listening', resolve));
const storage = memoryStorage();
const pairing = new Pairing(server, { id, name, fp: cert.fp }, { storage });
const links = new LinkManager({ device: { id, name }, storage, hostPairing: pairing });
links.bindServer(server);
const mesh = new Mesh({ selfId: id });
const hub = new Hub({ device: { id, name }, links, server, pairing, mesh });
const n = { name, id, port: server.port, fp: cert.fp, server, pairing, links, mesh, hub, log: [], kvm: null, extra: [], settings: normalize({ kvm: { mouse: true, keyboard: true } }) }; // saved settings with the cursor on (a fresh install has it off)
n.layoutSync = new LayoutSync({
bus: hub.bus,
mesh,
selfId: id,
storage: { get: () => n.settings.layout, set: (l) => { n.settings.layout = l; } },
knownIds: () => [...hub.peers().map((p) => p.id), ...n.extra],
onChange: () => { if (n.kvm) n.kvm.layoutChanged(); },
});
return n;
}
async function world(names, pairs) {
const N = {};
for (const name of names) N[name] = await hubNode(name);
for (const [a, b] of pairs) await pair(N[a], N[b]);
const up = (a, b) => { const s = a.hub.sessions.get(b.id); return !!s && s.connected; };
await waitFor(() => pairs.every(([a, b]) => up(N[a], N[b]) && up(N[b], N[a])), 'every pair linked on both sides');
return N;
}
const all = (N) => Object.values(N);
const closeAll = (N) => close(...all(N));
// The helper of a computer as Kvm sees it: send() is recorded, and the test emits its events.
class RecHelper extends EventEmitter {
constructor() {
super();
this.running = true;
this.error = '';
this.sent = [];
}
send(name, args) {
this.sent.push({ name, args });
}
last(name) {
for (let i = this.sent.length - 1; i >= 0; i--) if (this.sent[i].name === name) return this.sent[i].args;
return undefined;
}
count(name) {
return this.sent.filter((x) => x.name === name).length;
}
stop() {}
}
// A real Kvm on node n, with a recording helper and the kvm messages of its bus recorded.
function kvmOn(n) {
n.helper = new RecHelper();
n.kvm = new Kvm({
hub: n.hub,
layoutSync: n.layoutSync,
helper: n.helper,
clip: { sendIfChanged: () => true },
fileDrop: { prepareDrag: () => null, beginDrag() {}, cancelRelease() {} },
fileRecv: { onRelease() {} },
dragRecv: null,
settings: n.settings,
save: () => {},
onChange: () => {},
});
for (const t of [T.KVM_TAKE, T.KVM_RELEASE, T.KVM_HOME, T.KVM_ACCEPT, T.KVM_NEXT, T.K_MOVE]) {
n.hub.bus.on(t, (m, from) => n.log.push({ t, from, m }));
}
return n.kvm;
}
const got = (n, t) => n.log.filter((x) => x.t === t);
// A drives `to` through `side` of A (the helper saw the edge), and `to` accepts.
async function driveVia(A, to, side, offset = 0.5) {
const before = got(to, T.KVM_TAKE).length;
A.helper.emit('event', 'edge', { side, offset });
await waitFor(() => got(to, T.KVM_TAKE).length === before + 1, `${to.name} gets kvm.take`);
const take = got(to, T.KVM_TAKE)[before];
to.hub.bus.send(A.id, { t: T.KVM_ACCEPT, route: take.m.route });
await waitFor(() => A.kvm.mode === 'driving' && A.kvm.partner === to.id, `A drives ${to.name}`);
return take;
}
// The row A B C on A's layout, and the same on every node (the layout is synced).
async function row(N, cells = { A: [0, 0], B: [1, 0], C: [2, 0] }) {
const { A } = N;
for (const name of Object.keys(cells)) {
assert.strictEqual(A.layoutSync.place(N[name].id, ...cells[name]), true, `place ${name}`);
}
await waitFor(() => all(N).every((n) => n.layoutSync.get() && n.layoutSync.get().rev === A.layoutSync.get().rev), 'the layout on every computer');
}
test('A - B - C in a row: B hands the input on to the right (C), C hands it back to the left (B), B goes home to A', async () => {
const N = await world(['A', 'B', 'C'], TRIANGLE);
try {
const { A, B, C } = N;
kvmOn(A); kvmOn(B); kvmOn(C);
await row(N);
await waitFor(() => B.layoutSync.neighbors().left === A.id && B.layoutSync.neighbors().right === C.id, 'B sees A on its left and C on its right');
assert.deepStrictEqual(A.layoutSync.neighbors(), { left: null, right: B.id, top: null, bottom: null });
// A drives B through A's right edge.
const take = await driveVia(A, B, 'right', 0.4);
assert.strictEqual(take.m.side, 'left', 'B sees the take at its left');
assert.strictEqual(A.hub.selectedId, B.id);
// B is driven: its return watch covers both sides with a device behind them (left: A, right: C).
await waitFor(() => B.kvm.mode === 'driven', 'B is driven');
assert.deepStrictEqual(B.helper.last('armReturn').sides, ['left', 'right'], 'B watches the way back and the edge to C');
assert.strictEqual(B.helper.last('armReturn').on, true);
// B's pointer leaves to the right: A releases B and takes C (the same offset, the side opposite to right).
B.helper.emit('event', 'return', { side: 'right', offset: 0.3 });
await waitFor(() => got(C, T.KVM_TAKE).length === 1, 'C gets kvm.take');
const takeC = got(C, T.KVM_TAKE)[0].m;
assert.strictEqual(takeC.side, 'left', 'C sees the take at its left: the side opposite to B’s right');
assert.strictEqual(takeC.offset, 0.3, 'the same offset');
await waitFor(() => got(B, T.KVM_RELEASE).length === 1, 'B gets kvm.release');
assert.strictEqual(got(B, T.KVM_HOME).length, 0, 'B did not send kvm.home: it was passed on');
await waitFor(() => B.kvm.mode === 'home', 'B is home');
assert.strictEqual(B.helper.last('armReturn').on, false, 'B stops watching its edges');
await waitFor(() => A.kvm.mode === 'driving' && A.kvm.partner === C.id && A.hub.selectedId === C.id, 'A drives C');
assert.strictEqual(A.kvm.keyboardOnly, false, 'the keyboard owner role is kept');
assert.deepStrictEqual(C.helper.last('armReturn').sides, ['left'], 'C watches only its edge to B');
// C's pointer leaves to the left, towards B (not the owner A): A takes B again, from the right.
C.helper.emit('event', 'return', { side: 'left', offset: 0.6 });
await waitFor(() => got(B, T.KVM_TAKE).length === 2, 'B is taken again');
const takeB = got(B, T.KVM_TAKE)[1].m;
assert.strictEqual(takeB.side, 'right', 'B sees the take at its right');
assert.strictEqual(takeB.offset, 0.6);
await waitFor(() => got(C, T.KVM_RELEASE).length === 1, 'C gets kvm.release');
await waitFor(() => A.kvm.partner === B.id && A.hub.selectedId === B.id && A.kvm.mode === 'driving', 'A drives B again');
await waitFor(() => C.kvm.mode === 'home', 'C is home');
assert.strictEqual(got(C, T.KVM_HOME).length, 0);
// B's pointer leaves to the left, back to A: kvm.home, and A is home.
B.helper.emit('event', 'return', { side: 'left', offset: 0.2 });
await waitFor(() => A.kvm.mode === 'home', 'A is home');
await waitFor(() => B.kvm.mode === 'home', 'B is home');
assert.strictEqual(got(A, T.KVM_HOME).length, 1, 'A got kvm.home from B');
assert.strictEqual(A.hub.selectedId, B.id, 'going home keeps the selection');
assert.strictEqual(A.helper.last('capture').on, false, 'A stops capturing');
snaps(N);
} finally {
closeAll(N);
}
});
test('kvm.next from the wrong device or to the wrong device is ignored: A keeps driving B', async () => {
const N = await world(['A', 'B', 'C'], TRIANGLE);
try {
const { A, B, C } = N;
kvmOn(A); kvmOn(B); kvmOn(C);
await row(N);
await driveVia(A, B, 'right');
const route = A.kvm.route;
const next = (from, body) => from.hub.bus.send(A.id, { t: T.KVM_NEXT, route, offset: 0.5, ...body });
const wrong = [
{ to: A.id, side: 'right' }, // A is on B’s left, not on its right
{ to: C.id, side: 'left' }, // C is on B’s right, not on its left
{ to: C.id, side: 'top' }, // no device on top
{ to: B.id, side: 'right' }, // to itself
{ to: C.id, side: 'diagonal' }, // not a side
{ side: 'right' }, // no device named
{ to: C.id, side: 'right', route: route + 5 }, // a route A is not in
];
for (const body of wrong) {
next(B, body);
await sleep(60);
assert.strictEqual(A.kvm.partner, B.id, `ignored: ${JSON.stringify(body)}`);
assert.strictEqual(got(C, T.KVM_TAKE).length, 0, `no take of C: ${JSON.stringify(body)}`);
assert.strictEqual(A.kvm.mode, 'driving');
}
// The right one takes C (so the checks above were not passing for the wrong reason).
next(B, { to: C.id, side: 'right' });
await waitFor(() => got(C, T.KVM_TAKE).length === 1, 'the valid kvm.next takes C');
// A drives C now. B is no longer driven by A, so a move asked by B is ignored: no release of C, no take.
assert.strictEqual(A.kvm.partner, C.id);
B.hub.bus.send(A.id, { t: T.KVM_NEXT, route: A.kvm.route, to: C.id, side: 'right', offset: 0.5 });
await sleep(200);
assert.strictEqual(A.kvm.partner, C.id, 'still driving C');
assert.strictEqual(got(C, T.KVM_RELEASE).length, 0, 'C was not released');
assert.strictEqual(got(B, T.KVM_TAKE).length, 1, 'no second take of B');
assert.strictEqual(got(C, T.KVM_TAKE).length, 1, 'no second take of C');
snaps(N);
} finally {
closeAll(N);
}
});
test('kvm.next is ignored while nothing is driven, and from a device that is not driven (no chain without a drive)', async () => {
const N = await world(['A', 'B', 'C'], TRIANGLE);
try {
const { A, B, C } = N;
kvmOn(A); kvmOn(B); kvmOn(C);
await row(N);
B.hub.bus.send(A.id, { t: T.KVM_NEXT, route: 1, to: C.id, side: 'right', offset: 0.5 });
await sleep(150);
assert.strictEqual(A.kvm.mode, 'home');
assert.strictEqual(got(C, T.KVM_TAKE).length, 0, 'nothing taken');
snaps(N);
} finally {
closeAll(N);
}
});
test('the device in the middle drops while driven: the owner goes home; a drop of it after the chain moved on leaves the owner on C', async () => {
const N = await world(['A', 'B', 'C'], TRIANGLE);
try {
const { A, B, C } = N;
kvmOn(A); kvmOn(B); kvmOn(C);
await row(N);
await driveVia(A, B, 'right');
A.links.get(B.id).ws.terminate();
await waitFor(() => A.kvm.mode === 'home', 'A home when B dropped');
assert.match(A.kvm.message, /disconnected/);
snaps(N);
// B is back, the chain runs to C, and then B drops: A keeps driving C.
await waitFor(() => A.hub.sessions.get(B.id).connected && B.hub.sessions.get(A.id).connected, 'B is back');
await driveVia(A, B, 'right');
B.helper.emit('event', 'return', { side: 'right', offset: 0.5 });
await waitFor(() => A.kvm.partner === C.id && A.kvm.mode === 'driving', 'A drives C');
A.links.get(B.id).ws.terminate();
await waitFor(() => !A.hub.sessions.get(B.id).connected, 'B dropped on A');
assert.strictEqual(A.kvm.mode, 'driving', 'still driving C');
assert.strictEqual(A.kvm.partner, C.id);
A.kvm.home();
await waitFor(() => A.kvm.mode === 'home', 'A home');
snaps(N);
} finally {
closeAll(N);
}
});
test('a phone at the end of a chain: taken through kvm.local, and it cannot pass the input on', async () => {
const N = await world(['A', 'B', 'C'], TRIANGLE);
let phone = null;
try {
const { A, B, C } = N;
kvmOn(A); kvmOn(B); kvmOn(C);
// The phone pairs with A and is placed to the right of B; B knows the phone's place in the layout.
phone = await pairPhone(A, { name: 'Phone' });
const w = await helloPhone(phone.client, { id: phone.id, token: phone.token });
assert.strictEqual(w.t, P.T.WELCOME);
await waitFor(() => A.hub.sessions.get(phone.id) && A.hub.sessions.get(phone.id).connected, 'the phone is connected to A');
B.extra = [phone.id];
const localLog = [];
A.kvm.local = {
drives: () => A.hub.selectedId === phone.id,
handle: (m) => {
localLog.push(m);
if (m.t === T.KVM_TAKE) setImmediate(() => A.hub.sessions.get(phone.id).inject({ t: T.KVM_ACCEPT, route: m.route }));
},
};
assert.ok(A.layoutSync.place(A.id, 0, 0) && A.layoutSync.place(B.id, 1, 0), 'A and B placed');
assert.ok(A.layoutSync.place(phone.id, 2, 0), 'the phone placed right of B');
assert.ok(A.layoutSync.place(C.id, 0, 1), 'C placed below A');
await waitFor(() => B.layoutSync.neighbors().right === phone.id && B.layoutSync.neighbors().left === A.id, 'B sees the phone on its right');
await driveVia(A, B, 'right');
B.helper.emit('event', 'return', { side: 'right', offset: 0.4 });
await waitFor(() => localLog.some((m) => m.t === T.KVM_TAKE), 'the phone is taken through kvm.local');
const take = localLog.find((m) => m.t === T.KVM_TAKE);
assert.strictEqual(take.side, 'left', 'the phone sees the take at its left');
assert.strictEqual(take.offset, 0.4);
await waitFor(() => A.kvm.mode === 'driving' && A.kvm.partner === phone.id, 'A drives the phone');
assert.strictEqual(A.hub.selectedId, phone.id);
assert.strictEqual(got(B, T.KVM_RELEASE).length, 1, 'B was released');
assert.strictEqual(B.kvm.mode, 'home');
// Input goes to the phone through kvm.local, not to B.
A.helper.emit('event', 'ev', { ev: { t: T.K_MOVE, dx: 3, dy: 4 } });
await waitFor(() => localLog.some((m) => m.t === T.K_MOVE), 'the move goes to the phone');
assert.strictEqual(got(B, T.K_MOVE).length, 0);
// The phone has no edges of its own: a kvm.next from it is ignored, even towards B (on its left).
const route = take.route;
phone.client.send({ t: T.KVM_NEXT, route, to: B.id, side: 'left', offset: 0.5 });
await sleep(200);
assert.strictEqual(A.kvm.partner, phone.id, 'still driving the phone');
assert.strictEqual(got(B, T.KVM_TAKE).length, 1, 'B was not taken again');
assert.strictEqual(A.kvm.mode, 'driving');
A.kvm.home();
await waitFor(() => localLog.some((m) => m.t === T.KVM_RELEASE), 'the phone gets kvm.release through kvm.local');
assert.strictEqual(A.kvm.mode, 'home');
snaps(N);
} finally {
if (phone) phone.client.close();
closeAll(N);
}
});
function snaps(N) {
for (const n of Object.values(N)) {
const s = n.hub.snapshot();
assert.ok(s && Array.isArray(s.peers), `${n.name}: snapshot has peers`);
}
}
runTests(tests);