-
-
Notifications
You must be signed in to change notification settings - Fork 590
feat: wl-togglesink to mute/unmute active-window. #1598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IvyProtocol
wants to merge
14
commits into
HyDE-Project:dev
Choose a base branch
from
IvyProtocol:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−0
Open
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
051d337
Introducing wl-togglesink to mute/unmute active-window.
IvyProtocol 3dd12cb
Update keybindings.conf
IvyProtocol 6b3b3d6
Update wl-togglesink.sh
IvyProtocol 3dd62d6
Update Configs/.local/lib/hyde/wl-togglesink.sh
IvyProtocol e3afb68
Rehencing the script.
IvyProtocol 437cd76
Update wl-togglesink.sh
IvyProtocol c6236bd
Update wl-togglesink.sh
IvyProtocol be3a4be
Update wl-togglesink.sh
IvyProtocol 056b876
Update wl-togglesink.sh
IvyProtocol f23d9a4
Update wl-togglesink.sh
IvyProtocol b310e34
Update wl-togglesink.sh
IvyProtocol b695f90
Update Configs/.local/lib/hyde/wl-togglesink.sh
IvyProtocol 70f0130
Update wl-togglesink.sh
IvyProtocol e05ba07
Merge branch 'HyDE-Project:dev' into dev
IvyProtocol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| #!/usr/bin/env bash | ||
| set -eo pipefail | ||
|
|
||
| scrDir="$(dirname "$(realpath "${0}")")" | ||
| source "${scrDir}/globalcontrol.sh" | ||
| [[ -n "${iconsDir}" ]] || { echo "globalcontrol.sh did not set iconsDir" >&2; iconsDir="${XDG_DATA_HOME}/icons"; } | ||
| dunstDir="${iconsDir}/Wallbash-Icon" | ||
|
|
||
| #// Credits to sl1ng for the orginal script. Rewritten by Vyle. | ||
| ctlcheck=("pactl" "jq" "notify-send" "awk" "pgrep" "hyprctl" "iconv") | ||
| missing=() | ||
|
|
||
| for ctl in "${ctlcheck[@]}"; do | ||
| command -v "${ctl}" >/dev/null || missing+=("${ctl}") | ||
| done | ||
|
|
||
| if (( ${#missing[@]} )); then | ||
| if printf '%s\n' "${missing[@]}" | grep -qx "pactl"; then | ||
| printf '%s\n' "${missing[@]}" | grep -qx "notify-send" || notify-send -a "t1" -r 91190 -t 2000 -i "${dunstDir}/hyprdots.svg" "Pactl Not Installed!" | ||
| fi | ||
| echo "Missing required dependencies: \"${missing[*]}\"" >&2 | ||
| exit 1 | ||
IvyProtocol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fi | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #// Parse .pid, .class, .title to __pid, __class, __title. | ||
| active_json="$(hyprctl -j activewindow 2>/dev/null)" || { echo "Did hyprctl fail to run? [EXIT-CODE:-1]" >&2; exit 1; } | ||
| PID="$(jq -r '"\(.pid)\t\(.class)\t\(.title)\t\(.initialTitle)"' <<< "${active_json}")" || { echo "Did jq fail to run? [EXIT-CODE:-1]" >&2; exit 1; } | ||
|
|
||
| IFS=$'\t' read -r __pid __class __title __initialTitle <<< "${PID}" | ||
|
|
||
| [[ -z "${__pid}" || "${__pid}" == "null" || "${__pid}" -le 0 ]] 2>/dev/null && { echo "Could not resolve PID for focused window." >&2; exit 1; } | ||
| sink_json="$(pactl -f json list sink-inputs 2>/dev/null | iconv -f utf-8 -t utf-8 -c)" || { echo "Did pactl or iconv fail to run? Required manual intervention." >&2; exit 1; } | ||
|
|
||
| #// Collect all descendant PIDs for the active window (Chrome/Wayland audio often runs in child processes). | ||
| declare -A seen_pids=() | ||
| queue=("${__pid}") | ||
| all_pids=() | ||
| while ((${#queue[@]})); do | ||
| pid="${queue[0]}" | ||
| queue=("${queue[@]:1}") | ||
| [[ -n "${seen_pids[$pid]:-}" ]] && continue | ||
| seen_pids["$pid"]=1 | ||
| all_pids+=("$pid") | ||
| mapfile -t children < <(pgrep -P "$pid" || true) | ||
| for child in "${children[@]}"; do | ||
| [[ -n "${seen_pids[$child]:-}" ]] || queue+=("$child") | ||
| done | ||
| done | ||
| idsJson="$(printf '%s\n' "${all_pids[@]}" | jq -s 'map(tonumber)')" | ||
|
|
||
| #// Check if any descendant PID matches application.process.id or else verify other statements. | ||
| +mapfile -t sink_ids < <(jq -r --argjson pids "${idsJson}" --arg class "${__class}" --arg title "${__title}" ' | ||
| .[] | | ||
| def lc(x): (x // "" | ascii_downcase); | ||
| def normalize(x): x | gsub("[-_~.]+";" ") ; | ||
| select( | ||
| (.properties["application.process.id"] | tostring | tonumber? as $p | $p != null and ($pids | index($p) != null)) | ||
| or | ||
| ($class != "" and (lc(.properties["application.name"]) | contains(lc($class)))) | ||
| or | ||
| ($class != "" and (lc(.properties["application.id"]) | contains(lc($class)))) | ||
| or | ||
| ($class != "" and (lc(.properties["application.process.binary"]) | contains(lc($class)))) | ||
| or | ||
| ($title != "" and (normalize(lc(.properties["media.name"])) | contains(normalize(lc($title))))) | ||
| ) | .index' <<< "${sink_json}" | ||
| ) | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if [[ "${#sink_ids[@]}" -eq 0 ]]; then | ||
| mapfile -t fallback_pids < <(pgrep -x "${__class}" || true) | ||
| if [[ "${#fallback_pids[@]}" -gt 0 ]]; then | ||
| declare -A seen_fallback=() | ||
| queue=("${fallback_pids[@]}") | ||
| all_fallback=() | ||
| while ((${#queue[@]})); do | ||
| pid="${queue[0]}" | ||
| queue=("${queue[@]:1}") | ||
| [[ -n "${seen_fallback[$pid]:-}" ]] && continue | ||
| seen_fallback["$pid"]=1 | ||
| all_fallback+=("$pid") | ||
| mapfile -t children < <(pgrep -P "$pid" || true) | ||
| for child in "${children[@]}"; do | ||
| [[ -n "${seen_fallback[$child]:-}" ]] || queue+=("$child") | ||
| done | ||
| done | ||
| fallbackJson="$(printf '%s\n' "${all_fallback[@]}" | jq -s 'map(tonumber)')" | ||
| mapfile -t sink_ids < <( jq -r --argjson pids "${fallbackJson}" '.[] | | ||
| select((.properties["application.process.id"] | tostring | tonumber? as $p | $p != null and ($pids | index($p)))) | .index' <<< "${sink_json}" ) | ||
| fi | ||
IvyProtocol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fi | ||
|
|
||
| #// Auto-Detect if the environment is on Hyprland or $HYPRLAND_INSTANCE_SIGNATURE. | ||
| if [[ ${#sink_ids[@]} -eq 0 ]]; then | ||
| if [[ -n "${HYPRLAND_INSTANCE_SIGNATURE}" ]]; then | ||
| # If even the fallback_pid remains empty, we will dispatch exit code based on $HYPRLAND_INSTANCE_SIGNATURE. | ||
| notify-send -a "t1" -r 91190 -t 1200 -i "${dunstDir}/hyprdots.svg" "No sink input available." | ||
| echo "No sink input for focused window: ${__class}" >&2 | ||
| exit 1 | ||
| else | ||
| echo "No sink input for focused active_window ${__class}" >&2 | ||
| exit 1 | ||
| fi | ||
IvyProtocol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fi | ||
|
|
||
| idsJson=$(printf '%s\n' "${sink_ids[@]}" | jq -s 'map(tonumber)') | ||
|
|
||
| #// Get the available option from pactl. | ||
| want_mute=$(jq -r --argjson ids "$idsJson" ' | ||
| [ .[] | select(.index as $i | $ids | index($i)) | .mute ] as $m | | ||
| if all($m[]; . == true) then "no" | ||
| else "yes" | ||
| end' <<< "${sink_json}" | ||
| ) | ||
|
|
||
| if [[ "${want_mute}" == "no" ]]; then | ||
| state_msg="Unmuted" | ||
| swayIcon="${dunstDir}/media/unmuted-speaker.svg" | ||
| else | ||
| state_msg="Muted" | ||
| swayIcon="${dunstDir}/media/muted-speaker.svg" | ||
| fi | ||
|
|
||
| [[ -f "${swayIcon}" ]] || { echo -e "Missing swaync icons." >&2; swayIcon=""; } | ||
|
|
||
| errors=0 | ||
| for id in "${sink_ids[@]}"; do | ||
| pactl set-sink-input-mute "$id" "$want_mute" || ((++errors)) | ||
| done | ||
| if ((errors)); then | ||
| echo -e "pactl failed to set \"${id}\" to be \"${state_msg}\"! Manual intervention required." >&2 | ||
| notify-send -a "t1" -r 91190 -t 1200 -i "${dunstDir}/hyprdots.svg" "Failed to set \"${id}\" to be \"${state_msg}\"!" | ||
| else | ||
| if ((errors)); then | ||
| echo -e "pactl failed to set \"${id}\" to be \"${state_msg}\"! Manual intervention required." >&2 | ||
| notify-send -a "t1" -r 91190 -t 1200 -i "${dunstDir}/hyprdots.svg" "Failed to set \"${id}\" to be \"${state_msg}\"!" | ||
| else | ||
| # // Append paxmier to get a nice result. Pamixer is complete optional here. | ||
| if command -v pamixer >/dev/null; then | ||
| notify-send -a "t2" -r 91190 -t 800 -i "${swayIcon}" "${state_msg} ${__initialTitle}" "$(pamixer --get-default-sink | awk -F '"' 'END{print $(NF - 1)}' || true)" | ||
| else | ||
| notify-send -a "t2" -r 91190 -t 800 -i "${swayIcon}" "${state_msg} ${__initialTitle}" | ||
| fi | ||
| fi | ||
IvyProtocol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.