summaryrefslogtreecommitdiff
path: root/src/status.cpp
blob: 8e810d8c3883e76f3b1a48364cbcbe2072db32e1 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/***************************************************************************
 *   Copyright (C) 2008-2012 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 <sys/time.h>

#include "browser.h"
#include "charset.h"
#include "global.h"
#include "helpers.h"
#include "lyrics.h"
#include "media_library.h"
#include "outputs.h"
#include "playlist.h"
#include "playlist_editor.h"
#include "search_engine.h"
#include "sel_items_adder.h"
#include "settings.h"
#include "status.h"
#include "statusbar.h"
#include "tag_editor.h"
#include "visualizer.h"
#include "title.h"

using Global::myScreen;

using Global::wFooter;
using Global::wHeader;

using Global::Timer;
using Global::VolumeState;

namespace {//

timeval past = { 0, 0 };

size_t playing_song_scroll_begin = 0;
size_t first_line_scroll_begin = 0;
size_t second_line_scroll_begin = 0;
std::string player_state;

char mpd_repeat;
char mpd_random;
char mpd_single;
char mpd_consume;
char mpd_crossfade;
char mpd_db_updating;

void drawTitle(const MPD::Song &np)
{
	assert(!np.empty());
	windowTitle(np.toString(Config.song_window_title_format, Config.tags_separator));
}

}

void Status::trace()
{
	gettimeofday(&Timer, 0);
	if (Mpd.Connected() && (Mpd.SupportsIdle() || Timer.tv_sec > past.tv_sec))
	{
		if (!Mpd.SupportsIdle())
		{
			past = Timer;
		}
		else if (Config.display_bitrate && Global::Timer.tv_sec > past.tv_sec && Mpd.isPlaying())
		{
			// ncmpcpp doesn't fetch status constantly if mpd supports
			// idle mode so current song's bitrate is never updated.
			// we need to force ncmpcpp to fetch it.
			Mpd.OrderDataFetching();
			past = Timer;
		}
		Mpd.UpdateStatus();
	}
	
	applyToVisibleWindows(&BasicScreen::update);
	
	if (isVisible(myPlaylist) && myPlaylist->activeWindow() == myPlaylist->main()
	&&  Timer.tv_sec == myPlaylist->Timer().tv_sec+Config.playlist_disable_highlight_delay
	&&  myPlaylist->main()->isHighlighted()
	&&  Config.playlist_disable_highlight_delay)
	{
		myPlaylist->main()->setHighlighting(false);
		myPlaylist->main()->refresh();
	}
	
	Statusbar::tryRedraw();
}

void Status::handleError(MPD::Connection * , int errorid, const char *msg, void *)
{
	// for errorid:
	// - 0-7 bits define MPD_ERROR_* codes, compare them with (0xff & errorid)
	// - 8-15 bits define MPD_SERVER_ERROR_* codes, compare them with (errorid >> 8)
	if ((errorid >> 8) == MPD_SERVER_ERROR_PERMISSION)
	{
		wFooter->setGetStringHelper(0);
		Statusbar::put() << "Password: ";
		Mpd.SetPassword(wFooter->getString(-1, 0, 1));
		if (Mpd.SendPassword())
			Statusbar::msg("Password accepted");
		wFooter->setGetStringHelper(Statusbar::Helpers::getString);
	}
	else if ((errorid >> 8) == MPD_SERVER_ERROR_NO_EXIST && myScreen == myBrowser)
	{
		myBrowser->GetDirectory(getParentDirectory(myBrowser->CurrentDir()));
		myBrowser->refresh();
	}
	else
		Statusbar::msg("MPD: %s", msg);
}

void Status::Changes::playlist()
{
	myPlaylist->main()->clearSearchResults();
	withUnfilteredMenuReapplyFilter(*myPlaylist->main(), []() {
		size_t playlist_length = Mpd.GetPlaylistLength();
		if (playlist_length < myPlaylist->main()->size())
		{
			auto it = myPlaylist->main()->begin()+playlist_length;
			auto end = myPlaylist->main()->end();
			for (; it != end; ++it)
				myPlaylist->unregisterHash(it->value().getHash());
			myPlaylist->main()->resizeList(playlist_length);
		}
		
		auto songs = Mpd.GetPlaylistChanges(Mpd.GetOldPlaylistID());
		for (auto s = songs.begin(); s != songs.end(); ++s)
		{
			size_t pos = s->getPosition();
			if (pos < myPlaylist->main()->size())
			{
				// if song's already in playlist, replace it with a new one
				MPD::Song &old_s = (*myPlaylist->main())[pos].value();
				myPlaylist->unregisterHash(old_s.getHash());
				old_s = *s;
			}
			else // otherwise just add it to playlist
				myPlaylist->main()->addItem(*s);
			myPlaylist->registerHash(s->getHash());
		}
	});
	
	if (Mpd.isPlaying())
		drawTitle(myPlaylist->nowPlayingSong());
	
	Playlist::ReloadTotalLength = true;
	Playlist::ReloadRemaining = true;
	
	if (isVisible(myBrowser))
		markSongsInPlaylist(myBrowser->getProxySongList());
	if (isVisible(mySearcher))
		markSongsInPlaylist(mySearcher->getProxySongList());
	if (isVisible(myLibrary))
		markSongsInPlaylist(myLibrary->songsProxyList());
	if (isVisible(myPlaylistEditor))
		markSongsInPlaylist(myPlaylistEditor->contentProxyList());
}

void Status::Changes::storedPlaylists()
{
	myPlaylistEditor->requestPlaylistsUpdate();
	myPlaylistEditor->requestContentsUpdate();
	if (myBrowser->CurrentDir() == "/")
	{
		myBrowser->GetDirectory("/");
		if (isVisible(myBrowser))
			myBrowser->refresh();
	}
}

void Status::Changes::database()
{
	if (isVisible(myBrowser))
		myBrowser->GetDirectory(myBrowser->CurrentDir());
	else
		myBrowser->main()->clear();
#	ifdef HAVE_TAGLIB_H
	myTagEditor->Dirs->clear();
#	endif // HAVE_TAGLIB_H
	if (myLibrary->Columns() == 2)
		myLibrary->Albums->clear();
	else
		myLibrary->Tags->clear();
}

void Status::Changes::playerState()
{
	MPD::PlayerState state = Mpd.GetState();
	switch (state)
	{
		case MPD::psUnknown:
		{
			player_state = "[unknown]";
			break;
		}
		case MPD::psPlay:
		{
			drawTitle(myPlaylist->nowPlayingSong());
			player_state = Config.new_design ? "[playing]" : "Playing: ";
			Playlist::ReloadRemaining = true;
			if (Mpd.GetOldState() == MPD::psStop) // show track info in status immediately
				elapsedTime();
			break;
		}
		case MPD::psPause:
		{
			player_state = Config.new_design ? "[paused] " : "[Paused] ";
			break;
		}
		case MPD::psStop:
		{
			windowTitle("ncmpcpp " VERSION);
			if (Progressbar::isUnlocked())
				Progressbar::draw(0, 0);
			Playlist::ReloadRemaining = true;
			if (Config.new_design)
			{
				*wHeader << NC::XY(0, 0) << wclrtoeol << NC::XY(0, 1) << wclrtoeol;
				player_state = "[stopped]";
				mixer();
				flags();
			}
			else
				player_state.clear();
#			ifdef ENABLE_VISUALIZER
			if (isVisible(myVisualizer))
				myVisualizer->main()->clear();
#			endif // ENABLE_VISUALIZER
			break;
		}
	}
	
#	ifdef ENABLE_VISUALIZER
	if (myScreen == myVisualizer)
		wFooter->setTimeout(state == MPD::psPlay ? Visualizer::WindowTimeout : 500);
#	endif // ENABLE_VISUALIZER
	
	if (Config.new_design)
	{
		*wHeader << NC::XY(0, 1) << NC::fmtBold << player_state << NC::fmtBoldEnd;
		wHeader->refresh();
	}
	else if (Statusbar::isUnlocked() && Config.statusbar_visibility)
	{
		*wFooter << NC::XY(0, 1);
		if (player_state.empty())
			*wFooter << wclrtoeol;
		else
			*wFooter << NC::fmtBold << player_state << NC::fmtBoldEnd;
	}
}

void Status::Changes::songID()
{
	Playlist::ReloadRemaining = true;
	playing_song_scroll_begin = 0;
	first_line_scroll_begin = 0;
	second_line_scroll_begin = 0;
	if (Mpd.isPlaying())
	{
		GNUC_UNUSED int res;
		if (!Config.execute_on_song_change.empty())
			res = system(Config.execute_on_song_change.c_str());
		
#		ifdef HAVE_CURL_CURL_H
		if (Config.fetch_lyrics_in_background)
			Lyrics::DownloadInBackground(myPlaylist->nowPlayingSong());
#		endif // HAVE_CURL_CURL_H
		
		drawTitle(myPlaylist->nowPlayingSong());
		
		if (Config.autocenter_mode && !myPlaylist->main()->isFiltered())
			myPlaylist->main()->highlight(Mpd.GetCurrentlyPlayingSongPos());
		
		if (Config.now_playing_lyrics && isVisible(myLyrics) && Global::myOldScreen == myPlaylist)
			myLyrics->ReloadNP = 1;
		
		elapsedTime();
	}
}

void Status::Changes::elapsedTime()
{
	if (!Mpd.isPlaying())
	{
		if (Statusbar::isUnlocked() && Config.statusbar_visibility)
			*wFooter << NC::XY(0, 1) << wclrtoeol;
		return;
	}
	
	MPD::Song np = myPlaylist->nowPlayingSong();
	drawTitle(np);
	
	std::string tracklength;
	if (Config.new_design)
	{
		if (Config.display_remaining_time)
		{
			tracklength = "-";
			tracklength += MPD::Song::ShowTime(Mpd.GetTotalTime()-Mpd.GetElapsedTime());
		}
		else
			tracklength = MPD::Song::ShowTime(Mpd.GetElapsedTime());
		if (Mpd.GetTotalTime())
		{
			tracklength += "/";
			tracklength += MPD::Song::ShowTime(Mpd.GetTotalTime());
		}
		// bitrate here doesn't look good, but it can be moved somewhere else later
		if (Config.display_bitrate && Mpd.GetBitrate())
		{
			tracklength += " ";
			tracklength += intTo<std::string>::apply(Mpd.GetBitrate());
			tracklength += " kbps";
		}
		
		NC::WBuffer first, second;
		stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_first_line, Config.tags_separator, "$"))), first);
		stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_second_line, Config.tags_separator, "$"))), second);
		
		size_t first_len = wideLength(first.str());
		size_t first_margin = (std::max(tracklength.length()+1, VolumeState.length()))*2;
		size_t first_start = first_len < COLS-first_margin ? (COLS-first_len)/2 : tracklength.length()+1;
		
		size_t second_len = wideLength(second.str());
		size_t second_margin = (std::max(player_state.length(), size_t(8))+1)*2;
		size_t second_start = second_len < COLS-second_margin ? (COLS-second_len)/2 : player_state.length()+1;
		
		if (!Global::SeekingInProgress)
			*wHeader << NC::XY(0, 0) << wclrtoeol << tracklength;
		*wHeader << NC::XY(first_start, 0);
		first.write(*wHeader, first_line_scroll_begin, COLS-tracklength.length()-VolumeState.length()-1, L" ** ");
		
		*wHeader << NC::XY(0, 1) << wclrtoeol << NC::fmtBold << player_state << NC::fmtBoldEnd;
		*wHeader << NC::XY(second_start, 1);
		second.write(*wHeader, second_line_scroll_begin, COLS-player_state.length()-8-2, L" ** ");
		
		*wHeader << NC::XY(wHeader->getWidth()-VolumeState.length(), 0) << Config.volume_color << VolumeState << NC::clEnd;
		
		flags();
	}
	else if (Statusbar::isUnlocked() && Config.statusbar_visibility)
	{
		if (Config.display_bitrate && Mpd.GetBitrate())
		{
			tracklength += " [";
			tracklength += intTo<std::string>::apply(Mpd.GetBitrate());
			tracklength += " kbps]";
		}
		tracklength += " [";
		if (Mpd.GetTotalTime())
		{
			if (Config.display_remaining_time)
			{
				tracklength += "-";
				tracklength += MPD::Song::ShowTime(Mpd.GetTotalTime()-Mpd.GetElapsedTime());
			}
			else
				tracklength += MPD::Song::ShowTime(Mpd.GetElapsedTime());
			tracklength += "/";
			tracklength += MPD::Song::ShowTime(Mpd.GetTotalTime());
			tracklength += "]";
		}
		else
		{
			tracklength += MPD::Song::ShowTime(Mpd.GetElapsedTime());
			tracklength += "]";
		}
		NC::WBuffer np_song;
		stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.song_status_format, Config.tags_separator, "$"))), np_song);
		*wFooter << NC::XY(0, 1) << wclrtoeol << NC::fmtBold << player_state << NC::fmtBoldEnd;
		np_song.write(*wFooter, playing_song_scroll_begin, wFooter->getWidth()-player_state.length()-tracklength.length(), L" ** ");
		*wFooter << NC::fmtBold << NC::XY(wFooter->getWidth()-tracklength.length(), 1) << tracklength << NC::fmtBoldEnd;
	}
	if (Progressbar::isUnlocked())
		Progressbar::draw(Mpd.GetElapsedTime(), Mpd.GetTotalTime());
}

void Status::Changes::repeat()
{
	mpd_repeat = Mpd.GetRepeat() ? 'r' : 0;
	Statusbar::msg("Repeat mode is %s", !mpd_repeat ? "off" : "on");
}

void Status::Changes::random()
{
	mpd_random = Mpd.GetRandom() ? 'z' : 0;
	Statusbar::msg("Random mode is %s", !mpd_random ? "off" : "on");
}

void Status::Changes::single()
{
	mpd_single = Mpd.GetSingle() ? 's' : 0;
	Statusbar::msg("Single mode is %s", !mpd_single ? "off" : "on");
}

void Status::Changes::consume()
{
	mpd_consume = Mpd.GetConsume() ? 'c' : 0;
	Statusbar::msg("Consume mode is %s", !mpd_consume ? "off" : "on");
}

void Status::Changes::crossfade()
{
	int crossfade = Mpd.GetCrossfade();
	mpd_crossfade = crossfade ? 'x' : 0;
	Statusbar::msg("Crossfade set to %d seconds", crossfade);
}

void Status::Changes::dbUpdateState()
{
	mpd_db_updating = Mpd.GetDBIsUpdating() ? 'U' : 0;
	Statusbar::msg(Mpd.GetDBIsUpdating() ? "Database update started" : "Database update finished");
}

void Status::Changes::flags()
{
	if (!Config.header_visibility && !Config.new_design)
		return;
	
	std::string switch_state;
	if (Config.new_design)
	{
		switch_state += '[';
		switch_state += mpd_repeat ? mpd_repeat : '-';
		switch_state += mpd_random ? mpd_random : '-';
		switch_state += mpd_single ? mpd_single : '-';
		switch_state += mpd_consume ? mpd_consume : '-';
		switch_state += mpd_crossfade ? mpd_crossfade : '-';
		switch_state += mpd_db_updating ? mpd_db_updating : '-';
		switch_state += ']';
		*wHeader << NC::XY(COLS-switch_state.length(), 1) << NC::fmtBold << Config.state_flags_color << switch_state << NC::clEnd << NC::fmtBoldEnd;
		if (Config.new_design && !Config.header_visibility) // in this case also draw separator
		{
			*wHeader << NC::fmtBold << NC::clBlack;
			mvwhline(wHeader->raw(), 2, 0, 0, COLS);
			*wHeader << NC::clEnd << NC::fmtBoldEnd;
		}
		wHeader->refresh();
	}
	else
	{
		if (mpd_repeat)
			switch_state += mpd_repeat;
		if (mpd_random)
			switch_state += mpd_random;
		if (mpd_single)
			switch_state += mpd_single;
		if (mpd_consume)
			switch_state += mpd_consume;
		if (mpd_crossfade)
			switch_state += mpd_crossfade;
		if (mpd_db_updating)
			switch_state += mpd_db_updating;
		
		// this is done by raw ncurses because creating another
		// window only for handling this is quite silly
		attrset(A_BOLD|COLOR_PAIR(Config.state_line_color));
		mvhline(1, 0, 0, COLS);
		if (!switch_state.empty())
		{
			mvprintw(1, COLS-switch_state.length()-3, "[");
			attroff(COLOR_PAIR(Config.state_line_color));
			attron(COLOR_PAIR(Config.state_flags_color));
			mvprintw(1, COLS-switch_state.length()-2, "%s", switch_state.c_str());
			attroff(COLOR_PAIR(Config.state_flags_color));
			attron(COLOR_PAIR(Config.state_line_color));
			mvprintw(1, COLS-2, "]");
		}
		attroff(A_BOLD|COLOR_PAIR(Config.state_line_color));
		refresh();
	}
}

void Status::Changes::mixer()
{
	if (!Config.display_volume_level || (!Config.header_visibility && !Config.new_design))
		return;
	
	VolumeState = Config.new_design ? " Vol: " : " Volume: ";
	int volume = Mpd.GetVolume();
	if (volume < 0)
		VolumeState += "n/a";
	else
	{
		VolumeState += intTo<std::string>::apply(volume);
		VolumeState += "%";
	}
	*wHeader << Config.volume_color;
	*wHeader << NC::XY(wHeader->getWidth()-VolumeState.length(), 0) << VolumeState;
	*wHeader << NC::clEnd;
	wHeader->refresh();
}

void Status::Changes::outputs()
{
#	ifdef ENABLE_OUTPUTS
	myOutputs->FetchList();
#	endif // ENABLE_OUTPUTS
}

void Status::update(MPD::Connection *, MPD::StatusChanges changes, void *)
{
	if (changes.Playlist)
		Changes::playlist();
	if (changes.StoredPlaylists)
		Changes::storedPlaylists();
	if (changes.Database)
		Changes::database();
	if (changes.PlayerState)
		Changes::playerState();
	if (changes.SongID)
		Changes::songID();
	if (changes.ElapsedTime)
		Changes::elapsedTime();
	if (changes.Repeat)
		Changes::repeat();
	if (changes.Random)
		Changes::random();
	if (changes.Single)
		Changes::single();
	if (changes.Consume)
		Changes::consume();
	if (changes.Crossfade)
		Changes::crossfade();
	if (changes.DBUpdating)
		Changes::dbUpdateState();
	if (changes.StatusFlags)
		Changes::flags();
	if (changes.Volume)
		Changes::mixer();
	if (changes.Outputs)
		Changes::outputs();
	
	if (changes.PlayerState || (changes.ElapsedTime && (!Config.new_design || Mpd.GetState() == MPD::psPlay)))
		wFooter->refresh();
	if (changes.Playlist || changes.Database || changes.PlayerState || changes.SongID)
		applyToVisibleWindows(&BasicScreen::refreshWindow);
}