1 /***************************************************************************
2 * Copyright (C) 2008-2014 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 ***************************************************************************/
26 #include "media_library.h"
29 #include "playlist_editor.h"
30 #include "search_engine.h"
31 #include "sel_items_adder.h"
34 #include "statusbar.h"
35 #include "tag_editor.h"
36 #include "visualizer.h"
38 #include "utility/string.h"
40 using Global::myScreen
;
42 using Global::wFooter
;
43 using Global::wHeader
;
46 using Global::VolumeState
;
50 boost::posix_time::ptime past
= boost::posix_time::from_time_t(0);
52 size_t playing_song_scroll_begin
= 0;
53 size_t first_line_scroll_begin
= 0;
54 size_t second_line_scroll_begin
= 0;
61 char m_db_updating
= 0;
64 MPD::PlayerState m_player_state
= MPD::psUnknown
;
66 unsigned m_elapsed_time
= 0;
68 void drawTitle(const MPD::Song
&np
)
71 windowTitle(np
.toString(Config
.song_window_title_format
, Config
.tags_separator
));
74 std::string
playerStateToString(MPD::PlayerState ps
)
83 if (Config
.design
== Design::Alternative
)
89 if (Config
.design
== Design::Alternative
)
95 if (Config
.design
== Design::Alternative
)
106 void Status::handleClientError(MPD::ClientError
&e
)
110 Statusbar::printf("ncmpcpp: %1%", e
.what());
113 void Status::handleServerError(MPD::ServerError
&e
)
115 if (e
.code() == MPD_SERVER_ERROR_PERMISSION
)
117 wFooter
->setGetStringHelper(nullptr);
118 Statusbar::put() << "Password: ";
119 Mpd
.SetPassword(wFooter
->getString(0, true));
121 Statusbar::print("Password accepted");
122 wFooter
->setGetStringHelper(Statusbar::Helpers::getString
);
124 else if (e
.code() == MPD_SERVER_ERROR_NO_EXIST
&& myScreen
== myBrowser
)
126 myBrowser
->GetDirectory(getParentDirectory(myBrowser
->CurrentDir()));
127 myBrowser
->refresh();
129 Statusbar::printf("MPD: %1%", e
.what());
132 /*************************************************************************/
136 Timer
= boost::posix_time::microsec_clock::local_time();
139 if (State::player() == MPD::psPlay
140 && Global::Timer
- past
> boost::posix_time::seconds(1))
142 // update elapsed time/bitrate of the current song
143 Status::Changes::elapsedTime(true);
148 applyToVisibleWindows(&BaseScreen::update
);
149 Statusbar::tryRedraw();
155 void Status::update(int event
)
157 MPD::Status old_status
= m_status
;
158 m_status
= Mpd
.getStatus();
159 m_player_state
= m_status
.playerState();
160 m_volume
= m_status
.volume();
161 myPlaylist
->setStatus(m_status
);
163 if (event
& MPD_IDLE_DATABASE
)
165 if (event
& MPD_IDLE_STORED_PLAYLIST
)
166 Changes::storedPlaylists();
167 if (event
& MPD_IDLE_PLAYLIST
)
169 if (event
& MPD_IDLE_PLAYER
)
171 Changes::playerState();
172 if (old_status
.empty() || old_status
.currentSongID() != m_status
.currentSongID())
175 if (event
& MPD_IDLE_MIXER
)
177 if (event
& MPD_IDLE_OUTPUT
)
179 if (event
& (MPD_IDLE_UPDATE
| MPD_IDLE_OPTIONS
))
181 if (event
& MPD_IDLE_UPDATE
)
182 Changes::dbUpdateState(!old_status
.empty());
183 if (event
& MPD_IDLE_OPTIONS
)
185 if (old_status
.empty() || old_status
.repeat() != m_status
.repeat())
186 Changes::repeat(!old_status
.empty());
187 if (old_status
.empty() || old_status
.random() != m_status
.random())
188 Changes::random(!old_status
.empty());
189 if (old_status
.empty() || old_status
.single() != m_status
.single())
190 Changes::single(!old_status
.empty());
191 if (old_status
.empty() || old_status
.consume() != m_status
.consume())
192 Changes::consume(!old_status
.empty());
193 if (old_status
.empty() || old_status
.crossfade() != m_status
.crossfade())
194 Changes::crossfade(!old_status
.empty());
199 if (event
& MPD_IDLE_PLAYER
)
202 if (event
& (MPD_IDLE_PLAYLIST
| MPD_IDLE_DATABASE
| MPD_IDLE_PLAYER
))
203 applyToVisibleWindows(&BaseScreen::refreshWindow
);
206 /*************************************************************************/
208 bool Status::State::repeat()
213 bool Status::State::random()
218 bool Status::State::single()
223 bool Status::State::consume()
228 bool Status::State::crossfade()
233 MPD::PlayerState
Status::State::player()
235 return m_player_state
;
238 int Status::State::volume()
243 unsigned int Status::State::elapsedTime()
245 return m_elapsed_time
;
248 /*************************************************************************/
250 void Status::Changes::playlist()
252 myPlaylist
->main().clearSearchResults();
253 withUnfilteredMenuReapplyFilter(myPlaylist
->main(), []() {
254 size_t playlist_length
= m_status
.playlistLength();
255 if (playlist_length
< myPlaylist
->main().size())
257 auto it
= myPlaylist
->main().begin()+playlist_length
;
258 auto end
= myPlaylist
->main().end();
259 for (; it
!= end
; ++it
)
260 myPlaylist
->unregisterSong(it
->value());
261 myPlaylist
->main().resizeList(playlist_length
);
264 Mpd
.GetPlaylistChanges(myPlaylist
->oldVersion(), [](MPD::Song s
) {
265 size_t pos
= s
.getPosition();
266 if (pos
< myPlaylist
->main().size())
268 // if song's already in playlist, replace it with a new one
269 MPD::Song
&old_s
= myPlaylist
->main()[pos
].value();
270 myPlaylist
->unregisterSong(old_s
);
273 else // otherwise just add it to playlist
274 myPlaylist
->main().addItem(s
);
275 myPlaylist
->registerSong(s
);
279 if (State::player() != MPD::psStop
)
280 drawTitle(myPlaylist
->nowPlayingSong());
282 myPlaylist
->reloadTotalLength();
283 myPlaylist
->reloadRemaining();
285 if (isVisible(myBrowser
))
286 markSongsInPlaylist(myBrowser
->proxySongList());
287 if (isVisible(mySearcher
))
288 markSongsInPlaylist(mySearcher
->proxySongList());
289 if (isVisible(myLibrary
))
291 markSongsInPlaylist(myLibrary
->songsProxyList());
292 myLibrary
->Songs
.refresh();
294 if (isVisible(myPlaylistEditor
))
296 markSongsInPlaylist(myPlaylistEditor
->contentProxyList());
297 myPlaylistEditor
->Content
.refresh();
301 void Status::Changes::storedPlaylists()
303 myPlaylistEditor
->requestPlaylistsUpdate();
304 myPlaylistEditor
->requestContentsUpdate();
305 if (myBrowser
->CurrentDir() == "/")
307 myBrowser
->GetDirectory("/");
308 if (isVisible(myBrowser
))
309 myBrowser
->refresh();
313 void Status::Changes::database()
315 if (isVisible(myBrowser
))
316 myBrowser
->GetDirectory(myBrowser
->CurrentDir());
318 myBrowser
->main().clear();
319 # ifdef HAVE_TAGLIB_H
320 myTagEditor
->Dirs
->clear();
321 # endif // HAVE_TAGLIB_H
322 myLibrary
->requestTagsUpdate();
323 myLibrary
->requestAlbumsUpdate();
324 myLibrary
->requestSongsUpdate();
327 void Status::Changes::playerState()
329 switch (State::player())
332 drawTitle(myPlaylist
->nowPlayingSong());
333 myPlaylist
->reloadRemaining();
336 windowTitle("ncmpcpp " VERSION
);
337 if (Progressbar::isUnlocked())
338 Progressbar::draw(0, 0);
339 myPlaylist
->reloadRemaining();
340 if (Config
.design
== Design::Alternative
)
342 *wHeader
<< NC::XY(0, 0) << wclrtoeol
<< NC::XY(0, 1) << wclrtoeol
;
346 # ifdef ENABLE_VISUALIZER
347 if (isVisible(myVisualizer
))
348 myVisualizer
->main().clear();
349 # endif // ENABLE_VISUALIZER
355 std::string state
= playerStateToString(State::player());
356 if (Config
.design
== Design::Alternative
)
358 *wHeader
<< NC::XY(0, 1) << NC::Format::Bold
<< state
<< NC::Format::NoBold
;
361 else if (Statusbar::isUnlocked() && Config
.statusbar_visibility
)
363 *wFooter
<< NC::XY(0, 1);
365 *wFooter
<< wclrtoeol
;
367 *wFooter
<< NC::Format::Bold
<< state
<< NC::Format::NoBold
;
370 // needed for immediate display after starting
371 // player from stopped state or seeking
375 void Status::Changes::songID()
377 // update information about current song
378 myPlaylist
->setStatus(m_status
);
379 myPlaylist
->reloadRemaining();
380 playing_song_scroll_begin
= 0;
381 first_line_scroll_begin
= 0;
382 second_line_scroll_begin
= 0;
383 if (State::player() != MPD::psStop
)
386 if (!Config
.execute_on_song_change
.empty())
387 res
= system(Config
.execute_on_song_change
.c_str());
389 # ifdef HAVE_CURL_CURL_H
390 if (Config
.fetch_lyrics_in_background
)
391 Lyrics::DownloadInBackground(myPlaylist
->nowPlayingSong());
392 # endif // HAVE_CURL_CURL_H
394 drawTitle(myPlaylist
->nowPlayingSong());
396 if (Config
.autocenter_mode
&& !myPlaylist
->main().isFiltered())
397 myPlaylist
->main().highlight(myPlaylist
->currentSongPosition());
399 if (Config
.now_playing_lyrics
&& isVisible(myLyrics
) && myLyrics
->previousScreen() == myPlaylist
)
400 myLyrics
->ReloadNP
= 1;
405 void Status::Changes::elapsedTime(bool update_elapsed
)
407 MPD::Status st
= m_status
;
410 st
= Mpd
.getStatus();
411 m_elapsed_time
= st
.elapsedTime();
414 if (st
.playerState() == MPD::psStop
)
416 if (Statusbar::isUnlocked() && Config
.statusbar_visibility
)
417 *wFooter
<< NC::XY(0, 1) << wclrtoeol
;
421 std::string ps
= playerStateToString(st
.playerState());
422 MPD::Song np
= myPlaylist
->nowPlayingSong();
425 std::string tracklength
;
426 switch (Config
.design
)
428 case Design::Classic
:
429 if (Statusbar::isUnlocked() && Config
.statusbar_visibility
)
431 if (Config
.display_bitrate
&& st
.kbps())
434 tracklength
+= boost::lexical_cast
<std::string
>(st
.kbps());
435 tracklength
+= " kbps]";
440 if (Config
.display_remaining_time
)
443 tracklength
+= MPD::Song::ShowTime(st
.totalTime()-st
.elapsedTime());
446 tracklength
+= MPD::Song::ShowTime(st
.elapsedTime());
448 tracklength
+= MPD::Song::ShowTime(st
.totalTime());
453 tracklength
+= MPD::Song::ShowTime(st
.elapsedTime());
457 stringToBuffer(ToWString(Charset::utf8ToLocale(np
.toString(Config
.song_status_format
, Config
.tags_separator
, "$"))), np_song
);
458 *wFooter
<< NC::XY(0, 1) << wclrtoeol
<< NC::Format::Bold
<< ps
<< NC::Format::NoBold
;
459 writeCyclicBuffer(np_song
, *wFooter
, playing_song_scroll_begin
, wFooter
->getWidth()-ps
.length()-tracklength
.length(), L
" ** ");
460 *wFooter
<< NC::Format::Bold
<< NC::XY(wFooter
->getWidth()-tracklength
.length(), 1) << tracklength
<< NC::Format::NoBold
;
463 case Design::Alternative
:
464 if (Config
.display_remaining_time
)
467 tracklength
+= MPD::Song::ShowTime(st
.totalTime()-st
.elapsedTime());
470 tracklength
= MPD::Song::ShowTime(st
.elapsedTime());
474 tracklength
+= MPD::Song::ShowTime(st
.totalTime());
476 // bitrate here doesn't look good, but it can be moved somewhere else later
477 if (Config
.display_bitrate
&& st
.kbps())
480 tracklength
+= boost::lexical_cast
<std::string
>(st
.kbps());
481 tracklength
+= " kbps";
484 NC::WBuffer first
, second
;
485 stringToBuffer(ToWString(Charset::utf8ToLocale(np
.toString(Config
.new_header_first_line
, Config
.tags_separator
, "$"))), first
);
486 stringToBuffer(ToWString(Charset::utf8ToLocale(np
.toString(Config
.new_header_second_line
, Config
.tags_separator
, "$"))), second
);
488 size_t first_len
= wideLength(first
.str());
489 size_t first_margin
= (std::max(tracklength
.length()+1, VolumeState
.length()))*2;
490 size_t first_start
= first_len
< COLS
-first_margin
? (COLS
-first_len
)/2 : tracklength
.length()+1;
492 size_t second_len
= wideLength(second
.str());
493 size_t second_margin
= (std::max(ps
.length(), size_t(8))+1)*2;
494 size_t second_start
= second_len
< COLS
-second_margin
? (COLS
-second_len
)/2 : ps
.length()+1;
496 if (!Global::SeekingInProgress
)
497 *wHeader
<< NC::XY(0, 0) << wclrtoeol
<< tracklength
;
498 *wHeader
<< NC::XY(first_start
, 0);
499 writeCyclicBuffer(first
, *wHeader
, first_line_scroll_begin
, COLS
-tracklength
.length()-VolumeState
.length()-1, L
" ** ");
501 *wHeader
<< NC::XY(0, 1) << wclrtoeol
<< NC::Format::Bold
<< ps
<< NC::Format::NoBold
;
502 *wHeader
<< NC::XY(second_start
, 1);
503 writeCyclicBuffer(second
, *wHeader
, second_line_scroll_begin
, COLS
-ps
.length()-8-2, L
" ** ");
505 *wHeader
<< NC::XY(wHeader
->getWidth()-VolumeState
.length(), 0) << Config
.volume_color
<< VolumeState
<< NC::Color::End
;
509 if (Progressbar::isUnlocked())
510 Progressbar::draw(st
.elapsedTime(), st
.totalTime());
513 void Status::Changes::repeat(bool show_msg
)
515 m_repeat
= m_status
.repeat() ? 'r' : 0;
517 Statusbar::printf("Repeat mode is %1%", !m_repeat
? "off" : "on");
520 void Status::Changes::random(bool show_msg
)
522 m_random
= m_status
.random() ? 'z' : 0;
524 Statusbar::printf("Random mode is %1%", !m_random
? "off" : "on");
527 void Status::Changes::single(bool show_msg
)
529 m_single
= m_status
.single() ? 's' : 0;
531 Statusbar::printf("Single mode is %1%", !m_single
? "off" : "on");
534 void Status::Changes::consume(bool show_msg
)
536 m_consume
= m_status
.consume() ? 'c' : 0;
538 Statusbar::printf("Consume mode is %1%", !m_consume
? "off" : "on");
541 void Status::Changes::crossfade(bool show_msg
)
543 int crossfade
= m_status
.crossfade();
544 m_crossfade
= crossfade
? 'x' : 0;
546 Statusbar::printf("Crossfade set to %1% seconds", crossfade
);
549 void Status::Changes::dbUpdateState(bool show_msg
)
551 m_db_updating
= m_status
.updateID() ? 'U' : 0;
553 Statusbar::printf("Database update %1%", m_status
.updateID() ? "started" : "finished");
556 void Status::Changes::flags()
558 if (!Config
.header_visibility
&& Config
.design
== Design::Classic
)
561 std::string switch_state
;
562 switch (Config
.design
)
564 case Design::Classic
:
566 switch_state
+= m_repeat
;
568 switch_state
+= m_random
;
570 switch_state
+= m_single
;
572 switch_state
+= m_consume
;
574 switch_state
+= m_crossfade
;
576 switch_state
+= m_db_updating
;
578 // this is done by raw ncurses because creating another
579 // window only for handling this is quite silly
580 attrset(A_BOLD
|COLOR_PAIR(int(Config
.state_line_color
)));
581 mvhline(1, 0, 0, COLS
);
582 if (!switch_state
.empty())
584 mvprintw(1, COLS
-switch_state
.length()-3, "[");
585 attroff(COLOR_PAIR(int(Config
.state_line_color
)));
586 attron(COLOR_PAIR(int(Config
.state_flags_color
)));
587 mvprintw(1, COLS
-switch_state
.length()-2, "%s", switch_state
.c_str());
588 attroff(COLOR_PAIR(int(Config
.state_flags_color
)));
589 attron(COLOR_PAIR(int(Config
.state_line_color
)));
590 mvprintw(1, COLS
-2, "]");
592 attrset(COLOR_PAIR(int(Config
.main_color
)));
595 case Design::Alternative
:
597 switch_state
+= m_repeat
? m_repeat
: '-';
598 switch_state
+= m_random
? m_random
: '-';
599 switch_state
+= m_single
? m_single
: '-';
600 switch_state
+= m_consume
? m_consume
: '-';
601 switch_state
+= m_crossfade
? m_crossfade
: '-';
602 switch_state
+= m_db_updating
? m_db_updating
: '-';
604 *wHeader
<< NC::XY(COLS
-switch_state
.length(), 1) << NC::Format::Bold
<< Config
.state_flags_color
<< switch_state
<< NC::Color::End
<< NC::Format::NoBold
;
605 if (!Config
.header_visibility
) // in this case also draw separator
607 *wHeader
<< NC::Format::Bold
<< NC::Color::Black
;
608 mvwhline(wHeader
->raw(), 2, 0, 0, COLS
);
609 *wHeader
<< NC::Color::End
<< NC::Format::NoBold
;
616 void Status::Changes::mixer()
618 if (!Config
.display_volume_level
|| (!Config
.header_visibility
&& Config
.design
== Design::Classic
))
621 switch (Config
.design
)
623 case Design::Classic
:
624 VolumeState
= " " "Volume" ": ";
626 case Design::Alternative
:
627 VolumeState
= " " "Vol" ": ";
630 if (State::volume() < 0)
631 VolumeState
+= "n/a";
634 VolumeState
+= boost::lexical_cast
<std::string
>(State::volume());
637 *wHeader
<< Config
.volume_color
;
638 *wHeader
<< NC::XY(wHeader
->getWidth()-VolumeState
.length(), 0) << VolumeState
;
639 *wHeader
<< NC::Color::End
;
643 void Status::Changes::outputs()
645 # ifdef ENABLE_OUTPUTS
646 myOutputs
->FetchList();
647 # endif // ENABLE_OUTPUTS