wmusic: Bump to version 2.1.0
[dockapps.git] / wmbattery / sonypi.c
blob003a6cde77a220f059542a5054ae26b35a2e4d06
1 #include <stdio.h>
2 #ifdef __sun
3 #include <sys/filio.h>
4 #include <sys/sockio.h>
5 #endif
6 #include <sys/ioctl.h>
7 #include "apm.h"
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <stdint.h>
12 #include <unistd.h>
14 #include "sonypi.h"
16 signed int spicfd = -1;
18 int sonypi_supported(void)
20 spicfd = open("/dev/sonypi", O_RDWR);
21 if (spicfd == -1)
22 return 0;
23 else
24 return 1;
27 inline int sonypi_ioctl(int ioctlno, void *param)
29 if (ioctl(spicfd, ioctlno, param) < 0)
30 return 0;
31 else
32 return 1;
35 /* Read battery info from sonypi device and shove it into an apm_info
36 * struct. */
37 int sonypi_read(apm_info *info)
39 uint8_t batflags;
40 uint16_t cap, rem;
41 int havebatt = 0;
43 info->using_minutes = info->battery_flags = 0;
45 if (!sonypi_ioctl(SONYPI_IOCGBATFLAGS, &batflags))
46 return 1;
48 info->ac_line_status = (batflags & SONYPI_BFLAGS_AC) != 0;
49 if (batflags & SONYPI_BFLAGS_B1) {
50 if (!sonypi_ioctl(SONYPI_IOCGBAT1CAP, &cap))
51 return 1;
52 if (!sonypi_ioctl(SONYPI_IOCGBAT1REM, &rem))
53 return 1;
54 havebatt = 1;
55 } else if (batflags & SONYPI_BFLAGS_B2) {
56 /* Not quite right, if there is a second battery I should
57 * probably merge the two somehow.. */
58 if (!sonypi_ioctl(SONYPI_IOCGBAT2CAP, &cap))
59 return 1;
60 if (!sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem))
61 return 1;
62 havebatt = 1;
63 } else {
64 info->battery_percentage = 0;
65 info->battery_status = BATTERY_STATUS_ABSENT;
68 if (havebatt) {
69 info->battery_percentage = 100 * rem / cap;
70 /* Guess at whether the battery is charging. */
71 if (info->battery_percentage < 99 && info->ac_line_status == 1) {
72 info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING;
73 info->battery_status = BATTERY_STATUS_CHARGING;
77 /* Sadly, there is no way to estimate this. */
78 info->battery_time = 0;
80 return 0;