move timerlib.h -> timerlib.c, also slight update

This commit is contained in:
randomuser 2021-06-11 00:06:29 -05:00
parent b700a35ded
commit 3ad3002985
2 changed files with 25 additions and 15 deletions

View File

@ -4,7 +4,7 @@
#include <poll.h> #include <poll.h>
#include <string.h> #include <string.h>
#include "timerlib.h" #include "timerlib.c"
struct settings { struct settings {
int e:1; /* use escape (v assumed) */ int e:1; /* use escape (v assumed) */
@ -51,12 +51,10 @@ void timerloop() {
t->c = timerissettings; t->c = timerissettings;
} }
char *c; char *c;
struct pollfd *p = malloc(sizeof p); struct pollfd p = { .fd = STDIN_FILENO, .events = POLLIN };
p->fd = STDIN_FILENO;
p->events = POLLIN;
for(;;) { for(;;) {
poll(p, 1, 60); poll(&p, 1, 60);
if(p->revents == POLLIN) { if(p.revents == POLLIN) {
/* TODO: make this nicer */ /* TODO: make this nicer */
getchar(); getchar();
if(settings.e) { if(settings.e) {
@ -73,7 +71,7 @@ void timerloop() {
fflush(stdout); fflush(stdout);
} }
else if(settings.v) printf("%s\n", c); else if(settings.v) printf("%s\n", c);
if(timerstate(t)) break; if(timerstop(t)) break;
timerupdate(t); timerupdate(t);
sleep(1); sleep(1);
} }

View File

@ -1,8 +1,10 @@
struct timer { struct timer {
int m; int m; /* minutes */
int s; int s; /* seconds */
void (*u)(struct timer *t); int d; /* data storage */
int (*c)(struct timer *t); 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) { void timerdec(struct timer *t) {
@ -25,24 +27,34 @@ void timerupdate(struct timer *t) {
if(t->u != NULL) t->u(t); if(t->u != NULL) t->u(t);
} }
int timerstate(struct timer *t) { int timerstate(int (*f)(struct timer *t), struct timer *t) {
if(t->c != NULL) { if(f != NULL) {
if(t->c(t)) return 1; if(f(t)) return 1;
else return 0; else return 0;
} }
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) { int timerzero(struct timer *t) {
if(t->m == 0 && t->s == 0) return 1; if(t->m == 0 && t->s == 0) return 1;
return 0; return 0;
} }
struct timer *timerinit(void) { struct timer *timerinit(void) {
struct timer *t = malloc(sizeof t); struct timer *t = malloc(sizeof *t);
t->m = 0; t->m = 0;
t->s = 0; t->s = 0;
t->d = 0;
t->u = NULL; t->u = NULL;
t->c = NULL; t->c = NULL;
t->p = NULL;
return t; return t;
} }