Battstat, Ubuntu, And You

The Gnome Battery Charge Monitor (battstat) has never played nice with my laptop, a Compaq Presario 1720. Maybe its ACPI implementation isn’t complete. Whatever the reason, even though perfectly usable information is available from the ACPI subsystem (in recent kernels under /proc/acpi), battstat has never used it appropriately. The problem in a nutshell is that battstat can’t find official information about the battery capacity, and assumes the maximum capacity is zero. Since the battery percentage is calculated using the maximum capacity value (division by zero, anyone?), we’re screwed.

Once again upon installing Ubuntu 5.04 I’ve been forced to look into the gnome-applets source to see how this can be fixed. The answer, as always, is to take a stab at estimating battery capacity, and just run with the guess. Some information is always better than none at all, right? Capacity information can always be dynamically updated as we gather more information by simply polling the ACPI subsystem during charging.

Anyone else out there can’t get battstat to work?

A bit of preparatory work. I’m assuming you’ve already installed gnome-applets on your system. If not, go ahead and do that using the Synaptics Package Manager. While you’re at it, make sure you also install the following development libraries and all their myriad dependencies:

  • libgail-gnome-dev
  • libpanelappletmm
  • libwnck-dev
  • libxklavier-dev

The above may not be a complete list. You’ll know if this is the case in the configure step.

Next, I had to make sure the XML::Parser module for Perl was installed. Use CPAN:

% sudo perl -MCPAN -e shell
...
cpan> install XML::Parser
...
cpan> quit

Now, download the gnome-applets source (2.10.1) from ftp.ubuntulinux.org . Then download my tiny acpi-linux.c patch.

Assuming you’ve downloaded both to the same folder, unpack the archive:

% tar xvfz gnome-applets_2.10.1.orig.tar.gz

Now apply the patch:

% cd gnome-applets-2.10.1/battstat/
% cp ../../acpi-linux.c.patch .
% patch -p0 <acpi-linux.c.patch

Time to configure the build process. Because the gnome-applets package is already installed on our system, we only need to build battstat! Sneaky, eh?

% cd ..
% ./configure --prefix=/usr
% cd battstat/
% make

If battstat is running, turn it off by invoking Remove From Panel. Now let’s copy over the new binary. We can make a backup of the original just in case:

% sudo mv /usr/lib/gnome-applets/battstat-applet-2{,.orig}
% sudo cp battstat-applet-2 /usr/lib/gnome-applets/

Moment of truth – fire up the battstat applet using Add to Panel. The applet takes a moment to start on my computer. Does it work? Do you see a whole lotta green, instead of a disappointing slab of red? If so, jump up and down and scratch yourself like a monkey while belching out a yodel of jubilation!

By the way, if it does work, DON’T rain on your own party by trying:

% cat /proc/acpi/battery/CMB0/info

On my computer cat becomes an unkillable process that makes the cpu go into overdrive! And it’ll eat up all your memory too. When I heard the unmistakable sound of hard drive thrashing, I knew it was time for a hard reset. That’s just mest up yo, mest up!

I would appreciate feedback in the form of comments to this blog entry if this is useful to anyone.

Update! 2005-09-14

For convenience (in case you didn’t want to go through all the steps mentioned above), here is my bzipped i686 battstat-applet-2 binary, which you should be able to drop into the /usr/lib/gnome-applets directory. It might still be a good idea to back up the original file just in case.

Update! 2005-10-07

StonHaus offers his bzipped i686 battstat-applet-2 binary built for gnome-applets 2.12 (shipped with Ubuntu 5.10 “Breezy”). After unzipping the file,

sudo mv /usr/lib/gnome-applets/battstat-applet-2{,.orig}
sudo mv battstat-applet-2.12 \
/usr/lib/gnome-applets/battstat-applet-2

Just to be clear, it will be necessary to rename the unzipped file from battstat-applet-2.12 to simply battstat-applet-2 as shown above.

Update! 2005-10-10

An easier way to retrieve the source archive and resolve build dependences at the same time is to run:

% sudo apt-get source gnome-applets
% sudo apt-get build-dep gnome-applets

References:
APT HOWTO

Update! 2005-10-18

I upgraded to Ubuntu 5.10 “Breezy” on my laptop today, and experienced probably the same problem EdPsych and others have related – even after downloading the gnome-applets sources, applying the acpi-linux.c patch, and installing the patched version, the battstat tooltip continued to display “No battery present”.

Battstat now defaults to using HAL as the backend for retrieving power management information. That’s well and good; however apparently it doesn’t work for my laptop, while ACPI worked just fine before. The battstat Help tells you just how to disable HAL in the Troubleshooting section:

Select Configuration Editor from the Applications menu, under System Tools. Search for the key value OAFIID:GNOME_BattstatApplet which should be located in the path /apps/panel/applets.

Assuming the path is /apps/panel/applets/applet_1. In /apps/panel/applets/applet_1/prefs add a New Key… called no_hal and set it to the boolean value of true. This will disable the usage of HAL (see Section 3.1 ― Determining the backend to learn how to check this).

However, what it DOESN’T tell you is that the no_hal gconf key is read only once, when battstat is loaded, and never again. The catch 22 is that you cannot edit this value using the Configuration Editor when the applet isn’t loaded.

The solution? After you’ve installed the patched version and fired up the applet, go ahead and add the boolean no_hal value to battstat using the Configuration Editor, and set it to true. Then simply log out, and log back in! Battstat will be started with the correct configuration (goodbye HAL), and you will be invited to the battery life party!

9 Responses to “Battstat, Ubuntu, And You”

  1. StonHaus Says:

    I tried your precompiled file and when I install it, I get nothing in the gnome panel. I replace my original file with the one that you provided and then chmod it to 755. When I try to add the applet to the panel, it looks like it is loading something for a second then will just stop. The battstat applet never loads. Any suggestions?

  2. EdPsych Says:

    I am having a similar problem to StonHaus where I get nothing when I add the applet to the panel. I have Breezy on a Presario 1700. Help is appreciated.

  3. Administrator Says:

    Hi EdPsych,

    StonHaus was able to resolve his problem by going through the
    compilation steps with gnome-applets for gnome 2.12 (Breezy uses 2.12,
    Hoary uses 2.10). He shared his binary with me, and it’s posted on the
    blog entry now. Why don’t you give that a shot and let me know if it
    works?

  4. ANthonyS Says:

    Hi,

    I have a Presario 17XL570 laptop and installed Ubuntu this weekend.

    I had to recompile the kernel earlier today to get ACPI to work as this laptop’s ACPI is balcklisted.

    I applied your patch and recompiled the battstat applet and it works great!

    Thanks!

  5. Administrator Says:

    Hi ANthonyS,

    Great to hear that it works!

  6. dakira Says:

    THANK you!! I was really pissed that battstat didn’t work anymore after I updated to Breezy. Now it at least recognized when I pull the plug. I’m not sure, yet, if the battery state will be shown correctly. I guess I’ll have to reload my battery completely and it should work again. So thanks!

    Btw: Your problem at first just sounded as if you have a messed up DSDT which can (and should) be easily fixed.

  7. dakira Says:

    okay.. i was a little too fast. It doesn’t work ;( Worked perfectly for me (and all people I know) in Hoary. Maybe somethings wrong with Breezys ACPI?!

  8. Administrator Says:

    Here’s a record of my correspondence with EdPsych regarding trying to get battstat working on his Breezy system.

    gyrm:

    StonHaus was able to resolve his problem by going through the compilation steps with gnome-applets for gnome 2.12 (Breezy uses 2.12, Hoary uses 2.10). He shared his binary with me, and it’s posted on the blog entry now. Why don’t you give that a shot and let me know if it works?

    EdPsych:

    I unzipped StonHaus’s file and added it to my /usr/lib/gnome-applets/ directory. When I tried adding the applet to the panel, a menu came up saying “The panel encountered a problem while loading ‘OAFFID:GNOME_BattstatApplet’”.

    I also noticed that, when I use your original file, the applet works correctly when the computer isn’t running off the battery. When the computer is running off battery, nothing appears on the panel. Any other suggestions?

    gyrm:

    Did you by chance forget to rename the unzipped file from “battstat-applet-2.12″ to “battstat-applet-2″? I didn’t mention this directly aside from within the command line instructions following StonHaus’ update. Hmm, I think I’ll change that just to avoid confusion.

    If that doesn’t work, could you send me the output of:

    % cat /proc/acpi/battery/yourbatt/state

    both on AC and off? I’ll take a look.

    EdPsych:

    I made sure that I renamed the file, and I still got the error message that I referred to before (“panel encountered a problem…”).

    Here is my battery state output; it seems like everything is being recognized on some level.

    Thanks,
    Ed

    AC on:
    ed@bernie:/proc/acpi/battery/CMB0$ cat state
    present: yes
    capacity state: ok
    charging state: charging
    present rate: 179 mW
    remaining capacity: 3340 mWh
    present voltage: 16756 mV

    AC off:
    ed@bernie:/proc/acpi/battery/CMB0$ cat state
    present: yes
    capacity state: ok
    charging state: discharging
    present rate: 179 mW
    remaining capacity: 3340 mWh
    present voltage: 16741 mV

    gyrm:

    It looks like ACPI is working. My guess would be that you are experiencing a problem with library linking … this should be resolved by compiling battstat according to the steps given in the blog entry. In your case you will probably need to start out with the source for gnome-applets-2.12.1. Try this first:

    % sudo apt-get source gnome-applets

    That’ll get you the zipped source archive. The patching and compilation steps should go smoothly if you follow the instructions from the blog entry.

    http://www.ttwhy.org/home/blog/2005/09/09/battstat-ubuntu-and-you/

    Let me know if you need more help, or if it works!!

    EdPsych:

    Thanks for all your help. I got the source for gnome-applets-2.12.1 and compiled battstat over again, using your patch in the process. Upon restarting the battstat applet, my battery is still not recognized (it always says I’m on AC).

    I know I’m taking lots of your time and am close to giving up, but let me know if you have any other ideas.

    gyrm:

    Hmm … not sure what’s going on. Did you make sure to copy the new (patched) battstat-applet-2 file to /usr/lib/gnome-applets? There are no dumb questions when trying to debug this kind of problem … :)

    If that wasn’t it … at least we got past the loading step, so the problem you have now is different from the one before.

    Can you go to /proc/acpi/ac_adapter and tell me what files / subdirectories you see, if any?

    Afterwards, try this:

    In the patched file acpi-linux.c, look for this section:

    /* If there are no AC adaptors registered in the system, then we’re
    probably on a desktop (and therefore, on AC power).
    */
    if (have_adaptor == FALSE)
    acpiinfo->ac_online = 1;

    and comment out the last two lines, like this:

    /* If there are no AC adaptors registered in the system, then we’re
    probably on a desktop (and therefore, on AC power).
    */
    /* if (have_adaptor == FALSE)
    acpiinfo->ac_online = 1;
    */

    then recompile the applet, and try it. Kind of a shot in the dark, let’s see what happens.

    That’s all I can think of right now. Don’t worry about my time … it’s no problem really!

    EdPsych:

    Yes, I copied the patched battstat-applet-2 file to the appropriate directory. I also tried commenting the lines out of acpi-linux.c and recompiling, with no luck. Even if I can’t get this to work, I am at least learning alot.

    I checked out my /proc/acpi/ac_adapter/ directory. There is a subdirectory called “AC”, which has a single file called “state”. When the AC is plugged in, I get:

    ed@bernie:/proc/acpi/ac_adapter/AC$ cat state
    state: on-line

    When the AC is not plugged in, I get:

    ed@bernie:/proc/acpi/ac_adapter/AC$ cat state
    state: off-line

    gyrm:

    I must admit I am stumped!

    Questions: what does the battstat applet LOOK like now? Do you see a green bar at all? When you hover the mouse over the applet, what does the popup tooltip say? Unplug the computer and wait for a couple of minutes, then hover the mouse again. Anything change? Does changing the battstat Preferences reveal anything?

    Just one more shot in the dark:

    /* Update AC info on battery info updates. This works around
    * a bug in ACPI (as per bug #163013).
    */
    update_ac_info(acpiinfo);

    Comment out that last line in acpi-linux.c, recompile, remove from panel, copy, add to panel. Same drill! Anything?

    EdPsych:

    The battstat applet just looks like an AC plug. There is no green bar; when I switch to the expanded view, the battery meter looks completely drained (only a small amount of red at the right end), and there is an “N/A” when I select “Show time/percentage”. Nothing changes when I unplug the AC: the icons look the same, and when I hover the mouse over them, it always says “System is running on AC power, No battery present”. I tried commenting out the other command in acpi-linux.c, and it didn’t help.

    I read the help file for the battery applet, and they make a distinction about using Hardware Abstraction Layer vs. using ACPI. I checked, and I am using the Hardware Abstraction Layer. I don’t know enough to know whether this makes a difference, but it seems like my ACPI is working correctly. What do you think?

    gyrm:

    Aha, I had no idea there were different backends to battstat. Just installed 5.10 on my desktop and was reading the battstat help. Did you try disabling HAL through the steps outlined in the help? It may cause the battery monitor to fall back on ACPI.

    From the help:

    “Select Configuration Editor from the Applications menu, under System Tools. Search for the key value OAFIID:GNOME_BattstatApplet which should be located in the path /apps/panel/applets.

    “Assuming the path is /apps/panel/applets/applet_1. In /apps/panel/applets/applet_1/prefs add a New Key… called no_hal and set it to the boolean value of true. This will disable the usage of HAL (see Section 3.1: Determining the backend to learn how to check this).”

    EdPsych:

    I did try disabling HAL like it said in the help file. This didn’t work, though. I’m not sure that it actually disabled HAL, though, because there was always a star next to the guy’s name in the battstat credits (which is supposed to signify that HAL is being used).

    I have, however, had some success with getting the battery meter to work. I found a HOWTO page that shows how to get a new DSDT and put it into your initrd (the web address is below). After doing this, I got a battery icon but it didn’t tell me how much charge I had left. So I then patched the applet a la your website, and the battery applet actually reads my battery!

    The new DSDT isn’t perfect, though. The boot time is noticeably slow, I get a “hal couldn’t be started” error message after I log in, and I also get some errors about the applets after I log in. I feel like hal is causing my problems, and I am wondering if there is a way to generally disable hal when booting with my old DSDT. Any ideas?

    Also, is the battstat icon supposed to change when you plug/unplug? Although mine tells me the charge that is left, it doesn’t seem to notice changes in the power source.

    It’s not perfect, but I’m at least making progress…

    that web address is:

    https://wiki.ubuntu.com/ACPIBattery

  9. gyrmination » Blog Archive » KThinkBat, KDE, And You Says:

    [...] I’ve worked using Arch Linux for a while without a battery monitor app, and finally decided to cure myself of this issue today. (I’ve dealt with this problem before.) [...]

Leave a Reply


Bad Behavior has blocked 42 access attempts in the last 7 days.