Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 57 additions & 3 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -737,7 +767,11 @@
</style>
</head>
<body>
<div id="surface" tabindex="0"><canvas id="screen"></canvas></div>
<div id="pad" hidden></div>
<div id="surface" tabindex="0">
<canvas id="screen"></canvas>
<div id="pointer" class="none"></div>
</div>
<textarea
id="kbd"
autocomplete="off"
Expand Down Expand Up @@ -806,6 +840,26 @@
stroke-linejoin="round" />
</svg>
</button>
<button
id="tp-btn"
class="ib"
type="button"
title="Trackpad mode"
aria-label="Trackpad mode"
aria-pressed="false">
<svg class="i" viewBox="0 0 24 24" fill="none">
<path
d="M19 4H5C3.89543 4 3 4.89543 3 6V18C3 19.1046 3.89543 20 5 20H19C20.1046 20 21 19.1046 21 18V6C21 4.89543 20.1046 4 19 4Z"
stroke="currentColor"
stroke-width="2"
stroke-linejoin="round" />
<path
d="M3 15H21M12 15V20"
stroke="currentColor"
stroke-width="2"
stroke-linejoin="round" />
</svg>
</button>
<button
id="mic-btn"
class="ib"
Expand Down
12 changes: 12 additions & 0 deletions web/src/client/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// latency. Cursor messages: type byte, pad, width, height, hot x, hot y (u16 each),
// serial (u32), then RGBA.
import type { App } from './app';
import { el } from './app';

// On-screen cursor size bounds in CSS pixels, applied to the image's longer side.
// 24 is the standard pointer size (Xcursor's default), so a desktop scaled down
Expand All @@ -12,6 +13,8 @@ const MAX_CURSOR_PX = 128;
export function createCursor(app: App) {
let msg: ArrayBuffer | null = null;
const scaled = document.createElement('canvas');
/** Trackpad mode's cursor (pointer.ts), which shows the same image. */
const pointer = el('pointer');

function apply() {
if (!msg) return;
Expand All @@ -22,6 +25,7 @@ export function createCursor(app: App) {
hy = v.getUint16(8);
if (!w || !h) {
app.surface.style.cursor = 'none';
pointer.classList.add('none');
return;
}
const src = document.createElement('canvas');
Expand Down Expand Up @@ -61,6 +65,14 @@ export function createCursor(app: App) {
: `url(${url}) ${hsx} ${hsy}, default`;
if (dpr > 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 {
Expand Down
4 changes: 2 additions & 2 deletions web/src/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
74 changes: 71 additions & 3 deletions web/src/client/pointer.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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',
Expand All @@ -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();
},
};
}
40 changes: 37 additions & 3 deletions web/src/client/toolbar.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLButtonElement>('kb-btn');
const tpBtn = el<HTMLButtonElement>('tp-btn');
const fsBtn = el<HTMLButtonElement>('fs-btn');
const picker = el('display-picker');
const displayBtn = el<HTMLButtonElement>('display-btn');
Expand Down Expand Up @@ -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
Expand All @@ -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(() => {});
Expand All @@ -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;
Expand Down
Loading
Loading