Hi everyone,
I recently made the switch to a labwc + Waybar environment. While I’m enjoying the experience, I haven’t found an automatic screen locking tool that perfectly suits my needs, particularly when watching videos.
To address this, I’ve wrote a custom script. This script detects video playback and temporarily disables automatic screen locking during viewing. When no video is playing, it reverts to the standard timeout-based screen lock.
I plan to share my script code here, as it might be helpful to others with similar requirements.
In addition, I’m interested in learning if anyone knows of other screen locking tools that might better align with my specific needs. If so, I’d appreciate any recommendations. Also, if you notice any potential issues or areas for improvement in my script, please provide your feedback.
Thank you for your assistance!
#!/bin/bash
# Set the screen lock delay (seconds)
IDLE_TIMEOUT=300
SCREEN_OFF_AFTER_IDLE=320
LOCK_SCREEN_CMD="swaylock --daemonize -c 000000 --ignore-empty-password"
SCREEN_OFF_CMD="wlopm --off '*'"
SCREEN_ON_CMD="wlopm --on '*'"
swayidle_pid="" # Initialize swayidle_pid
is_media_playing() {
playerctl status | grep -q "Playing"
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
start_swayidle() {
swayidle \
timeout "$IDLE_TIMEOUT" "$LOCK_SCREEN_CMD" \
timeout "$SCREEN_OFF_AFTER_IDLE" "$SCREEN_OFF_CMD" \
resume "$SCREEN_ON_CMD" >/dev/null 2>&1 &
swayidle_pid=$! # Get the swayidle process ID
}
kill_swayidle() {
if [ -n "$swayidle_pid" ]; then
echo "Stopping swayidle $swayidle_pid"
pkill -TERM -P "$swayidle_pid" || kill -9 "$swayidle_pid" # Stop swayidle
if [ $? -eq 0 ]; then
wait "$swayidle_pid" 2>/dev/null # Wait for swayidle to finish
swayidle_pid="" # Clear the swayidle process ID
else
echo "Failed to stop swayidle"
fi
fi
}
trap 'kill_swayidle' EXIT # Ensure that swayidle is stopped on script exit
while true; do
if is_media_playing; then
echo "Media Playing"
kill_swayidle
else
if [ -z "$swayidle_pid" ]; then
echo "Starting swayidle"
start_swayidle
else
echo "Swayidle already running"
fi
fi
sleep 1
done