10 signed int spicfd
= -1;
12 int sonypi_supported (void) {
13 if ((spicfd
= open("/dev/sonypi", O_RDWR
)) == -1)
19 inline int sonypi_ioctl(int ioctlno
, void *param
) {
20 if (ioctl(spicfd
, ioctlno
, param
) < 0)
26 /* Read battery info from sonypi device and shove it into an apm_info
28 int sonypi_read (apm_info
*info
) {
33 info
->using_minutes
= info
->battery_flags
= 0;
35 if (! sonypi_ioctl(SONYPI_IOCGBATFLAGS
, &batflags
)) {
39 info
->ac_line_status
= (batflags
& SONYPI_BFLAGS_AC
) != 0;
40 if (batflags
& SONYPI_BFLAGS_B1
) {
41 if (! sonypi_ioctl(SONYPI_IOCGBAT1CAP
, &cap
))
43 if (! sonypi_ioctl(SONYPI_IOCGBAT1REM
, &rem
))
47 else if (batflags
& SONYPI_BFLAGS_B2
) {
48 /* Not quite right, if there is a second battery I should
49 * probably merge the two somehow.. */
50 if (! sonypi_ioctl(SONYPI_IOCGBAT2CAP
, &cap
))
52 if (! sonypi_ioctl(SONYPI_IOCGBAT2REM
, &rem
))
57 info
->battery_percentage
= 0;
58 info
->battery_status
= BATTERY_STATUS_ABSENT
;
62 info
->battery_percentage
= 100 * rem
/ cap
;
63 /* Guess at whether the battery is charging. */
64 if (info
->battery_percentage
< 99 && info
->ac_line_status
== 1) {
65 info
->battery_flags
= info
->battery_flags
| BATTERY_FLAGS_CHARGING
;
66 info
->battery_status
= BATTERY_STATUS_CHARGING
;
70 /* Sadly, there is no way to estimate this. */
71 info
->battery_time
= 0;