From 1b17f498ab78560a43988cc84b864d7f92f456a1 Mon Sep 17 00:00:00 2001 From: Sergey Lvov Date: Mon, 4 Jul 2022 17:00:29 +0200 Subject: [PATCH] Decouple WhenBatteryIsNotCharging/WhenBatteryIsAtWarnLevel and minimal capacity in parameter `hbfx-ahbm` --- Changelog.md | 1 + HibernationFixup/kern_hbfx.cpp | 11 +++++------ README.md | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Changelog.md b/Changelog.md index 193d4ec..91dc53e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,7 @@ HibernationFixup Changelog ============================ #### v1.4.6 - Added constants for macOS 13 support +- Decouple WhenBatteryIsNotCharging/WhenBatteryIsAtWarnLevel and minimal capacity in parameter `hbfx-ahbm` #### v1.4.5 - When battery level is critical, try to put macOS into sleep/hibernate mode only once per minute. diff --git a/HibernationFixup/kern_hbfx.cpp b/HibernationFixup/kern_hbfx.cpp index c151a94..8957638 100644 --- a/HibernationFixup/kern_hbfx.cpp +++ b/HibernationFixup/kern_hbfx.cpp @@ -381,8 +381,7 @@ IOReturn HBFX::X86PlatformPlugin_sleepPolicyHandler(void * target, IOPMSystemSle forceHibernate = true; } - if (!forceHibernate && (whenBatteryIsAtWarnLevel || whenBatteryAtCriticalLevel) && minimalRemainingCapacity != 0 && - power_source->capacityPercentRemaining() <= minimalRemainingCapacity) + if (!forceHibernate && minimalRemainingCapacity != 0 && power_source->capacityPercentRemaining() <= minimalRemainingCapacity) { DBGLOG("HBFX", "Auto hibernate: capacity remaining: %d less than minimal: %d, force to hibernate", power_source->capacityPercentRemaining(), minimalRemainingCapacity); forceHibernate = true; @@ -557,8 +556,9 @@ void HBFX::processKernel(KernelPatcher &patcher) bool auto_hibernate_mode_on = (ADDPR(hbfx_config).autoHibernateMode & Configuration::EnableAutoHibernation); bool whenBatteryIsAtWarnLevel = (ADDPR(hbfx_config).autoHibernateMode & Configuration::WhenBatteryIsAtWarnLevel); bool whenBatteryAtCriticalLevel = (ADDPR(hbfx_config).autoHibernateMode & Configuration::WhenBatteryAtCriticalLevel); + int minimalRemainingCapacity = ((ADDPR(hbfx_config).autoHibernateMode & 0xF00) >> 8); - if (whenBatteryIsAtWarnLevel || whenBatteryAtCriticalLevel) { + if (whenBatteryIsAtWarnLevel || whenBatteryAtCriticalLevel || minimalRemainingCapacity != 0) { if (!checkCapacityTimer) { if (!workLoop) workLoop = IOWorkLoop::workLoop(); @@ -589,7 +589,7 @@ void HBFX::processKernel(KernelPatcher &patcher) } } - if (auto_hibernate_mode_on || whenBatteryIsAtWarnLevel || whenBatteryAtCriticalLevel) { + if (auto_hibernate_mode_on || whenBatteryIsAtWarnLevel || whenBatteryAtCriticalLevel || minimalRemainingCapacity != 0) { KernelPatcher::RouteRequest requests[] = { {"__ZN14IOPMrootDomain14evaluatePolicyEij", IOPMrootDomain_evaluatePolicy, orgIOPMrootDomain_evaluatePolicy}, {"__ZN14IOPMrootDomain17willEnterFullWakeEv", IOPMrootDomain_willEnterFullWake, orgIOPMrootDomain_willEnterFullWake}, @@ -1156,8 +1156,7 @@ void HBFX::checkCapacity() forceSleep = true; } - if (!forceSleep && (whenBatteryIsAtWarnLevel || whenBatteryAtCriticalLevel) && minimalRemainingCapacity != 0 && - power_source->capacityPercentRemaining() <= minimalRemainingCapacity) + if (!forceSleep && minimalRemainingCapacity != 0 && power_source->capacityPercentRemaining() <= minimalRemainingCapacity) { DBGLOG("HBFX", "Auto hibernate: capacity remaining: %d less than minimal: %d, force to sleep", power_source->capacityPercentRemaining(), minimalRemainingCapacity); forceSleep = true; diff --git a/README.md b/README.md index 8a4b1ac..7917d3c 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,8 @@ IOHibernateRTCVariables from the system registry and writes it to NVRAM. - `DisableStimulusDarkWakeActivityTickle` = 128: Disable power event kStimulusDarkWakeActivityTickle in kernel, so this event cannot trigger a switching from dark wake to full wake - Next 4 bits are used to specify minimal capacity percent remaining value when hibernation will be forced. - Can be used together with WhenBatteryIsAtWarnLevel or WhenBatteryAtCriticalLevel, when IOPMPowerSource cannot detect warning or critical battery level + When when IOPMPowerSource cannot detect warning or critical battery level (it happens on hackintosh), + next 4 bits can be used to specify minimal capacity percent remaining value when hibernation will be forced: - `RemainCapacityBit1` = 256 (1) - `RemainCapacityBit2` = 512 (2) - `RemainCapacityBit3` = 1024 (4)