Skip to content

Commit 1636242

Browse files
committed
Add media source switching
1 parent 94530d8 commit 1636242

4 files changed

Lines changed: 209 additions & 12 deletions

File tree

‎bin/omarchy-audio-source-switch‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
# omarchy:summary=Cycle to the next media source and transfer playback when the current source is playing
4+
# omarchy:args=[next|previous]
5+
# omarchy:examples=omarchy audio source switch | omarchy-audio-source-switch previous
6+
7+
direction="${1:-next}"
8+
9+
case "$direction" in
10+
next)
11+
omarchy-shell media sourceSwitch
12+
;;
13+
previous)
14+
omarchy-shell media sourceSwitchPrevious
15+
;;
16+
*)
17+
echo "Usage: omarchy-audio-source-switch [next|previous]" >&2
18+
exit 1
19+
;;
20+
esac

‎default/hypr/bindings/media.lua‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ o.bind("ALT + XF86MonBrightnessDown", "Brightness down precise", "omarchy-bright
2424
o.bind("XF86AudioNext", "Next track", "omarchy-shell media next", { locked = true })
2525
o.bind("XF86AudioPause", "Pause", "omarchy-shell media playPause", { locked = true })
2626
o.bind("XF86AudioPlay", "Play", "omarchy-shell media playPause", { locked = true })
27+
o.bind("ALT + XF86AudioPause", "Switch media source", "omarchy-audio-source-switch", { locked = true })
28+
o.bind("ALT + XF86AudioPlay", "Switch media source", "omarchy-audio-source-switch", { locked = true })
2729
o.bind("XF86AudioPrev", "Previous track", "omarchy-shell media previous", { locked = true })
2830

29-
o.bind("SUPER + XF86AudioMute", "Switch audio output", "omarchy-audio-output-switch", { locked = true })
31+
o.bind("ALT + XF86AudioMute", "Switch audio output", "omarchy-audio-output-switch", { locked = true })

‎shell/plugins/osd/Osd.qml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ Item {
1616
property bool hasProgress: true
1717
property int duration: 1200
1818
readonly property int cardWidth: Style.space(269)
19+
readonly property int mediaCardWidth: Math.round(cardWidth * 1.5)
1920
readonly property int messageWidth: Style.space(190)
21+
readonly property int mediaMessageWidth: messageWidth + mediaCardWidth - cardWidth
22+
readonly property bool mediaOsd: iconKey.indexOf("media") === 0 || iconKey.indexOf("player") === 0
2023

2124
function iconFor(name, percent) {
2225
var n = String(name || "").toLowerCase()
@@ -31,6 +34,7 @@ Item {
3134
if (n === "touchpad") return "󰟸"
3235
if (n === "touch" || n === "touchscreen") return "󰜉"
3336
if (n === "media" || n === "player") return "󰝚"
37+
if (n === "media-source" || n === "player-source") return "󰝚"
3438
if (n === "media-play" || n === "player-play") return "󰐊"
3539
if (n === "media-pause" || n === "player-pause") return "󰏤"
3640
if (n === "media-next" || n === "player-next") return "󰒭"
@@ -95,7 +99,7 @@ Item {
9599

96100
Rectangle {
97101
id: card
98-
width: root.cardWidth
102+
width: root.mediaOsd ? root.mediaCardWidth : root.cardWidth
99103
height: Math.max(Style.space(68), Style.font.displayLarge + Style.spacing.panelGap)
100104
anchors.horizontalCenter: parent.horizontalCenter
101105
anchors.bottom: parent.bottom
@@ -133,7 +137,7 @@ Item {
133137
}
134138
}
135139
Text {
136-
width: root.hasProgress ? Style.space(41) : root.messageWidth
140+
width: root.hasProgress ? Style.space(41) : (root.mediaOsd ? root.mediaMessageWidth : root.messageWidth)
137141
anchors.verticalCenter: parent.verticalCenter
138142
text: root.message
139143
font.family: Style.font.family

‎shell/plugins/services/media/Service.qml‎

Lines changed: 180 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import QtQuick
22
import Quickshell
33
import Quickshell.Io
44
import Quickshell.Services.Mpris
5+
import Quickshell.Services.Pipewire
56

67
Item {
78
id: root
@@ -12,7 +13,17 @@ Item {
1213
property int playSerial: 0
1314

1415
readonly property var players: Mpris.players ? Mpris.players.values : []
16+
readonly property var nodes: Pipewire.nodes ? Pipewire.nodes.values : []
17+
readonly property var playbackStreams: {
18+
var list = []
19+
for (var i = 0; i < nodes.length; i++) {
20+
var n = nodes[i]
21+
if (n && n.isStream && isPlaybackStream(n) && n.audio) list.push(n)
22+
}
23+
return list
24+
}
1525
readonly property var sourcePlayers: orderedSourcePlayers()
26+
readonly property var sourceCyclePlayers: orderedCycleSourcePlayers()
1627
readonly property var activePlayer: selectActivePlayer()
1728
readonly property bool hasMedia: activePlayer !== null && (activePlayer.trackTitle || activePlayer.trackArtist)
1829
readonly property string title: activePlayer ? (activePlayer.trackTitle || "") : ""
@@ -35,6 +46,81 @@ Item {
3546
return !!(player && (player.trackTitle || player.trackArtist || player.trackAlbum || player.trackArtUrl))
3647
}
3748

49+
function playerCanControl(player) {
50+
return !!(player && (player.canTogglePlaying || player.canPlay || player.canPause || player.canGoNext || player.canGoPrevious))
51+
}
52+
53+
function canHandleAction(player, action) {
54+
if (!player) return false
55+
if (action === "next") return !!player.canGoNext
56+
if (action === "previous") return !!player.canGoPrevious
57+
if (action === "play") return !!(player.canPlay || player.canTogglePlaying)
58+
if (action === "pause") return !!(player.canPause || player.canTogglePlaying)
59+
if (action === "playPause") return !!(player.canTogglePlaying || player.canPlay || player.canPause)
60+
return false
61+
}
62+
63+
function canCycleSource(player) {
64+
return !!(player && hasMetadata(player) && (player.isPlaying || player.canPlay))
65+
}
66+
67+
function nodeProps(node) {
68+
return node && node.ready && node.properties ? node.properties : {}
69+
}
70+
71+
function isPlaybackStream(node) {
72+
if (!node || !node.isStream) return false
73+
if (node.isSink === true) return true
74+
75+
var mediaClass = String(node.type || "")
76+
return mediaClass.indexOf("Stream/Output/Audio") !== -1
77+
|| mediaClass.indexOf("AudioOutStream") !== -1
78+
|| mediaClass.indexOf("Output") !== -1
79+
}
80+
81+
function streamLabelKey(label) {
82+
var key = String(label || "").toLowerCase()
83+
key = key.replace(/^pipewire alsa \[/, "")
84+
key = key.replace(/\]$/, "")
85+
key = key.replace(/^alsa playback \[/, "")
86+
key = key.replace(/[^a-z0-9]+/g, "")
87+
return key
88+
}
89+
90+
function rawStreamLabel(node) {
91+
if (!node) return ""
92+
var p = nodeProps(node)
93+
return p["application.name"]
94+
|| node.description
95+
|| p["media.name"]
96+
|| p["node.name"]
97+
|| node.name
98+
}
99+
100+
function playerAppLabel(player) {
101+
if (!player) return ""
102+
var dbus = String(player.dbusName || "")
103+
dbus = dbus.replace(/^org\.mpris\.MediaPlayer2\./, "")
104+
dbus = dbus.replace(/\.instance[0-9]+$/, "")
105+
return player.desktopEntry || player.identity || dbus
106+
}
107+
108+
function playerHasPlaybackStream(player) {
109+
var playerKey = streamLabelKey(playerAppLabel(player))
110+
if (!playerKey) return false
111+
112+
for (var i = 0; i < playbackStreams.length; i++) {
113+
var streamKey = streamLabelKey(rawStreamLabel(playbackStreams[i]))
114+
if (!streamKey) continue
115+
if (streamKey === playerKey
116+
|| streamKey.indexOf(playerKey) !== -1
117+
|| playerKey.indexOf(streamKey) !== -1)
118+
return true
119+
}
120+
121+
return false
122+
}
123+
38124
function playerKey(player) {
39125
if (!player) return ""
40126
return String(player.dbusName || player.desktopEntry || player.identity || "")
@@ -102,7 +188,22 @@ Item {
102188
return list
103189
}
104190

105-
function oldestPlayingPlayer() {
191+
function orderedCycleSourcePlayers() {
192+
var list = []
193+
for (var i = 0; i < players.length; i++) {
194+
var p = players[i]
195+
if (canCycleSource(p)) list.push(p)
196+
}
197+
198+
list.sort(function(a, b) {
199+
if (isProxyPlayer(a) !== isProxyPlayer(b)) return isProxyPlayer(a) ? 1 : -1
200+
return labelFor(a).localeCompare(labelFor(b))
201+
})
202+
203+
return list
204+
}
205+
206+
function oldestPlayingPlayer(requirePlaybackStream) {
106207
var oldest = null
107208
var oldestOrder = 0
108209
var playingProxy = null
@@ -114,6 +215,8 @@ Item {
114215

115216
var proxyPlayer = isProxyPlayer(p)
116217
if (p.isPlaying) {
218+
if (requirePlaybackStream && !playerHasPlaybackStream(p)) continue
219+
117220
var order = playerOrder(p, i + 1000)
118221
if (!proxyPlayer && (!oldest || order < oldestOrder)) {
119222
oldest = p
@@ -132,6 +235,10 @@ Item {
132235
var preferred = null
133236
var trackPlayer = null
134237
var trackProxy = null
238+
var streamPlayer = null
239+
var streamProxy = null
240+
var controllablePlayer = null
241+
var controllableProxy = null
135242
var identityPlayer = null
136243
var identityProxy = null
137244

@@ -143,17 +250,25 @@ Item {
143250

144251
if (preferredPlayerKey && playerKey(p) === preferredPlayerKey && hasMetadata(p)) preferred = p
145252

146-
if (hasTrackMetadata(p)) {
253+
if (playerHasPlaybackStream(p)) {
254+
if (!proxy && !streamPlayer) streamPlayer = p
255+
else if (proxy && !streamProxy) streamProxy = p
256+
} else if (hasTrackMetadata(p)) {
147257
if (!proxy && !trackPlayer) trackPlayer = p
148258
else if (proxy && !trackProxy) trackProxy = p
259+
} else if (playerCanControl(p)) {
260+
if (!proxy && !controllablePlayer) controllablePlayer = p
261+
else if (proxy && !controllableProxy) controllableProxy = p
149262
} else if (hasMetadata(p)) {
150263
if (!proxy && !identityPlayer) identityPlayer = p
151264
else if (proxy && !identityProxy) identityProxy = p
152265
}
153266
}
154267

155268
if (preferred && preferred.isPlaying) return preferred
156-
return oldestPlayingPlayer() || preferred || trackPlayer || trackProxy || identityPlayer || identityProxy || null
269+
var streamCandidate = streamPlayer || streamProxy
270+
var streamPreferred = preferred && playerHasPlaybackStream(preferred) ? preferred : null
271+
return oldestPlayingPlayer(true) || oldestPlayingPlayer(false) || streamPreferred || streamCandidate || preferred || trackPlayer || trackProxy || controllablePlayer || controllableProxy || identityPlayer || identityProxy || null
157272
}
158273

159274
function labelFor(player) {
@@ -183,8 +298,30 @@ Item {
183298
return true
184299
}
185300

186-
function switchSource(delta) {
187-
var list = sourcePlayers
301+
function playPlayer(player) {
302+
if (!player) return false
303+
if (player.canPlay) {
304+
player.play()
305+
return true
306+
}
307+
return false
308+
}
309+
310+
function pausePlayer(player) {
311+
if (!player) return false
312+
if (player.canPause) {
313+
player.pause()
314+
return true
315+
}
316+
if (player.canTogglePlaying && player.isPlaying) {
317+
player.togglePlaying()
318+
return true
319+
}
320+
return false
321+
}
322+
323+
function switchSource(delta, transferPlayback, showFeedback) {
324+
var list = sourceCyclePlayers
188325
if (!list || list.length === 0) return false
189326

190327
var activeKey = playerKey(activePlayer)
@@ -197,7 +334,24 @@ Item {
197334
}
198335

199336
index = (index + delta + list.length) % list.length
200-
preferredPlayerKey = playerKey(list[index])
337+
var current = activePlayer
338+
var next = list[index]
339+
var currentWasPlaying = current && current.isPlaying
340+
var currentKey = playerKey(current)
341+
var nextKey = playerKey(next)
342+
343+
preferredPlayerKey = nextKey
344+
345+
if (transferPlayback && currentWasPlaying && next && nextKey !== currentKey) {
346+
var nextWasPlaying = next.isPlaying
347+
var nextStarted = nextWasPlaying || playPlayer(next)
348+
if (nextStarted) pausePlayer(current)
349+
}
350+
351+
if (showFeedback !== false) Qt.callLater(function() {
352+
root.showOsd("Source", "media-source", next)
353+
})
354+
201355
return true
202356
}
203357

@@ -206,10 +360,17 @@ Item {
206360
if (targeted) return targeted
207361

208362
if (action === "pause" || action === "playPause") {
209-
var oldest = oldestPlayingPlayer()
363+
var oldest = oldestPlayingPlayer(true) || oldestPlayingPlayer(false)
210364
if (oldest) return oldest
211365
}
212366

367+
if (canHandleAction(activePlayer, action)) return activePlayer
368+
369+
var list = sourcePlayers
370+
for (var i = 0; i < list.length; i++) {
371+
if (canHandleAction(list[i], action)) return list[i]
372+
}
373+
213374
return activePlayer
214375
}
215376

@@ -282,6 +443,8 @@ Item {
282443
onTriggered: root.syncPlayingOrder()
283444
}
284445

446+
PwObjectTracker { objects: root.playbackStreams }
447+
285448
function statusJson() {
286449
var p = activePlayer
287450
return JSON.stringify({
@@ -328,11 +491,19 @@ Item {
328491
}
329492

330493
function sourceNext(): string {
331-
return root.switchSource(1) ? "ok" : "unhandled"
494+
return root.switchSource(1, false, true) ? "ok" : "unhandled"
332495
}
333496

334497
function sourcePrevious(): string {
335-
return root.switchSource(-1) ? "ok" : "unhandled"
498+
return root.switchSource(-1, false, true) ? "ok" : "unhandled"
499+
}
500+
501+
function sourceSwitch(): string {
502+
return root.switchSource(1, true, true) ? "ok" : "unhandled"
503+
}
504+
505+
function sourceSwitchPrevious(): string {
506+
return root.switchSource(-1, true, true) ? "ok" : "unhandled"
336507
}
337508

338509
function ping(): string {

0 commit comments

Comments
 (0)