distribution/packages/sysutils/powerstate/sources/powerstate.sh

51 lines
1.3 KiB
Bash
Raw Normal View History

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2022-present Fewtarius (https://github.com/fewtarius)
2022-12-21 22:24:01 +00:00
###
### Normally this would be a udev rule, but some devices like the AyaNeo Air
### do not properly report the applied power state to udev, and we can't use
### inotifyd to watch the status in /sys.
###
. /etc/profile
2022-12-21 22:24:01 +00:00
while true
do
STATUS="$(cat /sys/class/power_supply/{BAT*,bat*}/status 2>/dev/null)"
${DEBUG} && echo "Status: ${STATUS}"
if [ ! "${STATUS}" = "${CURRENT_MODE}" ]
then
${DEBUG} && echo "Status changed."
case ${STATUS} in
Disch*)
log $0 "Switching to battery mode."
if [ "$(get_setting system.powersave)" = 1 ]
2022-12-21 22:24:01 +00:00
then
audio_powersave 1
2022-12-21 22:24:01 +00:00
perftune battery
performance_level auto
power_dpm_state battery
pcie_aspm_policy powersave
device_powersave 1
2022-12-21 22:24:01 +00:00
fi
;;
*)
log $0 "Switching to performance mode."
if [ "$(get_setting system.powersave)" = 1 ]
2022-12-21 22:24:01 +00:00
then
audio_powersave 0
perftune performance
performance_level profile_peak
2022-12-21 22:24:01 +00:00
power_dpm_state performance
pcie_aspm_policy default
device_powersave 0
2022-12-21 22:24:01 +00:00
fi
;;
esac
fi
CURRENT_MODE="${STATUS}"
${DEBUG} && echo "Current Mode: ${CURRENT_MODE}"
sleep 2
done