+ dbg("Start time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
+
+ clock_gettime(CLOCK_MONOTONIC, &stats->time_end);
+ dbg("End time: s: %ld, ns: %ld", stats->time_end.tv_sec, stats->time_end.tv_nsec);
+
+ timespecsub(&stats->time_end, &stats->time_start, &elapsed);
+ dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec);
+
+ if (elapsed.tv_sec == 0 && elapsed.tv_nsec < NSEC_PER_SEC / stats->fps) {
+ struct timespec remaining = {0, 0};
+
+ /* remaining delay, _relative_ to time_end */
+ remaining.tv_nsec = (NSEC_PER_SEC / stats->fps) - elapsed.tv_nsec;
+ dbg("Sleeping for: ns: %ld", remaining.tv_nsec);
+
+ clock_nanosleep(CLOCK_MONOTONIC, 0, &remaining, NULL);
+ }
+
+ /* update the stats for the next iteration */
+ clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
+}
+
+int main(void)
+{
+ struct fps_limit_stats stats;