Skip to content
Open
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
6 changes: 6 additions & 0 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {
Palette string
Renderer string // auto, sixel, kitty, or tcell
CaptureFormat string // auto, or an explicit shared jpeg/png source
Scale int // device pixel ratio; the server treats zero as 1
}

func parseFlags() (*Config, error) {
Expand Down Expand Up @@ -68,6 +69,7 @@ func parseFlags() (*Config, error) {
flag.StringVar(&cfg.Renderer, "renderer", "auto", "Rendering protocol: auto, sixel, kitty, tcell")
flag.StringVar(&cfg.Renderer, "r", "auto", "Rendering protocol (short form)")
flag.StringVar(&cfg.CaptureFormat, "capture-format", "auto", "Screenshot source: auto (PNG for Kitty, JPEG otherwise), jpeg, or png")
flag.IntVar(&cfg.Scale, "scale", 1, "Device pixel ratio of the display, from 1 to 4 (e.g. 2 on Retina)")

// Handle both --flag and -flag formats
flag.BoolVar(&cfg.Debug, "d", false, "Enable debug output (shorthand)")
Expand Down Expand Up @@ -154,6 +156,10 @@ func parseFlags() (*Config, error) {
return nil, fmt.Errorf("invalid capture format %q: must be auto, jpeg, or png", cfg.CaptureFormat)
}

if cfg.Scale < 1 || cfg.Scale > 4 {
return nil, fmt.Errorf("invalid scale %d: must be from 1 to 4", cfg.Scale)
}

// Check if splash image exists (only if specified and not NONE)
if cfg.SplashPath != "" && cfg.SplashPath != "NONE" {
if _, err := os.Stat(cfg.SplashPath); os.IsNotExist(err) {
Expand Down
10 changes: 8 additions & 2 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ func updateViewportSize() error {
_, err := grpcClient.SetViewport(ctx, &pb.ViewportSize{
Width: int32(sDims.InnerWidthPx),
Height: int32(sDims.InnerHeightPx),
Scale: uint32(cfg.Scale),
})
if err != nil {
return fmt.Errorf("failed to update viewport size: %v", err)
Expand Down Expand Up @@ -777,7 +778,12 @@ func runMainLoop(s tcell.Screen) error {
func updateScreenDimensions(s tcell.Screen) {
w, h := s.Size()
r := viewportRect(w, h)
sDims = ScreenDimensions{Width: w, Height: h, ViewTop: r.Min.Y, LogHeight: 1, LogPanelTop: h - 2, ViewHeight: r.Dy(), InnerWidth: r.Dx(), InnerViewHeight: r.Dy(), InnerWidthPx: r.Dx() * charSize.Width, InnerHeightPx: r.Dy() * charSize.Height}
// Screenshots span whole CSS pixels, so their size is a multiple of the scale.
wPx, hPx := r.Dx()*charSize.Width, r.Dy()*charSize.Height
if cfg != nil && cfg.Scale > 1 {
wPx, hPx = wPx-wPx%cfg.Scale, hPx-hPx%cfg.Scale
}
sDims = ScreenDimensions{Width: w, Height: h, ViewTop: r.Min.Y, LogHeight: 1, LogPanelTop: h - 2, ViewHeight: r.Dy(), InnerWidth: r.Dx(), InnerViewHeight: r.Dy(), InnerWidthPx: wPx, InnerHeightPx: hPx}
}

// tcell's resize notification is best effort when its event queue is full.
Expand All @@ -801,7 +807,7 @@ func handleResize(s tcell.Screen) {
keyboardHandler.capturePage = false
keyboardHandler.input(&pb.InputEvent{Kind: pb.InputKind_RESET_INPUT})
if sDims.InnerWidthPx > 0 && sDims.InnerHeightPx > 0 {
keyboardHandler.queue(browserOperation{viewport: &pb.ViewportSize{Width: int32(sDims.InnerWidthPx), Height: int32(sDims.InnerHeightPx)}})
keyboardHandler.queue(browserOperation{viewport: &pb.ViewportSize{Width: int32(sDims.InnerWidthPx), Height: int32(sDims.InnerHeightPx), Scale: uint32(cfg.Scale)}})
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions client/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,16 +363,17 @@ func TestLostResizeNotificationStillUpdatesBrowserViewport(t *testing.T) {
s := uiScreen(t, 80, 24)
kh, ops := recorder()
old := keyboardHandler
keyboardHandler = kh
t.Cleanup(func() { keyboardHandler = old })
oldCfg := cfg
keyboardHandler, cfg = kh, &Config{Scale: 2}
t.Cleanup(func() { keyboardHandler, cfg = old, oldCfg })
// SimulationScreen changes size without delivering EventResize, reproducing
// the dropped notification in tcell's bounded event queue under frame load.
s.SetSize(100, 30)
ensureLayout(s)
if sDims.Width != 100 || sDims.Height != 30 {
t.Fatal("layout did not catch up")
}
if len(*ops) != 2 || (*ops)[1].viewport.Width != 784 || (*ops)[1].viewport.Height != 416 {
if len(*ops) != 2 || (*ops)[1].viewport.Width != 784 || (*ops)[1].viewport.Height != 416 || (*ops)[1].viewport.Scale != 2 {
t.Fatal("browser viewport remained stale", *ops)
}
ensureLayout(s)
Expand Down
1 change: 1 addition & 0 deletions proto/bc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ message Message {
message ViewportSize {
int32 width = 1;
int32 height = 2;
uint32 scale = 3;
}

message Coordinate {
Expand Down
29 changes: 18 additions & 11 deletions server/src/browser-controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export class BrowserControls {
private loading = false;
private error = '';
private navigation = 0;
private viewport = { width: 800, height: 600 };
// Successful overrides belong to a control session, not a screenshot session.
private appliedViewports = new WeakMap<CDPSession, { width: number; height: number }>();
// Width and height are device pixels, as are pointer coordinates.
private viewport = { width: 800, height: 600, scale: 1 };
// Successful overrides, per control or capture session.
private appliedViewports = new WeakMap<CDPSession, { width: number; height: number; scale: number }>();

constructor(private readonly ensurePage: () => Promise<Page>) { }

Expand Down Expand Up @@ -76,11 +77,13 @@ export class BrowserControls {
private async applyViewport(session: CDPSession) {
const desired = this.viewport;
const applied = this.appliedViewports.get(session);
if (applied?.width === desired.width && applied?.height === desired.height) return;
if (applied?.width === desired.width && applied?.height === desired.height && applied?.scale === desired.scale) return;
// Failure leaves the browser's size uncertain, including the old size.
this.appliedViewports.delete(session);
await session.send('Emulation.setDeviceMetricsOverride', {
...desired, deviceScaleFactor: 1, mobile: false,
width: Math.floor(desired.width / desired.scale),
height: Math.floor(desired.height / desired.scale),
deviceScaleFactor: desired.scale, mobile: false,
});
this.appliedViewports.set(session, desired);
}
Expand Down Expand Up @@ -128,10 +131,11 @@ export class BrowserControls {
}
}

async setViewport(width: number, height: number) {
async setViewport(width: number, height: number, scale = 1) {
await this.ensurePage();
if (width < 1 || height < 1 || width > 16384 || height > 16384 || width * height > 16 * 1024 * 1024) fail(grpc.status.INVALID_ARGUMENT, 'Invalid viewport size');
this.viewport = { width, height };
if (!Number.isInteger(scale) || scale < 1 || scale > 4 || width < scale || height < scale) fail(grpc.status.INVALID_ARGUMENT, 'Invalid viewport scale');
this.viewport = { width, height, scale };
await this.applyViewport(await this.session());
}

Expand All @@ -144,7 +148,7 @@ export class BrowserControls {
// Retry this read once on a fresh session; persistent failures and
// unrelated errors must still reach the caller.
if (!(error instanceof Error) ||
!error.message.includes('Protocol error (Page.captureScreenshot): Not attached to an active page')) throw error;
!error.message.includes('Not attached to an active page')) throw error;
return this.captureOnce(format);
}
}
Expand All @@ -167,6 +171,8 @@ export class BrowserControls {
const timer = setTimeout(abort, 3000);
try {
if (generation !== this.generation) fail(grpc.status.FAILED_PRECONDITION, 'Page changed during capture');
// Without its own override, a session captures at CSS size.
if (this.viewport.scale !== 1 || this.appliedViewports.has(session)) await this.applyViewport(session);
const cdpPhase = beginCapturePhase('cdp.screenshot');
let result;
try { result = await session.send('Page.captureScreenshot', {
Expand Down Expand Up @@ -321,15 +327,16 @@ export class BrowserControls {
if (!viewport || event.x < 0 || event.y < 0 || event.x >= viewport.width || event.y >= viewport.height || event.buttons > 7) {
fail(grpc.status.INVALID_ARGUMENT, 'Pointer is outside the viewport');
}
const cdp = await this.session(); this.x = event.x; this.y = event.y;
const cdp = await this.session();
this.x = event.x / viewport.scale; this.y = event.y / viewport.scale;
if (inputTraceEnabled) inputTrace('pointer.session', { session: cdp.id() });
const base = { x: event.x, y: event.y, modifiers: event.modifiers };
const base = { x: this.x, y: this.y, modifiers: event.modifiers };
// Chromium needs the active button as well as the held mask
// to treat this as a drag (including text selection).
const button = this.held & 1 ? 'left' : this.held & 2 ? 'right' : this.held & 4 ? 'middle' : 'none';
await cdp.send('Input.dispatchMouseEvent', { ...base, type: 'mouseMoved', buttons: this.held, button });
if (event.kind === InputKind.WHEEL_INPUT) {
await cdp.send('Input.dispatchMouseEvent', { ...base, type: 'mouseWheel', buttons: this.held, deltaX: event.deltaX, deltaY: event.deltaY });
await cdp.send('Input.dispatchMouseEvent', { ...base, type: 'mouseWheel', buttons: this.held, deltaX: event.deltaX / viewport.scale, deltaY: event.deltaY / viewport.scale });
break;
}
for (const [mask, button] of [[1, 'left'], [2, 'right'], [4, 'middle']] as const) {
Expand Down
10 changes: 5 additions & 5 deletions server/src/browser-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ export class BrowserSession {
}));
}

private viewport = { width: 800, height: 600 };
async setViewport(width: number, height: number) {
private viewport = { width: 800, height: 600, scale: 1 };
async setViewport(width: number, height: number, scale = 1) {
const s = await this.snapshot();
await s.active.controls.setViewport(width, height);
this.viewport = { width, height };
await s.active.controls.setViewport(width, height, scale);
this.viewport = { width, height, scale };
}

private captureSelection(): Pick<Snapshot, 'active' | 'generation'> | undefined {
Expand All @@ -346,7 +346,7 @@ export class BrowserSession {
const s = this.captureSelection() ?? await this.snapshot();
endCapturePhase(selection);
const viewport = beginCapturePhase('server.viewport');
try { await s.active.controls.setViewport(this.viewport.width, this.viewport.height); }
try { await s.active.controls.setViewport(this.viewport.width, this.viewport.height, this.viewport.scale); }
finally { endCapturePhase(viewport); }
let data: Buffer;
try { data = await s.active.controls.capture(format); }
Expand Down
4 changes: 2 additions & 2 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ const browserControlHandlers: BrowserControlServer = {
setViewport: async (call: ServerUnaryCall<ViewportSize, Message>, callback: sendUnaryData<Message>) => {
try {
await ensurePage();
const { width, height } = call.request;
await withViewport(() => controls.setViewport(width,height));
const { width, height, scale } = call.request;
await withViewport(() => controls.setViewport(width, height, scale || 1));
logDebug(`Viewport set to ${width}x${height}`);
callback(null, { text: 'Viewport set' });
} catch (error) {
Expand Down
19 changes: 19 additions & 0 deletions server/tests/capture.integration.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { test } = require('node:test');
const assert = require('node:assert/strict');
const puppeteer = require('puppeteer');
const { BrowserControls } = require('../dist/src/browser-controls');
const { InputEvent, InputKind } = require('../dist/generated/bc');

// Read width/height from a JPEG's first SOF marker without a decoder.
function jpegSize(buffer) {
Expand Down Expand Up @@ -64,3 +65,21 @@ test('navigation aborts an in-flight capture without disabling resize or future
assert.deepEqual(jpegSize(jpeg), [400, 217]);
assert.equal(captureCreations, steadyStateCreations, 'steady-state captures reattached the CDP session');
});

test('scale renders whole CSS pixels at device resolution and maps pointer input', { timeout: 60000 }, async t => {
const browser = await puppeteer.launch({ headless: true, defaultViewport: null, args: ['--no-sandbox', '--disable-dev-shm-usage'] });
t.after(() => browser.close());
const page = await browser.newPage();
await page.setContent('<body style="height:5000px" onmousedown="window.down=[event.clientX,event.clientY]">');
const controls = new BrowserControls(async () => page);
await controls.attach(page);
// Odd device sizes floor to whole CSS pixels; the client expects this size.
await controls.setViewport(1601, 1001, 2);
assert.deepEqual(await page.evaluate(() => [innerWidth, innerHeight, devicePixelRatio]), [800, 500, 2]);
const png = await controls.capture('png');
assert.deepEqual([png.readUInt32BE(16), png.readUInt32BE(20)], [1600, 1000]);
for (const buttons of [1, 0]) await controls.input(InputEvent.fromPartial({ kind: InputKind.POINTER_INPUT, x: 400, y: 300, buttons, clickCount: 1 }));
assert.deepEqual(await page.evaluate(() => window.down), [200, 150]);
await controls.input(InputEvent.fromPartial({ kind: InputKind.WHEEL_INPUT, x: 400, y: 300, deltaY: 200 }));
await page.waitForFunction(() => scrollY === 100, { timeout: 5000 });
});