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)
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
----

31
spt.c
View File

@ -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 */
@ -75,6 +79,21 @@ notify_send(char *cmt)
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
usage(void)
{
@ -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;