diff --git a/README.md b/README.md
index 7de2ceb9..0eac25f3 100644
--- a/README.md
+++ b/README.md
@@ -350,6 +350,10 @@ localhost:
Phones and tablets get touch input (tap to click, drag, long press to right click, pinch
to zoom, two fingers to move around a zoomed-in view and scroll) and an on-screen
keyboard.
+The toolbar's trackpad button switches to trackpad mode, which works like a laptop's
+trackpad: one finger moves a cursor from where it is (farther the faster it moves), a
+tap clicks and a two-finger tap right clicks where the cursor is, and press and hold,
+then move, drags. The whole screen is the trackpad, and the browser remembers the mode.
**Wayland?** Not yet; on Linux voxl streams X11 displays, Xvfb included. The virtual
desktops voxl is built for run on Xvfb (E2B's and Daytona's do too), and X11 lets voxl
diff --git a/web/index.html b/web/index.html
index 2a901ac6..7d66a532 100644
--- a/web/index.html
+++ b/web/index.html
@@ -92,6 +92,29 @@
display: block;
pointer-events: none;
}
+ /* Trackpad mode takes touches anywhere in the window, around the desktop too. */
+ #pad {
+ position: fixed;
+ inset: 0;
+ touch-action: none;
+ }
+ /* Trackpad mode's cursor (pointer.ts): touch screens show no pointer, so the page
+ draws the desktop's cursor image (cursor.ts) where the pointer is. It scales
+ back by the pinch zoom (zoom.ts) to keep its size, around its hotspot. */
+ #pointer {
+ display: none;
+ position: absolute;
+ z-index: 1;
+ pointer-events: none;
+ background: 0 0 / 100% 100% no-repeat;
+ transform: scale(calc(1 / var(--zoom, 1)));
+ }
+ #surface.trackpad #pointer {
+ display: block;
+ }
+ #surface.trackpad #pointer.none {
+ display: none;
+ }
.stripe {
position: absolute;
left: 0;
@@ -340,19 +363,26 @@
height: 20px;
}
#kb-btn,
+ #tp-btn,
#files-btn,
#mic-btn {
display: none !important;
}
#tools.touch #kb-btn,
+ #tools.touch #tp-btn,
#tools.files #files-btn,
#tools.mic #mic-btn {
display: inline-grid !important;
}
- /* Read-only: nothing on the desktop accepts what the keyboard would send. */
- #tools.read-only #kb-btn {
+ /* Read-only: nothing on the desktop accepts what the keyboard or trackpad would send. */
+ #tools.read-only #kb-btn,
+ #tools.read-only #tp-btn {
display: none !important;
}
+ #tp-btn.on,
+ #tp-btn.on:hover {
+ background: var(--state-pressed);
+ }
/* While the microphone is live the toolbar stays up as a recording indicator. */
#tools.mic-on {
opacity: 1;
@@ -737,7 +767,11 @@
-
+
+
+
+
+
+
+
+
1 && !app.surface.style.cursor)
app.surface.style.cursor = `url(${url}) ${hsx} ${hsy}, default`;
+ Object.assign(pointer.style, {
+ backgroundImage: `url(${url})`,
+ width: `${cw}px`,
+ height: `${ch}px`,
+ margin: `${-hsy}px 0 0 ${-hsx}px`,
+ transformOrigin: `${hsx}px ${hsy}px`,
+ });
+ pointer.classList.remove('none');
}
return {
diff --git a/web/src/client/main.ts b/web/src/client/main.ts
index 5ea78880..81fa93e3 100644
--- a/web/src/client/main.ts
+++ b/web/src/client/main.ts
@@ -100,9 +100,9 @@ function main() {
audio.start();
clipboard.flush();
};
- setupPointer(app, zoom, onInteract);
+ const pointer = setupPointer(app, zoom, onInteract);
setupKeyboard(app, clipboard, onInteract);
- const updateDisplays = setupToolbar(app, video.layout);
+ const updateDisplays = setupToolbar(app, video.layout, pointer.setTrackpad);
const files = createFiles(app);
const mic = createMic(app);
const resize = createResize(app);
diff --git a/web/src/client/pointer.ts b/web/src/client/pointer.ts
index 0878f098..60c7373a 100644
--- a/web/src/client/pointer.ts
+++ b/web/src/client/pointer.ts
@@ -1,14 +1,32 @@
// Mouse, wheel/trackpad and touch input on the desktop surface.
import { cmd } from '../protocol';
import type { App } from './app';
+import { el } from './app';
import { createTouch } from './touch';
import type { Zoom } from './zoom';
/** DOM button -> X button (left, middle, right, back, forward). */
const BUTTONS = [1, 2, 3, 8, 9];
+/** Trackpad mode keeps the cursor this far (CSS px) inside the window in a zoomed-in view. */
+const EDGE = 32;
+
+/** How far (dx) to move the view so that `at` is EDGE inside [0, size]. */
+const inside = (at: number, size: number) =>
+ at < EDGE ? EDGE - at : at > size - EDGE ? size - EDGE - at : 0;
+
+/** A desktop coordinate as a CSS percentage of the surface; the middle if unknown. */
+const percent = (v: number | undefined, size: number) =>
+ v !== undefined && size ? `${(v * 100) / size}%` : '50%';
export function setupPointer(app: App, zoom: Zoom, onInteract: () => void) {
const { surface, send } = app;
+ /** Trackpad mode's cursor: touch screens show no pointer, so the page draws one. */
+ const pointer = el('pointer');
+ /** Behind the desktop and as big as the window: trackpad mode's touches around it. */
+ const pad = el('pad');
+ /** The pointer's desktop position (fractional in trackpad mode); null until it moves. */
+ let pos: { x: number; y: number } | null = null;
+ let trackpad = false;
function toDesk(p: { clientX: number; clientY: number }): [number, number] {
const r = surface.getBoundingClientRect();
@@ -20,13 +38,42 @@ export function setupPointer(app: App, zoom: Zoom, onInteract: () => void) {
];
}
let lastMove = '';
- function move(p: { clientX: number; clientY: number }, force = false) {
- if (!app.desk.w) return;
- const s = cmd.move(...toDesk(p));
+ /** Move the remote pointer to a desktop position; returns it within the desktop. */
+ function moveDesk(x: number, y: number, force = false) {
+ const at = (pos = {
+ x: Math.max(0, Math.min(app.desk.w - 1, x)),
+ y: Math.max(0, Math.min(app.desk.h - 1, y)),
+ });
+ place();
+ const s = cmd.move(Math.round(at.x), Math.round(at.y));
if (s !== lastMove || force) {
lastMove = s;
send(s);
}
+ return at;
+ }
+ function move(p: { clientX: number; clientY: number }, force = false) {
+ if (!app.desk.w) return;
+ moveDesk(...toDesk(p), force);
+ }
+ /** Draw trackpad mode's cursor where the pointer is (the middle until it moves). */
+ function place() {
+ if (!trackpad) return;
+ pointer.style.left = percent(pos?.x, app.desk.w);
+ pointer.style.top = percent(pos?.y, app.desk.h);
+ }
+ /** Trackpad mode: move the pointer by (dx, dy) desktop pixels, and a zoomed-in view
+ * along with it when the cursor nears the window's edge. */
+ function moveBy(dx: number, dy: number, force = false) {
+ if (!app.desk.w) return;
+ const from = pos ?? { x: app.desk.w / 2, y: app.desk.h / 2 };
+ const at = moveDesk(from.x + dx, from.y + dy, force);
+ const r = surface.getBoundingClientRect();
+ const cx = r.left + (at.x * r.width) / app.desk.w,
+ cy = r.top + (at.y * r.height) / app.desk.h;
+ const px = inside(cx, window.innerWidth),
+ py = inside(cy, window.innerHeight);
+ if (px || py) zoom.pan(px, py);
}
// Scroll is sent in desktop pixels, batched per frame; the server turns it into
@@ -52,6 +99,8 @@ export function setupPointer(app: App, zoom: Zoom, onInteract: () => void) {
const touch = createTouch(app, {
moveTo: (x, y) => move({ clientX: x, clientY: y }, true),
+ moveBy,
+ point: () => moveBy(0, 0, true),
queueScroll,
deskPerPx,
zoom,
@@ -88,6 +137,14 @@ export function setupPointer(app: App, zoom: Zoom, onInteract: () => void) {
if (e.pointerType === 'touch') touch.cancel(e);
else send(cmd.releaseAll);
});
+ // The touch captures the pointer to the surface, so its other events come there.
+ pad.addEventListener('pointerdown', (e) => {
+ if (e.pointerType !== 'touch') return;
+ e.preventDefault();
+ onInteract();
+ touch.down(e);
+ });
+ pad.addEventListener('contextmenu', (e) => e.preventDefault());
surface.addEventListener('contextmenu', (e) => e.preventDefault());
surface.addEventListener(
'wheel',
@@ -100,4 +157,15 @@ export function setupPointer(app: App, zoom: Zoom, onInteract: () => void) {
},
{ passive: false }
);
+
+ return {
+ /** Touch moves a cursor like a laptop's trackpad (touch.ts) instead of pointing. */
+ setTrackpad(on: boolean) {
+ trackpad = on;
+ surface.classList.toggle('trackpad', on);
+ pad.hidden = !on;
+ touch.setTrackpad(on);
+ place();
+ },
+ };
}
diff --git a/web/src/client/toolbar.ts b/web/src/client/toolbar.ts
index 1b6c1fda..5fdba765 100644
--- a/web/src/client/toolbar.ts
+++ b/web/src/client/toolbar.ts
@@ -1,13 +1,22 @@
// Toolbar (top right on hover; bottom right and always visible on touch devices):
-// display picker, on-screen keyboard, files, microphone and fullscreen buttons.
+// display picker, on-screen keyboard, trackpad mode, files, microphone and fullscreen
+// buttons.
import { cmd } from '../protocol';
import type { Hello } from '../protocol';
import type { App } from './app';
import { el } from './app';
-export function setupToolbar(app: App, onFullscreenChange: () => void) {
+/** localStorage key that remembers trackpad mode on this device. */
+const TRACKPAD_KEY = 'voxl-trackpad';
+
+export function setupToolbar(
+ app: App,
+ onFullscreenChange: () => void,
+ onTrackpad: (on: boolean) => void
+) {
const { tools, kbd } = app;
const kbBtn = el('kb-btn');
+ const tpBtn = el('tp-btn');
const fsBtn = el('fs-btn');
const picker = el('display-picker');
const displayBtn = el('display-btn');
@@ -112,7 +121,8 @@ export function setupToolbar(app: App, onFullscreenChange: () => void) {
if (ms) hideTimer = setTimeout(() => tools.classList.remove('show'), ms);
}
// Touch devices keep the toolbar visible; mice reveal it near the top edge.
- if (matchMedia('(pointer: coarse)').matches) {
+ const touch = matchMedia('(pointer: coarse)').matches;
+ if (touch) {
tools.classList.add('touch');
show(0);
} else
@@ -138,6 +148,28 @@ export function setupToolbar(app: App, onFullscreenChange: () => void) {
}
});
+ // Trackpad mode: only touch devices show its button, and read-only viewers send
+ // no input to move a cursor with.
+ let trackpad = false;
+ try {
+ trackpad = localStorage.getItem(TRACKPAD_KEY) === '1';
+ } catch {}
+ function applyTrackpad() {
+ const on = trackpad && touch && !app.readOnly;
+ tpBtn.classList.toggle('on', on);
+ tpBtn.setAttribute('aria-pressed', String(on));
+ onTrackpad(on);
+ }
+ tpBtn.addEventListener('click', () => {
+ trackpad = !trackpad;
+ try {
+ if (trackpad) localStorage.setItem(TRACKPAD_KEY, '1');
+ else localStorage.removeItem(TRACKPAD_KEY);
+ } catch {}
+ applyTrackpad();
+ });
+ applyTrackpad();
+
if (!document.fullscreenEnabled) fsBtn.style.display = 'none';
fsBtn.addEventListener('click', () => {
if (document.fullscreenElement) document.exitFullscreen().catch(() => {});
@@ -156,7 +188,9 @@ export function setupToolbar(app: App, onFullscreenChange: () => void) {
fsBtn.classList.toggle('on', !!document.fullscreenElement);
onFullscreenChange();
});
+ // Called with each hello.
return (info: Hello['displays']) => {
+ applyTrackpad();
closeMenu(false);
picker.hidden = !info || info.available.length < 2 || app.readOnly;
displayBtn.disabled = false;
diff --git a/web/src/client/touch.ts b/web/src/client/touch.ts
index ea3651b4..7b2549d5 100644
--- a/web/src/client/touch.ts
+++ b/web/src/client/touch.ts
@@ -1,6 +1,11 @@
// Touch: tap = click, drag = left-drag, long press = right click, pinch = zoom the
// view in or out, two fingers = scroll. The content follows the fingers: two fingers
// move a zoomed-in view, and scroll the desktop once the view reaches its edge.
+//
+// Trackpad mode (the toolbar's trackpad button) makes the screen work like a laptop's
+// trackpad instead: one finger moves the cursor from where it is, farther the faster
+// it moves; tap = click and two-finger tap = right click where the cursor is; press
+// and hold, then move = drag. Two fingers and pinch work as above.
import { cmd } from '../protocol';
import type { App } from './app';
import type { Zoom } from './zoom';
@@ -12,10 +17,21 @@ const LONG_PRESS = 550;
* finger lands a little off. */
const MULTI_TAP_MS = 400;
const MULTI_TAP_PX = 24;
+/** Trackpad mode: two fingers lifted this soon after landing, without moving, right click. */
+const TWO_TAP_MS = 300;
+/** Trackpad mode's pointer acceleration: the cursor moves (1 + ACCEL * speed) times as
+ * far across the desktop on screen as the finger, speed in CSS px per ms, at most
+ * MAX_GAIN times: slow moves are precise, fast ones cross the screen. */
+const ACCEL = 0.8;
+const MAX_GAIN = 3;
interface Hooks {
/** Move the remote pointer to a client (CSS pixel) position. */
moveTo(x: number, y: number): void;
+ /** Trackpad mode: move the remote pointer by (dx, dy) desktop pixels. */
+ moveBy(dx: number, dy: number): void;
+ /** Trackpad mode: send the cursor's position again, so a click lands on it. */
+ point(): void;
queueScroll(dx: number, dy: number): void;
deskPerPx(): number;
zoom: Zoom;
@@ -24,15 +40,28 @@ interface Hooks {
export function createTouch(app: App, hooks: Hooks) {
const { send } = app;
const pts = new Map();
- /** 'two': two fingers down, not yet moved enough to tell a scroll from a pinch. */
- let mode: 'pending' | 'drag' | 'two' | 'scroll' | 'pinch' | 'done' | null =
- null;
+ let trackpad = false;
+ /** 'two': two fingers down, not yet moved enough to tell a scroll from a pinch.
+ * 'move': trackpad mode, one finger moving the cursor. */
+ let mode:
+ | 'pending'
+ | 'drag'
+ | 'move'
+ | 'two'
+ | 'scroll'
+ | 'pinch'
+ | 'done'
+ | null = null;
let start = { x: 0, y: 0 };
let timer: ReturnType | undefined;
let lastMid: [number, number] = [0, 0];
let startMid: [number, number] = [0, 0];
let startSpread = 0;
let lastTap = { x: 0, y: 0, t: -Infinity };
+ /** Trackpad mode: where the finger was, and when, at the last cursor move. */
+ let last = { x: 0, y: 0, t: 0 };
+ /** When the second finger landed. */
+ let twoAt = 0;
const mid = (): [number, number] => {
let x = 0,
y = 0;
@@ -59,6 +88,15 @@ export function createTouch(app: App, hooks: Hooks) {
send(cmd.button(button, true));
send(cmd.button(button, false));
};
+ /** Trackpad mode: move the cursor as far as the finger moved, more when it's fast. */
+ function glide(p: { x: number; y: number }, t: number) {
+ const dx = p.x - last.x,
+ dy = p.y - last.y;
+ const speed = Math.hypot(dx, dy) / Math.max(1, t - last.t);
+ const k = hooks.deskPerPx() * Math.min(MAX_GAIN, 1 + ACCEL * speed);
+ last = { x: p.x, y: p.y, t };
+ hooks.moveBy(dx * k, dy * k);
+ }
function down(e: PointerEvent) {
try {
@@ -68,11 +106,19 @@ export function createTouch(app: App, hooks: Hooks) {
if (pts.size === 1) {
mode = 'pending';
start = { x: e.clientX, y: e.clientY };
+ last = { ...start, t: e.timeStamp };
timer = setTimeout(() => {
if (mode !== 'pending') return;
- hooks.moveTo(start.x, start.y);
- click(3);
- mode = 'done';
+ if (trackpad) {
+ // Held still: press the button where the cursor is and drag from there.
+ hooks.point();
+ send(cmd.button(1, true));
+ mode = 'drag';
+ } else {
+ hooks.moveTo(start.x, start.y);
+ click(3);
+ mode = 'done';
+ }
navigator.vibrate?.(10);
}, LONG_PRESS);
} else {
@@ -80,6 +126,7 @@ export function createTouch(app: App, hooks: Hooks) {
clearTimeout(timer);
if (mode === 'drag') send(cmd.button(1, false));
mode = 'two';
+ twoAt = e.timeStamp;
}
rebase();
}
@@ -91,11 +138,15 @@ export function createTouch(app: App, hooks: Hooks) {
p.y = e.clientY;
if (mode === 'pending' && Math.hypot(p.x - start.x, p.y - start.y) > SLOP) {
clearTimeout(timer);
- mode = 'drag';
- hooks.moveTo(start.x, start.y);
- send(cmd.button(1, true));
+ if (trackpad) mode = 'move';
+ else {
+ mode = 'drag';
+ hooks.moveTo(start.x, start.y);
+ send(cmd.button(1, true));
+ }
}
- if (mode === 'drag') hooks.moveTo(p.x, p.y);
+ if (mode === 'move' || (mode === 'drag' && trackpad)) glide(p, e.timeStamp);
+ else if (mode === 'drag') hooks.moveTo(p.x, p.y);
if (pts.size < 2) return;
if (mode === 'two') {
// A pinch changes the fingers' gap faster than it moves their centroid
@@ -121,19 +172,35 @@ export function createTouch(app: App, hooks: Hooks) {
if (!pts.delete(e.pointerId)) return;
if (mode === 'pending') {
clearTimeout(timer);
- const now = performance.now();
- const at =
- now - lastTap.t < MULTI_TAP_MS &&
- Math.hypot(start.x - lastTap.x, start.y - lastTap.y) < MULTI_TAP_PX
- ? lastTap
- : start;
- lastTap = { x: at.x, y: at.y, t: now };
- hooks.moveTo(at.x, at.y);
- click(1);
+ if (trackpad) {
+ // The cursor stays put between taps, so double-taps double-click.
+ hooks.point();
+ click(1);
+ } else {
+ const now = performance.now();
+ const at =
+ now - lastTap.t < MULTI_TAP_MS &&
+ Math.hypot(start.x - lastTap.x, start.y - lastTap.y) < MULTI_TAP_PX
+ ? lastTap
+ : start;
+ lastTap = { x: at.x, y: at.y, t: now };
+ hooks.moveTo(at.x, at.y);
+ click(1);
+ }
mode = 'done';
} else if (mode === 'drag') {
send(cmd.button(1, false));
mode = 'done';
+ } else if (mode === 'move') mode = 'done';
+ else if (
+ mode === 'two' &&
+ trackpad &&
+ pts.size === 1 &&
+ e.timeStamp - twoAt < TWO_TAP_MS
+ ) {
+ hooks.point();
+ click(3);
+ mode = 'done';
} else if (mode === 'two' || mode === 'scroll' || mode === 'pinch') {
if (pts.size < 2) mode = 'done';
else rebase();
@@ -146,5 +213,12 @@ export function createTouch(app: App, hooks: Hooks) {
pts.delete(e.pointerId);
mode = pts.size ? 'done' : null;
}
- return { down, move, up, cancel };
+ /** Switch trackpad mode on or off, ending a gesture in progress. */
+ function setTrackpad(on: boolean) {
+ trackpad = on;
+ clearTimeout(timer);
+ if (mode === 'drag') send(cmd.button(1, false));
+ mode = pts.size ? 'done' : null;
+ }
+ return { down, move, up, cancel, setTrackpad };
}
diff --git a/web/src/client/zoom.ts b/web/src/client/zoom.ts
index 5681bc27..f00cb96b 100644
--- a/web/src/client/zoom.ts
+++ b/web/src/client/zoom.ts
@@ -38,6 +38,8 @@ export function createZoom(app: App) {
function set(nz: number, ntx: number, nty: number) {
z = limit(nz);
+ // Trackpad mode's cursor scales back by this to keep its size (index.html).
+ surface.style.setProperty('--zoom', String(z));
if (z === 1) {
tx = ty = 0;
surface.style.transform = '';