xaizek / pms (License: GPLv3+) (since 2018-12-07)
Older version of Practical Music Search written in C++.
<root> / src / action.cpp (8ce082403e07d2c9def9827d4f5ba4e28bba89b9) (47KiB) (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 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
/* vi:set ts=8 sts=8 sw=8 noet:
 *
 * PMS	<<Practical Music Search>>
 * Copyright (C) 2006-2015  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/>.
 *
 *
 * action.cpp
 *	Executes key-bound actions
 */

#include "action.h"
#include "command.h"
#include "display.h"
#include "config.h"
#include "input.h"
#include "i18n.h"
#include "pms.h"

extern Pms *			pms;



Interface::Interface()
{
	msg = new Message();
}

Interface::~Interface()
{
	delete msg;
}


/*
 * Handle any events.
 * This is a frontend to all the other functions in this class.
 */
bool		Interface::check_events()
{
	Songlist *		list;
	pms_window *		win;

	msg->clear();
	action = pms->input->getpending();

	win = pms->disp->actwin();
	if (win != NULL)
	{
		list = win->plist();
	}

	switch(action)
	{
		default:
		case PEND_NONE:
			return false;

		/*
		 * PMS specific stuff
		 */

		case PEND_EXEC:
			exec(param);
			break;

		case PEND_VERSION:
			version();
			break;

		case PEND_CLEAR_TOPBAR:
			clear_topbar(atoi(param.c_str()));
			break;

		case PEND_REDRAW:
			redraw();
			break;

		case PEND_REHASH:
			rehash();
			break;

		case PEND_WRITE_CONFIG:
			write_config(param);
			break;

		case PEND_SOURCE:
			source(param);
			break;

		case PEND_QUIT:
			quit();
			break;
		
		case PEND_SHELL:
			shell(param);
			break;

		case PEND_SHOW_INFO:
			show_info();
			break;

		/*
		 * MPD admin
		 */
		case PEND_PASSWORD:
			password(param);
			break;

		case PEND_UPDATE_DB:
			update_db(param);
			break;

		/*
		 * Normal player actions
		 */
		case PEND_ADD:
		case PEND_ADDTO:
			add(param);
			break;

		case PEND_PLAY:
			play();
			break;

		case PEND_PAUSE:
			pause(false);
			break;

		case PEND_TOGGLEPLAY:
			pause(true);
			break;

		case PEND_STOP:
			stop();
			break;

		case PEND_NEXT:
			next(false);
			break;

		case PEND_REALLY_NEXT:
			next(true);
			break;

		case PEND_PREV:
			prev();
			break;

		case PEND_VOLUME:
			setvolume(param);
			break;

		case PEND_MUTE:
			mute();
			break;

		case PEND_CROSSFADE:
			crossfade(atoi(param.c_str()));
			break;

		case PEND_SEEK:
			seek(atoi(param.c_str()));
			break;

		case PEND_SHUFFLE:
			shuffle();
			break;

		case PEND_CLEAR:
			clear();
			break;

		case PEND_CROP:
			crop(CROP_PLAYING);
			break;

		case PEND_CROPSELECTION:
			crop(CROP_SELECTION);
			break;

		case PEND_DELETE:
			remove(list);
			break;

		case PEND_TOGGLESELECT:
			select(win, SELECT_TOGGLE, param);
			break;

		case PEND_SELECT:
			select(win, SELECT_ON, param);
			break;

		case PEND_UNSELECT:
			select(win, SELECT_OFF, param);
			break;

		case PEND_CLEARSELECTION:
			select(win, SELECT_CLEAR, param);
			break;

		case PEND_SELECTALL:
			select(win, SELECT_ALL, param);
			break;



		case PEND_FILTERMODE:
			set_input_mode(INPUT_FILTER);
			break;

		case PEND_COMMANDMODE:
			set_input_mode(INPUT_COMMAND);
			break;

		case PEND_CLEARFILTERS:
			clear_filters();
			break;

	}

	if (msg->code != 0 && msg->str.size() > 0)
	{
		pms->putlog(msg);
		msg->clear();
	}

	return true;
}

/*
 * Execute an input string from the command line
 */
long		Interface::exec(string s)
{
	if (pms->input->run(s, *msg))
	{
		pms->drawstatus();
		handle_command(pms->input->getpending()); //FIXME

		return STOK;
	}
	else if (pms->input->text.substr(0, 1) == "!")
	{
		return shell(pms->input->text.substr(1));
	}
	else
	{
		if (pms->config->readline(s))
		{
			//pms->resetstatus(-1);
		}
		else
		{
			if (pms->msg->code == CERR_NONE)
				pms->log(MSG_STATUS, STOK, "  %s", pms->msg->str.c_str());
			else
				pms->log(MSG_STATUS, STERR, _("Error %d: %s"), pms->msg->code, pms->msg->str.c_str());
		}

		return pms->msg->code;
	}
}

/*
 * Print program name and version.
 */
long		Interface::version()
{
	pms->log(MSG_STATUS, STOK, "%s %s", PMS_NAME, PACKAGE_VERSION);
	return STOK;
}

/*
 * Clear out a line from the topbar.
 * If line is 0, clear everything.
 */
long		Interface::clear_topbar(int line = 0)
{
	if (pms->options->topbar.size() == 0)
		return STOK;

	if (line == 0)
	{
		pms->options->topbar.clear();
		pms->log(MSG_STATUS, STOK, _("Cleared out the entire topbar."));
		return STOK;
	}

	if (line < 1 || line > pms->options->topbar.size())
	{
		pms->log(MSG_STATUS, STERR, _("Out of range, acceptable range is 1-%d."), pms->options->topbar.size());
		return STERR;
	}
	pms->options->topbar.erase(pms->options->topbar.begin() + line - 1);
	pms->log(MSG_STATUS, STOK, _("Removed line %d from topbar."), line);

	pms->mediator->add("redraw.topbar");

	return STOK;
}

/*
 * Redraw everything
 */
long		Interface::redraw()
{
	pms->mediator->add("redraw");
	return STOK;
}

/*
 * Re-read the configuration file
 */
long		Interface::rehash()
{
	pms->options->reset();
	msg->code = pms->config->loadconfigs();

	if (msg->code == 0)
		pms->log(MSG_STATUS, STOK, _("Reloaded configuration files."));

	return msg->code;
}

/*
 * Save the current configuration to a file
 */
long		Interface::write_config(string file)
{
	if (file.size() == 0)
		file = pms->options->get_string("configfile");

	return STERR;
	//FIXME: implement this
}

/*
 * Read a configuration file or script
 */
long		Interface::source(string file)
{
	if (pms->config->source(file))
	{
		pms->log(MSG_STATUS, STOK, _("Read configuration file: %s"), file.c_str());
		return STOK;
	}
	else
	{
		pms->log(MSG_STATUS, STERR, _("Error reading %s: %s"), file.c_str(), pms->msg->str.c_str());
		return STERR;
	}
}

/*
 * Quit PMS.
 */
long		Interface::quit()
{
	pms->shutdown();
	return STOK;
}

/*
 * Run a shell command
 */
long		Interface::shell(string command)
{
	pms->run_shell(command);
	pms->drawstatus();
	return pms->msg->code;
}

/*
 * Put song info into the console
 */
long		Interface::show_info()
{
	Song *		song;

	song = pms->disp->cursorsong();
	if (song == NULL)
	{
		pms->log(MSG_STATUS, STERR, _("No info could be retrieved."));
		return STERR;
	}

	pms->log(MSG_STATUS, STOK, "%s%s", pms->options->get_string("libraryroot").c_str(), song->file.c_str());
	pms->log(MSG_CONSOLE, STOK, _("--- song info ---\n"));
	pms->log(MSG_CONSOLE, STOK, "id\t\t = %d\n", song->id);
	pms->log(MSG_CONSOLE, STOK, "pos\t\t = %d\n", song->pos);
	pms->log(MSG_CONSOLE, STOK, "file\t\t = %s%s\n", pms->options->get_string("libraryroot").c_str(), song->file.c_str());
	pms->log(MSG_CONSOLE, STOK, "artist\t\t = %s\n", song->artist.c_str());
	pms->log(MSG_CONSOLE, STOK, "albumartist\t = %s\n", song->albumartist.c_str());
	pms->log(MSG_CONSOLE, STOK, "albumartistsort\t = %s\n", song->albumartistsort.c_str());
	pms->log(MSG_CONSOLE, STOK, "date\t\t = %s\n", song->date.c_str());
	pms->log(MSG_CONSOLE, STOK, "year\t\t = %s\n", song->year.c_str());
	pms->log(MSG_CONSOLE, STOK, "artistsort\t = %s\n", song->artistsort.c_str());
	pms->log(MSG_CONSOLE, STOK, "title\t\t = %s\n", song->title.c_str());
	pms->log(MSG_CONSOLE, STOK, "album\t\t = %s\n", song->album.c_str());
	pms->log(MSG_CONSOLE, STOK, "track\t\t = %s\n", song->track.c_str());
	pms->log(MSG_CONSOLE, STOK, "disc\t\t = %s\n", song->disc.c_str());
	pms->log(MSG_CONSOLE, STOK, _("--- end of info ---\n"));

	return STOK;
}

/*
 * Clear the filter list
 */

void		Interface::clear_filters()
{
	Songlist *	list;

	list = pms->disp->actwin()->plist();
	if (!list) return;

	set_input_mode(INPUT_NORMAL);
	list->filter_clear();
	pms->mediator->add("redraw");
}


/*
 * Change input mode
 */
int		Interface::set_input_mode(Input_mode mode)
{
	Songlist *	list;

	if (mode == INPUT_JUMP || mode == INPUT_FILTER)
	{
		if (!pms->disp->actwin() || pms->disp->actwin()->type() != WIN_ROLE_PLAYLIST)
		{
			pms->log(MSG_STATUS, STERR, _("Can't search within this window."));
			return pms->input->mode();
		}
	}
	pms->input->mode(mode);
	pms->drawstatus();
	return pms->input->mode();
}


/*
 * Send a password to MPD
 */
long		Interface::password(string pass)
{
	if (pass.size() == 0)
	{
		pms->log(MSG_STATUS, STERR, _("You have to specify a password."));
		return STERR;
	}
	if (pms->comm->sendpassword(pass))
	{
		pms->log(MSG_STATUS, STOK, _("Password accepted by mpd."));
		pms->options->set_string("password", pms->input->param);
		return STOK;
	}
	else
	{
		generr();
		return STERR;
	}
}

/*
 * Update mpd's library.
 * If location is empty, update everything.
 * location 'this' means the selected (cursor) song.
 * location 'current' means the currently playing song.
 * location 'thisdir' means the selected (cursor) song's directory.
 * location 'currentdir' means the currently playing song's directory.
 */
long		Interface::update_db(string location)
{
	string		libroot;

	libroot = pms->options->get_string("libraryroot");

	if (location.size() > 0)
	{
		if (location == "this" && pms->disp && pms->disp->cursorsong())
		{
			location = pms->disp->cursorsong()->file;
			pms->log(MSG_DEBUG, STOK, "Encountered location 'this', translating to %s\n", location.c_str());
		}
		else if (location == "current" && pms->cursong())
		{
			location = pms->cursong()->file;
			pms->log(MSG_DEBUG, STOK, "Encountered location 'current', translating to %s\n", location.c_str());
		}
		else if (location == "thisdir" && pms->disp && pms->disp->cursorsong())
		{
			location = pms->disp->cursorsong()->dirname();
			pms->log(MSG_DEBUG, STOK, "Encountered location 'thisdir', translating to %s\n", location.c_str());
		}
		else if (location == "currentdir" && pms->cursong())
		{
			location = pms->cursong()->dirname();
			pms->log(MSG_DEBUG, STOK, "Encountered location 'currentdir', translating to %s\n", location.c_str());
		}
		else if (libroot.size() > 0)
		{
			if (location.substr(0, libroot.size()) == libroot && location.size() > libroot.size())
			{
				location = location.substr(libroot.size());
				pms->log(MSG_DEBUG, STOK, "Encountered library root in update parameter, stripping to %s\n", location.c_str());
			}
		}
	}
	else
	{
		location = "/";
	}

	if (pms->comm->rescandb(location))
	{
		if (location == "/")
			pms->log(MSG_STATUS, STOK, _("Scanning entire library for changes..."));
		else
			pms->log(MSG_STATUS, STOK, _("Scanning '%s' for changes..."), location.c_str());

		return STOK;
	}
	else if (pms->comm->status()->db_updating)
	{
		pms->log(MSG_STATUS, STERR, _("A library update is already in progress. Please wait for it to finish first."));
	}
	else
	{
		generr();
	}

	return STERR;
}


/*
 *************************** NORMAL PLAYER ACTIONS **************************
 */


/*
 * Play song under cursor
 */
long		Interface::play()
{
	Songlist *	list;
	Song *		song;
	song_t		s;

	list = pms->disp->actwin()->plist();
	assert(list != NULL);

	song = list->cursorsong();
	assert(song != NULL);

	pms->log(MSG_CONSOLE, STOK, "Playing %s\n", song->file.c_str());

	s = song->id;

	if (song->id == MPD_SONG_NO_ID)
	{
		s = pms->comm->add(pms->comm->playlist(), song);
		if (s == MPD_SONG_NO_ID)
		{
			generr();
			return STERR;
		}
	}
	if (pms->comm->playid(s))
	{
		//pms->drawstatus();
		return STOK;
	}
	generr();
	return STOK;
}

/*
 * Add selected song(s) to playlist
 * FIXME: rewrite....
 */
long		Interface::add(string param)
{
	pms_window *	win;
	Songlist *	list;
	Songlist *	dlist;
	Song *		song;
	string		s;
	size_t		i = 0;

	win = pms->disp->actwin();
	list = win->plist();
	dlist = pms->comm->playlist();

	if (!pms->disp->cursorsong() && !win->current())
	{
		pms->log(MSG_STATUS, STERR, _("This is not a song, so you can't add it."));
		return STERR;
	}

	/* Add list to list */
	if (win && win->type() == WIN_ROLE_WINDOWLIST && win->current() && pms->input->win == NULL && action == PEND_ADD)
	{
		pms->log(MSG_DEBUG, 0, "Adding list to list.\n");
		list = win->current()->plist();
		pms->comm->add(list, pms->comm->playlist());
		pms->log(MSG_STATUS, STOK, _("%d songs from %s appended to playlist."), list->size(), list->filename.c_str());
		setwin(pms->disp->findwlist(pms->comm->playlist()));
		return STOK;
	}

	/* Addto spawns windowlist */
	if (action == PEND_ADDTO)
	{
		/* Need additional window param */
		if (pms->input->win == NULL)
		{
			/* Add list from windowlist not supported - TODO */
			if (win->type() == WIN_ROLE_WINDOWLIST)
			{
				pms->log(MSG_STATUS, STERR, _("Not supported. Please select the songs you want to add before using the add-to command."));
				return STERR;
			}
			pms->log(MSG_DEBUG, 0, "Storing window parameters: win=%p\n", win);
			pms->input->winstore(win);
			setwin(pms->disp->create_windowlist());
			return STOK;
		}
		if (win->current())
			dlist = win->current()->plist();

		pms->log(MSG_DEBUG, 0, "Returned window parameters: win=%p, destination list=%p\n", win->current(), dlist);

		if (dlist == NULL)
		{
			pms->input->winclear();
			return STOK;
		}

		if (pms->input->win)
			list = pms->input->win->plist();
		else
			list = NULL;
	}

	if (!list || !dlist) return STERR;

	if (dlist == pms->comm->playlist())
		s = _("playlist");
	else if (dlist == pms->comm->library())
		s = _("library");
	else
		s = dlist->filename;

	/* Add arbitrary file or stream */
	if (param.size() > 0)
	{
		song = new Song(param);
		if (pms->comm->add(dlist, song) != MPD_SONG_NO_ID)
			pms->log(MSG_STATUS, STOK, _("Added '%s' to %s."), param.c_str(), s.c_str());
		else
			generr();

		delete song;
		return STOK;
	}

	/* Add selected song(s) */
	song = list->popnextselected();
	while (song != NULL)
	{
		pms->log(MSG_DEBUG, 0, "Adding song at %p with id=%d pos=%d filename=%s\n", song, song->id, song->pos, song->file.c_str());
		if (pms->comm->add(dlist, song) != MPD_SONG_NO_ID)
			++i;
		else
			generr();

		song = list->popnextselected();
	}
	if (i == 0)
	{
		generr();
	}
	else
	{
		if (i == 1 && pms->options->get_bool("nextafteraction"))
			pms->disp->movecursor(1);
		pms->log(MSG_STATUS, STOK, _("Added %d %s to %s."), i, (i == 1 ? "song" : "songs"), s.c_str());
	}

	win->wantdraw = true;
	return STOK;
}

/*
 * Skip to the next song in line.
 */
long
Interface::next(bool ignore_playmode = false)
{
	if (playnext(true) == MPD_SONG_NO_ID) {
		pms->log(MSG_STATUS, STERR, _("You have reached the end of the list."));
		return STERR;
	}
	//pms->drawstatus();
	return STOK;
}

/*
 * Skip to the previous song in playlist.
 * If repeat mode is set, wrap around to the last song.
 */
long
Interface::prev()
{
	Song *		cs;
	song_t		i;

	cs = pms->cursong();
	if (cs == NULL)
	{
		if (pms->comm->playlist()->size() == 0) {
			pms->log(MSG_STATUS, STERR, _("Can't skip backwards because the playlist is empty."));
			return STERR;
		}
		i = pms->comm->activelist()->size();
	}
	else
	{
		if (cs->pos <= 0)
		{
			if (pms->comm->status()->repeat) {
				i = pms->comm->playlist()->size();
			}
			else
			{
				pms->log(MSG_STATUS, STERR, _("No previous song."));
				return STERR;
			}
		}
		else
		{
			i = cs->pos;
		}
	}

	--i;
	if (i < 0 || i >= pms->comm->playlist()->size())
	{
		pms->log(MSG_CONSOLE, STERR, _("Previous song: out of range.\n"));
		return STERR;
	}

	cs = pms->comm->playlist()->song(i);
	pms->comm->playid(cs->id);
	//pms->drawstatus();

	return STOK;
}

/*
 * Pause playback.
 * If tryplay is true, toggle playback instead.
 */
bool
Interface::pause(bool tryplay = false)
{
	return pms->comm->pause(tryplay);
}

/*
 * Stop playback.
 */
long		Interface::stop()
{
	if (pms->comm->stop())
	{
		//pms->drawstatus();
		return STOK;
	}
	generr();
	return STERR;
}

/*
 * Set or adjust volume.
 * A pure integer value means set volume to this value.
 * +/- before the integer means adjust volume by this percentage.
 */
long		Interface::setvolume(string vol)
{
	bool		ok;

	if (vol.size() == 0)
	{
		pms->log(MSG_STATUS, STOK, _("Volume: %d%%%%"), pms->comm->status()->volume);
		return STOK;
	}
	if (vol[0] != '+' && vol[0] != '-')
	{
		ok = pms->comm->setvolume(atoi(vol.c_str()));
	}
	else
	{
		if (vol.size() == 1)
		{
			pms->log(MSG_STATUS, STERR, _("Unexpected end of line, expected integer value."));
			return STERR;
		}
		ok = pms->comm->volume(atoi(vol.c_str()));
	}
	if (ok)
	{
		pms->log(MSG_STATUS, STOK, _("Volume: %d%%%%"), pms->comm->status()->volume);
		return STOK;
	}
	else
	{
		generr();
		return STERR;
	}
}

/*
 * Toggle muted status
 */
long		Interface::mute()
{
	if (!pms->comm->mute())
	{
		generr();
		return STERR;
	}

	if (pms->comm->muted())
		pms->log(MSG_STATUS, STOK, "Mute is on, from %d%%%%", pms->comm->mvolume());
	else
		pms->log(MSG_STATUS, STOK, "Mute is off, volume=%d%%%%", pms->comm->status()->volume);

	return STOK;
}

/*
 * Set crossfade time
 */
long		Interface::crossfade(int seconds)
{
	if (seconds == 0)
	{
		seconds = pms->comm->crossfade();
		if (seconds == 0)
			pms->log(MSG_STATUS, STOK, "Crossfade switched off."); 
		else if (seconds > 0)
			pms->log(MSG_STATUS, STOK, "Crossfade switched on and is set to %d seconds.", seconds);
	}
	else
	{
		seconds = pms->comm->crossfade(seconds);
		if (seconds >= 0)
			pms->log(MSG_STATUS, STOK, "Crossfade set to %d seconds.", seconds);
	}
	if (seconds == -1)
	{
		generr();
		return STERR;
	}
	return STOK;
}

/*
 * Seek in the current stream
 */
long		Interface::seek(int seconds)
{
	if (!pms->cursong() || pms->comm->status()->state < MPD_STATE_PLAY)
	{
		pms->log(MSG_STATUS, STERR, _("Can't seek when player is stopped."));
		return STERR;
	}

	if (seconds == 0)
		return STERR;

	/* Overflow handling */
	if (pms->comm->status()->time_elapsed + seconds >= pms->comm->status()->time_total)
		/* Skip forwards */
		playnext(true);
	else if (pms->comm->status()->time_elapsed + seconds < 0)
	{
		/* Skip backwards */
		if (prev() == STOK)
		{
			if (!pms->comm->get_status()) {
				return STERR;
			}
			if (pms->comm->seek(pms->cursong()->time + seconds)) {
				return STOK;
			}
		}
		else
		{
			stop();
		}
	}
	else
	{
		/* Normal seek */
		if (pms->comm->seek(seconds))
			return STOK;
	}
	generr();
	return STERR;
}

/*
 * Shuffle the playlist (re-order tracks)
 */
long		Interface::shuffle()
{
	if (pms->comm->shuffle())
	{
		pms->log(MSG_STATUS, STOK, _("Playlist shuffled."));
		return STOK;
	}
	generr();
	return STERR;
}

/*
 * Clear out the playlist
 */
long		Interface::clear()
{
	if (pms->comm->clear(pms->disp->actwin()->plist()))
	{
		pms->log(MSG_STATUS, STOK, _("Playlist shuffled."));
		return STOK;
	}
	generr();
	return STERR;
}

/*
 * Crop the selected list
 */
long		Interface::crop(int crop_mode)
{
	if (pms->comm->crop(pms->disp->actwin()->plist(), crop_mode))
	{
		pms->log(MSG_CONSOLE, STOK, _("Playlist cropped.\n"));
		//pms->drawstatus();
		pms->disp->actwin()->wantdraw = true;
		return STOK;
	}
	generr();
	return STERR;
}

/*
 * Remove selected songs from list
 */
long
Interface::remove(Songlist * list)
{
	Song *					song;
	vector<Song *>				songs;
	vector<Song *>::reverse_iterator	i;

	/* FIXME: this check should, perhaps, be done earlier? */
	if (!list) {
		pms->log(MSG_STATUS, STERR, _("This is not a playlist: you can't remove songs from here."));
		return STERR;
	}

	/* FIXME: same goes for this check */
	if (list == pms->comm->library()) {
		pms->log(MSG_STATUS, STERR, _("The library is read-only."));
		return STERR;
	}

	while ((song = list->popnextselected()) != NULL) {
		songs.push_back(song);
	}

	i = songs.rbegin();
	while (i != songs.rend()) {
		if (!pms->comm->remove(list, *i)) {
			return STERR;
		}
		++i;
	}

	/* FIXME: should wantdraw really be set _here_? */
	pms->disp->actwin()->wantdraw = true;
	pms->log(MSG_STATUS, STOK, _("Removed %d %s."), songs.size(), (songs.size() == 1 ? _("song") : _("songs")));

	return STOK;
}

/*
 * Perform select, unselect or toggle select on one or more entries
 */
long		Interface::select(pms_window * win, int mode, string param)
{
	Songlist *	list;
	Song *		song;
	int		i = -1;

	list = win->plist();

	if (!list)
	{
		pms->log(MSG_STATUS, STERR, _("Can't select: this is not a playlist."));
		return STERR;
	}

	switch(mode)
	{
		case SELECT_CLEAR:
			for(i = 0; i <= list->end(); i++)
				list->selectsong(list->song(i), 0);
			pms->log(MSG_DEBUG, STOK, _("Cleared selection.\n"));
			win->wantdraw = true;
			return STOK;

		case SELECT_ALL:
			for(i = 0; i <= list->end(); i++)
				list->selectsong(list->song(i), 1);
			pms->log(MSG_DEBUG, STOK, _("Selected all songs.\n"));
			win->wantdraw = true;
			return STOK;

		default:
			break;
	}

	/* Perform only on one object */
	if (param.size() == 0)
	{
		song = list->cursorsong();
		if (!song)
		{
			pms->log(MSG_STATUS, STERR, _("Can't select: no song under cursor."));
			return STERR;
		}
		if (mode == SELECT_TOGGLE)
			list->selectsong(song, !song->selected);
		else if (mode == SELECT_OFF)
			list->selectsong(song, false);
		else if (mode == SELECT_ON)
			list->selectsong(song, true);

		if (pms->options->get_bool("nextafteraction"))
			win->movecursor(1);

		win->wantdraw = true;
		return STOK;
	}

	/* Perform on range of objects */
	i = list->match(param, 0, list->end(), MATCH_ALL);
	if (i == MATCH_FAILED)
	{
		pms->log(MSG_STATUS, STERR, _("No songs matching pattern %s"), param.c_str());
		return STERR;
	}
	while (i != MATCH_FAILED)
	{
		song = list->song(i);
		if (!song)
		{
			pms->log(MSG_DEBUG, STERR, "***BUG*** in Interface::select(): Encountered NULL song!\n");
			continue;
		}

		if (mode == SELECT_TOGGLE)
			list->selectsong(song, !song->selected);
		else if (mode == SELECT_OFF)
			list->selectsong(song, false);
		else if (mode == SELECT_ON)
			list->selectsong(song, true);

		if (static_cast<unsigned int>(i) == list->end())
			break;

		i = list->match(param, ++i, list->end(), MATCH_ALL);
	}

	win->wantdraw = true;
	return STOK;
}





/*
 * Executes actions. This is the last bit of the user interface.
 */
bool
handle_command(pms_pending_keys action)
{
	Message		err;
	Song *		song = NULL;
	Songlist *	list = NULL;
	Songlist *	dlist = NULL;
	pms_window *	win = pms->disp->actwin();
	pms_window *	tmpwin;
	Input_mode	mode;
	int		i = 0;
	long		l = 0;
	song_t		sn = 0;
	string		s;

	/* FIXME */
	{
		pms->interface->action = action;
		pms->interface->param = pms->input->param;

		if (pms->interface->check_events())
			return true;
	}

	if (win) list = win->plist();

	switch(action)
	{
		case PEND_NONE:
			return false;


		case PEND_MOVE_DOWN:
		case PEND_MOVE_UP:
			if (pms->input->mode() == INPUT_COMMAND || pms->input->mode() == INPUT_JUMP)
			{
				pms->input->goprev();
				//pms->drawstatus();
				break;
			}
			pms->disp->movecursor(action == PEND_MOVE_DOWN ? 1 : -1);
			if (win) win->wantdraw = true;
			break;

		case PEND_MOVE_HALFPGDN:
		case PEND_MOVE_HALFPGUP:
			if (!win) break;
			//vim seems to integer divide number of rows visible by 
			//2 unless only one row is visible
			i = (win->bheight() - 1) / 2;
			if (i < 1)
				i = 1;
			pms->disp->scrollwin(i * (action == PEND_MOVE_HALFPGDN ? 1 : -1));
			if (win) win->wantdraw = true;
			break;

		case PEND_MOVE_PGDN:
		case PEND_MOVE_PGUP:
			if (!win) break;
			if (win->bheight() - 1 > 4)
			{
				//more than four lines visible: vim leaves two 
				//previously visible lines visible
				i = win->bheight() - 1 - 2;
			}
			else if (win->bheight() - 1 == 4)
			{
				//four lines visible: vim leaves one previously 
				//visible line visible
				i = 3;
			}
			else
			{
				//three or fewer lines visible: vim moves a full 
				//page
				i = win->bheight() - 1;
			}
			pms->disp->scrollwin(i * (action == PEND_MOVE_PGDN ? 1 : -1));
			if (win) win->wantdraw = true;
			break;

		case PEND_SCROLL_DOWN:
		case PEND_SCROLL_UP:
			if (!win) break;
			pms->disp->scrollwin(action == PEND_SCROLL_DOWN ? 1 : -1);
			if (win) win->wantdraw = true;
			break;

		case PEND_CENTER_CURSOR:
			if (!win) break;
			if (pms->options->get_long("scroll_mode") != SCROLL_NORMAL) break;
			pms->disp->scrollwin(win->scursor() - win->cursordrawstart() - (win->bheight() - 1) / 2);
			break;

		case PEND_MOVE_HOME:
			pms->disp->setcursor(0);
			if (win) win->wantdraw = true;
			break;

		case PEND_MOVE_END:
			pms->disp->setcursor(pms->disp->actwin()->size() - 1);
			if (win) win->wantdraw = true;
			break;

		case PEND_GOTO_CURRENT:
			if (!pms->cursong() || !list) return false;
			if (!list->gotocurrent())
			{
				pms->log(MSG_STATUS, STERR, "Currently playing song is not here.");
				return false;
			}

			win->wantdraw = true;
			break;


		case PEND_PLAYALBUM:
			multiplay(MATCH_ALBUM, 0);
			break;

		case PEND_PLAYARTIST:
			multiplay(MATCH_ARTIST, 0);
			break;

		case PEND_ADDALBUM:
			multiplay(MATCH_ALBUM, 1);
			break;

		case PEND_ADDARTIST:
			multiplay(MATCH_ARTIST, 1);
			break;

		case PEND_ADDALL:
			multiplay(MATCH_ALL, 1);
			break;

		case PEND_ADD:
		case PEND_ADDTO:


		case PEND_PLAYRANDOM:
		case PEND_ADDRANDOM:
			if (!list) list = pms->comm->library();

			/* Don't re-add songs from playlist, but rather play them again. */
			if (list->role == LIST_ROLE_MAIN)
			{
				if (action == PEND_PLAYRANDOM)
				{
					song = list->randsong();
					pms->comm->playid(song->id);
					if (win) win->wantdraw = true;
					break;
				}
				/* Don't add songs from playlist, use library instead */
				else
				{
					list = pms->comm->library();
				}
			}

			/* Accept numeric parameter */
			if (pms->input->param.size())
			{
				i = atoi(pms->input->param.c_str());
				sn = MPD_SONG_NO_NUM;
				for (l = 0; l < i; l++)
				{
					song = list->randsong();
					if (sn == MPD_SONG_NO_NUM)
						sn = pms->comm->add(pms->comm->playlist(), song);
					else
						pms->comm->add(pms->comm->playlist(), song);
				}
			}
			else
			{
				song = list->randsong();
				sn = pms->comm->add(pms->comm->playlist(), song);
			}

			if (sn == MPD_SONG_NO_NUM)
				break;
			if (action == PEND_PLAYRANDOM)
				pms->comm->playid(sn);
			if (win)
				win->wantdraw = true;
			//pms->drawstatus();
			break;

		case PEND_GOTORANDOM:
			if (!list)
			{
				pms->log(MSG_STATUS, STERR, _("This command can only be run within a playlist."));
				break;
			}
			song = list->randsong(&sn);
			if (song == NULL) break;
			pms->disp->setcursor(sn);
			break;

		case PEND_MOVEITEMS:
			if (!list || !win)
			{
				pms->log(MSG_STATUS, STERR, _("You can't move anything else than songs."));
				break;
			}
			i = pms->comm->move(list, atoi(pms->input->param.c_str()));
			if (i == 0)
			{
				pms->log(MSG_STATUS, STERR, _("Can't move."));
				break;
			}
			else if (i == 1)
			{
				pms->disp->movecursor(atoi(pms->input->param.c_str()));
			}
			else
			{
				win->wantdraw = true;
			}
			//pms->drawstatus();
			break;

		case PEND_REPEAT:
			pms->comm->repeat(!pms->comm->status()->repeat);
			break;

		case PEND_RANDOM:
			pms->comm->random(!pms->comm->status()->random);
			break;

		case PEND_SINGLE:
			pms->comm->single(!pms->comm->status()->single);
			break;

		case PEND_CONSUME:
			pms->comm->consume(!pms->comm->status()->consume);
			break;

		case PEND_TEXT_UPDATED:
			pms->drawstatus();
			if (pms->input->mode() == INPUT_JUMP)
			{
				if (!win) break;
				i = win->scursor();
				if ((unsigned int)i >= win->size()) i = 0;
				win->jumpto(pms->input->text, i);
			}
			break;

		case PEND_TEXT_RETURN:

			mode = pms->input->mode();
			pms->input->savehistory();

			if (win && win->type() == WIN_ROLE_WINDOWLIST)
				pms->input->mode(INPUT_LIST);
			else
				pms->input->mode(INPUT_NORMAL);

			if (mode == INPUT_COMMAND)
			{
				pms->interface->exec(pms->input->text);
			}
			else if (mode == INPUT_JUMP)
			{
				pms->input->searchterm = pms->input->text;

				if (win->posof_jump(pms->input->text, 0) == -1)
					pms->log(MSG_STATUS, STERR, _("Pattern not found: %s"), pms->input->text.c_str());

				//else do nothing so the search command is left visible
			}
			else if (mode == INPUT_FILTER)
			{
				if (!list) break;
				list->filter_add(pms->input->text, MATCH_ALL);
				pms->mediator->add("redraw");
			}
			else
			{
				pms->drawstatus();
			}

			break;

		/* Special case for list */
		case PEND_RETURN:
			if (win && win->type() == WIN_ROLE_WINDOWLIST)
			{
				win = win->current();

				/* Stored mode - return to original call */
				if (pms->input->winpop())
				{
					handle_command(pms->input->getpending());
					if (pms->options->get_bool("addtoreturns"))
					{
						setwin(pms->input->win);
						pms->disp->lastwin = win;
					}
					else
					{
						setwin(win);
						pms->disp->lastwin = pms->input->win;
					}
					pms->input->winclear();
				}
				else
				{
					/* Windowlist mode, switch to new window */
					if (!setwin(win))
						pms->log(MSG_STATUS, STERR, _("Can't change window."));
				}
			}
			break;

		case PEND_TEXT_ESCAPE:
		case PEND_RETURN_ESCAPE:
			if (!win) break;
			switch(win->type())
			{
				case WIN_ROLE_BINDLIST:
					pms->input->mode(INPUT_NORMAL);
					setwin(pms->disp->lastwin);
					break;
				case WIN_ROLE_WINDOWLIST:
					pms->input->winclear();
					setwin(win->lastwin());
					break;
				default:
					pms->input->mode(INPUT_NORMAL);
					pms->drawstatus();
					break;
			}
			
			break;

		/* Searching */
		case PEND_JUMPNEXT:
			if (!win || win->type() != WIN_ROLE_PLAYLIST)
			{
				pms->log(MSG_STATUS, STERR, _("Can't search within this window."));
				break;
			}
			i = win->plist()->cursor() + 1;
			if ((unsigned int)i > win->plist()->end()) i = 0;
			if (win->jumpto(pms->input->searchterm, i))
			{
				pms->log(MSG_STATUS, STOK, "/%s", pms->input->searchterm.c_str());
			}
			else
			{
				pms->log(MSG_STATUS, STERR, "Pattern not found: %s", pms->input->searchterm.c_str());
			}
			break;

		case PEND_JUMPPREV:
			if (!win || win->type() != WIN_ROLE_PLAYLIST)
			{
				pms->log(MSG_STATUS, STERR, _("Can't search within this window."));
				break;
			}
			pms->log(MSG_STATUS, STOK, "?%s", pms->input->searchterm.c_str());
			if (win->jumpto(pms->input->searchterm, win->plist()->cursor(), true))
			{
				pms->log(MSG_STATUS, STOK, "?%s", pms->input->searchterm.c_str());
			}
			else
			{
				pms->log(MSG_STATUS, STERR, "Pattern not found: %s", pms->input->searchterm.c_str());
			}
			break;

		case PEND_JUMPMODE:
			if (!win || win->type() != WIN_ROLE_PLAYLIST)
			{
				pms->log(MSG_STATUS, STERR, _("Can't search within this window."));
				break;
			}
			pms->input->mode(INPUT_JUMP);
			pms->drawstatus();
			break;

		case PEND_PREVOF:
		case PEND_NEXTOF:
			if (pms->input->param.size() == 0)
			{
				pms->log(MSG_STATUS, STERR, _("This command has to be run with a field argument."));
				return false;
			}

			if (list == NULL)
			{
				pms->log(MSG_STATUS, STERR, _("This command has to be run within a playlist."));
				return false;
			}

			if (action == PEND_NEXTOF)
				sn = list->nextof(pms->input->param);
			else
				sn = list->prevof(pms->input->param);

			if (sn != MATCH_FAILED && win != NULL)
				win->setcursor(sn);
			else
				pms->log(MSG_STATUS, STERR, _("Could not find another entry of type '%s'."), pms->input->param.c_str());

			break;

		/* Window control */
		case PEND_CREATEPLAYLIST:
			pms->comm->create_playlist(pms->input->param);
			break;

		case PEND_SAVEPLAYLIST:

			/* FIXME: clean up the mess below */
			assert(false);

			tmpwin = win;
			i = createwindow(pms->input->param, win, list);

			switch(i)
			{
			/* Created both playlist and window */
			case 0:
				win->setplist(list);
				if (action == PEND_SAVEPLAYLIST)
				{
					/* if this is not the exact version of the playlist itself, remember to clear it out. */
					if (tmpwin->plist() != pms->comm->playlist() || tmpwin->plist()->filtercount() > 0)
					{
						pms->comm->clear(list);
					}

					/* Save the current list with it's filters.
					   Also clear out filters when done. */
					pms->comm->add(tmpwin->plist(), list);
					list->set(tmpwin->plist());
					if (tmpwin->plist() != NULL)
					{
						tmpwin->plist()->filter_clear();
					}
				}
				else
				{
					pms->comm->clear(list);

					/* In case "create" was in reply to addto or something else */
					if (tmpwin && tmpwin->type() == WIN_ROLE_WINDOWLIST && pms->input->winpop())
					{
						tmpwin->setcursor(tmpwin->size());
						handle_command(pms->input->getpending());
						if (pms->options->get_bool("addtoreturns"))
						{
							setwin(pms->input->win);
							pms->disp->lastwin = win;
						}
						else
						{
							setwin(win);
							pms->disp->lastwin = pms->input->win;
						}
						pms->input->winclear();
						break;
					}
				}
				setwin(win);
				break;
			/* Already exists */
			case 1:
				setwin(win);
				s = "\"%s\" already exists.";
				pms->log(MSG_STATUS, STERR, s.c_str(), pms->input->param.c_str());
				break;
			/* No parameter */
			case -1:
				pms->input->mode(INPUT_COMMAND);
				pms->input->text = "create " + pms->input->param;
				pms->drawstatus();
				break;
			case -2:
				generr();
				break;
			case -3:
			default:
				pms->log(MSG_STATUS, STERR, "Internal error: can't create a window.");
				pms->log(MSG_DEBUG, 0, "Window creation failed in PEND_CREATEPLAYLIST, win=%p list=%p\n", win, list);
				break;
			case -4:
				pms->log(MSG_STATUS, STERR, "Internal error: can't find the right window.");
				pms->log(MSG_DEBUG, 0, "Window search failed in PEND_CREATEPLAYLIST, win=%p list=%p\n", win, list);
			}
			break;

		/* Delete a playlist */
		case PEND_DELETEPLAYLIST:

			if (pms->input->param.size() > 0) {
				s = pms->input->param;
				list = pms->comm->find_playlist(s);
			} else {

				/* In case of windowlist, get the selected window and list */
				/* FIXME: not a good way to do it */
				if (!list) {
					win = win->current();
					if (!win) break;
					list = win->plist();
					if (!list) break;
				}

				if (list->filename.size() == 0) {
					pms->log(MSG_STATUS, STERR, "You can't remove a pre-defined playlist.");
					break;
				}
			}

			win = pms->disp->findwlist(list);
			if (pms->comm->delete_playlist(list->filename)) {
				/* FIXME: removing this is _not_ our responsibility! */
				pms->disp->delete_window(win);
				pms->log(MSG_STATUS, STOK, "Deleted playlist '%s'.", list->filename.c_str());
				break;
			}

			win = pms->disp->actwin();
			if (win)
			{
				/* Update cursor position if this was a windowlist */
				win->current();
				win->wantdraw = true;
			}

			break;
			

		case PEND_NEXTWIN:
			if (!setwin(pms->disp->nextwindow()))
				pms->log(MSG_STATUS, STERR, "There is no next window.");
			break;

		case PEND_PREVWIN:
			if (!setwin(pms->disp->prevwindow()))
				pms->log(MSG_STATUS, STERR, "There is no previous window.");
			break;

		case PEND_CHANGEWIN:
			if (pms->input->param == "playlist")
				win = pms->disp->findwlist(pms->comm->playlist());
			else if (pms->input->param == "library")
				win = pms->disp->findwlist(pms->comm->library());
			else if (pms->input->param == "windowlist")
				win = pms->disp->create_windowlist();
			else
			{
				win = pms->disp->findwlist(pms->comm->find_playlist(pms->input->param));
				if (!win)
				{
					pms->log(MSG_STATUS, STERR, "Change window: invalid parameter '%s'", pms->input->param.c_str());
					break;
				}
			}

			if (win)
				setwin(win);

			break;

		case PEND_LASTWIN:
			if (win && win->type() == WIN_ROLE_WINDOWLIST)
			{
				win->switchlastwin();
				break;
			}
			setwin(pms->disp->lastwin);
			break;

		case PEND_SHOWBIND:
			if (win)
			{
				if (win->type() == WIN_ROLE_BINDLIST)
					break;
			}

			win = pms->disp->create_bindlist();
			if (!win)
				pms->log(MSG_STATUS, STERR, "Can not show the list of key pms->bindings.");
			else
				setwin(win);

			break;

		/*
		 * Specifies which playlist should be played through
		 */
		case PEND_ACTIVATELIST:
			if (!win) break;

			if (pms->input->param.size() == 0)
			{
				/* Inside windowlist window, select window from cursor - else use active window */
				if (list == NULL)
				{
					win = win->current();
					if (win == NULL)
						break;
					list = win->plist();
				}
			}
			else
			{
				/* Use parameter as list */
				list = pms->comm->find_playlist(pms->input->param);
			}

			if (list == NULL)
			{
				pms->log(MSG_STATUS, STERR, "Invalid playlist name.");
				break;
			}

			if (pms->comm->activatelist(list))
			{
				pms->drawstatus();
				win = pms->disp->actwin();
				if (win && win->type() == WIN_ROLE_WINDOWLIST)
					win->wantdraw = true;
			}
			else
				pms->log(MSG_STATUS, STERR, "Can not activate playlist '%s'.", list->filename.c_str());

			break;

		case PEND_RESIZE:
			pms->disp->resized();
			pms->disp->forcedraw();
			pms->drawstatus();
			break;

		default:
			return false;
	}

	return true;
}

/*
 * Reports a generic error onto the statusbar
 */
void		generr()
{
	pms->log(MSG_STATUS, STERR, "%s", pms->comm->err());
}

/*
 * Adds or enqueues the next song based on play mode
 *
 * FIXME: misleading function name, too many responsibilities
 *
 * Returns the id of the song that was added.
 */
int		playnext(int playnow)
{
	Song *		song;
	int		i;

	if (!pms->comm->status()->random) {
		if (!pms->cursong() || (int)pms->comm->playlist()->end() != pms->cursong()->pos)
			song = pms->comm->playlist()->nextsong();
		else
			song = pms->comm->activelist()->nextsong();

		if (!song) return MPD_SONG_NO_ID;

		if (song->id == MPD_SONG_NO_NUM)
			i = pms->comm->add(pms->comm->playlist(), song);
		else
			i = song->id;
	} else {
		if (pms->cursong() && static_cast<int>(pms->comm->playlist()->end()) != pms->cursong()->pos)
		{
			song = pms->comm->playlist()->nextsong();
			if (!song) return MPD_SONG_NO_ID;
			i = song->id;
		}
		else
		{
			song = pms->comm->activelist()->randsong();
			if (!song) return MPD_SONG_NO_ID;
			i = pms->comm->add(pms->comm->playlist(), song);
		}
	}

	if (i == MPD_SONG_NO_NUM)
		return MPD_SONG_NO_ID;

	/* FIXME: error handling */
	if (playnow == true)
		pms->comm->playid(i);

	return i;
}

/*
 * Plays or adds all of one type
 */
int		multiplay(long mode, int playmode)
{
	Songlist *		list;
	Song *			song;
	pms_window *		win;
	int			i = MATCH_FAILED;
	int			listend;
	int			first = -1;
	string			pattern;
	string			pmode;

	win = pms->disp->actwin();
	if (!win || !win->plist()) return false;
	list = win->plist();
	if (list == pms->comm->playlist()) return false;
	song = win->plist()->cursorsong();
	if (song == NULL) return false;

	pmode = (playmode == 0 ? _("Playing") : _("Adding"));

	switch(mode)
	{
		case MATCH_ARTIST:
			if (!song->artist.size()) return false;
			pattern = song->artist;
			pms->log(MSG_STATUS, STOK, _("%s all songs by %s"), pmode.c_str(), song->artist.c_str());
			i = 0;
			break;

		case MATCH_ALBUM:
			if (!song->album.size()) return false;
			pattern = song->album;

			if (pms->comm->playlist()->match(pattern, pms->comm->playlist()->end(), pms->comm->playlist()->end(), mode | MATCH_EXACT) == MATCH_FAILED)
			{
				//last track of the current playlist is not part of this album
				i = 0;
				pms->log(MSG_STATUS, STOK, _("%s album '%s' by %s"), pmode.c_str(), song->album.c_str(), song->artist.c_str());
			}
			else
			{
				//last track of the current playlist is part of this album
				//get last track of the album
				song = list->song(list->match(pattern, 0, list->end(), mode | MATCH_EXACT | MATCH_REVERSE));
				if (pms->comm->playlist()->match(song->file, pms->comm->playlist()->end(), pms->comm->playlist()->end(), MATCH_FILE | MATCH_EXACT) != MATCH_FAILED)
				{
					//last track of playlist matches last track of album
					i = 0;
					pms->log(MSG_STATUS, STOK, _("%s album '%s' by %s"), pmode.c_str(), song->album.c_str(), song->artist.c_str());
				}
				else
				{
					//find position in the library of the playlist's last track, 
					//start adding from the one after that
					i = list->match(pms->comm->playlist()->song(pms->comm->playlist()->end())->file, 0, list->end(), MATCH_FILE | MATCH_EXACT) + 1;
					pms->log(MSG_STATUS, STOK, _("%s remainder of album '%s' by %s"), pmode.c_str(), song->album.c_str(), song->artist.c_str());
				}
			}
			break;

		case MATCH_ALL:
			pms->log(MSG_STATUS, STOK, _("%s all songs on the current list"), pmode.c_str());
			pattern = "";
			i = 0;
			break;

		default:
			return false;
	}

	listend = static_cast<int>(list->end());

	/* FIXME */
	//if (!pms->comm->list_start()) {
		//return false;
	//}

	while (true)
	{
		i = list->match(pattern, i, list->end(), mode | MATCH_EXACT);
		if (i == MATCH_FAILED) break;
		if (first == -1) {
			first = pms->comm->playlist()->size();
		}
		if (pms->comm->add(pms->comm->playlist(), list->song(i)) == MPD_SONG_NO_ID) {
			return false;
		}
		if (++i > listend) break;
	}

	//if (!pms->comm->list_end()) {
		//return false;
	//}

	if (first != -1 && playmode == 0) {
		pms->comm->playpos(first);
	}

	return true;
}

/*
 * Changes to a window, and sets appropriate input mode
 */
bool		setwin(pms_window * win)
{
	if (!win) return false;
	if (!pms->disp->activate(win)) return false;

	if (win->type() == WIN_ROLE_WINDOWLIST || win->type() == WIN_ROLE_BINDLIST)
		pms->input->mode(INPUT_LIST);
	else
		pms->input->mode(INPUT_NORMAL);

	if (pms->options->get_bool("followwindow"))
	{
		pms->comm->activatelist(win->plist());
		pms->drawstatus();
	}
	return true;
}



/*
 * Create a playlist and connect a window to it, returns the window if successful
 *
 * FIXME: arbitrary numbered return values
 * FIXME: this function is only used from one place, which is also a real mess
 * FIXME: remove this altogether
 */
int		createwindow(string param, pms_window *& win, Songlist *& list)
{
	/*
	win = NULL;
	list = NULL;

	if (param.size() == 0)
		return -1;

	list = pms->comm->find_playlist(param);

	if (list)
	{
		win = pms->disp->findwlist(list);
		if (win)
		{
			return 1;
		}
		return -4;
	}

	list = pms->comm->newplaylist(param);
	
	if (!list)
		return -2;

	win = static_cast<pms_window *> (pms->disp->create_playlist());

	if (!win)
		return -3;

	return 0;
	*/
}



/*
 * Defines all commands which can be used
 */
bool init_commandmap()
{
	pms->commands = new Commandmap();
	if (pms->commands == NULL)	return false;
	pms->bindings = new Bindings(pms->commands);
	if (pms->bindings == NULL)
	{
		delete pms->commands;
		return false;
	}

	/* Misc stuff */
	pms->commands->add("!", "Run a shell command", PEND_SHELL);
	pms->commands->add("command-mode", "Switch to command mode", PEND_COMMANDMODE);
	pms->commands->add("info", "Show file information in console", PEND_SHOW_INFO);
	pms->commands->add("password", "Send a password to the server", PEND_PASSWORD);
	pms->commands->add("source", "Read a script or configuration file", PEND_SOURCE);
	pms->commands->add("write-config", "Save configuration file", PEND_WRITE_CONFIG);
	pms->commands->add("rehash", "Read configuration file", PEND_REHASH);
	pms->commands->add("redraw", "Force screen redraw", PEND_REDRAW);
	pms->commands->add("version", "Show program version", PEND_VERSION);
	pms->commands->add("v", "Show program version", PEND_VERSION);
	pms->commands->add("clear-topbar", "Remove all contents in topbar", PEND_CLEAR_TOPBAR);
	pms->commands->add("quit", "Quit program", PEND_QUIT);
	pms->commands->add("q", "Quit program", PEND_QUIT);

	/* Playlist management */
	pms->commands->add("update", "Update MPD music library", PEND_UPDATE_DB);
	pms->commands->add("create", "Create an empty playlist", PEND_CREATEPLAYLIST);
	pms->commands->add("save", "Save the playlist as a new playlist", PEND_SAVEPLAYLIST);
	pms->commands->add("delete-list", "Delete the current playlist", PEND_DELETEPLAYLIST);
	pms->commands->add("select", "Add song under cursor to selection", PEND_SELECT);
	pms->commands->add("unselect", "Remove song under cursor to selection", PEND_UNSELECT);
	pms->commands->add("clear-selection", "Clear selection", PEND_CLEARSELECTION);
	pms->commands->add("toggle-select", "Toggle selection", PEND_TOGGLESELECT);
	pms->commands->add("remove", "Remove selected song(s) from list", PEND_DELETE);
	pms->commands->add("move", "Move songs by offset N", PEND_MOVEITEMS);

	/* Searching */
	pms->commands->add("next-result", "Jump to next result", PEND_JUMPNEXT);
	pms->commands->add("prev-result", "Jump to previous result", PEND_JUMPPREV);
	pms->commands->add("quick-find", "Go to jump mode", PEND_JUMPMODE);
	pms->commands->add("next-of", "Jump to next of given field", PEND_NEXTOF);
	pms->commands->add("prev-of", "Jump to previous of given field", PEND_PREVOF);
	pms->commands->add("filter", "Go to filtering mode", PEND_FILTERMODE);
	pms->commands->add("clear-filters", "Clear filters", PEND_CLEARFILTERS);

	/* Playback */
	pms->commands->add("play", "Play song under cursor", PEND_PLAY);
	pms->commands->add("play-album", "Play entire album of song under cursor", PEND_PLAYALBUM);
	pms->commands->add("play-artist", "Play all songs from artist of song under cursor", PEND_PLAYARTIST);
	pms->commands->add("play-random", "Play a random song", PEND_PLAYRANDOM);
	pms->commands->add("add", "Add selected song(s) to playlist", PEND_ADD);
	pms->commands->add("add-to", "Add selected song(s) to a named playlist", PEND_ADDTO);
	pms->commands->add("add-album", "Add entire album of song under cursor to playlist", PEND_ADDALBUM);
	pms->commands->add("add-artist", "Add all songs from artist of song under cursor to playlist", PEND_ADDARTIST);
	pms->commands->add("add-random", "Add a random song to playlist", PEND_ADDRANDOM);
	pms->commands->add("add-all", "Add all songs from the currently visible list to playlist", PEND_ADDALL);
	pms->commands->add("next", "Next song by play mode", PEND_NEXT);
	pms->commands->add("really-next", "Next song in list regardless of play mode", PEND_REALLY_NEXT);
	pms->commands->add("prev", "Previous song", PEND_PREV);
	pms->commands->add("pause", "Pause or play", PEND_PAUSE);
	pms->commands->add("toggle-play", "Toggle playback, play if stopped", PEND_TOGGLEPLAY);
	pms->commands->add("stop", "Stop", PEND_STOP);
	pms->commands->add("shuffle", "Shuffle playlist", PEND_SHUFFLE);
	pms->commands->add("clear", "Clear the list", PEND_CLEAR);
	pms->commands->add("crop", "Crops list to currently playing song", PEND_CROP);
	pms->commands->add("cropsel", "Crops list to selected songs", PEND_CROPSELECTION);
	pms->commands->add("repeat", "Toggle repeat on/off", PEND_REPEAT);
	pms->commands->add("random", "Toggle random on/off", PEND_RANDOM);
	pms->commands->add("single", "Toggle single on/off", PEND_SINGLE);
	pms->commands->add("consume", "Toggle consume on/off", PEND_CONSUME);
	pms->commands->add("volume", "Increase or decrease volume", PEND_VOLUME);
	pms->commands->add("mute", "Toggle mute", PEND_MUTE);
	pms->commands->add("crossfade", "Set crossfade time", PEND_CROSSFADE);
	pms->commands->add("seek", "Seek in stream", PEND_SEEK);

	/* Movement */
	pms->commands->add("next-window", "Go to next playlist", PEND_NEXTWIN);
	pms->commands->add("prev-window", "Go to previous playlist", PEND_PREVWIN);
	pms->commands->add("change-window", "Go to named playlist", PEND_CHANGEWIN);
	pms->commands->add("last-window", "Switch to last window used", PEND_LASTWIN);
	pms->commands->add("help", "Show current key pms->bindings", PEND_SHOWBIND);
	pms->commands->add("goto-random", "Set the cursor to a random song.", PEND_GOTORANDOM);
	pms->commands->add("goto-current", "Find current song", PEND_GOTO_CURRENT);
	pms->commands->add("activate-list", "Activate list for playback", PEND_ACTIVATELIST);
	pms->commands->add("move-up", "Move cursor up", PEND_MOVE_UP);
	pms->commands->add("move-down", "Move cursor down", PEND_MOVE_DOWN);
	pms->commands->add("move-halfpgup", "Move cursor one half page up", PEND_MOVE_HALFPGUP);
	pms->commands->add("move-halfpgdn", "Move cursor one half page down", PEND_MOVE_HALFPGDN);
	pms->commands->add("move-pgup", "Move cursor one page up", PEND_MOVE_PGUP);
	pms->commands->add("move-pgdn", "Move cursor one page down", PEND_MOVE_PGDN);
	pms->commands->add("move-home", "Move cursor to start of list", PEND_MOVE_HOME);
	pms->commands->add("move-end", "Move cursor to end of list", PEND_MOVE_END);
	pms->commands->add("scroll-up", "Scroll up one line", PEND_SCROLL_UP);
	pms->commands->add("scroll-down", "Scroll down one line", PEND_SCROLL_DOWN);
	pms->commands->add("center-cursor", "Center the cursor", PEND_CENTER_CURSOR);
	pms->commands->add("centre-cursor", "Centre the cursor", PEND_CENTER_CURSOR);

	return true;
}


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