It varies from battery to battery how this works, but here is a brief overview.
As a battery dies its voltage decreases. Your laptop (or other battery powered device) has a safe range of voltages it will operate at. Too high and it gets fried, too low and the capacitors cannot maintain a charge so it looses power (which can actually be more damaging then too high of a voltage).
So the way a laptop determines how much juice is left is by checking the voltage that the battery is putting out. Of course it is significantly more complicated then that.
A "battery" is actually a collection of cells. For example a 1.5v AAA is a single cell, but a 9v is actually a battery containing Six 1.5v AAAA (slightly smaller then a AAA). 6 x 1.5 = 9V. So a laptop battery is a collection of multiple cells. Most laptop batteries have circuitry internally to monitor and regulate each individual cell. The long charge life and number of recharges from a modern battery comes from both the chemical material (metals and acids usually) in the individual cells and the circuitry that regulates it all. Some laptop batteries even have built in charge meters.
If you look at a laptop battery you will see it has multiple metal connectors on it. Typically one is positive, one is negative, one is neutral, and then the remaining ones are used to communicate charge information to the computer. Thanks to all of this circuitry it actually maintains a fairly even voltage throughout the lifetime of the battery. When it finally is not able to maintain voltage it signals the laptop to shut off.
Now your battery can only say how much juice it has left. The computer on the other hand can take that information and look at how long it has been operating on that battery and possibly what the current power consumption profile is to extrapolate the how much time is left in the battery. This of course can change if you start burning a DVD or adjust your LCD brightness.
So what about hacking this to do it with a UPS? Well, most UPS's actually have a pretty simple battery similar to what you would find in a car, but much smaller (depending on the UPS). The UPS itself has the circuitry to transform that voltage (usually 12v) to what you computer is taking (usually 110v) and then kill the power when it is no longer able to maintain appropriate voltage.
Most higher quality UPSs have a serial or USB connector that you can run into your computer. Since about Windows 98 the OS has had a built in indicator to handle the information provided by the UPS.
As far as hacking this, assuming your UPS does not have any reporting connector then you could rig up your own volt meter that samples the battery at regular intervals (each sample will use up some juice, so don't sample too often!) You would need to run your computer off the UPS and monitor the battery's voltage to see what is its safe range. Once you have that, then you can feed that information to your computer. Serial is a pretty simple protocol, or you might be able to use X10 or some other instrumentation monitoring hardware. Your software could then use the base line you established earlier to see how long until your battery drops below the safe voltage and the UPS cuts power.
Sounds like a fun project. Here is an outline on what you need:
- Periodic sampling voltmeter
- Ability to report readings to computer (serial or USB most likely)
- Software to monitor reported information.
- Baseline of what your safe voltage is on your battery.
- A bonus would be to determine the current power profile and consumption to adjust the estimates.
- Some way to display the estimated time remaining to the user.
Good luck! Let us know how it goes. For practicality you might find it better to upgrade to a UPS that has a status port. But that wouldn't be near as much fun now would it!