From 7cb02514ad3a51ac98326e71c7edad8e6ae23fde Mon Sep 17 00:00:00 2001 From: Mark Nipper Date: Fri, 24 Jan 2025 15:20:50 -0800 Subject: Add xscreensaver hot corners script --- home/nipsy/common/optional/desktops/i3/default.nix | 7 ++ .../optional/desktops/i3/xscreensaver-activate | 94 ++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100755 home/nipsy/common/optional/desktops/i3/xscreensaver-activate (limited to 'home') diff --git a/home/nipsy/common/optional/desktops/i3/default.nix b/home/nipsy/common/optional/desktops/i3/default.nix index c7a521d..bbfc50c 100644 --- a/home/nipsy/common/optional/desktops/i3/default.nix +++ b/home/nipsy/common/optional/desktops/i3/default.nix @@ -36,6 +36,8 @@ foreground = #e5e5e5 cursor-color = #ffffff ''; + + "bin/xscreensaver-activate".source = ./xscreensaver-activate; }; packages = [ @@ -79,6 +81,11 @@ always = true; notification = false; } + { + command = "~/bin/xscreensaver-activate"; + always = true; + notification = false; + } ]; window.border = 0; window.commands = [ diff --git a/home/nipsy/common/optional/desktops/i3/xscreensaver-activate b/home/nipsy/common/optional/desktops/i3/xscreensaver-activate new file mode 100755 index 0000000..b558da8 --- /dev/null +++ b/home/nipsy/common/optional/desktops/i3/xscreensaver-activate @@ -0,0 +1,94 @@ +#!/usr/bin/env zsh + +# record our own PID to avoid duplicate invocations +PIDFILE="/dev/shm/${0:t}.pid" + +# check for already running script +if [[ -f ${PIDFILE} ]]; then + for i in $(pidof -x ${0:t}); do + if [[ ${i} -eq $(cat ${PIDFILE}) ]]; then + echo "${0:t} already running!" >&2 + exit 1 + fi + done +fi + +# record current PID +echo ${$} > ${PIDFILE} + +# wait a bit for everything to start +sleep 30 + +# check whether xscreensaver itself is running and start it if not +if ! systemctl --user --quiet is-active xscreensaver.service; then + systemctl --user start xscreensaver.service +fi + +# retrieve current Xorg screen size so we know where the corners are +xrandr | grep ^Screen | grep -Eo 'current [[:digit:]]+ x [[:digit:]]+' | cut -d' ' -f2,4 | read max_x max_y +echo "read screen size as ${max_x} x ${max_y}" + +# main loop +while true; do + + # retrieve current mouse position and set environment variables + eval $(xdotool getmouselocation --shell) + + # keep xscreensaver deactivated if we're in the top right corner of the screen + if [[ ${X} -eq $((max_x - 1)) && ${Y} -eq $((max_y - max_y)) ]]; then + + # make sure xscreensaver is even running before telling it to stay idle + if pidof xscreensaver &>/dev/null; then + sleep 5 + xscreensaver-command -deactivate &>/dev/null + fi + + # mouse is in the top left corner -- potentially activate xscreensaver right now + elif [[ ${X} -eq $((max_x - max_x)) && ${Y} -eq $((max_y - max_y)) ]]; then + + # sleep a bit and then check mouse coordinates again + sleep 5 + eval $(xdotool getmouselocation --shell) + + # mouse is still in the top left corner -- we must really want to activate xscreensaver + if [[ ${X} -eq $((max_x - max_x)) && ${Y} -eq $((max_y - max_y)) ]]; then + + # make sure xscreensaver is actually running + if pidof xscreensaver &>/dev/null; then + + # make sure it hasn't already activated + if ! xscreensaver-command -time | grep -q 'screen blanked since'; then + + # activate it! + xscreensaver-command -activate &>/dev/null + + fi + + fi + + fi + + # a Steam game is running + elif ps axfu | grep '/home/nipsy/.steam/debian-installation/steamapps/common' | grep -qv grep; then + + # make sure xscreensaver is even running before telling it to stay idle + if pidof xscreensaver &>/dev/null; then + sleep 5 + xscreensaver-command -deactivate &>/dev/null + fi + + fi + + # die off if we're no longer running on a connected Xorg screen any longer -- this should be the normal termination path for this script + if ! xdpyinfo &>/dev/null; then + rm ${PIDFILE} + exit 0 + fi + + # arbitrary sleep for script to avoid CPU sucking infinite loop + sleep 5 + +done + +# exit with error since we should never get here +exit 1 -- cgit v1.2.3