From 195d9d6c6f1e327bcbf2f48200d7a8aea72eebd3 Mon Sep 17 00:00:00 2001 From: Ziad Arafat Date: Fri, 30 Oct 2026 15:21:56 -0600 Subject: [PATCH] Sounds actually audible: unmute the event stream, Konsole bell via notifications, chime for every app notification MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The WirePlumber 'Notification' role stream was saved muted, silencing every theme sound except Plasma's volume tick; dot sounds build/test/notify now unmutes it at 100% (ensure_event_stream). - Konsole profiles: BellMode=1 (notification) — the default system beep is silent on Wayland. - notification-sounds: user service watching Notify calls via dbus-monitor; plays message-new-instant for notifications without their own sound (skips KNotification events, sound hints, and apps with in-app sounds: Telegram, Vesktop). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013UzjWUxheYfqqjWGH7Ktmc --- bin/dot | 33 ++++++++++- config/konsole/Dark.profile | 3 + config/konsole/Light.profile | 3 + config/konsole/etv.profile | 3 + config/konsole/general.profile | 3 + home/gui/sounds.nix | 32 +++++++++- scripts/notification-sounds.py | 104 +++++++++++++++++++++++++++++++++ 7 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 scripts/notification-sounds.py diff --git a/bin/dot b/bin/dot index 8e5e361..3cd0b95 100755 --- a/bin/dot +++ b/bin/dot @@ -420,6 +420,34 @@ sound_output_info() { wpctl status 2>/dev/null | sed -n '/Sinks:/,/Sources:/p' | grep -E "MUTED|vol: 0\.00" | sed 's/^[ │*]*/ muted sink: /' } +# All theme/notification sounds share one "event"/Notification role stream whose +# mute + volume WirePlumber remembers. Plasma's mixer can mute it ("Notification +# Sounds"), which silences everything except the volume tick. Unmute it at 100%. +ensure_event_stream() { + have pactl || return 0 + local state="$HOME/.local/state/wireplumber/stream-properties" + if [ -f "$state" ] && ! grep -q 'media.role:Notification=.*"mute":true' "$state" \ + && ! grep -qE 'media.role:Notification=.*"volume":0\.[0-8]' "$state"; then + return 0 + fi + local f; f="$(ls "$HOME"/.local/share/sounds/portal2/stereo/bell.* 2>/dev/null | head -1)" + [ -n "$f" ] || return 0 + paplay --property=media.role=event --property=application.name=dot-sounds "$f" & local p=$! + local id="" + for _ in $(seq 20); do + id="$(pactl -f json list sink-inputs 2>/dev/null | jq -r '.[] | select(.properties["application.name"] == "dot-sounds") | .index' | head -1)" + [ -n "$id" ] && break; sleep 0.1 + done + if [ -n "$id" ]; then + pactl set-sink-input-mute "$id" 0 + pactl set-sink-input-volume "$id" 100% + ok "notification sounds stream unmuted at 100%" + else + warn "couldn't catch the event stream to unmute it; unmute 'Notification Sounds' in the volume applet" + fi + wait $p 2>/dev/null || true +} + play_event() { # freedesktop sound name if have canberra-gtk-play; then canberra-gtk-play -i "$1" -d "dot sounds test" 2>/dev/null @@ -433,16 +461,19 @@ cmd_sounds() { case "${1:-build}" in build) have build-sound-theme || die "build-sound-theme missing (full profile)" - shift || true; build-sound-theme "$@" ;; + shift || true; build-sound-theme "$@" + ensure_event_stream ;; test) # dot sounds test [event...] — plays each theme sound with its name sound_output_info + ensure_event_stream local events=("${@:2}") [ ${#events[@]} -gt 0 ] || mapfile -t events < <(ls "$HOME/.local/share/sounds/portal2/stereo" | sed 's/\.[^.]*$//' | sort) for e in "${events[@]}"; do printf ' ♪ %s\n' "$e"; play_event "$e"; sleep 0.4; done ;; notify) # A real desktop notification, the same path Chrome/Telegram/etc. take. sound_output_info + ensure_event_stream notify-send -a "dot sounds" "Aperture Science" "This was a triumph. (If you heard a ding, app notifications have sound.)" ;; *) die "usage: dot sounds [build|test [event...]|notify]" ;; esac diff --git a/config/konsole/Dark.profile b/config/konsole/Dark.profile index 441a8db..5d5ae9a 100644 --- a/config/konsole/Dark.profile +++ b/config/konsole/Dark.profile @@ -7,3 +7,6 @@ Command=zsh Environment=TERM=xterm-256color,COLORTERM=truecolor Name=Dark Parent=FALLBACK/ + +[Terminal Features] +BellMode=1 diff --git a/config/konsole/Light.profile b/config/konsole/Light.profile index bb3c61e..5048b4e 100644 --- a/config/konsole/Light.profile +++ b/config/konsole/Light.profile @@ -7,3 +7,6 @@ Command=zsh Environment=TERM=xterm-256color,COLORTERM=truecolor Name=Light Parent=FALLBACK/ + +[Terminal Features] +BellMode=1 diff --git a/config/konsole/etv.profile b/config/konsole/etv.profile index 7291a15..d7e67fb 100644 --- a/config/konsole/etv.profile +++ b/config/konsole/etv.profile @@ -7,3 +7,6 @@ Environment=TERM=xterm-256color,COLORTERM=truecolor InheritContainerContext=true Name=etv Parent=FALLBACK/ + +[Terminal Features] +BellMode=1 diff --git a/config/konsole/general.profile b/config/konsole/general.profile index c91cd45..cf0be8f 100644 --- a/config/konsole/general.profile +++ b/config/konsole/general.profile @@ -7,3 +7,6 @@ Environment=TERM=xterm-256color,COLORTERM=truecolor InheritContainerContext=false Name=general Parent=FALLBACK/ + +[Terminal Features] +BellMode=1 diff --git a/home/gui/sounds.nix b/home/gui/sounds.nix index 7625dfc..d1ad0af 100644 --- a/home/gui/sounds.nix +++ b/home/gui/sounds.nix @@ -1,5 +1,8 @@ -{ config, lib, ... }: +{ config, lib, pkgs, ... }: let + notification-sounds = pkgs.writeShellScriptBin "notification-sounds" '' + exec ${pkgs.python3}/bin/python -u ${../../scripts/notification-sounds.py} "$@" + ''; # KNotification reads per-user overrides from ~/.config/.notifyrc. Groups are # literally "[Event/]"; plasma-manager treats "/" as nesting, hence "Event\\/". # Sound names resolve through the active theme (portal2, see `dot sounds`), @@ -9,6 +12,31 @@ let sound = event "Sound"; in lib.mkIf config.dotfiles.gui { + home.packages = [ notification-sounds ]; + + # Sounds for app notifications that don't bring their own (see scripts/notification-sounds.py). + systemd.user.services.notification-sounds = { + Unit = { + Description = "Play a sound for desktop notifications without one"; + After = [ "graphical-session.target" ]; + PartOf = [ "graphical-session.target" ]; + }; + Service = { + ExecStart = "${notification-sounds}/bin/notification-sounds"; + Restart = "on-failure"; + RestartSec = 5; + Environment = "PATH=/usr/bin:/bin:${config.home.profileDirectory}/bin"; + }; + Install.WantedBy = [ "graphical-session.target" ]; + }; + xdg.configFile."autostart/notification-sounds.desktop".text = '' + [Desktop Entry] + Type=Application + Name=notification-sounds + Exec=sh -c '[ -d /run/systemd/system ] || exec ${notification-sounds}/bin/notification-sounds' + NoDisplay=true + ''; + programs.plasma.configFile = { kdeglobals.Sounds.Theme = "portal2"; @@ -16,7 +44,7 @@ lib.mkIf config.dotfiles.gui { plasmaparc.General.AudioFeedback = true; "plasma_workspace.notifyrc" = { - # Every app notification that has no notifyrc of its own (Chrome, Telegram, Vesktop, notify-send...). + # KDE-internal generic event; other apps' notifications are covered by notification-sounds. "Event\\/notification" = popupSound "message-new-instant"; "Event\\/startkde" = sound "desktop-login"; "Event\\/exitkde" = sound "desktop-logout"; diff --git a/scripts/notification-sounds.py b/scripts/notification-sounds.py new file mode 100644 index 0000000..06157dc --- /dev/null +++ b/scripts/notification-sounds.py @@ -0,0 +1,104 @@ +"""Give every desktop notification a sound. + +Plasma only plays a sound for a freedesktop notification when the sender asks +for one, so most apps (Chrome PWAs, notify-send, Electron apps) are silent. +This watches org.freedesktop.Notifications.Notify calls via `dbus-monitor` +(read-only) and plays the theme's message-new-instant sound unless: + * the sender is a KNotification (x-kde-eventId hint) - KDE plays those itself, + * the notification carries sound-file / sound-name / suppress-sound, + * the app plays its own sound (SKIP_APPS, override with NOTIFICATION_SOUNDS_SKIP). +""" + +import os +import re +import select +import subprocess +import time +from pathlib import Path + +SKIP_APPS = { + a.strip().lower() + for a in os.environ.get("NOTIFICATION_SOUNDS_SKIP", "Telegram Desktop,Telegram,vesktop,Vesktop,discord").split(",") + if a.strip() +} +SOUND = os.environ.get("NOTIFICATION_SOUNDS_EVENT", "message-new-instant") +OWN_SOUND_HINTS = ('"x-kde-eventId"', '"sound-file"', '"sound-name"', '"suppress-sound"') +MIN_GAP = 0.4 # seconds; a burst of notifications gets one sound + +last = 0.0 + + +def sound_file() -> str | None: + theme = subprocess.run( + ["kreadconfig6", "--file", "kdeglobals", "--group", "Sounds", "--key", "Theme", "--default", "ocean"], + capture_output=True, text=True, + ).stdout.strip() + for base in (Path.home() / ".local/share/sounds", Path("/usr/share/sounds")): + for t in (theme, "ocean", "freedesktop"): + hits = sorted((base / t / "stereo").glob(f"{SOUND}.*")) + if hits: + return str(hits[0]) + return None + + +def play() -> None: + global last + now = time.monotonic() + if now - last < MIN_GAP: + return + last = now + f = sound_file() + if f: + # Same "event" role stream as KDE's own sounds, so the Notification Sounds slider applies. + subprocess.Popen(["paplay", "--property=media.role=event", "--property=application.name=notification-sounds", f]) + + +def judge(block: list[str]) -> None: + """block = dbus-monitor lines of one Notify call: header, app_name, ..., hints dict.""" + strings = [m.group(1) for line in block if (m := re.match(r'\s*string "(.*)"$', line))] + if not strings: + return + app = strings[0] + text = "\n".join(block) + # suppress-sound may be present but false; only treat true as "has its own sound". + suppressed = re.search(r'"suppress-sound"\s*\n\s*variant\s+boolean true', text) + own = any(h in text for h in OWN_SOUND_HINTS[:3]) or suppressed + if not own and app.lower() not in SKIP_APPS: + print(f"ding for {app!r}", flush=True) + play() + + +def main() -> None: + mon = subprocess.Popen( + ["dbus-monitor", "--session", "type='method_call',interface='org.freedesktop.Notifications',member='Notify'"], + stdout=subprocess.PIPE, + ) + fd = mon.stdout.fileno() + print(f"listening; skipping {sorted(SKIP_APPS)}", flush=True) + pending = b"" + block: list[str] = [] + while True: + # Raw fd + select (no buffered readline, which would hide lines from select). + ready, _, _ = select.select([fd], [], [], 0.15) + if not ready: + if block: # quiet for 150 ms: the call is complete + judge(block) + block = [] + continue + chunk = os.read(fd, 65536) + if not chunk: + raise SystemExit(f"dbus-monitor exited ({mon.wait()})") + pending += chunk + *lines, pending = pending.split(b"\n") + for raw in lines: + line = raw.decode(errors="replace") + if line.startswith(("method call", "signal", "method return", "error")): + if block: + judge(block) + block = [line] if "member=Notify" in line else [] + elif block: + block.append(line) + + +if __name__ == "__main__": + main()