Config: reformat all descriptions to fill 80 columns
[ncmpcpp.git] / src / bindings.h
blob6cf277c6259e1708286b75eb7a6d2a3f9a7dc677
1 /***************************************************************************
2 * Copyright (C) 2008-2017 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef NCMPCPP_BINDINGS_H
22 #define NCMPCPP_BINDINGS_H
24 #include <algorithm>
25 #include <cassert>
26 #include <unordered_map>
27 #include "actions.h"
28 #include "macro_utilities.h"
30 NC::Key::Type readKey(NC::Window &w);
31 std::wstring keyToWString(const NC::Key::Type key);
33 /// Represents either single action or chain of actions bound to a certain key
34 struct Binding
36 typedef std::vector<std::shared_ptr<Actions::BaseAction>> ActionChain;
38 template <typename ArgT>
39 Binding(ArgT &&actions_)
40 : m_actions(std::forward<ArgT>(actions_)) {
41 assert(!m_actions.empty());
43 Binding(Actions::Type at)
44 : Binding(ActionChain({Actions::get_(at)})) { }
46 bool execute() const {
47 return std::all_of(m_actions.begin(), m_actions.end(),
48 std::bind(&Actions::BaseAction::execute, std::placeholders::_1)
52 bool isSingle() const {
53 return m_actions.size() == 1;
56 Actions::BaseAction &action() const {
57 assert(isSingle());
58 assert(m_actions[0] != nullptr);
59 return *m_actions[0];
62 const ActionChain &actions() const {
63 return m_actions;
66 private:
67 ActionChain m_actions;
70 /// Represents executable command
71 struct Command
73 template <typename ArgT>
74 Command(ArgT &&binding_, bool immediate_)
75 : m_impl(std::forward<ArgT>(binding_), immediate_) { }
77 const Binding &binding() const { return std::get<0>(m_impl); }
78 bool immediate() const { return std::get<1>(m_impl); }
80 private:
81 std::tuple<Binding, bool> m_impl;
84 /// Keybindings configuration
85 class BindingsConfiguration
87 typedef std::unordered_map<std::string, Command> CommandsSet;
88 typedef std::unordered_map<NC::Key::Type, std::vector<Binding>> BindingsMap;
90 public:
91 typedef BindingsMap::value_type::second_type::iterator BindingIterator;
92 typedef BindingsMap::value_type::second_type::const_iterator ConstBindingIterator;
93 typedef std::pair<BindingIterator, BindingIterator> BindingIteratorPair;
95 bool read(const std::string &file);
96 void generateDefaults();
98 const Command *findCommand(const std::string &name);
99 BindingIteratorPair get(const NC::Key::Type &k);
101 BindingsMap::const_iterator begin() const { return m_bindings.begin(); }
102 BindingsMap::const_iterator end() const { return m_bindings.end(); }
104 private:
105 bool notBound(const NC::Key::Type &k) const {
106 return k != NC::Key::None && m_bindings.find(k) == m_bindings.end();
109 template <typename ArgT>
110 void bind(NC::Key::Type k, ArgT &&t) {
111 m_bindings[k].push_back(std::forward<ArgT>(t));
114 BindingsMap m_bindings;
115 CommandsSet m_commands;
118 extern BindingsConfiguration Bindings;
120 #endif // NCMPCPP_BINDINGS_H