bindings: Support a bindings file in $XDG_CONFIG_HOME/ncmpcpp

Fixes https://github.com/arybczak/ncmpcpp/issues/91

This also rewrites the intro in doc/bindings to be more like doc/config.
This commit is contained in:
Wieland Hoffmann
2016-11-01 20:31:36 +01:00
committed by Andrzej Rybczak
parent 67df6b556b
commit e107edd4cd
4 changed files with 39 additions and 25 deletions

View File

@@ -1,7 +1,8 @@
########################################################## ##############################################################
## this is example bindings configuration file, copy it ## ## This is the example bindings file. Copy it to ##
## to ~/.ncmpcpp/bindings and set up your preferences ## ## ~/.ncmpcpp/bindings or $XDG_CONFIG_HOME/ncmpcpp/bindings ##
########################################################## ## and set up your preferences ##
##############################################################
## ##
##### General rules ##### ##### General rules #####
## ##

View File

@@ -467,6 +467,17 @@ bool BindingsConfiguration::read(const std::string &file)
return result; return result;
} }
bool BindingsConfiguration::read(const std::vector<std::string> &binding_paths)
{
return std::all_of(
binding_paths.begin(),
binding_paths.end(),
[&](const std::string &binding_path) {
return read(binding_path);
}
);
}
void BindingsConfiguration::generateDefaults() void BindingsConfiguration::generateDefaults()
{ {
NC::Key::Type k = NC::Key::None; NC::Key::Type k = NC::Key::None;

View File

@@ -34,7 +34,7 @@ std::wstring keyToWString(const NC::Key::Type key);
struct Binding struct Binding
{ {
typedef std::vector<std::shared_ptr<Actions::BaseAction>> ActionChain; typedef std::vector<std::shared_ptr<Actions::BaseAction>> ActionChain;
template <typename ArgT> template <typename ArgT>
Binding(ArgT &&actions_) Binding(ArgT &&actions_)
: m_actions(std::forward<ArgT>(actions_)) { : m_actions(std::forward<ArgT>(actions_)) {
@@ -42,13 +42,13 @@ struct Binding
} }
Binding(Actions::Type at) Binding(Actions::Type at)
: Binding(ActionChain({Actions::get_(at)})) { } : Binding(ActionChain({Actions::get_(at)})) { }
bool execute() const { bool execute() const {
return std::all_of(m_actions.begin(), m_actions.end(), return std::all_of(m_actions.begin(), m_actions.end(),
std::bind(&Actions::BaseAction::execute, std::placeholders::_1) std::bind(&Actions::BaseAction::execute, std::placeholders::_1)
); );
} }
bool isSingle() const { bool isSingle() const {
return m_actions.size() == 1; return m_actions.size() == 1;
} }
@@ -73,10 +73,10 @@ struct Command
template <typename ArgT> template <typename ArgT>
Command(ArgT &&binding_, bool immediate_) Command(ArgT &&binding_, bool immediate_)
: m_impl(std::forward<ArgT>(binding_), immediate_) { } : m_impl(std::forward<ArgT>(binding_), immediate_) { }
const Binding &binding() const { return std::get<0>(m_impl); } const Binding &binding() const { return std::get<0>(m_impl); }
bool immediate() const { return std::get<1>(m_impl); } bool immediate() const { return std::get<1>(m_impl); }
private: private:
std::tuple<Binding, bool> m_impl; std::tuple<Binding, bool> m_impl;
}; };
@@ -86,31 +86,33 @@ class BindingsConfiguration
{ {
typedef std::unordered_map<std::string, Command> CommandsSet; typedef std::unordered_map<std::string, Command> CommandsSet;
typedef std::unordered_map<NC::Key::Type, std::vector<Binding>> BindingsMap; typedef std::unordered_map<NC::Key::Type, std::vector<Binding>> BindingsMap;
public: public:
typedef BindingsMap::value_type::second_type::iterator BindingIterator; typedef BindingsMap::value_type::second_type::iterator BindingIterator;
typedef BindingsMap::value_type::second_type::const_iterator ConstBindingIterator; typedef BindingsMap::value_type::second_type::const_iterator ConstBindingIterator;
typedef std::pair<BindingIterator, BindingIterator> BindingIteratorPair; typedef std::pair<BindingIterator, BindingIterator> BindingIteratorPair;
bool read(const std::string &file); bool read(const std::vector<std::string> &binding_paths);
void generateDefaults(); void generateDefaults();
const Command *findCommand(const std::string &name); const Command *findCommand(const std::string &name);
BindingIteratorPair get(const NC::Key::Type &k); BindingIteratorPair get(const NC::Key::Type &k);
BindingsMap::const_iterator begin() const { return m_bindings.begin(); } BindingsMap::const_iterator begin() const { return m_bindings.begin(); }
BindingsMap::const_iterator end() const { return m_bindings.end(); } BindingsMap::const_iterator end() const { return m_bindings.end(); }
private: private:
bool notBound(const NC::Key::Type &k) const { bool notBound(const NC::Key::Type &k) const {
return k != NC::Key::None && m_bindings.find(k) == m_bindings.end(); return k != NC::Key::None && m_bindings.find(k) == m_bindings.end();
} }
template <typename ArgT> template <typename ArgT>
void bind(NC::Key::Type k, ArgT &&t) { void bind(NC::Key::Type k, ArgT &&t) {
m_bindings[k].push_back(std::forward<ArgT>(t)); m_bindings[k].push_back(std::forward<ArgT>(t));
} }
bool read(const std::string &file);
BindingsMap m_bindings; BindingsMap m_bindings;
CommandsSet m_commands; CommandsSet m_commands;
}; };

View File

@@ -74,7 +74,12 @@ bool configure(int argc, char **argv)
xdg_config_home() + "ncmpcpp/config" xdg_config_home() + "ncmpcpp/config"
}; };
std::string bindings_path; const std::vector<std::string> default_bindings_paths = {
"~/.ncmpcpp/bindings",
xdg_config_home() + "ncmpcpp/bindings"
};
std::vector<std::string> bindings_paths;
std::vector<std::string> config_paths; std::vector<std::string> config_paths;
po::options_description options("Options"); po::options_description options("Options");
@@ -85,7 +90,7 @@ bool configure(int argc, char **argv)
("config,c", po::value<std::vector<std::string>>(&config_paths)->value_name("PATH")->default_value(default_config_paths, join<std::string>(default_config_paths, " AND ")), "specify configuration file(s)") ("config,c", po::value<std::vector<std::string>>(&config_paths)->value_name("PATH")->default_value(default_config_paths, join<std::string>(default_config_paths, " AND ")), "specify configuration file(s)")
("ignore-config-errors", "ignore unknown and invalid options in configuration files") ("ignore-config-errors", "ignore unknown and invalid options in configuration files")
("test-lyrics-fetchers", "check if lyrics fetchers work") ("test-lyrics-fetchers", "check if lyrics fetchers work")
("bindings,b", po::value<std::string>(&bindings_path)->value_name("PATH")->default_value("~/.ncmpcpp/bindings"), "specify bindings file") ("bindings,b", po::value<std::vector<std::string>>(&bindings_paths)->value_name("PATH")->default_value(default_bindings_paths, join<std::string>(default_bindings_paths, " AND ")), "specify bindings file(s)")
("screen,s", po::value<std::string>()->value_name("SCREEN"), "specify the startup screen") ("screen,s", po::value<std::string>()->value_name("SCREEN"), "specify the startup screen")
("slave-screen,S", po::value<std::string>()->value_name("SCREEN"), "specify the startup slave screen") ("slave-screen,S", po::value<std::string>()->value_name("SCREEN"), "specify the startup slave screen")
("help,?", "show help message") ("help,?", "show help message")
@@ -190,14 +195,9 @@ bool configure(int argc, char **argv)
if (Config.read(config_paths, vm.count("ignore-config-errors")) == false) if (Config.read(config_paths, vm.count("ignore-config-errors")) == false)
exit(1); exit(1);
// if bindings file was not specified, use the one from main directory.
if (vm["bindings"].defaulted())
bindings_path = Config.ncmpcpp_directory + "bindings";
else
expand_home(bindings_path);
// read bindings // read bindings
if (Bindings.read(bindings_path) == false) std::for_each(bindings_paths.begin(), bindings_paths.end(), expand_home);
if (Bindings.read(bindings_paths) == false)
exit(1); exit(1);
Bindings.generateDefaults(); Bindings.generateDefaults();