From fb54982c5ade5bee05b8d4a817d258ba89df701b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 29 Nov 2020 00:35:02 +0100 Subject: Replace attached and detached signals with events --- src/AlsaDriver.cpp | 8 ++--- src/Driver.hpp | 5 ---- src/JackDbusDriver.cpp | 19 ++++++------ src/JackDriver.cpp | 12 ++++---- src/Patchage.cpp | 79 +++++++++++++++++++++---------------------------- src/Patchage.hpp | 11 +++---- src/PatchageEvent.hpp | 15 +++++++++- src/PatchageModule.cpp | 5 ++-- src/event_to_string.cpp | 21 +++++++++++++ src/handle_event.cpp | 10 +++++++ 10 files changed, 105 insertions(+), 80 deletions(-) diff --git a/src/AlsaDriver.cpp b/src/AlsaDriver.cpp index 1936d30..79c1491 100644 --- a/src/AlsaDriver.cpp +++ b/src/AlsaDriver.cpp @@ -18,6 +18,7 @@ #include "ClientID.hpp" #include "ClientInfo.hpp" +#include "ClientType.hpp" #include "ILog.hpp" #include "PortInfo.hpp" #include "PortType.hpp" @@ -103,7 +104,7 @@ AlsaDriver::attach(bool /*launch_daemon*/) _log.error("[ALSA] Unable to attach"); _seq = nullptr; } else { - _log.info("[ALSA] Attached"); + _emit_event(DriverAttachmentEvent{ClientType::alsa}); snd_seq_set_client_name(_seq, "Patchage"); @@ -116,8 +117,6 @@ AlsaDriver::attach(bool /*launch_daemon*/) if (ret) { _log.error("[ALSA] Failed to start refresh thread"); } - - signal_attached.emit(); } } @@ -129,8 +128,7 @@ AlsaDriver::detach() pthread_join(_refresh_thread, nullptr); snd_seq_close(_seq); _seq = nullptr; - signal_detached.emit(); - _log.info("[ALSA] Detached"); + _emit_event(DriverDetachmentEvent{ClientType::alsa}); } } diff --git a/src/Driver.hpp b/src/Driver.hpp index fcd0b80..e7c4510 100644 --- a/src/Driver.hpp +++ b/src/Driver.hpp @@ -19,8 +19,6 @@ #include "PatchageEvent.hpp" -#include - #include #include @@ -54,9 +52,6 @@ public: virtual bool disconnect(const PortID& tail_id, const PortID& head_id) = 0; - sigc::signal signal_attached; - sigc::signal signal_detached; - protected: EventSink _emit_event; }; diff --git a/src/JackDbusDriver.cpp b/src/JackDbusDriver.cpp index e998c66..d3caee3 100644 --- a/src/JackDbusDriver.cpp +++ b/src/JackDbusDriver.cpp @@ -17,6 +17,7 @@ #include "JackDbusDriver.hpp" +#include "ClientType.hpp" #include "Driver.hpp" #include "ILog.hpp" #include "PatchageEvent.hpp" @@ -85,18 +86,18 @@ JackDriver::update_attached() if (!_server_responding) { if (was_attached) { - signal_detached.emit(); + _emit_event(DriverDetachmentEvent{ClientType::jack}); } return; } if (_server_started && !was_attached) { - signal_attached.emit(); + _emit_event(DriverAttachmentEvent{ClientType::jack}); return; } if (!_server_started && was_attached) { - signal_detached.emit(); + _emit_event(DriverDetachmentEvent{ClientType::jack}); return; } } @@ -119,7 +120,7 @@ JackDriver::on_jack_disappeared() _server_responding = false; if (_server_started) { - signal_detached.emit(); + _emit_event(DriverDetachmentEvent{ClientType::jack}); } _server_started = false; @@ -206,7 +207,7 @@ JackDriver::dbus_message_hook(DBusConnection* /*connection*/, if (!me->_server_started) { me->_server_started = true; - me->signal_attached.emit(); + me->_emit_event(DriverAttachmentEvent{ClientType::jack}); } me->_emit_event( @@ -241,7 +242,7 @@ JackDriver::dbus_message_hook(DBusConnection* /*connection*/, if (!me->_server_started) { me->_server_started = true; - me->signal_attached.emit(); + me->_emit_event(DriverAttachmentEvent{ClientType::jack}); } me->_emit_event( @@ -285,7 +286,7 @@ JackDriver::dbus_message_hook(DBusConnection* /*connection*/, if (!me->_server_started) { me->_server_started = true; - me->signal_attached.emit(); + me->_emit_event(DriverAttachmentEvent{ClientType::jack}); } me->_emit_event( @@ -330,7 +331,7 @@ JackDriver::dbus_message_hook(DBusConnection* /*connection*/, if (!me->_server_started) { me->_server_started = true; - me->signal_attached.emit(); + me->_emit_event(DriverAttachmentEvent{ClientType::jack}); } me->_emit_event( @@ -455,7 +456,7 @@ JackDriver::stop_server() } dbus_message_unref(reply_ptr); - signal_detached.emit(); + _emit_event(DriverDetachmentEvent{ClientType::jack}); } void diff --git a/src/JackDriver.cpp b/src/JackDriver.cpp index e0b4080..e19460d 100644 --- a/src/JackDriver.cpp +++ b/src/JackDriver.cpp @@ -17,6 +17,7 @@ #include "JackDriver.hpp" #include "ClientID.hpp" +#include "ClientType.hpp" #include "ILog.hpp" #include "PatchageEvent.hpp" #include "PortNames.hpp" @@ -93,8 +94,7 @@ JackDriver::attach(const bool launch_daemon) _is_activated = true; _buffer_size = jack_get_buffer_size(_client); - signal_attached.emit(); - _log.info("[JACK] Attached"); + _emit_event(DriverAttachmentEvent{ClientType::jack}); } void @@ -109,8 +109,7 @@ JackDriver::detach() } _is_activated = false; - signal_detached.emit(); - _log.info("[JACK] Detached"); + _emit_event(DriverDetachmentEvent{ClientType::jack}); } static std::string @@ -191,7 +190,7 @@ JackDriver::get_port_info(const jack_port_t* const port) void JackDriver::shutdown() { - signal_detached.emit(); + _emit_event(DriverDetachmentEvent{ClientType::jack}); } void @@ -380,9 +379,8 @@ JackDriver::jack_shutdown_cb(void* const jack_driver) me->_client = nullptr; me->_is_activated = false; - me->signal_detached.emit(); - me->_log.info("[JACK] Shutdown"); + me->_emit_event(DriverDetachmentEvent{ClientType::jack}); } jack_nframes_t diff --git a/src/Patchage.cpp b/src/Patchage.cpp index 2307045..3d2853c 100644 --- a/src/Patchage.cpp +++ b/src/Patchage.cpp @@ -324,9 +324,6 @@ Patchage::Patchage(int argc, char** argv) _connector.add_driver(PortID::Type::jack, _jack_driver.get()); - _jack_driver->signal_detached.connect( - sigc::mem_fun(this, &Patchage::driver_detached)); - _menu_jack_connect->signal_activate().connect(sigc::bind( sigc::mem_fun(_jack_driver.get(), &JackDriver::attach), true)); _menu_jack_disconnect->signal_activate().connect( @@ -340,7 +337,6 @@ Patchage::Patchage(int argc, char** argv) _connector.add_driver(PortID::Type::alsa, _alsa_driver.get()); #endif - connect_widgets(); update_state(); _menu_view_toolbar->set_active(_conf.get_show_toolbar()); _menu_view_sprung_layout->set_active(_conf.get_sprung_layout()); @@ -406,6 +402,7 @@ Patchage::attach() _enable_refresh = true; + process_events(); refresh(); update_toolbar(); } @@ -539,6 +536,38 @@ Patchage::refresh() } } +void +Patchage::driver_attached(const ClientType type) +{ + switch (type) { + case ClientType::jack: + _menu_jack_connect->set_sensitive(false); + _menu_jack_disconnect->set_sensitive(true); + refresh(); + break; + case ClientType::alsa: + _menu_alsa_connect->set_sensitive(false); + _menu_alsa_disconnect->set_sensitive(true); + refresh(); + break; + } +} + +void +Patchage::driver_detached(const ClientType type) +{ + switch (type) { + case ClientType::jack: + _menu_jack_connect->set_sensitive(true); + _menu_jack_disconnect->set_sensitive(false); + break; + case ClientType::alsa: + _menu_alsa_connect->set_sensitive(true); + _menu_alsa_disconnect->set_sensitive(false); + break; + } +} + void Patchage::store_window_location() { @@ -602,48 +631,6 @@ Patchage::process_events() } } -/** Update the sensitivity status of menus to reflect the present. - * - * (eg. disable "Connect to Jack" when Patchage is already connected to Jack) - */ -void -Patchage::connect_widgets() -{ -#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) - _jack_driver->signal_attached.connect(sigc::bind( - sigc::mem_fun(*_menu_jack_connect, &Gtk::MenuItem::set_sensitive), - false)); - _jack_driver->signal_attached.connect( - sigc::mem_fun(this, &Patchage::refresh)); - _jack_driver->signal_attached.connect(sigc::bind( - sigc::mem_fun(*_menu_jack_disconnect, &Gtk::MenuItem::set_sensitive), - true)); - - _jack_driver->signal_detached.connect(sigc::bind( - sigc::mem_fun(*_menu_jack_connect, &Gtk::MenuItem::set_sensitive), - true)); - _jack_driver->signal_detached.connect(sigc::bind( - sigc::mem_fun(*_menu_jack_disconnect, &Gtk::MenuItem::set_sensitive), - false)); -#endif - -#ifdef HAVE_ALSA - _alsa_driver->signal_attached.connect(sigc::bind( - sigc::mem_fun(*_menu_alsa_connect, &Gtk::MenuItem::set_sensitive), - false)); - _alsa_driver->signal_attached.connect(sigc::bind( - sigc::mem_fun(*_menu_alsa_disconnect, &Gtk::MenuItem::set_sensitive), - true)); - - _alsa_driver->signal_detached.connect(sigc::bind( - sigc::mem_fun(*_menu_alsa_connect, &Gtk::MenuItem::set_sensitive), - true)); - _alsa_driver->signal_detached.connect(sigc::bind( - sigc::mem_fun(*_menu_alsa_disconnect, &Gtk::MenuItem::set_sensitive), - false)); -#endif -} - #ifdef HAVE_ALSA void Patchage::menu_alsa_connect() diff --git a/src/Patchage.hpp b/src/Patchage.hpp index ba34dcc..c55953c 100644 --- a/src/Patchage.hpp +++ b/src/Patchage.hpp @@ -38,6 +38,7 @@ #include #include +#include "ClientType.hpp" #include "Connector.hpp" #include "ILog.hpp" #include "Legend.hpp" @@ -85,9 +86,10 @@ public: void save(); void quit() { _main_win->hide(); } - void refresh(); - inline void queue_refresh() { _refresh = true; } - inline void driver_detached() { _driver_detached = true; } + void refresh(); + + void driver_attached(ClientType type); + void driver_detached(ClientType type); void update_state(); void store_window_location(); @@ -96,6 +98,7 @@ public: { return _menu_view_human_names->get_active(); } + bool sort_ports() const { return _menu_view_sort_ports->get_active(); } protected: @@ -110,8 +113,6 @@ protected: void on_driver_event(const PatchageEvent& event); void process_events(); - void connect_widgets(); - void on_arrange(); void on_sprung_layout_toggled(); void on_help_about(); diff --git a/src/PatchageEvent.hpp b/src/PatchageEvent.hpp index d5add47..0095cde 100644 --- a/src/PatchageEvent.hpp +++ b/src/PatchageEvent.hpp @@ -19,6 +19,7 @@ #include "ClientID.hpp" #include "ClientInfo.hpp" +#include "ClientType.hpp" #include "PortID.hpp" #include "PortInfo.hpp" @@ -26,6 +27,16 @@ #include +struct DriverAttachmentEvent +{ + ClientType type; +}; + +struct DriverDetachmentEvent +{ + ClientType type; +}; + struct ClientCreationEvent { ClientID id; @@ -61,7 +72,9 @@ struct DisconnectionEvent }; /// An event from drivers that is processed by the GUI -using PatchageEvent = boost::variantis_input()) { + for (const auto* p : *this) { + if (p->is_input()) { has_in = true; } else { has_out = true; } + if (has_in && has_out) { _menu->items()[0].show(); // Show "Split" menu item return; diff --git a/src/event_to_string.cpp b/src/event_to_string.cpp index 83455bf..503144c 100644 --- a/src/event_to_string.cpp +++ b/src/event_to_string.cpp @@ -16,6 +16,7 @@ #include "event_to_string.hpp" +#include "ClientType.hpp" #include "PatchageEvent.hpp" #include "warnings.hpp" @@ -34,6 +35,26 @@ struct EventPrinter { using result_type = std::string; ///< For boost::apply_visitor + std::string operator()(const ClientType type) + { + switch (type) { + case ClientType::jack: + return "JACK"; + case ClientType::alsa: + return "ALSA"; + } + } + + std::string operator()(const DriverAttachmentEvent& event) + { + return fmt::format("Attached to {}", (*this)(event.type)); + } + + std::string operator()(const DriverDetachmentEvent& event) + { + return fmt::format("Detached from {}", (*this)(event.type)); + } + std::string operator()(const ClientCreationEvent& event) { return fmt::format( diff --git a/src/handle_event.cpp b/src/handle_event.cpp index 98a81f3..d3752d1 100644 --- a/src/handle_event.cpp +++ b/src/handle_event.cpp @@ -38,6 +38,16 @@ public: : _patchage{patchage} {} + void operator()(const DriverAttachmentEvent& event) + { + _patchage.driver_attached(event.type); + } + + void operator()(const DriverDetachmentEvent& event) + { + _patchage.driver_detached(event.type); + } + void operator()(const ClientCreationEvent& event) { // Don't create empty modules, they will be created when ports are added -- cgit v1.2.1