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 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847
|
/* Copyright (C) Bram Avontuur (bram@avontuur.org)
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* If you like this program, or if you have any comments, tips for improve-
* ment, money, etc, please do so on the sourceforge project page :
* http://mp3blaster.sourceforge.net/
*
* May 21 2000: removed all calls to attr_get() since not all [n]curses
* : versions use the same paramaters (sigh)
* Dec 31 2000: Applied a patch from Rob Funk to fix randomness bug
* Newer dates: changes made are stored in CVS.
*/
/*
* Added basic LIRC support.
* Olgierd Pieczul <wojrus@linux.slupsk.net>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "history.h"
#include "mp3blaster.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h>
#include <fnmatch.h>
#if defined(PTHREADEDMPEG) && defined(HAVE_PTHREAD_H)
#include <pthread.h>
#elif defined (LIBPTH) && defined(HAVE_PTH_H)
#include <pth.h>
#endif
#include NCURSES_HEADER
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
#include "getopt_local.h"
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include "global.h"
#include "scrollwin.h"
#include "mp3win.h"
#include "fileman.h"
#include "exceptions.h"
#include <mpegsound.h>
#include <nmixer.h>
//load keybindings
#include "keybindings.h"
#include "getinput.h"
#ifdef INCLUDE_LIRC
#include <lirc/lirc_client.h>
#endif
#ifdef LIBPTH
#define USLEEP(x) pth_usleep(x)
#else
#define USLEEP(x) usleep(x)
#endif
/* paranoia define[s] */
#ifndef FILENAME_MAX
#define FILENAME_MAX 1024
#endif
#define MIN_SCREEN_WIDTH 80
#define MIN_SCREEN_HEIGHT 23
/* values for global var 'window' */
enum _action { AC_NONE, AC_REWIND, AC_FORWARD, AC_NEXT, AC_PREV,
AC_STOP, AC_ONE_MP3, AC_PLAY,
AC_STOP_LIST, AC_SKIPEND, AC_NEXTGRP, AC_PREVGRP } action;
/* There are different types of input modes, e.g. searching for text,
* inputting text, ..
*/
enum _input_mode { IM_DEFAULT, IM_SEARCH, IM_INPUT } input_mode;
/* External functions */
extern short cf_parse_config_file(const char *);
extern cf_error cf_get_error();
extern const char *cf_get_error_string();
/* Prototypes */
void cw_toggle_group_mode();
void cw_draw_group_mode();
void cw_draw_play_mode(short cleanit=0);
short set_play_mode(const char*);
void cw_toggle_play_mode();
void mw_clear();
void mw_settxt(const char*);
short read_playlist(const char *);
short write_playlist(const char *);
void *play_list(void *);
short start_song(short was_playing=0);
void set_one_mp3(char*);
void stop_song();
void stop_list();
void update_play_display();
void lock_playing_mutex();
void unlock_playing_mutex();
void update_status_file();
void write_status_file();
#ifdef PTHREADEDMPEG
void change_threads();
void cw_draw_threads(short cleanit=0);
short set_threads(int);
#endif
void add_selected_file(const char*);
char *get_current_working_path();
void usage();
void show_help();
void draw_static(int file_mode=0);
void draw_settings(int cleanit=0);
void fw_getpath(const char *, void *);
void fw_convmp3(const char *, void *);
void fw_addurl(const char *, void *);
void fw_search_next_char(char);
void fw_start_search(int timeout=2);
void fw_end_search();
void fw_delete();
void set_sound_device(const char *);
void set_mixer_device(const char *);
short set_fpl(int);
void set_default_colours();
void set_action(_action bla);
char *determine_song(short set_played=1);
void set_song_info(const char *fl, struct song_info si);
void set_song_status(playstatus_t);
void set_song_time(int,int);
void popup_win(const char **label, void (*func)(const char *, void *), void *func_args = NULL, const char *init_str = NULL, short want_input=0, int maxlen=0);
int myrand(int);
void reset_playlist(int);
void cw_draw_repeat();
void init_helpwin();
void repaint_help();
void get_label(command_t, char *);
void set_keylabel(int k, char *label);
void init_playopts();
void init_globalopts();
void draw_next_song(const char*);
void set_next_song(int);
void reset_next_song();
mp3Win* newgroup();
short fw_markfilebad(const char *file);
void get_mainwin_size(int*, int*, int*, int*);
void get_mainwin_borderchars(chtype*, chtype*, chtype*, chtype*,
chtype*, chtype*, chtype*, chtype*);
void change_program_mode(unsigned short pm);
void end_help();
void update_songinfo(playstatus_t, short);
void update_ncurses(int update_all);
void end_program();
const char *get_error_string(int);
void fw_toggle_sort();
void fw_draw_sortmode(sortmodes_t);
short fw_toggle_display();
short toggle_display();
void warning(const char *, ...);
void wake_player();
void add_init_opt(struct init_opts *myopts, const char *option, void *value);
void get_input(
WINDOW *win, const char*defval, unsigned int size, unsigned int maxlen,
int y, int x, void (*got_input)(const char *, void *), void *args = NULL
);
void set_inout_opts();
void select_files_by_pattern(const char *, void *);
void playlist_write(const char *playlistfile, void *args);
void fw_rename_file(const char *newname, void *args);
void *lirc_loop(void *);
#if defined(PTHREADEDMPEG)
# define LOCK_NCURSES (!pthread_mutex_lock(&ncurses_mutex))
# define UPDATE_CURSES
# define UNLOCK_NCURSES pthread_mutex_unlock(&ncurses_mutex)
#elif defined(LIBPTH)
# define LOCK_NCURSES (pth_mutex_acquire(&ncurses_mutex, FALSE, NULL) != -1)
# define UPDATE_CURSES
# define UNLOCK_NCURSES pth_mutex_release(&ncurses_mutex)
#endif
#define OPT_LOADLIST 1
#define OPT_DEBUG 2
#define OPT_PLAYMP3 4
#define OPT_NOMIXER 8
#define OPT_AUTOLIST 16
#define OPT_QUIT 32
#define OPT_CHROOT 64
#define OPT_DOWNSAMPLE 128
#define OPT_SOUNDDEV 256
#define OPT_PLAYMODE 512
#define OPT_FPL 1024
#define OPT_8BITS 2048
#define OPT_THREADS 4096
#define OPT_MIXERDEV 8192
#define OPT_REPEAT 16384
/* global vars */
/* songinf contains info about the currently playing audio file.
* Access to songinf may only occur when a lock has been acquired on the
* ncurses mutex (see LOCK_NCURSES)
*/
struct song_t
{
struct song_info songinfo;
playstatus_t status;
int elapsed;
int remain;
char *path;
char *next_song;
char *warning;
int update; // 0, don't update, 1: update songinfo, 2: update time, 3: both
} songinf;
struct playopts_t
{
short song_played;
short playing_one_mp3;
short allow_repeat; //true if at least 1 song in the list finished correctly
short pause;
short playing;
char * one_mp3;
Fileplayer *player;
time_t tyd;
time_t newtyd;
short quit_program;
} playopts;
struct ptag_t
{
char tag[80];
char keywords[5][80];
char values[5][255];
};
unsigned short
progmode;
#ifdef PTHREADEDMPEG
pthread_mutex_t
playing_mutex = PTHREAD_MUTEX_INITIALIZER,
ncurses_mutex = PTHREAD_MUTEX_INITIALIZER,
onemp3_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t
playing_cond = PTHREAD_COND_INITIALIZER;
pthread_t
thread_playlist; //thread that handles playlist (main handles input)
#ifdef INCLUDE_LIRC
pthread_t thread_lirc;
#endif
#elif defined(LIBPTH)
pth_mutex_t
playing_mutex,
ncurses_mutex,
onemp3_mutex;
pth_cond_t
playing_cond;
pth_t
thread_playlist;
#endif /* PTHREADEDMPEG */
mp3Win
*mp3_curwin = NULL, /* currently displayed mp3 window */
*mp3_rootwin = NULL, /* root mp3 window */
*mp3_groupwin = NULL, /* starting group for PLAY_GROUPS mode */
*playing_group = NULL; /* in group-mode playback: group being played */
char
*status_file = NULL;
fileManager
*file_window = NULL; /* window to select files with */
scrollWin
*helpwin = NULL,
*bighelpwin = NULL;
NMixer
*mixer = NULL;
int
nselfiles = 0,
current_song = -1,
nr_played_songs = 1,
next_song = -1;
short
popup = 0, /* 1 when a `popup' window is onscreen */
quit_after_playlist = 0;
struct _globalopts
globalopts;
char
**selected_files = NULL,
*startup_path = NULL,
*fw_searchstring,
**played_songs = NULL,
**environment = NULL;
History
*history = NULL;
getInput
*global_input = NULL;
#ifdef INCLUDE_LIRC
command_t lirc_cmd = CMD_NONE;
#endif
/* Function Name: usage
* Description : Warn user about this program's usage and then exit nicely
* : Program must NOT be in ncurses-mode when this function is
* : called.
*/
void
usage()
{
fprintf(stderr, "Mp3blaster v%s (C)1997 - 2009 Bram Avontuur.\n%s", \
VERSION, \
"Usage:\n" \
"\tmp3blaster [options]\n"\
"\tmp3blaster [options] [file1 ...]\n"\
"\t\tPlay one or more mp3's\n"\
"\tmp3blaster [options] --list/-l <playlist.lst>\n"\
"\t\tLoad a playlist but don't start playing.\n"\
"\tmp3blaster [options] --autolist/-a <playlist.lst>\n"\
"\t\tLoad a playlist and start playing.\n"\
"\nOptions:\n"\
"\t--downsample/-2: Downsample (44->22Khz etc)\n"\
"\t--8bits/-8: 8bit audio (autodetected if your card can't handle 16 bits)\n"\
"\t--config-file/-c=file: Use other config file than the default\n"\
"\t\t~/.mp3blasterrc\n"\
"\t--debug/-d: Log debug-info in $HOME/.mp3blaster.\n"\
"\t--status-file/-f=file: Keep info on the mp3s being played, in the\n"\
"\t\tspecified file.\n"\
"\t--help/-h: This help screen.\n"\
"\t--mixer-device/-m: Mixer device to use (use 'NAS' for NAS mixer)\n"\
"\t--no-mixer/-n: Don't start the built-in mixer.\n"\
"\t--chroot/-o=<rootdir>: Set <rootdir> as mp3blaster's root dir.\n"\
"\t\tThis affects *ALL* file operations in mp3blaster!!(including\n"\
"\t\tplaylist reading&writing!) Note that only users with uid 0\n"\
"\t\tcan use this option (yet?). This feature will change soon.\n"\
"\t--playmode/-p={onegroup,allgroups,allrandom}\n"\
"\t\tDefault playing mode is resp. Play first group only, Play\n"\
"\t\tall groups, Play all songs in random order.\n"\
"\t--dont-quit/-q: Don't quit after playing mp3[s] (only makes sense\n"\
"\t\tin combination with --autolist or files from command-line)\n"\
"\t--repeat/-R: Repeat playlist.\n"\
"\t--runframes/-r=<number>: Number of frames to decode in one loop.\n"\
"\t\tRange: 1 to 10 (default=5). A low value means that the\n"\
"\t\tinterface (while playing) reacts faster but slow CPU's might\n"\
"\t\thick. A higher number implies a slow interface but less\n"\
"\t\thicks on slow CPU's.\n"\
"\t--sound-device/-s=<device>: Device to use to output sound.\n"\
"\t\tDefault for your system is " SOUND_DEVICE ".\n"\
"\t\tIf you want to use NAS (Network Audio System) as playback\n"\
"\t\tdevice, then enter the nasserver's address as device (e.g.\n"\
"\t\thost.name.com:0; it *must* contain a colon)\n"\
"\t--threads/-t=<amount>: Numbers of threads to use for buffering\n"\
"\t\t(only works if mp3blaster was compiled with threads). Range is \n"\
"\t\t0..500 in increments of 50 only.\n"\
"\t--version,v: Display version number.\n"\
"");
exit(1);
}
/* Function : calc_mainwindow_width
* Description: Calculates the width of the main window (the big
* : scrollable one) based on the current term size
* Parameters : termWidth
* : Current width of the terminal
* Returns : The calculated width
* SideEffects: None.
*/
int
calc_mainwindow_width(int termWidth) {
int result = termWidth - 12;
if (result < 1)
result = 1;
return result;
}
/* Function : calc_mainwindow_height
* Description: Calculates the height of the main window (the big
* : scrollable one) based on the current term size
* Parameters : termHeight
* : Current height of the terminal
* Returns : The calculated height
* SideEffects: None.
*/
int
calc_mainwindow_height(int termHeight) {
int result = termHeight - 13;
if (result < 1)
result = 1;
return result;
}
/* Function : fw_begin
* Description: Enters file manager mode. Program should be in normal mode
* : when this function's called.
* Parameters : None.
* Returns : Nothing.
* SideEffects: None.
*/
void
fw_begin()
{
quit_after_playlist = 0;
change_program_mode(PM_FILESELECTION);
fw_draw_sortmode(globalopts.fw_sortingmode);
if (file_window)
delete file_window;
file_window = new fileManager(NULL, calc_mainwindow_height(LINES),
calc_mainwindow_width(COLS), 10, 0, CP_DEFAULT, 1);
file_window->drawTitleInBorder(1);
file_window->setBorder(ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE,
ACS_LTEE, ACS_PLUS, ACS_LTEE, ACS_PLUS);
if (globalopts.want_id3names)
file_window->setDisplayMode(2); //show as ID3 names
else
file_window->setDisplayMode(1); //show file sizes as well
file_window->setWrap(globalopts.wraplist);
file_window->swRefresh(0);
draw_static(1);
}
/* fw_end closes the file_window if it's on-screen, and moves all selected
* files into the currently used group. Finally, the selection-window of the
* current group is put back on the screen and the title in the header-window
* is set to match that of the current group. If files were selected in another
* dir than the current dir, these will be added too (they're stored in
* selected_files then). selected_files will be freed as well.
*/
void
fw_end()
{
int
i,
nselected;
char
**selitems = NULL;
mp3Win
*sw = mp3_curwin;
if (progmode != PM_FILESELECTION) /* or: !file_window */
return;
selitems = file_window->getSelectedItems(&nselected);
for ( i = 0; i < nselected; i++)
{
if (!is_audiofile(selitems[i]) || is_dir(selitems[i]))
continue;
const char *path = file_window->getPath();
char *itemname = (char *)malloc(strlen(selitems[i]) +
strlen(path) + 2);
if (path[strlen(path) - 1] != '/')
sprintf(itemname, "%s/%s", path, selitems[i]);
else
sprintf(itemname, "%s%s", path, selitems[i]);
add_selected_file(itemname);
free(itemname);
}
if (nselected)
{
for (i = 0; i < nselected; i++)
delete[] selitems[i];
delete[] selitems;
}
if (selected_files)
{
short foo[2] = { 0, 1 };
const char *bla[4];
int idx;
for (i = 0; i < nselfiles; i++)
{
bla[0] = (const char*)selected_files[i];
if (is_httpstream(selected_files[i]))
bla[1] = (const char*)selected_files[i];
else
bla[1] = chop_path(selected_files[i]);
bla[2] = NULL;
bla[3] = NULL;
idx=sw->addItem(bla, foo, CP_FILE_MP3);
if (idx!=-1)
sw->changeItem(idx,&id3_filename,strdup(bla[0]),2);
free(selected_files[i]);
}
free(selected_files);
selected_files = NULL;
nselfiles = 0;
}
delete file_window;
file_window = NULL;
change_program_mode(PM_NORMAL);
sw->swRefresh(1);
draw_static();
mp3_curwin->swRefresh(2);
}
void
fw_changedir(const char *newpath = 0)
{
if (progmode != PM_FILESELECTION)
return;
int
nselected = 0;
char
**selitems;
const char
*path;
if (!newpath)
path = file_window->getSelectedItem();
else
path = newpath;
selitems = file_window->getSelectedItems(&nselected);
/* if not changed to current dir and files have been selected add them to
* the selection list
*/
if (strcmp(path, ".") && nselected)
{
int i;
for (i = 0; i < nselected; i++)
{
const char
*pwd = file_window->getPath();
char
*file = (char *)malloc((strlen(pwd) + strlen(selitems[i]) +
2) * sizeof(char));
strcpy(file, pwd);
if (pwd[strlen(pwd) - 1] != '/')
strcat(file, "/");
strcat(file, selitems[i]);
if (is_audiofile(file))
add_selected_file(file);
free(file);
delete[] selitems[i];
}
delete[] selitems;
}
/* If the old dir is a directory in the new dir, set the current
* selection to the entry for that dir. */
const char *oldPath = file_window->getPath();
char *foundItem = NULL;
if (oldPath != NULL)
{
if (strcmp(path, "../") == 0) {
const char *oldPtr = oldPath + strlen(oldPath) - 1;
if (oldPtr > oldPath && *oldPtr == '/')
oldPtr--;
while (oldPtr >= oldPath)
{
if (*oldPtr == '/') {
foundItem = strdup(oldPtr + 1);
break;
}
oldPtr--;
}
}
else if (strcmp(path, "./") == 0)
foundItem = strdup(path);
}
file_window->changeDir(path);
if (foundItem != NULL)
{
int index = file_window->findItem(foundItem);
if (index != -1)
file_window->setItem(index);
free(foundItem);
}
refresh();
}
/* PRE: Be in PM_FILESELECTION
*/
void
fw_getpath(const char *npath, void *opts)
{
DIR
*dir = NULL;
char
*newpath;
if (opts);
if (!npath || !(newpath = expand_path(npath)))
return;
if ( !(dir = opendir(newpath)))
{
free(newpath);
return;
}
closedir(dir);
fw_changedir(newpath);
free(newpath);
return;
}
void
draw_play_key(short do_refresh)
{
char klabel[4];
int maxy,maxx;
int r;
getmaxyx(stdscr, maxy, maxx);
r = maxx - 12;
attrset(COLOR_PAIR(CP_LABEL));
move(maxy-9,r+4);
lock_playing_mutex();
if (!playopts.playing || (playopts.playing && playopts.song_played && playopts.pause))
addstr(" |>");
else
addstr(" ||");
unlock_playing_mutex();
get_label(CMD_PLAY_PLAY, klabel);
move(maxy-8,r+4); addstr(klabel);
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
if (do_refresh)
refresh();
}
/* Draws static interface components (and refreshes the screen). */
void
draw_static(int file_mode)
{
int maxy, maxx, r;
char klabel[4];
getmaxyx(stdscr,maxy,maxx);
r = maxx - 12;
if (file_mode);
//First, the border
box(stdscr, ACS_VLINE, ACS_HLINE);
//Now, the line under the function keys and above the status window
move(5,1); hline(ACS_HLINE, maxx-2);
move(5,0); addch(ACS_LTEE); move(5,maxx-1); addch(ACS_RTEE);
move(maxy-4,1); hline(ACS_HLINE, maxx-2);
move(maxy-4,0); addch(ACS_LTEE); move(maxy-4,maxx-1); addch(ACS_RTEE);
//draw the box bottom-right from the function keys
move(10,r); hline(ACS_HLINE, 11);
move(6,r-1); vline(ACS_VLINE,4);
move(5,r-1); addch(ACS_TTEE);
move(10,maxx-1); addch(ACS_RTEE);
//fix upperleft corner for playlist window
move(10,0); addch(ACS_LTEE);
move(11,r); addch('[');
move(11,r+2); addstr("] shuffle");
move(12,r); addch('[');
move(12,r+2); addstr("] repeat");
move(9,r); addstr("Threads:");
move(0,COLS-6);
addstr("( )");
move(5,COLS-6);
addstr("( )");
attrset(COLOR_PAIR(CP_BUTTON));
get_label(CMD_HELP_PREV, klabel);
move(0,COLS-5); addnstr(klabel, 3);
get_label(CMD_HELP_NEXT, klabel);
move(5,COLS-5); addnstr(klabel, 3);
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
move(maxy-4,maxx-13); addch(ACS_PLUS);
move(maxy-3,maxx-13); addch(ACS_VLINE);
move(maxy-2,maxx-13); addch(ACS_VLINE);
move(maxy-1,maxx-13); addch(ACS_BTEE);
//draw cd-style keys
draw_play_key(0);
attrset(COLOR_PAIR(CP_LABEL));
move(maxy-9, r); addstr(" |<"); move(maxy-8, r);
get_label(CMD_PLAY_PREVIOUS, klabel); addstr(klabel);
move(maxy-9,r+8); addstr(" >|"); move(maxy-8,r+8);
get_label(CMD_PLAY_NEXT, klabel); addstr(klabel);
move(maxy-6, r); addstr(" <<"); move(maxy-5, r);
get_label(CMD_PLAY_REWIND, klabel); addstr(klabel);
move(maxy-6,r+4); addstr(" []"); move(maxy-5,r+4);
get_label(CMD_PLAY_STOP, klabel); addstr(klabel);
move(maxy-6,r+8); addstr(" >>"); move(maxy-5,r+8);
get_label(CMD_PLAY_FORWARD, klabel); addstr(klabel);
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
if (!globalopts.no_mixer)
{
char klabel[4];
get_label(CMD_TOGGLE_MIXER, klabel);
move(maxy-4,maxx-12);
addstr("[ ]Mixer");
move(maxy-4, maxx-11);
attrset(COLOR_PAIR(CP_BUTTON));
addnstr(klabel, 3);
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
}
//Draw Next Song:
move(7,2);
addstr("Next Song :");
refresh();
return;
}
void
refresh_screen()
{
wrefresh(curscr);
}
/*
* Returns the main (scrollable) window. This windows is
* Dependant on the context the program is currently in.
*/
scrollWin *
getMainWindow()
{
scrollWin * mainwindow = (scrollWin *)mp3_curwin;
if (progmode == PM_FILESELECTION)
mainwindow = (scrollWin *)file_window;
else if (progmode == PM_HELP)
mainwindow = (scrollWin *)bighelpwin;
return mainwindow;
}
/* Function : init_mixer
* Description: (re)creates the mini mixer in the bottom right of the
* : screen, but only if a mixer is desired.
* Parameters : None.
* Returns : Nothing.
* SideEffects: None.
*/
void
init_mixer()
{
/* initialize mixer */
if (!globalopts.no_mixer)
{
const int color_pairs[4] = { 13,14,15,16 };
int mixerXOffset = COLS - 12;
int mixerYOffset = LINES - 3;
if (mixer) {
delete mixer;
}
//sanity check against too small screen[resize]s
if (mixerXOffset < 0)
mixerXOffset = 0;
if (mixerYOffset < 0)
mixerYOffset = 0;
mixer = new NMixer(stdscr, globalopts.mixer_device, mixerXOffset,
mixerYOffset, 2, color_pairs, globalopts.colours.default_bg, 1);
if (!(mixer->NMixerInit()))
{
delete mixer;
}
else
{
//TODO: These should be customizable too..
mixer->setMixerCommandKey(MCMD_NEXTDEV, 't');
mixer->setMixerCommandKey(MCMD_PREVDEV, 'T');
mixer->setMixerCommandKey(MCMD_VOLUP, '>');
mixer->setMixerCommandKey(MCMD_VOLDN, '<');
}
}
}
void
handle_resize(int dummy = 0)
{
int newWindowWidth;
int newWindowHeight;
(void)dummy; //unused param
/* re-initialize ncurses, this updates LINES & COLS */
endwin();
refresh();
clear();
newWindowWidth = calc_mainwindow_width(COLS);
newWindowHeight = calc_mainwindow_height(LINES);
try {
//resize the window that's currently onscreen.
getMainWindow()->resize(newWindowWidth, newWindowHeight);
//resize all playlist windows recursively recursively. Just one of them
//may be onscreen.
mp3_rootwin->resize(newWindowWidth, newWindowHeight, 1);
} catch (IllegalArgumentsException e) {
endwin();
fprintf(stderr, "Resize too small!\n");
exit(1);
}
draw_settings();
init_helpwin();
init_mixer();
repaint_help();
getMainWindow()->swRefresh(2);
draw_static();
//redraw current song status / info
update_ncurses(1);
refresh_screen();
}
/* Adds a new group to mp3_curwin and returns it.
*/
/* Function : add_group
* Description: Adds a new group to mp3_curwin and returns it.
* Parameters : newgroupname: Name of the group. If no name is supplied,
* : [Default] is used. If the groupname does not start and end with
* : '[' and ']', these will be added.
* Returns : The newly created group.
* SideEffects: None.
*/
mp3Win *
add_group(const char *newgroupname=0)
{
mp3Win
*sw;
sw = newgroup();
if (!newgroupname)
newgroupname = "[Default]";
sw->setTitle(newgroupname);
mp3_curwin->addGroup(sw, newgroupname);
mp3_curwin->swRefresh(1);
return sw;
}
/* Function : set_group_name
* Description: Sets the group name of the currently highlighted group.
* : It is called from popup_win.
* Parameters : name: Name to give to group. free() after use.
* : args: not used
* Returns : Nothing.
* SideEffects: None.
*/
void
set_group_name(const char *name, void *args)
{
const char
*nm;
mp3Win
*sw = mp3_curwin;
int
index;
if (args);
if (!sw)
return;
index = sw->sw_selection;
if (!sw->isGroup(index) || !(nm = sw->getItem(index)) || !strcmp(nm, "[..]"))
return;
if (!strcmp(name, ".."))
{
return;
}
char tmpname[strlen(name)+3];
sprintf(tmpname, "[%s]", name);
sw->changeItem(index, tmpname);
mp3Win *gr = sw->getGroup(index);
if (gr)
gr->setTitle(tmpname);
}
void
mw_clear()
{
int width = (COLS > 14 ? COLS - 14 : 1);
char emptyline[width+1];
//clear old text
memset(emptyline, ' ', width * sizeof(char));
emptyline[width] = '\0';
move(LINES-2,1);
addstr(emptyline);
}
void
mw_settxt(const char *txt)
{
mw_clear();
move(LINES-2,1);
addnstr(txt, (COLS > 14 ? COLS-14 : 1));
refresh();
}
/* PRE: f is opened
* POST: if a group was successfully read from f, non-zero will be returned
* and the group is added.
*/
int
read_group_from_file(FILE *f)
{
return 0 || f; //gotta fix this..
#if 0
int
linecount = 0;
char
*line = NULL;
short
read_ok = 1,
group_ok = 0;
scrollWin
*sw = NULL;
while (read_ok)
{
line = readline(f);
if (!line)
{
read_ok = 0;
continue;
}
if (linecount == 0)
{
char
*tmp = (char*)malloc((strlen(line) + 1) * sizeof(char));
int
success = sscanf(line, "GROUPNAME: %[^\n]", tmp);
if (success < 1) /* bad group-header! */
read_ok = 0;
else /* valid group - add it */
{
char
*title;
group_ok = 1;
sw = group_stack->entry(globalopts.current_group - 1);
/* if there are no entries in the current group, the first
* group will be read into that group.
*/
if (sw->getNitems())
{
add_group();
sw = group_stack->entry(group_stack->stackSize() - 1);
}
title = (char*)malloc((strlen(tmp) + 16) * sizeof(char));
sprintf(title, "%02d:%s", globalopts.current_group, tmp);
sw->setTitle(tmp);
sw->swRefresh(0);
free(title);
}
free(tmp);
}
else if (linecount == 1) /* read play order from file */
{
char
*tmp = new char[strlen(line) + 1];
int
success = sscanf(line, "PLAYMODE: %[^\n]", tmp);
if (success < 1) /* bad group-header! */
{
read_ok = 0; group_ok = 0;
}
else
((mp3Win*)sw)->setPlaymode((short)(atoi(tmp) ? 1 : 0));
delete[] tmp;
}
else
{
if (!strcmp(line, "\n")) /* line is empty, great. */
read_ok = 0;
else
{
char *songname = new char[strlen(line) + 1];
sscanf(line, "%[^\n]", songname);
if (is_audiofile(songname))
{
const char *bla[4];
short foo[2] = { 0, 1 };
int idx;
bla[0] = (const char*)songname;
bla[1] = chop_path(songname);
bla[2] = NULL;
bla[3] = NULL;
idx=sw->addItem(bla, foo, CP_FILE_MP3);
if (idx!=-1)
{
sw->changeItem(idx,&id3_filename,strdup(bla[0]),2);
}
}
delete[] songname;
}
/* ignore non-mp3 entries.. */
}
free(line);
++linecount;
}
if (group_ok)
sw->swRefresh(1);
return group_ok;
#endif
}
/* return value = allocated using new, so delete[] it */
void
popup_win
(
const char **label,
void (*func)(const char *, void *), void *func_args,
const char *init_str,
short want_input, int maxlen
)
{
int maxx, maxy, width, height, by, bx, i, j;
popup = 1; /* global variable, indicating input status */
getmaxyx(stdscr, maxy, maxx);
width = maxx - 14;
height = maxy - 15;
by = 11; bx = 1;
if (!maxlen)
maxlen = width - 2;
bkgdset(COLOR_PAIR(CP_POPUP)|A_BOLD);
//color the warning screen. It's really awkward, but it works..
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
//move(by+i, bx+j); addch('x');
move(by+i, bx+j); addch(' ');
}
}
//write text
i = 0;
while (label[i])
{
move(by+i+2, bx+1); addstr(label[i++]);
}
if (want_input)
{
//attrset(A_BOLD|COLOR_PAIR(CP_POPUP_INPUT));
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
bkgdset(COLOR_PAIR(CP_DEFAULT));
for (i = 1; i < width -1; i++)
{
move(by + height - 2, bx + i); addch(' ');
}
move(by+height-2, bx+1);
echo();
leaveok(stdscr, FALSE);
curs_set(1);
refresh();
#if 0
text = new char[MIN(width-2,maxlen)+1];
getnstr(text, MIN(width-2,maxlen));
#else
mw_settxt("Use up and down arrows to toggle insert mode");
get_input(stdscr, init_str, width - 2, maxlen, by + height -2,
bx + 1, func, func_args);
return;
/*
set_inout_opts();
*/
#endif
}
/* TODO: IIRC want_input is always true, so this part is never called */
#if 0
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
bkgdset(COLOR_PAIR(CP_DEFAULT));
popup = 0;
refresh_screen();
return text;
#endif
}
//playlist functions
struct ptag_t
parse_playlist_token(const char *str)
{
struct ptag_t restag, badtag;
short status = 0;
/* meaning of status (state in state machine):
* 0: scanning the tagname
* 1: scanning whitespace (searching for keyword)
* 2: scanning keywordname
* 3: scanning '=' separating keyword/value
* 4: scanning '"' that starts value
* 5: scanning value
*/
short tagcnt = 0;
short current_keyword = 0;
short current_value = 0;
char token, last_token;
short i;
char *mystr = strdup(str);
restag.tag[0] = badtag.tag[0] = '\0';
for (i = 0; i < 5; i++)
{
restag.keywords[i][0] = badtag.keywords[i][0] = '\0';
restag.values[i][0] = badtag.values[i][0] = '\0';
}
if (!mystr || !strlen(mystr) || strlen(mystr) < 2 || mystr[0] != '<' ||
mystr[strlen(mystr)-1] != '>')
return badtag;
last_token = '\0';
mystr[strlen(mystr)-1] = '\0';
while(*(++mystr))
{
token = *mystr;
if (!status) //still completing keyword
{
if (token == ' ' || token == '\t')
{
status++; //look for keywords now
restag.tag[tagcnt] = '\0';
tagcnt = 0;
last_token = token;
continue;
}
last_token = token;
if (tagcnt > 78) //just chop off too long tagnames
continue;
restag.tag[tagcnt++] = token;
}
else if (status == 1 || status == 2) //looking for the start o/a keyword
{
if (status == 1)
{
if (token == ' ' || token == '\t')
continue;
else
status = 2; //found keyword
}
if (token == ' ' || token == '\t' || token == '=') //end of keyword
{
restag.keywords[current_keyword][tagcnt] = '\0';
tagcnt = 0;
current_keyword++;
last_token = token;
tagcnt = 0;
if (token == '=')
status = 4;
else
status = 3;
continue;
}
last_token = token;
if (tagcnt > 78)
continue;
restag.keywords[current_keyword][tagcnt++] = token;
}
else if (status == 3) //searching for =
{
if (token == '=')
{
last_token = token;
status = 4;
}
else
{
last_token = token;
}
}
else if (status == 4) //looking for " starting value;
{
if (token != '"')
{
last_token = token;
continue;
}
else
{
status = 5; //start of value
last_token = token;
}
}
else if (status == 5) //scanning value
{
if (token == '"' && last_token != '\\')
{
last_token = token;
status = 1; //start looking for keyword again
restag.values[current_value][tagcnt] = '\0';
current_value++;
tagcnt = 0;
continue;
}
if (tagcnt > 78)
{
last_token = token;
continue;
}
if (token != '\\' || last_token == '\\')
restag.values[current_value][tagcnt++] = token;
last_token = token;
}
}
if (!status) //a tag with just a tagname is valid too ofcourse
{
restag.tag[tagcnt] = '\0';
status = 1;
}
if (status != 1) //not good.
return badtag;
debug("Scanning done\n");
{
short i;
debug("tag[tag]: \"%s\"\n", restag.tag);
for (i = 0; i < 5; i++)
{
if (restag.keywords[i][0])
{
debug("tag.keywords[%d] = \"%s\"\n", i, restag.keywords[i]);
}
if (restag.values[i][0])
{
debug("tag.values[%d] = \"%s\"\n", i, restag.values[i]);
}
}
}
return restag;
}
/* Function : tag_keyword_value
* Description: Searches for the presence of a keyword in a HTML-like tag
* : and returns the value if it's present. If the keyword is not
* : found, NULL is returned. Searches are case-insensitive.
* Parameters : tag: the HTML-like tag, e.g. generated by parse_playlist_token
* Returns : See description.
* SideEffects: None.
* Example : If the tag is something like <GROUP TYPE="shuffle" MODE="0">
* : and the requested keyword is "type", "shuffle" will be
* : returned.
*/
const char*
tag_keyword_value(const struct ptag_t * tag, const char *kword)
{
int i;
for (i = 0; i < 5; i++)
{
if (!strcasecmp(tag->keywords[i], kword))
{
return tag->values[i];
}
}
return NULL;
}
/* Function : read_playlist
* Description: Reads a playlist from a file and adds the songs to the playlist
* Parameters : filename:
* : Path to the playlist
* Returns : If succesful 1, else 0.
* SideEffects: None.
* TODO : Adding groups seems a bit buggy to me. If sigsegv's happen,
* : this is probably the place to look for them.
*/
short
read_playlist(const char *filename)
{
FILE
*f;
char
*tmpline, *line;
const char
*extension;
mp3Win
*current_group = mp3_curwin;
int
is_m3u = 0;
f = NULL;
line = tmpline = NULL;
if (!filename || !(f = fopen(filename, "r")))
return 0;
if (strlen(filename)>=4)
{
extension = filename + (strlen(filename)-4);
if (strcmp(".m3u", extension) == 0)
is_m3u = 1;
}
while ( (tmpline = readline(f)) )
{
line = crop_whitespace((const char *)tmpline);
free(tmpline);
if (!strlen(line))
{
free(line);
continue;
}
if (is_m3u && strncmp(line, "#EXT", 4)==0)
{
free(line);
continue;
}
if (line[0] == '<')
{
struct ptag_t ptag = parse_playlist_token((const char *)line);
const char *keyval;
if (!strcasecmp(ptag.tag, "global"))
{
keyval = tag_keyword_value(&ptag, "playmode");
if (keyval)
{
set_play_mode(keyval);
debug("Set playmode from playlist file\n");
}
keyval = tag_keyword_value(&ptag, "mode");
if (keyval && !strcasecmp(keyval, "shuffle"))
{
mp3_rootwin->setPlaymode(1);
debug("Shufflemode on!\n");
}
}
else if (!strcasecmp(ptag.tag, "group"))
{
const char *group_name = tag_keyword_value(&ptag, "name");
const char *group_mode = NULL;
if (group_name)
{
char *gname = new char[strlen(group_name) + 3];
memset(gname, '\0', (strlen(group_name) + 3) * sizeof(char));
strcat(gname, "[");
strncat(gname, group_name, 48);
strcat(gname, "]");
debug("GroupName: %s\n",gname);
mp3Win *new_group = newgroup();
if (!new_group)
continue;
new_group->setTitle(gname);
if ( (group_mode = tag_keyword_value(&ptag, "mode")) &&
!strcasecmp(group_mode, "shuffle"))
new_group->setPlaymode(1);
current_group->addGroup(new_group, gname);
current_group = new_group;
}
}
else if (!strcasecmp(ptag.tag, "/group"))
{
mp3Win *prev_group = current_group->getGroup(0); //should be [..]
if (prev_group)
current_group = prev_group;
}
free(line);
continue;
}
const char *bla[4];
short foo[2] = { 0, 1 };
short idx;
if (!strchr(line, '/'))
{
char *currentPath = get_current_working_path();
currentPath = (char*)realloc(currentPath, (strlen(currentPath) +
strlen(line) + 1) * sizeof(char));
strcat(currentPath, line);
free(line);
line = currentPath;
//file without a path in the playlist dir
}
bla[0] = (const char*)line;
bla[1] = chop_path((const char *)line);
bla[2] = NULL;
bla[3] = NULL;
idx=current_group->addItem(bla, foo, CP_FILE_MP3);
if (idx!=-1)
current_group->changeItem(idx,&id3_filename,strdup(bla[0]),2);
free(line);
}
//refresh_screen();
return 1;
}
#if 0
FILE
*f;
char
*name;
if (!filename)
name = gettext("Enter filename:", 1);
else
{
name = (char*)malloc((strlen(filename) + 1) * sizeof(char));
strcpy(name, filename);
}
/*append startup path if filename does not start with a slash */
if (strlen(name) && name[0] != '/')
{
char *newname = (char*)malloc(strlen(startup_path) +
strlen(name) + 1);
strcpy(newname, startup_path);
strcat(newname, name);
free(name);
name = newname;
}
f = fopen(name, "r");
free(name);
if (!f)
{
warning("Couldn't open playlist-file.");
refresh_screen();
return;
}
short header_ok = 1;
/* read global playmode from file */
{
char
*line = readline(f);
playmode
old_mode = globalopts.play_mode;
if (!line)
{
fclose(f); return;
}
char tmp[strlen(line)+1];
if ( sscanf(line, "GLOBALMODE: %[^\n]", tmp) == 1)
{
short dum = set_play_mode((const char *)tmp);
if (!dum)
header_ok = 0;
}
else
header_ok = 0;
if (header_ok && old_mode != globalopts.play_mode)
cw_draw_play_mode();
free(line);
}
if (header_ok)
{
while (read_group_from_file(f))
;
cw_draw_group_mode();
}
else
{
warning("No global playmode in playlist found.");
refresh_screen();
}
fclose(f);
//mw_settxt("Added playlist!");
#endif
short
write_group(mp3Win *group, FILE *f, short indent_level)
{
int nitems = group->getNitems();
int itemcount = 0;
char *indent_string = new char[indent_level + 1];
if (indent_level > 10)
return 0;
memset(indent_string, '\t', indent_level);
indent_string[indent_level] = '\0';
while (itemcount < nitems)
{
char *itemname = NULL;
const char *tmpname = NULL;
if (group->isGroup(itemcount))
{
if (strcmp(group->getItem(itemcount), "[..]")) //skip [..] !!!
{
mp3Win *new_group = group->getGroup(itemcount);
const char *tmp_gname1 = new_group->getTitle();
char *tmp_gname = strdup(tmp_gname1);
tmp_gname[strlen(tmp_gname)-1] = '\0'; //chop off ']';
char *gname = new char[strlen(indent_string) + strlen(tmp_gname) + 20];
//TODO: escape "'s in groupname
sprintf(gname, "%s<GROUP NAME=\"%s\"%s>\n", indent_string,
(const char *)(tmp_gname + 1), (new_group->getPlaymode() ? " MODE=\"shuffle\"" : ""));
debug(gname);
free(tmp_gname);
if (fwrite(gname, sizeof(char), strlen(gname), f) < strlen(gname) *
sizeof(char))
{
debug("Short write!\n");
delete[] gname;
return 0;
}
delete[] gname; gname = NULL;
write_group(new_group, f, indent_level + 1);
gname = new char[strlen(indent_string) + 10];
sprintf(gname, "%s</GROUP>\n", indent_string);
if (fwrite(gname, sizeof(char), strlen(gname), f) < strlen(gname) *
sizeof(char))
{
delete[] gname;
return 0;
}
delete[] gname;
}
}
else
{
tmpname = group->getItem(itemcount);
itemname = new char[strlen(indent_string) + strlen(tmpname) + 2];
sprintf(itemname, "%s%s\n", indent_string, tmpname);
if (fwrite(itemname, sizeof(char), strlen(itemname), f) <
strlen(itemname) * sizeof(char))
{
delete[] itemname;
return 0;
}
delete[] itemname;
}
itemcount++;
}
return 1;
}
short
write_playlist(const char *filename)
{
mp3Win *current_group = mp3_rootwin;
FILE *f = fopen(filename, "w");
const char *pmstr = NULL;
char globalstr[255];
if (!f)
return 0;
switch(globalopts.play_mode)
{
case PLAY_GROUP: pmstr = "onegroup"; break;
case PLAY_GROUPS: pmstr = "allgroups"; break;
//case PLAY_GROUPS_RANDOMLY: pmstr = "groupsrandom"; break;
case PLAY_SONGS: pmstr = "allrandom"; break;
default: pmstr = "unknown";
}
//write global stuff
sprintf(globalstr, "<GLOBAL PLAYMODE=\"%s\"%s>\n", pmstr,
(mp3_rootwin->getPlaymode() ? " MODE=\"shuffle\"" : ""));
if (fwrite(globalstr, sizeof(char), strlen(globalstr), f) !=
strlen(globalstr) * sizeof(char))
{
fclose(f);
return 0;
}
if (!write_group(current_group, f, 0))
{
fclose(f);
return 0;
}
fclose(f);
return 1;
}
#if 0
char *name = NULL;
name = gettext("Enter filename", 1);
/*append startup path if filename does not start with a slash */
if (strlen(name) && name[0] != '/')
{
char *newname = (char*)malloc(strlen(startup_path) +
strlen(name) + 1);
strcpy(newname, startup_path);
strcat(newname, name);
free(name);
name = newname;
//fprintf(stderr, "New path: %s\n", name);
}
if (!(group_stack->writePlaylist(name, globalopts.play_mode)))
Error("Error writing playlist!");
#if 0
f = fopen(name, "w");
free(name);
if (!f)
{
Error("Error opening playlist for writing!");
return;
}
for (i = 0; i < group_window->getNitems(); i++)
{
if ( !((group_stack->entry(i))->writeToFile(f)) ||
!(fwrite("\n", sizeof(char), 1, f)))
{
Error("Error writing playlist!");
fclose(f);
return;
}
}
fclose(f);
#endif
#endif
void
cw_toggle_group_mode()
{
mp3Win
*sw = mp3_curwin;
sw->setPlaymode(1 - sw->getPlaymode());
cw_draw_group_mode();
refresh();
}
/* draws (or clears) group mode. Does *not* refresh screen */
void
cw_draw_group_mode()
{
mp3Win
*sw = mp3_curwin;
int r = COLS - 12;
move(11, r+1);
if (!sw->getPlaymode())
addch(' ');
else
addch('X');
}
/* set playmode */
short
set_play_mode(const char *pm)
{
if (!pm)
return 0;
if (!strcasecmp(pm, "onegroup"))
globalopts.play_mode = PLAY_GROUP;
else if (!strcasecmp(pm, "allgroups"))
globalopts.play_mode = PLAY_GROUPS;
//else if (!strcasecmp(pm, "groupsrandom"))
// globalopts.play_mode = PLAY_GROUPS_RANDOMLY;
else if (!strcasecmp(pm, "allrandom"))
globalopts.play_mode = PLAY_SONGS;
else
return 0;
return 1;
}
/* toggle & redraw playmode */
void
cw_toggle_play_mode()
{
switch(globalopts.play_mode)
{
case PLAY_GROUPS: globalopts.play_mode = PLAY_GROUP; break;
case PLAY_GROUP: globalopts.play_mode = PLAY_SONGS; break;
case PLAY_SONGS: globalopts.play_mode = PLAY_GROUPS; break;
default: globalopts.play_mode = PLAY_GROUPS;
}
cw_draw_play_mode();
refresh();
}
/* redraw playmode, but does not update the screen. */
void
cw_draw_play_mode(short cleanit)
{
unsigned int i;
const char *playmodes_desc[] = {
/* PLAY_NONE */
"",
// PLAY_GROUP
"Global Playback: Play current group, but not its subgroups",
//PLAY_GROUPS
"Global Playback: Play current group, including subgroups",
//PLAY_GROUPS_RANDOMLY
"Global Playback: You Should Not See This",
//PLAY_SONGS
"Global Playback: Shuffle all songs from all groups",
};
unsigned int nrchars = (COLS > 16 ? COLS - 16 : 1);
for (i = 2; i < nrchars + 2; i++)
{
move(6, i);
addch(' ');
}
if (!cleanit)
{
move(6, 2);
addstr(playmodes_desc[globalopts.play_mode]);
}
}
void *
play_list(void *arg)
{
(void)arg; //unused
#ifdef PTHREADEDMPEG
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL),
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
#endif
//this loops controls the 'playing' variable. The 'action' variable is
//controlled by the input thread.
//playing=1: user wants to play
//playing=0: user doesn't want to play
while (1)
{
_action my_action;
#ifdef PTHREADEDMPEG
pthread_testcancel();
#elif defined(LIBPTH)
pth_cancel_point();
#endif
lock_playing_mutex();
if (!playopts.playing)
{
debug("Not playing, waiting to start playlist\n");
while (action != AC_PLAY && action != AC_ONE_MP3)
{
debug("THREAD playing going to sleep.\n");
#ifdef PTHREADEDMPEG
pthread_cond_wait(&playing_cond, &playing_mutex);
#elif defined(LIBPTH)
pth_cond_await(&playing_cond, &playing_mutex, NULL);
#endif
if (playopts.quit_program)
{
break; //we need to die.
}
}
debug("THREAD playing woke up!\n");
}
/* are we requested to die? */
if (playopts.quit_program)
{
if (playopts.player)
{
//clean up the player
debug("destroying player - we're quitting\n");
playopts.player->stop();
delete playopts.player;
debug("Destroyed!\n");
}
//exit loop and terminate thread
break;
}
my_action = action;
playopts.playing = 1;
unlock_playing_mutex();
switch(my_action)
{
case AC_STOP:
case AC_STOP_LIST:
/* explicit user stop request */
stop_song(); //sets action to AC_NONE
lock_playing_mutex();
playopts.playing = 0;
unlock_playing_mutex();
update_songinfo(PS_NONE, 16);
if (my_action == AC_STOP_LIST)
reset_playlist(1);
playopts.playing_one_mp3 = 0;
break;
case AC_NEXT:
stop_song(); //status => AC_NONE
/*29-10-2000: This isn't really necessary..the next loop will fix this.
if (!start_song())
stop_list();
*/
break;
case AC_PREV: //previous song
if (!history->atStart())
{
/* skip 2 places in history; once to skip current song, once more to
* jump to song before current one
*/
history->previous();
history->previous();
debug ("Not at end of history, selecting older song.\n");
stop_song(); //status => AC_NONE
}
//mw_settxt("Not implemented (yet).");
break;
case AC_NONE:
{
if (!playopts.pause && playopts.song_played) {
bool mystatus = (playopts.player)->run(globalopts.fpl);
if (!mystatus) {
if ((playopts.player)->ready()) {
/* ugly. If run returns false, all playback should be finished! */
stop_song(); //status => AC_NONE
} else {
//output buffer full, wait a bit.
// USLEEP(10000);
}
} else {
/* TODO: different thread for status updates. Now, this function
* gets called way too often when buffering. It does prevent
* updating the actual display too often, but that check is
* costly as well..
*/
update_play_display();
// USLEEP(10000);
}
}
if (!playopts.song_played) //let's start one then
{
short
start_song_result;
debug("No song to play, let's find one!\n");
start_song_result = start_song();
if (start_song_result == -1) //nothing more to play
{
short allow_repeat = playopts.allow_repeat;
debug("End of playlist reached.\nI want to repeat: %d\n" \
"Am I allowed to?: %d\n", globalopts.repeat, allow_repeat);
stop_list();
//end the program?
if (quit_after_playlist)
end_program(); //this is *so* not thread-safe!
if (allow_repeat && globalopts.repeat)
{
debug("I may repeat the playlist.\n");
reset_playlist(1);
lock_playing_mutex();
playopts.playing = 1;
unlock_playing_mutex();
}
}
}
else if (playopts.pause)
USLEEP(10000);
}
break;
case AC_ONE_MP3: //plays 1 mp3, can interrupt a playlist
{
short was_playing = 0;
if (playopts.song_played)
{
stop_song(); //action => AC_NONE
if (!playopts.playing_one_mp3 || playopts.playing_one_mp3 == 1)
was_playing = 1;
}
playopts.playing_one_mp3 = 0;
start_song(was_playing); //(fails: action => AC_NONE, plist continues)
}
break;
case AC_FORWARD:
if (playopts.song_played && playopts.player)
{
(playopts.player)->forward(globalopts.skipframes);
update_play_display();
}
set_action(AC_NONE);
break;
case AC_REWIND:
if (playopts.song_played && playopts.player)
{
(playopts.player)->rewind(globalopts.skipframes);
update_play_display();
}
set_action(AC_NONE);
break;
case AC_SKIPEND:
if (playopts.song_played)
{
(playopts.player)->forward(-300);
update_play_display();
}
set_action(AC_NONE);
break;
case AC_PLAY: //pause,unpause,na stop
set_action(AC_NONE);
if (playopts.song_played && playopts.pause)
{
playopts.pause = 0;
update_songinfo(PS_PLAY, 16|4);
if (!((playopts.player)->unpause()))
stop_song();
}
else if (playopts.song_played && !playopts.pause)
{
playopts.pause = 1;
update_songinfo(PS_PAUSE, 16|4);
(playopts.player)->pause();
}
else if (!playopts.song_played)
{
playopts.allow_repeat = 0;
debug("AC_PLAY: allow_repeat = 0\n");
}
break;
case AC_NEXTGRP:
if ((globalopts.play_mode == PLAY_GROUP || globalopts.play_mode ==
PLAY_GROUPS) && playopts.song_played && playing_group)
{
stop_song();
playing_group->setAllSongsPlayed();
}
break;
case AC_PREVGRP:
mw_settxt("Sorry, that doesn't work yet.");
break;
}
#ifdef LIBPTH
pth_yield(NULL);
#endif
}
return NULL;
}
#ifdef INCLUDE_LIRC
void *
lirc_loop(void *arg)
{
struct lirc_config *config;
if (arg) {}
if (lirc_init("mp3blaster", 1) == -1)
{
LOCK_NCURSES;
warning("Couldn't initialize LIRC support (no config?)");
UNLOCK_NCURSES;
return NULL;
}
if (lirc_readconfig(NULL, &config, NULL) == 0)
{
char *code;
char *name;
int ret;
while (lirc_nextcode(&code) == 0 && code) //blocks
{
while ((ret = lirc_code2char(config, code, &name)) == 0 && name)
{
if (!strcasecmp(name, "PLAY"))
lirc_cmd = CMD_PLAY_PLAY;
else if (!strcasecmp(name, "NEXT"))
lirc_cmd = CMD_PLAY_NEXT;
else if (!strcasecmp(name, "PREVIOUS"))
lirc_cmd = CMD_PLAY_PREVIOUS;
else if (!strcasecmp(name, "REWIND"))
lirc_cmd = CMD_PLAY_REWIND;
else if (!strcasecmp(name, "FORWARD"))
lirc_cmd = CMD_PLAY_FORWARD;
else if (!strcasecmp(name, "STOP"))
lirc_cmd = CMD_PLAY_STOP;
else if (!strcasecmp(name, "UP"))
lirc_cmd = CMD_UP;
else if (!strcasecmp(name, "DOWN"))
lirc_cmd = CMD_DOWN;
else if (!strcasecmp(name, "PLAYMODE"))
lirc_cmd = CMD_TOGGLE_PLAYMODE;
else if (!strcasecmp(name, "ENTER"))
{
if (progmode == PM_NORMAL)
lirc_cmd = CMD_ENTER;
else if (progmode == PM_FILESELECTION)
lirc_cmd = CMD_FILE_ENTER;
}
else if (!strcasecmp(name, "FILES"))
{
if (progmode == PM_NORMAL)
lirc_cmd = CMD_SELECT_FILES;
else if (progmode == PM_FILESELECTION)
lirc_cmd = CMD_FILE_ADD_FILES;
}
else if (!strcasecmp(name, "SELECT"))
{
if (progmode == PM_NORMAL)
lirc_cmd = CMD_SELECT;
else if (progmode == PM_FILESELECTION)
lirc_cmd = CMD_FILE_SELECT;
}
else if (!strcasecmp(name, "QUIT"))
lirc_cmd = CMD_QUIT_PROGRAM;
}
free(code);
if (ret == -1)
break;
}
lirc_freeconfig(config);
}
lirc_deinit();
return NULL;
}
#endif
void
update_playlist_history()
{
//TODO: implement ;-)
}
void
update_play_display()
{
//update elapsed / remaining time?
if (!playopts.player)
return;
time(&(playopts.newtyd));
if (difftime(playopts.newtyd, playopts.tyd) >= 1)
{
if (LOCK_NCURSES)
{
songinf.elapsed = (playopts.player)->elapsed_time();
songinf.remain = (playopts.player)->remaining_time();
songinf.update |= 2;
UPDATE_CURSES;
UNLOCK_NCURSES;
}
playopts.tyd = playopts.newtyd;
}
}
void
set_one_mp3(const char *mp3)
{
#ifdef PTHREADEDMPEG
pthread_mutex_lock(&onemp3_mutex);
#elif defined(LIBPTH)
pth_mutex_acquire(&onemp3_mutex, FALSE, NULL);
#endif
if (playopts.one_mp3)
delete[] playopts.one_mp3;
if (mp3)
{
playopts.one_mp3 = new char[strlen(mp3)+1];
strcpy(playopts.one_mp3, mp3);
}
else
playopts.one_mp3 = NULL;
#ifdef PTHREADEDMPEG
pthread_mutex_unlock(&onemp3_mutex);
#elif defined(LIBPTH)
pth_mutex_release(&onemp3_mutex);
#endif
}
/* POST: Delete[] returned char*-pointer when you don't use it anymore!! */
/* Function : get_one_mp3
* Description: Returns a char*-pointer with a filename of a single MP3,
* : if the user chose to play a single mp3 outside the usual
* : playlist (which happens when you hit enter over an mp3)
* Parameters : None.
* Returns : A C++ allocated char*-pointer (delete[] after use), or NULL
* : if no single mp3 was requested.
* SideEffects: None.
*/
char *
get_one_mp3()
{
char *newmp3 = NULL;
if (!playopts.one_mp3)
return NULL;
newmp3 = new char[strlen(playopts.one_mp3)+1];
strcpy(newmp3,playopts.one_mp3);
return newmp3;
}
void
stop_list()
{
playopts.playing = 0;
playopts.playing_one_mp3 = 0;
debug("stop_list() : allow_repeat = 0\n");
playopts.allow_repeat = 0;
next_song = -1;
reset_playlist(1);
if (LOCK_NCURSES)
{
songinf.update |= 32;
UPDATE_CURSES;
UNLOCK_NCURSES;
}
}
const char *
get_error_string(int errcode)
{
const char *sound_errors[21] =
{
"Everything's OK (You shouldnt't see this!)",
"Failed to open sound device.",
"Sound device is busy.",
"Buffersize of sound device is wrong.",
"Sound device control error.",
"Failed to open file for reading.", // 5
"Failed to read file.",
"Unkown proxy.",
"Unkown host.",
"Socket error.",
"Connection failed.", // 10
"Fdopen error.",
"Http request failed.",
"Http write failed.",
"Too many relocations",
"Out of memory.", // 15
"Unexpected EOF.",
"Bad sound file format.",
"Can't make thread.",
"Bad mpeg header.",
"Unknown error."
};
if (errcode < 0 || errcode > 20)
return "";
return sound_errors[errcode];
}
void
stop_song()
{
int vaut;
if (playopts.player)
{
(playopts.player)->stop();
playopts.pause = 0;
vaut = (playopts.player)->geterrorcode();
}
else
{
vaut = SOUND_ERROR_FINISH;
debug("stop_song(): no playopts.player\n");
}
if (vaut == SOUND_ERROR_OK || vaut == SOUND_ERROR_FINISH)
{
debug("Allowing playlist repeat\n");
playopts.allow_repeat = 1;
}
else
{
debug("stop_song() ended in error (allow_repeat = %d)\n",
playopts.allow_repeat);
}
//generate warning
if (LOCK_NCURSES)
{
songinf.update = 0;
if (songinf.warning)
{
delete[] songinf.warning;
songinf.warning = NULL;
}
if (vaut != SOUND_ERROR_OK && vaut != SOUND_ERROR_FINISH)
{
songinf.warning = new char[strlen(get_error_string(vaut))+1];
strcpy(songinf.warning, get_error_string(vaut));
songinf.update |= 8;
}
songinf.status = PS_STOP;
songinf.update |= 4;
UPDATE_CURSES;
UNLOCK_NCURSES;
}
if (playopts.player)
{
delete playopts.player;
playopts.player = NULL;
}
playopts.song_played = 0;
//playopts.playing_one_mp3 = 0; (DON'T!)
//set_one_mp3((const char *)NULL); (DON'T!!)
set_action(AC_NONE);
if (vaut == SOUND_ERROR_DEVOPENFAIL)
stop_list();
}
/* TODO: Implement history
* PRE: This function can only be called from the playing thread, otherwise
* : we'll enter a deadlock.
* was_playing: if one mp3 is to be played, this determines if the playlist
* continues playing after the one mp3 (is was_playing = 1, it
* will)
* POST: playopts.one_mp3 is always NULL
*
* Returns: >0 on success, <=0 on failure:
* 0: general error
* -1: could not find song to play
*/
short
start_song(short was_playing)
{
char *song = NULL;
short one_mp3 = 0;
const char *sp = NULL;
debug("start_song()\n");
/* If there's no standalone mp3 to be played, and a previous standalone mp3
* has just been played while there was no playlist playing, return without
* choosing a new song.
*/
if (!(song = get_one_mp3()) && playopts.playing_one_mp3 == 2)
{
debug("Not going to play a new song.\n");
playopts.playing_one_mp3 = 0;
return -1;
}
if (!song)
{
/* No request to play a standalone mp3 (A standalone mp3 is one that is
* being started by hitting enter over a song in file- or playlist
* manager; it can interrupt playlists)
*/
/* If we're in the playlist history, advance one position in the history */
/*
if (!history->atEnd())
{
debug("Advancing one entry in history\n");
history->next();
}
*/
/* If we're still in the playlist history, play a song from it. */
if (!history->atEnd())
{
debug("Going to play song from history\n");
const char *tmpsong = history->element();
if (tmpsong)
{
song = new char[strlen(tmpsong) + 1];
strcpy(song, tmpsong);
debug("Select song from history (%s)\n", song);
}
history->next();
}
else //regular playlist
{
if ((song = determine_song()))
history->add(song); //add to history.
}
if (!song)
debug("Could not determine song in start_song()\n");
playopts.playing_one_mp3 = 0;
if (!song)
return -1;
}
else //one_mp3
{
playopts.playing_one_mp3 = (was_playing ? 1 : 2);
one_mp3 = 1;
//let's not add standalone mp3's (those not in playlist) to the history.
//it would be too confusing.
//history->add(song);
}
set_one_mp3((const char*)NULL);
void *init_args = NULL;
Fileplayer::audiodriver_t audiodriver = Fileplayer::AUDIODRV_SDL;
switch(globalopts.audio_driver)
{
case AUDIODEV_SDL: audiodriver = Fileplayer::AUDIODRV_SDL; break;
case AUDIODEV_OSS: audiodriver = Fileplayer::AUDIODRV_OSS; break;
case AUDIODEV_ESD: audiodriver = Fileplayer::AUDIODRV_ESD; break;
case AUDIODEV_NAS: audiodriver = Fileplayer::AUDIODRV_NAS; break;
//TODO: 'dump' driver which dumps output to a file! Better than current
//crappy mp3->wav converter class..
}
if (is_wav(song))
playopts.player = new Wavefileplayer(audiodriver);
#ifdef HAVE_SIDPLAYER
else if (is_sid(song))
playopts.player = new SIDfileplayer(audiodriver);
#endif
#ifdef INCLUDE_OGG
else if (is_ogg(song))
playopts.player = new Oggplayer(audiodriver);
#endif
else if (is_audiofile(song)) //mp3 assumed
{
struct init_opts *opts = new init_opts;
memset(opts->option, 0, 30);
#ifdef PTHREADEDMPEG
int draadjes = globalopts.threads;
if (draadjes)
add_init_opt(opts, "threads", (void*)&(globalopts.threads));
#endif
add_init_opt(opts, "scanmp3s", (void*)&(globalopts.scan_mp3s));
//init options have been set?
if (opts->option[0])
init_args = (void *)opts;
playopts.player = new Mpegfileplayer(audiodriver);
}
if (!playopts.player)
{
debug("start_song: No player object could be created!\n");
stop_song();
delete[] song;
return 0;
}
if (!(playopts.player)->openfile(song, (globalopts.sound_device ?
globalopts.sound_device : NULL)))
{
debug("start_song: openfile failed\n");
//TODO: add warning here (geterrorcode())
warning("Couldn't open file.");
stop_song();
delete[] song;
return 0;
}
if (globalopts.downsample)
(playopts.player)->setdownfrequency(1);
if (globalopts.eightbits)
(playopts.player)->set8bitmode();
if ( !(playopts.player)->initialize(init_args) )
{
debug("start_song: player->initialize failed\n");
stop_song();
delete[] song;
return 0;
}
playopts.song_played = 1;
sp = chop_path(song);
if (LOCK_NCURSES)
{
if (songinf.path)
delete[] songinf.path;
songinf.path = new char[strlen(sp)+1];
strcpy(songinf.path, sp);
songinf.songinfo = (playopts.player)->getsonginfo();
if (globalopts.recode_table)
{
recode_string(songinf.songinfo.artist);
recode_string(songinf.songinfo.songname);
recode_string(songinf.songinfo.album);
recode_string(songinf.songinfo.comment);
}
songinf.elapsed = 0;
songinf.remain = songinf.songinfo.totaltime;
songinf.status = PS_PLAY;
songinf.update |= (1|2|4); //update time,songinfo,status
UPDATE_CURSES;
UNLOCK_NCURSES;
}
time(&(playopts.tyd));
delete[] song;
if (!one_mp3)
update_playlist_history();
else
playopts.playing_one_mp3 = (was_playing ? 1 : 2);
set_action(AC_NONE);
return 1;
}
//Pre-condtion:
// The global playmode and group modes must be set according to the wishes
// of the user.
// if playing_one_mp3 != NULL, then only that mp3 will be played, after which
// the thread will exit.
void
play_one_mp3(const char *filename)
{
set_one_mp3(filename);
set_action(AC_ONE_MP3);
lock_playing_mutex();
wake_player();
unlock_playing_mutex();
}
/* Function : add_selected_file
* Description: Used by recsel_files() to temporarily store files until
* : the recursive process has finished.
* Parameters : file: file to add
* Returns : Nothing.
* SideEffects: adds file to selected_files array.
*/
void
add_selected_file(const char *file)
{
int
offset;
if (!file)
return;
selected_files = (char **)realloc(selected_files, (nselfiles + 1) *
sizeof(char*) );
offset = nselfiles;
selected_files[offset] = NULL;
selected_files[offset] = (char *)malloc((strlen(file) + 1) * sizeof(char));
strcpy(selected_files[offset], file);
++nselfiles;
}
/* Function : add_selected_files
* Description: Adds an array of files to the selected_files array.
* : It's much more efficient than repeatedly calling
* : add_selected_file.
* Parameters : files: array of char-pointers, each of which must be
* : allocated with malloc() or friends. Don't free them!
* : filecount: Number of pointers in 'files' array
* Returns : Nothing.
* SideEffects: enlarges selected_files array & increases nselfiles
*/
void
add_selected_files(char **files, unsigned int fileCount)
{
unsigned int i, offset;
if (!files || !fileCount)
return;
selected_files = (char **)realloc(selected_files, (nselfiles + fileCount) *
sizeof(char*) );
offset = nselfiles;
for (i = 0; i < fileCount; i++)
{
selected_files[i + offset] = files[i];
}
nselfiles += fileCount;
}
int
is_symlink(const char *path)
{
struct stat
*buf = (struct stat*)malloc( 1 * sizeof(struct stat) );
int retval = 1;
if (lstat(path, buf) < 0 || ((buf->st_mode) & S_IFMT) != S_IFLNK)
retval = 0;
/* forgot to free this. Thanks to Steve Kemp for pointing it out. */
free(buf);
return retval;
}
/* This function adds all mp3s recursively found in ``path'' to the current
* group when called by F3: recursively add mp3's.
* When called by F5: Add dirs as groups (d2g), and d2g_init is not set,
* all mp3's found in this dir will be added to a group with the name of
* the dir. When d2f is set, 'files' and 'fileCount' should be NULL.
* Only when d2g is NOT set:
* 'files' must be a pointer to a char** array, in which all
* found files will be stored. The char** array will be allocated with
* (re/m)alloc(), as will its members.
*/
void
recsel_files(const char *path, char ***files, unsigned int *fileCount, short d2g=0, int d2g_init=0)
{
struct dirent
*entry = NULL;
DIR
*dir = NULL;
short
mp3_found = 0;
mp3Win*
d2g_groupindex = NULL;
if (progmode != PM_FILESELECTION || !strcmp(path, "/dev") ||
is_symlink(path))
return;
if ( !(dir = opendir(path)) )
return;
char **dirfiles;
char **dirs = NULL;
int dirs_size = 0, dirfiles_size = 0;
if (d2g) //don't add to files array
{
dirfiles = NULL;
dirfiles_size = 0;
}
else //add to files array, so that total array can be sorted afterwards
{
dirfiles = *files;
dirfiles_size = *fileCount;
}
/* first, find all files and chuck them in arrays with direntries and
* audiofile entries
*/
while ( (entry = readdir(dir)) )
{
DIR *dir2 = NULL;
char *newpath = (char *)malloc((strlen(entry->d_name) + 2 + strlen(path)) *
sizeof(char));
PTH_YIELD;
strcpy(newpath, path);
if (path[strlen(path) - 1] != '/')
strcat(newpath, "/");
strcat(newpath, entry->d_name);
if ( (dir2 = opendir(newpath)) )
{
int i = 0;
short skip_dir = 0;
const char *dummy = entry->d_name;
const char *baddirs[] = {
".", "..",
".AppleDouble",
".resources", //HPFS stuff
".finderinfo",
NULL,
};
closedir(dir2);
while (baddirs[i])
{
if (!strcmp(dummy, baddirs[i++]))
{
skip_dir = 1;
break;
}
}
if (skip_dir)
{
free(newpath);
continue;
}
dirs = (char**)realloc(dirs, ++dirs_size * sizeof(char*));
dirs[dirs_size - 1] = newpath;
continue;
}
else if (is_audiofile(newpath))
{
dirfiles = (char**)realloc(dirfiles, ++dirfiles_size * sizeof(char*));
dirfiles[dirfiles_size -1] = newpath;
continue;
}
else
free(newpath);
}
closedir(dir);
if (dirs_size)
sort_files(dirs, dirs_size);
PTH_YIELD;
if (d2g && dirfiles_size)
sort_files(dirfiles, dirfiles_size);
if (!d2g)
{
*files = dirfiles;
*fileCount = dirfiles_size;
}
int i;
for (i = 0; i < dirs_size; i++)
{
PTH_YIELD;
recsel_files(dirs[i], files, fileCount, d2g);
free(dirs[i]);
}
free(dirs);
dirs = NULL;
if (!d2g)
return;
/* rest of code is only interesting for 'Add Dirs As Groups' */
for (i = 0; i < dirfiles_size; i++)
{
PTH_YIELD;
/* Add Dirs As Groups (not first call to recsel_files) ? */
if (!d2g_init)
{
/* create new group if no mp3's have been found for this dir. */
if (!mp3_found)
{
char const *npath = 0;
npath = strrchr(path, '/');
if (!npath)
npath = path;
else
{
if (strlen(npath) > 1)
npath = npath + 1;
else
npath = path;
}
char rnpath[strlen(npath)+3];
sprintf(rnpath, "[%s]", npath);
d2g_groupindex = newgroup();
d2g_groupindex->setTitle(rnpath);
mp3_curwin->addGroup(d2g_groupindex, rnpath);
/* If there are no entries in the current group,
* this group will be filled instead of skipping it
*/
mp3_found = 1;
}
if (d2g_groupindex)
{
const char *bla[4];
short foo[2] = { 0, 1 };
int idx;
bla[0] = (const char*)dirfiles[i];
bla[1] = chop_path(dirfiles[i]);
bla[2] = NULL;
bla[3] = NULL;
idx=d2g_groupindex->addItem(bla, foo,
CP_FILE_MP3);
if (idx!=-1)
{
d2g_groupindex->changeItem(idx,&id3_filename,strdup(bla[0]),2);
}
}
else
{
mw_settxt("Something's screwy with recsel_files");
refresh_screen();
}
}
free(dirfiles[i]);
}
free(dirfiles);
}
#ifdef PTHREADEDMPEG
void
change_threads()
{
if ( (globalopts.threads += 50) > 500)
globalopts.threads = 0;
cw_draw_threads();
}
/* draws threads. Does not refresh screen. */
void
cw_draw_threads(short cleanit)
{
int y, x;
getmaxyx(stdscr, y,x);
move(9, x - 4);
if (!cleanit)
printw("%03d", globalopts.threads);
else
addstr(" ");
}
#endif
/* returns a malloc()'d path */
char *
get_current_working_path()
{
char
*tmppwd = (char *)malloc(65 * sizeof(char));
int
size = 65;
while ( !(getcwd(tmppwd, size - 1)) )
{
size += 64;
tmppwd = (char *)realloc(tmppwd, size * sizeof(char));
}
tmppwd = (char *)realloc(tmppwd, strlen(tmppwd) + 2);
if (strcmp(tmppwd, "/")) /* pwd != "/" */
strcat(tmppwd, "/");
return tmppwd;
}
void
clean_popup()
{
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
bkgdset(COLOR_PAIR(CP_DEFAULT));
popup = 0;
mw_settxt("");
refresh_screen();
}
/* Return the command corresponding to the given keycode */
command_t
get_command(int kcode, unsigned short pm)
{
int i = 0;
command_t cmd = CMD_NONE;
struct keybind_t k = keys[i];
while (k.cmd != CMD_NONE)
{
if (k.key == kcode && (pm & k.pm))
{
cmd = k.cmd;
break;
}
k = keys[++i];
}
return cmd;
}
/* handle_input reads input.
* If no_delay = 0, then this function will block until input arrives.
* Returns -1 when the program should finish, 0 or greater when everything's
* okay.
*/
int
handle_input(short no_delay)
{
int
key = 0,
retval = 0;
fd_set fdsr;
struct timeval tv;
command_t cmd;
mp3Win
*sw = mp3_curwin;
fileManager
*fm = file_window;
//get a pointer to the main window, which depends on program mode.
scrollWin *mainwindow = getMainWindow();
#ifdef LIBPTH
pth_yield(NULL);
#endif
if (no_delay)
{
fflush(stdin);
FD_ZERO(&fdsr);
FD_SET(0, &fdsr); /* stdin file descriptor */
tv.tv_sec = tv.tv_usec = 0;
#ifdef INCLUDE_LIRC
if (lirc_cmd == CMD_NONE) {
if (select(FD_SETSIZE, &fdsr, NULL, NULL, &tv) == 1)
key = getch();
else
{
key = ERR;
}
}
#else
if (select(FD_SETSIZE, &fdsr, NULL, NULL, &tv) == 1)
key = getch();
else
{
key = ERR;
}
#endif
}
else
{
debug("THREAD main waiting for blocking input.\n");
nodelay(stdscr, FALSE);
#ifdef INCLUDE_LIRC
if (lirc_cmd == CMD_NONE) key = getch();
#else
key = getch();
#endif
debug("THREAD main woke up!\n");
}
if (LOCK_NCURSES);
if (songinf.update)
{
update_status_file();
update_ncurses(0);
}
UNLOCK_NCURSES;
if (no_delay && key == ERR)
{
#ifdef LIBPTH
pth_yield(NULL);
USLEEP(10000);
#else
USLEEP(1000);
#endif
return 0;
}
if (key == KEY_RESIZE) {
handle_resize(0);
return retval;
}
#ifdef INCLUDE_LIRC
if (lirc_cmd != CMD_NONE)
{
cmd = lirc_cmd;
lirc_cmd = CMD_NONE;
}
else
{
cmd = get_command(key, progmode);
}
#else
cmd = get_command(key, progmode);
#endif
if (input_mode == IM_SEARCH)
{
/* the user is searching for something */
if (
(key >= 'a' && key <= 'z') ||
(key >= 'A' && key <= 'Z') ||
(key >= '0' && key <= '9') ||
strchr(" (){}[]<>,.?;:\"'=+-_!@#$%^&*", key)
)
{
fw_search_next_char(key);
}
else if (key == 13)
{
fw_end_search();
input_mode = IM_DEFAULT;
}
retval = 1;
}
else if (input_mode == IM_INPUT)
{
/* The user is entering text into a text input field. */
//TODO: make keys configurable.
switch(key)
{
case KEY_LEFT:
global_input->moveLeft(1);
break;
case KEY_RIGHT:
global_input->moveRight(1);
break;
case KEY_UP:
case KEY_DOWN:
{
short insmode = global_input->setInsertMode(global_input->getInsertMode() ^ 1);
//insert_mode = 1;
//TODO: This should be put in a more appriopriate place.
mw_settxt((insmode ? "-- INSERT --" : "-- REPLACE --"));
global_input->updateCursor();
break;
}
case KEY_BACKSPACE:
global_input->deleteChar();
break;
case KEY_DC:
global_input->deleteChar(0);
break;
case KEY_HOME: global_input->moveToBegin(); break;
case KEY_END: global_input->moveToEnd(); break;
case 13:
{
void (*callback_fun)(const char *, void*);
//global_input->getCallbackFunction(&callback_fun);
clean_popup();
set_inout_opts();
callback_fun = global_input->getCallbackFunction();
callback_fun(global_input->getString(), global_input->getCallbackData());
delete global_input;
global_input = NULL;
input_mode = IM_DEFAULT;
mainwindow->swRefresh(1);
}
break;
default:
global_input->addChar(key);
}
if (global_input)
{
global_input->updateCursor();
}
//set_inout_opts();
retval = 1;
}
if (retval)
{
return retval;
}
switch(cmd)
{
case CMD_PLAY_PLAY: set_action(AC_PLAY); wake_player(); break;
case CMD_PLAY_NEXT: set_action(AC_NEXT); break;
case CMD_PLAY_PREVIOUS: set_action(AC_PREV); break;
case CMD_PLAY_FORWARD: set_action(AC_FORWARD); break;
case CMD_PLAY_REWIND: set_action(AC_REWIND); break;
//case CMD_PLAY_STOP: set_action(AC_STOP); break;
case CMD_PLAY_STOP: set_action(AC_STOP_LIST); break;
case CMD_PLAY_SKIPEND: set_action(AC_SKIPEND); break;
case CMD_PLAY_NEXTGROUP: set_action(AC_NEXTGRP); break;
case CMD_PLAY_PREVGROUP: set_action(AC_PREVGRP); break;
//case 'i': sw->setDisplayMode(0); sw->swRefresh(2); break;
//case 'I': sw->setDisplayMode(1); sw->swRefresh(2); break;
case CMD_MOVE_AFTER:
sw->moveSelectedItems(sw->sw_selection);
sw->swRefresh(1);
break;
case CMD_MOVE_BEFORE:
sw->moveSelectedItems(sw->sw_selection, 1);
sw->swRefresh(1);
break;
case CMD_QUIT_PROGRAM: //quit mp3blaster
retval = -1;
break;
case CMD_HELP:
if (progmode == PM_HELP)
{
end_help();
change_program_mode(PM_NORMAL);
}
else
{
change_program_mode(PM_HELP);
show_help();
}
break;
case CMD_DOWN:
if (progmode == PM_NORMAL)
sw->changeSelection(1);
else if (progmode == PM_FILESELECTION)
fm->changeSelection(1);
else if (progmode == PM_HELP)
bighelpwin->changeSelection(1);
break;
case CMD_UP:
if (progmode == PM_NORMAL)
sw->changeSelection(-1);
else if (progmode == PM_FILESELECTION)
fm->changeSelection(-1);
else if (progmode == PM_HELP)
bighelpwin->changeSelection(-1);
break;
case CMD_DEL:
case CMD_DEL_MARK:
{
mp3Win *tmpgroup;
if (progmode != PM_NORMAL && cmd == CMD_DEL_MARK)
break;
if (sw->isPreviousGroup(sw->sw_selection))
break;
lock_playing_mutex();
//don't delete a group that's playing right now (or contains a group
//that's being played)
if (sw->isGroup(sw->sw_selection) && (tmpgroup =
sw->getGroup(sw->sw_selection)) && tmpgroup->isPlaying())
{
if (playopts.playing)
{
warning("Can't delete an active group.");
unlock_playing_mutex();
//don't delete a group that's playing right now (or contains a group
break;
}
reset_playlist(1);
mp3_groupwin = NULL;
}
/* Also mark file as bad? */
if (cmd == CMD_DEL_MARK && is_audiofile(sw->getSelectedItem()))
fw_markfilebad(sw->getSelectedItem());
sw->delItem(sw->sw_selection);
reset_next_song();
unlock_playing_mutex();
}
break;
case CMD_NEXT_PAGE:
if (progmode == PM_NORMAL)
sw->pageDown();
else if (progmode == PM_FILESELECTION)
fm->pageDown();
else if (progmode == PM_HELP)
bighelpwin->pageDown();
break;
case CMD_PREV_PAGE:
if (progmode == PM_NORMAL)
sw->pageUp();
else if (progmode == PM_FILESELECTION)
fm->pageUp();
else if (progmode == PM_HELP)
bighelpwin->pageUp();
break;
case CMD_SELECT:
if (!sw->isGroup(sw->sw_selection))
{
sw->selectItem();
sw->changeSelection(1);
}
break;
case CMD_SELECT_ITEMS:
{
const char *label[] = {
"Enter search pattern",
NULL,
};
popup_win(label, select_files_by_pattern, (void*)mainwindow, NULL, 1, 256);
break;
}
case CMD_DESELECT_ITEMS:
mainwindow->deselectItems();
mainwindow->swRefresh(0);
break;
case CMD_REFRESH: refresh_screen(); break; // C-l
case CMD_ENTER: // play 1 mp3 or dive into a subgroup.
if (sw->isGroup(sw->sw_selection))
{
/* Copy display mode */
mp3_curwin = sw->getGroup(sw->sw_selection);
mp3_curwin->setDisplayMode(sw->getDisplayMode());
mp3_curwin->resetPan();
mp3_curwin->swRefresh(2);
cw_draw_group_mode();
refresh();
}
else if (sw->getNitems() > 0)
play_one_mp3(sw->getSelectedItem());
break;
case CMD_TOGGLE_DISPLAY:
if (progmode == PM_NORMAL)
{
short dispmode = sw->getDisplayMode() + 1;
if (dispmode > 2)
dispmode = 0;
sw->setDisplayMode(dispmode);
sw->swRefresh(2);
}
else if (progmode == PM_FILESELECTION)
{
fw_toggle_display();
fm->swRefresh(2);
}
break;
case CMD_SELECT_FILES: /* file-selection */
fw_begin();
break;
case CMD_ADD_GROUP: quit_after_playlist = 0; add_group(); break; //add group
//TODO: rewrite read/write_playlist
case CMD_WRITE_PLAYLIST:
{
const char
*label[] = {
"Playlist will be saved in: ",
globalopts.playlist_dir,
"(Configuration keyword to set default dir: PlaylistDir)",
"Example filename: myplaylist.lst",
NULL
};
popup_win(label, playlist_write, NULL, NULL, 1, 40);
}
break;
case CMD_LOAD_PLAYLIST:
if (progmode == PM_NORMAL)
fw_begin();
if (globalopts.playlist_dir)
{
char *tmpStr = expand_path(globalopts.playlist_dir);
if (tmpStr)
{
fw_changedir(tmpStr);
free(tmpStr);
}
else
warning("Couldn't load playlist (wrong path?)");
}
break;
// read_playlist((const char*)NULL); break; // read playlist
// write_playlist(); break; // write playlist
case CMD_SET_GROUP_TITLE:
if (sw->isGroup(sw->sw_selection) &&
!sw->isPreviousGroup(sw->sw_selection))
{
const char *label[] = { "Enter Name:", NULL };
popup_win(label,set_group_name, NULL, NULL, 1,48);
}
break; // change groupname
case CMD_TOGGLE_REPEAT: //toggle repeat
//TODO: repeat's not thread-safe..low priority
globalopts.repeat = 1 - globalopts.repeat;
if (globalopts.repeat)
quit_after_playlist = 0;
cw_draw_repeat();
refresh();
break;
case CMD_TOGGLE_SHUFFLE: cw_toggle_group_mode(); reset_next_song(); break;
case CMD_TOGGLE_PLAYMODE:
lock_playing_mutex();
if (!playopts.playing)
{
//TODO: is this safe? What if the users STOPS a song (!playing), and
//later on continues the list? playing_group might be a dangling
//pointer then...or not, because deleting a group that is being played
//is caught in CMD_DEL. Or was there something else that could mess
//up...have to check into this.
cw_toggle_play_mode();
reset_next_song();
unlock_playing_mutex();
}
else
{
debug("Playing, can't toggle playmode\n");
//TODO: FIX WARNING to not use sleep() in a threaded env.
warning("Not possible during playback");
unlock_playing_mutex();
refresh_screen();
}
break;
case CMD_FILE_SELECT: fm->selectItem(); fm->changeSelection(1); break;
case CMD_FILE_UP_DIR: fw_changedir(".."); break;
case CMD_FILE_START_SEARCH: fw_start_search(); break;
case CMD_FILE_TOGGLE_SORT: fw_toggle_sort(); fw_changedir("."); break;
case CMD_FILE_MARK_BAD:
{
char *fullpath = NULL;
const char *halfpath = NULL;
char **selitems = NULL;
int itemcnt = 0, i = 0, moved_items_count = 0;
if (progmode != PM_FILESELECTION)
break;
debug("Mark file(s) as bad\n");
selitems = fm->getSelectedItems(&itemcnt);
if (!itemcnt)
{
selitems = new char*[1];
selitems[0] = new char[strlen(fm->getSelectedItem())+1];
strcpy(selitems[0], fm->getSelectedItem());
itemcnt = 1;
debug("Used highlighted file to mark as bad.\n");
}
halfpath = fm->getPath();
for (i = 0; i < itemcnt; i++)
{
fullpath = new char[strlen(halfpath) + strlen(selitems[i]) + 1];
strcpy(fullpath, halfpath);
strcat(fullpath, selitems[i]);
debug(fullpath);
if (is_audiofile(fullpath))
{
if (fw_markfilebad((const char *)fullpath))
moved_items_count++;
}
delete[] fullpath;
delete[] selitems[i];
fullpath = NULL;
}
delete[] selitems;
if (moved_items_count)
fw_changedir(".");
}
case CMD_FILE_DELETE:
fw_delete();
break;
case CMD_FILE_ENTER: /* change into dir, play soundfile, load playlist,
* end search */
if (fm->isDir(fm->sw_selection))
fw_changedir();
else if (is_playlist(fm->getSelectedItem()))
{
char
bla[strlen(fm->getPath()) + strlen(fm->getSelectedItem()) + 2];
sprintf(bla, "%s/%s", fm->getPath(), fm->getSelectedItem());
fw_end();
read_playlist(bla);
mp3_curwin->swRefresh(2);
break;
}
else if (fm->getNitems() > 0)
play_one_mp3(fm->getSelectedItem());
break;
case CMD_FILE_ADD_FILES: fw_end(); break;
case CMD_FILE_INV_SELECTION: fm->invertSelection(); break;
case CMD_FILE_RECURSIVE_SELECT:
{
char
*tmppwd = get_current_working_path(),
**files = NULL;
unsigned int fileCount = 0;
mw_settxt("Recursively selecting files..");
recsel_files(tmppwd, &files, &fileCount); //TODO: move this to fileManager class?
free(tmppwd);
if (fileCount)
{
sort_files(files, fileCount);
add_selected_files(files, fileCount); //copies allocated pointers
free(files);
}
fw_end();
mw_settxt("Added all mp3's in this dir and all subdirs " \
"to current group.");
}
break;
case CMD_FILE_SET_PATH:
{
const char *label[] = {
"Enter path below",
NULL,
};
popup_win(label, fw_getpath, NULL, NULL, 1, 1024);
break;
}
case CMD_FILE_DIRS_AS_GROUPS:
{
char
*tmppwd = get_current_working_path();
mw_settxt("Adding groups..");
recsel_files(tmppwd, NULL, NULL, 1, 1);
free(tmppwd);
fw_end();
mw_settxt("Added dirs as groups.");
}
break;
case CMD_FILE_MP3_TO_WAV: /* Convert mp3 to wav */
{
const char *label[] = { "Dir to put wavefiles:", startup_path, NULL };
popup_win(label, fw_convmp3, NULL, NULL, 1, 1024);
}
break;
case CMD_FILE_ADD_URL: /* Add HTTP url to play */
{
const char *label[] = { "URL:", NULL };
popup_win(label, fw_addurl, NULL, NULL, 1, 2048);
}
break;
case CMD_HELP_PREV: helpwin->pageUp(); repaint_help(); break;
case CMD_HELP_NEXT: helpwin->pageDown(); repaint_help(); break;
#ifdef PTHREADEDMPEG
case CMD_CHANGE_THREAD: change_threads(); break;
#endif
case CMD_CLEAR_PLAYLIST:
lock_playing_mutex();
if (!playopts.playing)
{
mp3_rootwin->delItems();
mp3_curwin = mp3_rootwin;
mp3_rootwin->swRefresh(2);
}
unlock_playing_mutex();
break;
case CMD_FILE_RENAME:
{
const char *label[] = { "Filename:", NULL };
if (!fm->isDir(fm->sw_selection) &&
fm->getSelectedItem(0))
{
popup_win(label, fw_rename_file, NULL,
chop_path(fm->getSelectedItem(0)), 1, 2048);
}
}
break;
case CMD_LEFT:
if (progmode == PM_NORMAL)
sw->pan(-globalopts.pan_size);
else if (progmode == PM_FILESELECTION)
fm->pan(-globalopts.pan_size);
break;
case CMD_RIGHT:
if (progmode == PM_NORMAL)
sw->pan(globalopts.pan_size);
else if (progmode == PM_FILESELECTION)
fm->pan(globalopts.pan_size);
break;
case CMD_TOGGLE_WRAP:
globalopts.wraplist = !globalopts.wraplist;
break;
case CMD_JUMP_TOP:
if (progmode == PM_NORMAL)
sw->jumpTop();
else if (progmode == PM_FILESELECTION)
fm->jumpTop();
else if (progmode == PM_HELP)
bighelpwin->jumpTop();
break;
case CMD_JUMP_BOT:
if (progmode == PM_NORMAL)
sw->jumpBottom();
else if (progmode == PM_FILESELECTION)
fm->jumpBottom();
else if (progmode == PM_HELP)
bighelpwin->jumpBottom();
break;
case CMD_NONE: break;
default:
if (mixer)
mixer->ProcessKey(key);
}
return retval;
}
/* Function : show_help
* Description: Displays a help screen in the main window. "progmode" should
* : be in PM_HELP.
* Parameters : None.
* Returns : Nothing.
* SideEffects: None.
*/
void
show_help()
{
if (!bighelpwin)
{
int height, width, y, x;
chtype b1, b2, b3, b4, b5, b6, b7, b8;
char **lines = NULL;
char *configfile = NULL;
int linecount = 0;
//short h=(COLS/2)+1;
get_mainwin_size(&height, &width, &y, &x);
get_mainwin_borderchars(&b1, &b2, &b3, &b4, &b5, &b6, &b7, &b8);
configfile = new char[strlen(MP3BLASTER_DOCDIR) + 20];
sprintf(configfile, "%s/%s", MP3BLASTER_DOCDIR, "commands.txt");
if (read_file(configfile, &lines, &linecount))
{
int i;
bighelpwin = new scrollWin(height, width, y, x, NULL, 0,
CP_DEFAULT, 1);
bighelpwin->setWrap(globalopts.wraplist);
for (i = 0; i < linecount; i++)
{
char *newline = strrchr(lines[i], '\n');
if (newline)
newline[0] = '\0';
bighelpwin->addItem(lines[i]);
free(lines[i]);
}
free(lines);
}
else
{
bighelpwin = new scrollWin(height, width, y, x, lines, linecount,
CP_DEFAULT, 1);
bighelpwin->setWrap(globalopts.wraplist);
bighelpwin->addItem("Couldn't read mp3blaster helpfile:");
bighelpwin->addItem(configfile);
}
bighelpwin->setBorder(b1, b2, b3, b4, b5, b6, b7, b8);
delete[] configfile;
}
mw_settxt("Press '?' to exit help screen.");
bighelpwin->swRefresh(2);
}
void
end_help()
{
//simpy 'hide' bighelpwin..
mp3_curwin->swRefresh(2);
mw_settxt("Visit http://mp3blaster.sourceforge.net/");
}
void
cw_draw_filesort(short cleanit)
{
if (!cleanit)
{
if (progmode == PM_FILESELECTION)
fw_draw_sortmode(globalopts.fw_sortingmode);
}
else
{
//clear 'Sorting mode' line.
move(6, 2);
int stextlen = (COLS > 16 ? COLS - 16 : 1);
char emptyline[stextlen + 1];
memset(emptyline, ' ', stextlen * sizeof(char));
emptyline[stextlen] = '\0';
addstr(emptyline);
}
}
void
draw_settings(int cleanit)
{
cw_draw_group_mode();
if (progmode == PM_FILESELECTION)
cw_draw_filesort(cleanit);
else if (progmode == PM_NORMAL)
cw_draw_play_mode(cleanit);
cw_draw_repeat();
#ifdef PTHREADEDMPEG
cw_draw_threads(cleanit);
#endif
refresh();
}
void
fw_convmp3(const char *tmp, void *args)
{
char **selitems;
int nselected;
char *dir2write;
if (args);
if (tmp && !strlen(tmp))
{
const char *tmp2 = file_window->getPath();
dir2write = strdup(tmp2);
}
else if (tmp)
dir2write = expand_path(tmp);
else
{
warning("Invalid dir entered.");
refresh();
return;
}
if (!dir2write || !is_dir(dir2write))
{
warning("Invalid path entered.");
refresh();
free(dir2write);
return;
}
if (progmode != PM_FILESELECTION)
{
free(dir2write);
return;
}
selitems = file_window->getSelectedItems(&nselected);
if (!nselected && file_window->getSelectedItem())
{
selitems = new char*[1];
selitems[0] = new char[strlen(file_window->getSelectedItem()) + 1];
strcpy(selitems[0], file_window->getSelectedItem());
nselected = 1;
}
else if (!nselected)
{
warning("No item[s] selected.");
refresh();
free(dir2write);
return;
}
for (int i = 0; i < nselected; i++)
{
const char
*pwd = file_window->getPath();
char
*file = new char[(strlen(pwd) + strlen(selitems[i]) +
2) * sizeof(char)],
*file2write = new char[(strlen(dir2write) + strlen(selitems[i]) +
6)];
int len;
strcpy(file, pwd);
if (pwd[strlen(pwd) - 1] != '/')
strcat(file, "/");
strcat(file, selitems[i]);
strcpy(file2write, dir2write);
if (pwd[strlen(dir2write) - 1] != '/')
strcat(file2write, "/");
strcat(file2write, selitems[i]);
if ((len = strlen(file2write)) > 4 && !fnmatch(".[mm][pP][23]",
(file2write + (len - 4)), 0))
bcopy(".wav", (void*)(file2write + (len - 4)), 4);
else
strcat(file2write, ".wav");
if (is_audiofile(file))
{
char bla[strlen(file)+80];
Mpegfileplayer *decoder = NULL;
if (!(decoder = new Mpegfileplayer(Fileplayer::AUDIODRV_OSS)) ||
!decoder->openfile(file,
file2write, ST_WAV) || !decoder->initialize(NULL))
{
sprintf(bla, "Decoding of %s failed.", selitems[i]);
warning(bla);
refresh();
if (decoder)
delete decoder;
}
else
{
sprintf(bla, "Converting '%s' to wavefile, please wait.",
selitems[i]);
mw_settxt(bla);
while (decoder->run(10));
int decode_error = decoder->geterrorcode();
decoder->stop();
if (decode_error != SOUND_ERROR_OK &&
decode_error != SOUND_ERROR_FINISH)
{
warning("Decode error: %s", get_error_string(decode_error));
}
else
mw_settxt("Conversion(s) finished without errors.");
delete decoder;
}
}
else
{
warning("Skipping non-audio file");
refresh();
}
delete[] selitems[i];
delete[] file;
delete[] file2write;
}
if (selitems)
delete[] selitems;
free(dir2write);
}
void
fw_addurl(const char *urlname, void *args)
{
mp3Win
*sw;
if (args);
if (!urlname || progmode != PM_FILESELECTION)
{
return;
}
sw = mp3_curwin;
add_selected_file(urlname);
}
void
fw_set_search_timeout(int timeout=2)
{
struct itimerval tijd, dummy;
tijd.it_value.tv_sec = tijd.it_interval.tv_sec = timeout;
tijd.it_value.tv_usec = tijd.it_interval.tv_usec = 0;
setitimer(ITIMER_REAL, &tijd, &dummy);
}
void
fw_end_search()
{
if (fw_searchstring)
delete[] fw_searchstring;
fw_searchstring = NULL;
//signal(SIGALRM, SIG_IGN);
mw_settxt("");
}
/* called when sigalarm is triggered (i.e. when, in searchmode in filemanager,
* a key has not been pressed for a few seconds (default: 2). This will
* disable the searchmode.
*/
void
fw_search_timeout(int blub)
{
if (blub); //no warning this way ;)
if (input_mode != IM_SEARCH)
return;
fw_end_search();
input_mode = IM_DEFAULT;
}
/* called when someone presses [a-zA-Z0-9] in searchmode in filemanager
* or in playlist
*/
void
fw_search_next_char(char nxt)
{
char *tmp;
short foundmatch = 0;
scrollWin *sw = file_window;
if (progmode == PM_NORMAL)
sw = mp3_curwin;
if (!fw_searchstring)
{
tmp = new char[2];
tmp[0] = nxt;
tmp[1] = '\0';
}
else
{
tmp = new char[strlen(fw_searchstring)+2];
strcpy(tmp, fw_searchstring);
strncat(tmp, &nxt, 1);
}
for (int i = 0; i < sw->getNitems(); i++)
{
const char *item = sw->getItem(i);
if (progmode == PM_NORMAL)
item = chop_path(item);
if (!strncmp(item, tmp, strlen(tmp)))
{
sw->setItem(i);
foundmatch = 1;
break;
}
}
fw_set_search_timeout(2);
if (!foundmatch)
beep();
else
{
if (fw_searchstring)
delete[] fw_searchstring;
fw_searchstring = new char[strlen(tmp) + 1];
strcpy(fw_searchstring, tmp);
char bla[strlen(fw_searchstring)+20];
sprintf(bla, "Search: %s", fw_searchstring);
mw_settxt(bla);
}
}
/* called when someone presses '/' in filemanager */
void
fw_start_search(int timeout)
{
signal(SIGALRM, &fw_search_timeout);
fw_set_search_timeout(timeout);
mw_settxt("Search:");
input_mode = IM_SEARCH;
}
void
set_sound_device(const char *devname)
{
if (!devname)
return;
if (globalopts.sound_device)
delete[] globalopts.sound_device;
globalopts.sound_device = new char[strlen(devname)+1];
strcpy(globalopts.sound_device, devname);
}
void
set_mixer_device(const char *devname)
{
if (!devname)
return;
if (globalopts.mixer_device)
delete[] globalopts.mixer_device;
globalopts.mixer_device = new char[strlen(devname)+1];
strcpy(globalopts.mixer_device, devname);
}
short
set_fpl(int fpl)
{
if (fpl < 1 || fpl > 10)
return 0;
globalopts.fpl = fpl;
return 1;
}
#ifdef PTHREADEDMPEG
short
set_threads(int t)
{
if (t < 0 || t > 500 || t%50 != 0)
return 0;
else
globalopts.threads = t;
//t = 0; //prevent compile warning
return 1;
}
#endif
short
set_audiofile_matching(const char **matches, int nmatches)
{
if (!matches || nmatches < 1)
return 0;
if (globalopts.extensions)
{
int i=0;
while (globalopts.extensions[i])
{
delete[] globalopts.extensions[i];
i++;
}
delete[] globalopts.extensions;
}
globalopts.extensions = new char*[nmatches+1];
for (int i=0; i < nmatches; i++)
{
globalopts.extensions[i] = new char[strlen(matches[i])+1];
strcpy(globalopts.extensions[i], matches[i]);
}
globalopts.extensions[nmatches] = NULL;
return 1;
}
short
set_playlist_matching(const char **matches, int nmatches)
{
if (!matches || nmatches < 1)
return 0;
if (globalopts.plist_exts)
{
int i=0;
while (globalopts.plist_exts[i])
{
delete[] globalopts.plist_exts[i];
i++;
}
delete[] globalopts.plist_exts;
}
globalopts.plist_exts = new char*[nmatches+1];
for (int i=0; i < nmatches; i++)
{
globalopts.plist_exts[i] = new char[strlen(matches[i])+1];
strcpy(globalopts.plist_exts[i], matches[i]);
}
globalopts.plist_exts[nmatches] = NULL;
return 1;
}
short
set_warn_delay(unsigned int seconds)
{
if (seconds > 60)
return 0;
globalopts.warndelay = seconds;
return 1;
}
short
set_skip_frames(unsigned int frames)
{
if (frames > 1000 || frames < 1) /* get real */
return 0;
globalopts.skipframes = frames;
return 1;
}
void
set_default_colours()
{
globalopts.colours.default_fg = COLOR_WHITE;
globalopts.colours.default_bg = COLOR_BLACK;
globalopts.colours.popup_fg = COLOR_WHITE;
globalopts.colours.popup_bg = COLOR_BLUE;
globalopts.colours.popup_input_fg = COLOR_WHITE;
globalopts.colours.popup_input_bg = COLOR_MAGENTA;
globalopts.colours.error_fg = COLOR_WHITE;
globalopts.colours.error_bg = COLOR_RED;
globalopts.colours.button_fg = COLOR_MAGENTA;
globalopts.colours.button_bg = COLOR_BLACK;
globalopts.colours.shortcut_fg = COLOR_MAGENTA;
globalopts.colours.shortcut_bg = COLOR_BLUE;
globalopts.colours.label_fg = COLOR_BLACK;
globalopts.colours.label_bg = COLOR_YELLOW;
globalopts.colours.number_fg = COLOR_YELLOW;
globalopts.colours.number_bg = COLOR_BLACK;
globalopts.colours.file_mp3_fg = COLOR_GREEN;
globalopts.colours.file_dir_fg = COLOR_BLUE;
globalopts.colours.file_lst_fg = COLOR_YELLOW;
globalopts.colours.file_win_fg = COLOR_MAGENTA;
}
mp3Win *
newgroup()
{
mp3Win *tmp;
tmp = new mp3Win(calc_mainwindow_height(LINES),
calc_mainwindow_width(COLS), 10, 0, NULL, 0, CP_DEFAULT, 1);
tmp->drawTitleInBorder(1);
tmp->setBorder(ACS_VLINE,ACS_VLINE,ACS_HLINE,ACS_HLINE,
ACS_LTEE, ACS_PLUS, ACS_LTEE, ACS_PLUS);
tmp->setDisplayMode(globalopts.display_mode);
tmp->setWrap(globalopts.wraplist);
return tmp;
}
void
set_action(_action bla)
{
lock_playing_mutex();
action = bla;
if (bla == AC_STOP)
quit_after_playlist = 0;
unlock_playing_mutex();
}
/* determine_song returns a pointer to the song to play (or NULL if there is
* none). delete[] it after use.
* Argument set_played = 0 => don't set it to 'played' status!
*/
char*
determine_song(short set_played)
{
const char *mysong = NULL;
char *song;
int total_songs;
total_songs = mp3_rootwin->getUnplayedSongs();
if (!total_songs)
return NULL;
switch(globalopts.play_mode)
{
case PLAY_GROUPS_RANDOMLY:
return NULL;
break;
case PLAY_NONE:
break;
case PLAY_SONGS: //all songs in random order
if (next_song > -1)
mysong = mp3_rootwin->getUnplayedSong(next_song, set_played);
else
mysong = mp3_rootwin->getUnplayedSong(myrand(total_songs), set_played);
next_song = (total_songs - 1 ? myrand(total_songs - 1) : -1);
break;
case PLAY_GROUP: //current group:
case PLAY_GROUPS:
{
if (globalopts.play_mode == PLAY_GROUPS && !mp3_groupwin)
{
mp3_groupwin = mp3_curwin;
playing_group = NULL;
}
while (!playing_group || !playing_group->getUnplayedSongs())
{
if (playing_group) //this means that there are no unplayed songs in it
{
playing_group->setNotPlaying();
playing_group = NULL;
if (globalopts.play_mode == PLAY_GROUP)
//finished playing group
break;
}
if (globalopts.play_mode == PLAY_GROUPS)
{
if (!mp3_groupwin) //shouldn't happen...
{
reset_playlist(0);
break;
}
//getUnplayedGroups counts the group itself as well if it contains
//unplayed songs. Just so you know. I forgot this myself ;-)
int
tg = mp3_groupwin->getUnplayedGroups();
if (!tg) //no more groups left to play!
{
reset_playlist(0);
//mp3_groupwin = NULL;
break;
}
//determine which group to play.
if (mp3_groupwin->getPlaymode())
playing_group = mp3_groupwin->getUnplayedGroup(myrand(tg));
else
playing_group = mp3_groupwin->getUnplayedGroup(0);
if (playing_group->getUnplayedSongs(0))
{
playing_group->setPlayed();
playing_group->setPlaying();
}
else
{
playing_group = NULL;
mp3_groupwin = NULL;
}
}
else
{
playing_group = mp3_curwin;
playing_group->setPlaying();
}
}
if (!playing_group) //no valid group could be found.
return NULL;
if ((total_songs = playing_group->getUnplayedSongs(0)))
{
if (!playing_group->getPlaymode()) //normal playback
{
if (next_song > -1)
mysong = playing_group->getUnplayedSong(next_song, set_played, 0);
else
mysong = playing_group->getUnplayedSong(0, set_played, 0);
next_song = 0;
}
else //random playback
{
if (next_song > -1)
mysong = playing_group->getUnplayedSong(next_song, set_played, 0);
else
mysong = playing_group->getUnplayedSong(myrand(total_songs), set_played, 0);
if (total_songs - 1)
next_song = myrand(total_songs - 1);
else
next_song = -1;
}
}
else
reset_playlist(1); //only reset playing group
}
break;
}
if (mysong)
{
if (next_song > -1)
set_next_song(next_song);
song = new char[strlen(mysong)+1];
strcpy(song, mysong);
return song;
}
return NULL;
}
void
update_status_file()
{
if (status_file == NULL)
return;
if (songinf.update & (1 | 4 | 16 | 32))
{
// song info, song status, song status too (?), song change
write_status_file();
}
}
void
write_status_file()
{
FILE *file;
struct song_info &si = songinf.songinfo; // mpeg internal song info
file = fopen(status_file, "w");
if (file == NULL)
return;
if (songinf.path != NULL)
fprintf(file, "path %s\n", songinf.path);
switch (songinf.status)
{
case PS_PLAY: fprintf(file, "status playing\n"); break;
case PS_PAUSE: fprintf(file, "status paused\n"); break;
case PS_REWIND: fprintf(file, "status rewinding\n"); break;
case PS_FORWARD: fprintf(file, "status fast forwarding\n"); break;
case PS_PREV: fprintf(file, "status selecting previous song\n"); break;
case PS_NEXT: fprintf(file, "status selecting next song\n"); break;
case PS_STOP: fprintf(file, "status stopped\n"); break;
case PS_RECORD: fprintf(file, "status recording\n"); break;
case PS_NONE: break;
}
if (si.songname[0] != '\0')
fprintf(file, "title %s\n", si.songname);
if (si.artist[0] != '\0')
fprintf(file, "artist %s\n", si.artist);
if (si.album[0] != '\0')
fprintf(file, "album %s\n", si.album);
if (si.year[0] != '\0')
fprintf(file, "year %s\n", si.year);
if (si.comment[0] != '\0')
fprintf(file, "comment %s\n", si.comment);
if (si.mode[0] != '\0')
fprintf(file, "mode %s\n", si.mode);
fprintf(file, "format MPEG %d layer %d\n", si.mp3_version + 1,
si.mp3_layer);
fprintf(file, "bitrate %d\n", si.bitrate);
fprintf(file, "samplerate %d\n", si.samplerate);
fprintf(file, "length %d\n", si.totaltime);
if (songinf.next_song)
fprintf(file, "next %s\n", songinf.next_song);
fclose(file);
}
void
set_next_song(int next_song)
{
const char *ns = NULL;
switch(globalopts.play_mode)
{
case PLAY_SONGS:
ns = mp3_rootwin->getUnplayedSong(next_song, 0); break;
case PLAY_GROUP:
case PLAY_GROUPS:
if (!playing_group)
break;
ns = playing_group->getUnplayedSong(next_song, 0, 0);
if (globalopts.play_mode == PLAY_GROUPS && !ns)
ns = "??? (can't know yet)";
break;
case PLAY_NONE: break;
case PLAY_GROUPS_RANDOMLY:
ns = "You shouldn't see this!";
}
//don't draw next_song on the screen yet, because this function can be
//called from different threads.
if (!ns)
return;
if (LOCK_NCURSES)
{
songinf.update = 32;
if (songinf.next_song)
free(songinf.next_song);
songinf.next_song = strdup(chop_path(ns));
UPDATE_CURSES;
UNLOCK_NCURSES;
}
}
void
set_song_info(const char *fl, struct song_info si)
{
int maxy, maxx, r, i;
bool show_hours = (si.totaltime >= 3600);
char bla[64];
int
hours = si.totaltime / 3600,
minutes = (si.totaltime % 3600) / 60,
seconds = si.totaltime % 60;
draw_static();
getmaxyx(stdscr, maxy, maxx);
r = maxx - 12;
//total time
move(13,r+8);addch(':');
move(14,r+8);addch(':');
if (show_hours)
{
move(14, r+5);
addch(':');
}
move(6,r); addstr("Mpg Layer");
move(7,r); addstr(" Khz kb");
attrset(COLOR_PAIR(CP_NUMBER)|A_BOLD);
if (si.totaltime >= 3600)
{
move(14, r+3);
printw("%2d", hours);
move(14,r+6); printw("%02d", minutes);
}
else
{
move(14, r+3);
addstr(" ");
move(14,r+6); printw("%2d", minutes);
}
move(14,r+9); printw("%02d", seconds);
attroff(A_BOLD);
//mp3 info
move(8,r); addstr(" ");
move(8,r); addnstr(si.mode,11);
move(6,r+3); printw("%1d", si.mp3_version + 1);
move(6,r+10); printw("%1d", si.mp3_layer);
move(7,r); printw("%2d", (si.samplerate > 1000 ? si.samplerate / 1000 :
si.samplerate));
move(7,r+6); printw("%-3d", si.bitrate);
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
//clear songname
for (i = 4; i < maxx - 14; i++)
{
move(maxy-3, i); addch('x');
move(maxy-3, i); addch(' ');
}
move(maxy-3,4);
if (!strlen(si.artist))
addnstr(fl, maxx - 18);
else
printw("%s - %s", (strlen(si.artist) ? si.artist :
"<Unknown Artist>"), (strlen(si.songname) ? si.songname :
"<Unknown Songname>") );
sprintf(bla, "%s (%s)", (si.album && strlen(si.album) ? si.album :
"<Unknown Album>"), (si.comment && strlen(si.comment) ? si.comment :
"no comments"));
mw_settxt(bla);
refresh();
}
void
set_song_status(playstatus_t s)
{
int maxy, maxx;
getmaxyx(stdscr,maxy,maxx);
move(maxy-3,1);
switch(s)
{
case PS_PLAY: addstr("|>");break;
case PS_PAUSE: addstr("||");break;
case PS_REWIND: addstr("<<");break;
case PS_FORWARD: addstr(">>");break;
case PS_PREV: addstr("|<");break;
case PS_NEXT: addstr(">|");break;
case PS_STOP: addstr("[]"); mw_clear(); break;
case PS_RECORD: addstr("()");break;
default: break;
}
refresh();
}
void
set_song_time(int elapsed, int remaining)
{
int maxy, maxx, r;
bool show_hours = (elapsed >= 3600);
int
seconds = elapsed %60,
minutes = (elapsed % 3600) / 60,
hours = (elapsed / 3600);
if (remaining); //suppress warning
getmaxyx(stdscr, maxy, maxx);
r = maxx - 12;
//print elapsed time
attrset(COLOR_PAIR(CP_NUMBER)|A_BOLD);
if (show_hours)
{
move(13,r+3); printw("%2d", hours);
move(13,r+6); printw("%02d", minutes);
}
else
{
move(13,r+3); addstr(" ");
move(13,r+6); printw("%2d", minutes);
}
move(13,r+9); printw("%02d", seconds);
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
move(13,r+8); addch(':');
if (show_hours)
{
move(13, r+5); addch(':');
}
refresh();
}
int
myrand(int max_nr)
{
return (int)(((max_nr * 1.0) * rand()) / (RAND_MAX + 1.0));
}
void
reset_playlist(int full)
{
debug("reset_playlist(%d)\n", full);
if (!full) //only reset currently played group
{
if (playing_group)
{
playing_group->resetSongs(0); //reset SONGS in played group
playing_group->setNotPlaying(); //reset playing status of played group
playing_group->setNotPlayed(); //reset PLAYLIST status of played group
playing_group = NULL;
}
}
else
{
if (playing_group)
playing_group->setNotPlaying();
playing_group = NULL;
mp3_groupwin = NULL;
mp3_rootwin->resetSongs();
mp3_rootwin->resetGroups();
}
//reset history as well
if (history)
{
delete history;
history = new History();
}
}
void
cw_draw_repeat()
{
move(12,COLS-11);
if (globalopts.repeat)
addch('X');
else
addch(' ');
}
/* Function : init_helpwin
* Description: sets up the helpwin that's on top of the ncurses window.
* : Do this whenever you change program mode (normal mode to
* : file manager mode e.g.)
* Parameters : None.
* Returns : Nothing.
* SideEffects: None.
*/
void
init_helpwin()
{
int i = 0, item_count = 0;
char line[80], desc[27], keylabel[4];
struct keybind_t k = keys[i];
if (helpwin)
{
delete helpwin;
helpwin = NULL;
}
memset(line, 0, sizeof(line));
//TODO: scrollwin never uses first&last line if no border's used!!
helpwin = new scrollWin(4, COLS > 2 ? COLS-2 : 1,1,1,NULL,0,CP_DEFAULT,0);
helpwin->setWrap(globalopts.wraplist);
helpwin->hideScrollbar();
//content is afhankelijk van program-mode, dus bij verandereing van
//program mode moet ook de commandset meeveranderen.
while (k.cmd != CMD_NONE)
{
if (k.pm & progmode)
{
set_keylabel(k.key, keylabel);
sprintf(desc, "[%3s] %-19s ", keylabel, k.desc);
if (!(item_count%3))
strcpy(line, desc);
else
strcat(line, desc);
if (item_count%3 == 2)
{
helpwin->addItem(line);
memset(line, 0, sizeof(line));
}
item_count++;
}
k = keys[++i];
}
if (item_count%3)
helpwin->addItem(line);
helpwin->swRefresh(2);
}
void
bindkey(command_t cmd, int kcode)
{
int i = 0;
struct keybind_t k = keys[i];
while (k.cmd != CMD_NONE)
{
if (k.cmd == cmd)
{
keys[i].key = kcode;
break;
}
k = keys[++i];
}
}
/* puts the label corresponding to a command in 'label', which must have at
* least size of 4.
*/
void
get_label(command_t cmd, char *label)
{
int i = 0;
struct keybind_t k = keys[i];
while (k.cmd != cmd)
{
k = keys[i++];
}
set_keylabel(k.key, label);
}
/* label must be alloc'd with size 4.
* Produces a 3-character label + terminating 0 that corresponds to the given
* keycode 'k'
*/
void
set_keylabel(int k, char *label)
{
int i;
struct keylabel_t kl;
label[3] = '\0';
if (k > 32 && k < 177)
{
sprintf(label, " %c ", k);
return;
}
for (i = 0; i < 64; i++)
{
if (k == KEY_F(i))
{
sprintf(label, "F%2d", i);
return;
}
}
i = 0; kl = klabels[i];
while (kl.key != ERR)
{
if (kl.key == k)
{
sprintf(label, "%s", kl.desc);
return;
}
kl = klabels[++i];
}
sprintf(label, "s%02x", (unsigned int)k);
return;
}
/* Draws the purple colour over the keylabels.
* It's ugly, but there are (currently) no means to have scrollWin colour
* only parts of a string. TODO: implement that!
*/
void
repaint_help()
{
int i, j;
/* Redhat 7.2 ships with a buggy ncurses version, which will crash on
* chgat. Therefore, they will not get funky colours in the help menu.
*/
#if defined(NCURSES_VERSION_MAJOR) && (!defined(NCURSES_VERSION_PATCH) || !(NCURSES_VERSION_MAJOR == 5 && NCURSES_VERSION_MINOR == 2 && NCURSES_VERSION_PATCH == 20010714))
for (i = 1; i < 5; i++)
{
for (j = 2; j < 55; j += 26)
{
move (i, j);
chgat(3, A_NORMAL, CP_BUTTON, NULL);
}
}
#endif
touchline(stdscr, 1, 4);
refresh();
//refresh_screen();
}
/* mode == 0 (default): next song
* mode == 1: jump to previous song
* mode == 2: generate new 'next' song without jumping to another song
* returns NULL if no next song to play could be determined..
char *
determine(short mode)
{
if (!mode)
{
if (!(played_songs[++current_song])) //no next song.
played_songs[current_song] = determine_song();
//there's a need to realloc the played_songs array?
if (current_song == nr_played_songs - 1)
{
played_songs = (char**)realloc(played_songs, ++nr_played_songs *
sizeof(char*));
played_songs[current_song + 1] = NULL;
}
if (!played_songs[current_song]) //end of playlist probably
return NULL;
//determine next song to play
if (!played_songs[current_song +1 ])
played_songs[current_song + 1] = determine_song();
return played_songs[current_song];
}
else if (mode == 1) // jump to previous song
{
if (current_song == 0) // this is the first song in the list
return played_songs[current_song];
else
return played_songs[--current_song];
}
else if (mode == 2) // determine new 'next' song
{
if (current_song == nr_played_songs - 1)
{
played_songs = (char**)realloc(played_songs, ++nr_played_songs *
sizeof(char*));
played_songs[current_song + 1] = NULL;
}
if (played_songs[current_song + 1])
{
delete[] played_songs[current_song + 1];
played_songs[current_song + 1] = NULL;
}
}
}
*/
void
init_playopts()
{
playopts.song_played = 0;
debug("init_playopts(): allow_repeat = 0\n");
playopts.allow_repeat = 0;
playopts.playing_one_mp3 = 0;
playopts.pause = 0;
playopts.playing = 0;
playopts.one_mp3 = NULL;
playopts.player = NULL;
playopts.quit_program = 0;
}
void
init_globalopts()
{
globalopts.no_mixer = 0; /* mixer on by default */
globalopts.fpl = 10; /* 5 frames are played between input handling */
globalopts.sound_device = NULL; /* default sound device */
globalopts.mixer_device = NULL; /* default mixer device */
globalopts.downsample = 0; /* no downsampling */
globalopts.eightbits = 0; /* 8bits audio off by default */
globalopts.fw_sortingmode = FW_SORT_ALPHA; /* case insensitive dirlist */
globalopts.fw_hideothers = 0; /* show all files */
globalopts.play_mode = PLAY_GROUPS;
globalopts.warndelay = 2; /* wait 2 seconds for a warning to disappear */
globalopts.skipframes=10; /* skip 100 frames during music search */
globalopts.pan_size = 5; /* pan left/right 5 chars at once */
globalopts.debug = 0; /* no debugging info per default */
globalopts.display_mode = 1; /* file display mode (1=filename, 2=id3,
* not finished yet.. */
globalopts.repeat = 0;
globalopts.extensions = NULL;
globalopts.plist_exts = NULL;
globalopts.playlist_dir = get_homedir(NULL);
globalopts.recode_table = NULL;
#ifdef PTHREADEDMPEG
#if !defined(__FreeBSD__)
globalopts.threads = 100;
#else
/* FreeBSD playback using threads is much less efficient for some reason */
globalopts.threads = 0;
#endif
#endif
globalopts.want_id3names = 0;
globalopts.selectitems_unselectfirst = 0;
globalopts.selectitems_searchusingregexp = 0;
globalopts.selectitems_caseinsensitive = 1; //only works for regexp search
globalopts.scan_mp3s = 0; //scan mp3's to calculate correct total time.
globalopts.wraplist = true;
#if WANT_SDL
globalopts.audio_driver = AUDIODEV_SDL; //recommended for hick-free playback
#elif WANT_OSS
globalopts.audio_driver = AUDIODEV_OSS;
#else
globalopts.audio_driver - AUDIODEV_ESD; //hicks too much, don't use or fix.
#endif
}
void
reset_next_song()
{
int width = (COLS > 33 ? COLS - 33 : 1);
char emptyline[width+1];
next_song = -1;
move(7,19);
//clear old text
memset(emptyline, ' ', width * sizeof(char));
emptyline[width] = '\0';
addstr(emptyline);
refresh();
}
/* Draws next song to be played on the screen */
void
draw_next_song(const char *ns)
{
int mywidth = (COLS > 33 ? COLS - 33 : 1);
char empty[mywidth+1];
if (!ns)
return;
memset((void*)empty, ' ', mywidth * sizeof(char));
empty[mywidth] = '\0';
move(7,19);
addstr(empty);
move(7,19);
addnstr(ns, mywidth);
refresh();
}
short
set_playlist_dir(const char *dirname)
{
if (!dirname || !strlen(dirname))
return 0;
if (globalopts.playlist_dir)
{
free(globalopts.playlist_dir);
globalopts.playlist_dir = NULL;
}
globalopts.playlist_dir = strdup(dirname);
if (globalopts.playlist_dir)
return 1;
return 0;
}
/* Function : fw_markfilebad
* Description: Moves the given file to a subdir called 'baditems' in the same
* : dir as this file is in. If the subdir does not exist, one will
* : be created.
* Parameters : file: The file to mark as bad.
* Returns : If the move was successful, 1, else 0.
* SideEffects: If called from within PM_FILEMANAGER mode, you'll likely want
* : to refresh the current directory (fw_changedir(".")).
*/
short
fw_markfilebad(const char *file)
{
//extract path from filename.
//check/create subdir baditems
//move file to subdir
const char
*baditem,
*app_path = "baditems/";
char
*baddir = NULL,
*oldpath = NULL;
struct stat
bdstat;
short
result = 0;
if (file){ debug("FILE: ");debug(file);debug("\n");}
baditem = chop_path(file);
oldpath = chop_file(file); //must be delete[]'d
baddir = new char[strlen(oldpath) + strlen(app_path) + strlen(baditem) + 1];
strcpy(baddir, oldpath);
strcat(baddir, app_path);
debug("baddir: "); debug(baddir); debug("\n");
if (stat(baddir, &bdstat) < 0)
{
int trymkdir = mkdir(baddir, 0755);
if (trymkdir < 0)
{
//warning("Could not make directory 'baditems' for bad audiofile\n");
delete[] baddir;
delete[] oldpath;
return 0;
}
}
else if (!is_dir(baddir))
{
/*
warning("Can't create a dir 'baditems' because a file with that "\
"name already exists\n");
*/
delete[] baddir;
delete[] oldpath;
return 0;
}
strcat(baddir, baditem);
debug("baddir(2): ");
debug(baddir);
debug("\n");
int rsuccess = rename(file, (const char*)baddir);
if (rsuccess < 0)
result = 0;
else
result = 1;
//warning("Failed to mark file as bad.\n");
delete[] baddir;
delete[] oldpath;
return result;
}
/* Function : get_mainwin_size
* Description: Stores the main window (the biggest square window on the screen
* : size and position in the provided int pointers.
* Parameters : y,x: top-left coordinate of window on stdscr
* : height,width: You'll figure this out..
* Returns : Nothing. The values are stored in the provided pointers.
* SideEffects: None.
*/
void
get_mainwin_size(int *height, int *width, int *y, int *x)
{
*height = calc_mainwindow_height(LINES);
*width = calc_mainwindow_width(COLS);
*y = 10;
*x = 0;
}
void
get_mainwin_borderchars(chtype *ls, chtype *rs, chtype *ts, chtype *bs,
chtype *tl, chtype *tr, chtype *bl, chtype *br)
{
*ls = ACS_VLINE;
*rs = ACS_VLINE;
*ts = ACS_HLINE;
*bs = ACS_HLINE;
*tl = ACS_LTEE;
*tr = ACS_PLUS;
*bl = ACS_LTEE;
*br = ACS_PLUS;
}
void
change_program_mode(unsigned short pm)
{
progmode = pm;
if (pm != PM_NORMAL)
draw_settings(1);
else
draw_settings();
init_helpwin();
repaint_help();
};
/* PRE: ncurses_mutex *must* be locked
*/
void
update_ncurses(int update_all)
{
if (update_all || songinf.update & 1)
set_song_info((const char *)songinf.path, songinf.songinfo);
if (update_all || songinf.update & 2)
set_song_time(songinf.elapsed, songinf.remain);
if (update_all || songinf.update & 4)
set_song_status(songinf.status);
if ((songinf.update & 8 && songinf.warning))
{
mw_settxt((const char *)songinf.warning);
delete[] songinf.warning; songinf.warning = NULL;
refresh_screen();
}
if (update_all || songinf.update & 16)
draw_play_key(1);
if (update_all || songinf.update & 32) //draw next_song on screen.
{
if (songinf.next_song)
{
draw_next_song((const char*)(songinf.next_song));
//free(songinf.next_song);
}
else
reset_next_song();
//songinf.next_song = NULL;
}
songinf.update = 0;
}
void
update_songinfo(playstatus_t status, short update)
{
if (LOCK_NCURSES)
{
songinf.update = update;
songinf.status = status;
UPDATE_CURSES;
UNLOCK_NCURSES;
}
}
void
end_program()
{
quit_after_playlist = 0;
if (playopts.playing)
stop_song();
//prevent ncurses clash
if (LOCK_NCURSES);
endwin();
UNLOCK_NCURSES;
exit(0);
};
void
fw_toggle_sort()
{
sortmodes_t newmode = globalopts.fw_sortingmode;
switch(globalopts.fw_sortingmode)
{
case FW_SORT_ALPHA: newmode = FW_SORT_ALPHA_CASE; break;
case FW_SORT_ALPHA_CASE: newmode = FW_SORT_MODIFY_NEW; break;
case FW_SORT_MODIFY_NEW: newmode = FW_SORT_MODIFY_OLD; break;
case FW_SORT_MODIFY_OLD: newmode = FW_SORT_SIZE_SMALL; break;
case FW_SORT_SIZE_SMALL: newmode = FW_SORT_SIZE_BIG; break;
case FW_SORT_SIZE_BIG: newmode = FW_SORT_NONE; break;
case FW_SORT_NONE: newmode = FW_SORT_ALPHA; break;
}
globalopts.fw_sortingmode = newmode;
fw_draw_sortmode(newmode);
}
void
fw_draw_sortmode(sortmodes_t sm)
{
const char *dsc[] = {
"Sort alphabetically, case-insensitive",
"Sort alphabetically, case-sensitive",
"Sort by modification date, newest first",
"Sort by modification date, oldest first",
"Sort by file size, smallest first",
"Sort by file size, biggest first",
"Don't sort",
NULL,
};
short
idx = 0;
int maxlen = (COLS > 16 ? COLS - 16 : 1);
char emptyline[maxlen + 1];
memset(emptyline, ' ', maxlen * sizeof(char));
emptyline[maxlen] = '\0';
switch(sm)
{
case FW_SORT_ALPHA: idx = 0; break;
case FW_SORT_ALPHA_CASE: idx = 1; break;
case FW_SORT_MODIFY_NEW: idx = 2; break;
case FW_SORT_MODIFY_OLD: idx = 3; break;
case FW_SORT_SIZE_SMALL: idx = 4; break;
case FW_SORT_SIZE_BIG: idx = 5; break;
case FW_SORT_NONE: idx = 6; break;
}
move(6, 2); addstr(emptyline);
move(6, 2); printw("Sorting mode : %s", dsc[idx]);
}
/* Function : set_sort_mode
* Description: Sets the sort mode for the file manager
* Parameters : mstring: canonical name for sortmode
* Returns : -1 on failure, >=0 on success
* SideEffects: None.
*/
short
set_sort_mode(const char *mstring)
{
//TODO: make a nice struct with canonical name, enum, indexNR
const char *dsc[] = {
"alpha",
"alpha-case",
"modify-new",
"modify-old",
"size-small",
"size-big",
"none",
NULL
};
unsigned int i = 0;
short found_it = -1;
while (dsc[i])
{
if (!strcasecmp(mstring, dsc[i]))
{
found_it = i;
break;
}
i++;
}
if (found_it < 0)
return -1;
sortmodes_t smode = globalopts.fw_sortingmode;
switch(found_it)
{
case 0: smode = FW_SORT_ALPHA; break;
case 1: smode = FW_SORT_ALPHA_CASE; break;
case 2: smode = FW_SORT_MODIFY_NEW; break;
case 3: smode = FW_SORT_MODIFY_OLD; break;
case 4: smode = FW_SORT_SIZE_SMALL; break;
case 5: smode = FW_SORT_SIZE_BIG; break;
case 6: smode = FW_SORT_NONE; break;
}
globalopts.fw_sortingmode = smode;
if (progmode == PM_FILESELECTION)
fw_draw_sortmode(smode);
return found_it;
}
short
fw_toggle_display()
{
if (!file_window)
return 0;
short dispmode = file_window->getDisplayMode() + 1;
if (dispmode > 2)
dispmode = 0;
file_window->setDisplayMode(dispmode);
//refresh fm after this function has been called.
return 1;
}
/* delete currently selected file */
void
fw_delete()
{
const char
*file = file_window->getSelectedItem();
if (file && !is_dir(file))
{
if (unlink(file) < 0)
warning("Could not delete file.");
else
{
fw_changedir(".");
mw_settxt("File deleted.");
}
}
}
void
warning(const char *txt, ... )
{
va_list ap;
char buf[1025];
mw_clear();
move(LINES-2,1);
attrset(COLOR_PAIR(CP_ERROR)|A_BOLD);
va_start(ap, txt);
vsnprintf(buf, 1024, txt, ap);
va_end(ap);
addnstr(buf, (COLS > 14 ? COLS - 14 : 1));
attrset(COLOR_PAIR(CP_DEFAULT)|A_NORMAL);
refresh();
}
void
unlock_playing_mutex()
{
#ifdef PTHREADEDMPEG
pthread_mutex_unlock(&playing_mutex);
#elif defined(LIBPTH)
pth_mutex_release(&playing_mutex);
#endif
}
void
lock_playing_mutex()
{
#ifdef PTHREADEDMPEG
pthread_mutex_lock(&playing_mutex);
#elif defined(LIBPTH)
pth_mutex_acquire(&playing_mutex, FALSE, NULL);
#endif
}
void
wake_player()
{
debug("wake_player\n");
#ifdef PTHREADEDMPEG
pthread_cond_signal(&playing_cond);
#elif defined(LIBPTH)
pth_cond_notify(&playing_cond, TRUE);
#endif
debug("/wake_player\n");
}
void
add_init_opt(struct init_opts *myopts, const char *option, void *value)
{
struct init_opts *tmp;
//find the last init_opts, and add a new one. Unless the first one's
//empty (!tmp->option[0])
tmp = myopts;
if (tmp->option[0])
{
while (tmp->next)
tmp = tmp->next;
tmp->next = new init_opts;
tmp = tmp->next;
}
tmp->value = value;
memset(tmp->option, 0, 30);
strncpy(tmp->option, option, 29);
tmp->next = NULL;
}
void
get_input(
WINDOW *win, const char*defval, unsigned int size, unsigned int maxlen, int y, int x,
void (*got_input)(const char *, void *), void *args
)
{
if (global_input)
{
delete global_input;
global_input = NULL;
}
global_input = new getInput(win, defval, size, maxlen, y, x);
input_mode = IM_INPUT;
global_input->setCallbackFunction(got_input, args);
mw_settxt("Use up and down arrow keys to toggle insert mode");
}
void
set_inout_opts()
{
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
leaveok(stdscr, FALSE);
curs_set(0);
}
/* Function : select_files_by_pattern
* Description: Select a bunch of files by giving a pattern. Function is called
* : from popup_win
* Parameters : pattern: Pattern to search files for.
* : args: pointer to scrollWin instance to search in
* Returns : Nothing.
* SideEffects: Selects files in the scrollWin instance.
*/
void
select_files_by_pattern(const char *pattern, void *args)
{
scrollWin
*mainwindow = (scrollWin*)args;
if (!pattern || !strlen(pattern))
{
warning("No pattern given.");
return;
}
if (globalopts.selectitems_unselectfirst)
mainwindow->deselectItems();
mainwindow->selectItems(pattern,
(globalopts.selectitems_searchusingregexp ? "regex" : "global"),
(globalopts.selectitems_caseinsensitive ? SW_SEARCH_CASE_INSENSITIVE :
SW_SEARCH_NONE),
mainwindow->getDisplayMode());
mainwindow->swRefresh(0);
}
/* Function : playlist_write
* Description: called from popup_win, this function will write the current
* : playlist to a file given by the user.
* Parameters : playlistfile
* : name of the playlist file to write to. free() after use
* : args: unused
* Returns : Nothing.
* SideEffects: None.
*/
void
playlist_write(const char *playlistfile, void *args)
{
const char
*plistfile = NULL;
char
*org_path = NULL;
char
*tmp_pfile = NULL;
if (args);
if (!playlistfile || !strlen(playlistfile))
return;
if (!is_playlist(playlistfile))
{
tmp_pfile = (char*)malloc((strlen(playlistfile) + 5) * sizeof(char));
sprintf(tmp_pfile, "%s.lst", playlistfile);
plistfile = chop_path(tmp_pfile);
}
else
{
plistfile = chop_path(playlistfile);
}
if (!plistfile || !strlen(plistfile))
{
free(tmp_pfile);
return;
}
org_path = get_current_working_path();
char *tmpDir = expand_path(globalopts.playlist_dir);
do {
if (!tmpDir) {
warning("Failed to change to playlist dir.");
break;
}
if (chdir(tmpDir) == -1) {
warning("Failed to change to playlist dir.");
break;
}
if (!write_playlist(plistfile))
warning("Failed to write playlist.");
else
mw_settxt("Playlist written.");
} while (0);
if (tmpDir)
free(tmpDir);
if (chdir(org_path) == -1) {
warning("Failed to change to original path.");
}
free(org_path);
free(tmp_pfile);
}
void
fw_rename_file(const char *newname, void *args)
{
fileManager
*fm = file_window;
if (args);
if (progmode != PM_FILESELECTION || !fm)
return;
const char *base_path = fm->getPath();
const char *oldname = fm->getSelectedItem(0);
if (!oldname || !newname || !strlen(newname))
{
warning("Nothing to rename (to).");
return;
}
char *new_path = new char[strlen(base_path) + 1 + strlen(newname) + 1];
sprintf(new_path, "%s/%s", base_path, newname);
struct stat tmpbuf;
if (stat(new_path, &tmpbuf) == 0) //it exists!
{
warning("Cowardly refusing to overwrite existing file.");
return;
}
int rename_result = rename(oldname, new_path);
delete[] new_path;
if (rename_result == -1)
{
const char *errormsg = (const char *)strerror(errno);
if (!errormsg)
errormsg = "Unknown error occurred";
char *warningmsg = new char[strlen(errormsg) + 50];
sprintf(warningmsg, "Rename failed: %s", errormsg);
warning(warningmsg);
debug(warningmsg);
delete[] warningmsg;
}
else
{
fw_changedir("."); //refresh dir
mw_settxt("File renamed.");
}
return;
}
int ctoi(char *s)
{
char *foo;
int res = strtoul(s, &foo, 0);
if (*foo) /* parse error */
return 0;
return res;
}
short
read_recode_table(const char *charMapFileName)
{
FILE *fp;
unsigned char buf[512],*p,*q;
int in,on,count;
int line;
int i;
short result = 1;
if ((fp=fopen((char *)charMapFileName,"r")) == NULL)
{
fprintf(stderr,"readRecodeTable: cannot open char map file \"%s\"\n", charMapFileName);
return 0;
}
count=0;
line = 0;
if (globalopts.recode_table)
free(globalopts.recode_table);
globalopts.recode_table = (unsigned char *) malloc(sizeof(unsigned char) * 256);
for (i = 0; i < 256; i++)
globalopts.recode_table[i] = i;
while (fgets((char*)buf,sizeof(buf),fp))
{
line++;
p=(unsigned char *)strtok((char*)buf," \t\n#");
q=(unsigned char *)strtok(NULL," \t\n#");
if (p && q)
{
in = ctoi((char *)p);
if (in > 255)
{
fprintf(stderr, "readRecodeTable: %s: line %d: char val too big\n", charMapFileName, line);
result = 0;
break;
}
on=ctoi((char *)q);
if (in && on)
{
if ( count++ < 256 )
globalopts.recode_table[in]=(char)on;
else
{
fprintf(stderr,"readRecodeTable: char map table \"%s\" is big\n",charMapFileName);
result = 0;
break;
}
}
}
}
fclose(fp);
return result;
}
short
set_charset_table(const char *tabName)
{
if (!strlen(tabName))
return 0;
return read_recode_table(tabName);
}
short
set_audio_driver(const char *driverName)
{
#ifdef WANT_OSS
if (!strcasecmp(driverName, "oss"))
{
globalopts.audio_driver = AUDIODEV_OSS;
return 1;
}
#endif
#ifdef WANT_ESD
if (!strcasecmp(driverName, "esd"))
{
globalopts.audio_driver = AUDIODEV_ESD;
return 1;
}
#endif
#ifdef WANT_SDL
if (!strcasecmp(driverName, "sdl"))
{
globalopts.audio_driver = AUDIODEV_SDL;
return 1;
}
#endif
#ifdef WANT_NAS
if (!strcasecmp(driverName, "nas"))
{
globalopts.audio_driver = AUDIODEV_NAS;
return 1;
}
#endif
return 0; //bad value or unsupported audio driver
}
short
set_pan_size(int psize)
{
if (psize < 1 || psize > 40)
return 0;
globalopts.pan_size = psize;
return 1;
};
int
main(int argc, char *argv[], char *envp[])
{
int
c,
long_index,
key,
options = 0,
playmp3s_nr = 0;
time_t t;
char
**playmp3s = NULL,
*init_playlist = NULL,
*chroot_dir = NULL,
*config_file = NULL;
struct _tmp
{
char *play_mode;
char *sound_device;
char *mixer_device;
int fpl;
#ifdef PTHREADEDMPEG
int threads;
#endif
} tmp;
#ifdef LIBPTH
pth_init();
#endif
tmp.sound_device = NULL;
tmp.mixer_device = NULL;
tmp.play_mode = NULL;
tmp.fpl = 5;
tmp.threads = 10;
environment = envp;
songinf.update = 0;
songinf.path = NULL;
songinf.next_song = NULL;
songinf.warning = NULL;
playing_group = NULL;
status_file = NULL;
mixer = NULL;
helpwin = NULL;
bighelpwin = NULL;
mp3_groupwin = NULL;
history = new History();
long_index = 0;
played_songs = (char**)malloc(sizeof(char*));
played_songs[0] = NULL;
nr_played_songs = 1;
current_song = -1;
srand((unsigned int)time(&t));
init_globalopts();
init_playopts();
set_default_colours(); // fill globalopts.colours with default values.
/* parse arguments */
while (1)
{
static struct option long_options[] =
{
{ "downsample", 0, 0, '2'},
{ "8bits", 0, 0, '8'},
{ "autolist", 1, 0, 'a'},
{ "config-file", 1, 0, 'c'},
{ "debug", 0, 0, 'd'},
{ "help", 0, 0, 'h'},
{ "list", 1, 0, 'l'},
{ "mixer-device", 1, 0, 'm' },
{ "no-mixer", 0, 0, 'n'},
{ "chroot", 1, 0, 'o'},
{ "playmode", 1, 0, 'p'},
{ "dont-quit", 0, 0, 'q'},
{ "repeat", 0, 0, 'R'},
{ "runframes", 1, 0, 'r'},
{ "sound-device", 1, 0, 's'},
{ "status-file", 1, 0, 'f'},
{ "threads", 1, 0, 't'},
{ "version", 0, 0, 'v'},
{ 0, 0, 0, 0}
};
c = getopt_long(argc, argv, "28a:c:df:hl:m:no:p:qRr:s:t:v", long_options,
&long_index);
if (c == EOF)
break;
switch(c)
{
case ':':
case '?':
usage();
break;
case '2': /* downsample */
options |= OPT_DOWNSAMPLE;
break;
case '8': /* 8bit audio */
options |= OPT_8BITS;
break;
case 'a': /* load playlist and play */
options |= OPT_AUTOLIST;
if (init_playlist)
delete[] init_playlist;
init_playlist = new char[strlen(optarg)+1];
strcpy(init_playlist, optarg);
break;
case 'c': /* configuration file */
if (config_file) free(config_file);
config_file = strdup(optarg);
break;
case 'd':
options |= OPT_DEBUG;
break;
case 'f': /* status file */
if (status_file)
free(status_file);
status_file = strdup(optarg);
break;
case 'h': /* help */
usage();
break;
case 'l': /* load playlist */
options |= OPT_LOADLIST;
if (init_playlist)
delete[] init_playlist;
init_playlist = new char[strlen(optarg)+1];
strcpy(init_playlist, optarg);
break;
case 'm': /* mixer device */
options |= OPT_MIXERDEV;
if (tmp.mixer_device)
free(tmp.mixer_device);
tmp.mixer_device = strdup(optarg);
break;
case 'n': /* disable mixer */
options |= OPT_NOMIXER;
break;
case 'o': /* chroot */
options |= OPT_CHROOT;
if (chroot_dir)
delete[] chroot_dir;
chroot_dir = new char[strlen(optarg)+1];
strcpy(chroot_dir, optarg);
break;
case 'p': /* set initial playmode */
options |= OPT_PLAYMODE;
tmp.play_mode = new char[strlen(optarg)+1];
strcpy(tmp.play_mode, optarg);
break;
case 'q': /* don't quit after playing 1 mp3/playlist */
options |= OPT_QUIT;
break;
case 'R': /* playlist repeat */
options |= OPT_REPEAT;
break;
case 'r': /* numbers of frames to decode in 1 loop 1<=fpl<=10 */
options |= OPT_FPL;
tmp.fpl = atoi(optarg);
break;
case 's': /* sound-device other than the default /dev/{dsp,audio} ? */
options |= OPT_SOUNDDEV;
if (tmp.sound_device)
free(tmp.sound_device);
tmp.sound_device = strdup(optarg);
break;
case 't': /* threads */
options |= OPT_THREADS;
#ifdef PTHREADEDMPEG
tmp.threads = atoi(optarg);
#endif
break;
case 'v': /* version info */
printf("%s version %s - http://mp3blaster.sourceforge.net/\n",
argv[0], VERSION);
printf("Supported audio formats: " BUILDOPTS_AUDIOFORMATS "\n");
printf("Supported audio output drivers: " BUILDOPTS_AUDIODRIVERS "\n");
printf("Build features: " BUILDOPTS_FEATURES "\n");
exit(0);
break;
default:
usage();
}
}
if (options & OPT_DEBUG)
{
globalopts.debug = 1;
debug("Debugging of mp3blaster started.\n");
}
//read .mp3blasterrc
if (!cf_parse_config_file(config_file) &&
(config_file || cf_get_error() != NOSUCHFILE))
{
fprintf(stderr, "%s\n", cf_get_error_string());
exit(1);
}
if (config_file)
free(config_file);
if (optind < argc) /* assume all other arguments are mp3's */
{
/* this is not valid if a playlist is loaded from commandline */
if ( (options&OPT_LOADLIST) || (options&OPT_AUTOLIST) )
usage();
if (!(playmp3s = (char**)malloc((argc - optind) * sizeof(char*))))
{
fprintf(stderr, "Cannot allocate enough memory.\n"); exit(1);
}
/* Build a playlist from the remaining arguments */
for (int i = optind ; i < argc; i++)
{
if (!is_audiofile(argv[i]))
{
fprintf(stderr, "%s is not a valid audio file!\n", argv[i]);
usage();
}
playmp3s[i - optind] = (char*)malloc((strlen(argv[i])+1) *
sizeof(char));
strcpy(playmp3s[i - optind], argv[i]);
}
options |= OPT_PLAYMP3;
}
//signal(SIGALRM, SIG_IGN);
//Initialize NCURSES
initscr();
start_color();
set_inout_opts();
startup_path = get_current_working_path();
if (false && (LINES < MIN_SCREEN_HEIGHT || COLS < MIN_SCREEN_WIDTH))
{
mvaddstr(0, 0, "You'll need at least an 80x23 screen, sorry.\n");
getch();
endwin();
exit(1);
}
/* setup colours */
init_pair(CP_DEFAULT, globalopts.colours.default_fg,
globalopts.colours.default_bg);
init_pair(CP_POPUP, globalopts.colours.popup_fg,
globalopts.colours.popup_bg);
init_pair(CP_POPUP_INPUT, globalopts.colours.popup_input_fg,
globalopts.colours.popup_input_bg);
init_pair(CP_ERROR, globalopts.colours.error_fg,
globalopts.colours.error_bg);
init_pair(CP_BUTTON, globalopts.colours.button_fg,
globalopts.colours.button_bg);
init_pair(CP_SHORTCUTS, globalopts.colours.shortcut_fg,
globalopts.colours.shortcut_bg);
init_pair(CP_LABEL, globalopts.colours.label_fg,
globalopts.colours.label_bg);
init_pair(CP_NUMBER, globalopts.colours.number_fg,
globalopts.colours.number_bg);
init_pair(CP_FILE_MP3, globalopts.colours.file_mp3_fg,
globalopts.colours.default_bg);
init_pair(CP_FILE_DIR, globalopts.colours.file_dir_fg,
globalopts.colours.default_bg);
init_pair(CP_FILE_LST, globalopts.colours.file_lst_fg,
globalopts.colours.default_bg);
init_pair(CP_FILE_WIN, globalopts.colours.file_win_fg,
globalopts.colours.default_bg);
/* initialize selection window */
mp3_rootwin = newgroup();
mp3_rootwin->setTitle("[Default]");
mp3_rootwin->swRefresh(0);
mp3_rootwin->drawBorder(); //to get the title
mp3_curwin = mp3_rootwin;
progmode = PM_NORMAL;
/* initialize helpwin */
init_helpwin();
repaint_help();
draw_static();
mw_settxt("Press '?' to get help on available commands");
refresh();
if (options & OPT_NOMIXER)
{
globalopts.no_mixer = 1;
}
if (options & OPT_SOUNDDEV)
{
set_sound_device(tmp.sound_device);
free(tmp.sound_device);
}
if (options & OPT_MIXERDEV)
{
set_mixer_device(tmp.mixer_device);
free(tmp.mixer_device);
}
if (options & OPT_REPEAT)
{
globalopts.repeat = 1;
}
if (options & OPT_PLAYMODE)
{
if (!set_play_mode(tmp.play_mode))
{
endwin();
usage();
}
delete[] tmp.play_mode;
}
if (options & OPT_FPL)
{
if (!set_fpl(tmp.fpl))
{
endwin();
usage();
}
}
if (options & OPT_THREADS)
{
#ifdef PTHREADEDMPEG
if (!set_threads(tmp.threads))
#else
if (1) //threading is not enabled..
#endif
{
endwin();
usage();
}
}
if (options & OPT_CHROOT) {
if (chroot(chroot_dir) < 0) {
endwin();
perror("chroot");
fprintf(stderr, "Could not chroot to %s! (are you root?)\n",
chroot_dir);
exit(1);
}
if (-1 == chdir("/")) {
fprintf(stderr, "Failed to change to root directory\n");
exit(1);
}
delete[] chroot_dir;
}
if (options & OPT_DOWNSAMPLE)
globalopts.downsample = 1;
if (options & OPT_8BITS)
globalopts.eightbits = 1;
/* create nmixer interface */
init_mixer();
/* All options from configfile and commandline have been read. */
draw_settings();
/* Accept signal for redrawing the interface */
signal(SIGWINCH, handle_resize);
/*\
|*| very rude hack caused by nasplayer lib that outputs rubbish to stderr
\*/
//fclose(stderr);
/*\
|*| EORH
\*/
action = AC_NONE;
if (options & OPT_PLAYMP3)
{
const char *bla[4];
short idx;
short foo[2] = { 0, 1 };
for (int i = 0; i < (argc - optind); i++)
{
bla[0] = (const char*)playmp3s[i];
if (is_httpstream(bla[0]))
bla[1] = bla[0];
else
bla[1] = chop_path(playmp3s[i]);
bla[2] = NULL;
bla[3] = NULL;
idx=mp3_curwin->addItem(bla, foo, CP_FILE_MP3);
if (idx!=-1)
mp3_curwin->changeItem(idx,&id3_filename,strdup(bla[0]),2);
delete[] playmp3s[i];
}
delete[] playmp3s; playmp3s_nr = 0;
mp3_curwin->swRefresh(0);
set_action(AC_PLAY);
if (!(options & OPT_QUIT))
quit_after_playlist = 1;
}
else if ((options & OPT_LOADLIST) || (options & OPT_AUTOLIST))
{
read_playlist(init_playlist);
mp3_curwin->swRefresh(1);
delete[] init_playlist;
if (options & OPT_AUTOLIST)
set_action(AC_PLAY);
}
#ifdef PTHREADEDMPEG
pthread_create(&thread_playlist, NULL, play_list, NULL);
# ifdef INCLUDE_LIRC
pthread_create(&thread_lirc, NULL, lirc_loop, NULL);
# endif
#elif defined(LIBPTH)
warning("Setting up PTH thread");
pth_mutex_init(&playing_mutex);
pth_mutex_init(&ncurses_mutex);
pth_mutex_init(&onemp3_mutex);
pth_cond_init(&playing_cond);
thread_playlist = pth_spawn(PTH_ATTR_DEFAULT, play_list, NULL);
pth_yield(NULL);
#endif /* PTHREADEDMPEG */
/* read input */
while ( (key = handle_input(1)) >= 0)
{
}
/* signal running threads to terminate themselves */
playopts.quit_program = 1;
wake_player();
#ifdef PTHREADEDMPEG
pthread_join(thread_playlist, NULL);
//the lirc thread blocks..sod it.
#elif defined (LIBPTH)
pth_join(thread_playlist, NULL);
#endif
endwin();
return 0;
}
|