From c8cb080d5d39c67dff8932de5cb1df985453f738 Mon Sep 17 00:00:00 2001 From: aonemd Date: Thu, 29 Mar 2018 10:25:40 +0200 Subject: [PATCH] Add detection of connected headphone for volume level --- components/volume_level.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/components/volume_level.go b/components/volume_level.go index eade2f6..8e06c84 100644 --- a/components/volume_level.go +++ b/components/volume_level.go @@ -7,6 +7,17 @@ import ( "strings" ) +const ( + // find an entry that has `headphone jack` in it using `amixer controls` + headphone_numid = "1" + sound_card = "0" + + speaker_up_icon = "" + speaker_mute_icon = " MUTE" + headphone_up_icon = "" + headphone_mute_icon = " MUTE" +) + func main() { response, _ := exec.Command("amixer", "sget", "Master").Output() @@ -16,9 +27,22 @@ func main() { level := levelPattern.FindString(string(response)) status := strings.Trim(statusPattern.FindString(string(response)), "[]") + headPhoneStatusArgs := strings.Split(fmt.Sprintf("-c %s cget numid=%s", sound_card, headphone_numid), " ") + headphoneResponse, _ := exec.Command("amixer", headPhoneStatusArgs...).Output() + headPhoneStatusPattern, _ := regexp.Compile("values=(on|off)") + headPhoneStatus := strings.Trim(headPhoneStatusPattern.FindString(string(headphoneResponse)), "values=") + + upIcon := speaker_up_icon + muteIcon := speaker_mute_icon + + if headPhoneStatus == "on" { + upIcon = headphone_up_icon + muteIcon = headphone_mute_icon + } + if status == "off" { - fmt.Println(" MUTE") + fmt.Println(muteIcon) } else { - fmt.Println(fmt.Sprintf(" %s", level)) + fmt.Println(fmt.Sprintf("%s %s", upIcon, level)) } }