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.
// Full KVM on Linux, the app's side (docs/full-kvm.md), the Full KVM build only (main.js attaches
// it when shared/edition.js says so): the Full KVM tab's state and its Off / Signed in / Always,
// which install.sh carries out as root through pkexec (one approval per change).
// Biometric unlock (shared/fullkvm/unlock-host.js): Add phone gives its key to install.sh unlock-add.
// require('./fullkvm/manager.js').attach({ helper, link | hub, name() })
'use strict';
const fs = require('fs');
const path = require('path');
const { execFile } = require('child_process');
const { app, ipcMain, session } = require('electron');
const UNIT = '/etc/systemd/system/twindesk-fullkvm.service';
const CONF = '/etc/twindesk/fullkvm.json';
const SCRIPT = path.join(__dirname, 'install.sh');
const APP_ROOT = path.join(__dirname, '..', '..');
const MODES = ['off', 'simple', 'system'];
function run(cmd, args, ms = 10000) {
return new Promise((resolve) => execFile(cmd, args, { timeout: ms, maxBuffer: 1 << 20 }, (err, out, errOut) =>
resolve({ code: err ? (typeof err.code === 'number' ? err.code : -1) : 0, out: String(out || ''), err: String(errOut || '').trim() })));
}
function readConf() {
try { return JSON.parse(fs.readFileSync(CONF, 'utf8')) || {}; } catch (_) { return {}; }
}
function readMode() {
if (!fs.existsSync(UNIT)) return 'off';
return readConf().mode === 'system' ? 'system' : 'simple';
}
// NixOS (docs/nixos.md): the unit, its config (marked "declarative") and the account are the system
// configuration's (programs.twindesk.fullKvm), and /etc/systemd and /usr/local can't be written, so
// install.sh has nothing to do here: the tab shows what the configuration says and changes nothing.
function declarative() {
return readConf().declarative === true;
}
function nixos() {
return declarative() || fs.existsSync('/etc/NIXOS');
}
let helper = null;
let lastError = '';
async function state() {
const mode = readMode();
const active = mode !== 'off' && (await run('systemctl', ['is-active', 'twindesk-fullkvm.service'])).out.trim() === 'active';
return { os: 'linux', mode, active, connected: !!(helper && helper.viaService), error: lastError, ...(declarative() ? { declarative: true } : {}) };
}
// install.sh as root: its text through `pkexec sh -c` (inside an AppImage root can't read our mount).
async function asRoot(args) {
const script = fs.readFileSync(SCRIPT, 'utf8');
const r = await run('pkexec', ['sh', '-c', script, 'install.sh', ...args], 180000);
if (r.code === 0) return;
throw new Error(r.code === 126 || r.code === 127 ? 'The administrator’s approval was not given' : r.err.split('\n').pop() || 'Full KVM could not be changed');
}
async function set(mode) {
if (!MODES.includes(mode)) throw new Error('bad mode');
const now = readMode();
lastError = '';
try {
if (mode === now) return state();
if (nixos()) {
throw new Error(declarative()
? 'Full KVM is set in the NixOS configuration (programs.twindesk.fullKvm and fullKvmMode); change it there and rebuild'
: 'On NixOS, turn Full KVM on in the configuration (programs.twindesk.fullKvm = true) and rebuild');
}
const cfg = app.getPath('userData'); // System-wide: our certificate, device id and pairings for the machine node
if (mode === 'off') await asRoot(['remove']);
else if (now === 'off') {
const appImage = process.env.APPIMAGE;
await asRoot(['install', mode, appImage || process.execPath, appImage ? '@APPDIR' : APP_ROOT, cfg]);
} else await asRoot(['mode', mode, cfg]);
} catch (e) {
lastError = e.message;
}
return state();
}
// System-wide: while seat0 isn't ours (the login screen, another person), the machine node or their
// TwinDesk is this computer to the mesh, and we must not hold the network (discovery's port, the links
// to the same devices with the same id). On X11 an app in a session off screen can't even notice: its
// main thread waits on the display (seen in the VM: the Mac's link went silent, discovery's queue
// filled). So a guard outside us does it: plain sh as this user (no AppImage mount, which ends with
// us), started with the app. With System-wide on and seat0 someone else's for 3 s, it ends TwinDesk;
// once seat0 is ours again it starts it as it was. TwinDesk quit by the person (seat0 ours) stays quit.
function guard() {
const q = (a) => "'" + String(a).replace(/'/g, "'\\''") + "'";
const exe = process.env.APPIMAGE || process.execPath;
const again = 'cd ' + q(process.cwd()) + ' && exec ' + [exe, ...process.argv.slice(1)].map(q).join(' ');
const sh = `pid=${process.pid}; uid=${process.getuid()}
ours() { s=$(loginctl show-seat seat0 -p ActiveSession --value 2>/dev/null); [ -z "$s" ] && return 0; [ "$(loginctl show-session "$s" -p User --value 2>/dev/null)" = "$uid" ]; }
system() { grep -q '"system"' ${CONF} 2>/dev/null && [ -e ${UNIT} ]; }
away=0
while kill -0 $pid 2>/dev/null; do
sleep 1
if system && ! ours; then away=$((away + 1)); else away=0; fi
if [ $away -ge 3 ]; then
kill -TERM $pid 2>/dev/null; sleep 2; kill -KILL $pid 2>/dev/null
break
fi
done
[ $away -ge 3 ] || exit 0
until ours; do sleep 2; done
sleep 1
${again}`;
const { spawn } = require('child_process');
const env = { ...process.env };
delete env.ELECTRON_RUN_AS_NODE;
// Its own unit (systemd-run --user), so it outlives whatever started TwinDesk (a launcher's scope
// is torn down with the app); plain detached where there is no user manager.
const fs2 = require('fs');
const bin = ['/usr/bin/systemd-run', '/bin/systemd-run'].find((f) => fs2.existsSync(f));
const p = bin
? spawn(bin, ['--user', '--collect', '--quiet', '--unit', 'twindesk-fullkvm-guard-' + process.pid,
// The unit has the user manager's environment, not ours: what starting TwinDesk again needs.
...['DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'XDG_SESSION_TYPE', 'XDG_CURRENT_DESKTOP', 'DBUS_SESSION_BUS_ADDRESS', 'XDG_RUNTIME_DIR', 'APPIMAGE', 'HOME', 'PATH', 'LANG']
.filter((k) => env[k]).map((k) => '--setenv=' + k + '=' + env[k]),
'/bin/sh', '-c', sh], { detached: true, stdio: 'ignore', env })
: spawn('/bin/sh', ['-c', sh], { detached: true, stdio: 'ignore', env });
p.on('exit', (code) => { if (code && bin) spawn('/bin/sh', ['-c', sh], { detached: true, stdio: 'ignore', env }).unref(); });
p.unref();
}
// Biometric unlock: the phone's key for this person, and pam_twindesk.so for the login screen. The
// module is copied out first (root can't read inside our AppImage), to a file only we can write.
const PAM_SO = path.join(__dirname, 'bin', 'pam_twindesk.so');
let unlock = null;
async function unlockAdd({ key, kind, pub, name }) {
if (readMode() === 'off') throw new Error('Turn on Full KVM first');
if (nixos()) throw new Error('On NixOS, Biometric unlock isn’t available yet');
if (!fs.existsSync(PAM_SO)) throw new Error('This build has no pam_twindesk.so');
const dir = fs.mkdtempSync(path.join(require('os').tmpdir(), 'twindesk-unlock-'));
const so = path.join(dir, 'pam_twindesk.so');
try {
fs.copyFileSync(PAM_SO, so);
await asRoot(['unlock-add', key, kind, pub, so, String(name || 'Phone').replace(/[^A-Za-z0-9 ._()-]/g, '').slice(0, 64) || 'Phone']);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
function attach(ctx) {
helper = ctx.helper;
guard();
unlock = require('../../shared/fullkvm/unlock-host.js').unlockHost({
helper, hub: ctx.hub, link: ctx.link, name: ctx.name || (() => require('os').hostname()),
on: () => !!(ctx.settings && ctx.settings().fullkvm && ctx.settings().fullkvm.unlock),
asks: () => !!(ctx.settings && ctx.settings().fullkvm && ctx.settings().fullkvm.unlockAsk === true),
phones: () => !!(ctx.settings && ctx.settings().fullkvm && ctx.settings().fullkvm.unlockPhones), // Allow phones (off: turned on after a warning)
computers: () => !!(ctx.settings && ctx.settings().fullkvm && ctx.settings().fullkvm.unlockComputers), // Allow computers (off)
may: ctx.may, // Multi-device: a computer only as a mesh manager this one accepted for Unlock (shared/mesh-mgr.js)
enroll: unlockAdd,
remove: (key) => asRoot(['unlock-remove', key]),
});
ipcMain.handle('fullkvm', async (_e, op, arg, arg2) => {
if (op === 'set') return set(String(arg));
if (op === 'unlock') { await unlock.refresh(); return unlock.state(); }
if (op === 'unlockAdd') return unlock.add(String(arg));
if (op === 'unlockDrop') return unlock.drop(String(arg));
const KEY = { unlockOn: 'unlock', unlockAsk: 'unlockAsk', unlockPhones: 'unlockPhones', unlockComputers: 'unlockComputers' };
if (KEY[op] && ctx.settings) {
ctx.settings().fullkvm = { ...(ctx.settings().fullkvm || {}), [KEY[op]]: arg === true };
ctx.save();
await unlock.changed();
return unlock.state();
}
return state();
});
const preload = path.join(__dirname, 'preload.js');
const ses = session.defaultSession;
if (typeof ses.registerPreloadScript === 'function') ses.registerPreloadScript({ type: 'frame', id: 'twindesk-fullkvm', filePath: preload });
else ses.setPreloads([...ses.getPreloads(), preload]);
}
// What may unlock here changed outside the Full KVM tab (a mesh manager accepted or revoked): every
// connected device hears it now (shared/mesh-mgr.js).
function unlockChanged() {
if (unlock) unlock.changed().catch(() => {});
}
module.exports = { attach, state, set, unlockChanged };