scripts: rewrite volumecontrol script to sh

More portable than luajit I guess (no need to install anything to run).
Also stop templating `fzmp` script.
This commit is contained in:
Hoang Nguyen 2024-02-08 00:00:00 +07:00
parent 55dc973260
commit ee3f623095
Signed by: folliehiyuki
GPG Key ID: B0567C20730E9B11
3 changed files with 54 additions and 92 deletions

View File

@ -1,7 +1,6 @@
#!/bin/bash
export MPD_HOST="{{ ansible_env.XDG_RUNTIME_DIR }}/mpd.sock"
{%- raw %}
export MPD_HOST="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/mpd.sock"
declare -r config_file="${XDG_CONFIG_HOME:-$HOME/.config}/fzmp/conf"
usage() {
@ -413,4 +412,3 @@ is_running mpd || [[ -v MPD_HOST ]] || die "can't connect to mpd"
tput smcup
"$default_filter"
{%- endraw %}

View File

@ -1,87 +1,60 @@
#!/usr/bin/luajit
#!/bin/sh -e
local fmt = string.format
local argv = table.pack(...)
local icon_dir = os.getenv('XDG_DATA_HOME') .. '/icons/notifications/'
local volume_step = 0.05
icon_dir="${XDG_DATA_HOME:-$HOME/.local/share}/icons/notifications"
volume_step=0.05
local function print_help()
io.stdout:write(fmt([[
Usage: volumecontrol [--increase|--decrease|--toggle-source|--toggle]
--increase : Increase volume of default sink by %s
--decrease : Decrease volume of default sink by %s
--toggle : Toggle mute state of default sink
--toggle-source: Toggle mute state of default source
]], volume_step, volume_step))
end
_notify_volume() {
volume="$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk '{print $2*100}')"
notify-send -u normal -t 1000 -a "Volume control" \
-h string:synchronous:audio \
-h string:x-canonical-private-synchronous:audio \
-h int:value:"$volume" \
-i "${icon_dir}/speaker.png" \
"Volume" "${volume}%"
}
if #argv == 0 then
print_help()
return
end
_notify_mute() {
if ! (printf "SINK\nSOURCE" | grep -q -F "$1"); then
return 1
fi
local action = argv[1]:match('%-%-(.+)')
local options = { 'toggle', 'toggle-source', 'increase', 'decrease' }
status="$(wpctl get-volume @DEFAULT_AUDIO_"$1"@ | awk '{print $2*100 ":" $3}')"
volume="$(printf "%d" "${status%%:*}")"
local function findValue(tbl, value)
for _, v in pairs(tbl) do
if v == value then
return true
end
end
return false
end
class="$([ "$1" = "SINK" ] && printf "Sound" || printf "Microphone")"
icon="$([ "$1" = "SINK" ] && printf "speaker.png" || printf "microphone.png")"
state="$([ "${status##*:}" = "[MUTED]" ] && printf "muted" || printf "unmuted")"
if not findValue(options, action) then
print_help()
return
end
notify-send -u normal -t 2000 \
-i "${icon_dir}/${icon}" \
"${class} ${state}" "Volume: ${volume}%"
}
local function read_output(command)
-- No buffering to get all output at once
io.stdout:setvbuf('no')
local file = assert(io.popen(command, 'r'))
file:flush() -- receive all the output immediately (in case the output is big)
local output = file:read('a')
file:close()
return output
end
if action == 'toggle' or action == 'toggle-source' then
local target = action == 'toggle' and 'SINK' or 'SOURCE'
local ok = os.execute('/usr/bin/wpctl set-mute @DEFAULT_AUDIO_' .. target .. '@ toggle')
if ok then
local status = read_output('/usr/bin/wpctl get-volume @DEFAULT_AUDIO_' .. target .. '@')
local class = action == 'toggle' and 'Sound' or 'Microphone'
-- Round the volume to 2 decimal numbers
local volume = tonumber(fmt('%.2f', status:match('Volume: (%S+)'))) * 100
-- Either nil or the string 'MUTED' is returned
local mute = status:match('Volume: %S+ %[(%S+)%]')
if mute == 'MUTED' then
local icon = icon_dir .. (action == 'toggle' and 'mute.png' or 'microphone_mute.png')
os.execute('/usr/bin/notify-send -u normal -t 2000 -i ' ..
icon .. ' "' .. class .. ' muted" "Volume: ' .. volume .. '%"')
else
local icon = icon_dir .. (action == 'toggle' and 'speaker.png' or 'microphone.png')
os.execute('/usr/bin/notify-send -u normal -t 2000 -i ' ..
icon .. ' "' .. class .. ' unmuted" "Volume: ' .. volume .. '%"')
end
end
else
local direction = action == 'increase' and '+' or '-'
local ok = os.execute('/usr/bin/wpctl set-volume @DEFAULT_AUDIO_SINK@ ' .. volume_step .. direction)
if ok then
local status = read_output('/usr/bin/wpctl get-volume @DEFAULT_AUDIO_SINK@')
local volume = tonumber(fmt('%.2f', status:match('Volume: (%S+)'))) * 100
os.execute('/usr/bin/notify-send -u normal -t 1000 -a "Volume control" ' ..
'-h string:synchronous:audio ' ..
'-h string:x-canonical-private-synchronous:audio ' ..
'-h int:value:' .. volume .. ' -i ' .. icon_dir .. 'speaker.png ' ..
'"Volume" "' .. volume .. '%"')
end
end
case "$1" in
--toggle)
wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle
_notify_mute SINK
;;
--toggle-source)
wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle
_notify_mute SOURCE
;;
--increase)
wpctl set-volume @DEFAULT_AUDIO_SINK@ "${volume_step}+"
_notify_volume
;;
--decrease)
wpctl set-volume @DEFAULT_AUDIO_SINK@ "${volume_step}-"
_notify_volume
;;
*)
cat <<-EOF
Usage: volumecontrol [--increase|--decrease|--toggle|--toggle-source]
--increase : Increase volume of default sink by $volume_step
--decrease : Decrease volume of default sink by $volume_step
--toggle : Toggle mute state of default sink
--toggle-source: Toggle mute state of default source
EOF
exit 1
;;
esac

View File

@ -14,15 +14,6 @@
dest: '{{ xdg_dir.bin_home }}/'
mode: '755'
- name: Copy templates to XDG_BIN_HOME
template:
src: '{{ item }}.j2'
dest: '{{ xdg_dir.bin_home }}/{{ item }}'
force: true
mode: '755'
loop:
- fzmp
- name: Synchronize libexec
ansible.posix.synchronize:
src: libexec/