From 59780bd8f9abf376a79ee192325ae868df7d1b5a Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 30 Mar 2014 23:41:21 +0000 Subject: StateManager => Configuration. git-svn-id: http://svn.drobilla.net/lad/trunk/patchage@5348 a436a847-0d15-0410-975c-d299462d15a1 --- src/AlsaDriver.cpp | 8 +- src/Configuration.cpp | 290 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Configuration.hpp | 76 +++++++++++++ src/JackDriver.cpp | 9 +- src/Patchage.cpp | 28 +++-- src/Patchage.hpp | 12 +- src/PatchageCanvas.hpp | 1 - src/PatchageModule.cpp | 8 +- src/PatchageModule.hpp | 2 +- src/PatchagePort.hpp | 4 +- src/StateManager.cpp | 290 ------------------------------------------------- src/StateManager.hpp | 76 ------------- 12 files changed, 401 insertions(+), 403 deletions(-) create mode 100644 src/Configuration.cpp create mode 100644 src/Configuration.hpp delete mode 100644 src/StateManager.cpp delete mode 100644 src/StateManager.hpp (limited to 'src') diff --git a/src/AlsaDriver.cpp b/src/AlsaDriver.cpp index d894883..b4b43d7 100644 --- a/src/AlsaDriver.cpp +++ b/src/AlsaDriver.cpp @@ -268,11 +268,11 @@ AlsaDriver::create_port_view_internal( bool split = false; if (is_duplex) { split = true; - if (!_app->state_manager()->get_module_split(client_name, !is_application)) { - _app->state_manager()->set_module_split(client_name, true); + if (!_app->configuration()->get_module_split(client_name, !is_application)) { + _app->configuration()->set_module_split(client_name, true); } } else { - split = _app->state_manager()->get_module_split(client_name, !is_application); + split = _app->configuration()->get_module_split(client_name, !is_application); } /*cout << "ALSA PORT: " << client_name << " : " << port_name @@ -312,7 +312,7 @@ AlsaDriver::create_port(PatchageModule& parent, { PatchagePort* ret = new PatchagePort( parent, ALSA_MIDI, name, is_input, - _app->state_manager()->get_port_color(ALSA_MIDI)); + _app->configuration()->get_port_color(ALSA_MIDI)); dynamic_cast(parent.canvas())->index_port( PortID(addr, is_input), ret); diff --git a/src/Configuration.cpp b/src/Configuration.cpp new file mode 100644 index 0000000..ae621cb --- /dev/null +++ b/src/Configuration.cpp @@ -0,0 +1,290 @@ +/* This file is part of Patchage. + * Copyright 2007-2013 David Robillard + * + * Patchage is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License + * along with Patchage. If not, see . + */ + +#include +#include + +#include +#include +#include +#include +#include + +#include "Configuration.hpp" +#include "Patchage.hpp" + +Configuration::Configuration() + : _window_location(0, 0) + , _window_size(640, 480) + , _zoom(1.0) +{ +} + +bool +Configuration::get_module_location(const std::string& name, ModuleType type, Coord& loc) +{ + std::map::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::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, @a 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::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 +get_filenames() +{ + std::vector 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 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 ommitted 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 == "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::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::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 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; + + for (std::map::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.input_location && settings.inout_location) { + write_module_position(file, name, "inputoutput", *settings.inout_location); + } + } + } + + file.close(); +} + +float +Configuration::get_zoom() +{ + return _zoom; +} + +void +Configuration::set_zoom(float zoom) +{ + _zoom = zoom; +} + +int +Configuration::get_port_color(PortType type) +{ + // Darkest tango palette colour, with S -= 6, V -= 6, w/ transparency + + if (type == JACK_AUDIO) + return 0x244678FF; + else if (type == JACK_MIDI) + return 0x960909FF; + else if (type == ALSA_MIDI) + return 0x4A8A0EFF; + else + return 0xFF0000FF; +} + diff --git a/src/Configuration.hpp b/src/Configuration.hpp new file mode 100644 index 0000000..b169c34 --- /dev/null +++ b/src/Configuration.hpp @@ -0,0 +1,76 @@ +/* This file is part of Patchage. + * Copyright 2007-2013 David Robillard + * + * Patchage is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License + * along with Patchage. If not, see . + */ + +#ifndef PATCHAGE_CONFIGURATION_HPP +#define PATCHAGE_CONFIGURATION_HPP + +#include +#include +#include + +#include + +enum ModuleType { Input, Output, InputOutput }; + +enum PortType { JACK_AUDIO, JACK_MIDI, ALSA_MIDI }; + +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(); + void set_zoom(float zoom); + + int get_port_color(PortType type); + + 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 input_location; + boost::optional output_location; + boost::optional inout_location; + bool split; + }; + + std::map _module_settings; + + Coord _window_location; + Coord _window_size; + float _zoom; +}; + +#endif // PATCHAGE_CONFIGURATION_HPP diff --git a/src/JackDriver.cpp b/src/JackDriver.cpp index b8ea2a4..9638ace 100644 --- a/src/JackDriver.cpp +++ b/src/JackDriver.cpp @@ -147,7 +147,7 @@ JackDriver::create_port_view(Patchage* patchage, port_names(id, module_name, port_name); ModuleType type = InputOutput; - if (_app->state_manager()->get_module_split( + if (_app->configuration()->get_module_split( module_name, (jack_flags & JackPortIsTerminal))) { if (jack_flags & JackPortIsInput) { type = Input; @@ -194,7 +194,7 @@ JackDriver::create_port(PatchageModule& parent, jack_port_t* port, PortID id) PatchagePort* ret( new PatchagePort(parent, port_type, jack_port_short_name(port), (jack_port_flags(port) & JackPortIsInput), - _app->state_manager()->get_port_color(port_type))); + _app->configuration()->get_port_color(port_type))); if (id.type != PortID::NULL_PORT_ID) { dynamic_cast(parent.canvas())->index_port(id, ret); @@ -248,8 +248,9 @@ JackDriver::refresh() client1_name = client1_name.substr(0, client1_name.find(":")); ModuleType type = InputOutput; - if (_app->state_manager()->get_module_split(client1_name, - (jack_port_flags(port) & JackPortIsTerminal))) { + if (_app->configuration()->get_module_split( + client1_name, + (jack_port_flags(port) & JackPortIsTerminal))) { if (jack_port_flags(port) & JackPortIsInput) { type = Input; } else { diff --git a/src/Patchage.cpp b/src/Patchage.cpp index 4531cad..cb2bbda 100644 --- a/src/Patchage.cpp +++ b/src/Patchage.cpp @@ -39,7 +39,7 @@ #include "Patchage.hpp" #include "PatchageCanvas.hpp" #include "PatchageEvent.hpp" -#include "StateManager.hpp" +#include "Configuration.hpp" #if defined(HAVE_JACK_DBUS) #include "JackDbusDriver.hpp" @@ -82,7 +82,7 @@ Patchage::Patchage(int argc, char** argv) , _alsa_driver(NULL) #endif , _jack_driver(NULL) - , _state_manager(NULL) + , _configuration(NULL) , INIT_WIDGET(_about_win) , INIT_WIDGET(_main_scrolledwin) , INIT_WIDGET(_main_win) @@ -119,7 +119,7 @@ Patchage::Patchage(int argc, char** argv) , _alsa_driver_autoattach(true) #endif { - _state_manager = new StateManager(); + _configuration = new Configuration(); _canvas = boost::shared_ptr(new PatchageCanvas(this, 1600*2, 1200*2)); while (argc > 0) { @@ -220,15 +220,15 @@ Patchage::Patchage(int argc, char** argv) _canvas->widget().show(); _main_win->present(); - _state_manager->load(); + _configuration->load(); _main_win->resize( - static_cast(_state_manager->get_window_size().x), - static_cast(_state_manager->get_window_size().y)); + static_cast(_configuration->get_window_size().x), + static_cast(_configuration->get_window_size().y)); _main_win->move( - static_cast(_state_manager->get_window_location().x), - static_cast(_state_manager->get_window_location().y)); + static_cast(_configuration->get_window_location().x), + static_cast(_configuration->get_window_location().y)); _about_win->set_transient_for(*_main_win); @@ -272,7 +272,7 @@ Patchage::Patchage(int argc, char** argv) Patchage::~Patchage() { store_window_location(); - _state_manager->save(); + _configuration->save(); #if defined(PATCHAGE_LIBJACK) || defined(HAVE_JACK_DBUS) delete _jack_driver; @@ -281,7 +281,7 @@ Patchage::~Patchage() delete _alsa_driver; #endif - delete _state_manager; + delete _configuration; _about_win.destroy(); _messages_win.destroy(); @@ -355,7 +355,7 @@ Patchage::idle_callback() void Patchage::zoom(double z) { - _state_manager->set_zoom(z); + _configuration->set_zoom(z); _canvas->set_zoom(z); } @@ -377,8 +377,6 @@ Patchage::refresh() } } -/** Update the stored window location and size in the StateManager (in memory). - */ void Patchage::store_window_location() { @@ -391,8 +389,8 @@ Patchage::store_window_location() Coord window_size; window_size.x = size_x; window_size.y = size_y; - _state_manager->set_window_location(window_location); - _state_manager->set_window_size(window_size); + _configuration->set_window_location(window_location); + _configuration->set_window_size(window_size); } void diff --git a/src/Patchage.hpp b/src/Patchage.hpp index ece59b0..e78c511 100644 --- a/src/Patchage.hpp +++ b/src/Patchage.hpp @@ -44,7 +44,7 @@ class AlsaDriver; class JackDriver; class PatchageCanvas; -class StateManager; +class Configuration; namespace Ganv { class Module; } @@ -57,10 +57,10 @@ public: Gtk::Window* window() { return _main_win.get(); } - StateManager* state_manager() const { return _state_manager; } - JackDriver* jack_driver() const { return _jack_driver; } + Configuration* configuration() const { return _configuration; } + JackDriver* jack_driver() const { return _jack_driver; } #ifdef HAVE_ALSA - AlsaDriver* alsa_driver() const { return _alsa_driver; } + AlsaDriver* alsa_driver() const { return _alsa_driver; } #endif #ifdef PATCHAGE_JACK_SESSION void show_open_session_dialog(); @@ -124,8 +124,8 @@ protected: boost::shared_ptr _canvas; - JackDriver* _jack_driver; - StateManager* _state_manager; + JackDriver* _jack_driver; + Configuration* _configuration; Gtk::Main* _gtk_main; diff --git a/src/PatchageCanvas.hpp b/src/PatchageCanvas.hpp index 6f1a88b..078bc63 100644 --- a/src/PatchageCanvas.hpp +++ b/src/PatchageCanvas.hpp @@ -31,7 +31,6 @@ #include "PatchageEvent.hpp" #include "PatchageModule.hpp" #include "PortID.hpp" -#include "StateManager.hpp" class Patchage; class PatchageModule; diff --git a/src/PatchageModule.cpp b/src/PatchageModule.cpp index 3c16352..0d2d77b 100644 --- a/src/PatchageModule.cpp +++ b/src/PatchageModule.cpp @@ -106,7 +106,7 @@ PatchageModule::load_location() { Coord loc; - if (_app->state_manager()->get_module_location(_name, _type, loc)) + if (_app->configuration()->get_module_location(_name, _type, loc)) move_to(loc.x, loc.y); else move_to(20 + rand() % 640, @@ -117,14 +117,14 @@ void PatchageModule::store_location(double x, double y) { Coord loc(get_x(), get_y()); - _app->state_manager()->set_module_location(_name, _type, loc); + _app->configuration()->set_module_location(_name, _type, loc); } void PatchageModule::split() { assert(_type == InputOutput); - _app->state_manager()->set_module_split(_name, true); + _app->configuration()->set_module_split(_name, true); _app->refresh(); } @@ -132,7 +132,7 @@ void PatchageModule::join() { assert(_type != InputOutput); - _app->state_manager()->set_module_split(_name, false); + _app->configuration()->set_module_split(_name, false); _app->refresh(); } diff --git a/src/PatchageModule.hpp b/src/PatchageModule.hpp index c46cc2c..67e80d1 100644 --- a/src/PatchageModule.hpp +++ b/src/PatchageModule.hpp @@ -24,7 +24,7 @@ #include "ganv/Module.hpp" #include "ganv/Port.hpp" -#include "StateManager.hpp" +#include "Configuration.hpp" class Patchage; class PatchagePort; diff --git a/src/PatchagePort.hpp b/src/PatchagePort.hpp index 24617f9..0ddbc6e 100644 --- a/src/PatchagePort.hpp +++ b/src/PatchagePort.hpp @@ -27,10 +27,10 @@ #include "ganv/Port.hpp" #include "ganv/Module.hpp" -#include "patchage_config.h" +#include "Configuration.hpp" #include "PatchageCanvas.hpp" #include "PortID.hpp" -#include "StateManager.hpp" +#include "patchage_config.h" /** A Port on a PatchageModule */ diff --git a/src/StateManager.cpp b/src/StateManager.cpp deleted file mode 100644 index 62d9ea7..0000000 --- a/src/StateManager.cpp +++ /dev/null @@ -1,290 +0,0 @@ -/* This file is part of Patchage. - * Copyright 2007-2013 David Robillard - * - * Patchage is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * - * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License - * along with Patchage. If not, see . - */ - -#include -#include - -#include -#include -#include -#include -#include - -#include "StateManager.hpp" -#include "Patchage.hpp" - -StateManager::StateManager() - : _window_location(0, 0) - , _window_size(640, 480) - , _zoom(1.0) -{ -} - -bool -StateManager::get_module_location(const std::string& name, ModuleType type, Coord& loc) -{ - std::map::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 -StateManager::set_module_location(const std::string& name, ModuleType type, Coord loc) -{ - std::map::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, @a default_val is returned (this is - * to allow driver's to request terminal ports get split by default). - */ -bool -StateManager::get_module_split(const std::string& name, bool default_val) const -{ - std::map::const_iterator i = _module_settings.find(name); - if (i == _module_settings.end()) { - return default_val; - } - - return (*i).second.split; -} - -void -StateManager::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 -get_filenames() -{ - std::vector 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 -StateManager::load() -{ - // Try to find a readable configuration file - const std::vector 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 ommitted 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 == "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::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::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 -StateManager::save() -{ - // Try to find a writable configuration file - const std::vector 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; - - for (std::map::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.input_location && settings.inout_location) { - write_module_position(file, name, "inputoutput", *settings.inout_location); - } - } - } - - file.close(); -} - -float -StateManager::get_zoom() -{ - return _zoom; -} - -void -StateManager::set_zoom(float zoom) -{ - _zoom = zoom; -} - -int -StateManager::get_port_color(PortType type) -{ - // Darkest tango palette colour, with S -= 6, V -= 6, w/ transparency - - if (type == JACK_AUDIO) - return 0x244678FF; - else if (type == JACK_MIDI) - return 0x960909FF; - else if (type == ALSA_MIDI) - return 0x4A8A0EFF; - else - return 0xFF0000FF; -} - diff --git a/src/StateManager.hpp b/src/StateManager.hpp deleted file mode 100644 index d8c1b4b..0000000 --- a/src/StateManager.hpp +++ /dev/null @@ -1,76 +0,0 @@ -/* This file is part of Patchage. - * Copyright 2007-2013 David Robillard - * - * Patchage is free software: you can redistribute it and/or modify it under - * the terms of the GNU General Public License as published by the Free - * Software Foundation, either version 3 of the License, or (at your option) - * any later version. - * - * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. - * - * You should have received a copy of the GNU General Public License - * along with Patchage. If not, see . - */ - -#ifndef PATCHAGE_STATEMANAGER_HPP -#define PATCHAGE_STATEMANAGER_HPP - -#include -#include -#include - -#include - -enum ModuleType { Input, Output, InputOutput }; - -enum PortType { JACK_AUDIO, JACK_MIDI, ALSA_MIDI }; - -struct Coord { - Coord(double x_=0, double y_=0) : x(x_), y(y_) {} - double x; - double y; -}; - -class StateManager -{ -public: - StateManager(); - - 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(); - void set_zoom(float zoom); - - int get_port_color(PortType type); - - 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 input_location; - boost::optional output_location; - boost::optional inout_location; - bool split; - }; - - std::map _module_settings; - - Coord _window_location; - Coord _window_size; - float _zoom; -}; - -#endif // PATCHAGE_STATEMANAGER_HPP -- cgit v1.2.1