summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Light <sam@lightscale.co.uk>2025-09-10 19:37:28 +0100
committerSam Light <sam@lightscale.co.uk>2025-09-10 19:37:28 +0100
commit8101b1aa507795224054a752dd603ec3426c40ee (patch)
tree79c593f1d2a8c54f380f1d373a79f21cc5fc4a06
parentd9b456d738e5013d48f41f5e6315ef7c22d6acf0 (diff)
Created better management of updates + update on SIGUSR1
-rw-r--r--src/modules/battery.c16
-rw-r--r--src/modules/battery.h2
-rw-r--r--src/signal.c54
-rw-r--r--src/signal.h3
-rw-r--r--src/text.c20
-rw-r--r--src/text.h2
6 files changed, 73 insertions, 24 deletions
diff --git a/src/modules/battery.c b/src/modules/battery.c
index 6c656fe..18a68de 100644
--- a/src/modules/battery.c
+++ b/src/modules/battery.c
@@ -7,8 +7,6 @@
#include <fcntl.h>
#include <unistd.h>
-#define UPDATE_FREQUENCY 20
-
static int
open_battery_file(const char * name)
{
@@ -39,7 +37,6 @@ battery_new()
b->full = battery_value(file);
b->file_current = open_battery_file("charge_now");
- b->last_update = 0;
close(file);
@@ -58,16 +55,11 @@ void battery_update(struct battery * b)
int now;
float percentf;
int percent;
- time_t now_time = time(NULL);
-
- if((now_time - b->last_update) >= UPDATE_FREQUENCY) {
- b->last_update = now_time;
- now = battery_value(b->file_current);
+ now = battery_value(b->file_current);
- percentf = (100.0f / b->full) * now;
- percent = round(percentf);
+ percentf = (100.0f / b->full) * now;
+ percent = round(percentf);
- snprintf(b->value, BATTERY_VALUE_SIZE, "%d%%", percent);
- }
+ snprintf(b->value, BATTERY_VALUE_SIZE, "%d%%", percent);
}
diff --git a/src/modules/battery.h b/src/modules/battery.h
index 92489b5..f358e46 100644
--- a/src/modules/battery.h
+++ b/src/modules/battery.h
@@ -2,14 +2,12 @@
#define _DWXINFO_MODULES_BATTERY_H_
#include <stdio.h>
-#include <time.h>
#define BATTERY_VALUE_SIZE 5
struct battery {
int full;
int file_current;
- time_t last_update;
char value[BATTERY_VALUE_SIZE];
};
diff --git a/src/signal.c b/src/signal.c
index e993ae3..37aa26b 100644
--- a/src/signal.c
+++ b/src/signal.c
@@ -1,8 +1,10 @@
#include "signal.h"
+#include <stdio.h>
#include <string.h>
sig_atomic_t signal_running;
+sig_atomic_t signal_update;
static void
handle_signal_shutdown(int sig)
@@ -10,25 +12,61 @@ handle_signal_shutdown(int sig)
signal_running = 0;
}
+static void
+handle_signal_wake(int sig)
+{
+ signal_update = 1;
+}
+
+static int
+add_signal(struct sigaction * action, int sig)
+{
+ int rc;
+
+ rc = sigemptyset(&action->sa_mask);
+ if(rc < 0) return rc;
+ rc = sigaction(sig, action, 0);
+ if(rc < 0) return rc;
+
+ return rc;
+}
+
+static void
+init_action(struct sigaction * action, void(*cb)(int))
+{
+ memset(action, 0, sizeof(struct sigaction));
+
+ action->sa_handler = cb;
+}
+
int
signal_setup_actions()
{
int rc;
- struct sigaction action;
+ struct sigaction action_sd;
+ struct sigaction action_wake;
signal_running = 1;
+ signal_update = 0;
- memset(&action, 0, sizeof(action));
- action.sa_handler = handle_signal_shutdown;
- rc = sigemptyset(&action.sa_mask);
+ init_action(&action_sd, handle_signal_shutdown);
+ init_action(&action_wake, handle_signal_wake);
+
+ rc = add_signal(&action_sd, SIGINT);
if(rc < 0) return rc;
- rc = sigaction(SIGINT, &action, 0);
+ rc = add_signal(&action_sd, SIGTERM);
if(rc < 0) return rc;
- rc = sigemptyset(&action.sa_mask);
- if(rc < 0) return rc;
- rc = sigaction(SIGTERM, &action, 0);
+ rc = add_signal(&action_wake, SIGUSR1);
if(rc < 0) return rc;
return rc;
}
+
+bool
+signal_should_update()
+{
+ bool r = signal_update;
+ signal_update = 0;
+ return r;
+}
diff --git a/src/signal.h b/src/signal.h
index d3a513a..4205d2f 100644
--- a/src/signal.h
+++ b/src/signal.h
@@ -2,9 +2,12 @@
#define _DWXINFO_SIGNAL_H_
#include <signal.h>
+#include <stdbool.h>
extern sig_atomic_t signal_running;
+extern sig_atomic_t signal_update;
int signal_setup_actions();
+bool signal_should_update();
#endif
diff --git a/src/text.c b/src/text.c
index df3b3c4..ece25d5 100644
--- a/src/text.c
+++ b/src/text.c
@@ -1,5 +1,7 @@
#include "text.h"
+#include "signal.h"
+
#include "modules/pulse.h"
#include "modules/time.h"
#include "modules/battery.h"
@@ -17,6 +19,8 @@ text_new()
memset(t->value, '\0', TEXT_SIZE);
+ t->last_update = 0;
+
t->battery = battery_new();
t->time = time_new();
t->pulse = pulse_new();
@@ -36,9 +40,21 @@ text_free(struct text * t)
void
text_update(struct text * t)
{
+ time_t now_time = time(NULL);
+ double time_diff = difftime(now_time, t->last_update);
+ bool should_update = signal_should_update();
+
+ t->last_update = now_time;
+
time_update(t->time);
- pulse_update(t->pulse);
- battery_update(t->battery);
+
+ if(should_update || time_diff > 3) {
+ pulse_update(t->pulse);
+ }
+
+ if(should_update || time_diff > 5) {
+ battery_update(t->battery);
+ }
snprintf(
t->value, TEXT_SIZE,
diff --git a/src/text.h b/src/text.h
index 752d15f..ff6ead7 100644
--- a/src/text.h
+++ b/src/text.h
@@ -2,10 +2,12 @@
#define _DWXINFO_TEXT_H_
#include <stdio.h>
+#include <time.h>
#define TEXT_SIZE 512
struct text {
+ time_t last_update;
char value[TEXT_SIZE];
struct battery * battery;