You are not logged in.

#1 2021-06-28 08:41:10

AmitGold
Member
Registered: 2019-07-08
Posts: 39

How to toggle caffeine from terminal

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

#2 2021-07-01 16:41:31

natervance
Member
Registered: 2017-04-20
Posts: 53

Re: How to toggle caffeine from terminal

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
fi

It's toggled using

bindsym $mod+i exec ~/.config/i3/caffeine.sh

Then my i3blocks config has the following block:

# Caffiene
[caffeine]
interval=once
signal=2

and the ~/.config/i3blocks/caffeine/caffeine script is as follows:

if pgrep xss-lock &> /dev/null; then
    echo ""
else
    echo "Caffeine"
fi

It 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

#3 2021-07-02 01:29:29

jonno2002
Member
Registered: 2016-11-21
Posts: 844

Re: How to toggle caffeine from terminal

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 0

Offline

#4 2021-07-06 14:45:28

AmitGold
Member
Registered: 2019-07-08
Posts: 39

Re: How to toggle caffeine from terminal

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 0

toggle_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 ﯈
fi

Please 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

Board footer

Powered by FluxBB