2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
9 #define _POSIX_C_SOURCE 199506
18 #include <dev/acpica/acpiio.h>
19 #include <sys/ioctl.h>
25 static const char *ACPIDEV
= "/dev/acpi";
27 struct battery_context
{
29 char battery_str
[BATTERY_BUFFLEN
];
32 struct battery_context
*
33 battery_context_open()
35 struct battery_context
*ctx
;
37 if ((ctx
= malloc(sizeof(*ctx
))) == NULL
)
38 err(EX_SOFTWARE
, "malloc(%d) battery_context", sizeof(*ctx
));
39 if ((ctx
->fd
= open(ACPIDEV
, O_RDONLY
)) == -1)
40 err(EX_OSFILE
, "open(%s)", ACPIDEV
);
46 battery_context_close(struct battery_context
*ctx
)
50 if (close(ctx
->fd
) == -1)
51 err(EX_OSFILE
, "close(%s)", ACPIDEV
);
56 battery_str(struct battery_context
*ctx
)
58 union acpi_battery_ioctl_arg battio
;
60 char cap
[3 + 1]; /* capacity is a percentage so the
61 * string representation will be at
62 * most three character long */
66 battio
.unit
= ACPI_BATTERY_ALL_UNITS
;
67 if (ioctl(ctx
->fd
, ACPIIO_BATT_GET_BATTINFO
, &battio
) == -1)
68 err(EX_IOERR
, "ioctl(ACPIIO_BATT_GET_BATTINFO)");
70 if (battio
.battinfo
.state
== 0)
72 else if (battio
.battinfo
.state
& ACPI_BATT_STAT_CRITICAL
)
74 else if (battio
.battinfo
.state
& ACPI_BATT_STAT_DISCHARG
)
76 else if (battio
.battinfo
.state
& ACPI_BATT_STAT_CHARGING
)
81 sprintf(cap
, "%d", battio
.battinfo
.cap
);
82 tools_catitems(ctx
->battery_str
, sizeof(ctx
->battery_str
), cap
, "% [", state
, "]", NULL
);
84 return (ctx
->battery_str
);