You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Looks like we used to have an initial capacity set from CallNtPowerInformation. The other place we use that in the code (Processor Frequency) has a note that approach broke in Win11 23H2, so it probably broke here too.
And the problem is that we calculated the more accurate values but never re-did the calculation of capacity with it. 🤦
The fix adds a single line of code to recalculate psRemainingCapacityPercent after the IOCTL queries update both psCurrentCapacity and psMaxCapacity
This ensures the reported percentage matches the authoritative per-battery IOCTL data rather than using stale or defaulted Phase 1 values
The fix mirrors the existing calculation pattern for consistency and requires no new abstractions or refactoring
Implementation Steps
Fix Battery Percentage Calculation
This phase addresses the root cause of the bug by ensuring psRemainingCapacityPercent is recalculated after the IOCTL queries update the capacity values.
Add Percentage Recalculation After IOCTL Status Query
After the IOCTL BATTERY_STATUS query successfully retrieves the current capacity, recalculate the remaining capacity percentage using the IOCTL-sourced values.
Update oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.java in the getPowerSource() method
Inside the IOCTL_BATTERY_QUERY_STATUS success block (after psCurrentCapacity = bs.Capacity is assigned), add the recalculation: psRemainingCapacityPercent = Math.min(1d, (double) psCurrentCapacity / psMaxCapacity)
Place this after all status values are extracted (after the psAmperage calculation) but still within the same if block that confirms the IOCTL call succeeded
Use the same formula as Phase 1 (line 124) to maintain consistency: Math.min(1d, (double) psCurrentCapacity / psMaxCapacity)
No additional guard for division by zero is needed since psMaxCapacity is initialized to 1 and the existing Phase 1 code follows the same pattern
🤖 Prompt for AI agents
Fix the battery percentage bug in `WindowsPowerSource.java` by recalculating
`psRemainingCapacityPercent` after the IOCTL phase overwrites the capacity
values.
- File to modify:
`oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.java`
- In the `getPowerSource()` method, locate the `IOCTL_BATTERY_QUERY_STATUS`
success block where `psCurrentCapacity = bs.Capacity` is assigned
- After all status values are extracted from the IOCTL result (after the
`psAmperage` calculation), add the following recalculation still within the same
`if` block that confirms the IOCTL call succeeded:
- `psRemainingCapacityPercent = Math.min(1d, (double) psCurrentCapacity /
psMaxCapacity)`
- Use this exact formula to mirror the existing Phase 1 calculation at line 124
for consistency
- No division-by-zero guard is needed since `psMaxCapacity` is initialized to 1
and the existing Phase 1 code follows the same pattern
Research
The battery implementation for Windows lives in oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.java. The getPowerSource() method retrieves battery data in two phases: first via CallNtPowerInformation (PowrProf API) to get SystemBatteryState, then via DeviceIoControl (IOCTL) to query per-battery BATTERY_INFORMATION and BATTERY_STATUS. The percentage is calculated only in Phase 1, but the capacity values are overwritten by Phase 2 without recalculating the percentage—causing the inconsistency reported in the ticket. When using SystemInfoFFM on Windows, the code falls back to this JNA-based implementation since no FFM Windows hardware layer exists yet.
🚀 Next Steps
🤖 All AI agent prompts combined
Fix the battery percentage bug in `WindowsPowerSource.java` by recalculating
`psRemainingCapacityPercent` after the IOCTL phase overwrites the capacity
values.
- File to modify:
`oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.java`
- In the `getPowerSource()` method, locate the `IOCTL_BATTERY_QUERY_STATUS`
success block where `psCurrentCapacity = bs.Capacity` is assigned
- After all status values are extracted from the IOCTL result (after the
`psAmperage` calculation), add the following recalculation still within the same
`if` block that confirms the IOCTL call succeeded:
- `psRemainingCapacityPercent = Math.min(1d, (double) psCurrentCapacity /
psMaxCapacity)`
- Use this exact formula to mirror the existing Phase 1 calculation at line 124
for consistency
- No division-by-zero guard is needed since `psMaxCapacity` is initialized to 1
and the existing Phase 1 code follows the same pattern
💡 Iterate on the plan with:
@coderabbitai <feedback>
Example Feedback
- `@coderabbitai` You can skip phase 3. Add a simple unit test case for phase 2.
- `@coderabbitai` For design choice 1 go ahead with option 3 and replan.
💬 Have feedback or questions? Drop into our discord!
Activity
dbwiddis commentedon Apr 2, 2026
The variable naming is poor, but that
1is a1.0and you need to multiply by 100 to get percentage.From the javadoc:
From the
toString():draganbjedov commentedon Apr 7, 2026
Actually the problem is that it always shows 1.0
dbwiddis commentedon Apr 7, 2026
Ah, now I understand.
Looks like we used to have an initial capacity set from
CallNtPowerInformation. The other place we use that in the code (Processor Frequency) has a note that approach broke in Win11 23H2, so it probably broke here too.And the problem is that we calculated the more accurate values but never re-did the calculation of capacity with it. 🤦
coderabbitai commentedon Apr 7, 2026
Coding Plan
Summary
psRemainingCapacityPercentafter the IOCTL queries update bothpsCurrentCapacityandpsMaxCapacityImplementation Steps
Fix Battery Percentage Calculation
This phase addresses the root cause of the bug by ensuring
psRemainingCapacityPercentis recalculated after the IOCTL queries update the capacity values.Add Percentage Recalculation After IOCTL Status Query
After the IOCTL
BATTERY_STATUSquery successfully retrieves the current capacity, recalculate the remaining capacity percentage using the IOCTL-sourced values.oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.javain thegetPowerSource()methodIOCTL_BATTERY_QUERY_STATUSsuccess block (afterpsCurrentCapacity = bs.Capacityis assigned), add the recalculation:psRemainingCapacityPercent = Math.min(1d, (double) psCurrentCapacity / psMaxCapacity)psAmperagecalculation) but still within the sameifblock that confirms the IOCTL call succeededMath.min(1d, (double) psCurrentCapacity / psMaxCapacity)psMaxCapacityis initialized to 1 and the existing Phase 1 code follows the same pattern🤖 Prompt for AI agents
Research
The battery implementation for Windows lives in
oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSource.java. ThegetPowerSource()method retrieves battery data in two phases: first viaCallNtPowerInformation(PowrProf API) to getSystemBatteryState, then viaDeviceIoControl(IOCTL) to query per-batteryBATTERY_INFORMATIONandBATTERY_STATUS. The percentage is calculated only in Phase 1, but the capacity values are overwritten by Phase 2 without recalculating the percentage—causing the inconsistency reported in the ticket. When usingSystemInfoFFMon Windows, the code falls back to this JNA-based implementation since no FFM Windows hardware layer exists yet.🚀 Next Steps
🤖 All AI agent prompts combined
💡 Iterate on the plan with:
@coderabbitai <feedback>💬 Have feedback or questions? Drop into our discord!
[-]Window 11 battery percent always 1%[/-][+]Window 11 battery percent always 100%[/+]