/* PairSwitch (formerly LogiSwitch) explainer: a simulated Mac + PC sharing one keyboard and mouse. Nothing here talks to real devices or the network; it mirrors what the real tool does and logs. */ (function () { 'use strict'; var $ = function (id) { return document.getElementById(id); }; var reduce = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches; var T = function (ms) { return reduce ? Math.min(ms, 60) : ms; }; var sleep = function (ms) { return new Promise(function (r) { setTimeout(r, T(ms)); }); }; var st = { kb: 'mac', ms: 'mac', busy: false, pending: null, rejected: 0, connected: true, clipMac: '', clipPC: '' }; var phrases = ['Meeting moved to 3:30', 'https://example.com/report', 'Invoice 2026-114: $1,240.00', 'Grocery list: eggs, lemons, rice', 'café ☕ – naïve résumé', 'git commit -m "fix edge"']; var phraseAt = 0; var actBtns = ['bEdge', 'bKey', 'bMouse', 'bCopyMac', 'bCopyPC'].map($); /* ---------- log ---------- */ function stamp() { var d = new Date(), p = function (n) { return (n < 10 ? '0' : '') + n; }; return p(d.getHours()) + ':' + p(d.getMinutes()) + ':' + p(d.getSeconds()); } function log(msg, cls) { var li = document.createElement('li'); if (cls) li.className = cls; var t = document.createElement('span'); t.className = 't'; t.textContent = stamp(); li.appendChild(t); li.appendChild(document.createTextNode(msg)); var ol = $('log'); ol.appendChild(li); while (ol.children.length > 60) ol.removeChild(ol.firstChild); ol.scrollTop = ol.scrollHeight; } /* ---------- wire pane ---------- */ function hex(n) { return (n < 16 ? '0' : '') + n.toString(16).toUpperCase(); } function rnd(n) { var a = []; for (var i = 0; i < n; i++) a.push(hex(Math.floor(Math.random() * 256))); return a.join(' '); } function box(v, lbl, key) { var b = document.createElement('span'); b.className = 'b' + (key ? ' k' : ''); b.appendChild(document.createTextNode(v)); var i = document.createElement('i'); i.textContent = lbl; b.appendChild(i); return b; } function wireHid(dev) { var w = $('bytes'); w.textContent = ''; var cap = document.createElement('p'); cap.className = 'cap'; cap.textContent = 'HID++ 2.0 "Change Host" to the ' + dev + ' (20 bytes, over Bluetooth):'; var g = document.createElement('div'); g.className = 'grid'; g.appendChild(box('11', 'long report')); g.appendChild(box('FF', 'Bluetooth')); g.appendChild(box('0A', 'feature index', true)); g.appendChild(box('1B', 'set host', true)); g.appendChild(box('00', 'channel 1', true)); g.appendChild(box('00 ×15', 'padding')); var n = document.createElement('p'); n.className = 'wire'; n.textContent = 'The feature index is looked up once per device (where it keeps feature 0x1814); 0A is an example. The host byte counts from 0, so 00 means channel 1.'; w.appendChild(cap); w.appendChild(g); w.appendChild(n); } function wireClip(text, from) { var bytes = new TextEncoder().encode(text).length; var plain = 1 + 8 + 2 + 4 + bytes; // type, timestamp, format count + format, length, text var ct = (Math.floor(plain / 16) + 1) * 16; // AES-CBC with PKCS#7 padding var body = 16 + ct + 32; var w = $('bytes'); w.textContent = ''; var cap = document.createElement('p'); cap.className = 'cap'; cap.textContent = 'One encrypted message, ' + from + ' (' + (4 + body) + ' bytes for ' + bytes + ' bytes of text):'; var g = document.createElement('div'); g.className = 'grid'; g.appendChild(box(hex(body >> 24 & 255) + ' ' + hex(body >> 16 & 255) + ' ' + hex(body >> 8 & 255) + ' ' + hex(body & 255), 'length')); g.appendChild(box(rnd(3) + ' …', 'IV, 16 random bytes')); g.appendChild(box(rnd(3) + ' …', 'AES-256 ciphertext, ' + ct, true)); g.appendChild(box(rnd(3) + ' …', 'HMAC-SHA256 tag, 32', true)); var n = document.createElement('p'); n.className = 'wire'; n.textContent = 'Inside the ciphertext: type 0x10, the time in ms (anything over 2 minutes old is refused), then the text as UTF-8. A wrong pairing code fails the tag check, so the message is dropped unread. (Illustration: the bytes shown are random.)'; w.appendChild(cap); w.appendChild(g); w.appendChild(n); } /* ---------- drawing ---------- */ function setHost(which, host) { st[which] = host; var el = which === 'kb' ? $('devKb') : $('devMs'); el.setAttribute('data-host', host); $(which === 'kb' ? 'kbHost' : 'msHost').textContent = host === 'pc' ? 'on the PC' : 'on the Mac'; if (which === 'ms') { $('cursor').classList.toggle('off', host === 'pc'); $('cursorPC').classList.toggle('on', host === 'pc'); } } function flashDev(which) { var el = which === 'kb' ? $('devKb') : $('devMs'); el.classList.add('moving'); setTimeout(function () { el.classList.remove('moving'); }, T(900)); } async function flyPacket(label, to, hid) { var p = $('packet'); p.classList.remove('at-pc', 'at-mac', 'show', 'hid'); if (hid) p.classList.add('hid'); p.classList.add(to === 'pc' ? 'at-mac' : 'at-pc'); p.textContent = label; void p.offsetWidth; // restart the transition from the sender p.classList.add('show'); await sleep(60); p.classList.remove('at-mac', 'at-pc'); p.classList.add(to === 'pc' ? 'at-pc' : 'at-mac'); await sleep(950); p.classList.remove('show'); } function setClip(side, text, cls) { var box = $(side === 'mac' ? 'clipMac' : 'clipPC'); $(side === 'mac' ? 'clipMactxt' : 'clipPCtxt').textContent = text; st[side === 'mac' ? 'clipMac' : 'clipPC'] = text; if (cls) { box.classList.add(cls); setTimeout(function () { box.classList.remove(cls); }, T(900)); } } /* ---------- buttons ---------- */ function refresh() { var home = st.kb === 'mac' && st.ms === 'mac'; $('bEdge').disabled = st.busy || !home; $('bKey').disabled = st.busy || !home; $('bMouse').disabled = st.busy || !home; $('bBack').disabled = st.busy || home; $('bCopyMac').disabled = st.busy; $('bCopyPC').disabled = st.busy; $('bEdge').title = home ? '' : 'The mouse is on the PC now: bring both back first.'; } function run(fn) { return async function () { if (st.busy) return; st.busy = true; refresh(); try { await fn(); } finally { st.busy = false; refresh(); } }; } async function sendBoth(reason) { // mouse first, then keyboard, as the real tool does var order = ['ms', 'kb'].filter(function (w) { return st[w] === 'mac'; }); for (var i = 0; i < order.length; i++) { var w = order[i], name = w === 'kb' ? 'Keyboard' : 'Mouse'; wireHid(w === 'kb' ? 'keyboard' : 'mouse'); flashDev(w); await flyPacket('Change Host → 1', 'pc', true); setHost(w, 'pc'); log(name + ' left (part of our own switch)'); log(name + ' -> channel 1: sent', 'ok'); } if (reason) log(reason); } var doEdge = run(async function () { var c = $('cursor'), e = $('edge'); c.classList.add('atedge'); await sleep(600); e.classList.add('hot'); await sleep(250); log('cursor held against the left edge for 50 ms -> the PC is on the left, channel 1'); await sendBoth(); e.classList.remove('hot'); c.classList.remove('atedge'); }); async function follow(first) { var other = first === 'kb' ? 'ms' : 'kb'; var n1 = first === 'kb' ? 'Keyboard' : 'Mouse', n2 = other === 'kb' ? 'Keyboard' : 'Mouse'; var w = $('bytes'); w.textContent = ''; var p = document.createElement('p'); p.className = 'cap'; p.textContent = 'You pressed the ' + n1.toLowerCase() + "'s own button, so the device sent nothing through PairSwitch: it simply dropped off this Mac. PairSwitch saw it disappear."; w.appendChild(p); flashDev(first); setHost(first, 'pc'); log(n1 + ' switched away -> the other devices follow to channel 1'); await sleep(700); wireHid(other === 'kb' ? 'keyboard' : 'mouse'); flashDev(other); await flyPacket('Change Host → 1', 'pc', true); setHost(other, 'pc'); log(n2 + ' left (part of our own switch)'); log(n2 + ' -> channel 1: sent', 'ok'); } var doKey = run(function () { return follow('kb'); }); var doMouse = run(function () { return follow('ms'); }); var doBack = run(async function () { if (st.kb === 'mac' && st.ms === 'mac') return; var w = $('bytes'); w.textContent = ''; var p = document.createElement('p'); p.className = 'cap'; p.textContent = 'On the PC side, the same tool does the same thing the other way: push against its right edge, or press button 2 on either device.'; w.appendChild(p); ['kb', 'ms'].forEach(function (k) { if (st[k] === 'pc') flashDev(k); }); await sleep(300); setHost('kb', 'mac'); setHost('ms', 'mac'); await sleep(500); log('Keyboard and mouse are back on this Mac'); }); function nextPhrase() { var t = phrases[phraseAt % phrases.length]; phraseAt++; return t; } function linkUp() { return $('cSync').checked && $('cMatch').checked; } var doCopyMac = run(async function () { var t = nextPhrase(); setClip('mac', t, 'new'); if (!linkUp()) { st.pending = t; log('(not sent: the PC is not connected; it goes as soon as the PC connects)'); $('bytes').textContent = ''; var p = document.createElement('p'); p.className = 'cap'; p.textContent = $('cSync').checked ? 'The codes differ, so the Mac never accepted the PC and there is no connection to send on. The text waits.' : 'Sharing is off, so nothing leaves this Mac. The text waits in case you turn it on.'; $('bytes').appendChild(p); return; } wireClip(t, 'Mac to PC'); await flyPacket('encrypted · ' + t.length + ' chars', 'pc'); setClip('pc', t, 'new'); st.pending = null; log('clipboard: sent ' + t.length + ' characters to the PC', 'ok'); }); var doCopyPC = run(async function () { var t = nextPhrase(); setClip('pc', t, 'new'); if (!$('cSync').checked) { log('(sharing is off: the PC keeps it to itself)'); return; } wireClip(t, 'PC to Mac'); await flyPacket('encrypted · ' + t.length + ' chars', 'mac'); if (!$('cMatch').checked) { st.rejected++; $('clipMac').classList.add('bad'); setTimeout(function () { $('clipMac').classList.remove('bad'); }, T(900)); if (st.rejected === 1) log('ERROR: clipboard sync rejected a message (the two computers’ pairing codes do not match, or a clock is more than 2 minutes off); further rejections are not logged', 'err'); return; } setClip('mac', t, 'new'); log('clipboard: received ' + t.length + ' characters from the PC', 'ok'); }); async function relink() { if (!linkUp()) { if (!$('cSync').checked) log('clipboard sync: off'); else log('(the PC keeps dialling in every few seconds; the Mac refuses it)'); return; } if (st.rejected > 0) { log('clipboard sync: pairing codes now match (after ' + st.rejected + ' rejected attempts)', 'ok'); st.rejected = 0; } log('clipboard sync: the PC connected'); if (st.pending) { var t = st.pending; st.pending = null; st.busy = true; refresh(); try { wireClip(t, 'Mac to PC'); await flyPacket('encrypted · ' + t.length + ' chars', 'pc'); setClip('pc', t, 'new'); log('clipboard: sent ' + t.length + ' characters to the PC (copied while it was not connected)', 'ok'); } finally { st.busy = false; refresh(); } } } $('bEdge').addEventListener('click', doEdge); $('bKey').addEventListener('click', doKey); $('bMouse').addEventListener('click', doMouse); $('bBack').addEventListener('click', doBack); $('bCopyMac').addEventListener('click', doCopyMac); $('bCopyPC').addEventListener('click', doCopyPC); $('cSync').addEventListener('change', relink); $('cMatch').addEventListener('change', relink); /* ---------- tour ---------- */ var steps = [ { el: 'desk', title: 'Two computers, one set of devices', text: 'The keyboard and mouse are paired with both: the PC on channel 1, the Mac on channel 2. Right now they are on the Mac.', go: function () { return st.kb === 'pc' || st.ms === 'pc' ? doBack() : null; } }, { el: 'rowSwitch', title: 'Push against the edge', text: 'The PC sits to the left, so holding the cursor against the Mac’s left edge for 50 ms sends both devices there. Watch them move.', go: function () { return doEdge(); } }, { el: 'paneWire', title: 'The exact command', text: 'This is the 20-byte HID++ 2.0 message PairSwitch writes to each device: mouse first, then keyboard. It then checks that each one really left.' }, { el: 'rowSwitch', title: 'The other one follows', text: 'Press Easy-Switch on the keyboard instead. It drops off the Mac by itself, and PairSwitch sends the mouse after it.', go: async function () { await doBack(); await doKey(); } }, { el: 'rowClip', title: 'Copy here, paste there', text: 'With sharing on, text you copy goes to the other computer in one encrypted message, usually in well under a second. Try the two checkboxes to see what happens when it cannot connect.', go: async function () { if (!linkUp()) { $('cSync').checked = true; $('cMatch').checked = true; await relink(); } return doCopyMac(); } }, { el: 'paneLog', title: 'Everything is written down', text: 'The real tool keeps this log on each computer. When something goes wrong, it builds a report you can paste into Claude. Scroll down to download it.' } ]; var at = -1; function lit(id, on) { var e = $(id); if (e) e.classList.toggle('lit', on); } function showStep(i) { if (at >= 0) lit(steps[at].el, false); at = Math.max(0, Math.min(steps.length - 1, i)); var s = steps[at]; $('tour').hidden = false; $('tourStep').textContent = 'Step ' + (at + 1) + ' of ' + steps.length; $('tourTitle').textContent = s.title; $('tourText').textContent = s.text; $('tourPrev').disabled = at === 0; $('tourNext').textContent = at === steps.length - 1 ? 'Done' : 'Next'; lit(s.el, true); var e = $(s.el); if (e && e.scrollIntoView) e.scrollIntoView({ block: window.innerHeight < 760 ? 'start' : 'center', behavior: reduce ? 'auto' : 'smooth' }); if (s.go) { var wait = function () { if (st.busy) { setTimeout(wait, 120); } else { s.go(); } }; setTimeout(wait, T(450)); } } function closeTour() { if (at >= 0) lit(steps[at].el, false); at = -1; $('tour').hidden = true; $('tourBtn').focus(); } $('tourBtn').addEventListener('click', function () { showStep(0); $('tourNext').focus(); }); $('tourNext').addEventListener('click', function () { if (at >= steps.length - 1) closeTour(); else showStep(at + 1); }); $('tourPrev').addEventListener('click', function () { showStep(at - 1); }); $('tourClose').addEventListener('click', closeTour); document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && at >= 0) closeTour(); }); refresh(); log('started: PC on the left edge -> channel 1; devices: Keyboard, Mouse (connected now: 2)'); log('clipboard sync: the PC connected'); if (/[?&]tour=1\b/.test(location.search)) showStep(0); })();