forked from ncmpcpp/ncmpcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.cpp
More file actions
273 lines (246 loc) · 8.7 KB
/
configuration.cpp
File metadata and controls
273 lines (246 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/***************************************************************************
* Copyright (C) 2008-2021 by Andrzej Rybczak *
* andrzej@rybczak.net *
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <algorithm>
#include <boost/algorithm/string/trim.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/program_options.hpp>
#include <iomanip>
#include <iostream>
#include <fstream>
#include "bindings.h"
#include "configuration.h"
#include "config.h"
#include "mpdpp.h"
#include "format_impl.h"
#include "settings.h"
#include "utility/string.h"
namespace po = boost::program_options;
using std::cerr;
using std::cout;
namespace {
const char *env_home;
std::string xdg_config_home()
{
std::string result;
const char *env_xdg_config_home = getenv("XDG_CONFIG_HOME");
if (env_xdg_config_home == nullptr)
result = "~/.config/";
else
{
result = env_xdg_config_home;
if (!result.empty() && result.back() != '/')
result += "/";
}
return result;
}
}
void expand_home(std::string &path)
{
assert(env_home != nullptr);
if (!path.empty())
{
size_t i = path.find("~");
if (i != std::string::npos && (i == 0 || path[i - 1] == '@'))
path.replace(i, 1, env_home);
}
}
bool configure(int argc, char **argv)
{
const std::vector<std::string> default_config_paths = {
xdg_config_home() + "ncmpcpp/config",
"~/.ncmpcpp/config"
};
const std::vector<std::string> default_bindings_paths = {
xdg_config_home() + "ncmpcpp/bindings",
"~/.ncmpcpp/bindings"
};
std::vector<std::string> bindings_paths;
std::vector<std::string> config_paths;
po::options_description options("Options");
options.add_options()
("host,h", po::value<std::string>()->value_name("HOST")->default_value("localhost"), "connect to server at host")
("port,p", po::value<int>()->value_name("PORT")->default_value(6600), "connect to server at port")
("current-song", po::value<std::string>()->value_name("FORMAT")->implicit_value("{{{(%l) }{{%a - }%t}}|{%f}}"), "print current song using given format and exit")
("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")
("test-lyrics-fetchers", "check if lyrics fetchers work")
("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")
("slave-screen,S", po::value<std::string>()->value_name("SCREEN"), "specify the startup slave screen")
("help,?", "show help message")
("version,v", "display version information")
("quiet,q", "suppress logs and excess output")
;
po::variables_map vm;
try
{
po::store(po::parse_command_line(argc, argv, options), vm);
// suppress messages from std::clog
if (vm.count("quiet"))
std::clog.rdbuf(nullptr);
if (vm.count("help"))
{
cout << "Usage: " << argv[0] << " [options]...\n" << options << "\n";
return false;
}
if (vm.count("version"))
{
std::cout << "ncmpcpp " << VERSION << "\n\n"
<< "optional screens compiled-in:\n"
# ifdef HAVE_TAGLIB_H
<< " - tag editor\n"
<< " - tiny tag editor\n"
# endif
# ifdef ENABLE_OUTPUTS
<< " - outputs\n"
# endif
# ifdef ENABLE_VISUALIZER
<< " - visualizer\n"
# endif
# ifdef ENABLE_CLOCK
<< " - clock\n"
# endif
<< "\nencoding detection: "
# ifdef HAVE_LANGINFO_H
<< "enabled"
# else
<< "disabled"
# endif // HAVE_LANGINFO_H
<< "\nbuilt with support for:"
# ifdef HAVE_FFTW3_H
<< " fftw"
# endif
<< " ncurses"
# ifdef HAVE_TAGLIB_H
<< " taglib"
# endif
<< "\n";
return false;
}
po::notify(vm);
if (vm.count("test-lyrics-fetchers"))
{
std::vector<std::tuple<std::string, std::string, std::string>> fetcher_data = {
std::make_tuple("genius", "rihanna", "umbrella"),
std::make_tuple("justsomelyrics", "rihanna", "umbrella"),
std::make_tuple("jahlyrics", "sean kingston", "dry your eyes"),
std::make_tuple("plyrics", "rihanna", "umbrella"),
std::make_tuple("tekstowo", "rihanna", "umbrella"),
std::make_tuple("zeneszoveg", "rihanna", "umbrella"),
};
for (auto &data : fetcher_data)
{
auto fetcher = boost::lexical_cast<LyricsFetcher_>(std::get<0>(data));
std::cout << std::setw(20)
<< std::left
<< fetcher->name()
<< " : "
<< std::flush;
auto result = fetcher->fetch(std::get<1>(data), std::get<2>(data), {});
std::cout << (result.first ? "ok" : "failed")
<< "\n";
}
exit(0);
}
// get home directory
env_home = getenv("HOME");
if (env_home == nullptr)
{
cerr << "Fatal error: HOME environment variable is not defined\n";
return false;
}
// read configuration
std::for_each(config_paths.begin(), config_paths.end(), expand_home);
if (Config.read(config_paths, vm.count("ignore-config-errors")) == false)
exit(1);
// read bindings
std::for_each(bindings_paths.begin(), bindings_paths.end(), expand_home);
if (Bindings.read(bindings_paths) == false)
exit(1);
Bindings.generateDefaults();
// create directories
boost::filesystem::create_directories(Config.ncmpcpp_directory);
boost::filesystem::create_directory(Config.lyrics_directory);
// try to get MPD connection details from environment variables
// as they take precedence over these from the configuration.
auto env_host = getenv("MPD_HOST");
auto env_port = getenv("MPD_PORT");
if (env_host != nullptr)
Mpd.SetHostname(env_host);
if (env_port != nullptr)
{
auto trimmed_env_port = boost::trim_copy<std::string>(env_port);
try {
Mpd.SetPort(boost::lexical_cast<int>(trimmed_env_port));
} catch (boost::bad_lexical_cast &) {
throw std::runtime_error("MPD_PORT environment variable ("
+ std::string(env_port)
+ ") is not a number");
}
}
// if MPD connection details are provided as command line
// parameters, use them as their priority is the highest.
if (!vm["host"].defaulted())
Mpd.SetHostname(vm["host"].as<std::string>());
if (!vm["port"].defaulted())
Mpd.SetPort(vm["port"].as<int>());
Mpd.SetTimeout(Config.mpd_connection_timeout);
// print current song
if (vm.count("current-song"))
{
Mpd.Connect();
auto s = Mpd.GetCurrentSong();
if (!s.empty())
{
auto format = Format::parse(vm["current-song"].as<std::string>(), Format::Flags::Tag);
std::cout << Format::stringify<char>(format, &s);
}
return false;
}
// custom startup screen
if (vm.count("screen"))
{
auto screen = vm["screen"].as<std::string>();
Config.startup_screen_type = stringtoStartupScreenType(screen);
if (Config.startup_screen_type == ScreenType::Unknown)
{
std::cerr << "Unknown screen: " << screen << "\n";
exit(1);
}
}
// custom startup slave screen
if (vm.count("slave-screen"))
{
auto screen = vm["slave-screen"].as<std::string>();
Config.startup_slave_screen_type = stringtoStartupScreenType(screen);
if (Config.startup_slave_screen_type == ScreenType::Unknown)
{
std::cerr << "Unknown slave screen: " << screen << "\n";
exit(1);
}
}
}
catch (std::exception &e)
{
cerr << "Error while processing configuration: " << e.what() << "\n";
exit(1);
}
return true;
}