plugin: convert plugin_fd_add() to method
[ncmpc-debian.git] / src / Bindings.cxx
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2019 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include "Bindings.hxx"
21 #include "Command.hxx"
22 #include "KeyName.hxx"
23 #include "i18n.h"
24 #include "util/CharUtil.hxx"
25
26 #include <assert.h>
27 #include <stdio.h>
28
29 const char *
30 KeyBindings::GetFirstKeyName(Command command) const noexcept
31 {
32         const auto &b = key_bindings[size_t(command)];
33         return b.keys.front() != 0
34                 ? key2str(b.keys[0])
35                 : nullptr;
36 }
37
38 std::string
39 KeyBindings::GetKeyNames(Command command) const noexcept
40 {
41         const auto &b = key_bindings[size_t(command)];
42
43         std::string s = key2str(b.keys[0]);
44
45         for (size_t i = 1; i < b.keys.size() && b.keys[i] != 0; ++i) {
46                 s.push_back(' ');
47                 s.append(key2str(b.keys[i]));
48         }
49         return s;
50 }
51
52 Command
53 KeyBindings::FindKey(int key) const noexcept
54 {
55         assert(key != 0);
56
57         for (size_t i = 0; i < size_t(Command::NONE); ++i) {
58                 if (key_bindings[i].HasKey(key))
59                         return Command(i);
60         }
61
62         return Command::NONE;
63 }
64
65 #ifndef NCMPC_MINI
66
67 bool
68 KeyBindings::Check(char *buf, size_t bufsize) const noexcept
69 {
70         bool success = true;
71
72         for (size_t i = 0; i < size_t(Command::NONE); ++i) {
73                 for (const auto key : key_bindings[i].keys) {
74                         if (key == 0)
75                                 break;
76
77                         Command cmd;
78                         if ((cmd = FindKey(key)) != Command(i)) {
79                                 if (buf) {
80                                         snprintf(buf, bufsize,
81                                                  _("Key %s assigned to %s and %s"),
82                                                  key2str(key),
83                                                  get_key_command_name(Command(i)),
84                                                  get_key_command_name(cmd));
85                                 } else {
86                                         fprintf(stderr,
87                                                 _("Key %s assigned to %s and %s"),
88                                                 key2str(key),
89                                                 get_key_command_name(Command(i)),
90                                                 get_key_command_name(cmd));
91                                         fputc('\n', stderr);
92                                 }
93                                 success = false;
94                         }
95                 }
96         }
97
98         return success;
99 }
100
101 void
102 KeyBinding::WriteToFile(FILE *f, const command_definition_t &cmd,
103                         bool comment) const noexcept
104 {
105         fprintf(f, "## %s\n", cmd.description);
106         if (comment)
107                 fprintf(f, "#");
108         fprintf(f, "key %s = ", cmd.name);
109
110         if (keys.front() == 0) {
111                 fputs("0\n\n", f);
112                 return;
113         }
114
115         bool first = true;
116         for (const auto key : keys) {
117                 if (key == 0)
118                         break;
119
120                 if (first)
121                         first = false;
122                 else
123                         fprintf(f, ",  ");
124
125                 if (key < 256 && IsAlphaNumericASCII(key))
126                         fprintf(f, "\'%c\'", key);
127                 else
128                         fprintf(f, "%d", key);
129         }
130         fprintf(f,"\n\n");
131 }
132
133 bool
134 KeyBindings::WriteToFile(FILE *f, int flags) const noexcept
135 {
136         const auto *cmds = get_command_definitions();
137
138         if (flags & KEYDEF_WRITE_HEADER)
139                 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
140
141         for (size_t i = 0; i < size_t(Command::NONE) && !ferror(f); ++i) {
142                 if (key_bindings[i].modified || flags & KEYDEF_WRITE_ALL) {
143                         key_bindings[i].WriteToFile(f, cmds[i],
144                                                     flags & KEYDEF_COMMENT_ALL);
145                 }
146         }
147
148         return ferror(f) == 0;
149 }
150
151 #endif /* NCMPC_MINI */