wmbattery: Restore BSD support.
[dockapps.git] / wmbattery / sonypi.c
blob92ba106babf0e7ddda335e3efa21463723544fce
1 #include <stdio.h>
2 #include <sys/ioctl.h>
3 #include "apm.h"
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stdint.h>
9 #include "sonypi.h"
11 signed int spicfd = -1;
13 int sonypi_supported(void)
15 spicfd = open("/dev/sonypi", O_RDWR);
16 if (spicfd == -1)
17 return 0;
18 else
19 return 1;
22 inline int sonypi_ioctl(int ioctlno, void *param)
24 if (ioctl(spicfd, ioctlno, param) < 0)
25 return 0;
26 else
27 return 1;
30 /* Read battery info from sonypi device and shove it into an apm_info
31 * struct. */
32 int sonypi_read(apm_info *info)
34 uint8_t batflags;
35 uint16_t cap, rem;
36 int havebatt = 0;
38 info->using_minutes = info->battery_flags = 0;
40 if (!sonypi_ioctl(SONYPI_IOCGBATFLAGS, &batflags))
41 return 1;
43 info->ac_line_status = (batflags & SONYPI_BFLAGS_AC) != 0;
44 if (batflags & SONYPI_BFLAGS_B1) {
45 if (!sonypi_ioctl(SONYPI_IOCGBAT1CAP, &cap))
46 return 1;
47 if (!sonypi_ioctl(SONYPI_IOCGBAT1REM, &rem))
48 return 1;
49 havebatt = 1;
50 } else if (batflags & SONYPI_BFLAGS_B2) {
51 /* Not quite right, if there is a second battery I should
52 * probably merge the two somehow.. */
53 if (!sonypi_ioctl(SONYPI_IOCGBAT2CAP, &cap))
54 return 1;
55 if (!sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem))
56 return 1;
57 havebatt = 1;
58 } else {
59 info->battery_percentage = 0;
60 info->battery_status = BATTERY_STATUS_ABSENT;
63 if (havebatt) {
64 info->battery_percentage = 100 * rem / cap;
65 /* Guess at whether the battery is charging. */
66 if (info->battery_percentage < 99 && info->ac_line_status == 1) {
67 info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING;
68 info->battery_status = BATTERY_STATUS_CHARGING;
72 /* Sadly, there is no way to estimate this. */
73 info->battery_time = 0;
75 return 0;