Skip to content

Commit

Permalink
Decouple WhenBatteryIsNotCharging/WhenBatteryIsAtWarnLevel and minima…
Browse files Browse the repository at this point in the history
…l capacity in parameter `hbfx-ahbm`
  • Loading branch information
Sergey Lvov committed Jul 4, 2022
1 parent a4a1d52 commit 1b17f49
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 5 additions & 6 deletions HibernationFixup/kern_hbfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1b17f49

Please sign in to comment.