[PATCH poweralertd v2 1/3] feat: implement ignore and silent start option
Export this patch
From: Cyril Levis <cyril@levis.name>
we can use several `-i`, eg: `-i "line power" -i mouse`.
---
main.c | 41 +++++++++++++++++++++++++++++++++++++++++
upower.c | 11 ++++++++++-
upower.h | 4 +++-
3 files changed, 54 insertions(+), 2 deletions(-)
diff --git a/main.c b/main.c
index 6676933..3e11d83 100644
--- a/main.c
+++ b/main.c
@@ -2,6 +2,7 @@
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
@@ -107,6 +108,7 @@ static int send_warning_update(sd_bus *bus, struct upower_device *device) {
break;
case UPOWER_DEVICE_LEVEL_CRITICAL:
msg = "Warning: power level critical\n";
+ urgency = URGENCY_CRITICAL;
break;
case UPOWER_DEVICE_LEVEL_ACTION:
msg = "Warning: power level at action threshold\n";
@@ -126,6 +128,31 @@ static int send_warning_update(sd_bus *bus, struct upower_device *device) {
}
int main(int argc, char *argv[]) {
+ int opt = 0;
+ int device_type = 0;
+ int ignore_types_mask = 0;
+ bool ignore_initial = false;
+
+ while ((opt = getopt(argc, argv, "si:")) != -1) {
+ switch (opt) {
+ case 'i':
+ device_type = upower_device_type_int(optarg);
+ if (device_type > -1) {
+ ignore_types_mask |= 1 << device_type;
+ }
+ else {
+ printf("Unrecognized device type: %s\n", optarg);
+ }
+ break;
+ case 's':
+ ignore_initial = true;
+ break;
+ case '?':
+ fprintf(stderr, "Usage: %s [-i type] [-s]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ }
+
struct upower state = { 0 };
sd_bus *user_bus = NULL;
sd_bus *system_bus = NULL;
@@ -155,6 +182,14 @@ int main(int argc, char *argv[]) {
for (int idx = 0; idx < state.devices->length; idx++) {
struct upower_device *device = state.devices->items[idx];
+ if ((ignore_types_mask & (1 << device->type))) {
+ continue;
+ }
+
+ if (device->current.serial == 0 && ignore_initial) {
+ continue;
+ }
+
if (upower_device_has_battery(device)) {
ret = send_state_update(user_bus, device);
if (ret < 0) {
@@ -203,6 +238,12 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "could not wait for system bus messages: %s\n", strerror(-ret));
goto finish;
}
+
+ for (int idx = 0; idx < state.devices->length; idx++) {
+ struct upower_device *device = state.devices->items[idx];
+ device->current.serial = device->last.serial + 1;
+ }
+
}
finish:
diff --git a/upower.c b/upower.c
index 5ffa922..62678f2 100644
--- a/upower.c
+++ b/upower.c
@@ -120,6 +120,15 @@ char* upower_device_type_string(struct upower_device *device) {
return "unknown";
}
+int upower_device_type_int(char *device) {
+ for (int i=0; i < UPOWER_DEVICE_TYPE_LAST; i++) {
+ if (!strcmp(upower_type_string[i], device)) {
+ return i;
+ }
+ }
+ return -1;
+}
+
static int upower_device_update_state(sd_bus *bus, struct upower_device *device) {
sd_bus_error error = SD_BUS_ERROR_NULL;
int ret;
@@ -515,4 +524,4 @@ void destroy_upower(sd_bus *bus, struct upower *state) {
list_free(state->devices);
state->devices = NULL;
}
-}
\ No newline at end of file
+}
diff --git a/upower.h b/upower.h
index 81d3d41..a397c40 100644
--- a/upower.h
+++ b/upower.h
@@ -62,6 +62,7 @@ enum change_slot {
struct upower_device_props {
int generation;
int online;
+ int serial;
double percentage;
enum upower_device_state state;
enum upower_device_level warning_level;
@@ -98,9 +99,10 @@ char* upower_device_state_string(struct upower_device *device);
char* upower_device_warning_level_string(struct upower_device *device);
char* upower_device_battery_level_string(struct upower_device *device);
char* upower_device_type_string(struct upower_device *device);
+int upower_device_type_int(char *device);
void upower_device_destroy(struct upower_device *device);
int init_upower(sd_bus *bus, struct upower *state);
void destroy_upower(sd_bus *bus, struct upower *state);
-#endif
\ No newline at end of file
+#endif
--
2.32.0
[PATCH poweralertd v2 2/3] feat: add usage -h
Export this patch
From: Cyril Levis <cyril@levis.name>
---
main.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/main.c b/main.c
index 3e11d83..2148b6d 100644
--- a/main.c
+++ b/main.c
@@ -127,13 +127,19 @@ static int send_warning_update(sd_bus *bus, struct upower_device *device) {
return notify(bus, title, msg, &device->notifications[SLOT_WARNING], urgency);
}
+static const char usage[] = "usage: %s [options]\n"
+" -h show this help message\n"
+" -s ignore the events at startup\n"
+" -i <device_type> ignore this device type, can be use several times\n";
+
+
int main(int argc, char *argv[]) {
int opt = 0;
int device_type = 0;
int ignore_types_mask = 0;
bool ignore_initial = false;
- while ((opt = getopt(argc, argv, "si:")) != -1) {
+ while ((opt = getopt(argc, argv, "hsi:")) != -1) {
switch (opt) {
case 'i':
device_type = upower_device_type_int(optarg);
@@ -147,9 +153,10 @@ int main(int argc, char *argv[]) {
case 's':
ignore_initial = true;
break;
- case '?':
- fprintf(stderr, "Usage: %s [-i type] [-s]\n", argv[0]);
- exit(EXIT_FAILURE);
+ case 'h':
+ default:
+ fprintf(stderr, usage, argv[0]);
+ return opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE;
}
}
--
2.32.0
[PATCH poweralertd v2 3/3] fix: make silent global instead of local
Export this patch
From: Cyril Levis <cyril@levis.name>
I want at startup no notification but everything after.
---
main.c | 15 +++++++--------
upower.h | 1 -
2 files changed, 7 insertions(+), 9 deletions(-)
diff --git a/main.c b/main.c
index 2148b6d..1be4449 100644
--- a/main.c
+++ b/main.c
@@ -138,6 +138,7 @@ int main(int argc, char *argv[]) {
int device_type = 0;
int ignore_types_mask = 0;
bool ignore_initial = false;
+ bool initialized = false;
while ((opt = getopt(argc, argv, "hsi:")) != -1) {
switch (opt) {
@@ -190,13 +191,14 @@ int main(int argc, char *argv[]) {
struct upower_device *device = state.devices->items[idx];
if ((ignore_types_mask & (1 << device->type))) {
- continue;
+ goto next_device;
}
- if (device->current.serial == 0 && ignore_initial) {
- continue;
+ if (!initialized && ignore_initial) {
+ goto next_device;
}
+
if (upower_device_has_battery(device)) {
ret = send_state_update(user_bus, device);
if (ret < 0) {
@@ -215,7 +217,7 @@ int main(int argc, char *argv[]) {
goto finish;
}
}
-
+next_device:
device->last = device->current;
}
@@ -246,10 +248,7 @@ int main(int argc, char *argv[]) {
goto finish;
}
- for (int idx = 0; idx < state.devices->length; idx++) {
- struct upower_device *device = state.devices->items[idx];
- device->current.serial = device->last.serial + 1;
- }
+ initialized = true;
}
diff --git a/upower.h b/upower.h
index a397c40..f914a10 100644
--- a/upower.h
+++ b/upower.h
@@ -62,7 +62,6 @@ enum change_slot {
struct upower_device_props {
int generation;
int online;
- int serial;
double percentage;
enum upower_device_state state;
enum upower_device_level warning_level;
--
2.32.0
poweralertd/patches/.build.yml: SUCCESS in 18s
[feat: implement ignore and silent start option][0] v2 from [~cyrinux][1]
[0]: https://lists.sr.ht/~kennylevinsen/poweralertd-devel/patches/26916
[1]: mailto:cyril@levis.name
✓ #637702 SUCCESS poweralertd/patches/.build.yml https://builds.sr.ht/~kennylevinsen/job/637702
Applied after squashing and fixing some mixed indentation, thanks!