Add timespec helpers

Borrow timespec helpers from Weston [1]. We'll re-use these helpers for
adaptive sync in a future commit.

Rename refresh_nsec to next_refresh_nsec, because this value only
applies to the next output presentation.

[1]: 467e6b9883/shared/timespec-util.h
This commit is contained in:
Simon Ser 2020-03-06 18:09:35 +01:00
parent c0811fcf87
commit a5fbffb40d
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
4 changed files with 56 additions and 21 deletions

View file

@ -7,6 +7,7 @@
#include <string.h>
#include <strings.h>
#include <wayland-server-protocol.h>
#include <time.h>
#include "log.h"
#include "util.h"
@ -102,3 +103,40 @@ bool sway_set_cloexec(int fd, bool cloexec) {
}
return true;
}
static const int64_t NSEC_PER_SEC = 1000000000;
void timespec_sub(struct timespec *r, const struct timespec *a,
const struct timespec *b) {
r->tv_sec = a->tv_sec - b->tv_sec;
r->tv_nsec = a->tv_nsec - b->tv_nsec;
if (r->tv_nsec < 0) {
r->tv_sec--;
r->tv_nsec += NSEC_PER_SEC;
}
}
int64_t timespec_sub_to_nsec(const struct timespec *a,
const struct timespec *b) {
struct timespec r;
timespec_sub(&r, a, b);
return timespec_to_nsec(&r);
}
void timespec_add_nsec(struct timespec *r, const struct timespec *a,
int64_t nsec) {
r->tv_sec = a->tv_sec + (nsec / NSEC_PER_SEC);
r->tv_nsec = a->tv_nsec + (nsec % NSEC_PER_SEC);
if (r->tv_nsec >= NSEC_PER_SEC) {
r->tv_sec++;
r->tv_nsec -= NSEC_PER_SEC;
} else if (r->tv_nsec < 0) {
r->tv_sec--;
r->tv_nsec += NSEC_PER_SEC;
}
}
int64_t timespec_to_nsec(const struct timespec *t) {
return (int64_t)t->tv_sec * NSEC_PER_SEC + t->tv_nsec;
}