commit b7e94be5d0c75c73367303f8819c4bff39f38618
parent bf41fc8e6c0b01735e1f3e546a30d08e17af5913
Author: Simon Lieb <simon@tibu.fr>
Date: Thu, 25 Feb 2016 13:14:36 +0100
Notify remaining time on SIGUSR1 signal
Diffstat:
| M | TODO | | | 7 | +++---- |
| M | spt.c | | | 31 | ++++++++++++++++++++++++++++--- |
2 files changed, 31 insertions(+), 7 deletions(-)
diff --git a/TODO b/TODO
@@ -1,7 +1,6 @@
-1. Show remaining time to the next timer (using SIGUSR1)
-2. Add #ifdef to make libnotify an optional dependency
-3. Add more support for non-libnotify such as echo(1)
-4. Pause and continue the timer
+1. Add #ifdef to make libnotify an optional dependency
+2. Add more support for non-libnotify such as echo(1)
+3. Pause and continue the timer
Misc
----
diff --git a/spt.c b/spt.c
@@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <signal.h>
#include <libnotify/notify.h>
@@ -21,10 +22,13 @@ typedef struct {
#include "config.h"
+static int i, timecount;
+
/* function declarations */
static void die(const char *errstr, ...);
static void spawn(char *);
static void notify_send(char *);
+static void remaining_time(int);
static void usage(void);
/* functions implementations */
@@ -76,6 +80,21 @@ notify_send(char *cmt)
}
void
+remaining_time(int sigint)
+{
+ char remainingtext[17];
+ if (signal(SIGUSR1, SIG_IGN) != SIG_IGN) {
+ signal(SIGUSR1, remaining_time);
+ }
+
+ snprintf(remainingtext, 17, "Remaining: %02d:%02d\n",
+ (timers[i].tmr - timecount) / 60,
+ (timers[i].tmr - timecount) % 60);
+
+ notify_send(remainingtext);
+}
+
+void
usage(void)
{
die("usage: %s [-e notifyext] [-n notifycmd] [-v]\n", argv0);
@@ -84,8 +103,6 @@ usage(void)
int
main(int argc, char *argv[])
{
- int i = 0;
-
ARGBEGIN {
case 'e':
notifyext = EARGF(usage());
@@ -101,9 +118,17 @@ main(int argc, char *argv[])
break;
} ARGEND;
+ if (signal(SIGUSR1, SIG_IGN) != SIG_IGN) {
+ signal(SIGUSR1, remaining_time);
+ }
+
run:
notify_send(timers[i].cmt);
- sleep(timers[i].tmr);
+
+ for (timecount = 0; timecount < timers[i].tmr; timecount++) {
+ sleep(1);
+ }
+
i + 1 >= LEN(timers) ? i = 0 : i++; /* i infinal loop */
goto run;