mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-04-23 20:35:42 +02:00
module/battery: use libudev to monitor for battery changes
This allows us to detect plug/unplug events immediately, instead of having to wait for the next poll event. Unfortunately, capacity changes do not appear to generate events :(
This commit is contained in:
parent
47b36bdd35
commit
fa3c17aa4a
2 changed files with 44 additions and 7 deletions
|
@ -15,6 +15,7 @@ pkg_check_modules(CAIRO REQUIRED cairo cairo-xcb) # Core
|
||||||
pkg_check_modules(YAML REQUIRED yaml-0.1) # Core (configuration)
|
pkg_check_modules(YAML REQUIRED yaml-0.1) # Core (configuration)
|
||||||
|
|
||||||
pkg_check_modules(JSON REQUIRED json-c) # Module/i3
|
pkg_check_modules(JSON REQUIRED json-c) # Module/i3
|
||||||
|
pkg_check_modules(UDEV REQUIRED libudev) # Module/battery
|
||||||
|
|
||||||
add_executable(f00bar
|
add_executable(f00bar
|
||||||
bar.c bar.h
|
bar.c bar.h
|
||||||
|
@ -47,6 +48,7 @@ target_compile_options(f00bar PRIVATE
|
||||||
${CAIRO_CFLAGS_OTHER}
|
${CAIRO_CFLAGS_OTHER}
|
||||||
${YAML_CFLAGS_OTHER}
|
${YAML_CFLAGS_OTHER}
|
||||||
${JSON_CFLAGS_OTHER}
|
${JSON_CFLAGS_OTHER}
|
||||||
|
${UDEV_CFLAGS_OTHER}
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(f00bar PRIVATE
|
target_include_directories(f00bar PRIVATE
|
||||||
|
@ -54,6 +56,7 @@ target_include_directories(f00bar PRIVATE
|
||||||
${CAIRO_INCLUDE_DIRS}
|
${CAIRO_INCLUDE_DIRS}
|
||||||
${YAML_INCLUDE_DIRS}
|
${YAML_INCLUDE_DIRS}
|
||||||
${JSON_INCLUDE_DIRS}
|
${JSON_INCLUDE_DIRS}
|
||||||
|
${UDEV_INCLUDE_DIRS}
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(f00bar
|
target_link_libraries(f00bar
|
||||||
|
@ -62,4 +65,5 @@ target_link_libraries(f00bar
|
||||||
${CAIRO_LIBRARIES}
|
${CAIRO_LIBRARIES}
|
||||||
${YAML_LIBRARIES}
|
${YAML_LIBRARIES}
|
||||||
${JSON_LIBRARIES}
|
${JSON_LIBRARIES}
|
||||||
|
${UDEV_LIBRARIES}
|
||||||
)
|
)
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include <libudev.h>
|
||||||
|
|
||||||
#include "../../bar.h"
|
#include "../../bar.h"
|
||||||
|
|
||||||
enum state { STATE_FULL, STATE_CHARGING, STATE_DISCHARGING };
|
enum state { STATE_FULL, STATE_CHARGING, STATE_DISCHARGING };
|
||||||
|
@ -183,7 +185,41 @@ run(struct module_run_context *ctx)
|
||||||
int power_fd = openat(base_dir_fd, "power_now", O_RDONLY);
|
int power_fd = openat(base_dir_fd, "power_now", O_RDONLY);
|
||||||
assert(power_fd != -1);
|
assert(power_fd != -1);
|
||||||
|
|
||||||
do {
|
struct udev *udev = udev_new();
|
||||||
|
assert(udev != NULL);
|
||||||
|
|
||||||
|
struct udev_monitor *mon = udev_monitor_new_from_netlink(udev, "udev");
|
||||||
|
assert(mon != NULL);
|
||||||
|
|
||||||
|
int r = udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
|
||||||
|
assert(r == 0);
|
||||||
|
r = udev_monitor_enable_receiving(mon);
|
||||||
|
assert(r == 0);
|
||||||
|
|
||||||
|
bool first = true;
|
||||||
|
while (true) {
|
||||||
|
if (!first) {
|
||||||
|
struct pollfd fds[] = {
|
||||||
|
{.fd = ctx->abort_fd, .events = POLLIN},
|
||||||
|
{.fd = udev_monitor_get_fd(mon), .events = POLLIN},
|
||||||
|
};
|
||||||
|
poll(fds, 2, m->poll_interval * 1000);
|
||||||
|
|
||||||
|
if (fds[0].revents & POLLIN)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (fds[1].revents & POLLIN) {
|
||||||
|
struct udev_device *dev = udev_monitor_receive_device(mon);
|
||||||
|
bool is_us = strcmp(udev_device_get_sysname(dev), m->battery) == 0;
|
||||||
|
|
||||||
|
udev_device_unref(dev);
|
||||||
|
|
||||||
|
if (!is_us)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
first = false;
|
||||||
|
|
||||||
const char *status = readline_from_fd(status_fd);
|
const char *status = readline_from_fd(status_fd);
|
||||||
if (strcmp(status, "Full") == 0)
|
if (strcmp(status, "Full") == 0)
|
||||||
m->state = STATE_FULL;
|
m->state = STATE_FULL;
|
||||||
|
@ -207,13 +243,10 @@ run(struct module_run_context *ctx)
|
||||||
// m->capacity, m->energy, m->power);
|
// m->capacity, m->energy, m->power);
|
||||||
|
|
||||||
bar->refresh(bar);
|
bar->refresh(bar);
|
||||||
|
}
|
||||||
|
|
||||||
struct pollfd fds[] = {{.fd = ctx->abort_fd, .events = POLLIN}};
|
udev_monitor_unref(mon);
|
||||||
poll(fds, 1, m->poll_interval * 1000);
|
udev_unref(udev);
|
||||||
|
|
||||||
if (fds[0].revents & POLLIN)
|
|
||||||
break;
|
|
||||||
} while (true);
|
|
||||||
|
|
||||||
close(power_fd);
|
close(power_fd);
|
||||||
close(energy_fd);
|
close(energy_fd);
|
||||||
|
|
Loading…
Add table
Reference in a new issue