Caffeine-i3WM

“Caffeine” Module for Polybar & i3WM

Posted by

Arch Linux is my distribution of choice. It’s installed on all my computers and GNOME is what I’ve always used as a Desktop Environment.

Recently I came across an Arch based distro called Axyl OS while I was looking at a low resource distribution to install on my ageing Xiaomi Air 12.5 laptop and did not want to put in the time of installing vanilla Arch. Like their website says, “Axyl OS is a Linux distro for tiling window managers” and comes with a beautiful pre-configured i3WM desktop by default.

As it was the first time using a tiling window manager I was kind of lost. I got used to it pretty quickly but then went down the rabbit hole of trying to make i3WM behave like the GNOME desktop I was used to.

I got everything working like I wanted and was starting to feel right at home in i3WM. Well almost everything! It was when I had a browser window open on my laptop watching a video while working on my desktop that I noticed that something was missing! The laptop’s screen turned off mid viewing.

Axyl OS, like many distros with tiling window managers, rely on other desktop power management tools as, like the name says, i3 is just a window manager, not a complete desktop environment. In this case Axyl OS comes with XFCE’s power management tools. You can setup the screen turn off feature on and off but I wanted something I could control on the fly and on demand. Just like the Caffeine extension on GNOME or Windows.

Endeavour OS has a similar feature in the Sway edition which uses a module on the Waybar to inhibit screen sleep. However that version of Endeavour OS uses Wayland and Sway. I needed something that would work on Xorg, Polybar and i3.

I searched Google and even made a post on Reddit. When I couldn’t find anything I made my own “Caffeine” module for Polybar.

Dependencies

I haven’t tested this module on any other distros but it should work on any distro which uses the i3WM (it may even work on other tiling window managers as long as you know how to edit their config files).

However there are some dependencies that are needed for this to work:

  • xfce4-power-manager
  • polybar
  • dunst #Optional – For showing notifications
  • Nerd Fonts / FontAwesome #To use for the toggle icons on the Polybar module

Note: If you are using a system tray you can use the xfce4-power-manager applet to toggle presentation mode on/off and get the same functionality but I wanted something that would integrate with Polybar and provide a visual feedback when enabled/disabled.

HOWTO

The script I made uses the xfce4-power-manager and a custom IPC module in polybar with an icon to toggle it on & off.

Create a script called “Caffeine” – no quotes – in /usr/local/bin with the following code:

#!/bin/env bash

status=`xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -v`
if [ $status == 'false' ];
then
  polybar-msg hook caffeine 2
  xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -T && \
  dunstify 'Caffeine Enabled!'
else
  polybar-msg hook caffeine 1
  xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/presentation-mode -T && \
  dunstify 'Caffeine Disabled!'
fi

Then create the module by editing the modules file in your Polybar config folder (in Axyl OS this is located in $HOME/.config/i3/polybar/modules) to point to the script with a toggle:

[module/caffeine]
type = custom/ipc
hook-0 = echo   #Use a Nerd or FontAwesome icon for the off state
hook-1 = echo   #Use a Nerd or FontAwesome icon for the on state
click-left = /usr/local/bin/caffeine
initial = 1
format-foreground = ${color.background}
format-background = ${color.magenta}

Then just add the module in your polybar by editing your polybar config file (in Axyl OS this is located in $HOME/.config/i3/polybar/config) and adding “caffeine” – no quotes – in the module line.

TIP: To find an icon to use for the on/off states of the module you can refer to the Nerd Fonts Cheatsheet or the FontAwesome cheatsheet (depending on what you are using). Just copy and paste the icon from the corresponding site when editing the modules file above.

Log out or restart i3WM and you should now have an icon on your Polybar which you can left click to enable/disable screen idle turn off on the fly!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments