statusbar: clear statusbar after message delay even if player is stopped
[ncmpcpp.git] / src / statusbar.cpp
blob32690a75bf58572bd0d8260372863f481cabbbfb
1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
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. *
9 * *
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. *
14 * *
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 "global.h"
22 #include "settings.h"
23 #include "status.h"
24 #include "statusbar.h"
25 #include "bindings.h"
26 #include "playlist.h"
27 #include "utility/wide_string.h"
29 using Global::wFooter;
31 namespace {//
33 boost::posix_time::ptime statusbarLockTime;
34 boost::posix_time::seconds statusbarLockDelay(-1);
36 bool statusbarBlockUpdate = false;
37 bool progressbarBlockUpdate = false;
38 bool statusbarAllowUnlock = true;
42 void Progressbar::lock()
44 progressbarBlockUpdate = true;
47 void Progressbar::unlock()
49 progressbarBlockUpdate = false;
52 bool Progressbar::isUnlocked()
54 return !progressbarBlockUpdate;
57 void Progressbar::draw(unsigned int elapsed, unsigned int time)
59 unsigned pb_width = wFooter->getWidth();
60 unsigned howlong = time ? pb_width*elapsed/time : 0;
61 if (Config.progressbar_boldness)
62 *wFooter << NC::Format::Bold;
63 *wFooter << Config.progressbar_color;
64 if (Config.progressbar[2] != '\0')
66 wFooter->goToXY(0, 0);
67 for (unsigned i = 0; i < pb_width; ++i)
68 *wFooter << Config.progressbar[2];
69 wFooter->goToXY(0, 0);
71 else
72 mvwhline(wFooter->raw(), 0, 0, 0, pb_width);
73 if (time)
75 *wFooter << Config.progressbar_elapsed_color;
76 pb_width = std::min(size_t(howlong), wFooter->getWidth());
77 for (unsigned i = 0; i < pb_width; ++i)
78 *wFooter << Config.progressbar[0];
79 if (howlong < wFooter->getWidth())
80 *wFooter << Config.progressbar[1];
81 *wFooter << NC::Color::End;
83 *wFooter << NC::Color::End;
84 if (Config.progressbar_boldness)
85 *wFooter << NC::Format::NoBold;
88 void Statusbar::lock()
90 if (Config.statusbar_visibility)
91 statusbarBlockUpdate = true;
92 else
93 progressbarBlockUpdate = true;
94 statusbarAllowUnlock = false;
97 void Statusbar::unlock()
99 statusbarAllowUnlock = true;
100 if (statusbarLockDelay.is_negative())
102 if (Config.statusbar_visibility)
103 statusbarBlockUpdate = false;
104 else
105 progressbarBlockUpdate = false;
107 if (Status::State::player() == MPD::psStop)
109 switch (Config.design)
111 case Design::Classic:
112 put() << wclrtoeol;
113 break;
114 case Design::Alternative:
115 Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime());
116 break;
118 wFooter->refresh();
122 bool Statusbar::isUnlocked()
124 return !statusbarBlockUpdate;
127 void Statusbar::tryRedraw()
129 using Global::Timer;
130 if (statusbarLockDelay > boost::posix_time::seconds(0)
131 && Timer - statusbarLockTime > statusbarLockDelay)
133 statusbarLockDelay = boost::posix_time::seconds(-1);
135 if (Config.statusbar_visibility)
136 statusbarBlockUpdate = !statusbarAllowUnlock;
137 else
138 progressbarBlockUpdate = !statusbarAllowUnlock;
140 if (!statusbarBlockUpdate && !progressbarBlockUpdate)
142 switch (Config.design)
144 case Design::Classic:
145 switch (Status::State::player())
147 case MPD::psUnknown:
148 case MPD::psStop:
149 put() << wclrtoeol;
150 break;
151 case MPD::psPlay:
152 case MPD::psPause:
153 Status::Changes::elapsedTime(false);
154 break;
156 break;
157 case Design::Alternative:
158 Progressbar::draw(Status::State::elapsedTime(), Status::State::totalTime());
159 break;
161 wFooter->refresh();
166 NC::Window &Statusbar::put()
168 *wFooter << NC::XY(0, Config.statusbar_visibility ? 1 : 0) << wclrtoeol;
169 return *wFooter;
172 void Statusbar::print(int delay, const std::string &message)
174 if (statusbarAllowUnlock)
176 statusbarLockTime = Global::Timer;
177 statusbarLockDelay = boost::posix_time::seconds(delay);
178 if (Config.statusbar_visibility)
179 statusbarBlockUpdate = true;
180 else
181 progressbarBlockUpdate = true;
182 wFooter->goToXY(0, Config.statusbar_visibility);
183 *wFooter << message << wclrtoeol;
184 wFooter->refresh();
188 void Statusbar::Helpers::mpd()
190 Status::update(Mpd.noidle());
193 bool Statusbar::Helpers::getString(const char *)
195 Status::trace();
196 return true;
199 bool Statusbar::Helpers::ApplyFilterImmediately::operator()(const char *s)
201 using Global::myScreen;
202 // if input queue is not empty, we don't want to update filter since next
203 // character will be taken from queue immediately, trigering this function
204 // again and thus making it inefficient, so let's apply filter only if
205 // "real" user input arrived. however, we want to apply filter if ENTER
206 // is next in queue, so its effects will be seen.
207 if (wFooter->inputQueue().empty() || wFooter->inputQueue().front() == KEY_ENTER)
209 if (m_s != s)
211 m_s = s;
212 m_f->applyFilter(m_s);
213 myScreen->refreshWindow();
215 Status::trace();
217 return true;
220 bool Statusbar::Helpers::TryExecuteImmediateCommand::operator()(const char *s)
222 bool continue_ = true;
223 if (m_s != s)
225 m_s = s;
226 auto cmd = Bindings.findCommand(m_s);
227 if (cmd && cmd->immediate())
228 continue_ = false;
230 Status::trace();
231 return continue_;