1 /***************************************************************************
2 * Copyright (C) 2008-2016 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 #include <boost/tuple/tuple.hpp>
22 #include <boost/tokenizer.hpp>
26 #include "configuration.h"
27 #include "format_impl.h"
30 #include "utility/conversion.h"
31 #include "utility/option_parser.h"
32 #include "utility/type_conversions.h"
34 #ifdef HAVE_LANGINFO_H
35 # include <langinfo.h>
42 std::vector
<Column
> generate_columns(const std::string
&format
)
44 std::vector
<Column
> result
;
47 while (!(width
= getEnclosedString(format
, '(', ')', &pos
)).empty())
50 auto scolor
= getEnclosedString(format
, '[', ']', &pos
);
52 col
.color
= NC::Color::Default
;
54 col
.color
= boost::lexical_cast
<NC::Color
>(scolor
);
56 if (*width
.rbegin() == 'f')
59 width
.resize(width
.size()-1);
64 auto tag_type
= getEnclosedString(format
, '{', '}', &pos
);
66 size_t tag_type_colon_pos
= tag_type
.find(':');
67 if (tag_type_colon_pos
!= std::string::npos
)
69 col
.name
= ToWString(tag_type
.substr(tag_type_colon_pos
+1));
70 tag_type
.resize(tag_type_colon_pos
);
73 if (!tag_type
.empty())
77 // extract tag types in format a|b|c etc.
79 col
.type
+= tag_type
[(++i
)++]; // nice one.
80 while (tag_type
[i
] == '|');
83 for (; i
< tag_type
.length(); ++i
)
88 col
.right_alignment
= 1;
91 col
.display_empty_tag
= 0;
97 col
.display_empty_tag
= 0;
99 col
.width
= boost::lexical_cast
<int>(width
);
100 result
.push_back(col
);
103 // calculate which column is the last one to have relative width and stretch it accordingly
106 int stretch_limit
= 0;
107 auto it
= result
.rbegin();
108 for (; it
!= result
.rend(); ++it
)
111 stretch_limit
+= it
->width
;
115 // if it's false, all columns are fixed
116 if (it
!= result
.rend())
117 it
->stretch_limit
= stretch_limit
;
123 Format::AST
<char> columns_to_format(const std::vector
<Column
> &columns
)
125 std::vector
<Format::Expression
<char>> result
;
127 auto column
= columns
.begin();
130 Format::FirstOf
<char> first_of
;
131 for (const auto &type
: column
->type
)
133 auto f
= charToGetFunction(type
);
134 assert(f
!= nullptr);
135 first_of
.base().push_back(f
);
137 result
.push_back(std::move(first_of
));
139 if (++column
!= columns
.end())
140 result
.push_back(" ");
145 return Format::AST
<char>(std::move(result
));
148 void add_slash_at_the_end(std::string
&s
)
150 if (s
.empty() || *s
.rbegin() != '/')
152 s
.resize(s
.size()+1);
157 std::string
adjust_directory(std::string s
)
159 add_slash_at_the_end(s
);
164 std::string
adjust_path(std::string s
)
170 NC::Buffer
buffer(const std::string
&v
)
174 Format::parse(v
, Format::Flags::Color
| Format::Flags::Format
),
180 void deprecated(const char *option
, double version_removal
, const char *advice
)
182 std::cerr
<< "WARNING: Variable '" << option
183 << "' is deprecated and will be removed in "
186 std::cerr
<< " (" << advice
<< ")";
192 bool Configuration::read(const std::vector
<std::string
> &config_paths
, bool ignore_errors
)
196 p
.add
<void>("visualizer_sample_multiplier", nullptr, "", [](std::string v
) {
199 "visualizer_sample_multiplier",
201 "visualizer scales automatically");
203 p
.add
<void>("progressbar_boldness", nullptr, "", [](std::string v
) {
206 "progressbar_boldness",
208 "use extended progressbar_color and progressbar_elapsed_color instead");
211 // keep the same order of variables as in configuration file
212 p
.add("ncmpcpp_directory", &ncmpcpp_directory
, "~/.ncmpcpp/", adjust_directory
);
213 p
.add("lyrics_directory", &lyrics_directory
, "~/.lyrics/", adjust_directory
);
214 p
.add
<void>("mpd_host", nullptr, "localhost", [](std::string host
) {
216 Mpd
.SetHostname(host
);
218 p
.add
<void>("mpd_port", nullptr, "6600", [](std::string port
) {
219 Mpd
.SetPort(verbose_lexical_cast
<unsigned>(port
));
221 p
.add("mpd_music_dir", &mpd_music_dir
, "~/music", adjust_directory
);
222 p
.add("mpd_connection_timeout", &mpd_connection_timeout
, "5");
223 p
.add("mpd_crossfade_time", &crossfade_time
, "5");
224 p
.add("visualizer_fifo_path", &visualizer_fifo_path
, "/tmp/mpd.fifo", adjust_path
);
225 p
.add("visualizer_output_name", &visualizer_output_name
, "Visualizer feed");
226 p
.add("visualizer_in_stereo", &visualizer_in_stereo
, "yes", yes_no
);
227 p
.add("visualizer_sync_interval", &visualizer_sync_interval
, "30",
229 unsigned sync_interval
= verbose_lexical_cast
<unsigned>(v
);
230 lowerBoundCheck
<unsigned>(sync_interval
, 10);
231 return boost::posix_time::seconds(sync_interval
);
233 p
.add("visualizer_type", &visualizer_type
, "wave");
234 p
.add("visualizer_look", &visualizer_chars
, "●▮", [](std::string s
) {
235 auto result
= ToWString(std::move(s
));
236 boundsCheck
<std::wstring::size_type
>(result
.size(), 2, 2);
239 p
.add("visualizer_color", &visualizer_colors
,
240 "blue, cyan, green, yellow, magenta, red", list_of
<NC::FormattedColor
>);
241 p
.add("system_encoding", &system_encoding
, "", [](std::string encoding
) {
242 #ifdef HAVE_LANGINFO_H
243 // try to autodetect system encoding
244 if (encoding
.empty())
246 encoding
= nl_langinfo(CODESET
);
247 if (encoding
== "UTF-8") // mpd uses utf-8 by default so no need to convert
250 #endif // HAVE_LANGINFO_H
253 p
.add("playlist_disable_highlight_delay", &playlist_disable_highlight_delay
,
254 "5", [](std::string v
) {
255 return boost::posix_time::seconds(verbose_lexical_cast
<unsigned>(v
));
257 p
.add("message_delay_time", &message_delay_time
, "5");
258 p
.add("song_list_format", &song_list_format
,
259 "{%a - }{%t}|{$8%f$9}$R{$3(%l)$9}", [](std::string v
) {
260 return Format::parse(v
);
262 p
.add("song_status_format", &song_status_format
,
263 "{{%a{ \"%b\"{ (%y)}} - }{%t}}|{%f}", [this](std::string v
) {
264 auto flags
= Format::Flags::All
^ Format::Flags::OutputSwitch
;
265 // precompute wide format for status display
266 song_status_wformat
= Format::parse(ToWString(v
), flags
);
267 return Format::parse(v
, flags
);
269 p
.add("song_library_format", &song_library_format
,
270 "{%n - }{%t}|{%f}", [](std::string v
) {
271 return Format::parse(v
);
273 p
.add("alternative_header_first_line_format", &new_header_first_line
,
274 "$b$1$aqqu$/a$9 {%t}|{%f} $1$atqq$/a$9$/b", [](std::string v
) {
275 return Format::parse(ToWString(std::move(v
)),
276 Format::Flags::All
^ Format::Flags::OutputSwitch
);
278 p
.add("alternative_header_second_line_format", &new_header_second_line
,
279 "{{$4$b%a$/b$9}{ - $7%b$9}{ ($4%y$9)}}|{%D}", [](std::string v
) {
280 return Format::parse(ToWString(std::move(v
)),
281 Format::Flags::All
^ Format::Flags::OutputSwitch
);
283 p
.add("now_playing_prefix", &now_playing_prefix
,
284 "$b", [this](std::string v
) {
285 NC::Buffer result
= buffer(v
);
286 now_playing_prefix_length
= wideLength(ToWString(result
.str()));
289 p
.add("now_playing_suffix", &now_playing_suffix
,
290 "$/b", [this](std::string v
) {
291 NC::Buffer result
= buffer(v
);
292 now_playing_suffix_length
= wideLength(ToWString(result
.str()));
295 p
.add("browser_playlist_prefix", &browser_playlist_prefix
, "$2playlist$9 ", buffer
);
296 p
.add("selected_item_prefix", &selected_item_prefix
,
297 "$6", [this](std::string v
) {
298 NC::Buffer result
= buffer(v
);
299 selected_item_prefix_length
= wideLength(ToWString(result
.str()));
302 p
.add("selected_item_suffix", &selected_item_suffix
,
303 "$9", [this](std::string v
) {
304 NC::Buffer result
= buffer(v
);
305 selected_item_suffix_length
= wideLength(ToWString(result
.str()));
308 p
.add("modified_item_prefix", &modified_item_prefix
, "$3>$9 ", buffer
);
309 p
.add("song_window_title_format", &song_window_title_format
,
310 "{%a - }{%t}|{%f}", [](std::string v
) {
311 return Format::parse(v
, Format::Flags::Tag
);
313 p
.add("browser_sort_mode", &browser_sort_mode
, "name");
314 p
.add("browser_sort_format", &browser_sort_format
,
315 "{%a - }{%t}|{%f} {(%l)}", [](std::string v
) {
316 return Format::parse(v
, Format::Flags::Tag
);
318 p
.add("song_columns_list_format", &song_columns_mode_format
,
319 "(20)[]{a} (6f)[green]{NE} (50)[white]{t|f:Title} (20)[cyan]{b} (7f)[magenta]{l}",
320 [this](std::string v
) {
321 columns
= generate_columns(v
);
322 return columns_to_format(columns
);
324 p
.add("execute_on_song_change", &execute_on_song_change
, "", adjust_path
);
325 p
.add("execute_on_player_state_change", &execute_on_player_state_change
,
327 p
.add("playlist_show_mpd_host", &playlist_show_mpd_host
, "no", yes_no
);
328 p
.add("playlist_show_remaining_time", &playlist_show_remaining_time
, "no", yes_no
);
329 p
.add("playlist_shorten_total_times", &playlist_shorten_total_times
, "no", yes_no
);
330 p
.add("playlist_separate_albums", &playlist_separate_albums
, "no", yes_no
);
331 p
.add("playlist_display_mode", &playlist_display_mode
, "columns");
332 p
.add("browser_display_mode", &browser_display_mode
, "classic");
333 p
.add("search_engine_display_mode", &search_engine_display_mode
, "classic");
334 p
.add("playlist_editor_display_mode", &playlist_editor_display_mode
, "classic");
335 p
.add("discard_colors_if_item_is_selected", &discard_colors_if_item_is_selected
,
337 p
.add("show_duplicate_tags", &MPD::Song::ShowDuplicateTags
, "yes", yes_no
);
338 p
.add("incremental_seeking", &incremental_seeking
, "yes", yes_no
);
339 p
.add("seek_time", &seek_time
, "1");
340 p
.add("volume_change_step", &volume_change_step
, "2");
341 p
.add("autocenter_mode", &autocenter_mode
, "no", yes_no
);
342 p
.add("centered_cursor", ¢ered_cursor
, "no", yes_no
);
343 p
.add("progressbar_look", &progressbar
, "=>", [](std::string v
) {
344 auto result
= ToWString(std::move(v
));
345 boundsCheck
<std::wstring::size_type
>(result
.size(), 2, 3);
346 // If two characters were specified, fill \0 as the third one.
350 p
.add("default_place_to_search_in", &search_in_db
, "database", [](std::string v
) {
353 else if (v
== "playlist")
358 p
.add("user_interface", &design
, "classic");
359 p
.add("data_fetching_delay", &data_fetching_delay
, "yes", yes_no
);
360 p
.add("media_library_primary_tag", &media_lib_primary_tag
, "artist", [](std::string v
) {
362 return MPD_TAG_ARTIST
;
363 else if (v
== "album_artist")
364 return MPD_TAG_ALBUM_ARTIST
;
365 else if (v
== "date")
367 else if (v
== "genre")
368 return MPD_TAG_GENRE
;
369 else if (v
== "composer")
370 return MPD_TAG_COMPOSER
;
371 else if (v
== "performer")
372 return MPD_TAG_PERFORMER
;
376 p
.add("default_find_mode", &wrapped_search
, "wrapped", [](std::string v
) {
379 else if (v
== "normal")
384 p
.add("default_tag_editor_pattern", &pattern
, "%n - %t");
385 p
.add("header_visibility", &header_visibility
, "yes", yes_no
);
386 p
.add("statusbar_visibility", &statusbar_visibility
, "yes", yes_no
);
387 p
.add("titles_visibility", &titles_visibility
, "yes", yes_no
);
388 p
.add("header_text_scrolling", &header_text_scrolling
, "yes", yes_no
);
389 p
.add("cyclic_scrolling", &use_cyclic_scrolling
, "no", yes_no
);
390 p
.add("lines_scrolled", &lines_scrolled
, "2");
391 p
.add("lyrics_fetchers", &lyrics_fetchers
,
392 "lyricwiki, azlyrics, genius, sing365, lyricsmania, metrolyrics, justsomelyrics, tekstowo, internet",
393 list_of
<LyricsFetcher_
>);
394 p
.add("follow_now_playing_lyrics", &now_playing_lyrics
, "no", yes_no
);
395 p
.add("fetch_lyrics_for_current_song_in_background", &fetch_lyrics_in_background
,
397 p
.add("store_lyrics_in_song_dir", &store_lyrics_in_song_dir
, "no", yes_no
);
398 p
.add("generate_win32_compatible_filenames", &generate_win32_compatible_filenames
,
400 p
.add("allow_for_physical_item_deletion", &allow_for_physical_item_deletion
,
402 p
.add("lastfm_preferred_language", &lastfm_preferred_language
, "en");
403 p
.add("space_add_mode", &space_add_mode
, "add_remove");
404 p
.add("show_hidden_files_in_local_browser", &local_browser_show_hidden_files
,
407 "screen_switcher_mode", nullptr, "playlist, browser",
408 [this](std::string v
) {
410 screen_switcher_previous
= true;
413 screen_switcher_previous
= false;
414 screen_sequence
= list_of
<ScreenType
>(v
, [](std::string s
) {
415 auto screen
= stringtoStartupScreenType(s
);
416 if (screen
== ScreenType::Unknown
)
422 p
.add("startup_screen", &startup_screen_type
, "playlist", [](std::string v
) {
423 auto screen
= stringtoStartupScreenType(v
);
424 if (screen
== ScreenType::Unknown
)
428 p
.add("startup_slave_screen", &startup_slave_screen_type
, "", [](std::string v
) {
429 boost::optional
<ScreenType
> screen
;
432 screen
= stringtoStartupScreenType(v
);
433 if (screen
== ScreenType::Unknown
)
438 p
.add("startup_slave_screen_focus", &startup_slave_screen_focus
, "no", yes_no
);
439 p
.add("locked_screen_width_part", &locked_screen_width_part
,
440 "50", [](std::string v
) {
441 return verbose_lexical_cast
<double>(v
) / 100;
443 p
.add("ask_for_locked_screen_width_part", &ask_for_locked_screen_width_part
,
445 p
.add("jump_to_now_playing_song_at_start", &jump_to_now_playing_song_at_start
,
447 p
.add("ask_before_clearing_playlists", &ask_before_clearing_playlists
,
449 p
.add("ask_before_shuffling_playlists", &ask_before_shuffling_playlists
,
451 p
.add("clock_display_seconds", &clock_display_seconds
, "no", yes_no
);
452 p
.add("display_volume_level", &display_volume_level
, "yes", yes_no
);
453 p
.add("display_bitrate", &display_bitrate
, "no", yes_no
);
454 p
.add("display_remaining_time", &display_remaining_time
, "no", yes_no
);
455 p
.add("regular_expressions", ®ex_type
, "perl", [](std::string v
) {
457 return boost::regex::icase
| boost::regex::literal
;
458 else if (v
== "basic")
459 return boost::regex::icase
| boost::regex::basic
;
460 else if (v
== "extended")
461 return boost::regex::icase
| boost::regex::extended
;
462 else if (v
== "perl")
463 return boost::regex::icase
| boost::regex::perl
;
467 p
.add("ignore_leading_the", &ignore_leading_the
, "no", yes_no
);
468 p
.add("block_search_constraints_change_if_items_found",
469 &block_search_constraints_change
, "yes", yes_no
);
470 p
.add("mouse_support", &mouse_support
, "yes", yes_no
);
471 p
.add("mouse_list_scroll_whole_page", &mouse_list_scroll_whole_page
, "yes", yes_no
);
472 p
.add("empty_tag_marker", &empty_tag
, "<empty>");
473 p
.add("tags_separator", &MPD::Song::TagsSeparator
, " | ");
474 p
.add("tag_editor_extended_numeration", &tag_editor_extended_numeration
, "no", yes_no
);
475 p
.add("media_library_sort_by_mtime", &media_library_sort_by_mtime
, "no", yes_no
);
476 p
.add("enable_window_title", &set_window_title
, "yes", [](std::string v
) {
477 // Consider this variable only if TERM variable is available and we're not
478 // in emacs terminal nor tty (through any wrapper like screen).
479 auto term
= getenv("TERM");
481 && strstr(term
, "linux") == nullptr
482 && strncmp(term
, "eterm", const_strlen("eterm")))
486 std::clog
<< "Terminal doesn't support window title, skipping 'enable_window_title'.\n";
490 p
.add("search_engine_default_search_mode", &search_engine_default_search_mode
,
491 "1", [](std::string v
) {
492 auto mode
= verbose_lexical_cast
<unsigned>(v
);
493 boundsCheck
<unsigned>(mode
, 1, 3);
496 p
.add("external_editor", &external_editor
, "nano", adjust_path
);
497 p
.add("use_console_editor", &use_console_editor
, "yes", yes_no
);
498 p
.add("colors_enabled", &colors_enabled
, "yes", yes_no
);
499 p
.add("empty_tag_color", &empty_tags_color
, "cyan");
500 p
.add("header_window_color", &header_color
, "default");
501 p
.add("volume_color", &volume_color
, "default");
502 p
.add("state_line_color", &state_line_color
, "default");
503 p
.add("state_flags_color", &state_flags_color
, "default:b");
504 p
.add("main_window_color", &main_color
, "yellow");
505 p
.add("color1", &color1
, "white");
506 p
.add("color2", &color2
, "green");
507 p
.add("main_window_highlight_color", &main_highlight_color
, "yellow");
508 p
.add("progressbar_color", &progressbar_color
, "black:b");
509 p
.add("progressbar_elapsed_color", &progressbar_elapsed_color
, "green:b");
510 p
.add("statusbar_color", &statusbar_color
, "default");
511 p
.add("statusbar_time_color", &statusbar_time_color
, "default:b");
512 p
.add("player_state_color", &player_state_color
, "default:b");
513 p
.add("alternative_ui_separator_color", &alternative_ui_separator_color
, "black:b");
514 p
.add("active_column_color", &active_column_color
, "red");
515 p
.add("window_border_color", &window_border
, "green", verbose_lexical_cast
<NC::Color
>);
516 p
.add("active_window_border", &active_window_border
, "red",
517 verbose_lexical_cast
<NC::Color
>);
520 config_paths
.begin(),
522 [&](const std::string
&config_path
) {
523 std::ifstream
f(config_path
);
525 std::clog
<< "Reading configuration from " << config_path
<< "...\n";
526 return p
.run(f
, ignore_errors
);
528 ) && p
.initialize_undefined(ignore_errors
);