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.

4.4 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// A cabled iPhone on iOS 27 (mac/iosusb.js with ios-control's helper): the phone window's input as
// the helper's commands, and the H.265 codec string the window's decoder gets from the helper's
// config packet (an iPhone SE's, as it sent it).
// Run with: node --test mac/test/iosusb-control.test.js
'use strict';

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { test } = require('node:test');

const { IosUsbScreen } = require('../../shared/iosusb.js');
const { ACTION, TYPE } = require('../scrcpy');
const { ANDROID } = require('../android-keys.js');

// VPS, SPS and PPS from an iPhone SE on iOS 27.2.1 (750×1334, coded as 752×1344).
const SE_CONFIG = Buffer.from('0000000140010c01ffff016000000300b000000300000300780cc09000000001420101016000000300b00000030000030078a005e20054162033b914862e7f13f0bfa1bf50ffaa08fd5413faaa0afd55417faaaa0cfd5554a6e040404010000000014401c072f05b24', 'hex');

function screen() {
  const s = new IosUsbScreen({ id: 'udid', control: true });
  const sent = [];
  s.controlled = true;
  s.proc = { stdin: { write: (l) => sent.push(JSON.parse(l)), end() {} }, kill() {} };
  return { s, sent };
}

// The window's left button (phoneview-renderer.js's FINGER: scrcpy's generic finger).
const FINGER = -2;

test('the mouse is one finger, across the picture', () => {
  const { s, sent } = screen();
  s.touch(ACTION.HOVER_MOVE, -1, 10, 10, 750, 1334);
  s.touch(ACTION.MOVE, FINGER, 10, 10, 750, 1334); // no finger down yet
  s.touch(ACTION.DOWN, FINGER, 375, 667, 750, 1334);
  s.touch(ACTION.MOVE, 1, 0, 0, 750, 1334); // a second finger: one at a time
  s.touch(ACTION.MOVE, FINGER, 750, 1334, 750, 1334);
  s.touch(ACTION.UP, FINGER, 900, 1400, 750, 1334);
  assert.deepStrictEqual(sent, [
    { op: 'touch', s: 'down', x: 0.5, y: 0.5 },
    { op: 'touch', s: 'move', x: 1, y: 1 },
    { op: 'touch', s: 'up', x: 1, y: 1 },
  ]);
});

test('keys go by their place, buttons by name, Recents is two presses of home', async () => {
  const { s, sent } = screen();
  assert.strictEqual(s.keyCode('KeyA', true), true);
  s.keyCode('ShiftLeft', false);
  assert.strictEqual(s.keyCode('Fn', true), false);
  s.tap(ANDROID.HOME);
  s.tap(ANDROID.POWER);
  s.tap(ANDROID.BACK); // iOS has none
  s.tap(ANDROID.APP_SWITCH);
  await new Promise((r) => setTimeout(r, 200));
  assert.deepStrictEqual(sent, [
    { op: 'key', u: 0x04, s: 'down' },
    { op: 'key', u: 0xe1, s: 'up' },
    { op: 'button', b: 'home', s: 'press' },
    { op: 'button', b: 'lock', s: 'press' },
    { op: 'button', b: 'home', s: 'press' },
    { op: 'button', b: 'home', s: 'press' },
  ]);
});

test('text is typed key by key, Shift around capitals; a keyframe only for RESET_VIDEO', () => {
  const { s, sent } = screen();
  s.text('Hi!é');
  s.simple(TYPE.ROTATE);
  s.simple(TYPE.RESET_VIDEO);
  assert.deepStrictEqual(sent.map((m) => m.op === 'key' ? `${m.u.toString(16)}${m.s === 'down' ? 'v' : '^'}` : m.op), [
    'e1v', 'bv', 'b^', 'e1^', // H
    'cv', 'c^', // i
    'e1v', '1ev', '1e^', 'e1^', // !
    'keyframe',
  ]);
});

test('the wheel is a short drag from the pointer, the content following it', async () => {
  const { s, sent } = screen();
  s.scroll(375, 667, 750, 1334, 0, 2); // up: the finger moves down
  await new Promise((r) => setTimeout(r, 300));
  assert.strictEqual(sent[0].s, 'down');
  assert.deepStrictEqual({ x: sent[0].x, y: sent[0].y }, { x: 0.5, y: 0.5 });
  const up = sent[sent.length - 1];
  assert.strictEqual(up.s, 'up');
  assert.ok(Math.abs(up.y - 0.62) < 1e-9 && up.x === 0.5, JSON.stringify(up));
  s.stop();
});

test('without the helper running, input goes nowhere', () => {
  const s = new IosUsbScreen({ id: '' });
  s.proc = { stdin: { write: () => assert.fail('wrote'), end() {} }, kill() {} };
  s.touch(ACTION.DOWN, -1, 1, 1, 2, 2);
  s.tap(ANDROID.HOME);
});

test("the window's decoder gets an hvc1 codec string from the helper's config", () => {
  const code = fs.readFileSync(path.join(__dirname, '../phoneview-renderer.js'), 'utf8');
  const hevcCodecOf = new Function(`${code.slice(code.indexOf('function hevcCodecOf'), code.indexOf('let rail = null'))}; return hevcCodecOf;`)();
  assert.strictEqual(hevcCodecOf(new Uint8Array(SE_CONFIG)), 'hvc1.1.6.L120.B0');
  assert.strictEqual(hevcCodecOf(new Uint8Array([0, 0, 0, 1, 0x67, 0x42, 0xe0, 0x1f, 0, 0])), null); // H.264's SPS
});