aboutsummaryrefslogtreecommitdiff
path: root/stopwatch.c
blob: bb7a90ef14aa10e81282bbe38e27d4b8843ab140 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>

#define NB_EOF   -1
#define NB_AGAIN -2

static char   buffer[ BUFSIZ ];
static size_t buf_pos  = 0;
static size_t buf_fill = 0;

#define fold inline __attribute__((always_inline)) static

fold void set_stdin_nonblocking(void) {
    static int done = 0;
    if (!done) {
        int flags = fcntl(STDIN_FILENO, F_GETFL);
        if (flags != -1) { fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); }
        done = 1;
    }
}

fold int nb_getchar(void) {
    set_stdin_nonblocking();

    if (buf_pos < buf_fill) { return (unsigned char) buffer[ buf_pos++ ]; }

    ssize_t n;
    do { n = read(STDIN_FILENO, buffer, sizeof(buffer)); } while (n == -1 && errno == EINTR);

    if (n == -1) {
        if (errno == EAGAIN || errno == EWOULDBLOCK) { return NB_AGAIN; }
        return NB_EOF;
    }

    if (n == 0) { return NB_EOF; }

    buf_pos  = 0;
    buf_fill = n;

    return (unsigned char) buffer[ buf_pos++ ];
}

fold uint64_t time_ms(void) {
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    return (uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000;
}

#define clear() printf("\033[2J\033[H")

static struct termios original_tio;
static int            tio_saved = 0;

void enter_state(void) {
    if (!isatty(STDIN_FILENO)) return;
    if (!tio_saved) {
        if (tcgetattr(STDIN_FILENO, &original_tio) == -1) {
            perror("tcgetattr");
            exit(1);
        }
        tio_saved = 1;
    }
    struct termios raw = original_tio;
    raw.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
    raw.c_oflag &= ~OPOST;
    raw.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    raw.c_cflag &= ~(CSIZE | PARENB);
    raw.c_cflag |= CS8;
    raw.c_cc[ VMIN ]  = 1;
    raw.c_cc[ VTIME ] = 0;
    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) {
        perror("tcsetattr (raw)");
        exit(1);
    }
}

void exit_state(void) {
    if (tio_saved && isatty(STDIN_FILENO)) {
        if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_tio) == -1) {
            perror("tcsetattr (restore)");
            exit(1);
        }
    }
}

void bye() {
    printf("\r\n\ngoodbye.\r\n");
    exit(0);
}

fold void print_dur(uint64_t duration) {
    uint64_t hours = 0, minutes = 0, seconds = 0, ms = 0;

    ms = duration % 1000;
    duration /= 1000;
    seconds = duration % 60;
    duration /= 60;
    minutes = duration % 60;
    duration /= 60;
    hours = duration;

    printf("%2" PRIu64 "h ", hours);
    printf("%2" PRIu64 "m ", minutes);
    printf("%2" PRIu64 "s ", seconds);
    printf("%4" PRIu64 "ms", ms);
    fflush(stdout);
}

fold void yield(void) {
    struct timespec ts = { .tv_sec = 0, .tv_nsec = 999000 };
    nanosleep(&ts, NULL);
}

#define unlikely(a) __builtin_expect(!!(a), 0)

int main() {
    enter_state();
    atexit(exit_state);

    printf(
        "\r\nstopwatch\r\n\n"
        "instructions:\r\n"
        "  space => pause/resume\r\n"
        "  ctrl+c / ctrl+d / eof => exit\r\n"
        "  press space to start.\r\n"
        "\r\n");
    int c;
    while ((c = nb_getchar()) != ' ') {
        if (unlikely(c == 3 || c == 4 || c == NB_EOF)) bye();
        yield();
    }

    uint64_t tally = 0;
    uint64_t start;

    while (1) {
        uint64_t cur;
        start = time_ms();

        char parity = 1;
        while ((c = nb_getchar()) != ' ') {
            // clear();
            cur = time_ms();

            if (parity) {
                printf("\r");
                printf("[ ] ");
                print_dur(cur - start + tally);
            }

            parity = !parity;

            if (unlikely(c == 3 || c == 4 || c == NB_EOF)) bye();
            yield();
        }

        tally += time_ms() - start;

        printf("\r");
        printf("[x] ");
        print_dur(tally);

        while ((c = nb_getchar()) != ' ') {
            if (unlikely(c == 3 || c == 4 || c == NB_EOF)) bye();
            yield();
        }
    }

    bye();
}