@sdeck/plugin-audio 0.0.0-stage → 0.1.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.
- package/bin/sdeck-audio.exe +0 -0
- package/dist/helper.d.ts +95 -0
- package/dist/helper.d.ts.map +1 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1466 -0
- package/dist/index.js.map +22 -0
- package/native/Cargo.lock +230 -0
- package/native/Cargo.toml +29 -0
- package/native/src/main.rs +28 -0
- package/native/src/policy.rs +41 -0
- package/native/src/protocol.rs +132 -0
- package/native/src/windows_audio.rs +609 -0
- package/package.json +65 -4
- package/src/helper.ts +270 -0
- package/src/index.tsx +541 -0
- package/README.md +0 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,1466 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.tsx
|
|
3
|
+
import { existsSync as existsSync2 } from "fs";
|
|
4
|
+
|
|
5
|
+
// ../core/src/element.ts
|
|
6
|
+
var runtime;
|
|
7
|
+
function registerElementRuntime(next) {
|
|
8
|
+
runtime = next;
|
|
9
|
+
}
|
|
10
|
+
// ../react/src/render.ts
|
|
11
|
+
function isReactElement(value) {
|
|
12
|
+
return typeof value === "object" && value !== null && "$$typeof" in value;
|
|
13
|
+
}
|
|
14
|
+
var defaultRenderer = {};
|
|
15
|
+
var fontRenderers = new WeakMap;
|
|
16
|
+
async function createRenderer(fonts) {
|
|
17
|
+
const { Renderer } = await import("@takumi-rs/core");
|
|
18
|
+
const renderer = new Renderer;
|
|
19
|
+
for (const font of fonts) {
|
|
20
|
+
await renderer.registerFont({
|
|
21
|
+
name: font.name,
|
|
22
|
+
data: font.data,
|
|
23
|
+
...font.weight !== undefined && { weight: font.weight },
|
|
24
|
+
...font.style !== undefined && { style: font.style }
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return renderer;
|
|
28
|
+
}
|
|
29
|
+
function getRenderer(fonts) {
|
|
30
|
+
if (!fonts?.length) {
|
|
31
|
+
defaultRenderer.current ??= createRenderer([]);
|
|
32
|
+
defaultRenderer.current.catch(() => {
|
|
33
|
+
defaultRenderer.current = undefined;
|
|
34
|
+
});
|
|
35
|
+
return defaultRenderer.current;
|
|
36
|
+
}
|
|
37
|
+
let renderer = fontRenderers.get(fonts);
|
|
38
|
+
if (!renderer) {
|
|
39
|
+
renderer = createRenderer(fonts);
|
|
40
|
+
renderer.catch(() => fontRenderers.delete(fonts));
|
|
41
|
+
fontRenderers.set(fonts, renderer);
|
|
42
|
+
}
|
|
43
|
+
return renderer;
|
|
44
|
+
}
|
|
45
|
+
var number = (value) => typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
46
|
+
function toNode(node, text = true) {
|
|
47
|
+
if ("text" in node) {
|
|
48
|
+
return text ? { type: "text", text: node.text } : { type: "container", children: [] };
|
|
49
|
+
}
|
|
50
|
+
const { style, tw, src, width, height } = node.props;
|
|
51
|
+
const common = {
|
|
52
|
+
...typeof tw === "string" && { tw }
|
|
53
|
+
};
|
|
54
|
+
if (node.type === "img") {
|
|
55
|
+
if (typeof src !== "string")
|
|
56
|
+
throw new Error("<img> needs a string `src` (URL or data: URI)");
|
|
57
|
+
return {
|
|
58
|
+
type: "image",
|
|
59
|
+
src,
|
|
60
|
+
...number(width) !== undefined && { width: number(width) },
|
|
61
|
+
...number(height) !== undefined && { height: number(height) },
|
|
62
|
+
...typeof style === "object" && style !== null && { style },
|
|
63
|
+
...common
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
type: "container",
|
|
68
|
+
style: { display: "flex", ...typeof style === "object" ? style : {} },
|
|
69
|
+
children: node.children.filter((child) => text || !("text" in child)).map((child) => toNode(child, text)),
|
|
70
|
+
...common
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
var reconciler;
|
|
74
|
+
var flushing = 0;
|
|
75
|
+
function flushAll(renderer) {
|
|
76
|
+
flushing++;
|
|
77
|
+
try {
|
|
78
|
+
renderer.flushSyncWork();
|
|
79
|
+
for (let i = 0;i < 20 && renderer.flushPassiveEffects(); i++)
|
|
80
|
+
renderer.flushSyncWork();
|
|
81
|
+
} finally {
|
|
82
|
+
flushing--;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function loadReconciler() {
|
|
86
|
+
reconciler ??= (async () => {
|
|
87
|
+
let factoryModule;
|
|
88
|
+
let constants;
|
|
89
|
+
let React;
|
|
90
|
+
try {
|
|
91
|
+
[factoryModule, constants, React] = await Promise.all([
|
|
92
|
+
import("react-reconciler"),
|
|
93
|
+
import("react-reconciler/constants.js"),
|
|
94
|
+
import("react")
|
|
95
|
+
]);
|
|
96
|
+
} catch (cause) {
|
|
97
|
+
throw new Error("Rendering React elements requires `react` (>=19.3) to be installed", {
|
|
98
|
+
cause
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
let priority = constants.NoEventPriority;
|
|
102
|
+
const noop = () => {};
|
|
103
|
+
const hostConfig = {
|
|
104
|
+
supportsMutation: true,
|
|
105
|
+
supportsPersistence: false,
|
|
106
|
+
supportsHydration: false,
|
|
107
|
+
isPrimaryRenderer: true,
|
|
108
|
+
warnsIfNotActing: false,
|
|
109
|
+
noTimeout: -1,
|
|
110
|
+
scheduleTimeout: setTimeout,
|
|
111
|
+
cancelTimeout: clearTimeout,
|
|
112
|
+
supportsMicrotasks: true,
|
|
113
|
+
scheduleMicrotask: queueMicrotask,
|
|
114
|
+
createInstance: (type, props) => {
|
|
115
|
+
const { children: _children, ...rest } = props;
|
|
116
|
+
return { type, props: rest, children: [] };
|
|
117
|
+
},
|
|
118
|
+
createTextInstance: (text) => ({ text }),
|
|
119
|
+
shouldSetTextContent: () => false,
|
|
120
|
+
appendInitialChild: (parent, child) => parent.children.push(child),
|
|
121
|
+
appendChild: (parent, child) => parent.children.push(child),
|
|
122
|
+
appendChildToContainer: (container, child) => container.children.push(child),
|
|
123
|
+
insertBefore: (parent, child, before) => {
|
|
124
|
+
const children = parent.children;
|
|
125
|
+
const existing = children.indexOf(child);
|
|
126
|
+
if (existing !== -1)
|
|
127
|
+
children.splice(existing, 1);
|
|
128
|
+
children.splice(children.indexOf(before), 0, child);
|
|
129
|
+
},
|
|
130
|
+
insertInContainerBefore: (container, child, before) => {
|
|
131
|
+
const children = container.children;
|
|
132
|
+
const existing = children.indexOf(child);
|
|
133
|
+
if (existing !== -1)
|
|
134
|
+
children.splice(existing, 1);
|
|
135
|
+
children.splice(children.indexOf(before), 0, child);
|
|
136
|
+
},
|
|
137
|
+
removeChild: (parent, child) => {
|
|
138
|
+
parent.children.splice(parent.children.indexOf(child), 1);
|
|
139
|
+
},
|
|
140
|
+
removeChildFromContainer: (container, child) => {
|
|
141
|
+
container.children.splice(container.children.indexOf(child), 1);
|
|
142
|
+
},
|
|
143
|
+
clearContainer: (container) => {
|
|
144
|
+
container.children = [];
|
|
145
|
+
},
|
|
146
|
+
commitUpdate: (instance, _type, _prev, next) => {
|
|
147
|
+
const { children: _children, ...rest } = next;
|
|
148
|
+
instance.props = rest;
|
|
149
|
+
},
|
|
150
|
+
commitTextUpdate: (text, _old, next) => {
|
|
151
|
+
text.text = next;
|
|
152
|
+
},
|
|
153
|
+
finalizeInitialChildren: () => false,
|
|
154
|
+
getPublicInstance: (instance) => instance,
|
|
155
|
+
getRootHostContext: () => ({}),
|
|
156
|
+
getChildHostContext: (parent) => parent,
|
|
157
|
+
prepareForCommit: () => null,
|
|
158
|
+
resetAfterCommit: (container) => container.onCommit(),
|
|
159
|
+
preparePortalMount: noop,
|
|
160
|
+
detachDeletedInstance: noop,
|
|
161
|
+
hideInstance: noop,
|
|
162
|
+
unhideInstance: noop,
|
|
163
|
+
hideTextInstance: noop,
|
|
164
|
+
unhideTextInstance: noop,
|
|
165
|
+
getInstanceFromNode: () => null,
|
|
166
|
+
beforeActiveInstanceBlur: noop,
|
|
167
|
+
afterActiveInstanceBlur: noop,
|
|
168
|
+
prepareScopeUpdate: noop,
|
|
169
|
+
getInstanceFromScope: () => null,
|
|
170
|
+
bindToConsole: (method, args) => console[method]?.bind(console, ...args) ?? noop,
|
|
171
|
+
extraDevToolsConfig: null,
|
|
172
|
+
rendererVersion: "0.0.0",
|
|
173
|
+
rendererPackageName: "sdeck",
|
|
174
|
+
getCurrentUpdatePriority: () => priority,
|
|
175
|
+
setCurrentUpdatePriority: (next) => {
|
|
176
|
+
priority = next;
|
|
177
|
+
},
|
|
178
|
+
resolveUpdatePriority: () => priority !== constants.NoEventPriority ? priority : flushing > 0 ? constants.DiscreteEventPriority : constants.DefaultEventPriority,
|
|
179
|
+
resolveEventType: () => null,
|
|
180
|
+
resolveEventTimeStamp: () => -1.1,
|
|
181
|
+
shouldAttemptEagerTransition: () => false,
|
|
182
|
+
trackSchedulerEvent: noop,
|
|
183
|
+
requestPostPaintCallback: noop,
|
|
184
|
+
maySuspendCommit: () => false,
|
|
185
|
+
preloadInstance: () => true,
|
|
186
|
+
startSuspendingCommit: noop,
|
|
187
|
+
suspendInstance: noop,
|
|
188
|
+
waitForCommitToBeReady: () => null,
|
|
189
|
+
getSuspendedCommitReason: () => null,
|
|
190
|
+
NotPendingTransition: null,
|
|
191
|
+
HostTransitionContext: React.createContext(null),
|
|
192
|
+
resetFormInstance: noop
|
|
193
|
+
};
|
|
194
|
+
const factory = factoryModule.default ?? factoryModule;
|
|
195
|
+
return factory(hostConfig);
|
|
196
|
+
})();
|
|
197
|
+
reconciler.catch(() => {
|
|
198
|
+
reconciler = undefined;
|
|
199
|
+
});
|
|
200
|
+
return reconciler;
|
|
201
|
+
}
|
|
202
|
+
function noopIndicator() {}
|
|
203
|
+
async function createReactRoot(size, onFrame, options = {}) {
|
|
204
|
+
const [takumi, renderer] = await Promise.all([getRenderer(options.fonts), loadReconciler()]);
|
|
205
|
+
let dirty = false;
|
|
206
|
+
let drawing;
|
|
207
|
+
let lastTree;
|
|
208
|
+
let unmounted = false;
|
|
209
|
+
let renderError;
|
|
210
|
+
let initial = true;
|
|
211
|
+
const fail = (error) => {
|
|
212
|
+
if (initial)
|
|
213
|
+
renderError ??= { error };
|
|
214
|
+
else
|
|
215
|
+
(options.onError ?? ((e) => console.error("sdeck: React render failed:", e)))(error);
|
|
216
|
+
};
|
|
217
|
+
async function draw() {
|
|
218
|
+
await Promise.resolve();
|
|
219
|
+
while (dirty && !unmounted) {
|
|
220
|
+
dirty = false;
|
|
221
|
+
try {
|
|
222
|
+
const [root] = container.children;
|
|
223
|
+
if (!root)
|
|
224
|
+
continue;
|
|
225
|
+
const tree = toNode(root, options.text !== false);
|
|
226
|
+
const key = JSON.stringify(tree);
|
|
227
|
+
if (key === lastTree)
|
|
228
|
+
continue;
|
|
229
|
+
lastTree = key;
|
|
230
|
+
const pixels = await takumi.render(tree, {
|
|
231
|
+
width: size.width,
|
|
232
|
+
height: size.height,
|
|
233
|
+
format: "raw",
|
|
234
|
+
...options.scale !== undefined && { devicePixelRatio: options.scale }
|
|
235
|
+
});
|
|
236
|
+
await onFrame({
|
|
237
|
+
data: new Uint8Array(pixels.buffer, pixels.byteOffset, pixels.byteLength),
|
|
238
|
+
width: size.width,
|
|
239
|
+
height: size.height,
|
|
240
|
+
channels: 4
|
|
241
|
+
});
|
|
242
|
+
} catch (error) {
|
|
243
|
+
lastTree = undefined;
|
|
244
|
+
fail(error);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
drawing = undefined;
|
|
248
|
+
}
|
|
249
|
+
const container = {
|
|
250
|
+
children: [],
|
|
251
|
+
onCommit() {
|
|
252
|
+
dirty = true;
|
|
253
|
+
drawing ??= draw();
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
const onError = (error) => fail(error);
|
|
257
|
+
const onCaughtError = () => {};
|
|
258
|
+
const root = renderer.createContainer(container, 1, null, false, null, "", onError, onCaughtError, onError, noopIndicator, null);
|
|
259
|
+
const flush = () => flushAll(renderer);
|
|
260
|
+
const handle = {
|
|
261
|
+
render(element) {
|
|
262
|
+
if (unmounted)
|
|
263
|
+
throw new Error("This React root was unmounted");
|
|
264
|
+
renderer.updateContainerSync(element, root, null, null);
|
|
265
|
+
flush();
|
|
266
|
+
},
|
|
267
|
+
async idle() {
|
|
268
|
+
while (drawing)
|
|
269
|
+
await drawing;
|
|
270
|
+
if (initial && renderError) {
|
|
271
|
+
const { error } = renderError;
|
|
272
|
+
renderError = undefined;
|
|
273
|
+
throw error;
|
|
274
|
+
}
|
|
275
|
+
initial = false;
|
|
276
|
+
},
|
|
277
|
+
async unmount() {
|
|
278
|
+
if (unmounted)
|
|
279
|
+
return;
|
|
280
|
+
renderer.updateContainerSync(null, root, null, null);
|
|
281
|
+
flush();
|
|
282
|
+
unmounted = true;
|
|
283
|
+
await drawing;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
return handle;
|
|
287
|
+
}
|
|
288
|
+
async function renderReact(element, size, fonts, scale) {
|
|
289
|
+
let frame;
|
|
290
|
+
const root = await createReactRoot(size, (next) => {
|
|
291
|
+
frame = next;
|
|
292
|
+
}, { ...fonts && { fonts }, ...scale !== undefined && { scale } });
|
|
293
|
+
try {
|
|
294
|
+
root.render(element);
|
|
295
|
+
await root.idle();
|
|
296
|
+
} finally {
|
|
297
|
+
await root.unmount();
|
|
298
|
+
}
|
|
299
|
+
if (!frame)
|
|
300
|
+
throw new Error("React element rendered nothing");
|
|
301
|
+
return frame;
|
|
302
|
+
}
|
|
303
|
+
registerElementRuntime({
|
|
304
|
+
matches: isReactElement,
|
|
305
|
+
render: (element, size, options) => renderReact(element, size, options?.fonts),
|
|
306
|
+
createRoot: (size, onFrame, options) => createReactRoot(size, onFrame, options)
|
|
307
|
+
});
|
|
308
|
+
// ../app/src/app.tsx
|
|
309
|
+
import { Fragment, useSyncExternalStore } from "react";
|
|
310
|
+
|
|
311
|
+
// ../app/src/context.ts
|
|
312
|
+
import { createContext, useContext } from "react";
|
|
313
|
+
var DeckContext = createContext(undefined);
|
|
314
|
+
var SlotContext = createContext(undefined);
|
|
315
|
+
var NavigatorContext = createContext(undefined);
|
|
316
|
+
function useDeck() {
|
|
317
|
+
const deck = useContext(DeckContext);
|
|
318
|
+
if (!deck)
|
|
319
|
+
throw new Error("useDeck() must be used inside a component mounted by @sdeck/app");
|
|
320
|
+
return deck;
|
|
321
|
+
}
|
|
322
|
+
function useSlot() {
|
|
323
|
+
return useContext(SlotContext);
|
|
324
|
+
}
|
|
325
|
+
// ../app/src/hooks.ts
|
|
326
|
+
import { useContext as useContext2, useEffect, useRef, useState, useSyncExternalStore as useSyncExternalStore2 } from "react";
|
|
327
|
+
|
|
328
|
+
// ../app/src/swipe.ts
|
|
329
|
+
var SWIPE_WINDOW_MS = 400;
|
|
330
|
+
|
|
331
|
+
class SwipeTracker {
|
|
332
|
+
#handlers = new Set;
|
|
333
|
+
#stamps = new Map;
|
|
334
|
+
#completing = new Set;
|
|
335
|
+
#first;
|
|
336
|
+
constructor(deck) {
|
|
337
|
+
deck.on("sensorDown", (index) => this.#down(index));
|
|
338
|
+
}
|
|
339
|
+
get active() {
|
|
340
|
+
return this.#handlers.size > 0;
|
|
341
|
+
}
|
|
342
|
+
stamp(index) {
|
|
343
|
+
return this.#stamps.get(index) ?? 0;
|
|
344
|
+
}
|
|
345
|
+
completes(index) {
|
|
346
|
+
return this.#completing.has(index);
|
|
347
|
+
}
|
|
348
|
+
listen(handler) {
|
|
349
|
+
this.#handlers.add(handler);
|
|
350
|
+
return () => void this.#handlers.delete(handler);
|
|
351
|
+
}
|
|
352
|
+
#down(index) {
|
|
353
|
+
this.#completing.delete(index);
|
|
354
|
+
const now = performance.now();
|
|
355
|
+
const first = this.#first;
|
|
356
|
+
if (!this.active || !first || first.index === index || now - first.at > SWIPE_WINDOW_MS) {
|
|
357
|
+
this.#first = { index, at: now };
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
this.#first = undefined;
|
|
361
|
+
this.#completing.add(index);
|
|
362
|
+
for (const sensor of [first.index, index])
|
|
363
|
+
this.#stamps.set(sensor, this.stamp(sensor) + 1);
|
|
364
|
+
const direction = index > first.index ? "right" : "left";
|
|
365
|
+
for (const handler of [...this.#handlers])
|
|
366
|
+
handler(direction);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
var trackers = new WeakMap;
|
|
370
|
+
function swipeTracker(deck) {
|
|
371
|
+
let tracker = trackers.get(deck);
|
|
372
|
+
if (!tracker) {
|
|
373
|
+
tracker = new SwipeTracker(deck);
|
|
374
|
+
trackers.set(deck, tracker);
|
|
375
|
+
}
|
|
376
|
+
return tracker;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// ../app/src/hooks.ts
|
|
380
|
+
function useLatest(value) {
|
|
381
|
+
const ref = useRef(value);
|
|
382
|
+
useEffect(() => {
|
|
383
|
+
ref.current = value;
|
|
384
|
+
});
|
|
385
|
+
return ref;
|
|
386
|
+
}
|
|
387
|
+
function useTarget(kind, explicit) {
|
|
388
|
+
const slot = useSlot();
|
|
389
|
+
if (explicit !== undefined)
|
|
390
|
+
return explicit;
|
|
391
|
+
return slot?.kind === kind ? slot.index : undefined;
|
|
392
|
+
}
|
|
393
|
+
function usePressed(kind, explicit) {
|
|
394
|
+
const deck = useDeck();
|
|
395
|
+
const target = useTarget(kind, explicit);
|
|
396
|
+
const [pressed, setPressed] = useState(false);
|
|
397
|
+
if (target === undefined) {
|
|
398
|
+
throw new Error(`use${kind === "key" ? "Key" : "Sensor"}Pressed() needs an index outside a ${kind} slot`);
|
|
399
|
+
}
|
|
400
|
+
useEffect(() => {
|
|
401
|
+
setPressed(false);
|
|
402
|
+
const offDown = deck.on(`${kind}Down`, (i) => i === target && setPressed(true));
|
|
403
|
+
const offUp = deck.on(`${kind}Up`, (i) => i === target && setPressed(false));
|
|
404
|
+
return () => {
|
|
405
|
+
offDown();
|
|
406
|
+
offUp();
|
|
407
|
+
};
|
|
408
|
+
}, [deck, kind, target]);
|
|
409
|
+
return pressed;
|
|
410
|
+
}
|
|
411
|
+
function useRepeat(kind, handler, explicit, options) {
|
|
412
|
+
const deck = useDeck();
|
|
413
|
+
const target = useTarget(kind, explicit);
|
|
414
|
+
const latest = useLatest({ handler, ...options });
|
|
415
|
+
useEffect(() => {
|
|
416
|
+
const swipes = kind === "sensor" ? swipeTracker(deck) : undefined;
|
|
417
|
+
let first;
|
|
418
|
+
let delay;
|
|
419
|
+
let repeat;
|
|
420
|
+
const stop = () => {
|
|
421
|
+
clearTimeout(delay);
|
|
422
|
+
clearInterval(repeat);
|
|
423
|
+
};
|
|
424
|
+
const offDown = deck.on(`${kind}Down`, (index) => {
|
|
425
|
+
if (target !== undefined && index !== target)
|
|
426
|
+
return;
|
|
427
|
+
if (swipes?.completes(index))
|
|
428
|
+
return;
|
|
429
|
+
stop();
|
|
430
|
+
clearTimeout(first);
|
|
431
|
+
const stamp = swipes?.stamp(index);
|
|
432
|
+
const swiped = () => swipes !== undefined && swipes.stamp(index) !== stamp;
|
|
433
|
+
const run = () => {
|
|
434
|
+
if (!swiped())
|
|
435
|
+
latest.current.handler(index);
|
|
436
|
+
};
|
|
437
|
+
if (swipes?.active)
|
|
438
|
+
first = setTimeout(run, SWIPE_WINDOW_MS);
|
|
439
|
+
else
|
|
440
|
+
run();
|
|
441
|
+
const wait = latest.current.delayMs ?? 400;
|
|
442
|
+
delay = setTimeout(() => {
|
|
443
|
+
repeat = setInterval(run, latest.current.intervalMs ?? 100);
|
|
444
|
+
}, swipes?.active ? Math.max(wait, SWIPE_WINDOW_MS) : wait);
|
|
445
|
+
});
|
|
446
|
+
const offUp = deck.on(`${kind}Up`, (index) => {
|
|
447
|
+
if (target === undefined || index === target)
|
|
448
|
+
stop();
|
|
449
|
+
});
|
|
450
|
+
return () => {
|
|
451
|
+
offDown();
|
|
452
|
+
offUp();
|
|
453
|
+
stop();
|
|
454
|
+
clearTimeout(first);
|
|
455
|
+
};
|
|
456
|
+
}, [deck, kind, target, latest]);
|
|
457
|
+
}
|
|
458
|
+
function useKeyRepeat(handler, options = {}) {
|
|
459
|
+
useRepeat("key", handler, options.key, options);
|
|
460
|
+
}
|
|
461
|
+
function useSensorRepeat(handler, options = {}) {
|
|
462
|
+
useRepeat("sensor", handler, options.sensor, options);
|
|
463
|
+
}
|
|
464
|
+
function useSensorPressed(sensor) {
|
|
465
|
+
return usePressed("sensor", sensor);
|
|
466
|
+
}
|
|
467
|
+
// ../engine/src/state.ts
|
|
468
|
+
function sameValue(a, b) {
|
|
469
|
+
if (Object.is(a, b))
|
|
470
|
+
return true;
|
|
471
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null)
|
|
472
|
+
return false;
|
|
473
|
+
if (Array.isArray(a) !== Array.isArray(b))
|
|
474
|
+
return false;
|
|
475
|
+
const ka = Object.keys(a);
|
|
476
|
+
const kb = Object.keys(b);
|
|
477
|
+
if (ka.length !== kb.length)
|
|
478
|
+
return false;
|
|
479
|
+
return ka.every((k) => Object.hasOwn(b, k) && sameValue(a[k], b[k]));
|
|
480
|
+
}
|
|
481
|
+
// ../schema/src/layers.ts
|
|
482
|
+
function iconLayer(id, icon, options = {}) {
|
|
483
|
+
const { color, size = 52, position = "middle", visible, row, frame } = options;
|
|
484
|
+
const src = /^[a-z0-9]+\/[A-Za-z0-9-]+$/.test(icon) ? `icon:${icon}` : icon;
|
|
485
|
+
return {
|
|
486
|
+
id,
|
|
487
|
+
type: "image",
|
|
488
|
+
src,
|
|
489
|
+
size,
|
|
490
|
+
position,
|
|
491
|
+
...color && { color },
|
|
492
|
+
...visible && { visible },
|
|
493
|
+
...row && { row },
|
|
494
|
+
...frame && { frame }
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
function textLayer(id, text, options = {}) {
|
|
498
|
+
const { color = "#ffffff", size = 14, weight = 700, position = "middle" } = options;
|
|
499
|
+
const { visible, row, frame } = options;
|
|
500
|
+
return {
|
|
501
|
+
id,
|
|
502
|
+
type: "text",
|
|
503
|
+
text,
|
|
504
|
+
color,
|
|
505
|
+
size,
|
|
506
|
+
weight,
|
|
507
|
+
position,
|
|
508
|
+
...visible && { visible },
|
|
509
|
+
...row && { row },
|
|
510
|
+
...frame && { frame }
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
// ../plugin/src/bridge.ts
|
|
514
|
+
import {
|
|
515
|
+
createContext as createContext2,
|
|
516
|
+
useCallback,
|
|
517
|
+
useContext as useContext3,
|
|
518
|
+
useEffect as useEffect2,
|
|
519
|
+
useMemo,
|
|
520
|
+
useRef as useRef2,
|
|
521
|
+
useState as useState2,
|
|
522
|
+
useSyncExternalStore as useSyncExternalStore3
|
|
523
|
+
} from "react";
|
|
524
|
+
var HostBridgeContext = createContext2(undefined);
|
|
525
|
+
function useBridge() {
|
|
526
|
+
const bridge = useContext3(HostBridgeContext);
|
|
527
|
+
if (!bridge)
|
|
528
|
+
throw new Error("sdeck live-state hooks must be used inside an action drawn by the host");
|
|
529
|
+
return bridge;
|
|
530
|
+
}
|
|
531
|
+
function useWatched(paths, read) {
|
|
532
|
+
const bridge = useBridge();
|
|
533
|
+
const key = paths.join(`
|
|
534
|
+
`);
|
|
535
|
+
const subscribe = useCallback((listener) => bridge.watch(paths, listener), [bridge, key]);
|
|
536
|
+
const last = useRef2(undefined);
|
|
537
|
+
const snapshot = () => {
|
|
538
|
+
const value = read();
|
|
539
|
+
if (last.current && sameValue(last.current.value, value))
|
|
540
|
+
return last.current.value;
|
|
541
|
+
last.current = { value };
|
|
542
|
+
return value;
|
|
543
|
+
};
|
|
544
|
+
return useSyncExternalStore3(subscribe, snapshot);
|
|
545
|
+
}
|
|
546
|
+
function useHostState(key) {
|
|
547
|
+
const bridge = useBridge();
|
|
548
|
+
const paths = useMemo(() => [key], [key]);
|
|
549
|
+
return useWatched(paths, () => {
|
|
550
|
+
const dot = key.indexOf(".");
|
|
551
|
+
const ns = bridge.scope()[key.slice(0, dot)];
|
|
552
|
+
return dot > 0 ? ns?.[key.slice(dot + 1)] : undefined;
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
function useImage(src, color) {
|
|
556
|
+
const bridge = useBridge();
|
|
557
|
+
return useSyncExternalStore3(bridge.watchImages, () => src ? bridge.image(src, color) : undefined);
|
|
558
|
+
}
|
|
559
|
+
// ../plugin/src/settings.ts
|
|
560
|
+
function checkSchema(schema, sources = {}) {
|
|
561
|
+
const problems = [];
|
|
562
|
+
const types = new Set([
|
|
563
|
+
"text",
|
|
564
|
+
"number",
|
|
565
|
+
"boolean",
|
|
566
|
+
"color",
|
|
567
|
+
"select",
|
|
568
|
+
"page",
|
|
569
|
+
"image",
|
|
570
|
+
"secret"
|
|
571
|
+
]);
|
|
572
|
+
for (const [key, field] of Object.entries(schema)) {
|
|
573
|
+
if (!field || typeof field !== "object" || !types.has(field.type)) {
|
|
574
|
+
problems.push(`setting "${key}" has an unknown type`);
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
if (typeof field.label !== "string")
|
|
578
|
+
problems.push(`setting "${key}" needs a label`);
|
|
579
|
+
const source = "source" in field ? field.source : undefined;
|
|
580
|
+
if (source !== undefined && !(source in sources)) {
|
|
581
|
+
problems.push(`setting "${key}" uses unknown option source "${source}"`);
|
|
582
|
+
}
|
|
583
|
+
if (field.type === "select" && (!Array.isArray(field.options) || field.options.length === 0 && source === undefined)) {
|
|
584
|
+
problems.push(`setting "${key}" needs at least one option or a source`);
|
|
585
|
+
}
|
|
586
|
+
for (const other of "dependsOn" in field && field.dependsOn || []) {
|
|
587
|
+
if (!(other in schema))
|
|
588
|
+
problems.push(`setting "${key}" depends on unknown "${other}"`);
|
|
589
|
+
}
|
|
590
|
+
if (field.visibleIf !== undefined) {
|
|
591
|
+
for (const other of Object.keys(field.visibleIf)) {
|
|
592
|
+
if (!(other in schema))
|
|
593
|
+
problems.push(`setting "${key}" depends on unknown "${other}"`);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
return problems;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// ../plugin/src/plugin.ts
|
|
601
|
+
var PERMISSIONS = [
|
|
602
|
+
"shell",
|
|
603
|
+
"process",
|
|
604
|
+
"filesystem",
|
|
605
|
+
"network",
|
|
606
|
+
"input",
|
|
607
|
+
"audio",
|
|
608
|
+
"clipboard"
|
|
609
|
+
];
|
|
610
|
+
var ID = /^(@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/;
|
|
611
|
+
var NAMESPACE = /^[a-z][\w-]{0,31}$/;
|
|
612
|
+
var LOCAL_ID = /^[\w.-]+$/;
|
|
613
|
+
function defineAction(def) {
|
|
614
|
+
return {
|
|
615
|
+
...def,
|
|
616
|
+
slots: def.slots ?? ["key"],
|
|
617
|
+
settings: def.settings ?? {}
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
function definePlugin(def) {
|
|
621
|
+
const problems = checkPlugin(def);
|
|
622
|
+
if (problems.length > 0) {
|
|
623
|
+
throw new TypeError(`invalid plugin "${def?.id}": ${problems.join("; ")}`);
|
|
624
|
+
}
|
|
625
|
+
return def;
|
|
626
|
+
}
|
|
627
|
+
function checkLocalIds(kind, items, problems) {
|
|
628
|
+
const seen = new Set;
|
|
629
|
+
for (const item of items) {
|
|
630
|
+
const name = typeof item?.id === "string" ? item.id : "?";
|
|
631
|
+
if (typeof item?.id !== "string" || !LOCAL_ID.test(item.id)) {
|
|
632
|
+
problems.push(`${kind} "${name}": id must be letters, digits, ".", "-" or "_"`);
|
|
633
|
+
} else if (seen.has(item.id)) {
|
|
634
|
+
problems.push(`${kind} "${name}" is defined twice`);
|
|
635
|
+
}
|
|
636
|
+
seen.add(name);
|
|
637
|
+
}
|
|
638
|
+
return seen;
|
|
639
|
+
}
|
|
640
|
+
function checkPlugin(value) {
|
|
641
|
+
if (!value || typeof value !== "object")
|
|
642
|
+
return ["not an object"];
|
|
643
|
+
const def = value;
|
|
644
|
+
const problems = [];
|
|
645
|
+
if (typeof def.id !== "string" || !ID.test(def.id)) {
|
|
646
|
+
problems.push("id must be a package-style name (lowercase, e.g. `my-plugin` or `@me/plugin`)");
|
|
647
|
+
}
|
|
648
|
+
if (typeof def.name !== "string" || def.name === "")
|
|
649
|
+
problems.push("name is required");
|
|
650
|
+
if (typeof def.version !== "string" || def.version === "")
|
|
651
|
+
problems.push("version is required");
|
|
652
|
+
if (def.namespace !== undefined && !NAMESPACE.test(def.namespace)) {
|
|
653
|
+
problems.push("namespace must be lowercase letters, digits, - or _ (starting with a letter)");
|
|
654
|
+
}
|
|
655
|
+
if (def.activate !== undefined && typeof def.activate !== "function") {
|
|
656
|
+
problems.push("activate must be a function");
|
|
657
|
+
}
|
|
658
|
+
if (def.permissions !== undefined) {
|
|
659
|
+
if (!Array.isArray(def.permissions))
|
|
660
|
+
problems.push("permissions must be an array");
|
|
661
|
+
else
|
|
662
|
+
for (const p of def.permissions)
|
|
663
|
+
if (!PERMISSIONS.includes(p))
|
|
664
|
+
problems.push(`unknown permission "${String(p)}"`);
|
|
665
|
+
}
|
|
666
|
+
if (def.settings !== undefined) {
|
|
667
|
+
for (const p of checkSchema(def.settings, def.optionSources ?? {})) {
|
|
668
|
+
problems.push(`plugin ${p}`);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
if (def.commands !== undefined) {
|
|
672
|
+
if (!Array.isArray(def.commands))
|
|
673
|
+
problems.push("commands must be an array");
|
|
674
|
+
else {
|
|
675
|
+
checkLocalIds("command", def.commands, problems);
|
|
676
|
+
for (const c of def.commands) {
|
|
677
|
+
const name = typeof c?.id === "string" ? c.id : "?";
|
|
678
|
+
if (typeof c?.name !== "string")
|
|
679
|
+
problems.push(`command "${name}": name is required`);
|
|
680
|
+
if (typeof c?.run !== "function")
|
|
681
|
+
problems.push(`command "${name}": run is required`);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
if (def.onboarding !== undefined)
|
|
686
|
+
problems.push(...checkOnboarding(def));
|
|
687
|
+
const sources = def.optionSources ?? {};
|
|
688
|
+
if (typeof sources !== "object")
|
|
689
|
+
problems.push("optionSources must be an object");
|
|
690
|
+
else
|
|
691
|
+
for (const [name, fn] of Object.entries(sources))
|
|
692
|
+
if (typeof fn !== "function")
|
|
693
|
+
problems.push(`option source "${name}" must be a function`);
|
|
694
|
+
if (!Array.isArray(def.actions))
|
|
695
|
+
return [...problems, "actions must be an array"];
|
|
696
|
+
checkLocalIds("action", def.actions, problems);
|
|
697
|
+
for (const action of def.actions) {
|
|
698
|
+
const name = typeof action?.id === "string" ? action.id : "?";
|
|
699
|
+
if (typeof action?.name !== "string")
|
|
700
|
+
problems.push(`action "${name}": name is required`);
|
|
701
|
+
const hasComponent = typeof action?.component === "function" || typeof action?.component === "object";
|
|
702
|
+
const hasRun = typeof action?.run === "function";
|
|
703
|
+
if (!hasComponent && !hasRun)
|
|
704
|
+
problems.push(`action "${name}": needs run or component`);
|
|
705
|
+
if (action?.look !== undefined && typeof action.look !== "function") {
|
|
706
|
+
problems.push(`action "${name}": look must be a function`);
|
|
707
|
+
}
|
|
708
|
+
const slots = action?.slots ?? ["key"];
|
|
709
|
+
if (!Array.isArray(slots) || slots.length === 0 || slots.some((s) => !["key", "sensor", "window"].includes(s))) {
|
|
710
|
+
problems.push(`action "${name}": slots must list "key", "sensor" and/or "window"`);
|
|
711
|
+
}
|
|
712
|
+
for (const p of checkSchema(action?.settings ?? {}, sources)) {
|
|
713
|
+
problems.push(`action "${name}": ${p}`);
|
|
714
|
+
}
|
|
715
|
+
for (const [key, field] of Object.entries(action?.settings ?? {})) {
|
|
716
|
+
if (field?.type === "secret") {
|
|
717
|
+
problems.push(`action "${name}": setting "${key}": secrets belong in the plugin settings`);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
if (def.state !== undefined) {
|
|
722
|
+
if (!Array.isArray(def.state))
|
|
723
|
+
problems.push("state must be an array");
|
|
724
|
+
else {
|
|
725
|
+
checkLocalIds("state", def.state, problems);
|
|
726
|
+
for (const s of def.state) {
|
|
727
|
+
const name = typeof s?.id === "string" ? s.id : "?";
|
|
728
|
+
if (typeof s?.name !== "string")
|
|
729
|
+
problems.push(`state "${name}": name is required`);
|
|
730
|
+
if (!["number", "string", "boolean", "json"].includes(s?.type)) {
|
|
731
|
+
problems.push(`state "${name}": type must be number, string, boolean or json`);
|
|
732
|
+
}
|
|
733
|
+
if (s?.poll && (typeof s.poll.read !== "function" || !(s.poll.everyMs > 0))) {
|
|
734
|
+
problems.push(`state "${name}": poll needs read() and everyMs > 0`);
|
|
735
|
+
}
|
|
736
|
+
if (s?.start !== undefined && typeof s.start !== "function") {
|
|
737
|
+
problems.push(`state "${name}": start must be a function`);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
if (def.triggers !== undefined) {
|
|
743
|
+
if (!Array.isArray(def.triggers))
|
|
744
|
+
problems.push("triggers must be an array");
|
|
745
|
+
else {
|
|
746
|
+
checkLocalIds("trigger", def.triggers, problems);
|
|
747
|
+
for (const t of def.triggers) {
|
|
748
|
+
const name = typeof t?.id === "string" ? t.id : "?";
|
|
749
|
+
if (typeof t?.name !== "string")
|
|
750
|
+
problems.push(`trigger "${name}": name is required`);
|
|
751
|
+
if (typeof t?.subscribe !== "function") {
|
|
752
|
+
problems.push(`trigger "${name}": subscribe is required`);
|
|
753
|
+
}
|
|
754
|
+
for (const p of checkSchema(t?.settings ?? {}, sources)) {
|
|
755
|
+
problems.push(`trigger "${name}": ${p}`);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return problems;
|
|
761
|
+
}
|
|
762
|
+
function checkOnboarding(def) {
|
|
763
|
+
const onboarding = def.onboarding;
|
|
764
|
+
if (!Array.isArray(onboarding?.steps))
|
|
765
|
+
return ["onboarding: steps must be an array"];
|
|
766
|
+
const problems = [];
|
|
767
|
+
checkLocalIds("onboarding step", onboarding.steps, problems);
|
|
768
|
+
const settings = def.settings ?? {};
|
|
769
|
+
const commands = new Set((def.commands ?? []).map((c) => c.id));
|
|
770
|
+
for (const step of onboarding.steps) {
|
|
771
|
+
const name = typeof step?.id === "string" ? step.id : "?";
|
|
772
|
+
if (typeof step?.title !== "string")
|
|
773
|
+
problems.push(`onboarding step "${name}": title is required`);
|
|
774
|
+
for (const key of step?.settings ?? []) {
|
|
775
|
+
if (!(key in settings))
|
|
776
|
+
problems.push(`onboarding step "${name}": no setting "${key}"`);
|
|
777
|
+
}
|
|
778
|
+
if (step?.command !== undefined && !commands.has(step.command)) {
|
|
779
|
+
problems.push(`onboarding step "${name}": no command "${step.command}"`);
|
|
780
|
+
}
|
|
781
|
+
if (step?.done !== undefined && typeof step.done !== "function") {
|
|
782
|
+
problems.push(`onboarding step "${name}": done must be a function`);
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
const page = onboarding.starterPage;
|
|
786
|
+
if (page) {
|
|
787
|
+
const actions = new Set((def.actions ?? []).map((a) => a.id));
|
|
788
|
+
const used = [
|
|
789
|
+
...Object.values(page.keys ?? {}),
|
|
790
|
+
...Object.values(page.sensors ?? {}),
|
|
791
|
+
...page.window ? [page.window] : []
|
|
792
|
+
];
|
|
793
|
+
for (const { action } of used) {
|
|
794
|
+
if (!actions.has(action))
|
|
795
|
+
problems.push(`onboarding starter page: no action "${action}"`);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
return problems;
|
|
799
|
+
}
|
|
800
|
+
// src/helper.ts
|
|
801
|
+
import { spawn } from "child_process";
|
|
802
|
+
import { existsSync } from "fs";
|
|
803
|
+
import { join } from "path";
|
|
804
|
+
|
|
805
|
+
class AudioUnavailable extends Error {
|
|
806
|
+
}
|
|
807
|
+
var BUILD_HINT = "Build the audio helper with `bun run build:audio`";
|
|
808
|
+
function findHelper(env = process.env, base = import.meta.dir, exists = existsSync) {
|
|
809
|
+
const name = process.platform === "win32" ? "sdeck-audio.exe" : "sdeck-audio";
|
|
810
|
+
return [
|
|
811
|
+
env.SDECK_AUDIO_BIN,
|
|
812
|
+
join(base, "..", "native", "target", "release", name),
|
|
813
|
+
join(base, "..", "bin", name),
|
|
814
|
+
join(base, "..", "native", "target", "debug", name)
|
|
815
|
+
].find((path) => Boolean(path) && exists(path));
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
class AudioHelper {
|
|
819
|
+
#options;
|
|
820
|
+
#listeners = new Set;
|
|
821
|
+
#pending = new Map;
|
|
822
|
+
#child;
|
|
823
|
+
#snapshot;
|
|
824
|
+
#users = 0;
|
|
825
|
+
#next = 1;
|
|
826
|
+
#linger;
|
|
827
|
+
#restart;
|
|
828
|
+
#delay = 1000;
|
|
829
|
+
constructor(options) {
|
|
830
|
+
this.#options = options;
|
|
831
|
+
}
|
|
832
|
+
get snapshot() {
|
|
833
|
+
return this.#snapshot;
|
|
834
|
+
}
|
|
835
|
+
get running() {
|
|
836
|
+
return this.#child !== undefined;
|
|
837
|
+
}
|
|
838
|
+
subscribe(listener) {
|
|
839
|
+
this.#listeners.add(listener);
|
|
840
|
+
const release = this.#acquire();
|
|
841
|
+
if (this.#snapshot)
|
|
842
|
+
listener(this.#snapshot);
|
|
843
|
+
return () => {
|
|
844
|
+
this.#listeners.delete(listener);
|
|
845
|
+
release();
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
async request(method, params) {
|
|
849
|
+
const release = this.#acquire();
|
|
850
|
+
try {
|
|
851
|
+
const child = this.#child ?? this.#start();
|
|
852
|
+
const id = this.#next++;
|
|
853
|
+
return await new Promise((resolve, reject) => {
|
|
854
|
+
const timer = setTimeout(() => {
|
|
855
|
+
this.#pending.delete(id);
|
|
856
|
+
reject(new Error("the audio helper did not answer"));
|
|
857
|
+
}, this.#options.timeoutMs ?? 5000);
|
|
858
|
+
this.#pending.set(id, { resolve, reject, timer });
|
|
859
|
+
child.stdin?.write(`${JSON.stringify({ id, method, params })}
|
|
860
|
+
`);
|
|
861
|
+
});
|
|
862
|
+
} finally {
|
|
863
|
+
release();
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
close() {
|
|
867
|
+
this.#users = 0;
|
|
868
|
+
this.#listeners.clear();
|
|
869
|
+
clearTimeout(this.#linger);
|
|
870
|
+
this.#stop();
|
|
871
|
+
}
|
|
872
|
+
#acquire() {
|
|
873
|
+
this.#users++;
|
|
874
|
+
clearTimeout(this.#linger);
|
|
875
|
+
if (!this.#child && !this.#restart) {
|
|
876
|
+
try {
|
|
877
|
+
this.#start();
|
|
878
|
+
} catch (error) {
|
|
879
|
+
this.#options.onProblem?.(error);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
let released = false;
|
|
883
|
+
return () => {
|
|
884
|
+
if (released)
|
|
885
|
+
return;
|
|
886
|
+
released = true;
|
|
887
|
+
this.#users--;
|
|
888
|
+
if (this.#users > 0)
|
|
889
|
+
return;
|
|
890
|
+
this.#linger = setTimeout(() => this.#stop(), this.#options.lingerMs ?? 3000);
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
#start() {
|
|
894
|
+
clearTimeout(this.#restart);
|
|
895
|
+
this.#restart = undefined;
|
|
896
|
+
const [file, ...args] = this.#options.command();
|
|
897
|
+
if (!file)
|
|
898
|
+
throw new AudioUnavailable(BUILD_HINT);
|
|
899
|
+
const child = spawn(file, args, { stdio: ["pipe", "pipe", "pipe"], windowsHide: true });
|
|
900
|
+
this.#child = child;
|
|
901
|
+
let buffer = "";
|
|
902
|
+
let errors = "";
|
|
903
|
+
child.stdout?.setEncoding("utf8");
|
|
904
|
+
child.stdout?.on("data", (chunk) => {
|
|
905
|
+
buffer += chunk;
|
|
906
|
+
const lines = buffer.split(`
|
|
907
|
+
`);
|
|
908
|
+
buffer = lines.pop() ?? "";
|
|
909
|
+
for (const line of lines)
|
|
910
|
+
this.#receive(line);
|
|
911
|
+
});
|
|
912
|
+
child.stderr?.setEncoding("utf8");
|
|
913
|
+
child.stderr?.on("data", (chunk) => {
|
|
914
|
+
errors = (errors + chunk).slice(-2000);
|
|
915
|
+
});
|
|
916
|
+
child.stdin?.on("error", () => {});
|
|
917
|
+
const ended = (reason) => {
|
|
918
|
+
if (this.#child !== child)
|
|
919
|
+
return;
|
|
920
|
+
this.#child = undefined;
|
|
921
|
+
this.#fail(new Error(reason));
|
|
922
|
+
this.#publish(undefined);
|
|
923
|
+
this.#options.onProblem?.(new AudioUnavailable(reason));
|
|
924
|
+
if (this.#users <= 0)
|
|
925
|
+
return;
|
|
926
|
+
this.#restart = setTimeout(() => {
|
|
927
|
+
this.#restart = undefined;
|
|
928
|
+
if (this.#users <= 0 || this.#child)
|
|
929
|
+
return;
|
|
930
|
+
try {
|
|
931
|
+
this.#start();
|
|
932
|
+
} catch (error) {
|
|
933
|
+
this.#options.onProblem?.(error);
|
|
934
|
+
}
|
|
935
|
+
}, this.#delay);
|
|
936
|
+
this.#delay = Math.min(this.#delay * 2, 30000);
|
|
937
|
+
};
|
|
938
|
+
child.once("error", (error) => ended(`the audio helper cannot run: ${error.message}`));
|
|
939
|
+
child.once("close", (code) => ended(errors.trim() || `the audio helper stopped (exit ${code})`));
|
|
940
|
+
return child;
|
|
941
|
+
}
|
|
942
|
+
#stop() {
|
|
943
|
+
clearTimeout(this.#restart);
|
|
944
|
+
this.#restart = undefined;
|
|
945
|
+
const child = this.#child;
|
|
946
|
+
this.#child = undefined;
|
|
947
|
+
this.#fail(new Error("the audio helper was stopped"));
|
|
948
|
+
this.#publish(undefined);
|
|
949
|
+
if (!child)
|
|
950
|
+
return;
|
|
951
|
+
child.stdin?.end();
|
|
952
|
+
const kill = setTimeout(() => child.kill(), 2000);
|
|
953
|
+
child.once("close", () => clearTimeout(kill));
|
|
954
|
+
}
|
|
955
|
+
#receive(line) {
|
|
956
|
+
let message;
|
|
957
|
+
try {
|
|
958
|
+
message = JSON.parse(line);
|
|
959
|
+
} catch {
|
|
960
|
+
return;
|
|
961
|
+
}
|
|
962
|
+
if (message.event === "snapshot") {
|
|
963
|
+
this.#delay = 1000;
|
|
964
|
+
this.#options.onProblem?.(undefined);
|
|
965
|
+
this.#publish(message.data);
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
const pending = this.#pending.get(message.id ?? -1);
|
|
969
|
+
if (!pending)
|
|
970
|
+
return;
|
|
971
|
+
this.#pending.delete(message.id);
|
|
972
|
+
clearTimeout(pending.timer);
|
|
973
|
+
if (message.error !== undefined)
|
|
974
|
+
pending.reject(new Error(message.error));
|
|
975
|
+
else
|
|
976
|
+
pending.resolve(message.result);
|
|
977
|
+
}
|
|
978
|
+
#publish(snapshot) {
|
|
979
|
+
if (this.#snapshot === undefined && snapshot === undefined)
|
|
980
|
+
return;
|
|
981
|
+
this.#snapshot = snapshot;
|
|
982
|
+
for (const listener of [...this.#listeners])
|
|
983
|
+
listener(snapshot);
|
|
984
|
+
}
|
|
985
|
+
#fail(error) {
|
|
986
|
+
for (const pending of this.#pending.values()) {
|
|
987
|
+
clearTimeout(pending.timer);
|
|
988
|
+
pending.reject(error);
|
|
989
|
+
}
|
|
990
|
+
this.#pending.clear();
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// src/index.tsx
|
|
995
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
996
|
+
function appKey(name) {
|
|
997
|
+
return name.trim().toLowerCase().replace(/\.exe$/, "");
|
|
998
|
+
}
|
|
999
|
+
function audioState(snapshot) {
|
|
1000
|
+
if (!snapshot) {
|
|
1001
|
+
return {
|
|
1002
|
+
master: undefined,
|
|
1003
|
+
mic: undefined,
|
|
1004
|
+
output: undefined,
|
|
1005
|
+
input: undefined,
|
|
1006
|
+
outputs: undefined,
|
|
1007
|
+
inputs: undefined,
|
|
1008
|
+
apps: undefined
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
const level = (d) => d && { volume: d.volume, muted: d.muted, name: d.name };
|
|
1012
|
+
const output = snapshot.outputs.find((d) => d.default);
|
|
1013
|
+
const input = snapshot.inputs.find((d) => d.default);
|
|
1014
|
+
return {
|
|
1015
|
+
master: level(output),
|
|
1016
|
+
mic: level(input),
|
|
1017
|
+
output: output?.name,
|
|
1018
|
+
input: input?.name,
|
|
1019
|
+
outputs: snapshot.outputs,
|
|
1020
|
+
inputs: snapshot.inputs,
|
|
1021
|
+
apps: Object.fromEntries(snapshot.apps.map((app) => [appKey(app.name), app]))
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
var context;
|
|
1025
|
+
var helper2 = new AudioHelper({
|
|
1026
|
+
command: () => {
|
|
1027
|
+
if (process.platform !== "win32") {
|
|
1028
|
+
throw new AudioUnavailable("Audio control is available on Windows only");
|
|
1029
|
+
}
|
|
1030
|
+
const path = String(context?.settings().helper || "") || findHelper();
|
|
1031
|
+
if (!path)
|
|
1032
|
+
throw new AudioUnavailable(`${BUILD_HINT}.`);
|
|
1033
|
+
return [path];
|
|
1034
|
+
},
|
|
1035
|
+
onProblem: (problem) => context?.status(problem?.message, "error")
|
|
1036
|
+
});
|
|
1037
|
+
var stateOf = (id, name, type, description) => ({
|
|
1038
|
+
id,
|
|
1039
|
+
name,
|
|
1040
|
+
type,
|
|
1041
|
+
description,
|
|
1042
|
+
start: (set) => helper2.subscribe((snapshot) => set(audioState(snapshot)[id]))
|
|
1043
|
+
});
|
|
1044
|
+
var useIcon = (icon, color) => useImage(`icon:${icon}`, color);
|
|
1045
|
+
var TARGETS = [
|
|
1046
|
+
{ value: "output", label: "Speakers (default output)" },
|
|
1047
|
+
{ value: "input", label: "Microphone (default input)" },
|
|
1048
|
+
{ value: "app", label: "One app" }
|
|
1049
|
+
];
|
|
1050
|
+
function targetOf(s) {
|
|
1051
|
+
if (s.target !== "app")
|
|
1052
|
+
return { kind: s.target };
|
|
1053
|
+
if (!s.app.trim())
|
|
1054
|
+
throw new Error("choose an app");
|
|
1055
|
+
return { kind: "app", app: s.app.trim() };
|
|
1056
|
+
}
|
|
1057
|
+
function targetPath(s) {
|
|
1058
|
+
if (s.target === "output")
|
|
1059
|
+
return "audio.master";
|
|
1060
|
+
if (s.target === "input")
|
|
1061
|
+
return "audio.mic";
|
|
1062
|
+
return `audio.apps[${JSON.stringify(appKey(s.app))}]`;
|
|
1063
|
+
}
|
|
1064
|
+
var targetName = (s) => s.target === "output" ? "Volume" : s.target === "input" ? "Mic" : appKey(s.app) || "App";
|
|
1065
|
+
var APP_ICONS = {
|
|
1066
|
+
spotify: "si/SiSpotify",
|
|
1067
|
+
discord: "si/SiDiscord",
|
|
1068
|
+
chrome: "si/SiGooglechrome",
|
|
1069
|
+
firefox: "si/SiFirefoxbrowser",
|
|
1070
|
+
msedge: "fa6/FaEdge",
|
|
1071
|
+
brave: "si/SiBrave",
|
|
1072
|
+
opera: "si/SiOpera",
|
|
1073
|
+
steam: "si/SiSteam",
|
|
1074
|
+
vlc: "si/SiVlcmediaplayer",
|
|
1075
|
+
obs64: "si/SiObsstudio",
|
|
1076
|
+
slack: "fa6/FaSlack",
|
|
1077
|
+
zoom: "si/SiZoom",
|
|
1078
|
+
system: "lucide/bell"
|
|
1079
|
+
};
|
|
1080
|
+
function targetIcons(s) {
|
|
1081
|
+
if (s.target === "output")
|
|
1082
|
+
return { on: "lucide/volume-2", off: "lucide/volume-x" };
|
|
1083
|
+
if (s.target === "input")
|
|
1084
|
+
return { on: "lucide/mic", off: "lucide/mic-off" };
|
|
1085
|
+
const brand = APP_ICONS[appKey(s.app)];
|
|
1086
|
+
return brand ? { on: brand, off: brand } : { on: "lucide/app-window", off: "lucide/volume-x" };
|
|
1087
|
+
}
|
|
1088
|
+
var targetFields = {
|
|
1089
|
+
target: { type: "select", label: "Control", default: "output", options: TARGETS },
|
|
1090
|
+
app: {
|
|
1091
|
+
type: "text",
|
|
1092
|
+
label: "App",
|
|
1093
|
+
source: "apps",
|
|
1094
|
+
placeholder: "spotify.exe",
|
|
1095
|
+
description: "The executable name, as it appears while the app plays sound.",
|
|
1096
|
+
visibleIf: { target: "app" }
|
|
1097
|
+
}
|
|
1098
|
+
};
|
|
1099
|
+
async function adjust(target, operation, step, level) {
|
|
1100
|
+
switch (operation) {
|
|
1101
|
+
case "up":
|
|
1102
|
+
return helper2.request("setVolume", { target, delta: step });
|
|
1103
|
+
case "down":
|
|
1104
|
+
return helper2.request("setVolume", { target, delta: -step });
|
|
1105
|
+
case "set":
|
|
1106
|
+
return helper2.request("setVolume", { target, volume: level });
|
|
1107
|
+
case "toggle":
|
|
1108
|
+
return helper2.request("setMute", { target });
|
|
1109
|
+
default:
|
|
1110
|
+
return helper2.request("setMute", { target, muted: operation === "mute" });
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
var OPERATION_NAMES = {
|
|
1114
|
+
up: "Volume up",
|
|
1115
|
+
down: "Volume down",
|
|
1116
|
+
set: "Set volume",
|
|
1117
|
+
mute: "Mute",
|
|
1118
|
+
unmute: "Unmute",
|
|
1119
|
+
toggle: "Toggle mute"
|
|
1120
|
+
};
|
|
1121
|
+
var volume = defineAction({
|
|
1122
|
+
id: "volume",
|
|
1123
|
+
name: "Volume",
|
|
1124
|
+
description: "Changes or mutes the speakers, the microphone or one app, and shows its level.",
|
|
1125
|
+
category: "Audio",
|
|
1126
|
+
icon: "volume-2",
|
|
1127
|
+
slots: ["key", "sensor"],
|
|
1128
|
+
settings: {
|
|
1129
|
+
...targetFields,
|
|
1130
|
+
operation: {
|
|
1131
|
+
type: "select",
|
|
1132
|
+
label: "When pressed",
|
|
1133
|
+
default: "toggle",
|
|
1134
|
+
options: Object.entries(OPERATION_NAMES).map(([value, label]) => ({ value, label }))
|
|
1135
|
+
},
|
|
1136
|
+
step: {
|
|
1137
|
+
type: "number",
|
|
1138
|
+
label: "Step (%)",
|
|
1139
|
+
default: 5,
|
|
1140
|
+
min: 1,
|
|
1141
|
+
max: 50,
|
|
1142
|
+
visibleIf: { operation: ["up", "down"] }
|
|
1143
|
+
},
|
|
1144
|
+
level: {
|
|
1145
|
+
type: "number",
|
|
1146
|
+
label: "Level (%)",
|
|
1147
|
+
default: 50,
|
|
1148
|
+
min: 0,
|
|
1149
|
+
max: 100,
|
|
1150
|
+
visibleIf: { operation: "set" }
|
|
1151
|
+
},
|
|
1152
|
+
background: { type: "color", label: "Background", default: "#172554" }
|
|
1153
|
+
},
|
|
1154
|
+
run: async ({ settings }) => {
|
|
1155
|
+
const target = targetOf(settings);
|
|
1156
|
+
const { before, after } = await adjust(target, settings.operation, settings.step, settings.level);
|
|
1157
|
+
return {
|
|
1158
|
+
result: after,
|
|
1159
|
+
label: `${OPERATION_NAMES[settings.operation]} (${targetName(settings)})`,
|
|
1160
|
+
undo: async () => {
|
|
1161
|
+
await helper2.request("setVolume", { target, volume: before.volume });
|
|
1162
|
+
await helper2.request("setMute", { target, muted: before.muted });
|
|
1163
|
+
}
|
|
1164
|
+
};
|
|
1165
|
+
},
|
|
1166
|
+
look: (s) => {
|
|
1167
|
+
const path = targetPath(s);
|
|
1168
|
+
const muted = `${path}.muted`;
|
|
1169
|
+
const icons = targetIcons(s);
|
|
1170
|
+
const step = s.operation === "up" ? "plus" : s.operation === "down" ? "minus" : "";
|
|
1171
|
+
return {
|
|
1172
|
+
background: s.background,
|
|
1173
|
+
layers: [
|
|
1174
|
+
iconLayer("icon", icons.on, {
|
|
1175
|
+
size: 42,
|
|
1176
|
+
position: "top",
|
|
1177
|
+
visible: `!${muted}`,
|
|
1178
|
+
row: "icon"
|
|
1179
|
+
}),
|
|
1180
|
+
iconLayer("muted", icons.off, {
|
|
1181
|
+
color: "#f87171",
|
|
1182
|
+
size: 42,
|
|
1183
|
+
position: "top",
|
|
1184
|
+
visible: muted,
|
|
1185
|
+
row: "icon"
|
|
1186
|
+
}),
|
|
1187
|
+
...step ? [iconLayer("step", `lucide/${step}`, { size: 22, position: "top", row: "icon" })] : [],
|
|
1188
|
+
textLayer("level", `{{${path}.volume == null ? "N/A" : ${path}.volume + "%"}}`, {
|
|
1189
|
+
size: 18,
|
|
1190
|
+
position: "bottom"
|
|
1191
|
+
}),
|
|
1192
|
+
{
|
|
1193
|
+
id: "bar",
|
|
1194
|
+
type: "bar",
|
|
1195
|
+
value: `${path}.volume ?? 0`,
|
|
1196
|
+
color: "#60a5fa",
|
|
1197
|
+
track: "#1e293b",
|
|
1198
|
+
position: "bottom",
|
|
1199
|
+
thickness: 6
|
|
1200
|
+
}
|
|
1201
|
+
]
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
});
|
|
1205
|
+
var slider = defineAction({
|
|
1206
|
+
id: "slider",
|
|
1207
|
+
name: "Volume slider",
|
|
1208
|
+
description: "A level bar on a sensor strip: touch it to step the volume (the left one lowers, the right one raises), hold to keep going.",
|
|
1209
|
+
category: "Audio",
|
|
1210
|
+
icon: "sliders-horizontal",
|
|
1211
|
+
slots: ["sensor", "window", "key"],
|
|
1212
|
+
settings: {
|
|
1213
|
+
...targetFields,
|
|
1214
|
+
direction: {
|
|
1215
|
+
type: "select",
|
|
1216
|
+
label: "Touching it",
|
|
1217
|
+
default: "auto",
|
|
1218
|
+
options: [
|
|
1219
|
+
{ value: "auto", label: "Left lowers, right raises" },
|
|
1220
|
+
{ value: "down", label: "Lowers" },
|
|
1221
|
+
{ value: "up", label: "Raises" }
|
|
1222
|
+
]
|
|
1223
|
+
},
|
|
1224
|
+
step: { type: "number", label: "Step (%)", default: 4, min: 1, max: 50 },
|
|
1225
|
+
color: { type: "color", label: "Bar colour", default: "#60a5fa" }
|
|
1226
|
+
},
|
|
1227
|
+
run: async ({ settings }) => {
|
|
1228
|
+
const target = targetOf(settings);
|
|
1229
|
+
const delta = settings.direction === "down" ? -settings.step : settings.step;
|
|
1230
|
+
const { before, after } = await helper2.request("setVolume", { target, delta });
|
|
1231
|
+
return {
|
|
1232
|
+
result: after,
|
|
1233
|
+
label: `${targetName(settings)} ${after.volume}%`,
|
|
1234
|
+
undo: () => helper2.request("setVolume", { target, volume: before.volume })
|
|
1235
|
+
};
|
|
1236
|
+
},
|
|
1237
|
+
component: ({ settings, slot, plugin }) => {
|
|
1238
|
+
const name = settings.target === "output" ? "master" : settings.target === "input" ? "mic" : "";
|
|
1239
|
+
const whole = useHostState(name ? `audio.${name}` : "audio.apps");
|
|
1240
|
+
const level = name ? whole : whole?.[appKey(settings.app)];
|
|
1241
|
+
const down = settings.direction === "down" || settings.direction === "auto" && slot.kind !== "window" && slot.index % 2 === 0;
|
|
1242
|
+
const step = () => {
|
|
1243
|
+
helper2.request("setVolume", {
|
|
1244
|
+
target: targetOf(settings),
|
|
1245
|
+
delta: down ? -settings.step : settings.step
|
|
1246
|
+
}).catch((error) => plugin.log("slider:", error.message));
|
|
1247
|
+
};
|
|
1248
|
+
const sensorPressed = useSensorPressed(slot.kind === "sensor" ? slot.index : 0);
|
|
1249
|
+
const index = slot.kind === "window" ? -1 : slot.index;
|
|
1250
|
+
useSensorRepeat((i) => slot.kind === "sensor" && i === index && step(), {
|
|
1251
|
+
sensor: slot.kind === "sensor" ? index : -1,
|
|
1252
|
+
intervalMs: 150
|
|
1253
|
+
});
|
|
1254
|
+
useKeyRepeat((i) => slot.kind === "key" && i === index && step(), {
|
|
1255
|
+
key: slot.kind === "key" ? index : -1,
|
|
1256
|
+
intervalMs: 150
|
|
1257
|
+
});
|
|
1258
|
+
const strip = slot.kind === "sensor";
|
|
1259
|
+
const text = !level ? "N/A" : `${level.volume}%`;
|
|
1260
|
+
const icons = targetIcons(settings);
|
|
1261
|
+
const size = strip ? 12 : 24;
|
|
1262
|
+
const icon = useIcon(level?.muted ? icons.off : icons.on, "#ffffff");
|
|
1263
|
+
const arrow = useIcon(down ? "lucide/minus" : "lucide/plus", "#ffffff");
|
|
1264
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1265
|
+
style: {
|
|
1266
|
+
display: "flex",
|
|
1267
|
+
position: "relative",
|
|
1268
|
+
width: "100%",
|
|
1269
|
+
height: "100%",
|
|
1270
|
+
background: strip && sensorPressed ? "#334155" : "#0f172a",
|
|
1271
|
+
alignItems: "center",
|
|
1272
|
+
justifyContent: "center"
|
|
1273
|
+
},
|
|
1274
|
+
children: [
|
|
1275
|
+
/* @__PURE__ */ jsx("div", {
|
|
1276
|
+
style: {
|
|
1277
|
+
display: "flex",
|
|
1278
|
+
position: "absolute",
|
|
1279
|
+
left: 0,
|
|
1280
|
+
top: 0,
|
|
1281
|
+
bottom: 0,
|
|
1282
|
+
width: `${level?.volume ?? 0}%`,
|
|
1283
|
+
background: level?.muted ? "#7f1d1d" : settings.color,
|
|
1284
|
+
opacity: 0.6
|
|
1285
|
+
}
|
|
1286
|
+
}),
|
|
1287
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1288
|
+
style: {
|
|
1289
|
+
display: "flex",
|
|
1290
|
+
flexDirection: "row",
|
|
1291
|
+
alignItems: "center",
|
|
1292
|
+
gap: strip ? 3 : 6,
|
|
1293
|
+
color: "#ffffff",
|
|
1294
|
+
fontWeight: 700,
|
|
1295
|
+
fontSize: 20
|
|
1296
|
+
},
|
|
1297
|
+
children: [
|
|
1298
|
+
strip && arrow ? /* @__PURE__ */ jsx("img", {
|
|
1299
|
+
src: arrow,
|
|
1300
|
+
width: 10,
|
|
1301
|
+
height: 10,
|
|
1302
|
+
alt: ""
|
|
1303
|
+
}) : null,
|
|
1304
|
+
icon ? /* @__PURE__ */ jsx("img", {
|
|
1305
|
+
src: icon,
|
|
1306
|
+
width: size,
|
|
1307
|
+
height: size,
|
|
1308
|
+
alt: ""
|
|
1309
|
+
}) : null,
|
|
1310
|
+
strip ? null : /* @__PURE__ */ jsx("div", {
|
|
1311
|
+
style: { display: "flex" },
|
|
1312
|
+
children: text
|
|
1313
|
+
})
|
|
1314
|
+
]
|
|
1315
|
+
})
|
|
1316
|
+
]
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
});
|
|
1320
|
+
var NEXT = "__next";
|
|
1321
|
+
function nextDevice(devices) {
|
|
1322
|
+
const at = devices.findIndex((d) => d.default);
|
|
1323
|
+
return devices[(at + 1) % devices.length];
|
|
1324
|
+
}
|
|
1325
|
+
var defaultDevice = defineAction({
|
|
1326
|
+
id: "default-device",
|
|
1327
|
+
name: "Switch audio device",
|
|
1328
|
+
description: "Makes another speaker or microphone the default, or steps through them.",
|
|
1329
|
+
category: "Audio",
|
|
1330
|
+
icon: "speaker",
|
|
1331
|
+
slots: ["key", "sensor"],
|
|
1332
|
+
settings: {
|
|
1333
|
+
flow: {
|
|
1334
|
+
type: "select",
|
|
1335
|
+
label: "Switch the",
|
|
1336
|
+
default: "output",
|
|
1337
|
+
options: [
|
|
1338
|
+
{ value: "output", label: "Output (speakers)" },
|
|
1339
|
+
{ value: "input", label: "Input (microphone)" }
|
|
1340
|
+
]
|
|
1341
|
+
},
|
|
1342
|
+
output: {
|
|
1343
|
+
type: "select",
|
|
1344
|
+
label: "To",
|
|
1345
|
+
default: NEXT,
|
|
1346
|
+
source: "outputs",
|
|
1347
|
+
options: [{ value: NEXT, label: "The next device" }],
|
|
1348
|
+
visibleIf: { flow: "output" }
|
|
1349
|
+
},
|
|
1350
|
+
input: {
|
|
1351
|
+
type: "select",
|
|
1352
|
+
label: "To",
|
|
1353
|
+
default: NEXT,
|
|
1354
|
+
source: "inputs",
|
|
1355
|
+
options: [{ value: NEXT, label: "The next device" }],
|
|
1356
|
+
visibleIf: { flow: "input" }
|
|
1357
|
+
},
|
|
1358
|
+
background: { type: "color", label: "Background", default: "#3b0764" }
|
|
1359
|
+
},
|
|
1360
|
+
run: async ({ settings }) => {
|
|
1361
|
+
const wanted = settings.flow === "output" ? settings.output : settings.input;
|
|
1362
|
+
const snapshot = await helper2.request("snapshot");
|
|
1363
|
+
const devices = settings.flow === "output" ? snapshot.outputs : snapshot.inputs;
|
|
1364
|
+
const device = wanted === NEXT ? nextDevice(devices) : devices.find((d) => d.id === wanted);
|
|
1365
|
+
if (!device)
|
|
1366
|
+
throw new Error("that audio device is not connected");
|
|
1367
|
+
const { previous } = await helper2.request("setDefault", { device: device.id });
|
|
1368
|
+
return {
|
|
1369
|
+
result: device.name,
|
|
1370
|
+
label: `Audio ${settings.flow}: ${device.name}`,
|
|
1371
|
+
...previous && {
|
|
1372
|
+
undo: () => helper2.request("setDefault", { device: previous })
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
},
|
|
1376
|
+
look: (s) => ({
|
|
1377
|
+
background: s.background,
|
|
1378
|
+
layers: [
|
|
1379
|
+
iconLayer("icon", s.flow === "output" ? "lucide/headphones" : "lucide/mic", {
|
|
1380
|
+
size: 44,
|
|
1381
|
+
position: "top",
|
|
1382
|
+
row: "icon"
|
|
1383
|
+
}),
|
|
1384
|
+
iconLayer("switch", "lucide/arrow-left-right", {
|
|
1385
|
+
color: "#c4b5fd",
|
|
1386
|
+
size: 22,
|
|
1387
|
+
position: "top",
|
|
1388
|
+
row: "icon"
|
|
1389
|
+
}),
|
|
1390
|
+
textLayer("device", `{{audio.${s.flow} ?? "N/A"}}`, { size: 11, position: "bottom" })
|
|
1391
|
+
]
|
|
1392
|
+
})
|
|
1393
|
+
});
|
|
1394
|
+
var deviceOptions = async (flow) => (await helper2.request("snapshot"))[flow].map((d) => ({ value: d.id, label: d.name }));
|
|
1395
|
+
var src_default = definePlugin({
|
|
1396
|
+
id: "@sdeck/plugin-audio",
|
|
1397
|
+
name: "Audio",
|
|
1398
|
+
version: "0.1.0",
|
|
1399
|
+
namespace: "audio",
|
|
1400
|
+
description: "Volume and mute for speakers, microphone and each app, and device switching.",
|
|
1401
|
+
author: "sdeck",
|
|
1402
|
+
permissions: ["audio", "process"],
|
|
1403
|
+
settings: {
|
|
1404
|
+
helper: {
|
|
1405
|
+
type: "text",
|
|
1406
|
+
label: "Audio helper",
|
|
1407
|
+
placeholder: "Found automatically",
|
|
1408
|
+
description: "Full path of sdeck-audio, if it is somewhere unusual."
|
|
1409
|
+
}
|
|
1410
|
+
},
|
|
1411
|
+
onboarding: {
|
|
1412
|
+
steps: [
|
|
1413
|
+
{
|
|
1414
|
+
id: "helper",
|
|
1415
|
+
title: "The audio helper",
|
|
1416
|
+
body: "sdeck reaches your speakers, microphone and apps through a small helper that comes with this plugin on Windows. Leave this empty unless it is somewhere else.",
|
|
1417
|
+
settings: ["helper"],
|
|
1418
|
+
done: (activated) => {
|
|
1419
|
+
const custom = String(activated.settings().helper ?? "").trim();
|
|
1420
|
+
return custom ? existsSync2(custom) : findHelper() !== undefined;
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
]
|
|
1424
|
+
},
|
|
1425
|
+
actions: [volume, slider, defaultDevice],
|
|
1426
|
+
optionSources: {
|
|
1427
|
+
outputs: () => deviceOptions("outputs"),
|
|
1428
|
+
inputs: () => deviceOptions("inputs"),
|
|
1429
|
+
apps: async () => (await helper2.request("snapshot")).apps.map((a) => ({ value: a.name, label: a.name }))
|
|
1430
|
+
},
|
|
1431
|
+
state: [
|
|
1432
|
+
stateOf("master", "Speakers", "json", "audio.master.volume, .muted, .name"),
|
|
1433
|
+
stateOf("mic", "Microphone", "json", "audio.mic.volume, .muted, .name"),
|
|
1434
|
+
stateOf("output", "Output device", "string", "The default output's name."),
|
|
1435
|
+
stateOf("input", "Input device", "string", "The default input's name."),
|
|
1436
|
+
stateOf("outputs", "Output devices", "json", "Every output with its level."),
|
|
1437
|
+
stateOf("inputs", "Input devices", "json", "Every input with its level."),
|
|
1438
|
+
stateOf("apps", "App volumes", "json", "audio.apps.spotify.volume, .muted, .active")
|
|
1439
|
+
],
|
|
1440
|
+
activate(activated) {
|
|
1441
|
+
context = activated;
|
|
1442
|
+
if (process.platform !== "win32") {
|
|
1443
|
+
activated.status("Audio control is available on Windows only", "info");
|
|
1444
|
+
} else if (!findHelper() && !activated.settings().helper) {
|
|
1445
|
+
activated.status(`${BUILD_HINT}.`, "info");
|
|
1446
|
+
}
|
|
1447
|
+
return () => {
|
|
1448
|
+
helper2.close();
|
|
1449
|
+
context = undefined;
|
|
1450
|
+
};
|
|
1451
|
+
}
|
|
1452
|
+
});
|
|
1453
|
+
export {
|
|
1454
|
+
AudioHelper,
|
|
1455
|
+
AudioUnavailable,
|
|
1456
|
+
BUILD_HINT,
|
|
1457
|
+
appKey,
|
|
1458
|
+
audioState,
|
|
1459
|
+
src_default as default,
|
|
1460
|
+
findHelper,
|
|
1461
|
+
nextDevice,
|
|
1462
|
+
targetIcons,
|
|
1463
|
+
targetPath
|
|
1464
|
+
};
|
|
1465
|
+
|
|
1466
|
+
//# debugId=234E92790EEA874864756E2164756E21
|