iye / lightson Goto Github PK
View Code? Open in Web Editor NEWBash script that prevents the screensaver and display power # management (DPMS) to be activated when you are watching Flash Videos fullscreen on Firefox.
Bash script that prevents the screensaver and display power # management (DPMS) to be activated when you are watching Flash Videos fullscreen on Firefox.
would be much appreciated.
Should be relatively straightforward, but unfortunately I don't have the time to sit down and do it at the moment. It will probably be similar to the Firefox one. The wrapper process looks like this:
/usr/lib/opera/pluginwrapper/operapluginwrapper-native -multiproc 2,0.1.8,7,133,153 -logfolder /home/user/.opera/logs/ -type native
Hi, there's no kscreensaver in current KDE4 (4.12.4). However, I have no idea how to detect if kde4's screensaver facility is actually is use (at least I can't find a process that would be running). The script works otherwise, if I force screensaver=kscreensaver (the simulateuseractivity etc.).
Here's some pgrep output (while KDE4's screensaver is actually in use):
$ pgrep -l kde
19 kdevtmpfs
2699 startkde
2761 kdeinit4
2764 kded4
3575 polkit-kde-auth
$ pgrep -l screen
3626 screen
3627 screen
Currently, it matching program by whole 'command line', except by just 'process name'.
So for example:
I have some program named bar
in $delay_progs. This program is not running currently. I decided to open file with name 'foobar.sh' in vim:
ps ax | grep bar
5678 pts/5 S+ 0:00 vi foobar.sh
5744 pts/1 S+ 0:00 grep bar
And here is problem: lightsOn.sh would think that bar
program running. But it's not, there are just something, that have string 'bar' in it's arguments.
Delaying the screensaver because a program on the delay list, "bar", is running...
Possible solutions:
Also, if legacy is an issue, then this behavior may be implemented as optional.
Youtube is switching to webm when possible, it would be nice if the screensaver was disabled for it as well.
Reason: An xdg-utils package should be present in all Linux/BSD distributions updated in the past decade. This package is a standardized way for apps to have cross-desktop-environment compatibility/integration. In particular, it supports screensaver management via xdg-screensaver
. They also encourage bundling whichever xdg
tools you use for a stronger compatibility guarantee; the license looks BSD-like.
If I'm right about xdg-screensaver
working on all distros that people want to run this on, then you can simultaneously fix issues #6 and #19 (and prevent future issues of that sort until desktop environments stop supporting xdg-utils
) by removing lines 55-68 (screensaver detection) and replacing lines 191-212 with delayScreensaver() { xdg-screensaver reset; }
To test if this would work on a given system, run xdg-screensaver status
. It should print enabled
if you're on a modern-ish system with a screensaver. If you want to test with a video, here's a Bash/Zsh/sh one-liner: while true; do xdg-screensaver reset; sleep 50; done
In theory, media-playing apps like VLC and browsers should be running xdg-screensaver suspend WindowID
with the ID of whichever window is playing media. In practice, they're not all doing that properly or lightsOn.sh
wouldn't exist. ;-)
.
(Personal xdg-screensaver
and lightsOn.sh
experience, incidental advert for my code, and long run-on sentence: This is essentially what I do in my own lights-on.sh
script that was originally copy-paste-forked from lightsOn.sh
, minimized and fixed for just KDE4, split into a pair of scripts for enabling/disabling "keep screen on" mode, deleted out of frustration when updates way later broke it again, and rewritten from scratch just a few hours ago.)
It would be nice if it could detect when I'm playing a steam game. There should be a steam binary running hopefully with a --fullscreen parameter. I haven't looked into it yet.
This could be detected just by looking at mouse, keyboard and joystick inputs. I think there is just a joystick detection needed.
Is it possible to look for the fullscreen flag on X windows? Wouldn't this be the easiest way to find fullscreen applications?
I'm getting syntax error on line 52 after just cloning the repo and trying to run the script. Not familiar with Bash, but it says something about a redirect error.
$ sh lightsOn.sh 20
lightsOn.sh: 52: lightsOn.sh: Syntax error: redirection unexpected
Could you please add Opera Browser support? Thank you.
Hello,
when i work with :
Name : flashplugin
Version : 11.1.102.55-1
in Arch Linux, your script didn't worked, i've started to analyze, and problem was, that :
$activ_win_title for flash player plugin had value : WM_CLASS(STRING) = "plugin-container", "Plugin-container", so I easily edited a line to look like :
...
if [ $firefox_flash_detection == 1 ];then
if [[ "$activ_win_title" = plugin-cont ]];then
# Check if plugin-container process is running
...
Do i have to commit a patch? Or is it Arch Linux related somehow? If it's even real. Dunno much about X-related things
Hello! iye.cba . A few days ago I got this script on github. I like the script very much. It taught me a lot, gave me another idea to solve the problem. But I am sorry that I do not like some places in the script.
So I modified some code that makes the script more powerful and more elegant.
#!/bin/bash
# lightsOn.sh
# Copyright (c) 2013 iye.cba at gmail com
# url: https://github.com/iye/lightsOn
# This script is licensed under GNU GPL version 2.0 or above
flash_detection=1
mplayer_detection=1
vlc_detection=1
minitube_detection=1
gnome_mplayer_detection=1
smplayer_detection=1
totem_detection=1
delay_progs=()
displays=""
while read id
do
displays="$displays $id"
done < <(xvinfo | sed -n 's/^screen #\([0-9]\+\)$/\1/p')
# I think "pidof name" is better than "pgrep -lfc name |grep -wc name". It only output the PIDs when detects the process,
# but output nothing when no process.Most importantly, it is shorter.
if [ `pidof xscreensaver` ]; then
screensaver=xscreensaver
elif [ `pidof gnome-screensaver` ]; then # Most desktop enviromments use gnome-screensaver.
screensaver=gnome-screensaver
elif [ `pidof cinnamon-screensaver` ]; then # cinnamon desktop enviromment is getting more and more popular.
screensaver=cinnamon-screensaver
elif [ `pidof kscreensaver` ]; then # This can not effect on latest KDE version.
screensaver=kscreensaver
elif [ -f $HOME/.kde*/share/config/kscreensaverrc ] && [ -z `grep -iw "enabled=false" $HOME/.kde*/share/config/kscreensaverrc` ]; then #Effect on new KDE version.
screensaver=kscreensaver
else
screensaver=None
echo "No screensaver detected"
fi
checkDelayProgs()
{
for prog in "${delay_progs[@]}"; do
if [ `pidof "$prog"` ]; then
echo "Delaying the screensaver because a program on the delay list, \"$prog\", is running..."
delayScreensaver
break
fi
done
}
checkFullscreen()
{
for display in $displays
do
activ_win_id=`DISPLAY=:0.${display} xprop -root _NET_ACTIVE_WINDOW`
activ_win_id=${activ_win_id:40:9}
if [ "$activ_win_id" = "0x0" ]; then # Skip invalid window ids (It returns "0x0" when ScreenSaver is actived)
continue
fi
isActivWinFullscreen=`DISPLAY=:0.${display} xprop -id $activ_win_id | grep _NET_WM_STATE_FULLSCREEN`
if [ "$isActivWinFullscreen" ];then
isAppRunning
var=$?
if [[ $var -eq 1 ]];then
delayScreensaver
fi
fi
done
}
isAppRunning()
{
#Get PID of active window, I think it makes the code easier.
activ_win_pid=`xprop -id $activ_win_id | grep "_NET_WM_PID(CARDINAL)"`
activ_win_pid=${activ_win_pid##* }
if [ $flash_detection == 1 ]; then
if [[ `lsof -p $activ_win_pid | grep flashplayer.so` ]]; then # match all browers (which use libflashplayer.so , libpepflashplayer.so & operapluginwrapper-native)
return 1
fi
fi
if [ $mplayer_detection == 1 ];then
if [[ `ps p $activ_win_pid o comm=` = "mplayer" ]];then # Which is more simple and accurate.
return 1
fi
fi
if [ $vlc_detection == 1 ];then
if [[ `ps p $activ_win_pid o comm=` = "vlc" ]];then
return 1
fi
fi
if [ $minitube_detection == 1 ];then
if [[ `ps p $activ_win_pid o comm=` = "minitube" ]];then
return 1
fi
fi
if [ $gnome_mplayer_detection == 1 ];then # It is easy to add video player detection.
if [[ `ps p $activ_win_pid o comm=` = "gnome-mplayer" ]];then
return 1
fi
fi
if [ $smplayer_detection == 1 ];then
if [[ `ps p $activ_win_pid o comm=` = "smplayer" ]];then
return 1
fi
fi
if [ $totem_detection == 1 ];then
if [[ `ps p $activ_win_pid o comm=` = "totem" ]];then
return 1
fi
fi
return 0
}
delayScreensaver()
{
if [ "$screensaver" == "xscreensaver" ]; then
xscreensaver-command -deactivate > /dev/null
elif [ "$screensaver" == "gnome-screensaver" ]; then
gnome-screensaver-command --deactivate > /dev/null # It is similar to xscreensaver.
elif [ "$screensaver" == "cinnamon-screensaver" ]; then
cinnamon-screensaver-command --deactivate > /dev/null # I have not tried, but I guess it do so.
elif [ "$screensaver" == "kscreensaver" ]; then
qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity > /dev/null
fi
dpmsStatus=`xset -q | grep -ce 'DPMS is Enabled'`
if [ $dpmsStatus == 1 ];then
xset -dpms
xset dpms
fi
}
delay=$1
if [ -z "$1" ];then
delay=50
fi
if [[ $1 = *[^0-9]* ]]; then
echo "The Argument \"$1\" is not valid, not an integer"
echo "Please use the time in seconds you want the checks to repeat."
echo "You want it to be ~10 seconds less than the time it takes your screensaver or DPMS to activate"
exit 1
fi
while true
do
checkDelayProgs
checkFullscreen
sleep $delay
done
exit 0
I find this to be a better option...
prevent powermangement while video is playing, rather than fullscreen only.
referring no.3; ability to define program @top of code.
Otherwise, is the code only possible to find hooks in the 'fullscreen' aspect? meaning regardless of playback-state fullscreen is the indicator?
-l
option says pgrep to output list of process names.
-c
option says pgrep to output ONLY count of matched processes.
So it is safe to simply remove -l
option.
dbus-send --session --dest=org.gnome.ScreenSaver --type=method_call /org/gnome/ScreenSaver org.gnome.ScreenSaver.SimulateUserActivity
does for gnome-screensaver what qdbus org.freedesktop.ScreenSaver /ScreenSaver SimulateUserActivity
does for kscreensaver.
Should be pretty easy to add gnome-screensaver support with that.
hi.
I don't see the point of checking if the windows is full screen or not.
If you are watching a video on youtube in your browser whether it is in fullscreen or not does not matter as in either case you don't want the screensaver to kick in.
the same is true for vlc or mplayer
EDIT: Ok: it's because you can't check if the player is actually playing a video or not ? right ?
Trying to run lightsOn.sh
in my xmonad setup, the script fails for me. I have tried to investigate where the problem comes from, and it seems to be coming from xprop
(currently line 64).
Here is a little rundown of where I think the problems crops up.
$ display=xvinfo | sed -n 's/^screen #\([0-9]\+\)$/\1/p'
0
$ DISPLAY=:0.${display} xprop -root _NET_ACTIVE_WINDOW
_NET_ACTIVE_WINDOW: not found.
Since apparently _NET_ACTIVE_WINDOW
doesn't exist (I wonder why?), everything thereafter fails as well. and we get for:
isActivWinFullscreen=`DISPLAY=:0.${display} xprop -id $activ_win_id | grep _NET_WM_STATE_FULLSCREEN`
sage: xprop [-options ...] [[format [dformat]] atom] ...
where options include:
-grammar print out full grammar for command line
-display host:dpy the X server to contact
-id id resource id of window to examine
-name name name of window to examine
-font name name of font to examine
-remove propname remove a property
-set propname value set a property to a given value
-root examine the root window
-len n display at most n bytes of any property
-notype do not display the type field
-fs filename where to look for formats for properties
-frame don't ignore window manager frames
-f propname format [dformat] formats to use for property of given name
-spy
Since I don't know if it is imporant, but I start my X session with:
[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && exec startx
Any ideas how to fix this? I would really love to use lightsOn
!
Hello,
Just to share a small enhancement of the script, a small script which detect when a video is playing on the active windows. It need xdotool. In fact, it detect not directly the video but if the active window run a process producing an audio output. Like most of videos come with sound, it works. If the program is running on the background, not on the active windows, as a music player working on the background, it don't stop the idle screen. It is designed for Lubuntu/Ubuntu 20.04, using pulse audio.
Just add this fonction to the loop of the lightsOn script (here I have disabled checkFullscreen) :
while true
do
checkDelayProgs
checkAudioProcessRunning
sleep $delay
done
And add this function :
checkAudioProcessRunning()
{
W=$(xprop -id $(xdotool getactivewindow) |awk '/_NET_WM_PID/ {print $3}')
S=$(pacmd list-sink-inputs | awk -F "=|:" '/application.process.id/||/state/ {gsub(""","");print $2}' ORS=' ')
R=$(echo $S | awk '{for (i=1;i<NF;i++) if (
if grep -q $W <<< $R; then
#echo "An audio process is running on active windows"
delayScreensaver
else
:
#echo "No audio process is running or not on active windows"
fi
}
W check the process id of the active window, S list the processes which have an audio output to pulse audio, extracting the application.process.id field (process id) and the state field (CORKED or RUNNING), R select the process id of the actually running audio processes. If the process id of the active window is one of these (if W if contain in R), that means than the active window is running an audio process (like a video, or a podcast, or a music file...).
Hope it will help those who don't want to be limited at the fullscreen checking.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.