updated waybar script to not crash with my usb c hub
This commit is contained in:
@@ -5,7 +5,8 @@ DESCRIPTION=$(pactl list sources | grep -A2 "Name: $DEFAULT_SOURCE" | grep "Desc
|
||||
|
||||
case $1 in
|
||||
cycle)
|
||||
SOURCES=($(pactl list short sources | awk '{print $2}'))
|
||||
# Filter out monitor sources
|
||||
SOURCES=($(pactl list short sources | awk '{print $2}' | grep -v ".monitor"))
|
||||
NUM_SOURCES=${#SOURCES[@]}
|
||||
CURRENT_SOURCE=$(pactl info | grep 'Default Source' | cut -d ' ' -f3)
|
||||
|
||||
@@ -16,6 +17,10 @@ case $1 in
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
# If current source was a monitor or not in list, just pick the first one
|
||||
if [ $NUM_SOURCES -gt 0 ]; then
|
||||
pactl set-default-source "${SOURCES[0]}"
|
||||
fi
|
||||
;;
|
||||
show)
|
||||
TEXT=$(echo "$DESCRIPTION" | cut -c -20)
|
||||
@@ -35,6 +40,6 @@ case $1 in
|
||||
printf '{"text": "%s", "tooltip": "%s", "class": "%s"}' "$TEXT" "$DESCRIPTION" "$CLASS"
|
||||
;;
|
||||
*)
|
||||
echo "usage audio.sh {cycle|show}"
|
||||
echo "usage cycle_input.sh {cycle|show}"
|
||||
;;
|
||||
esac
|
||||
|
||||
37
waybar/scripts/volume.sh
Executable file
37
waybar/scripts/volume.sh
Executable file
@@ -0,0 +1,37 @@
|
||||
#!/bin/bash
|
||||
|
||||
TYPE=$1 # "mic" for source, empty for sink
|
||||
|
||||
if [ "$TYPE" == "mic" ]; then
|
||||
TARGET="@DEFAULT_AUDIO_SOURCE@"
|
||||
else
|
||||
TARGET="@DEFAULT_AUDIO_SINK@"
|
||||
fi
|
||||
|
||||
# Get volume and mute status from wpctl
|
||||
OUTPUT=$(wpctl get-volume $TARGET)
|
||||
VOLUME_RAW=$(echo "$OUTPUT" | awk '{print $2}')
|
||||
VOLUME=$(echo "$VOLUME_RAW * 100 / 1" | bc)
|
||||
MUTE=$(echo "$OUTPUT" | grep -c "MUTED")
|
||||
|
||||
if [ "$MUTE" -eq 1 ]; then
|
||||
if [ "$TYPE" == "mic" ]; then
|
||||
ICON=""
|
||||
else
|
||||
ICON=""
|
||||
fi
|
||||
printf '{"text": "%s", "class": "muted", "percentage": %d}' "$ICON" "$VOLUME"
|
||||
else
|
||||
if [ "$TYPE" == "mic" ]; then
|
||||
ICON=""
|
||||
else
|
||||
if [ "$VOLUME" -le 30 ]; then
|
||||
ICON=""
|
||||
elif [ "$VOLUME" -le 60 ]; then
|
||||
ICON=""
|
||||
else
|
||||
ICON=""
|
||||
fi
|
||||
fi
|
||||
printf '{"text": "%d%% %s", "class": "unmuted", "percentage": %d}' "$VOLUME" "$ICON" "$VOLUME"
|
||||
fi
|
||||
64
waybar/scripts/volume_combined.py
Normal file
64
waybar/scripts/volume_combined.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import subprocess
|
||||
import json
|
||||
import sys
|
||||
|
||||
def get_wpctl_status(target):
|
||||
try:
|
||||
output = subprocess.check_output(["wpctl", "get-volume", target], text=True)
|
||||
parts = output.strip().split()
|
||||
if len(parts) < 2:
|
||||
return 0, False
|
||||
vol = int(float(parts[1]) * 100)
|
||||
muted = "[MUTED]" in output
|
||||
return vol, muted
|
||||
except:
|
||||
return 0, False
|
||||
|
||||
def get_pactl_description(target_type):
|
||||
try:
|
||||
cmd_info = ["pactl", "info"]
|
||||
info = subprocess.check_output(cmd_info, text=True)
|
||||
search_key = "Default Sink" if target_type == "sink" else "Default Source"
|
||||
default_dev = next(line.split(": ")[1] for line in info.splitlines() if search_key in line)
|
||||
|
||||
cmd_list = ["pactl", "list", "sinks" if target_type == "sink" else "sources"]
|
||||
output = subprocess.check_output(cmd_list, text=True)
|
||||
|
||||
blocks = output.split("\n\n")
|
||||
for block in blocks:
|
||||
if f"Name: {default_dev}" in block:
|
||||
for line in block.splitlines():
|
||||
if "Description:" in line:
|
||||
desc = line.split(": ")[1].strip()
|
||||
# Add a small hint if it is a monitor, though cycle_input should prevent it
|
||||
if ".monitor" in default_dev:
|
||||
return "Monitor: " + desc[:11]
|
||||
return desc[:20]
|
||||
except:
|
||||
pass
|
||||
return "Unknown"
|
||||
|
||||
try:
|
||||
target_type = sys.argv[1] if len(sys.argv) > 1 else "sink"
|
||||
target = "@DEFAULT_AUDIO_SINK@" if target_type == "sink" else "@DEFAULT_AUDIO_SOURCE@"
|
||||
|
||||
vol, muted = get_wpctl_status(target)
|
||||
name = get_pactl_description(target_type)
|
||||
|
||||
if muted:
|
||||
icon = "" if target_type == "sink" else ""
|
||||
text = f"{name} {icon}"
|
||||
cls = "muted"
|
||||
else:
|
||||
if target_type == "sink":
|
||||
if vol <= 30: icon = ""
|
||||
elif vol <= 60: icon = ""
|
||||
else: icon = ""
|
||||
else:
|
||||
icon = ""
|
||||
text = f"{name} {vol}% {icon}"
|
||||
cls = "unmuted"
|
||||
|
||||
print(json.dumps({"text": text, "class": cls, "percentage": vol}))
|
||||
except Exception as e:
|
||||
print(json.dumps({"text": "Error", "class": "error"}))
|
||||
Reference in New Issue
Block a user