bindinds: add support for alt/ctrl/shift modifiers and escape key
[ncmpcpp.git] / src / bindings.h
blob35d3e3dd7ce309a295941f166eac26de32eb367d
1 /***************************************************************************
2 * Copyright (C) 2008-2014 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<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 return m_actions[0];
61 private:
62 ActionChain m_actions;
65 /// Represents executable command
66 struct Command
68 template <typename ArgT>
69 Command(ArgT &&binding_, bool immediate_)
70 : m_impl(std::forward<ArgT>(binding_), immediate_) { }
72 const Binding &binding() const { return std::get<0>(m_impl); }
73 bool immediate() const { return std::get<1>(m_impl); }
75 private:
76 std::tuple<Binding, bool> m_impl;
79 /// Keybindings configuration
80 class BindingsConfiguration
82 typedef std::unordered_map<std::string, Command> CommandsSet;
83 typedef std::unordered_map<NC::Key::Type, std::vector<Binding>> BindingsMap;
85 public:
86 typedef BindingsMap::value_type::second_type::iterator BindingIterator;
87 typedef BindingsMap::value_type::second_type::const_iterator ConstBindingIterator;
88 typedef std::pair<BindingIterator, BindingIterator> BindingIteratorPair;
90 bool read(const std::string &file);
91 void generateDefaults();
93 const Command *findCommand(const std::string &name);
94 BindingIteratorPair get(const NC::Key::Type &k);
96 BindingsMap::const_iterator begin() const { return m_bindings.begin(); }
97 BindingsMap::const_iterator end() const { return m_bindings.end(); }
99 private:
100 bool notBound(const NC::Key::Type &k) const {
101 return k != NC::Key::None && m_bindings.find(k) == m_bindings.end();
104 template <typename ArgT>
105 void bind(NC::Key::Type k, ArgT &&t) {
106 m_bindings[k].push_back(std::forward<ArgT>(t));
109 BindingsMap m_bindings;
110 CommandsSet m_commands;
113 extern BindingsConfiguration Bindings;
115 #endif // NCMPCPP_BINDINGS_H