repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
quickshell/nuguland/Audio.qml
Rawimport QtQuick
import Quickshell
import Quickshell.Services.Pipewire
Item {
id: root
property QtObject osdService
readonly property string audioSamplerBackend: "Pipewire.links"
// Audio activity is derived from PipeWire link presence (event-driven), not from
// capturing audio. nodeActive() scans Pipewire.links for an active link touching
// the node, so these re-evaluate when links change — no capture stream, no timer.
readonly property bool outputActive: nodeActive(defaultSinkNode()) && !nodeMuted(defaultSinkNode())
readonly property bool inputActive: nodeActive(defaultSourceNode()) && !nodeMuted(defaultSourceNode())
property var lastAction: ({
})
property var lastOsd: ({
})
readonly property bool pipewireReady: Pipewire.ready
readonly property var boundedActions: ["increase", "decrease", "muteOutput", "muteInput", "set-volume", "toggle-mute", "set-default-sink", "set-default-source"]
function pipewireNodes() {
return Pipewire.nodes.values || [];
}
function nodeId(node) {
return node ? String(node.id) : "";
}
function nodeTypeText(node) {
return String(PwNodeType.toString(node && node.type ? node.type : PwNodeType.Untracked));
}
function nodeProps(node) {
return node && node.properties ? node.properties : ({
});
}
function nodeName(node) {
if (!node)
return "";
const props = nodeProps(node);
return String(node.description || node.nickname || props["node.description"] || props["application.name"] || props["media.name"] || node.name || node.id);
}
function channelNames(node) {
if (!node || !node.audio)
return [];
const channels = node.audio.channels || [];
const names = [];
for (let i = 0; i < channels.length; i++) names.push(String(PwAudioChannel.toString(channels[i])).toLowerCase())
return names;
}
function nodeVolume(node) {
return Math.max(0, Math.min(1.5, Number(node && node.audio ? node.audio.volume : 0)));
}
function nodeMuted(node) {
return !!(node && node.audio && node.audio.muted);
}
function isAudioSink(node) {
if (!node || !node.audio || node.isStream)
return false;
return !!node.isSink || nodeTypeText(node).indexOf("AudioSink") >= 0;
}
function isAudioSource(node) {
if (!node || !node.audio || node.isStream)
return false;
const type = nodeTypeText(node);
return type.indexOf("AudioSource") >= 0 || (!node.isSink && type.indexOf("Source") >= 0 && type.indexOf("Video") < 0);
}
function isAudioStream(node) {
if (!node || !node.audio)
return false;
const type = nodeTypeText(node);
return !!node.isStream || type.indexOf("AudioOutStream") >= 0 || type.indexOf("AudioInStream") >= 0;
}
function audioSinks() {
return pipewireNodes().filter(isAudioSink);
}
function audioSources() {
return pipewireNodes().filter(isAudioSource);
}
function audioStreams() {
return pipewireNodes().filter(isAudioStream);
}
function defaultSinkNode() {
return Pipewire.defaultAudioSink || audioSinks()[0] || null;
}
function defaultSourceNode() {
return Pipewire.defaultAudioSource || audioSources()[0] || null;
}
function nodeById(id) {
const wanted = String(id);
const nodes = pipewireNodes();
for (let i = 0; i < nodes.length; i++) if (nodeId(nodes[i]) === wanted) {
return nodes[i];
}
return null;
}
function linkActive(link) {
return String(PwLinkState.toString(link && link.state ? link.state : PwLinkState.Unlinked)).toLowerCase() === "active";
}
function linkedNodeId(node) {
const id = nodeId(node);
const links = Pipewire.links.values || [];
for (let i = 0; i < links.length; i++) {
const link = links[i];
if (!linkActive(link))
continue;
if (nodeId(link.source) === id)
return nodeId(link.target);
if (nodeId(link.target) === id)
return nodeId(link.source);
}
return "";
}
function nodeActive(node) {
return linkedNodeId(node).length > 0;
}
function nodeRow(node, kind) {
const props = nodeProps(node);
return {
"id": nodeId(node),
"name": nodeName(node),
"description": String(node ? node.description || node.name || "" : ""),
"volume": Math.round(nodeVolume(node) * 100) / 100,
"muted": nodeMuted(node),
"isDefault": kind === "sink" ? node === Pipewire.defaultAudioSink : (kind === "source" ? node === Pipewire.defaultAudioSource : false),
"channels": channelNames(node),
"active": nodeActive(node),
"nodeName": String(node ? node.name || "" : ""),
"mediaClass": String(props["media.class"] || ""),
"application": String(props["application.name"] || ""),
"title": String(props["media.name"] || nodeName(node)),
"target": kind === "stream" ? linkedNodeId(node) : ""
};
}
function rowsById(rows) {
const indexed = {
};
for (let i = 0; i < rows.length; i++) indexed[rows[i].id] = rows[i]
return indexed;
}
function roleForSink(sink) {
if (!sink || nodeMuted(sink))
return "ui.minor";
const volume = nodeVolume(sink);
if (volume >= 0.95)
return "warning";
if (volume <= 0.01)
return "ui.minor";
return "ui.accent";
}
function microphoneActive() {
const sources = audioSources();
for (let i = 0; i < sources.length; i++) if (nodeActive(sources[i]) && !nodeMuted(sources[i])) {
return true;
}
return false;
}
function microphoneActiveValue() {
return microphoneActive() ? 1 : 0;
}
function microphoneMuted() {
const source = defaultSourceNode();
return source ? nodeMuted(source) : true;
}
function microphoneRole() {
return microphoneActive() ? "warning" : "ui.minor";
}
function cameraNodeActive(node) {
if (!node || !node.ready)
return false;
const type = nodeTypeText(node).toLowerCase();
const props = nodeProps(node);
const mediaClass = String(props["media.class"] || "").toLowerCase();
return type.indexOf("videosource") >= 0 || mediaClass.indexOf("video/source") >= 0 || mediaClass.indexOf("camera") >= 0;
}
function nativeCameraActive() {
const nodes = pipewireNodes();
for (let i = 0; i < nodes.length; i++) if (cameraNodeActive(nodes[i])) {
return true;
}
return false;
}
function cameraActive() {
return nativeCameraActive();
}
function privacyLabel() {
const mic = microphoneActive();
const cam = cameraActive();
if (mic && cam)
return "Mic Cam";
if (mic)
return "Mic";
if (cam)
return "Cam";
return "Audio";
}
function privacyActive() {
return microphoneActive() || cameraActive();
}
function privacyShaderValues() {
return [microphoneActiveValue(), cameraActive() ? 1 : 0, privacyActive() ? 1 : 0, outputMutedValue()];
}
function privacyWidgetState() {
const mic = microphoneActive();
const cam = cameraActive();
return {
"backend": "Audio.qml",
"compact": {
"label": privacyLabel(),
"role": mic || cam ? "warning" : roleForSink(defaultSinkNode()),
"opens": "audio"
},
"privacy": {
"active": mic || cam,
"microphoneActive": mic,
"cameraActive": cam,
"shader": {
"kind": "privacy-veil",
"clipped": true,
"source": "shaders/privacy-veil.frag.qsb",
"priorArt": "Scanline overlay and warning veil GLSL ideas plus Inigo Quilez-style rounded-box SDF alpha mask",
"values": privacyShaderValues(),
"colors": ["warning", "error", "ui.accent", "ui.backdrop"]
}
}
};
}
function privacyWidgetJson() {
return JSON.stringify(privacyWidgetState());
}
function outputMutedValue() {
return nodeMuted(defaultSinkNode()) ? 1 : 0;
}
function inputMutedValue() {
return nodeMuted(defaultSourceNode()) ? 1 : 0;
}
function osd(event, value) {
if (!osdService)
return {
"event": event,
"value": value
};
try {
return JSON.parse(osdService.trigger(event, value));
} catch (error) {
return {
"event": event,
"value": value
};
}
}
function audioPanelContract() {
return {
"source": "PipeWire service nodes (event-driven link state)",
"ownedBy": "PopoutHost.qml inline AudioPanel",
"pipewireMixerPanel": true,
"outputSelector": true,
"inputSelector": true,
"deviceVolumeSliders": true,
"perStreamRows": true,
"streamTargetReassignment": false,
"nativeStreamMove": false,
"noExternalMixer": true,
"serviceActionsOnly": true,
"rowKinds": ["sink", "source", "stream"],
"wheelParity": ["volume-up", "volume-down"],
"privacyRole": "microphoneRole"
};
}
function stateObject(osdState) {
const sink = defaultSinkNode();
const source = defaultSourceNode();
const sinkRows = audioSinks().map((node) => {
return nodeRow(node, "sink");
});
const sourceRows = audioSources().map((node) => {
return nodeRow(node, "source");
});
const streamRows = audioStreams().map((node) => {
const row = nodeRow(node, "stream");
row.app = row.application || row.name;
return row;
});
return {
"backend": "Audio.qml",
"usesNativePipewire": true,
"pipewireReady": pipewireReady,
"helperProcesses": [],
"sampler": {
"backend": "Pipewire.links",
"outputActive": outputActive,
"inputActive": inputActive,
"targets": inputActive ? ["defaultAudioSink", "defaultAudioSource"] : ["defaultAudioSink"],
"efficient": true,
"native": true,
"capture": false
},
"serviceOwnedActions": true,
"boundedActions": boundedActions,
"audioPanelContract": audioPanelContract(),
"defaults": {
"sink": nodeId(sink),
"source": nodeId(source)
},
"sinks": sinkRows,
"sources": sourceRows,
"streams": streamRows,
"sinksById": rowsById(sinkRows),
"sourcesById": rowsById(sourceRows),
"streamsById": rowsById(streamRows),
"compact": {
"label": privacyLabel(),
"sink": nodeName(sink),
"volumeLabel": `${Math.round(nodeVolume(sink) * 100)}%`,
"muted": nodeMuted(sink),
"volumeRole": roleForSink(sink)
},
"shader": {
"kind": "audio-waveform",
"clipped": true,
"source": "shaders/audio-waveform.frag.qsb",
"priorArt": "ISF raw audio waveform convention plus WebAudio oscilloscope time-domain visualizer; Quickshell PipeWire link-state activity",
"values": [outputActive ? 1 : 0, outputMutedValue(), inputActive ? 1 : 0, inputMutedValue()],
"colors": ["ui.accent", "ui.focus", "ui.minor", "ui.backdrop", "warning", "error"]
},
"privacy": Object.assign({
"microphoneActive": microphoneActive(),
"microphoneMuted": microphoneMuted(),
"microphoneRole": microphoneRole(),
"cameraActive": cameraActive(),
"active": privacyActive()
}, privacyWidgetState().privacy),
"lastAction": lastAction,
"osd": osdState || lastOsd || {
}
};
}
function stateJson() {
return JSON.stringify(stateObject({
}));
}
function setVolume(id, volume) {
const node = nodeById(id);
const value = Math.max(0, Math.min(1.5, Number(volume)));
if (node && node.audio)
node.audio.volume = value;
lastAction = {
"kind": node ? "set-volume" : "missing-node",
"id": String(id),
"volume": Math.round(value * 100) / 100
};
lastOsd = osd(isAudioSource(node) ? "microphone" : "volume", String(Math.round(value * 100)));
return JSON.stringify(stateObject(lastOsd));
}
function toggleMute(id) {
const node = nodeById(id);
let muted = false;
if (node && node.audio) {
muted = !node.audio.muted;
node.audio.muted = muted;
lastOsd = osd(isAudioSource(node) ? "microphone" : "volume", muted ? "muted" : "unmuted");
}
lastAction = {
"kind": node ? "toggle-mute" : "missing-node",
"id": String(id),
"muted": muted
};
return JSON.stringify(stateObject(lastOsd));
}
function increase() {
const sink = defaultSinkNode();
return sink ? setVolume(nodeId(sink), Math.min(1.5, nodeVolume(sink) + 0.05)) : stateJson();
}
function decrease() {
const sink = defaultSinkNode();
return sink ? setVolume(nodeId(sink), Math.max(0, nodeVolume(sink) - 0.05)) : stateJson();
}
function muteOutput() {
const sink = defaultSinkNode();
return sink ? toggleMute(nodeId(sink)) : stateJson();
}
function muteInput() {
const source = defaultSourceNode();
return source ? toggleMute(nodeId(source)) : stateJson();
}
function route(action) {
const name = String(action);
if (name === "increase")
return increase();
if (name === "decrease")
return decrease();
if (name === "muteOutput")
return muteOutput();
if (name === "muteInput")
return muteInput();
lastAction = {
"kind": "unknown-volume-action",
"action": name
};
return stateJson();
}
function setDefaultSink(id) {
const node = nodeById(id);
if (node && isAudioSink(node))
Pipewire.preferredDefaultAudioSink = node;
lastAction = {
"kind": node && isAudioSink(node) ? "set-default-sink" : "missing-sink",
"id": String(id)
};
return stateJson();
}
function setDefaultSource(id) {
const node = nodeById(id);
if (node && isAudioSource(node))
Pipewire.preferredDefaultAudioSource = node;
lastAction = {
"kind": node && isAudioSource(node) ? "set-default-source" : "missing-source",
"id": String(id)
};
return stateJson();
}
function moveStream(id, target) {
lastAction = {
"kind": "unsupported-native-stream-move",
"id": String(id),
"target": String(target)
};
return stateJson();
}
PwObjectTracker {
objects: [Pipewire.defaultAudioSink, Pipewire.defaultAudioSource]
}
}