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 <string.h>
#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);
}

View File

@ -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;
}