From dc13bb827d6b7dd40d76ff6f7f3f06688e7d58a0 Mon Sep 17 00:00:00 2001 From: Tudor Brindus Date: Fri, 5 Jun 2020 17:44:30 -0400 Subject: [PATCH] util: fix and move `get_current_time_msec` into a util file This commit makes `get_current_time_msec` correctly return milliseconds as opposed to microseconds. It also considers the value of `tv_sec`, so we don't lose occasionally go back in time by one second. Finally, the function is moved into `util/time.cc` so that it can be reused elsewhere without having to consider these pitfalls. --- backend/wayland/seat.c | 7 +------ backend/wayland/tablet_v2.c | 7 +------ include/util/time.h | 9 +++++++++ util/meson.build | 1 + util/time.c | 11 +++++++++++ 5 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 include/util/time.h create mode 100644 util/time.c diff --git a/backend/wayland/seat.c b/backend/wayland/seat.c index b995dbe5..1fcb93e5 100644 --- a/backend/wayland/seat.c +++ b/backend/wayland/seat.c @@ -20,6 +20,7 @@ #include "relative-pointer-unstable-v1-client-protocol.h" #include "backend/wayland.h" #include "util/signal.h" +#include "util/time.h" static struct wlr_wl_pointer *output_get_pointer(struct wlr_wl_output *output) { struct wlr_input_device *wlr_dev; @@ -197,12 +198,6 @@ static void keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard, close(fd); } -static uint32_t get_current_time_msec(void) { - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); - return now.tv_nsec / 1000; -} - static void keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys) { struct wlr_input_device *dev = data; diff --git a/backend/wayland/tablet_v2.c b/backend/wayland/tablet_v2.c index 26e2d32a..826c36ba 100644 --- a/backend/wayland/tablet_v2.c +++ b/backend/wayland/tablet_v2.c @@ -13,6 +13,7 @@ #include #include "util/signal.h" +#include "util/time.h" #include "wlr/util/log.h" #include "tablet-unstable-v2-client-protocol.h" @@ -84,12 +85,6 @@ struct wlr_wl_tablet_pad_group { struct wl_list strips; // wlr_wl_tablet_pad_strips::link }; -static uint32_t get_current_time_msec(void) { - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); - return now.tv_nsec / (1000 * 1000) + now.tv_sec * 1000; -} - static void handle_tablet_pad_ring_source(void *data, struct zwp_tablet_pad_ring_v2 *zwp_tablet_pad_ring_v2, uint32_t source) { diff --git a/include/util/time.h b/include/util/time.h new file mode 100644 index 00000000..a24656a9 --- /dev/null +++ b/include/util/time.h @@ -0,0 +1,9 @@ +#ifndef UTIL_TIME_H +#define UTIL_TIME_H + +/** + * Get the current time, in milliseconds. + */ +uint32_t get_current_time_msec(void); + +#endif diff --git a/util/meson.build b/util/meson.build index 8bdd7a74..14cd59c5 100644 --- a/util/meson.build +++ b/util/meson.build @@ -5,4 +5,5 @@ wlr_files += files( 'region.c', 'shm.c', 'signal.c', + 'time.c', ) diff --git a/util/time.c b/util/time.c new file mode 100644 index 00000000..b2333921 --- /dev/null +++ b/util/time.c @@ -0,0 +1,11 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include + +#include "util/time.h" + +uint32_t get_current_time_msec(void) { + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + return now.tv_sec * 1000 + now.tv_nsec / 1000000; +}