@sdeck/core 0.4.0 → 0.5.1

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.
package/dist/index.js CHANGED
@@ -150,14 +150,26 @@ async function toRgb(source, size, options = {}) {
150
150
  async function encodeJpeg(rgb, size, options = {}) {
151
151
  const { quality = 90, rotation = 0 } = options;
152
152
  let pipeline = sharp(rgb, { raw: { width: size.width, height: size.height, channels: 3 } });
153
- if (rotation === 180)
154
- pipeline = pipeline.rotate(180);
153
+ if (rotation !== 0)
154
+ pipeline = pipeline.rotate(rotation);
155
155
  const jpeg = await pipeline.jpeg({ quality, progressive: false, chromaSubsampling: "4:4:4" }).toBuffer();
156
156
  return new Uint8Array(jpeg.buffer, jpeg.byteOffset, jpeg.byteLength);
157
157
  }
158
158
 
159
159
  // src/layout.ts
160
160
  import sharp2 from "sharp";
161
+ function slotRect(layout, kind, index = 0) {
162
+ switch (kind) {
163
+ case "key":
164
+ return layout.keys[index];
165
+ case "sensor":
166
+ return layout.sensors[index];
167
+ case "dial":
168
+ return layout.dials?.[index];
169
+ case "window":
170
+ return index > 0 ? layout.windows?.[index - 1] : layout.window;
171
+ }
172
+ }
161
173
  function placements(layout, geometry) {
162
174
  if ((layout.keys?.length ?? 0) > geometry.keys.length) {
163
175
  throw new RangeError(`at most ${geometry.keys.length} key images, got ${layout.keys?.length}`);
@@ -225,17 +237,25 @@ class StreamDeck {
225
237
  keyUp: new Set,
226
238
  sensorDown: new Set,
227
239
  sensorUp: new Set,
240
+ dialDown: new Set,
241
+ dialUp: new Set,
242
+ dialTurn: new Set,
243
+ dialTap: new Set,
244
+ windowTap: new Set,
245
+ windowSwipe: new Set,
228
246
  draw: new Set,
229
247
  close: new Set,
230
248
  error: new Set
231
249
  };
232
250
  #unsubscribe;
233
251
  #pressed = [];
252
+ #dials = [];
234
253
  #closed = false;
235
254
  #roots = new Set;
236
255
  #queue = Promise.resolve();
237
256
  #screen;
238
257
  #allSlots;
258
+ #windowSlots;
239
259
  #known = new Set;
240
260
  #gen = new Map;
241
261
  #seq = new Map;
@@ -245,10 +265,16 @@ class StreamDeck {
245
265
  this.transport = transport;
246
266
  this.model = model;
247
267
  this.#screen = new Framebuffer(model.layout.screen);
268
+ const dials = model.layout.dials ?? [];
269
+ const dialSlots = dials.map((_, i) => `dial:${i}`);
270
+ const shared = dials.length > 0 && !model.layout.separateDials;
271
+ this.#windowSlots = shared ? dialSlots : ["window"];
248
272
  this.#allSlots = [
249
273
  ...model.layout.keys.map((_, i) => `key:${i}`),
250
274
  ...model.layout.sensors.map((_, i) => `sensor:${i}`),
251
- ...model.layout.window ? ["window"] : [],
275
+ ...model.layout.window ? this.#windowSlots : [],
276
+ ...model.layout.separateDials ? dialSlots : [],
277
+ ...(model.layout.windows ?? []).map((_, i) => `window:${i + 1}`),
252
278
  "rest"
253
279
  ];
254
280
  this.#unsubscribe = [
@@ -262,12 +288,18 @@ class StreamDeck {
262
288
  get sensorCount() {
263
289
  return this.model.layout.sensors.length;
264
290
  }
291
+ get dialCount() {
292
+ return this.model.layout.dials?.length ?? 0;
293
+ }
265
294
  get keySize() {
266
295
  return this.#size(this.model.layout.keys[0], "keys");
267
296
  }
268
297
  get sensorSize() {
269
298
  return this.#size(this.model.layout.sensors[0], "sensor strips");
270
299
  }
300
+ get dialSize() {
301
+ return this.#size(this.model.layout.dials?.[0], "dials");
302
+ }
271
303
  get windowSize() {
272
304
  return this.#size(this.model.layout.window, "a window");
273
305
  }
@@ -279,8 +311,9 @@ class StreamDeck {
279
311
  }
280
312
  slotDrawing(kind, index = 0) {
281
313
  const { layout, design } = this.model;
282
- const rect = kind === "window" ? layout.window : (kind === "key" ? layout.keys : layout.sensors)[index];
283
- const size = this.#size(rect, kind === "window" ? "a window" : `${kind} ${index}`);
314
+ const rect = slotRect(layout, kind, index);
315
+ const what = kind === "window" ? index ? `window ${index}` : "a window" : `${kind} ${index}`;
316
+ const size = this.#size(rect, what);
284
317
  const base = design?.[kind];
285
318
  if (!base)
286
319
  return { size, scale: 1 };
@@ -321,19 +354,26 @@ class StreamDeck {
321
354
  }
322
355
  }
323
356
  }
324
- sendWindowJpeg(_jpeg) {
357
+ sendWindowImage(_image) {
325
358
  return Promise.reject(new Error(`${this.model.name} has no window`));
326
359
  }
327
- sendWindowRegionJpeg(_region, _jpeg) {
360
+ sendWindowRegionImage(_region, _image) {
328
361
  return Promise.reject(new Error(`${this.model.name} cannot update part of the window`));
329
362
  }
330
363
  async sendFillKey(index, color) {
331
364
  const size = this.#size(this.model.layout.keys[index], "keys");
332
- await this.sendKeyJpeg(index, await this.#encode(solidRgb(size, color), size));
365
+ await this.sendKeyImage(index, await this.encodeImage(solidRgb(size, color), size));
333
366
  }
334
367
  async sendFillScreen(color) {
335
368
  const size = this.screenSize;
336
- await this.sendScreenJpeg(await this.#encode(solidRgb(size, color), size));
369
+ await this.sendScreenImage(await this.encodeImage(solidRgb(size, color), size));
370
+ }
371
+ encodeImage(rgb, size, quality) {
372
+ return encodeJpeg(rgb, size, { quality, rotation: this.model.rotation });
373
+ }
374
+ prepareFrame(screen, quality) {
375
+ const image = this.#encode(screen.data.slice(), this.screenSize, quality);
376
+ return async () => this.sendScreenImage(await image);
337
377
  }
338
378
  async onClosing() {}
339
379
  async ping() {
@@ -348,27 +388,59 @@ class StreamDeck {
348
388
  if (!current())
349
389
  return;
350
390
  await this.#commit([slot], rect, rgb, () => {
351
- const jpeg = this.#encode(rgb, rect, options?.quality);
352
- return async () => this.sendKeyJpeg(index, await jpeg);
391
+ const encoded = this.#encode(rgb, rect, options?.quality);
392
+ return async () => this.sendKeyImage(index, await encoded);
353
393
  });
354
394
  }
355
395
  async setWindowImage(image, options) {
356
396
  const rect = this.#windowRect();
357
- const current = this.#claim(["window"]);
397
+ const current = this.#claim(this.#windowSlots);
358
398
  const rgb = await toRgb(image, rect, options);
359
399
  if (!current())
360
400
  return;
361
- await this.#commit(["window"], rect, rgb, () => {
362
- const jpeg = this.#encode(rgb, rect, options?.quality);
363
- return async () => this.sendWindowJpeg(await jpeg);
401
+ await this.#commit(this.#windowSlots, rect, rgb, () => {
402
+ const encoded = this.#encode(rgb, rect, options?.quality);
403
+ return async () => this.sendWindowImage(await encoded);
404
+ });
405
+ }
406
+ async setExtraWindowImage(index, image, options) {
407
+ const rect = slotRect(this.model.layout, "window", index);
408
+ if (index < 1 || !rect)
409
+ throw new RangeError(`${this.model.name} has no window ${index}`);
410
+ await this.#setOwnPlace(`window:${index}`, rect, image, options);
411
+ }
412
+ async#setOwnPlace(slot, rect, image, options) {
413
+ const current = this.#claim([slot]);
414
+ const rgb = await toRgb(image, rect, options);
415
+ if (!current() || this.#unchanged([slot], rect, rgb))
416
+ return;
417
+ this.#screen.blit(rect, rgb);
418
+ this.#touch([slot]);
419
+ await this.#requestFrame(options?.quality);
420
+ }
421
+ async setDialImage(index, image, options) {
422
+ this.assertDial(index);
423
+ const slot = `dial:${index}`;
424
+ const rect = this.model.layout.dials?.[index];
425
+ if (this.model.layout.separateDials)
426
+ return this.#setOwnPlace(slot, rect, image, options);
427
+ const win = this.#windowRect();
428
+ const current = this.#claim([slot]);
429
+ const rgb = await toRgb(image, rect, options);
430
+ if (!current())
431
+ return;
432
+ await this.#commit([slot], rect, rgb, () => {
433
+ const encoded = this.#encode(rgb, rect, options?.quality);
434
+ const region = { ...rect, x: rect.x - win.x, y: rect.y - win.y };
435
+ return async () => this.sendWindowRegionImage(region, await encoded);
364
436
  });
365
437
  }
366
438
  async setWindowRegionImage(region, image, options) {
367
439
  const rect = this.#windowRegion(region);
368
440
  const rgb = await toRgb(image, rect, options);
369
- await this.#commit(["window"], rect, rgb, () => {
370
- const jpeg = this.#encode(rgb, rect, options?.quality);
371
- return async () => this.sendWindowRegionJpeg(region, await jpeg);
441
+ await this.#commit(this.#windowSlots, rect, rgb, () => {
442
+ const encoded = this.#encode(rgb, rect, options?.quality);
443
+ return async () => this.sendWindowRegionImage(region, await encoded);
372
444
  });
373
445
  }
374
446
  async setSensorImage(index, image, options) {
@@ -409,17 +481,17 @@ class StreamDeck {
409
481
  }
410
482
  setKeyJpeg(index, jpeg) {
411
483
  this.assertKey(index);
412
- return this.#raw([`key:${index}`], () => this.sendKeyJpeg(index, jpeg));
484
+ return this.#raw([`key:${index}`], () => this.sendKeyImage(index, jpeg));
413
485
  }
414
486
  setScreenJpeg(jpeg) {
415
- return this.#raw(this.#allSlots, () => this.sendScreenJpeg(jpeg));
487
+ return this.#raw(this.#allSlots, () => this.sendScreenImage(jpeg));
416
488
  }
417
489
  setWindowJpeg(jpeg) {
418
- return this.#raw(["window"], () => this.sendWindowJpeg(jpeg));
490
+ return this.#raw(this.#windowSlots, () => this.sendWindowImage(jpeg));
419
491
  }
420
492
  setWindowRegionJpeg(region, jpeg) {
421
493
  this.#windowRegion(region);
422
- return this.#raw(["window"], () => this.sendWindowRegionJpeg(region, jpeg));
494
+ return this.#raw(this.#windowSlots, () => this.sendWindowRegionImage(region, jpeg));
423
495
  }
424
496
  readScreen(region) {
425
497
  return this.#screen.read(region);
@@ -430,9 +502,9 @@ class StreamDeck {
430
502
  return new Uint8Array(png.buffer, png.byteOffset, png.byteLength);
431
503
  }
432
504
  #encode(rgb, size, quality) {
433
- const jpeg = encodeJpeg(rgb, size, { quality, rotation: this.model.rotation });
434
- jpeg.catch(() => {});
435
- return jpeg;
505
+ const image = this.encodeImage(rgb, size, quality);
506
+ image.catch(() => {});
507
+ return image;
436
508
  }
437
509
  #windowRegion(region) {
438
510
  const win = this.#windowRect();
@@ -516,8 +588,7 @@ class StreamDeck {
516
588
  const frame = this.#frame;
517
589
  this.#frame = undefined;
518
590
  const gens = this.#gens(this.#allSlots);
519
- const jpeg = this.#encode(this.#screen.data.slice(), this.screenSize, frame.quality);
520
- const write = this.enqueue(async () => this.sendScreenJpeg(await jpeg));
591
+ const write = this.enqueue(this.prepareFrame(this.#screen, frame.quality));
521
592
  try {
522
593
  await this.#settle(write, gens, this.#screenRect);
523
594
  for (const waiter of frame.waiters)
@@ -539,9 +610,19 @@ class StreamDeck {
539
610
  this.assertSensor(index);
540
611
  return this.#mount("sensor", index, (frame) => this.setSensorImage(index, frame, { ...options, fit: "fill" }), element, options);
541
612
  }
613
+ mountDial(index, element, options) {
614
+ this.assertDial(index);
615
+ return this.#mount("dial", index, (frame) => this.setDialImage(index, frame, { ...options, fit: "fill" }), element, options);
616
+ }
542
617
  mountWindow(element, options) {
543
618
  return this.#mount("window", 0, (frame) => this.setWindowImage(frame, { ...options, fit: "fill" }), element, options);
544
619
  }
620
+ mountExtraWindow(index, element, options) {
621
+ if (index < 1 || !slotRect(this.model.layout, "window", index)) {
622
+ throw new RangeError(`${this.model.name} has no window ${index}`);
623
+ }
624
+ return this.#mount("window", index, (frame) => this.setExtraWindowImage(index, frame, { ...options, fit: "fill" }), element, options);
625
+ }
545
626
  mountScreen(element, options) {
546
627
  return this.#mount("screen", 0, (frame) => this.setScreenImage(frame, { ...options, fit: "fill" }), element, options);
547
628
  }
@@ -601,6 +682,11 @@ class StreamDeck {
601
682
  throw new RangeError(`sensor index must be 0–${Math.max(0, this.sensorCount - 1)}, got ${index}`);
602
683
  }
603
684
  }
685
+ assertDial(index) {
686
+ if (!Number.isInteger(index) || index < 0 || index >= this.dialCount) {
687
+ throw new RangeError(`dial index must be 0–${Math.max(0, this.dialCount - 1)}, got ${index}`);
688
+ }
689
+ }
604
690
  enqueue(task) {
605
691
  if (this.#closed)
606
692
  return Promise.reject(new Error(`${this.model.name} is closed`));
@@ -614,23 +700,52 @@ class StreamDeck {
614
700
  return run;
615
701
  }
616
702
  #handleInput(data) {
617
- const states = this.parseInput(data);
618
- if (!states)
619
- return;
620
- const keys = this.keyCount;
621
- for (let i = 0;i < states.length; i++) {
622
- const pressed = states[i] === true;
623
- if (pressed === (this.#pressed[i] === true))
624
- continue;
625
- if (i < keys) {
626
- this.emit(pressed ? "keyDown" : "keyUp", i);
627
- } else {
628
- this.emit(pressed ? "sensorDown" : "sensorUp", i - keys);
703
+ const input = this.parseInput(data);
704
+ switch (input?.type) {
705
+ case "buttons": {
706
+ const before = this.#pressed;
707
+ this.#pressed = input.pressed;
708
+ changes(before, input.pressed, (i, down) => {
709
+ if (i < this.keyCount)
710
+ this.emit(down ? "keyDown" : "keyUp", i);
711
+ else
712
+ this.emit(down ? "sensorDown" : "sensorUp", i - this.keyCount);
713
+ });
714
+ break;
715
+ }
716
+ case "dials": {
717
+ const before = this.#dials;
718
+ this.#dials = input.pressed;
719
+ changes(before, input.pressed, (i, down) => this.emit(down ? "dialDown" : "dialUp", i));
720
+ break;
629
721
  }
722
+ case "turn":
723
+ input.ticks.forEach((ticks, i) => {
724
+ if (ticks !== 0)
725
+ this.emit("dialTurn", i, ticks);
726
+ });
727
+ break;
728
+ case "tap": {
729
+ this.emit("windowTap", input.at, input.long);
730
+ const { dials = [], window } = this.model.layout;
731
+ const x = (window?.x ?? 0) + input.at.x;
732
+ const dial = dials.findIndex((r) => x >= r.x && x < r.x + r.width);
733
+ if (dial >= 0)
734
+ this.emit("dialTap", dial, input.long);
735
+ break;
736
+ }
737
+ case "swipe":
738
+ this.emit("windowSwipe", input.from, input.to);
739
+ break;
630
740
  }
631
- this.#pressed = states;
632
741
  }
633
742
  }
743
+ function changes(before, now, changed) {
744
+ now.forEach((down, i) => {
745
+ if (down !== (before[i] === true))
746
+ changed(i, down);
747
+ });
748
+ }
634
749
  // src/detect.ts
635
750
  import { devices, devicesAsync, HID } from "node-hid";
636
751
 
@@ -729,7 +844,8 @@ async function openDeck(drivers, options = {}) {
729
844
  break;
730
845
  await sleep(Math.min(pollIntervalMs, remaining), signal);
731
846
  }
732
- const names = asArray(drivers).map(({ model }) => model.name).join(" or ");
847
+ const all = asArray(drivers).map(({ model }) => model.name);
848
+ const names = all.length > 3 ? "deck" : all.join(" or ");
733
849
  const filtered = options.path !== undefined || options.serialNumber !== undefined;
734
850
  throw new Error(`No ${names} found${filtered ? " matching the given path/serialNumber" : ""}`, {
735
851
  cause: lastError
@@ -946,12 +1062,12 @@ class SoftwareDeck extends StreamDeck {
946
1062
  this.#serial = serial;
947
1063
  }
948
1064
  parseInput(data) {
949
- return Array.from(data, (byte) => byte === 1);
1065
+ return JSON.parse(new TextDecoder().decode(data));
950
1066
  }
951
- async sendKeyJpeg() {}
952
- async sendScreenJpeg() {}
953
- async sendWindowJpeg() {}
954
- async sendWindowRegionJpeg() {}
1067
+ async sendKeyImage() {}
1068
+ async sendScreenImage() {}
1069
+ async sendWindowImage() {}
1070
+ async sendWindowRegionImage() {}
955
1071
  async sendFillKey() {}
956
1072
  async sendFillScreen() {}
957
1073
  async setBrightness() {}
@@ -971,23 +1087,41 @@ function createVirtualDeck(model, options = {}) {
971
1087
  ...model
972
1088
  };
973
1089
  const deck = new SoftwareDeck(transport, full, options.serialNumber ?? "VIRTUAL");
974
- const { keys, sensors } = full.layout;
975
- const states = new Uint8Array(keys.length + sensors.length);
976
- const set = (kind, index, pressed) => {
977
- const count = kind === "key" ? keys.length : sensors.length;
1090
+ const send = (input) => transport.input(new TextEncoder().encode(JSON.stringify(input)));
1091
+ return { deck, ...inputDriver(deck, send), unplug: () => transport.fail() };
1092
+ }
1093
+ function inputDriver(deck, send) {
1094
+ const { keyCount, sensorCount, dialCount } = deck;
1095
+ const buttons = new Array(keyCount + sensorCount).fill(false);
1096
+ const dials = new Array(dialCount).fill(false);
1097
+ const check = (kind, index, count) => {
978
1098
  if (!Number.isInteger(index) || index < 0 || index >= count) {
979
1099
  throw new RangeError(`there is no ${kind} ${index}`);
980
1100
  }
981
- states[kind === "key" ? index : keys.length + index] = pressed ? 1 : 0;
982
- transport.input(states.slice());
1101
+ };
1102
+ const button = (kind, index, down) => {
1103
+ check(kind, index, kind === "key" ? keyCount : sensorCount);
1104
+ buttons[kind === "key" ? index : keyCount + index] = down;
1105
+ send({ type: "buttons", pressed: [...buttons] });
1106
+ };
1107
+ const dial = (index, down) => {
1108
+ check("dial", index, dialCount);
1109
+ dials[index] = down;
1110
+ send({ type: "dials", pressed: [...dials] });
983
1111
  };
984
1112
  return {
985
- deck,
986
- press: (index) => set("key", index, true),
987
- release: (index) => set("key", index, false),
988
- touch: (index) => set("sensor", index, true),
989
- untouch: (index) => set("sensor", index, false),
990
- unplug: () => transport.fail()
1113
+ press: (index) => button("key", index, true),
1114
+ release: (index) => button("key", index, false),
1115
+ touch: (index) => button("sensor", index, true),
1116
+ untouch: (index) => button("sensor", index, false),
1117
+ pressDial: (index) => dial(index, true),
1118
+ releaseDial: (index) => dial(index, false),
1119
+ turnDial(index, ticks) {
1120
+ check("dial", index, dialCount);
1121
+ send({ type: "turn", ticks: dials.map((_, i) => i === index ? ticks : 0) });
1122
+ },
1123
+ tapWindow: (at, long = false) => send({ type: "tap", at, long }),
1124
+ swipeWindow: (from, to) => send({ type: "swipe", from, to })
991
1125
  };
992
1126
  }
993
1127
  export {
@@ -1002,6 +1136,7 @@ export {
1002
1136
  flattenRgba,
1003
1137
  fromNodeHid,
1004
1138
  getElementRuntime,
1139
+ inputDriver,
1005
1140
  isRawImage,
1006
1141
  isRenderable,
1007
1142
  listDecks,
@@ -1011,6 +1146,7 @@ export {
1011
1146
  placements,
1012
1147
  prepareImage,
1013
1148
  registerElementRuntime,
1149
+ slotRect,
1014
1150
  solidRgb,
1015
1151
  toDeviceJpeg,
1016
1152
  toError,
@@ -1018,4 +1154,4 @@ export {
1018
1154
  watchDeck
1019
1155
  };
1020
1156
 
1021
- //# debugId=D9F9FD60DFCB57FA64756E2164756E21
1157
+ //# debugId=F69F19C3447687EB64756E2164756E21