summaryrefslogtreecommitdiff
path: root/src/helpers.cpp
blob: 6cecec015c4a53d9de20f4c76912c61da7c9f37d (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
/***************************************************************************
 *   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 <cassert>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stdexcept>

#include "clock.h"
#include "charset.h"
#include "global.h"
#include "helpers.h"
#include "playlist.h"
#include "status.h"
#include "tag_editor.h"
#include "help.h"
#include "playlist_editor.h"
#include "browser.h"
#include "media_library.h"
#include "search_engine.h"
#include "outputs.h"
#include "visualizer.h"

bool ConnectToMPD()
{
	if (!Mpd.Connect())
	{
		std::cout << "Couldn't connect to MPD (host = " << Mpd.GetHostname() << ", port = " << Mpd.GetPort() << "): " << Mpd.GetErrorMessage() << std::endl;
		return false;
	}
	return true;
}

void ParseArgv(int argc, char **argv)
{
	bool quit = 0;
	std::string now_playing_format = "{{{(%l) }{{%a - }%t}}|{%f}}";
	
	for (int i = 1; i < argc; ++i)
	{
		if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--host"))
		{
			if (++i >= argc)
				exit(0);
			Mpd.SetHostname(argv[i]);
			continue;
		}
		if (!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port"))
		{
			if (++i >= argc)
				exit(0);
			Mpd.SetPort(atoi(argv[i]));
			continue;
		}
		else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version"))
		{
			std::cout << "ncmpcpp version: " << VERSION << "\n\n"
			<< "optional screens compiled-in:\n"
#			ifdef HAVE_TAGLIB_H
			<< " - tag editor\n"
			<< " - tiny tag editor\n"
#			endif
#			ifdef HAVE_CURL_CURL_H
			<< " - artist info\n"
#			endif
#			ifdef ENABLE_OUTPUTS
			<< " - outputs\n"
#			endif
#			ifdef ENABLE_VISUALIZER
			<< " - visualizer\n"
#			endif
#			ifdef ENABLE_CLOCK
			<< " - clock\n"
#			endif
			<< "\nencoding detection: "
#			ifdef HAVE_LANGINFO_H
			<< "enabled"
#			else
			<< "disabled"
#			endif // HAVE_LANGINFO_H
			<< "\nbuilt with support for:"
#			ifdef HAVE_CURL_CURL_H
			<< " curl"
#			endif
#			ifdef HAVE_ICONV_H
			<< " iconv"
#			endif
#			ifdef HAVE_FFTW3_H
			<< " fftw"
#			endif
#			ifdef USE_PDCURSES
			<< " pdcurses"
#			else
			<< " ncurses"
#			endif
#			ifdef HAVE_TAGLIB_H
			<< " taglib"
#			endif
#			ifdef _UTF8
			<< " unicode"
#			endif
			<< std::endl;
			exit(0);
		}
		else if (!strcmp(argv[i], "-?") || !strcmp(argv[i], "--help"))
		{
			std::cout
			<< "Usage: ncmpcpp [OPTION]...\n"
			<< "  -h, --host                connect to server at host [localhost]\n"
			<< "  -p, --port                connect to server at port [6600]\n"
			<< "  -c, --config              use alternative configuration file\n"
			<< "  -s, --screen <name>       specify the startup screen\n"
			<< "  -?, --help                show this help message\n"
			<< "  -v, --version             display version information\n"
			<< "  --now-playing             display now playing song [" << now_playing_format << "]\n"
			<< "\n"
			<< "  play                      start playing\n"
			<< "  pause                     pause the currently playing song\n"
			<< "  toggle                    toggle play/pause mode\n"
			<< "  stop                      stop playing\n"
			<< "  next                      play the next song\n"
			<< "  prev                      play the previous song\n"
			<< "  volume [+-]<num>          adjusts volume by [+-]<num>\n"
			;
			exit(0);
		}
		
		if (!ConnectToMPD())
			exit(1);
		
		if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--screen"))
		{
			if (++i == argc) {
				std::cout << "ncmpcpp: no screen specified" << std::endl;
				exit(0);
			}
			if (!strcmp(argv[i], "help"))
				Config.startup_screen = myHelp;
			else if (!strcmp(argv[i], "playlist"))
				Config.startup_screen = myPlaylist;
			else if (!strcmp(argv[i], "browser"))
				Config.startup_screen = myBrowser;
			else if (!strcmp(argv[i], "search-engine"))
				Config.startup_screen = mySearcher;
			else if (!strcmp(argv[i], "media-library"))
				Config.startup_screen = myLibrary;
			else if (!strcmp(argv[i], "playlist-editor"))
				Config.startup_screen = myPlaylistEditor;
#			ifdef HAVE_TAGLIB_H
			else if (!strcmp(argv[i], "tag-editor"))
				Config.startup_screen = myTagEditor;
#			endif // HAVE_TAGLIB_H
#			ifdef ENABLE_OUTPUTS
			else if (!strcmp(argv[i], "outputs"))
				Config.startup_screen = myOutputs;
#			endif // ENABLE_OUTPUTS
#			ifdef ENABLE_VISUALIZER
			else if (!strcmp(argv[i], "visualizer"))
				Config.startup_screen = myVisualizer;
#			endif // ENABLE_VISUALIZER
#			ifdef ENABLE_CLOCK
			else if (!strcmp(argv[i], "clock"))
				Config.startup_screen = myClock;
#			endif // ENABLE_CLOCK
			else {
				std::cout << "ncmpcpp: invalid screen: " << argv[i] << std::endl;
				exit(0);
			}
		}
		else if (!strcmp(argv[i], "--now-playing"))
		{
			Mpd.UpdateStatus();
			if (!Mpd.GetErrorMessage().empty())
			{
				std::cout << "Error: " << Mpd.GetErrorMessage() << std::endl;
				exit(1);
			}
			if (Mpd.isPlaying())
			{
				if (argc > ++i)
				{
					if (MPD::Song::isFormatOk("now-playing format", argv[i]))
					{
						// apply additional pair of braces
						now_playing_format = "{";
						now_playing_format += argv[i];
						now_playing_format += "}";
						Replace(now_playing_format, "\\n", "\n");
						Replace(now_playing_format, "\\t", "\t");
					}
				}
				std::cout << utf_to_locale_cpy(Mpd.GetCurrentlyPlayingSong().toString(now_playing_format)) << "\n";
			}
			exit(0);
		}
		else if (!strcmp(argv[i], "play"))
		{
			Mpd.Play();
			quit = 1;
		}
		else if (!strcmp(argv[i], "pause"))
		{
			Mpd.Pause(1);
			quit = 1;
		}
		else if (!strcmp(argv[i], "toggle"))
		{
			Mpd.UpdateStatus();
			if (!Mpd.GetErrorMessage().empty())
			{
				std::cout << "Error: " << Mpd.GetErrorMessage() << std::endl;
				exit(1);
			}
			Mpd.Toggle();
			quit = 1;
		}
		else if (!strcmp(argv[i], "stop"))
		{
			Mpd.Stop();
			quit = 1;
		}
		else if (!strcmp(argv[i], "next"))
		{
			Mpd.Next();
			quit = 1;
		}
		else if (!strcmp(argv[i], "prev"))
		{
			Mpd.Prev();
			quit = 1;
		}
		else if (!strcmp(argv[i], "volume"))
		{
			i++;
			Mpd.UpdateStatus();
			if (!Mpd.GetErrorMessage().empty())
			{
				std::cout << "Error: " << Mpd.GetErrorMessage() << std::endl;
				exit(1);
			}
			if (i != argc)
				Mpd.SetVolume(Mpd.GetVolume()+atoi(argv[i]));
			quit = 1;
		}
		else if (!strcmp(argv[i], "-c") || !strcmp(argv[i], "--config"))
		{
			// this is used in NcmpcppConfig::CheckForCommandLineConfigFilePath, ignoring here.
			++i;
		}
		else
		{
			std::cout << "ncmpcpp: invalid option: " << argv[i] << std::endl;
			exit(0);
		}
		if (!Mpd.GetErrorMessage().empty())
		{
			std::cout << "Error: " << Mpd.GetErrorMessage() << std::endl;
			exit(0);
		}
	}
	if (quit)
		exit(0);
}

int CaseInsensitiveStringComparison::operator()(const std::string &a, const std::string &b)
{
	const char *i = a.c_str();
	const char *j = b.c_str();
	if (Config.ignore_leading_the)
	{
		if (hasTheWord(a))
			i += 4;
		if (hasTheWord(b))
			j += 4;
	}
	int dist;
	while (!(dist = tolower(*i)-tolower(*j)) && *j)
		++i, ++j;
	return dist;
}

bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b)
{
	bool result = false;
	if (a.type == b.type)
	{
		switch (a.type)
		{
			case MPD::itDirectory:
				result = cmp(ExtractTopName(a.name), ExtractTopName(b.name)) < 0;
				break;
			case MPD::itPlaylist:
				result = cmp(a.name, b.name) < 0;
				break;
			case MPD::itSong:
				switch (Config.browser_sort_mode)
				{
					case smName:
						result = operator()(a.song, b.song);
						break;
					case smMTime:
						result = a.song->GetMTime() > b.song->GetMTime();
						break;
					case smCustomFormat:
						result = cmp(a.song->toString(Config.browser_sort_format), b.song->toString(Config.browser_sort_format)) < 0;
						break;
				}
				break;
			default: // there is no other option, silence compiler
				assert(false);
		}
	}
	else
		result = a.type < b.type;
	return result;
}

std::string Timestamp(time_t t)
{
	char result[32];
#	ifdef WIN32
	result[strftime(result, 31, "%x %X", localtime(&t))] = 0;
#	else
	tm info;
	result[strftime(result, 31, "%x %X", localtime_r(&t, &info))] = 0;
#	endif // WIN32
	return result;
}

void UpdateSongList(Menu<MPD::Song> *menu)
{
	bool bold = 0;
	for (size_t i = 0; i < menu->Size(); ++i)
	{
		for (size_t j = 0; j < myPlaylist->Items->Size(); ++j)
		{
			if (myPlaylist->Items->at(j).GetHash() == menu->at(i).GetHash())
			{
				bold = 1;
				break;
			}
		}
		menu->Bold(i, bold);
		bold = 0;
	}
	menu->Refresh();
}

#ifdef HAVE_TAGLIB_H
std::string FindSharedDir(Menu<MPD::Song> *menu)
{
	MPD::SongList list;
	for (size_t i = 0; i < menu->Size(); ++i)
		list.push_back(&(*menu)[i]);
	return FindSharedDir(list);
}

std::string FindSharedDir(const MPD::SongList &v)
{
	if (v.empty()) // this should never happen, but in case...
		FatalError("empty SongList passed to FindSharedDir(const SongList &)!");
	size_t i = -1;
	std::string first = v.front()->GetDirectory();
	for (MPD::SongList::const_iterator it = ++v.begin(); it != v.end(); ++it)
	{
		size_t j = 0;
		std::string dir = (*it)->GetDirectory();
		size_t length = std::min(first.length(), dir.length());
		while (!first.compare(j, 1, dir, j, 1) && j < length && j < i)
			++j;
		i = j;
	}
	return i ? first.substr(0, i) : "/";
}
#endif // HAVE_TAGLIB_H

std::string FindSharedDir(const std::string &one, const std::string &two)
{
	if (one == two)
		return one;
	size_t i = 0;
	while (!one.compare(i, 1, two, i, 1))
		++i;
	i = one.rfind("/", i);
	return i != std::string::npos ? one.substr(0, i) : "/";
}

std::string GetLineValue(std::string &line, char a, char b, bool once)
{
	int pos[2] = { -1, -1 };
	char x = a;
	size_t i = 0;
	while ((i = line.find(x, i)) != std::string::npos && pos[1] < 0)
	{
		if (i && line[i-1] == '\\')
		{
			i++;
			continue;
		}
		if (once)
			line[i] = 0;
		pos[pos[0] >= 0] = i++;
		if (x == a)
			x = b;
	}
	++pos[0];
	std::string result = pos[0] >= 0 && pos[1] >= 0 ? line.substr(pos[0], pos[1]-pos[0]) : "";
	
	// replace \a and \b with a and b respectively
	char r1[] = "\\ ", r2[] = " ";
	r1[1] = r2[0] = a;
	Replace(result, r1, r2);
	if (a != b)
	{
		r1[1] = r2[0] = b;
		Replace(result, r1, r2);
	}
	
	return result;
}

std::string ExtractTopName(const std::string &s)
{
	size_t slash = s.rfind("/");
	return slash != std::string::npos ? s.substr(++slash) : s;
}

std::string PathGoDownOneLevel(const std::string &path)
{
	size_t i = path.rfind('/');
	return i == std::string::npos ? "/" : path.substr(0, i);
}

std::basic_string<my_char_t> Scroller(const std::basic_string<my_char_t> &str, size_t &pos, size_t width)
{
	std::basic_string<my_char_t> s(str);
	if (!Config.header_text_scrolling)
		return s;
	std::basic_string<my_char_t> result;
	size_t len = Window::Length(s);
	
	if (len > width)
	{
		s += U(" ** ");
		len = 0;
		std::basic_string<my_char_t>::const_iterator b = s.begin(), e = s.end();
		for (std::basic_string<my_char_t>::const_iterator it = b+pos; it < e && len < width; ++it)
		{
			if ((len += wcwidth(*it)) > width)
				break;
			result += *it;
		}
		if (++pos >= s.length())
			pos = 0;
		for (; len < width; ++b)
		{
			if ((len += wcwidth(*b)) > width)
				break;
			result += *b;
		}
	}
	else
		result = s;
	return result;
}