Notify remaining time on SIGUSR1 signal

This commit is contained in:
Simon Lieb 2016-02-25 13:14:36 +01:00 committed by Ivan Tham
parent bf41fc8e6c
commit b7e94be5d0
2 changed files with 31 additions and 7 deletions

7
TODO
View File

@ -1,7 +1,6 @@
1. Show remaining time to the next timer (using SIGUSR1) 1. Add #ifdef to make libnotify an optional dependency
2. Add #ifdef to make libnotify an optional dependency 2. Add more support for non-libnotify such as echo(1)
3. Add more support for non-libnotify such as echo(1) 3. Pause and continue the timer
4. Pause and continue the timer
Misc Misc
---- ----

31
spt.c
View File

@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#include <libnotify/notify.h> #include <libnotify/notify.h>
@ -21,10 +22,13 @@ typedef struct {
#include "config.h" #include "config.h"
static int i, timecount;
/* function declarations */ /* function declarations */
static void die(const char *errstr, ...); static void die(const char *errstr, ...);
static void spawn(char *); static void spawn(char *);
static void notify_send(char *); static void notify_send(char *);
static void remaining_time(int);
static void usage(void); static void usage(void);
/* functions implementations */ /* functions implementations */
@ -75,6 +79,21 @@ notify_send(char *cmt)
spawn(notifyext); spawn(notifyext);
} }
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 void
usage(void) usage(void)
{ {
@ -84,8 +103,6 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i = 0;
ARGBEGIN { ARGBEGIN {
case 'e': case 'e':
notifyext = EARGF(usage()); notifyext = EARGF(usage());
@ -101,9 +118,17 @@ main(int argc, char *argv[])
break; break;
} ARGEND; } ARGEND;
if (signal(SIGUSR1, SIG_IGN) != SIG_IGN) {
signal(SIGUSR1, remaining_time);
}
run: run:
notify_send(timers[i].cmt); 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 */ i + 1 >= LEN(timers) ? i = 0 : i++; /* i infinal loop */
goto run; goto run;