diff --git a/rofi-pulse-sink.sh b/rofi-pulse-sink.sh new file mode 100755 index 0000000..180f476 --- /dev/null +++ b/rofi-pulse-sink.sh @@ -0,0 +1,285 @@ +#!/bin/sh +# +# Purpose {{{ +# This script will … +# 1. … +# … +# +# 2021-11-27 +# }}} +# Vars {{{ +readonly PROGNAME=$(basename "${0}") +readonly PROGDIR=$(readlink -m $(dirname "${0}")) +readonly ARGS="${*}" +readonly NBARGS="${#}" +[ -z "${DEBUG}" ] && DEBUG=1 +## Export DEBUG for sub-script +export DEBUG + +# Default values for some vars +readonly MY_VAR_XY_DEFAULT="666" + +## Colors +readonly PURPLE='\033[1;35m' +readonly RED='\033[0;31m' +readonly RESET='\033[0m' +readonly COLOR_DEBUG="${PURPLE}" +## Rofi colors +readonly BLACK="#000000" +readonly BLUE="#0094cc" +# }}} +usage() { # {{{ + + cat <<- EOF +usage: $PROGNAME [-d|-h] + +Try to give a description… + +EXAMPLES : + - Apply my script to… + ${PROGNAME} + +OPTIONS : + -d,--debug + Enable debug messages. + + -h,--help + Print this help message. + + EOF + +} +# }}} +debug_message() { # {{{ + + local_message="${1}" + + ## Print message if DEBUG is enable (=0) + [ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_message}" + + return 0 +} +# }}} +error_message() { # {{{ + + local_error_message="${1}" + local_error_code="${2}" + + ## Print message if DEBUG is enable (=0) + [ "${DEBUG}" -eq "0" ] && printf '%b\n' "ERROR − ${PROGNAME} : ${RED}${local_error_message}${RESET}" + + exit "${local_error_code:=66}" +} +# }}} +define_vars() { # {{{ + + ## Temp file vars {{{ + readonly pa_sink_list_path="/tmp/${PROGNAME}.list" + ## }}} + +} +# }}} +is_command_absent() { # {{{ + + local_command_absent_cmd="${1}" + + ## A command is absent by default + return_command_absent="0" + + if [ "$(command -v ${local_command_absent_cmd})" ]; then + debug_message "is_command_absent − \ +${RED}${local_command_absent_cmd}${COLOR_DEBUG} seems present on this host." + return_command_absent="1" + else + debug_message "is_command_absent − \ +${RED}${local_command_absent_cmd}${COLOR_DEBUG} is not available on this host." + return_command_absent="0" + fi + + return "${return_command_absent}" + +} +# }}} +get_pulseaudio_sink_list() { # {{{ + + ## Return False by default + return_get_pulseaudio_sink_list="1" + + debug_message "get_pulseaudio_sink_list − \ +Create or empty ${RED}${pa_sink_list_path}${COLOR_DEBUG} file to store pulseaudio sinks." + true > "${pa_sink_list_path}" + + ## Get sinks list with pactl (index and description) + ## Merge every two lines + if pactl list sinks | \ + grep --only-matching --perl-regexp '((?<=Sink #)[[:digit:].]*|(?<=Description: ).*)' | \ + paste --delimiters=" " - - > "${pa_sink_list_path}"; then + if [ -s "${pa_sink_list_path}" ]; then + debug_message "get_pulseaudio_sink_list − \ +PulseAudio sinks list successfully created (see ${pa_sink_list_path} file)." + return_get_pulseaudio_sink_list="0" + else + debug_message "get_pulseaudio_sink_list − \ +Error, the PulseAudio sinks list is empty (${pa_sink_list_path} file)." + return_get_pulseaudio_sink_list="1" + fi + else + debug_message "get_pulseaudio_sink_list − \ +Error in ${RED}pacmd list-sinks${COLOR_DEBUG} command." + return_get_pulseaudio_sink_list="1" + fi + + return "${return_get_pulseaudio_sink_list}" + +} +# }}} +choose_pulseaudio_sink() { # {{{ + + ## Return False by default + return_choose_pulseaudio_sink="1" + + debug_message "choose_pulseaudio_sink − \ +Display PulseAudio sinks list with rofi to select one." + + choosen_sink=$(rofi -location 2 -lines 4 -no-auto-select -i -dmenu -p "Choose new default output sink for PulseAudio" -color-enabled -color-normal "${BLACK},${BLUE},${BLACK},${BLUE},${BLACK}" -color-window "${BLACK},${BLACK}" < "${pa_sink_list_path}") + choosen_sink_index=$(printf -- '%s' "${choosen_sink}" | cut --delimiter=" " --field=1) + choosen_sink_description=$(printf -- '%s' "${choosen_sink}" | cut --delimiter=" " --field=2-) + + ## If no sink was selected (empty var) + if [ -z "${choosen_sink_index}" ]; then + debug_message "choose_pulseaudio_sink − \ +Choosen sink var is ${RED}empty${COLOR_DEBUG}." + return_choose_pulseaudio_sink="1" + else + debug_message "choose_pulseaudio_sink − \ +PulseAudio choosen sink is ${RED}${choosen_sink_description}${COLOR_DEBUG} (index: ${RED}${choosen_sink_index}${COLOR_DEBUG})." + return_choose_pulseaudio_sink="0" + fi + + return "${return_choose_pulseaudio_sink}" + +} +# }}} +set_default_pulseaudio_sink() { # {{{ + + ## Return False by default + return_set_default_pulseaudio_sink="1" + + debug_message "set_default_pulseaudio_sink − \ +Try to set ${RED}${choosen_sink_description}${COLOR_DEBUG} (index: ${RED}${choosen_sink_index}${COLOR_DEBUG}) default PulseAudio sink." + + if pactl set-default-sink "${choosen_sink_index}"; then + debug_message "set_default_pulseaudio_sink − \ +${RED}${choosen_sink_description}${COLOR_DEBUG} is now the default output device." + return_set_default_pulseaudio_sink="0" + else + debug_message "set_default_pulseaudio_sink − \ +Error with ${RED}pacmd set-default-sink${COLOR_DEBUG} command." + return_set_default_pulseaudio_sink="1" + fi + + return "${return_set_default_pulseaudio_sink}" + +} +# }}} + +main() { # {{{ + + ## If rofi command is absent from the system {{{ + ### Exit + is_command_absent "rofi" \ + && exit 0 + ## }}} + ## If pacmd command is absent from the system {{{ + ### Exit + is_command_absent "pacmd" \ + && exit 0 + ## }}} + + ## Define all vars + define_vars + + ## Try to get the sinks list {{{ + ### OR Exit with error message + get_pulseaudio_sink_list \ + || error_message "Can't get the PulseAudio sinks list. Please use --debug option." 1 + ## }}} + ## Choose a sink with rofi {{{ + ### OR Exit with error message + choose_pulseaudio_sink \ + || error_message "Can't get the PulseAudio sinks list. Please use --debug option." 2 + ## }}} + ## Set default output sink/device for PulseAudio {{{ + ### OR Exit with error message + set_default_pulseaudio_sink \ + || error_message "Can't set the selected sink (${choosen_sink_description}) as default output sink/device. Please use --debug option." 3 + ## }}} + +} +# }}} + +# Manage arguments # {{{ +# This code can't be in a function due to argument management + +if [ ! "${NBARGS}" -eq "0" ]; then + + manage_arg="0" + + ## If the first argument is not an option + if ! printf -- '%s' "${1}" | grep -q -E -- "^-+"; + then + ## Print help message and exit + printf '%b\n' "${RED}Invalid option: ${1}${RESET}" + printf '%b\n' "---" + usage + + exit 1 + fi + + # Parse all options (start with a "-") one by one + while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do + + case "${1}" in + -d|--debug ) ## debug + DEBUG=0 + ;; + -h|--help ) ## help + usage + ## Exit after help informations + exit 0 + ;; + #-v|--var ) ## Define var with given arg + ### Move to the next argument + #shift + ### Define var + #readonly my_var_xy="${1}" + #;; + * ) ## unknow option + printf '%b\n' "${RED}Invalid option: ${1}${RESET}" + printf '%b\n' "---" + usage + exit 1 + ;; + esac + + debug_message "Arguments management − \ +${RED}${1}${COLOR_DEBUG} option managed." + + ## Move to the next argument + shift + manage_arg=$((manage_arg+1)) + + done + + debug_message "Arguments management − \ +${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed." +else + debug_message "Arguments management − \ +No arguments/options to manage." +fi + +# }}} + +main + +exit 255