diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/AlsaDriver.cpp | 585 | ||||
-rw-r--r-- | src/AlsaDriver.hpp | 116 | ||||
-rw-r--r-- | src/Configuration.cpp | 333 | ||||
-rw-r--r-- | src/Configuration.hpp | 109 | ||||
-rw-r--r-- | src/Driver.hpp | 55 | ||||
-rw-r--r-- | src/JackDbusDriver.cpp | 1048 | ||||
-rw-r--r-- | src/JackDbusDriver.hpp | 161 | ||||
-rw-r--r-- | src/JackDriver.cpp | 588 | ||||
-rw-r--r-- | src/JackDriver.hpp | 109 | ||||
-rw-r--r-- | src/Legend.hpp | 71 | ||||
-rw-r--r-- | src/Patchage.cpp | 1078 | ||||
-rw-r--r-- | src/Patchage.hpp | 212 | ||||
-rw-r--r-- | src/PatchageCanvas.cpp | 338 | ||||
-rw-r--r-- | src/PatchageCanvas.hpp | 85 | ||||
-rw-r--r-- | src/PatchageEvent.cpp | 110 | ||||
-rw-r--r-- | src/PatchageEvent.hpp | 87 | ||||
-rw-r--r-- | src/PatchageModule.cpp | 157 | ||||
-rw-r--r-- | src/PatchageModule.hpp | 67 | ||||
-rw-r--r-- | src/PatchagePort.hpp | 104 | ||||
-rw-r--r-- | src/PortID.hpp | 120 | ||||
-rw-r--r-- | src/Queue.hpp | 131 | ||||
-rw-r--r-- | src/UIFile.hpp | 66 | ||||
-rw-r--r-- | src/Widget.hpp | 46 | ||||
-rw-r--r-- | src/binary_location.h | 54 | ||||
-rw-r--r-- | src/jackey.h | 72 | ||||
-rw-r--r-- | src/main.cpp | 93 | ||||
-rw-r--r-- | src/patchage.gladep | 9 | ||||
l--------- | src/patchage.svg | 1 | ||||
-rw-r--r-- | src/patchage.ui | 1260 |
29 files changed, 7265 insertions, 0 deletions
diff --git a/src/AlsaDriver.cpp b/src/AlsaDriver.cpp new file mode 100644 index 0000000..1ebd12d --- /dev/null +++ b/src/AlsaDriver.cpp @@ -0,0 +1,585 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <cassert> +#include <set> +#include <string> +#include <utility> + +#include <boost/format.hpp> + +#include "AlsaDriver.hpp" +#include "Patchage.hpp" +#include "PatchageCanvas.hpp" +#include "PatchageModule.hpp" +#include "PatchagePort.hpp" + +using std::endl; +using std::string; +using boost::format; + +AlsaDriver::AlsaDriver(Patchage* app) + : _app(app) + , _seq(NULL) +{ +} + +AlsaDriver::~AlsaDriver() +{ + detach(); +} + +/** Attach to ALSA. */ +void +AlsaDriver::attach(bool /*launch_daemon*/) +{ + int ret = snd_seq_open(&_seq, "default", SND_SEQ_OPEN_DUPLEX, 0); + if (ret) { + _app->error_msg("Alsa: Unable to attach."); + _seq = NULL; + } else { + _app->info_msg("Alsa: Attached."); + + snd_seq_set_client_name(_seq, "Patchage"); + + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 50000); + + ret = pthread_create(&_refresh_thread, &attr, &AlsaDriver::refresh_main, this); + if (ret) + _app->error_msg("Alsa: Failed to start refresh thread."); + + signal_attached.emit(); + } +} + +void +AlsaDriver::detach() +{ + if (_seq) { + pthread_cancel(_refresh_thread); + pthread_join(_refresh_thread, NULL); + snd_seq_close(_seq); + _seq = NULL; + signal_detached.emit(); + _app->info_msg("Alsa: Detached."); + } +} + +static bool +is_alsa_port(const PatchagePort* port) +{ + return port->type() == ALSA_MIDI; +} + +/** Destroy all JACK (canvas) ports. + */ +void +AlsaDriver::destroy_all() +{ + _app->canvas()->remove_ports(is_alsa_port); + _modules.clear(); + _port_addrs.clear(); +} + +/** Refresh all Alsa Midi ports and connections. + */ +void +AlsaDriver::refresh() +{ + if (!is_attached()) + return; + + assert(_seq); + + _modules.clear(); + _ignored.clear(); + _port_addrs.clear(); + + snd_seq_client_info_t* cinfo; + snd_seq_client_info_alloca(&cinfo); + snd_seq_client_info_set_client(cinfo, -1); + + snd_seq_port_info_t* pinfo; + snd_seq_port_info_alloca(&pinfo); + + PatchageModule* parent = NULL; + PatchagePort* port = NULL; + + // Create port views + while (snd_seq_query_next_client(_seq, cinfo) >= 0) { + snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo)); + snd_seq_port_info_set_port(pinfo, -1); + while (snd_seq_query_next_port(_seq, pinfo) >= 0) { + const snd_seq_addr_t& addr = *snd_seq_port_info_get_addr(pinfo); + if (ignore(addr)) { + continue; + } + + create_port_view_internal(_app, addr, parent, port); + } + } + + // Create connections + snd_seq_client_info_set_client(cinfo, -1); + while (snd_seq_query_next_client(_seq, cinfo) >= 0) { + snd_seq_port_info_set_client(pinfo, snd_seq_client_info_get_client(cinfo)); + snd_seq_port_info_set_port(pinfo, -1); + while (snd_seq_query_next_port(_seq, pinfo) >= 0) { + const snd_seq_addr_t* addr = snd_seq_port_info_get_addr(pinfo); + if (ignore(*addr)) { + continue; + } + + PatchagePort* port = _app->canvas()->find_port(PortID(*addr, false)); + if (!port) { + continue; + } + + snd_seq_query_subscribe_t* subsinfo; + snd_seq_query_subscribe_alloca(&subsinfo); + snd_seq_query_subscribe_set_root(subsinfo, addr); + snd_seq_query_subscribe_set_index(subsinfo, 0); + while (!snd_seq_query_port_subscribers(_seq, subsinfo)) { + const snd_seq_addr_t* addr2 = snd_seq_query_subscribe_get_addr(subsinfo); + if (addr2) { + const PortID id2(*addr2, true); + PatchagePort* port2 = _app->canvas()->find_port(id2); + if (port2 && !_app->canvas()->get_edge(port, port2)) { + _app->canvas()->make_connection(port, port2); + } + } + + snd_seq_query_subscribe_set_index( + subsinfo, snd_seq_query_subscribe_get_index(subsinfo) + 1); + } + } + } +} + +PatchagePort* +AlsaDriver::create_port_view(Patchage* patchage, + const PortID& id) +{ + PatchageModule* parent = NULL; + PatchagePort* port = NULL; + create_port_view_internal(patchage, id.id.alsa_addr, parent, port); + return port; +} + +PatchageModule* +AlsaDriver::find_module(uint8_t client_id, ModuleType type) +{ + const Modules::const_iterator i = _modules.find(client_id); + if (i == _modules.end()) + return NULL; + + PatchageModule* io_module = NULL; + for (Modules::const_iterator j = i; + j != _modules.end() && j->first == client_id; + ++j) { + if (j->second->type() == type) { + return j->second; + } else if (j->second->type() == InputOutput) { + io_module = j->second; + } + } + + // Return InputOutput module for Input or Output, or NULL if not found + return io_module; +} + +PatchageModule* +AlsaDriver::find_or_create_module( + Patchage* patchage, + uint8_t client_id, + const std::string& client_name, + ModuleType type) +{ + PatchageModule* m = find_module(client_id, type); + if (!m) { + m = new PatchageModule(patchage, client_name, type); + m->load_location(); + _app->canvas()->add_module(client_name, m); + _modules.insert(std::make_pair(client_id, m)); + } + return m; +} + +void +AlsaDriver::create_port_view_internal( + Patchage* patchage, + snd_seq_addr_t addr, + PatchageModule*& m, + PatchagePort*& port) +{ + if (ignore(addr)) + return; + + snd_seq_client_info_t* cinfo; + snd_seq_client_info_alloca(&cinfo); + snd_seq_client_info_set_client(cinfo, addr.client); + snd_seq_get_any_client_info(_seq, addr.client, cinfo); + + snd_seq_port_info_t* pinfo; + snd_seq_port_info_alloca(&pinfo); + snd_seq_port_info_set_client(pinfo, addr.client); + snd_seq_port_info_set_port(pinfo, addr.port); + snd_seq_get_any_port_info(_seq, addr.client, addr.port, pinfo); + + const string client_name = snd_seq_client_info_get_name(cinfo); + const string port_name = snd_seq_port_info_get_name(pinfo); + bool is_input = false; + bool is_duplex = false; + bool is_application = true; + + int caps = snd_seq_port_info_get_capability(pinfo); + int type = snd_seq_port_info_get_type(pinfo); + + // Figure out direction + if ((caps & SND_SEQ_PORT_CAP_READ) && (caps & SND_SEQ_PORT_CAP_WRITE)) + is_duplex = true; + else if (caps & SND_SEQ_PORT_CAP_READ) + is_input = false; + else if (caps & SND_SEQ_PORT_CAP_WRITE) + is_input = true; + + is_application = (type & SND_SEQ_PORT_TYPE_APPLICATION); + + // Because there would be name conflicts, we must force a split if (stupid) + // alsa duplex ports are present on the client + bool split = false; + if (is_duplex) { + split = true; + if (!_app->conf()->get_module_split(client_name, !is_application)) { + _app->conf()->set_module_split(client_name, true); + } + } else { + split = _app->conf()->get_module_split(client_name, !is_application); + } + + /*cout << "ALSA PORT: " << client_name << " : " << port_name + << " is_application = " << is_application + << " is_duplex = " << is_duplex + << " split = " << split << endl;*/ + + if (!split) { + m = find_or_create_module(_app, addr.client, client_name, InputOutput); + if (!m->get_port(port_name)) { + port = create_port(*m, port_name, is_input, addr); + port->show(); + } + + } else { // split + ModuleType type = ((is_input) ? Input : Output); + m = find_or_create_module(_app, addr.client, client_name, type); + if (!m->get_port(port_name)) { + port = create_port(*m, port_name, is_input, addr); + port->show(); + } + + if (is_duplex) { + type = ((!is_input) ? Input : Output); + m = find_or_create_module(_app, addr.client, client_name, type); + if (!m->get_port(port_name)) { + port = create_port(*m, port_name, !is_input, addr); + port->show(); + } + } + } +} + +PatchagePort* +AlsaDriver::create_port(PatchageModule& parent, + const string& name, bool is_input, snd_seq_addr_t addr) +{ + PatchagePort* ret = new PatchagePort( + parent, ALSA_MIDI, name, "", is_input, + _app->conf()->get_port_color(ALSA_MIDI), + _app->show_human_names()); + + dynamic_cast<PatchageCanvas*>(parent.canvas())->index_port( + PortID(addr, is_input), ret); + + _app->canvas()->index_port(PortID(addr, is_input), ret); + _port_addrs.insert(std::make_pair(ret, PortID(addr, is_input))); + return ret; +} + +bool +AlsaDriver::ignore(const snd_seq_addr_t& addr, bool add) +{ + if (_ignored.find(addr) != _ignored.end()) + return true; + + if (!add) + return false; + + snd_seq_client_info_t* cinfo; + snd_seq_client_info_alloca(&cinfo); + snd_seq_client_info_set_client(cinfo, addr.client); + snd_seq_get_any_client_info(_seq, addr.client, cinfo); + + snd_seq_port_info_t* pinfo; + snd_seq_port_info_alloca(&pinfo); + snd_seq_port_info_set_client(pinfo, addr.client); + snd_seq_port_info_set_port(pinfo, addr.port); + snd_seq_get_any_port_info(_seq, addr.client, addr.port, pinfo); + + const int type = snd_seq_port_info_get_type(pinfo); + const int caps = snd_seq_port_info_get_capability(pinfo); + + if (caps & SND_SEQ_PORT_CAP_NO_EXPORT) { + _ignored.insert(addr); + return true; + } else if ( !( (caps & SND_SEQ_PORT_CAP_READ) + || (caps & SND_SEQ_PORT_CAP_WRITE) + || (caps & SND_SEQ_PORT_CAP_DUPLEX))) { + _ignored.insert(addr); + return true; + } else if ((snd_seq_client_info_get_type(cinfo) != SND_SEQ_USER_CLIENT) + && ((type == SND_SEQ_PORT_SYSTEM_TIMER + || type == SND_SEQ_PORT_SYSTEM_ANNOUNCE))) { + _ignored.insert(addr); + return true; + } + + return false; +} + +/** Connects two Alsa Midi ports. + * + * \return Whether connection succeeded. + */ +bool +AlsaDriver::connect(PatchagePort* src_port, + PatchagePort* dst_port) +{ + PortAddrs::const_iterator s = _port_addrs.find(src_port); + PortAddrs::const_iterator d = _port_addrs.find(dst_port); + + if (s == _port_addrs.end() || d == _port_addrs.end()) { + _app->error_msg("Alsa: Attempt to connect port with no address."); + return false; + } + + const PortID src = s->second; + const PortID dst = d->second; + + if (src.id.alsa_addr.client == dst.id.alsa_addr.client + && src.id.alsa_addr.port == dst.id.alsa_addr.port) { + _app->warning_msg("Alsa: Refusing to connect port to itself."); + return false; + } + + bool result = true; + + snd_seq_port_subscribe_t* subs; + snd_seq_port_subscribe_malloc(&subs); + snd_seq_port_subscribe_set_sender(subs, &src.id.alsa_addr); + snd_seq_port_subscribe_set_dest(subs, &dst.id.alsa_addr); + snd_seq_port_subscribe_set_exclusive(subs, 0); + snd_seq_port_subscribe_set_time_update(subs, 0); + snd_seq_port_subscribe_set_time_real(subs, 0); + + // Already connected (shouldn't happen) + if (!snd_seq_get_port_subscription(_seq, subs)) { + _app->error_msg("Alsa: Attempt to double subscribe ports."); + result = false; + } + + int ret = snd_seq_subscribe_port(_seq, subs); + if (ret < 0) { + _app->error_msg((format("Alsa: Subscription failed (%1%).") + % snd_strerror(ret)).str()); + result = false; + } + + if (result) + _app->info_msg(string("Alsa: Connected ") + + src_port->full_name() + " => " + dst_port->full_name()); + else + _app->error_msg(string("Alsa: Unable to connect ") + + src_port->full_name() + " => " + dst_port->full_name()); + + return (!result); +} + +/** Disconnects two Alsa Midi ports. + * + * \return Whether disconnection succeeded. + */ +bool +AlsaDriver::disconnect(PatchagePort* src_port, + PatchagePort* dst_port) +{ + PortAddrs::const_iterator s = _port_addrs.find(src_port); + PortAddrs::const_iterator d = _port_addrs.find(dst_port); + + if (s == _port_addrs.end() || d == _port_addrs.end()) { + _app->error_msg("Alsa: Attempt to connect port with no address"); + return false; + } + + const PortID src = s->second; + const PortID dst = d->second; + + snd_seq_port_subscribe_t* subs; + snd_seq_port_subscribe_malloc(&subs); + snd_seq_port_subscribe_set_sender(subs, &src.id.alsa_addr); + snd_seq_port_subscribe_set_dest(subs, &dst.id.alsa_addr); + snd_seq_port_subscribe_set_exclusive(subs, 0); + snd_seq_port_subscribe_set_time_update(subs, 0); + snd_seq_port_subscribe_set_time_real(subs, 0); + + // Not connected (shouldn't happen) + if (snd_seq_get_port_subscription(_seq, subs) != 0) { + _app->error_msg("Alsa: Attempt to unsubscribe ports that are not subscribed."); + return false; + } + + int ret = snd_seq_unsubscribe_port(_seq, subs); + if (ret < 0) { + _app->error_msg(string("Alsa: Unable to disconnect ") + + src_port->full_name() + " => " + dst_port->full_name() + + "(" + snd_strerror(ret) + ")"); + return false; + } + + _app->info_msg(string("Alsa: Disconnected ") + + src_port->full_name() + " => " + dst_port->full_name()); + + return true; +} + +bool +AlsaDriver::create_refresh_port() +{ + snd_seq_port_info_t* port_info; + snd_seq_port_info_alloca(&port_info); + snd_seq_port_info_set_name(port_info, "System Announcement Reciever"); + snd_seq_port_info_set_type(port_info, SND_SEQ_PORT_TYPE_APPLICATION); + snd_seq_port_info_set_capability(port_info, + SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE|SND_SEQ_PORT_CAP_NO_EXPORT); + + int ret = snd_seq_create_port(_seq, port_info); + if (ret) { + _app->error_msg((format("Alsa: Error creating port (%1%): ") + % snd_strerror(ret)).str()); + return false; + } + + // Subscribe the port to the system announcer + ret = snd_seq_connect_from(_seq, + snd_seq_port_info_get_port(port_info), + SND_SEQ_CLIENT_SYSTEM, + SND_SEQ_PORT_SYSTEM_ANNOUNCE); + if (ret) { + _app->error_msg((format("Alsa: Failed to connect to system announce port (%1%)") + % snd_strerror(ret)).str()); + return false; + } + + return true; +} + +void* +AlsaDriver::refresh_main(void* me) +{ + AlsaDriver* ad = (AlsaDriver*)me; + ad->_refresh_main(); + return NULL; +} + +void +AlsaDriver::_refresh_main() +{ + if (!create_refresh_port()) { + _app->error_msg("Alsa: Could not create listen port, auto-refresh disabled."); + return; + } + + int caps = 0; + + snd_seq_client_info_t* cinfo; + snd_seq_client_info_alloca(&cinfo); + + snd_seq_port_info_t* pinfo; + snd_seq_port_info_alloca(&pinfo); + + snd_seq_event_t* ev; + while (snd_seq_event_input(_seq, &ev) > 0) { + assert(ev); + + Glib::Mutex::Lock lock(_events_mutex); + + switch (ev->type) { + case SND_SEQ_EVENT_PORT_SUBSCRIBED: + if (!ignore(ev->data.connect.sender) && !ignore(ev->data.connect.dest)) + _events.push(PatchageEvent(PatchageEvent::CONNECTION, + ev->data.connect.sender, ev->data.connect.dest)); + break; + case SND_SEQ_EVENT_PORT_UNSUBSCRIBED: + if (!ignore(ev->data.connect.sender) && !ignore(ev->data.connect.dest)) + _events.push(PatchageEvent(PatchageEvent::DISCONNECTION, + ev->data.connect.sender, ev->data.connect.dest)); + break; + case SND_SEQ_EVENT_PORT_START: + snd_seq_get_any_client_info(_seq, ev->data.addr.client, cinfo); + snd_seq_get_any_port_info(_seq, ev->data.addr.client, ev->data.addr.port, pinfo); + caps = snd_seq_port_info_get_capability(pinfo); + + if (!ignore(ev->data.addr)) + _events.push(PatchageEvent(PatchageEvent::PORT_CREATION, + PortID(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 (in case this is a duplex port) + _events.push(PatchageEvent(PatchageEvent::PORT_DESTRUCTION, + PortID(ev->data.addr, true))); + _events.push(PatchageEvent(PatchageEvent::PORT_DESTRUCTION, + PortID(ev->data.addr, false))); + _port_addrs.erase(_app->canvas()->find_port( + PortID(ev->data.addr, false))); + _port_addrs.erase(_app->canvas()->find_port( + PortID(ev->data.addr, true))); + } + break; + case SND_SEQ_EVENT_CLIENT_CHANGE: + case SND_SEQ_EVENT_CLIENT_EXIT: + case SND_SEQ_EVENT_CLIENT_START: + case SND_SEQ_EVENT_PORT_CHANGE: + case SND_SEQ_EVENT_RESET: + default: + //_events.push(PatchageEvent(PatchageEvent::REFRESH)); + break; + } + } +} + +void +AlsaDriver::process_events(Patchage* app) +{ + Glib::Mutex::Lock lock(_events_mutex); + while (!_events.empty()) { + PatchageEvent& ev = _events.front(); + ev.execute(app); + _events.pop(); + } +} diff --git a/src/AlsaDriver.hpp b/src/AlsaDriver.hpp new file mode 100644 index 0000000..8bf837a --- /dev/null +++ b/src/AlsaDriver.hpp @@ -0,0 +1,116 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_ALSADRIVER_HPP +#define PATCHAGE_ALSADRIVER_HPP + +#include <queue> +#include <set> +#include <string> +#include <map> + +#include <alsa/asoundlib.h> +#include <pthread.h> + +#include "Driver.hpp" +#include "PatchageModule.hpp" + +class Patchage; +class PatchagePort; + +/** Handles all externally driven functionality, registering ports etc. + */ +class AlsaDriver : public Driver +{ +public: + explicit AlsaDriver(Patchage* app); + ~AlsaDriver(); + + void attach(bool launch_daemon = false); + void detach(); + + bool is_attached() const { return (_seq != NULL); } + + void refresh(); + void destroy_all(); + + PatchagePort* create_port_view( + Patchage* patchage, + const PortID& id); + + bool connect(PatchagePort* src_port, + PatchagePort* dst_port); + + bool disconnect(PatchagePort* src_port, + PatchagePort* dst_port); + + void print_addr(snd_seq_addr_t addr); + + void process_events(Patchage* app); + +private: + bool create_refresh_port(); + static void* refresh_main(void* me); + void _refresh_main(); + + PatchageModule* find_module(uint8_t client_id, ModuleType type); + + PatchageModule* + find_or_create_module( + Patchage* patchage, + uint8_t client_id, + const std::string& client_name, + ModuleType type); + + void + create_port_view_internal( + Patchage* patchage, + snd_seq_addr_t addr, + PatchageModule*& parent, + PatchagePort*& port); + + PatchagePort* create_port( + PatchageModule& parent, + const std::string& name, + bool is_input, + snd_seq_addr_t addr); + + Patchage* _app; + snd_seq_t* _seq; + pthread_t _refresh_thread; + + Glib::Mutex _events_mutex; + std::queue<PatchageEvent> _events; + + struct SeqAddrComparator { + bool operator() (const snd_seq_addr_t& a, const snd_seq_addr_t& b) const { + return ((a.client < b.client) || ((a.client == b.client) && a.port < b.port)); + } + }; + + typedef std::set<snd_seq_addr_t, SeqAddrComparator> Ignored; + Ignored _ignored; + + typedef std::multimap<uint8_t, PatchageModule*> Modules; + Modules _modules; + + typedef std::map<PatchagePort*, PortID> PortAddrs; + PortAddrs _port_addrs; + + bool ignore(const snd_seq_addr_t& addr, bool add=true); +}; + +#endif // PATCHAGE_ALSADRIVER_HPP diff --git a/src/Configuration.cpp b/src/Configuration.cpp new file mode 100644 index 0000000..d9537c0 --- /dev/null +++ b/src/Configuration.cpp @@ -0,0 +1,333 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <ctype.h> +#include <stdlib.h> + +#include <fstream> +#include <ios> +#include <iostream> +#include <limits> +#include <stdexcept> +#include <vector> + +#include "Configuration.hpp" +#include "Patchage.hpp" + +static const char* port_type_names[N_PORT_TYPES] = { + "JACK_AUDIO", + "JACK_MIDI", + "ALSA_MIDI", + "JACK_OSC", + "JACK_CV" +}; + +Configuration::Configuration() + : _window_location(0, 0) + , _window_size(640, 480) + , _zoom(1.0) + , _font_size(12.0) + , _messages_height(0) + , _show_toolbar(true) + , _show_messages(false) + , _sort_ports(true) +{ +#ifdef PATCHAGE_USE_LIGHT_THEME + _port_colors[JACK_AUDIO] = _default_port_colors[JACK_AUDIO] = 0xA4BC8CFF; + _port_colors[JACK_MIDI] = _default_port_colors[JACK_MIDI] = 0xC89595FF; + _port_colors[ALSA_MIDI] = _default_port_colors[ALSA_MIDI] = 0x8F7198FF; + _port_colors[JACK_OSC] = _default_port_colors[JACK_OSC] = 0x7E8EAAFF; + _port_colors[JACK_CV] = _default_port_colors[JACK_CV] = 0x83AFABFF; +#else + _port_colors[JACK_AUDIO] = _default_port_colors[JACK_AUDIO] = 0x3E5E00FF; + _port_colors[JACK_MIDI] = _default_port_colors[JACK_MIDI] = 0x650300FF; + _port_colors[ALSA_MIDI] = _default_port_colors[ALSA_MIDI] = 0x2D0043FF; + _port_colors[JACK_OSC] = _default_port_colors[JACK_OSC] = 0x4100FEFF; + _port_colors[JACK_CV] = _default_port_colors[JACK_CV] = 0x005E4EFF; +#endif +} + +bool +Configuration::get_module_location(const std::string& name, ModuleType type, Coord& loc) +{ + std::map<std::string, ModuleSettings>::const_iterator i = _module_settings.find(name); + if (i == _module_settings.end()) { + return false; + } + + const ModuleSettings& settings = (*i).second; + if (type == Input && settings.input_location) { + loc = *settings.input_location; + } else if (type == Output && settings.output_location) { + loc = *settings.output_location; + } else if (type == InputOutput && settings.inout_location) { + loc = *settings.inout_location; + } else { + return false; + } + + return true; +} + +void +Configuration::set_module_location(const std::string& name, ModuleType type, Coord loc) +{ + std::map<std::string, ModuleSettings>::iterator i = _module_settings.find(name); + if (i == _module_settings.end()) { + i = _module_settings.insert( + std::make_pair(name, ModuleSettings(type != InputOutput))).first; + } + + ModuleSettings& settings = (*i).second; + switch (type) { + case Input: + settings.input_location = loc; + break; + case Output: + settings.output_location = loc; + break; + case InputOutput: + settings.inout_location = loc; + break; + default: + break; // shouldn't reach here + } +} + +/** Returns whether or not this module should be split. + * + * If nothing is known about the given module, `default_val` is returned (this is + * to allow driver's to request terminal ports get split by default). + */ +bool +Configuration::get_module_split(const std::string& name, bool default_val) const +{ + std::map<std::string, ModuleSettings>::const_iterator i = _module_settings.find(name); + if (i == _module_settings.end()) { + return default_val; + } + + return (*i).second.split; +} + +void +Configuration::set_module_split(const std::string& name, bool split) +{ + _module_settings[name].split = split; +} + +/** Return a vector of filenames in descending order by preference. */ +static std::vector<std::string> +get_filenames() +{ + std::vector<std::string> filenames; + std::string prefix; + + const char* xdg_config_home = getenv("XDG_CONFIG_HOME"); + const char* home = getenv("HOME"); + + // XDG spec + if (xdg_config_home) { + filenames.push_back(std::string(xdg_config_home) + "/patchagerc"); + } else if (home) { + filenames.push_back(std::string(home) + "/.config/patchagerc"); + } + + // Old location + if (home) { + filenames.push_back(std::string(home) + "/.patchagerc"); + } + + // Current directory (bundle or last-ditch effort) + filenames.push_back("patchagerc"); + + return filenames; +} + +void +Configuration::load() +{ + // Try to find a readable configuration file + const std::vector<std::string> filenames = get_filenames(); + std::ifstream file; + for (size_t i = 0; i < filenames.size(); ++i) { + file.open(filenames[i].c_str(), std::ios::in); + if (file.good()) { + std::cout << "Loading configuration from " << filenames[i] << std::endl; + break; + } + } + + if (!file.good()) { + std::cout << "No configuration file present" << std::endl; + return; + } + + _module_settings.clear(); + while (file.good()) { + std::string key; + if (file.peek() == '\"') { + /* Old versions omitted the module_position key and listed + positions starting with module name in quotes. */ + key = "module_position"; + } else { + file >> key; + } + + if (key == "window_location") { + file >> _window_location.x >> _window_location.y; + } else if (key == "window_size") { + file >> _window_size.x >> _window_size.y; + } else if (key == "zoom_level") { + file >> _zoom; + } else if (key == "font_size") { + file >> _font_size; + } else if (key == "show_toolbar") { + file >> _show_toolbar; + } else if (key == "sprung_layout") { + file >> _sprung_layout; + } else if (key == "show_messages") { + file >> _show_messages; + } else if (key == "sort_ports") { + file >> _sort_ports; + } else if (key == "messages_height") { + file >> _messages_height; + } else if (key == "port_color") { + std::string type_name; + uint32_t rgba; + file >> type_name; + file.ignore(1, '#'); + file >> std::hex >> std::uppercase; + file >> rgba; + file >> std::dec >> std::nouppercase; + + bool found = false; + for (int i = 0; i < N_PORT_TYPES; ++i) { + if (type_name == port_type_names[i]) { + _port_colors[i] = rgba; + found = true; + break; + } + } + if (!found) { + std::cerr << "error: color for unknown port type `" + << type_name << "'" << std::endl; + } + } else if (key == "module_position" || key[0] == '\"') { + Coord loc; + std::string name; + file.ignore(1, '\"'); + std::getline(file, name, '\"'); + + ModuleType type; + std::string type_str; + file >> type_str; + if (type_str == "input") { + type = Input; + } else if (type_str == "output") { + type = Output; + } else if (type_str == "inputoutput") { + type = InputOutput; + } else { + std::cerr << "error: bad position type `" << type_str + << "' for module `" << name << "'" << std::endl; + file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + continue; + } + + file >> loc.x; + file >> loc.y; + + set_module_location(name, type, loc); + } else { + std::cerr << "warning: unknown configuration key `" << key << "'" + << std::endl; + file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + } + + // Skip trailing whitespace, including newline + while (file.good() && isspace(file.peek())) { + file.ignore(1); + } + } + + file.close(); +} + +static inline void +write_module_position(std::ofstream& os, + const std::string& name, + const char* type, + const Coord& loc) +{ + os << "module_position \"" << name << "\"" + << " " << type << " " << loc.x << " " << loc.y << std::endl; +} + +void +Configuration::save() +{ + // Try to find a writable configuration file + const std::vector<std::string> filenames = get_filenames(); + std::ofstream file; + for (size_t i = 0; i < filenames.size(); ++i) { + file.open(filenames[i].c_str(), std::ios::out); + if (file.good()) { + std::cout << "Writing configuration to " << filenames[i] << std::endl; + break; + } + } + + if (!file.good()) { + std::cout << "Unable to open configuration file to write" << std::endl; + return; + } + + file << "window_location " << _window_location.x << " " << _window_location.y << std::endl; + file << "window_size " << _window_size.x << " " << _window_size.y << std::endl; + file << "zoom_level " << _zoom << std::endl; + file << "font_size " << _font_size << std::endl; + file << "show_toolbar " << _show_toolbar << std::endl; + file << "sprung_layout " << _sprung_layout << std::endl; + file << "show_messages " << _show_messages << std::endl; + file << "sort_ports " << _sort_ports << std::endl; + file << "messages_height " << _messages_height << std::endl; + + file << std::hex << std::uppercase; + for (int i = 0; i < N_PORT_TYPES; ++i) { + if (_port_colors[i] != _default_port_colors[i]) { + file << "port_color " << port_type_names[i] << " " << _port_colors[i] << std::endl; + } + } + file << std::dec << std::nouppercase; + + for (std::map<std::string, ModuleSettings>::iterator i = _module_settings.begin(); + i != _module_settings.end(); ++i) { + const ModuleSettings& settings = (*i).second; + const std::string& name = (*i).first; + + if (settings.split) { + if (settings.input_location && settings.output_location) { + write_module_position(file, name, "input", *settings.input_location); + write_module_position(file, name, "output", *settings.output_location); + } + } else if (settings.inout_location) { + write_module_position(file, name, "inputoutput", *settings.inout_location); + } + } + + file.close(); +} diff --git a/src/Configuration.hpp b/src/Configuration.hpp new file mode 100644 index 0000000..127a4a8 --- /dev/null +++ b/src/Configuration.hpp @@ -0,0 +1,109 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_CONFIGURATION_HPP +#define PATCHAGE_CONFIGURATION_HPP + +#include <stdint.h> + +#include <string> +#include <list> +#include <map> + +#include <boost/optional.hpp> + +enum ModuleType { Input, Output, InputOutput }; + +enum PortType { JACK_AUDIO, JACK_MIDI, ALSA_MIDI, JACK_OSC, JACK_CV }; + +#define N_PORT_TYPES 5 + +struct Coord { + Coord(double x_=0, double y_=0) : x(x_), y(y_) {} + double x; + double y; +}; + +class Configuration +{ +public: + Configuration(); + + void load(); + void save(); + + bool get_module_location(const std::string& name, ModuleType type, Coord& loc); + void set_module_location(const std::string& name, ModuleType type, Coord loc); + + void set_module_split(const std::string& name, bool split); + bool get_module_split(const std::string& name, bool default_val) const; + + float get_zoom() const { return _zoom; } + void set_zoom(float zoom) { _zoom = zoom; } + float get_font_size() const { return _font_size; } + void set_font_size(float font_size) { _font_size = font_size; } + + float get_show_toolbar() const { return _show_toolbar; } + void set_show_toolbar(float show_toolbar) { _show_toolbar = show_toolbar; } + + float get_sprung_layout() const { return _sprung_layout; } + void set_sprung_layout(float sprung_layout) { _sprung_layout = sprung_layout; } + + bool get_show_messages() const { return _show_messages; } + void set_show_messages(bool show_messages) { _show_messages = show_messages; } + + bool get_sort_ports() const { return _sort_ports; } + void set_sort_ports(bool sort_ports) { _sort_ports = sort_ports; } + + int get_messages_height() const { return _messages_height; } + void set_messages_height(int height) { _messages_height = height; } + + uint32_t get_port_color(PortType type) const { return _port_colors[type]; } + void set_port_color(PortType type, uint32_t rgba) { + _port_colors[type] = rgba; + } + + Coord get_window_location() { return _window_location; } + void set_window_location(Coord loc) { _window_location = loc; } + Coord get_window_size() { return _window_size; } + void set_window_size(Coord size) { _window_size = size; } + +private: + struct ModuleSettings { + ModuleSettings(bool s=false) : split(s) {} + boost::optional<Coord> input_location; + boost::optional<Coord> output_location; + boost::optional<Coord> inout_location; + bool split; + }; + + std::map<std::string, ModuleSettings> _module_settings; + + uint32_t _default_port_colors[N_PORT_TYPES]; + uint32_t _port_colors[N_PORT_TYPES]; + + Coord _window_location; + Coord _window_size; + float _zoom; + float _font_size; + int _messages_height; + bool _show_toolbar; + bool _sprung_layout; + bool _show_messages; + bool _sort_ports; +}; + +#endif // PATCHAGE_CONFIGURATION_HPP diff --git a/src/Driver.hpp b/src/Driver.hpp new file mode 100644 index 0000000..3837382 --- /dev/null +++ b/src/Driver.hpp @@ -0,0 +1,55 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_DRIVER_HPP +#define PATCHAGE_DRIVER_HPP + +#include <boost/shared_ptr.hpp> +#include <sigc++/sigc++.h> + +#include "PatchageEvent.hpp" + +class PatchagePort; +class PatchageCanvas; + +/** Trival driver base class */ +class Driver { +public: + virtual ~Driver() {} + + virtual void process_events(Patchage* app) = 0; + + virtual void attach(bool launch_daemon) = 0; + virtual void detach() = 0; + virtual bool is_attached() const = 0; + + virtual void refresh() = 0; + virtual void destroy_all() {} + + virtual PatchagePort* create_port_view(Patchage* patchage, + const PortID& id) = 0; + + virtual bool connect(PatchagePort* src_port, + PatchagePort* dst_port) = 0; + + virtual bool disconnect(PatchagePort* src_port, + PatchagePort* dst_port) = 0; + + sigc::signal<void> signal_attached; + sigc::signal<void> signal_detached; +}; + +#endif // PATCHAGE_DRIVER_HPP diff --git a/src/JackDbusDriver.cpp b/src/JackDbusDriver.cpp new file mode 100644 index 0000000..7953051 --- /dev/null +++ b/src/JackDbusDriver.cpp @@ -0,0 +1,1048 @@ +/* This file is part of Patchage. + * Copyright 2008 Nedko Arnaudov <nedko@arnaudov.name> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <cassert> +#include <cstring> +#include <string> +#include <set> + +#include "patchage_config.h" + +#include <glib.h> +#include <dbus/dbus.h> +#include <dbus/dbus-glib.h> +#include <dbus/dbus-glib-lowlevel.h> + +#include <boost/format.hpp> + +#include "Driver.hpp" +#include "JackDbusDriver.hpp" +#include "Patchage.hpp" +#include "PatchageCanvas.hpp" +#include "PatchageEvent.hpp" +#include "PatchageModule.hpp" + +#define JACKDBUS_SERVICE "org.jackaudio.service" +#define JACKDBUS_OBJECT "/org/jackaudio/Controller" +#define JACKDBUS_IFACE_CONTROL "org.jackaudio.JackControl" +#define JACKDBUS_IFACE_PATCHBAY "org.jackaudio.JackPatchbay" + +#define JACKDBUS_CALL_DEFAULT_TIMEOUT 1000 // in milliseconds + +#define JACKDBUS_PORT_FLAG_INPUT 0x00000001 +#define JACKDBUS_PORT_FLAG_OUTPUT 0x00000002 +#define JACKDBUS_PORT_FLAG_PHYSICAL 0x00000004 +#define JACKDBUS_PORT_FLAG_CAN_MONITOR 0x00000008 +#define JACKDBUS_PORT_FLAG_TERMINAL 0x00000010 + +#define JACKDBUS_PORT_TYPE_AUDIO 0 +#define JACKDBUS_PORT_TYPE_MIDI 1 + +//#define USE_FULL_REFRESH + +JackDriver::JackDriver(Patchage* app) + : _app(app) + , _dbus_connection(0) + , _max_dsp_load(0) + , _server_responding(false) + , _server_started(false) + , _graph_version(0) +{ + dbus_error_init(&_dbus_error); +} + +JackDriver::~JackDriver() +{ + if (_dbus_connection) { + dbus_connection_flush(_dbus_connection); + } + + if (dbus_error_is_set(&_dbus_error)) { + dbus_error_free(&_dbus_error); + } +} + +static bool +is_jack_port(const PatchagePort* port) +{ + return port->type() == JACK_AUDIO || port->type() == JACK_MIDI; +} + +/** Destroy all JACK (canvas) ports. + */ +void +JackDriver::destroy_all() +{ + _app->canvas()->remove_ports(is_jack_port); +} + +void +JackDriver::update_attached() +{ + bool was_attached = _server_started; + _server_started = is_started(); + + if (!_server_responding) { + if (was_attached) { + signal_detached.emit(); + } + return; + } + + if (_server_started && !was_attached) { + signal_attached.emit(); + return; + } + + if (!_server_started && was_attached) { + signal_detached.emit(); + return; + } +} + +void +JackDriver::on_jack_appeared() +{ + info_msg("JACK appeared."); + update_attached(); +} + +void +JackDriver::on_jack_disappeared() +{ + info_msg("JACK disappeared."); + + // we are not calling update_attached() here, because it will activate jackdbus + + _server_responding = false; + + if (_server_started) { + signal_detached.emit(); + } + + _server_started = false; +} + +/** Handle signals we have subscribed for in attach(). */ +DBusHandlerResult +JackDriver::dbus_message_hook(DBusConnection* connection, + DBusMessage* message, + void* jack_driver) +{ + const char* client2_name; + const char* client_name; + const char* new_owner; + const char* object_name; + const char* old_owner; + const char* port2_name; + const char* port_name; + dbus_uint32_t port_flags; + dbus_uint32_t port_type; + dbus_uint64_t client2_id; + dbus_uint64_t client_id; + dbus_uint64_t connection_id; + dbus_uint64_t new_graph_version; + dbus_uint64_t port2_id; + dbus_uint64_t port_id; + + assert(jack_driver); + JackDriver* me = reinterpret_cast<JackDriver*>(jack_driver); + assert(me->_dbus_connection); + + if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) { + if (!dbus_message_get_args( message, &me->_dbus_error, + DBUS_TYPE_STRING, &object_name, + DBUS_TYPE_STRING, &old_owner, + DBUS_TYPE_STRING, &new_owner, + DBUS_TYPE_INVALID)) { + me->error_msg(str(boost::format("dbus_message_get_args() failed to extract " + "NameOwnerChanged signal arguments (%s)") % me->_dbus_error.message)); + dbus_error_free(&me->_dbus_error); + return DBUS_HANDLER_RESULT_HANDLED; + } + + if (old_owner[0] == '\0') { + me->on_jack_appeared(); + } else if (new_owner[0] == '\0') { + me->on_jack_disappeared(); + } + } + +#if defined(USE_FULL_REFRESH) + if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "GraphChanged")) { + if (!dbus_message_get_args(message, &me->_dbus_error, + DBUS_TYPE_UINT64, &new_graph_version, + DBUS_TYPE_INVALID)) { + me->error_msg(str(boost::format("dbus_message_get_args() failed to extract " + "GraphChanged signal arguments (%s)") % me->_dbus_error.message)); + dbus_error_free(&me->_dbus_error); + return DBUS_HANDLER_RESULT_HANDLED; + } + + if (!me->_server_started) { + me->_server_started = true; + me->signal_attached.emit(); + } + + if (new_graph_version > me->_graph_version) { + me->refresh_internal(false); + } + + return DBUS_HANDLER_RESULT_HANDLED; + } +#else +// if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "ClientAppeared")) { +// me->info_msg("ClientAppeared"); +// return DBUS_HANDLER_RESULT_HANDLED; +// } + +// if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "ClientDisappeared")) { +// me->info_msg("ClientDisappeared"); +// return DBUS_HANDLER_RESULT_HANDLED; +// } + + if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "PortAppeared")) { + if (!dbus_message_get_args( message, &me->_dbus_error, + DBUS_TYPE_UINT64, &new_graph_version, + DBUS_TYPE_UINT64, &client_id, + DBUS_TYPE_STRING, &client_name, + DBUS_TYPE_UINT64, &port_id, + DBUS_TYPE_STRING, &port_name, + DBUS_TYPE_UINT32, &port_flags, + DBUS_TYPE_UINT32, &port_type, + DBUS_TYPE_INVALID)) { + me->error_msg(str(boost::format("dbus_message_get_args() failed to extract " + "PortAppeared signal arguments (%s)") % me->_dbus_error.message)); + dbus_error_free(&me->_dbus_error); + return DBUS_HANDLER_RESULT_HANDLED; + } + + //me->info_msg(str(boost::format("PortAppeared, %s(%llu):%s(%llu), %lu, %lu") % client_name % client_id % port_name % port_id % port_flags % port_type)); + + if (!me->_server_started) { + me->_server_started = true; + me->signal_attached.emit(); + } + + me->add_port(client_id, client_name, port_id, port_name, port_flags, port_type); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "PortDisappeared")) { + if (!dbus_message_get_args( message, &me->_dbus_error, + DBUS_TYPE_UINT64, &new_graph_version, + DBUS_TYPE_UINT64, &client_id, + DBUS_TYPE_STRING, &client_name, + DBUS_TYPE_UINT64, &port_id, + DBUS_TYPE_STRING, &port_name, + DBUS_TYPE_INVALID)) { + me->error_msg(str(boost::format("dbus_message_get_args() failed to extract " + "PortDisappeared signal arguments (%s)") % me->_dbus_error.message)); + dbus_error_free(&me->_dbus_error); + return DBUS_HANDLER_RESULT_HANDLED; + } + + //me->info_msg(str(boost::format("PortDisappeared, %s(%llu):%s(%llu)") % client_name % client_id % port_name % port_id)); + + if (!me->_server_started) { + me->_server_started = true; + me->signal_attached.emit(); + } + + me->remove_port(client_id, client_name, port_id, port_name); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "PortsConnected")) { + if (!dbus_message_get_args(message, &me->_dbus_error, + DBUS_TYPE_UINT64, &new_graph_version, + DBUS_TYPE_UINT64, &client_id, + DBUS_TYPE_STRING, &client_name, + DBUS_TYPE_UINT64, &port_id, + DBUS_TYPE_STRING, &port_name, + DBUS_TYPE_UINT64, &client2_id, + DBUS_TYPE_STRING, &client2_name, + DBUS_TYPE_UINT64, &port2_id, + DBUS_TYPE_STRING, &port2_name, + DBUS_TYPE_UINT64, &connection_id, + DBUS_TYPE_INVALID)) { + me->error_msg(str(boost::format("dbus_message_get_args() failed to extract " + "PortsConnected signal arguments (%s)") % me->_dbus_error.message)); + dbus_error_free(&me->_dbus_error); + return DBUS_HANDLER_RESULT_HANDLED; + } + + if (!me->_server_started) { + me->_server_started = true; + me->signal_attached.emit(); + } + + me->connect_ports( + connection_id, + client_id, client_name, + port_id, port_name, + client2_id, client2_name, + port2_id, port2_name); + + return DBUS_HANDLER_RESULT_HANDLED; + } + + if (dbus_message_is_signal(message, JACKDBUS_IFACE_PATCHBAY, "PortsDisconnected")) { + if (!dbus_message_get_args(message, &me->_dbus_error, + DBUS_TYPE_UINT64, &new_graph_version, + DBUS_TYPE_UINT64, &client_id, + DBUS_TYPE_STRING, &client_name, + DBUS_TYPE_UINT64, &port_id, + DBUS_TYPE_STRING, &port_name, + DBUS_TYPE_UINT64, &client2_id, + DBUS_TYPE_STRING, &client2_name, + DBUS_TYPE_UINT64, &port2_id, + DBUS_TYPE_STRING, &port2_name, + DBUS_TYPE_UINT64, &connection_id, + DBUS_TYPE_INVALID)) { + me->error_msg(str(boost::format("dbus_message_get_args() failed to extract " + "PortsConnected signal arguments (%s)") % me->_dbus_error.message)); + dbus_error_free(&me->_dbus_error); + return DBUS_HANDLER_RESULT_HANDLED; + } + + if (!me->_server_started) { + me->_server_started = true; + me->signal_attached.emit(); + } + + me->disconnect_ports( + connection_id, + client_id, client_name, + port_id, port_name, + client2_id, client2_name, + port2_id, port2_name); + + return DBUS_HANDLER_RESULT_HANDLED; + } +#endif + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} + +bool +JackDriver::call( + bool response_expected, + const char* iface, + const char* method, + DBusMessage** reply_ptr_ptr, + int in_type, ...) +{ + DBusMessage* request_ptr; + DBusMessage* reply_ptr; + va_list ap; + + request_ptr = dbus_message_new_method_call( + JACKDBUS_SERVICE, + JACKDBUS_OBJECT, + iface, + method); + if (!request_ptr) { + throw std::runtime_error("dbus_message_new_method_call() returned 0"); + } + + va_start(ap, in_type); + + dbus_message_append_args_valist(request_ptr, in_type, ap); + + va_end(ap); + + // send message and get a handle for a reply + reply_ptr = dbus_connection_send_with_reply_and_block(_dbus_connection, request_ptr, + JACKDBUS_CALL_DEFAULT_TIMEOUT, &_dbus_error); + + dbus_message_unref(request_ptr); + + if (!reply_ptr) { + if (response_expected) { + error_msg(str(boost::format("no reply from server when calling method '%s'" + ", error is '%s'") % method % _dbus_error.message)); + } + _server_responding = false; + dbus_error_free(&_dbus_error); + } else { + _server_responding = true; + *reply_ptr_ptr = reply_ptr; + } + + return reply_ptr; +} + +bool +JackDriver::is_started() +{ + DBusMessage* reply_ptr; + dbus_bool_t started; + + if (!call(false, JACKDBUS_IFACE_CONTROL, "IsStarted", &reply_ptr, DBUS_TYPE_INVALID)) { + return false; + } + + if (!dbus_message_get_args(reply_ptr, &_dbus_error, + DBUS_TYPE_BOOLEAN, &started, + DBUS_TYPE_INVALID)) { + dbus_message_unref(reply_ptr); + dbus_error_free(&_dbus_error); + error_msg("decoding reply of IsStarted failed."); + return false; + } + + dbus_message_unref(reply_ptr); + + return started; +} + +void +JackDriver::start_server() +{ + DBusMessage* reply_ptr; + + if (!call(false, JACKDBUS_IFACE_CONTROL, "StartServer", &reply_ptr, DBUS_TYPE_INVALID)) { + return; + } + + dbus_message_unref(reply_ptr); + + update_attached(); +} + +void +JackDriver::stop_server() +{ + DBusMessage* reply_ptr; + + if (!call(false, JACKDBUS_IFACE_CONTROL, "StopServer", &reply_ptr, DBUS_TYPE_INVALID)) { + return; + } + + dbus_message_unref(reply_ptr); + + if (!_server_started) { + _server_started = false; + signal_detached.emit(); + } +} + +void +JackDriver::attach(bool launch_daemon) +{ + // Connect to the bus + _dbus_connection = dbus_bus_get(DBUS_BUS_SESSION, &_dbus_error); + if (dbus_error_is_set(&_dbus_error)) { + error_msg("dbus_bus_get() failed"); + error_msg(_dbus_error.message); + dbus_error_free(&_dbus_error); + return; + } + + dbus_connection_setup_with_g_main(_dbus_connection, NULL); + + dbus_bus_add_match(_dbus_connection, "type='signal',interface='" DBUS_INTERFACE_DBUS "',member=NameOwnerChanged,arg0='org.jackaudio.service'", NULL); +#if defined(USE_FULL_REFRESH) + dbus_bus_add_match(_dbus_connection, "type='signal',interface='" JACKDBUS_IFACE_PATCHBAY "',member=GraphChanged", NULL); +#else + // dbus_bus_add_match(_dbus_connection, "type='signal',interface='" JACKDBUS_IFACE_PATCHBAY "',member=ClientAppeared", NULL); + // dbus_bus_add_match(_dbus_connection, "type='signal',interface='" JACKDBUS_IFACE_PATCHBAY "',member=ClientDisappeared", NULL); + dbus_bus_add_match(_dbus_connection, "type='signal',interface='" JACKDBUS_IFACE_PATCHBAY "',member=PortAppeared", NULL); + dbus_bus_add_match(_dbus_connection, "type='signal',interface='" JACKDBUS_IFACE_PATCHBAY "',member=PortDisappeared", NULL); + dbus_bus_add_match(_dbus_connection, "type='signal',interface='" JACKDBUS_IFACE_PATCHBAY "',member=PortsConnected", NULL); + dbus_bus_add_match(_dbus_connection, "type='signal',interface='" JACKDBUS_IFACE_PATCHBAY "',member=PortsDisconnected", NULL); +#endif + dbus_connection_add_filter(_dbus_connection, dbus_message_hook, this, NULL); + + update_attached(); + + if (!_server_responding) { + return; + } + + if (launch_daemon) { + start_server(); + } +} + +void +JackDriver::detach() +{ + stop_server(); +} + +bool +JackDriver::is_attached() const +{ + return _dbus_connection && _server_responding; +} + +void +JackDriver::add_port(PatchageModule* module, + PortType type, + const std::string& name, + bool is_input) +{ + if (module->get_port(name)) { + return; + } + + new PatchagePort( + *module, + type, + name, + "", // TODO: pretty name + is_input, + _app->conf()->get_port_color(type), + _app->show_human_names()); +} + +void +JackDriver::add_port(dbus_uint64_t client_id, + const char* client_name, + dbus_uint64_t port_id, + const char* port_name, + dbus_uint32_t port_flags, + dbus_uint32_t port_type) +{ + PortType local_port_type; + + switch (port_type) { + case JACKDBUS_PORT_TYPE_AUDIO: + local_port_type = JACK_AUDIO; + break; + case JACKDBUS_PORT_TYPE_MIDI: + local_port_type = JACK_MIDI; + break; + default: + error_msg("Unknown JACK D-Bus port type"); + return; + } + + ModuleType type = InputOutput; + if (_app->conf()->get_module_split(client_name, port_flags & JACKDBUS_PORT_FLAG_TERMINAL)) { + if (port_flags & JACKDBUS_PORT_FLAG_INPUT) { + type = Input; + } else { + type = Output; + } + } + + PatchageModule* module = find_or_create_module(type, client_name); + + add_port(module, local_port_type, port_name, port_flags & JACKDBUS_PORT_FLAG_INPUT); +} + +void +JackDriver::remove_port(dbus_uint64_t client_id, + const char* client_name, + dbus_uint64_t port_id, + const char* port_name) +{ + PatchagePort* port = _app->canvas()->find_port_by_name(client_name, port_name); + if (!port) { + error_msg("Unable to remove unknown port"); + return; + } + + PatchageModule* module = dynamic_cast<PatchageModule*>(port->get_module()); + + delete port; + + // No empty modules (for now) + if (module->num_ports() == 0) { + delete module; + } + + if (_app->canvas()->empty()) { + if (_server_started) { + signal_detached.emit(); + } + + _server_started = false; + } +} + +PatchageModule* +JackDriver::find_or_create_module( + ModuleType type, + const std::string& name) +{ + PatchageModule* module = _app->canvas()->find_module(name, type); + + if (!module) { + module = new PatchageModule(_app, name, type); + module->load_location(); + _app->canvas()->add_module(name, module); + } + + return module; +} + +void +JackDriver::connect_ports(dbus_uint64_t connection_id, + dbus_uint64_t client1_id, + const char* client1_name, + dbus_uint64_t port1_id, + const char* port1_name, + dbus_uint64_t client2_id, + const char* client2_name, + dbus_uint64_t port2_id, + const char* port2_name) +{ + PatchagePort* port1 = _app->canvas()->find_port_by_name(client1_name, port1_name); + if (!port1) { + error_msg((std::string)"Unable to connect unknown port '" + port1_name + "' of client '" + client1_name + "'"); + return; + } + + PatchagePort* port2 = _app->canvas()->find_port_by_name(client2_name, port2_name); + if (!port2) { + error_msg((std::string)"Unable to connect unknown port '" + port2_name + "' of client '" + client2_name + "'"); + return; + } + + _app->canvas()->connect(port1, port2); +} + +void +JackDriver::disconnect_ports(dbus_uint64_t connection_id, + dbus_uint64_t client1_id, + const char* client1_name, + dbus_uint64_t port1_id, + const char* port1_name, + dbus_uint64_t client2_id, + const char* client2_name, + dbus_uint64_t port2_id, + const char* port2_name) +{ + PatchagePort* port1 = _app->canvas()->find_port_by_name(client1_name, port1_name); + if (!port1) { + error_msg((std::string)"Unable to disconnect unknown port '" + port1_name + "' of client '" + client1_name + "'"); + return; + } + + PatchagePort* port2 = _app->canvas()->find_port_by_name(client2_name, port2_name); + if (!port2) { + error_msg((std::string)"Unable to disconnect unknown port '" + port2_name + "' of client '" + client2_name + "'"); + return; + } + + _app->canvas()->disconnect(port1, port2); +} + +void +JackDriver::refresh_internal(bool force) +{ + DBusMessage* reply_ptr; + DBusMessageIter iter; + dbus_uint64_t version; + const char* reply_signature; + DBusMessageIter clients_array_iter; + DBusMessageIter client_struct_iter; + DBusMessageIter ports_array_iter; + DBusMessageIter port_struct_iter; + DBusMessageIter connections_array_iter; + DBusMessageIter connection_struct_iter; + dbus_uint64_t client_id; + const char* client_name; + dbus_uint64_t port_id; + const char* port_name; + dbus_uint32_t port_flags; + dbus_uint32_t port_type; + dbus_uint64_t client2_id; + const char* client2_name; + dbus_uint64_t port2_id; + const char* port2_name; + dbus_uint64_t connection_id; + + if (force) { + version = 0; // workaround module split/join stupidity + } else { + version = _graph_version; + } + + if (!call(true, JACKDBUS_IFACE_PATCHBAY, "GetGraph", &reply_ptr, DBUS_TYPE_UINT64, &version, DBUS_TYPE_INVALID)) { + error_msg("GetGraph() failed."); + return; + } + + reply_signature = dbus_message_get_signature(reply_ptr); + + if (strcmp(reply_signature, "ta(tsa(tsuu))a(tstststst)") != 0) { + error_msg((std::string)"GetGraph() reply signature mismatch. " + reply_signature); + goto unref; + } + + dbus_message_iter_init(reply_ptr, &iter); + + //info_msg((string)"version " + (char)dbus_message_iter_get_arg_type(&iter)); + dbus_message_iter_get_basic(&iter, &version); + dbus_message_iter_next(&iter); + + if (!force && version <= _graph_version) { + goto unref; + } + + destroy_all(); + + //info_msg(str(boost::format("got new graph version %llu") % version)); + _graph_version = version; + + //info_msg((string)"clients " + (char)dbus_message_iter_get_arg_type(&iter)); + + for (dbus_message_iter_recurse(&iter, &clients_array_iter); + dbus_message_iter_get_arg_type(&clients_array_iter) != DBUS_TYPE_INVALID; + dbus_message_iter_next(&clients_array_iter)) { + //info_msg((string)"a client " + (char)dbus_message_iter_get_arg_type(&clients_array_iter)); + dbus_message_iter_recurse(&clients_array_iter, &client_struct_iter); + + dbus_message_iter_get_basic(&client_struct_iter, &client_id); + dbus_message_iter_next(&client_struct_iter); + + dbus_message_iter_get_basic(&client_struct_iter, &client_name); + dbus_message_iter_next(&client_struct_iter); + + //info_msg((string)"client '" + client_name + "'"); + + for (dbus_message_iter_recurse(&client_struct_iter, &ports_array_iter); + dbus_message_iter_get_arg_type(&ports_array_iter) != DBUS_TYPE_INVALID; + dbus_message_iter_next(&ports_array_iter)) { + //info_msg((string)"a port " + (char)dbus_message_iter_get_arg_type(&ports_array_iter)); + dbus_message_iter_recurse(&ports_array_iter, &port_struct_iter); + + dbus_message_iter_get_basic(&port_struct_iter, &port_id); + dbus_message_iter_next(&port_struct_iter); + + dbus_message_iter_get_basic(&port_struct_iter, &port_name); + dbus_message_iter_next(&port_struct_iter); + + dbus_message_iter_get_basic(&port_struct_iter, &port_flags); + dbus_message_iter_next(&port_struct_iter); + + dbus_message_iter_get_basic(&port_struct_iter, &port_type); + dbus_message_iter_next(&port_struct_iter); + + //info_msg((string)"port: " + port_name); + + add_port(client_id, client_name, port_id, port_name, port_flags, port_type); + } + + dbus_message_iter_next(&client_struct_iter); + } + + dbus_message_iter_next(&iter); + + for (dbus_message_iter_recurse(&iter, &connections_array_iter); + dbus_message_iter_get_arg_type(&connections_array_iter) != DBUS_TYPE_INVALID; + dbus_message_iter_next(&connections_array_iter)) { + //info_msg((string)"a connection " + (char)dbus_message_iter_get_arg_type(&connections_array_iter)); + dbus_message_iter_recurse(&connections_array_iter, &connection_struct_iter); + + dbus_message_iter_get_basic(&connection_struct_iter, &client_id); + dbus_message_iter_next(&connection_struct_iter); + + dbus_message_iter_get_basic(&connection_struct_iter, &client_name); + dbus_message_iter_next(&connection_struct_iter); + + dbus_message_iter_get_basic(&connection_struct_iter, &port_id); + dbus_message_iter_next(&connection_struct_iter); + + dbus_message_iter_get_basic(&connection_struct_iter, &port_name); + dbus_message_iter_next(&connection_struct_iter); + + dbus_message_iter_get_basic(&connection_struct_iter, &client2_id); + dbus_message_iter_next(&connection_struct_iter); + + dbus_message_iter_get_basic(&connection_struct_iter, &client2_name); + dbus_message_iter_next(&connection_struct_iter); + + dbus_message_iter_get_basic(&connection_struct_iter, &port2_id); + dbus_message_iter_next(&connection_struct_iter); + + dbus_message_iter_get_basic(&connection_struct_iter, &port2_name); + dbus_message_iter_next(&connection_struct_iter); + + dbus_message_iter_get_basic(&connection_struct_iter, &connection_id); + dbus_message_iter_next(&connection_struct_iter); + + //info_msg(str(boost::format("connection(%llu) %s(%llu):%s(%llu) <-> %s(%llu):%s(%llu)") % + // connection_id % + // client_name % + // client_id % + // port_name % + // port_id % + // client2_name % + // client2_id % + // port2_name % + // port2_id)); + + connect_ports( + connection_id, + client_id, client_name, + port_id, port_name, + client2_id, client2_name, + port2_id, port2_name); + } + +unref: + dbus_message_unref(reply_ptr); +} + +void +JackDriver::refresh() +{ + refresh_internal(true); +} + +bool +JackDriver::connect(PatchagePort* src, + PatchagePort* dst) +{ + const char* client1_name = src->get_module()->get_label(); + const char* port1_name = src->get_label(); + const char* client2_name = dst->get_module()->get_label(); + const char* port2_name = dst->get_label(); + + DBusMessage* reply_ptr; + if (!call(true, JACKDBUS_IFACE_PATCHBAY, "ConnectPortsByName", &reply_ptr, + DBUS_TYPE_STRING, &client1_name, + DBUS_TYPE_STRING, &port1_name, + DBUS_TYPE_STRING, &client2_name, + DBUS_TYPE_STRING, &port2_name, + DBUS_TYPE_INVALID)) { + error_msg("ConnectPortsByName() failed."); + return false; + } + + return true; +} + +bool +JackDriver::disconnect(PatchagePort* src, + PatchagePort* dst) +{ + const char* client1_name = src->get_module()->get_label(); + const char* port1_name = src->get_label(); + const char* client2_name = dst->get_module()->get_label(); + const char* port2_name = dst->get_label(); + + DBusMessage* reply_ptr; + if (!call(true, JACKDBUS_IFACE_PATCHBAY, "DisconnectPortsByName", &reply_ptr, + DBUS_TYPE_STRING, &client1_name, + DBUS_TYPE_STRING, &port1_name, + DBUS_TYPE_STRING, &client2_name, + DBUS_TYPE_STRING, &port2_name, + DBUS_TYPE_INVALID)) { + error_msg("DisconnectPortsByName() failed."); + return false; + } + + return true; +} + +jack_nframes_t +JackDriver::buffer_size() +{ + DBusMessage* reply_ptr; + dbus_uint32_t buffer_size; + + if (_server_responding && !_server_started) { + goto fail; + } + + if (!call(true, JACKDBUS_IFACE_CONTROL, "GetBufferSize", &reply_ptr, DBUS_TYPE_INVALID)) { + goto fail; + } + + if (!dbus_message_get_args(reply_ptr, &_dbus_error, DBUS_TYPE_UINT32, &buffer_size, DBUS_TYPE_INVALID)) { + dbus_message_unref(reply_ptr); + dbus_error_free(&_dbus_error); + error_msg("decoding reply of GetBufferSize failed."); + goto fail; + } + + dbus_message_unref(reply_ptr); + + return buffer_size; + +fail: + return 4096; // something fake, patchage needs it to match combobox value +} + +bool +JackDriver::set_buffer_size(jack_nframes_t size) +{ + DBusMessage* reply_ptr; + dbus_uint32_t buffer_size; + + buffer_size = size; + + if (!call(true, JACKDBUS_IFACE_CONTROL, "SetBufferSize", &reply_ptr, DBUS_TYPE_UINT32, &buffer_size, DBUS_TYPE_INVALID)) { + return false; + } + + dbus_message_unref(reply_ptr); + + return true; +} + +float +JackDriver::sample_rate() +{ + DBusMessage* reply_ptr; + double sample_rate; + + if (!call(true, JACKDBUS_IFACE_CONTROL, "GetSampleRate", &reply_ptr, DBUS_TYPE_INVALID)) { + return false; + } + + if (!dbus_message_get_args(reply_ptr, &_dbus_error, DBUS_TYPE_DOUBLE, &sample_rate, DBUS_TYPE_INVALID)) { + dbus_message_unref(reply_ptr); + dbus_error_free(&_dbus_error); + error_msg("decoding reply of GetSampleRate failed."); + return false; + } + + dbus_message_unref(reply_ptr); + + return sample_rate; +} + +bool +JackDriver::is_realtime() const +{ + DBusMessage* reply_ptr; + dbus_bool_t realtime; + + JackDriver* me = const_cast<JackDriver*>(this); + if (!me->call(true, JACKDBUS_IFACE_CONTROL, "IsRealtime", + &reply_ptr, DBUS_TYPE_INVALID)) { + return false; + } + + if (!dbus_message_get_args(reply_ptr, &me->_dbus_error, DBUS_TYPE_BOOLEAN, + &realtime, DBUS_TYPE_INVALID)) { + dbus_message_unref(reply_ptr); + dbus_error_free(&me->_dbus_error); + error_msg("decoding reply of IsRealtime failed."); + return false; + } + + dbus_message_unref(reply_ptr); + + return realtime; +} + +size_t +JackDriver::get_xruns() +{ + DBusMessage* reply_ptr; + dbus_uint32_t xruns; + + if (_server_responding && !_server_started) { + return 0; + } + + if (!call(true, JACKDBUS_IFACE_CONTROL, "GetXruns", &reply_ptr, DBUS_TYPE_INVALID)) { + return 0; + } + + if (!dbus_message_get_args(reply_ptr, &_dbus_error, DBUS_TYPE_UINT32, &xruns, DBUS_TYPE_INVALID)) { + dbus_message_unref(reply_ptr); + dbus_error_free(&_dbus_error); + error_msg("decoding reply of GetXruns failed."); + return 0; + } + + dbus_message_unref(reply_ptr); + + return xruns; +} + +void +JackDriver::reset_xruns() +{ + DBusMessage* reply_ptr; + + if (!call(true, JACKDBUS_IFACE_CONTROL, "ResetXruns", &reply_ptr, DBUS_TYPE_INVALID)) { + return; + } + + dbus_message_unref(reply_ptr); +} + +float +JackDriver::get_max_dsp_load() +{ + DBusMessage* reply_ptr; + double load; + + if (_server_responding && !_server_started) { + return 0.0; + } + + if (!call(true, JACKDBUS_IFACE_CONTROL, "GetLoad", &reply_ptr, DBUS_TYPE_INVALID)) { + return 0.0; + } + + if (!dbus_message_get_args(reply_ptr, &_dbus_error, DBUS_TYPE_DOUBLE, &load, DBUS_TYPE_INVALID)) { + dbus_message_unref(reply_ptr); + dbus_error_free(&_dbus_error); + error_msg("decoding reply of GetLoad failed."); + return 0.0; + } + + dbus_message_unref(reply_ptr); + + load /= 100.0; // convert from percent to [0..1] + + if (load > _max_dsp_load) { + _max_dsp_load = load; + } + + return _max_dsp_load; +} + + +void +JackDriver::reset_max_dsp_load() +{ + _max_dsp_load = 0.0; +} + +PatchagePort* +JackDriver::create_port_view(Patchage* patchage, + const PortID& id) +{ + assert(false); // we dont use events at all + return NULL; +} + +void +JackDriver::error_msg(const std::string& msg) const +{ + _app->error_msg((std::string)"Jack: " + msg); +} + +void +JackDriver::info_msg(const std::string& msg) const +{ + _app->info_msg((std::string)"Jack: " + msg); +} diff --git a/src/JackDbusDriver.hpp b/src/JackDbusDriver.hpp new file mode 100644 index 0000000..69cc0a5 --- /dev/null +++ b/src/JackDbusDriver.hpp @@ -0,0 +1,161 @@ +/* This file is part of Patchage. + * Copyright 2008 Nedko Arnaudov <nedko@arnaudov.name> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_JACKDBUSDRIVER_HPP +#define PATCHAGE_JACKDBUSDRIVER_HPP + +#include <string> +#include <boost/shared_ptr.hpp> +#include <jack/jack.h> +#include <jack/statistics.h> +#include <glibmm/thread.h> +#include <dbus/dbus.h> +#include "Driver.hpp" +#include "Patchage.hpp" +#include "PatchageModule.hpp" + +class PatchageEvent; +class PatchageCanvas; +class PatchagePort; + +class JackDriver : public Driver +{ +public: + explicit JackDriver(Patchage* app); + ~JackDriver(); + + void attach(bool launch_daemon); + void detach(); + + bool is_attached() const; + bool is_realtime() const; + + void refresh(); + void destroy_all(); + + bool connect( + PatchagePort* src, + PatchagePort* dst); + + bool disconnect( + PatchagePort* src, + PatchagePort* dst); + + size_t get_xruns(); + void reset_xruns(); + float get_max_dsp_load(); + void reset_max_dsp_load(); + + float sample_rate(); + jack_nframes_t buffer_size(); + bool set_buffer_size(jack_nframes_t size); + + void process_events(Patchage* app) {} + + PatchagePort* create_port_view( + Patchage* patchage, + const PortID& ref); + +private: + void error_msg(const std::string& msg) const; + void info_msg(const std::string& msg) const; + + PatchageModule* find_or_create_module( + ModuleType type, + const std::string& name); + + void add_port( + PatchageModule* module, + PortType type, + const std::string& name, + bool is_input); + + void add_port( + dbus_uint64_t client_id, + const char* client_name, + dbus_uint64_t port_id, + const char* port_name, + dbus_uint32_t port_flags, + dbus_uint32_t port_type); + + void remove_port( + dbus_uint64_t client_id, + const char* client_name, + dbus_uint64_t port_id, + const char* port_name); + + void connect_ports( + dbus_uint64_t connection_id, + dbus_uint64_t client1_id, + const char* client1_name, + dbus_uint64_t port1_id, + const char* port1_name, + dbus_uint64_t client2_id, + const char* client2_name, + dbus_uint64_t port2_id, + const char* port2_name); + + void disconnect_ports( + dbus_uint64_t connection_id, + dbus_uint64_t client1_id, + const char* client1_name, + dbus_uint64_t port1_id, + const char* port1_name, + dbus_uint64_t client2_id, + const char* client2_name, + dbus_uint64_t port2_id, + const char* port2_name); + + bool call( + bool response_expected, + const char* iface, + const char* method, + DBusMessage** reply_ptr_ptr, + int in_type, + ...); + + void update_attached(); + + bool is_started(); + + void start_server(); + + void stop_server(); + + void refresh_internal(bool force); + + static DBusHandlerResult dbus_message_hook( + DBusConnection *connection, + DBusMessage *message, + void *me); + + void on_jack_appeared(); + + void on_jack_disappeared(); + +private: + Patchage* _app; + DBusError _dbus_error; + DBusConnection* _dbus_connection; + float _max_dsp_load; + + bool _server_responding; + bool _server_started; + + dbus_uint64_t _graph_version; +}; + +#endif // PATCHAGE_JACKDBUSDRIVER_HPP diff --git a/src/JackDriver.cpp b/src/JackDriver.cpp new file mode 100644 index 0000000..5daedae --- /dev/null +++ b/src/JackDriver.cpp @@ -0,0 +1,588 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <cassert> +#include <cstring> +#include <set> +#include <string> + +#include <boost/format.hpp> + +#include <jack/jack.h> +#include <jack/statistics.h> + +#include "JackDriver.hpp" +#include "Patchage.hpp" +#include "PatchageCanvas.hpp" +#include "PatchageEvent.hpp" +#include "PatchageModule.hpp" +#include "Queue.hpp" +#include "patchage_config.h" +#ifdef HAVE_JACK_METADATA +#include <jack/metadata.h> +#include "jackey.h" +#endif + +using std::endl; +using std::string; +using boost::format; + +JackDriver::JackDriver(Patchage* app) + : _app(app) + , _client(NULL) + , _events(128) + , _xruns(0) + , _xrun_delay(0) + , _is_activated(false) +{ + _last_pos.frame = 0; + _last_pos.valid = (jack_position_bits_t)0; +} + +JackDriver::~JackDriver() +{ + detach(); +} + +/** Connect to Jack. + */ +void +JackDriver::attach(bool launch_daemon) +{ + // Already connected + if (_client) + return; + + jack_options_t options = (!launch_daemon) ? JackNoStartServer : JackNullOption; + _client = jack_client_open("Patchage", options, NULL); + if (_client == NULL) { + _app->error_msg("Jack: Unable to create client."); + _is_activated = false; + } else { + jack_client_t* const client = _client; + + jack_on_shutdown(client, jack_shutdown_cb, this); + jack_set_client_registration_callback(client, jack_client_registration_cb, this); + jack_set_port_registration_callback(client, jack_port_registration_cb, this); + jack_set_port_connect_callback(client, jack_port_connect_cb, this); + jack_set_xrun_callback(client, jack_xrun_cb, this); + + _buffer_size = jack_get_buffer_size(client); + + if (!jack_activate(client)) { + _is_activated = true; + signal_attached.emit(); + std::stringstream ss; + _app->info_msg("Jack: Attached."); + } else { + _app->error_msg("Jack: Client activation failed."); + _is_activated = false; + } + } +} + +void +JackDriver::detach() +{ + Glib::Mutex::Lock lock(_shutdown_mutex); + if (_client) { + jack_deactivate(_client); + jack_client_close(_client); + _client = NULL; + } + _is_activated = false; + signal_detached.emit(); + _app->info_msg("Jack: Detached."); +} + +static bool +is_jack_port(const PatchagePort* port) +{ + return (port->type() == JACK_AUDIO || + port->type() == JACK_MIDI || + port->type() == JACK_OSC || + port->type() == JACK_CV); +} + +/** Destroy all JACK (canvas) ports. + */ +void +JackDriver::destroy_all() +{ + if (_app->canvas()) { + _app->canvas()->remove_ports(is_jack_port); + } +} + +PatchagePort* +JackDriver::create_port_view(Patchage* patchage, + const PortID& id) +{ + assert(id.type == PortID::JACK_ID); + + jack_port_t* jack_port = jack_port_by_id(_client, id.id.jack_id); + if (!jack_port) { + _app->error_msg((format("Jack: Failed to find port with ID `%1%'.") + % id).str());; + return NULL; + } + + const int jack_flags = jack_port_flags(jack_port); + + string module_name, port_name; + port_names(id, module_name, port_name); + + ModuleType type = InputOutput; + if (_app->conf()->get_module_split( + module_name, (jack_flags & JackPortIsTerminal))) { + if (jack_flags & JackPortIsInput) { + type = Input; + } else { + type = Output; + } + } + + PatchageModule* parent = _app->canvas()->find_module(module_name, type); + if (!parent) { + parent = new PatchageModule(patchage, module_name, type); + parent->load_location(); + patchage->canvas()->add_module(module_name, parent); + } + + if (parent->get_port(port_name)) { + _app->error_msg((format("Jack: Module `%1%' already has port `%2%'.") + % module_name % port_name).str()); + return NULL; + } + + PatchagePort* port = create_port(*parent, jack_port, id); + port->show(); + if (port->is_input()) { + parent->set_is_source(false); + } + + return port; +} + +#ifdef HAVE_JACK_METADATA +static std::string +get_property(jack_uuid_t subject, const char* key) +{ + std::string result; + + char* value = NULL; + char* datatype = NULL; + if (!jack_get_property(subject, key, &value, &datatype)) { + result = value; + } + jack_free(datatype); + jack_free(value); + + return result; +} +#endif + +PatchagePort* +JackDriver::create_port(PatchageModule& parent, jack_port_t* port, PortID id) +{ + if (!port) { + return NULL; + } + + std::string label; + boost::optional<int> order; + +#ifdef HAVE_JACK_METADATA + const jack_uuid_t uuid = jack_port_uuid(port); + if (_app->conf()->get_sort_ports()) { + const std::string order_str = get_property(uuid, JACKEY_ORDER); + label = get_property(uuid, JACK_METADATA_PRETTY_NAME); + if (!order_str.empty()) { + order = atoi(order_str.c_str()); + } + } +#endif + + const char* const type_str = jack_port_type(port); + PortType port_type; + if (!strcmp(type_str, JACK_DEFAULT_AUDIO_TYPE)) { + port_type = JACK_AUDIO; +#ifdef HAVE_JACK_METADATA + if (get_property(uuid, JACKEY_SIGNAL_TYPE) == "CV") { + port_type = JACK_CV; + } +#endif + } else if (!strcmp(type_str, JACK_DEFAULT_MIDI_TYPE)) { + port_type = JACK_MIDI; +#ifdef HAVE_JACK_METADATA + if (get_property(uuid, JACKEY_EVENT_TYPES) == "OSC") { + port_type = JACK_OSC; + } +#endif + } else { + _app->warning_msg((format("Jack: Port `%1%' has unknown type `%2%'.") + % jack_port_name(port) % type_str).str()); + return NULL; + } + + PatchagePort* ret( + new PatchagePort(parent, port_type, jack_port_short_name(port), + label, + (jack_port_flags(port) & JackPortIsInput), + _app->conf()->get_port_color(port_type), + _app->show_human_names(), + order)); + + if (id.type != PortID::NULL_PORT_ID) { + dynamic_cast<PatchageCanvas*>(parent.canvas())->index_port(id, ret); + } + + return ret; +} + +void +JackDriver::shutdown() +{ + signal_detached.emit(); +} + +/** Refresh all Jack audio ports/connections. + * To be called from GTK thread only. + */ +void +JackDriver::refresh() +{ + const char** ports; + jack_port_t* port; + + // Jack can take _client away from us at any time throughout here :/ + // Shortest locks possible is the best solution I can figure out + + Glib::Mutex::Lock lock(_shutdown_mutex); + + if (_client == NULL) { + shutdown(); + return; + } + + ports = jack_get_ports(_client, NULL, NULL, 0); // get all existing ports + + if (!ports) { + return; + } + + string client1_name; + string port1_name; + string client2_name; + string port2_name; + size_t colon; + + // Add all ports + for (int i = 0; ports[i]; ++i) { + port = jack_port_by_name(_client, ports[i]); + + client1_name = ports[i]; + client1_name = client1_name.substr(0, client1_name.find(":")); + + ModuleType type = InputOutput; + if (_app->conf()->get_module_split( + client1_name, + (jack_port_flags(port) & JackPortIsTerminal))) { + if (jack_port_flags(port) & JackPortIsInput) { + type = Input; + } else { + type = Output; + } + } + + PatchageModule* m = _app->canvas()->find_module(client1_name, type); + + if (!m) { + m = new PatchageModule(_app, client1_name, type); + m->load_location(); + _app->canvas()->add_module(client1_name, m); + } + + if (!m->get_port(jack_port_short_name(port))) + create_port(*m, port, PortID()); + } + + // Add all connections + for (int i = 0; ports[i]; ++i) { + port = jack_port_by_name(_client, ports[i]); + const char** connected_ports = jack_port_get_all_connections(_client, port); + + client1_name = ports[i]; + colon = client1_name.find(':'); + port1_name = client1_name.substr(colon + 1); + client1_name = client1_name.substr(0, colon); + + const ModuleType port1_type = (jack_port_flags(port) & JackPortIsInput) + ? Input : Output; + + PatchageModule* client1_module + = _app->canvas()->find_module(client1_name, port1_type); + + if (connected_ports) { + for (int j = 0; connected_ports[j]; ++j) { + + client2_name = connected_ports[j]; + colon = client2_name.find(':'); + port2_name = client2_name.substr(colon+1); + client2_name = client2_name.substr(0, colon); + + const ModuleType port2_type = (port1_type == Input) ? Output : Input; + + PatchageModule* client2_module + = _app->canvas()->find_module(client2_name, port2_type); + + Ganv::Port* port1 = client1_module->get_port(port1_name); + Ganv::Port* port2 = client2_module->get_port(port2_name); + + if (!port1 || !port2) + continue; + + Ganv::Port* src = NULL; + Ganv::Port* dst = NULL; + + if (port1->is_output() && port2->is_input()) { + src = port1; + dst = port2; + } else { + src = port2; + dst = port1; + } + + if (src && dst && !_app->canvas()->get_edge(src, dst)) + _app->canvas()->make_connection(src, dst); + } + + jack_free(connected_ports); + } + } + + jack_free(ports); +} + +bool +JackDriver::port_names(const PortID& id, + string& module_name, + string& port_name) +{ + jack_port_t* jack_port = NULL; + + if (id.type == PortID::JACK_ID) + jack_port = jack_port_by_id(_client, id.id.jack_id); + + if (!jack_port) { + module_name.clear(); + port_name.clear(); + return false; + } + + const string full_name = jack_port_name(jack_port); + + module_name = full_name.substr(0, full_name.find(":")); + port_name = full_name.substr(full_name.find(":")+1); + + return true; +} + +/** Connects two Jack audio ports. + * To be called from GTK thread only. + * \return Whether connection succeeded. + */ +bool +JackDriver::connect(PatchagePort* src_port, + PatchagePort* dst_port) +{ + if (_client == NULL) + return false; + + int result = jack_connect(_client, src_port->full_name().c_str(), dst_port->full_name().c_str()); + + if (result == 0) + _app->info_msg(string("Jack: Connected ") + + src_port->full_name() + " => " + dst_port->full_name()); + else + _app->error_msg(string("Jack: Unable to connect ") + + src_port->full_name() + " => " + dst_port->full_name()); + + return (!result); +} + +/** Disconnects two Jack audio ports. + * To be called from GTK thread only. + * \return Whether disconnection succeeded. + */ +bool +JackDriver::disconnect(PatchagePort* const src_port, + PatchagePort* const dst_port) +{ + if (_client == NULL) + return false; + + int result = jack_disconnect(_client, src_port->full_name().c_str(), dst_port->full_name().c_str()); + + if (result == 0) + _app->info_msg(string("Jack: Disconnected ") + + src_port->full_name() + " => " + dst_port->full_name()); + else + _app->error_msg(string("Jack: Unable to disconnect ") + + src_port->full_name() + " => " + dst_port->full_name()); + + return (!result); +} + +void +JackDriver::jack_client_registration_cb(const char* name, int registered, void* jack_driver) +{ + JackDriver* me = reinterpret_cast<JackDriver*>(jack_driver); + assert(me->_client); + + if (registered) { + me->_events.push(PatchageEvent(PatchageEvent::CLIENT_CREATION, name)); + } else { + me->_events.push(PatchageEvent(PatchageEvent::CLIENT_DESTRUCTION, name)); + } +} + +void +JackDriver::jack_port_registration_cb(jack_port_id_t port_id, int registered, void* jack_driver) +{ + JackDriver* me = reinterpret_cast<JackDriver*>(jack_driver); + assert(me->_client); + + if (registered) { + me->_events.push(PatchageEvent(PatchageEvent::PORT_CREATION, port_id)); + } else { + me->_events.push(PatchageEvent(PatchageEvent::PORT_DESTRUCTION, port_id)); + } +} + +void +JackDriver::jack_port_connect_cb(jack_port_id_t src, jack_port_id_t dst, int connect, void* jack_driver) +{ + JackDriver* me = reinterpret_cast<JackDriver*>(jack_driver); + assert(me->_client); + + if (connect) { + me->_events.push(PatchageEvent(PatchageEvent::CONNECTION, src, dst)); + } else { + me->_events.push(PatchageEvent(PatchageEvent::DISCONNECTION, src, dst)); + } +} + +int +JackDriver::jack_xrun_cb(void* jack_driver) +{ + JackDriver* me = reinterpret_cast<JackDriver*>(jack_driver); + assert(me->_client); + + ++me->_xruns; + me->_xrun_delay = jack_get_xrun_delayed_usecs(me->_client); + + jack_reset_max_delayed_usecs(me->_client); + + return 0; +} + +void +JackDriver::jack_shutdown_cb(void* jack_driver) +{ + assert(jack_driver); + JackDriver* me = reinterpret_cast<JackDriver*>(jack_driver); + me->_app->info_msg("Jack: Shutdown."); + Glib::Mutex::Lock lock(me->_shutdown_mutex); + me->_client = NULL; + me->_is_activated = false; + me->signal_detached.emit(); +} + +jack_nframes_t +JackDriver::buffer_size() +{ + if (_is_activated) + return _buffer_size; + else + return jack_get_buffer_size(_client); +} + +void +JackDriver::reset_xruns() +{ + _xruns = 0; + _xrun_delay = 0; +} + +float +JackDriver::get_max_dsp_load() +{ + float max_load = 0.0f; + if (_client) { + const float max_delay = jack_get_max_delayed_usecs(_client); + const float rate = sample_rate(); + const float size = buffer_size(); + const float period = size / rate * 1000000; // usec + + if (max_delay > period) { + max_load = 1.0; + jack_reset_max_delayed_usecs(_client); + } else { + max_load = max_delay / period; + } + } + return max_load; +} + +void +JackDriver::reset_max_dsp_load() +{ + if (_client) { + jack_reset_max_delayed_usecs(_client); + } +} + +bool +JackDriver::set_buffer_size(jack_nframes_t size) +{ + if (buffer_size() == size) { + return true; + } + + if (!_client) { + _buffer_size = size; + return true; + } + + if (jack_set_buffer_size(_client, size)) { + _app->error_msg("[JACK] Unable to set buffer size"); + return false; + } else { + _buffer_size = size; + return true; + } +} + +void +JackDriver::process_events(Patchage* app) +{ + while (!_events.empty()) { + PatchageEvent& ev = _events.front(); + ev.execute(app); + _events.pop(); + } +} diff --git a/src/JackDriver.hpp b/src/JackDriver.hpp new file mode 100644 index 0000000..875bd61 --- /dev/null +++ b/src/JackDriver.hpp @@ -0,0 +1,109 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_JACKDRIVER_HPP +#define PATCHAGE_JACKDRIVER_HPP + +#include <string> + +#include <jack/jack.h> + +#include <glibmm/thread.h> + +#include "Driver.hpp" +#include "Queue.hpp" + +class Patchage; +class PatchageEvent; +class PatchageCanvas; +class PatchagePort; +class PatchageModule; + +/** Handles all externally driven functionality, registering ports etc. + * + * Jack callbacks and connect methods and things like that live here. + * Right now just for jack ports, but that will change... + */ +class JackDriver : public Driver +{ +public: + explicit JackDriver(Patchage* app); + ~JackDriver(); + + void attach(bool launch_daemon); + void detach(); + + bool is_attached() const { return (_client != NULL); } + bool is_realtime() const { return _client && jack_is_realtime(_client); } + + void refresh(); + void destroy_all(); + + bool port_names(const PortID& id, + std::string& module_name, + std::string& port_name); + + PatchagePort* create_port_view(Patchage* patchage, + const PortID& id); + + bool connect(PatchagePort* src, + PatchagePort* dst); + + bool disconnect(PatchagePort* src, + PatchagePort* dst); + + uint32_t get_xruns() { return _xruns; } + void reset_xruns(); + float get_max_dsp_load(); + void reset_max_dsp_load(); + + jack_client_t* client() { return _client; } + + jack_nframes_t sample_rate() { return jack_get_sample_rate(_client); } + jack_nframes_t buffer_size(); + bool set_buffer_size(jack_nframes_t size); + + void process_events(Patchage* app); + +private: + PatchagePort* create_port( + PatchageModule& parent, + jack_port_t* port, + PortID id); + + void shutdown(); + + static void jack_client_registration_cb(const char* name, int registered, void* me); + static void jack_port_registration_cb(jack_port_id_t port_id, int registered, void* me); + static void jack_port_connect_cb(jack_port_id_t src, jack_port_id_t dst, int connect, void* me); + static int jack_xrun_cb(void* me); + static void jack_shutdown_cb(void* me); + + Patchage* _app; + jack_client_t* _client; + + Queue<PatchageEvent> _events; + + Glib::Mutex _shutdown_mutex; + + jack_position_t _last_pos; + jack_nframes_t _buffer_size; + uint32_t _xruns; + float _xrun_delay; + bool _is_activated :1; +}; + +#endif // PATCHAGE_JACKDRIVER_HPP diff --git a/src/Legend.hpp b/src/Legend.hpp new file mode 100644 index 0000000..b95d30c --- /dev/null +++ b/src/Legend.hpp @@ -0,0 +1,71 @@ +/* This file is part of Patchage. + * Copyright 2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_LEGEND_HPP +#define PATCHAGE_LEGEND_HPP + +#include <gtkmm/colorbutton.h> +#include <gtkmm/box.h> + +#include "Configuration.hpp" + +class Legend : public Gtk::HBox { +public: + Legend(const Configuration& configuration) { + add_button(JACK_AUDIO, "Audio", configuration.get_port_color(JACK_AUDIO)); +#ifdef HAVE_JACK_METADATA + add_button(JACK_CV, "CV", configuration.get_port_color(JACK_CV)); + add_button(JACK_OSC, "OSC", configuration.get_port_color(JACK_OSC)); +#endif + add_button(JACK_MIDI, "MIDI", configuration.get_port_color(JACK_MIDI)); + add_button(ALSA_MIDI, "ALSA MIDI", configuration.get_port_color(ALSA_MIDI)); + show_all_children(); + } + + void add_button(int id, const std::string& label, uint32_t rgba) { + Gdk::Color col; + col.set_rgb(((rgba >> 24) & 0xFF) * 0x100, + ((rgba>> 16) & 0xFF) * 0x100, + ((rgba >> 8) & 0xFF) * 0x100); + Gtk::HBox* box = new Gtk::HBox(); + Gtk::ColorButton* but = new Gtk::ColorButton(col); + but->set_use_alpha(false); + but->signal_color_set().connect( + sigc::bind(sigc::mem_fun(this, &Legend::on_color_set), + id, label, but)); + + box->pack_end(*Gtk::manage(but)); + box->pack_end(*Gtk::manage(new Gtk::Label(label)), false, false, 2); + + this->pack_start(*Gtk::manage(box), false, false, 6); + } + + void on_color_set(const int id, + const std::string& label, + const Gtk::ColorButton* but) { + const Gdk::Color col = but->get_color(); + const uint32_t rgba = (((col.get_red() / 0x100) << 24) | + ((col.get_green() / 0x100) << 16) | + ((col.get_blue() / 0x100) << 8) | + 0xFF); + + signal_color_changed.emit(id, label, rgba); + } + + sigc::signal<void, int, std::string, uint32_t> signal_color_changed; +}; + +#endif // PATCHAGE_LEGEND_HPP diff --git a/src/Patchage.cpp b/src/Patchage.cpp new file mode 100644 index 0000000..eae2ef9 --- /dev/null +++ b/src/Patchage.cpp @@ -0,0 +1,1078 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <pthread.h> + +#include <cmath> +#include <fstream> + +#include <glib.h> +#include <glib/gstdio.h> +#include <gtk/gtkwindow.h> + +#include <boost/format.hpp> + +#include <gtkmm/button.h> +#include <gtkmm/filechooserdialog.h> +#include <gtkmm/liststore.h> +#include <gtkmm/menuitem.h> +#include <gtkmm/messagedialog.h> +#include <gtkmm/stock.h> +#include <gtkmm/treemodel.h> + +#include "ganv/Module.hpp" +#include "ganv/Edge.hpp" + +#include "Configuration.hpp" +#include "Legend.hpp" +#include "Patchage.hpp" +#include "PatchageCanvas.hpp" +#include "PatchageEvent.hpp" +#include "UIFile.hpp" +#include "patchage_config.h" + +#if defined(HAVE_JACK_DBUS) + #include "JackDbusDriver.hpp" +#elif defined(PATCHAGE_LIBJACK) + #include "JackDriver.hpp" + #include <jack/statistics.h> +#endif + +#ifdef PATCHAGE_JACK_SESSION + #include <jack/session.h> +#endif + +#ifdef HAVE_ALSA + #include "AlsaDriver.hpp" +#endif + +#ifdef PATCHAGE_GTK_OSX + #include <gtkosxapplication.h> + +static gboolean +can_activate_cb(GtkWidget* widget, guint signal_id, gpointer data) +{ + return gtk_widget_is_sensitive(widget); +} + +static void +terminate_cb(GtkosxApplication* app, gpointer data) +{ + Patchage* patchage = (Patchage*)data; + patchage->save(); + Gtk::Main::quit(); +} + +#endif + +static bool +configure_cb(GtkWindow* parentWindow, GdkEvent* event, gpointer data) +{ + ((Patchage*)data)->store_window_location(); + return FALSE; +} + +static int +port_order(const GanvPort* a, const GanvPort* b, void* data) +{ + const PatchagePort* pa = dynamic_cast<const PatchagePort*>(Glib::wrap(a)); + const PatchagePort* pb = dynamic_cast<const PatchagePort*>(Glib::wrap(b)); + if (pa && pb) { + if (pa->order() && pb->order()) { + return *pa->order() - *pb->order(); + } else if (pa->order()) { + return -1; + } else if (pb->order()) { + return 1; + } + return pa->name().compare(pb->name()); + } + return 0; +} + +struct ProjectList_column_record : public Gtk::TreeModel::ColumnRecord { + Gtk::TreeModelColumn<Glib::ustring> label; +}; + +using std::cout; +using std::endl; +using std::string; + +#define INIT_WIDGET(x) x(_xml, ((const char*)#x) + 1) + +Patchage::Patchage(int argc, char** argv) + : _xml(UIFile::open("patchage")) +#ifdef HAVE_ALSA + , _alsa_driver(NULL) +#endif + , _jack_driver(NULL) + , _conf(NULL) + , INIT_WIDGET(_about_win) + , INIT_WIDGET(_main_scrolledwin) + , INIT_WIDGET(_main_win) + , INIT_WIDGET(_main_vbox) + , INIT_WIDGET(_menubar) + , INIT_WIDGET(_menu_alsa_connect) + , INIT_WIDGET(_menu_alsa_disconnect) + , INIT_WIDGET(_menu_file_quit) + , INIT_WIDGET(_menu_export_image) + , INIT_WIDGET(_menu_help_about) + , INIT_WIDGET(_menu_jack_connect) + , INIT_WIDGET(_menu_jack_disconnect) + , INIT_WIDGET(_menu_open_session) + , INIT_WIDGET(_menu_save_session) + , INIT_WIDGET(_menu_save_close_session) + , INIT_WIDGET(_menu_view_arrange) + , INIT_WIDGET(_menu_view_sprung_layout) + , INIT_WIDGET(_menu_view_messages) + , INIT_WIDGET(_menu_view_toolbar) + , INIT_WIDGET(_menu_view_refresh) + , INIT_WIDGET(_menu_view_human_names) + , INIT_WIDGET(_menu_view_sort_ports) + , INIT_WIDGET(_menu_zoom_in) + , INIT_WIDGET(_menu_zoom_out) + , INIT_WIDGET(_menu_zoom_normal) + , INIT_WIDGET(_menu_zoom_full) + , INIT_WIDGET(_menu_increase_font_size) + , INIT_WIDGET(_menu_decrease_font_size) + , INIT_WIDGET(_menu_normal_font_size) + , INIT_WIDGET(_toolbar) + , INIT_WIDGET(_clear_load_but) + , INIT_WIDGET(_xrun_progress) + , INIT_WIDGET(_buf_size_combo) + , INIT_WIDGET(_latency_label) + , INIT_WIDGET(_legend_alignment) + , INIT_WIDGET(_main_paned) + , INIT_WIDGET(_log_scrolledwindow) + , INIT_WIDGET(_status_text) + , _legend(NULL) + , _pane_initialized(false) + , _attach(true) + , _driver_detached(false) + , _refresh(false) + , _enable_refresh(true) + , _jack_driver_autoattach(true) +#ifdef HAVE_ALSA + , _alsa_driver_autoattach(true) +#endif +{ + _conf = new Configuration(); + _canvas = boost::shared_ptr<PatchageCanvas>(new PatchageCanvas(this, 1600*2, 1200*2)); + + while (argc > 0) { + if (!strcmp(*argv, "-h") || !strcmp(*argv, "--help")) { + cout << "Usage: patchage [OPTION]..." << endl; + cout << "Visually connect JACK and ALSA Audio/MIDI ports." << endl << endl; + cout << "Options:" << endl; + cout << "\t-h --help Show this help" << endl; + cout << "\t-A --no-alsa Do not automatically attach to ALSA" << endl; + cout << "\t-J --no-jack Do not automatically attack to JACK" << endl; + exit(0); +#ifdef HAVE_ALSA + } else if (!strcmp(*argv, "-A") || !strcmp(*argv, "--no-alsa")) { + _alsa_driver_autoattach = false; +#endif +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + } else if (!strcmp(*argv, "-J") || !strcmp(*argv, "--no-jack")) { + _jack_driver_autoattach = false; +#endif + } + + argv++; + argc--; + } + + Glib::set_application_name("Patchage"); + _about_win->property_program_name() = "Patchage"; + _about_win->property_logo_icon_name() = "patchage"; + gtk_window_set_default_icon_name("patchage"); + + // Create list model for buffer size selector + Glib::RefPtr<Gtk::ListStore> buf_size_store = Gtk::ListStore::create(_buf_size_columns); + for (size_t i = 32; i <= 4096; i *= 2) { + Gtk::TreeModel::Row row = *(buf_size_store->append()); + row[_buf_size_columns.label] = std::to_string(i); + } + + _buf_size_combo->set_model(buf_size_store); + _buf_size_combo->pack_start(_buf_size_columns.label); + + _main_scrolledwin->add(_canvas->widget()); + + _main_scrolledwin->property_hadjustment().get_value()->set_step_increment(10); + _main_scrolledwin->property_vadjustment().get_value()->set_step_increment(10); + + _main_scrolledwin->signal_scroll_event().connect( + sigc::mem_fun(this, &Patchage::on_scroll)); + _clear_load_but->signal_clicked().connect( + sigc::mem_fun(this, &Patchage::clear_load)); + _buf_size_combo->signal_changed().connect( + sigc::mem_fun(this, &Patchage::buffer_size_changed)); + _status_text->signal_size_allocate().connect( + sigc::mem_fun(this, &Patchage::on_messages_resized)); + +#ifdef PATCHAGE_JACK_SESSION + _menu_open_session->signal_activate().connect( + sigc::mem_fun(this, &Patchage::show_open_session_dialog)); + _menu_save_session->signal_activate().connect( + sigc::mem_fun(this, &Patchage::show_save_session_dialog)); + _menu_save_close_session->signal_activate().connect( + sigc::mem_fun(this, &Patchage::show_save_close_session_dialog)); +#else + _menu_open_session->hide(); + _menu_save_session->hide(); + _menu_save_close_session->hide(); +#endif + +#ifdef HAVE_ALSA + _menu_alsa_connect->signal_activate().connect( + sigc::mem_fun(this, &Patchage::menu_alsa_connect)); + _menu_alsa_disconnect->signal_activate().connect( + sigc::mem_fun(this, &Patchage::menu_alsa_disconnect)); +#else + _menu_alsa_connect->set_sensitive(false); + _menu_alsa_disconnect->set_sensitive(false); +#endif + + _menu_file_quit->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_quit)); + _menu_export_image->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_export_image)); + _menu_view_refresh->signal_activate().connect( + sigc::mem_fun(this, &Patchage::refresh)); + _menu_view_human_names->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_view_human_names)); + _menu_view_sort_ports->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_view_sort_ports)); + _menu_view_arrange->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_arrange)); + _menu_view_sprung_layout->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_sprung_layout_toggled)); + _menu_view_messages->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_view_messages)); + _menu_view_toolbar->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_view_toolbar)); + _menu_help_about->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_help_about)); + _menu_zoom_in->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_zoom_in)); + _menu_zoom_out->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_zoom_out)); + _menu_zoom_normal->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_zoom_normal)); + _menu_zoom_full->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_zoom_full)); + _menu_increase_font_size->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_increase_font_size)); + _menu_decrease_font_size->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_decrease_font_size)); + _menu_normal_font_size->signal_activate().connect( + sigc::mem_fun(this, &Patchage::on_normal_font_size)); + + if (_canvas->supports_sprung_layout()) { + _menu_view_sprung_layout->set_active(true); + } else { + _menu_view_sprung_layout->set_active(false); + _menu_view_sprung_layout->set_sensitive(false); + } + + for (int s = Gtk::STATE_NORMAL; s <= Gtk::STATE_INSENSITIVE; ++s) { + _status_text->modify_base((Gtk::StateType)s, Gdk::Color("#000000")); + _status_text->modify_text((Gtk::StateType)s, Gdk::Color("#FFFFFF")); + } + + _error_tag = Gtk::TextTag::create(); + _error_tag->property_foreground() = "#CC0000"; + _status_text->get_buffer()->get_tag_table()->add(_error_tag); + + _warning_tag = Gtk::TextTag::create(); + _warning_tag->property_foreground() = "#C4A000"; + _status_text->get_buffer()->get_tag_table()->add(_warning_tag); + + _canvas->widget().show(); + _main_win->present(); + + _conf->set_font_size(_canvas->get_default_font_size()); + _conf->load(); + _canvas->set_zoom(_conf->get_zoom()); + _canvas->set_font_size(_conf->get_font_size()); + if (_conf->get_sort_ports()) { + _canvas->set_port_order(port_order, NULL); + } + + _main_win->resize( + static_cast<int>(_conf->get_window_size().x), + static_cast<int>(_conf->get_window_size().y)); + + _main_win->move( + static_cast<int>(_conf->get_window_location().x), + static_cast<int>(_conf->get_window_location().y)); + + _legend = new Legend(*_conf); + _legend->signal_color_changed.connect( + sigc::mem_fun(this, &Patchage::on_legend_color_change)); + _legend_alignment->add(*Gtk::manage(_legend)); + _legend->show_all(); + + _about_win->set_transient_for(*_main_win); +#ifdef __APPLE__ + try { + _about_win->set_logo( + Gdk::Pixbuf::create_from_file( + bundle_location() + "/Resources/Patchage.icns")); + } catch (const Glib::Exception& e) { + error_msg((boost::format("failed to set logo (%s)") % e.what()).str()); + } +#endif + +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + _jack_driver = new JackDriver(this); + _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, &JackDriver::attach), true)); + _menu_jack_disconnect->signal_activate().connect( + sigc::mem_fun(_jack_driver, &JackDriver::detach)); +#endif + +#ifdef HAVE_ALSA + _alsa_driver = new AlsaDriver(this); +#endif + + connect_widgets(); + update_state(); + _menu_view_toolbar->set_active(_conf->get_show_toolbar()); + _menu_view_sprung_layout->set_active(_conf->get_sprung_layout()); + _menu_view_sort_ports->set_active(_conf->get_sort_ports()); + _status_text->set_pixels_inside_wrap(2); + _status_text->set_left_margin(4); + _status_text->set_right_margin(4); + _status_text->set_pixels_below_lines(2); + + g_signal_connect(_main_win->gobj(), "configure-event", + G_CALLBACK(configure_cb), this); + + _canvas->widget().grab_focus(); + + // Idle callback, check if we need to refresh + Glib::signal_timeout().connect( + sigc::mem_fun(this, &Patchage::idle_callback), 100); + +#ifdef PATCHAGE_GTK_OSX + // Set up Mac menu bar + GtkosxApplication* osxapp = (GtkosxApplication*)g_object_new( + GTKOSX_TYPE_APPLICATION, NULL); + _menubar->hide(); + _menu_file_quit->hide(); + gtkosx_application_set_menu_bar(osxapp, GTK_MENU_SHELL(_menubar->gobj())); + gtkosx_application_insert_app_menu_item( + osxapp, GTK_WIDGET(_menu_help_about->gobj()), 0); + g_signal_connect(_menubar->gobj(), "can-activate-accel", + G_CALLBACK(can_activate_cb), NULL); + g_signal_connect(osxapp, "NSApplicationWillTerminate", + G_CALLBACK(terminate_cb), this); + gtkosx_application_ready(osxapp); +#endif +} + +Patchage::~Patchage() +{ +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + delete _jack_driver; +#endif +#ifdef HAVE_ALSA + delete _alsa_driver; +#endif + + delete _conf; + + _about_win.destroy(); + _xml.reset(); +} + +void +Patchage::attach() +{ + _enable_refresh = false; + +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + if (_jack_driver_autoattach) + _jack_driver->attach(true); +#endif + +#ifdef HAVE_ALSA + if (_alsa_driver_autoattach) + _alsa_driver->attach(); +#endif + + _enable_refresh = true; + + refresh(); + update_toolbar(); +} + +bool +Patchage::idle_callback() +{ + // Initial run, attach + if (_attach) { + attach(); + _menu_view_messages->set_active(_conf->get_show_messages()); + _attach = false; + } + + // Process any JACK events +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + if (_jack_driver) { + _jack_driver->process_events(this); + } +#endif + + // Process any ALSA events +#ifdef HAVE_ALSA + if (_alsa_driver) { + _alsa_driver->process_events(this); + } +#endif + + // Do a full refresh + if (_refresh) { + refresh(); + } else if (_driver_detached) { +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + if (_jack_driver && !_jack_driver->is_attached()) + _jack_driver->destroy_all(); +#endif +#ifdef HAVE_ALSA + if (_alsa_driver && !_alsa_driver->is_attached()) + _alsa_driver->destroy_all(); +#endif + } + + _refresh = false; + _driver_detached = false; + + // Update load every 5 idle callbacks + static int count = 0; + if (++count == 5) { + update_load(); + count = 0; + } + + return true; +} + +void +Patchage::update_toolbar() +{ + static bool updating = false; + if (updating) { + return; + } else { + updating = true; + } + +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + if (_jack_driver->is_attached()) { + const jack_nframes_t buffer_size = _jack_driver->buffer_size(); + const jack_nframes_t sample_rate = _jack_driver->sample_rate(); + if (sample_rate != 0) { + const int latency_ms = lrintf(buffer_size * 1000 / (float)sample_rate); + std::stringstream ss; + ss << " frames @ " << (sample_rate / 1000) + << "kHz (" << latency_ms << "ms)"; + _latency_label->set_label(ss.str()); + _latency_label->set_visible(true); + _buf_size_combo->set_active((int)log2f(_jack_driver->buffer_size()) - 5); + updating = false; + return; + } + } +#endif + _latency_label->set_visible(false); + updating = false; +} + +bool +Patchage::update_load() +{ +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + if (_jack_driver->is_attached()) { + char buf[8]; + snprintf(buf, sizeof(buf), "%u", _jack_driver->get_xruns()); + _xrun_progress->set_text(std::string(buf) + " Dropouts"); + _xrun_progress->set_fraction(_jack_driver->get_max_dsp_load()); + } +#endif + + return true; +} + +void +Patchage::zoom(double z) +{ + _conf->set_zoom(z); + _canvas->set_zoom(z); +} + +void +Patchage::refresh() +{ + if (_canvas && _enable_refresh) { + _canvas->clear(); + +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + if (_jack_driver) + _jack_driver->refresh(); +#endif + +#ifdef HAVE_ALSA + if (_alsa_driver) + _alsa_driver->refresh(); +#endif + } +} + +void +Patchage::store_window_location() +{ + int loc_x, loc_y, size_x, size_y; + _main_win->get_position(loc_x, loc_y); + _main_win->get_size(size_x, size_y); + Coord window_location; + window_location.x = loc_x; + window_location.y = loc_y; + Coord window_size; + window_size.x = size_x; + window_size.y = size_y; + _conf->set_window_location(window_location); + _conf->set_window_size(window_size); +} + +void +Patchage::clear_load() +{ +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + _xrun_progress->set_fraction(0.0); + _jack_driver->reset_xruns(); + _jack_driver->reset_max_dsp_load(); +#endif +} + +void +Patchage::error_msg(const std::string& msg) +{ + Glib::RefPtr<Gtk::TextBuffer> buffer = _status_text->get_buffer(); + buffer->insert_with_tag(buffer->end(), std::string("\n") + msg, _error_tag); + _status_text->scroll_to_mark(buffer->get_insert(), 0); + _menu_view_messages->set_active(true); +} + +void +Patchage::info_msg(const std::string& msg) +{ + Glib::RefPtr<Gtk::TextBuffer> buffer = _status_text->get_buffer(); + buffer->insert(buffer->end(), std::string("\n") + msg); + _status_text->scroll_to_mark(buffer->get_insert(), 0); +} + +void +Patchage::warning_msg(const std::string& msg) +{ + Glib::RefPtr<Gtk::TextBuffer> buffer = _status_text->get_buffer(); + buffer->insert_with_tag(buffer->end(), std::string("\n") + msg, _warning_tag); + _status_text->scroll_to_mark(buffer->get_insert(), 0); +} + +static void +load_module_location(GanvNode* node, void* data) +{ + if (GANV_IS_MODULE(node)) { + Ganv::Module* gmod = Glib::wrap(GANV_MODULE(node)); + PatchageModule* pmod = dynamic_cast<PatchageModule*>(gmod); + if (pmod) { + pmod->load_location(); + } + } +} + +void +Patchage::update_state() +{ + _canvas->for_each_node(load_module_location, NULL); +} + +/** 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 PATCHAGE_JACK_SESSION +void +Patchage::show_open_session_dialog() +{ + Gtk::FileChooserDialog dialog(*_main_win, "Open Session", + Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); + + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + Gtk::Button* open_but = dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + open_but->property_has_default() = true; + + if (dialog.run() != Gtk::RESPONSE_OK) { + return; + } + + const std::string dir = dialog.get_filename(); + if (g_chdir(dir.c_str())) { + error_msg("Failed to switch to session directory " + dir); + return; + } + + if (system("./jack-session") < 0) { + error_msg("Error executing `./jack-session' in " + dir); + } else { + info_msg("Loaded session " + dir); + } +} + +static void +print_edge(GanvEdge* edge, void* data) +{ + std::ofstream* script = (std::ofstream*)data; + Ganv::Edge* edgemm = Glib::wrap(edge); + + PatchagePort* src = dynamic_cast<PatchagePort*>((edgemm)->get_tail()); + PatchagePort* dst = dynamic_cast<PatchagePort*>((edgemm)->get_head()); + + if (!src || !dst || src->type() == ALSA_MIDI || dst->type() == ALSA_MIDI) { + return; + } + + (*script) << "jack_connect '" << src->full_name() + << "' '" << dst->full_name() << "' &" << endl; +} + +void +Patchage::save_session(bool close) +{ + Gtk::FileChooserDialog dialog(*_main_win, "Save Session", + Gtk::FILE_CHOOSER_ACTION_SAVE); + + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + Gtk::Button* save_but = dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK); + save_but->property_has_default() = true; + + if (dialog.run() != Gtk::RESPONSE_OK) { + return; + } + + std::string path = dialog.get_filename(); + if (g_mkdir_with_parents(path.c_str(), 0740)) { + error_msg("Failed to create session directory " + path); + return; + } + + path += '/'; + jack_session_command_t* cmd = jack_session_notify( + _jack_driver->client(), + NULL, + close ? JackSessionSaveAndQuit : JackSessionSave, + path.c_str()); + + const std::string script_path = path + "jack-session"; + std::ofstream script(script_path.c_str()); + script << "#!/bin/sh" << endl << endl; + + const std::string var("${SESSION_DIR}"); + for (int c = 0; cmd[c].uuid; ++c) { + std::string command = cmd[c].command; + const size_t index = command.find(var); + if (index != string::npos) { + command.replace(index, var.length(), cmd[c].client_name); + } + + script << command << " &" << endl; + } + + script << endl; + script << "sleep 3" << endl; + script << endl; + + _canvas->for_each_edge(print_edge, &script); + + script.close(); + g_chmod(script_path.c_str(), 0740); +} + +void +Patchage::show_save_session_dialog() +{ + save_session(false); +} + +void +Patchage::show_save_close_session_dialog() +{ + save_session(true); +} + +#endif + +#ifdef HAVE_ALSA +void +Patchage::menu_alsa_connect() +{ + _alsa_driver->attach(false); + _alsa_driver->refresh(); +} + +void +Patchage::menu_alsa_disconnect() +{ + _alsa_driver->detach(); + refresh(); +} +#endif + +void +Patchage::on_arrange() +{ + if (_canvas) { + _canvas->arrange(); + } +} + +void +Patchage::on_sprung_layout_toggled() +{ + const bool sprung = _menu_view_sprung_layout->get_active(); + + _canvas->set_sprung_layout(sprung); + _conf->set_sprung_layout(sprung); +} + +void +Patchage::on_help_about() +{ + _about_win->run(); + _about_win->hide(); +} + +static void +update_labels(GanvNode* node, void* data) +{ + const bool human_names = *(const bool*)data; + if (GANV_IS_MODULE(node)) { + Ganv::Module* gmod = Glib::wrap(GANV_MODULE(node)); + PatchageModule* pmod = dynamic_cast<PatchageModule*>(gmod); + if (pmod) { + for (Ganv::Port* gport : *gmod) { + PatchagePort* pport = dynamic_cast<PatchagePort*>(gport); + if (pport) { + pport->show_human_name(human_names); + } + } + } + } +} + +void +Patchage::on_view_human_names() +{ + bool human_names = show_human_names(); + _canvas->for_each_node(update_labels, &human_names); +} + +void +Patchage::on_view_sort_ports() +{ + const bool sort_ports = this->sort_ports(); + _canvas->set_port_order(sort_ports ? port_order : NULL, NULL); + _conf->set_sort_ports(sort_ports); + refresh(); +} + +void +Patchage::on_zoom_in() +{ + const float zoom = _canvas->get_zoom() * 1.25; + _canvas->set_zoom(zoom); + _conf->set_zoom(zoom); +} + +void +Patchage::on_zoom_out() +{ + const float zoom = _canvas->get_zoom() * 0.75; + _canvas->set_zoom(zoom); + _conf->set_zoom(zoom); +} + +void +Patchage::on_zoom_normal() +{ + _canvas->set_zoom(1.0); + _conf->set_zoom(1.0); +} + +void +Patchage::on_zoom_full() +{ + _canvas->zoom_full(); + _conf->set_zoom(_canvas->get_zoom()); +} + +void +Patchage::on_increase_font_size() +{ + const float points = _canvas->get_font_size() + 1.0; + _canvas->set_font_size(points); + _conf->set_font_size(points); +} + +void +Patchage::on_decrease_font_size() +{ + const float points = _canvas->get_font_size() - 1.0; + _canvas->set_font_size(points); + _conf->set_font_size(points); +} + +void +Patchage::on_normal_font_size() +{ + _canvas->set_font_size(_canvas->get_default_font_size()); + _conf->set_font_size(_canvas->get_default_font_size()); +} + +static inline guint +highlight_color(guint c, guint delta) +{ + const guint max_char = 255; + const guint r = MIN((c >> 24) + delta, max_char); + const guint g = MIN(((c >> 16) & 0xFF) + delta, max_char); + const guint b = MIN(((c >> 8) & 0xFF) + delta, max_char); + const guint a = c & 0xFF; + + return ((((guint)(r)) << 24) | + (((guint)(g)) << 16) | + (((guint)(b)) << 8) | + (((guint)(a)))); +} + +static void +update_port_colors(GanvNode* node, void* data) +{ + Patchage* patchage = (Patchage*)data; + if (!GANV_IS_MODULE(node)) { + return; + } + + Ganv::Module* gmod = Glib::wrap(GANV_MODULE(node)); + PatchageModule* pmod = dynamic_cast<PatchageModule*>(gmod); + if (!pmod) { + return; + } + + for (PatchageModule::iterator i = pmod->begin(); i != pmod->end(); ++i) { + PatchagePort* port = dynamic_cast<PatchagePort*>(*i); + if (port) { + const uint32_t rgba = patchage->conf()->get_port_color(port->type()); + port->set_fill_color(rgba); + port->set_border_color(highlight_color(rgba, 0x20)); + } + } +} + +static void +update_edge_color(GanvEdge* edge, void* data) +{ + Patchage* patchage = (Patchage*)data; + Ganv::Edge* edgemm = Glib::wrap(edge); + + PatchagePort* tail = dynamic_cast<PatchagePort*>((edgemm)->get_tail()); + if (tail) { + edgemm->set_color(patchage->conf()->get_port_color(tail->type())); + } +} + +void +Patchage::on_legend_color_change(int id, const std::string& label, uint32_t rgba) +{ + _conf->set_port_color((PortType)id, rgba); + _canvas->for_each_node(update_port_colors, this); + _canvas->for_each_edge(update_edge_color, this); +} + +void +Patchage::on_messages_resized(Gtk::Allocation& alloc) +{ + const int max_pos = _main_paned->get_allocation().get_height(); + _conf->set_messages_height(max_pos - _main_paned->get_position()); +} + +void +Patchage::save() +{ + _conf->set_zoom(_canvas->get_zoom()); // Can be changed by ganv + _conf->save(); +} + +void +Patchage::on_quit() +{ +#ifdef HAVE_ALSA + _alsa_driver->detach(); +#endif +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + _jack_driver->detach(); +#endif + _main_win->hide(); +} + +void +Patchage::on_export_image() +{ + Gtk::FileChooserDialog dialog("Export Image", Gtk::FILE_CHOOSER_ACTION_SAVE); + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK); + dialog.set_default_response(Gtk::RESPONSE_OK); + dialog.set_transient_for(*_main_win); + + typedef std::map<std::string, std::string> Types; + Types types; + types["*.dot"] = "Graphviz DOT"; + types["*.pdf"] = "Portable Document Format"; + types["*.ps"] = "PostScript"; + types["*.svg"] = "Scalable Vector Graphics"; + for (Types::const_iterator t = types.begin(); t != types.end(); ++t) { + Gtk::FileFilter filt; + filt.add_pattern(t->first); + filt.set_name(t->second); + dialog.add_filter(filt); + } + + Gtk::CheckButton* bg_but = new Gtk::CheckButton("Draw _Background", true); + Gtk::Alignment* extra = new Gtk::Alignment(1.0, 0.5, 0.0, 0.0); + bg_but->set_active(true); + extra->add(*Gtk::manage(bg_but)); + extra->show_all(); + dialog.set_extra_widget(*Gtk::manage(extra)); + + if (dialog.run() == Gtk::RESPONSE_OK) { + const std::string filename = dialog.get_filename(); + if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS)) { + Gtk::MessageDialog confirm( + std::string("File exists! Overwrite ") + filename + "?", + true, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_YES_NO, true); + confirm.set_transient_for(dialog); + if (confirm.run() != Gtk::RESPONSE_YES) { + return; + } + } + _canvas->export_image(filename.c_str(), bg_but->get_active()); + } +} + +void +Patchage::on_view_messages() +{ + if (_menu_view_messages->get_active()) { + Glib::RefPtr<Gtk::TextBuffer> buffer = _status_text->get_buffer(); + if (!_pane_initialized) { + int y, line_height; + _status_text->get_line_yrange(buffer->begin(), y, line_height); + const int pad = _status_text->get_pixels_inside_wrap(); + const int max_pos = _main_paned->get_allocation().get_height(); + const int min_height = (line_height + 2 * pad); + const int conf_height = _conf->get_messages_height(); + _main_paned->set_position(max_pos - std::max(conf_height, min_height)); + _pane_initialized = true; + } + + _log_scrolledwindow->show(); + _status_text->scroll_to_mark( + _status_text->get_buffer()->get_insert(), 0); + _conf->set_show_messages(true); + } else { + _log_scrolledwindow->hide(); + _conf->set_show_messages(false); + } +} + +void +Patchage::on_view_toolbar() +{ + if (_menu_view_toolbar->get_active()) { + _toolbar->show(); + } else { + _toolbar->hide(); + } + _conf->set_show_toolbar(_menu_view_toolbar->get_active()); +} + +bool +Patchage::on_scroll(GdkEventScroll* ev) +{ + return false; +} + +void +Patchage::buffer_size_changed() +{ +#if defined(HAVE_JACK) || defined(HAVE_JACK_DBUS) + const int selected = _buf_size_combo->get_active_row_number(); + + if (selected == -1) { + update_toolbar(); + } else { + const jack_nframes_t buffer_size = 1 << (selected + 5); + _jack_driver->set_buffer_size(buffer_size); + update_toolbar(); + } +#endif +} + diff --git a/src/Patchage.hpp b/src/Patchage.hpp new file mode 100644 index 0000000..cf550f0 --- /dev/null +++ b/src/Patchage.hpp @@ -0,0 +1,212 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_PATCHAGE_HPP +#define PATCHAGE_PATCHAGE_HPP + +#include <set> +#include <string> + +#include <boost/shared_ptr.hpp> + +#include <gtkmm/aboutdialog.h> +#include <gtkmm/alignment.h> +#include <gtkmm/builder.h> +#include <gtkmm/button.h> +#include <gtkmm/checkmenuitem.h> +#include <gtkmm/combobox.h> +#include <gtkmm/dialog.h> +#include <gtkmm/imagemenuitem.h> +#include <gtkmm/label.h> +#include <gtkmm/main.h> +#include <gtkmm/menubar.h> +#include <gtkmm/menuitem.h> +#include <gtkmm/paned.h> +#include <gtkmm/progressbar.h> +#include <gtkmm/scrolledwindow.h> +#include <gtkmm/textview.h> +#include <gtkmm/toolbar.h> +#include <gtkmm/toolbutton.h> +#include <gtkmm/viewport.h> +#include <gtkmm/window.h> + +#include "patchage_config.h" +#include "Widget.hpp" +#include "Legend.hpp" + +class AlsaDriver; +class JackDriver; +class PatchageCanvas; +class Configuration; + +namespace Ganv { class Module; } + +class Patchage { +public: + Patchage(int argc, char** argv); + ~Patchage(); + + const boost::shared_ptr<PatchageCanvas>& canvas() const { return _canvas; } + + Gtk::Window* window() { return _main_win.get(); } + + Configuration* conf() const { return _conf; } + JackDriver* jack_driver() const { return _jack_driver; } +#ifdef HAVE_ALSA + AlsaDriver* alsa_driver() const { return _alsa_driver; } +#endif +#ifdef PATCHAGE_JACK_SESSION + void show_open_session_dialog(); + void show_save_session_dialog(); + void show_save_close_session_dialog(); +#endif + + Glib::RefPtr<Gtk::Builder> xml() { return _xml; } + + void attach(); + void save(); + void quit() { _main_win->hide(); } + + void refresh(); + inline void queue_refresh() { _refresh = true; } + inline void driver_detached() { _driver_detached = true; } + + void info_msg(const std::string& msg); + void error_msg(const std::string& msg); + void warning_msg(const std::string& msg); + + void update_state(); + void store_window_location(); + + bool show_human_names() const { return _menu_view_human_names->get_active(); } + bool sort_ports() const { return _menu_view_sort_ports->get_active(); } + +protected: + class BufferSizeColumns : public Gtk::TreeModel::ColumnRecord { + public: + BufferSizeColumns() { add(label); } + + Gtk::TreeModelColumn<Glib::ustring> label; + }; + + void connect_widgets(); + + void on_arrange(); + void on_sprung_layout_toggled(); + void on_help_about(); + void on_quit(); + void on_export_image(); + void on_view_messages(); + void on_view_toolbar(); + void on_store_positions(); + void on_view_human_names(); + void on_view_sort_ports(); + void on_zoom_in(); + void on_zoom_out(); + void on_zoom_normal(); + void on_zoom_full(); + void on_increase_font_size(); + void on_decrease_font_size(); + void on_normal_font_size(); + void on_legend_color_change(int id, const std::string& label, uint32_t rgba); + void on_messages_resized(Gtk::Allocation& alloc); + + bool on_scroll(GdkEventScroll* ev); + + void zoom(double z); + bool idle_callback(); + void clear_load(); + bool update_load(); + void update_toolbar(); + + void buffer_size_changed(); + + Glib::RefPtr<Gtk::Builder> _xml; + +#ifdef HAVE_ALSA + AlsaDriver* _alsa_driver; + void menu_alsa_connect(); + void menu_alsa_disconnect(); +#endif + +#ifdef PATCHAGE_JACK_SESSION + void save_session(bool close); +#endif + + boost::shared_ptr<PatchageCanvas> _canvas; + + JackDriver* _jack_driver; + Configuration* _conf; + + Gtk::Main* _gtk_main; + + BufferSizeColumns _buf_size_columns; + + Widget<Gtk::AboutDialog> _about_win; + Widget<Gtk::ScrolledWindow> _main_scrolledwin; + Widget<Gtk::Window> _main_win; + Widget<Gtk::VBox> _main_vbox; + Widget<Gtk::MenuBar> _menubar; + Widget<Gtk::MenuItem> _menu_alsa_connect; + Widget<Gtk::MenuItem> _menu_alsa_disconnect; + Widget<Gtk::MenuItem> _menu_file_quit; + Widget<Gtk::MenuItem> _menu_export_image; + Widget<Gtk::MenuItem> _menu_help_about; + Widget<Gtk::MenuItem> _menu_jack_connect; + Widget<Gtk::MenuItem> _menu_jack_disconnect; + Widget<Gtk::MenuItem> _menu_open_session; + Widget<Gtk::MenuItem> _menu_save_session; + Widget<Gtk::MenuItem> _menu_save_close_session; + Widget<Gtk::MenuItem> _menu_view_arrange; + Widget<Gtk::CheckMenuItem> _menu_view_sprung_layout; + Widget<Gtk::CheckMenuItem> _menu_view_messages; + Widget<Gtk::CheckMenuItem> _menu_view_toolbar; + Widget<Gtk::MenuItem> _menu_view_refresh; + Widget<Gtk::CheckMenuItem> _menu_view_human_names; + Widget<Gtk::CheckMenuItem> _menu_view_sort_ports; + Widget<Gtk::ImageMenuItem> _menu_zoom_in; + Widget<Gtk::ImageMenuItem> _menu_zoom_out; + Widget<Gtk::ImageMenuItem> _menu_zoom_normal; + Widget<Gtk::ImageMenuItem> _menu_zoom_full; + Widget<Gtk::MenuItem> _menu_increase_font_size; + Widget<Gtk::MenuItem> _menu_decrease_font_size; + Widget<Gtk::MenuItem> _menu_normal_font_size; + Widget<Gtk::Toolbar> _toolbar; + Widget<Gtk::ToolButton> _clear_load_but; + Widget<Gtk::ProgressBar> _xrun_progress; + Widget<Gtk::ComboBox> _buf_size_combo; + Widget<Gtk::Label> _latency_label; + Widget<Gtk::Alignment> _legend_alignment; + Widget<Gtk::Paned> _main_paned; + Widget<Gtk::ScrolledWindow> _log_scrolledwindow; + Widget<Gtk::TextView> _status_text; + Legend* _legend; + + Glib::RefPtr<Gtk::TextTag> _error_tag; + Glib::RefPtr<Gtk::TextTag> _warning_tag; + + bool _pane_initialized; + bool _attach; + bool _driver_detached; + bool _refresh; + bool _enable_refresh; + bool _jack_driver_autoattach; +#ifdef HAVE_ALSA + bool _alsa_driver_autoattach; +#endif +}; + +#endif // PATCHAGE_PATCHAGE_HPP diff --git a/src/PatchageCanvas.cpp b/src/PatchageCanvas.cpp new file mode 100644 index 0000000..4d63a4b --- /dev/null +++ b/src/PatchageCanvas.cpp @@ -0,0 +1,338 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <boost/format.hpp> + +#include "patchage_config.h" + +#if defined(HAVE_JACK_DBUS) + #include "JackDbusDriver.hpp" +#elif defined(PATCHAGE_LIBJACK) + #include "JackDriver.hpp" +#endif +#ifdef HAVE_ALSA + #include "AlsaDriver.hpp" +#endif + +#include "ganv/Edge.hpp" + +#include "Patchage.hpp" +#include "PatchageCanvas.hpp" +#include "PatchageModule.hpp" +#include "PatchagePort.hpp" + +using std::string; +using boost::format; + +PatchageCanvas::PatchageCanvas(Patchage* app, int width, int height) + : Ganv::Canvas(width, height) + , _app(app) +{ + signal_event.connect( + sigc::mem_fun(this, &PatchageCanvas::on_event)); + signal_connect.connect( + sigc::mem_fun(this, &PatchageCanvas::connect)); + signal_disconnect.connect( + sigc::mem_fun(this, &PatchageCanvas::disconnect)); +} + +PatchageModule* +PatchageCanvas::find_module(const string& name, ModuleType type) +{ + const ModuleIndex::const_iterator i = _module_index.find(name); + if (i == _module_index.end()) + return NULL; + + PatchageModule* io_module = NULL; + for (ModuleIndex::const_iterator j = i; j != _module_index.end() && j->first == name; ++j) { + if (j->second->type() == type) { + return j->second; + } else if (j->second->type() == InputOutput) { + io_module = j->second; + } + } + + // Return InputOutput module for Input or Output (or NULL if not found at all) + return io_module; +} + +void +PatchageCanvas::remove_module(const string& name) +{ + ModuleIndex::iterator i = _module_index.find(name); + while (i != _module_index.end()) { + PatchageModule* mod = i->second; + _module_index.erase(i); + i = _module_index.find(name); + delete mod; + } +} + +PatchagePort* +PatchageCanvas::find_port(const PortID& id) +{ + PatchagePort* pp = NULL; + + PortIndex::iterator i = _port_index.find(id); + if (i != _port_index.end()) { + assert(i->second->get_module()); + return i->second; + } + +#ifdef PATCHAGE_LIBJACK + // Alsa ports are always indexed (or don't exist at all) + if (id.type == PortID::JACK_ID) { + jack_port_t* jack_port = jack_port_by_id(_app->jack_driver()->client(), id.id.jack_id); + if (!jack_port) + return NULL; + + string module_name; + string port_name; + _app->jack_driver()->port_names(id, module_name, port_name); + + PatchageModule* module = find_module( + module_name, (jack_port_flags(jack_port) & JackPortIsInput) ? Input : Output); + + if (module) + pp = dynamic_cast<PatchagePort*>(module->get_port(port_name)); + + if (pp) + index_port(id, pp); + } +#endif // PATCHAGE_LIBJACK + + return pp; +} + +void +PatchageCanvas::remove_port(const PortID& id) +{ + PatchagePort* const port = find_port(id); + _port_index.erase(id); + delete port; +} + +struct RemovePortsData { + typedef bool (*Predicate)(const PatchagePort*); + + RemovePortsData(Predicate p) : pred(p) {} + + Predicate pred; + std::set<PatchageModule*> empty; +}; + +static void +delete_port_if_matches(GanvPort* port, void* cdata) +{ + RemovePortsData* data = (RemovePortsData*)cdata; + PatchagePort* pport = dynamic_cast<PatchagePort*>(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)); + PatchageModule* pmodule = dynamic_cast<PatchageModule*>(cmodule); + if (!pmodule) { + return; + } + + RemovePortsData* data = (RemovePortsData*)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 (PortIndex::iterator i = _port_index.begin(); + i != _port_index.end();) { + PortIndex::iterator next = i; + ++next; + if (pred(i->second)) { + _port_index.erase(i); + } + i = next; + } + + for (std::set<PatchageModule*>::iterator i = data.empty.begin(); + i != data.empty.end(); ++i) { + delete *i; + } +} + +PatchagePort* +PatchageCanvas::find_port_by_name(const std::string& client_name, + const std::string& port_name) +{ + const ModuleIndex::const_iterator i = _module_index.find(client_name); + if (i == _module_index.end()) + return NULL; + + for (ModuleIndex::const_iterator j = i; j != _module_index.end() && j->first == client_name; ++j) { + PatchagePort* port = dynamic_cast<PatchagePort*>(j->second->get_port(port_name)); + if (port) + return port; + } + + return NULL; +} + +void +PatchageCanvas::connect(Ganv::Node* port1, + Ganv::Node* port2) +{ + PatchagePort* p1 = dynamic_cast<PatchagePort*>(port1); + PatchagePort* p2 = dynamic_cast<PatchagePort*>(port2); + if (!p1 || !p2) + return; + + if ((p1->type() == JACK_AUDIO && p2->type() == JACK_AUDIO) || + (p1->type() == JACK_MIDI && p2->type() == JACK_MIDI) || + (p1->type() == JACK_AUDIO && p2->type() == JACK_CV) || + (p1->type() == JACK_CV && p2->type() == JACK_CV) || + (p1->type() == JACK_OSC && p2->type() == JACK_OSC)) { +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + _app->jack_driver()->connect(p1, p2); +#endif +#ifdef HAVE_ALSA + } else if (p1->type() == ALSA_MIDI && p2->type() == ALSA_MIDI) { + _app->alsa_driver()->connect(p1, p2); +#endif + } else { + _app->warning_msg("Cannot make connection, incompatible port types."); + } +} + +void +PatchageCanvas::disconnect(Ganv::Node* port1, + Ganv::Node* port2) +{ + PatchagePort* input = dynamic_cast<PatchagePort*>(port1); + PatchagePort* output = dynamic_cast<PatchagePort*>(port2); + if (!input || !output) + return; + + if (input->is_output() && output->is_input()) { + // Damn, guessed wrong + PatchagePort* swap = input; + input = output; + output = swap; + } + + if (!input || !output || input->is_output() || output->is_input()) { + _app->error_msg("Attempt to disconnect mismatched/unknown ports."); + return; + } + + if (input->type() == JACK_AUDIO || + input->type() == JACK_MIDI || + input->type() == JACK_CV || + input->type() == JACK_OSC) { +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + _app->jack_driver()->disconnect(output, input); +#endif +#ifdef HAVE_ALSA + } else if (input->type() == ALSA_MIDI) { + _app->alsa_driver()->disconnect(output, input); +#endif + } else { + _app->error_msg("Attempt to disconnect ports with strange types."); + } +} + +void +PatchageCanvas::add_module(const std::string& name, PatchageModule* module) +{ + _module_index.insert(std::make_pair(name, module)); + + // Join partners, if applicable + PatchageModule* in_module = NULL; + PatchageModule* out_module = NULL; + if (module->type() == Input) { + in_module = module; + out_module = find_module(name, Output); + } else if (module->type() == Output) { + in_module = find_module(name, Output); + out_module = module; + } + + if (in_module && out_module) + out_module->set_partner(in_module); +} + +static void +disconnect_edge(GanvEdge* edge, void* data) +{ + PatchageCanvas* canvas = (PatchageCanvas*)data; + Ganv::Edge* edgemm = Glib::wrap(edge); + canvas->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 (ModuleIndex::iterator i = _module_index.find(module->get_label()); + i != _module_index.end() && i->first == module->get_label(); ++i) { + if (i->second == module) { + _module_index.erase(i); + return; + } + } +} + +void +PatchageCanvas::clear() +{ + _port_index.clear(); + _module_index.clear(); + Ganv::Canvas::clear(); +} diff --git a/src/PatchageCanvas.hpp b/src/PatchageCanvas.hpp new file mode 100644 index 0000000..4b5fac1 --- /dev/null +++ b/src/PatchageCanvas.hpp @@ -0,0 +1,85 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_PATCHAGECANVAS_HPP +#define PATCHAGE_PATCHAGECANVAS_HPP + +#include <map> +#include <string> + +#include "patchage_config.h" + +#ifdef HAVE_ALSA + #include <alsa/asoundlib.h> +#endif + +#include "ganv/Canvas.hpp" + +#include "PatchageEvent.hpp" +#include "PatchageModule.hpp" +#include "PortID.hpp" + +class Patchage; +class PatchageModule; +class PatchagePort; + +class PatchageCanvas : public Ganv::Canvas { +public: + PatchageCanvas(Patchage* _app, int width, int height); + + PatchageModule* find_module(const std::string& name, ModuleType type); + PatchagePort* find_port(const PortID& id); + + void remove_module(const std::string& name); + void remove_module(PatchageModule* module); + + PatchagePort* find_port_by_name(const std::string& client_name, + const std::string& port_name); + + void connect(Ganv::Node* port1, + Ganv::Node* port2); + + void disconnect(Ganv::Node* port1, + Ganv::Node* port2); + + 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 std::string& name, PatchageModule* module); + + bool make_connection(Ganv::Node* tail, Ganv::Node* head); + + void remove_port(const PortID& id); + + void clear(); + +private: + Patchage* _app; + + bool on_event(GdkEvent* ev); + bool on_connection_event(Ganv::Edge* c, GdkEvent* ev); + + typedef std::map<const PortID, PatchagePort*> PortIndex; + PortIndex _port_index; + + typedef std::multimap<const std::string, PatchageModule*> ModuleIndex; + ModuleIndex _module_index; +}; + +#endif // PATCHAGE_PATCHAGECANVAS_HPP diff --git a/src/PatchageEvent.cpp b/src/PatchageEvent.cpp new file mode 100644 index 0000000..ea9a758 --- /dev/null +++ b/src/PatchageEvent.cpp @@ -0,0 +1,110 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include <boost/format.hpp> + +#include "patchage_config.h" +#include "Patchage.hpp" +#include "PatchageCanvas.hpp" +#include "PatchageModule.hpp" +#include "PatchageEvent.hpp" +#include "Driver.hpp" +#if defined(HAVE_JACK_DBUS) +# include "JackDbusDriver.hpp" +#elif defined(PATCHAGE_LIBJACK) +# include "JackDriver.hpp" +#endif +#ifdef HAVE_ALSA +# include "AlsaDriver.hpp" +#endif + +using std::endl; +using boost::format; + +void +PatchageEvent::execute(Patchage* patchage) +{ + if (_type == REFRESH) { + patchage->refresh(); + + } else if (_type == CLIENT_CREATION) { + // No empty modules (for now) + g_free(_str); + _str = NULL; + + } else if (_type == CLIENT_DESTRUCTION) { + patchage->canvas()->remove_module(_str); + g_free(_str); + _str = NULL; + + } else if (_type == PORT_CREATION) { + + Driver* driver = NULL; + if (_port_1.type == PortID::JACK_ID) { +#if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) + driver = patchage->jack_driver(); +#endif +#ifdef HAVE_ALSA + } else if (_port_1.type == PortID::ALSA_ADDR) { + driver = patchage->alsa_driver(); +#endif + } + + if (driver) { + PatchagePort* port = driver->create_port_view(patchage, _port_1); + if (!port) { + patchage->error_msg( + (format("Unable to create view for port `%1%'") + % _port_1).str()); + } + } else { + patchage->error_msg( + (format("Unknown type for port `%1%'") % _port_1).str()); + } + + } else if (_type == PORT_DESTRUCTION) { + + patchage->canvas()->remove_port(_port_1); + + } else if (_type == CONNECTION) { + + PatchagePort* port_1 = patchage->canvas()->find_port(_port_1); + PatchagePort* port_2 = patchage->canvas()->find_port(_port_2); + + if (!port_1) + patchage->error_msg((format("Unable to find port `%1%' to connect") + % _port_1).str()); + else if (!port_2) + patchage->error_msg((format("Unable to find port `%1%' to connect") + % _port_2).str()); + else + patchage->canvas()->make_connection(port_1, port_2); + + } else if (_type == DISCONNECTION) { + + PatchagePort* port_1 = patchage->canvas()->find_port(_port_1); + PatchagePort* port_2 = patchage->canvas()->find_port(_port_2); + + if (!port_1) + patchage->error_msg((format("Unable to find port `%1%' to disconnect") + % _port_1).str()); + else if (!port_2) + patchage->error_msg((format("Unable to find port `%1%' to disconnect") + % _port_2).str()); + else + patchage->canvas()->remove_edge_between(port_1, port_2); + } +} diff --git a/src/PatchageEvent.hpp b/src/PatchageEvent.hpp new file mode 100644 index 0000000..899f77f --- /dev/null +++ b/src/PatchageEvent.hpp @@ -0,0 +1,87 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_PATCHAGEEVENT_HPP +#define PATCHAGE_PATCHAGEEVENT_HPP + +#include <cstring> + +#include "patchage_config.h" + +#ifdef PATCHAGE_LIBJACK + #include <jack/jack.h> +#endif +#ifdef HAVE_ALSA + #include <alsa/asoundlib.h> +#endif + +#include "PatchagePort.hpp" +#include "PortID.hpp" + +class Patchage; + +/** A Driver event to be processed by the GUI thread. + */ +class PatchageEvent { +public: + enum Type { + NULL_EVENT = 0, + REFRESH, + CLIENT_CREATION, + CLIENT_DESTRUCTION, + PORT_CREATION, + PORT_DESTRUCTION, + CONNECTION, + DISCONNECTION + }; + + explicit PatchageEvent(Type type=NULL_EVENT) + : _str(NULL) + , _type(type) + {} + + PatchageEvent(Type type, const char* str) + : _str(g_strdup(str)) + , _type(type) + {} + + template <typename P> + PatchageEvent(Type type, P port) + : _str(NULL) + , _port_1(port) + , _type(type) + {} + + template <typename P> + PatchageEvent(Type type, P port_1, P port_2) + : _str(NULL) + , _port_1(port_1, false) + , _port_2(port_2, true) + , _type(type) + {} + + void execute(Patchage* patchage); + + inline Type type() const { return (Type)_type; } + +private: + char* _str; + PortID _port_1; + PortID _port_2; + uint8_t _type; +}; + +#endif // PATCHAGE_PATCHAGEEVENT_HPP diff --git a/src/PatchageModule.cpp b/src/PatchageModule.cpp new file mode 100644 index 0000000..8ba5296 --- /dev/null +++ b/src/PatchageModule.cpp @@ -0,0 +1,157 @@ +/* This file is part of Patchage. + * Copyright 2010-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#include "Patchage.hpp" +#include "PatchageCanvas.hpp" +#include "PatchageModule.hpp" +#include "PatchagePort.hpp" + +PatchageModule::PatchageModule( + Patchage* app, const std::string& name, ModuleType type, double x, double y) + : Module(*app->canvas().get(), name, x, y) + , _app(app) + , _menu(NULL) + , _name(name) + , _type(type) +{ + 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 = NULL; +} + +void +PatchageModule::update_menu() +{ + if (!_menu) + return; + + if (_type == InputOutput) { + bool has_in = false; + bool has_out = false; + for (const_iterator p = begin(); p != end(); ++p) { + 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 == InputOutput) { + 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) +{ + Coord loc(get_x(), get_y()); + _app->conf()->set_module_location(_name, _type, loc); +} + +void +PatchageModule::split() +{ + assert(_type == InputOutput); + _app->conf()->set_module_split(_name, true); + _app->refresh(); +} + +void +PatchageModule::join() +{ + assert(_type != InputOutput); + _app->conf()->set_module_split(_name, false); + _app->refresh(); +} + +void +PatchageModule::menu_disconnect_all() +{ + for (iterator p = begin(); p != end(); ++p) + (*p)->disconnect(); +} + +PatchagePort* +PatchageModule::get_port(const std::string& name) +{ + for (iterator p = begin(); p != end(); ++p) { + PatchagePort* pport = dynamic_cast<PatchagePort*>(*p); + if (pport && pport->name() == name) { + return pport; + } + } + + return NULL; +} diff --git a/src/PatchageModule.hpp b/src/PatchageModule.hpp new file mode 100644 index 0000000..99527ac --- /dev/null +++ b/src/PatchageModule.hpp @@ -0,0 +1,67 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_PATCHAGEMODULE_HPP +#define PATCHAGE_PATCHAGEMODULE_HPP + +#include <string> + +#include <gtkmm/menu_elems.h> + +#include "ganv/Module.hpp" +#include "ganv/Port.hpp" + +#include "Configuration.hpp" + +class Patchage; +class PatchagePort; + +class PatchageModule : public Ganv::Module +{ +public: + PatchageModule(Patchage* app, + const std::string& name, + ModuleType type, + double x = 0, + double y = 0); + ~PatchageModule(); + + void split(); + void join(); + + bool show_menu(GdkEventButton* ev); + void update_menu(); + + PatchagePort* get_port(const std::string& name); + + void load_location(); + void menu_disconnect_all(); + void show_dialog() {} + void store_location(double x, double y); + + ModuleType type() const { return _type; } + const std::string& name() const { return _name; } + +protected: + bool on_event(GdkEvent* ev); + + Patchage* _app; + Gtk::Menu* _menu; + std::string _name; + ModuleType _type; +}; + +#endif // PATCHAGE_PATCHAGEMODULE_HPP diff --git a/src/PatchagePort.hpp b/src/PatchagePort.hpp new file mode 100644 index 0000000..d5d6cb3 --- /dev/null +++ b/src/PatchagePort.hpp @@ -0,0 +1,104 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_PATCHAGEPORT_HPP +#define PATCHAGE_PATCHAGEPORT_HPP + +#include <string> + +#include <boost/shared_ptr.hpp> + +#include <gtkmm/menu.h> +#include <gtkmm/menushell.h> + +#include "ganv/Port.hpp" +#include "ganv/Module.hpp" + +#include "Configuration.hpp" +#include "PatchageCanvas.hpp" +#include "PatchageModule.hpp" +#include "PortID.hpp" +#include "patchage_config.h" + +/** A Port on a PatchageModule + */ +class PatchagePort : public Ganv::Port +{ +public: + PatchagePort(Ganv::Module& module, + PortType type, + const std::string& name, + const std::string& human_name, + bool is_input, + uint32_t color, + bool show_human_name, + boost::optional<int> order=boost::optional<int>()) + : Port(module, + (show_human_name && !human_name.empty()) ? human_name : name, + is_input, + color) + , _type(type) + , _name(name) + , _human_name(human_name) + , _order(order) + { + signal_event().connect( + sigc::mem_fun(this, &PatchagePort::on_event)); + } + + virtual ~PatchagePort() {} + + /** Returns the full name of this port, as "modulename:portname" */ + std::string full_name() const { + PatchageModule* pmod = dynamic_cast<PatchageModule*>(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) { + 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; } + const std::string& name() const { return _name; } + const std::string& human_name() const { return _human_name; } + const boost::optional<int>& order() const { return _order; } + +private: + PortType _type; + std::string _name; + std::string _human_name; + boost::optional<int> _order; +}; + +#endif // PATCHAGE_PATCHAGEPORT_HPP diff --git a/src/PortID.hpp b/src/PortID.hpp new file mode 100644 index 0000000..3f916c0 --- /dev/null +++ b/src/PortID.hpp @@ -0,0 +1,120 @@ +/* This file is part of Patchage. + * Copyright 2008-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_PORTID_HPP +#define PATCHAGE_PORTID_HPP + +#include <cstring> +#include <iostream> + +#include "patchage_config.h" + +#ifdef PATCHAGE_LIBJACK + #include <jack/jack.h> +#endif +#ifdef HAVE_ALSA + #include <alsa/asoundlib.h> +#endif + +#include "PatchagePort.hpp" + +struct PortID { + PortID() : type(NULL_PORT_ID) { memset(&id, 0, sizeof(id)); } + PortID(const PortID& copy) : type(copy.type) { + memcpy(&id, ©.id, sizeof(id)); + } + + enum { NULL_PORT_ID, JACK_ID, ALSA_ADDR } type; + +#ifdef PATCHAGE_LIBJACK + PortID(jack_port_id_t jack_id, bool ign=false) + : type(JACK_ID) { id.jack_id = jack_id; } +#endif + +#ifdef HAVE_ALSA + PortID(snd_seq_addr_t addr, bool in) + : type(ALSA_ADDR) { id.alsa_addr = addr; id.is_input = in; } +#endif + + union { +#ifdef PATCHAGE_LIBJACK + jack_port_id_t jack_id; +#endif +#ifdef HAVE_ALSA + struct { + snd_seq_addr_t alsa_addr; + bool is_input : 1; + }; +#endif + } id; +}; + +static inline std::ostream& +operator<<(std::ostream& os, const PortID& id) +{ + switch (id.type) { + case PortID::NULL_PORT_ID: + return os << "(null)"; + case PortID::JACK_ID: +#ifdef PATCHAGE_LIBJACK + return os << "jack:" << id.id.jack_id; +#endif + break; + case PortID::ALSA_ADDR: +#ifdef HAVE_ALSA + return os << "alsa:" << (int)id.id.alsa_addr.client << ":" << (int)id.id.alsa_addr.port + << ":" << (id.id.is_input ? "in" : "out"); +#endif + break; + } + assert(false); + return os; +} + +static inline bool +operator<(const PortID& a, const PortID& b) +{ + if (a.type != b.type) + return a.type < b.type; + + switch (a.type) { + case PortID::NULL_PORT_ID: + return true; + case PortID::JACK_ID: +#ifdef PATCHAGE_LIBJACK + return a.id.jack_id < b.id.jack_id; +#endif + break; + case PortID::ALSA_ADDR: +#ifdef HAVE_ALSA + if ((a.id.alsa_addr.client < b.id.alsa_addr.client) + || ((a.id.alsa_addr.client == b.id.alsa_addr.client) + && a.id.alsa_addr.port < b.id.alsa_addr.port)) { + return true; + } else if (a.id.alsa_addr.client == b.id.alsa_addr.client + && a.id.alsa_addr.port == b.id.alsa_addr.port) { + return (a.id.is_input < b.id.is_input); + } else { + return false; + } +#endif + break; + } + assert(false); + return false; +} + +#endif // PATCHAGE_PORTID_HPP diff --git a/src/Queue.hpp b/src/Queue.hpp new file mode 100644 index 0000000..ab47aed --- /dev/null +++ b/src/Queue.hpp @@ -0,0 +1,131 @@ +/* This file is part of Patchage. + * Copyright 2007-2017 David Robillard <http://drobilla.net> + * + * 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 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef QUEUE_HPP_INCLUDED +#define QUEUE_HPP_INCLUDED + +#include <atomic> +#include <cassert> + +/** Realtime-safe single-reader single-writer queue */ +template <typename T> +class Queue +{ +public: + /** @param size Size in number of elements */ + explicit Queue(size_t size); + ~Queue(); + + // Any thread: + + inline size_t capacity() const { return _size - 1; } + + // Write thread(s): + + inline bool full() const; + inline bool push(const T& obj); + + // Read thread: + + inline bool empty() const; + inline T& front() const; + inline void pop(); + +private: + std::atomic<size_t> _front; ///< Index to front of queue + std::atomic<size_t> _back; ///< Index to back of queue (one past end) + const size_t _size; ///< Size of `_objects` (at most _size-1) + T* const _objects; ///< Fixed array containing queued elements +}; + +template<typename T> +Queue<T>::Queue(size_t size) + : _front(0) + , _back(0) + , _size(size + 1) + , _objects(new T[_size]) +{ + assert(size > 1); +} + +template <typename T> +Queue<T>::~Queue() +{ + delete[] _objects; +} + +/** Return whether or not the queue is empty. + */ +template <typename T> +inline bool +Queue<T>::empty() const +{ + return (_back.load() == _front.load()); +} + +/** Return whether or not the queue is full. + */ +template <typename T> +inline bool +Queue<T>::full() const +{ + return (((_front.load() - _back.load() + _size) % _size) == 1); +} + +/** Return the element at the front of the queue without removing it + */ +template <typename T> +inline T& +Queue<T>::front() const +{ + return _objects[_front.load()]; +} + +/** Push an item onto the back of the Queue - realtime-safe, not thread-safe. + * + * @returns true if `elem` was successfully pushed onto the queue, + * false otherwise (queue is full). + */ +template <typename T> +inline bool +Queue<T>::push(const T& elem) +{ + if (full()) { + return false; + } else { + unsigned back = _back.load(); + _objects[back] = elem; + _back = (back + 1) % _size; + return true; + } +} + +/** Pop an item off the front of the queue - realtime-safe, not thread-safe. + * + * It is a fatal error to call pop() when the queue is empty. + * + * @returns the element popped. + */ +template <typename T> +inline void +Queue<T>::pop() +{ + assert(!empty()); + assert(_size > 0); + + _front = (_front.load() + 1) % (_size); +} + +#endif // QUEUE_HPP_INCLUDED diff --git a/src/UIFile.hpp b/src/UIFile.hpp new file mode 100644 index 0000000..f1ab5f8 --- /dev/null +++ b/src/UIFile.hpp @@ -0,0 +1,66 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_GLADEFILE_HPP +#define PATCHAGE_GLADEFILE_HPP + +#include <fstream> +#include <iostream> +#include <sstream> +#include <string> + +#include <gtkmm/builder.h> + +#include "patchage_config.h" +#ifdef PATCHAGE_BINLOC +#include "binary_location.h" +#endif + +class UIFile { +public: + inline static bool is_readable(const std::string& filename) { + std::ifstream fs(filename.c_str()); + const bool fail = fs.fail(); + fs.close(); + return !fail; + } + + static Glib::RefPtr<Gtk::Builder> open(const std::string& base_name) { + std::string ui_filename; +#ifdef PATCHAGE_BINLOC + const std::string bundle = bundle_location(); + if (!bundle.empty()) { + ui_filename = bundle + "/" + base_name + ".ui"; + if (is_readable(ui_filename)) { + std::cout << "Loading UI file " << ui_filename << std::endl; + return Gtk::Builder::create_from_file(ui_filename); + } + } +#endif + ui_filename = std::string(PATCHAGE_DATA_DIR) + "/" + base_name + ".ui"; + if (is_readable(ui_filename)) { + std::cout << "Loading UI file " << ui_filename << std::endl; + return Gtk::Builder::create_from_file(ui_filename); + } + + std::stringstream ss; + ss << "Unable to find " << base_name << std::endl; + throw std::runtime_error(ss.str()); + return Glib::RefPtr<Gtk::Builder>(); + } +}; + +#endif // PATCHAGE_GLADEFILE_HPP diff --git a/src/Widget.hpp b/src/Widget.hpp new file mode 100644 index 0000000..038f880 --- /dev/null +++ b/src/Widget.hpp @@ -0,0 +1,46 @@ +/* This file is part of Patchage + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef PATCHAGE_WIDGET_HPP +#define PATCHAGE_WIDGET_HPP + +#include <string> + +#include <boost/utility.hpp> + +#include <gtkmm/builder.h> + +template <typename W> +class Widget : public boost::noncopyable { +public: + Widget(Glib::RefPtr<Gtk::Builder> xml, const std::string& name) { + xml->get_widget(name, _me); + } + + void destroy() { delete _me; } + + W* get() { return _me; } + const W* get() const { return _me; } + W* operator->() { return _me; } + const W* operator->() const { return _me; } + W& operator*() { return *_me; } + const W& operator*() const { return *_me; } + +private: + W* _me; +}; + +#endif // PATCHAGE_WIDGET_HPP diff --git a/src/binary_location.h b/src/binary_location.h new file mode 100644 index 0000000..303a3bd --- /dev/null +++ b/src/binary_location.h @@ -0,0 +1,54 @@ +/* This file is part of Patchage. + * Copyright 2008-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifndef _GNU_SOURCE + #define _GNU_SOURCE +#endif + +#include <assert.h> +#include <limits.h> +#include <stdlib.h> +#include <dlfcn.h> + +#include <string> + +/** Return the absolute path of the binary. */ +static std::string +binary_location() +{ + Dl_info dli; + std::string loc; + const int ret = dladdr((void*)&binary_location, &dli); + if (ret) { + char* const bin_loc = (char*)calloc(PATH_MAX, 1); + if (realpath(dli.dli_fname, bin_loc)) { + loc = bin_loc; + } + free(bin_loc); + } + return loc; +} + +/** Return the absolute path of the bundle (binary parent directory). */ +static std::string +bundle_location() +{ + const std::string binary = binary_location(); + if (binary.empty()) { + return ""; + } + return binary.substr(0, binary.find_last_of('/')); +} diff --git a/src/jackey.h b/src/jackey.h new file mode 100644 index 0000000..02a7735 --- /dev/null +++ b/src/jackey.h @@ -0,0 +1,72 @@ +/* + Copyright 2014 David Robillard <http://drobilla.net> + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +/** + The supported event types of an event port. + + This is a kludge around Jack only supporting MIDI, particularly for OSC. + This property is a comma-separated list of event types, currently "MIDI" or + "OSC". If this contains "OSC", the port may carry OSC bundles (first byte + '#') or OSC messages (first byte '/'). Note that the "status byte" of both + OSC events is not a valid MIDI status byte, so MIDI clients that check the + status byte will gracefully ignore OSC messages if the user makes an + inappropriate connection. +*/ +#define JACKEY_EVENT_TYPES "http://jackaudio.org/metadata/event-types" + +/** + The type of an audio signal. + + This property allows audio ports to be tagged with a "meaning". The value + is a simple string. Currently, the only type is "CV", for "control voltage" + ports. Hosts SHOULD be take care to not treat CV ports as audibile and send + their output directly to speakers. In particular, CV ports are not + necessarily periodic at all and may have very high DC. +*/ +#define JACKEY_SIGNAL_TYPE "http://jackaudio.org/metadata/signal-type" + +/** + The name of the icon for the subject (typically client). + + This is used for looking up icons on the system, possibly with many sizes or + themes. Icons should be searched for according to the freedesktop Icon + Theme Specification: + + http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html +*/ +#define JACKEY_ICON_NAME "http://jackaudio.org/metadata/icon-name" + +/** + Channel designation for a port. + + This allows ports to be tagged with a meaningful designation like "left", + "right", "lfe", etc. + + The value MUST be a URI. An extensive set of URIs for designating audio + channels can be found at http://lv2plug.in/ns/ext/port-groups +*/ +#define JACKEY_DESIGNATION "http://lv2plug.in/ns/lv2core#designation" + +/** + Order for a port. + + This is used to specify the best order to show ports in user interfaces. + The value MUST be an integer. There are no other requirements, so there may + be gaps in the orders for several ports. Applications should compare the + orders of ports to determine their relative order, but must not assign any + other relevance to order values. +*/ +#define JACKEY_ORDER "http://jackaudio.org/metadata/order" diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..4822d3d --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,93 @@ +/* This file is part of Patchage. + * Copyright 2007-2014 David Robillard <http://drobilla.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +#ifdef __APPLE__ +#include <stdlib.h> +#include <unistd.h> +#include <string> +#include <gtk/gtkrc.h> +#include "binary_location.h" +#endif + +#include <iostream> + +#include <glibmm/exception.h> + +#include "Patchage.hpp" + +#ifdef __APPLE__ +void +set_bundle_environment() +{ + const std::string bundle = bundle_location(); + const std::string lib_path = bundle + "/lib"; + if (!Glib::file_test(lib_path, Glib::FILE_TEST_EXISTS)) { + // If lib does not exist, we have not been bundleified, do nothing + return; + } + + setenv("GTK_PATH", lib_path.c_str(), 1); + setenv("DYLD_LIBRARY_PATH", lib_path.c_str(), 1); + + const std::string pangorc_path(bundle + "/Resources/pangorc"); + if (Glib::file_test(pangorc_path, Glib::FILE_TEST_EXISTS)) { + setenv("PANGO_RC_FILE", pangorc_path.c_str(), 1); + } + + const std::string fonts_conf_path(bundle + "/Resources/fonts.conf"); + if (Glib::file_test(fonts_conf_path, Glib::FILE_TEST_EXISTS)) { + setenv("FONTCONFIG_FILE", fonts_conf_path.c_str(), 1); + } + + const std::string loaders_cache_path(bundle + "/Resources/loaders.cache"); + if (Glib::file_test(loaders_cache_path, Glib::FILE_TEST_EXISTS)) { + setenv("GDK_PIXBUF_MODULE_FILE", loaders_cache_path.c_str(), 1); + } + + const std::string gtkrc_path(bundle + "/Resources/gtkrc"); + if (Glib::file_test(gtkrc_path, Glib::FILE_TEST_EXISTS)) { + gtk_rc_parse(gtkrc_path.c_str()); + } +} +#endif + +int +main(int argc, char** argv) +{ +#ifdef __APPLE__ + set_bundle_environment(); +#endif + + try { + + Glib::thread_init(); + + Gtk::Main app(argc, argv); + + Patchage patchage(argc, argv); + app.run(*patchage.window()); + patchage.save(); + + } catch (std::exception& e) { + std::cerr << "patchage: error: " << e.what() << std::endl; + return 1; + } catch (Glib::Exception& e) { + std::cerr << "patchage: error: " << e.what() << std::endl; + return 1; + } + + return 0; +} diff --git a/src/patchage.gladep b/src/patchage.gladep new file mode 100644 index 0000000..8d205c3 --- /dev/null +++ b/src/patchage.gladep @@ -0,0 +1,9 @@ +<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*--> +<!DOCTYPE glade-project SYSTEM "http://glade.gnome.org/glade-project-2.0.dtd"> + +<glade-project> + <name>Patchage</name> + <program_name>patchage</program_name> + <language>C++</language> + <gnome_support>FALSE</gnome_support> +</glade-project> diff --git a/src/patchage.svg b/src/patchage.svg new file mode 120000 index 0000000..ce73588 --- /dev/null +++ b/src/patchage.svg @@ -0,0 +1 @@ +../icons/scalable/patchage.svg
\ No newline at end of file diff --git a/src/patchage.ui b/src/patchage.ui new file mode 100644 index 0000000..355d4dd --- /dev/null +++ b/src/patchage.ui @@ -0,0 +1,1260 @@ +<?xml version="1.0" encoding="UTF-8"?> +<interface> + <requires lib="gtk+" version="2.24"/> + <!-- interface-naming-policy toplevel-contextual --> + <object class="GtkWindow" id="main_win"> + <property name="can_focus">False</property> + <property name="border_width">1</property> + <property name="title" translatable="yes">Patchage</property> + <child> + <object class="GtkVBox" id="main_vbox"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <child> + <object class="GtkMenuBar" id="menubar"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <child> + <object class="GtkMenuItem" id="file_menu"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_File</property> + <property name="use_underline">True</property> + <child type="submenu"> + <object class="GtkMenu" id="file_menu_menu"> + <property name="can_focus">False</property> + <child> + <object class="GtkImageMenuItem" id="menu_open_session"> + <property name="label">gtk-open</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">True</property> + <accelerator key="O" signal="activate" modifiers="GDK_CONTROL_MASK"/> + <signal name="activate" handler="on_open_session_menuitem_activate" swapped="no"/> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_save_session"> + <property name="label">gtk-save</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">True</property> + <accelerator key="s" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkMenuItem" id="menu_save_close_session"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">Save and _Close</property> + <property name="use_underline">True</property> + </object> + </child> + <child> + <object class="GtkMenuItem" id="menu_export_image"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_Export Image...</property> + <property name="use_underline">True</property> + <accelerator key="e" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkSeparatorMenuItem" id="menu_file_quit_sep"> + <property name="visible">True</property> + <property name="can_focus">False</property> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_file_quit"> + <property name="label">gtk-quit</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">True</property> + <accelerator key="q" signal="activate" modifiers="GDK_CONTROL_MASK"/> + <signal name="activate" handler="on_quit1_activate" swapped="no"/> + </object> + </child> + </object> + </child> + </object> + </child> + <child> + <object class="GtkMenuItem" id="menu_file_system"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_System</property> + <property name="use_underline">True</property> + <child type="submenu"> + <object class="GtkMenu" id="file_system_menuitem_menu"> + <property name="can_focus">False</property> + <child> + <object class="GtkImageMenuItem" id="menu_jack_connect"> + <property name="label">Connect to _Jack</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">False</property> + <accelerator key="J" signal="activate" modifiers="GDK_CONTROL_MASK"/> + <signal name="activate" handler="on_menu_jack_connect_activate" swapped="no"/> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_jack_disconnect"> + <property name="label">Disconnect from Jack</property> + <property name="visible">True</property> + <property name="sensitive">False</property> + <property name="can_focus">False</property> + <property name="use_stock">False</property> + <accelerator key="J" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/> + <signal name="activate" handler="on_disconnect_from_jack1_activate" swapped="no"/> + </object> + </child> + <child> + <object class="GtkSeparatorMenuItem" id="separator4"> + <property name="visible">True</property> + <property name="can_focus">False</property> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_alsa_connect"> + <property name="label">Connect to _Alsa</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">False</property> + <accelerator key="A" signal="activate" modifiers="GDK_CONTROL_MASK"/> + <signal name="activate" handler="on_menu_alsa_connect_activate" swapped="no"/> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_alsa_disconnect"> + <property name="label">Disconnect from ALSA</property> + <property name="visible">True</property> + <property name="sensitive">False</property> + <property name="can_focus">False</property> + <property name="use_stock">False</property> + <accelerator key="A" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/> + <signal name="activate" handler="on_menu_alsa_disconnect_activate" swapped="no"/> + </object> + </child> + </object> + </child> + </object> + </child> + <child> + <object class="GtkMenuItem" id="view_menu"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_View</property> + <property name="use_underline">True</property> + <child type="submenu"> + <object class="GtkMenu" id="view_menu_menu"> + <property name="can_focus">False</property> + <child> + <object class="GtkCheckMenuItem" id="menu_view_messages"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_Messages</property> + <property name="use_underline">True</property> + <accelerator key="M" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkCheckMenuItem" id="menu_view_toolbar"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">Tool_bar</property> + <property name="use_underline">True</property> + <property name="active">True</property> + <accelerator key="b" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkSeparatorMenuItem" id="menuitem0"> + <property name="visible">True</property> + <property name="can_focus">False</property> + </object> + </child> + <child> + <object class="GtkCheckMenuItem" id="menu_view_human_names"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_Human Names</property> + <property name="use_underline">True</property> + <property name="active">True</property> + <accelerator key="H" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkCheckMenuItem" id="menu_view_sort_ports"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_Sort Ports by Name</property> + <property name="use_underline">True</property> + <property name="active">True</property> + <accelerator key="S" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkSeparatorMenuItem" id="menuitem1"> + <property name="visible">True</property> + <property name="can_focus">False</property> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_zoom_in"> + <property name="label">gtk-zoom-in</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">True</property> + <accelerator key="plus" signal="activate" modifiers="GDK_CONTROL_MASK"/> + <accelerator key="equal" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_zoom_out"> + <property name="label">gtk-zoom-out</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">True</property> + <accelerator key="minus" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_zoom_normal"> + <property name="label">gtk-zoom-100</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">True</property> + <accelerator key="0" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_zoom_full"> + <property name="label">gtk-zoom-fit</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">True</property> + <accelerator key="F" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkSeparatorMenuItem" id="menuitem2"> + <property name="visible">True</property> + <property name="can_focus">False</property> + </object> + </child> + <child> + <object class="GtkMenuItem" id="menu_increase_font_size"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_Increase Font Size</property> + <property name="use_underline">True</property> + <accelerator key="Up" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkMenuItem" id="menu_decrease_font_size"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_Decrease Font Size</property> + <property name="use_underline">True</property> + <accelerator key="Down" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkMenuItem" id="menu_normal_font_size"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_Normal Font Size</property> + <property name="use_underline">True</property> + <accelerator key="1" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + <child> + <object class="GtkSeparatorMenuItem" id="menuitem3"> + <property name="visible">True</property> + <property name="can_focus">False</property> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_view_refresh"> + <property name="label">gtk-refresh</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">True</property> + <accelerator key="R" signal="activate" modifiers="GDK_CONTROL_MASK"/> + <signal name="activate" handler="on_refresh2_activate" swapped="no"/> + </object> + </child> + <child> + <object class="GtkImageMenuItem" id="menu_view_arrange"> + <property name="label">_Arrange</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">False</property> + <accelerator key="G" signal="activate" modifiers="GDK_CONTROL_MASK"/> + <signal name="activate" handler="on_menu_view_arrange" swapped="no"/> + </object> + </child> + <child> + <object class="GtkCheckMenuItem" id="menu_view_sprung_layout"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">Sprung Layou_t</property> + <property name="use_underline">True</property> + <accelerator key="t" signal="activate" modifiers="GDK_CONTROL_MASK"/> + </object> + </child> + </object> + </child> + </object> + </child> + <child> + <object class="GtkMenuItem" id="help_menu"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes">_Help</property> + <property name="use_underline">True</property> + <child type="submenu"> + <object class="GtkMenu" id="help_menu_menu"> + <property name="can_focus">False</property> + <child> + <object class="GtkImageMenuItem" id="menu_help_about"> + <property name="label">gtk-about</property> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="use_underline">True</property> + <property name="use_stock">True</property> + <signal name="activate" handler="on_about1_activate" swapped="no"/> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + <packing> + <property name="expand">False</property> + <property name="fill">False</property> + <property name="position">0</property> + </packing> + </child> + <child> + <object class="GtkToolbar" id="toolbar"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="toolbar_style">icons</property> + <property name="show_arrow">False</property> + <property name="icon_size">1</property> + <property name="icon_size_set">True</property> + <child> + <object class="GtkToolButton" id="clear_load_but"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="has_tooltip">True</property> + <property name="tooltip_markup">Clear the dropout indicator</property> + <property name="tooltip_text" translatable="yes">Clear dropout indicator.</property> + <property name="stock_id">gtk-clear</property> + </object> + <packing> + <property name="expand">False</property> + <property name="homogeneous">True</property> + </packing> + </child> + <child> + <object class="GtkToolItem" id="toolitem30"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <child> + <object class="GtkAlignment" id="alignment3"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="yscale">0</property> + <child> + <object class="GtkProgressBar" id="xrun_progress"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="has_tooltip">True</property> + <property name="tooltip_markup">Drouput (XRun) Indicator + +The bar represents the percentage of available time used for audio processing (i.e. the DSP load). If the bar reaches 100%, a dropout will occur.</property> + <property name="tooltip_text" translatable="yes">Load and dropout gauge. The bar shows the percentage of available time used for audio processing. If it reaches 100%, a dropout will occur, and the bar is reset. Click to reset.</property> + <property name="show_text">True</property> + <property name="pulse_step">0.10000000149</property> + <property name="text" translatable="yes">0 Dropouts</property> + <property name="discrete_blocks">100</property> + </object> + </child> + </object> + </child> + </object> + <packing> + <property name="expand">False</property> + </packing> + </child> + <child> + <object class="GtkToolItem" id="toolitem28"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="visible_vertical">False</property> + <child> + <object class="GtkAlignment" id="alignment2"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="tooltip_text" translatable="yes">Jack buffer size and sample rate.</property> + <property name="yscale">0</property> + <child> + <object class="GtkHBox" id="hbox4"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <child> + <object class="GtkLabel" id="label10"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="label" translatable="yes"> / </property> + </object> + <packing> + <property name="expand">False</property> + <property name="fill">False</property> + <property name="position">0</property> + </packing> + </child> + <child> + <object class="GtkComboBox" id="buf_size_combo"> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="has_tooltip">True</property> + <property name="tooltip_markup">Jack buffer length in frames</property> + <property name="tooltip_text" translatable="yes">Jack buffer length in frames</property> + <property name="border_width">1</property> + </object> + <packing> + <property name="expand">False</property> + <property name="fill">False</property> + <property name="position">1</property> + </packing> + </child> + <child> + <object class="GtkLabel" id="latency_label"> + <property name="can_focus">False</property> + <property name="label" translatable="yes">frames @ ? kHz (? ms)</property> + </object> + <packing> + <property name="expand">False</property> + <property name="fill">False</property> + <property name="padding">1</property> + <property name="position">2</property> + </packing> + </child> + </object> + </child> + </object> + </child> + </object> + <packing> + <property name="expand">False</property> + </packing> + </child> + <child> + <object class="GtkToolItem" id="toolitem1"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <child> + <object class="GtkAlignment" id="legend_alignment"> + <property name="visible">True</property> + <property name="can_focus">False</property> + <property name="xalign">1</property> + <property name="xscale">0</property> + <child> + <placeholder/> + </child> + </object> + </child> + </object> + <packing> + <property name="expand">True</property> + </packing> + </child> + </object> + <packing> + <property name="expand">False</property> + <property name="fill">True</property> + <property name="position">1</property> + </packing> + </child> + <child> + <object class="GtkVPaned" id="main_paned"> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="position">3200</property> + <child> + <object class="GtkScrolledWindow" id="main_scrolledwin"> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="can_default">True</property> + <property name="has_default">True</property> + <property name="receives_default">True</property> + <property name="shadow_type">in</property> + <child> + <placeholder/> + </child> + </object> + <packing> + <property name="resize">True</property> + <property name="shrink">False</property> + </packing> + </child> + <child> + <object class="GtkScrolledWindow" id="log_scrolledwindow"> + <property name="can_focus">True</property> + <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property> + <property name="hscrollbar_policy">never</property> + <property name="vscrollbar_policy">automatic</property> + <property name="shadow_type">in</property> + <child> + <object class="GtkTextView" id="status_text"> + <property name="visible">True</property> + <property name="sensitive">False</property> + <property name="can_focus">False</property> + <property name="editable">False</property> + <property name="wrap_mode">word</property> + <property name="cursor_visible">False</property> + <property name="accepts_tab">False</property> + </object> + </child> + </object> + <packing> + <property name="resize">False</property> + <property name="shrink">True</property> + </packing> + </child> + </object> + <packing> + <property name="expand">True</property> + <property name="fill">True</property> + <property name="position">2</property> + </packing> + </child> + </object> + </child> + </object> + <object class="GtkAboutDialog" id="about_win"> + <property name="can_focus">False</property> + <property name="destroy_with_parent">True</property> + <property name="type_hint">dialog</property> + <property name="transient_for">main_win</property> + <property name="program_name">Patchage</property> + <property name="version">@PATCHAGE_VERSION@</property> + <property name="copyright" translatable="yes">© 2005-2017 David Robillard +© 2008 Nedko Arnaudov</property> + <property name="comments" translatable="yes">A JACK and ALSA front-end.</property> + <property name="website">http://drobilla.net/software/patchage</property> + <property name="license" translatable="yes"> GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program 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. + + This program 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 more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<http://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<http://www.gnu.org/philosophy/why-not-lgpl.html>.</property> + <property name="authors">David Robillard <d@drobilla.net> +Nedko Arnaudov <nedko@arnaudov.name></property> + <property name="translator_credits" translatable="yes" comments="TRANSLATORS: Replace this string with your names, one name per line.">translator-credits</property> + <property name="artists">Icon: + Lapo Calamandrei</property> + <property name="logo_icon_name">patchage</property> + <child internal-child="vbox"> + <object class="GtkVBox" id="dialog-vbox1"> + <property name="can_focus">False</property> + <child internal-child="action_area"> + <object class="GtkHButtonBox" id="dialog-action_area1"> + <property name="can_focus">False</property> + </object> + <packing> + <property name="expand">False</property> + <property name="fill">True</property> + <property name="pack_type">end</property> + <property name="position">0</property> + </packing> + </child> + </object> + </child> + </object> +</interface> |