added pixel buds control

This commit is contained in:
2025-07-23 20:58:55 +02:00
parent 23e2f7492e
commit 97b5d87d30
4 changed files with 165 additions and 3 deletions

View File

@@ -5,7 +5,7 @@
"height": 35,
"modules-left": ["hyprland/workspaces", "hyprland/window"],
"modules-center": [],
"modules-right": ["tray", "wireplumber", "network", "cpu", "memory", "custom/tlp", "clock", "custom/power"],
"modules-right": ["tray", "wireplumber", "custom/pixelbuds", "custom/anc", "network", "cpu", "memory", "custom/tlp", "clock", "custom/power"],
"hyprland/workspaces": {
"format": "{icon}",
@@ -72,7 +72,7 @@
"format": "{}",
"exec": "~/.config/waybar/scripts/tlp-profile.sh",
"return-type": "json",
"interval": 10
"interval": 5
},
"battery": {
"states": {
@@ -86,4 +86,19 @@
"format-full": "{capacity}%",
"format-icons": ["", "", "", "", ""]
},
"custom/pixelbuds": {
"format": "{}",
"return-type": "json",
"exec": "~/.config/waybar/scripts/pixelbuds.sh",
"interval": 5, // Check status every 30 seconds
"on-click": "bluetoothctl disconnect B4:23:A2:09:D3:53" // Optional: opens Bluetooth manager on click
},
"custom/anc": {
"format": "{}",
"return-type": "json",
"exec": "~/.config/waybar/scripts/anc_control.sh",
"interval": 5, // Check status every 5 seconds
"on-click": "~/.config/waybar/scripts/anc_control.sh off", // Left-click
"on-click-right": "~/.config/waybar/scripts/anc_control.sh cycle" // Right-click
}
}

73
waybar/scripts/anc_control.sh Executable file
View File

@@ -0,0 +1,73 @@
#!/bin/bash
# --- CONFIGURATION ---
# Your Pixel Buds Pro's MAC Address
MAC_ADDRESS="B4:23:A2:09:D3:53"
# --- END CONFIGURATION ---
# First, check if the device is connected using bluetoothctl.
if bluetoothctl info "$MAC_ADDRESS" | grep -q "Connected: yes"; then
# --- DEVICE IS CONNECTED ---
# This function gets the current ANC status and formats it for Waybar.
get_status() {
# Get the mode from pbpctrl. Redirect errors to null in case of a temporary glitch.
current_mode=$(pbpctrl get anc 2>/dev/null)
# Fallback: If pbpctrl fails or returns 'unknown', hide the module.
# This handles cases where buds are connected but not fully ready.
if [[ $? -ne 0 || "$current_mode" == "unknown (0)"* || -z "$current_mode" ]]; then
echo "{}"
exit 0
fi
# Set icon and tooltip based on the current mode.
case "$current_mode" in
"active")
icon="ANC: Active"
class="anc-active"
;;
"aware")
icon="ANC: Aware"
class="anc-aware"
;;
"off")
icon="ANC: Off"
class="anc-off"
;;
*)
# Failsafe to hide the module if the mode is unrecognized.
echo "{}"
exit 0
;;
esac
echo "{\"text\":\"$icon\", \"class\":\"$class\"}"
}
# Handle click actions passed from Waybar.
case "$1" in
# Right-click: Cycle between active and aware modes.
cycle)
current_mode=$(pbpctrl get anc 2>/dev/null)
if [[ "$current_mode" == "active" ]]; then
pbpctrl set anc aware
else
pbpctrl set anc active
fi
sleep 0.1 # Brief pause for the state to update.
;;
# Left-click: Turn ANC off.
off)
pbpctrl set anc off
sleep 0.1 # Brief pause for the state to update.
;;
esac
# Always display the current status after any action or on a scheduled interval.
get_status
else
# --- DEVICE IS NOT CONNECTED ---
# Output an empty JSON object to completely hide the Waybar module.
echo "{}"
fi

49
waybar/scripts/pixelbuds.sh Executable file
View File

@@ -0,0 +1,49 @@
#!/bin/bash
# --- CONFIGURATION ---
# Your Pixel Buds Pro's MAC Address
MAC_ADDRESS="B4:23:A2:09:D3:53"
# --- END CONFIGURATION ---
# Check if the device is connected using bluetoothctl.
if bluetoothctl info "$MAC_ADDRESS" | grep -q "Connected: yes"; then
# If connected, get battery info from pbpctrl.
if battery_output=$(pbpctrl show battery); then
# Use awk to grab the third field, which is the percentage or "unknown".
left_bud=$(echo "$battery_output" | grep "left bud:" | awk '{print $3}')
right_bud=$(echo "$battery_output" | grep "right bud:" | awk '{print $3}')
# If both buds are unknown (e.g., case is closed and buds are out of range),
# or if the command output is empty, hide the module.
if ([[ "$left_bud" == "unknown" ]] && [[ "$right_bud" == "unknown" ]]) || \
[[ -z "$left_bud" && -z "$right_bud" ]]; then
echo "{}"
exit 0
fi
# Prepare the display string for the left bud.
if [[ "$left_bud" == "unknown" ]]; then
left_display="L: ---"
else
left_display="L: $left_bud"
fi
# Prepare the display string for the right bud.
if [[ "$right_bud" == "unknown" ]]; then
right_display="R: ---"
else
right_display="R: $right_bud"
fi
# Format the final output for Waybar as JSON.
printf '{"text": "%s | %s", "tooltip": "Pixel Buds Pro 2", "class": "connected"}\n' "$left_display" "$right_display"
else
# pbpctrl failed to run, so hide the module.
echo "{}"
fi
else
# Not connected, output an empty JSON object to hide the module.
echo "{}"
fi

View File

@@ -63,7 +63,9 @@ window#waybar.hidden {
#wireplumber,
#network,
#clock,
#tray {
#tray,
#custom-pixelbuds,
#custom-anc {
border-radius: 4px;
margin: 6px 3px;
padding: 6px 12px;
@@ -71,6 +73,24 @@ window#waybar.hidden {
color: @text;
}
#custom-anc.anc-active {
color: @sky;
border-bottom: 3px solid @sky;
}
#custom-anc.anc-aware {
color: @green;
border-bottom: 3px solid @green;
}
#custom-anc.anc-off {
color: @text;
border-bottom: 3px solid @text;
}
#custom-anc.disconnected {
}
#custom-power {
color: @red;
padding: 6px 12px 6px 10px;
@@ -151,3 +171,8 @@ tooltip label {
padding: 5px;
background-color: @base;
}
#custom-pixelbuds.connected {
color: @teal; /* A pleasant green */
border-bottom: 3px solid @teal;
}