refactor(scrcpy): rewrite option classes to improve tree-shaking

This commit is contained in:
Simon Chan 2024-11-25 18:10:15 +08:00
parent db8466f6ee
commit 92472007db
No known key found for this signature in database
GPG key ID: A8B69F750B9BCEDD
218 changed files with 5412 additions and 2380 deletions

View file

@ -0,0 +1,50 @@
import { getInt16, setInt16 } from "@yume-chan/no-data-view";
import type { Field, StructInit } from "@yume-chan/struct";
import { bipedal, struct, u16, u32, u8 } from "@yume-chan/struct";
import type { ScrcpyScrollController } from "../../base/index.js";
import type { ScrcpyInjectScrollControlMessage } from "../../latest.js";
import { PrevImpl } from "./prev.js";
export const SignedFloat: Field<number, never, never> = {
size: 2,
serialize(value, { buffer, index, littleEndian }) {
// https://github.com/Genymobile/scrcpy/blob/1f138aef41de651668043b32c4effc2d4adbfc44/app/src/util/binary.h#L51
value = PrevImpl.clamp(value, -1, 1);
value = value === 1 ? 0x7fff : value * 0x8000;
setInt16(buffer, index, value, littleEndian);
},
deserialize: bipedal(function* (then, { reader, littleEndian }) {
const data = yield* then(reader.readExactly(2));
const value = getInt16(data, 0, littleEndian);
// https://github.com/Genymobile/scrcpy/blob/1f138aef41de651668043b32c4effc2d4adbfc44/server/src/main/java/com/genymobile/scrcpy/Binary.java#L34
return value === 0x7fff ? 1 : value / 0x8000;
}),
};
export const InjectScrollControlMessage = struct(
{
type: u8,
pointerX: u32,
pointerY: u32,
screenWidth: u16,
screenHeight: u16,
scrollX: SignedFloat,
scrollY: SignedFloat,
buttons: u32,
},
{ littleEndian: false },
);
export type InjectScrollControlMessage = StructInit<
typeof InjectScrollControlMessage
>;
export class ScrollController implements ScrcpyScrollController {
serializeScrollMessage(
message: ScrcpyInjectScrollControlMessage,
): Uint8Array | undefined {
return InjectScrollControlMessage.serialize(message);
}
}