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 ***************************************************************************/
21 #include <boost/date_time/posix_time/posix_time.hpp>
28 #include "media_library.h"
31 #include "playlist_editor.h"
32 #include "search_engine.h"
33 #include "sel_items_adder.h"
36 #include "statusbar.h"
37 #include "tag_editor.h"
38 #include "visualizer.h"
40 #include "utility/string.h"
42 using Global::myScreen
;
44 using Global::wFooter
;
45 using Global::wHeader
;
48 using Global::VolumeState
;
52 boost::posix_time::ptime past
= boost::posix_time::from_time_t(0);
54 size_t playing_song_scroll_begin
= 0;
55 size_t first_line_scroll_begin
= 0;
56 size_t second_line_scroll_begin
= 0;
59 unsigned m_elapsed_time
= 0;
61 // local copies of these are needed to be independent
62 // of the order of idle events incoming from MPD.
68 char m_db_updating
= 0;
69 int m_current_song_id
= 0;
70 unsigned m_playlist_version
= 0;
72 void drawTitle(const MPD::Song
&np
)
75 windowTitle(np
.toString(Config
.song_window_title_format
, Config
.tags_separator
));
78 std::string
playerStateToString(MPD::PlayerState ps
)
87 if (Config
.design
== Design::Alternative
)
93 if (Config
.design
== Design::Alternative
)
99 if (Config
.design
== Design::Alternative
)
100 result
= "[stopped]";
110 void Status::handleClientError(MPD::ClientError
&e
)
114 Statusbar::printf("ncmpcpp: %1%", e
.what());
117 void Status::handleServerError(MPD::ServerError
&e
)
119 if (e
.code() == MPD_SERVER_ERROR_PERMISSION
)
121 wFooter
->setGetStringHelper(nullptr);
122 Statusbar::put() << "Password: ";
123 Mpd
.SetPassword(wFooter
->getString(0, true));
125 Statusbar::print("Password accepted");
126 wFooter
->setGetStringHelper(Statusbar::Helpers::getString
);
128 else if (e
.code() == MPD_SERVER_ERROR_NO_EXIST
&& myScreen
== myBrowser
)
130 myBrowser
->GetDirectory(getParentDirectory(myBrowser
->CurrentDir()));
131 myBrowser
->refresh();
133 Statusbar::printf("MPD: %1%", e
.what());
136 /*************************************************************************/
138 void Status::trace(bool update_timer
, bool update_window_timeout
)
141 Timer
= boost::posix_time::microsec_clock::local_time();
144 if (m_status
.playerState() == MPD::psPlay
145 && Global::Timer
- past
> boost::posix_time::seconds(1))
147 // update elapsed time/bitrate of the current song
148 Status::Changes::elapsedTime(true);
153 applyToVisibleWindows(&BaseScreen::update
);
154 Statusbar::tryRedraw();
156 if (update_window_timeout
)
158 // set appropriate window timeout
159 int nc_wtimeout
= std::numeric_limits
<int>::max();
160 applyToVisibleWindows([&nc_wtimeout
](BaseScreen
*s
) {
161 nc_wtimeout
= std::min(nc_wtimeout
, s
->windowTimeout());
163 wFooter
->setTimeout(nc_wtimeout
);
170 void Status::update(int event
)
172 MPD::Status old_status
= m_status
;
173 m_status
= Mpd
.getStatus();
174 m_elapsed_time
= m_status
.elapsedTime();
176 if (event
& MPD_IDLE_DATABASE
)
178 if (event
& MPD_IDLE_STORED_PLAYLIST
)
179 Changes::storedPlaylists();
180 if (event
& MPD_IDLE_PLAYLIST
)
182 Changes::playlist(m_playlist_version
);
183 m_playlist_version
= m_status
.playlistVersion();
185 if (event
& MPD_IDLE_PLAYER
)
187 Changes::playerState();
188 if (m_current_song_id
!= m_status
.currentSongID())
191 m_current_song_id
= m_status
.currentSongID();
194 if (event
& MPD_IDLE_MIXER
)
196 if (event
& MPD_IDLE_OUTPUT
)
198 if (event
& (MPD_IDLE_UPDATE
| MPD_IDLE_OPTIONS
))
200 bool show_msg
= !old_status
.empty();
201 if (event
& MPD_IDLE_UPDATE
)
202 Changes::dbUpdateState(show_msg
);
203 if (event
& MPD_IDLE_OPTIONS
)
205 if (('r' == m_repeat
) != m_status
.repeat())
206 Changes::repeat(show_msg
);
207 if (('z' == m_random
) != m_status
.random())
208 Changes::random(show_msg
);
209 if (('s' == m_single
) != m_status
.single())
210 Changes::single(show_msg
);
211 if (('c' == m_consume
) != m_status
.consume())
212 Changes::consume(show_msg
);
213 if (('x' == m_crossfade
) != m_status
.crossfade())
214 Changes::crossfade(show_msg
);
219 if (event
& MPD_IDLE_PLAYER
)
222 if (event
& (MPD_IDLE_PLAYLIST
| MPD_IDLE_DATABASE
| MPD_IDLE_PLAYER
))
223 applyToVisibleWindows(&BaseScreen::refreshWindow
);
228 // reset local variables
236 m_current_song_id
= 0;
237 m_playlist_version
= 0;
240 const MPD::Status
&Status::get()
245 unsigned Status::elapsedTime()
247 return m_elapsed_time
;
250 /*************************************************************************/
252 void Status::Changes::playlist(unsigned previous_version
)
254 myPlaylist
->main().clearSearchResults();
255 withUnfilteredMenuReapplyFilter(myPlaylist
->main(), [previous_version
]() {
256 size_t playlist_length
= m_status
.playlistLength();
257 if (playlist_length
< myPlaylist
->main().size())
259 auto it
= myPlaylist
->main().begin()+playlist_length
;
260 auto end
= myPlaylist
->main().end();
261 for (; it
!= end
; ++it
)
262 myPlaylist
->unregisterSong(it
->value());
263 myPlaylist
->main().resizeList(playlist_length
);
266 Mpd
.GetPlaylistChanges(previous_version
, [](MPD::Song s
) {
267 size_t pos
= s
.getPosition();
268 if (pos
< myPlaylist
->main().size())
270 // if song's already in playlist, replace it with a new one
271 MPD::Song
&old_s
= myPlaylist
->main()[pos
].value();
272 myPlaylist
->unregisterSong(old_s
);
275 else // otherwise just add it to playlist
276 myPlaylist
->main().addItem(s
);
277 myPlaylist
->registerSong(s
);
281 if (m_status
.playerState() != MPD::psStop
)
282 drawTitle(myPlaylist
->nowPlayingSong());
284 myPlaylist
->reloadTotalLength();
285 myPlaylist
->reloadRemaining();
287 if (isVisible(myBrowser
))
288 markSongsInPlaylist(myBrowser
->proxySongList());
289 if (isVisible(mySearcher
))
290 markSongsInPlaylist(mySearcher
->proxySongList());
291 if (isVisible(myLibrary
))
293 markSongsInPlaylist(myLibrary
->songsProxyList());
294 myLibrary
->Songs
.refresh();
296 if (isVisible(myPlaylistEditor
))
298 markSongsInPlaylist(myPlaylistEditor
->contentProxyList());
299 myPlaylistEditor
->Content
.refresh();
303 void Status::Changes::storedPlaylists()
305 myPlaylistEditor
->requestPlaylistsUpdate();
306 myPlaylistEditor
->requestContentsUpdate();
307 if (myBrowser
->CurrentDir() == "/")
309 myBrowser
->GetDirectory("/");
310 if (isVisible(myBrowser
))
311 myBrowser
->refresh();
315 void Status::Changes::database()
317 if (isVisible(myBrowser
))
318 myBrowser
->GetDirectory(myBrowser
->CurrentDir());
320 myBrowser
->main().clear();
321 # ifdef HAVE_TAGLIB_H
322 myTagEditor
->Dirs
->clear();
323 # endif // HAVE_TAGLIB_H
324 myLibrary
->requestTagsUpdate();
325 myLibrary
->requestAlbumsUpdate();
326 myLibrary
->requestSongsUpdate();
329 void Status::Changes::playerState()
331 switch (m_status
.playerState())
334 drawTitle(myPlaylist
->nowPlayingSong());
335 myPlaylist
->reloadRemaining();
338 windowTitle("ncmpcpp " VERSION
);
339 if (Progressbar::isUnlocked())
340 Progressbar::draw(0, 0);
341 myPlaylist
->reloadRemaining();
342 if (Config
.design
== Design::Alternative
)
344 *wHeader
<< NC::XY(0, 0) << wclrtoeol
<< NC::XY(0, 1) << wclrtoeol
;
348 # ifdef ENABLE_VISUALIZER
349 if (isVisible(myVisualizer
))
350 myVisualizer
->main().clear();
351 # endif // ENABLE_VISUALIZER
357 std::string state
= playerStateToString(m_status
.playerState());
358 if (Config
.design
== Design::Alternative
)
360 *wHeader
<< NC::XY(0, 1) << NC::Format::Bold
<< state
<< NC::Format::NoBold
;
363 else if (Statusbar::isUnlocked() && Config
.statusbar_visibility
)
365 *wFooter
<< NC::XY(0, 1);
367 *wFooter
<< wclrtoeol
;
369 *wFooter
<< NC::Format::Bold
<< state
<< NC::Format::NoBold
;
372 // needed for immediate display after starting
373 // player from stopped state or seeking
377 void Status::Changes::songID()
379 // update information about current song
380 myPlaylist
->reloadRemaining();
381 playing_song_scroll_begin
= 0;
382 first_line_scroll_begin
= 0;
383 second_line_scroll_begin
= 0;
384 if (m_status
.playerState() != MPD::psStop
)
387 if (!Config
.execute_on_song_change
.empty())
388 res
= system(Config
.execute_on_song_change
.c_str());
390 # ifdef HAVE_CURL_CURL_H
391 if (Config
.fetch_lyrics_in_background
)
392 Lyrics::DownloadInBackground(myPlaylist
->nowPlayingSong());
393 # endif // HAVE_CURL_CURL_H
395 drawTitle(myPlaylist
->nowPlayingSong());
397 if (Config
.autocenter_mode
&& !myPlaylist
->main().isFiltered())
398 myPlaylist
->main().highlight(Status::get().currentSongPosition());
400 if (Config
.now_playing_lyrics
&& isVisible(myLyrics
) && myLyrics
->previousScreen() == myPlaylist
)
401 myLyrics
->ReloadNP
= 1;
406 void Status::Changes::elapsedTime(bool update_elapsed
)
409 m_elapsed_time
= Mpd
.getStatus().elapsedTime();
410 const auto &st
= m_status
;
412 if (st
.playerState() == MPD::psStop
)
414 if (Statusbar::isUnlocked() && Config
.statusbar_visibility
)
415 *wFooter
<< NC::XY(0, 1) << wclrtoeol
;
419 std::string ps
= playerStateToString(st
.playerState());
420 MPD::Song np
= myPlaylist
->nowPlayingSong();
423 std::string tracklength
;
424 switch (Config
.design
)
426 case Design::Classic
:
427 if (Statusbar::isUnlocked() && Config
.statusbar_visibility
)
429 if (Config
.display_bitrate
&& st
.kbps())
432 tracklength
+= boost::lexical_cast
<std::string
>(st
.kbps());
433 tracklength
+= " kbps]";
438 if (Config
.display_remaining_time
)
441 tracklength
+= MPD::Song::ShowTime(st
.totalTime()-m_elapsed_time
);
444 tracklength
+= MPD::Song::ShowTime(m_elapsed_time
);
446 tracklength
+= MPD::Song::ShowTime(st
.totalTime());
451 tracklength
+= MPD::Song::ShowTime(m_elapsed_time
);
455 stringToBuffer(ToWString(Charset::utf8ToLocale(np
.toString(Config
.song_status_format
, Config
.tags_separator
, "$"))), np_song
);
456 *wFooter
<< NC::XY(0, 1) << wclrtoeol
<< NC::Format::Bold
<< ps
<< NC::Format::NoBold
;
457 writeCyclicBuffer(np_song
, *wFooter
, playing_song_scroll_begin
, wFooter
->getWidth()-ps
.length()-tracklength
.length(), L
" ** ");
458 *wFooter
<< NC::Format::Bold
<< NC::XY(wFooter
->getWidth()-tracklength
.length(), 1) << tracklength
<< NC::Format::NoBold
;
461 case Design::Alternative
:
462 if (Config
.display_remaining_time
)
465 tracklength
+= MPD::Song::ShowTime(st
.totalTime()-m_elapsed_time
);
468 tracklength
= MPD::Song::ShowTime(m_elapsed_time
);
472 tracklength
+= MPD::Song::ShowTime(st
.totalTime());
474 // bitrate here doesn't look good, but it can be moved somewhere else later
475 if (Config
.display_bitrate
&& st
.kbps())
478 tracklength
+= boost::lexical_cast
<std::string
>(st
.kbps());
479 tracklength
+= " kbps";
482 NC::WBuffer first
, second
;
483 stringToBuffer(ToWString(Charset::utf8ToLocale(np
.toString(Config
.new_header_first_line
, Config
.tags_separator
, "$"))), first
);
484 stringToBuffer(ToWString(Charset::utf8ToLocale(np
.toString(Config
.new_header_second_line
, Config
.tags_separator
, "$"))), second
);
486 size_t first_len
= wideLength(first
.str());
487 size_t first_margin
= (std::max(tracklength
.length()+1, VolumeState
.length()))*2;
488 size_t first_start
= first_len
< COLS
-first_margin
? (COLS
-first_len
)/2 : tracklength
.length()+1;
490 size_t second_len
= wideLength(second
.str());
491 size_t second_margin
= (std::max(ps
.length(), size_t(8))+1)*2;
492 size_t second_start
= second_len
< COLS
-second_margin
? (COLS
-second_len
)/2 : ps
.length()+1;
494 if (!Global::SeekingInProgress
)
495 *wHeader
<< NC::XY(0, 0) << wclrtoeol
<< tracklength
;
496 *wHeader
<< NC::XY(first_start
, 0);
497 writeCyclicBuffer(first
, *wHeader
, first_line_scroll_begin
, COLS
-tracklength
.length()-VolumeState
.length()-1, L
" ** ");
499 *wHeader
<< NC::XY(0, 1) << wclrtoeol
<< NC::Format::Bold
<< ps
<< NC::Format::NoBold
;
500 *wHeader
<< NC::XY(second_start
, 1);
501 writeCyclicBuffer(second
, *wHeader
, second_line_scroll_begin
, COLS
-ps
.length()-8-2, L
" ** ");
503 *wHeader
<< NC::XY(wHeader
->getWidth()-VolumeState
.length(), 0) << Config
.volume_color
<< VolumeState
<< NC::Color::End
;
507 if (Progressbar::isUnlocked())
508 Progressbar::draw(m_elapsed_time
, st
.totalTime());
511 void Status::Changes::repeat(bool show_msg
)
513 m_repeat
= m_status
.repeat() ? 'r' : 0;
515 Statusbar::printf("Repeat mode is %1%", !m_repeat
? "off" : "on");
518 void Status::Changes::random(bool show_msg
)
520 m_random
= m_status
.random() ? 'z' : 0;
522 Statusbar::printf("Random mode is %1%", !m_random
? "off" : "on");
525 void Status::Changes::single(bool show_msg
)
527 m_single
= m_status
.single() ? 's' : 0;
529 Statusbar::printf("Single mode is %1%", !m_single
? "off" : "on");
532 void Status::Changes::consume(bool show_msg
)
534 m_consume
= m_status
.consume() ? 'c' : 0;
536 Statusbar::printf("Consume mode is %1%", !m_consume
? "off" : "on");
539 void Status::Changes::crossfade(bool show_msg
)
541 int crossfade
= m_status
.crossfade();
542 m_crossfade
= crossfade
? 'x' : 0;
544 Statusbar::printf("Crossfade set to %1% seconds", crossfade
);
547 void Status::Changes::dbUpdateState(bool show_msg
)
549 m_db_updating
= m_status
.updateID() ? 'U' : 0;
551 Statusbar::printf("Database update %1%", m_status
.updateID() ? "started" : "finished");
554 void Status::Changes::flags()
556 if (!Config
.header_visibility
&& Config
.design
== Design::Classic
)
559 std::string switch_state
;
560 switch (Config
.design
)
562 case Design::Classic
:
564 switch_state
+= m_repeat
;
566 switch_state
+= m_random
;
568 switch_state
+= m_single
;
570 switch_state
+= m_consume
;
572 switch_state
+= m_crossfade
;
574 switch_state
+= m_db_updating
;
576 // this is done by raw ncurses because creating another
577 // window only for handling this is quite silly
578 attrset(A_BOLD
|COLOR_PAIR(int(Config
.state_line_color
)));
579 mvhline(1, 0, 0, COLS
);
580 if (!switch_state
.empty())
582 mvprintw(1, COLS
-switch_state
.length()-3, "[");
583 attroff(COLOR_PAIR(int(Config
.state_line_color
)));
584 attron(COLOR_PAIR(int(Config
.state_flags_color
)));
585 mvprintw(1, COLS
-switch_state
.length()-2, "%s", switch_state
.c_str());
586 attroff(COLOR_PAIR(int(Config
.state_flags_color
)));
587 attron(COLOR_PAIR(int(Config
.state_line_color
)));
588 mvprintw(1, COLS
-2, "]");
593 case Design::Alternative
:
595 switch_state
+= m_repeat
? m_repeat
: '-';
596 switch_state
+= m_random
? m_random
: '-';
597 switch_state
+= m_single
? m_single
: '-';
598 switch_state
+= m_consume
? m_consume
: '-';
599 switch_state
+= m_crossfade
? m_crossfade
: '-';
600 switch_state
+= m_db_updating
? m_db_updating
: '-';
602 *wHeader
<< NC::XY(COLS
-switch_state
.length(), 1) << NC::Format::Bold
<< Config
.state_flags_color
<< switch_state
<< NC::Color::End
<< NC::Format::NoBold
;
603 if (!Config
.header_visibility
) // in this case also draw separator
605 *wHeader
<< NC::Format::Bold
<< NC::Color::Black
;
606 mvwhline(wHeader
->raw(), 2, 0, 0, COLS
);
607 *wHeader
<< NC::Color::End
<< NC::Format::NoBold
;
614 void Status::Changes::mixer()
616 if (!Config
.display_volume_level
|| (!Config
.header_visibility
&& Config
.design
== Design::Classic
))
619 switch (Config
.design
)
621 case Design::Classic
:
622 VolumeState
= " " "Volume" ": ";
624 case Design::Alternative
:
625 VolumeState
= " " "Vol" ": ";
628 if (m_status
.volume() < 0)
629 VolumeState
+= "n/a";
632 VolumeState
+= boost::lexical_cast
<std::string
>(m_status
.volume());
635 *wHeader
<< Config
.volume_color
;
636 *wHeader
<< NC::XY(wHeader
->getWidth()-VolumeState
.length(), 0) << VolumeState
;
637 *wHeader
<< NC::Color::End
;
641 void Status::Changes::outputs()
643 # ifdef ENABLE_OUTPUTS
644 myOutputs
->FetchList();
645 # endif // ENABLE_OUTPUTS