💾 Archived View for sysrq.in › pub › distfiles › boincgfx-1.0.sh captured on 2022-06-11 at 20:44:59.

View Raw

More Information

⬅️ Previous capture (2021-12-03)

➡️ Next capture (2023-01-29)

-=-=-=-=-=-=-

#!/bin/sh
# Copyright (C) 2020-2021 CyberTailor <cyber@sysrq.in>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.

# Shows random BOINC graphics application
# Version: 1.0 (2021-08-02)

# To run this script you must have the following software installed:
#  - Xephyr
#  - xinit
#  - some window manager (like ratpoison or dwm)
#  - (optional) shuf from GNU coreutils

# The script can be controlled via the following environment variables:
#
# 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
# SWITCH_TIME = time the script waits before switching to another
#               graphics application
# TRANSITION_TIME = time the script waits before exiting the running screensaver
#                   when a new one is launched
# DISP = fake Xephyr display name
: "${BOINC_DIR:=/var/lib/boinc}"
: "${WINDOW_MANAGER:=ratpoison}"
: "${SWITCH_TIME:=300}"
: "${TRANSITION_TIME:=10}"
: "${DISP=:2}"

SHUF="shuf"
if ! command -v ${SHUF} >/dev/null; then
	SHUF="gshuf"
	if ! command -v ${SHUF} >/dev/null; then
		echo "!! shuff not found: the same application" \
			"will be launched all the time" 1>&2
		SHUF="head"
	fi
fi

if ! command -v "${WINDOW_MANAGER}" >/dev/null; then
	printf "!! %s not found: try different value of WINDOW_MANAGER environment variable\n" \
		"${WINDOW_MANAGER}" 1>&2
	exit 1
fi
WINDOW_MANAGER=$(command -v "${WINDOW_MANAGER}")

if [ ! -d "${BOINC_DIR}" ]; then
	printf "!! %s: no such directory: try different value of BOINC_DIR environment variable\n" \
		"${BOINC_DIR}" 1>&2
	exit 1
fi

trap "quit 0" INT # catch CTRL-C

# exit gracefully
quit() {
	echo "- Quitting..."
	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 "$1"
}

# `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
	sleep_time=$2
	[ -z "$pid" ] && \
		return
	[ -n "$sleep_time" ] && \
		sleep "$sleep_time" 2>/dev/null

	printf "  * stopping running graphics application (PID %d) " "$pid"
	while kill "$pid" 2>/dev/null; do
		printf ". "
		sleep 0.1
	done
	echo
}

# check whether graphics application launched successfully
# not used right now
check_window() {
	sleep "${TRANSITION_TIME}"

	DISPLAY="${DISP}" xdotool search --pid "$(cat /tmp/boincgfx.pid)" && return 0

	echo "  !! couldn't create window - leaving..." 1>&2
	wait_then_kill "$(cat /tmp/boincgfx.pid)"
	return 1
}

show_screensaver() {
	random_gfx=$(find "${BOINC_DIR}"/slots -name graphics_app | ${SHUF} -n 1)
	slot=$(dirname "$random_gfx")
	printf -- "- Found a slot with graphics: %s\n" "$slot"
	if grep -q suspend "$slot/boinc_mmap_file"; then
		echo "  * the app is suspended - leaving..."
		sleep 0.1
		return
	fi

	gfx_exec=$(sed "s/<soft_link>\(.*\)<\/soft_link>/\1/"  "$random_gfx")

	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 &
	printf "%d" $! > /tmp/boincgfx.pid
	printf "  * started a new graphics application (PID %d)\n" "$(cat /tmp/boincgfx.pid)"

	#check_window || return
	sleep "${SWITCH_TIME}"
}

rm /tmp/xephyr.pid 2>/dev/null
startx "${WINDOW_MANAGER}" -- "$(command -v Xephyr)" "${DISP}" -fullscreen >/dev/null 2>&1 &
printf "%d" "$!" > /tmp/xephyr.pid

sleep 5
if ps -p "$(cat /tmp/xephyr.pid)" >/dev/null 2>&1; then
	echo "- Started a Xephyr server"
else
	echo "!! Xephyr server crashed" 1>&2
	echo "!! Check WINDOW_MANAGER variable in the script" 1>&2
	quit 1
fi

while true; do
	show_screensaver

	if ! ps -p "$(cat /tmp/xephyr.pid)" >/dev/null 2>&1; then
		# Xephyr window was closed
		quit 0
	fi
done