Add a RAII wrapper around SetTimer()

This commit is contained in:
Robbert van der Helm
2020-05-28 15:27:35 +02:00
parent 10cc871798
commit 00bcdf8fca
4 changed files with 60 additions and 8 deletions
+1 -8
View File
@@ -79,19 +79,12 @@ Editor::Editor(const std::string& window_class_name,
GetModuleHandle(nullptr),
this),
DestroyWindow),
idle_timer(win32_handle.get(), idle_timer_id, 100),
parent_window(parent_window_handle),
child_window(get_x11_handle(win32_handle.get())),
topmost_window(find_topmost_window(*x11_connection, parent_window)),
// Needed to send update messages on a timer
plugin(effect) {
// The Win32 API will block the `DispatchMessage` call when opening e.g. a
// dropdown, but it will still allow timers to be run so the GUI can still
// update in the background. Because of this we send `effEditIdle` to the
// plugin on a timer. The refresh rate is purposely fairly low since the
// host will call `effEditIdle()` explicitely when the plugin is not busy.
// TODO: Add a `KillTimer()` now that we are hosting multiple plugins
SetTimer(win32_handle.get(), idle_timer_id, 100, nullptr);
// Because we're not using XEmbed Wine will interpret any local coordinates
// as global coordinates. To work around this we'll tell the Wine window
// it's located at its actual coordinates on screen rather than somewhere
+11
View File
@@ -32,6 +32,8 @@
#include <optional>
#include <string>
#include "utils.h"
/**
* Used to store the maximum width and height of a screen.
*/
@@ -151,6 +153,15 @@ class Editor {
win32_handle;
private:
/**
* The Win32 API will block the `DispatchMessage` call when opening e.g. a
* dropdown, but it will still allow timers to be run so the GUI can still
* update in the background. Because of this we send `effEditIdle` to the
* plugin on a timer. The refresh rate is purposely fairly low since the
* host will call `effEditIdle()` explicitely when the plugin is not busy.
*/
Win32Timer idle_timer;
/**
* The window handle of the editor window created by the DAW.
*/
+25
View File
@@ -17,3 +17,28 @@
#include "utils.h"
Win32Thread::Win32Thread() : handle(nullptr, nullptr) {}
Win32Timer::Win32Timer(HWND window_handle,
size_t timer_id,
unsigned int interval_ms)
: window_handle(window_handle), timer_id(timer_id) {
SetTimer(window_handle, timer_id, interval_ms, nullptr);
}
Win32Timer::~Win32Timer() {
if (timer_id.has_value()) {
KillTimer(window_handle, timer_id.value());
}
}
Win32Timer::Win32Timer(Win32Timer&& o) {
timer_id = o.timer_id;
o.timer_id = std::nullopt;
}
Win32Timer& Win32Timer::operator=(Win32Timer&& o) {
timer_id = o.timer_id;
o.timer_id = std::nullopt;
return *this;
}
+23
View File
@@ -14,7 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <memory>
#include <optional>
#define NOMINMAX
#define NOSERVICE
@@ -71,3 +74,23 @@ class Win32Thread {
std::unique_ptr<std::remove_pointer_t<HANDLE>, decltype(&CloseHandle)>
handle;
};
/**
* A simple RAII wrapper around `SetTimer`. Does not support timer procs since
* we don't use them.
*/
class Win32Timer {
public:
Win32Timer(HWND window_handle, size_t timer_id, unsigned int interval_ms);
~Win32Timer();
Win32Timer(const Win32Timer&) = delete;
Win32Timer& operator=(const Win32Timer&) = delete;
Win32Timer(Win32Timer&&);
Win32Timer& operator=(Win32Timer&&);
private:
HWND window_handle;
std::optional<size_t> timer_id;
};