askrobots / network-computer
Public
Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.
Dan Quellhorst
Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353)
a1d92f3
1 month, 3 weeks ago
History
index.html · 58.1 KB · 1133 lines
| 1 | <!doctype html> |
| 2 | <html lang="en"> |
| 3 | <meta charset="utf-8"> |
| 4 | <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no"> |
| 5 | <title>Network Computer</title> |
| 6 | <style> |
| 7 | :root{ |
| 8 | --bg:#0b0d10; --panel:#15181d; --panel2:#1d222a; --line:#2a2f38; |
| 9 | --text:#e7ebf0; --dim:#9aa4b2; --accent:#4c8dff; --accent2:#3ad29f; |
| 10 | --warn:#ffb454; --bad:#ff5d5d; |
| 11 | } |
| 12 | *{box-sizing:border-box} |
| 13 | html,body{margin:0;height:100%;background:var(--bg);color:var(--text); |
| 14 | font:14px/1.45 -apple-system,BlinkMacSystemFont,"Segoe UI",system-ui,sans-serif; |
| 15 | overflow:hidden;-webkit-user-select:none;user-select:none;-webkit-touch-callout:none} |
| 16 | button,input,select{font:inherit;color:inherit} |
| 17 | button{cursor:pointer} |
| 18 | |
| 19 | /* ---------- connect screen ---------- */ |
| 20 | #connect{position:fixed;inset:0;display:flex;align-items:center;justify-content:center; |
| 21 | padding:24px;background:radial-gradient(1200px 600px at 50% -10%, #182030, var(--bg))} |
| 22 | .card{width:100%;max-width:420px;background:var(--panel);border:1px solid var(--line); |
| 23 | border-radius:16px;padding:28px;box-shadow:0 20px 60px rgba(0,0,0,.5)} |
| 24 | .brand{display:flex;align-items:center;gap:12px;margin-bottom:22px} |
| 25 | .logo{width:38px;height:38px;border-radius:10px;background:linear-gradient(135deg,var(--accent),var(--accent2)); |
| 26 | display:grid;place-items:center;font-size:20px} |
| 27 | .brand h1{font-size:19px;margin:0;font-weight:650;letter-spacing:.2px} |
| 28 | .brand p{margin:0;color:var(--dim);font-size:12px} |
| 29 | .field{margin-bottom:14px} |
| 30 | .field label{display:block;font-size:12px;color:var(--dim);margin-bottom:5px} |
| 31 | .field input,.field select{width:100%;padding:10px 12px;background:var(--panel2); |
| 32 | border:1px solid var(--line);border-radius:9px;outline:none} |
| 33 | .field input:focus,.field select:focus{border-color:var(--accent)} |
| 34 | .row{display:flex;gap:10px} |
| 35 | .row .field{flex:1} |
| 36 | .hostrow{display:flex;gap:8px;align-items:flex-end} |
| 37 | .hostrow .field{flex:1;margin-bottom:0} |
| 38 | .icon-btn{padding:10px;background:var(--panel2);border:1px solid var(--line);border-radius:9px; |
| 39 | color:var(--dim);line-height:1;height:40px} |
| 40 | .icon-btn:hover{color:var(--text);border-color:var(--accent)} |
| 41 | .toggles{display:flex;flex-direction:column;gap:8px;margin:8px 0 18px} |
| 42 | .toggle{display:flex;align-items:center;gap:9px;color:var(--dim);font-size:13px;cursor:pointer} |
| 43 | .toggle input{width:16px;height:16px;accent-color:var(--accent)} |
| 44 | .connect-btn{width:100%;padding:12px;border:none;border-radius:10px;font-weight:600;font-size:15px; |
| 45 | background:linear-gradient(135deg,var(--accent),#3f7ef0);color:#fff} |
| 46 | .connect-btn:disabled{opacity:.6;cursor:default} |
| 47 | .connect-btn.busy{background:var(--panel2);color:var(--dim)} |
| 48 | .msg{margin-top:12px;font-size:13px;min-height:18px} |
| 49 | .msg.err{color:var(--bad)} .msg.ok{color:var(--accent2)} .msg.info{color:var(--dim)} |
| 50 | .hint{margin-top:16px;font-size:11px;color:var(--dim);text-align:center;line-height:1.6} |
| 51 | |
| 52 | /* ---------- session ---------- */ |
| 53 | #session{position:fixed;inset:0;display:none;background:#000} |
| 54 | #session.on{display:block} |
| 55 | #stage{position:absolute;inset:0;display:grid;place-items:center;overflow:hidden} |
| 56 | video{max-width:100%;max-height:100%;display:block;background:#000} |
| 57 | #cursor{position:absolute;width:22px;height:22px;margin:-3px 0 0 -3px;pointer-events:none; |
| 58 | opacity:0;transition:opacity .25s;z-index:5; |
| 59 | filter:drop-shadow(0 1px 2px rgba(0,0,0,.6))} |
| 60 | #cursor.on{opacity:1} |
| 61 | |
| 62 | /* toolbar: compact and centred, hidden until the pointer rests against the very |
| 63 | top edge (or the handle is clicked), so the desktop's own top panel stays usable */ |
| 64 | #bar{position:absolute;top:0;left:50%;transform:translate(-50%,0);z-index:20;display:flex;align-items:center;gap:6px; |
| 65 | padding:6px 8px;margin-top:env(safe-area-inset-top,0px);background:#12151ae6;border:1px solid var(--line); |
| 66 | border-top:none;border-radius:0 0 12px 12px;backdrop-filter:blur(8px);transition:opacity .25s,transform .25s} |
| 67 | #bar.hidden{opacity:0;transform:translate(-50%,-110%);pointer-events:none} |
| 68 | #bar .pill{pointer-events:none} |
| 69 | #barhandle{position:absolute;top:0;left:50%;transform:translateX(-50%);z-index:19;width:56px;height:6px; |
| 70 | margin-top:env(safe-area-inset-top,0px);border-radius:0 0 6px 6px;background:#4c8dff88;cursor:pointer} |
| 71 | .pill{display:inline-flex;align-items:center;gap:7px;padding:6px 11px;background:#1c2129cc; |
| 72 | border:1px solid var(--line);border-radius:20px;font-size:12.5px;backdrop-filter:blur(8px)} |
| 73 | .pill.host{font-weight:600} |
| 74 | .dot{width:8px;height:8px;border-radius:50%;background:var(--accent2)} |
| 75 | .dot.relay{background:var(--warn)} .dot.bad{background:var(--bad)} |
| 76 | #bar .spacer{flex:1} |
| 77 | .bar-btn{width:36px;height:36px;border-radius:50%;border:1px solid var(--line);background:#1c2129cc; |
| 78 | color:var(--text);display:grid;place-items:center;font-size:15px;backdrop-filter:blur(8px)} |
| 79 | .bar-btn:hover{border-color:var(--accent)} |
| 80 | .bar-btn.active{background:var(--accent);border-color:var(--accent)} |
| 81 | |
| 82 | /* mic panel: which microphone, and a live level so "it hears me" is obvious */ |
| 83 | #micpanel{position:absolute;top:calc(56px + env(safe-area-inset-top,0px));left:50%;transform:translateX(-50%); |
| 84 | z-index:16;display:none;align-items:center;gap:10px;padding:8px 12px;background:#12151acc; |
| 85 | border:1px solid var(--line);border-radius:10px;backdrop-filter:blur(8px);font-size:12.5px} |
| 86 | #micpanel.on{display:flex} |
| 87 | #disppanel{position:absolute;top:calc(56px + env(safe-area-inset-top,0px));right:12px;z-index:16;display:none; |
| 88 | align-items:center;gap:8px;padding:8px 12px;background:#12151acc;border:1px solid var(--line);border-radius:10px; |
| 89 | backdrop-filter:blur(8px);font-size:12.5px} |
| 90 | #disppanel.on{display:flex} |
| 91 | #disppanel select{background:var(--panel2);border:1px solid var(--line);border-radius:6px;padding:4px 6px} |
| 92 | #micpanel select{max-width:220px;background:var(--panel2);border:1px solid var(--line);border-radius:6px;padding:4px 6px} |
| 93 | #micmeter{width:120px;height:8px;border-radius:4px;background:#0c0f13;overflow:hidden} |
| 94 | #micmeter i{display:block;height:100%;width:0;background:var(--accent2);transition:width .08s linear} |
| 95 | #micquiet{color:var(--warn);display:none} |
| 96 | |
| 97 | /* stats card */ |
| 98 | #stats{position:absolute;top:60px;top:calc(56px + env(safe-area-inset-top,0px));left:12px;z-index:15; |
| 99 | background:#12151acc;border:1px solid var(--line);border-radius:10px;padding:10px 12px; |
| 100 | font:11.5px/1.6 ui-monospace,SFMono-Regular,Menlo,monospace;color:var(--dim); |
| 101 | backdrop-filter:blur(8px);display:none;min-width:210px} |
| 102 | #stats.on{display:block} |
| 103 | #stats b{color:var(--text);font-weight:600} |
| 104 | #stats .k{display:inline-block;width:64px;color:var(--dim)} |
| 105 | |
| 106 | /* graphs panel */ |
| 107 | #graphs{position:absolute;top:calc(56px + env(safe-area-inset-top,0px));right:12px;z-index:15; |
| 108 | width:280px;background:#12151acc;border:1px solid var(--line);border-radius:10px;padding:10px 12px; |
| 109 | backdrop-filter:blur(8px);display:none} |
| 110 | #graphs.on{display:block} |
| 111 | .g{margin-bottom:9px} |
| 112 | .g:last-child{margin-bottom:0} |
| 113 | .g-head{display:flex;justify-content:space-between;font:11px ui-monospace,Menlo,monospace;color:var(--dim);margin-bottom:2px} |
| 114 | .g-head b{color:var(--text);font-weight:600} |
| 115 | .g canvas{width:100%;height:34px;display:block;border-radius:4px;background:#0c0f13} |
| 116 | |
| 117 | /* keyboard bar */ |
| 118 | #keys{position:absolute;left:0;right:0;bottom:0;z-index:20;display:none;gap:6px;padding:10px; |
| 119 | padding-bottom:calc(10px + env(safe-area-inset-bottom,0px));overflow-x:auto; |
| 120 | background:linear-gradient(#0000,#000c);scrollbar-width:none} |
| 121 | #keys.on{display:flex} |
| 122 | #keys::-webkit-scrollbar{display:none} |
| 123 | .key{flex:0 0 auto;padding:9px 13px;background:#1c2129e6;border:1px solid var(--line); |
| 124 | border-radius:8px;font:13px ui-monospace,Menlo,monospace} |
| 125 | .key.sticky.on{background:var(--accent);border-color:var(--accent);color:#fff} |
| 126 | |
| 127 | /* toast + reconnect overlay */ |
| 128 | #toast{position:absolute;left:50%;bottom:70px;transform:translateX(-50%);z-index:25; |
| 129 | background:#12151aee;border:1px solid var(--line);border-radius:20px;padding:8px 16px;font-size:13px; |
| 130 | opacity:0;transition:opacity .3s;pointer-events:none} |
| 131 | #toast.on{opacity:1} |
| 132 | #reconnect{position:absolute;inset:0;z-index:18;display:none;place-items:center;background:#000a} |
| 133 | #reconnect.on{display:grid} |
| 134 | .spin{width:34px;height:34px;border:3px solid var(--line);border-top-color:var(--accent); |
| 135 | border-radius:50%;animation:spin 1s linear infinite} |
| 136 | @keyframes spin{to{transform:rotate(360deg)}} |
| 137 | #disppanel label{display:flex;align-items:center;gap:4px;white-space:nowrap} |
| 138 | #dropzone{position:fixed;inset:0;z-index:40;display:none;align-items:center;justify-content:center; |
| 139 | background:rgba(20,30,60,.55);border:3px dashed #8ab4ff;color:#fff;font-size:22px;pointer-events:none} |
| 140 | #dropzone.on{display:flex} |
| 141 | #downloads{position:fixed;right:12px;bottom:calc(12px + env(safe-area-inset-bottom,0px));z-index:30;display:none; |
| 142 | flex-direction:column;gap:6px;max-width:min(360px,80vw)} |
| 143 | #downloads.on{display:flex} |
| 144 | #downloads a{background:var(--panel2);border:1px solid var(--line);border-radius:8px;padding:8px 10px;color:inherit; |
| 145 | text-decoration:none;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;box-shadow:0 4px 14px rgba(0,0,0,.35)} |
| 146 | #focushint{position:fixed;left:50%;top:40%;transform:translateX(-50%);z-index:35;display:none;cursor:pointer; |
| 147 | background:rgba(20,24,34,.88);border:1px solid var(--line);border-radius:10px;padding:12px 18px;color:#fff} |
| 148 | #focushint.on{display:block} |
| 149 | #voicefab{position:fixed;left:16px;bottom:calc(16px + env(safe-area-inset-bottom,0px));z-index:31;display:none; |
| 150 | width:56px;height:56px;border-radius:50%;border:1px solid var(--line);background:var(--panel2);font-size:26px; |
| 151 | cursor:pointer;box-shadow:0 4px 14px rgba(0,0,0,.4)} |
| 152 | #session.on #voicefab{display:block} |
| 153 | #voicefab.on{background:#d33;border-color:#f66;animation:vpulse 1.6s ease-in-out infinite} |
| 154 | @keyframes vpulse{50%{box-shadow:0 0 0 10px rgba(221,51,51,.25)}} |
| 155 | #voicepanel{position:fixed;left:16px;bottom:calc(84px + env(safe-area-inset-bottom,0px));z-index:31;display:none; |
| 156 | width:min(440px,80vw);max-height:42vh;flex-direction:column;background:rgba(18,22,32,.9);color:#eee; |
| 157 | border:1px solid var(--line);border-radius:10px;font-size:14px;box-shadow:0 6px 20px rgba(0,0,0,.4)} |
| 158 | #voicepanel.show{display:flex} |
| 159 | #voicehead{display:flex;justify-content:space-between;align-items:center;padding:8px 12px;border-bottom:1px solid var(--line)} |
| 160 | #voicehead label{display:flex;align-items:center;gap:4px;font-size:12px;color:#abc;margin-left:auto;margin-right:10px} |
| 161 | #voicehead button{background:none;border:0;color:inherit;font-size:16px;cursor:pointer} |
| 162 | #voicelog{overflow:auto;padding:8px 12px;display:flex;flex-direction:column;gap:6px} |
| 163 | #voicelog .you{color:#fff} #voicelog .you::before{content:"You: ";color:#9ab} |
| 164 | #voicelog .ai{color:#bfe0ff} #voicelog .ai::before{content:"Desk: ";color:#7ab} |
| 165 | #voicelog .did{color:#9d9;font-size:13px} #voicelog .err{color:#fb8;font-size:13px} |
| 166 | #soundhint{position:fixed;top:calc(12px + env(safe-area-inset-top,0px));right:12px;z-index:36;display:none; |
| 167 | background:#d33;color:#fff;border:0;border-radius:8px;padding:8px 12px;font-size:14px;cursor:pointer} |
| 168 | #soundhint.on{display:block} |
| 169 | #clipbox{position:fixed;left:-9999px;top:0;width:1px;height:1px;opacity:0} |
| 170 | </style> |
| 171 | |
| 172 | <!-- ================= CONNECT ================= --> |
| 173 | <div id="connect"> |
| 174 | <div class="card"> |
| 175 | <div class="brand"> |
| 176 | <div class="logo">🖥️</div> |
| 177 | <div><h1>Network Computer</h1><p>Your phone is the terminal</p></div> |
| 178 | </div> |
| 179 | <div class="field"> |
| 180 | <label>Rendezvous</label> |
| 181 | <input id="f-url" placeholder="https://host:8765" autocomplete="off" autocapitalize="off" spellcheck="false"> |
| 182 | </div> |
| 183 | <div class="row"> |
| 184 | <div class="field"><label>User</label> |
| 185 | <input id="f-user" value="nc" autocomplete="off" autocapitalize="off" spellcheck="false"></div> |
| 186 | <div class="field"><label>Password</label> |
| 187 | <input id="f-pass" type="password" autocomplete="off"></div> |
| 188 | </div> |
| 189 | <div class="hostrow"> |
| 190 | <div class="field"><label>Host</label> |
| 191 | <select id="f-host"><option value="">—</option></select></div> |
| 192 | <button class="icon-btn" id="f-refresh" title="Refresh hosts">↻</button> |
| 193 | </div> |
| 194 | <div class="field" style="margin-top:14px"><label>Host PIN</label> |
| 195 | <input id="f-pin" inputmode="numeric" placeholder="6 digits" autocomplete="off"></div> |
| 196 | <div class="toggles"> |
| 197 | <label class="toggle"><input type="checkbox" id="f-cursor" checked> Local cursor (snappier pointer)</label> |
| 198 | <label class="toggle"><input type="checkbox" id="f-relay"> Force TURN relay</label> |
| 199 | </div> |
| 200 | <button class="connect-btn" id="f-connect">Connect</button> |
| 201 | <div class="msg info" id="f-msg"></div> |
| 202 | <div class="hint">Click the screen to control it · Shift+Esc releases the mouse</div> |
| 203 | </div> |
| 204 | </div> |
| 205 | |
| 206 | <!-- ================= SESSION ================= --> |
| 207 | <div id="session"> |
| 208 | <div id="stage"> |
| 209 | <video id="v" autoplay playsinline></video> |
| 210 | <svg id="cursor" viewBox="0 0 24 24"><path d="M4 2l16 9-7 1.5L9.5 20z" fill="#fff" stroke="#000" stroke-width="1.2"/></svg> |
| 211 | </div> |
| 212 | |
| 213 | <div id="barhandle" title="Show the toolbar"></div> |
| 214 | <div id="bar"> |
| 215 | <span class="pill host"><span class="dot" id="dot"></span><span id="p-host">—</span></span> |
| 216 | <span class="pill" id="p-net">connecting…</span> |
| 217 | <button class="bar-btn" id="b-display" title="Screen size and scale">🖥️</button> |
| 218 | <button class="bar-btn" id="b-mic" title="Microphone">🎤</button> |
| 219 | <button class="bar-btn" id="b-cam" title="Camera: the desk's webcam (network-computer camera)">📷</button> |
| 220 | <button class="bar-btn" id="b-gray" title="No colour: the desk in grey on this device,, to compare">◐</button> |
| 221 | <button class="bar-btn" id="b-graphs" title="Graphs">📈</button> |
| 222 | <button class="bar-btn" id="b-stats" title="Stats">📊</button> |
| 223 | <button class="bar-btn" id="b-launch" title="Search and launch on the desk (Alt+Space)">🔍</button> |
| 224 | <button class="bar-btn" id="b-clipup" title="Send this device's clipboard to the desk">📋↑</button> |
| 225 | <button class="bar-btn" id="b-clipdown" title="Copy the desk's clipboard to this device">📋↓</button> |
| 226 | <button class="bar-btn" id="b-keys" title="Keyboard">⌨️</button> |
| 227 | <button class="bar-btn" id="b-full" title="Fullscreen">⛶</button> |
| 228 | <button class="bar-btn" id="b-quit" title="Disconnect">✕</button> |
| 229 | </div> |
| 230 | |
| 231 | <div id="stats"></div> |
| 232 | <div id="disppanel" class="panel"> |
| 233 | <span>🖥️</span> |
| 234 | <select id="d-res" title="Screen size"></select> |
| 235 | <select id="d-scale" title="Text and UI scale"></select> |
| 236 | <span>⌨️</span> |
| 237 | <select id="d-kbd" title="The layout of the keyboard on this device"></select> |
| 238 | <label id="d-cmdl" title="⌘C, ⌘V, ⌘Z… work as Ctrl shortcuts on the desk" hidden><input type="checkbox" id="d-cmd"> ⌘ = Ctrl</label> |
| 239 | </div> |
| 240 | <button id="soundhint">🔇 Sound is off: click for sound</button> |
| 241 | <button id="voicefab" title="Talk to the desk (voice on/off)">🎙️</button> |
| 242 | <div id="voicepanel"><div id="voicehead"><b id="voicestate">Voice off</b><label title="Off: one phrase per click. On: keeps listening until you click again."><input type="checkbox" id="voicekeep"> keep listening</label><button id="voiceclose" title="Hide">✕</button></div><div id="voicelog"></div></div> |
| 243 | <div id="focushint">⌨️ Typing is going to another app. Click here to type on the desk.</div> |
| 244 | <div id="dropzone">Drop to send to the desk's Desktop</div> |
| 245 | <div id="downloads" title="Files sent from the desk: click to save"></div> |
| 246 | <textarea id="clipbox" aria-hidden="true" tabindex="-1"></textarea> |
| 247 | <div id="micpanel"> |
| 248 | <span>🎤</span> |
| 249 | <select id="mic-dev" title="Microphone"></select> |
| 250 | <div id="micmeter"><i></i></div> |
| 251 | <span id="micquiet">not hearing you — pick another mic</span> |
| 252 | </div> |
| 253 | <div id="graphs"> |
| 254 | <div class="g"><div class="g-head"><span>frame rate</span><b id="gv-fps">0 fps</b></div><canvas id="gc-fps"></canvas></div> |
| 255 | <div class="g"><div class="g-head"><span>bitrate</span><b id="gv-mbps">0 Mb/s</b></div><canvas id="gc-mbps"></canvas></div> |
| 256 | <div class="g"><div class="g-head"><span>packet loss</span><b id="gv-loss">0 /s</b></div><canvas id="gc-loss"></canvas></div> |
| 257 | <div class="g"><div class="g-head"><span>round trip</span><b id="gv-rtt">0 ms</b></div><canvas id="gc-rtt"></canvas></div> |
| 258 | <div class="g"><div class="g-head"><span>jitter</span><b id="gv-jit">0 ms</b></div><canvas id="gc-jit"></canvas></div> |
| 259 | </div> |
| 260 | |
| 261 | <div id="keys"> |
| 262 | <button class="key" data-code="Escape">esc</button> |
| 263 | <button class="key sticky" data-code="ControlLeft">ctrl</button> |
| 264 | <button class="key sticky" data-code="AltLeft">alt</button> |
| 265 | <button class="key sticky" data-code="MetaLeft">cmd</button> |
| 266 | <button class="key" data-code="Tab">tab</button> |
| 267 | <button class="key" data-combo="ControlLeft+KeyC">^C</button> |
| 268 | <button class="key" data-combo="ControlLeft+KeyV">^V</button> |
| 269 | <button class="key" data-code="ArrowLeft">←</button> |
| 270 | <button class="key" data-code="ArrowUp">↑</button> |
| 271 | <button class="key" data-code="ArrowDown">↓</button> |
| 272 | <button class="key" data-code="ArrowRight">→</button> |
| 273 | <button class="key" data-code="Home">home</button> |
| 274 | <button class="key" data-code="End">end</button> |
| 275 | <button class="key" data-code="Delete">del</button> |
| 276 | </div> |
| 277 | |
| 278 | <div id="toast"></div> |
| 279 | <div id="reconnect"><div style="text-align:center"><div class="spin"></div> |
| 280 | <div style="margin-top:14px;color:var(--dim)">Reconnecting…</div></div></div> |
| 281 | </div> |
| 282 | |
| 283 | <script> |
| 284 | const $=s=>document.querySelector(s); |
| 285 | const store={ |
| 286 | get:(k,d)=>{try{const v=localStorage.getItem('nc.'+k);return v===null?d:v}catch(e){return d}}, |
| 287 | set:(k,v)=>{try{localStorage.setItem('nc.'+k,v)}catch(e){}}, |
| 288 | }; |
| 289 | |
| 290 | // restore saved fields |
| 291 | $('#f-url').value = store.get('url', location.origin); |
| 292 | $('#f-user').value = store.get('user','nc'); |
| 293 | $('#f-pass').value = store.get('pass',''); |
| 294 | try{localStorage.removeItem('nc.pin')}catch(e){} // PIN no longer stored; pairing replaces it |
| 295 | $('#f-cursor').checked = store.get('cursor','1')==='1'; |
| 296 | $('#f-relay').checked = store.get('relay','0')==='1'; |
| 297 | |
| 298 | let ws, pc, dc, ctl, statsTimer, watchdog, cfg, hostName, audioTx=null, micStream=null, camTx=null, camStream=null; |
| 299 | let localCursor=true, retryWanted=false, retries=0, lastFrames=0; |
| 300 | |
| 301 | function msg(t,cls){const m=$('#f-msg');m.textContent=t||'';m.className='msg '+(cls||'info');} |
| 302 | function toast(t){const el=$('#toast');el.textContent=t;el.classList.add('on');clearTimeout(el._t);el._t=setTimeout(()=>el.classList.remove('on'),1600);} |
| 303 | |
| 304 | let token=null, mode='insecure'; |
| 305 | |
| 306 | // Pairing: the host returns a signed token after a correct PIN. We keep one per |
| 307 | // rendezvous+host and send it next time, so the PIN is typed once. |
| 308 | function pairKey(h){ return 'pair:'+$('#f-url').value.replace(/\/$/,'')+'|'+h; } |
| 309 | function getPair(h){ return store.get(pairKey(h),''); } |
| 310 | function setPair(h,t){ store.set(pairKey(h),t); } |
| 311 | function clearPair(h){ try{localStorage.removeItem('nc.'+pairKey(h))}catch(e){} } |
| 312 | function showPairState(){ |
| 313 | const h=$('#f-host').value, paired=!!(h&&getPair(h)); |
| 314 | $('#f-pin').placeholder = paired ? 'paired — PIN not needed' : '6 digits'; |
| 315 | } |
| 316 | |
| 317 | // One credential exchange: POST /auth -> token. The page itself needs no auth, |
| 318 | // so the browser never shows its own login dialog; this form asks once. |
| 319 | async function authenticate(){ |
| 320 | const base=$('#f-url').value.replace(/\/$/,''); |
| 321 | const r=await fetch(base+'/auth',{ |
| 322 | method:'POST', headers:{'Content-Type':'application/json'}, |
| 323 | body:JSON.stringify({user:$('#f-user').value, password:$('#f-pass').value}), |
| 324 | }); |
| 325 | if(r.status===401) throw new Error('Password rejected'); |
| 326 | if(!r.ok) throw new Error('Rendezvous HTTP '+r.status); |
| 327 | const j=await r.json(); |
| 328 | token=j.token; mode=j.mode||'insecure'; |
| 329 | return token; |
| 330 | } |
| 331 | |
| 332 | async function api(path){ |
| 333 | const url=$('#f-url').value.replace(/\/$/,'')+path; |
| 334 | if(!token) await authenticate(); |
| 335 | let r=await fetch(url,{headers:{Authorization:'Bearer '+token}}); |
| 336 | if(r.status===401){ token=null; await authenticate(); r=await fetch(url,{headers:{Authorization:'Bearer '+token}}); } |
| 337 | if(r.status===401) throw new Error('Password rejected'); |
| 338 | if(!r.ok) throw new Error('HTTP '+r.status); |
| 339 | return r.json(); |
| 340 | } |
| 341 | |
| 342 | async function refreshHosts(){ |
| 343 | try{ |
| 344 | const hosts=await api('/hosts'); |
| 345 | const sel=$('#f-host'), prev=store.get('host',''); |
| 346 | sel.innerHTML=''; |
| 347 | if(!hosts.length){sel.innerHTML='<option value="">no hosts online</option>';return;} |
| 348 | for(const h of hosts){const o=document.createElement('option');o.value=o.textContent=h;sel.appendChild(o);} |
| 349 | if(hosts.includes(prev)) sel.value=prev; |
| 350 | showPairState(); |
| 351 | msg(''); |
| 352 | }catch(e){msg(e.message,'err');} |
| 353 | } |
| 354 | $('#f-refresh').onclick=refreshHosts; |
| 355 | $('#f-host').addEventListener('change',showPairState); |
| 356 | $('#f-url').addEventListener('change',refreshHosts); |
| 357 | $('#f-pass').addEventListener('change',refreshHosts); |
| 358 | |
| 359 | function wsURL(){ |
| 360 | const u=new URL($('#f-url').value); |
| 361 | return (u.protocol==='https:'?'wss:':'ws:')+'//'+u.host+'/ws?token='+encodeURIComponent(token||''); |
| 362 | } |
| 363 | |
| 364 | $('#f-connect').onclick=()=>startConnect(false); |
| 365 | |
| 366 | async function startConnect(isRetry){ |
| 367 | const btn=$('#f-connect'); |
| 368 | if(!isRetry){ |
| 369 | hostName=$('#f-host').value; |
| 370 | if(!hostName){msg('Pick a host','err');return;} |
| 371 | store.set('url',$('#f-url').value);store.set('user',$('#f-user').value); |
| 372 | store.set('pass',$('#f-pass').value); |
| 373 | store.set('host',hostName); |
| 374 | store.set('cursor',$('#f-cursor').checked?'1':'0'); |
| 375 | store.set('relay',$('#f-relay').checked?'1':'0'); |
| 376 | localCursor=$('#f-cursor').checked; |
| 377 | retryWanted=true; retries=0; |
| 378 | } |
| 379 | btn.disabled=true; btn.classList.add('busy'); btn.textContent='Connecting…'; |
| 380 | msg('fetching config…'); |
| 381 | try{ |
| 382 | await authenticate(); |
| 383 | cfg=await api('/config'); |
| 384 | await openPeer(); |
| 385 | }catch(e){ |
| 386 | msg(e.message,'err'); resetConnectBtn(); |
| 387 | } |
| 388 | } |
| 389 | function resetConnectBtn(){const b=$('#f-connect');b.disabled=false;b.classList.remove('busy');b.textContent='Connect';} |
| 390 | |
| 391 | async function openPeer(){ |
| 392 | const rtc={iceServers:cfg.iceServers}; |
| 393 | if($('#f-relay').checked) rtc.iceTransportPolicy='relay'; |
| 394 | pc=new RTCPeerConnection(rtc); |
| 395 | const stream=new MediaStream(); |
| 396 | const video=$('#v'); |
| 397 | video.srcObject=stream; |
| 398 | |
| 399 | pc.addTransceiver('video',{direction:'recvonly'}); |
| 400 | audioTx = pc.addTransceiver('audio',{direction:'sendrecv'}); // mic attached on demand |
| 401 | // the camera: sent only, attached while it is on (so the camera light is honest) |
| 402 | camTx = pc.addTransceiver('video',{direction:'sendonly'}); |
| 403 | if(camStream) camTx.sender.replaceTrack(camStream.getVideoTracks()[0]); // reconnected with it on |
| 404 | dc=pc.createDataChannel('input',{ordered:false,maxRetransmits:0}); |
| 405 | // reliable + ordered: requests that must not be dropped (screen size, later files and clipboard) |
| 406 | ctl=pc.createDataChannel('control'); |
| 407 | ctl.onmessage=onCtl; |
| 408 | pc.ondatachannel=e=>{ if(e.channel.label==='file') receiveFile(e.channel); }; |
| 409 | ctl.onopen=()=>{ lastDisplay=null; sendDisplay(); sendKeyboard(false); voiceShow(false); |
| 410 | try{ ctl.send(JSON.stringify({t:'tz',text:Intl.DateTimeFormat().resolvedOptions().timeZone})); }catch(e){} |
| 411 | inputMode=''; reportInput(matchMedia('(pointer: coarse)').matches ? 'touch' : 'pointer'); }; |
| 412 | |
| 413 | pc.ontrack=ev=>{ |
| 414 | stream.addTrack(ev.track); |
| 415 | // Reliable attach: unmute for audio, then force playback (connect was a user gesture). |
| 416 | video.muted=false; |
| 417 | video.play().catch(()=>{ // autoplay blocked → retry muted so at least video shows |
| 418 | video.muted=true; video.play().catch(()=>{}); soundHint(); |
| 419 | }); |
| 420 | }; |
| 421 | pc.onicecandidate=e=>{if(e.candidate) sig({type:'ice',to:hostName,candidate:e.candidate.toJSON()});}; |
| 422 | pc.onconnectionstatechange=()=>{ |
| 423 | const s=pc.connectionState; |
| 424 | if(s==='connected'){onConnected();} |
| 425 | else if(s==='failed'||s==='disconnected'){onDropped();} |
| 426 | }; |
| 427 | |
| 428 | await openWS(); |
| 429 | const offer=await pc.createOffer(); |
| 430 | await pc.setLocalDescription(offer); |
| 431 | sig({type:'offer',to:hostName,sdp:offer.sdp,pin:$('#f-pin').value.trim(),pair:getPair(hostName)}); |
| 432 | } |
| 433 | |
| 434 | function openWS(){ |
| 435 | return new Promise((res,rej)=>{ |
| 436 | ws=new WebSocket(wsURL()); // token travels in the query string |
| 437 | ws.onopen=()=>res(); |
| 438 | ws.onerror=e=>rej(new Error('signaling failed (check URL/password)')); |
| 439 | ws.onmessage=ev=>onSignal(JSON.parse(ev.data)); |
| 440 | ws.onclose=()=>{}; |
| 441 | }); |
| 442 | } |
| 443 | function sig(m){ if(ws&&ws.readyState===1) ws.send(JSON.stringify(m)); } |
| 444 | |
| 445 | async function onSignal(m){ |
| 446 | if(m.type==='answer'){ if(m.pair) setPair(hostName,m.pair); await pc.setRemoteDescription({type:'answer',sdp:m.sdp}); } |
| 447 | else if(m.type==='ice'){try{await pc.addIceCandidate(m.candidate);}catch(e){}} |
| 448 | else if(m.type==='error'){ |
| 449 | msg(m.error,'err'); |
| 450 | if(/pairing/i.test(m.error)){ clearPair(hostName); showPairState(); } |
| 451 | if(/PIN/i.test(m.error)){retryWanted=false;teardown();resetConnectBtn();} |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | function onConnected(){ |
| 456 | retries=0; |
| 457 | $('#connect').style.display='none'; |
| 458 | $('#session').classList.add('on'); |
| 459 | $('#reconnect').classList.remove('on'); |
| 460 | $('#dot').className='dot'; |
| 461 | $('#p-net').textContent='connected'; |
| 462 | resetConnectBtn(); |
| 463 | startStats(); |
| 464 | startWatchdog(); |
| 465 | armAutoHide(); |
| 466 | } |
| 467 | |
| 468 | function onDropped(){ |
| 469 | if(!retryWanted) return; |
| 470 | $('#reconnect').classList.add('on'); |
| 471 | $('#dot').className='dot bad'; |
| 472 | stopTimers(); |
| 473 | try{pc.close();}catch(e){} try{ws.close();}catch(e){} |
| 474 | retries++; |
| 475 | const delay=Math.min(1000*retries,5000); |
| 476 | setTimeout(()=>{ if(retryWanted) openPeer().catch(()=>onDropped()); },delay); |
| 477 | } |
| 478 | |
| 479 | function teardown(){ |
| 480 | retryWanted=false; stopTimers(); setMic(false); setCam(false); |
| 481 | try{dc&&dc.close()}catch(e){} try{pc&&pc.close()}catch(e){} try{ws&&ws.close()}catch(e){} |
| 482 | const v=$('#v'); v.srcObject=null; |
| 483 | $('#session').classList.remove('on'); |
| 484 | $('#connect').style.display='flex'; |
| 485 | } |
| 486 | $('#b-quit').onclick=teardown; |
| 487 | |
| 488 | /* ---------- watchdog: detect a frozen/black stream and recover ---------- */ |
| 489 | function startWatchdog(){ |
| 490 | lastFrames=0; let stalls=0; |
| 491 | watchdog=setInterval(async()=>{ |
| 492 | if(!pc) return; |
| 493 | const r=await pc.getStats(); let f=0; |
| 494 | r.forEach(s=>{if(s.type==='inbound-rtp'&&s.kind==='video') f=s.framesDecoded||0;}); |
| 495 | if(f<=lastFrames){ stalls++; if(stalls===2) toast('no video — recovering'); if(stalls>=4){stalls=0;onDropped();} } |
| 496 | else stalls=0; |
| 497 | lastFrames=f; |
| 498 | },1000); |
| 499 | } |
| 500 | |
| 501 | /* ---------- stats ---------- */ |
| 502 | $('#b-stats').onclick=()=>{$('#stats').classList.toggle('on');store.set('stats',$('#stats').classList.contains('on')?'1':'0');}; |
| 503 | if(store.get('stats','0')==='1') $('#stats').classList.add('on'); |
| 504 | $('#b-graphs').onclick=()=>{const on=$('#graphs').classList.toggle('on');$('#b-graphs').classList.toggle('active',on);store.set('graphs',on?'1':'0');}; |
| 505 | if(store.get('graphs','0')==='1'){$('#graphs').classList.add('on');$('#b-graphs').classList.add('active');} |
| 506 | |
| 507 | // rolling time-series for the graphs |
| 508 | const SERIES={fps:[],mbps:[],loss:[],rtt:[],jit:[]}, SPAN=60; |
| 509 | let prevLoss=0; |
| 510 | function push(name,v){const a=SERIES[name];a.push(v);if(a.length>SPAN)a.shift();} |
| 511 | function drawSpark(id,data,color,warnAt){ |
| 512 | const c=document.getElementById(id); if(!c) return; |
| 513 | const w=c.clientWidth||256,h=c.clientHeight||34,dpr=devicePixelRatio||1; |
| 514 | if(c.width!==w*dpr){c.width=w*dpr;c.height=h*dpr;} |
| 515 | const g=c.getContext('2d'); g.setTransform(dpr,0,0,dpr,0,0); g.clearRect(0,0,w,h); |
| 516 | if(!data.length) return; |
| 517 | const max=Math.max(warnAt||0,...data,1e-6), n=data.length; |
| 518 | const x=i=>i/(SPAN-1)*w, y=v=>h-2-(v/max)*(h-4); |
| 519 | // area |
| 520 | g.beginPath(); g.moveTo(x(SPAN-n),h); |
| 521 | data.forEach((v,i)=>g.lineTo(x(SPAN-n+i),y(v))); |
| 522 | g.lineTo(x(SPAN-1),h); g.closePath(); |
| 523 | g.fillStyle=color+'22'; g.fill(); |
| 524 | // line |
| 525 | g.beginPath(); |
| 526 | data.forEach((v,i)=>{const xx=x(SPAN-n+i),yy=y(v);i?g.lineTo(xx,yy):g.moveTo(xx,yy);}); |
| 527 | g.strokeStyle=color; g.lineWidth=1.5; g.stroke(); |
| 528 | } |
| 529 | function drawGraphs(){ |
| 530 | drawSpark('gc-fps',SERIES.fps,'#4c8dff'); |
| 531 | drawSpark('gc-mbps',SERIES.mbps,'#3ad29f'); |
| 532 | drawSpark('gc-loss',SERIES.loss,'#ff5d5d',1); |
| 533 | drawSpark('gc-rtt',SERIES.rtt,'#ffb454'); |
| 534 | drawSpark('gc-jit',SERIES.jit,'#b98bff'); |
| 535 | } |
| 536 | let prev={b:0,f:0,t:0}; |
| 537 | function startStats(){ |
| 538 | statsTimer=setInterval(async()=>{ |
| 539 | if(!pc) return; |
| 540 | const r=await pc.getStats(); let pair,cands={},inb,codec='?'; |
| 541 | r.forEach(s=>{ |
| 542 | if(s.type==='candidate-pair'&&s.nominated&&s.state==='succeeded')pair=s; |
| 543 | if(s.type==='local-candidate'||s.type==='remote-candidate')cands[s.id]=s; |
| 544 | if(s.type==='inbound-rtp'&&s.kind==='video')inb=s; |
| 545 | }); |
| 546 | let relay=false,net='connected'; |
| 547 | if(pair){ |
| 548 | const l=cands[pair.localCandidateId],rc=cands[pair.remoteCandidateId]; |
| 549 | relay=(l&&l.candidateType==='relay')||(rc&&rc.candidateType==='relay'); |
| 550 | net=(relay?'relay':'direct')+' · '+((pair.currentRoundTripTime||0)*1000).toFixed(0)+'ms'; |
| 551 | } |
| 552 | $('#p-net').textContent = mode==='secure' ? net : net+' · insecure'; |
| 553 | $('#dot').className='dot'+(relay?' relay':''); |
| 554 | if(inb){ |
| 555 | const now=performance.now(),dt=(now-prev.t)/1000||1; |
| 556 | const mbps=(inb.bytesReceived-prev.b)*8/dt/1e6, fps=(inb.framesDecoded-prev.f)/dt; |
| 557 | prev={b:inb.bytesReceived,f:inb.framesDecoded,t:now}; |
| 558 | const dec=inb.framesDecoded?(inb.totalDecodeTime/inb.framesDecoded*1000).toFixed(1):'?'; |
| 559 | r.forEach(s=>{if(s.id===inb.codecId)codec=(s.mimeType||'').split('/')[1]||'?';}); |
| 560 | $('#stats').innerHTML= |
| 561 | `<div><span class="k">size</span><b>${inb.frameWidth||0}×${inb.frameHeight||0}</b></div>`+ |
| 562 | `<div><span class="k">rate</span><b>${fps.toFixed(0)} fps</b> · ${mbps.toFixed(1)} Mb/s · ${codec}</b></div>`+ |
| 563 | `<div><span class="k">latency</span><b>${net}</b></div>`+ |
| 564 | `<div><span class="k">decode</span>${dec} ms · lost ${inb.packetsLost}</div>`; |
| 565 | |
| 566 | // graphs |
| 567 | const lossNow=Math.max(0,(inb.packetsLost||0)-prevLoss); prevLoss=inb.packetsLost||0; |
| 568 | const rttMs=pair?(pair.currentRoundTripTime||0)*1000:0; |
| 569 | const jit=(inb.jitter||0)*1000; |
| 570 | push('fps',fps); push('mbps',mbps); push('loss',lossNow); push('rtt',rttMs); push('jit',jit); |
| 571 | $('#gv-fps').textContent=fps.toFixed(0)+' fps'; |
| 572 | $('#gv-mbps').textContent=mbps.toFixed(1)+' Mb/s'; |
| 573 | $('#gv-loss').textContent=lossNow+' /s'; |
| 574 | $('#gv-rtt').textContent=rttMs.toFixed(0)+' ms'; |
| 575 | $('#gv-jit').textContent=jit.toFixed(1)+' ms'; |
| 576 | if($('#graphs').classList.contains('on')) drawGraphs(); |
| 577 | } |
| 578 | },1000); |
| 579 | } |
| 580 | function stopTimers(){clearInterval(statsTimer);clearInterval(watchdog);} |
| 581 | |
| 582 | /* ---------- microphone ---------- */ |
| 583 | // The browser only allows microphone access on https (or localhost), so on a |
| 584 | // plain-http rendezvous this explains why instead of failing silently. |
| 585 | let micCtx=null, micRaf=0, quietSince=0; |
| 586 | async function openMic(deviceId){ |
| 587 | const c={echoCancellation:true,noiseSuppression:true,autoGainControl:true}; |
| 588 | if(deviceId) c.deviceId={exact:deviceId}; |
| 589 | return navigator.mediaDevices.getUserMedia({audio:c}); |
| 590 | } |
| 591 | async function fillMicList(current){ |
| 592 | const sel=$('#mic-dev'), devs=(await navigator.mediaDevices.enumerateDevices()).filter(d=>d.kind==='audioinput'); |
| 593 | sel.innerHTML=''; |
| 594 | for(const d of devs){ const o=document.createElement('option'); o.value=d.deviceId; o.textContent=d.label||'microphone'; sel.appendChild(o); } |
| 595 | if(current) sel.value=current; |
| 596 | } |
| 597 | function startMeter(stream){ |
| 598 | stopMeter(); |
| 599 | const AC=window.AudioContext||window.webkitAudioContext; micCtx=new AC(); |
| 600 | const an=micCtx.createAnalyser(); an.fftSize=1024; |
| 601 | micCtx.createMediaStreamSource(stream).connect(an); |
| 602 | const buf=new Float32Array(an.fftSize), bar=$('#micmeter i'); |
| 603 | quietSince=performance.now(); |
| 604 | (function tick(){ |
| 605 | an.getFloatTimeDomainData(buf); |
| 606 | let peak=0; for(const v of buf) peak=Math.max(peak,Math.abs(v)); |
| 607 | const db=20*Math.log10(peak||1e-9); // -inf..0 dBFS |
| 608 | bar.style.width=Math.max(0,Math.min(100,(db+60)/60*100))+'%'; // -60 dB = empty, 0 = full |
| 609 | if(db>-40) quietSince=performance.now(); |
| 610 | $('#micquiet').style.display = performance.now()-quietSince>4000 ? 'inline':'none'; |
| 611 | micRaf=requestAnimationFrame(tick); |
| 612 | })(); |
| 613 | } |
| 614 | function stopMeter(){ cancelAnimationFrame(micRaf); if(micCtx){ micCtx.close(); micCtx=null; } } |
| 615 | async function useMic(deviceId){ |
| 616 | const next=await openMic(deviceId); |
| 617 | const track=next.getAudioTracks()[0]; |
| 618 | await audioTx.sender.replaceTrack(track); |
| 619 | if(micStream) micStream.getTracks().forEach(t=>t.stop()); |
| 620 | micStream=next; |
| 621 | const id=track.getSettings().deviceId||deviceId; |
| 622 | store.set('micdev',id||''); |
| 623 | await fillMicList(id); |
| 624 | startMeter(next); |
| 625 | } |
| 626 | async function setMic(on){ |
| 627 | const btn=$('#b-mic'); |
| 628 | if(on){ |
| 629 | if(!window.isSecureContext){ toast('Microphone needs secure mode (https)'); return; } |
| 630 | try{ |
| 631 | await useMic(store.get('micdev','')||undefined); |
| 632 | btn.classList.add('active'); $('#micpanel').classList.add('on'); toast('Microphone on'); |
| 633 | }catch(e){ |
| 634 | // a remembered device may be gone (unplugged): fall back to the default once |
| 635 | if(store.get('micdev','')){ store.set('micdev',''); return setMic(true); } |
| 636 | toast('Microphone blocked: '+e.message); |
| 637 | } |
| 638 | }else{ |
| 639 | stopMeter(); |
| 640 | if(audioTx) await audioTx.sender.replaceTrack(null); |
| 641 | if(micStream){ micStream.getTracks().forEach(t=>t.stop()); micStream=null; } |
| 642 | btn.classList.remove('active'); $('#micpanel').classList.remove('on'); |
| 643 | } |
| 644 | } |
| 645 | $('#b-mic').onclick=()=>setMic(!micStream); |
| 646 | |
| 647 | /* ---------- camera ---------- */ |
| 648 | // This device's camera becomes "network-computer camera" on the desk, a |
| 649 | // webcam any app there can pick (a video call in the desk's browser). |
| 650 | async function setCam(on){ |
| 651 | const btn=$('#b-cam'); |
| 652 | if(on){ |
| 653 | if(!window.isSecureContext){ toast('The camera needs secure mode (https)'); return; } |
| 654 | try{ |
| 655 | const s=await navigator.mediaDevices.getUserMedia({audio:false, |
| 656 | video:{width:{ideal:1280},height:{ideal:720},frameRate:{ideal:30},facingMode:'user'}}); |
| 657 | if(camTx) await camTx.sender.replaceTrack(s.getVideoTracks()[0]); |
| 658 | camStream=s; btn.classList.add('active'); |
| 659 | toast('Camera on: pick "network-computer camera" in the app on the desk'); |
| 660 | }catch(e){ toast('Camera blocked: '+e.message); } |
| 661 | }else{ |
| 662 | if(camTx) await camTx.sender.replaceTrack(null).catch(()=>{}); |
| 663 | if(camStream){ camStream.getTracks().forEach(t=>t.stop()); camStream=null; } |
| 664 | btn.classList.remove('active'); |
| 665 | } |
| 666 | } |
| 667 | $('#b-cam').onclick=()=>setCam(!camStream); |
| 668 | |
| 669 | /* ---------- no colour (A/B) ---------- */ |
| 670 | // The desk's picture in grey on this device only (the desk is unchanged): a |
| 671 | // quick A/B for whether something is readable by contrast alone, not by |
| 672 | // colour. Remembered per device. |
| 673 | function setGray(on){ |
| 674 | $('#v').style.filter = on ? 'grayscale(1)' : ''; |
| 675 | $('#b-gray').classList.toggle('active', on); |
| 676 | store.set('gray', on ? '1' : ''); |
| 677 | } |
| 678 | $('#b-gray').onclick=()=>{ setGray(!$('#b-gray').classList.contains('active')); toast($('#b-gray').classList.contains('active') ? 'No colour: the desk in grey (◐ again for colour)' : 'Colour'); }; |
| 679 | setGray(store.get('gray','')==='1'); |
| 680 | $('#mic-dev').onchange=e=>useMic(e.target.value).catch(err=>toast('Could not switch: '+err.message)); |
| 681 | |
| 682 | /* ---------- screen size and scale ---------- */ |
| 683 | // The remote screen can match this window, so it is drawn pixel for pixel |
| 684 | // instead of scaled; "sharp" matches physical pixels on Retina displays. |
| 685 | // Scale makes the desktop's text and controls bigger. |
| 686 | const RES=[['window','Match window'],['sharp','Match window, sharp'],['1280x720','1280 × 720'], |
| 687 | ['1600x900','1600 × 900'],['1920x1080','1920 × 1080'],['2560x1440','2560 × 1440']]; |
| 688 | const SCALES=[1,1.25,1.5,1.75,2]; |
| 689 | let lastDisplay=null, resizeTimer=0; |
| 690 | for(const [v,label] of RES){ const o=document.createElement('option'); o.value=v; o.textContent=label; $('#d-res').appendChild(o); } |
| 691 | for(const v of SCALES){ const o=document.createElement('option'); o.value=v; o.textContent=Math.round(v*100)+'%'; $('#d-scale').appendChild(o); } |
| 692 | $('#d-res').value=store.get('res','window'); $('#d-scale').value=store.get('scale','1'); |
| 693 | function wantedSize(){ |
| 694 | const mode=$('#d-res').value; |
| 695 | if(mode==='window'||mode==='sharp'){ |
| 696 | const k=mode==='sharp'?(window.devicePixelRatio||1):1; |
| 697 | let w=window.innerWidth*k, h=window.innerHeight*k; |
| 698 | const f=Math.min(1,2560/w,1600/h); // the host caps at 2560x1600 |
| 699 | return [Math.floor(w*f/8)*8, Math.floor(h*f/8)*8]; |
| 700 | } |
| 701 | return mode.split('x').map(Number); |
| 702 | } |
| 703 | function sendDisplay(){ |
| 704 | if(!ctl||ctl.readyState!=='open') return; |
| 705 | const [w,h]=wantedSize(), s=parseFloat($('#d-scale').value); |
| 706 | if(lastDisplay&&lastDisplay.w===w&&lastDisplay.h===h&&lastDisplay.s===s) return; |
| 707 | lastDisplay={w,h,s}; |
| 708 | ctl.send(JSON.stringify({t:'display',w,h,s})); |
| 709 | toast(`Screen ${w}×${h} at ${Math.round(s*100)}%`); |
| 710 | } |
| 711 | $('#b-display').onclick=()=>{ const on=$('#disppanel').classList.toggle('on'); $('#b-display').classList.toggle('active',on); }; |
| 712 | $('#d-res').onchange=()=>{ store.set('res',$('#d-res').value); sendDisplay(); }; |
| 713 | $('#d-scale').onchange=()=>{ store.set('scale',$('#d-scale').value); sendDisplay(); }; |
| 714 | window.addEventListener('resize',()=>{ |
| 715 | if(!/^(window|sharp)$/.test($('#d-res').value)) return; |
| 716 | clearTimeout(resizeTimer); resizeTimer=setTimeout(sendDisplay,700); // settle before asking |
| 717 | }); |
| 718 | |
| 719 | /* ---------- keyboard layout ---------- */ |
| 720 | // Keys travel as physical positions (so shortcuts and special keys are exact); |
| 721 | // the desktop turns them into characters with a layout, which has to be the |
| 722 | // layout of the keyboard on this device. Remembered per browser. |
| 723 | // 'auto': this browser translates each character to the US key that makes it, |
| 724 | // so QWERTY, Dvorak, Colemak... work with no setting and the desk stays US. |
| 725 | // Layouts whose shifted characters differ from US set the desk layout instead. |
| 726 | const KBDS=[['auto','Automatic (QWERTY, Dvorak, Colemak…)'],['gb','UK'],['de','German'],['fr','French'],['es','Spanish']]; |
| 727 | for(const [v,label] of KBDS){ const o=document.createElement('option'); o.value=v; o.textContent=label; $('#d-kbd').appendChild(o); } |
| 728 | $('#d-kbd').value=KBDS.some(k=>k[0]===store.get('kbd','auto'))?store.get('kbd','auto'):'auto'; |
| 729 | function sendKeyboard(announce){ |
| 730 | if(!ctl||ctl.readyState!=='open') return; |
| 731 | const v=$('#d-kbd').value; |
| 732 | ctl.send(JSON.stringify({t:'keyboard',layout: v==='auto' ? 'us' : v})); |
| 733 | if(announce) toast('Keyboard: '+$('#d-kbd').selectedOptions[0].textContent); |
| 734 | } |
| 735 | $('#d-kbd').onchange=()=>{ store.set('kbd',$('#d-kbd').value); sendKeyboard(true); }; |
| 736 | // On a Mac, ⌘ acts as Ctrl on the desk, so ⌘C/⌘V/⌘Z do what the fingers expect. |
| 737 | const IS_MAC=/Mac|iPhone|iPad/.test(navigator.platform||navigator.userAgent); |
| 738 | $('#d-cmdl').hidden=!IS_MAC; |
| 739 | $('#d-cmd').checked=store.get('cmd','ctrl')==='ctrl'; |
| 740 | $('#d-cmd').onchange=()=>store.set('cmd',$('#d-cmd').checked?'ctrl':'super'); |
| 741 | function cmdIsCtrl(){ return IS_MAC&&$('#d-cmd').checked; } |
| 742 | |
| 743 | /* ---------- clipboard (text) ---------- */ |
| 744 | // Paste: Ctrl/⌘+V focuses a hidden box so the browser pastes this device's |
| 745 | // clipboard into it (no permission prompt); the text goes to the desk, then |
| 746 | // the V keystroke, both on the ordered control channel. |
| 747 | // Copy: Ctrl/⌘+C or X hands the browser a clipboard write that completes when |
| 748 | // the desk reports the new text (allowed because it starts in the key press). |
| 749 | // Copies made other ways (menus) arrive on their own and are written right |
| 750 | // away, or at the next key press or click where the browser insists on one. |
| 751 | const clipBox=$('#clipbox'); |
| 752 | let clipAsked=false, clipIn='', clipPending=null, copyWaiter=null, pasteArmed=false, pasteTimer=0; |
| 753 | function ctlOpen(){ return ctl&&ctl.readyState==='open'; } |
| 754 | function sendClip(text){ |
| 755 | let i=0; |
| 756 | while(i<text.length){ |
| 757 | let n=Math.min(4096,text.length-i); |
| 758 | const c=text.charCodeAt(i+n-1); |
| 759 | if(n>1&&c>=0xD800&&c<=0xDBFF&&i+n<text.length) n--; // keep surrogate pairs together |
| 760 | const piece=text.slice(i,i+n); i+=n; |
| 761 | ctl.send(JSON.stringify({t:'clip',text:piece,more:i<text.length})); |
| 762 | } |
| 763 | } |
| 764 | function startPaste(){ |
| 765 | pasteArmed=true; clipBox.value=''; clipBox.focus(); |
| 766 | clearTimeout(pasteTimer); pasteTimer=setTimeout(()=>finishPaste(''),400); // no paste event: no text here |
| 767 | } |
| 768 | function finishPaste(text){ |
| 769 | if(!pasteArmed) return; |
| 770 | pasteArmed=false; clearTimeout(pasteTimer); clipBox.blur(); |
| 771 | if(!ctlOpen()) return; |
| 772 | if(text) sendClip(text); |
| 773 | toast(text ? 'Pasted from this device' : "Pasted the desk's clipboard (no text here to send)"); |
| 774 | ctl.send(JSON.stringify({t:'kd',code:'KeyV'})); ctl.send(JSON.stringify({t:'ku',code:'KeyV'})); |
| 775 | } |
| 776 | clipBox.addEventListener('paste',e=>{ e.preventDefault(); finishPaste(e.clipboardData.getData('text/plain')); }); |
| 777 | function startCopy(ask='clipget'){ |
| 778 | ctl.send(JSON.stringify({t:ask})); |
| 779 | if(!window.ClipboardItem||!navigator.clipboard||!navigator.clipboard.write) return; |
| 780 | if(copyWaiter) copyWaiter.rej(new Error('superseded')); |
| 781 | const w={}; const p=new Promise((res,rej)=>{ w.res=res; w.rej=rej; }); |
| 782 | copyWaiter=w; |
| 783 | setTimeout(()=>{ if(copyWaiter===w){ copyWaiter=null; w.rej(new Error('timeout')); } },3000); |
| 784 | navigator.clipboard.write([new ClipboardItem({'text/plain':p})]).then(()=>toast('Copied to this device')).catch(()=>{}); |
| 785 | } |
| 786 | function onCtl(ev){ |
| 787 | let m; try{ m=JSON.parse(ev.data); }catch(e){ return; } |
| 788 | if(m.t==='clip'){ |
| 789 | clipIn+=m.text||''; if(m.more) return; |
| 790 | const t=clipIn; clipIn=''; clipAsked=false; |
| 791 | if(copyWaiter){ copyWaiter.res(new Blob([t],{type:'text/plain'})); copyWaiter=null; return; } |
| 792 | clipPending=t; flushClip(); |
| 793 | }else if(m.t==='voice'){ |
| 794 | onVoice(m); |
| 795 | }else if(m.t==='clipnone'){ |
| 796 | if(copyWaiter){ copyWaiter.rej(new Error('nothing copied')); copyWaiter=null; } |
| 797 | if(clipAsked){ clipAsked=false; toast("The desk's clipboard has no text"); } |
| 798 | } |
| 799 | } |
| 800 | function flushClip(){ |
| 801 | if(clipPending===null||!navigator.clipboard) return; |
| 802 | const t=clipPending; |
| 803 | navigator.clipboard.writeText(t).then(()=>{ if(clipPending===t) clipPending=null; toast('Copied to this device'); }).catch(()=>{}); |
| 804 | } |
| 805 | // The ^V button: no paste event here, so ask for the clipboard (Safari shows |
| 806 | // a small Paste button to confirm, Chrome asks once for permission). |
| 807 | async function pasteButton(){ |
| 808 | let text=''; |
| 809 | try{ text=await navigator.clipboard.readText(); }catch(e){} |
| 810 | if(!ctlOpen()) return; |
| 811 | if(text) sendClip(text); |
| 812 | toast(text ? 'Pasted from this device' : "Pasted the desk's clipboard (no text here to send)"); |
| 813 | for(const [t,code] of [['kd','ControlLeft'],['kd','KeyV'],['ku','KeyV'],['ku','ControlLeft']]) ctl.send(JSON.stringify({t,code})); |
| 814 | } |
| 815 | // Toolbar buttons: move the clipboard without a keystroke, so the desk's own |
| 816 | // menus (right-click → Paste) and this device's apps see it. |
| 817 | $('#b-clipup').onclick=async()=>{ |
| 818 | let text=''; |
| 819 | try{ text=await navigator.clipboard.readText(); }catch(e){} |
| 820 | if(!ctlOpen()) return; |
| 821 | if(!text){ toast('No text on this clipboard (or the browser said no)'); return; } |
| 822 | sendClip(text); toast('Clipboard sent to the desk'); |
| 823 | }; |
| 824 | $('#b-launch').onclick=()=>{ if(ctlOpen()) ctl.send(JSON.stringify({t:'launch'})); if(!grabbing) setGrab(true); }; |
| 825 | $('#b-clipdown').onclick=()=>{ if(!ctlOpen()) return; clipAsked=true; startCopy('clipnow'); }; |
| 826 | /* ---------- files ---------- */ |
| 827 | // Each file has its own reliable channel: {"name","size"}, binary pieces, |
| 828 | // "end"; the other side answers "ok <name>" or "error: <why>". |
| 829 | // Drop on the window: saved on the desk's Desktop. From the desk: the file |
| 830 | // manager's "Send to my device" puts a 📥 button here; click it to save. |
| 831 | // The keyboard only reaches the desk while this page has focus. Dragging a |
| 832 | // file in from Finder, for one, leaves Finder the active app: say so. |
| 833 | function focusHint(){ $('#focushint').classList.toggle('on', !!pc&&pc.connectionState==='connected'&&grabbing&&!document.hasFocus()); } |
| 834 | window.addEventListener('blur',()=>setTimeout(focusHint,300)); |
| 835 | window.addEventListener('focus',focusHint); |
| 836 | $('#focushint').onclick=()=>{ window.focus(); $('#focushint').classList.remove('on'); }; |
| 837 | function fmtSize(n){ return n>=1<<30?(n/(1<<30)).toFixed(1)+' GB':n>=1<<20?(n/(1<<20)).toFixed(1)+' MB':n>=1024?Math.round(n/1024)+' KB':n+' bytes'; } |
| 838 | let dragDepth=0; |
| 839 | window.addEventListener('dragenter',e=>{ if(!pc||!e.dataTransfer||![...e.dataTransfer.types].includes('Files')) return; e.preventDefault(); dragDepth++; $('#dropzone').classList.add('on'); }); |
| 840 | window.addEventListener('dragover',e=>{ if(dragDepth) e.preventDefault(); }); |
| 841 | window.addEventListener('dragleave',()=>{ if(dragDepth&&--dragDepth===0) $('#dropzone').classList.remove('on'); }); |
| 842 | window.addEventListener('drop',async e=>{ |
| 843 | if(!dragDepth) return; |
| 844 | e.preventDefault(); dragDepth=0; $('#dropzone').classList.remove('on'); |
| 845 | if(!pc||pc.connectionState!=='connected'){ toast('Not connected'); return; } |
| 846 | const files=[...e.dataTransfer.files]; |
| 847 | for(const f of files){ |
| 848 | if(!f.size&&!f.type){ toast(f.name+': folders are not supported yet (zip it)'); continue; } |
| 849 | try{ await sendFile(f); }catch(err){ toast(f.name+': '+err.message); } |
| 850 | } |
| 851 | }); |
| 852 | async function sendFile(file){ |
| 853 | const ch=pc.createDataChannel('file'); ch.binaryType='arraybuffer'; |
| 854 | await new Promise((res,rej)=>{ ch.onopen=res; ch.onerror=()=>rej(new Error('channel failed')); }); |
| 855 | let answer=null; const answered=new Promise(res=>{ ch.onmessage=e=>{ answer=String(e.data); res(answer); }; }); |
| 856 | ch.bufferedAmountLowThreshold=1<<20; |
| 857 | ch.send(JSON.stringify({name:file.name,size:file.size})); |
| 858 | const PIECE=16384, READ=PIECE*32; let off=0, shown=0; |
| 859 | while(off<file.size&&answer===null){ |
| 860 | if(ch.bufferedAmount>4<<20) await new Promise(r=>{ ch.onbufferedamountlow=r; }); |
| 861 | const buf=await file.slice(off,off+READ).arrayBuffer(); |
| 862 | for(let i=0;i<buf.byteLength;i+=PIECE) ch.send(buf.slice(i,i+PIECE)); |
| 863 | off+=buf.byteLength; |
| 864 | if(Date.now()-shown>400){ shown=Date.now(); toast(`Sending ${file.name}: ${Math.round(off*100/file.size)}%`); } |
| 865 | } |
| 866 | if(answer===null) ch.send('end'); |
| 867 | const r=await answered; ch.close(); |
| 868 | focusHint(); |
| 869 | if(r.startsWith('ok ')) toast(`On the desk's Desktop: ${r.slice(3)}`); |
| 870 | else throw new Error(r.replace(/^error: /,'')); |
| 871 | } |
| 872 | function receiveFile(ch){ |
| 873 | ch.binaryType='arraybuffer'; |
| 874 | let hdr=null, parts=[], got=0, shown=0; |
| 875 | ch.onmessage=e=>{ |
| 876 | if(typeof e.data==='string'){ |
| 877 | if(!hdr){ try{ hdr=JSON.parse(e.data); }catch(err){ ch.send('error: bad header'); } return; } |
| 878 | if(e.data==='end'){ |
| 879 | if(got!==hdr.size){ ch.send(`error: got ${got} of ${hdr.size} bytes`); return; } |
| 880 | const blob=new Blob(parts, hdr.print?{type:'application/pdf'}:{}); |
| 881 | if(hdr.print) printFile(hdr.name, blob); else offerDownload(hdr.name, blob); |
| 882 | parts=[]; ch.send('ok '+hdr.name); hdr=null; got=0; |
| 883 | } |
| 884 | return; |
| 885 | } |
| 886 | parts.push(e.data); got+=e.data.byteLength; |
| 887 | if(hdr&&hdr.size>1<<20&&Date.now()-shown>400){ shown=Date.now(); toast(`Receiving ${hdr.name}: ${Math.round(got*100/hdr.size)}%`); } |
| 888 | }; |
| 889 | } |
| 890 | // Printed on the desk's "My device" printer: this browser's print dialog. |
| 891 | // Chrome and Firefox print a PDF from a hidden frame; Safari (and iOS) does |
| 892 | // not, and a pop-up without a click is blocked, so there it is a 🖨 button |
| 893 | // that opens the PDF, whose viewer prints. Either way it can be saved too. |
| 894 | function printFile(name,blob){ |
| 895 | const url=URL.createObjectURL(blob), box=$('#downloads'), a=document.createElement('a'); |
| 896 | a.href=url; a.target='_blank'; a.rel='noopener'; a.textContent=`🖨 ${name}`; |
| 897 | a.onclick=()=>setTimeout(()=>{ a.remove(); box.classList.toggle('on',box.children.length>0); },1500); |
| 898 | box.appendChild(a); box.classList.add('on'); |
| 899 | const safari=/^((?!chrome|chromium|android|crios|fxios|edg).)*safari/i.test(navigator.userAgent); |
| 900 | if(safari){ toast(`To print from the desk: ${name} (click 🖨)`); return; } |
| 901 | const f=document.createElement('iframe'); |
| 902 | f.style.cssText='position:fixed;right:0;bottom:0;width:1px;height:1px;border:0;opacity:0'; |
| 903 | f.onload=()=>{ |
| 904 | try{ f.contentWindow.focus(); f.contentWindow.print(); toast(`Printing ${name}`); } |
| 905 | catch(err){ toast(`To print from the desk: ${name} (click 🖨)`); } |
| 906 | }; |
| 907 | f.src=url; document.body.appendChild(f); |
| 908 | setTimeout(()=>f.remove(),10*60*1000); |
| 909 | } |
| 910 | function offerDownload(name,blob){ |
| 911 | const box=$('#downloads'), a=document.createElement('a'), url=URL.createObjectURL(blob); |
| 912 | a.href=url; a.download=name; a.textContent=`📥 ${name} · ${fmtSize(blob.size)}`; |
| 913 | a.onclick=()=>setTimeout(()=>{ a.remove(); URL.revokeObjectURL(url); box.classList.toggle('on',box.children.length>0); },1500); |
| 914 | box.appendChild(a); box.classList.add('on'); |
| 915 | toast(`From the desk: ${name} (click 📥 to save)`); |
| 916 | } |
| 917 | /* ---------- sound ---------- */ |
| 918 | // Browsers may refuse to start audio without a click, and then the page plays |
| 919 | // the video muted. Say so, and unmute at the first click or key press. |
| 920 | function soundHint(){ $('#soundhint').classList.toggle('on', !!$('#v').muted && !!pc); } |
| 921 | function unmute(){ |
| 922 | const v=$('#v'); |
| 923 | if(!v.muted) return; |
| 924 | v.muted=false; v.play().then(()=>{ $('#soundhint').classList.remove('on'); toast('Sound on'); }).catch(()=>{ v.muted=true; soundHint(); }); |
| 925 | } |
| 926 | $('#soundhint').onclick=unmute; |
| 927 | window.addEventListener('pointerdown',unmute,true); |
| 928 | // What the person is using right now: fingers, or a mouse or trackpad (an iPad |
| 929 | // can have either). The desk's dbbasic apps grow their buttons for touch. |
| 930 | let inputMode=''; |
| 931 | // and what kind of device: a phone (small screen, fingers), a tablet, or a desktop; |
| 932 | // the desk lays itself out for it (nc-device) |
| 933 | function deviceClass(){ |
| 934 | const coarse=matchMedia('(pointer: coarse)').matches, small=Math.min(screen.width,screen.height)<600; |
| 935 | return coarse ? (small ? 'phone' : 'tablet') : 'desktop'; |
| 936 | } |
| 937 | function reportInput(kind){ |
| 938 | if(kind===inputMode || !ctl || ctl.readyState!=='open') return; |
| 939 | inputMode=kind; |
| 940 | try{ ctl.send(JSON.stringify({t:'input',kind,device:deviceClass()})); }catch(e){} |
| 941 | } |
| 942 | window.addEventListener('pointerdown',e=>reportInput(e.pointerType==='touch'?'touch':'pointer'),true); |
| 943 | window.addEventListener('keydown',unmute,true); |
| 944 | |
| 945 | /* ---------- voice ---------- */ |
| 946 | // 🎙️ turns listening on the desk on and off (nc-voice hears the microphone |
| 947 | // this page already sends). The panel shows what it heard, said and did. |
| 948 | // One click = one phrase: the desk stops listening when the phrase ends, and |
| 949 | // the microphone goes back off if voice turned it on. "keep listening" makes |
| 950 | // it continuous until the button is clicked again. |
| 951 | let voiceOn=false, micByVoice=false, micOffTimer=0; |
| 952 | $('#voicekeep').checked=store.get('voicekeep','')==='1'; |
| 953 | $('#voicekeep').onchange=()=>store.set('voicekeep',$('#voicekeep').checked?'1':''); |
| 954 | const VOICE_STATE={listening:'Listening…',hearing:'Hearing you…',thinking:'Thinking…',off:'Voice off'}; |
| 955 | function voiceShow(on){ |
| 956 | voiceOn=on; $('#voicefab').classList.toggle('on',on); |
| 957 | clearTimeout(micOffTimer); |
| 958 | if(!on){ |
| 959 | $('#voicestate').textContent='Voice off'; |
| 960 | // the reply's last words are still on their way: stopping the mic now |
| 961 | // makes Safari switch audio modes and cut them off |
| 962 | if(micByVoice) micOffTimer=setTimeout(()=>{ if(!voiceOn&&micByVoice){ micByVoice=false; setMic(false); } },1500); |
| 963 | } |
| 964 | } |
| 965 | async function setVoice(on){ |
| 966 | if(!ctlOpen()) return; |
| 967 | if(on&&!micStream){ await setMic(true); if(!micStream) return; micByVoice=true; } |
| 968 | const once=!$('#voicekeep').checked; |
| 969 | ctl.send(JSON.stringify({t:'voice',on,once})); |
| 970 | voiceShow(on); |
| 971 | $('#voicepanel').classList.add('show'); |
| 972 | if(on) $('#voicestate').textContent=once?'Say one thing…':'Starting…'; |
| 973 | } |
| 974 | function voiceLine(cls,text){ |
| 975 | const log=$('#voicelog'), d=document.createElement('div'); |
| 976 | d.className=cls; d.textContent=text; log.appendChild(d); |
| 977 | while(log.children.length>40) log.firstChild.remove(); |
| 978 | log.scrollTop=log.scrollHeight; |
| 979 | } |
| 980 | function onVoice(m){ |
| 981 | $('#voicepanel').classList.add('show'); |
| 982 | if(m.kind==='state'){ $('#voicestate').textContent=VOICE_STATE[m.text]||m.text; if(m.text==='off') voiceShow(false); } |
| 983 | else if(m.kind==='heard') voiceLine('you',m.text); |
| 984 | else if(m.kind==='said') voiceLine('ai',m.text); |
| 985 | else if(m.kind==='did') voiceLine('did','✓ '+m.text); |
| 986 | else if(m.kind==='error'){ voiceLine('err','⚠ '+m.text); if(/not running/.test(m.text)) voiceShow(false); } |
| 987 | } |
| 988 | $('#voicefab').onclick=()=>setVoice(!voiceOn); |
| 989 | $('#voiceclose').onclick=()=>$('#voicepanel').classList.remove('show'); |
| 990 | function isShortcut(e,code,...codes){ return codes.includes(code)&&(e.ctrlKey||(e.metaKey&&cmdIsCtrl()))&&!e.altKey; } |
| 991 | |
| 992 | /* ---------- fullscreen ---------- */ |
| 993 | $('#b-full').onclick=()=>{ |
| 994 | const el=document.documentElement; |
| 995 | if(document.fullscreenElement) document.exitFullscreen(); |
| 996 | else (el.requestFullscreen||el.webkitRequestFullscreen).call(el); |
| 997 | }; |
| 998 | |
| 999 | /* ---------- keyboard bar ---------- */ |
| 1000 | $('#b-keys').onclick=()=>{ |
| 1001 | const on=$('#keys').classList.toggle('on'); |
| 1002 | $('#b-keys').classList.toggle('active',on); |
| 1003 | }; |
| 1004 | document.querySelectorAll('#keys .key').forEach(k=>{ |
| 1005 | k.onclick=e=>{ |
| 1006 | e.preventDefault(); |
| 1007 | if(k.dataset.combo==='ControlLeft+KeyV'&&ctlOpen()){ pasteButton(); return; } |
| 1008 | if(k.dataset.combo){ |
| 1009 | const codes=k.dataset.combo.split('+'); |
| 1010 | if(k.dataset.combo==='ControlLeft+KeyC'&&ctlOpen()) startCopy(); // a tap counts as a gesture, like a key press |
| 1011 | codes.forEach(c=>sendKey('kd',c)); |
| 1012 | codes.slice().reverse().forEach(c=>sendKey('ku',c)); |
| 1013 | return; |
| 1014 | } |
| 1015 | const code=k.dataset.code; |
| 1016 | if(k.classList.contains('sticky')){ |
| 1017 | const on=k.classList.toggle('on'); |
| 1018 | sendKey(on?'kd':'ku',code); |
| 1019 | }else{ sendKey('kd',code); sendKey('ku',code); } |
| 1020 | }; |
| 1021 | }); |
| 1022 | |
| 1023 | /* ---------- input ---------- */ |
| 1024 | const video=$('#v'), cursor=$('#cursor'), stage=$('#stage'); |
| 1025 | let grabbing=false; |
| 1026 | function send(o){ if(dc&&dc.readyState==='open') dc.send(JSON.stringify(o)); } |
| 1027 | function sendKey(t,code){ send({t,code}); } |
| 1028 | function videoRect(){ |
| 1029 | const r=video.getBoundingClientRect(), vw=video.videoWidth, vh=video.videoHeight; |
| 1030 | if(!vw||!vh) return r; |
| 1031 | const sc=Math.min(r.width/vw,r.height/vh), w=vw*sc, h=vh*sc; |
| 1032 | return {left:r.left+(r.width-w)/2, top:r.top+(r.height-h)/2, width:w, height:h}; |
| 1033 | } |
| 1034 | let pending=null; |
| 1035 | // Presses carry their position (at): moves are batched per frame and travel on an |
| 1036 | // unordered channel, so a quick move-and-click could press where the pointer was. |
| 1037 | function pointAt(e){ const r=videoRect(); return {x:Math.min(1,Math.max(0,(e.clientX-r.left)/r.width)),y:Math.min(1,Math.max(0,(e.clientY-r.top)/r.height)),at:true}; } |
| 1038 | function flushMove(){ if(pending){ send(pending); pending=null; } } |
| 1039 | stage.addEventListener('mousemove',e=>{ |
| 1040 | if(localCursor){cursor.classList.add('on');cursor.style.left=e.clientX+'px';cursor.style.top=e.clientY+'px';} |
| 1041 | if(!grabbing) return; |
| 1042 | const r=videoRect(); |
| 1043 | pending={t:'mm',x:Math.min(1,Math.max(0,(e.clientX-r.left)/r.width)),y:Math.min(1,Math.max(0,(e.clientY-r.top)/r.height))}; |
| 1044 | }); |
| 1045 | (function flush(){ if(pending){send(pending);pending=null;} requestAnimationFrame(flush); })(); |
| 1046 | stage.addEventListener('mousedown',e=>{ |
| 1047 | flushClip(); |
| 1048 | if(!grabbing){setGrab(true);return;} |
| 1049 | e.preventDefault(); flushMove(); send({t:'md',b:e.button,...pointAt(e)}); |
| 1050 | }); |
| 1051 | stage.addEventListener('mouseup',e=>{ if(grabbing){e.preventDefault();flushMove();send({t:'mu',b:e.button,...pointAt(e)});} }); |
| 1052 | stage.addEventListener('contextmenu',e=>e.preventDefault()); |
| 1053 | stage.addEventListener('wheel',e=>{ if(grabbing){e.preventDefault();send({t:'wh',dx:e.deltaX,dy:e.deltaY});} },{passive:false}); |
| 1054 | // Character keys are sent as the US key that produces the same character, so |
| 1055 | // the desktop (US QWERTY) types what this device's layout typed. Other keys |
| 1056 | // (Enter, arrows, F-keys, modifiers) go by physical position. |
| 1057 | const US_CODE={}; |
| 1058 | 'abcdefghijklmnopqrstuvwxyz'.split('').forEach(c=>{ US_CODE[c]=US_CODE[c.toUpperCase()]='Key'+c.toUpperCase(); }); |
| 1059 | '0123456789'.split('').forEach(d=>{ US_CODE[d]='Digit'+d; }); |
| 1060 | Object.assign(US_CODE,{'!':'Digit1','@':'Digit2','#':'Digit3','$':'Digit4','%':'Digit5','^':'Digit6','&':'Digit7', |
| 1061 | '*':'Digit8','(':'Digit9',')':'Digit0','`':'Backquote','~':'Backquote','-':'Minus','_':'Minus','=':'Equal','+':'Equal', |
| 1062 | '[':'BracketLeft','{':'BracketLeft',']':'BracketRight','}':'BracketRight','\\':'Backslash','|':'Backslash', |
| 1063 | ';':'Semicolon',':':'Semicolon',"'":'Quote','"':'Quote',',':'Comma','<':'Comma','.':'Period','>':'Period', |
| 1064 | '/':'Slash','?':'Slash',' ':'Space'}); |
| 1065 | const downAs={}; // physical key -> code sent on keydown, so the keyup always matches |
| 1066 | function codeFor(e){ |
| 1067 | if(cmdIsCtrl()&&(e.code==='MetaLeft'||e.code==='MetaRight')) return e.code==='MetaLeft'?'ControlLeft':'ControlRight'; |
| 1068 | if($('#d-kbd').value!=='auto') return e.code; |
| 1069 | return (e.key && e.key.length===1 && US_CODE[e.key]) || e.code; |
| 1070 | } |
| 1071 | window.addEventListener('keydown',e=>{ |
| 1072 | if(!grabbing) return; |
| 1073 | if(e.code==='Escape'&&e.shiftKey){setGrab(false);return;} |
| 1074 | flushClip(); |
| 1075 | const c=codeFor(e); |
| 1076 | if(isShortcut(e,c,'KeyV')&&ctlOpen()){ |
| 1077 | if(!e.repeat){ downAs[e.code]=null; startPaste(); } // null: its keyup is not sent either |
| 1078 | return; // no preventDefault: the browser must paste |
| 1079 | } |
| 1080 | e.preventDefault(); |
| 1081 | if(e.repeat) return; |
| 1082 | downAs[e.code]=c; send({t:'kd',code:c}); |
| 1083 | if(isShortcut(e,c,'KeyC','KeyX')&&ctlOpen()) startCopy(); |
| 1084 | }); |
| 1085 | window.addEventListener('keyup',e=>{ |
| 1086 | if(!grabbing) return; |
| 1087 | e.preventDefault(); |
| 1088 | if(downAs[e.code]===null){ delete downAs[e.code]; return; } |
| 1089 | const c=downAs[e.code]||codeFor(e); delete downAs[e.code]; send({t:'ku',code:c}); |
| 1090 | // macOS browsers send no keyup for keys released while ⌘ is held: release them now |
| 1091 | if(e.key==='Meta') for(const k of Object.keys(downAs)){ |
| 1092 | if(/^(Shift|Control|Alt|Meta)/.test(k)) continue; |
| 1093 | const d=downAs[k]; delete downAs[k]; if(d) send({t:'ku',code:d}); |
| 1094 | } |
| 1095 | }); |
| 1096 | function setGrab(on){ |
| 1097 | grabbing=on; |
| 1098 | cursor.classList.toggle('on', on&&localCursor); |
| 1099 | stage.style.cursor = on&&localCursor ? 'none' : 'default'; |
| 1100 | toast(on?'Mouse captured · Shift+Esc to release':'Mouse released'); |
| 1101 | } |
| 1102 | |
| 1103 | /* touch: tap = move + click (works from a phone browser) */ |
| 1104 | stage.addEventListener('touchstart',e=>{ |
| 1105 | if(!e.touches.length) return; |
| 1106 | const t=e.touches[0], r=videoRect(); |
| 1107 | send({t:'mm',x:Math.min(1,Math.max(0,(t.clientX-r.left)/r.width)),y:Math.min(1,Math.max(0,(t.clientY-r.top)/r.height))}); |
| 1108 | },{passive:true}); |
| 1109 | stage.addEventListener('touchend',e=>{ flushClip(); send({t:'md',b:0}); send({t:'mu',b:0}); },{passive:true}); |
| 1110 | |
| 1111 | /* auto-hide the top bar */ |
| 1112 | let hideTimer; |
| 1113 | function armAutoHide(){ |
| 1114 | const bar=$('#bar'), handle=$('#barhandle'); |
| 1115 | let edgeTimer=0; |
| 1116 | const show=()=>{ bar.classList.remove('hidden'); clearTimeout(hideTimer); }; |
| 1117 | const hideSoon=ms=>{ clearTimeout(hideTimer); hideTimer=setTimeout(()=>{ if(!bar.matches(':hover')) bar.classList.add('hidden'); },ms); }; |
| 1118 | // reveal only when the pointer rests at the very top edge for a moment |
| 1119 | window.addEventListener('mousemove',e=>{ |
| 1120 | if(e.clientY<=3){ if(!edgeTimer) edgeTimer=setTimeout(()=>{ edgeTimer=0; show(); hideSoon(3000); },250); } |
| 1121 | else { clearTimeout(edgeTimer); edgeTimer=0; |
| 1122 | if(!bar.classList.contains('hidden') && e.clientY>bar.getBoundingClientRect().bottom+40) hideSoon(700); } |
| 1123 | }); |
| 1124 | bar.addEventListener('mouseenter',show); |
| 1125 | bar.addEventListener('mouseleave',()=>hideSoon(1200)); |
| 1126 | handle.addEventListener('click',()=>{ show(); hideSoon(4000); }); |
| 1127 | handle.addEventListener('touchstart',e=>{ e.preventDefault(); show(); hideSoon(5000); },{passive:false}); |
| 1128 | show(); hideSoon(3000); // shown briefly on connect so people know it exists |
| 1129 | } |
| 1130 | |
| 1131 | refreshHosts(); |
| 1132 | </script> |
| 1133 | </html> |