Author Topic: Scripting help  (Read 705 times)

0 Members and 1 Guest are viewing this topic.

Offline llogg

  • Sr. Mitglied
  • ****
  • Posts: 421
  • Skill: Novice
Scripting help
« on: 28. March 2016, 06:36:58 »
I'm driving myself nuts.  I can't get this script to work correctly.  It is intended to determine if I have connected my TV via the HDMI cable and if so change the audio output and put the power manager into presentation mode.  If it determines the TV is not connected it should change the audio output back to the analog and disable presentation mode in the power manager.  It handles the audio correctly in both cases.  It handles the presentation mode switch ONLY in the case of the TV not being connected.  If someone can tell me why it will not correctly toggle the presentation mode I'd be really grateful. 

Code: [Select]
#! /bin/bash
printenv
export PULSE_RUNTIME_PATH="/run/user/1000/pulse/"
export DISPLAY=:0
read hdmi_status < /sys/class/drm/card0-HDMI-A-1/status
echo $hdmi_status
read hdmi_status2 < /sys/class/drm/card0-HDMI-A-2/status
echo $hdmi_status2

chk_presentation_mode () {

$(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode)

}

toggle_presentation_mode_on () {
(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -s "true")
}

toggle_presentation_mode_off () {
(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -s "false")
}

sleep 1

if [[ $hdmi_status == "connected" ]]; then
pacmd set-card-profile 0 "output:hdmi-stereo"
  if chk_presentation_mode == "false" ; then
  notify-send -i gtk-dialog-info "Presentation mode is off"
  toggle_presentation_mode_on
  notify-send -i gtk-dialog-info "Presentation mode is now turned on"
  else
  notify-send -i gtk-dialog-info "Presentation mode is already on"
  fi

elif [[ $hdmi_status2 == "connected" ]]; then
pacmd set-card-profile 0 "output:hdmi-stereo"
  if chk_presentation_mode == "false" ; then
  notify-send -i gtk-dialog-info "Presentation mode is off"
  toggle_presentation_mode_on
  notify-send -i gtk-dialog-info "Presentation mode is now turned on"
  else
  notify-send -i gtk-dialog-info "Presentation mode is already on"
  fi

else
pacmd set-card-profile 0 "output:analog-stereo"
  if chk_presentation_mode == "true" ; then
  notify-send -i gtk-dialog-info "Presentation mode is on"
  toggle_presentation_mode_off
  notify-send -i gtk-dialog-info "Presentation mode is now turned off"
  else
  notify-send -i gtk-dialog-info "Presentation mode is already off"
  fi
fi
All the notifications are mostly for debugging purposes, which has been helpful as the notifications for finding presentation mode is NOT on triggers the notifications "Presentation mode is off" followed by "Presentation mode is now turned on" without actually toggling presentation mode.  And if presentation mode IS already on it sends the same notifications rather than the "Presentation mode is already on".
Laptop: Ideapad U310, Kernel 4.1, 4.3, MDM, XFCE

Desktop: Kernel 4.0, 4.1, MDM, XFCE, Nvidia non-free drivers

Offline jonathon

  • Core Team
  • *****
  • Posts: 2104
  • Technologist - Teacher - Tea drinker
  • Branch: Unstable
  • Desktop: MATE 1.14
  • GPU Card: Nvidia GTX680M
  • GPU driver: Bumblebee nvidia+intel
  • Kernel: 4.6.0-*-MANJARO x86_64
  • Skill: Advanced
Re: Scripting help
« Reply #1 on: 09. April 2016, 21:37:29 »
I'd suggest you add

Code: [Select]
set -euxo pipefail

just below the shebang line.

"-e" will exit immediately on an error
"-u" will error if you reference a variable that wasn't initialised
"-x" reports what each step is doing in detail so e.g. you don't need "echo"s everywhere
"-o pipefail" will have the script fail reporting errors from sub-processes


Post the output and we'll get a better picture of what's going on.
--
MSI GT70: Core i7-3630QM, 16GB, Nvidia GTX680M, Intel 2230, Manjaro-MATE-amd64-EFI
Lenovo X230: Core i5-3320M, 4GB, Intel HD4000, Intel 6205, Manjaro-MATE-amd64
Dell Studio 1749: Core i5 540, 8GB, ATi HD5650, Intel WLAN, Manjaro-Xfce-amd64
Let's go in the garden; you'll find something waiting.

Offline llogg

  • Sr. Mitglied
  • ****
  • Posts: 421
  • Skill: Novice
Re: Scripting help
« Reply #2 on: 30. April 2016, 18:21:48 »
Sorry I didn't see this earlier.

Here is the script in its current form:
Code: [Select]
#! /bin/bash
set -euxo pipefail

printenv
export PULSE_RUNTIME_PATH="/run/user/1000/pulse/"
export DISPLAY=:0
read hdmi_status < /sys/class/drm/card0-HDMI-A-1/status
echo $hdmi_status
read hdmi_status2 < /sys/class/drm/card0-HDMI-A-2/status
echo $hdmi_status2

sleep 1

if [[ $hdmi_status == "connected" ]]; then
pacmd set-card-profile 0 "output:hdmi-stereo"

elif [[ $hdmi_status2 == "connected" ]]; then
pacmd set-card-profile 0 "output:hdmi-stereo"

else
pacmd set-card-profile 0 "output:analog-stereo"

fi
chk_presentation_mode="$(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode)"

toggle_presentation_mode_on () {
(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -s "true")
}

toggle_presentation_mode_off () {
(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -s "false")
}

if [[ $hdmi_status == "connected" ]]; then
  if [[ $chk_presentation_mode == "false" ]]; then
  notify-send -i gtk-dialog-info "Presentation mode is off"
  toggle_presentation_mode_on
  notify-send -i gtk-dialog-info "Presentation mode is now turned on"
  else
  notify-send -i gtk-dialog-info "Presentation mode is already on"
  fi
 
elif [[ $hdmi_status2 == "connected" ]]; then
  if [[ $chk_presentation_mode == "false" ]]; then
  notify-send -i gtk-dialog-info "Presentation mode is off"
  toggle_presentation_mode_on
  notify-send -i gtk-dialog-info "Presentation mode is now turned on"
  else
  notify-send -i gtk-dialog-info "Presentation mode is already on"
  fi

else
  if [[ $chk_presentation_mode == "true" ]]; then
  notify-send -i gtk-dialog-info "Presentation mode is on"
  toggle_presentation_mode_off
  notify-send -i gtk-dialog-info "Presentation mode is now turned off"
  else
  notify-send -i gtk-dialog-info "Presentation mode is already off"
  fi
fi
When running in terminal as user it now works exactly as intended.  I think the original problem was due to how  I defined the chk_presentation_mode function.  Now there is a new problem, though.

I would like it to automatically run when the HDMI display is connected.  I am using this udev rule:
Code: [Select]
SUBSYSTEM=="drm", ACTION=="change", RUN+="/bin/bash /home/llogg/.scripts/hdmitoggle_trigger.sh"
which calls this script
Code: [Select]
#!/bin/bash

for dir in /home/*/
do
    dir=${dir%*/}
    sudo -u ${dir##*/} /home/llogg/.scripts/hdmitoggle.2.sh
done
I have to do that because udev can't directly affect pulseaudio, I guess.  The audio switch works perfectly, but the presentation mode does not switch and none of the notifications are displayed.  No terminal output because udev doesn't really run it in a terminal.  Any ideas?
« Last Edit: 30. April 2016, 22:21:53 by llogg »
Laptop: Ideapad U310, Kernel 4.1, 4.3, MDM, XFCE

Desktop: Kernel 4.0, 4.1, MDM, XFCE, Nvidia non-free drivers