summaryrefslogtreecommitdiff
path: root/i3status.c
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2010-01-30 00:02:17 +0100
committerMichael Stapelberg <michael@stapelberg.de>2010-01-30 00:02:17 +0100
commit18c4b9866d395720077e815d85755a8ac54354ca (patch)
tree8f768a808eceb6b0e415ca3a48fd902e7b6a36f1 /i3status.c
parent9c14b7a527a34f0ed04a53c72fe85f4d21f094ec (diff)
Instead of a relative sleep(1), sleep until the full second
This makes sure you don’t lag behind up to nearly one second in the worst case, depending on the start time of your i3status.
Diffstat (limited to 'i3status.c')
-rw-r--r--i3status.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/i3status.c b/i3status.c
index f2d055d..59fedf8 100644
--- a/i3status.c
+++ b/i3status.c
@@ -22,6 +22,8 @@
#include <glob.h>
#include <sys/stat.h>
#include <sys/types.h>
+#include <time.h>
+#include <sys/time.h>
#include "i3status.h"
@@ -186,6 +188,8 @@ int main(int argc, char *argv[]) {
if ((general_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
die("Could not create socket\n");
+ int interval = cfg_getint(cfg_general, "interval");
+
while (1) {
for (j = 0; j < cfg_size(cfg, "order"); j++) {
if (j > 0)
@@ -223,6 +227,13 @@ int main(int argc, char *argv[]) {
printf("\n");
fflush(stdout);
- sleep(cfg_getint(cfg_general, "interval"));
+ /* To provide updates on every full second (as good as possible)
+ * we don’t use sleep(interval) but we sleep until the next
+ * second (with microsecond precision) plus (interval-1)
+ * seconds. */
+ struct timeval current_time;
+ gettimeofday(&current_time, NULL);
+ struct timespec ts = {interval - 1, (10e5 - current_time.tv_usec) * 1000};
+ nanosleep(&ts, NULL);
}
}