2023-09-14 21:36:33 +00:00
|
|
|
#include <ncurses.h>
|
|
|
|
#include <stdlib.h>
|
2023-10-07 14:04:55 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2023-09-14 21:36:33 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
clock_t sec;
|
|
|
|
clock_t msec;
|
|
|
|
} TIME;
|
|
|
|
TIME *timer;
|
|
|
|
size_t timer_size;
|
|
|
|
size_t cursor;
|
|
|
|
|
|
|
|
void die(char *msg) {
|
|
|
|
endwin();
|
2023-10-07 14:04:55 +00:00
|
|
|
printf("%s: %s\n", msg, strerror(errno));
|
2023-09-14 21:36:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_value(clock_t sec, clock_t msec) {
|
2023-10-07 14:04:55 +00:00
|
|
|
timer_size++;
|
2023-09-14 21:36:33 +00:00
|
|
|
|
|
|
|
timer = realloc(timer, (1 + timer_size) * sizeof(TIME));
|
|
|
|
if (timer == NULL)
|
|
|
|
die("realloc returned NULL");
|
|
|
|
|
|
|
|
timer[timer_size].sec = sec;
|
|
|
|
timer[timer_size].msec = msec;
|
|
|
|
}
|
|
|
|
|
2023-10-07 14:04:55 +00:00
|
|
|
void keyboard(clock_t sec, clock_t mcsec) {
|
|
|
|
int key = getch();
|
|
|
|
if (key < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
clear();
|
|
|
|
switch (key) {
|
|
|
|
case ' ':
|
|
|
|
add_value(sec, mcsec / 10000);
|
|
|
|
cursor = timer_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
if (cursor + 1 <= timer_size)
|
|
|
|
cursor++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
if (cursor - 1 > 0)
|
|
|
|
cursor--;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
timeout(-1);
|
|
|
|
getch();
|
|
|
|
timeout(0);
|
|
|
|
clear();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
endwin();
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_timer(void) {
|
|
|
|
if (timer_size > 0) {
|
|
|
|
/* Start y */
|
|
|
|
unsigned int y = 3;
|
|
|
|
mvprintw(y, 0, ">");
|
|
|
|
|
|
|
|
for (size_t i = cursor; i < cursor + 10; i++) {
|
|
|
|
if (i > timer_size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
mvprintw(y++, 2, "(%ld) %ld.%ld", i, timer[i].sec, timer[i].msec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 21:36:33 +00:00
|
|
|
int main(void) {
|
|
|
|
|
|
|
|
/* Init array */
|
|
|
|
timer = malloc(sizeof(TIME));
|
|
|
|
if (timer == NULL)
|
|
|
|
die("malloc returned NULL");
|
|
|
|
|
2023-10-07 14:04:55 +00:00
|
|
|
/* Init terminal */
|
2023-09-14 21:36:33 +00:00
|
|
|
initscr();
|
2023-09-15 07:02:41 +00:00
|
|
|
noecho();
|
2023-09-14 21:36:33 +00:00
|
|
|
timeout(0);
|
|
|
|
curs_set(0);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
/* Microsecond */
|
|
|
|
clock_t mcsec = clock();
|
|
|
|
if (mcsec == -1)
|
2023-10-07 14:04:55 +00:00
|
|
|
die("");
|
2023-09-14 21:36:33 +00:00
|
|
|
|
|
|
|
clock_t sec = mcsec / 1000000;
|
|
|
|
clock_t msec = (mcsec / 100000) % 10;
|
|
|
|
|
|
|
|
mvprintw(1, 2, "%ld.%ld", sec, msec);
|
|
|
|
|
2023-10-07 14:04:55 +00:00
|
|
|
print_timer();
|
|
|
|
keyboard(sec, mcsec);
|
2023-09-14 21:36:33 +00:00
|
|
|
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|