Author Topic: [How To] Control xfce4-power-manager based on running applications (bash script)  (Read 966 times)

0 Members and 1 Guest are viewing this topic.

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
UPDATED 7/18/13 -- Changed the technique from killing xfce4-power-manager (which did not actually stop power saving features all the time, even when no xfce4-power-manager process was running) to simulating a mouse movement 2x2 pixels.  You'll want to remember to turn this off if you're drawing while listening to music.  See the comments #### below for other changes.

Since caffeine doesn't seem to work anymore and is no longer in active development, I have been looking for a way to disable the xfce power manager while watching videos or listening to music.  I didn't find anything, so I learned some things and came up with this script.  A couple of notes:
1. This script as written is only checking for Spotify, Pithos, and flash.  To add other applications you'll have to modify the script using the entries for spotify and pithos as a template.  Also, it's checking the activity of flash, but only checking if Spotify and Pithos are open.  So if, e.g., Spotify is open but not playing anything it will still turn off the power manager.

###Changed the method, so this is not true anymore.  2. This script as written does nothing about xscreensaver because I don't use it (or any other screensaver).  If you need that control, you'll need to modify the script using the entries for xfce4-power-manager as a template (I think).

3. The flash monitoring is tricky.  The method I've gone with looks at the CPU usage of the flash plugin.  The values I've chosen as triggers seem to work for my machine, but you might have to adjust them for yours.  If you find the default values are not working for your system, you can use $top or a separate script consisting only of the flash_check and FLASH_USAGE operations to see how much CPU it typically uses when playing a video or just idle.  Set the numbers according to your observations.

#### I think this was screwing it up after several iterations, so I've added a second script to call this one every desired interval.  Add the second script to autostart list.  The first sleep lets power manager load then calls the other script.  The second sleep determines the interval between checking.  4. The sleep command near  the end of the script sets how often it checks for these applications in seconds.  Adjust to your preferences but make sure it is less than the trigger for your power manager settings.

5. The notifications can be altered to your liking as well.  In particular you can remove the notifications for the situations when power manager is already off and a triggering application is running.  I have notifications there now just to see that it worked in all scenarios.  You can simply replace the "notify-send ..." with ":" if you don't want these notifications popping up while you're watching your movie.

Since this is in a tutorial section, here is a quick flowchart of what the script is doing.  This seemed better than cluttering it up with a bunch of comments.
####Replace kill power manager with move mouse.

Code: [Select]
PWRMGRKILL='xdotool mousemove_relative 2 2'
PWRMGRSTART='xfce4-power-manager'
flash_check=$(ps x | awk '/libflashplayer.so\ /{print $1}' | grep -v ps)
FLASH_USAGE=$(ps aux | grep $flash_check | grep -v grep | grep -v ps | awk '{print $3*100}')
SPOTIFY_CHECK=$(pidof -x spotify)
PITHOS_CHECK=$(pidof -x pithos)

if pidof -x xfce4-power-manager ; then
  if [[ $SPOTIFY_CHECK ]] ; then
     $PWRMGRKILL &
     notify-send -i gtk-dialog-info "Power Manager disabled for Spotify."
  elif [[ $PITHOS_CHECK ]] ; then
     $PWRMGRKILL &
     notify-send -i gtk-dialog-info "Power Manager disabled for Pithos."
  elif [[ $flash_check ]] ; then
    if [[ $FLASH_USAGE -gt 1120 ]] ; then
      $PWRMGRKILL &
      notify-send -i gtk-dialog-info "Power Manager disabled for Flash."
    fi
  else
     notify-send -i gtk-dialog-info "Power Manager keepin' running, boss."
  fi
else
  if [[ $SPOTIFY_CHECK ]] ; then
   notify-send -i gtk-dialog-info "Power Manager stays disabled for Spotify."
  elif [[ $PITHOS_CHECK ]] ; then
   notify-send -i gtk-dialog-info "Power Manager stays disabled for Pithos."
  elif [[ $flash_check ]] ; then
    if  [[ $FLASH_USAGE -gt 1120 ]] ; then
       notify-send -i gtk-dialog-info "Power Manager stays disabled for Flash."
    elif [[ $FLASH_USAGE -lt 1120 ]] ; then
       $PWRMGRSTART &
       notify-send -i gtk-dialog-info "Starting Power Manager since Flash isn't active."
    fi
  else
     $PWRMGRSTART &
     notify-send -i gtk-dialog-info "Starting Power Manager, boss."
  fi
fi
The new second script for calling the above script at regular intervals is here:
Code: [Select]
#!/bin/bash

sleep 20
notify-send -i gtk-dialog-info "Checking power management."
while true
do
/<pathtoscript>
sleep 880
notify-send -i gtk-dialog-info "Rechecking."
done
  Again, the notifications are mostly just so I can tell it's running correctly.  Feel free to delete or comment them out.
I wish I could name everyone who put scripts out there for me to learn and steal from, but there were too many.  Suffice it to say, this work is released under the same spirit as that of all the examples it draws upon.
P.S. My daughter insists we try out the smilies:
 :'( :D ??? :-X
« Last Edit: 19. July 2013, 06:06:50 by llogg »
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE

Offline handy

  • Global Moderator
  • *****
  • Posts: 3895
  • Old experiential pantheist/animist/gnostic...
  • Kernel: x86_64 Linux 3.13.9-1
  • Desktop: Openbox 3.5.2-6 (custom setup: Worker based, Alsa, Auto-login, No windows/icons, tiny Ob menu...)
  • Branch: Testing
  • GPU Card: nVidia GTX 660 Ti
  • GPU driver: non-free
  • Skill: Intermediate
Thanks for putting the time in on this one llogg.

I'm sure that there will be people that are very grateful to you for it, we all hate having our movies interrupted (at least a black screen isn't as bad as a string of damned TV commercials!). :)
Forget about them, all they do is bring the world down. Ignore them & you are bringing the world up, just by ignoring them.  ;D

A species of conscience, or an evolutionary mishap?

The Forum Rules

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
One other thing I just noticed, since the Spotify entry is searching for pid based on name, any process with spotify in the name will trigger the command, so if you have spotify-controller installed for your phone, you have to quit both spotify and spotify-controller to allow power manager to turn back on.
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
Updated the script with a "sleep 20" at the beginning.  This is for people who will have this script run at startup automatically.  Without that delay you can end up with two instances of power-manager running.  I'm not sure if that causes any problems, but it's at least inefficient.  Making the script wait a few seconds to check will prevent this.

Also looked into ways to include xscreensaver in the script, but what I read is that killing xcreensaver can lead to window manager instability.  Not sure about other screensavers.  Anyone know if killing and restarting power-manager has any ill effects?  I haven't noticed any yet.
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
Updated script with a new kill function of "xfce4-power-manager --quit" and a new restart of "xfce4-power-manager --no-daemon".  Not sure if the no-daemon will make any difference.

It seems that after running for several hours the script misbehaves, returning "Power Manager stays disabled for Spotify" or flash or pithos even well after those applications have been closed and the processes are no longer active.  Anyone know how to troubleshoot this?  It only seems to happen after many hours of continuous running, so I'm wondering if the sleep functions are causing the problem.
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE

Offline Quantum

  • Vollwertiges Mitglied
  • ***
  • Posts: 199
  • Skill: Novice
Post removed at Quantum's request.
« Last Edit: 24. September 2013, 02:35:57 by handy »
Vision without execution is hallucination.
-- Thos A. Edison

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
Wow, that seems much more efficient and tidy, but I don't understand it.  I'm very new to this, so excuse what is probably a dumb question, but this seems like it would just run each of those commands in succession, turning off dpms, then launching spotify, then turning on dpms.  Wouldn't you have to make it conditional in some way?
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE

Offline Quantum

  • Vollwertiges Mitglied
  • ***
  • Posts: 199
  • Skill: Novice
Post removed at Quantum's request.
« Last Edit: 24. September 2013, 02:35:41 by handy »
Vision without execution is hallucination.
-- Thos A. Edison

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
Oh, nice.  Will work for pinning to launchers for media applications, but I think you still need something that monitors for flash activity.
« Last Edit: 17. July 2013, 18:45:29 by llogg »
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
Updated.  See OP.  Depends on xdotools if not installed already.
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE

Offline Quantum

  • Vollwertiges Mitglied
  • ***
  • Posts: 199
  • Skill: Novice
Post removed at Quantum's request.
« Last Edit: 24. September 2013, 02:35:17 by handy »
Vision without execution is hallucination.
-- Thos A. Edison

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
I might look at that and see if the source is available, just to see how they're handling it.  Thanks.
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
Hmm.  Found this script, which seems pretty good and does not use my new xdotools kludgy technique.
https://raw.github.com/hotice/lightsOn/master/lightsOn.sh
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE

Offline mkennedy150

  • Sr. Mitglied
  • ****
  • Posts: 313
  • Kernel: linux39-x64
  • Desktop: KDE
  • Branch: Stable
  • GPU Card: Intel 4000 HD
  • GPU driver: free
  • Skill: Intermediate
does this happen on desktop and laptops?  Never noticed the problem

Offline lloggTopic starter

  • Vollwertiges Mitglied
  • ***
  • Posts: 190
  • Skill: Novice
mkennedy, by problem, do you mean screensaver/power management kicking in while playing videos/dvds/music?  It's pretty common, and if you do not have that issue it must be because you have no screensaver and no dpms enabled.
Kernel 3.10, 3.13
nVidia non-free
LXDM
XFCE