xaizek / pms (License: GPLv3+) (since 2018-12-07)
Older version of Practical Music Search written in C++.
<root> / src / pms.cpp (c4272fba335a15f8d9d3dfe899d18ba6fc1f5cee) (20KiB) (mode 100644) [raw]
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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
/* vi:set ts=8 sts=8 sw=8 noet:
 *
 * Practical Music Search
 * Copyright (c) 2006-2011  Kim Tore Jensen
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "pms.h"
#include "console.h"
#include "config.h"
#include "window.h"
#include "command.h"
#include "mpd.h"
#include "input.h"
#include "songlist.h"
#include "search.h"
#include "clipboard.h"
#include <stdlib.h>

extern Config		* config;
extern MPD		* mpd;
extern Windowmanager	* wm;
extern Input		* input;
extern Commandlist 	* commandlist;
extern Keybindings 	* keybindings;

int PMS::run_event(Inputevent * ev)
{
	static Inputevent lastev;
	Inputevent sev;

	if (!ev) return false;

	if (ev->result == INPUT_RESULT_RUN && ev->action != ACT_REPEATACTION && ev->action != ACT_RUN_CMD && ev != &lastev)
		lastev = *ev;

	if (ev->result == INPUT_RESULT_RUN)
	switch(ev->action)
	{
		case ACT_MODE_INPUT:
			input->setmode(INPUT_MODE_INPUT);
			wm->statusbar->qdraw();
			return true;

		case ACT_MODE_COMMAND:
			input->setmode(INPUT_MODE_COMMAND);
			wm->statusbar->qdraw();
			return true;

		case ACT_MODE_SEARCH:
			input->setmode(INPUT_MODE_SEARCH);
			wm->statusbar->qdraw();
			return true;

		case ACT_MODE_LIVESEARCH:
			input->setmode(INPUT_MODE_LIVESEARCH);
			wm->statusbar->qdraw();
			return true;

		case ACT_VISUAL:
			return visual();

		case ACT_EXIT_LIVESEARCH:
			return livesearch(ev->text, true);

		case ACT_RESET_SEARCH:
			return resetsearch();

		case ACT_CONNECT:
			config->autoconnect = true;
			mpd->mpd_disconnect();
			return true;

		case ACT_REHASH:
			sev = *ev;
			config->load_default_config();
			config->source_default_config();
			lastev = sev;
			return true;

		case ACT_SOURCE:
			sev = *ev;
			config->source(ev->text);
			lastev = sev;
			return true;

		case ACT_MAP:
			return map_keys(ev->text);

		case ACT_UNMAP:
			return keybindings->remove(ev->text);

		case ACT_RUN_CMD:
			run_cmd(ev->text, ev->multiplier);
			input->setmode(INPUT_MODE_COMMAND);
			wm->statusbar->qdraw();
			return true;

		case ACT_RUN_SEARCH:
			run_search(ev->text, ev->multiplier);
			input->setmode(INPUT_MODE_COMMAND);
			wm->statusbar->qdraw();
			wm->active->qdraw();
			wm->topbar->qdraw();
			return true;

		case ACT_REPEATACTION:
			if (ev->multiplier != 1)
				lastev.multiplier = ev->multiplier;
			return run_event(&lastev);

		case ACT_SET:
			return set_opt(ev);

		case ACT_QUIT:
			return quit();

		case ACT_RESIZE:
			wm->detect_dimensions();
			wm->playlist->update_column_length();
			wm->library->update_column_length();
			wm->draw();
			return true;

		case ACT_NEXT_WINDOW:
			return cycle_windows(ev->multiplier);

		case ACT_PREVIOUS_WINDOW:
			return cycle_windows(-ev->multiplier);

		case ACT_TOGGLE_WINDOW:
			return wm->toggle();

		case ACT_GOTO_WINDOW:
			return wm->go(ev->text);

		case ACT_GOTO_WINDOW_POS:
			return wm->go((ev->text.size() ? atoi(ev->text.c_str()) : ev->multiplier) - 1);

		case ACT_SORT:
			return sortlist(ev->text.size() ? ev->text : config->default_sort);

		case ACT_ACTIVATE_SONGLIST:
			return activate_songlist();

		case ACT_ADD:
			if (ev->text.size())
				return add(ev->text, ev->multiplier);
			return add(ev->multiplier);

		case ACT_ADD_SAME:
			return add_same(ev->text, ev->multiplier);

		case ACT_REMOVE:
			return remove(ev->multiplier);

		case ACT_YANK:
			return yank(ev->multiplier);

		case ACT_PUT:
			return put(ev->multiplier);

		case ACT_UPDATE:
			return update(ev->text);

		case ACT_SCROLL_UP:
			return scroll_window(-ev->multiplier);

		case ACT_SCROLL_DOWN:
			return scroll_window(ev->multiplier);

		case ACT_CURSOR_UP:
			return move_cursor(-ev->multiplier);

		case ACT_CURSOR_DOWN:
			return move_cursor(ev->multiplier);

		case ACT_CURSOR_PGUP:
			return move_cursor_page(-ev->multiplier);

		case ACT_CURSOR_PGDOWN:
			return move_cursor_page(ev->multiplier);

		case ACT_CURSOR_TOP:
			return set_cursor_top(ev->multiplier);

		case ACT_CURSOR_BOTTOM:
			return set_cursor_bottom(ev->multiplier);

		case ACT_CURSOR_HOME:
			return set_cursor_home(ev->multiplier);

		case ACT_CURSOR_END:
			return set_cursor_end(ev->multiplier);

		case ACT_CURSOR_CURRENTSONG:
			return set_cursor_currentsong();

		case ACT_CURSOR_RANDOM:
			return set_cursor_random();

		case ACT_CROSSFADE:
			return set_crossfade(ev->text);

		case ACT_PASSWORD:
			return set_password(ev->text);

		case ACT_TOGGLEPLAY:
			return toggle_play();

		case ACT_PLAY:
			return play();

		case ACT_STOP:
			return stop();

		case ACT_NEXT:
			return change_song(ev->multiplier);

		case ACT_PREVIOUS:
			return change_song(-ev->multiplier);

		case ACT_SEEK_FORWARD:
			return seek(ev->multiplier);

		case ACT_SEEK_BACK:
			return seek(-ev->multiplier);

		default:
			return false;
	}

	else if (ev->result == INPUT_RESULT_BUFFERED)
	{
		wm->statusbar->draw();
		wm->flush();
		wm->topbar->qdraw();

		if (input->mode == INPUT_MODE_LIVESEARCH)
			return livesearch(ev->text);
	}


	return false;
}

int PMS::run_cmd(string cmd, unsigned int multiplier, bool batch)
{
	Inputevent ev;
	Command * c;
	size_t i;

	/* Strip whitespace from beginning */
	if ((i = cmd.find_first_not_of(' ')) > 0)
	{
		if (cmd.size() > i)
			cmd = cmd.substr(i);
		else
			return false;
	}

	/* Separate command and param */
	if ((i = cmd.find(' ')) != string::npos)
	{
		ev.text = cmd.size() > i ? cmd.substr(i + 1) : "";
		cmd = cmd.substr(0, i);
	}

	/* Ignore comments and empty lines */
	if (!cmd.size() || cmd[0] == '#')
		return false;

	c = commandlist->find(wm->context, cmd);
	if (!c)
	{
		sterr("Undefined command '%s'", cmd.c_str());
		return false;
	}

	ev.action = c->action;
	ev.context = wm->context;
	ev.result = INPUT_RESULT_RUN;
	ev.silent = batch;
	ev.multiplier = multiplier > 0 ? multiplier : 1;
	return run_event(&ev);
}

int PMS::run_search(string terms, unsigned int multiplier)
{
	Wsonglist * win;
	Song * song;
	size_t i;

	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Current window is not a song list, so cannot locate any songs here.", NULL);
		return false;
	}

	song = win->cursorsong();

	if (terms.size() > 0)
		win->songlist->search(SEARCH_MODE_FILTER, config->search_field_mask, terms);
	else
		win->songlist->search(SEARCH_MODE_NONE);

	if (song && (i = win->songlist->sfind(song->fhash)) != string::npos)
		win->set_cursor(i);
	else if (win->cursor >= win->content_size())
		win->set_cursor(win->content_size() - 1);

	return true;
}

int PMS::map_keys(string params)
{
	Command * cmd;
	size_t start = 0, end = 0;
	int context;
	vector<string> s;

	while (s.size() <= 3)
	{
		if ((end = params.find(' ', start)) == string::npos)
		{
			if (s.size() >= 2)
			{
				s.push_back(params.substr(start));
				break;
			}

			sterr("Syntax: map <context> <sequence> <command>", NULL);
			return false;
		}
		s.push_back(params.substr(start, end - start));
		start = end + 1;
	}

	if (s[0] == "console")
		context = CONTEXT_CONSOLE;
	else if (s[0] == "songlist")
		context = CONTEXT_SONGLIST;
	else if (s[0] == "list")
		context = CONTEXT_LIST;
	else if (s[0] == "all")
		context = CONTEXT_ALL;
	else
	{
		sterr("Invalid context `%s', expecteded one of `console', `songlist', `list', `all'", s[0].c_str());
		return false;
	}

	if ((cmd = commandlist->find(context, s[2])) == NULL)
	{
		sterr("Invalid command `%s' for context `%s'", s[2].c_str(), s[0].c_str());
		return false;
	}

	return (keybindings->add(context, cmd->action, s[1], (s.size() == 4 ? s[3] : "")) != NULL);

}

int PMS::set_opt(Inputevent * ev)
{
	option_t * opt;

	opt = config->readline(ev->text, ev->multiplier, !ev->silent);
	if (!opt)
		return false;

	if (opt->mask & OPT_CHANGE_MPD)
		mpd->apply_opts();
	if (opt->mask & OPT_CHANGE_DIMENSIONS)
		wm->detect_dimensions();
	if (opt->mask & OPT_CHANGE_PLAYMODE)
	{
		mpd->update_playstring();
		wm->statusbar->draw();
	}

	if (opt->mask & OPT_CHANGE_REDRAW)
	{
		wm->draw();
	}
	else
	{
		if (opt->mask & OPT_CHANGE_DRAWLIST)
			wm->active->draw();
		if (opt->mask & OPT_CHANGE_TOPBAR)
			wm->topbar->draw();

		if (!(opt->mask & OPT_CHANGE_DRAWLIST) && !(opt->mask & OPT_CHANGE_TOPBAR))
			return true;
	}

	wm->flush();

	return true;
}

int PMS::quit()
{
	config->quit = true;
	return true;
}

int PMS::scroll_window(int offset)
{
	Wmain * window;
	window = WMAIN(wm->active);
	window->scroll_window(offset);
	return true;
}

int PMS::move_cursor(int offset)
{
	Wmain * window;
	window = WMAIN(wm->active);
	window->move_cursor(offset);
	return true;
}

int PMS::move_cursor_page(int offset)
{
	bool beep;
	Wmain * window;
	window = WMAIN(wm->active);

	beep = config->use_bell;
	config->use_bell = false;
	window->move_cursor(offset * window->height());

	if (offset < 0)
		window->set_position(window->cursor - window->height());
	else
		window->set_position(window->cursor);

	config->use_bell = beep;
	return true;
}

int PMS::set_cursor_top(int offset)
{
	Wmain * window;
	window = WMAIN(wm->active);
	if ((offset = window->position + offset - 1) >= window->position + window->height())
		offset = window->position + window->height() - 1;
	window->set_cursor(offset);
	return true;
}

int PMS::set_cursor_bottom(int offset)
{
	Wmain * window;
	window = WMAIN(wm->active);
	if ((offset = window->position + window->height() - offset + 1) < window->position)
		offset = window->position;
	window->set_cursor(offset);
	return true;
}

int PMS::set_cursor_home(int offset)
{
	Wmain * window;
	window = WMAIN(wm->active);
	window->set_cursor(offset - 1);
	return true;
}

int PMS::set_cursor_end(int offset)
{
	Wmain * window;
	window = WMAIN(wm->active);
	window->set_cursor(window->content_size() - offset);
	return true;
}

int PMS::set_cursor_currentsong()
{
	Wsonglist * window;
	Song * song;
	size_t pos;

	/* Silently ignore if there is no song playing. */
	if ((song = mpd->currentsong) == NULL)
		return false;

	/* Get current window */
	window = WSONGLIST(wm->active);
	if (window == NULL)
	{
		sterr("Current window is not a playlist, so cannot locate any songs here.", NULL);
		return false;
	}

	/* If the song has a position, and we are in the playlist, jump to that spot. */
	if (song->pos != -1 && window->songlist->playlist && song->pos < (int)window->songlist->size())
	{
		pos = song->pos;
	}

	/* Use song hash to look it up. */
	else if ((pos = window->songlist->sfind(song->fhash)) == string::npos)
	{
		sterr("Currently playing song is not in this songlist.", NULL);
		return false;
	}

	window->set_cursor(pos);
	return true;
}

int PMS::set_cursor_random()
{
	Wsonglist * window;

	/* Get current window */
	window = WSONGLIST(wm->active);
	if (window == NULL)
	{
		sterr("Current window is not a playlist, so cannot locate any songs here.", NULL);
		return false;
	}

	if (window->songlist->size() == 0)
		return false;
	
	window->set_cursor(window->songlist->randpos());
	return true;
}

int PMS::cycle_windows(int offset)
{
	wm->cycle(offset);
	return true;
}

int PMS::activate_songlist()
{
	Wsonglist * win;
	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Current window is not a song list, and cannot be set as the primary playback list.", NULL);
		return false;
	}

	return mpd->activate_songlist(win->songlist);
}

int PMS::sortlist(string sortstr)
{
	Wsonglist * win;
	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Current window is not a song list, and cannot be sorted.", NULL);
		return false;
	}

	win->songlist->sort(sortstr);
	win->qdraw();
	return true;
}

int PMS::livesearch(string terms, bool exitsearch)
{
	Wsonglist * win;
	Song * song;
	song_t i;

	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Current window is not a song list, and cannot be searched.", NULL);
		return false;
	}

	song = win->cursorsong();
	win->songlist->search(SEARCH_MODE_LIVE, config->search_field_mask, terms);
	if (exitsearch)
	{
		win->songlist->liveclear();
		input->setmode(INPUT_MODE_COMMAND);
		wm->statusbar->qdraw();
		wm->flush();
	}

	if (song && (i = win->songlist->sfind(song->fhash)) != string::npos)
		win->set_cursor(i);
	else if (win->cursor >= win->content_size())
		win->set_cursor(win->content_size() - 1);

	win->qdraw();
	return true;
}

int PMS::resetsearch()
{
	Wsonglist * win;
	Song * song;
	song_t i;

	if ((win = WSONGLIST(wm->active)) == NULL)
		return false;
	
	song = win->cursorsong();
	win->songlist->search(SEARCH_MODE_NONE);

	if (song && (i = win->songlist->sfind(song->fhash)) != string::npos)
		win->set_cursor(i);
	else if (win->cursor >= win->content_size())
		win->set_cursor(win->content_size() - 1);

	win->qdraw();
	return true;
}

int PMS::add(int count)
{
	bool status = true;
	size_t i;
	int added = 0;
	Wsonglist * win;
	selection_t sel;
	vector<Song *>::iterator it;

	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Current window is not a playlist. Cannot add any songs from here.", NULL);
		return false;
	}

	sel = win->get_selection(count);
	if (sel->empty())
		return false;

	for (it = sel->begin(); it != sel->end(); ++it)
		if (mpd->addid((*it)->f[FIELD_FILE]))
			++added;

	if (added > 0)
	{
		if (added > 1)
			stinfo("%d songs added to playlist.", added);
		else
			stinfo("`%s' added to playlist.", (*--it)->f[FIELD_TITLE].c_str());

		if (config->advance_cursor)
			win->move_cursor(count);
	}
	else
	{
		stinfo("Failed to add some songs to playlist.", NULL);
	}

	win->songlist->clear_visual();

	return status;
}

int PMS::add(string uri, int count)
{
	bool status = true;

	while (count-- > 0)
		if (mpd->addid(uri) != -1)
			stinfo("`%s' added to playlist.", uri.c_str());
		else
			status = false;
	
	return status;
}

int PMS::add_same(string fields, int count)
{
	vector<Field *> fieldlist;
	vector<Field *>::const_iterator it;
	size_t i, j;
	int added = 0;
	Wsonglist * win;
	Song * source;
	Song * song;
	Song * lastsong;

	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Current window is not a song list, so cannot add songs from here.", NULL);
		return false;
	}

	if ((source = win->cursorsong()) == NULL)
		return false;

	/* Always use library to locate songs */
	if (config->add_same_exhaustive)
		i = 0;
	else if (win->songlist == &mpd->library)
		i = win->cursor;
	else
	{
		if ((i = mpd->library.find(source->fhash)) == string::npos)
		{
			sterr("This song is not in the library.", NULL);
			return false;
		}
	}

	config->get_fields(fields, fieldlist);
	if (fieldlist.empty())
	{
		sterr("Missing parameters. What do you want to add more of?", NULL);
		return false;
	}

	/* Find the hash of the last song in the playlist */
	if (mpd->playlist.size() > 0)
		lastsong = mpd->playlist.at(mpd->playlist.size() - 1);

	/* Iterate backwards and find the first song that doesn't match. */
	if (!config->add_same_exhaustive)
	{
		while (i > 0)
		{
			--i;
			if ((song = mpd->library.at(i)) == NULL)
				return false;

			for (it = fieldlist.begin(); it != fieldlist.end(); ++it)
			{
				if (song->f[(*it)->type] != source->f[(*it)->type])
				{
					song = NULL;
					break;
				}
			}

			if (song == NULL)
			{
				++i;
				break;
			}
		}

		/* Check if the last song in the playlist is part of the set */
		for (it = fieldlist.begin(); it != fieldlist.end(); ++it)
		{
			if (lastsong->f[(*it)->type] != source->f[(*it)->type])
				break;
		}

		/* Yes, it is: start from next song. */
		if (it == fieldlist.end())
		{
			j = mpd->library.find(lastsong->fhash) + 1;

			/* But is the next song part of the set? If not, start from the beginning. */
			for (it = fieldlist.begin(); it != fieldlist.end(); ++it)
			{
				if ((song = mpd->library.at(j)) == NULL)
				{
					it = fieldlist.end();
					break;
				}

				if (song->f[(*it)->type] == source->f[(*it)->type])
					break;
			}
			if (it != fieldlist.end())
				i = j;
		}
	}

	/* Iterate through list and add all songs that match. */
	while (i < mpd->library.size())
	{
		if ((song = mpd->library.at(i)) == NULL)
			return false;

		for (it = fieldlist.begin(); it != fieldlist.end(); ++it)
		{
			if (song->f[(*it)->type] != source->f[(*it)->type])
			{
				/* Exit loop if not doing exhaustive search */
				if (!config->add_same_exhaustive)
					i = mpd->library.size();
				break;
			}

			if (mpd->addid(song->f[FIELD_FILE]))
				++added;

			break;
		}
		++i;
	}

	if (added == 0)
		sterr("All songs have already been added.", NULL);
	else
		stinfo("%d songs added to playlist.", added);

	return true;
}

int PMS::remove(int count)
{
	Wsonglist * win;
	selection_t sel;
	size_t start;

	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Current window is not a song list. Cannot remove any songs from here.", NULL);
		return false;
	}
	if (win->songlist->readonly)
	{
		sterr("This song list is read-only.", NULL);
		return false;
	}

	sel = win->get_selection(count);

	if (sel->empty())
		return false;

	if ((count = mpd->remove(win->songlist, sel)) > 0)
	{
		win->songlist->visual_pos(&start, NULL);
		if (start == -1)
			start = win->cursor;
		if (start >= win->songlist->size() - sel->size())
			--start;
		win->set_cursor(start);
		clipboard.set(sel);
		win->songlist->clear_visual();
		return true;
	}

	return false;
}

int PMS::visual()
{
	Wsonglist * win;
	int pos;

	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Visual mode can only be used in songlists.", NULL);
		return false;
	}

	if (win->songlist->visual_start == -1)
		pos = win->cursor;
	else
		pos = -1;

	win->songlist->visual_start = pos;
	win->songlist->visual_stop = pos;
	win->qdraw();

	return true;
}

int PMS::yank(int count)
{
	Wsonglist * win;

	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Only songs can be yanked, and this is not a song list.", NULL);
		return false;
	}

	clipboard.set(win->get_selection(count));
	win->songlist->clear_visual();
	win->qdraw();
	stinfo("%d songs yanked to clipboard.", clipboard.size());

	if (config->advance_cursor)
		win->move_cursor(count);

	return true;
}

int PMS::put(int count)
{
	Wsonglist * win;

	if ((win = WSONGLIST(wm->active)) == NULL)
	{
		sterr("Can only put songs into a song list.", NULL);
		return false;
	}

	if (clipboard.empty())
	{
		sterr("Clipboard is empty.", NULL);
		return false;
	}

	if (win->songlist->readonly)
	{
		sterr("Cannot put songs into a read-only song list.", NULL);
		return false;
	}

	if (mpd->put(win->songlist, win->cursor + 1, &clipboard.songs))
	{
		stinfo("Put %d songs into song list.", clipboard.size());
		return true;
	}

	return false;
}

int PMS::update(string dir)
{
	if (dir.empty())
		dir = "/";

	if (mpd->update(dir))
	{
		if (dir == "/")
			stinfo("Updating music library...", NULL);
		else
			stinfo("Updating %s...", dir.c_str());
		return true;
	}
	return false;
}

int PMS::set_crossfade(string crossfade)
{
	return mpd->set_crossfade(atoi(crossfade.c_str()));
}

int PMS::set_password(string password)
{
	int i;
	if ((i = mpd->set_password(password)) == true)
		mpd->apply_opts(); /* any option desynch should be fixed here. */
	return i;
}

int PMS::toggle_play()
{
	return mpd->pause(mpd->status.state == MPD_STATE_PLAY ? true : false);
}

int PMS::play()
{
	Song * s;
	if ((s = cursorsong()) == NULL)
		return false;

	if (s->id != -1)
		return (mpd->playid(s->id) == MPD_GETLINE_OK);
	else
		return (mpd->playid(mpd->addid(s->f[FIELD_FILE])) == MPD_GETLINE_OK);
}

int PMS::stop()
{
	return mpd->stop();
}

int PMS::change_song(int steps)
{
	Song * song;

	if (mpd->status.single && mpd->status.repeat && mpd->currentsong)
		return mpd->playid(mpd->currentsong->id);

	if ((song = mpd->next_song_in_line(steps)) == NULL)
	{
		sterr("There is no candidate for %s song, try switching lists or setting repeat mode.", string(steps > 0 ? "next" : "previous").c_str());
		return false;
	}

	if (song->id != -1)
		return mpd->playid(song->id);
	else
		return mpd->playid(mpd->addid(song->f[FIELD_FILE]));
}

int PMS::seek(int seconds)
{
	return mpd->seek(seconds);
}



Song * cursorsong()
{
	Wsonglist * win;
	win = WSONGLIST(wm->active);
	if (win == NULL)
		return NULL;
	return win->cursorsong();
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pms

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pms

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master