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
|
/***************************************************************************
* Copyright (C) 2008-2014 by Andrzej Rybczak *
* electricityispower@gmail.com *
* *
* 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/filesystem/operations.hpp>
#include <boost/program_options.hpp>
#include <iostream>
#include "bindings.h"
#include "configuration.h"
#include "config.h"
#include "mpdpp.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() && path[0] == '~')
path.replace(0, 1, env_home);
}
bool configure(int argc, char **argv)
{
const std::vector<std::string> default_config_paths = {
"~/.ncmpcpp/config",
xdg_config_home() + "ncmpcpp/config"
};
std::string bindings_path;
std::vector<std::string> config_paths;
po::options_description options("Options");
options.add_options()
("host,h", po::value<std::string>()->default_value("localhost"), "connect to server at host")
("port,p", po::value<int>()->default_value(6600), "connect to server at port")
("config,c", po::value<std::vector<std::string>>(&config_paths)->default_value(default_config_paths, join<std::string>(default_config_paths, " AND ")), "specify configuration file(s)")
("ignore-config-errors", po::value<bool>()->default_value(false), "ignore unknown and invalid options in configuration files")
("bindings,b", po::value<std::string>(&bindings_path)->default_value("~/.ncmpcpp/bindings"), "specify bindings file")
("screen,s", po::value<std::string>(), "specify initial screen")
("slave-screen,S", po::value<std::string>(), "specify initial slave screen")
("help,?", "show help message")
("version,v", "display version information")
;
po::variables_map vm;
try
{
po::store(po::parse_command_line(argc, argv, options), vm);
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 HAVE_CURL_CURL_H
<< " - artist info\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_CURL_CURL_H
<< " curl"
# endif
# ifdef HAVE_FFTW3_H
<< " fftw"
# endif
<< " ncurses"
# ifdef HAVE_TAGLIB_H
<< " taglib"
# endif
# ifdef NCMPCPP_UNICODE
<< " unicode"
# endif
<< "\n";
return false;
}
po::notify(vm);
// 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["ignore-config-errors"].as<bool>()) == false)
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
if (Bindings.read(bindings_path) == false)
exit(1);
Bindings.generateDefaults();
// create directories
boost::filesystem::create_directory(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)
Mpd.SetPort(boost::lexical_cast<int>(env_port));
// 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);
// 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;
}
|