-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolybar_alsa_module
More file actions
executable file
·152 lines (133 loc) · 4.74 KB
/
polybar_alsa_module
File metadata and controls
executable file
·152 lines (133 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
# =================================================================== #
# DESCRIPTION:
# =================================================================== #
# This script is a polybar module.
# It allows the user to toggle between headphones and speakers
# without plugging/unplugging headphones. You can leave your
# headphones plugged in switch between the two ports by clicking
# on the icon for the module on your polybar.
# =================================================================== #
# CONFIGURATION
# =================================================================== #
# 4. Set `headphones` & `speakers` variable in script to the name
# of your headphone and speaker ports.
# The following will list all ports for your current sink, assuming
# only one sink is available:
#
# $(pacmd list | grep analog-output | awk '($1 !~ /active/) && (!x[$0]++) && gsub(":","") {print $1}')
#
# example:
#
# > $(pacmd list | grep analog-output | awk '($1 !~ /active/) && (!x[$0]++) && gsub(":","") {print $1}')
# analog-output-lineout
# analog-output-headphones
#
# You would set the folowing in this script:
# headphones="analog-output-headphones"
# speakers="analog-output-lineout"
# Although it's not completely relevant, if you'd like to see what your
# default port is:
# pacmd stat | awk -F": " '/^Default sink name: /{print $2}' | awk -F"." '{print $NF}'
# =================================================================== #
# USAGE
# =================================================================== #
# To use this script, include the following custom module definition
# to your polybar config file (replace values in brackets with your own):
# [module/{YOUR_MODULE_NAME}]
# type = custom/script
# exec = {PATH_TO_THIS_SCRIPT}/polybar_alsa_module
# tail = true
# interval = 0
# click-left = zsh -c "kill -SIGUSR1 %pid%"
# format-background = {COLOR_FOR_BACKGROUND_HEX}
# format-foreground = {COLOR_FOR_ICON_HEX}
# include the module in your bar section:
# example: placing 'alsa-switcher' module on the left side of a
# bar named 'main'
# [bar/main]
# ...
# modules-left = alsa-switcher
# ...
# More information on Polybar 'script' modules can be found here:
# https://github.com/jaagr/polybar/wiki/Module:-script
# =================================================================== #
# KNOWN BUGS:
# =================================================================== #
# 1:
# TLDR: Do not plug in your speakers into line out. Choose rear
# center speaker or something else.
# On one of my desktops the rear 'line out' jack and the front
# 'headphone' jack share the 'Front' alsa 'Simple Control'.
# This wouldn't be a problem except for...
# When you set the sink port using pacmd to either headphones or
# lineout, it will keep the Front port at 100% volume. You can
# confirm this using alsamixer. It will contiue to output audio
# through the speakers even when this script sets headphones as
# output.
headphones="analog-output-headphones"
speakers="analog-output-lineout"
listen() {
while true; do
muted=$(volume status | awk '/^muted/ {print $2}')
[[ $(dot sound port) == $speakers ]] && icon="" || icon=""
[[ $(volume status | awk '/^muted/ {print $2}') == true ]] && colors="%{ F#$mute_fg B#$mute_bg }" || colors="%{ F#$fg B#$bg }"
# the expra space on the left acts as a pad-left: 1
echo "$padleft$colors $icon"
sleep 0.5 &
wait
done
}
switch() {
sink_id=$(pacmd list | grep '*' | awk 'NR==1{print $3}')
current_port=$(dot sound port)
if [[ $current_port == $speakers ]]; then
vol=$(volume get)
pacmd set-sink-port $sink_id $headphones > /dev/null 2>&1
dot sound port $headphones
volume set $vol
amixer sset Front 0 1&2>/dev/null
# set front channel to 0 volume, because muting it mutes headphones as well...
# annoying bug with alsa..
elif [[ $current_port == $headphones ]]; then
vol=$(volume get)
pacmd set-sink-port $sink_id $speakers > /dev/null 2>&1
dot sound port $speakers
volume set $vol
else
echo 'ERROR: $current_port is '$current_port" expected either $headphones or $speakers"
exit 1
fi
exit 0
}
while test $# -gt 0; do
case "$1" in
--switch)
switch;;
--mutefg)
mute_fg=$2
shift;;
--mutebg)
mute_bg=$2
shift;;
--bg)
bg=$2
shift;;
--fg)
fg=$2
shift;;
--padleft)
padleft=$2
shift;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1;;
*)
break;;
esac
shift
done
if [[ -z $padleft ]]; then
padleft=""
fi
listen