diff --git a/progs/timer.c b/progs/timer.c index bcef269..f5ba88e 100644 --- a/progs/timer.c +++ b/progs/timer.c @@ -4,7 +4,7 @@ #include #include -#include "timerlib.h" +#include "timerlib.c" struct settings { int e:1; /* use escape (v assumed) */ @@ -51,12 +51,10 @@ void timerloop() { t->c = timerissettings; } char *c; - struct pollfd *p = malloc(sizeof p); - p->fd = STDIN_FILENO; - p->events = POLLIN; + struct pollfd p = { .fd = STDIN_FILENO, .events = POLLIN }; for(;;) { - poll(p, 1, 60); - if(p->revents == POLLIN) { + poll(&p, 1, 60); + if(p.revents == POLLIN) { /* TODO: make this nicer */ getchar(); if(settings.e) { @@ -73,7 +71,7 @@ void timerloop() { fflush(stdout); } else if(settings.v) printf("%s\n", c); - if(timerstate(t)) break; + if(timerstop(t)) break; timerupdate(t); sleep(1); } diff --git a/progs/timerlib.h b/progs/timerlib.c similarity index 50% rename from progs/timerlib.h rename to progs/timerlib.c index df5aeb3..f0ea3c3 100644 --- a/progs/timerlib.h +++ b/progs/timerlib.c @@ -1,8 +1,10 @@ struct timer { - int m; - int s; - void (*u)(struct timer *t); - int (*c)(struct timer *t); + int m; /* minutes */ + int s; /* seconds */ + int d; /* data storage */ + void (*u)(struct timer *t); /* update function */ + int (*c)(struct timer *t); /* stop check function */ + int (*p)(struct timer *t); /* pause check function */ }; void timerdec(struct timer *t) { @@ -25,24 +27,34 @@ void timerupdate(struct timer *t) { if(t->u != NULL) t->u(t); } -int timerstate(struct timer *t) { - if(t->c != NULL) { - if(t->c(t)) return 1; +int timerstate(int (*f)(struct timer *t), struct timer *t) { + if(f != NULL) { + if(f(t)) return 1; else return 0; } return 0; } +int timerstop(struct timer *t) { + return timerstate(t->c, t); +} + +int timerpause(struct timer *t) { + return timerstate(t->p, t); +} + int timerzero(struct timer *t) { if(t->m == 0 && t->s == 0) return 1; return 0; } struct timer *timerinit(void) { - struct timer *t = malloc(sizeof t); + struct timer *t = malloc(sizeof *t); t->m = 0; t->s = 0; + t->d = 0; t->u = NULL; t->c = NULL; + t->p = NULL; return t; }