Thank you for reviewing over my script. All that stuff in there is just to test to see what happens when the computer is suspending. I've gone through a couple of iterations of the script, so I'll upload the latest version I've been working on. On 12/23/2014 01:50 PM, Sergey "Shnatsel" Davidoff wrote: > Cody asked me to review this, so I've commented on the diff. > > Diff comments: > >> === added file '13_pause-media-players' >> --- 13_pause-media-players 1970-01-01 00:00:00 +0000 >> +++ 13_pause-media-players 2014-12-18 20:36:38 +0000 >> @@ -0,0 +1,66 @@ >> +#!/bin/bash >> +# >> +# Copyright (c) Jason Anderson 2014 >> +# License: GPL-2 >> +# * December 1, 2014 >> +# - Version 0.1 >> +# - Initial version >> +# * December 4, 2014 >> +# - Version 0.2 >> +# - Changed it for Totem video player >> +# - This is compatible with Audacious, Clementine, and VLC >> +# - Doesn't work for SMplayer >> +# * December 13, 2014 >> +# - Cleaned up the code a little (added "suspend" to the case arguments). >> +# - Found out Banshee works, too. >> +# * December 14, 2014 >> +# - Version 0.3 >> +# - Refactored D-Bus code that sends the pause signal. >> +# * Version 0.4 >> +# - December 15, 2014 >> +# - Really changed around the pause_music() function. >> +# - Script still doesn't work. >> +# * December 16, 2014 >> +# - Tried adding "DISPLAY" to the script as well as "dbus-launch" because >> +# when the computer is suspending the script can't use "qdbus" nor >> +# dbus-send. >> + >> +le_title="[13_pause-media-players]" > > why is this needed and not take from "$0"? Just a placeholder. I'll add in "$0". > >> +# Just some random hacks >> +DISPLAY=":0" > > Well that's an inspiring comment! > > Are you sure display ":0" will be always available, though? And which command requires it to be set, specifically? > >> +export $DISPLAY >> +DBUS_SESSION_BUS_ADDRESS="unix:path=/run/dbus/system_bus_socket" >> +export DBUS_SESSION_BUS_ADDRESS >> +# Just some random hacks >> +# TODO: use another D-Bus application that's installed by default (i.e. gdbus). >> +output=$(qdbus org.mpris.MediaPlayer2.*) > > We could ship qdbus since it doesn't seem to have any Qt dependencies, despite its name. > But if it's introducing the dependency on DISPLAY being set, then it's probably easiest to replace it with something that doesn't. > > I'm pretty sure that there is a way to send a message to all MPRIS players without querying them like this, because this way potentially introduces race conditions. Yeah, I've been looking into gdbus, as well as dbus-monitor. If it's not apparent, I check to see if the media player's dbus interface is available. The thing is, qdbus can easily get the currently playing media players. I've looked into using gdbus, but I can't figure out how to get the exact interface, and use that in a call to dbus-send. What's also strange is that, when I test this script (e.g. closing my laptop lid to send the computer into suspension), it doesn't pick up the currently playing media player. So I'll have Audacious or Clementine playing, then suspend the laptop, and finally bring it out of suspension. My hunch is that the script is run after the user session is suspended, and thus it doesn't even affect the media player. Though I hope I'm wrong about that. > >> +logger "$le_title The media players currently running: $output" >> + >> +# Function for pausing the audio of each MPRIS-enabled media player. >> +pause_music() { >> + logger "$le_title Now attempting to pause music." >> + if [ -z $output ]; then >> + logger "$le_title The variable \$output has nothing in it." > > "No music players detected" would be a better idea for a log message Thank you. I'll add that in. > >> + exit 0 >> + else >> + eval 'dbus-launch --auto-syntax' dbus-send --print-reply --dest=$output /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause > > why the eval? What had happened was that dbus kept complaining about $DISPLAY not being set. After adding that in, dbus gave an error about a service not found. I found that, in some cases with this error, using dbus-launch solved the problem. Sadly, it didn't. I have since removed that. > >> + fi >> + >> +} >> + >> +# We need to use dbus-send so as to pause all of the media players that are listening via MPRIS. >> +case "$1" in >> + hibernate|suspend) >> + logger "$le_title We're hibernating. Going down!" >> + pause_music >> + ;; >> + resume|thaw) >> + logger "$le_title We're unthawing. Rise and shine!" >> + ;; >> +# If we have anything else than a hibernate or a thaw, try to exit gracefully. >> + *) >> + exit $NA > > $NA does not seem to be set anywhere in this script. Is it exported from the parent shell? I think it was from a parent shell. Just in case, I'll take it out (it doesn't affect the script, anyway). > >> + ;; >> +esac >> + >> +exit 0 >> >> === modified file 'debian/install' >> --- debian/install 2012-09-22 10:38:10 +0000 >> +++ debian/install 2014-12-18 20:36:38 +0000 >> @@ -1,3 +1,4 @@ >> midori/config /etc/xdg/midori/ >> plank/dock1 /etc/skel/.config/plank/ >> settings.ini /etc/gtk-3.0/ >> +13_pause-media-players /etc/pm/sleep.d/ >> > >