From 7b5285b51e9e97c746f7222cd9472894b53c23a8 Mon Sep 17 00:00:00 2001
From: patrick96 <p.ziegler96@gmail.com>
Date: Sun, 12 Sep 2021 15:53:14 +0200
Subject: [PATCH] Add wrapper for uv_timer_t

---
 include/components/eventloop.hpp | 10 ++++++++++
 src/components/controller.cpp    | 14 ++++----------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/include/components/eventloop.hpp b/include/components/eventloop.hpp
index 89694e84..3aeae15a 100644
--- a/include/components/eventloop.hpp
+++ b/include/components/eventloop.hpp
@@ -128,6 +128,16 @@ struct PipeHandle : public UVHandleGeneric<uv_pipe_t, uv_stream_t, ssize_t, cons
   }
 };
 
+struct TimerHandle : public UVHandle<uv_timer_t> {
+  TimerHandle(uv_loop_t* loop, std::function<void(void)> fun) : UVHandle(fun) {
+    UV(uv_timer_init, loop, handle.get());
+  }
+
+  void start(uint64_t timeout, uint64_t repeat) {
+    UV(uv_timer_start, handle.get(), &cb.callback, timeout, repeat);
+  }
+};
+
 class eventloop {
  public:
   eventloop();
diff --git a/src/components/controller.cpp b/src/components/controller.cpp
index 18d52fc9..94f10166 100644
--- a/src/components/controller.cpp
+++ b/src/components/controller.cpp
@@ -246,10 +246,6 @@ void controller::screenshot_handler() {
   trigger_update(true);
 }
 
-static void screenshot_cb_wrapper(uv_timer_t* handle) {
-  static_cast<controller*>(handle->data)->screenshot_handler();
-}
-
 /**
  * Read events from configured file descriptors
  */
@@ -261,7 +257,7 @@ void controller::read_events(bool confwatch) {
   }
 
   auto ipc_handle = std::unique_ptr<PipeHandle>(nullptr);
-  auto screenshot_timer_handle = std::unique_ptr<uv_timer_t>(nullptr);
+  auto screenshot_timer_handle = std::unique_ptr<TimerHandle>(nullptr);
 
   try {
     eloop = std::make_unique<eventloop>();
@@ -289,11 +285,9 @@ void controller::read_events(bool confwatch) {
     m_notifier->data = this;
 
     if (!m_snapshot_dst.empty()) {
-      screenshot_timer_handle = std::make_unique<uv_timer_t>();
-      UV(uv_timer_init, loop, screenshot_timer_handle.get());
-      screenshot_timer_handle->data = this;
-      // Trigger a screenshot after 3 seconds
-      UV(uv_timer_start, screenshot_timer_handle.get(), screenshot_cb_wrapper, 3000, 0);
+      screenshot_timer_handle = std::make_unique<TimerHandle>(loop, [this]() { screenshot_handler(); });
+      // Trigger a single screenshot after 3 seconds
+      screenshot_timer_handle->start(3000, 0);
     }
 
     m_eloop_ready.store(true);