-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathService.qml
More file actions
465 lines (419 loc) · 16.1 KB
/
Copy pathService.qml
File metadata and controls
465 lines (419 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Services.Mpris
import Quickshell.Services.Pipewire
import "MediaModel.js" as MediaModel
Item {
id: root
property var manifest: null
readonly property string pluginId: "io.github.erikburdett.wavebar"
readonly property int sampleCount: 24
property var samples: MediaModel.zeroSamples(sampleCount)
property bool receivingFrames: false
property string lastError: ""
property string frameRemainder: ""
property bool shuttingDown: false
property bool fatalHelperError: false
readonly property bool helperRunning: visualizer.running
// WaveBar reads MPRIS and PipeWire itself. Omarchy's scoped plugin shell
// lends the first-party media service only to full-bar plugins, so
// borrowing the first-party media service left this widget permanently
// hidden (Omarchy 4.0). The source the user picked is remembered by key.
property string preferredPlayerKey: ""
property var playOrder: ({})
property int playSerial: 0
readonly property var mprisPlayers: Mpris.players ? Mpris.players.values : []
readonly property var pipewireNodes: Pipewire.nodes ? Pipewire.nodes.values : []
readonly property int maxPlayers: MediaModel.maxPlayerCount()
readonly property int maxStreams: MediaModel.maxStreamCount()
readonly property var rawSourcePlayers: MediaModel.toArray(mprisPlayers)
readonly property var rawPlaybackStreams: MediaModel.playbackStreams(pipewireNodes)
readonly property bool inputRejected: MediaModel.inputsOversized(
rawSourcePlayers, rawPlaybackStreams)
readonly property var sourcePlayers: inputRejected ? [] : rawSourcePlayers
readonly property var playbackStreams: inputRejected ? [] : rawPlaybackStreams
readonly property var focusedPlayers: MediaModel.focusedPlayers(sourcePlayers)
readonly property var activePlayer: MediaModel.chooseActivePlayer(
preferredPlayerKey, focusedPlayers, playOrder)
readonly property bool hasMedia: activePlayer !== null
readonly property bool playing: activePlayer ? !!activePlayer.isPlaying : false
readonly property string title: MediaModel.playerTitle(activePlayer)
readonly property string artist: MediaModel.playerArtist(activePlayer)
readonly property string album: MediaModel.playerAlbum(activePlayer)
readonly property string identity: MediaModel.playerIdentity(activePlayer)
readonly property string trackArtUrl: safeTrackArt(activePlayer && activePlayer.trackArtUrl
? activePlayer.trackArtUrl : "")
// Matching player metadata to PipeWire nodes is the most expensive model
// pass. Reuse one bounded result for capture and volume instead of scoring
// the same collection twice on every property update.
readonly property var streamMatch: MediaModel.chooseVolumeNode(activePlayer, playbackStreams)
readonly property var captureMatch: !playing
? ({ node: null, reason: "paused", score: 0 }) : streamMatch
readonly property var captureNode: captureMatch ? captureMatch.node : null
readonly property string captureTarget: MediaModel.captureTarget(captureNode)
readonly property string captureReason: captureMatch ? String(captureMatch.reason || "unmatched") : "unmatched"
readonly property bool shouldCapture: playing && captureTarget !== ""
readonly property var volumeNode: streamMatch ? streamMatch.node : null
readonly property bool volumeSupported: !!(volumeNode && volumeNode.audio)
|| !!(activePlayer && activePlayer.volumeSupported
&& !MediaModel.isBrowserPlayer(activePlayer))
readonly property real volume: {
var value = volumeNode && volumeNode.audio
? Number(volumeNode.audio.volume)
: activePlayer ? Number(activePlayer.volume) : 0
return isFinite(value) ? Math.max(0, Math.min(1, value)) : 0
}
readonly property string volumeBackend: volumeNode && volumeNode.audio
? "pipewire" : volumeSupported ? "mpris" : "unavailable"
readonly property string captureState: inputRejected ? "input-rejected"
: !hasMedia ? "no-media"
: !playing ? "paused"
: receivingFrames ? "live"
: captureReason === "ambiguous" ? "ambiguous"
: captureTarget !== "" ? "connecting"
: "unavailable"
// Use an argv array and an absolute path; Quickshell does not invoke a
// shell for Process commands.
readonly property string pluginSourceDir: {
var source = manifest && manifest.__sourceDir ? String(manifest.__sourceDir) : ""
if (source.length > 0 && source.length <= 4096 && source.charAt(0) === "/")
return source.replace(/\/+$/, "")
return Quickshell.env("HOME") + "/.config/omarchy/plugins/" + pluginId
}
readonly property string helperPath: pluginSourceDir + "/waveform.py"
readonly property string pythonPath: "/usr/bin/python3"
function playerTitle(player) { return MediaModel.playerTitle(player) }
function playerArtist(player) { return MediaModel.playerArtist(player) }
function playerIdentity(player) { return MediaModel.playerIdentity(player) }
// Album art hardening: only local files or trusted cover CDNs (Spotify,
// YouTube, Apple Music, Tidal/Deezer) are ever loaded, so an untrusted URL
// supplied by a media player never reaches an Image. The rules live in
// MediaModel.js, where the CI unit tests can reach them.
function isSafeTrackArt(url) { return MediaModel.isSafeTrackArt(url) }
function safeTrackArt(url) { return MediaModel.safeTrackArt(url) }
function playerKey(player) {
return MediaModel.playerKey(player)
}
function playerForKey(key) {
return MediaModel.playerForKey(focusedPlayers, key)
}
function playPlayer(player) {
if (!player) return false
if (player.canPlay) {
player.play()
return true
}
if (player.canTogglePlaying && !player.isPlaying) {
player.togglePlaying()
return true
}
return false
}
function pausePlayer(player) {
if (!player) return false
if (player.canPause) {
player.pause()
return true
}
if (player.canTogglePlaying && player.isPlaying) {
player.togglePlaying()
return true
}
return false
}
function runActionOn(player, action) {
if (!player) return false
if (action === "next") {
if (!player.canGoNext) return false
player.next()
return true
}
if (action === "previous") {
if (!player.canGoPrevious) return false
player.previous()
return true
}
if (action === "play") return playPlayer(player)
if (action === "pause") return pausePlayer(player)
if (action === "playPause") {
if (player.isPlaying ? pausePlayer(player) : playPlayer(player)) return true
if (!player.canTogglePlaying) return false
player.togglePlaying()
return true
}
return false
}
function runAction(action) {
var player = activePlayer
if (!player || inputRejected) return false
var key = playerKey(player)
if (key === "") return false
var handled = runActionOn(player, action)
if (handled) preferredPlayerKey = key
return handled
}
function selectPlayer(key) {
var safeKey = MediaModel.playerKey({ dbusName: key })
if (safeKey === "" || !playerForKey(safeKey)) return false
preferredPlayerKey = safeKey
return true
}
// Clicking a source is an explicit playback request. Start that source even
// when every player is paused, then pause the previously playing source so
// switching does not leave two media sessions playing at once.
function selectAndPlay(key) {
if (inputRejected) return false
var safeKey = MediaModel.playerKey({ dbusName: key })
if (safeKey === "") return false
var next = playerForKey(safeKey)
if (!next || !MediaModel.isFocusedMediaPlayer(next)) return false
var current = activePlayer
var currentKey = playerKey(current)
var currentWasPlaying = current && current.isPlaying
if (!selectPlayer(safeKey)) return false
var started = next.isPlaying || playPlayer(next)
if (started && currentWasPlaying && currentKey !== safeKey)
pausePlayer(current)
return started
}
// Forget a pick once its player leaves the bus so a stale key cannot pin
// the selection to a source that no longer exists, and refresh which
// players are playing and in what order they started.
function syncPlayers() {
if (preferredPlayerKey !== "" && !MediaModel.playerForKey(mprisPlayers, preferredPlayerKey))
preferredPlayerKey = ""
var synced = MediaModel.syncPlayOrder(mprisPlayers, playOrder, playSerial)
playSerial = synced.serial
playOrder = synced.order
}
function seekTo(position) {
var player = activePlayer
if (!player || !player.canSeek || !player.positionSupported) return false
var maximum = player.lengthSupported ? Math.max(0, Number(player.length) || 0) : Number(position)
player.position = Math.max(0, Math.min(maximum, Number(position) || 0))
return true
}
function setVolume(value) {
var next = Math.max(0, Math.min(1, Number(value) || 0))
if (volumeNode && volumeNode.audio) {
volumeNode.audio.volume = next
return true
}
var player = activePlayer
if (!player || !player.volumeSupported || MediaModel.isBrowserPlayer(player))
return false
player.volume = next
return true
}
function resetWaveform() {
samples = MediaModel.zeroSamples(sampleCount)
receivingFrames = false
frameRemainder = ""
staleTimer.stop()
}
function shutdown() {
if (shuttingDown) return
shuttingDown = true
startTimer.stop()
retryTimer.stop()
staleTimer.stop()
visualizer.running = false
resetWaveform()
}
function rejectProtocol(message) {
lastError = message
fatalHelperError = true
visualizer.running = false
resetWaveform()
}
function consumeChunk(chunk) {
var framed = MediaModel.frameChunk(frameRemainder, chunk)
if (!framed.ok) {
rejectProtocol("Waveform output exceeded the protocol limit")
return
}
frameRemainder = framed.remainder
for (var i = 0; i < framed.frames.length; i++) {
if (!consumeFrame(framed.frames[i])) return
}
}
function consumeFrame(line) {
if (String(line || "").length > MediaModel.maxFrameChars()) {
rejectProtocol("Waveform frame exceeded the protocol limit")
return false
}
var parsed = MediaModel.parseFrame(line, sampleCount)
if (!parsed) {
rejectProtocol("Waveform helper emitted an invalid frame")
return false
}
samples = parsed
receivingFrames = true
lastError = ""
staleTimer.restart()
return true
}
function restartVisualizer() {
if (shuttingDown) return
fatalHelperError = false
startTimer.stop()
retryTimer.stop()
visualizer.running = false
resetWaveform()
if (shouldCapture) startTimer.restart()
}
function startVisualizer() {
if (shuttingDown || fatalHelperError || !shouldCapture || visualizer.running) return false
// Set this immediately before every exec. Keeping the object behind a
// function avoids qmllint's QVariantMap/QVariantHash false positive while
// preserving Quickshell's documented Process environment semantics.
visualizer.environment = sanitizedVisualizerEnvironment()
visualizer.exec([
pythonPath, "-I", "-S", helperPath,
"--target", captureTarget, "--bars", String(sampleCount)
])
return true
}
function sanitizedVisualizerEnvironment() {
return {
"LANG": "C.UTF-8",
"LC_ALL": "C.UTF-8",
"PATH": "/usr/bin",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONNOUSERSITE": "1",
"PYTHONSAFEPATH": "1"
}
}
function handleVisualizerExited(exitCode) {
root.receivingFrames = false
if (exitCode !== 0 && root.shouldCapture) {
if (exitCode === 127) {
root.fatalHelperError = true
root.lastError = "Waveform dependencies are unavailable: python and pipewire-audio are required"
} else if (exitCode === 126) {
root.fatalHelperError = true
root.lastError = "Waveform helper rejected an unsafe executable or runtime"
} else if (!root.fatalHelperError) {
root.lastError = "Waveform helper exited with code " + String(exitCode)
}
}
if (!root.shuttingDown && !root.fatalHelperError
&& root.shouldCapture && !startTimer.running)
retryTimer.restart()
}
onCaptureTargetChanged: restartVisualizer()
onPlayingChanged: restartVisualizer()
onMprisPlayersChanged: syncPlayers()
// Play order only changes when a player appears, leaves, or flips its
// playing state, so listen for exactly that instead of polling.
Instantiator {
model: root.mprisPlayers
delegate: Connections {
required property var modelData
target: modelData
function onIsPlayingChanged() { root.syncPlayers() }
}
}
// Binding the playback streams keeps their PipeWire properties and volume
// live; without a tracker the app-name matching sees empty metadata.
PwObjectTracker { objects: root.playbackStreams }
Component.onCompleted: {
// Connecting explicitly avoids a qmllint false positive caused by the
// Quickshell type description omitting QProcess::ExitStatus. Qt owns and
// disconnects this connection with the QML component.
visualizer.exited.connect(root.handleVisualizerExited)
syncPlayers()
restartVisualizer()
}
Component.onDestruction: root.shutdown()
Timer {
id: startTimer
interval: 80
repeat: false
onTriggered: {
if (!root.shouldCapture) return
root.lastError = ""
root.startVisualizer()
}
}
Timer {
id: retryTimer
interval: 1500
repeat: false
onTriggered: root.startVisualizer()
}
// QProcess can fail before emitting exited (for example while the user's
// PipeWire session is still settling during login). Keep a low-frequency
// watchdog active until the first frame arrives so startup self-heals.
Timer {
id: watchdogTimer
interval: 2000
repeat: true
running: !root.shuttingDown && !root.fatalHelperError
&& root.shouldCapture && !root.receivingFrames
onTriggered: root.startVisualizer()
}
Timer {
id: staleTimer
interval: 800
repeat: false
onTriggered: root.receivingFrames = false
}
Process {
id: visualizer
command: [
root.pythonPath, "-I", "-S", root.helperPath,
"--target", root.captureTarget, "--bars", String(root.sampleCount)
]
running: false
clearEnvironment: true
stdout: SplitParser {
// Empty-marker mode emits raw QProcess chunks and does not retain an
// unterminated line. WaveBar applies its own strict framing ceiling.
splitMarker: ""
onRead: function(chunk) { root.consumeChunk(chunk) }
}
// The helper sends only a fixed diagnostic on its own stderr and discards
// pw-record stderr. Leaving this channel unbound avoids a resident parser.
}
IpcHandler {
target: root.pluginId
function status(): string {
return JSON.stringify({
hasMedia: root.hasMedia,
playing: root.playing,
title: root.title,
artist: root.artist,
identity: root.identity,
sourceCount: root.focusedPlayers.length,
inputRejected: root.inputRejected,
volume: root.volume,
volumeBackend: root.volumeBackend,
captureState: root.captureState,
captureReason: root.captureReason,
captureTarget: root.captureTarget,
lastError: root.lastError,
helperPath: root.helperPath,
helperRunning: root.helperRunning,
fatalHelperError: root.fatalHelperError,
shuttingDown: root.shuttingDown,
startPending: startTimer.running,
retryPending: retryTimer.running,
watchdogRunning: watchdogTimer.running
})
}
function restart(): string {
root.restartVisualizer()
return "ok"
}
function playSource(key: string): string {
return root.selectAndPlay(key) ? "ok" : "unhandled"
}
function playPause(): string { return root.runAction("playPause") ? "ok" : "unhandled" }
function next(): string { return root.runAction("next") ? "ok" : "unhandled" }
function previous(): string { return root.runAction("previous") ? "ok" : "unhandled" }
function setVolume(value: real): string { return root.setVolume(value) ? "ok" : "unhandled" }
}
}