UNPKG

@yume-chan/scrcpy

Version:
74 lines 2.7 kB
// cspell: ignore autosync import { PromiseResolver } from "@yume-chan/async"; import Struct, { placeholder } from "@yume-chan/struct"; import { ScrcpyOptions1_18 } from "./1_18.js"; import { ScrcpyOptions, toScrcpyOptionValue } from "./types.js"; export const ScrcpyAckClipboardDeviceMessage = new Struct().uint64("sequence"); function toSnakeCase(input) { return input.replace(/([A-Z])/g, "_$1").toLowerCase(); } export const ScrcpySetClipboardControlMessage1_21 = new Struct() .uint8("type") .uint64("sequence") .int8("paste", placeholder()) .uint32("length") .string("content", { lengthField: "length" }); export class ScrcpyOptions1_21 extends ScrcpyOptions { static DEFAULTS = { ...ScrcpyOptions1_18.DEFAULTS, clipboardAutosync: true, }; static serialize(options, defaults) { // 1.21 changed the format of arguments const result = []; for (const [key, value] of Object.entries(options)) { const serializedValue = toScrcpyOptionValue(value, undefined); if (!serializedValue) { continue; } const defaultValue = toScrcpyOptionValue(defaults[key], undefined); if (serializedValue == defaultValue) { continue; } result.push(`${toSnakeCase(key)}=${serializedValue}`); } return result; } get defaults() { return ScrcpyOptions1_21.DEFAULTS; } #clipboardAck = new Map(); constructor(init) { super(ScrcpyOptions1_18, init, ScrcpyOptions1_21.DEFAULTS); } async parseDeviceMessage(id, stream) { switch (id) { case 1: { const message = await ScrcpyAckClipboardDeviceMessage.deserialize(stream); const resolver = this.#clipboardAck.get(message.sequence); if (resolver) { resolver.resolve(); this.#clipboardAck.delete(message.sequence); } return true; } default: return await super.parseDeviceMessage(id, stream); } } serialize() { return ScrcpyOptions1_21.serialize(this.value, this.defaults); } serializeSetClipboardControlMessage(message) { if (message.sequence === 0n) { return ScrcpySetClipboardControlMessage1_21.serialize(message); } const resolver = new PromiseResolver(); this.#clipboardAck.set(message.sequence, resolver); return [ ScrcpySetClipboardControlMessage1_21.serialize(message), resolver.promise, ]; } } //# sourceMappingURL=1_21.js.map