From 772d70ae4c8d7f7cb6c9a32dda1c1d026cb3e350 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Thu, 19 Nov 2020 19:20:34 -0700 Subject: [PATCH] use glib event callback to invoke per-thread code every time the GUI event loop does stuff --- gtk2_ardour/ardour_ui.cc | 14 ++++++++++++++ gtk2_ardour/ardour_ui.h | 2 ++ gtk2_ardour/main.cc | 11 ++++++++++- libs/gtkmm2ext/gtk_ui.cc | 12 ++++++++++++ libs/gtkmm2ext/gtkmm2ext/gtk_ui.h | 4 ++++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/gtk2_ardour/ardour_ui.cc b/gtk2_ardour/ardour_ui.cc index 95743f1ca8..93c3c4b345 100644 --- a/gtk2_ardour/ardour_ui.cc +++ b/gtk2_ardour/ardour_ui.cc @@ -85,6 +85,8 @@ #include "pbd/scoped_file_descriptor.h" #include "pbd/xml++.h" +#include "temporal/superclock.h" + #include "gtkmm2ext/application.h" #include "gtkmm2ext/bindings.h" #include "gtkmm2ext/gtk_ui.h" @@ -3111,3 +3113,15 @@ ARDOUR_UI::setup_toplevel_window (Gtk::Window& window, const string& name, void* window.signal_key_press_event().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::key_event_handler), &window), false); window.signal_key_release_event().connect (sigc::bind (sigc::mem_fun (*this, &ARDOUR_UI::key_event_handler), &window), false); } + +void +ARDOUR_UI::event_loop_precall () +{ + std::cout << "ev precall\n"; + + if (_session) { + Temporal::_thread_sample_rate = _session->sample_rate (); + } else { + Temporal::_thread_sample_rate = 44100; + } +} diff --git a/gtk2_ardour/ardour_ui.h b/gtk2_ardour/ardour_ui.h index d2f30c4233..cccf2ddeb3 100644 --- a/gtk2_ardour/ardour_ui.h +++ b/gtk2_ardour/ardour_ui.h @@ -408,6 +408,8 @@ protected: void toggle_rc_options_window (); void toggle_session_options_window (); + void event_loop_precall (); + private: Gtk::Window _main_window; diff --git a/gtk2_ardour/main.cc b/gtk2_ardour/main.cc index 22153530ef..6353d742dc 100644 --- a/gtk2_ardour/main.cc +++ b/gtk2_ardour/main.cc @@ -48,6 +48,8 @@ #include "pbd/boost_debug.h" #endif +#include "temporal/superclock.h" + #include "ardour/revision.h" #include "ardour/ardour.h" #include "ardour/audioengine.h" @@ -383,7 +385,14 @@ int main (int argc, char *argv[]) SetErrorMode (prev_error_mode); #endif - if (!ARDOUR::init (ARDOUR_COMMAND_LINE::try_hw_optimization, localedir.c_str(), true)) { + /* this is the GUI thread. Normally this will be done by + * Gtkmm2ext::UI::event_loop_precall() but we need to make sure that + * things are set up for this thread before we get started + */ + + Temporal::_thread_sample_rate = 44100; + + if (!ARDOUR::init (ARDOUR_COMMAND_LINE::use_vst, ARDOUR_COMMAND_LINE::try_hw_optimization, localedir.c_str(), true)) { error << string_compose (_("could not initialize %1."), PROGRAM_NAME) << endmsg; Gtk::Main main (argc, argv); Gtk::MessageDialog msg (string_compose (_("Could not initialize %1 (likely due to corrupt config files).\n" diff --git a/libs/gtkmm2ext/gtk_ui.cc b/libs/gtkmm2ext/gtk_ui.cc index d3335e3476..b7fa23d1d3 100644 --- a/libs/gtkmm2ext/gtk_ui.cc +++ b/libs/gtkmm2ext/gtk_ui.cc @@ -73,11 +73,17 @@ BaseUI::RequestType Gtkmm2ext::AddTimeout = BaseUI::new_request_type(); template class AbstractUI; +void +UI::event_loop_precall () +{ +} + UI::UI (string application_name, string thread_name, int *argc, char ***argv) : AbstractUI (thread_name) , _receiver (*this) , global_bindings (0) , errors (0) + , event_callback (boost::bind (&UI::event_loop_precall, this)) { theMain = new Main (argc, argv); @@ -111,6 +117,12 @@ UI::UI (string application_name, string thread_name, int *argc, char ***argv) EventLoop::register_request_buffer_factory ("gui", request_buffer_factory); + /* + * every time the main loop runs (i.e. before any actual event handling + */ + + event_callback.attach (MainContext::get_default()); + /* attach our request source to the default main context */ attach_request_source (); diff --git a/libs/gtkmm2ext/gtkmm2ext/gtk_ui.h b/libs/gtkmm2ext/gtkmm2ext/gtk_ui.h index a9938026f0..cc38766d54 100644 --- a/libs/gtkmm2ext/gtkmm2ext/gtk_ui.h +++ b/libs/gtkmm2ext/gtkmm2ext/gtk_ui.h @@ -50,6 +50,7 @@ #define ABSTRACT_UI_EXPORTS #endif #include "pbd/abstract_ui.h" +#include "pbd/glib_event_source.h" #include "pbd/ringbufferNPT.h" #include "pbd/pool.h" #include "pbd/error.h" @@ -170,6 +171,8 @@ protected: Glib::RefPtr ptag, Glib::RefPtr mtag, const char *msg); + virtual void event_loop_precall (); + private: static UI *theGtkUI; @@ -203,6 +206,7 @@ private: void do_request (UIRequest*); + GlibEventLoopCallback event_callback; }; } /* namespace */