rephus / chromium-screensaver

Use chromium to show full screen screensaver (requires xprintidle
1 stars 0 forks source link

Improved version suggestion #2

Open komoricodrutz opened 19 hours ago

komoricodrutz commented 19 hours ago

Here is the script I adapted, which allows you to use any different browser and also an ICE or progressive Web App (I'm using Linux Mint and a dedicated Web App so as not to interfere with normal browser operations). It also uses wmctrl instead of kill, because it gracefully quits the browser, thus avoiding those session restore interactive popups if the browser has been quit ungracefully for too many times. So you'll also have to install wmctrl besides xprintidle. I've also added comments with what each command does.

#!/bin/bash

# Variables to be modified if needed
#1: Time in seconds. Change as needed. As xprintidle works with milliseconds, this makes it easier for you to set the timeout value in seconds instead.
waitsec=3
#2: Enter your browser launch command herek
browser="firefox"
#3: Enter the command-line initialisation string for the browser
browsinitstrg="--class WebApp-TestScr8396 --name WebApp-TestScr8396 --profile --no-remote"
#4: If you are not using a web app, comment out the browseprofpath entry here.
#   If you are, then enter the profile path to that web app:
#   (for some reason, using "~" doesn't work from this script and I am too lazy to investigate why.
#   That's why the full profile path needs to be specified)
browsprofpath="/home/[your-user-ID]/.local/share/ice/firefox/TestScr8396"
#5: Enter the web url of the page you wish to display:
browsurl="http://about:blank"

# Variables which MIGHT need modifying
#1: If you are not using a web app, where a dedicated profile is needed, delete the $browseprofpath variable from here:
browstart="$browser $browsinitstrg $browsprofpath $browsurl"

# These variables do not really need modifying
# Multiplier for milliseconds. DO NOT TOUCH! xprintidle works with milliseconds:
multiplix=1000
# Seconds converted to milliseconds:
waitms=$((waitsec * multiplix))
# User-friendly translation output of seconds to milliseconds
echo $waitsec Seconds translates to $waitms Milliseconds

# Resets the browser ID variable for the initial run
browser_id=""

# Start the loop
while true; do 
    # Sets the variable to how long X has been idle
    idle=`xprintidle`
    # If the idle time has been greater than the waiting time, then starts running the commandsL
    if [ $idle -gt $waitms ]; then
    # If there is NO browser ID, then
    if [ -z $browser_id ]; then
        # Start the browser
        $browstart &
        # Get the browser process PID
        browser_id="$!"
        # User-friendly notification
        echo "Away, running screensaver (id:$browser_id)"
        # This sleep entry is necessary, as for some reason the wmctrlid variable below does not work if set to be run immediately:
        sleep 3
        # Get the wmctrl WID (window ID) for our browser ID
        wmctrlid="$(wmctrl -l -p | grep $browser_id | cut -c1-10)"
        # Set the browser window to full-screen:
        wmctrl -ir "$wmctrlid" -b toggle,fullscreen
    fi
    # If you moved the mouse, then xprintidle time has been reset to smaller than the wait time value, so the loop resets:
    # If there IS a browser ID, this means that the pseudo-screensaver is already running:
    elif [ $browser_id ]; then
        # User-friendly message for shutting down the pseudo-screensaver:
        echo "Welcome back, closing screensaver (id:$browser_id)"
        # Gracefully quit the browser:
        wmctrl -ic "$wmctrlid"
        # Resets the browser ID:
        browser_id=""
    fi
# Waits for a second before finishing the current loop
sleep 1
# Finish current loop
done

Enjoy!

komoricodrutz commented 18 hours ago

Ha! Works also with mplayer and a video file of your choice... If you wish to also play sound, you can remove the "-nosound" entry in the initialisation string. If you wish to set the volume, replace the "-nosound" option with "-volume xx" (in percent) to the initialisation string. For example, "-volume 40" worked fine to have my selected video at a non-disturbing audio level. Depending on your specific file, this value may differ, so you should experiment with that value a bit. You can most definitely experiment with the playlist option of mplayer.

LE: I have noticed that mplayer causes the idle time to reset randomly. Currently researching a way around it or using a different player

Example with sound at 30%: playrinitstrg="-volume 30 -fs -loop 0 -vo gl"

#!/bin/bash

# Variables to be modified if needed
#1: Time in seconds. Change as needed. As xprintidle works with milliseconds, this makes it easier for you to set the timeout value in seconds instead.
waitsec=3
#2: Enter your player launch command here
player="mplayer"
#3: Enter the command-line initialisation string for the player
playrinitstrg="-nosound -fs -loop 0 -fixed-vo -vo gl"
#4: If you are not using a web app, comment out the playrprofpath entry here.
#   If you are, then enter the profile path to that web app:
#   (for some reason, using "~" doesn't work from this script and I am too lazy to investigate why.
#   That's why the full profile path needs to be specified)
#   Left-over from the initial browser version. Not needed in this context.
#playrprofpath="/home/[your-user-id]/.local/share/ice/firefox/TestScr8396"
#5: Enter the path of the file or playlist you wish to display:
playrurl="/[path]/[to]/[video.mp4]"

# Variables which MIGHT need modifying
#1: If you are NOT using a web app, where a dedicated profile is needed, delete the $playrprofpath variable from here:
playrtart="$player $playrinitstrg $playrurl"

# These variables do not really need modifying
# Multiplier for milliseconds. DO NOT TOUCH! xprintidle works with milliseconds:
multiplix=1000
# Seconds converted to milliseconds:
waitms=$((waitsec * multiplix))
# User-friendly translation output of seconds to milliseconds
echo $waitsec Seconds translates to $waitms Milliseconds

# Resets the player ID variable for the initial run
player_id=""

# Start the loop
while true; do 
    # Sets the variable to how long X has been idle
    idle=`xprintidle`
    # If the idle time has been greater than the waiting time, then starts running the commandsL
    if [ $idle -gt $waitms ]; then
    # If there is NO player ID, then
    if [ -z $player_id ]; then
        # Start the player
        $playrtart &
        # Get the player process PID
        player_id="$!"
        # User-friendly notification
        echo "Away, running screensaver (id:$player_id)"
        # This sleep entry is necessary, as for some reason the wmctrlid variable below does not work if set to be run immediately:
        sleep 3
        # Get the wmctrl WID (window ID) for our player ID
        wmctrlid="$(wmctrl -l -p | grep $player_id | cut -c1-10)"
        # Set the player window to full-screen (left-over from the browser version. Not needed, as the initialisation string also has the full-screen entry:
        # wmctrl -ir "$wmctrlid" -b toggle,fullscreen
    fi
    # If you moved the mouse, then xprintidle time has been reset to smaller than the wait time value, so the loop resets:
    # If there IS a player ID, this means that the pseudo-screensaver is already running:
    elif [ $player_id ]; then
        # User-friendly message for shutting down the pseudo-screensaver:
        echo "Welcome back, closing screensaver (id:$player_id)"
        # Gracefully quit the player:
        wmctrl -ic "$wmctrlid"
        # Resets the player ID:
        player_id=""
    fi
# Waits for a second before finishing the current loop
sleep 1
# Finish current loop
done

Enjoy!

komoricodrutz commented 16 hours ago

Sooooo, I tried various solutions, because it seems there is no option to have mplayer stop resetting the idle time. Tested it out with vlc, but vlc has the behaviour that it reloads the player window on loop (same behaviour as WITHOUT the "-fixed-vo" option for mplayer, but apparently with no solution available), so the PID changes and the script can no longer close the vlc window on user interaction.

I tried it with mpv and it seems to work fine, without resetting the idle interval. You just need to add the "--stop-screensaver=no" option. And also install mpv, of course. Just as in the mplayer version, if you wish to hear the video sound, remove the "--no-audio" option or if you wish to hear audio, but at a specific volume, replace the "--no-audio" option with "--volume=xx", where "xx" stands for the audio volume percentage. Again, experiment with various percentage settings. As above, for me the value 40 worked fine: playrinitstrg="--stop-screensaver=no --fs --volume=40 --loop"

Here is the full script:

#!/bin/bash

# Variables to be modified if needed
#1: Time in seconds. Change as needed. As xprintidle works with milliseconds, this makes it easier for you to set the timeout value in seconds instead.
waitsec=3
#2: Enter your player launch command here
player="mpv"
#3: Enter the command-line initialisation string for the player
playrinitstrg="--stop-screensaver=no --fs --no-audio --loop"
#4: If you are not using a web app, comment out the playrprofpath entry here.
#   If you are, then enter the profile path to that web app:
#   (for some reason, using "~" doesn't work from this script and I am too lazy to investigate why.
#   That's why the full profile path needs to be specified)
#   Left-over from the initial browser version. Not needed in this context.
#playrprofpath="/home/[your-user-id]/.local/share/ice/firefox/TestScr8396"
#5: Enter the path of the file or playlist you wish to display:
playrurl="/[path]/[to]/[video.mp4]"

# Variables which MIGHT need modifying
#1: If you are NOT using a web app, where a dedicated profile is needed, delete the $playrprofpath variable from here:
playrtart="$player $playrinitstrg $playrurl"

# These variables do not really need modifying
# Multiplier for milliseconds. DO NOT TOUCH! xprintidle works with milliseconds:
multiplix=1000
# Seconds converted to milliseconds:
waitms=$((waitsec * multiplix))
# User-friendly translation output of seconds to milliseconds
echo $waitsec Seconds translates to $waitms Milliseconds

# Resets the player ID variable for the initial run
player_id=""

# Start the loop
while true; do 
    # Sets the variable to how long X has been idle
    idle=`xprintidle`
    # If the idle time has been greater than the waiting time, then starts running the commandsL
    if [ $idle -gt $waitms ]; then
    # If there is NO player ID, then
    if [ -z $player_id ]; then
        # Start the player
        $playrtart &
        # Get the player process PID
        player_id="$!"
        # User-friendly notification
        echo "Away, running screensaver (id:$player_id)"
        # This sleep entry is necessary, as for some reason the wmctrlid variable below does not work if set to be run immediately:
        sleep 3
        # Get the wmctrl WID (window ID) for our player ID
        wmctrlid="$(wmctrl -l -p | grep $player_id | cut -c1-10)"
        # Set the player window to full-screen (left-over from the browser version. Not needed, as the initialisation string also has the full-screen entry:
        # wmctrl -ir "$wmctrlid" -b toggle,fullscreen
    fi
    # If you moved the mouse, then xprintidle time has been reset to smaller than the wait time value, so the loop resets:
    # If there IS a player ID, this means that the pseudo-screensaver is already running:
    elif [ $player_id ]; then
        # User-friendly message for shutting down the pseudo-screensaver:
        echo "Welcome back, closing screensaver (id:$player_id)"
        # Gracefully quit the player:
        wmctrl -ic "$wmctrlid"
        # Resets the player ID:
        player_id=""
    fi
# Waits for a second before finishing the current loop
sleep 1
# Finish current loop
done

Enjoy!