2017-09-17 15:45:03 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2018-03-19 17:44:52 +00:00
|
|
|
#include <stdio.h>
|
2018-03-28 17:46:27 +00:00
|
|
|
#include <string.h>
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2022-10-26 20:14:53 +00:00
|
|
|
#include "../slstatus.h"
|
2022-10-27 21:18:30 +00:00
|
|
|
#include "../util.h"
|
2017-09-17 14:18:17 +00:00
|
|
|
|
Revert component-split
this reverts the commits from 92ab9ef52ebcb097add97d9f78e67ad1c1d6a6ec up to
d42870d6ca7fb587b38f8cf6d6821ae33a53a696.
After heavy consideration, the component split has more disadvantages
than advantages, especially given there will be utility-functions
sharing quite a lot of code that would then need to be duplicated, as it
does not fit into the util.c due to its speciality.
One big advantage of the component-wise build is readability, and
without doubt, this was achieved here. This point will be addressed
with a different approach that will be visible in the upcoming commits.
One big disadvantage of the component build is the fact that it
introduces state to the build process which is not necessary. Before its
introduction, the only influencing factors where the system-defines
__linux__ and __OpenBSD__. With the components, we are also relying on
the output of uname(1).
Additionally, if the os.mk is not present, make gives the output
$ make
Makefile:5: os.mk: No such file or directory
make: *** No rule to make target 'os.mk'. Stop.
This could easily be fixed by providing some sort of meta-rule for this
file, however, it indicates the problem we have here, and this entire
statefulness will heavily complicate packaging of this tool and makes
the build process much more complex than it actually has to be.
2018-05-01 16:10:39 +00:00
|
|
|
#if defined(__linux__)
|
2021-04-06 16:48:18 +00:00
|
|
|
/*
|
|
|
|
* https://www.kernel.org/doc/html/latest/power/power_supply_class.html
|
|
|
|
*/
|
2018-05-01 17:01:22 +00:00
|
|
|
#include <limits.h>
|
2018-07-07 09:08:26 +00:00
|
|
|
#include <stdint.h>
|
2018-05-20 07:42:16 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-10-27 21:44:52 +00:00
|
|
|
#define POWER_SUPPLY_CAPACITY "/sys/class/power_supply/%s/capacity"
|
|
|
|
#define POWER_SUPPLY_STATUS "/sys/class/power_supply/%s/status"
|
|
|
|
#define POWER_SUPPLY_CHARGE "/sys/class/power_supply/%s/charge_now"
|
|
|
|
#define POWER_SUPPLY_ENERGY "/sys/class/power_supply/%s/energy_now"
|
2022-11-27 04:11:01 +00:00
|
|
|
#define POWER_SUPPLY_CURRENT "/sys/class/power_supply/%s/current_now"
|
|
|
|
#define POWER_SUPPLY_POWER "/sys/class/power_supply/%s/power_now"
|
2022-10-27 21:44:52 +00:00
|
|
|
|
2018-05-20 07:42:16 +00:00
|
|
|
static const char *
|
2018-05-20 22:36:59 +00:00
|
|
|
pick(const char *bat, const char *f1, const char *f2, char *path,
|
|
|
|
size_t length)
|
2018-05-20 07:42:16 +00:00
|
|
|
{
|
2018-05-20 22:36:59 +00:00
|
|
|
if (esnprintf(path, length, f1, bat) > 0 &&
|
2022-10-27 22:51:34 +00:00
|
|
|
access(path, R_OK) == 0)
|
2018-05-20 07:42:16 +00:00
|
|
|
return f1;
|
|
|
|
|
2018-05-20 22:36:59 +00:00
|
|
|
if (esnprintf(path, length, f2, bat) > 0 &&
|
2022-10-27 22:51:34 +00:00
|
|
|
access(path, R_OK) == 0)
|
2018-05-20 07:42:16 +00:00
|
|
|
return f2;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2018-05-01 17:01:22 +00:00
|
|
|
const char *
|
|
|
|
battery_perc(const char *bat)
|
|
|
|
{
|
2021-04-06 16:48:18 +00:00
|
|
|
int cap_perc;
|
2018-05-01 17:01:22 +00:00
|
|
|
char path[PATH_MAX];
|
Revert component-split
this reverts the commits from 92ab9ef52ebcb097add97d9f78e67ad1c1d6a6ec up to
d42870d6ca7fb587b38f8cf6d6821ae33a53a696.
After heavy consideration, the component split has more disadvantages
than advantages, especially given there will be utility-functions
sharing quite a lot of code that would then need to be duplicated, as it
does not fit into the util.c due to its speciality.
One big advantage of the component-wise build is readability, and
without doubt, this was achieved here. This point will be addressed
with a different approach that will be visible in the upcoming commits.
One big disadvantage of the component build is the fact that it
introduces state to the build process which is not necessary. Before its
introduction, the only influencing factors where the system-defines
__linux__ and __OpenBSD__. With the components, we are also relying on
the output of uname(1).
Additionally, if the os.mk is not present, make gives the output
$ make
Makefile:5: os.mk: No such file or directory
make: *** No rule to make target 'os.mk'. Stop.
This could easily be fixed by providing some sort of meta-rule for this
file, however, it indicates the problem we have here, and this entire
statefulness will heavily complicate packaging of this tool and makes
the build process much more complex than it actually has to be.
2018-05-01 16:10:39 +00:00
|
|
|
|
2022-10-27 21:44:52 +00:00
|
|
|
if (esnprintf(path, sizeof(path), POWER_SUPPLY_CAPACITY, bat) < 0)
|
2018-05-19 17:33:04 +00:00
|
|
|
return NULL;
|
2021-04-06 16:48:18 +00:00
|
|
|
if (pscanf(path, "%d", &cap_perc) != 1)
|
2018-05-19 17:33:04 +00:00
|
|
|
return NULL;
|
|
|
|
|
2021-04-06 16:48:18 +00:00
|
|
|
return bprintf("%d", cap_perc);
|
Revert component-split
this reverts the commits from 92ab9ef52ebcb097add97d9f78e67ad1c1d6a6ec up to
d42870d6ca7fb587b38f8cf6d6821ae33a53a696.
After heavy consideration, the component split has more disadvantages
than advantages, especially given there will be utility-functions
sharing quite a lot of code that would then need to be duplicated, as it
does not fit into the util.c due to its speciality.
One big advantage of the component-wise build is readability, and
without doubt, this was achieved here. This point will be addressed
with a different approach that will be visible in the upcoming commits.
One big disadvantage of the component build is the fact that it
introduces state to the build process which is not necessary. Before its
introduction, the only influencing factors where the system-defines
__linux__ and __OpenBSD__. With the components, we are also relying on
the output of uname(1).
Additionally, if the os.mk is not present, make gives the output
$ make
Makefile:5: os.mk: No such file or directory
make: *** No rule to make target 'os.mk'. Stop.
This could easily be fixed by providing some sort of meta-rule for this
file, however, it indicates the problem we have here, and this entire
statefulness will heavily complicate packaging of this tool and makes
the build process much more complex than it actually has to be.
2018-05-01 16:10:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 17:01:22 +00:00
|
|
|
const char *
|
|
|
|
battery_state(const char *bat)
|
|
|
|
{
|
2018-05-19 11:25:35 +00:00
|
|
|
static struct {
|
|
|
|
char *state;
|
|
|
|
char *symbol;
|
|
|
|
} map[] = {
|
|
|
|
{ "Charging", "+" },
|
|
|
|
{ "Discharging", "-" },
|
2019-10-02 08:31:54 +00:00
|
|
|
{ "Full", "o" },
|
2022-10-26 19:34:47 +00:00
|
|
|
{ "Not charging", "o" },
|
2018-05-19 11:25:35 +00:00
|
|
|
};
|
2018-05-01 17:01:22 +00:00
|
|
|
size_t i;
|
|
|
|
char path[PATH_MAX], state[12];
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2022-10-27 21:44:52 +00:00
|
|
|
if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0)
|
2018-05-19 17:33:04 +00:00
|
|
|
return NULL;
|
2022-10-27 22:51:34 +00:00
|
|
|
if (pscanf(path, "%12[a-zA-Z ]", state) != 1)
|
2018-05-01 17:01:22 +00:00
|
|
|
return NULL;
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2022-10-27 22:51:34 +00:00
|
|
|
for (i = 0; i < LEN(map); i++)
|
|
|
|
if (!strcmp(map[i].state, state))
|
2018-05-01 17:01:22 +00:00
|
|
|
break;
|
2022-10-27 22:51:34 +00:00
|
|
|
|
2018-05-01 17:01:22 +00:00
|
|
|
return (i == LEN(map)) ? "?" : map[i].symbol;
|
|
|
|
}
|
2018-05-18 12:43:04 +00:00
|
|
|
|
|
|
|
const char *
|
|
|
|
battery_remaining(const char *bat)
|
|
|
|
{
|
2018-07-07 08:50:25 +00:00
|
|
|
uintmax_t charge_now, current_now, m, h;
|
2018-05-20 22:41:03 +00:00
|
|
|
double timeleft;
|
2018-05-18 15:25:09 +00:00
|
|
|
char path[PATH_MAX], state[12];
|
|
|
|
|
2022-10-27 21:44:52 +00:00
|
|
|
if (esnprintf(path, sizeof(path), POWER_SUPPLY_STATUS, bat) < 0)
|
2018-05-19 17:33:04 +00:00
|
|
|
return NULL;
|
2022-10-27 22:51:34 +00:00
|
|
|
if (pscanf(path, "%12[a-zA-Z ]", state) != 1)
|
2018-05-18 15:25:09 +00:00
|
|
|
return NULL;
|
|
|
|
|
2022-10-27 21:44:52 +00:00
|
|
|
if (!pick(bat, POWER_SUPPLY_CHARGE, POWER_SUPPLY_ENERGY, path,
|
2018-07-08 15:42:58 +00:00
|
|
|
sizeof(path)) ||
|
2022-10-27 21:44:52 +00:00
|
|
|
pscanf(path, "%ju", &charge_now) < 0)
|
2018-05-20 07:42:16 +00:00
|
|
|
return NULL;
|
|
|
|
|
2018-05-18 15:25:09 +00:00
|
|
|
if (!strcmp(state, "Discharging")) {
|
2022-10-27 21:44:52 +00:00
|
|
|
if (!pick(bat, POWER_SUPPLY_CURRENT, POWER_SUPPLY_POWER, path,
|
2018-05-20 22:36:59 +00:00
|
|
|
sizeof(path)) ||
|
2022-10-27 21:44:52 +00:00
|
|
|
pscanf(path, "%ju", ¤t_now) < 0)
|
2018-05-18 15:25:09 +00:00
|
|
|
return NULL;
|
|
|
|
|
2022-10-27 22:51:34 +00:00
|
|
|
if (current_now == 0)
|
2018-05-20 22:42:06 +00:00
|
|
|
return NULL;
|
|
|
|
|
2018-05-20 22:41:03 +00:00
|
|
|
timeleft = (double)charge_now / (double)current_now;
|
2018-05-18 15:25:09 +00:00
|
|
|
h = timeleft;
|
2018-05-20 22:41:03 +00:00
|
|
|
m = (timeleft - (double)h) * 60;
|
2018-05-18 15:25:09 +00:00
|
|
|
|
2018-07-07 09:08:26 +00:00
|
|
|
return bprintf("%juh %jum", h, m);
|
2018-05-18 15:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2018-05-18 12:43:04 +00:00
|
|
|
}
|
2018-05-01 17:01:22 +00:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <machine/apmvar.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2018-05-18 12:43:04 +00:00
|
|
|
static int
|
|
|
|
load_apm_power_info(struct apm_power_info *apm_info)
|
2018-05-01 17:01:22 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2018-05-01 17:01:22 +00:00
|
|
|
fd = open("/dev/apm", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2018-05-18 08:59:05 +00:00
|
|
|
warn("open '/dev/apm':");
|
2018-05-18 12:43:04 +00:00
|
|
|
return 0;
|
2018-05-01 17:01:22 +00:00
|
|
|
}
|
2017-09-17 14:18:17 +00:00
|
|
|
|
2018-05-18 12:43:04 +00:00
|
|
|
memset(apm_info, 0, sizeof(struct apm_power_info));
|
|
|
|
if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) {
|
2018-05-18 08:59:05 +00:00
|
|
|
warn("ioctl 'APM_IOC_GETPOWER':");
|
2018-05-01 17:01:22 +00:00
|
|
|
close(fd);
|
2018-05-18 12:43:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return close(fd), 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
battery_perc(const char *unused)
|
|
|
|
{
|
|
|
|
struct apm_power_info apm_info;
|
|
|
|
|
2022-10-27 22:51:34 +00:00
|
|
|
if (load_apm_power_info(&apm_info))
|
2018-05-20 22:16:54 +00:00
|
|
|
return bprintf("%d", apm_info.battery_life);
|
2018-05-01 17:01:22 +00:00
|
|
|
|
2018-05-18 12:43:04 +00:00
|
|
|
return NULL;
|
2017-09-17 14:18:17 +00:00
|
|
|
}
|
2018-05-01 18:45:29 +00:00
|
|
|
|
|
|
|
const char *
|
2018-05-17 18:05:57 +00:00
|
|
|
battery_state(const char *unused)
|
2018-05-01 18:45:29 +00:00
|
|
|
{
|
2018-05-19 11:25:35 +00:00
|
|
|
struct {
|
|
|
|
unsigned int state;
|
|
|
|
char *symbol;
|
|
|
|
} map[] = {
|
|
|
|
{ APM_AC_ON, "+" },
|
|
|
|
{ APM_AC_OFF, "-" },
|
|
|
|
};
|
2018-05-01 18:45:29 +00:00
|
|
|
struct apm_power_info apm_info;
|
2018-05-18 12:43:04 +00:00
|
|
|
size_t i;
|
2018-05-01 18:45:29 +00:00
|
|
|
|
2018-05-18 12:43:04 +00:00
|
|
|
if (load_apm_power_info(&apm_info)) {
|
2022-10-27 22:51:34 +00:00
|
|
|
for (i = 0; i < LEN(map); i++)
|
|
|
|
if (map[i].state == apm_info.ac_state)
|
2018-05-18 12:43:04 +00:00
|
|
|
break;
|
2022-10-27 22:51:34 +00:00
|
|
|
|
2018-05-18 12:43:04 +00:00
|
|
|
return (i == LEN(map)) ? "?" : map[i].symbol;
|
2018-05-01 18:45:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 12:43:04 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-01 18:45:29 +00:00
|
|
|
|
2018-05-18 12:43:04 +00:00
|
|
|
const char *
|
|
|
|
battery_remaining(const char *unused)
|
|
|
|
{
|
|
|
|
struct apm_power_info apm_info;
|
2022-10-27 21:47:01 +00:00
|
|
|
unsigned int h, m;
|
2018-05-18 12:43:04 +00:00
|
|
|
|
|
|
|
if (load_apm_power_info(&apm_info)) {
|
2018-05-18 14:33:51 +00:00
|
|
|
if (apm_info.ac_state != APM_AC_ON) {
|
2022-10-27 21:47:01 +00:00
|
|
|
h = apm_info.minutes_left / 60;
|
|
|
|
m = apm_info.minutes_left % 60;
|
|
|
|
return bprintf("%uh %02um", h, m);
|
2018-05-18 14:33:51 +00:00
|
|
|
} else {
|
2018-05-18 14:55:37 +00:00
|
|
|
return "";
|
2018-05-18 14:33:51 +00:00
|
|
|
}
|
2018-05-01 18:45:29 +00:00
|
|
|
}
|
2018-05-18 12:43:04 +00:00
|
|
|
|
|
|
|
return NULL;
|
2018-05-01 18:45:29 +00:00
|
|
|
}
|
2019-01-27 15:44:39 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
2022-10-27 21:44:52 +00:00
|
|
|
#define BATTERY_LIFE "hw.acpi.battery.life"
|
|
|
|
#define BATTERY_STATE "hw.acpi.battery.state"
|
|
|
|
#define BATTERY_TIME "hw.acpi.battery.time"
|
|
|
|
|
2019-01-27 15:44:39 +00:00
|
|
|
const char *
|
|
|
|
battery_perc(const char *unused)
|
|
|
|
{
|
2021-04-06 16:48:18 +00:00
|
|
|
int cap_perc;
|
2019-01-27 15:44:39 +00:00
|
|
|
size_t len;
|
|
|
|
|
2021-04-06 16:48:18 +00:00
|
|
|
len = sizeof(cap_perc);
|
|
|
|
if (sysctlbyname(BATTERY_LIFE, &cap_perc, &len, NULL, 0) < 0 || !len)
|
2019-01-27 15:44:39 +00:00
|
|
|
return NULL;
|
|
|
|
|
2021-04-06 16:48:18 +00:00
|
|
|
return bprintf("%d", cap_perc);
|
2019-01-27 15:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
battery_state(const char *unused)
|
|
|
|
{
|
|
|
|
int state;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = sizeof(state);
|
2022-10-27 21:44:52 +00:00
|
|
|
if (sysctlbyname(BATTERY_STATE, &state, &len, NULL, 0) < 0 || !len)
|
2019-01-27 15:44:39 +00:00
|
|
|
return NULL;
|
|
|
|
|
2022-10-27 22:49:31 +00:00
|
|
|
switch (state) {
|
|
|
|
case 0: /* FALLTHROUGH */
|
|
|
|
case 2:
|
|
|
|
return "+";
|
|
|
|
case 1:
|
|
|
|
return "-";
|
|
|
|
default:
|
|
|
|
return "?";
|
2019-01-27 15:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
battery_remaining(const char *unused)
|
|
|
|
{
|
|
|
|
int rem;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = sizeof(rem);
|
2022-10-27 21:44:52 +00:00
|
|
|
if (sysctlbyname(BATTERY_TIME, &rem, &len, NULL, 0) < 0 || !len
|
|
|
|
|| rem < 0)
|
2019-01-27 15:44:39 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return bprintf("%uh %02um", rem / 60, rem % 60);
|
|
|
|
}
|
Revert component-split
this reverts the commits from 92ab9ef52ebcb097add97d9f78e67ad1c1d6a6ec up to
d42870d6ca7fb587b38f8cf6d6821ae33a53a696.
After heavy consideration, the component split has more disadvantages
than advantages, especially given there will be utility-functions
sharing quite a lot of code that would then need to be duplicated, as it
does not fit into the util.c due to its speciality.
One big advantage of the component-wise build is readability, and
without doubt, this was achieved here. This point will be addressed
with a different approach that will be visible in the upcoming commits.
One big disadvantage of the component build is the fact that it
introduces state to the build process which is not necessary. Before its
introduction, the only influencing factors where the system-defines
__linux__ and __OpenBSD__. With the components, we are also relying on
the output of uname(1).
Additionally, if the os.mk is not present, make gives the output
$ make
Makefile:5: os.mk: No such file or directory
make: *** No rule to make target 'os.mk'. Stop.
This could easily be fixed by providing some sort of meta-rule for this
file, however, it indicates the problem we have here, and this entire
statefulness will heavily complicate packaging of this tool and makes
the build process much more complex than it actually has to be.
2018-05-01 16:10:39 +00:00
|
|
|
#endif
|