summaryrefslogtreecommitdiff
path: root/src/statusbar.cpp
blob: a7c99c83db480ecf62ee467d9db8172fb4141476 (plain)
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
220
221
222
/***************************************************************************
 *   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 "global.h"
#include "settings.h"
#include "status.h"
#include "statusbar.h"
#include "bindings.h"
#include "playlist.h"
#include "utility/wide_string.h"

using Global::wFooter;

namespace {//

boost::posix_time::ptime statusbarLockTime;
boost::posix_time::seconds statusbarLockDelay(-1);

bool statusbarBlockUpdate = false;
bool progressbarBlockUpdate = false;
bool statusbarAllowUnlock = true;

}

void Progressbar::lock()
{
	progressbarBlockUpdate = true;
}

void Progressbar::unlock()
{
	progressbarBlockUpdate = false;
}

bool Progressbar::isUnlocked()
{
	return !progressbarBlockUpdate;
}

void Progressbar::draw(unsigned int elapsed, unsigned int time)
{
	unsigned pb_width = wFooter->getWidth();
	unsigned howlong = time ? pb_width*elapsed/time : 0;
	if (Config.progressbar_boldness)
		*wFooter << NC::Format::Bold;
	*wFooter << Config.progressbar_color;
	if (Config.progressbar[2] != '\0')
	{
		wFooter->goToXY(0, 0);
		for (unsigned i = 0; i < pb_width; ++i)
			*wFooter << Config.progressbar[2];
		wFooter->goToXY(0, 0);
	}
	else
		mvwhline(wFooter->raw(), 0, 0, 0, pb_width);
	if (time)
	{
		*wFooter << Config.progressbar_elapsed_color;
		pb_width = std::min(size_t(howlong), wFooter->getWidth());
		for (unsigned i = 0; i < pb_width; ++i)
			*wFooter << Config.progressbar[0];
		if (howlong < wFooter->getWidth())
			*wFooter << Config.progressbar[1];
		*wFooter << NC::Color::End;
	}
	*wFooter << NC::Color::End;
	if (Config.progressbar_boldness)
		*wFooter << NC::Format::NoBold;
}

void Statusbar::lock()
{
	if (Config.statusbar_visibility)
		statusbarBlockUpdate = true;
	else
		progressbarBlockUpdate = true;
	statusbarAllowUnlock = false;
}

void Statusbar::unlock()
{
	statusbarAllowUnlock = true;
	if (statusbarLockDelay.is_negative())
	{
		if (Config.statusbar_visibility)
			statusbarBlockUpdate = false;
		else
			progressbarBlockUpdate = false;
	}
	if (Status::get().playerState() == MPD::psStop)
	{
		switch (Config.design)
		{
			case Design::Classic:
				put() << wclrtoeol;
				break;
			case Design::Alternative:
				Progressbar::draw(Status::elapsedTime(), Status::get().totalTime());
				break;
		}
		wFooter->refresh();
	}
}

bool Statusbar::isUnlocked()
{
	return !statusbarBlockUpdate;
}

void Statusbar::tryRedraw()
{
	using Global::Timer;
	if (statusbarLockDelay > boost::posix_time::seconds(0)
	&&  Timer - statusbarLockTime > statusbarLockDelay)
	{
		statusbarLockDelay = boost::posix_time::seconds(-1);
		
		if (Config.statusbar_visibility)
			statusbarBlockUpdate = !statusbarAllowUnlock;
		else
			progressbarBlockUpdate = !statusbarAllowUnlock;
		
		if (Status::get().playerState() != MPD::psStop && !statusbarBlockUpdate && !progressbarBlockUpdate)
		{
			switch (Config.design)
			{
				case Design::Classic:
					Status::Changes::elapsedTime(false);
					break;
				case Design::Alternative:
					Progressbar::draw(Status::elapsedTime(), Status::get().totalTime());
					break;
			}
			wFooter->refresh();
		}
	}
}

NC::Window &Statusbar::put()
{
	*wFooter << NC::XY(0, Config.statusbar_visibility ? 1 : 0) << wclrtoeol;
	return *wFooter;
}

void Statusbar::print(int delay, const std::string &message)
{
	if (statusbarAllowUnlock)
	{
		statusbarLockTime = Global::Timer;
		statusbarLockDelay = boost::posix_time::seconds(delay);
		if (Config.statusbar_visibility)
			statusbarBlockUpdate = true;
		else
			progressbarBlockUpdate = true;
		wFooter->goToXY(0, Config.statusbar_visibility);
		*wFooter << message << wclrtoeol;
		wFooter->refresh();
	}
}

void Statusbar::Helpers::mpd()
{
	Status::update(Mpd.noidle());
}

bool Statusbar::Helpers::getString(const char *)
{
	Status::trace();
	return true;
}

bool Statusbar::Helpers::ApplyFilterImmediately::operator()(const char *s)
{
	using Global::myScreen;
	// if input queue is not empty, we don't want to update filter since next
	// character will be taken from queue immediately, trigering this function
	// again and thus making it inefficient, so let's apply filter only if
	// "real" user input arrived. however, we want to apply filter if ENTER
	// is next in queue, so its effects will be seen.
	if (wFooter->inputQueue().empty() || wFooter->inputQueue().front() == KEY_ENTER)
	{
		if (m_s != s)
		{
			m_s = s;
			m_f->applyFilter(m_s);
			myScreen->refreshWindow();
		}
		Status::trace();
	}
	return true;
}

bool Statusbar::Helpers::TryExecuteImmediateCommand::operator()(const char *s)
{
	bool continue_ = true;
	if (m_s != s)
	{
		m_s = s;
		auto cmd = Bindings.findCommand(m_s);
		if (cmd && cmd->immediate())
			continue_ = false;
	}
	Status::trace();
	return continue_;
}