sway: suspend powerplay mousepad led in sync with mouse

This commit is contained in:
Leonardo Eugênio 2024-08-17 00:02:05 -03:00
parent 49d0cf16e3
commit 324814f7e2
4 changed files with 103 additions and 1 deletions

View file

@ -128,7 +128,12 @@
auto_connect_gamepad = [ auto_connect_gamepad = [
bluez bluez
coreutils coreutils
gnugrep final.gnugrep
];
powerplay-led-idle = [
final.bash
libinput
libratbag
]; ];
} }
// lib.mapAttrs import_script { // lib.mapAttrs import_script {

79
scripts/powerplay-led-idle Executable file
View file

@ -0,0 +1,79 @@
#!/usr/bin/env bash
set -e
# Constants
SECONDS_UNTIL_FADE=$(( 1 * 60))
SECONDS_UNTIL_OFF=$(( 5 * 60))
COLOR_ON=ff0000
COLOR_FADE=880000
COLOR_OFF=000000
# Logging
if [[ "$1" = "debug" ]]; then
echo "Running with debugging" >&2
DEBUG="true"
SECONDS_UNTIL_FADE=$(( 3 ))
SECONDS_UNTIL_OFF=$(( 5 ))
fi
log() {
if [[ "$DEBUG" = "true" ]]; then
echo "$@" >&2
fi
}
# Implementation
main() {
CURRENT_STATE="UNKNOWN"
LAST_POINTER_MOTION="$(date +%s)"
if [ "$(ratbagctl list | wc -l)" -ne 1 ]; then
echo "Not exactly one device found, exiting..."
exit 1
fi
DEVICE="$(ratbagctl list | cut -d: -f1)"
while true; do
while read line; do
LAST_POINTER_MOTION="$(date +%s)"
break
done < <(
timeout 5s \
libinput debug-events \
| grep POINTER_MOTION
)
TIME_SINCE_LAST=$(( "$(date +%s)" - "$LAST_POINTER_MOTION" ))
log "Last pointer motion was $TIME_SINCE_LAST seconds ago"
if [ "$TIME_SINCE_LAST" -gt "$SECONDS_UNTIL_OFF" ]; then
setState OFF "$COLOR_OFF"
elif [ "$TIME_SINCE_LAST" -gt "$SECONDS_UNTIL_FADE" ]; then
setState FADE "$COLOR_FADE"
else
setState ON "$COLOR_ON"
fi
done
}
setState() {
STATE="$1"
COLOR="$2"
MODE="$3"
if [[ "$STATE" = "$CURRENT_STATE" ]]; then
log "Already in $STATE state"
return
fi
log "Changing state to $STATE"
CURRENT_STATE="$STATE"
ratbagctl "$DEVICE" led 0 set mode on
ratbagctl "$DEVICE" led 0 set color "$COLOR"
}
main

View file

@ -23,6 +23,7 @@ in
./swayidle.nix ./swayidle.nix
./swaylock.nix ./swaylock.nix
./theme.nix ./theme.nix
./powerplay-led-idle.nix
]; ];
config = lib.mkIf (config.my.desktop == "sway") { config = lib.mkIf (config.my.desktop == "sway") {
services.mako.enable = true; services.mako.enable = true;

View file

@ -0,0 +1,17 @@
{ pkgs, lib, ... }:
{
systemd.user.services.powerplay-led-idle = {
Unit = {
Description = "Autosuspend Powerplay mousepad led";
PartOf = [ "graphical-session.target" ];
After = [ "graphical-session.target" ];
};
Service = {
ExecStart = lib.getExe pkgs.powerplay-led-idle;
Restart = "on-failure";
};
Install = {
WantedBy = [ "sway-session.target" ];
};
};
}