You are not logged in.
Hi, I've been using a tiling wm for the first time, and I'm using caffeine (caffeine-ng specifically).
I want to set a keybinding to toggle it, but I couldn't find a way (I also tried other caffeine programs like: caffeine, nightlight)
Is there a way to do this?
Offline
In terms of alternatives, my method (in i3) is to roll my own caffeine-like substitute. I have the following in ~/.config/i3/caffeine.sh
if pgrep xss-lock &> /dev/null; then
pkill xss-lock && pkill -RTMIN+2 i3blocks && xset s off -dpms
else
xss-lock --transfer-sleep-lock -- i3lock -c 000000 --nofork &
xset s on dpms
pkill -RTMIN+2 i3blocks
fiIt's toggled using
bindsym $mod+i exec ~/.config/i3/caffeine.shThen my i3blocks config has the following block:
# Caffiene
[caffeine]
interval=once
signal=2and the ~/.config/i3blocks/caffeine/caffeine script is as follows:
if pgrep xss-lock &> /dev/null; then
echo ""
else
echo "Caffeine"
fiIt may be possible to make caffeine-ng play nicely with i3 (or other non-gnome non-kde desktops), but I wasn't able to make it work (I couldn't even get it to work in the keeps-the-screen-on sort of way, let alone a keyboard shortcut to toggle). But if you're open to alternatives, that's how I do it.
Offline
on my media pc i gave up on caffeine when i found this simple script
#!/bin/bash
# xscreensaverstopper.sh
# This script is licensed under GNU GPL version 2.0 or above
# Uses elements from lightsOn.sh
# Copyright (c) 2011 iye.cba at gmail com
# url: https://github.com/iye/lightsOn
# This script is licensed under GNU GPL version 2.0 or above
# Description: Restarts xscreensaver's idle countdown while
# full screen applications are running.
# Checks every 30 seconds to see if a full screen application
# has focus, if so then the xscreensaver is told to restart
# its idle countdown.
# enumerate all the attached screens
displays=""
while read id
do
displays="$displays $id"
done< <(xvinfo | sed -n 's/^screen #\([0-9]\+\)$/\1/p')
checkFullscreen()
{
# loop through every display looking for a fullscreen window
for display in $displays
do
#get id of active window and clean output
activ_win_id=`DISPLAY=:0.${display} xprop -root _NET_ACTIVE_WINDOW`
activ_win_id=${activ_win_id:40:9}
# Check if Active Window (the foremost window) is in fullscreen state
isActivWinFullscreen=`DISPLAY=:0.${display} xprop -id $activ_win_id | grep _NET_WM_STATE_FULLSCREEN`
if [[ "$isActivWinFullscreen" == *NET_WM_STATE_FULLSCREEN* ]];then
xscreensaver-command -deactivate
fi
done
}
while sleep $((30)); do
checkFullscreen
done
exit 0Offline
Thanks!
I've combined your solutions and added the functionality of enabling caffeine automatically when a specific runs:
caffeine.sh:
#!/bin/bash
isActive=0
isManual=0
function enable_caffeine {
if [[ $isActive == 0 ]]; then
dunstify -r 6969 "Caffeine Enabled"
xset s off -dpms
echo 1 > ~/.config/qtile/caffeine/isActive
fi
isManual=$1
isActive=1
}
function disable_caffeine {
if [[ $isActive == 1 ]]; then
dunstify -r 6969 "Caffeine Disabled"
xset s on dpms
echo 0 > ~/.config/qtile/caffeine/isActive
fi
isActive=0
isManual=0
}
function toggle_caffeine {
if [[ $isActive == 1 ]]; then
disable_caffeine
else
enable_caffeine 1
fi
}
trap toggle_caffeine 10
trap disable_caffeine EXIT
displays=""
while read id
do
displays="$displays $id"
done< <(xvinfo | sed -n 's/^screen #\([0-9]\+\)$/\1/p')
function join_by {
local IFS="$1";
shift;
echo "$*";
}
importantPrograms=(zoom)
importantProgramsRunning() {
regex=$(join_by '|' "${importantPrograms[@]}")
procNum=$(ps aux | grep -E "$regex" | sed '$d' | wc -l)
if [[ $procNum > 0 ]]; then
true
else
false
fi
}
checkFullscreen()
{
bool=0
# loop through every display looking for a fullscreen window
for display in $displays
do
#get id of active window and clean output
activ_win_id=`DISPLAY=:0.${display} xprop -root _NET_ACTIVE_WINDOW`
activ_win_id=${activ_win_id:40:9}
# Check if Active Window (the foremost window) is in fullscreen state
isActivWinFullscreen=`DISPLAY=:0.${display} xprop -id $activ_win_id | grep _NET_WM_STATE_FULLSCREEN`
if [[ "$isActivWinFullscreen" == *NET_WM_STATE_FULLSCREEN* ]]; then
bool=1
fi
done
if [[ $bool == 1 ]]; then
true
else
false
fi
}
while true; do
if (checkFullscreen || importantProgramsRunning); then
enable_caffeine 0
elif [[ $isManual == 0 ]]; then
disable_caffeine
fi
sleep 15 &
wait $!
done
exit 0toggle_caffine.sh:
#!/bin/bash
kill -10 $(pidof -x "caffeine.sh")is_caffine_active.sh:
#!/bin/bash
isActive=$(cat ~/.config/qtile/caffeine/isActive)
if [[ $isActive == 1 ]]; then
echo
else
echo
fiPlease share if you have any suggestions as this is my first time doing serious work on bash scripting
Last edited by AmitGold (2021-07-06 14:46:34)
Offline