Skip to content

Commit

Permalink
request_callback impl on windows standalone (#293)
Browse files Browse the repository at this point in the history
* request_callback impl on windows standalone

* replaced atomic load and set with exchange, handle case where timer start fails

---------

Co-authored-by: MeijisIrlnd <[email protected]>
  • Loading branch information
MeijisIrlnd and MeijisIrlnd authored Aug 17, 2024
1 parent 3445292 commit d066aab
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/detail/standalone/standalone_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ struct StandaloneHost : Clap::IHost
{
TRACE;
}
std::atomic<bool> callbackRequested{false};
void request_callback() override
{
TRACE;
callbackRequested = true;
}
void setupWrapperSpecifics(const clap_plugin_t *plugin) override
{
Expand Down
11 changes: 11 additions & 0 deletions src/detail/standalone/windows/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ double getScale(::HWND window)
return static_cast<double>(::GetDpiForWindow(window)) / static_cast<double>(USER_DEFAULT_SCREEN_DPI);
}

bool startTimer(::HWND window, UINT_PTR timerId, UINT intervalMs)
{
const auto res = ::SetTimer(window, timerId, intervalMs, (::TIMERPROC)NULL);
return res != 0;
}

bool stopTimer(::HWND window, UINT_PTR timerId)
{
return ::KillTimer(window, timerId);
}

void abort(unsigned int exitCode)
{
::ExitProcess(exitCode);
Expand Down
2 changes: 2 additions & 0 deletions src/detail/standalone/windows/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ bool closeWindow(::HWND window);
bool checkWindowVisibility(::HWND window);
unsigned int getCurrentDpi(::HWND window);
double getScale(::HWND window);
bool startTimer(::HWND window, UINT_PTR timerId, UINT intervalMs);
bool stopTimer(::HWND window, UINT_PTR timerId);

void abort(unsigned int exitCode = EXIT_FAILURE);
void quit(unsigned int exitCode = EXIT_SUCCESS);
Expand Down
31 changes: 31 additions & 0 deletions src/detail/standalone/windows/host_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

namespace freeaudio::clap_wrapper::standalone::windows
{
// We could also just have these at the call-site, but this kinda makes them easier to find.
static constexpr auto s_timerId{0};
static constexpr auto s_timerIntervalMs{8};

HostWindow::HostWindow(std::shared_ptr<Clap::Plugin> clapPlugin)
: m_clapPlugin{clapPlugin}
, m_plugin{m_clapPlugin->_plugin}
Expand Down Expand Up @@ -73,6 +77,12 @@ void HostWindow::setupStandaloneHost()
{
freeaudio::clap_wrapper::standalone::getStandaloneHost()->onRequestResize =
[this](uint32_t width, uint32_t height) { return setWindowSize(width, height); };
// Launch our timer
m_isTimerRunning = helpers::startTimer(m_hWnd.get(), s_timerId, s_timerIntervalMs);
if (!m_isTimerRunning)
{
TRACE; // maybe
}
}

void HostWindow::setupPlugin()
Expand Down Expand Up @@ -137,6 +147,8 @@ LRESULT CALLBACK HostWindow::wndProc(::HWND hWnd, ::UINT uMsg, ::WPARAM wParam,
return self->onSysCommand(hWnd, uMsg, wParam, lParam);
case WM_DESTROY:
return self->onDestroy();
case WM_TIMER:
return self->onTimerEvent(wParam);
}
}

Expand Down Expand Up @@ -300,11 +312,30 @@ int HostWindow::onSysCommand(::HWND hWnd, ::UINT uMsg, ::WPARAM wParam, ::LPARAM
int HostWindow::onDestroy()
{
m_pluginGui->destroy(m_plugin);
if (m_isTimerRunning)
{
if (!helpers::stopTimer(m_hWnd.get(), s_timerId))
{
// We can grab the error with ::GetLastError, and format it with ::FormatMessage - but not sure if that's overkill
TRACE;
}
}

freeaudio::clap_wrapper::standalone::mainFinish();

helpers::quit();

return 0;
}

int HostWindow::onTimerEvent(::WPARAM wParam)
{
if (wParam != s_timerId) return -1; // or assert fail, etc
auto* standaloneHost = freeaudio::clap_wrapper::standalone::getStandaloneHost();
if (standaloneHost->callbackRequested.exchange(false))
{
m_plugin->on_main_thread(m_plugin);
}
return 0;
}
} // namespace freeaudio::clap_wrapper::standalone::windows
3 changes: 2 additions & 1 deletion src/detail/standalone/windows/host_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ struct HostWindow
int onWindowPosChanged(::LPARAM lParam);
int onSysCommand(::HWND hWnd, ::UINT uMsg, ::WPARAM wParam, ::LPARAM lParam);
int onDestroy();

int onTimerEvent(::WPARAM wParam);
std::shared_ptr<Clap::Plugin> m_clapPlugin;
const clap_plugin_t* m_plugin;
const clap_plugin_gui_t* m_pluginGui;
const clap_plugin_state_t* m_pluginState;
freeaudio::clap_wrapper::standalone::StandaloneHost* m_standaloneHost;
std::vector<COMDLG_FILTERSPEC> m_fileTypes{{L"clapwrapper", L"*.clapwrapper"}};
bool m_isTimerRunning{false};

// SettingsWindow m_settingsWindow;
wil::unique_hwnd m_hWnd;
Expand Down

0 comments on commit d066aab

Please sign in to comment.