Skip to content

Instantly share code, notes, and snippets.

@unennhexium
Last active January 5, 2025 22:42
Show Gist options
  • Select an option

  • Save unennhexium/60a1ff418fa7f4d53d061ec1baab96f3 to your computer and use it in GitHub Desktop.

Select an option

Save unennhexium/60a1ff418fa7f4d53d061ec1baab96f3 to your computer and use it in GitHub Desktop.
Presentation mode button for Waybar+Sway+Swayidle
~/.config
├── sway
│ ├── config
│ ├── presentation_mode.sh
│ ├── swayidle.sh
│ └── swayidle_status
└── waybar
└── config.jsonc
/tmp
└── presentation_mode_pipe
set $CONFIG ~/.config/sway
exec $CONFIG/swayidle.sh
{
"modules-center" : [ "custom/presentation_mode" ],
"custom/presentation_mode" :
{
"exec" : "~/.config/sway/presentation_mode.sh",
"on-click" : "~/.config/sway/presentation_mode.sh --toggle",
"return-type" : "json",
"exec-on-event" : true,
"tooltip" : true,
},
}
#! /usr/bin/bash
read -r -d '' ON <<- EOM
{
"text": "⚪",
"alt": "on",
"tooltip": "Swayidle is off",
"class": ["on"]
}
EOM
read -r -d '' OFF <<- EOM
{
"text": "⚫",
"alt": "off",
"tooltip": "Swayidle is on",
"class": ["off"]
}
EOM
SRC_DIR=`dirname "$(readlink -f "${BASH_SOURCE[0]}")"`
SIDLE_STS="$SRC_DIR/swayidle_status"
PMODE_PIPE='/tmp/presentation_mode_pipe'
toggle_state() {
if [[ $1 == '1' ]]; then
pkill swayidle
echo 0 > "$SIDLE_STS"
echo 1 > $PMODE_PIPE
else
swayidle -w -S seat0 &>/dev/null & disown
echo 1 > "$SIDLE_STS"
echo 0 > $PMODE_PIPE
fi
}
send_msg() {
[[ $1 == '1' ]] && MSG="$ON" || MSG="$OFF"
stdbuf -oL -eL \
echo "$MSG" | \
jq --unbuffered \
--compact-output \
--monochrome-output
}
if [[ $1 == '--toggle' ]]; then
toggle_state `cat "$SIDLE_STS"`
else
send_msg $((`cat "$SIDLE_STS"` ^ 1))
rm -f $PMODE_PIPE
mkfifo $PMODE_PIPE
while true; do
send_msg `cat $PMODE_PIPE`
done
fi
#! /usr/bin/bash
SRC_DIR=`dirname "$(readlink -f "${BASH_SOURCE[0]}")"`
ENABLED=`cat "$SRC_DIR/swayidle_status"`
if [[ $ENABLED == '1' ]]; then
swayidle -w -S seat0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment