2021-06-10 23:10:47 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2021-06-11 00:06:29 -05:00
|
|
|
#include "timerlib.c"
|
2021-06-10 23:10:47 -05:00
|
|
|
|
|
|
|
struct settings {
|
|
|
|
int e:1; /* use escape (v assumed) */
|
|
|
|
int v:1; /* verbose */
|
|
|
|
int d:1; /* count down/up (1/0) */
|
|
|
|
int b:1; /* ascii bel when done */
|
|
|
|
int f:1; /* display hours */
|
|
|
|
int s; /* seconds */
|
2021-06-11 00:29:37 -05:00
|
|
|
} s = {
|
2021-06-10 23:10:47 -05:00
|
|
|
.e = 0,
|
|
|
|
.v = 0,
|
|
|
|
.d = 0,
|
|
|
|
.b = 0,
|
|
|
|
.f = 0,
|
|
|
|
.s = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
int timerissettings(struct timer *t) {
|
2021-06-11 00:26:49 -05:00
|
|
|
if(t->s == 0) return 0;
|
2021-06-11 00:49:03 -05:00
|
|
|
if(s.s == t->s) return 1;
|
2021-06-10 23:10:47 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *timerdisp(struct timer *t) {
|
|
|
|
char *str = malloc(20);
|
2021-06-11 00:29:37 -05:00
|
|
|
if(s.f) snprintf(str, 20, "%02i:%02i:%02i",
|
2021-06-11 00:58:16 -05:00
|
|
|
hours(t->s), minutes(t->s), seconds(t->s));
|
|
|
|
/* TODO: give minute(...) and minutes(...) better names */
|
|
|
|
else snprintf(str, 20, "%02i:%02i", minute(t->s), seconds(t->s));
|
2021-06-10 23:10:47 -05:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void timerloop() {
|
|
|
|
struct timer *t = timerinit();
|
2021-06-11 00:29:37 -05:00
|
|
|
if(s.d) {
|
2021-06-10 23:10:47 -05:00
|
|
|
t->u = timerdec;
|
2021-06-11 00:49:03 -05:00
|
|
|
t->s = s.s;
|
2021-06-10 23:10:47 -05:00
|
|
|
t->c = timerzero;
|
|
|
|
} else {
|
|
|
|
t->u = timerinc;
|
|
|
|
t->c = timerissettings;
|
|
|
|
}
|
|
|
|
char *c;
|
2021-06-11 00:06:29 -05:00
|
|
|
struct pollfd p = { .fd = STDIN_FILENO, .events = POLLIN };
|
2021-06-10 23:10:47 -05:00
|
|
|
for(;;) {
|
2021-06-11 00:06:29 -05:00
|
|
|
poll(&p, 1, 60);
|
|
|
|
if(p.revents == POLLIN) {
|
2021-06-10 23:10:47 -05:00
|
|
|
/* TODO: make this nicer */
|
|
|
|
getchar();
|
2021-06-11 00:29:37 -05:00
|
|
|
if(s.e) {
|
2021-06-10 23:10:47 -05:00
|
|
|
c = timerdisp(t);
|
|
|
|
printf("\r\e[1A* %s", c);
|
|
|
|
}
|
|
|
|
getchar();
|
|
|
|
/* TODO: stop relying on hard assumptions */
|
2021-06-11 00:32:02 -05:00
|
|
|
if(s.e) printf("\r\e[1A \r");
|
2021-06-10 23:10:47 -05:00
|
|
|
}
|
|
|
|
c = timerdisp(t);
|
2021-06-11 00:29:37 -05:00
|
|
|
if(s.e) {
|
2021-06-10 23:10:47 -05:00
|
|
|
printf("%s\r", c);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2021-06-11 00:29:37 -05:00
|
|
|
else if(s.v) printf("%s\n", c);
|
2021-06-11 00:06:29 -05:00
|
|
|
if(timerstop(t)) break;
|
2021-06-11 00:26:49 -05:00
|
|
|
free(c);
|
2021-06-10 23:10:47 -05:00
|
|
|
timerupdate(t);
|
|
|
|
sleep(1);
|
|
|
|
}
|
2021-06-11 00:29:37 -05:00
|
|
|
if(s.b) putchar('\a');
|
2021-06-11 00:26:49 -05:00
|
|
|
free(t);
|
|
|
|
free(c);
|
2021-06-10 23:10:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
char c;
|
2021-06-11 00:59:07 -05:00
|
|
|
while((c = getopt(argc, argv, "evdbfm:s:")) != -1) {
|
2021-06-10 23:10:47 -05:00
|
|
|
switch(c) {
|
2021-06-11 00:29:37 -05:00
|
|
|
break; case 'e': s.e = 1;
|
|
|
|
break; case 'v': s.v = 1;
|
|
|
|
break; case 'd': s.d = 1;
|
|
|
|
break; case 'b': s.b = 1;
|
|
|
|
break; case 'f': s.f = 1;
|
2021-06-11 00:59:40 -05:00
|
|
|
break; case 'h': s.s = s.s + (atoi(optarg) * 3600);
|
2021-06-11 00:49:03 -05:00
|
|
|
break; case 'm': s.s = s.s + (atoi(optarg) * 60);
|
|
|
|
break; case 's': s.s = s.s + atoi(optarg);
|
2021-06-10 23:23:06 -05:00
|
|
|
break; case '?': return 1;
|
|
|
|
break; default: abort();
|
2021-06-10 23:10:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
timerloop();
|
|
|
|
return 0;
|
|
|
|
}
|