fix(battery): prefer Mains/USB supplies when auto-detecting the adapter - #5269
Open
klizas wants to merge 1 commit into
Open
fix(battery): prefer Mains/USB supplies when auto-detecting the adapter#5269klizas wants to merge 1 commit into
klizas wants to merge 1 commit into
Conversation
Without an "adapter" pin, adapter_ got the last node in /sys/class/power_supply with `online` or `status`. Batteries match via `status`, so the battery itself could win, and a battery has no `online`, so the "Plugged" state never showed. Pick by class instead: Mains > USB* > other online-capable > status-only. A configured "adapter" name still wins unchanged.
klizas
marked this pull request as ready for review
August 23, 2026 19:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes battery adapter auto-detection picking the battery itself as the adapter.
When no
"adapter"is configured,refreshBatteries()assignsadapter_to the last node in/sys/class/power_supplythat hasonlineorstatus. Batteries match throughstatus, so whether the real adapter or the battery wins depends on readdir order. A battery has noonline, so when the battery wins, everyadapter_check reads offline: theNot charging->Pluggedupgrade andfull-at-pluggedare silently dead.Observed on a MacBookPro16,1 (kernel 7.2.0-1-cachyos-t2):
ADP1(ACPI0003:00, typeMains, hasonline, nostatus) iterates beforeBAT0(ACPI0002:00, typeBattery, hasstatus, noonline), soBAT0always won and the bar showed the discharging/not-charging format while plugged in. Pinning"adapter": "ADP1"was the only workaround.The fix classifies candidates by
typeand picks in priority order:"adapter"name (unchanged behavior)MainsUSB*(prefix match, so legacy pre-4.19 types likeUSB_PDcount)Wireless,UPS, missingtypefile)First match wins within each tier. If a pass finds nothing,
adapter_keeps its previous value, as before.Tested live on the machine above: before, Waybar picked
BAT0, patched now picksADP1, andPluggedworks without a pin. Also compared old and new selection over nine mocked sysfs layouts (battery-only, Mains+battery, Mains+USB-C source nodes, legacyUSB_PD, wireless charger, UPS-only, typeless online node, empty dir, two batteries): the new logic finds an adapter in every case the old one did, and picks the same node everywhere except the order-dependent broken cases. Battery-only machines still get the battery asadapter_, and the configured-name path is identical.Related issues
None found
Checklist
clang-formatninja -C build)man/) — N/A, no new option;adapterdocs already say "instead of auto detect"