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.4 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Jump shortcuts (Multi-device, Mouse › Screen and Keyboard): a shortcut of 2 to 4 keys per device that
// takes the pointer straight there (kvm.jumpTo), 'self' for this computer (everything home).
// settings.kvm.jumps = { deviceId | 'self': 'Control+Alt+1', … }
//
//   clean(v)                         the settings value, checked (at most MAX, each 2 to 4 keys, no repeats)
//   new JumpKeys(getJumps, onJump)   .intercept(ev) while driving: the keys this computer captures (as the
//                                    lock key: shared/cursorlock.js LockKey), true when used up here
//   new GlobalJumps(globalShortcut, onJump)  .sync(jumps or null): at home, Electron's global shortcuts,
//                                    held only while a device is connected; .errors {id: text}
'use strict';

const { LockKey } = require('./cursorlock.js');

const MAX = 16;
const MODS = new Set(['Control', 'Ctrl', 'Alt', 'Option', 'Shift', 'Command', 'Cmd', 'Super', 'Meta', 'CommandOrControl', 'CmdOrCtrl']);

// 2 to 4 keys: modifiers, then one key that isn't one.
function valid(acc) {
  if (typeof acc !== 'string' || acc.length > 60) return false;
  const parts = acc.split('+');
  if (parts.length < 2 || parts.length > 4 || parts.some((p) => !p)) return false;
  const key = parts[parts.length - 1];
  return !MODS.has(key) && parts.slice(0, -1).every((p) => MODS.has(p)) && new Set(parts).size === parts.length;
}

function clean(v) {
  const out = {};
  if (!v || typeof v !== 'object' || Array.isArray(v)) return out;
  const seen = new Set();
  for (const [id, acc] of Object.entries(v)) {
    if (Object.keys(out).length >= MAX) break;
    if (typeof id !== 'string' || !id || id.length > 80 || !valid(acc) || seen.has(acc)) continue;
    seen.add(acc);
    out[id] = acc;
  }
  return out;
}

class JumpKeys {
  constructor(getJumps, onJump) {
    this.getJumps = getJumps;
    this.onJump = onJump;
    this.keys = new Map(); // id → LockKey for its accelerator
  }

  intercept(ev) {
    const jumps = this.getJumps() || {};
    let used = false;
    for (const [id, acc] of Object.entries(jumps)) {
      let k = this.keys.get(id);
      if (!k || k.acc !== acc) { k = new LockKey(() => acc); k.acc = acc; this.keys.set(id, k); }
      const hit = k.intercept(ev);
      if (hit === 'press') this.onJump(id);
      if (hit) used = true;
    }
    for (const id of this.keys.keys()) if (!(id in jumps)) this.keys.delete(id);
    return used;
  }

  reset() { for (const k of this.keys.values()) k.reset(); }
}

class GlobalJumps {
  constructor(globalShortcut, onJump) {
    this.gs = globalShortcut;
    this.onJump = onJump;
    this.held = new Map(); // accelerator → id
    this.want = '';
    this.errors = {};
  }

  sync(jumps) {
    const want = JSON.stringify(jumps || {});
    if (want === this.want) return;
    this.want = want;
    for (const acc of this.held.keys()) { try { this.gs.unregister(acc); } catch (_) { /* gone */ } }
    this.held.clear();
    this.errors = {};
    for (const [id, acc] of Object.entries(jumps || {})) {
      let ok = false;
      try { ok = this.gs.register(acc, () => this.onJump(id)); } catch (_) { /* not an accelerator here */ }
      if (ok) this.held.set(acc, id);
      else this.errors[id] = 'Another app uses this shortcut';
    }
  }
}

module.exports = { clean, valid, JumpKeys, GlobalJumps, MAX };