From e3f3f3586411136e5b7f61cc1726bbc0635deddd Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 28 Nov 2020 14:54:25 +0100 Subject: Make PatchageEvent a variant --- src/AlsaDriver.cpp | 31 ++++++-------- src/JackDbusDriver.hpp | 1 - src/JackDriver.cpp | 24 +++++------ src/JackDriver.hpp | 2 +- src/PatchageEvent.cpp | 108 +++++++++++++++++++++++++++++-------------------- src/PatchageEvent.hpp | 85 +++++++++++++++++++------------------- 6 files changed, 132 insertions(+), 119 deletions(-) (limited to 'src') diff --git a/src/AlsaDriver.cpp b/src/AlsaDriver.cpp index 89ef94e..3ef466d 100644 --- a/src/AlsaDriver.cpp +++ b/src/AlsaDriver.cpp @@ -564,19 +564,17 @@ AlsaDriver::_refresh_main() case SND_SEQ_EVENT_PORT_SUBSCRIBED: if (!ignore(ev->data.connect.sender) && !ignore(ev->data.connect.dest)) { - _events.push( - PatchageEvent(PatchageEvent::Type::connection, - addr_to_id(ev->data.connect.sender, false), - addr_to_id(ev->data.connect.dest, true))); + _events.emplace( + ConnectionEvent{addr_to_id(ev->data.connect.sender, false), + addr_to_id(ev->data.connect.dest, true)}); } break; case SND_SEQ_EVENT_PORT_UNSUBSCRIBED: if (!ignore(ev->data.connect.sender) && !ignore(ev->data.connect.dest)) { - _events.push( - PatchageEvent(PatchageEvent::Type::disconnection, - addr_to_id(ev->data.connect.sender, false), - addr_to_id(ev->data.connect.dest, true))); + _events.emplace(DisconnectionEvent{ + addr_to_id(ev->data.connect.sender, false), + addr_to_id(ev->data.connect.dest, true)}); } break; case SND_SEQ_EVENT_PORT_START: @@ -586,21 +584,18 @@ AlsaDriver::_refresh_main() caps = snd_seq_port_info_get_capability(pinfo); if (!ignore(ev->data.addr)) { - _events.push(PatchageEvent( - PatchageEvent::Type::port_creation, - addr_to_id(ev->data.addr, (caps & SND_SEQ_PORT_CAP_READ)))); + _events.emplace(PortCreationEvent{ + addr_to_id(ev->data.addr, (caps & SND_SEQ_PORT_CAP_READ))}); } break; case SND_SEQ_EVENT_PORT_EXIT: if (!ignore(ev->data.addr, false)) { // Note: getting caps at this point does not work // Delete both inputs and outputs (to handle duplex ports) - _events.push( - PatchageEvent(PatchageEvent::Type::port_destruction, - addr_to_id(ev->data.addr, true))); - _events.push( - PatchageEvent(PatchageEvent::Type::port_destruction, - addr_to_id(ev->data.addr, false))); + _events.emplace( + PortDestructionEvent{addr_to_id(ev->data.addr, true)}); + _events.emplace( + PortDestructionEvent{addr_to_id(ev->data.addr, false)}); _port_addrs.erase(_app->canvas()->find_port( addr_to_id(ev->data.addr, false))); @@ -627,7 +622,7 @@ AlsaDriver::process_events(Patchage* app) while (!_events.empty()) { PatchageEvent& ev = _events.front(); - ev.execute(app); + handle_event(*app, ev); _events.pop(); } } diff --git a/src/JackDbusDriver.hpp b/src/JackDbusDriver.hpp index ac1137d..c28c1d5 100644 --- a/src/JackDbusDriver.hpp +++ b/src/JackDbusDriver.hpp @@ -31,7 +31,6 @@ class ILog; class PatchageCanvas; -class PatchageEvent; class PatchagePort; class JackDriver : public Driver diff --git a/src/JackDriver.cpp b/src/JackDriver.cpp index b1cf0aa..904fcc6 100644 --- a/src/JackDriver.cpp +++ b/src/JackDriver.cpp @@ -476,11 +476,9 @@ JackDriver::jack_client_registration_cb(const char* name, assert(me->_client); if (registered) { - me->_events.push( - PatchageEvent(PatchageEvent::Type::client_creation, name)); + me->_events.emplace(ClientCreationEvent{name}); } else { - me->_events.push( - PatchageEvent(PatchageEvent::Type::client_destruction, name)); + me->_events.emplace(ClientDestructionEvent{name}); } } @@ -496,11 +494,9 @@ JackDriver::jack_port_registration_cb(jack_port_id_t port_id, const char* const name = jack_port_name(port); if (registered) { - me->_events.push(PatchageEvent(PatchageEvent::Type::port_creation, - PortID::jack(name))); + me->_events.emplace(PortCreationEvent{PortID::jack(name)}); } else { - me->_events.push(PatchageEvent(PatchageEvent::Type::port_destruction, - PortID::jack(name))); + me->_events.emplace(PortDestructionEvent{PortID::jack(name)}); } } @@ -519,13 +515,11 @@ JackDriver::jack_port_connect_cb(jack_port_id_t src, const char* const dst_name = jack_port_name(dst_port); if (connect) { - me->_events.push(PatchageEvent(PatchageEvent::Type::connection, - PortID::jack(src_name), - PortID::jack(dst_name))); + me->_events.emplace( + ConnectionEvent{PortID::jack(src_name), PortID::jack(dst_name)}); } else { - me->_events.push(PatchageEvent(PatchageEvent::Type::disconnection, - PortID::jack(src_name), - PortID::jack(dst_name))); + me->_events.emplace( + DisconnectionEvent{PortID::jack(src_name), PortID::jack(dst_name)}); } } @@ -628,7 +622,7 @@ JackDriver::process_events(Patchage* app) { while (!_events.empty()) { PatchageEvent& ev = _events.front(); - ev.execute(app); + handle_event(*app, ev); _events.pop(); } } diff --git a/src/JackDriver.hpp b/src/JackDriver.hpp index 03ff365..402ebc7 100644 --- a/src/JackDriver.hpp +++ b/src/JackDriver.hpp @@ -18,6 +18,7 @@ #define PATCHAGE_JACKDRIVER_HPP #include "Driver.hpp" +#include "PatchageEvent.hpp" #include #include @@ -29,7 +30,6 @@ class ILog; class Patchage; class PatchageCanvas; -class PatchageEvent; class PatchageModule; class PatchagePort; diff --git a/src/PatchageEvent.cpp b/src/PatchageEvent.cpp index abff4b8..353bb16 100644 --- a/src/PatchageEvent.cpp +++ b/src/PatchageEvent.cpp @@ -37,78 +37,100 @@ PATCHAGE_DISABLE_FMT_WARNINGS #include PATCHAGE_RESTORE_WARNINGS -void -PatchageEvent::execute(Patchage* patchage) +namespace { + +class EventHandler { - if (_type == Type::refresh) { - patchage->refresh(); +public: + using result_type = void; ///< For boost::apply_visitor - } else if (_type == Type::client_creation) { - // No empty modules (for now) - g_free(_str); - _str = nullptr; + explicit EventHandler(Patchage& patchage) + : _patchage{patchage} + {} - } else if (_type == Type::client_destruction) { - patchage->canvas()->remove_module(_str); - g_free(_str); - _str = nullptr; + void operator()(const NoopEvent&) {} + + void operator()(const ClientCreationEvent&) + { + // Don't create empty modules, they will be created when ports are added + } - } else if (_type == Type::port_creation) { + void operator()(const ClientDestructionEvent& event) + { + _patchage.canvas()->remove_module(event.name); + } + void operator()(const PortCreationEvent& event) + { Driver* driver = nullptr; - if (_port_1.type() == PortID::Type::jack) { + if (event.id.type() == PortID::Type::jack) { #if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) - driver = patchage->jack_driver(); + driver = _patchage.jack_driver(); #endif #ifdef HAVE_ALSA - } else if (_port_1.type() == PortID::Type::alsa) { - driver = patchage->alsa_driver(); + } else if (event.id.type() == PortID::Type::alsa) { + driver = _patchage.alsa_driver(); #endif } if (driver) { - PatchagePort* port = driver->create_port_view(patchage, _port_1); + PatchagePort* port = driver->create_port_view(&_patchage, event.id); if (!port) { - patchage->log().error(fmt::format( - "Unable to create view for port \"{}\"", _port_1)); + _patchage.log().error(fmt::format( + "Unable to create view for port \"{}\"", event.id)); } } else { - patchage->log().error( - fmt::format("Unknown type for port \"{}\"", _port_1)); + _patchage.log().error( + fmt::format("Unknown type for port \"{}\"", event.id)); } + } - } else if (_type == Type::port_destruction) { - - patchage->canvas()->remove_port(_port_1); - - } else if (_type == Type::connection) { + void operator()(const PortDestructionEvent& event) + { + _patchage.canvas()->remove_port(event.id); + } - PatchagePort* port_1 = patchage->canvas()->find_port(_port_1); - PatchagePort* port_2 = patchage->canvas()->find_port(_port_2); + void operator()(const ConnectionEvent& event) + { + PatchagePort* port_1 = _patchage.canvas()->find_port(event.tail); + PatchagePort* port_2 = _patchage.canvas()->find_port(event.head); if (!port_1) { - patchage->log().error( - fmt::format("Unable to find port \"{}\" to connect", _port_1)); + _patchage.log().error(fmt::format( + "Unable to find port \"{}\" to connect", event.tail)); } else if (!port_2) { - patchage->log().error( - fmt::format("Unable to find port \"{}\" to connect", _port_2)); + _patchage.log().error(fmt::format( + "Unable to find port \"{}\" to connect", event.head)); } else { - patchage->canvas()->make_connection(port_1, port_2); + _patchage.canvas()->make_connection(port_1, port_2); } + } - } else if (_type == Type::disconnection) { - - PatchagePort* port_1 = patchage->canvas()->find_port(_port_1); - PatchagePort* port_2 = patchage->canvas()->find_port(_port_2); + void operator()(const DisconnectionEvent& event) + { + PatchagePort* port_1 = _patchage.canvas()->find_port(event.tail); + PatchagePort* port_2 = _patchage.canvas()->find_port(event.head); if (!port_1) { - patchage->log().error(fmt::format( - "Unable to find port \"{}\" to disconnect", _port_1)); + _patchage.log().error(fmt::format( + "Unable to find port \"{}\" to disconnect", event.tail)); } else if (!port_2) { - patchage->log().error(fmt::format( - "Unable to find port \"{}\" to disconnect", _port_2)); + _patchage.log().error(fmt::format( + "Unable to find port \"{}\" to disconnect", event.head)); } else { - patchage->canvas()->remove_edge_between(port_1, port_2); + _patchage.canvas()->remove_edge_between(port_1, port_2); } } + +private: + Patchage& _patchage; +}; + +} // namespace + +void +handle_event(Patchage& patchage, const PatchageEvent& event) +{ + EventHandler handler{patchage}; + boost::apply_visitor(handler, event); } diff --git a/src/PatchageEvent.hpp b/src/PatchageEvent.hpp index dca4dbd..b4aa640 100644 --- a/src/PatchageEvent.hpp +++ b/src/PatchageEvent.hpp @@ -17,59 +17,62 @@ #ifndef PATCHAGE_PATCHAGEEVENT_HPP #define PATCHAGE_PATCHAGEEVENT_HPP -#include "patchage_config.h" - #include "PatchagePort.hpp" #include "PortID.hpp" -#include +#include + +#include +#include class Patchage; -/// An event from drivers that is processed by the GUI -class PatchageEvent -{ -public: - enum class Type : uint8_t - { - noop, - refresh, - client_creation, - client_destruction, - port_creation, - port_destruction, - connection, - disconnection, - }; +struct NoopEvent +{}; - PatchageEvent(Type type, const char* str) - : _str(g_strdup(str)) - , _port_1(PortID::nothing()) - , _port_2(PortID::nothing()) - , _type(type) - {} +struct ClientCreationEvent +{ + std::string name; +}; - PatchageEvent(Type type, PortID port) - : _port_1(std::move(port)) - , _port_2(PortID::nothing()) - , _type(type) - {} +struct ClientDestructionEvent +{ + std::string name; +}; - PatchageEvent(Type type, PortID tail, PortID head) - : _port_1(std::move(tail)) - , _port_2(std::move(head)) - , _type(type) - {} +struct PortCreationEvent +{ + PortID id; +}; - void execute(Patchage* patchage); +struct PortDestructionEvent +{ + PortID id; +}; - inline Type type() const { return _type; } +struct ConnectionEvent +{ + PortID tail; + PortID head; +}; -private: - char* _str{nullptr}; - PortID _port_1; - PortID _port_2; - Type _type; +struct DisconnectionEvent +{ + PortID tail; + PortID head; }; +/// An event from drivers that is processed by the GUI +using PatchageEvent = boost::variant; + +/// Handle an event in the GUI +void +handle_event(Patchage& patchage, const PatchageEvent& event); + #endif // PATCHAGE_PATCHAGEEVENT_HPP -- cgit v1.2.1