From eb951cda685b7a819d2fe123efb328aba82bff8f Mon Sep 17 00:00:00 2001 From: Squibid Date: Mon, 5 Jan 2026 14:13:40 -0500 Subject: [PATCH] fix battery events not getting called and add debug flag --- src/main.c | 78 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/main.c b/src/main.c index bda1518..8188486 100644 --- a/src/main.c +++ b/src/main.c @@ -31,19 +31,24 @@ bool led_ok, wayland_ok; static struct cag_option options[] = { - { - .identifier = 'c', - .access_letters = "c", - .access_name = "config", - .value_name = "PATH", - .description = "Config file" - }, - { - .identifier = 'h', - .access_letters = "h", - .access_name = "help", - .description = "Shows the command help" - } + { + .identifier = 'c', + .access_letters = "c", + .access_name = "config", + .value_name = "PATH", + .description = "Config file" + }, + { + .identifier = 'd', + .access_letters = "d", + .description = "Emit debug info" + }, + { + .identifier = 'h', + .access_letters = "h", + .access_name = "help", + .description = "Shows the command help" + } }; void @@ -68,10 +73,10 @@ on_battery_event(char *percent, bool plugged) } ); - wrap(wayland_ok, wayland_set_idle_lock(false)); + wrap(wayland_ok, wayland_set_idle_lock(false)); } else { wrap(led_ok, led_set_rate(0)); - wrap(wayland_ok, wayland_set_idle_lock(true)); + wrap(wayland_ok, wayland_set_idle_lock(true)); } free(percent); @@ -84,32 +89,36 @@ main(int argc, char *argv[]) bool plugged = plugged_in(battery_power_source); char buffer[MAX_BUFLEN], *out[4], *config_path, *percent; int sock_fd; + bool debug = false; /* defaults to working */ led_ok = wayland_ok = true; cag_option_init(&context, options, CAG_ARRAY_SIZE(options), argc, argv); while (cag_option_fetch(&context)) { - switch (cag_option_get_identifier(&context)) { - case 'c': - config_path = (char *)cag_option_get_value(&context); - parse_config_file(config_path); - break; - case 'h': - printf("Usage: eh [OPTION]...\n"); - cag_option_print(options, CAG_ARRAY_SIZE(options), stdout); - return EXIT_SUCCESS; - case '?': - cag_option_print_error(&context, stdout); - break; - } + switch (cag_option_get_identifier(&context)) { + case 'c': + config_path = (char *)cag_option_get_value(&context); + parse_config_file(config_path); + break; + case 'd': + debug = true; + break; + case 'h': + printf("Usage: eh [OPTION]...\n"); + cag_option_print(options, CAG_ARRAY_SIZE(options), stdout); + return EXIT_SUCCESS; + case '?': + cag_option_print_error(&context, stdout); + break; + } } - /* configure the led blinking formula */ - setup_led_formula(); + /* configure the led blinking formula */ + setup_led_formula(); /* set the logging level */ - log_set_level(LOG_DEBUG); + log_set_level(debug ? LOG_TRACE : LOG_INFO); /* open up socket address */ sock_fd = acpi_create_socket(acpi_daemon_socket_path); @@ -119,7 +128,7 @@ main(int argc, char *argv[]) led_ok = false; } - /* create the wayland thread */ + /* create the wayland thread */ if (wayland_create_thread()) { wayland_ok = false; } @@ -147,6 +156,7 @@ main(int argc, char *argv[]) if (acpi_parse(buffer, out) != 0) { continue; } + log_trace("parsed buf: %s, %s, %s, %s", out[0], out[1], out[2], out[3]); /* rules */ if (strcmp(out[0], "ac_adapter") == 0) { @@ -154,6 +164,7 @@ main(int argc, char *argv[]) if (strcmp(s, "AC") == 0) { plugged = plugged_in(battery_power_source); + log_trace("%splugged", plugged ? "" : "un"); } free(s); @@ -164,7 +175,8 @@ main(int argc, char *argv[]) if (strcmp(out[2], "00000080") == 0) { if (strcmp(out[3], "00000001") == 0) { percent = battery_percent(); - if (percent == NULL) { + log_trace("percent: %s", percent); + if (percent != NULL) { on_battery_event(percent, plugged); } }