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.

8.3 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// An Android phone's screen through the computer it's connected to (shared/android-relay.js): the
// phones we serve go to the computers of the mesh (not to phones), once per change; theirs say where
// Show Phone Screen goes; a viewer of ours holds adb up and, once it is, gets a scrcpy session of its
// own, the window's input on it in the picture's pixels (a finger, keys by their place, back, panels,
// the screen's power); adb is let go when the session ends.
// Run with: node --test shared/test/android-relay.test.js
'use strict';

const assert = require('assert');
const { EventEmitter } = require('events');
const { test } = require('node:test');

const P = require('../protocol.js');
const { androidRelay } = require('../android-relay.js');
const { PULL } = require('../phone-relay.js');
const { HEADER } = require('../nativescreen.js');
const { KeyState } = require('../../linux/android-keys.js');

const T = P.T;
const KEY = 'ab'.repeat(16);
const PIXEL = 'android-pixel-1';

function hub() {
  const handlers = new Map();
  const peer = new Map();
  const sent = [];
  const sessions = new Map([
    ['ryzen', { connected: true, peerOs: 'win' }],
    ['phone2', { connected: true, peerOs: 'android' }],
  ]);
  return {
    device: { id: 'nitro' },
    sessions,
    sent,
    bus: {
      on: (t, fn) => handlers.set(t, fn),
      onPeer: (ev, fn) => peer.set(ev, fn),
      send: (to, m) => { sent.push([to, m]); return true; },
      connectedIds: () => [...sessions.keys()],
    },
    fire: (t, m, from) => handlers.get(t)(m, from),
    peer: (ev, id) => peer.get(ev)(id),
  };
}

class FakeSession extends EventEmitter {
  constructor(adb, serial, opts) {
    super();
    Object.assign(this, { adb, serial, opts, calls: [], closed: false, width: 0, height: 0 });
    FakeSession.made.push(this);
  }
  start() { return Promise.resolve(); }
  stop() { this.closed = true; this.calls.push(['stop']); }
  touch(...a) { this.calls.push(['touch', ...a]); }
  scroll(...a) { this.calls.push(['scroll', ...a]); }
  key(...a) { this.calls.push(['key', ...a]); }
  tap(k) { this.calls.push(['tap', k]); }
  backOrScreenOn(a) { this.calls.push(['back', a]); }
  setDisplayPower(on) { this.calls.push(['power', on]); }
  simple(t) { this.calls.push(['simple', t]); }
}
FakeSession.made = [];

// A phone connected here: adb up (serial) or not yet; holds counted.
function phoneOf(id, name, serial, extra = {}) {
  const p = { id, name, serial, adb: { id: 'adb' }, holds: 0, opts: {}, ...extra };
  p.hold = (on) => { p.holds += on ? 1 : -1; };
  p.ready = () => (p.serial ? { adb: p.adb, serial: p.serial, opts: p.opts } : null);
  p.why = () => 'not yet';
  return p;
}

function relay(h, list) {
  const changes = [];
  const r = androidRelay({ hub: h, phones: () => list, Session: FakeSession, KeyState, onChange: () => changes.push(1) });
  return { r, changes };
}

function sink() {
  const out = new EventEmitter();
  out.chunks = [];
  out.writableLength = 0;
  out.write = (b) => out.chunks.push(Buffer.from(b));
  out.end = () => {};
  out.writeHead = (code) => { out.code = code; };
  return out;
}

test('the phones we serve go to the computers, not to phones, once per change', () => {
  const h = hub();
  const list = [];
  const { r } = relay(h, list);
  r.refresh();
  assert.deepStrictEqual(h.sent, [], 'none at first: nothing to say');
  list.push(phoneOf(PIXEL, 'Pixel', 'S1'), phoneOf('android-noadb-1', 'Off', ''), { id: 'bad id!', name: 'Bad' });
  r.refresh();
  r.refresh();
  const both = [{ id: PIXEL, name: 'Pixel' }, { id: 'android-noadb-1', name: 'Off' }]; // adb up or not: it comes up when asked
  assert.deepStrictEqual(h.sent, [['ryzen', { t: T.PHONE_SCREENS, phones: both }]]);
  h.peer('welcome', 'phone2');
  h.peer('welcome', 'ryzen');
  assert.deepStrictEqual(h.sent.at(-1), ['ryzen', { t: T.PHONE_SCREENS, phones: both }]);
  assert.strictEqual(h.sent.length, 2);
});

test('another computer\'s phones: Show Phone Screen goes through it, while it\'s connected', () => {
  const h = hub();
  const { r, changes } = relay(h, []);
  h.fire(T.PHONE_SCREENS, { phones: [{ id: PIXEL, name: 'Pixel' }, { id: 'bad id!' }] }, 'ryzen');
  assert.deepStrictEqual(r.screens(), [{ id: PIXEL, name: 'Pixel', os: 'android' }]);
  assert.deepStrictEqual(r.cableFor(PIXEL), { relay: { host: 'ryzen', id: PIXEL, os: 'android' } });
  assert.strictEqual(r.cableFor('android-other-1'), null);
  h.sessions.get('ryzen').connected = false;
  assert.strictEqual(r.cableFor(PIXEL), null);
  h.peer('closed', 'ryzen');
  assert.deepStrictEqual(r.screens(), []);
  assert.strictEqual(changes.length, 2);
});

test('a viewer of ours: its own scrcpy session, the window\'s input in the picture\'s pixels', () => {
  FakeSession.made.length = 0;
  const h = hub();
  const p = phoneOf(PIXEL, 'Pixel', 'S1', { opts: { audio: true, maxFps: 60 } });
  relay(h, [p]);
  h.fire(T.IOS_SCREEN_WANT, { on: true, id: PIXEL, key: KEY, pull: true }, 'ryzen');
  const s = FakeSession.made[0];
  assert.ok(s, 'a session made');
  assert.strictEqual(p.holds, 1, 'adb held up for it');
  assert.strictEqual(s.adb, p.adb);
  assert.strictEqual(s.serial, 'S1');
  assert.strictEqual(s.opts.audio, false, 'no sound for the mesh');
  s.width = 1080;
  s.height = 2400;
  s.emit('size', { width: 1080, height: 2400 });
  const res = sink();
  const { ScreenHost } = require('../phone-relay.js');
  assert.strictEqual(ScreenHost.active.request({ url: PULL, method: 'GET', headers: { [HEADER]: KEY } }, res), true);
  assert.strictEqual(res.code, 200);
  assert.strictEqual(res.chunks[0][0] & 0x80, 0x80, 'the size first');
  s.calls.length = 0;
  h.fire(T.IOS_INPUT, { key: KEY, op: 'touch', a: 0, x: 0.5, y: 0.25 }, 'ryzen');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'touch', a: 1, x: 0.5, y: 0.25 }, 'ryzen');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'scroll', x: 1, y: 1, hs: 0, vs: -2 }, 'ryzen');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'key', code: 'ShiftLeft', down: true }, 'ryzen');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'key', code: 'KeyA', down: true }, 'ryzen');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'back', a: 0 }, 'ryzen');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'button', b: 3 }, 'ryzen');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'power', on: false }, 'ryzen');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'simple', type: 5 }, 'ryzen');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'simple', type: 9 }, 'ryzen'); // not one it passes on (SET_CLIPBOARD)
  assert.deepStrictEqual(s.calls, [
    ['touch', 0, -2, 540, 600, 1080, 2400, 1], ['touch', 1, -2, 540, 600, 1080, 2400, 0],
    ['scroll', 1080, 2400, 1080, 2400, 0, -2],
    ['key', 0, 59, 0x41], ['key', 0, 29, 0x41],
    ['back', 0], ['tap', 3], ['power', false], ['simple', 5],
  ]);
  // Not watched any more: kept a while (a window opened again takes it), not stopped at once.
  h.fire(T.IOS_SCREEN_WANT, { on: false, key: KEY }, 'ryzen');
  assert.ok(!s.closed);
  assert.strictEqual(p.holds, 1);
});

test('a phone whose adb isn\'t up yet: held up, its session once it is; input before that goes nowhere', async () => {
  FakeSession.made.length = 0;
  const h = hub();
  const p = phoneOf(PIXEL, 'Pixel', '');
  relay(h, [p]);
  h.fire(T.IOS_SCREEN_WANT, { on: true, id: PIXEL, key: KEY, pull: true }, 'ryzen');
  assert.strictEqual(p.holds, 1);
  assert.strictEqual(FakeSession.made.length, 0, 'no session before adb is up');
  h.fire(T.IOS_INPUT, { key: KEY, op: 'button', b: 3 }, 'ryzen'); // nowhere to go yet: no throw
  p.serial = 'S9';
  await new Promise((r) => setTimeout(r, 700));
  assert.strictEqual(FakeSession.made.length, 1);
  assert.strictEqual(FakeSession.made[0].serial, 'S9');
  // The session ends (the phone went away): adb let go.
  FakeSession.made[0].emit('closed', 'gone');
  assert.strictEqual(p.holds, 0);
});

test('a phone not connected here any more is refused, and its session goes', () => {
  FakeSession.made.length = 0;
  const h = hub();
  const p = phoneOf(PIXEL, 'Pixel', 'S1');
  const list = [p];
  const { r } = relay(h, list);
  r.refresh();
  h.fire(T.IOS_SCREEN_WANT, { on: true, id: PIXEL, key: KEY, pull: true }, 'ryzen');
  list.length = 0;
  r.refresh();
  assert.deepStrictEqual(FakeSession.made[0].calls.at(-1), ['stop']);
  assert.strictEqual(p.holds, 0, 'adb let go');
  h.fire(T.IOS_SCREEN_WANT, { on: true, id: PIXEL, key: 'cd'.repeat(16), pull: true }, 'ryzen');
  assert.strictEqual(h.sent.at(-1)[1].on, false);
});