Add helper function to convert tv_sec_hi, tv_sec_lo, tv_nsec triplets,
used for sending high-resolution timestamp data over the wayland
protocol, to struct timespec values. Replace existing conversion code
with the helper function.

Signed-off-by: Alexandros Frantzis <alexandros.frant...@collabora.com>
---

Changes in v2:
 - New patch, previously part of [PATCH weston 2/8].
 - Cast long long value to time_t to ensure correct check in test.

 clients/presentation-shm.c |  9 +--------
 shared/timespec-util.h     | 15 +++++++++++++++
 tests/presentation-test.c  |  9 +--------
 tests/timespec-test.c      | 17 +++++++++++++++++
 4 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/clients/presentation-shm.c b/clients/presentation-shm.c
index c9fb66cc..d6a939e5 100644
--- a/clients/presentation-shm.c
+++ b/clients/presentation-shm.c
@@ -39,6 +39,7 @@
 #include <wayland-client.h>
 #include "shared/helpers.h"
 #include "shared/zalloc.h"
+#include "shared/timespec-util.h"
 #include "shared/os-compatibility.h"
 #include "presentation-time-client-protocol.h"
 
@@ -383,14 +384,6 @@ timespec_to_ms(const struct timespec *ts)
        return (uint32_t)ts->tv_sec * 1000 + ts->tv_nsec / 1000000;
 }
 
-static void
-timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi,
-                   uint32_t tv_sec_lo, uint32_t tv_nsec)
-{
-       tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
-       tm->tv_nsec = tv_nsec;
-}
-
 static int
 timespec_diff_to_usec(const struct timespec *a, const struct timespec *b)
 {
diff --git a/shared/timespec-util.h b/shared/timespec-util.h
index f9736c27..5184d281 100644
--- a/shared/timespec-util.h
+++ b/shared/timespec-util.h
@@ -181,6 +181,21 @@ timespec_from_msec(struct timespec *a, int64_t b)
        timespec_from_nsec(a, b * 1000000);
 }
 
+/* Convert protocol data to timespec
+ *
+ * \param a[out] timespec
+ * \param tv_sec_hi the high bytes of seconds part
+ * \param tv_sec_lo the low bytes of seconds part
+ * \param tv_nsec the nanoseconds part
+ */
+static inline void
+timespec_from_proto(struct timespec *a, uint32_t tv_sec_hi,
+                    uint32_t tv_sec_lo, uint32_t tv_nsec)
+{
+       a->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
+       a->tv_nsec = tv_nsec;
+}
+
 /* Check if a timespec is zero
  *
  * \param a timespec
diff --git a/tests/presentation-test.c b/tests/presentation-test.c
index f12f8eef..f6ffe480 100644
--- a/tests/presentation-test.c
+++ b/tests/presentation-test.c
@@ -34,6 +34,7 @@
 
 #include "shared/helpers.h"
 #include "shared/xalloc.h"
+#include "shared/timespec-util.h"
 #include "weston-test-client-helper.h"
 #include "presentation-time-client-protocol.h"
 
@@ -85,14 +86,6 @@ struct feedback {
        uint32_t flags;
 };
 
-static void
-timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi,
-                   uint32_t tv_sec_lo, uint32_t tv_nsec)
-{
-       tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
-       tm->tv_nsec = tv_nsec;
-}
-
 static void
 feedback_sync_output(void *data,
                     struct wp_presentation_feedback *presentation_feedback,
diff --git a/tests/timespec-test.c b/tests/timespec-test.c
index f10ed76c..a4d8dcfb 100644
--- a/tests/timespec-test.c
+++ b/tests/timespec-test.c
@@ -238,6 +238,23 @@ ZUC_TEST(timespec_test, timespec_from_msec)
        ZUC_ASSERT_EQ(1000000, a.tv_nsec);
 }
 
+ZUC_TEST(timespec_test, timespec_from_proto)
+{
+       struct timespec a;
+
+       timespec_from_proto(&a, 0, 0, 0);
+       ZUC_ASSERT_EQ(0, a.tv_sec);
+       ZUC_ASSERT_EQ(0, a.tv_nsec);
+
+       timespec_from_proto(&a, 0, 1234, 9999);
+       ZUC_ASSERT_EQ(1234, a.tv_sec);
+       ZUC_ASSERT_EQ(9999, a.tv_nsec);
+
+       timespec_from_proto(&a, 0x1234, 0x5678, 1);
+       ZUC_ASSERT_EQ((time_t)0x0000123400005678LL, a.tv_sec);
+       ZUC_ASSERT_EQ(1, a.tv_nsec);
+}
+
 ZUC_TEST(timespec_test, timespec_is_zero)
 {
        struct timespec zero = { 0 };
-- 
2.14.1

_______________________________________________
wayland-devel mailing list
wayland-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/wayland-devel

Reply via email to