Skip to content

Instantly share code, notes, and snippets.

@CyberTailor
Last active Jan 16, 2022
Embed
What would you like to do?
Shell script to emulate BOINC screensaver behavior
#!/bin/sh
# This script shows random BOINC graphics application
# To run this script you must have the following software installed:
# - Xephyr
# - xinit
# - some window manager (like ratpoison or dwm)
# BOINC_DIR = `/var/lib/boinc` for boinc>=7.10.2, `/var/lib/boinc-client` for older versions
# WINDOW_MANAGER = command to launch your preferred window manager (shold contain absolute path to the executable)
# SWITCH_TIME = time the script will wait before switching to another graphics application
# TRANSITION_TIME = time to wait before killing running screensaver after launching a new one
# DISP = fake Xephyr display name
BOINC_DIR=/var/lib/boinc
WINDOW_MANAGER=/usr/bin/ratpoison
SWITCH_TIME=300
TRANSITION_TIME=10
DISP=:2
trap "quit 0" INT # catch CTRL-C
# exit gracefully
quit() {
echo "- Quitting..."
exit_status=$1
for pidfile in boincgfx xephyr; do
old_pid=$(cat /tmp/$pidfile.pid 2>/dev/null)
wait_then_kill "$old_pid" >/dev/null &
done
rm /tmp/boincgfx.pid /tmp/xephyr.pid 2>/dev/null
exit "$exit_status"
}
# `wait_then_kill 3856 10`: waits 10 seconds and kills a process with PID 3856
# `wait_then_kill 3856`: kills a process with PID 3856 immediately
wait_then_kill() {
pid=$1
[ -z "$pid" ] && return
sleep "$TRANSITION_TIME" 2>/dev/null
echo -n " * killing running graphics application (PID $pid) "
while kill "$pid" 2>/dev/null; do
echo -n ". "
sleep 0.1
done
echo
}
# check whether graphics application has open windows
check_window() {
sleep $TRANSITION_TIME
DISPLAY=$DISP xdotool search --pid "$(cat /tmp/boincgfx.pid)" && return 0
echo " !! couldn't create window - leaving..."
wait_then_kill "$(cat /tmp/boincgfx.pid)" 0
return 1
}
show_screensaver() {
random_gfx=$(find $BOINC_DIR/slots/* -name graphics_app | shuf -n 1)
slot=$(dirname "$random_gfx")
echo "- Found a slot with graphics: $slot"
grep -q suspend "$slot/boinc_mmap_file" && {
echo " !! the app is suspended - leaving..."
return
}
gfx_exec=$(cat "$random_gfx")
gfx_exec=${gfx_exec#<soft_link>}
gfx_exec=${gfx_exec%</soft_link>}
if [ -f /tmp/boincgfx.pid ]; then
old_pid=$(cat /tmp/boincgfx.pid)
wait_then_kill "$old_pid" "$TRANSITION_TIME" &
rm /tmp/boincgfx.pid
fi
cd "$slot" || return
DISPLAY=$DISP $gfx_exec >/dev/null 2>&1 &
echo $! > /tmp/boincgfx.pid
echo " * started new graphics application (PID $(cat /tmp/boincgfx.pid))"
#check_window || return
sleep $SWITCH_TIME
}
rm /tmp/xephyr.pid 2>/dev/null
startx "$WINDOW_MANAGER" -- /usr/bin/Xephyr $DISP -fullscreen >/dev/null 2>&1 &
echo $! > /tmp/xephyr.pid
sleep 5
if pgrep -F /tmp/xephyr.pid; then
echo "- Started Xephyr server"
else
echo "!! Xephyr server crashed"
echo "!! Check WINDOW_MANAGER variable in the script"
quit 1
fi
while true; do
show_screensaver
pgrep -F /tmp/xephyr.pid || quit 0
done
@CyberTailor
Copy link
Author

CyberTailor commented Apr 17, 2020

Installing dependencies

  1. Ubuntu/Debian
    $ sudo apt-get install xserver-xephyr ratpoison
  2. Fedora
    $ sudo dnf install xorg-x11-server-Xephyr ratpoison
  3. Arch Linux
    # pacman -S xorg-server-xephyr ratpoison
  4. Gentoo
    • emerge x11-base/xorg-server[xephyr] and x11-wm/ratpoison

@spencerdumbkid
Copy link

spencerdumbkid commented Jun 19, 2020

Thank you!

@CyberTailor
Copy link
Author

CyberTailor commented Aug 13, 2021

Updated version can be found there:
https://sysrq.in/pub/distfiles/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment