From 98e2535b82ab601081a56c8a22d789d2da25cfd8 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 29 Nov 2020 18:31:45 +0100 Subject: Use more reasonable class names --- src/Canvas.cpp | 343 +++++++++++++++++++++++++++++++++++++++++++++++ src/Canvas.hpp | 94 +++++++++++++ src/CanvasModule.cpp | 165 +++++++++++++++++++++++ src/CanvasModule.hpp | 87 ++++++++++++ src/CanvasPort.hpp | 125 +++++++++++++++++ src/Driver.hpp | 4 +- src/Event.hpp | 88 ++++++++++++ src/JackDbusDriver.cpp | 2 +- src/JackLibDriver.cpp | 2 +- src/Patchage.cpp | 48 ++++--- src/Patchage.hpp | 14 +- src/PatchageCanvas.cpp | 347 ------------------------------------------------ src/PatchageCanvas.hpp | 94 ------------- src/PatchageEvent.hpp | 88 ------------ src/PatchageModule.cpp | 166 ----------------------- src/PatchageModule.hpp | 87 ------------ src/PatchagePort.hpp | 125 ----------------- src/event_to_string.cpp | 6 +- src/event_to_string.hpp | 6 +- src/handle_event.cpp | 18 +-- src/handle_event.hpp | 4 +- wscript | 4 +- 22 files changed, 955 insertions(+), 962 deletions(-) create mode 100644 src/Canvas.cpp create mode 100644 src/Canvas.hpp create mode 100644 src/CanvasModule.cpp create mode 100644 src/CanvasModule.hpp create mode 100644 src/CanvasPort.hpp create mode 100644 src/Event.hpp delete mode 100644 src/PatchageCanvas.cpp delete mode 100644 src/PatchageCanvas.hpp delete mode 100644 src/PatchageEvent.hpp delete mode 100644 src/PatchageModule.cpp delete mode 100644 src/PatchageModule.hpp delete mode 100644 src/PatchagePort.hpp diff --git a/src/Canvas.cpp b/src/Canvas.cpp new file mode 100644 index 0000000..a3d3a89 --- /dev/null +++ b/src/Canvas.cpp @@ -0,0 +1,343 @@ +/* This file is part of Patchage. + * Copyright 2007-2020 David Robillard + * + * Patchage is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License + * along with Patchage. If not, see . + */ + +#include "Canvas.hpp" + +#include "patchage_config.h" + +#include "CanvasModule.hpp" +#include "CanvasPort.hpp" +#include "Connector.hpp" +#include "Patchage.hpp" +#include "PortNames.hpp" +#include "SignalDirection.hpp" +#include "warnings.hpp" + +#include + +PATCHAGE_DISABLE_GANV_WARNINGS +#include "ganv/Edge.hpp" +PATCHAGE_RESTORE_WARNINGS + +PATCHAGE_DISABLE_FMT_WARNINGS +#include +#include +PATCHAGE_RESTORE_WARNINGS + +namespace patchage { + +Canvas::Canvas(Connector& connector, int width, int height) + : Ganv::Canvas(width, height) + , _connector(connector) +{ + signal_event.connect(sigc::mem_fun(this, &Canvas::on_event)); + signal_connect.connect(sigc::mem_fun(this, &Canvas::on_connect)); + signal_disconnect.connect(sigc::mem_fun(this, &Canvas::on_disconnect)); +} + +CanvasModule* +Canvas::create_module(Patchage& patchage, + const ClientID& id, + const ClientInfo& info) +{ + (void)patchage; + (void)id; + (void)info; + return nullptr; +} + +CanvasPort* +Canvas::create_port(Patchage& patchage, const PortID& id, const PortInfo& info) +{ + const auto client_id = id.client(); + + const auto port_name = + ((id.type() == PortID::Type::alsa) ? info.label : PortNames(id).port()); + + // Figure out the client name, for ALSA we need the metadata cache + std::string client_name; + if (id.type() == PortID::Type::alsa) { + const auto client_info = patchage.metadata().client(client_id); + if (!client_info.has_value()) { + patchage.log().error(fmt::format( + "Unable to add port \"{}\", client \"{}\" is unknown", + id, + client_id)); + + return nullptr; + } + + client_name = client_info->label; + } else { + client_name = PortNames(id).client(); + } + + // Determine the module type to place the port on in case of splitting + SignalDirection module_type = SignalDirection::duplex; + if (patchage.conf().get_module_split(client_name, info.is_terminal)) { + module_type = info.direction; + } + + // Find or create parent module + CanvasModule* parent = find_module(client_id, module_type); + if (!parent) { + parent = + new CanvasModule(&patchage, client_name, module_type, client_id); + + parent->load_location(); + add_module(client_id, parent); + } + + if (parent->get_port(id)) { + // TODO: Update existing port? + patchage.log().error(fmt::format( + "Module \"{}\" already has port \"{}\"", client_name, port_name)); + return nullptr; + } + + auto* const port = new CanvasPort(*parent, + info.type, + id, + port_name, + info.label, + info.direction == SignalDirection::input, + patchage.conf().get_port_color(info.type), + patchage.show_human_names(), + info.order); + + index_port(id, port); + + return port; +} + +CanvasModule* +Canvas::find_module(const ClientID& id, const SignalDirection type) +{ + auto i = _module_index.find(id); + + CanvasModule* io_module = nullptr; + for (; i != _module_index.end() && i->first == id; ++i) { + if (i->second->type() == type) { + return i->second; + } + + if (i->second->type() == SignalDirection::duplex) { + io_module = i->second; + } + } + + // Return InputOutput module for Input or Output (or nullptr if not found) + return io_module; +} + +void +Canvas::remove_module(const ClientID& id) +{ + auto i = _module_index.find(id); + while (i != _module_index.end()) { + CanvasModule* mod = i->second; + _module_index.erase(i); + i = _module_index.find(id); + delete mod; + } +} + +CanvasPort* +Canvas::find_port(const PortID& id) +{ + auto i = _port_index.find(id); + if (i != _port_index.end()) { + assert(i->second->get_module()); + return i->second; + } + + return nullptr; +} + +void +Canvas::remove_port(const PortID& id) +{ + CanvasPort* const port = find_port(id); + _port_index.erase(id); + delete port; +} + +struct RemovePortsData +{ + using Predicate = bool (*)(const CanvasPort*); + + explicit RemovePortsData(Predicate p) + : pred(p) + {} + + Predicate pred; + std::set empty; +}; + +static void +delete_port_if_matches(GanvPort* port, void* cdata) +{ + auto* data = static_cast(cdata); + auto* pport = dynamic_cast(Glib::wrap(port)); + if (pport && data->pred(pport)) { + delete pport; + } +} + +static void +remove_ports_matching(GanvNode* node, void* cdata) +{ + if (!GANV_IS_MODULE(node)) { + return; + } + + Ganv::Module* cmodule = Glib::wrap(GANV_MODULE(node)); + auto* pmodule = dynamic_cast(cmodule); + if (!pmodule) { + return; + } + + auto* data = static_cast(cdata); + + pmodule->for_each_port(delete_port_if_matches, data); + + if (pmodule->num_ports() == 0) { + data->empty.insert(pmodule); + } +} + +void +Canvas::remove_ports(bool (*pred)(const CanvasPort*)) +{ + RemovePortsData data(pred); + + for_each_node(remove_ports_matching, &data); + + for (auto i = _port_index.begin(); i != _port_index.end();) { + auto next = i; + ++next; + if (pred(i->second)) { + _port_index.erase(i); + } + i = next; + } + + for (CanvasModule* m : data.empty) { + delete m; + } +} + +void +Canvas::on_connect(Ganv::Node* port1, Ganv::Node* port2) +{ + auto* const p1 = dynamic_cast(port1); + auto* const p2 = dynamic_cast(port2); + + if (p1 && p2) { + if (p1->is_output() && p2->is_input()) { + _connector.connect(p1->id(), p2->id()); + } else if (p2->is_output() && p1->is_input()) { + _connector.connect(p2->id(), p1->id()); + } + } +} + +void +Canvas::on_disconnect(Ganv::Node* port1, Ganv::Node* port2) +{ + auto* const p1 = dynamic_cast(port1); + auto* const p2 = dynamic_cast(port2); + + if (p1 && p2) { + if (p1->is_output() && p2->is_input()) { + _connector.disconnect(p1->id(), p2->id()); + } else if (p2->is_output() && p1->is_input()) { + _connector.disconnect(p2->id(), p1->id()); + } + } +} + +void +Canvas::add_module(const ClientID& id, CanvasModule* module) +{ + _module_index.emplace(id, module); + + // Join partners, if applicable + CanvasModule* in_module = nullptr; + CanvasModule* out_module = nullptr; + if (module->type() == SignalDirection::input) { + in_module = module; + out_module = find_module(id, SignalDirection::output); + } else if (module->type() == SignalDirection::output) { + in_module = find_module(id, SignalDirection::input); + out_module = module; + } + + if (in_module && out_module) { + out_module->set_partner(in_module); + } +} + +void +disconnect_edge(GanvEdge* edge, void* data) +{ + auto* canvas = static_cast(data); + Ganv::Edge* edgemm = Glib::wrap(edge); + canvas->on_disconnect(edgemm->get_tail(), edgemm->get_head()); +} + +bool +Canvas::on_event(GdkEvent* ev) +{ + if (ev->type == GDK_KEY_PRESS && ev->key.keyval == GDK_Delete) { + for_each_selected_edge(disconnect_edge, this); + clear_selection(); + return true; + } + + return false; +} + +bool +Canvas::make_connection(Ganv::Node* tail, Ganv::Node* head) +{ + new Ganv::Edge(*this, tail, head); + return true; +} + +void +Canvas::remove_module(CanvasModule* module) +{ + // Remove module from cache + for (auto i = _module_index.find(module->id()); + i != _module_index.end() && i->first == module->id(); + ++i) { + if (i->second == module) { + _module_index.erase(i); + return; + } + } +} + +void +Canvas::clear() +{ + _port_index.clear(); + _module_index.clear(); + Ganv::Canvas::clear(); +} + +} // namespace patchage diff --git a/src/Canvas.hpp b/src/Canvas.hpp new file mode 100644 index 0000000..cf27155 --- /dev/null +++ b/src/Canvas.hpp @@ -0,0 +1,94 @@ +/* This file is part of Patchage. + * Copyright 2007-2020 David Robillard + * + * Patchage is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License + * along with Patchage. If not, see . + */ + +#ifndef PATCHAGE_PATCHAGECANVAS_HPP +#define PATCHAGE_PATCHAGECANVAS_HPP + +#include "patchage_config.h" + +#include "CanvasModule.hpp" +#include "Event.hpp" +#include "PortID.hpp" +#include "warnings.hpp" + +PATCHAGE_DISABLE_GANV_WARNINGS +#include "ganv/Canvas.hpp" +PATCHAGE_RESTORE_WARNINGS + +#include +#include +#include + +namespace patchage { + +class Patchage; +class CanvasModule; +class CanvasPort; +class Connector; + +class Canvas : public Ganv::Canvas +{ +public: + Canvas(Connector& connector, int width, int height); + + CanvasModule* create_module(Patchage& patchage, + const ClientID& id, + const ClientInfo& info); + + CanvasPort* + create_port(Patchage& patchage, const PortID& id, const PortInfo& info); + + CanvasModule* find_module(const ClientID& id, SignalDirection type); + CanvasPort* find_port(const PortID& id); + + void remove_module(const ClientID& id); + void remove_module(CanvasModule* module); + + void index_port(const PortID& id, CanvasPort* port) + { + _port_index.insert(std::make_pair(id, port)); + } + + void remove_ports(bool (*pred)(const CanvasPort*)); + + void add_module(const ClientID& id, CanvasModule* module); + + bool make_connection(Ganv::Node* tail, Ganv::Node* head); + + void remove_port(const PortID& id); + + void clear() override; + +private: + using PortIndex = std::map; + using ModuleIndex = std::multimap; + + friend void disconnect_edge(GanvEdge*, void*); + + bool on_event(GdkEvent* ev); + bool on_connection_event(Ganv::Edge* c, GdkEvent* ev); + + void on_connect(Ganv::Node* port1, Ganv::Node* port2); + void on_disconnect(Ganv::Node* port1, Ganv::Node* port2); + + Connector& _connector; + PortIndex _port_index; + ModuleIndex _module_index; +}; + +} // namespace patchage + +#endif // PATCHAGE_PATCHAGECANVAS_HPP diff --git a/src/CanvasModule.cpp b/src/CanvasModule.cpp new file mode 100644 index 0000000..c049346 --- /dev/null +++ b/src/CanvasModule.cpp @@ -0,0 +1,165 @@ +/* This file is part of Patchage. + * Copyright 2010-2020 David Robillard + * + * Patchage is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License + * along with Patchage. If not, see . + */ + +#include "CanvasModule.hpp" + +#include "Canvas.hpp" +#include "CanvasPort.hpp" +#include "Patchage.hpp" +#include "SignalDirection.hpp" + +namespace patchage { + +CanvasModule::CanvasModule(Patchage* app, + const std::string& name, + SignalDirection type, + ClientID id, + double x, + double y) + : Module(*app->canvas(), name, x, y) + , _app(app) + , _menu(nullptr) + , _name(name) + , _type(type) + , _id(std::move(id)) +{ + signal_event().connect(sigc::mem_fun(this, &CanvasModule::on_event)); + + signal_moved().connect(sigc::mem_fun(this, &CanvasModule::store_location)); + + // Set as source by default, turned off if input ports added + set_is_source(true); +} + +CanvasModule::~CanvasModule() +{ + _app->canvas()->remove_module(this); + delete _menu; + _menu = nullptr; +} + +void +CanvasModule::update_menu() +{ + if (!_menu) { + return; + } + + if (_type == SignalDirection::duplex) { + bool has_in = false; + bool has_out = false; + 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; + } + } + _menu->items()[0].hide(); // Hide "Split" menu item + } +} + +bool +CanvasModule::show_menu(GdkEventButton* ev) +{ + _menu = new Gtk::Menu(); + Gtk::Menu::MenuList& items = _menu->items(); + if (_type == SignalDirection::duplex) { + items.push_back(Gtk::Menu_Helpers::MenuElem( + "_Split", sigc::mem_fun(this, &CanvasModule::split))); + update_menu(); + } else { + items.push_back(Gtk::Menu_Helpers::MenuElem( + "_Join", sigc::mem_fun(this, &CanvasModule::join))); + } + items.push_back(Gtk::Menu_Helpers::MenuElem( + "_Disconnect All", + sigc::mem_fun(this, &CanvasModule::menu_disconnect_all))); + + _menu->popup(ev->button, ev->time); + return true; +} + +bool +CanvasModule::on_event(GdkEvent* ev) +{ + if (ev->type == GDK_BUTTON_PRESS && ev->button.button == 3) { + return show_menu(&ev->button); + } + return false; +} + +void +CanvasModule::load_location() +{ + Coord loc; + + if (_app->conf().get_module_location(_name, _type, loc)) { + move_to(loc.x, loc.y); + } else { + move_to(20 + rand() % 640, 20 + rand() % 480); + } +} + +void +CanvasModule::store_location(double x, double y) +{ + _app->conf().set_module_location(_name, _type, {x, y}); +} + +void +CanvasModule::split() +{ + assert(_type == SignalDirection::duplex); + _app->conf().set_module_split(_name, true); + _app->refresh(); +} + +void +CanvasModule::join() +{ + assert(_type != SignalDirection::duplex); + _app->conf().set_module_split(_name, false); + _app->refresh(); +} + +void +CanvasModule::menu_disconnect_all() +{ + for (Ganv::Port* p : *this) { + p->disconnect(); + } +} + +CanvasPort* +CanvasModule::get_port(const PortID& id) +{ + for (Ganv::Port* p : *this) { + auto* pport = dynamic_cast(p); + if (pport && pport->id() == id) { + return pport; + } + } + + return nullptr; +} + +} // namespace patchage diff --git a/src/CanvasModule.hpp b/src/CanvasModule.hpp new file mode 100644 index 0000000..c5ed699 --- /dev/null +++ b/src/CanvasModule.hpp @@ -0,0 +1,87 @@ +/* This file is part of Patchage. + * Copyright 2007-2020 David Robillard + * + * Patchage is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License + * along with Patchage. If not, see . + */ + +#ifndef PATCHAGE_PATCHAGEMODULE_HPP +#define PATCHAGE_PATCHAGEMODULE_HPP + +#include "ClientID.hpp" +#include "SignalDirection.hpp" +#include "warnings.hpp" + +PATCHAGE_DISABLE_GANV_WARNINGS +#include "ganv/Module.hpp" +#include "ganv/Port.hpp" +PATCHAGE_RESTORE_WARNINGS + +#include + +#include + +namespace patchage { + +struct PortID; + +class CanvasPort; +class Patchage; + +class CanvasModule : public Ganv::Module +{ +public: + CanvasModule(Patchage* app, + const std::string& name, + SignalDirection type, + ClientID id, + double x = 0, + double y = 0); + + CanvasModule(const CanvasModule&) = delete; + CanvasModule& operator=(const CanvasModule&) = delete; + + CanvasModule(CanvasModule&&) = delete; + CanvasModule& operator=(CanvasModule&&) = delete; + + ~CanvasModule() override; + + void split(); + void join(); + + bool show_menu(GdkEventButton* ev); + void update_menu(); + + CanvasPort* get_port(const PortID& id); + + void load_location(); + void menu_disconnect_all(); + void show_dialog() {} + void store_location(double x, double y); + + SignalDirection type() const { return _type; } + ClientID id() const { return _id; } + const std::string& name() const { return _name; } + +protected: + bool on_event(GdkEvent* ev) override; + + Patchage* _app; + Gtk::Menu* _menu; + std::string _name; + SignalDirection _type; + ClientID _id; +}; + +} // namespace patchage + +#endif // PATCHAGE_PATCHAGEMODULE_HPP diff --git a/src/CanvasPort.hpp b/src/CanvasPort.hpp new file mode 100644 index 0000000..5bd581c --- /dev/null +++ b/src/CanvasPort.hpp @@ -0,0 +1,125 @@ +/* This file is part of Patchage. + * Copyright 2007-2020 David Robillard + * + * Patchage is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License + * along with Patchage. If not, see . + */ + +#ifndef PATCHAGE_PATCHAGEPORT_HPP +#define PATCHAGE_PATCHAGEPORT_HPP + +#include "Canvas.hpp" +#include "CanvasModule.hpp" +#include "Configuration.hpp" +#include "PortID.hpp" +#include "warnings.hpp" + +PATCHAGE_DISABLE_GANV_WARNINGS +#include "ganv/Module.hpp" +#include "ganv/Port.hpp" +PATCHAGE_RESTORE_WARNINGS + +#include +#include + +#include + +namespace patchage { + +/// A port on a CanvasModule +class CanvasPort : public Ganv::Port +{ +public: + CanvasPort(Ganv::Module& module, + PortType type, + PortID id, + const std::string& name, + const std::string& human_name, + bool is_input, + uint32_t color, + bool show_human_name, + boost::optional order = boost::optional()) + : Port(module, + (show_human_name && !human_name.empty()) ? human_name : name, + is_input, + color) + , _type(type) + , _id(std::move(id)) + , _name(name) + , _human_name(human_name) + , _order(order) + { + signal_event().connect(sigc::mem_fun(this, &CanvasPort::on_event)); + } + + CanvasPort(const CanvasPort&) = delete; + CanvasPort& operator=(const CanvasPort&) = delete; + + CanvasPort(CanvasPort&&) = delete; + CanvasPort& operator=(CanvasPort&&) = delete; + + ~CanvasPort() override = default; + + /** Returns the name of the module/client this port is one */ + std::string module_name() const + { + auto* pmod = dynamic_cast(get_module()); + return std::string(pmod->name()); + } + + /** Returns the full name of this port, as "modulename:portname" */ + std::string full_name() const + { + auto* pmod = dynamic_cast(get_module()); + return std::string(pmod->name()) + ":" + _name; + } + + void show_human_name(bool human) + { + if (human && !_human_name.empty()) { + set_label(_human_name.c_str()); + } else { + set_label(_name.c_str()); + } + } + + bool on_event(GdkEvent* ev) override + { + if (ev->type != GDK_BUTTON_PRESS || ev->button.button != 3) { + return false; + } + + Gtk::Menu* menu = Gtk::manage(new Gtk::Menu()); + menu->items().push_back(Gtk::Menu_Helpers::MenuElem( + "Disconnect", sigc::mem_fun(this, &Port::disconnect))); + + menu->popup(ev->button.button, ev->button.time); + return true; + } + + PortType type() const { return _type; } + PortID id() const { return _id; } + const std::string& name() const { return _name; } + const std::string& human_name() const { return _human_name; } + const boost::optional& order() const { return _order; } + +private: + PortType _type; + PortID _id; + std::string _name; + std::string _human_name; + boost::optional _order; +}; + +} // namespace patchage + +#endif // PATCHAGE_PATCHAGEPORT_HPP diff --git a/src/Driver.hpp b/src/Driver.hpp index 7873ef3..231b7a2 100644 --- a/src/Driver.hpp +++ b/src/Driver.hpp @@ -17,7 +17,7 @@ #ifndef PATCHAGE_DRIVER_HPP #define PATCHAGE_DRIVER_HPP -#include "PatchageEvent.hpp" +#include "Event.hpp" #include #include @@ -28,7 +28,7 @@ namespace patchage { class Driver { public: - using EventSink = std::function; + using EventSink = std::function; explicit Driver(EventSink emit_event) : _emit_event{std::move(emit_event)} diff --git a/src/Event.hpp b/src/Event.hpp new file mode 100644 index 0000000..aa485c1 --- /dev/null +++ b/src/Event.hpp @@ -0,0 +1,88 @@ +/* This file is part of Patchage. + * Copyright 2007-2020 David Robillard + * + * Patchage is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License + * along with Patchage. If not, see . + */ + +#ifndef PATCHAGE_PATCHAGEEVENT_HPP +#define PATCHAGE_PATCHAGEEVENT_HPP + +#include "ClientID.hpp" +#include "ClientInfo.hpp" +#include "ClientType.hpp" +#include "PortID.hpp" +#include "PortInfo.hpp" + +#include + +#include + +namespace patchage { + +struct DriverAttachmentEvent +{ + ClientType type; +}; + +struct DriverDetachmentEvent +{ + ClientType type; +}; + +struct ClientCreationEvent +{ + ClientID id; + ClientInfo info; +}; + +struct ClientDestructionEvent +{ + ClientID id; +}; + +struct PortCreationEvent +{ + PortID id; + PortInfo info; +}; + +struct PortDestructionEvent +{ + PortID id; +}; + +struct ConnectionEvent +{ + PortID tail; + PortID head; +}; + +struct DisconnectionEvent +{ + PortID tail; + PortID head; +}; + +/// An event from drivers that is processed by the GUI +using Event = boost::variant; + +} // namespace patchage + +#endif // PATCHAGE_PATCHAGEEVENT_HPP diff --git a/src/JackDbusDriver.cpp b/src/JackDbusDriver.cpp index 9584901..1e276ef 100644 --- a/src/JackDbusDriver.cpp +++ b/src/JackDbusDriver.cpp @@ -17,8 +17,8 @@ #include "ClientType.hpp" #include "Driver.hpp" +#include "Event.hpp" #include "ILog.hpp" -#include "PatchageEvent.hpp" #include "PortNames.hpp" #include "PortType.hpp" #include "SignalDirection.hpp" diff --git a/src/JackLibDriver.cpp b/src/JackLibDriver.cpp index 63054e5..a120b6c 100644 --- a/src/JackLibDriver.cpp +++ b/src/JackLibDriver.cpp @@ -18,8 +18,8 @@ #include "ClientID.hpp" #include "ClientInfo.hpp" #include "ClientType.hpp" +#include "Event.hpp" #include "ILog.hpp" -#include "PatchageEvent.hpp" #include "PortInfo.hpp" #include "PortNames.hpp" #include "PortType.hpp" diff --git a/src/Patchage.cpp b/src/Patchage.cpp index a7c5198..5cb0fa3 100644 --- a/src/Patchage.cpp +++ b/src/Patchage.cpp @@ -16,11 +16,11 @@ #include "Patchage.hpp" +#include "Canvas.hpp" +#include "CanvasPort.hpp" #include "Configuration.hpp" +#include "Event.hpp" #include "Legend.hpp" -#include "PatchageCanvas.hpp" -#include "PatchageEvent.hpp" -#include "PatchagePort.hpp" #include "UIFile.hpp" #include "event_to_string.hpp" #include "handle_event.hpp" @@ -92,8 +92,8 @@ configure_cb(GtkWindow*, GdkEvent*, gpointer data) int port_order(const GanvPort* a, const GanvPort* b, void*) { - const auto* pa = dynamic_cast(Glib::wrap(a)); - const auto* pb = dynamic_cast(Glib::wrap(b)); + const auto* pa = dynamic_cast(Glib::wrap(a)); + const auto* pb = dynamic_cast(Glib::wrap(b)); if (pa && pb) { if (pa->order() && pb->order()) { return *pa->order() - *pb->order(); @@ -161,8 +161,8 @@ Patchage::Patchage(Options options) , _pane_initialized(false) , _attach(true) { - _canvas = std::unique_ptr{ - new PatchageCanvas(_connector, 1600 * 2, 1200 * 2)}; + _canvas = + std::unique_ptr{new Canvas(_connector, 1600 * 2, 1200 * 2)}; Glib::set_application_name("Patchage"); _about_win->property_program_name() = "Patchage"; @@ -274,7 +274,7 @@ Patchage::Patchage(Options options) // Make Jack driver if possible _jack_driver = make_jack_driver( - _log, [this](const PatchageEvent& event) { on_driver_event(event); }); + _log, [this](const Event& event) { on_driver_event(event); }); if (_jack_driver) { _connector.add_driver(PortID::Type::jack, _jack_driver.get()); @@ -290,7 +290,7 @@ Patchage::Patchage(Options options) // Make ALSA driver if possible _alsa_driver = make_alsa_driver( - _log, [this](const PatchageEvent& event) { on_driver_event(event); }); + _log, [this](const Event& event) { on_driver_event(event); }); if (_alsa_driver) { _connector.add_driver(PortID::Type::alsa, _alsa_driver.get()); @@ -446,7 +446,7 @@ Patchage::zoom(double z) void Patchage::refresh() { - auto sink = [this](const PatchageEvent& event) { + auto sink = [this](const Event& event) { _log.info("Refresh: " + event_to_string(event)); handle_event(*this, event); }; @@ -473,9 +473,8 @@ Patchage::driver_attached(const ClientType type) _menu_jack_disconnect->set_sensitive(true); if (_jack_driver) { - _jack_driver->refresh([this](const PatchageEvent& event) { - handle_event(*this, event); - }); + _jack_driver->refresh( + [this](const Event& event) { handle_event(*this, event); }); } break; @@ -484,9 +483,8 @@ Patchage::driver_attached(const ClientType type) _menu_alsa_disconnect->set_sensitive(true); if (_alsa_driver) { - _alsa_driver->refresh([this](const PatchageEvent& event) { - handle_event(*this, event); - }); + _alsa_driver->refresh( + [this](const Event& event) { handle_event(*this, event); }); } break; @@ -502,7 +500,7 @@ Patchage::driver_detached(const ClientType type) _menu_jack_disconnect->set_sensitive(false); if (_jack_driver && !_jack_driver->is_attached()) { - _canvas->remove_ports([](const PatchagePort* port) { + _canvas->remove_ports([](const CanvasPort* port) { return (port->type() == PortType::jack_audio || port->type() == PortType::jack_midi || port->type() == PortType::jack_osc || @@ -516,7 +514,7 @@ Patchage::driver_detached(const ClientType type) _menu_alsa_connect->set_sensitive(true); _menu_alsa_disconnect->set_sensitive(false); - _canvas->remove_ports([](const PatchagePort* port) { + _canvas->remove_ports([](const CanvasPort* port) { return port->type() == PortType::alsa_midi; }); @@ -555,7 +553,7 @@ load_module_location(GanvNode* node, void*) { if (GANV_IS_MODULE(node)) { Ganv::Module* gmod = Glib::wrap(GANV_MODULE(node)); - auto* pmod = dynamic_cast(gmod); + auto* pmod = dynamic_cast(gmod); if (pmod) { pmod->load_location(); } @@ -569,7 +567,7 @@ Patchage::update_state() } void -Patchage::on_driver_event(const PatchageEvent& event) +Patchage::on_driver_event(const Event& event) { std::lock_guard lock{_events_mutex}; @@ -618,10 +616,10 @@ update_labels(GanvNode* node, void* data) const bool human_names = *static_cast(data); if (GANV_IS_MODULE(node)) { Ganv::Module* gmod = Glib::wrap(GANV_MODULE(node)); - auto* pmod = dynamic_cast(gmod); + auto* pmod = dynamic_cast(gmod); if (pmod) { for (Ganv::Port* gport : *gmod) { - auto* pport = dynamic_cast(gport); + auto* pport = dynamic_cast(gport); if (pport) { pport->show_human_name(human_names); } @@ -720,13 +718,13 @@ update_port_colors(GanvNode* node, void* data) } Ganv::Module* gmod = Glib::wrap(GANV_MODULE(node)); - auto* pmod = dynamic_cast(gmod); + auto* pmod = dynamic_cast(gmod); if (!pmod) { return; } for (Ganv::Port* p : *pmod) { - auto* port = dynamic_cast(p); + auto* port = dynamic_cast(p); if (port) { const uint32_t rgba = patchage->conf().get_port_color(port->type()); port->set_fill_color(rgba); @@ -741,7 +739,7 @@ update_edge_color(GanvEdge* edge, void* data) auto* patchage = static_cast(data); Ganv::Edge* edgemm = Glib::wrap(edge); - auto* tail = dynamic_cast((edgemm)->get_tail()); + auto* tail = dynamic_cast((edgemm)->get_tail()); if (tail) { edgemm->set_color(patchage->conf().get_port_color(tail->type())); } diff --git a/src/Patchage.hpp b/src/Patchage.hpp index 80c3e84..51e6d99 100644 --- a/src/Patchage.hpp +++ b/src/Patchage.hpp @@ -39,11 +39,11 @@ #include "ClientType.hpp" #include "Connector.hpp" +#include "Event.hpp" #include "ILog.hpp" #include "Legend.hpp" #include "Metadata.hpp" #include "Options.hpp" -#include "PatchageEvent.hpp" #include "TextViewLog.hpp" #include "Widget.hpp" #include "patchage_config.h" @@ -56,7 +56,7 @@ namespace patchage { class AudioDriver; -class PatchageCanvas; +class Canvas; class Configuration; /// Main application class @@ -73,7 +73,7 @@ public: Patchage(Patchage&&) = delete; Patchage& operator=(Patchage&&) = delete; - const std::unique_ptr& canvas() const { return _canvas; } + const std::unique_ptr& canvas() const { return _canvas; } void attach(); void refresh(); @@ -108,7 +108,7 @@ protected: Gtk::TreeModelColumn label; }; - void on_driver_event(const PatchageEvent& event); + void on_driver_event(const Event& event); void process_events(); void on_arrange(); @@ -147,12 +147,12 @@ protected: Glib::RefPtr _xml; - std::mutex _events_mutex; - std::queue _driver_events; + std::mutex _events_mutex; + std::queue _driver_events; std::unique_ptr _alsa_driver; - std::unique_ptr _canvas; + std::unique_ptr _canvas; std::unique_ptr _jack_driver; Configuration _conf; diff --git a/src/PatchageCanvas.cpp b/src/PatchageCanvas.cpp deleted file mode 100644 index 0fc0ebe..0000000 --- a/src/PatchageCanvas.cpp +++ /dev/null @@ -1,347 +0,0 @@ -/* This file is part of Patchage. - * Copyright 2007-2020 David Robillard - * - * Patchage is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * - * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License - * along with Patchage. If not, see . - */ - -#include "PatchageCanvas.hpp" - -#include "patchage_config.h" - -#include "Connector.hpp" -#include "Patchage.hpp" -#include "PatchageModule.hpp" -#include "PatchagePort.hpp" -#include "PortNames.hpp" -#include "SignalDirection.hpp" -#include "warnings.hpp" - -#include - -PATCHAGE_DISABLE_GANV_WARNINGS -#include "ganv/Edge.hpp" -PATCHAGE_RESTORE_WARNINGS - -PATCHAGE_DISABLE_FMT_WARNINGS -#include -#include -PATCHAGE_RESTORE_WARNINGS - -namespace patchage { - -PatchageCanvas::PatchageCanvas(Connector& connector, int width, int height) - : Ganv::Canvas(width, height) - , _connector(connector) -{ - signal_event.connect(sigc::mem_fun(this, &PatchageCanvas::on_event)); - signal_connect.connect(sigc::mem_fun(this, &PatchageCanvas::on_connect)); - signal_disconnect.connect( - sigc::mem_fun(this, &PatchageCanvas::on_disconnect)); -} - -PatchageModule* -PatchageCanvas::create_module(Patchage& patchage, - const ClientID& id, - const ClientInfo& info) -{ - (void)patchage; - (void)id; - (void)info; - return nullptr; -} - -PatchagePort* -PatchageCanvas::create_port(Patchage& patchage, - const PortID& id, - const PortInfo& info) -{ - const auto client_id = id.client(); - - const auto port_name = - ((id.type() == PortID::Type::alsa) ? info.label : PortNames(id).port()); - - // Figure out the client name, for ALSA we need the metadata cache - std::string client_name; - if (id.type() == PortID::Type::alsa) { - const auto client_info = patchage.metadata().client(client_id); - if (!client_info.has_value()) { - patchage.log().error(fmt::format( - "Unable to add port \"{}\", client \"{}\" is unknown", - id, - client_id)); - - return nullptr; - } - - client_name = client_info->label; - } else { - client_name = PortNames(id).client(); - } - - // Determine the module type to place the port on in case of splitting - SignalDirection module_type = SignalDirection::duplex; - if (patchage.conf().get_module_split(client_name, info.is_terminal)) { - module_type = info.direction; - } - - // Find or create parent module - PatchageModule* parent = find_module(client_id, module_type); - if (!parent) { - parent = - new PatchageModule(&patchage, client_name, module_type, client_id); - - parent->load_location(); - add_module(client_id, parent); - } - - if (parent->get_port(id)) { - // TODO: Update existing port? - patchage.log().error(fmt::format( - "Module \"{}\" already has port \"{}\"", client_name, port_name)); - return nullptr; - } - - auto* const port = - new PatchagePort(*parent, - info.type, - id, - port_name, - info.label, - info.direction == SignalDirection::input, - patchage.conf().get_port_color(info.type), - patchage.show_human_names(), - info.order); - - index_port(id, port); - - return port; -} - -PatchageModule* -PatchageCanvas::find_module(const ClientID& id, const SignalDirection type) -{ - auto i = _module_index.find(id); - - PatchageModule* io_module = nullptr; - for (; i != _module_index.end() && i->first == id; ++i) { - if (i->second->type() == type) { - return i->second; - } - - if (i->second->type() == SignalDirection::duplex) { - io_module = i->second; - } - } - - // Return InputOutput module for Input or Output (or nullptr if not found) - return io_module; -} - -void -PatchageCanvas::remove_module(const ClientID& id) -{ - auto i = _module_index.find(id); - while (i != _module_index.end()) { - PatchageModule* mod = i->second; - _module_index.erase(i); - i = _module_index.find(id); - delete mod; - } -} - -PatchagePort* -PatchageCanvas::find_port(const PortID& id) -{ - auto i = _port_index.find(id); - if (i != _port_index.end()) { - assert(i->second->get_module()); - return i->second; - } - - return nullptr; -} - -void -PatchageCanvas::remove_port(const PortID& id) -{ - PatchagePort* const port = find_port(id); - _port_index.erase(id); - delete port; -} - -struct RemovePortsData -{ - using Predicate = bool (*)(const PatchagePort*); - - explicit RemovePortsData(Predicate p) - : pred(p) - {} - - Predicate pred; - std::set empty; -}; - -static void -delete_port_if_matches(GanvPort* port, void* cdata) -{ - auto* data = static_cast(cdata); - auto* pport = dynamic_cast(Glib::wrap(port)); - if (pport && data->pred(pport)) { - delete pport; - } -} - -static void -remove_ports_matching(GanvNode* node, void* cdata) -{ - if (!GANV_IS_MODULE(node)) { - return; - } - - Ganv::Module* cmodule = Glib::wrap(GANV_MODULE(node)); - auto* pmodule = dynamic_cast(cmodule); - if (!pmodule) { - return; - } - - auto* data = static_cast(cdata); - - pmodule->for_each_port(delete_port_if_matches, data); - - if (pmodule->num_ports() == 0) { - data->empty.insert(pmodule); - } -} - -void -PatchageCanvas::remove_ports(bool (*pred)(const PatchagePort*)) -{ - RemovePortsData data(pred); - - for_each_node(remove_ports_matching, &data); - - for (auto i = _port_index.begin(); i != _port_index.end();) { - auto next = i; - ++next; - if (pred(i->second)) { - _port_index.erase(i); - } - i = next; - } - - for (PatchageModule* m : data.empty) { - delete m; - } -} - -void -PatchageCanvas::on_connect(Ganv::Node* port1, Ganv::Node* port2) -{ - auto* const p1 = dynamic_cast(port1); - auto* const p2 = dynamic_cast(port2); - - if (p1 && p2) { - if (p1->is_output() && p2->is_input()) { - _connector.connect(p1->id(), p2->id()); - } else if (p2->is_output() && p1->is_input()) { - _connector.connect(p2->id(), p1->id()); - } - } -} - -void -PatchageCanvas::on_disconnect(Ganv::Node* port1, Ganv::Node* port2) -{ - auto* const p1 = dynamic_cast(port1); - auto* const p2 = dynamic_cast(port2); - - if (p1 && p2) { - if (p1->is_output() && p2->is_input()) { - _connector.disconnect(p1->id(), p2->id()); - } else if (p2->is_output() && p1->is_input()) { - _connector.disconnect(p2->id(), p1->id()); - } - } -} - -void -PatchageCanvas::add_module(const ClientID& id, PatchageModule* module) -{ - _module_index.emplace(id, module); - - // Join partners, if applicable - PatchageModule* in_module = nullptr; - PatchageModule* out_module = nullptr; - if (module->type() == SignalDirection::input) { - in_module = module; - out_module = find_module(id, SignalDirection::output); - } else if (module->type() == SignalDirection::output) { - in_module = find_module(id, SignalDirection::input); - out_module = module; - } - - if (in_module && out_module) { - out_module->set_partner(in_module); - } -} - -void -disconnect_edge(GanvEdge* edge, void* data) -{ - auto* canvas = static_cast(data); - Ganv::Edge* edgemm = Glib::wrap(edge); - canvas->on_disconnect(edgemm->get_tail(), edgemm->get_head()); -} - -bool -PatchageCanvas::on_event(GdkEvent* ev) -{ - if (ev->type == GDK_KEY_PRESS && ev->key.keyval == GDK_Delete) { - for_each_selected_edge(disconnect_edge, this); - clear_selection(); - return true; - } - - return false; -} - -bool -PatchageCanvas::make_connection(Ganv::Node* tail, Ganv::Node* head) -{ - new Ganv::Edge(*this, tail, head); - return true; -} - -void -PatchageCanvas::remove_module(PatchageModule* module) -{ - // Remove module from cache - for (auto i = _module_index.find(module->id()); - i != _module_index.end() && i->first == module->id(); - ++i) { - if (i->second == module) { - _module_index.erase(i); - return; - } - } -} - -void -PatchageCanvas::clear() -{ - _port_index.clear(); - _module_index.clear(); - Ganv::Canvas::clear(); -} - -} // namespace patchage diff --git a/src/PatchageCanvas.hpp b/src/PatchageCanvas.hpp deleted file mode 100644 index b823727..0000000 --- a/src/PatchageCanvas.hpp +++ /dev/null @@ -1,94 +0,0 @@ -/* This file is part of Patchage. - * Copyright 2007-2020 David Robillard - * - * Patchage is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * - * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License - * along with Patchage. If not, see . - */ - -#ifndef PATCHAGE_PATCHAGECANVAS_HPP -#define PATCHAGE_PATCHAGECANVAS_HPP - -#include "patchage_config.h" - -#include "PatchageEvent.hpp" -#include "PatchageModule.hpp" -#include "PortID.hpp" -#include "warnings.hpp" - -PATCHAGE_DISABLE_GANV_WARNINGS -#include "ganv/Canvas.hpp" -PATCHAGE_RESTORE_WARNINGS - -#include -#include -#include - -namespace patchage { - -class Patchage; -class PatchageModule; -class PatchagePort; -class Connector; - -class PatchageCanvas : public Ganv::Canvas -{ -public: - PatchageCanvas(Connector& connector, int width, int height); - - PatchageModule* create_module(Patchage& patchage, - const ClientID& id, - const ClientInfo& info); - - PatchagePort* - create_port(Patchage& patchage, const PortID& id, const PortInfo& info); - - PatchageModule* find_module(const ClientID& id, SignalDirection type); - PatchagePort* find_port(const PortID& id); - - void remove_module(const ClientID& id); - void remove_module(PatchageModule* module); - - void index_port(const PortID& id, PatchagePort* port) - { - _port_index.insert(std::make_pair(id, port)); - } - - void remove_ports(bool (*pred)(const PatchagePort*)); - - void add_module(const ClientID& id, PatchageModule* module); - - bool make_connection(Ganv::Node* tail, Ganv::Node* head); - - void remove_port(const PortID& id); - - void clear() override; - -private: - using PortIndex = std::map; - using ModuleIndex = std::multimap; - - friend void disconnect_edge(GanvEdge*, void*); - - bool on_event(GdkEvent* ev); - bool on_connection_event(Ganv::Edge* c, GdkEvent* ev); - - void on_connect(Ganv::Node* port1, Ganv::Node* port2); - void on_disconnect(Ganv::Node* port1, Ganv::Node* port2); - - Connector& _connector; - PortIndex _port_index; - ModuleIndex _module_index; -}; - -} // namespace patchage - -#endif // PATCHAGE_PATCHAGECANVAS_HPP diff --git a/src/PatchageEvent.hpp b/src/PatchageEvent.hpp deleted file mode 100644 index b98375c..0000000 --- a/src/PatchageEvent.hpp +++ /dev/null @@ -1,88 +0,0 @@ -/* This file is part of Patchage. - * Copyright 2007-2020 David Robillard - * - * Patchage is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * - * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License - * along with Patchage. If not, see . - */ - -#ifndef PATCHAGE_PATCHAGEEVENT_HPP -#define PATCHAGE_PATCHAGEEVENT_HPP - -#include "ClientID.hpp" -#include "ClientInfo.hpp" -#include "ClientType.hpp" -#include "PortID.hpp" -#include "PortInfo.hpp" - -#include - -#include - -namespace patchage { - -struct DriverAttachmentEvent -{ - ClientType type; -}; - -struct DriverDetachmentEvent -{ - ClientType type; -}; - -struct ClientCreationEvent -{ - ClientID id; - ClientInfo info; -}; - -struct ClientDestructionEvent -{ - ClientID id; -}; - -struct PortCreationEvent -{ - PortID id; - PortInfo info; -}; - -struct PortDestructionEvent -{ - PortID id; -}; - -struct ConnectionEvent -{ - PortID tail; - PortID head; -}; - -struct DisconnectionEvent -{ - PortID tail; - PortID head; -}; - -/// An event from drivers that is processed by the GUI -using PatchageEvent = boost::variant; - -} // namespace patchage - -#endif // PATCHAGE_PATCHAGEEVENT_HPP diff --git a/src/PatchageModule.cpp b/src/PatchageModule.cpp deleted file mode 100644 index 1931b3b..0000000 --- a/src/PatchageModule.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* This file is part of Patchage. - * Copyright 2010-2020 David Robillard - * - * Patchage is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * - * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License - * along with Patchage. If not, see . - */ - -#include "PatchageModule.hpp" - -#include "Patchage.hpp" -#include "PatchageCanvas.hpp" -#include "PatchagePort.hpp" -#include "SignalDirection.hpp" - -namespace patchage { - -PatchageModule::PatchageModule(Patchage* app, - const std::string& name, - SignalDirection type, - ClientID id, - double x, - double y) - : Module(*app->canvas(), name, x, y) - , _app(app) - , _menu(nullptr) - , _name(name) - , _type(type) - , _id(std::move(id)) -{ - signal_event().connect(sigc::mem_fun(this, &PatchageModule::on_event)); - - signal_moved().connect( - sigc::mem_fun(this, &PatchageModule::store_location)); - - // Set as source by default, turned off if input ports added - set_is_source(true); -} - -PatchageModule::~PatchageModule() -{ - _app->canvas()->remove_module(this); - delete _menu; - _menu = nullptr; -} - -void -PatchageModule::update_menu() -{ - if (!_menu) { - return; - } - - if (_type == SignalDirection::duplex) { - bool has_in = false; - bool has_out = false; - 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; - } - } - _menu->items()[0].hide(); // Hide "Split" menu item - } -} - -bool -PatchageModule::show_menu(GdkEventButton* ev) -{ - _menu = new Gtk::Menu(); - Gtk::Menu::MenuList& items = _menu->items(); - if (_type == SignalDirection::duplex) { - items.push_back(Gtk::Menu_Helpers::MenuElem( - "_Split", sigc::mem_fun(this, &PatchageModule::split))); - update_menu(); - } else { - items.push_back(Gtk::Menu_Helpers::MenuElem( - "_Join", sigc::mem_fun(this, &PatchageModule::join))); - } - items.push_back(Gtk::Menu_Helpers::MenuElem( - "_Disconnect All", - sigc::mem_fun(this, &PatchageModule::menu_disconnect_all))); - - _menu->popup(ev->button, ev->time); - return true; -} - -bool -PatchageModule::on_event(GdkEvent* ev) -{ - if (ev->type == GDK_BUTTON_PRESS && ev->button.button == 3) { - return show_menu(&ev->button); - } - return false; -} - -void -PatchageModule::load_location() -{ - Coord loc; - - if (_app->conf().get_module_location(_name, _type, loc)) { - move_to(loc.x, loc.y); - } else { - move_to(20 + rand() % 640, 20 + rand() % 480); - } -} - -void -PatchageModule::store_location(double x, double y) -{ - _app->conf().set_module_location(_name, _type, {x, y}); -} - -void -PatchageModule::split() -{ - assert(_type == SignalDirection::duplex); - _app->conf().set_module_split(_name, true); - _app->refresh(); -} - -void -PatchageModule::join() -{ - assert(_type != SignalDirection::duplex); - _app->conf().set_module_split(_name, false); - _app->refresh(); -} - -void -PatchageModule::menu_disconnect_all() -{ - for (Ganv::Port* p : *this) { - p->disconnect(); - } -} - -PatchagePort* -PatchageModule::get_port(const PortID& id) -{ - for (Ganv::Port* p : *this) { - auto* pport = dynamic_cast(p); - if (pport && pport->id() == id) { - return pport; - } - } - - return nullptr; -} - -} // namespace patchage diff --git a/src/PatchageModule.hpp b/src/PatchageModule.hpp deleted file mode 100644 index 378f35ed..0000000 --- a/src/PatchageModule.hpp +++ /dev/null @@ -1,87 +0,0 @@ -/* This file is part of Patchage. - * Copyright 2007-2020 David Robillard - * - * Patchage is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * - * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License - * along with Patchage. If not, see . - */ - -#ifndef PATCHAGE_PATCHAGEMODULE_HPP -#define PATCHAGE_PATCHAGEMODULE_HPP - -#include "ClientID.hpp" -#include "SignalDirection.hpp" -#include "warnings.hpp" - -PATCHAGE_DISABLE_GANV_WARNINGS -#include "ganv/Module.hpp" -#include "ganv/Port.hpp" -PATCHAGE_RESTORE_WARNINGS - -#include - -#include - -namespace patchage { - -struct PortID; - -class Patchage; -class PatchagePort; - -class PatchageModule : public Ganv::Module -{ -public: - PatchageModule(Patchage* app, - const std::string& name, - SignalDirection type, - ClientID id, - double x = 0, - double y = 0); - - PatchageModule(const PatchageModule&) = delete; - PatchageModule& operator=(const PatchageModule&) = delete; - - PatchageModule(PatchageModule&&) = delete; - PatchageModule& operator=(PatchageModule&&) = delete; - - ~PatchageModule() override; - - void split(); - void join(); - - bool show_menu(GdkEventButton* ev); - void update_menu(); - - PatchagePort* get_port(const PortID& id); - - void load_location(); - void menu_disconnect_all(); - void show_dialog() {} - void store_location(double x, double y); - - SignalDirection type() const { return _type; } - ClientID id() const { return _id; } - const std::string& name() const { return _name; } - -protected: - bool on_event(GdkEvent* ev) override; - - Patchage* _app; - Gtk::Menu* _menu; - std::string _name; - SignalDirection _type; - ClientID _id; -}; - -} // namespace patchage - -#endif // PATCHAGE_PATCHAGEMODULE_HPP diff --git a/src/PatchagePort.hpp b/src/PatchagePort.hpp deleted file mode 100644 index 0ef6752..0000000 --- a/src/PatchagePort.hpp +++ /dev/null @@ -1,125 +0,0 @@ -/* This file is part of Patchage. - * Copyright 2007-2020 David Robillard - * - * Patchage is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * - * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License - * along with Patchage. If not, see . - */ - -#ifndef PATCHAGE_PATCHAGEPORT_HPP -#define PATCHAGE_PATCHAGEPORT_HPP - -#include "Configuration.hpp" -#include "PatchageCanvas.hpp" -#include "PatchageModule.hpp" -#include "PortID.hpp" -#include "warnings.hpp" - -PATCHAGE_DISABLE_GANV_WARNINGS -#include "ganv/Module.hpp" -#include "ganv/Port.hpp" -PATCHAGE_RESTORE_WARNINGS - -#include -#include - -#include - -namespace patchage { - -/// A port on a PatchageModule -class PatchagePort : public Ganv::Port -{ -public: - PatchagePort(Ganv::Module& module, - PortType type, - PortID id, - const std::string& name, - const std::string& human_name, - bool is_input, - uint32_t color, - bool show_human_name, - boost::optional order = boost::optional()) - : Port(module, - (show_human_name && !human_name.empty()) ? human_name : name, - is_input, - color) - , _type(type) - , _id(std::move(id)) - , _name(name) - , _human_name(human_name) - , _order(order) - { - signal_event().connect(sigc::mem_fun(this, &PatchagePort::on_event)); - } - - PatchagePort(const PatchagePort&) = delete; - PatchagePort& operator=(const PatchagePort&) = delete; - - PatchagePort(PatchagePort&&) = delete; - PatchagePort& operator=(PatchagePort&&) = delete; - - ~PatchagePort() override = default; - - /** Returns the name of the module/client this port is one */ - std::string module_name() const - { - auto* pmod = dynamic_cast(get_module()); - return std::string(pmod->name()); - } - - /** Returns the full name of this port, as "modulename:portname" */ - std::string full_name() const - { - auto* pmod = dynamic_cast(get_module()); - return std::string(pmod->name()) + ":" + _name; - } - - void show_human_name(bool human) - { - if (human && !_human_name.empty()) { - set_label(_human_name.c_str()); - } else { - set_label(_name.c_str()); - } - } - - bool on_event(GdkEvent* ev) override - { - if (ev->type != GDK_BUTTON_PRESS || ev->button.button != 3) { - return false; - } - - Gtk::Menu* menu = Gtk::manage(new Gtk::Menu()); - menu->items().push_back(Gtk::Menu_Helpers::MenuElem( - "Disconnect", sigc::mem_fun(this, &Port::disconnect))); - - menu->popup(ev->button.button, ev->button.time); - return true; - } - - PortType type() const { return _type; } - PortID id() const { return _id; } - const std::string& name() const { return _name; } - const std::string& human_name() const { return _human_name; } - const boost::optional& order() const { return _order; } - -private: - PortType _type; - PortID _id; - std::string _name; - std::string _human_name; - boost::optional _order; -}; - -} // namespace patchage - -#endif // PATCHAGE_PATCHAGEPORT_HPP diff --git a/src/event_to_string.cpp b/src/event_to_string.cpp index 58d99ac..321ca4e 100644 --- a/src/event_to_string.cpp +++ b/src/event_to_string.cpp @@ -17,7 +17,7 @@ #include "event_to_string.hpp" #include "ClientType.hpp" -#include "PatchageEvent.hpp" +#include "Event.hpp" #include "warnings.hpp" PATCHAGE_DISABLE_FMT_WARNINGS @@ -138,14 +138,14 @@ struct EventPrinter } // namespace std::string -event_to_string(const PatchageEvent& event) +event_to_string(const Event& event) { EventPrinter printer; return boost::apply_visitor(printer, event); } std::ostream& -operator<<(std::ostream& os, const PatchageEvent& event) +operator<<(std::ostream& os, const Event& event) { return os << event_to_string(event); } diff --git a/src/event_to_string.hpp b/src/event_to_string.hpp index 0795bce..db55733 100644 --- a/src/event_to_string.hpp +++ b/src/event_to_string.hpp @@ -17,7 +17,7 @@ #ifndef PATCHAGE_EVENT_TO_STRING_HPP #define PATCHAGE_EVENT_TO_STRING_HPP -#include "PatchageEvent.hpp" +#include "Event.hpp" #include #include @@ -25,10 +25,10 @@ namespace patchage { std::string -event_to_string(const PatchageEvent& event); +event_to_string(const Event& event); std::ostream& -operator<<(std::ostream& os, const PatchageEvent& event); +operator<<(std::ostream& os, const Event& event); } // namespace patchage diff --git a/src/handle_event.cpp b/src/handle_event.cpp index b6412c2..40f6c22 100644 --- a/src/handle_event.cpp +++ b/src/handle_event.cpp @@ -16,11 +16,11 @@ #include "handle_event.hpp" +#include "Canvas.hpp" +#include "CanvasModule.hpp" +#include "CanvasPort.hpp" +#include "Event.hpp" #include "Patchage.hpp" -#include "PatchageCanvas.hpp" -#include "PatchageEvent.hpp" -#include "PatchageModule.hpp" -#include "PatchagePort.hpp" PATCHAGE_DISABLE_FMT_WARNINGS #include @@ -83,8 +83,8 @@ public: void operator()(const ConnectionEvent& event) { - PatchagePort* port_1 = _patchage.canvas()->find_port(event.tail); - PatchagePort* port_2 = _patchage.canvas()->find_port(event.head); + CanvasPort* port_1 = _patchage.canvas()->find_port(event.tail); + CanvasPort* port_2 = _patchage.canvas()->find_port(event.head); if (!port_1) { _patchage.log().error(fmt::format( @@ -99,8 +99,8 @@ public: void operator()(const DisconnectionEvent& event) { - PatchagePort* port_1 = _patchage.canvas()->find_port(event.tail); - PatchagePort* port_2 = _patchage.canvas()->find_port(event.head); + CanvasPort* port_1 = _patchage.canvas()->find_port(event.tail); + CanvasPort* port_2 = _patchage.canvas()->find_port(event.head); if (!port_1) { _patchage.log().error(fmt::format( @@ -120,7 +120,7 @@ private: } // namespace void -handle_event(Patchage& patchage, const PatchageEvent& event) +handle_event(Patchage& patchage, const Event& event) { EventHandler handler{patchage}; boost::apply_visitor(handler, event); diff --git a/src/handle_event.hpp b/src/handle_event.hpp index 0993db1..975b3bb 100644 --- a/src/handle_event.hpp +++ b/src/handle_event.hpp @@ -17,7 +17,7 @@ #ifndef PATCHAGE_HANDLE_EVENT_HPP #define PATCHAGE_HANDLE_EVENT_HPP -#include "PatchageEvent.hpp" +#include "Event.hpp" namespace patchage { @@ -25,7 +25,7 @@ class Patchage; /// Handle an event in the GUI void -handle_event(Patchage& patchage, const PatchageEvent& event); +handle_event(Patchage& patchage, const Event& event); } // namespace patchage diff --git a/wscript b/wscript index 0ed8339..98b80a5 100644 --- a/wscript +++ b/wscript @@ -236,13 +236,13 @@ def build(bld): uselib = 'DBUS GANV DBUS_GLIB FMT GTKMM GTHREAD GTK_OSX', install_path = '${BINDIR}') prog.source = ''' + src/Canvas.cpp + src/CanvasModule.cpp src/Configuration.cpp src/Connector.cpp src/Legend.cpp src/Metadata.cpp src/Patchage.cpp - src/PatchageCanvas.cpp - src/PatchageModule.cpp src/TextViewLog.cpp src/event_to_string.cpp src/handle_event.cpp -- cgit v1.2.1