site  contact  subhomenews

Tweak powerapplet_tray external charging

November 22, 2026 — BarryK

I posted about using Claude AI to detect battery types other than BAT0 and BAT1:

However, I noticed a difference from the original code; when the external power adapter is plugged in, but battery is at 100% so no longer charging, sometimes the tray icon was a red colour and mouse-over stated "Battery discharging, capacity 100%". I reported this to Claude:

In the original code, when the external power is plugged in and the charge has reached 100%, mouseover the tray icon says "Battery charging, capacity 100%" and the icon in the tray is color blue. However, your code says "Battery discharging, capacity 100%" and icon is red color, which is misleading. I think that when the external power is plugged in, the tray icon should always be color blue.

Claude responded:

That's a regression from my change, not your original logic. The original treats the battery as charging unless sysfs says "Discharging". I added "Not charging" as a discharging state, and many laptops report "Not charging" or "Full" at 100% on AC. I'll remove that and also check the AC adapter directly, so plugged-in always shows blue.

I've fixed it. The regression was my "Not charging" line, which I've removed.

There are two changes in the sysfs branch of `Update()`:
- **Original behaviour restored:** the battery counts as charging unless its status is exactly "Discharging". "Full" and "Not charging" at 100% on AC now show the blue icon and "Battery charging, capacity 100%", as in your original.
- **AC check added:** a new `ac_online()` function looks for a non-battery supply in `/sys/class/power_supply` (the AC adapter or a USB-C charger) with `online` = 1. If the battery reports "Discharging" while external power is connected, as some laptops do near a charge limit, the applet now treats that as charging too, so the icon stays blue whenever you're plugged in.

I haven't built or run this. The sandbox has no GTK headers and no power supplies, so I only compiled `ac_online()` on its own. It ran and returned 0, which is correct there. Please run `./compile` and test it on your laptop.

The updated `powerapplet_tray.c` is the file to use. The patch is attached as `battery-detect.patch.txt`, a diff against your original upload that includes both the detection fix and this one.

I have bumped powerapplet_tray PET to version 3.4.   

Tags: easy