From: Siraaj Khandkar Date: Wed, 1 Aug 2018 20:49:21 +0000 (-0400) Subject: Split-out and refactor 'pactl list sinks' parser X-Git-Url: https://git.xandkar.net/?a=commitdiff_plain;h=d06e7260cd2c4cb3813d197628ee224771f775cb;p=khatus.git Split-out and refactor 'pactl list sinks' parser --- diff --git a/bin/khatus b/bin/khatus index da0c78c..868f0b3 100755 --- a/bin/khatus +++ b/bin/khatus @@ -257,6 +257,7 @@ main() { cmd_sens_bluetooth="khatus_sensor_bluetooth_power $dir_bin" cmd_sens_mpd_state="khatus_sensor_mpd_state $dir_bin" cmd_sens_net_addr_io="khatus_sensor_net_addr_io $dir_bin" + cmd_sens_volume="khatus_sensor_volume $dir_bin" # TODO: Redirect each worker's stderr to a dedicated log file pipe="$file_pipe" @@ -269,7 +270,7 @@ main() { fork_poller $interval_inp_weather "$pipe" "$dir_bin" "$log" "$cmd_sens_weather" 'in:WEATHER' fork_poller $interval_inp_mpd_state "$pipe" "$dir_bin" "$log" "$cmd_sens_mpd_state" 'in:MPD_STATE' fork_poller $interval_inp_mpd_song "$pipe" "$dir_bin" "$log" khatus_sensor_mpd_song 'in:MPD_SONG' - fork_poller $interval_inp_volume "$pipe" "$dir_bin" "$log" khatus_sensor_volume 'in:VOLUME' + fork_poller $interval_inp_volume "$pipe" "$dir_bin" "$log" "$cmd_sens_volume" 'in:VOLUME' fork_poller $interval_inp_bluetooth "$pipe" "$dir_bin" "$log" "$cmd_sens_bluetooth" 'in:BLUETOOTH_POWER' fork_poller $interval_inp_net_wifi "$pipe" "$dir_bin" "$log" khatus_sensor_net_wifi_status 'in:NET_WIFI_STATUS' fork_poller $interval_inp_net_io "$pipe" "$dir_bin" "$log" "$cmd_sens_net_addr_io" 'in:NET_ADDR_IO' diff --git a/bin/khatus_controller b/bin/khatus_controller index d9218ce..c2e5d91 100755 --- a/bin/khatus_controller +++ b/bin/khatus_controller @@ -95,8 +95,7 @@ /^in:VOLUME/\ { - shift() - db["volume"] = $0 + set_volume() } /^in:MPD_SONG OK +MPD/ { delete db_mpd_song; next } @@ -124,6 +123,24 @@ output_msg_status_bar(make_status_bar()) } +function set_volume( mute, left, right) { + # 0 RUNNING no 75% 75% + #msg_head = $1 + #sink = $2 + #state = $3 + mute = $4 + left = $5 + right = $6 + + if (mute == "no") { + db["volume"] = sprintf("%s %s", left, right) + } else if (mute == "yes") { + db["volume"] = "X" + } else { + # TODO: Log error - unexpected mute value + } +} + function set_mpd_song( key, val) { key = $2 shift() diff --git a/bin/khatus_parse_pactl_list_sinks b/bin/khatus_parse_pactl_list_sinks new file mode 100755 index 0000000..2de73e8 --- /dev/null +++ b/bin/khatus_parse_pactl_list_sinks @@ -0,0 +1,42 @@ +#! /usr/bin/awk -f + +/^Sink \#[0-9]+$/ { + sub("^#", "", $2) + sink = $2 + next +} + +/\tState:/ { + state[sink] = $2 + next +} + +/\tMute:/ { + mute[sink] = $2 + next +} + +# Volume: front-left: 45732 / 70% / -9.38 dB, front-right: 45732 / 70% / -9.38 dB +/\tVolume:/ { + delete vol_parts + delete left_parts + delete right_parts + sub("^\t+Volume: +", "") + split($0, vol_parts, ", +") + sub("^front-left: +", "", vol_parts[1]) + sub("^front-right: +", "", vol_parts[2]) + split(vol_parts[1], left_parts, " +/ +") + split(vol_parts[2], right_parts, " +/ +") + vol_left[sink] = left_parts[2] + vol_right[sink] = right_parts[2] + next +} + +END { + for (sink in state) { + printf(\ + "%s %s %s %s %s\n", + sink, state[sink], mute[sink], vol_left[sink], vol_right[sink] \ + ) + } +} diff --git a/bin/khatus_sensor_volume b/bin/khatus_sensor_volume index 4c14243..6f69829 100755 --- a/bin/khatus_sensor_volume +++ b/bin/khatus_sensor_volume @@ -2,23 +2,6 @@ set -e -pactl list sinks \ -| awk ' - /^\tMute:/ { - printf("%s,", $0); - } - /^\tVolume:/ { - for (i=2; i<=NF; i++) printf(" %s", $i); - }' \ -| awk -v RS=',' ' - /^[ \t]*Mute:/ {mute = $2} - /^[ \t]*front-left:/ {left = $4} - /^[ \t]*front-right:/ {right = $4} - END { - if (mute == "yes") { - print("x") - } else { - printf("%s %s\n", left, right) - } - } - ' +dir_bin="$1" + +pactl list sinks | "$dir_bin"/khatus_parse_pactl_list_sinks