Remove trailing whitespace.
[dockapps.git] / wmbattery / sonypi.c
blobab9922f397158abca05afedb90fdd342e7c380ab
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>
8 #include "sonypi.h"
10 signed int spicfd = -1;
12 int sonypi_supported (void) {
13 if ((spicfd = open("/dev/sonypi", O_RDWR)) == -1)
14 return 0;
15 else
16 return 1;
19 inline int sonypi_ioctl(int ioctlno, void *param) {
20 if (ioctl(spicfd, ioctlno, param) < 0)
21 return 0;
22 else
23 return 1;
26 /* Read battery info from sonypi device and shove it into an apm_info
27 * struct. */
28 int sonypi_read (apm_info *info) {
29 __u8 batflags;
30 __u16 cap, rem;
31 int havebatt = 0;
33 info->using_minutes = info->battery_flags = 0;
35 if (! sonypi_ioctl(SONYPI_IOCGBATFLAGS, &batflags)) {
36 return 1;
39 info->ac_line_status = (batflags & SONYPI_BFLAGS_AC) != 0;
40 if (batflags & SONYPI_BFLAGS_B1) {
41 if (! sonypi_ioctl(SONYPI_IOCGBAT1CAP, &cap))
42 return 1;
43 if (! sonypi_ioctl(SONYPI_IOCGBAT1REM, &rem))
44 return 1;
45 havebatt = 1;
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))
51 return 1;
52 if (! sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem))
53 return 1;
54 havebatt = 1;
56 else {
57 info->battery_percentage = 0;
58 info->battery_status = BATTERY_STATUS_ABSENT;
61 if (havebatt) {
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;
73 return 0;