summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--components.c66
-rw-r--r--config.h8
2 files changed, 72 insertions, 2 deletions
diff --git a/components.c b/components.c
index 4611999..f00619b 100644
--- a/components.c
+++ b/components.c
@@ -13,10 +13,60 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <unistd.h>
+#include <signal.h>
#include "components.h"
#include "config.h"
int
+run(const char *program)
+{
+ pid_t pid = fork();
+ if (pid == 0)
+ {
+ // child process, we don't want to ignore signals
+ signal(SIGCHLD, SIG_DFL);
+ /*
+ * we don't want std{out,err} to be associated with the terminal,
+ * but we also don't want to close it to avoid the file descriptors
+ * being re-used potentially leading to problems, so reopen them to /dev/null
+ */
+ freopen("/dev/null", "w", stdout);
+ freopen("/dev/null", "w", stderr);
+ char *args[ARG_LIMIT];
+ char *buff = malloc(BUFF_SIZE);
+ if (buff == NULL)
+ {
+ perror("malloc");
+ return -1;
+ }
+ /*
+ * the program we're calling might have arguments,
+ * so we tokenise the string and add each part to an array
+ * that we will use in execvp
+ */
+ strncpy(buff, program, BUFF_SIZE-1);
+ char *t = strtok(buff, " ");
+ int z = 0;
+ while (t != NULL && z < ARG_LIMIT-1) // save a position for NULL
+ {
+ args[z] = t;
+ t = strtok(NULL, " ");
+ z++;
+ }
+ args[z] = (char *)0;
+ execvp(args[0], args);
+ _exit(1);
+ }
+ else if (pid == -1)
+ {
+ perror("fork");
+ return -1;
+ }
+ return 1;
+}
+
+int
dectobin(int dec)
{
if (dec == 0) return 0;
@@ -86,6 +136,8 @@ char *currenttime(char *store, size_t size, int flag)
return store;
}
+int has_low_batt = 0;
+
char *battery(char *store, size_t size, int flag)
{
FILE *capacity;
@@ -98,7 +150,19 @@ char *battery(char *store, size_t size, int flag)
}
fread(cap, 1, 4, capacity);
fclose(capacity);
- snprintf(store, size, "%d%% ", atoi(cap));
+ int percent = atoi(cap);
+
+ if ( !has_low_batt && percent < LOWBATT)
+ {
+ run(LOWBATTERY);
+ has_low_batt = 1;
+ }
+ else if (has_low_batt && percent > LOWBATT)
+ {
+ has_low_batt = 0;
+ }
+
+ snprintf(store, size, "%d%% ", percent);
return store;
}
diff --git a/config.h b/config.h
index 4db0606..87fd568 100644
--- a/config.h
+++ b/config.h
@@ -20,6 +20,12 @@
#define TEMPFILE "/sys/class/thermal/thermal_zone0/temp"
#define MAXLENGTH 256
+#define LOWBATT 65
+#define ARG_LIMIT 10
+#define BUFF_SIZE 512
+
+#define LOWBATTERY "sh -c /home/daniel_j/programming/bash/lowbattery.sh"
+
enum flag
{
NONE = 0,
@@ -42,10 +48,10 @@ struct component
*/
static const struct component components[] ={
/* function flag */
- {currenttime, NORMALTIME|SHOWMERIDIEM},
{battery, NONE},
{charging, NONE},
{cputemp, NONE},
+ {currenttime, NORMALTIME|SHOWMERIDIEM},
};
#endif