diff options
| author | 0scar <qgt268@alumni.ku.dk> | 2025-02-11 09:25:50 +0000 |
|---|---|---|
| committer | 0scar <qgt268@alumni.ku.dk> | 2025-02-11 09:26:19 +0000 |
| commit | e152ee5f0c5a47e17a6ce06de9e928eedae17152 (patch) | |
| tree | a508e6591af8ed5233349af192d50265314991d6 /status.c | |
| parent | e89cb2e54900402027ff850850319720978109ef (diff) | |
Fix timings haha
Diffstat (limited to 'status.c')
| -rw-r--r-- | status.c | 34 |
1 files changed, 17 insertions, 17 deletions
@@ -380,7 +380,7 @@ time_add(struct timespec a, struct timespec b) { dst.tv_nsec = a.tv_nsec + b.tv_nsec; /* larger than 1 second in μsec */ - if (dst.tv_nsec >= 1000000) { + if (dst.tv_nsec >= 1000000 * 1000) { dst.tv_nsec -= 1000000 * 1000; dst.tv_sec++; } @@ -393,8 +393,8 @@ static struct timespec time_sub(struct timespec a, struct timespec b) { struct timespec dst; - dst.tv_sec = a.tv_sec + b.tv_sec; - dst.tv_nsec = a.tv_nsec + b.tv_nsec; + dst.tv_sec = a.tv_sec - b.tv_sec; + dst.tv_nsec = a.tv_nsec - b.tv_nsec; if (dst.tv_nsec < 0) { dst.tv_nsec += 1000000 * 1000; @@ -426,39 +426,38 @@ main(void) { unsigned i; /* Stall updating for at most 1 minute */ - struct timespec next_update = {0}; - now = time_add(one_minute, next_update); + struct timespec next_update = time_add(now, one_minute); for (i = 0; i < num_elems; i++) { - struct timespec next_fire = {0}; - struct element* e = &statusbar[i]; - - e->fire_previous = time_add(e->fire_interval, next_fire); + struct element* e = &statusbar[i]; + // Next time this element updates + struct timespec next_fire = time_add(e->fire_previous, e->fire_interval); + /* test if this is not to be updated yet */ if (time_lt(now, next_fire)) { - /* Check if this is the next to-be-updated element */ + /* test if this element is to be updated next */ if (time_lt(next_fire, next_update)) { next_update = next_fire; } continue; } + /* Otherwise update this element */ memset(e->buf, 0, ELEMENT_STRBUF_SZ); e->f(statusbar[i].buf); e->fire_previous = now; /* Check if this element needs to be refreshed next, again */ - e->fire_previous = time_add(e->fire_interval, next_fire); - + next_fire = time_add(now, e->fire_interval); if (time_lt(next_fire, next_update)) { next_update = next_fire; } /* printf("[%ld.%ld] %s\n", - * statusbar[i].fire_interval.tv_sec, - * statusbar[i].fire_interval.tv_usec, - * (char*)statusbar[i].buf); + * e->fire_interval.tv_sec, + * e->fire_interval.tv_nsec, + * (char*)e->buf); */ } @@ -480,11 +479,12 @@ main(void) { puts(buf); fflush(stdout); - next_update = time_sub(now, next_update); + struct timespec sleep_for = time_sub(next_update, now); + sleep_for = time_add(sleep_for, (struct timespec){0, 500}); /* Replace NULL to get "remaining time", in case we got * interrupted / signaled */ - nanosleep(&next_update, NULL); + nanosleep(&sleep_for, NULL); } return 0; |
