From 2530370ff29009fccad472c9e9194ebdd561a398 Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Mon, 26 Nov 2018 23:45:04 -0800
Subject: Avoid relying on struct timespec

---
 examples/common/alhelpers.c | 61 ++++++++++++++++++++++-----------------------
 examples/common/alhelpers.h | 11 +-------
 2 files changed, 31 insertions(+), 41 deletions(-)

(limited to 'examples/common')

diff --git a/examples/common/alhelpers.c b/examples/common/alhelpers.c
index 6a583497..fec51e6b 100644
--- a/examples/common/alhelpers.c
+++ b/examples/common/alhelpers.c
@@ -28,6 +28,7 @@
  * finding an appropriate buffer format, and getting readable strings for
  * channel configs and sample types. */
 
+#include <time.h>
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
@@ -123,22 +124,21 @@ const char *FormatName(ALenum format)
 #include <windows.h>
 #include <mmsystem.h>
 
-int altimespec_get(struct timespec *ts, int base)
+unsigned int altime_get(void)
 {
-    if(base == AL_TIME_UTC)
-    {
-        union {
-            FILETIME ftime;
-            ULARGE_INTEGER ulint;
-        } systime;
-        GetSystemTimeAsFileTime(&systime.ftime);
-        /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */
-        ts->tv_sec = systime.ulint.QuadPart/10000000;
-        ts->tv_nsec = (systime.ulint.QuadPart%10000000) * 100;
-        return base;
-    }
-
-    return 0;
+    static unsigned int start_time = 0;
+    unsigned int cur_time;
+    union {
+        FILETIME ftime;
+        ULARGE_INTEGER ulint;
+    } systime;
+    GetSystemTimeAsFileTime(&systime.ftime);
+    /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */
+    cur_time = (unsigned int)(systime.ulint.QuadPart/10000);
+
+    if(!start_time)
+        start_time = cur_time;
+    return cur_time - start_time;
 }
 
 void al_nssleep(unsigned long nsec)
@@ -151,27 +151,26 @@ void al_nssleep(unsigned long nsec)
 #include <sys/time.h>
 #include <time.h>
 
-int altimespec_get(struct timespec *ts, int base)
+unsigned int altime_get(void)
 {
-    if(base == AL_TIME_UTC)
-    {
-        int ret;
+    static unsigned int start_time = 0u;
+    unsigned int cur_time;
+
 #if _POSIX_TIMERS > 0
-        ret = clock_gettime(CLOCK_REALTIME, ts);
-        if(ret == 0) return base;
+    struct timespec ts;
+    int ret = clock_gettime(CLOCK_REALTIME, ts);
+    if(ret != 0) return 0;
+    cur_time = ts.ts_sec*1000 + ts.ts_nsec/1000000;
 #else /* _POSIX_TIMERS > 0 */
-        struct timeval tv;
-        ret = gettimeofday(&tv, NULL);
-        if(ret == 0)
-        {
-            ts->tv_sec = tv.tv_sec;
-            ts->tv_nsec = tv.tv_usec * 1000;
-            return base;
-        }
+    struct timeval tv;
+    int ret = gettimeofday(&tv, NULL);
+    if(ret != 0) return 0;
+    cur_time = tv.tv_sec*1000 + tv.tv_usec/1000;
 #endif
-    }
 
-    return 0;
+    if(!start_time)
+        start_time = cur_time;
+    return cur_time - start_time;
 }
 
 void al_nssleep(unsigned long nsec)
diff --git a/examples/common/alhelpers.h b/examples/common/alhelpers.h
index 14edf5d9..4193e86f 100644
--- a/examples/common/alhelpers.h
+++ b/examples/common/alhelpers.h
@@ -1,8 +1,6 @@
 #ifndef ALHELPERS_H
 #define ALHELPERS_H
 
-#include <time.h>
-
 #include "AL/alc.h"
 #include "AL/al.h"
 #include "AL/alext.h"
@@ -19,14 +17,7 @@ int InitAL(char ***argv, int *argc);
 void CloseAL(void);
 
 /* Cross-platform timeget and sleep functions. */
-#ifndef HAVE_STRUCT_TIMESPEC
-struct timespec {
-    time_t tv_sec;
-    long tv_nsec;
-};
-#endif
-#define AL_TIME_UTC 1
-int altimespec_get(struct timespec *ts, int base);
+unsigned int altime_get(void);
 void al_nssleep(unsigned long nsec);
 
 #ifdef __cplusplus
-- 
cgit v1.2.3