Coder Social home page Coder Social logo

lightson's People

Contributors

darvelo avatar iye avatar namtabmai avatar unhammer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

lightson's Issues

The proposed changes

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

Arch Linux, or maybe new flash version issue

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

Opera

Could you please add Opera Browser support? Thank you.

There is no kscreensaver in current kde4

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

opera flash player detection

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

Maybe $delay_progs should be more strict?

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:

  • Don't match $delay_progs by whole 'command line' (remove -f option in pgrep)
  • Do exact match for $delay_progs (add -x option to pgrep)

Also, if legacy is an issue, then this behavior may be implemented as optional.

xprop returns: _NET_ACTIVE_WINDOW: not found.

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!

Syntax Error After Cloning Repo

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

Enhancement : small script to detect when a video is playing, NOT on fullscreen

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 ($i == "RUNNING") print $(i+1)}')

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.

why check for fullscreen ?

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 ?

[suggest] while playing video (regardless of fullscreen)

I find this to be a better option...
prevent powermangement while video is playing, rather than fullscreen only.

SET:

  1. flashplayer
  2. major video players in linux (mpv, mplayer, smplayer, vlc, etc)
  3. static-off for certain programs that don't use video (games, terminal, etc)

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?

gnome-screensaver?

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.

Detect Steam when running fullscrean games

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?

Enhancement: Use `xdg-screensaver` for broad desktop environment support

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.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.