From 09cdbaeb777293d26387499f16fbc66a823c3f53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Eug=C3=AAnio?= Date: Tue, 2 Aug 2022 00:07:04 -0300 Subject: [PATCH] add volume changing script --- user/home.nix | 14 ++++- user/scripts/volumesh | 131 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100755 user/scripts/volumesh diff --git a/user/home.nix b/user/home.nix index 54f6c8b..46ee716 100644 --- a/user/home.nix +++ b/user/home.nix @@ -155,6 +155,7 @@ let # vim: ft=fish ''; + volumesh = pkgs.writeShellScriptBin "volumesh" (builtins.readFile ./scripts/volumesh); in { # Home Manager needs a bit of information about you and the # paths it should manage. @@ -185,6 +186,9 @@ in { pass dhist bmenu + volumesh + pamixer + libnotify # media yt-dlp ffmpeg @@ -340,7 +344,10 @@ in { keys.insert = { "A-k" = "normal_mode"; }; }; }; - home.sessionVariables = { EDITOR = "hx"; }; + home.sessionVariables = { + EDITOR = "hx"; + VOLUME_CHANGE_SOUND = "${pkgs.sound-theme-freedesktop}/share/sounds/freedesktop/stereo/audio-volume-change.oga"; + }; programs.firefox = { enable = true; package = pkgs.firefox; @@ -598,7 +605,10 @@ in { }; modes = let return_mode = lib.mapAttrs (k: v: "${v}; mode default"); in { - audio = return_mode { + audio = { + ${key.tabL} = "volumes decrease"; + } // return_mode { + "space" = "exec mpc toggle"; "escape" = ""; "s" = "exec ${pulse_sink}/bin/pulse_sink"; }; diff --git a/user/scripts/volumesh b/user/scripts/volumesh new file mode 100755 index 0000000..86726c1 --- /dev/null +++ b/user/scripts/volumesh @@ -0,0 +1,131 @@ +#!/bin/sh + +# depends on: awk, pactl, pacmd, notify-send + +MAX_VOL=150 +STEP=10 + +notify() { + volume=$(get_vol_$TARGET) + + if is_muted_$TARGET; then + s="Muted" + else + s="Volume" + fi + + s=$(echo "${TARGET} ${s}" | sed 's/^\(.\)/\U\1/') + + notify-send "${s}" "${volume}%" \ + --app-name=volumesh \ + --hint=int:value:"$volume" + +} + +round() { + awk '{ + print int($1/'$STEP')*'$STEP'; + }' +} + +round_vol() { + rounded=$(get_vol_$TARGET | round) + newvol=$(min $MAX_VOL $rounded) +} + +min() { + printf '%i\n' ${@} | sort -n | head -n1 +} + +# Pulse{{{ +get_vol_pulse() { + pamixer --get-volume +} + +is_muted_pulse() { + pamixer --get-mute >/dev/null +} + +change_vol_pulse() { + pamixer "-$1" "$(min 120 $2)" + round_vol + pamixer --set-volume "${newvol}" + if + test -n "$VOLUME_CHANGE_SOUND" + then + paplay "$VOLUME_CHANGE_SOUND" + fi +} + +toggle_mute_pulse() { + pactl set-sink-mute @DEFAULT_SINK@ toggle +} +#}}} +# Mpd {{{ + +get_vol_mpd() { + env LC_ALL=C mpc vol | + sed -e 's/^.*://g' -e 's/%.*$//g' -e 's/ //g' +} + +is_muted_mpd() { + env LC_ALL=C mpc status | grep '\[paused\]' 1>/dev/null +} +change_vol_mpd() { + mpc vol "${1}${2}" &>/dev/null + round_vol + mpc vol "${newvol}" &>/dev/null +} + +toggle_mute_mpd() { + mpc toggle +} +#}}} +usage() { + local CNAME=$(basename $0) + echo "${CNAME} [-m][-di ]" + echo "${CNAME} [-m][-t]" + echo "" + echo "Options:" + echo " -m --mpd Target mpd instead of PulseAudio" + echo " -i --increase of volume to increase" + echo " -d --decrease of volume to decrease" + echo " -t --toggle Mute/Unmute target" + echo " -h --help Show This help message" + + exit "$1" +} +TARGET=pulse + +while [ $# -gt 0 ]; do + case $1 in + -m | --mpd) + TARGET=mpd + shift + ;; + -i | --increase) + shift + change_vol_$TARGET i $1 + shift + ;; + -d | --decrease) + shift + change_vol_$TARGET d $1 + shift + ;; + -t | --toggle) + toggle_mute_$TARGET + shift + ;; + -h | --help) + usage 0 + ;; + *) + usage 1 + ;; + esac +done + +notify + +# vim: fdm=marker