@sdeck/elgato 0.0.0-stage → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,317 @@
1
+ /**
2
+ * Encodes and decodes what every Stream Deck but the Mini and the very first one speaks. Nothing here
3
+ * talks to a device. See https://docs.elgato.com/streamdeck/hid/general
4
+ */
5
+ import type { DeckInput, Point, RGB } from "@sdeck/core";
6
+ import { asciiString, assertUint, byteAt, i32At, u16At, u32At } from "./bytes.ts";
7
+
8
+ /** Output reports (bulk data such as images) are at most 1024 bytes, zero padded. */
9
+ export const OUTPUT_REPORT_SIZE = 1024;
10
+ /** Feature reports are at most 32 bytes, zero padded. */
11
+ export const FEATURE_REPORT_SIZE = 32;
12
+
13
+ export const INPUT_REPORT_ID = 0x01;
14
+ export const OUTPUT_REPORT_ID = 0x02;
15
+ export const SETTER_REPORT_ID = 0x03;
16
+
17
+ /** Report IDs used to read data back with a HID "get feature report". */
18
+ export const GetterReportId = {
19
+ FirmwareLd: 0x04,
20
+ FirmwareAp2: 0x05,
21
+ SerialNumber: 0x06,
22
+ FirmwareAp1: 0x07,
23
+ UnitInformation: 0x08,
24
+ SleepDuration: 0x0a,
25
+ } as const;
26
+
27
+ const SetterCommand = {
28
+ ShowLogo: 0x02,
29
+ FillLcd: 0x05,
30
+ FillKey: 0x06,
31
+ Brightness: 0x08,
32
+ SleepDuration: 0x0d,
33
+ } as const;
34
+
35
+ const OutputCommand = {
36
+ ButtonImage: 0x07,
37
+ FullScreenImage: 0x08,
38
+ WindowImage: 0x0b,
39
+ PartialWindowImage: 0x0c,
40
+ } as const;
41
+
42
+ export interface PartialImageRegion {
43
+ /** Logical X coordinate (whatever way images are rotated). */
44
+ x: number;
45
+ /** Logical Y coordinate (whatever way images are rotated). */
46
+ y: number;
47
+ width: number;
48
+ height: number;
49
+ }
50
+
51
+ export interface UnitInformation {
52
+ keyRows: number;
53
+ keyColumns: number;
54
+ keyWidth: number;
55
+ keyHeight: number;
56
+ lcdWidth: number;
57
+ lcdHeight: number;
58
+ imageBitsPerPixel: number;
59
+ imageColorScheme: number;
60
+ galleryKeyImages: number;
61
+ galleryLcdImages: number;
62
+ demoFrames: number;
63
+ }
64
+
65
+ export interface FirmwareVersion {
66
+ checksum: number;
67
+ version: string;
68
+ }
69
+
70
+ function assertRgb([r, g, b]: RGB): void {
71
+ assertUint("red", r, 255);
72
+ assertUint("green", g, 255);
73
+ assertUint("blue", b, 255);
74
+ }
75
+
76
+ // ---------------------------------------------------------------------------
77
+ // Feature reports (setters)
78
+ // ---------------------------------------------------------------------------
79
+
80
+ function featureReport(command: number, ...payload: number[]): Uint8Array {
81
+ const report = new Uint8Array(FEATURE_REPORT_SIZE);
82
+ report[0] = SETTER_REPORT_ID;
83
+ report[1] = command;
84
+ report.set(payload, 2);
85
+ return report;
86
+ }
87
+
88
+ export function showLogoReport(): Uint8Array {
89
+ return featureReport(SetterCommand.ShowLogo);
90
+ }
91
+
92
+ export function fillLcdReport(color: RGB): Uint8Array {
93
+ assertRgb(color);
94
+ return featureReport(SetterCommand.FillLcd, ...color);
95
+ }
96
+
97
+ export function fillKeyReport(index: number, color: RGB): Uint8Array {
98
+ assertUint("key index", index, 255);
99
+ assertRgb(color);
100
+ return featureReport(SetterCommand.FillKey, index, ...color);
101
+ }
102
+
103
+ /** @param percent 0 – 100 */
104
+ export function brightnessReport(percent: number): Uint8Array {
105
+ assertUint("brightness", percent, 100);
106
+ return featureReport(SetterCommand.Brightness, percent);
107
+ }
108
+
109
+ /** @param seconds Idle time before the device sleeps. `0` disables sleep. */
110
+ export function sleepDurationReport(seconds: number): Uint8Array {
111
+ assertUint("sleep duration", seconds, 0x7fffffff);
112
+ return featureReport(
113
+ SetterCommand.SleepDuration,
114
+ seconds & 0xff,
115
+ (seconds >>> 8) & 0xff,
116
+ (seconds >>> 16) & 0xff,
117
+ (seconds >>> 24) & 0xff,
118
+ );
119
+ }
120
+
121
+ // ---------------------------------------------------------------------------
122
+ // Feature reports (getters)
123
+ // ---------------------------------------------------------------------------
124
+
125
+ export function parseFirmwareVersion(report: Uint8Array): FirmwareVersion {
126
+ return {
127
+ checksum: u32At(report, 2),
128
+ version: asciiString(report.subarray(6, 14)),
129
+ };
130
+ }
131
+
132
+ export function parseSerialNumber(report: Uint8Array): string {
133
+ const length = byteAt(report, 1);
134
+ return asciiString(report.subarray(2, 2 + length));
135
+ }
136
+
137
+ export function parseSleepDuration(report: Uint8Array): number {
138
+ return i32At(report, 2);
139
+ }
140
+
141
+ export function parseUnitInformation(report: Uint8Array): UnitInformation {
142
+ if (report.length < 0x0f) {
143
+ throw new RangeError(`unit information report too short: ${report.length} bytes`);
144
+ }
145
+ return {
146
+ keyRows: byteAt(report, 0x01),
147
+ keyColumns: byteAt(report, 0x02),
148
+ keyWidth: u16At(report, 0x03),
149
+ keyHeight: u16At(report, 0x05),
150
+ lcdWidth: u16At(report, 0x07),
151
+ lcdHeight: u16At(report, 0x09),
152
+ imageBitsPerPixel: byteAt(report, 0x0b),
153
+ imageColorScheme: byteAt(report, 0x0c),
154
+ galleryKeyImages: byteAt(report, 0x0d),
155
+ galleryLcdImages: byteAt(report, 0x0e),
156
+ demoFrames: byteAt(report, 0x0f),
157
+ };
158
+ }
159
+
160
+ // ---------------------------------------------------------------------------
161
+ // Output reports (images)
162
+ // ---------------------------------------------------------------------------
163
+
164
+ function chunkedImagePackets(
165
+ image: Uint8Array,
166
+ headerSize: number,
167
+ writeHeader: (packet: Uint8Array, chunkIndex: number, chunkSize: number, isLast: boolean) => void,
168
+ ): Uint8Array[] {
169
+ if (image.length === 0) throw new RangeError("image data is empty");
170
+
171
+ const maxChunk = OUTPUT_REPORT_SIZE - headerSize;
172
+ const packets: Uint8Array[] = [];
173
+
174
+ for (let offset = 0, index = 0; offset < image.length; offset += maxChunk, index++) {
175
+ const chunk = image.subarray(offset, offset + maxChunk);
176
+ const isLast = offset + maxChunk >= image.length;
177
+ if (index > 0xffff) throw new RangeError("image is too large");
178
+
179
+ const packet = new Uint8Array(OUTPUT_REPORT_SIZE);
180
+ packet[0] = OUTPUT_REPORT_ID;
181
+ writeHeader(packet, index, chunk.length, isLast);
182
+ packet.set(chunk, headerSize);
183
+ packets.push(packet);
184
+ }
185
+
186
+ return packets;
187
+ }
188
+
189
+ function standardImagePackets(command: number, byte2: number, image: Uint8Array): Uint8Array[] {
190
+ return chunkedImagePackets(image, 8, (packet, index, size, isLast) => {
191
+ packet[1] = command;
192
+ packet[2] = byte2;
193
+ packet[3] = isLast ? 1 : 0;
194
+ packet[4] = size & 0xff;
195
+ packet[5] = size >>> 8;
196
+ packet[6] = index & 0xff;
197
+ packet[7] = index >>> 8;
198
+ });
199
+ }
200
+
201
+ /** Packets for "Update Button Image" (0x07). `jpeg` must be key-sized, rotated as the model needs. */
202
+ export function buttonImagePackets(index: number, jpeg: Uint8Array): Uint8Array[] {
203
+ assertUint("key index", index, 255);
204
+ return standardImagePackets(OutputCommand.ButtonImage, index, jpeg);
205
+ }
206
+
207
+ /** Packets for "Update Full Screen Image" (0x08). `jpeg` must be screen-sized. */
208
+ export function fullScreenImagePackets(jpeg: Uint8Array): Uint8Array[] {
209
+ return standardImagePackets(OutputCommand.FullScreenImage, 0, jpeg);
210
+ }
211
+
212
+ /** Packets for "Update Window Image" (0x0B). `jpeg` must be window-sized. */
213
+ export function windowImagePackets(jpeg: Uint8Array): Uint8Array[] {
214
+ return standardImagePackets(OutputCommand.WindowImage, 0, jpeg);
215
+ }
216
+
217
+ /**
218
+ * Packets for "Update Partial Window Image" (0x0C).
219
+ * Note this command orders chunk index before chunk size, unlike the others.
220
+ */
221
+ export function partialWindowImagePackets(
222
+ region: PartialImageRegion,
223
+ jpeg: Uint8Array,
224
+ ): Uint8Array[] {
225
+ assertUint("x", region.x, 0xffff);
226
+ assertUint("y", region.y, 0xffff);
227
+ assertUint("width", region.width, 0xffff);
228
+ assertUint("height", region.height, 0xffff);
229
+
230
+ return chunkedImagePackets(jpeg, 16, (packet, index, size, isLast) => {
231
+ packet[1] = OutputCommand.PartialWindowImage;
232
+ packet[2] = region.x & 0xff;
233
+ packet[3] = region.x >>> 8;
234
+ packet[4] = region.y & 0xff;
235
+ packet[5] = region.y >>> 8;
236
+ packet[6] = region.width & 0xff;
237
+ packet[7] = region.width >>> 8;
238
+ packet[8] = region.height & 0xff;
239
+ packet[9] = region.height >>> 8;
240
+ packet[10] = isLast ? 1 : 0;
241
+ packet[11] = index & 0xff;
242
+ packet[12] = index >>> 8;
243
+ packet[13] = size & 0xff;
244
+ packet[14] = size >>> 8;
245
+ packet[15] = 0;
246
+ });
247
+ }
248
+
249
+ // ---------------------------------------------------------------------------
250
+ // Input reports
251
+ // ---------------------------------------------------------------------------
252
+
253
+ const InputCommand = { Buttons: 0x00, Touch: 0x02, Dials: 0x03 } as const;
254
+ const Touch = { Tap: 0x01, Press: 0x02, Flick: 0x03 } as const;
255
+ const Dials = { Pressed: 0x00, Turned: 0x01 } as const;
256
+
257
+ /** What an input report says, or `undefined` for one that is not about keys, dials or touches. */
258
+ export function parseInputReport(data: Uint8Array): DeckInput | undefined {
259
+ if (data.length < 4 || data[0] !== INPUT_REPORT_ID) return undefined;
260
+ const payload = data.subarray(4, 4 + u16At(data, 2));
261
+ const point = (offset: number): Point => ({
262
+ x: u16At(data, offset),
263
+ y: u16At(data, offset + 2),
264
+ });
265
+ switch (data[1]) {
266
+ case InputCommand.Buttons:
267
+ return { type: "buttons", pressed: Array.from(payload, (byte) => byte === 1) };
268
+ case InputCommand.Dials: {
269
+ const dials = payload.subarray(1);
270
+ if (payload[0] === Dials.Pressed) {
271
+ return { type: "dials", pressed: Array.from(dials, (byte) => byte === 1) };
272
+ }
273
+ if (payload[0] !== Dials.Turned) return undefined;
274
+ return { type: "turn", ticks: Array.from(dials, (byte) => (byte << 24) >> 24) };
275
+ }
276
+ case InputCommand.Touch:
277
+ if (data.length < 10) return undefined;
278
+ if (payload[0] === Touch.Tap || payload[0] === Touch.Press) {
279
+ return { type: "tap", at: point(6), long: payload[0] === Touch.Press };
280
+ }
281
+ if (payload[0] !== Touch.Flick || data.length < 14) return undefined;
282
+ return { type: "swipe", from: point(6), to: point(10) };
283
+ default:
284
+ return undefined;
285
+ }
286
+ }
287
+
288
+ /** The report a device sends for `input`. */
289
+ export function inputReport(input: DeckInput): Uint8Array {
290
+ const u16 = (value: number) => [value & 0xff, value >> 8];
291
+ const at = ({ x, y }: Point) => [...u16(x), ...u16(y)];
292
+ const report = (command: number, payload: readonly number[]) =>
293
+ Uint8Array.of(INPUT_REPORT_ID, command, ...u16(payload.length), ...payload);
294
+ switch (input.type) {
295
+ case "buttons":
296
+ return report(InputCommand.Buttons, input.pressed.map(Number));
297
+ case "dials":
298
+ return report(InputCommand.Dials, [Dials.Pressed, ...input.pressed.map(Number)]);
299
+ case "turn":
300
+ return report(InputCommand.Dials, [Dials.Turned, ...input.ticks.map((t) => t & 0xff)]);
301
+ case "tap":
302
+ return report(InputCommand.Touch, [
303
+ input.long ? Touch.Press : Touch.Tap,
304
+ 0,
305
+ ...at(input.at),
306
+ ...[0, 0, 0, 0],
307
+ ]);
308
+ case "swipe":
309
+ return report(InputCommand.Touch, [
310
+ Touch.Flick,
311
+ 0,
312
+ ...at(input.from),
313
+ ...at(input.to),
314
+ ...[0, 0, 0, 0],
315
+ ]);
316
+ }
317
+ }
package/src/virtual.ts ADDED
@@ -0,0 +1,75 @@
1
+ import {
2
+ createMemoryTransport,
3
+ inputDriver,
4
+ type MemoryTransport,
5
+ type VirtualDeck,
6
+ } from "@sdeck/core";
7
+ import { createStreamDeck } from "./detect.ts";
8
+ import { type ElgatoModel, MODELS, type ModelId } from "./models.ts";
9
+ import { FEATURE_REPORT_SIZE, GetterReportId, inputReport } from "./protocol.ts";
10
+
11
+ export interface VirtualStreamDeckOptions {
12
+ /** Serial number the virtual device reports (default `"VIRTUAL"`). */
13
+ serialNumber?: string;
14
+ /** Delay of each USB write in milliseconds (default 0). */
15
+ latencyMs?: number;
16
+ }
17
+
18
+ /**
19
+ * A Stream Deck without hardware, for tests, demos and development. Draw on `deck` as usual, read the
20
+ * result back with `deck.snapshot()`, and drive input with `press`, `tap`, `turnDial`, ...
21
+ */
22
+ export interface VirtualStreamDeck extends VirtualDeck {
23
+ deck: ReturnType<typeof createStreamDeck>;
24
+ /** Records every report the deck sends. */
25
+ transport: MemoryTransport;
26
+ /** Press and release a key. */
27
+ tap(key: number): void;
28
+ }
29
+
30
+ /** A virtual deck that speaks its model's own protocol. */
31
+ export function createVirtualStreamDeck(
32
+ id: ModelId = "neo",
33
+ options: VirtualStreamDeckOptions = {},
34
+ ): VirtualStreamDeck {
35
+ const model: ElgatoModel = MODELS[id];
36
+ const serial = new TextEncoder().encode(options.serialNumber ?? "VIRTUAL");
37
+ const general = model.protocol === "general";
38
+ const transport = createMemoryTransport({
39
+ ...(options.latencyMs !== undefined && { latencyMs: options.latencyMs }),
40
+ getFeature(reportId, length) {
41
+ const report = new Uint8Array(Math.max(length, FEATURE_REPORT_SIZE));
42
+ report[0] = reportId;
43
+ if (reportId === (general ? GetterReportId.SerialNumber : 0x03)) {
44
+ const at = general ? 2 : 5;
45
+ const bytes = serial.subarray(0, report.length - at);
46
+ if (general) report[1] = bytes.length;
47
+ report.set(bytes, at);
48
+ }
49
+ return report;
50
+ },
51
+ });
52
+ const deck = createStreamDeck(transport, model);
53
+ const input = inputDriver(deck, (event) => {
54
+ if (general) return transport.input(inputReport(event));
55
+ if (event.type !== "buttons") return;
56
+ const report = new Uint8Array(65);
57
+ report[0] = 0x01;
58
+ event.pressed.forEach((down, key) => {
59
+ // The first one counts the keys of a row from the right.
60
+ const number = model.protocol === "original" ? key + 4 - 2 * (key % 5) : key;
61
+ report[1 + number] = Number(down);
62
+ });
63
+ transport.input(report);
64
+ });
65
+ return {
66
+ deck,
67
+ transport,
68
+ ...input,
69
+ tap(key) {
70
+ input.press(key);
71
+ input.release(key);
72
+ },
73
+ unplug: () => transport.fail(),
74
+ };
75
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.